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