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