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