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