FormatTest.cpp revision 4e778091b0ff42895644ca8bef30f1a91ba6b32b
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 18// Uncomment to get debug output from tests: 19// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0) 20 21namespace clang { 22namespace format { 23 24class FormatTest : public ::testing::Test { 25protected: 26 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, 27 const FormatStyle &Style) { 28 DEBUG(llvm::errs() << "---\n"); 29 RewriterTestContext Context; 30 FileID ID = Context.createInMemoryFile("input.cc", Code); 31 SourceLocation Start = 32 Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); 33 std::vector<CharSourceRange> Ranges( 34 1, 35 CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); 36 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, 37 getFormattingLangOpts()); 38 tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, 39 Ranges, 40 new IgnoringDiagConsumer()); 41 ReplacementCount = Replace.size(); 42 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); 43 DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n"); 44 return Context.getRewrittenText(ID); 45 } 46 47 std::string format(llvm::StringRef Code, 48 const FormatStyle &Style = getLLVMStyle()) { 49 return format(Code, 0, Code.size(), Style); 50 } 51 52 std::string messUp(llvm::StringRef Code) { 53 std::string MessedUp(Code.str()); 54 bool InComment = false; 55 bool InPreprocessorDirective = false; 56 bool JustReplacedNewline = false; 57 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { 58 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { 59 if (JustReplacedNewline) 60 MessedUp[i - 1] = '\n'; 61 InComment = true; 62 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) { 63 if (i != 0) MessedUp[i - 1] = '\n'; 64 InPreprocessorDirective = true; 65 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { 66 MessedUp[i] = ' '; 67 MessedUp[i + 1] = ' '; 68 } else if (MessedUp[i] == '\n') { 69 if (InComment) { 70 InComment = false; 71 } else if (InPreprocessorDirective) { 72 InPreprocessorDirective = false; 73 } else { 74 JustReplacedNewline = true; 75 MessedUp[i] = ' '; 76 } 77 } else if (MessedUp[i] != ' ') { 78 JustReplacedNewline = false; 79 } 80 } 81 return MessedUp; 82 } 83 84 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 85 FormatStyle Style = getLLVMStyle(); 86 Style.ColumnLimit = ColumnLimit; 87 return Style; 88 } 89 90 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 91 FormatStyle Style = getGoogleStyle(); 92 Style.ColumnLimit = ColumnLimit; 93 return Style; 94 } 95 96 void verifyFormat(llvm::StringRef Code, 97 const FormatStyle &Style = getLLVMStyle()) { 98 EXPECT_EQ(Code.str(), format(messUp(Code), Style)); 99 } 100 101 void verifyGoogleFormat(llvm::StringRef Code) { 102 verifyFormat(Code, getGoogleStyle()); 103 } 104 105 void verifyIndependentOfContext(llvm::StringRef text) { 106 verifyFormat(text); 107 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 108 } 109 110 int ReplacementCount; 111}; 112 113TEST_F(FormatTest, MessUp) { 114 EXPECT_EQ("1 2 3", messUp("1 2 3")); 115 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); 116 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); 117 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); 118 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); 119} 120 121//===----------------------------------------------------------------------===// 122// Basic function tests. 123//===----------------------------------------------------------------------===// 124 125TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { 126 EXPECT_EQ(";", format(";")); 127} 128 129TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 130 EXPECT_EQ("int i;", format(" int i;")); 131 EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); 132 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 133 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 134} 135 136TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 137 EXPECT_EQ("int i;", format("int\ni;")); 138} 139 140TEST_F(FormatTest, FormatsNestedBlockStatements) { 141 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 142} 143 144TEST_F(FormatTest, FormatsNestedCall) { 145 verifyFormat("Method(f1, f2(f3));"); 146 verifyFormat("Method(f1(f2, f3()));"); 147 verifyFormat("Method(f1(f2, (f3())));"); 148} 149 150TEST_F(FormatTest, ImportantSpaces) { 151 verifyFormat("vector< ::Type> v;"); 152} 153 154TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 155 EXPECT_EQ("if (a) {\n" 156 " f();\n" 157 "}", format("if(a){f();}")); 158 EXPECT_EQ(4, ReplacementCount); 159 EXPECT_EQ("if (a) {\n" 160 " f();\n" 161 "}", format("if (a) {\n" 162 " f();\n" 163 "}")); 164 EXPECT_EQ(0, ReplacementCount); 165} 166 167//===----------------------------------------------------------------------===// 168// Tests for control statements. 169//===----------------------------------------------------------------------===// 170 171TEST_F(FormatTest, FormatIfWithoutCompountStatement) { 172 verifyFormat("if (true)\n f();\ng();"); 173 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 174 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 175 176 FormatStyle AllowsMergedIf = getGoogleStyle(); 177 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 178 verifyFormat("if (a)\n" 179 " // comment\n" 180 " f();", AllowsMergedIf); 181 182 verifyFormat("if (a) // Can't merge this\n" 183 " f();\n", AllowsMergedIf); 184 verifyFormat("if (a) /* still don't merge */\n" 185 " f();", AllowsMergedIf); 186 verifyFormat("if (a) { // Never merge this\n" 187 " f();\n" 188 "}", AllowsMergedIf); 189 verifyFormat("if (a) { /* Never merge this */\n" 190 " f();\n" 191 "}", AllowsMergedIf); 192 193 AllowsMergedIf.ColumnLimit = 14; 194 verifyFormat("if (a) return;", AllowsMergedIf); 195 verifyFormat("if (aaaaaaaaa)\n" 196 " return;", AllowsMergedIf); 197 198 AllowsMergedIf.ColumnLimit = 13; 199 verifyFormat("if (a)\n return;", AllowsMergedIf); 200} 201 202TEST_F(FormatTest, ParseIfElse) { 203 verifyFormat("if (true)\n" 204 " if (true)\n" 205 " if (true)\n" 206 " f();\n" 207 " else\n" 208 " g();\n" 209 " else\n" 210 " h();\n" 211 "else\n" 212 " i();"); 213 verifyFormat("if (true)\n" 214 " if (true)\n" 215 " if (true) {\n" 216 " if (true)\n" 217 " f();\n" 218 " } else {\n" 219 " g();\n" 220 " }\n" 221 " else\n" 222 " h();\n" 223 "else {\n" 224 " i();\n" 225 "}"); 226} 227 228TEST_F(FormatTest, ElseIf) { 229 verifyFormat("if (a) {\n} else if (b) {\n}"); 230 verifyFormat("if (a)\n" 231 " f();\n" 232 "else if (b)\n" 233 " g();\n" 234 "else\n" 235 " h();"); 236} 237 238TEST_F(FormatTest, FormatsForLoop) { 239 verifyFormat( 240 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 241 " ++VeryVeryLongLoopVariable)\n" 242 " ;"); 243 verifyFormat("for (;;)\n" 244 " f();"); 245 verifyFormat("for (;;) {\n}"); 246 verifyFormat("for (;;) {\n" 247 " f();\n" 248 "}"); 249 250 verifyFormat( 251 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 252 " E = UnwrappedLines.end();\n" 253 " I != E; ++I) {\n}"); 254 255 verifyFormat( 256 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 257 " ++IIIII) {\n}"); 258 verifyFormat( 259 "for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 260 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 261 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 262 263 // FIXME: Not sure whether we want extra identation in line 3 here: 264 verifyFormat( 265 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 267 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 269 " ++aaaaaaaaaaa) {\n}"); 270 271 verifyGoogleFormat( 272 "for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 273 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 274 "}"); 275 verifyGoogleFormat( 276 "for (int aaaaaaaaaaa = 1;\n" 277 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 278 " aaaaaaaaaaaaaaaa,\n" 279 " aaaaaaaaaaaaaaaa,\n" 280 " aaaaaaaaaaaaaaaa);\n" 281 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 282 "}"); 283} 284 285TEST_F(FormatTest, RangeBasedForLoops) { 286 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 288 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 289 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 290} 291 292TEST_F(FormatTest, FormatsWhileLoop) { 293 verifyFormat("while (true) {\n}"); 294 verifyFormat("while (true)\n" 295 " f();"); 296 verifyFormat("while () {\n}"); 297 verifyFormat("while () {\n" 298 " f();\n" 299 "}"); 300} 301 302TEST_F(FormatTest, FormatsDoWhile) { 303 verifyFormat("do {\n" 304 " do_something();\n" 305 "} while (something());"); 306 verifyFormat("do\n" 307 " do_something();\n" 308 "while (something());"); 309} 310 311TEST_F(FormatTest, FormatsSwitchStatement) { 312 verifyFormat("switch (x) {\n" 313 "case 1:\n" 314 " f();\n" 315 " break;\n" 316 "case kFoo:\n" 317 "case ns::kBar:\n" 318 "case kBaz:\n" 319 " break;\n" 320 "default:\n" 321 " g();\n" 322 " break;\n" 323 "}"); 324 verifyFormat("switch (x) {\n" 325 "case 1: {\n" 326 " f();\n" 327 " break;\n" 328 "}\n" 329 "}"); 330 verifyFormat("switch (x) {\n" 331 "case 1: {\n" 332 " f();\n" 333 " {\n" 334 " g();\n" 335 " h();\n" 336 " }\n" 337 " break;\n" 338 "}\n" 339 "}"); 340 verifyFormat("switch (x) {\n" 341 "case 1: {\n" 342 " f();\n" 343 " if (foo) {\n" 344 " g();\n" 345 " h();\n" 346 " }\n" 347 " break;\n" 348 "}\n" 349 "}"); 350 verifyFormat("switch (x) {\n" 351 "case 1: {\n" 352 " f();\n" 353 " g();\n" 354 "} break;\n" 355 "}"); 356 verifyFormat("switch (test)\n" 357 " ;"); 358 verifyGoogleFormat("switch (x) {\n" 359 " case 1:\n" 360 " f();\n" 361 " break;\n" 362 " case kFoo:\n" 363 " case ns::kBar:\n" 364 " case kBaz:\n" 365 " break;\n" 366 " default:\n" 367 " g();\n" 368 " break;\n" 369 "}"); 370 verifyGoogleFormat("switch (x) {\n" 371 " case 1: {\n" 372 " f();\n" 373 " break;\n" 374 " }\n" 375 "}"); 376 verifyGoogleFormat("switch (test)\n" 377 " ;"); 378} 379 380TEST_F(FormatTest, FormatsLabels) { 381 verifyFormat("void f() {\n" 382 " some_code();\n" 383 "test_label:\n" 384 " some_other_code();\n" 385 " {\n" 386 " some_more_code();\n" 387 " another_label:\n" 388 " some_more_code();\n" 389 " }\n" 390 "}"); 391 verifyFormat("some_code();\n" 392 "test_label:\n" 393 "some_other_code();"); 394} 395 396//===----------------------------------------------------------------------===// 397// Tests for comments. 398//===----------------------------------------------------------------------===// 399 400TEST_F(FormatTest, UnderstandsSingleLineComments) { 401 verifyFormat("// line 1\n" 402 "// line 2\n" 403 "void f() {}\n"); 404 405 verifyFormat("void f() {\n" 406 " // Doesn't do anything\n" 407 "}"); 408 verifyFormat("void f(int i, // some comment (probably for i)\n" 409 " int j, // some comment (probably for j)\n" 410 " int k); // some comment (probably for k)"); 411 verifyFormat("void f(int i,\n" 412 " // some comment (probably for j)\n" 413 " int j,\n" 414 " // some comment (probably for k)\n" 415 " int k);"); 416 417 verifyFormat("int i // This is a fancy variable\n" 418 " = 5; // with nicely aligned comment."); 419 420 verifyFormat("// Leading comment.\n" 421 "int a; // Trailing comment."); 422 verifyFormat("int a; // Trailing comment\n" 423 " // on 2\n" 424 " // or 3 lines.\n" 425 "int b;"); 426 verifyFormat("int a; // Trailing comment\n" 427 "\n" 428 "// Leading comment.\n" 429 "int b;"); 430 verifyFormat("int a; // Comment.\n" 431 " // More details.\n" 432 "int bbbb; // Another comment."); 433 verifyFormat( 434 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 435 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 436 "int cccccccccccccccccccccccccccccc; // comment\n" 437 "int ddd; // looooooooooooooooooooooooong comment\n" 438 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 439 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 440 "int ccccccccccccccccccc; // comment"); 441 442 verifyFormat("#include \"a\" // comment\n" 443 "#include \"a/b/c\" // comment"); 444 verifyFormat("#include <a> // comment\n" 445 "#include <a/b/c> // comment"); 446 447 verifyFormat("enum E {\n" 448 " // comment\n" 449 " VAL_A, // comment\n" 450 " VAL_B\n" 451 "};"); 452 453 verifyFormat( 454 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 455 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 456 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 457 " // Comment inside a statement.\n" 458 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 459 460 EXPECT_EQ("void f() { // This does something ..\n" 461 "}\n" 462 "int a; // This is unrelated", 463 format("void f() { // This does something ..\n" 464 " }\n" 465 "int a; // This is unrelated")); 466 EXPECT_EQ("void f() { // This does something ..\n" 467 "} // awesome..\n" 468 "\n" 469 "int a; // This is unrelated", 470 format("void f() { // This does something ..\n" 471 " } // awesome..\n" 472 " \n" 473 "int a; // This is unrelated")); 474 475 EXPECT_EQ("int i; // single line trailing comment", 476 format("int i;\\\n// single line trailing comment")); 477 478 verifyGoogleFormat("int a; // Trailing comment."); 479 480 verifyFormat("someFunction(anotherFunction( // Force break.\n" 481 " parameter));"); 482 483 verifyGoogleFormat("#endif // HEADER_GUARD"); 484} 485 486TEST_F(FormatTest, UnderstandsMultiLineComments) { 487 verifyFormat("f(/*test=*/ true);"); 488 EXPECT_EQ( 489 "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 490 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 491 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n" 492 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 493 EXPECT_EQ( 494 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 495 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 496 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 497 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 498 499 verifyGoogleFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 500 " /* parameter 2 */ aaaaaa,\n" 501 " /* parameter 3 */ aaaaaa,\n" 502 " /* parameter 4 */ aaaaaa);"); 503} 504 505TEST_F(FormatTest, CommentsInStaticInitializers) { 506 EXPECT_EQ( 507 "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 508 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 509 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 510 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 511 " aaaaaaaaaaaaaaaaaaaa };", 512 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 513 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 514 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 515 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 516 " aaaaaaaaaaaaaaaaaaaa };")); 517 verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" 518 " bbbbbbbbbbb, ccccccccccc };"); 519 verifyFormat("static SomeType type = { aaaaaaaaaaa,\n" 520 " // comment for bb....\n" 521 " bbbbbbbbbbb, ccccccccccc };"); 522 verifyGoogleFormat( 523 "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" 524 " bbbbbbbbbbb, ccccccccccc };"); 525 verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n" 526 " // comment for bb....\n" 527 " bbbbbbbbbbb, ccccccccccc };"); 528 529 verifyFormat("S s = { { a, b, c }, // Group #1\n" 530 " { d, e, f }, // Group #2\n" 531 " { g, h, i } }; // Group #3"); 532 verifyFormat("S s = { { // Group #1\n" 533 " a, b, c },\n" 534 " { // Group #2\n" 535 " d, e, f },\n" 536 " { // Group #3\n" 537 " g, h, i } };"); 538} 539 540//===----------------------------------------------------------------------===// 541// Tests for classes, namespaces, etc. 542//===----------------------------------------------------------------------===// 543 544TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 545 verifyFormat("class A {\n};"); 546} 547 548TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 549 verifyFormat("class A {\n" 550 "public:\n" 551 "protected:\n" 552 "private:\n" 553 " void f() {}\n" 554 "};"); 555 verifyGoogleFormat("class A {\n" 556 " public:\n" 557 " protected:\n" 558 " private:\n" 559 " void f() {}\n" 560 "};"); 561} 562 563TEST_F(FormatTest, FormatsDerivedClass) { 564 verifyFormat("class A : public B {\n};"); 565 verifyFormat("class A : public ::B {\n};"); 566} 567 568TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 569 verifyFormat("class A {\n} a, b;"); 570 verifyFormat("struct A {\n} a, b;"); 571 verifyFormat("union A {\n} a;"); 572} 573 574TEST_F(FormatTest, FormatsEnum) { 575 verifyFormat("enum {\n" 576 " Zero,\n" 577 " One = 1,\n" 578 " Two = One + 1,\n" 579 " Three = (One + Two),\n" 580 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 581 " Five = (One, Two, Three, Four, 5)\n" 582 "};"); 583 verifyFormat("enum Enum {\n" 584 "};"); 585 verifyFormat("enum {\n" 586 "};"); 587 verifyFormat("enum X E {\n} d;"); 588 verifyFormat("enum __attribute__((...)) E {\n} d;"); 589 verifyFormat("enum __declspec__((...)) E {\n} d;"); 590 verifyFormat("enum X f() {\n a();\n return 42;\n}"); 591} 592 593TEST_F(FormatTest, FormatsBitfields) { 594 verifyFormat("struct Bitfields {\n" 595 " unsigned sClass : 8;\n" 596 " unsigned ValueKind : 2;\n" 597 "};"); 598} 599 600TEST_F(FormatTest, FormatsNamespaces) { 601 verifyFormat("namespace some_namespace {\n" 602 "class A {\n};\n" 603 "void f() { f(); }\n" 604 "}"); 605 verifyFormat("namespace {\n" 606 "class A {\n};\n" 607 "void f() { f(); }\n" 608 "}"); 609 verifyFormat("inline namespace X {\n" 610 "class A {\n};\n" 611 "void f() { f(); }\n" 612 "}"); 613 verifyFormat("using namespace some_namespace;\n" 614 "class A {\n};\n" 615 "void f() { f(); }"); 616} 617 618TEST_F(FormatTest, FormatsExternC) { 619 verifyFormat("extern \"C\" {\nint a;"); 620} 621 622TEST_F(FormatTest, FormatTryCatch) { 623 // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll 624 // also not create single-line-blocks. 625 verifyFormat("try {\n" 626 " throw a * b;\n" 627 "}\n" 628 "catch (int a) {\n" 629 " // Do nothing.\n" 630 "}\n" 631 "catch (...) {\n" 632 " exit(42);\n" 633 "}"); 634 635 // Function-level try statements. 636 verifyFormat("int f() try { return 4; }\n" 637 "catch (...) {\n" 638 " return 5;\n" 639 "}"); 640 verifyFormat("class A {\n" 641 " int a;\n" 642 " A() try : a(0) {}\n" 643 " catch (...) {\n" 644 " throw;\n" 645 " }\n" 646 "};\n"); 647} 648 649TEST_F(FormatTest, FormatObjCTryCatch) { 650 verifyFormat("@try {\n" 651 " f();\n" 652 "}\n" 653 "@catch (NSException e) {\n" 654 " @throw;\n" 655 "}\n" 656 "@finally {\n" 657 " exit(42);\n" 658 "}"); 659} 660 661TEST_F(FormatTest, StaticInitializers) { 662 verifyFormat("static SomeClass SC = { 1, 'a' };"); 663 664 // FIXME: Format like enums if the static initializer does not fit on a line. 665 verifyFormat( 666 "static SomeClass WithALoooooooooooooooooooongName = {\n" 667 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 668 "};"); 669 670 verifyFormat( 671 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" 672 " looooooooooooooooooooooooooooooooooongname,\n" 673 " looooooooooooooooooooooooooooooong };"); 674 // Allow bin-packing in static initializers as this would often lead to 675 // terrible results, e.g.: 676 verifyGoogleFormat( 677 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" 678 " looooooooooooooooooooooooooooooooooongname,\n" 679 " looooooooooooooooooooooooooooooong };"); 680} 681 682TEST_F(FormatTest, NestedStaticInitializers) { 683 verifyFormat("static A x = { { {} } };\n"); 684 verifyFormat( 685 "static A x = { { { init1, init2, init3, init4 },\n" 686 " { init1, init2, init3, init4 } } };"); 687 688 verifyFormat( 689 "somes Status::global_reps[3] = {\n" 690 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" 691 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" 692 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" 693 "};"); 694 verifyGoogleFormat( 695 "somes Status::global_reps[3] = {\n" 696 " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" 697 " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" 698 " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" 699 "};"); 700 verifyFormat( 701 "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n" 702 " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop" 703 " } };"); 704 705 verifyFormat( 706 "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n" 707 " { 111111111111111111111111111111,\n" 708 " 222222222222222222222222222222,\n" 709 " 333333333333333333333333333333 },\n" 710 " { 1, 2, 3 }, { 1, 2, 3 } } };"); 711 verifyFormat( 712 "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n" 713 " { { 111111111111111111111111111111,\n" 714 " 222222222222222222222222222222,\n" 715 " 333333333333333333333333333333 } },\n" 716 " { { 1, 2, 3 } }, { { 1, 2, 3 } } };"); 717 718 // FIXME: We might at some point want to handle this similar to parameter 719 // lists, where we have an option to put each on a single line. 720 verifyFormat("struct {\n" 721 " unsigned bit;\n" 722 " const char *const name;\n" 723 "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n" 724 " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };"); 725} 726 727TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 728 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 729 " \\\n" 730 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 731} 732 733TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 734 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 735 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 736} 737 738TEST_F(FormatTest, LayoutUnknownPPDirective) { 739 EXPECT_EQ("#123 \"A string literal\"", 740 format(" # 123 \"A string literal\"")); 741 EXPECT_EQ("#;", format("#;")); 742 verifyFormat("#\n;\n;\n;"); 743} 744 745TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 746 EXPECT_EQ("#line 42 \"test\"\n", 747 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 748 EXPECT_EQ("#define A B\n", 749 format("# \\\n define \\\n A \\\n B\n", 750 getLLVMStyleWithColumns(12))); 751} 752 753TEST_F(FormatTest, EndOfFileEndsPPDirective) { 754 EXPECT_EQ("#line 42 \"test\"", 755 format("# \\\n line \\\n 42 \\\n \"test\"")); 756 EXPECT_EQ("#define A B", 757 format("# \\\n define \\\n A \\\n B")); 758} 759 760TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 761 // If the macro fits in one line, we still do not get the full 762 // line, as only the next line decides whether we need an escaped newline and 763 // thus use the last column. 764 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); 765 766 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); 767 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); 768 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 769 770 verifyFormat("#define A A\n#define A A"); 771 verifyFormat("#define A(X) A\n#define A A"); 772 773 verifyFormat("#define Something Other", getLLVMStyleWithColumns(24)); 774 verifyFormat("#define Something \\\n" 775 " Other", getLLVMStyleWithColumns(23)); 776} 777 778TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 779 EXPECT_EQ("// some comment\n" 780 "#include \"a.h\"\n" 781 "#define A(A,\\\n" 782 " B)\n" 783 "#include \"b.h\"\n" 784 "// some comment\n", 785 format(" // some comment\n" 786 " #include \"a.h\"\n" 787 "#define A(A,\\\n" 788 " B)\n" 789 " #include \"b.h\"\n" 790 " // some comment\n", getLLVMStyleWithColumns(13))); 791} 792 793TEST_F(FormatTest, LayoutSingleHash) { 794 EXPECT_EQ("#\na;", format("#\na;")); 795} 796 797TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 798 EXPECT_EQ("#define A \\\n" 799 " c; \\\n" 800 " e;\n" 801 "f;", format("#define A c; e;\n" 802 "f;", getLLVMStyleWithColumns(14))); 803} 804 805TEST_F(FormatTest, LayoutRemainingTokens) { 806 EXPECT_EQ("{}", format("{}")); 807} 808 809TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { 810 EXPECT_EQ("# define A\\\n b;", 811 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); 812} 813 814TEST_F(FormatTest, MacroDefinitionInsideStatement) { 815 EXPECT_EQ("int x,\n" 816 "#define A\n" 817 " y;", format("int x,\n#define A\ny;")); 818} 819 820TEST_F(FormatTest, HashInMacroDefinition) { 821 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 822 verifyFormat("#define A \\\n" 823 " { \\\n" 824 " f(#c);\\\n" 825 " }", getLLVMStyleWithColumns(11)); 826 827 verifyFormat("#define A(X) \\\n" 828 " void function##X()", getLLVMStyleWithColumns(22)); 829 830 verifyFormat("#define A(a, b, c) \\\n" 831 " void a##b##c()", getLLVMStyleWithColumns(22)); 832 833 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 834} 835 836TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 837 verifyFormat("#define A (1)"); 838} 839 840TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 841 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 842} 843 844TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 845 verifyFormat("{\n { a #c; }\n}"); 846} 847 848TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 849 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 850 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 851 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 852 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 853} 854 855TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { 856 EXPECT_EQ( 857 "#define A \\\n int i; \\\n int j;", 858 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 859} 860 861TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 862 verifyFormat("#define A \\\n" 863 " int v( \\\n" 864 " a); \\\n" 865 " int i;", getLLVMStyleWithColumns(11)); 866} 867 868TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 869 EXPECT_EQ( 870 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 871 " \\\n" 872 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 873 "\n" 874 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 875 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 876 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 877 "\\\n" 878 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 879 " \n" 880 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 881 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 882} 883 884TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 885 EXPECT_EQ("int\n" 886 "#define A\n" 887 " a;", 888 format("int\n#define A\na;")); 889 verifyFormat( 890 "functionCallTo(\n" 891 " someOtherFunction(\n" 892 " withSomeParameters, whichInSequence,\n" 893 " areLongerThanALine(andAnotherCall,\n" 894 "#define A B\n" 895 " withMoreParamters,\n" 896 " whichStronglyInfluenceTheLayout),\n" 897 " andMoreParameters), trailing);", 898 getLLVMStyleWithColumns(69)); 899} 900 901TEST_F(FormatTest, LayoutBlockInsideParens) { 902 EXPECT_EQ("functionCall({\n" 903 " int i;\n" 904 "});", format(" functionCall ( {int i;} );")); 905} 906 907TEST_F(FormatTest, LayoutBlockInsideStatement) { 908 EXPECT_EQ("SOME_MACRO { int i; }\n" 909 "int i;", format(" SOME_MACRO {int i;} int i;")); 910} 911 912TEST_F(FormatTest, LayoutNestedBlocks) { 913 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 914 " struct s {\n" 915 " int i;\n" 916 " };\n" 917 " s kBitsToOs[] = { { 10 } };\n" 918 " for (int i = 0; i < 10; ++i)\n" 919 " return;\n" 920 "}"); 921} 922 923TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 924 EXPECT_EQ("{}", format("{}")); 925 926 // Negative test for enum. 927 verifyFormat("enum E {\n};"); 928 929 // Note that when there's a missing ';', we still join... 930 verifyFormat("enum E {}"); 931} 932 933//===----------------------------------------------------------------------===// 934// Line break tests. 935//===----------------------------------------------------------------------===// 936 937TEST_F(FormatTest, FormatsFunctionDefinition) { 938 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," 939 " int h, int j, int f,\n" 940 " int c, int ddddddddddddd) {\n}"); 941} 942 943TEST_F(FormatTest, FormatsAwesomeMethodCall) { 944 verifyFormat( 945 "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 946 " parameter, parameter, parameter)),\n" 947 " SecondLongCall(parameter));"); 948} 949 950TEST_F(FormatTest, PreventConfusingIndents) { 951 verifyFormat( 952 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 953 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 954 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 955 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 956 verifyFormat( 957 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n" 958 " aaaaaaaaaaaaaaaaaaaaaaaa[\n" 959 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n" 960 " aaaaaaaaaaaaaaaaaaaaaaaa];"); 961 verifyFormat( 962 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 963 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 964 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 965 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 966 verifyFormat("int a = bbbb && ccc && fffff(\n" 967 "#define A Just forcing a new line\n" 968 " ddd);"); 969} 970 971TEST_F(FormatTest, ConstructorInitializers) { 972 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 973 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 974 getLLVMStyleWithColumns(45)); 975 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}", 976 getLLVMStyleWithColumns(44)); 977 verifyFormat("Constructor()\n" 978 " : Inttializer(FitsOnTheLine) {\n}", 979 getLLVMStyleWithColumns(43)); 980 981 verifyFormat( 982 "SomeClass::Constructor()\n" 983 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}"); 984 985 verifyFormat( 986 "SomeClass::Constructor()\n" 987 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 988 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); 989 verifyGoogleFormat( 990 "SomeClass::Constructor()\n" 991 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 992 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 993 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); 994 verifyGoogleFormat( 995 "SomeClass::Constructor()\n" 996 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 997 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 998 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); 999 1000 verifyFormat( 1001 "SomeClass::Constructor()\n" 1002 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1003 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}"); 1004 1005 verifyFormat("Constructor()\n" 1006 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1007 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1008 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1009 " aaaaaaaaaaaaaaaaaaaaaaa() {\n}"); 1010 1011 verifyFormat("Constructor()\n" 1012 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1014 1015 // Here a line could be saved by splitting the second initializer onto two 1016 // lines, but that is not desireable. 1017 verifyFormat( 1018 "Constructor()\n" 1019 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 1020 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 1021 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1022 1023 verifyGoogleFormat("MyClass::MyClass(int var)\n" 1024 " : some_var_(var), // 4 space indent\n" 1025 " some_other_var_(var + 1) { // lined up\n" 1026 "}"); 1027 1028 // This test takes VERY long when memoization is broken. 1029 std::string input = "Constructor()\n" 1030 " : aaaa(a,\n"; 1031 for (unsigned i = 0, e = 80; i != e; ++i) { 1032 input += " a,\n"; 1033 } 1034 input += " a) {\n}"; 1035 verifyGoogleFormat(input); 1036} 1037 1038TEST_F(FormatTest, BreaksAsHighAsPossible) { 1039 verifyFormat( 1040 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 1041 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 1042 " f();"); 1043} 1044 1045TEST_F(FormatTest, BreaksDesireably) { 1046 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 1047 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 1048 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 1049 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1051 "}"); 1052 1053 verifyFormat( 1054 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1056 1057 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1060 1061 verifyFormat( 1062 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 1064 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 1066 1067 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1068 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1069 1070 verifyFormat( 1071 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1073 verifyFormat( 1074 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1075 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1076 verifyFormat( 1077 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 1079 1080 // This test case breaks on an incorrect memoization, i.e. an optimization not 1081 // taking into account the StopAt value. 1082 verifyFormat( 1083 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1084 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1085 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 1086 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1087 1088 verifyFormat("{\n {\n {\n" 1089 " Annotation.SpaceRequiredBefore =\n" 1090 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 1091 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 1092 " }\n }\n}"); 1093} 1094 1095TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 1096 verifyGoogleFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 1097 " aaaaaaaaaaaaaaaaaaaa,\n" 1098 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);"); 1099 verifyGoogleFormat( 1100 "aaaaaaa(aaaaaaaaaaaaa,\n" 1101 " aaaaaaaaaaaaa,\n" 1102 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); 1103 verifyGoogleFormat( 1104 "aaaaaaaa(aaaaaaaaaaaaa,\n" 1105 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 1107 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1108 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 1109 verifyGoogleFormat( 1110 "aaaaaaaaaaaaaaa(\n" 1111 " aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();"); 1112 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1113 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);"); 1114 1115 verifyGoogleFormat( 1116 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1117 " aaaaaaaaaaaa,\n" 1118 " aaaaaaaaaaaa);"); 1119 verifyGoogleFormat( 1120 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 1121 " ddddddddddddddddddddddddddddd),\n" 1122 " test);"); 1123 1124 verifyGoogleFormat( 1125 "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 1126 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 1127 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;"); 1128 verifyGoogleFormat("a(\"a\"\n" 1129 " \"a\",\n" 1130 " a);"); 1131 1132 FormatStyle Style = getGoogleStyle(); 1133 Style.AllowAllParametersOfDeclarationOnNextLine = false; 1134 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 1135 " aaaaaaaaa,\n" 1136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 1137 Style); 1138 verifyFormat("void f() {\n" 1139 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 1140 " aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa).aaaaaaa();\n" 1141 "}", 1142 Style); 1143} 1144 1145TEST_F(FormatTest, FormatsBuilderPattern) { 1146 verifyFormat( 1147 "return llvm::StringSwitch<Reference::Kind>(name)\n" 1148 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 1149 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" 1150 " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" 1151 " .Default(ORDER_TEXT);\n"); 1152} 1153 1154TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { 1155 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1156 " GUARDED_BY(aaaaaaaaaaaaa);"); 1157 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 1158 " GUARDED_BY(aaaaaaaaaaaaa);"); 1159 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 1160 " GUARDED_BY(aaaaaaaaaaaaa) {\n}"); 1161} 1162 1163TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 1164 verifyFormat( 1165 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1166 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 1167 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 1168 " ccccccccccccccccccccccccc) {\n}"); 1169 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 1170 " ccccccccccccccccccccccccc) {\n}"); 1171 verifyFormat( 1172 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 1173 " ccccccccccccccccccccccccc) {\n}"); 1174} 1175 1176TEST_F(FormatTest, BreaksAfterAssignments) { 1177 verifyFormat( 1178 "unsigned Cost =\n" 1179 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 1180 " SI->getPointerAddressSpaceee());\n"); 1181 verifyFormat( 1182 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 1183 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 1184 1185 verifyFormat( 1186 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" 1187 " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 1188} 1189 1190TEST_F(FormatTest, AlignsAfterAssignments) { 1191 verifyFormat( 1192 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1193 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1194 verifyFormat( 1195 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1196 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1197 verifyFormat( 1198 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1199 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1200 verifyFormat( 1201 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1202 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 1203 verifyFormat( 1204 "double LooooooooooooooooooooooooongResult =\n" 1205 " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" 1206 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 1207} 1208 1209TEST_F(FormatTest, AlignsAfterReturn) { 1210 verifyFormat( 1211 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1212 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 1213 verifyFormat( 1214 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 1215 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 1216} 1217 1218TEST_F(FormatTest, BreaksConditionalExpressions) { 1219 verifyFormat( 1220 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 1221 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1222 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1223 verifyFormat( 1224 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1225 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1226 verifyFormat( 1227 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 1228 " : aaaaaaaaaaaaa);"); 1229 verifyFormat( 1230 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1231 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaa\n" 1232 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1233 " aaaaaaaaaaaaa);"); 1234 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1235 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1236 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1237 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1238 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1239 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1240 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1241 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1242 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1243 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1244 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 1245 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1246} 1247 1248TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 1249 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 1250 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 1251 verifyFormat("bool a = true, b = false;"); 1252 1253 // FIXME: Indentation looks weird. 1254 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 1256 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 1257 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 1258 1259 // FIXME: This is bad as we hide "d". 1260 verifyFormat( 1261 "bool aaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 1262 " cccccccccccccccccccccccccccc, d = e && f;"); 1263 1264} 1265 1266TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 1267 verifyFormat("arr[foo ? bar : baz];"); 1268 verifyFormat("f()[foo ? bar : baz];"); 1269 verifyFormat("(a + b)[foo ? bar : baz];"); 1270 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 1271} 1272 1273TEST_F(FormatTest, AlignsStringLiterals) { 1274 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 1275 " \"short literal\");"); 1276 verifyFormat( 1277 "looooooooooooooooooooooooongFunction(\n" 1278 " \"short literal\"\n" 1279 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 1280} 1281 1282TEST_F(FormatTest, AlignsPipes) { 1283 verifyFormat( 1284 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1285 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1286 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1287 verifyFormat( 1288 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 1289 " << aaaaaaaaaaaaaaaaaaaa;"); 1290 verifyFormat( 1291 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1292 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1293 verifyFormat( 1294 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1295 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 1296 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 1297 verifyFormat( 1298 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1299 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 1300 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1301 1302 verifyFormat("return out << \"somepacket = {\\n\"\n" 1303 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 1304 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 1305 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 1306 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 1307 " << \"}\";"); 1308} 1309 1310TEST_F(FormatTest, UnderstandsEquals) { 1311 verifyFormat( 1312 "aaaaaaaaaaaaaaaaa =\n" 1313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 1314 verifyFormat( 1315 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1317 verifyFormat( 1318 "if (a) {\n" 1319 " f();\n" 1320 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1322 "}"); 1323 1324 verifyFormat( 1325 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 1326 " 100000000 + 10000000) {\n}"); 1327} 1328 1329TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 1330 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 1331 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 1332 1333 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 1334 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 1335 1336 verifyFormat( 1337 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 1338 " Parameter2);"); 1339 1340 verifyFormat( 1341 "ShortObject->shortFunction(\n" 1342 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 1343 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 1344 1345 verifyFormat("loooooooooooooongFunction(\n" 1346 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 1347 1348 verifyFormat( 1349 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 1350 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 1351 1352 // Here, it is not necessary to wrap at "." or "->". 1353 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 1354 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1355 verifyFormat( 1356 "aaaaaaaaaaa->aaaaaaaaa(\n" 1357 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1358 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 1359} 1360 1361TEST_F(FormatTest, WrapsTemplateDeclarations) { 1362 verifyFormat("template <typename T>\n" 1363 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 1364 verifyFormat( 1365 "template <typename T>\n" 1366 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 1367 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 1368 verifyFormat( 1369 "template <typename T>\n" 1370 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 1371 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 1372 verifyFormat( 1373 "template <typename T>\n" 1374 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 1375 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 1376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1377 verifyFormat("template <typename T>\n" 1378 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1379 " int aaaaaaaaaaaaaaaaa);"); 1380 verifyFormat( 1381 "template <typename T1, typename T2 = char, typename T3 = char,\n" 1382 " typename T4 = char>\n" 1383 "void f();"); 1384 verifyFormat( 1385 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 1386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1387 1388 verifyFormat( 1389 "a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 1390 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); 1391} 1392 1393TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 1394 verifyFormat( 1395 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1396 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 1397 verifyFormat( 1398 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1400 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 1401 1402 // FIXME: Should we have an extra indent after the second break? 1403 verifyFormat( 1404 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 1407 1408 // FIXME: Look into whether we should indent 4 from the start or 4 from 1409 // "bbbbb..." here instead of what we are doing now. 1410 verifyFormat( 1411 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 1412 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 1413 1414 // Breaking at nested name specifiers is generally not desirable. 1415 verifyFormat( 1416 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1417 " aaaaaaaaaaaaaaaaaaaaaaa);"); 1418 1419 verifyFormat( 1420 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 1421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1422 " aaaaaaaaaaaaaaaaaaaaa);", 1423 getLLVMStyleWithColumns(74)); 1424} 1425 1426TEST_F(FormatTest, UnderstandsTemplateParameters) { 1427 verifyFormat("A<int> a;"); 1428 verifyFormat("A<A<A<int> > > a;"); 1429 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 1430 verifyFormat("bool x = a < 1 || 2 > a;"); 1431 verifyFormat("bool x = 5 < f<int>();"); 1432 verifyFormat("bool x = f<int>() > 5;"); 1433 verifyFormat("bool x = 5 < a<int>::x;"); 1434 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 1435 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 1436 1437 verifyGoogleFormat("A<A<int>> a;"); 1438 verifyGoogleFormat("A<A<A<int>>> a;"); 1439 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 1440 1441 verifyFormat("test >> a >> b;"); 1442 verifyFormat("test << a >> b;"); 1443 1444 verifyFormat("f<int>();"); 1445 verifyFormat("template <typename T> void f() {}"); 1446} 1447 1448TEST_F(FormatTest, UnderstandsUnaryOperators) { 1449 verifyFormat("int a = -2;"); 1450 verifyFormat("f(-1, -2, -3);"); 1451 verifyFormat("a[-1] = 5;"); 1452 verifyFormat("int a = 5 + -2;"); 1453 verifyFormat("if (i == -1) {\n}"); 1454 verifyFormat("if (i != -1) {\n}"); 1455 verifyFormat("if (i > -1) {\n}"); 1456 verifyFormat("if (i < -1) {\n}"); 1457 verifyFormat("++(a->f());"); 1458 verifyFormat("--(a->f());"); 1459 verifyFormat("(a->f())++;"); 1460 verifyFormat("a[42]++;"); 1461 verifyFormat("if (!(a->f())) {\n}"); 1462 1463 verifyFormat("a-- > b;"); 1464 verifyFormat("b ? -a : c;"); 1465 verifyFormat("n * sizeof char16;"); 1466 verifyFormat("n * alignof char16;"); 1467 verifyFormat("sizeof(char);"); 1468 verifyFormat("alignof(char);"); 1469 1470 verifyFormat("return -1;"); 1471 verifyFormat("switch (a) {\n" 1472 "case -1:\n" 1473 " break;\n" 1474 "}"); 1475 1476 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); 1477 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); 1478 1479 verifyFormat("int a = /* confusing comment */ -1;"); 1480 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 1481 verifyFormat("int a = i /* confusing comment */++;"); 1482} 1483 1484TEST_F(FormatTest, UndestandsOverloadedOperators) { 1485 verifyFormat("bool operator<();"); 1486 verifyFormat("bool operator>();"); 1487 verifyFormat("bool operator=();"); 1488 verifyFormat("bool operator==();"); 1489 verifyFormat("bool operator!=();"); 1490 verifyFormat("int operator+();"); 1491 verifyFormat("int operator++();"); 1492 verifyFormat("bool operator();"); 1493 verifyFormat("bool operator()();"); 1494 verifyFormat("bool operator[]();"); 1495 verifyFormat("operator bool();"); 1496 verifyFormat("operator SomeType<int>();"); 1497 verifyFormat("void *operator new(std::size_t size);"); 1498 verifyFormat("void *operator new[](std::size_t size);"); 1499 verifyFormat("void operator delete(void *ptr);"); 1500 verifyFormat("void operator delete[](void *ptr);"); 1501} 1502 1503TEST_F(FormatTest, UnderstandsNewAndDelete) { 1504 verifyFormat("A *a = new A;"); 1505 verifyFormat("A *a = new (placement) A;"); 1506 verifyFormat("delete a;"); 1507 verifyFormat("delete (A *)a;"); 1508} 1509 1510TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 1511 verifyFormat("int *f(int *a) {}"); 1512 verifyFormat("int main(int argc, char **argv) {}"); 1513 verifyIndependentOfContext("f(a, *a);"); 1514 verifyIndependentOfContext("f(*a);"); 1515 verifyIndependentOfContext("int a = b * 10;"); 1516 verifyIndependentOfContext("int a = 10 * b;"); 1517 verifyIndependentOfContext("int a = b * c;"); 1518 verifyIndependentOfContext("int a += b * c;"); 1519 verifyIndependentOfContext("int a -= b * c;"); 1520 verifyIndependentOfContext("int a *= b * c;"); 1521 verifyIndependentOfContext("int a /= b * c;"); 1522 verifyIndependentOfContext("int a = *b;"); 1523 verifyIndependentOfContext("int a = *b * c;"); 1524 verifyIndependentOfContext("int a = b * *c;"); 1525 verifyIndependentOfContext("return 10 * b;"); 1526 verifyIndependentOfContext("return *b * *c;"); 1527 verifyIndependentOfContext("return a & ~b;"); 1528 verifyIndependentOfContext("f(b ? *c : *d);"); 1529 verifyIndependentOfContext("int a = b ? *c : *d;"); 1530 verifyIndependentOfContext("*b = a;"); 1531 verifyIndependentOfContext("a * ~b;"); 1532 verifyIndependentOfContext("a * !b;"); 1533 verifyIndependentOfContext("a * +b;"); 1534 verifyIndependentOfContext("a * -b;"); 1535 verifyIndependentOfContext("a * ++b;"); 1536 verifyIndependentOfContext("a * --b;"); 1537 verifyIndependentOfContext("a[4] * b;"); 1538 verifyIndependentOfContext("f() * b;"); 1539 verifyIndependentOfContext("a * [self dostuff];"); 1540 verifyIndependentOfContext("a * (a + b);"); 1541 verifyIndependentOfContext("(a *)(a + b);"); 1542 verifyIndependentOfContext("int *pa = (int *)&a;"); 1543 1544 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 1545 1546 verifyIndependentOfContext("A<int *> a;"); 1547 verifyIndependentOfContext("A<int **> a;"); 1548 verifyIndependentOfContext("A<int *, int *> a;"); 1549 verifyIndependentOfContext( 1550 "const char *const p = reinterpret_cast<const char *const>(q);"); 1551 verifyIndependentOfContext("A<int **, int **> a;"); 1552 1553 verifyFormat( 1554 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1556 1557 verifyGoogleFormat("int main(int argc, char** argv) {}"); 1558 verifyGoogleFormat("A<int*> a;"); 1559 verifyGoogleFormat("A<int**> a;"); 1560 verifyGoogleFormat("A<int*, int*> a;"); 1561 verifyGoogleFormat("A<int**, int**> a;"); 1562 verifyGoogleFormat("f(b ? *c : *d);"); 1563 verifyGoogleFormat("int a = b ? *c : *d;"); 1564 verifyGoogleFormat("Type* t = **x;"); 1565 verifyGoogleFormat("Type* t = *++*x;"); 1566 verifyGoogleFormat("*++*x;"); 1567 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 1568 verifyGoogleFormat("Type* t = x++ * y;"); 1569 verifyGoogleFormat( 1570 "const char* const p = reinterpret_cast<const char* const>(q);"); 1571 1572 verifyIndependentOfContext("a = *(x + y);"); 1573 verifyIndependentOfContext("a = &(x + y);"); 1574 verifyIndependentOfContext("*(x + y).call();"); 1575 verifyIndependentOfContext("&(x + y)->call();"); 1576 verifyIndependentOfContext("&(*I).first"); 1577 1578 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 1579 verifyFormat( 1580 "int *MyValues = {\n" 1581 " *A, // Operator detection might be confused by the '{'\n" 1582 " *BB // Operator detection might be confused by previous comment\n" 1583 "};"); 1584 1585 verifyIndependentOfContext("if (int *a = &b)"); 1586 verifyIndependentOfContext("if (int &a = *b)"); 1587 verifyIndependentOfContext("if (a & b[i])"); 1588 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 1589 verifyIndependentOfContext("if (*b[i])"); 1590 verifyIndependentOfContext("if (int *a = (&b))"); 1591 verifyIndependentOfContext("while (int *a = &b)"); 1592 verifyFormat("void f() {\n" 1593 " for (const int &v : Values) {\n" 1594 " }\n" 1595 "}"); 1596 1597 verifyIndependentOfContext("A = new SomeType *[Length]();"); 1598 verifyGoogleFormat("A = new SomeType* [Length]();"); 1599} 1600 1601TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 1602 verifyFormat("void f() {\n" 1603 " x[aaaaaaaaa -\n" 1604 " b] = 23;\n" 1605 "}", getLLVMStyleWithColumns(15)); 1606} 1607 1608TEST_F(FormatTest, FormatsCasts) { 1609 verifyFormat("Type *A = static_cast<Type *>(P);"); 1610 verifyFormat("Type *A = (Type *)P;"); 1611 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 1612 verifyFormat("int a = (int)(2.0f);"); 1613 1614 // FIXME: These also need to be identified. 1615 verifyFormat("int a = (int) 2.0f;"); 1616 verifyFormat("int a = (int) * b;"); 1617 1618 // These are not casts. 1619 verifyFormat("void f(int *) {}"); 1620 verifyFormat("void f(int *);"); 1621 verifyFormat("void f(int *) = 0;"); 1622 verifyFormat("void f(SmallVector<int>) {}"); 1623 verifyFormat("void f(SmallVector<int>);"); 1624 verifyFormat("void f(SmallVector<int>) = 0;"); 1625} 1626 1627TEST_F(FormatTest, FormatsFunctionTypes) { 1628 // FIXME: Determine the cases that need a space after the return type and fix. 1629 verifyFormat("A<bool()> a;"); 1630 verifyFormat("A<SomeType()> a;"); 1631 verifyFormat("A<void(*)(int, std::string)> a;"); 1632 1633 verifyFormat("int(*func)(void *);"); 1634} 1635 1636TEST_F(FormatTest, BreaksFunctionDeclarations) { 1637 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 1638 " int LoooooooooooooooooooongParam2) {\n}"); 1639 verifyFormat( 1640 "TypeSpecDecl *\n" 1641 "TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n" 1642 " IdentifierIn *II, Type *T) {\n}"); 1643 verifyGoogleFormat( 1644 "TypeSpecDecl* TypeSpecDecl::Create(\n" 1645 " ASTContext& C, DeclContext* DC, SourceLocation L) {\n}"); 1646} 1647 1648TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 1649 verifyFormat("(a)->b();"); 1650 verifyFormat("--a;"); 1651} 1652 1653TEST_F(FormatTest, HandlesIncludeDirectives) { 1654 verifyFormat("#include <string>\n" 1655 "#include <a/b/c.h>\n" 1656 "#include \"a/b/string\"\n" 1657 "#include \"string.h\"\n" 1658 "#include \"string.h\"\n" 1659 "#include <a-a>\n" 1660 "#include < path with space >\n"); 1661 1662 verifyFormat("#import <string>"); 1663 verifyFormat("#import <a/b/c.h>"); 1664 verifyFormat("#import \"a/b/string\""); 1665 verifyFormat("#import \"string.h\""); 1666 verifyFormat("#import \"string.h\""); 1667} 1668 1669//===----------------------------------------------------------------------===// 1670// Error recovery tests. 1671//===----------------------------------------------------------------------===// 1672 1673TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 1674 verifyFormat("void f() { return; }\n42"); 1675 verifyFormat("void f() {\n" 1676 " if (0)\n" 1677 " return;\n" 1678 "}\n" 1679 "42"); 1680 verifyFormat("void f() { return }\n42"); 1681 verifyFormat("void f() {\n" 1682 " if (0)\n" 1683 " return\n" 1684 "}\n" 1685 "42"); 1686} 1687 1688TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 1689 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 1690 EXPECT_EQ("void f() {\n" 1691 " if (a)\n" 1692 " return\n" 1693 "}", format("void f ( ) { if ( a ) return }")); 1694 EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); 1695 EXPECT_EQ("namespace N {\n" 1696 "void f() {}\n" 1697 "void g()\n" 1698 "}", format("namespace N { void f( ) { } void g( ) }")); 1699} 1700 1701TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 1702 verifyFormat("int aaaaaaaa =\n" 1703 " // Overly long comment\n" 1704 " b;", getLLVMStyleWithColumns(20)); 1705 verifyFormat("function(\n" 1706 " ShortArgument,\n" 1707 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20)); 1708} 1709 1710TEST_F(FormatTest, IncorrectAccessSpecifier) { 1711 verifyFormat("public:"); 1712 verifyFormat("class A {\n" 1713 "public\n" 1714 " void f() {}\n" 1715 "};"); 1716 verifyFormat("public\n" 1717 "int qwerty;"); 1718 verifyFormat("public\n" 1719 "B {}"); 1720 verifyFormat("public\n" 1721 "{}"); 1722 verifyFormat("public\n" 1723 "B { int x; }"); 1724} 1725 1726TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 1727 verifyFormat("{"); 1728} 1729 1730TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 1731 verifyFormat("do {\n}"); 1732 verifyFormat("do {\n}\n" 1733 "f();"); 1734 verifyFormat("do {\n}\n" 1735 "wheeee(fun);"); 1736 verifyFormat("do {\n" 1737 " f();\n" 1738 "}"); 1739} 1740 1741TEST_F(FormatTest, IncorrectCodeMissingParens) { 1742 verifyFormat("if {\n foo;\n foo();\n}"); 1743 verifyFormat("switch {\n foo;\n foo();\n}"); 1744 verifyFormat("for {\n foo;\n foo();\n}"); 1745 verifyFormat("while {\n foo;\n foo();\n}"); 1746 verifyFormat("do {\n foo;\n foo();\n} while;"); 1747} 1748 1749TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 1750 verifyFormat("namespace {\n" 1751 "class Foo { Foo ( }; } // comment"); 1752} 1753 1754TEST_F(FormatTest, IncorrectCodeErrorDetection) { 1755 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); 1756 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 1757 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 1758 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); 1759 1760 EXPECT_EQ("{\n" 1761 " {\n" 1762 " breakme(\n" 1763 " qwe);\n" 1764 "}\n", format("{\n" 1765 " {\n" 1766 " breakme(qwe);\n" 1767 "}\n", getLLVMStyleWithColumns(10))); 1768} 1769 1770TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 1771 verifyFormat( 1772 "int x = {\n" 1773 " avariable,\n" 1774 " b(alongervariable)\n" 1775 "};", getLLVMStyleWithColumns(25)); 1776} 1777 1778TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 1779 verifyFormat("return (a)(b) { 1, 2, 3 };"); 1780} 1781 1782TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { 1783 verifyFormat( 1784 "Aaa({\n" 1785 " int i;\n" 1786 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1787 " ccccccccccccccccc));"); 1788} 1789 1790TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 1791 verifyFormat("void f() { return 42; }"); 1792 verifyFormat("void f() {\n" 1793 " // Comment\n" 1794 "}"); 1795 verifyFormat("{\n" 1796 "#error {\n" 1797 " int a;\n" 1798 "}"); 1799 verifyFormat("{\n" 1800 " int a;\n" 1801 "#error {\n" 1802 "}"); 1803 1804 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 1805 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 1806 1807 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 1808 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 1809} 1810 1811TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 1812 // Elaborate type variable declarations. 1813 verifyFormat("struct foo a = { bar };\nint n;"); 1814 verifyFormat("class foo a = { bar };\nint n;"); 1815 verifyFormat("union foo a = { bar };\nint n;"); 1816 1817 // Elaborate types inside function definitions. 1818 verifyFormat("struct foo f() {}\nint n;"); 1819 verifyFormat("class foo f() {}\nint n;"); 1820 verifyFormat("union foo f() {}\nint n;"); 1821 1822 // Templates. 1823 verifyFormat("template <class X> void f() {}\nint n;"); 1824 verifyFormat("template <struct X> void f() {}\nint n;"); 1825 verifyFormat("template <union X> void f() {}\nint n;"); 1826 1827 // Actual definitions... 1828 verifyFormat("struct {\n} n;"); 1829 verifyFormat( 1830 "template <template <class T, class Y>, class Z> class X {\n} n;"); 1831 verifyFormat("union Z {\n int n;\n} x;"); 1832 verifyFormat("class MACRO Z {\n} n;"); 1833 verifyFormat("class MACRO(X) Z {\n} n;"); 1834 verifyFormat("class __attribute__(X) Z {\n} n;"); 1835 verifyFormat("class __declspec(X) Z {\n} n;"); 1836 1837 // Redefinition from nested context: 1838 verifyFormat("class A::B::C {\n} n;"); 1839 1840 // Template definitions. 1841 // FIXME: This is still incorrectly handled at the formatter side. 1842 verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); 1843 1844 // FIXME: 1845 // This now gets parsed incorrectly as class definition. 1846 // verifyFormat("class A<int> f() {\n}\nint n;"); 1847 1848 // Elaborate types where incorrectly parsing the structural element would 1849 // break the indent. 1850 verifyFormat("if (true)\n" 1851 " class X x;\n" 1852 "else\n" 1853 " f();\n"); 1854} 1855 1856TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 1857 verifyFormat("#error Leave all white!!!!! space* alone!\n"); 1858 verifyFormat("#warning Leave all white!!!!! space* alone!\n"); 1859 EXPECT_EQ("#error 1", format(" # error 1")); 1860 EXPECT_EQ("#warning 1", format(" # warning 1")); 1861} 1862 1863TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 1864 FormatStyle AllowsMergedIf = getGoogleStyle(); 1865 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 1866 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 1867 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 1868 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 1869 EXPECT_EQ("if (true) return 42;", 1870 format("if (true)\nreturn 42;", AllowsMergedIf)); 1871 FormatStyle ShortMergedIf = AllowsMergedIf; 1872 ShortMergedIf.ColumnLimit = 25; 1873 verifyFormat("#define A \\\n" 1874 " if (true) return 42;", ShortMergedIf); 1875 verifyFormat("#define A \\\n" 1876 " f(); \\\n" 1877 " if (true)\n" 1878 "#define B", ShortMergedIf); 1879 verifyFormat("#define A \\\n" 1880 " f(); \\\n" 1881 " if (true)\n" 1882 "g();", ShortMergedIf); 1883 verifyFormat("{\n" 1884 "#ifdef A\n" 1885 " // Comment\n" 1886 " if (true) continue;\n" 1887 "#endif\n" 1888 " // Comment\n" 1889 " if (true) continue;", ShortMergedIf); 1890} 1891 1892TEST_F(FormatTest, BlockCommentsInControlLoops) { 1893 verifyFormat("if (0) /* a comment in a strange place */ {\n" 1894 " f();\n" 1895 "}"); 1896 verifyFormat("if (0) /* a comment in a strange place */ {\n" 1897 " f();\n" 1898 "} /* another comment */ else /* comment #3 */ {\n" 1899 " g();\n" 1900 "}"); 1901 verifyFormat("while (0) /* a comment in a strange place */ {\n" 1902 " f();\n" 1903 "}"); 1904 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 1905 " f();\n" 1906 "}"); 1907 verifyFormat("do /* a comment in a strange place */ {\n" 1908 " f();\n" 1909 "} /* another comment */ while (0);"); 1910} 1911 1912TEST_F(FormatTest, BlockComments) { 1913 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 1914 format("/* *//* */ /* */\n/* *//* */ /* */")); 1915 EXPECT_EQ("/* */ a /* */ b;", 1916 format(" /* */ a/* */ b;")); 1917 EXPECT_EQ("#define A /* */\\\n" 1918 " b\n" 1919 "/* */\n" 1920 "someCall(\n" 1921 " parameter);", 1922 format("#define A /* */ b\n" 1923 "/* */\n" 1924 "someCall(parameter);", getLLVMStyleWithColumns(15))); 1925 1926 EXPECT_EQ("#define A\n" 1927 "/* */ someCall(\n" 1928 " parameter);", 1929 format("#define A\n" 1930 "/* */someCall(parameter);", getLLVMStyleWithColumns(15))); 1931 1932 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 1933 " 2, /* comment 2 */\n" 1934 " 3, /* comment 3 */\n" 1935 " aaaa,\n" 1936 " bbbb);", 1937 format("someFunction (1, /* comment 1 */\n" 1938 " 2, /* comment 2 */ \n" 1939 " 3, /* comment 3 */\n" 1940 "aaaa, bbbb );", getGoogleStyle())); 1941} 1942 1943TEST_F(FormatTest, FormatStarDependingOnContext) { 1944 verifyFormat("void f(int *a);"); 1945 verifyFormat("void f() { f(fint * b); }"); 1946 verifyFormat("class A {\n void f(int *a);\n};"); 1947 verifyFormat("class A {\n int *a;\n};"); 1948 verifyFormat("namespace a {\n" 1949 "namespace b {\n" 1950 "class A {\n" 1951 " void f() {}\n" 1952 " int *a;\n" 1953 "};\n" 1954 "}\n" 1955 "}"); 1956} 1957 1958TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 1959 verifyFormat("while"); 1960 verifyFormat("operator"); 1961} 1962 1963//===----------------------------------------------------------------------===// 1964// Objective-C tests. 1965//===----------------------------------------------------------------------===// 1966 1967TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 1968 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 1969 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 1970 format("-(NSUInteger)indexOfObject:(id)anObject;")); 1971 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 1972 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 1973 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 1974 format("-(NSInteger)Method3:(id)anObject;")); 1975 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 1976 format("-(NSInteger)Method4:(id)anObject;")); 1977 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 1978 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 1979 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 1980 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 1981 EXPECT_EQ( 1982 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 1983 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 1984 1985 // Very long objectiveC method declaration. 1986 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 1987 " inRange:(NSRange)range\n" 1988 " outRange:(NSRange)out_range\n" 1989 " outRange1:(NSRange)out_range1\n" 1990 " outRange2:(NSRange)out_range2\n" 1991 " outRange3:(NSRange)out_range3\n" 1992 " outRange4:(NSRange)out_range4\n" 1993 " outRange5:(NSRange)out_range5\n" 1994 " outRange6:(NSRange)out_range6\n" 1995 " outRange7:(NSRange)out_range7\n" 1996 " outRange8:(NSRange)out_range8\n" 1997 " outRange9:(NSRange)out_range9;"); 1998 1999 verifyFormat("- (int)sum:(vector<int>)numbers;"); 2000 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 2001 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 2002 // protocol lists (but not for template classes): 2003 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 2004 2005 verifyFormat("- (int(*)())foo:(int(*)())f;"); 2006 verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); 2007 2008 // If there's no return type (very rare in practice!), LLVM and Google style 2009 // agree. 2010 verifyFormat("- foo:(int)f;"); 2011 verifyGoogleFormat("- foo:(int)foo;"); 2012} 2013 2014TEST_F(FormatTest, FormatObjCBlocks) { 2015 verifyFormat("int (^Block)(int, int);"); 2016 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); 2017} 2018 2019TEST_F(FormatTest, FormatObjCInterface) { 2020 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 2021 "@public\n" 2022 " int field1;\n" 2023 "@protected\n" 2024 " int field2;\n" 2025 "@private\n" 2026 " int field3;\n" 2027 "@package\n" 2028 " int field4;\n" 2029 "}\n" 2030 "+ (id)init;\n" 2031 "@end"); 2032 2033 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 2034 " @public\n" 2035 " int field1;\n" 2036 " @protected\n" 2037 " int field2;\n" 2038 " @private\n" 2039 " int field3;\n" 2040 " @package\n" 2041 " int field4;\n" 2042 "}\n" 2043 "+ (id)init;\n" 2044 "@end"); 2045 2046 verifyFormat("@interface /* wait for it */ Foo\n" 2047 "+ (id)init;\n" 2048 "// Look, a comment!\n" 2049 "- (int)answerWith:(int)i;\n" 2050 "@end"); 2051 2052 verifyFormat("@interface Foo\n" 2053 "@end\n" 2054 "@interface Bar\n" 2055 "@end"); 2056 2057 verifyFormat("@interface Foo : Bar\n" 2058 "+ (id)init;\n" 2059 "@end"); 2060 2061 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 2062 "+ (id)init;\n" 2063 "@end"); 2064 2065 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 2066 "+ (id)init;\n" 2067 "@end"); 2068 2069 verifyFormat("@interface Foo (HackStuff)\n" 2070 "+ (id)init;\n" 2071 "@end"); 2072 2073 verifyFormat("@interface Foo ()\n" 2074 "+ (id)init;\n" 2075 "@end"); 2076 2077 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 2078 "+ (id)init;\n" 2079 "@end"); 2080 2081 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 2082 "+ (id)init;\n" 2083 "@end"); 2084 2085 verifyFormat("@interface Foo {\n" 2086 " int _i;\n" 2087 "}\n" 2088 "+ (id)init;\n" 2089 "@end"); 2090 2091 verifyFormat("@interface Foo : Bar {\n" 2092 " int _i;\n" 2093 "}\n" 2094 "+ (id)init;\n" 2095 "@end"); 2096 2097 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 2098 " int _i;\n" 2099 "}\n" 2100 "+ (id)init;\n" 2101 "@end"); 2102 2103 verifyFormat("@interface Foo (HackStuff) {\n" 2104 " int _i;\n" 2105 "}\n" 2106 "+ (id)init;\n" 2107 "@end"); 2108 2109 verifyFormat("@interface Foo () {\n" 2110 " int _i;\n" 2111 "}\n" 2112 "+ (id)init;\n" 2113 "@end"); 2114 2115 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 2116 " int _i;\n" 2117 "}\n" 2118 "+ (id)init;\n" 2119 "@end"); 2120} 2121 2122TEST_F(FormatTest, FormatObjCImplementation) { 2123 verifyFormat("@implementation Foo : NSObject {\n" 2124 "@public\n" 2125 " int field1;\n" 2126 "@protected\n" 2127 " int field2;\n" 2128 "@private\n" 2129 " int field3;\n" 2130 "@package\n" 2131 " int field4;\n" 2132 "}\n" 2133 "+ (id)init {\n}\n" 2134 "@end"); 2135 2136 verifyGoogleFormat("@implementation Foo : NSObject {\n" 2137 " @public\n" 2138 " int field1;\n" 2139 " @protected\n" 2140 " int field2;\n" 2141 " @private\n" 2142 " int field3;\n" 2143 " @package\n" 2144 " int field4;\n" 2145 "}\n" 2146 "+ (id)init {\n}\n" 2147 "@end"); 2148 2149 verifyFormat("@implementation Foo\n" 2150 "+ (id)init {\n" 2151 " if (true)\n" 2152 " return nil;\n" 2153 "}\n" 2154 "// Look, a comment!\n" 2155 "- (int)answerWith:(int)i {\n" 2156 " return i;\n" 2157 "}\n" 2158 "+ (int)answerWith:(int)i {\n" 2159 " return i;\n" 2160 "}\n" 2161 "@end"); 2162 2163 verifyFormat("@implementation Foo\n" 2164 "@end\n" 2165 "@implementation Bar\n" 2166 "@end"); 2167 2168 verifyFormat("@implementation Foo : Bar\n" 2169 "+ (id)init {\n}\n" 2170 "- (void)foo {\n}\n" 2171 "@end"); 2172 2173 verifyFormat("@implementation Foo {\n" 2174 " int _i;\n" 2175 "}\n" 2176 "+ (id)init {\n}\n" 2177 "@end"); 2178 2179 verifyFormat("@implementation Foo : Bar {\n" 2180 " int _i;\n" 2181 "}\n" 2182 "+ (id)init {\n}\n" 2183 "@end"); 2184 2185 verifyFormat("@implementation Foo (HackStuff)\n" 2186 "+ (id)init {\n}\n" 2187 "@end"); 2188} 2189 2190TEST_F(FormatTest, FormatObjCProtocol) { 2191 verifyFormat("@protocol Foo\n" 2192 "@property(weak) id delegate;\n" 2193 "- (NSUInteger)numberOfThings;\n" 2194 "@end"); 2195 2196 verifyFormat("@protocol MyProtocol <NSObject>\n" 2197 "- (NSUInteger)numberOfThings;\n" 2198 "@end"); 2199 2200 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 2201 "- (NSUInteger)numberOfThings;\n" 2202 "@end"); 2203 2204 verifyFormat("@protocol Foo;\n" 2205 "@protocol Bar;\n"); 2206 2207 verifyFormat("@protocol Foo\n" 2208 "@end\n" 2209 "@protocol Bar\n" 2210 "@end"); 2211 2212 verifyFormat("@protocol myProtocol\n" 2213 "- (void)mandatoryWithInt:(int)i;\n" 2214 "@optional\n" 2215 "- (void)optional;\n" 2216 "@required\n" 2217 "- (void)required;\n" 2218 "@optional\n" 2219 "@property(assign) int madProp;\n" 2220 "@end\n"); 2221} 2222 2223TEST_F(FormatTest, FormatObjCMethodDeclarations) { 2224 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 2225 " rect:(NSRect)theRect\n" 2226 " interval:(float)theInterval {\n" 2227 "}"); 2228 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 2229 " longKeyword:(NSRect)theRect\n" 2230 " evenLongerKeyword:(float)theInterval\n" 2231 " error:(NSError **)theError {\n" 2232 "}"); 2233} 2234 2235TEST_F(FormatTest, FormatObjCMethodExpr) { 2236 verifyFormat("[foo bar:baz];"); 2237 verifyFormat("return [foo bar:baz];"); 2238 verifyFormat("f([foo bar:baz]);"); 2239 verifyFormat("f(2, [foo bar:baz]);"); 2240 verifyFormat("f(2, a ? b : c);"); 2241 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 2242 2243 // Unary operators. 2244 verifyFormat("int a = +[foo bar:baz];"); 2245 verifyFormat("int a = -[foo bar:baz];"); 2246 verifyFormat("int a = ![foo bar:baz];"); 2247 verifyFormat("int a = ~[foo bar:baz];"); 2248 verifyFormat("int a = ++[foo bar:baz];"); 2249 verifyFormat("int a = --[foo bar:baz];"); 2250 verifyFormat("int a = sizeof [foo bar:baz];"); 2251 verifyFormat("int a = alignof [foo bar:baz];"); 2252 verifyFormat("int a = &[foo bar:baz];"); 2253 verifyFormat("int a = *[foo bar:baz];"); 2254 // FIXME: Make casts work, without breaking f()[4]. 2255 //verifyFormat("int a = (int) [foo bar:baz];"); 2256 2257 // Binary operators. 2258 verifyFormat("[foo bar:baz], [foo bar:baz];"); 2259 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 2260 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 2261 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 2262 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 2263 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 2264 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 2265 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 2266 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 2267 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 2268 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 2269 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 2270 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 2271 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 2272 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 2273 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 2274 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 2275 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 2276 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 2277 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 2278 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 2279 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 2280 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 2281 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 2282 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 2283 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 2284 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 2285 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 2286 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 2287 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 2288 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 2289 // Whew! 2290 2291 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 2292 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 2293 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 2294 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 2295 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 2296 verifyFormat("[button setAction:@selector(zoomOut:)];"); 2297 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 2298 2299 verifyFormat("arr[[self indexForFoo:a]];"); 2300 verifyFormat("throw [self errorFor:a];"); 2301 verifyFormat("@throw [self errorFor:a];"); 2302 2303 // This tests that the formatter doesn't break after "backing" but before ":", 2304 // which would be at 80 columns. 2305 verifyFormat( 2306 "void f() {\n" 2307 " if ((self = [super initWithContentRect:contentRect\n" 2308 " styleMask:styleMask\n" 2309 " backing:NSBackingStoreBuffered\n" 2310 " defer:YES]))"); 2311 2312 verifyFormat( 2313 "[foo checkThatBreakingAfterColonWorksOk:\n" 2314 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 2315 2316 verifyFormat("[myObj short:arg1 // Force line break\n" 2317 " longKeyword:arg2\n" 2318 " evenLongerKeyword:arg3\n" 2319 " error:arg4];"); 2320 verifyFormat( 2321 "void f() {\n" 2322 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 2323 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 2324 " pos.width(), pos.height())\n" 2325 " styleMask:NSBorderlessWindowMask\n" 2326 " backing:NSBackingStoreBuffered\n" 2327 " defer:NO]);\n" 2328 "}"); 2329 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 2330 " with:contentsNativeView];"); 2331 2332 verifyFormat( 2333 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 2334 " owner:nillllll];"); 2335 2336 // FIXME: No line break necessary for the first nested call. 2337 verifyFormat( 2338 "[pboard setData:[NSData dataWithBytes:&button\n" 2339 " length:sizeof(button)]\n" 2340 " forType:kBookmarkButtonDragType];"); 2341 2342 verifyFormat("[defaultCenter addObserver:self\n" 2343 " selector:@selector(willEnterFullscreen)\n" 2344 " name:kWillEnterFullscreenNotification\n" 2345 " object:nil];"); 2346} 2347 2348TEST_F(FormatTest, ObjCAt) { 2349 verifyFormat("@autoreleasepool"); 2350 verifyFormat("@catch"); 2351 verifyFormat("@class"); 2352 verifyFormat("@compatibility_alias"); 2353 verifyFormat("@defs"); 2354 verifyFormat("@dynamic"); 2355 verifyFormat("@encode"); 2356 verifyFormat("@end"); 2357 verifyFormat("@finally"); 2358 verifyFormat("@implementation"); 2359 verifyFormat("@import"); 2360 verifyFormat("@interface"); 2361 verifyFormat("@optional"); 2362 verifyFormat("@package"); 2363 verifyFormat("@private"); 2364 verifyFormat("@property"); 2365 verifyFormat("@protected"); 2366 verifyFormat("@protocol"); 2367 verifyFormat("@public"); 2368 verifyFormat("@required"); 2369 verifyFormat("@selector"); 2370 verifyFormat("@synchronized"); 2371 verifyFormat("@synthesize"); 2372 verifyFormat("@throw"); 2373 verifyFormat("@try"); 2374 2375 verifyFormat("@\"String\""); 2376 verifyFormat("@1"); 2377 verifyFormat("@+4.8"); 2378 verifyFormat("@-4"); 2379 verifyFormat("@1LL"); 2380 verifyFormat("@.5"); 2381 verifyFormat("@'c'"); 2382 verifyFormat("@true"); 2383 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 2384 // FIXME: Array and dictionary literals need more work. 2385 verifyFormat("@["); 2386 verifyFormat("@{"); 2387 2388 EXPECT_EQ("@interface", format("@ interface")); 2389 2390 // The precise formatting of this doesn't matter, nobody writes code like 2391 // this. 2392 verifyFormat("@ /*foo*/ interface"); 2393} 2394 2395TEST_F(FormatTest, ObjCSnippets) { 2396 verifyFormat("@autoreleasepool {\n" 2397 " foo();\n" 2398 "}"); 2399 verifyFormat("@class Foo, Bar;"); 2400 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 2401 verifyFormat("@dynamic textColor;"); 2402 verifyFormat("char *buf1 = @encode(int *);"); 2403 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 2404 // FIXME: Enable once PR14884 is fixed: 2405 //verifyFormat("char *buf1 = @encode(int **);"); 2406 verifyFormat("Protocol *proto = @protocol(p1);"); 2407 verifyFormat("SEL s = @selector(foo:);"); 2408 verifyFormat("@synchronized(self) {\n" 2409 " f();\n" 2410 "}"); 2411 2412 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 2413 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 2414 2415 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 2416 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 2417 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 2418} 2419 2420} // end namespace tooling 2421} // end namespace clang 2422