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