FormatTest.cpp revision a4d4621b206f941cc58d9d0bc7c67a8e705c9d49
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 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 861 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 862 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 863 // FIXME: We never break before the macro name. 864 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); 865 866 verifyFormat("#define A A\n#define A A"); 867 verifyFormat("#define A(X) A\n#define A A"); 868 869 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 870 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 871} 872 873TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 874 EXPECT_EQ("// some comment\n" 875 "#include \"a.h\"\n" 876 "#define A( \\\n" 877 " A, B)\n" 878 "#include \"b.h\"\n" 879 "// some comment\n", 880 format(" // some comment\n" 881 " #include \"a.h\"\n" 882 "#define A(A,\\\n" 883 " B)\n" 884 " #include \"b.h\"\n" 885 " // some comment\n", 886 getLLVMStyleWithColumns(13))); 887} 888 889TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 890 891TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 892 EXPECT_EQ("#define A \\\n" 893 " c; \\\n" 894 " e;\n" 895 "f;", 896 format("#define A c; e;\n" 897 "f;", 898 getLLVMStyleWithColumns(14))); 899} 900 901TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 902 903TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { 904 EXPECT_EQ("# define A\\\n b;", 905 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); 906} 907 908TEST_F(FormatTest, MacroDefinitionInsideStatement) { 909 EXPECT_EQ("int x,\n" 910 "#define A\n" 911 " y;", 912 format("int x,\n#define A\ny;")); 913} 914 915TEST_F(FormatTest, HashInMacroDefinition) { 916 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 917 verifyFormat("#define A \\\n" 918 " { \\\n" 919 " f(#c);\\\n" 920 " }", 921 getLLVMStyleWithColumns(11)); 922 923 verifyFormat("#define A(X) \\\n" 924 " void function##X()", 925 getLLVMStyleWithColumns(22)); 926 927 verifyFormat("#define A(a, b, c) \\\n" 928 " void a##b##c()", 929 getLLVMStyleWithColumns(22)); 930 931 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 932} 933 934TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 935 verifyFormat("#define A (1)"); 936} 937 938TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 939 EXPECT_EQ("#define A b;", format("#define A \\\n" 940 " \\\n" 941 " b;", 942 getLLVMStyleWithColumns(25))); 943 EXPECT_EQ("#define A \\\n" 944 " \\\n" 945 " a; \\\n" 946 " b;", 947 format("#define A \\\n" 948 " \\\n" 949 " a; \\\n" 950 " b;", 951 getLLVMStyleWithColumns(11))); 952 EXPECT_EQ("#define A \\\n" 953 " a; \\\n" 954 " \\\n" 955 " b;", 956 format("#define A \\\n" 957 " a; \\\n" 958 " \\\n" 959 " b;", 960 getLLVMStyleWithColumns(11))); 961} 962 963TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 964 // FIXME: Improve formatting of case labels in macros. 965 verifyFormat("#define SOMECASES \\\n" 966 "case 1: \\\n" 967 " case 2\n", 968 getLLVMStyleWithColumns(20)); 969 970 verifyFormat("#define A template <typename T>"); 971 verifyFormat("#define STR(x) #x\n" 972 "f(STR(this_is_a_string_literal{));"); 973} 974 975TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 976 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 977} 978 979TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 980 verifyFormat("{\n { a #c; }\n}"); 981} 982 983TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 984 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 985 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 986 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 987 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 988} 989 990TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { 991 EXPECT_EQ( 992 "#define A \\\n int i; \\\n int j;", 993 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 994} 995 996TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 997 verifyFormat("#define A \\\n" 998 " int v( \\\n" 999 " a); \\\n" 1000 " int i;", 1001 getLLVMStyleWithColumns(11)); 1002} 1003 1004TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 1005 EXPECT_EQ( 1006 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1007 " \\\n" 1008 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 1009 "\n" 1010 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 1011 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 1012 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1013 "\\\n" 1014 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 1015 " \n" 1016 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 1017 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 1018} 1019 1020TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 1021 EXPECT_EQ("int\n" 1022 "#define A\n" 1023 " a;", 1024 format("int\n#define A\na;")); 1025 verifyFormat("functionCallTo(\n" 1026 " someOtherFunction(\n" 1027 " withSomeParameters, whichInSequence,\n" 1028 " areLongerThanALine(andAnotherCall,\n" 1029 "#define A B\n" 1030 " withMoreParamters,\n" 1031 " whichStronglyInfluenceTheLayout),\n" 1032 " andMoreParameters),\n" 1033 " trailing);", 1034 getLLVMStyleWithColumns(69)); 1035} 1036 1037TEST_F(FormatTest, LayoutBlockInsideParens) { 1038 EXPECT_EQ("functionCall({\n" 1039 " int i;\n" 1040 "});", 1041 format(" functionCall ( {int i;} );")); 1042} 1043 1044TEST_F(FormatTest, LayoutBlockInsideStatement) { 1045 EXPECT_EQ("SOME_MACRO { int i; }\n" 1046 "int i;", 1047 format(" SOME_MACRO {int i;} int i;")); 1048} 1049 1050TEST_F(FormatTest, LayoutNestedBlocks) { 1051 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 1052 " struct s {\n" 1053 " int i;\n" 1054 " };\n" 1055 " s kBitsToOs[] = { { 10 } };\n" 1056 " for (int i = 0; i < 10; ++i)\n" 1057 " return;\n" 1058 "}"); 1059} 1060 1061TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 1062 EXPECT_EQ("{}", format("{}")); 1063 1064 // Negative test for enum. 1065 verifyFormat("enum E {\n};"); 1066 1067 // Note that when there's a missing ';', we still join... 1068 verifyFormat("enum E {}"); 1069} 1070 1071//===----------------------------------------------------------------------===// 1072// Line break tests. 1073//===----------------------------------------------------------------------===// 1074 1075TEST_F(FormatTest, FormatsFunctionDefinition) { 1076 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," 1077 " int h, int j, int f,\n" 1078 " int c, int ddddddddddddd) {}"); 1079} 1080 1081TEST_F(FormatTest, FormatsAwesomeMethodCall) { 1082 verifyFormat( 1083 "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 1084 " parameter, parameter, parameter)),\n" 1085 " SecondLongCall(parameter));"); 1086} 1087 1088TEST_F(FormatTest, PreventConfusingIndents) { 1089 verifyFormat( 1090 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1091 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 1092 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1093 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 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("int a = bbbb && ccc && fffff(\n" 1105 "#define A Just forcing a new line\n" 1106 " ddd);"); 1107} 1108 1109TEST_F(FormatTest, ConstructorInitializers) { 1110 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 1111 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 1112 getLLVMStyleWithColumns(45)); 1113 verifyFormat("Constructor()\n" 1114 " : Inttializer(FitsOnTheLine) {}", 1115 getLLVMStyleWithColumns(44)); 1116 verifyFormat("Constructor()\n" 1117 " : Inttializer(FitsOnTheLine) {}", 1118 getLLVMStyleWithColumns(43)); 1119 1120 verifyFormat( 1121 "SomeClass::Constructor()\n" 1122 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 1123 1124 verifyFormat( 1125 "SomeClass::Constructor()\n" 1126 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1127 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 1128 verifyFormat( 1129 "SomeClass::Constructor()\n" 1130 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1131 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 1132 1133 verifyFormat("Constructor()\n" 1134 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1135 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1136 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1137 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 1138 1139 verifyFormat("Constructor()\n" 1140 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 1142 1143 verifyFormat("Constructor(int Parameter = 0)\n" 1144 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 1145 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 1146 1147 // Here a line could be saved by splitting the second initializer onto two 1148 // lines, but that is not desireable. 1149 verifyFormat("Constructor()\n" 1150 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 1151 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 1152 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 1153 1154 FormatStyle OnePerLine = getLLVMStyle(); 1155 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 1156 verifyFormat("SomeClass::Constructor()\n" 1157 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1158 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1159 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 1160 OnePerLine); 1161 verifyFormat("SomeClass::Constructor()\n" 1162 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 1163 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 1164 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 1165 OnePerLine); 1166 verifyFormat("MyClass::MyClass(int var)\n" 1167 " : some_var_(var), // 4 space indent\n" 1168 " some_other_var_(var + 1) { // lined up\n" 1169 "}", 1170 OnePerLine); 1171 verifyFormat("Constructor()\n" 1172 " : aaaaa(aaaaaa),\n" 1173 " aaaaa(aaaaaa),\n" 1174 " aaaaa(aaaaaa),\n" 1175 " aaaaa(aaaaaa),\n" 1176 " aaaaa(aaaaaa) {}", 1177 OnePerLine); 1178 1179 // This test takes VERY long when memoization is broken. 1180 OnePerLine.BinPackParameters = false; 1181 std::string input = "Constructor()\n" 1182 " : aaaa(a,\n"; 1183 for (unsigned i = 0, e = 80; i != e; ++i) { 1184 input += " a,\n"; 1185 } 1186 input += " a) {}"; 1187 verifyFormat(input, OnePerLine); 1188} 1189 1190TEST_F(FormatTest, BreaksAsHighAsPossible) { 1191 verifyFormat( 1192 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 1193 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 1194 " f();"); 1195 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 1196 " Intervals[i - 1].getRange().getLast()) {\n}"); 1197} 1198 1199TEST_F(FormatTest, BreaksDesireably) { 1200 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 1201 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 1202 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 1203 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1205 "}"); 1206 1207 verifyFormat( 1208 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 1210 1211 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1214 1215 verifyFormat( 1216 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1217 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 1218 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 1220 1221 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1222 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1223 1224 verifyFormat( 1225 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1227 verifyFormat( 1228 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1229 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1230 verifyFormat( 1231 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1233 1234 // This test case breaks on an incorrect memoization, i.e. an optimization not 1235 // taking into account the StopAt value. 1236 verifyFormat( 1237 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1238 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1239 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1240 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1241 1242 verifyFormat("{\n {\n {\n" 1243 " Annotation.SpaceRequiredBefore =\n" 1244 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 1245 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 1246 " }\n }\n}"); 1247} 1248 1249TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 1250 FormatStyle NoBinPacking = getLLVMStyle(); 1251 NoBinPacking.BinPackParameters = false; 1252 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 1253 " aaaaaaaaaaaaaaaaaaaa,\n" 1254 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 1255 NoBinPacking); 1256 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 1257 " aaaaaaaaaaaaa,\n" 1258 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 1259 NoBinPacking); 1260 verifyFormat( 1261 "aaaaaaaa(aaaaaaaaaaaaa,\n" 1262 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1263 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 1264 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1265 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 1266 NoBinPacking); 1267 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 1268 " .aaaaaaaaaaaaaaaaaa();", 1269 NoBinPacking); 1270 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1271 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);", 1272 NoBinPacking); 1273 1274 verifyFormat( 1275 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1276 " aaaaaaaaaaaa,\n" 1277 " aaaaaaaaaaaa);", 1278 NoBinPacking); 1279 verifyFormat( 1280 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 1281 " ddddddddddddddddddddddddddddd),\n" 1282 " test);", 1283 NoBinPacking); 1284 1285 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 1286 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 1287 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;", 1288 NoBinPacking); 1289 verifyFormat("a(\"a\"\n" 1290 " \"a\",\n" 1291 " a);"); 1292 1293 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 1294 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 1295 " aaaaaaaaa,\n" 1296 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 1297 NoBinPacking); 1298 verifyFormat( 1299 "void f() {\n" 1300 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 1301 " .aaaaaaa();\n" 1302 "}", 1303 NoBinPacking); 1304} 1305 1306TEST_F(FormatTest, FormatsBuilderPattern) { 1307 verifyFormat( 1308 "return llvm::StringSwitch<Reference::Kind>(name)\n" 1309 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 1310 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" 1311 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" 1312 " .Default(ORDER_TEXT);\n"); 1313 1314 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 1315 " aaaaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 1316 verifyFormat( 1317 "aaaaaaa->aaaaaaa\n" 1318 " ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1319 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 1320 verifyFormat( 1321 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 1322 " aaaaaaaaaaaaaa);"); 1323 verifyFormat( 1324 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n" 1325 " ->aaaaaaaaaaaaaaaa(\n" 1326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1327 " ->aaaaaaaaaaaaaaaaa();"); 1328} 1329 1330TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { 1331 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1332 " GUARDED_BY(aaaaaaaaaaaaa);"); 1333 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 1334 " GUARDED_BY(aaaaaaaaaaaaa);"); 1335 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 1336 " GUARDED_BY(aaaaaaaaaaaaa) {}"); 1337} 1338 1339TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 1340 verifyFormat( 1341 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1342 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 1343 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 1344 " ccccccccccccccccccccccccc) {\n}"); 1345 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 1346 " ccccccccccccccccccccccccc) {\n}"); 1347 verifyFormat( 1348 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 1349 " ccccccccccccccccccccccccc) {\n}"); 1350 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 1351 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 1352 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 1353 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 1354 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 1355 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 1356 " aaaaaaaaaaaaaaa != aa) {\n}"); 1357} 1358 1359TEST_F(FormatTest, BreaksAfterAssignments) { 1360 verifyFormat( 1361 "unsigned Cost =\n" 1362 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 1363 " SI->getPointerAddressSpaceee());\n"); 1364 verifyFormat( 1365 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 1366 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 1367 1368 verifyFormat( 1369 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" 1370 " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 1371} 1372 1373TEST_F(FormatTest, AlignsAfterAssignments) { 1374 verifyFormat( 1375 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1376 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1377 verifyFormat( 1378 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1379 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1380 verifyFormat( 1381 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1382 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1383 verifyFormat( 1384 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1385 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 1386 verifyFormat("double LooooooooooooooooooooooooongResult =\n" 1387 " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" 1388 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 1389} 1390 1391TEST_F(FormatTest, AlignsAfterReturn) { 1392 verifyFormat( 1393 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1394 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1395 verifyFormat( 1396 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1397 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 1398} 1399 1400TEST_F(FormatTest, BreaksConditionalExpressions) { 1401 verifyFormat( 1402 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 1403 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1404 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1405 verifyFormat( 1406 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1407 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1408 verifyFormat( 1409 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 1410 " : aaaaaaaaaaaaa);"); 1411 verifyFormat( 1412 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1413 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaa\n" 1414 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1415 " aaaaaaaaaaaaa);"); 1416 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1417 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1419 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1421 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1423 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1425 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1426 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1427 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1428 1429 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1430 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1431 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1432 verifyFormat( 1433 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1434 " ? aaaaaaaaaaaaaaa\n" 1435 " : aaaaaaaaaaaaaaa;"); 1436 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 1437 " aaaaaaaaa\n" 1438 " ? b\n" 1439 " : c);"); 1440 verifyFormat( 1441 "unsigned Indent =\n" 1442 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 1443 " ? IndentForLevel[TheLine.Level]\n" 1444 " : TheLine * 2,\n" 1445 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 1446 getLLVMStyleWithColumns(70)); 1447 1448 FormatStyle NoBinPacking = getLLVMStyle(); 1449 NoBinPacking.BinPackParameters = false; 1450 verifyFormat( 1451 "void f() {\n" 1452 " g(aaa,\n" 1453 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 1454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1455 " ? aaaaaaaaaaaaaaa\n" 1456 " : aaaaaaaaaaaaaaa);\n" 1457 "}", 1458 NoBinPacking); 1459} 1460 1461TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 1462 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 1463 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 1464 verifyFormat("bool a = true, b = false;"); 1465 1466 // FIXME: Indentation looks weird. 1467 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 1469 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 1470 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 1471 verifyFormat( 1472 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 1473 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 1474 " d = e && f;"); 1475 1476} 1477 1478TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 1479 verifyFormat("arr[foo ? bar : baz];"); 1480 verifyFormat("f()[foo ? bar : baz];"); 1481 verifyFormat("(a + b)[foo ? bar : baz];"); 1482 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 1483} 1484 1485TEST_F(FormatTest, AlignsStringLiterals) { 1486 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 1487 " \"short literal\");"); 1488 verifyFormat( 1489 "looooooooooooooooooooooooongFunction(\n" 1490 " \"short literal\"\n" 1491 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 1492 verifyFormat("someFunction(\"Always break between multi-line\"\n" 1493 " \" string literals\",\n" 1494 " and, other, parameters);"); 1495 EXPECT_EQ("fun + \"1243\" /* comment */\n" 1496 " \"5678\";", 1497 format("fun + \"1243\" /* comment */\n" 1498 " \"5678\";", 1499 getLLVMStyleWithColumns(28))); 1500 EXPECT_EQ( 1501 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 1502 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 1503 " \"aaaaaaaaaaaaaaaa\";", 1504 format("aaaaaa =" 1505 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 1506 "aaaaaaaaaaaaaaaaaaaaa\" " 1507 "\"aaaaaaaaaaaaaaaa\";")); 1508 verifyFormat("a = a + \"a\"\n" 1509 " \"a\"\n" 1510 " \"a\";"); 1511 1512 verifyFormat( 1513 "#define LL_FORMAT \"ll\"\n" 1514 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 1515 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 1516} 1517 1518TEST_F(FormatTest, AlignsPipes) { 1519 verifyFormat( 1520 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1521 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1522 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1523 verifyFormat( 1524 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 1525 " << aaaaaaaaaaaaaaaaaaaa;"); 1526 verifyFormat( 1527 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1528 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1529 verifyFormat( 1530 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1531 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 1532 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 1533 verifyFormat( 1534 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1535 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1536 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1537 1538 verifyFormat("return out << \"somepacket = {\\n\"\n" 1539 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 1540 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 1541 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 1542 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 1543 " << \"}\";"); 1544 1545 verifyFormat( 1546 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 1547 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 1548 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 1549 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 1550 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 1551} 1552 1553TEST_F(FormatTest, UnderstandsEquals) { 1554 verifyFormat( 1555 "aaaaaaaaaaaaaaaaa =\n" 1556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1557 verifyFormat( 1558 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1559 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1560 verifyFormat( 1561 "if (a) {\n" 1562 " f();\n" 1563 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1565 "}"); 1566 1567 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1568 " 100000000 + 10000000) {\n}"); 1569} 1570 1571TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 1572 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 1573 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 1574 1575 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 1576 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 1577 1578 verifyFormat( 1579 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 1580 " Parameter2);"); 1581 1582 verifyFormat( 1583 "ShortObject->shortFunction(\n" 1584 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 1585 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 1586 1587 verifyFormat("loooooooooooooongFunction(\n" 1588 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 1589 1590 verifyFormat( 1591 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 1592 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 1593 1594 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 1595 " .WillRepeatedly(Return(SomeValue));"); 1596 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" 1597 " .insert(ccccccccccccccccccccccc);"); 1598 1599 // Here, it is not necessary to wrap at "." or "->". 1600 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 1601 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1602 verifyFormat( 1603 "aaaaaaaaaaa->aaaaaaaaa(\n" 1604 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1605 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 1606 1607 verifyFormat( 1608 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1609 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 1610 1611 FormatStyle NoBinPacking = getLLVMStyle(); 1612 NoBinPacking.BinPackParameters = false; 1613 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 1614 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 1615 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 1616 " aaaaaaaaaaaaaaaaaaa,\n" 1617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 1618 NoBinPacking); 1619} 1620 1621TEST_F(FormatTest, WrapsTemplateDeclarations) { 1622 verifyFormat("template <typename T>\n" 1623 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 1624 verifyFormat( 1625 "template <typename T>\n" 1626 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 1627 verifyFormat("template <typename T>\n" 1628 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 1629 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 1630 verifyFormat( 1631 "template <typename T>\n" 1632 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 1633 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 1634 verifyFormat( 1635 "template <typename T>\n" 1636 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 1637 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 1638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1639 verifyFormat("template <typename T>\n" 1640 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1641 " int aaaaaaaaaaaaaaaaa);"); 1642 verifyFormat( 1643 "template <typename T1, typename T2 = char, typename T3 = char,\n" 1644 " typename T4 = char>\n" 1645 "void f();"); 1646 verifyFormat( 1647 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 1648 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1649 1650 verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 1651 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); 1652} 1653 1654TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 1655 verifyFormat( 1656 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 1658 verifyFormat( 1659 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1661 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 1662 1663 // FIXME: Should we have an extra indent after the second break? 1664 verifyFormat( 1665 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1666 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1667 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 1668 1669 // FIXME: Look into whether we should indent 4 from the start or 4 from 1670 // "bbbbb..." here instead of what we are doing now. 1671 verifyFormat( 1672 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 1673 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 1674 1675 // Breaking at nested name specifiers is generally not desirable. 1676 verifyFormat( 1677 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1678 " aaaaaaaaaaaaaaaaaaaaaaa);"); 1679 1680 verifyFormat( 1681 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1683 " aaaaaaaaaaaaaaaaaaaaa);", 1684 getLLVMStyleWithColumns(74)); 1685} 1686 1687TEST_F(FormatTest, UnderstandsTemplateParameters) { 1688 verifyFormat("A<int> a;"); 1689 verifyFormat("A<A<A<int> > > a;"); 1690 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 1691 verifyFormat("bool x = a < 1 || 2 > a;"); 1692 verifyFormat("bool x = 5 < f<int>();"); 1693 verifyFormat("bool x = f<int>() > 5;"); 1694 verifyFormat("bool x = 5 < a<int>::x;"); 1695 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 1696 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 1697 1698 verifyGoogleFormat("A<A<int>> a;"); 1699 verifyGoogleFormat("A<A<A<int>>> a;"); 1700 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 1701 verifyGoogleFormat("A<A<int> > a;"); 1702 verifyGoogleFormat("A<A<A<int> > > a;"); 1703 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 1704 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 1705 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 1706 1707 verifyFormat("test >> a >> b;"); 1708 verifyFormat("test << a >> b;"); 1709 1710 verifyFormat("f<int>();"); 1711 verifyFormat("template <typename T> void f() {}"); 1712} 1713 1714TEST_F(FormatTest, UnderstandsBinaryOperators) { 1715 verifyFormat("COMPARE(a, ==, b);"); 1716} 1717 1718TEST_F(FormatTest, UnderstandsUnaryOperators) { 1719 verifyFormat("int a = -2;"); 1720 verifyFormat("f(-1, -2, -3);"); 1721 verifyFormat("a[-1] = 5;"); 1722 verifyFormat("int a = 5 + -2;"); 1723 verifyFormat("if (i == -1) {\n}"); 1724 verifyFormat("if (i != -1) {\n}"); 1725 verifyFormat("if (i > -1) {\n}"); 1726 verifyFormat("if (i < -1) {\n}"); 1727 verifyFormat("++(a->f());"); 1728 verifyFormat("--(a->f());"); 1729 verifyFormat("(a->f())++;"); 1730 verifyFormat("a[42]++;"); 1731 verifyFormat("if (!(a->f())) {\n}"); 1732 1733 verifyFormat("a-- > b;"); 1734 verifyFormat("b ? -a : c;"); 1735 verifyFormat("n * sizeof char16;"); 1736 verifyFormat("n * alignof char16;"); 1737 verifyFormat("sizeof(char);"); 1738 verifyFormat("alignof(char);"); 1739 1740 verifyFormat("return -1;"); 1741 verifyFormat("switch (a) {\n" 1742 "case -1:\n" 1743 " break;\n" 1744 "}"); 1745 1746 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); 1747 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); 1748 1749 verifyFormat("int a = /* confusing comment */ -1;"); 1750 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 1751 verifyFormat("int a = i /* confusing comment */++;"); 1752} 1753 1754TEST_F(FormatTest, UndestandsOverloadedOperators) { 1755 verifyFormat("bool operator<();"); 1756 verifyFormat("bool operator>();"); 1757 verifyFormat("bool operator=();"); 1758 verifyFormat("bool operator==();"); 1759 verifyFormat("bool operator!=();"); 1760 verifyFormat("int operator+();"); 1761 verifyFormat("int operator++();"); 1762 verifyFormat("bool operator();"); 1763 verifyFormat("bool operator()();"); 1764 verifyFormat("bool operator[]();"); 1765 verifyFormat("operator bool();"); 1766 verifyFormat("operator int();"); 1767 verifyFormat("operator void *();"); 1768 verifyFormat("operator SomeType<int>();"); 1769 verifyFormat("operator SomeType<int, int>();"); 1770 verifyFormat("operator SomeType<SomeType<int> >();"); 1771 verifyFormat("void *operator new(std::size_t size);"); 1772 verifyFormat("void *operator new[](std::size_t size);"); 1773 verifyFormat("void operator delete(void *ptr);"); 1774 verifyFormat("void operator delete[](void *ptr);"); 1775 1776 verifyFormat( 1777 "ostream &operator<<(ostream &OutputStream,\n" 1778 " SomeReallyLongType WithSomeReallyLongValue);"); 1779 1780 verifyGoogleFormat("operator void*();"); 1781 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 1782} 1783 1784TEST_F(FormatTest, UnderstandsNewAndDelete) { 1785 verifyFormat("void f() {\n" 1786 " A *a = new A;\n" 1787 " A *a = new (placement) A;\n" 1788 " delete a;\n" 1789 " delete (A *)a;\n" 1790 "}"); 1791} 1792 1793TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 1794 verifyFormat("int *f(int *a) {}"); 1795 verifyFormat("int main(int argc, char **argv) {}"); 1796 verifyFormat("Test::Test(int b) : a(b * b) {}"); 1797 verifyIndependentOfContext("f(a, *a);"); 1798 verifyIndependentOfContext("f(*a);"); 1799 verifyIndependentOfContext("int a = b * 10;"); 1800 verifyIndependentOfContext("int a = 10 * b;"); 1801 verifyIndependentOfContext("int a = b * c;"); 1802 verifyIndependentOfContext("int a += b * c;"); 1803 verifyIndependentOfContext("int a -= b * c;"); 1804 verifyIndependentOfContext("int a *= b * c;"); 1805 verifyIndependentOfContext("int a /= b * c;"); 1806 verifyIndependentOfContext("int a = *b;"); 1807 verifyIndependentOfContext("int a = *b * c;"); 1808 verifyIndependentOfContext("int a = b * *c;"); 1809 verifyIndependentOfContext("return 10 * b;"); 1810 verifyIndependentOfContext("return *b * *c;"); 1811 verifyIndependentOfContext("return a & ~b;"); 1812 verifyIndependentOfContext("f(b ? *c : *d);"); 1813 verifyIndependentOfContext("int a = b ? *c : *d;"); 1814 verifyIndependentOfContext("*b = a;"); 1815 verifyIndependentOfContext("a * ~b;"); 1816 verifyIndependentOfContext("a * !b;"); 1817 verifyIndependentOfContext("a * +b;"); 1818 verifyIndependentOfContext("a * -b;"); 1819 verifyIndependentOfContext("a * ++b;"); 1820 verifyIndependentOfContext("a * --b;"); 1821 verifyIndependentOfContext("a[4] * b;"); 1822 verifyIndependentOfContext("a[a * a] = 1;"); 1823 verifyIndependentOfContext("f() * b;"); 1824 verifyIndependentOfContext("a * [self dostuff];"); 1825 verifyIndependentOfContext("a * (a + b);"); 1826 verifyIndependentOfContext("(a *)(a + b);"); 1827 verifyIndependentOfContext("int *pa = (int *)&a;"); 1828 verifyIndependentOfContext("return sizeof(int **);"); 1829 verifyIndependentOfContext("return sizeof(int ******);"); 1830 verifyIndependentOfContext("return (int **&)a;"); 1831 verifyGoogleFormat("return sizeof(int**);"); 1832 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 1833 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 1834 // FIXME: The newline is wrong. 1835 verifyFormat("auto a = [](int **&, int ***) {}\n;"); 1836 1837 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 1838 1839 verifyIndependentOfContext("A<int *> a;"); 1840 verifyIndependentOfContext("A<int **> a;"); 1841 verifyIndependentOfContext("A<int *, int *> a;"); 1842 verifyIndependentOfContext( 1843 "const char *const p = reinterpret_cast<const char *const>(q);"); 1844 verifyIndependentOfContext("A<int **, int **> a;"); 1845 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 1846 1847 verifyFormat( 1848 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1850 1851 verifyGoogleFormat("int main(int argc, char** argv) {}"); 1852 verifyGoogleFormat("A<int*> a;"); 1853 verifyGoogleFormat("A<int**> a;"); 1854 verifyGoogleFormat("A<int*, int*> a;"); 1855 verifyGoogleFormat("A<int**, int**> a;"); 1856 verifyGoogleFormat("f(b ? *c : *d);"); 1857 verifyGoogleFormat("int a = b ? *c : *d;"); 1858 verifyGoogleFormat("Type* t = **x;"); 1859 verifyGoogleFormat("Type* t = *++*x;"); 1860 verifyGoogleFormat("*++*x;"); 1861 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 1862 verifyGoogleFormat("Type* t = x++ * y;"); 1863 verifyGoogleFormat( 1864 "const char* const p = reinterpret_cast<const char* const>(q);"); 1865 1866 verifyIndependentOfContext("a = *(x + y);"); 1867 verifyIndependentOfContext("a = &(x + y);"); 1868 verifyIndependentOfContext("*(x + y).call();"); 1869 verifyIndependentOfContext("&(x + y)->call();"); 1870 verifyIndependentOfContext("&(*I).first"); 1871 1872 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 1873 verifyFormat( 1874 "int *MyValues = {\n" 1875 " *A, // Operator detection might be confused by the '{'\n" 1876 " *BB // Operator detection might be confused by previous comment\n" 1877 "};"); 1878 1879 verifyIndependentOfContext("if (int *a = &b)"); 1880 verifyIndependentOfContext("if (int &a = *b)"); 1881 verifyIndependentOfContext("if (a & b[i])"); 1882 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 1883 verifyIndependentOfContext("if (*b[i])"); 1884 verifyIndependentOfContext("if (int *a = (&b))"); 1885 verifyIndependentOfContext("while (int *a = &b)"); 1886 verifyFormat("void f() {\n" 1887 " for (const int &v : Values) {\n" 1888 " }\n" 1889 "}"); 1890 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 1891 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 1892 1893 verifyIndependentOfContext("A = new SomeType *[Length]();"); 1894 verifyGoogleFormat("A = new SomeType* [Length]();"); 1895 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 EXPECT_EQ("int *a;\n" 1911 "int *a;\n" 1912 "int *a;", 1913 format("int *a;\n" 1914 "int * a;\n" 1915 "int * a;", 1916 getGoogleStyle())); 1917} 1918 1919TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 1920 verifyFormat("void f() {\n" 1921 " x[aaaaaaaaa -\n" 1922 " b] = 23;\n" 1923 "}", 1924 getLLVMStyleWithColumns(15)); 1925} 1926 1927TEST_F(FormatTest, FormatsCasts) { 1928 verifyFormat("Type *A = static_cast<Type *>(P);"); 1929 verifyFormat("Type *A = (Type *)P;"); 1930 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 1931 verifyFormat("int a = (int)(2.0f);"); 1932 1933 // FIXME: These also need to be identified. 1934 verifyFormat("int a = (int) 2.0f;"); 1935 verifyFormat("int a = (int) * b;"); 1936 1937 // These are not casts. 1938 verifyFormat("void f(int *) {}"); 1939 verifyFormat("f(foo)->b;"); 1940 verifyFormat("f(foo).b;"); 1941 verifyFormat("f(foo)(b);"); 1942 verifyFormat("f(foo)[b];"); 1943 verifyFormat("[](foo) { return 4; }(bar)];"); 1944 verifyFormat("(*funptr)(foo)[4];"); 1945 verifyFormat("funptrs[4](foo)[4];"); 1946 verifyFormat("void f(int *);"); 1947 verifyFormat("void f(int *) = 0;"); 1948 verifyFormat("void f(SmallVector<int>) {}"); 1949 verifyFormat("void f(SmallVector<int>);"); 1950 verifyFormat("void f(SmallVector<int>) = 0;"); 1951 verifyFormat("void f(int i = (kValue) * kMask) {}"); 1952 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 1953 verifyFormat("int a = sizeof(int) * b;"); 1954 verifyFormat("int a = alignof(int) * b;"); 1955 1956 // These are not casts, but at some point were confused with casts. 1957 verifyFormat("virtual void foo(int *) override;"); 1958 verifyFormat("virtual void foo(char &) const;"); 1959 verifyFormat("virtual void foo(int *a, char *) const;"); 1960} 1961 1962TEST_F(FormatTest, FormatsFunctionTypes) { 1963 // FIXME: Determine the cases that need a space after the return type and fix. 1964 verifyFormat("A<bool()> a;"); 1965 verifyFormat("A<SomeType()> a;"); 1966 verifyFormat("A<void(*)(int, std::string)> a;"); 1967 1968 verifyFormat("int(*func)(void *);"); 1969} 1970 1971TEST_F(FormatTest, BreaksLongDeclarations) { 1972 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 1973 " int LoooooooooooooooooooongParam2) {}"); 1974 verifyFormat( 1975 "TypeSpecDecl *\n" 1976 "TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n" 1977 " IdentifierIn *II, Type *T) {}"); 1978 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 1979 "ReallyReallyLongFunctionName(\n" 1980 " const std::string &SomeParameter,\n" 1981 " const SomeType<string, SomeOtherTemplateParameter> &\n" 1982 " ReallyReallyLongParameterName,\n" 1983 " const SomeType<string, SomeOtherTemplateParameter> &\n" 1984 " AnotherLongParameterName) {}"); 1985 verifyFormat( 1986 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 1987 "aaaaaaaaaaaaaaaaaaaaaaa;"); 1988 1989 verifyGoogleFormat( 1990 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 1991 " SourceLocation L) {}"); 1992 verifyGoogleFormat( 1993 "some_namespace::LongReturnType\n" 1994 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 1995 " int first_long_parameter, int second_parameter) {}"); 1996 1997 verifyGoogleFormat("template <typename T>\n" 1998 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 1999 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 2000 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2001 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 2002} 2003 2004TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 2005 verifyFormat("(a)->b();"); 2006 verifyFormat("--a;"); 2007} 2008 2009TEST_F(FormatTest, HandlesIncludeDirectives) { 2010 verifyFormat("#include <string>\n" 2011 "#include <a/b/c.h>\n" 2012 "#include \"a/b/string\"\n" 2013 "#include \"string.h\"\n" 2014 "#include \"string.h\"\n" 2015 "#include <a-a>\n" 2016 "#include < path with space >\n" 2017 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 2018 getLLVMStyleWithColumns(35)); 2019 2020 verifyFormat("#import <string>"); 2021 verifyFormat("#import <a/b/c.h>"); 2022 verifyFormat("#import \"a/b/string\""); 2023 verifyFormat("#import \"string.h\""); 2024 verifyFormat("#import \"string.h\""); 2025} 2026 2027//===----------------------------------------------------------------------===// 2028// Error recovery tests. 2029//===----------------------------------------------------------------------===// 2030 2031TEST_F(FormatTest, IncompleteParameterLists) { 2032 FormatStyle NoBinPacking = getLLVMStyle(); 2033 NoBinPacking.BinPackParameters = false; 2034 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 2035 " double *min_x,\n" 2036 " double *max_x,\n" 2037 " double *min_y,\n" 2038 " double *max_y,\n" 2039 " double *min_z,\n" 2040 " double *max_z, ) {}", 2041 NoBinPacking); 2042} 2043 2044TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 2045 verifyFormat("void f() { return; }\n42"); 2046 verifyFormat("void f() {\n" 2047 " if (0)\n" 2048 " return;\n" 2049 "}\n" 2050 "42"); 2051 verifyFormat("void f() { return }\n42"); 2052 verifyFormat("void f() {\n" 2053 " if (0)\n" 2054 " return\n" 2055 "}\n" 2056 "42"); 2057} 2058 2059TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 2060 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 2061 EXPECT_EQ("void f() {\n" 2062 " if (a)\n" 2063 " return\n" 2064 "}", 2065 format("void f ( ) { if ( a ) return }")); 2066 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); 2067 EXPECT_EQ("namespace N {\n" 2068 "void f() {}\n" 2069 "void g()\n" 2070 "}", 2071 format("namespace N { void f( ) { } void g( ) }")); 2072} 2073 2074TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 2075 verifyFormat("int aaaaaaaa =\n" 2076 " // Overly long comment\n" 2077 " b;", 2078 getLLVMStyleWithColumns(20)); 2079 verifyFormat("function(\n" 2080 " ShortArgument,\n" 2081 " LoooooooooooongArgument);\n", 2082 getLLVMStyleWithColumns(20)); 2083} 2084 2085TEST_F(FormatTest, IncorrectAccessSpecifier) { 2086 verifyFormat("public:"); 2087 verifyFormat("class A {\n" 2088 "public\n" 2089 " void f() {}\n" 2090 "};"); 2091 verifyFormat("public\n" 2092 "int qwerty;"); 2093 verifyFormat("public\n" 2094 "B {}"); 2095 verifyFormat("public\n" 2096 "{}"); 2097 verifyFormat("public\n" 2098 "B { int x; }"); 2099} 2100 2101TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { verifyFormat("{"); } 2102 2103TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 2104 verifyFormat("do {\n}"); 2105 verifyFormat("do {\n}\n" 2106 "f();"); 2107 verifyFormat("do {\n}\n" 2108 "wheeee(fun);"); 2109 verifyFormat("do {\n" 2110 " f();\n" 2111 "}"); 2112} 2113 2114TEST_F(FormatTest, IncorrectCodeMissingParens) { 2115 verifyFormat("if {\n foo;\n foo();\n}"); 2116 verifyFormat("switch {\n foo;\n foo();\n}"); 2117 verifyFormat("for {\n foo;\n foo();\n}"); 2118 verifyFormat("while {\n foo;\n foo();\n}"); 2119 verifyFormat("do {\n foo;\n foo();\n} while;"); 2120} 2121 2122TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 2123 verifyFormat("namespace {\n" 2124 "class Foo { Foo ( }; } // comment"); 2125} 2126 2127TEST_F(FormatTest, IncorrectCodeErrorDetection) { 2128 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); 2129 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 2130 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 2131 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); 2132 2133 EXPECT_EQ("{\n" 2134 " {\n" 2135 " breakme(\n" 2136 " qwe);\n" 2137 "}\n", 2138 format("{\n" 2139 " {\n" 2140 " breakme(qwe);\n" 2141 "}\n", 2142 getLLVMStyleWithColumns(10))); 2143} 2144 2145TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 2146 verifyFormat("int x = {\n" 2147 " avariable,\n" 2148 " b(alongervariable)\n" 2149 "};", 2150 getLLVMStyleWithColumns(25)); 2151} 2152 2153TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 2154 verifyFormat("return (a)(b) { 1, 2, 3 };"); 2155} 2156 2157TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { 2158 // FIXME: This is bad, find a better and more generic solution. 2159 verifyFormat( 2160 "Aaa({\n" 2161 " int i;\n" 2162 "},\n" 2163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2164 " ccccccccccccccccc));"); 2165} 2166 2167TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 2168 verifyFormat("void f() { return 42; }"); 2169 verifyFormat("void f() {\n" 2170 " // Comment\n" 2171 "}"); 2172 verifyFormat("{\n" 2173 "#error {\n" 2174 " int a;\n" 2175 "}"); 2176 verifyFormat("{\n" 2177 " int a;\n" 2178 "#error {\n" 2179 "}"); 2180 2181 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 2182 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 2183 2184 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 2185 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 2186} 2187 2188TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 2189 // Elaborate type variable declarations. 2190 verifyFormat("struct foo a = { bar };\nint n;"); 2191 verifyFormat("class foo a = { bar };\nint n;"); 2192 verifyFormat("union foo a = { bar };\nint n;"); 2193 2194 // Elaborate types inside function definitions. 2195 verifyFormat("struct foo f() {}\nint n;"); 2196 verifyFormat("class foo f() {}\nint n;"); 2197 verifyFormat("union foo f() {}\nint n;"); 2198 2199 // Templates. 2200 verifyFormat("template <class X> void f() {}\nint n;"); 2201 verifyFormat("template <struct X> void f() {}\nint n;"); 2202 verifyFormat("template <union X> void f() {}\nint n;"); 2203 2204 // Actual definitions... 2205 verifyFormat("struct {\n} n;"); 2206 verifyFormat( 2207 "template <template <class T, class Y>, class Z> class X {\n} n;"); 2208 verifyFormat("union Z {\n int n;\n} x;"); 2209 verifyFormat("class MACRO Z {\n} n;"); 2210 verifyFormat("class MACRO(X) Z {\n} n;"); 2211 verifyFormat("class __attribute__(X) Z {\n} n;"); 2212 verifyFormat("class __declspec(X) Z {\n} n;"); 2213 verifyFormat("class A##B##C {\n} n;"); 2214 2215 // Redefinition from nested context: 2216 verifyFormat("class A::B::C {\n} n;"); 2217 2218 // Template definitions. 2219 // FIXME: This is still incorrectly handled at the formatter side. 2220 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); 2221 2222 // FIXME: 2223 // This now gets parsed incorrectly as class definition. 2224 // verifyFormat("class A<int> f() {\n}\nint n;"); 2225 2226 // Elaborate types where incorrectly parsing the structural element would 2227 // break the indent. 2228 verifyFormat("if (true)\n" 2229 " class X x;\n" 2230 "else\n" 2231 " f();\n"); 2232} 2233 2234TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 2235 verifyFormat("#error Leave all white!!!!! space* alone!\n"); 2236 verifyFormat("#warning Leave all white!!!!! space* alone!\n"); 2237 EXPECT_EQ("#error 1", format(" # error 1")); 2238 EXPECT_EQ("#warning 1", format(" # warning 1")); 2239} 2240 2241TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 2242 FormatStyle AllowsMergedIf = getGoogleStyle(); 2243 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 2244 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 2245 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 2246 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 2247 EXPECT_EQ("if (true) return 42;", 2248 format("if (true)\nreturn 42;", AllowsMergedIf)); 2249 FormatStyle ShortMergedIf = AllowsMergedIf; 2250 ShortMergedIf.ColumnLimit = 25; 2251 verifyFormat("#define A \\\n" 2252 " if (true) return 42;", 2253 ShortMergedIf); 2254 verifyFormat("#define A \\\n" 2255 " f(); \\\n" 2256 " if (true)\n" 2257 "#define B", 2258 ShortMergedIf); 2259 verifyFormat("#define A \\\n" 2260 " f(); \\\n" 2261 " if (true)\n" 2262 "g();", 2263 ShortMergedIf); 2264 verifyFormat("{\n" 2265 "#ifdef A\n" 2266 " // Comment\n" 2267 " if (true) continue;\n" 2268 "#endif\n" 2269 " // Comment\n" 2270 " if (true) continue;", 2271 ShortMergedIf); 2272} 2273 2274TEST_F(FormatTest, BlockCommentsInControlLoops) { 2275 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2276 " f();\n" 2277 "}"); 2278 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2279 " f();\n" 2280 "} /* another comment */ else /* comment #3 */ {\n" 2281 " g();\n" 2282 "}"); 2283 verifyFormat("while (0) /* a comment in a strange place */ {\n" 2284 " f();\n" 2285 "}"); 2286 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 2287 " f();\n" 2288 "}"); 2289 verifyFormat("do /* a comment in a strange place */ {\n" 2290 " f();\n" 2291 "} /* another comment */ while (0);"); 2292} 2293 2294TEST_F(FormatTest, BlockComments) { 2295 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 2296 format("/* *//* */ /* */\n/* *//* */ /* */")); 2297 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 2298 EXPECT_EQ("#define A /* */\\\n" 2299 " b\n" 2300 "/* */\n" 2301 "someCall(\n" 2302 " parameter);", 2303 format("#define A /* */ b\n" 2304 "/* */\n" 2305 "someCall(parameter);", 2306 getLLVMStyleWithColumns(15))); 2307 2308 EXPECT_EQ("#define A\n" 2309 "/* */ someCall(\n" 2310 " parameter);", 2311 format("#define A\n" 2312 "/* */someCall(parameter);", 2313 getLLVMStyleWithColumns(15))); 2314 2315 FormatStyle NoBinPacking = getLLVMStyle(); 2316 NoBinPacking.BinPackParameters = false; 2317 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 2318 " 2, /* comment 2 */\n" 2319 " 3, /* comment 3 */\n" 2320 " aaaa,\n" 2321 " bbbb);", 2322 format("someFunction (1, /* comment 1 */\n" 2323 " 2, /* comment 2 */ \n" 2324 " 3, /* comment 3 */\n" 2325 "aaaa, bbbb );", 2326 NoBinPacking)); 2327 verifyFormat( 2328 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2329 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2330 EXPECT_EQ( 2331 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2332 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 2334 format( 2335 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2336 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 2338 EXPECT_EQ( 2339 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2340 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2341 "int cccccccccccccccccccccccccccccc; /* comment */\n", 2342 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2343 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2344 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 2345} 2346 2347TEST_F(FormatTest, BlockCommentsInMacros) { 2348 EXPECT_EQ("#define A \\\n" 2349 " { \\\n" 2350 " /* one line */ \\\n" 2351 " someCall();", 2352 format("#define A { \\\n" 2353 " /* one line */ \\\n" 2354 " someCall();", 2355 getLLVMStyleWithColumns(20))); 2356 EXPECT_EQ("#define A \\\n" 2357 " { \\\n" 2358 " /* previous */ \\\n" 2359 " /* one line */ \\\n" 2360 " someCall();", 2361 format("#define A { \\\n" 2362 " /* previous */ \\\n" 2363 " /* one line */ \\\n" 2364 " someCall();", 2365 getLLVMStyleWithColumns(20))); 2366} 2367 2368TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 2369 // FIXME: This is not what we want... 2370 verifyFormat("{\n" 2371 "// a" 2372 "// b"); 2373} 2374 2375TEST_F(FormatTest, FormatStarDependingOnContext) { 2376 verifyFormat("void f(int *a);"); 2377 verifyFormat("void f() { f(fint * b); }"); 2378 verifyFormat("class A {\n void f(int *a);\n};"); 2379 verifyFormat("class A {\n int *a;\n};"); 2380 verifyFormat("namespace a {\n" 2381 "namespace b {\n" 2382 "class A {\n" 2383 " void f() {}\n" 2384 " int *a;\n" 2385 "};\n" 2386 "}\n" 2387 "}"); 2388} 2389 2390TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 2391 verifyFormat("while"); 2392 verifyFormat("operator"); 2393} 2394 2395//===----------------------------------------------------------------------===// 2396// Objective-C tests. 2397//===----------------------------------------------------------------------===// 2398 2399TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 2400 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 2401 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 2402 format("-(NSUInteger)indexOfObject:(id)anObject;")); 2403 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 2404 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 2405 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 2406 format("-(NSInteger)Method3:(id)anObject;")); 2407 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 2408 format("-(NSInteger)Method4:(id)anObject;")); 2409 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 2410 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 2411 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 2412 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 2413 EXPECT_EQ( 2414 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 2415 format( 2416 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 2417 2418 // Very long objectiveC method declaration. 2419 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 2420 " inRange:(NSRange)range\n" 2421 " outRange:(NSRange)out_range\n" 2422 " outRange1:(NSRange)out_range1\n" 2423 " outRange2:(NSRange)out_range2\n" 2424 " outRange3:(NSRange)out_range3\n" 2425 " outRange4:(NSRange)out_range4\n" 2426 " outRange5:(NSRange)out_range5\n" 2427 " outRange6:(NSRange)out_range6\n" 2428 " outRange7:(NSRange)out_range7\n" 2429 " outRange8:(NSRange)out_range8\n" 2430 " outRange9:(NSRange)out_range9;"); 2431 2432 verifyFormat("- (int)sum:(vector<int>)numbers;"); 2433 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 2434 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 2435 // protocol lists (but not for template classes): 2436 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 2437 2438 verifyFormat("- (int(*)())foo:(int(*)())f;"); 2439 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); 2440 2441 // If there's no return type (very rare in practice!), LLVM and Google style 2442 // agree. 2443 verifyFormat("- foo:(int)f;"); 2444 verifyGoogleFormat("- foo:(int)foo;"); 2445} 2446 2447TEST_F(FormatTest, FormatObjCBlocks) { 2448 verifyFormat("int (^Block)(int, int);"); 2449 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); 2450} 2451 2452TEST_F(FormatTest, FormatObjCInterface) { 2453 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 2454 "@public\n" 2455 " int field1;\n" 2456 "@protected\n" 2457 " int field2;\n" 2458 "@private\n" 2459 " int field3;\n" 2460 "@package\n" 2461 " int field4;\n" 2462 "}\n" 2463 "+ (id)init;\n" 2464 "@end"); 2465 2466 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 2467 " @public\n" 2468 " int field1;\n" 2469 " @protected\n" 2470 " int field2;\n" 2471 " @private\n" 2472 " int field3;\n" 2473 " @package\n" 2474 " int field4;\n" 2475 "}\n" 2476 "+ (id)init;\n" 2477 "@end"); 2478 2479 verifyFormat("@interface /* wait for it */ Foo\n" 2480 "+ (id)init;\n" 2481 "// Look, a comment!\n" 2482 "- (int)answerWith:(int)i;\n" 2483 "@end"); 2484 2485 verifyFormat("@interface Foo\n" 2486 "@end\n" 2487 "@interface Bar\n" 2488 "@end"); 2489 2490 verifyFormat("@interface Foo : Bar\n" 2491 "+ (id)init;\n" 2492 "@end"); 2493 2494 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 2495 "+ (id)init;\n" 2496 "@end"); 2497 2498 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 2499 "+ (id)init;\n" 2500 "@end"); 2501 2502 verifyFormat("@interface Foo (HackStuff)\n" 2503 "+ (id)init;\n" 2504 "@end"); 2505 2506 verifyFormat("@interface Foo ()\n" 2507 "+ (id)init;\n" 2508 "@end"); 2509 2510 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 2511 "+ (id)init;\n" 2512 "@end"); 2513 2514 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 2515 "+ (id)init;\n" 2516 "@end"); 2517 2518 verifyFormat("@interface Foo {\n" 2519 " int _i;\n" 2520 "}\n" 2521 "+ (id)init;\n" 2522 "@end"); 2523 2524 verifyFormat("@interface Foo : Bar {\n" 2525 " int _i;\n" 2526 "}\n" 2527 "+ (id)init;\n" 2528 "@end"); 2529 2530 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 2531 " int _i;\n" 2532 "}\n" 2533 "+ (id)init;\n" 2534 "@end"); 2535 2536 verifyFormat("@interface Foo (HackStuff) {\n" 2537 " int _i;\n" 2538 "}\n" 2539 "+ (id)init;\n" 2540 "@end"); 2541 2542 verifyFormat("@interface Foo () {\n" 2543 " int _i;\n" 2544 "}\n" 2545 "+ (id)init;\n" 2546 "@end"); 2547 2548 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 2549 " int _i;\n" 2550 "}\n" 2551 "+ (id)init;\n" 2552 "@end"); 2553} 2554 2555TEST_F(FormatTest, FormatObjCImplementation) { 2556 verifyFormat("@implementation Foo : NSObject {\n" 2557 "@public\n" 2558 " int field1;\n" 2559 "@protected\n" 2560 " int field2;\n" 2561 "@private\n" 2562 " int field3;\n" 2563 "@package\n" 2564 " int field4;\n" 2565 "}\n" 2566 "+ (id)init {\n}\n" 2567 "@end"); 2568 2569 verifyGoogleFormat("@implementation Foo : NSObject {\n" 2570 " @public\n" 2571 " int field1;\n" 2572 " @protected\n" 2573 " int field2;\n" 2574 " @private\n" 2575 " int field3;\n" 2576 " @package\n" 2577 " int field4;\n" 2578 "}\n" 2579 "+ (id)init {\n}\n" 2580 "@end"); 2581 2582 verifyFormat("@implementation Foo\n" 2583 "+ (id)init {\n" 2584 " if (true)\n" 2585 " return nil;\n" 2586 "}\n" 2587 "// Look, a comment!\n" 2588 "- (int)answerWith:(int)i {\n" 2589 " return i;\n" 2590 "}\n" 2591 "+ (int)answerWith:(int)i {\n" 2592 " return i;\n" 2593 "}\n" 2594 "@end"); 2595 2596 verifyFormat("@implementation Foo\n" 2597 "@end\n" 2598 "@implementation Bar\n" 2599 "@end"); 2600 2601 verifyFormat("@implementation Foo : Bar\n" 2602 "+ (id)init {\n}\n" 2603 "- (void)foo {\n}\n" 2604 "@end"); 2605 2606 verifyFormat("@implementation Foo {\n" 2607 " int _i;\n" 2608 "}\n" 2609 "+ (id)init {\n}\n" 2610 "@end"); 2611 2612 verifyFormat("@implementation Foo : Bar {\n" 2613 " int _i;\n" 2614 "}\n" 2615 "+ (id)init {\n}\n" 2616 "@end"); 2617 2618 verifyFormat("@implementation Foo (HackStuff)\n" 2619 "+ (id)init {\n}\n" 2620 "@end"); 2621} 2622 2623TEST_F(FormatTest, FormatObjCProtocol) { 2624 verifyFormat("@protocol Foo\n" 2625 "@property(weak) id delegate;\n" 2626 "- (NSUInteger)numberOfThings;\n" 2627 "@end"); 2628 2629 verifyFormat("@protocol MyProtocol <NSObject>\n" 2630 "- (NSUInteger)numberOfThings;\n" 2631 "@end"); 2632 2633 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 2634 "- (NSUInteger)numberOfThings;\n" 2635 "@end"); 2636 2637 verifyFormat("@protocol Foo;\n" 2638 "@protocol Bar;\n"); 2639 2640 verifyFormat("@protocol Foo\n" 2641 "@end\n" 2642 "@protocol Bar\n" 2643 "@end"); 2644 2645 verifyFormat("@protocol myProtocol\n" 2646 "- (void)mandatoryWithInt:(int)i;\n" 2647 "@optional\n" 2648 "- (void)optional;\n" 2649 "@required\n" 2650 "- (void)required;\n" 2651 "@optional\n" 2652 "@property(assign) int madProp;\n" 2653 "@end\n"); 2654} 2655 2656TEST_F(FormatTest, FormatObjCMethodDeclarations) { 2657 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 2658 " rect:(NSRect)theRect\n" 2659 " interval:(float)theInterval {\n" 2660 "}"); 2661 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 2662 " longKeyword:(NSRect)theRect\n" 2663 " evenLongerKeyword:(float)theInterval\n" 2664 " error:(NSError **)theError {\n" 2665 "}"); 2666} 2667 2668TEST_F(FormatTest, FormatObjCMethodExpr) { 2669 verifyFormat("[foo bar:baz];"); 2670 verifyFormat("return [foo bar:baz];"); 2671 verifyFormat("f([foo bar:baz]);"); 2672 verifyFormat("f(2, [foo bar:baz]);"); 2673 verifyFormat("f(2, a ? b : c);"); 2674 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 2675 2676 // Unary operators. 2677 verifyFormat("int a = +[foo bar:baz];"); 2678 verifyFormat("int a = -[foo bar:baz];"); 2679 verifyFormat("int a = ![foo bar:baz];"); 2680 verifyFormat("int a = ~[foo bar:baz];"); 2681 verifyFormat("int a = ++[foo bar:baz];"); 2682 verifyFormat("int a = --[foo bar:baz];"); 2683 verifyFormat("int a = sizeof [foo bar:baz];"); 2684 verifyFormat("int a = alignof [foo bar:baz];"); 2685 verifyFormat("int a = &[foo bar:baz];"); 2686 verifyFormat("int a = *[foo bar:baz];"); 2687 // FIXME: Make casts work, without breaking f()[4]. 2688 //verifyFormat("int a = (int)[foo bar:baz];"); 2689 //verifyFormat("return (int)[foo bar:baz];"); 2690 //verifyFormat("(void)[foo bar:baz];"); 2691 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 2692 2693 // Binary operators. 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];"); 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] : [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 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 2717 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 2718 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 2719 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 2720 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 2721 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 2722 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 2723 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 2724 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 2725 // Whew! 2726 2727 verifyFormat("return in[42];"); 2728 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 2729 "}"); 2730 2731 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 2732 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 2733 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 2734 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 2735 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 2736 verifyFormat("[button setAction:@selector(zoomOut:)];"); 2737 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 2738 2739 verifyFormat("arr[[self indexForFoo:a]];"); 2740 verifyFormat("throw [self errorFor:a];"); 2741 verifyFormat("@throw [self errorFor:a];"); 2742 2743 // This tests that the formatter doesn't break after "backing" but before ":", 2744 // which would be at 80 columns. 2745 verifyFormat( 2746 "void f() {\n" 2747 " if ((self = [super initWithContentRect:contentRect\n" 2748 " styleMask:styleMask\n" 2749 " backing:NSBackingStoreBuffered\n" 2750 " defer:YES]))"); 2751 2752 verifyFormat( 2753 "[foo checkThatBreakingAfterColonWorksOk:\n" 2754 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 2755 2756 verifyFormat("[myObj short:arg1 // Force line break\n" 2757 " longKeyword:arg2\n" 2758 " evenLongerKeyword:arg3\n" 2759 " error:arg4];"); 2760 verifyFormat( 2761 "void f() {\n" 2762 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 2763 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 2764 " pos.width(), pos.height())\n" 2765 " styleMask:NSBorderlessWindowMask\n" 2766 " backing:NSBackingStoreBuffered\n" 2767 " defer:NO]);\n" 2768 "}"); 2769 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 2770 " with:contentsNativeView];"); 2771 2772 verifyFormat( 2773 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 2774 " owner:nillllll];"); 2775 2776 verifyFormat( 2777 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 2778 " forType:kBookmarkButtonDragType];"); 2779 2780 verifyFormat("[defaultCenter addObserver:self\n" 2781 " selector:@selector(willEnterFullscreen)\n" 2782 " name:kWillEnterFullscreenNotification\n" 2783 " object:nil];"); 2784 verifyFormat("[image_rep drawInRect:drawRect\n" 2785 " fromRect:NSZeroRect\n" 2786 " operation:NSCompositeCopy\n" 2787 " fraction:1.0\n" 2788 " respectFlipped:NO\n" 2789 " hints:nil];"); 2790 2791 verifyFormat( 2792 "scoped_nsobject<NSTextField> message(\n" 2793 " // The frame will be fixed up when |-setMessageText:| is called.\n" 2794 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 2795} 2796 2797TEST_F(FormatTest, ObjCAt) { 2798 verifyFormat("@autoreleasepool"); 2799 verifyFormat("@catch"); 2800 verifyFormat("@class"); 2801 verifyFormat("@compatibility_alias"); 2802 verifyFormat("@defs"); 2803 verifyFormat("@dynamic"); 2804 verifyFormat("@encode"); 2805 verifyFormat("@end"); 2806 verifyFormat("@finally"); 2807 verifyFormat("@implementation"); 2808 verifyFormat("@import"); 2809 verifyFormat("@interface"); 2810 verifyFormat("@optional"); 2811 verifyFormat("@package"); 2812 verifyFormat("@private"); 2813 verifyFormat("@property"); 2814 verifyFormat("@protected"); 2815 verifyFormat("@protocol"); 2816 verifyFormat("@public"); 2817 verifyFormat("@required"); 2818 verifyFormat("@selector"); 2819 verifyFormat("@synchronized"); 2820 verifyFormat("@synthesize"); 2821 verifyFormat("@throw"); 2822 verifyFormat("@try"); 2823 2824 EXPECT_EQ("@interface", format("@ interface")); 2825 2826 // The precise formatting of this doesn't matter, nobody writes code like 2827 // this. 2828 verifyFormat("@ /*foo*/ interface"); 2829} 2830 2831TEST_F(FormatTest, ObjCSnippets) { 2832 verifyFormat("@autoreleasepool {\n" 2833 " foo();\n" 2834 "}"); 2835 verifyFormat("@class Foo, Bar;"); 2836 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 2837 verifyFormat("@dynamic textColor;"); 2838 verifyFormat("char *buf1 = @encode(int *);"); 2839 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 2840 verifyFormat("char *buf1 = @encode(int **);"); 2841 verifyFormat("Protocol *proto = @protocol(p1);"); 2842 verifyFormat("SEL s = @selector(foo:);"); 2843 verifyFormat("@synchronized(self) {\n" 2844 " f();\n" 2845 "}"); 2846 2847 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 2848 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 2849 2850 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 2851 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 2852 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 2853} 2854 2855TEST_F(FormatTest, ObjCLiterals) { 2856 verifyFormat("@\"String\""); 2857 verifyFormat("@1"); 2858 verifyFormat("@+4.8"); 2859 verifyFormat("@-4"); 2860 verifyFormat("@1LL"); 2861 verifyFormat("@.5"); 2862 verifyFormat("@'c'"); 2863 verifyFormat("@true"); 2864 2865 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 2866 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 2867 verifyFormat("NSNumber *favoriteColor = @(Green);"); 2868 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 2869 2870 verifyFormat("@["); 2871 verifyFormat("@[]"); 2872 verifyFormat( 2873 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 2874 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 2875 2876 verifyFormat("@{"); 2877 verifyFormat("@{}"); 2878 verifyFormat("@{ @\"one\" : @1 }"); 2879 verifyFormat("return @{ @\"one\" : @1 };"); 2880 verifyFormat("@{ @\"one\" : @1, }"); 2881 verifyFormat("@{ @\"one\" : @{ @2 : @1 } }"); 2882 verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }"); 2883 verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); 2884 verifyFormat("[self setDict:@{}"); 2885 verifyFormat("[self setDict:@{ @1 : @2 }"); 2886 verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);"); 2887 verifyFormat( 2888 "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };"); 2889 verifyFormat( 2890 "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); 2891 2892 // FIXME: Nested and multi-line array and dictionary literals need more work. 2893 verifyFormat( 2894 "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n" 2895 " @\"processInfo\" : [NSProcessInfo processInfo] };"); 2896} 2897 2898TEST_F(FormatTest, ReformatRegionAdjustsIndent) { 2899 EXPECT_EQ("{\n" 2900 "{\n" 2901 "a;\n" 2902 "b;\n" 2903 "}\n" 2904 "}", 2905 format("{\n" 2906 "{\n" 2907 "a;\n" 2908 " b;\n" 2909 "}\n" 2910 "}", 2911 13, 2, getLLVMStyle())); 2912 EXPECT_EQ("{\n" 2913 "{\n" 2914 " a;\n" 2915 "b;\n" 2916 "}\n" 2917 "}", 2918 format("{\n" 2919 "{\n" 2920 " a;\n" 2921 "b;\n" 2922 "}\n" 2923 "}", 2924 9, 2, getLLVMStyle())); 2925 EXPECT_EQ("{\n" 2926 "{\n" 2927 "public:\n" 2928 " b;\n" 2929 "}\n" 2930 "}", 2931 format("{\n" 2932 "{\n" 2933 "public:\n" 2934 " b;\n" 2935 "}\n" 2936 "}", 2937 17, 2, getLLVMStyle())); 2938 EXPECT_EQ("{\n" 2939 "{\n" 2940 "a;\n" 2941 "}\n" 2942 "{\n" 2943 " b;\n" 2944 "}\n" 2945 "}", 2946 format("{\n" 2947 "{\n" 2948 "a;\n" 2949 "}\n" 2950 "{\n" 2951 " b;\n" 2952 "}\n" 2953 "}", 2954 22, 2, getLLVMStyle())); 2955 EXPECT_EQ(" {\n" 2956 " a;\n" 2957 " }", 2958 format(" {\n" 2959 "a;\n" 2960 " }", 2961 4, 2, getLLVMStyle())); 2962 EXPECT_EQ("void f() {}\n" 2963 "void g() {}", 2964 format("void f() {}\n" 2965 "void g() {}", 2966 13, 0, getLLVMStyle())); 2967} 2968 2969TEST_F(FormatTest, BreakStringLiterals) { 2970 EXPECT_EQ("\"some text \"\n" 2971 "\"other\";", 2972 format("\"some text other\";", getLLVMStyleWithColumns(12))); 2973 EXPECT_EQ( 2974 "#define A \\\n" 2975 " \"some \" \\\n" 2976 " \"text \" \\\n" 2977 " \"other\";", 2978 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 2979 EXPECT_EQ( 2980 "#define A \\\n" 2981 " \"so \" \\\n" 2982 " \"text \" \\\n" 2983 " \"other\";", 2984 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 2985 2986 EXPECT_EQ("\"some text\"", 2987 format("\"some text\"", getLLVMStyleWithColumns(1))); 2988 EXPECT_EQ("\"some text\"", 2989 format("\"some text\"", getLLVMStyleWithColumns(11))); 2990 EXPECT_EQ("\"some \"\n" 2991 "\"text\"", 2992 format("\"some text\"", getLLVMStyleWithColumns(10))); 2993 EXPECT_EQ("\"some \"\n" 2994 "\"text\"", 2995 format("\"some text\"", getLLVMStyleWithColumns(7))); 2996 EXPECT_EQ("\"some text\"", 2997 format("\"some text\"", getLLVMStyleWithColumns(6))); 2998 2999 EXPECT_EQ("variable =\n" 3000 " \"long string \"\n" 3001 " \"literal\";", 3002 format("variable = \"long string literal\";", 3003 getLLVMStyleWithColumns(20))); 3004 3005 EXPECT_EQ("variable = f(\n" 3006 " \"long string \"\n" 3007 " \"literal\",\n" 3008 " short,\n" 3009 " loooooooooooooooooooong);", 3010 format("variable = f(\"long string literal\", short, " 3011 "loooooooooooooooooooong);", 3012 getLLVMStyleWithColumns(20))); 3013 EXPECT_EQ( 3014 "f(\"one two\".split(\n" 3015 " variable));", 3016 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 3017 EXPECT_EQ("f(\"one two three four five six \"\n" 3018 " \"seven\".split(\n" 3019 " really_looooong_variable));", 3020 format("f(\"one two three four five six seven\"." 3021 "split(really_looooong_variable));", 3022 getLLVMStyleWithColumns(33))); 3023 3024 EXPECT_EQ("f(\"some \"\n" 3025 " \"text\",\n" 3026 " other);", 3027 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 3028 3029 // Only break as a last resort. 3030 verifyFormat( 3031 "aaaaaaaaaaaaaaaaaaaa(\n" 3032 " aaaaaaaaaaaaaaaaaaaa,\n" 3033 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 3034} 3035 3036} // end namespace tooling 3037} // end namespace clang 3038