FormatTest.cpp revision 7d19bc2c8882457e833dfe9741ac5a4a7198e46f
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#include "clang/Format/Format.h" 11#include "../Tooling/RewriterTestContext.h" 12#include "clang/Lex/Lexer.h" 13#include "gtest/gtest.h" 14 15namespace clang { 16namespace format { 17 18class FormatTest : public ::testing::Test { 19protected: 20 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, 21 const FormatStyle &Style) { 22 RewriterTestContext Context; 23 FileID ID = Context.createInMemoryFile("input.cc", Code); 24 SourceLocation Start = 25 Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); 26 std::vector<CharSourceRange> Ranges( 27 1, 28 CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); 29 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, 30 getFormattingLangOpts()); 31 tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, 32 Ranges); 33 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); 34 return Context.getRewrittenText(ID); 35 } 36 37 std::string format(llvm::StringRef Code, 38 const FormatStyle &Style = getLLVMStyle()) { 39 return format(Code, 0, Code.size(), Style); 40 } 41 42 std::string messUp(llvm::StringRef Code) { 43 std::string MessedUp(Code.str()); 44 bool InComment = false; 45 bool InPreprocessorDirective = false; 46 bool JustReplacedNewline = false; 47 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { 48 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { 49 if (JustReplacedNewline) 50 MessedUp[i - 1] = '\n'; 51 InComment = true; 52 } else if (MessedUp[i] == '#' && JustReplacedNewline) { 53 MessedUp[i - 1] = '\n'; 54 InPreprocessorDirective = true; 55 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { 56 MessedUp[i] = ' '; 57 MessedUp[i + 1] = ' '; 58 } else if (MessedUp[i] == '\n') { 59 if (InComment) { 60 InComment = false; 61 } else if (InPreprocessorDirective) { 62 InPreprocessorDirective = false; 63 } else { 64 JustReplacedNewline = true; 65 MessedUp[i] = ' '; 66 } 67 } else if (MessedUp[i] != ' ') { 68 JustReplacedNewline = false; 69 } 70 } 71 return MessedUp; 72 } 73 74 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 75 FormatStyle Style = getLLVMStyle(); 76 Style.ColumnLimit = ColumnLimit; 77 return Style; 78 } 79 80 void verifyFormat(llvm::StringRef Code, 81 const FormatStyle &Style = getLLVMStyle()) { 82 EXPECT_EQ(Code.str(), format(messUp(Code), Style)); 83 } 84 85 void verifyGoogleFormat(llvm::StringRef Code) { 86 verifyFormat(Code, getGoogleStyle()); 87 } 88}; 89 90TEST_F(FormatTest, MessUp) { 91 EXPECT_EQ("1 2 3", messUp("1 2 3")); 92 EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); 93 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); 94 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); 95 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); 96} 97 98//===----------------------------------------------------------------------===// 99// Basic function tests. 100//===----------------------------------------------------------------------===// 101 102TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { 103 EXPECT_EQ(";", format(";")); 104} 105 106TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 107 EXPECT_EQ("int i;", format(" int i;")); 108 EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); 109 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 110 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 111} 112 113TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 114 EXPECT_EQ("int i;", format("int\ni;")); 115} 116 117TEST_F(FormatTest, FormatsNestedBlockStatements) { 118 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 119} 120 121TEST_F(FormatTest, FormatsNestedCall) { 122 verifyFormat("Method(f1, f2(f3));"); 123 verifyFormat("Method(f1(f2, f3()));"); 124} 125 126//===----------------------------------------------------------------------===// 127// Tests for control statements. 128//===----------------------------------------------------------------------===// 129 130TEST_F(FormatTest, FormatIfWithoutCompountStatement) { 131 verifyFormat("if (true)\n f();\ng();"); 132 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 133 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 134 verifyFormat("if (a)\n" 135 " // comment\n" 136 " f();"); 137} 138 139TEST_F(FormatTest, ParseIfElse) { 140 verifyFormat("if (true)\n" 141 " if (true)\n" 142 " if (true)\n" 143 " f();\n" 144 " else\n" 145 " g();\n" 146 " else\n" 147 " h();\n" 148 "else\n" 149 " i();"); 150 verifyFormat("if (true)\n" 151 " if (true)\n" 152 " if (true) {\n" 153 " if (true)\n" 154 " f();\n" 155 " } else {\n" 156 " g();\n" 157 " }\n" 158 " else\n" 159 " h();\n" 160 "else {\n" 161 " i();\n" 162 "}"); 163} 164 165TEST_F(FormatTest, ElseIf) { 166 verifyFormat("if (a) {} else if (b) {}"); 167 verifyFormat("if (a)\n" 168 " f();\n" 169 "else if (b)\n" 170 " g();\n" 171 "else\n" 172 " h();"); 173} 174 175TEST_F(FormatTest, FormatsForLoop) { 176 verifyFormat( 177 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 178 " ++VeryVeryLongLoopVariable)\n" 179 " ;"); 180 verifyFormat("for (;;)\n" 181 " f();"); 182 verifyFormat("for (;;) {}"); 183 verifyFormat("for (;;) {\n" 184 " f();\n" 185 "}"); 186 187 verifyFormat( 188 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 189 " E = UnwrappedLines.end();\n" 190 " I != E; ++I) {}"); 191 192 verifyFormat( 193 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 194 " ++IIIII) {}"); 195} 196 197TEST_F(FormatTest, FormatsWhileLoop) { 198 verifyFormat("while (true) {}"); 199 verifyFormat("while (true)\n" 200 " f();"); 201 verifyFormat("while () {}"); 202 verifyFormat("while () {\n" 203 " f();\n" 204 "}"); 205} 206 207TEST_F(FormatTest, FormatsDoWhile) { 208 verifyFormat("do {\n" 209 " do_something();\n" 210 "} while (something());"); 211 verifyFormat("do\n" 212 " do_something();\n" 213 "while (something());"); 214} 215 216TEST_F(FormatTest, FormatsSwitchStatement) { 217 verifyFormat("switch (x) {\n" 218 "case 1:\n" 219 " f();\n" 220 " break;\n" 221 "case kFoo:\n" 222 "case ns::kBar:\n" 223 "case kBaz:\n" 224 " break;\n" 225 "default:\n" 226 " g();\n" 227 " break;\n" 228 "}"); 229 verifyFormat("switch (x) {\n" 230 "case 1: {\n" 231 " f();\n" 232 " break;\n" 233 "}\n" 234 "}"); 235 verifyFormat("switch (test)\n" 236 " ;"); 237 verifyGoogleFormat("switch (x) {\n" 238 " case 1:\n" 239 " f();\n" 240 " break;\n" 241 " case kFoo:\n" 242 " case ns::kBar:\n" 243 " case kBaz:\n" 244 " break;\n" 245 " default:\n" 246 " g();\n" 247 " break;\n" 248 "}"); 249 verifyGoogleFormat("switch (x) {\n" 250 " case 1: {\n" 251 " f();\n" 252 " break;\n" 253 " }\n" 254 "}"); 255 verifyGoogleFormat("switch (test)\n" 256 " ;"); 257} 258 259TEST_F(FormatTest, FormatsLabels) { 260 verifyFormat("void f() {\n" 261 " some_code();\n" 262 "test_label:\n" 263 " some_other_code();\n" 264 " {\n" 265 " some_more_code();\n" 266 " another_label:\n" 267 " some_more_code();\n" 268 " }\n" 269 "}"); 270 verifyFormat("some_code();\n" 271 "test_label:\n" 272 "some_other_code();"); 273} 274 275//===----------------------------------------------------------------------===// 276// Tests for comments. 277//===----------------------------------------------------------------------===// 278 279TEST_F(FormatTest, UnderstandsSingleLineComments) { 280 verifyFormat("// line 1\n" 281 "// line 2\n" 282 "void f() {}\n"); 283 284 verifyFormat("void f() {\n" 285 " // Doesn't do anything\n" 286 "}"); 287 288 verifyFormat("int i // This is a fancy variable\n" 289 " = 5;"); 290 291 verifyFormat("enum E {\n" 292 " // comment\n" 293 " VAL_A, // comment\n" 294 " VAL_B\n" 295 "};"); 296 297 verifyFormat( 298 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 299 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 300 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 301 " // Comment inside a statement.\n" 302 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 303 304 EXPECT_EQ("int i; // single line trailing comment", 305 format("int i;\\\n// single line trailing comment")); 306 307 verifyGoogleFormat("int a; // Trailing comment."); 308} 309 310TEST_F(FormatTest, UnderstandsMultiLineComments) { 311 verifyFormat("f(/*test=*/ true);"); 312} 313 314//===----------------------------------------------------------------------===// 315// Tests for classes, namespaces, etc. 316//===----------------------------------------------------------------------===// 317 318TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 319 verifyFormat("class A {};"); 320} 321 322TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 323 verifyFormat("class A {\n" 324 "public:\n" 325 "protected:\n" 326 "private:\n" 327 " void f() {}\n" 328 "};"); 329 verifyGoogleFormat("class A {\n" 330 " public:\n" 331 " protected:\n" 332 " private:\n" 333 " void f() {}\n" 334 "};"); 335} 336 337TEST_F(FormatTest, FormatsDerivedClass) { 338 verifyFormat("class A : public B {};"); 339 verifyFormat("class A : public ::B {};"); 340} 341 342TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 343 verifyFormat("class A {} a, b;"); 344 verifyFormat("struct A {} a, b;"); 345} 346 347TEST_F(FormatTest, FormatsEnum) { 348 verifyFormat("enum {\n" 349 " Zero,\n" 350 " One = 1,\n" 351 " Two = One + 1,\n" 352 " Three = (One + Two),\n" 353 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 354 " Five = (One, Two, Three, Four, 5)\n" 355 "};"); 356 verifyFormat("enum Enum {\n" 357 "};"); 358 verifyFormat("enum {\n" 359 "};"); 360} 361 362TEST_F(FormatTest, FormatsNamespaces) { 363 verifyFormat("namespace some_namespace {\n" 364 "class A {};\n" 365 "void f() {\n" 366 " f();\n" 367 "}\n" 368 "}"); 369 verifyFormat("namespace {\n" 370 "class A {};\n" 371 "void f() {\n" 372 " f();\n" 373 "}\n" 374 "}"); 375 verifyFormat("inline namespace X {\n" 376 "class A {};\n" 377 "void f() {\n" 378 " f();\n" 379 "}\n" 380 "}"); 381 verifyFormat("using namespace some_namespace;\n" 382 "class A {};\n" 383 "void f() {\n" 384 " f();\n" 385 "}"); 386} 387 388TEST_F(FormatTest, FormatTryCatch) { 389 verifyFormat("try {\n" 390 " throw a * b;\n" 391 "}\n" 392 "catch (int a) {\n" 393 " // Do nothing.\n" 394 "}\n" 395 "catch (...) {\n" 396 " exit(42);\n" 397 "}"); 398 399 // Function-level try statements. 400 verifyFormat("int f() try {\n" 401 " return 4;\n" 402 "}\n" 403 "catch (...) {\n" 404 " return 5;\n" 405 "}"); 406 verifyFormat("class A {\n" 407 " int a;\n" 408 " A() try : a(0) {}\n" 409 " catch (...) {\n" 410 " throw;\n" 411 " }\n" 412 "};\n"); 413} 414 415TEST_F(FormatTest, FormatObjCTryCatch) { 416 verifyFormat("@try {\n" 417 " f();\n" 418 "}\n" 419 "@catch (NSException e) {\n" 420 " @throw;\n" 421 "}\n" 422 "@finally {\n" 423 " exit(42);\n" 424 "}"); 425} 426 427TEST_F(FormatTest, StaticInitializers) { 428 verifyFormat("static SomeClass SC = { 1, 'a' };"); 429 430 // FIXME: Format like enums if the static initializer does not fit on a line. 431 verifyFormat( 432 "static SomeClass WithALoooooooooooooooooooongName = {\n" 433 " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 434 "};"); 435 436 verifyFormat( 437 "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" 438 " looooooooooooooooooooooooooooooooooongname,\n" 439 " looooooooooooooooooooooooooooooong };"); 440} 441 442TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 443 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 444 " \\\n" 445 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 446} 447 448TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 449 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 450 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 451} 452 453TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) { 454 EXPECT_EQ("#\n;", format("#;")); 455 verifyFormat("#\n;\n;\n;"); 456} 457 458TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 459 EXPECT_EQ("#line 42 \"test\"\n", 460 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 461 EXPECT_EQ("#define A \\\n B\n", 462 format("# \\\n define \\\n A \\\n B\n", 463 getLLVMStyleWithColumns(12))); 464} 465 466TEST_F(FormatTest, EndOfFileEndsPPDirective) { 467 EXPECT_EQ("#line 42 \"test\"", 468 format("# \\\n line \\\n 42 \\\n \"test\"")); 469 EXPECT_EQ("#define A \\\n B", 470 format("# \\\n define \\\n A \\\n B", 471 getLLVMStyleWithColumns(12))); 472} 473 474TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 475 // If the macro fits in one line, we still do not get the full 476 // line, as only the next line decides whether we need an escaped newline and 477 // thus use the last column. 478 verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); 479 480 verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); 481 verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); 482 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 483} 484 485TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 486 EXPECT_EQ("// some comment\n" 487 "#include \"a.h\"\n" 488 "#define A(A,\\\n" 489 " B)\n" 490 "#include \"b.h\"\n" 491 "// some comment\n", 492 format(" // some comment\n" 493 " #include \"a.h\"\n" 494 "#define A(A,\\\n" 495 " B)\n" 496 " #include \"b.h\"\n" 497 " // some comment\n", getLLVMStyleWithColumns(13))); 498} 499 500TEST_F(FormatTest, LayoutSingleHash) { 501 EXPECT_EQ("#\na;", format("#\na;")); 502} 503 504TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 505 EXPECT_EQ("#define A \\\n" 506 " c; \\\n" 507 " e;\n" 508 "f;", format("#define A c; e;\n" 509 "f;", getLLVMStyleWithColumns(14))); 510} 511 512TEST_F(FormatTest, LayoutRemainingTokens) { 513 EXPECT_EQ("{}", format("{}")); 514} 515 516TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { 517 EXPECT_EQ("# define A\\\n b;", 518 format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); 519} 520 521TEST_F(FormatTest, MacroDefinitionInsideStatement) { 522 EXPECT_EQ("int x,\n" 523 "#define A\n" 524 " y;", format("int x,\n#define A\ny;")); 525} 526 527TEST_F(FormatTest, HashInMacroDefinition) { 528 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 529 verifyFormat("#define A \\\n" 530 " { \\\n" 531 " f(#c);\\\n" 532 " }", getLLVMStyleWithColumns(11)); 533 534 verifyFormat("#define A(X) \\\n" 535 " void function##X()", getLLVMStyleWithColumns(22)); 536 537 verifyFormat("#define A(a, b, c) \\\n" 538 " void a##b##c()", getLLVMStyleWithColumns(22)); 539 540 verifyFormat("#define A \\\n" 541 " void # ## #", getLLVMStyleWithColumns(22)); 542} 543 544TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 545 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 546} 547 548TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 549 verifyFormat("{\n {\n a #c;\n }\n}"); 550} 551 552TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 553 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 554 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 555 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 556 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 557} 558 559TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { 560 EXPECT_EQ( 561 "#define A \\\n int i; \\\n int j;", 562 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 563} 564 565TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 566 verifyFormat("#define A \\\n" 567 " int v( \\\n" 568 " a); \\\n" 569 " int i;", getLLVMStyleWithColumns(11)); 570} 571 572TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 573 EXPECT_EQ( 574 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 575 " \\\n" 576 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 577 "\n" 578 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 579 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 580 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 581 "\\\n" 582 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 583 " \n" 584 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 585 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 586} 587 588TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 589 EXPECT_EQ("int\n" 590 "#define A\n" 591 " a;", 592 format("int\n#define A\na;")); 593 verifyFormat( 594 "functionCallTo(someOtherFunction(\n" 595 " withSomeParameters, whichInSequence,\n" 596 " areLongerThanALine(andAnotherCall,\n" 597 "#define A \\\n" 598 " B\n" 599 " withMoreParamters,\n" 600 " whichStronglyInfluenceTheLayout),\n" 601 " andMoreParameters),\n" 602 " trailing);", getLLVMStyleWithColumns(69)); 603} 604 605TEST_F(FormatTest, LayoutBlockInsideParens) { 606 EXPECT_EQ("functionCall({\n" 607 " int i;\n" 608 "});", format(" functionCall ( {int i;} );")); 609} 610 611TEST_F(FormatTest, LayoutBlockInsideStatement) { 612 EXPECT_EQ("SOME_MACRO {\n" 613 " int i;\n" 614 "}\n" 615 "int i;", format(" SOME_MACRO {int i;} int i;")); 616} 617 618TEST_F(FormatTest, LayoutNestedBlocks) { 619 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 620 " struct s {\n" 621 " int i;\n" 622 " };\n" 623 " s kBitsToOs[] = { { 10 } };\n" 624 " for (int i = 0; i < 10; ++i)\n" 625 " return;\n" 626 "}"); 627} 628 629TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 630 EXPECT_EQ("{}", format("{}")); 631} 632 633//===----------------------------------------------------------------------===// 634// Line break tests. 635//===----------------------------------------------------------------------===// 636 637TEST_F(FormatTest, FormatsFunctionDefinition) { 638 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," 639 " int h, int j, int f,\n" 640 " int c, int ddddddddddddd) {}"); 641} 642 643TEST_F(FormatTest, FormatsAwesomeMethodCall) { 644 verifyFormat( 645 "SomeLongMethodName(SomeReallyLongMethod(\n" 646 " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" 647 " SecondLongCall(parameter));"); 648} 649 650TEST_F(FormatTest, ConstructorInitializers) { 651 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 652 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 653 getLLVMStyleWithColumns(45)); 654 verifyFormat("Constructor()\n" 655 " : Inttializer(FitsOnTheLine) {}", 656 getLLVMStyleWithColumns(44)); 657 658 verifyFormat( 659 "SomeClass::Constructor()\n" 660 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 661 662 verifyFormat( 663 "SomeClass::Constructor()\n" 664 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 665 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 666 verifyGoogleFormat( 667 "SomeClass::Constructor()\n" 668 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 669 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 670 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 671 672 verifyFormat( 673 "SomeClass::Constructor()\n" 674 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 675 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 676 677 verifyFormat("Constructor()\n" 678 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 679 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 680 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 681 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 682 683 // Here a line could be saved by splitting the second initializer onto two 684 // lines, but that is not desireable. 685 verifyFormat("Constructor()\n" 686 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 687 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 688 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 689 690 verifyGoogleFormat("MyClass::MyClass(int var)\n" 691 " : some_var_(var), // 4 space indent\n" 692 " some_other_var_(var + 1) { // lined up\n" 693 "}"); 694} 695 696TEST_F(FormatTest, BreaksAsHighAsPossible) { 697 verifyFormat( 698 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 699 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 700 " f();"); 701} 702 703TEST_F(FormatTest, BreaksDesireably) { 704 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 705 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 706 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}"); 707 708 verifyFormat( 709 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 711 712 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 713 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 715 716 verifyFormat( 717 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 719 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 721 722 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 723 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 724 725 verifyFormat( 726 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 728 729 // This test case breaks on an incorrect memoization, i.e. an optimization not 730 // taking into account the StopAt value. 731 verifyFormat( 732 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 733 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 734 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 735 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 736 737 verifyFormat("{\n {\n {\n" 738 " Annotation.SpaceRequiredBefore =\n" 739 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 740 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 741 " }\n }\n}"); 742} 743 744TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { 745 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 746 " GUARDED_BY(aaaaaaaaaaaaa);"); 747} 748 749TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 750 verifyFormat( 751 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 752 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}"); 753 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 754 " ccccccccccccccccccccccccc) {}"); 755 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 756 " ccccccccccccccccccccccccc) {}"); 757 verifyFormat( 758 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 759 " ccccccccccccccccccccccccc) {}"); 760} 761 762TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) { 763 verifyFormat( 764 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n" 765 " SI->getAlignment(),\n" 766 " SI->getPointerAddressSpaceee());\n"); 767 verifyFormat( 768 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 769 " Line.Tokens.front().Tok.getLocation(),\n" 770 " Line.Tokens.back().Tok.getLocation());"); 771} 772 773TEST_F(FormatTest, AlignsAfterAssignments) { 774 verifyFormat( 775 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 776 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 777 verifyFormat( 778 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 779 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 780 verifyFormat( 781 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 782 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 783 verifyFormat( 784 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 785 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 786 verifyFormat( 787 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 788 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 789 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 790} 791 792TEST_F(FormatTest, AlignsAfterReturn) { 793 verifyFormat( 794 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 795 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 796 verifyFormat( 797 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 798 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 799} 800 801TEST_F(FormatTest, BreaksConditionalExpressions) { 802 verifyFormat( 803 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 804 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 805 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 806 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 807 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);"); 808} 809 810TEST_F(FormatTest, AlignsStringLiterals) { 811 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 812 " \"short literal\");"); 813 verifyFormat( 814 "looooooooooooooooooooooooongFunction(\n" 815 " \"short literal\"\n" 816 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 817} 818 819TEST_F(FormatTest, AlignsPipes) { 820 verifyFormat( 821 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 822 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 823 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 824 verifyFormat( 825 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 826 " << aaaaaaaaaaaaaaaaaaaa;"); 827 verifyFormat( 828 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 829 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 830 verifyFormat( 831 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 832 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 833 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 834 verifyFormat( 835 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 836 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 837 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 838} 839 840TEST_F(FormatTest, UnderstandsEquals) { 841 verifyFormat( 842 "aaaaaaaaaaaaaaaaa =\n" 843 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 844 verifyFormat( 845 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 846 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 847 verifyFormat( 848 "if (a) {\n" 849 " f();\n" 850 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 852 853 verifyFormat( 854 // FIXME: Does an expression like this ever make sense? If yes, fix. 855 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n" 856 " 10000000) {}"); 857} 858 859TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 860 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 861 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 862 863 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 864 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 865 866 verifyFormat( 867 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 868 " Parameter2);"); 869 870 verifyFormat( 871 "ShortObject->shortFunction(\n" 872 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 873 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 874 875 verifyFormat("loooooooooooooongFunction(\n" 876 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 877 878 verifyFormat( 879 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 880 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 881 882 // Here, it is not necessary to wrap at "." or "->". 883 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 884 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 885 verifyFormat( 886 "aaaaaaaaaaa->aaaaaaaaa(\n" 887 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 888 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 889} 890 891TEST_F(FormatTest, WrapsTemplateDeclarations) { 892 verifyFormat("template <typename T>\n" 893 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 894 verifyFormat( 895 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 896 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 897 verifyFormat( 898 "template <typename T>\n" 899 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 900 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 901 verifyFormat( 902 "template <typename T>\n" 903 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 904 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 906 verifyFormat("template <typename T>\n" 907 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 908 " int aaaaaaaaaaaaaaaaa);"); 909 verifyFormat( 910 "template <typename T1, typename T2 = char, typename T3 = char,\n" 911 " typename T4 = char>\n" 912 "void f();"); 913} 914 915TEST_F(FormatTest, UnderstandsTemplateParameters) { 916 verifyFormat("A<int> a;"); 917 verifyFormat("A<A<A<int> > > a;"); 918 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 919 verifyFormat("bool x = a < 1 || 2 > a;"); 920 verifyFormat("bool x = 5 < f<int>();"); 921 verifyFormat("bool x = f<int>() > 5;"); 922 verifyFormat("bool x = 5 < a<int>::x;"); 923 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 924 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 925 926 verifyGoogleFormat("A<A<int>> a;"); 927 verifyGoogleFormat("A<A<A<int>>> a;"); 928 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 929 930 verifyFormat("test >> a >> b;"); 931 verifyFormat("test << a >> b;"); 932 933 verifyFormat("f<int>();"); 934 verifyFormat("template <typename T> void f() {}"); 935} 936 937TEST_F(FormatTest, UnderstandsUnaryOperators) { 938 verifyFormat("int a = -2;"); 939 verifyFormat("f(-1, -2, -3);"); 940 verifyFormat("a[-1] = 5;"); 941 verifyFormat("int a = 5 + -2;"); 942 verifyFormat("if (i == -1) {}"); 943 verifyFormat("if (i != -1) {}"); 944 verifyFormat("if (i > -1) {}"); 945 verifyFormat("if (i < -1) {}"); 946 verifyFormat("++(a->f());"); 947 verifyFormat("--(a->f());"); 948 verifyFormat("if (!(a->f())) {}"); 949 950 verifyFormat("a-- > b;"); 951 verifyFormat("b ? -a : c;"); 952 verifyFormat("n * sizeof char16;"); 953 verifyFormat("n * alignof char16;"); 954 verifyFormat("sizeof(char);"); 955 verifyFormat("alignof(char);"); 956 957 verifyFormat("return -1;"); 958 verifyFormat("switch (a) {\n" 959 "case -1:\n" 960 " break;\n" 961 "}"); 962} 963 964TEST_F(FormatTest, UndestandsOverloadedOperators) { 965 verifyFormat("bool operator<();"); 966 verifyFormat("bool operator>();"); 967 verifyFormat("bool operator=();"); 968 verifyFormat("bool operator==();"); 969 verifyFormat("bool operator!=();"); 970 verifyFormat("int operator+();"); 971 verifyFormat("int operator++();"); 972 verifyFormat("bool operator();"); 973 verifyFormat("bool operator()();"); 974 verifyFormat("bool operator[]();"); 975 verifyFormat("operator bool();"); 976 verifyFormat("operator SomeType<int>();"); 977 verifyFormat("void *operator new(std::size_t size);"); 978 verifyFormat("void *operator new[](std::size_t size);"); 979 verifyFormat("void operator delete(void *ptr);"); 980 verifyFormat("void operator delete[](void *ptr);"); 981} 982 983TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 984 verifyFormat("int *f(int *a) {}"); 985 verifyFormat("f(a, *a);"); 986 verifyFormat("f(*a);"); 987 verifyFormat("int a = b * 10;"); 988 verifyFormat("int a = 10 * b;"); 989 verifyFormat("int a = b * c;"); 990 verifyFormat("int a += b * c;"); 991 verifyFormat("int a -= b * c;"); 992 verifyFormat("int a *= b * c;"); 993 verifyFormat("int a /= b * c;"); 994 verifyFormat("int a = *b;"); 995 verifyFormat("int a = *b * c;"); 996 verifyFormat("int a = b * *c;"); 997 verifyFormat("int main(int argc, char **argv) {}"); 998 verifyFormat("return 10 * b;"); 999 verifyFormat("return *b * *c;"); 1000 verifyFormat("return a & ~b;"); 1001 verifyFormat("f(b ? *c : *d);"); 1002 verifyFormat("int a = b ? *c : *d;"); 1003 verifyFormat("*b = a;"); 1004 verifyFormat("a * ~b;"); 1005 verifyFormat("a * !b;"); 1006 verifyFormat("a * +b;"); 1007 verifyFormat("a * -b;"); 1008 verifyFormat("a * ++b;"); 1009 verifyFormat("a * --b;"); 1010 verifyFormat("int *pa = (int *)&a;"); 1011 1012 verifyFormat("InvalidRegions[*R] = 0;"); 1013 1014 verifyFormat("A<int *> a;"); 1015 verifyFormat("A<int **> a;"); 1016 verifyFormat("A<int *, int *> a;"); 1017 verifyFormat("A<int **, int **> a;"); 1018 verifyFormat("Type *A = static_cast<Type *>(P);"); 1019 verifyFormat("Type *A = (Type *)P;"); 1020 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 1021 1022 verifyFormat( 1023 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1024 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1025 1026 verifyGoogleFormat("int main(int argc, char** argv) {}"); 1027 verifyGoogleFormat("A<int*> a;"); 1028 verifyGoogleFormat("A<int**> a;"); 1029 verifyGoogleFormat("A<int*, int*> a;"); 1030 verifyGoogleFormat("A<int**, int**> a;"); 1031 verifyGoogleFormat("f(b ? *c : *d);"); 1032 verifyGoogleFormat("int a = b ? *c : *d;"); 1033} 1034 1035TEST_F(FormatTest, FormatsFunctionTypes) { 1036 // FIXME: Determine the cases that need a space after the return type and fix. 1037 verifyFormat("A<bool()> a;"); 1038 verifyFormat("A<SomeType()> a;"); 1039 verifyFormat("A<void(*)(int, std::string)> a;"); 1040 1041 verifyFormat("int(*func)(void *);"); 1042} 1043 1044TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { 1045 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n" 1046 " int LoooooooooooooooongParam2) {}"); 1047 verifyFormat( 1048 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 1049 " SourceLocation L, IdentifierIn *II,\n" 1050 " Type *T) {}"); 1051} 1052 1053TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 1054 verifyFormat("(a)->b();"); 1055 verifyFormat("--a;"); 1056} 1057 1058TEST_F(FormatTest, HandlesIncludeDirectives) { 1059 EXPECT_EQ("#include <string>\n", format("#include <string>\n")); 1060 EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n")); 1061 EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); 1062 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); 1063 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); 1064 1065 EXPECT_EQ("#import <string>\n", format("#import <string>\n")); 1066 EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); 1067 EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); 1068 EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); 1069 EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); 1070} 1071 1072//===----------------------------------------------------------------------===// 1073// Error recovery tests. 1074//===----------------------------------------------------------------------===// 1075 1076TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 1077 verifyFormat("void f() { return } 42"); 1078} 1079 1080TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 1081 verifyFormat("int aaaaaaaa =\n" 1082 " // Overly long comment\n" 1083 " b;", getLLVMStyleWithColumns(20)); 1084 verifyFormat("function(\n" 1085 " ShortArgument,\n" 1086 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20)); 1087} 1088 1089TEST_F(FormatTest, IncorrectAccessSpecifier) { 1090 verifyFormat("public:"); 1091 verifyFormat("class A {\n" 1092 "public\n" 1093 " void f() {}\n" 1094 "};"); 1095 verifyFormat("public\n" 1096 "int qwerty;"); 1097 verifyFormat("public\n" 1098 "B {}"); 1099 verifyFormat("public\n" 1100 "{}"); 1101 verifyFormat("public\n" 1102 "B {\n" 1103 " int x;\n" 1104 "}"); 1105} 1106 1107TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 1108 verifyFormat("{"); 1109} 1110 1111TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 1112 verifyFormat("do {}"); 1113 verifyFormat("do {}\n" 1114 "f();"); 1115 verifyFormat("do {}\n" 1116 "wheeee(fun);"); 1117 verifyFormat("do {\n" 1118 " f();\n" 1119 "}"); 1120} 1121 1122TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 1123 verifyFormat("namespace {\n" 1124 "class Foo { Foo ( }; } // comment"); 1125} 1126 1127TEST_F(FormatTest, IncorrectCodeErrorDetection) { 1128 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); 1129 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 1130 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 1131 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); 1132 1133 FormatStyle Style = getLLVMStyle(); 1134 Style.ColumnLimit = 10; 1135 EXPECT_EQ("{\n" 1136 " {\n" 1137 " breakme(\n" 1138 " qwe);\n" 1139 "}\n", format("{\n" 1140 " {\n" 1141 " breakme(qwe);\n" 1142 "}\n", Style)); 1143 1144} 1145 1146TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 1147 verifyFormat( 1148 "int x = {\n" 1149 " avariable,\n" 1150 " b(alongervariable)\n" 1151 "};", getLLVMStyleWithColumns(25)); 1152} 1153 1154TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { 1155 verifyFormat( 1156 "Aaa({\n" 1157 " int i;\n" 1158 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1159 " ccccccccccccccccc));"); 1160} 1161 1162//===----------------------------------------------------------------------===// 1163// Objective-C tests. 1164//===----------------------------------------------------------------------===// 1165 1166TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 1167 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 1168 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 1169 format("-(NSUInteger)indexOfObject:(id)anObject;")); 1170 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 1171 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 1172 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 1173 format("-(NSInteger)Method3:(id)anObject;")); 1174 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 1175 format("-(NSInteger)Method4:(id)anObject;")); 1176 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 1177 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 1178 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 1179 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 1180 EXPECT_EQ( 1181 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 1182 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 1183 1184 // Very long objectiveC method declaration. 1185 EXPECT_EQ( 1186 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " 1187 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " 1188 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " 1189 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " 1190 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " 1191 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", 1192 format( 1193 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " 1194 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " 1195 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " 1196 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " 1197 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " 1198 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); 1199 1200 verifyFormat("- (int)sum:(vector<int>)numbers;"); 1201 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;"); 1202 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 1203 // protocol lists (but not for template classes): 1204 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 1205 1206 verifyFormat("- (int(*)())foo:(int(*)())f;"); 1207 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;"); 1208 1209 // If there's no return type (very rare in practice!), LLVM and Google style 1210 // agree. 1211 verifyFormat("- foo:(int)f;"); 1212 verifyGoogleFormat("- foo:(int)foo;"); 1213} 1214 1215TEST_F(FormatTest, FormatObjCBlocks) { 1216 verifyFormat("int (^Block)(int, int);"); 1217 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); 1218} 1219 1220TEST_F(FormatTest, FormatObjCInterface) { 1221 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875 1222 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 1223 "@public\n" 1224 " int field1;\n" 1225 "@protected\n" 1226 " int field2;\n" 1227 "@private\n" 1228 " int field3;\n" 1229 "@package\n" 1230 " int field4;\n" 1231 "}\n" 1232 "+ (id)init;\n" 1233 "@end"); 1234 1235 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 1236 " @public\n" 1237 " int field1;\n" 1238 " @protected\n" 1239 " int field2;\n" 1240 " @private\n" 1241 " int field3;\n" 1242 " @package\n" 1243 " int field4;\n" 1244 "}\n" 1245 "+(id) init;\n" 1246 "@end"); 1247 1248 verifyFormat("@interface Foo\n" 1249 "+ (id)init;\n" 1250 "// Look, a comment!\n" 1251 "- (int)answerWith:(int)i;\n" 1252 "@end"); 1253 1254 verifyFormat("@interface Foo\n" 1255 "@end\n" 1256 "@interface Bar\n" 1257 "@end"); 1258 1259 verifyFormat("@interface Foo : Bar\n" 1260 "+ (id)init;\n" 1261 "@end"); 1262 1263 verifyFormat("@interface Foo : Bar <Baz, Quux>\n" 1264 "+ (id)init;\n" 1265 "@end"); 1266 1267 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 1268 "+(id) init;\n" 1269 "@end"); 1270 1271 verifyFormat("@interface Foo (HackStuff)\n" 1272 "+ (id)init;\n" 1273 "@end"); 1274 1275 verifyFormat("@interface Foo ()\n" 1276 "+ (id)init;\n" 1277 "@end"); 1278 1279 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 1280 "+ (id)init;\n" 1281 "@end"); 1282 1283 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 1284 "+(id) init;\n" 1285 "@end"); 1286 1287 verifyFormat("@interface Foo {\n" 1288 " int _i;\n" 1289 "}\n" 1290 "+ (id)init;\n" 1291 "@end"); 1292 1293 verifyFormat("@interface Foo : Bar {\n" 1294 " int _i;\n" 1295 "}\n" 1296 "+ (id)init;\n" 1297 "@end"); 1298 1299 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 1300 " int _i;\n" 1301 "}\n" 1302 "+ (id)init;\n" 1303 "@end"); 1304 1305 verifyFormat("@interface Foo (HackStuff) {\n" 1306 " int _i;\n" 1307 "}\n" 1308 "+ (id)init;\n" 1309 "@end"); 1310 1311 verifyFormat("@interface Foo () {\n" 1312 " int _i;\n" 1313 "}\n" 1314 "+ (id)init;\n" 1315 "@end"); 1316 1317 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 1318 " int _i;\n" 1319 "}\n" 1320 "+ (id)init;\n" 1321 "@end"); 1322} 1323 1324TEST_F(FormatTest, FormatObjCImplementation) { 1325 verifyFormat("@implementation Foo : NSObject {\n" 1326 "@public\n" 1327 " int field1;\n" 1328 "@protected\n" 1329 " int field2;\n" 1330 "@private\n" 1331 " int field3;\n" 1332 "@package\n" 1333 " int field4;\n" 1334 "}\n" 1335 "+ (id)init {}\n" 1336 "@end"); 1337 1338 verifyGoogleFormat("@implementation Foo : NSObject {\n" 1339 " @public\n" 1340 " int field1;\n" 1341 " @protected\n" 1342 " int field2;\n" 1343 " @private\n" 1344 " int field3;\n" 1345 " @package\n" 1346 " int field4;\n" 1347 "}\n" 1348 "+(id) init {}\n" 1349 "@end"); 1350 1351 verifyFormat("@implementation Foo\n" 1352 "+ (id)init {\n" 1353 " if (true)\n" 1354 " return nil;\n" 1355 "}\n" 1356 "// Look, a comment!\n" 1357 "- (int)answerWith:(int)i {\n" 1358 " return i;\n" 1359 "}\n" 1360 "@end"); 1361 1362 verifyFormat("@implementation Foo\n" 1363 "@end\n" 1364 "@implementation Bar\n" 1365 "@end"); 1366 1367 verifyFormat("@implementation Foo : Bar\n" 1368 "+ (id)init {}\n" 1369 "@end"); 1370 1371 verifyFormat("@implementation Foo {\n" 1372 " int _i;\n" 1373 "}\n" 1374 "+ (id)init {}\n" 1375 "@end"); 1376 1377 verifyFormat("@implementation Foo : Bar {\n" 1378 " int _i;\n" 1379 "}\n" 1380 "+ (id)init {}\n" 1381 "@end"); 1382 1383 verifyFormat("@implementation Foo (HackStuff)\n" 1384 "+ (id)init {}\n" 1385 "@end"); 1386} 1387 1388TEST_F(FormatTest, FormatObjCProtocol) { 1389 verifyFormat("@protocol Foo\n" 1390 "@property(weak) id delegate;\n" 1391 "- (NSUInteger)numberOfThings;\n" 1392 "@end"); 1393 1394 verifyFormat("@protocol MyProtocol <NSObject>\n" 1395 "- (NSUInteger)numberOfThings;\n" 1396 "@end"); 1397 1398 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 1399 "-(NSUInteger) numberOfThings;\n" 1400 "@end"); 1401 1402 verifyFormat("@protocol Foo;\n" 1403 "@protocol Bar;\n"); 1404 1405 verifyFormat("@protocol Foo\n" 1406 "@end\n" 1407 "@protocol Bar\n" 1408 "@end"); 1409 1410 verifyFormat("@protocol myProtocol\n" 1411 "- (void)mandatoryWithInt:(int)i;\n" 1412 "@optional\n" 1413 "- (void)optional;\n" 1414 "@required\n" 1415 "- (void)required;\n" 1416 "@optional\n" 1417 "@property(assign) int madProp;\n" 1418 "@end\n"); 1419} 1420 1421TEST_F(FormatTest, ObjCAt) { 1422 verifyFormat("@autoreleasepool"); 1423 verifyFormat("@catch"); 1424 verifyFormat("@class"); 1425 verifyFormat("@compatibility_alias"); 1426 verifyFormat("@defs"); 1427 verifyFormat("@dynamic"); 1428 verifyFormat("@encode"); 1429 verifyFormat("@end"); 1430 verifyFormat("@finally"); 1431 verifyFormat("@implementation"); 1432 verifyFormat("@import"); 1433 verifyFormat("@interface"); 1434 verifyFormat("@optional"); 1435 verifyFormat("@package"); 1436 verifyFormat("@private"); 1437 verifyFormat("@property"); 1438 verifyFormat("@protected"); 1439 verifyFormat("@protocol"); 1440 verifyFormat("@public"); 1441 verifyFormat("@required"); 1442 verifyFormat("@selector"); 1443 verifyFormat("@synchronized"); 1444 verifyFormat("@synthesize"); 1445 verifyFormat("@throw"); 1446 verifyFormat("@try"); 1447 1448 verifyFormat("@\"String\""); 1449 verifyFormat("@1"); 1450 verifyFormat("@+4.8"); 1451 verifyFormat("@-4"); 1452 verifyFormat("@1LL"); 1453 verifyFormat("@.5"); 1454 verifyFormat("@'c'"); 1455 verifyFormat("@true"); 1456 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 1457 verifyFormat("@["); 1458 verifyFormat("@{"); 1459 1460 EXPECT_EQ("@interface", format("@ interface")); 1461 1462 // The precise formatting of this doesn't matter, nobody writes code like 1463 // this. 1464 verifyFormat("@ /*foo*/ interface"); 1465} 1466 1467TEST_F(FormatTest, ObjCSnippets) { 1468 // FIXME: Make the uncommented lines below pass. 1469 verifyFormat("@autoreleasepool {\n" 1470 " foo();\n" 1471 "}"); 1472 verifyFormat("@class Foo, Bar;"); 1473 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 1474 verifyFormat("@dynamic textColor;"); 1475 //verifyFormat("char *buf1 = @encode(int **);"); 1476 verifyFormat("Protocol *proto = @protocol(p1);"); 1477 //verifyFormat("SEL s = @selector(foo:);"); 1478 verifyFormat("@synchronized(self) {\n" 1479 " f();\n" 1480 "}"); 1481 1482 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 1483 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 1484 1485 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 1486 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 1487 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 1488} 1489 1490} // end namespace tooling 1491} // end namespace clang 1492