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