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