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