FormatTest.cpp revision cd52bdaaf076b0082c07c6b3d88937fb737054f1
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 653 verifyFormat( 654 "SomeClass::Constructor()\n" 655 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 656 657 verifyFormat( 658 "SomeClass::Constructor()\n" 659 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 660 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 661 662 verifyFormat("Constructor()\n" 663 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 664 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 665 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 666 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 667 668 // Here a line could be saved by splitting the second initializer onto two 669 // lines, but that is not desireable. 670 verifyFormat("Constructor()\n" 671 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 672 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 673 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 674 675 verifyGoogleFormat("MyClass::MyClass(int var)\n" 676 " : some_var_(var), // 4 space indent\n" 677 " some_other_var_(var + 1) { // lined up\n" 678 "}"); 679} 680 681TEST_F(FormatTest, BreaksAsHighAsPossible) { 682 verifyFormat( 683 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 684 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 685 " f();"); 686} 687 688TEST_F(FormatTest, BreaksDesireably) { 689 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 690 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 691 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}"); 692 693 verifyFormat( 694 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 695 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 696 697 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 698 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 699 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 700 701 verifyFormat( 702 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 703 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 704 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 706 707 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 708 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 709 710 verifyFormat( 711 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 712 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 713 714 // This test case breaks on an incorrect memoization, i.e. an optimization not 715 // taking into account the StopAt value. 716 verifyFormat( 717 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 718 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 719 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 720 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 721 722 verifyFormat("{\n {\n {\n" 723 " Annotation.SpaceRequiredBefore =\n" 724 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 725 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 726 " }\n }\n}"); 727} 728 729TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 730 verifyFormat( 731 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 732 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}"); 733 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 734 " ccccccccccccccccccccccccc) {}"); 735 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 736 " ccccccccccccccccccccccccc) {}"); 737 verifyFormat( 738 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 739 " ccccccccccccccccccccccccc) {}"); 740} 741 742TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) { 743 verifyFormat( 744 "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n" 745 " SI->getAlignment(),\n" 746 " SI->getPointerAddressSpaceee());\n"); 747 verifyFormat( 748 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 749 " Line.Tokens.front().Tok.getLocation(),\n" 750 " Line.Tokens.back().Tok.getLocation());"); 751} 752 753TEST_F(FormatTest, AlignsAfterAssignments) { 754 verifyFormat( 755 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 756 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 757 verifyFormat( 758 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 759 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 760 verifyFormat( 761 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 762 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 763 verifyFormat( 764 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 765 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 766 verifyFormat( 767 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 768 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 769 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 770} 771 772TEST_F(FormatTest, AlignsAfterReturn) { 773 verifyFormat( 774 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 775 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 776 verifyFormat( 777 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 778 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 779} 780 781TEST_F(FormatTest, BreaksConditionalExpressions) { 782 verifyFormat( 783 "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" 784 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 786 verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 787 " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);"); 788} 789 790TEST_F(FormatTest, AlignsStringLiterals) { 791 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 792 " \"short literal\");"); 793 verifyFormat( 794 "looooooooooooooooooooooooongFunction(\n" 795 " \"short literal\"\n" 796 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 797} 798 799TEST_F(FormatTest, AlignsPipes) { 800 verifyFormat( 801 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 802 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 803 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 804 verifyFormat( 805 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 806 " << aaaaaaaaaaaaaaaaaaaa;"); 807 verifyFormat( 808 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 809 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 810 verifyFormat( 811 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 812 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 813 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 814 verifyFormat( 815 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 816 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 817 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 818} 819 820TEST_F(FormatTest, UnderstandsEquals) { 821 verifyFormat( 822 "aaaaaaaaaaaaaaaaa =\n" 823 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 824 verifyFormat( 825 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 826 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 827 verifyFormat( 828 "if (a) {\n" 829 " f();\n" 830 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 831 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 832 833 verifyFormat( 834 // FIXME: Does an expression like this ever make sense? If yes, fix. 835 "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n" 836 " 10000000) {}"); 837} 838 839TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 840 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 841 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 842 843 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 844 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 845 846 verifyFormat( 847 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 848 " Parameter2);"); 849 850 verifyFormat( 851 "ShortObject->shortFunction(\n" 852 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 853 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 854 855 verifyFormat("loooooooooooooongFunction(\n" 856 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 857 858 verifyFormat( 859 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 860 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 861 862 // Here, it is not necessary to wrap at "." or "->". 863 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 864 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 865 verifyFormat( 866 "aaaaaaaaaaa->aaaaaaaaa(\n" 867 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 868 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 869} 870 871TEST_F(FormatTest, WrapsTemplateDeclarations) { 872 verifyFormat("template <typename T>\n" 873 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 874 verifyFormat( 875 "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 876 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 877 verifyFormat( 878 "template <typename T>\n" 879 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 880 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 881 verifyFormat( 882 "template <typename T>\n" 883 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 884 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 886 verifyFormat("template <typename T>\n" 887 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 888 " int aaaaaaaaaaaaaaaaa);"); 889 verifyFormat( 890 "template <typename T1, typename T2 = char, typename T3 = char,\n" 891 " typename T4 = char>\n" 892 "void f();"); 893} 894 895TEST_F(FormatTest, UnderstandsTemplateParameters) { 896 verifyFormat("A<int> a;"); 897 verifyFormat("A<A<A<int> > > a;"); 898 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 899 verifyFormat("bool x = a < 1 || 2 > a;"); 900 verifyFormat("bool x = 5 < f<int>();"); 901 verifyFormat("bool x = f<int>() > 5;"); 902 verifyFormat("bool x = 5 < a<int>::x;"); 903 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 904 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 905 906 verifyGoogleFormat("A<A<int>> a;"); 907 verifyGoogleFormat("A<A<A<int>>> a;"); 908 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 909 910 verifyFormat("test >> a >> b;"); 911 verifyFormat("test << a >> b;"); 912 913 verifyFormat("f<int>();"); 914 verifyFormat("template <typename T> void f() {}"); 915} 916 917TEST_F(FormatTest, UnderstandsUnaryOperators) { 918 verifyFormat("int a = -2;"); 919 verifyFormat("f(-1, -2, -3);"); 920 verifyFormat("a[-1] = 5;"); 921 verifyFormat("int a = 5 + -2;"); 922 verifyFormat("if (i == -1) {}"); 923 verifyFormat("if (i != -1) {}"); 924 verifyFormat("if (i > -1) {}"); 925 verifyFormat("if (i < -1) {}"); 926 verifyFormat("++(a->f());"); 927 verifyFormat("--(a->f());"); 928 verifyFormat("if (!(a->f())) {}"); 929 930 verifyFormat("a-- > b;"); 931 verifyFormat("b ? -a : c;"); 932 verifyFormat("n * sizeof char16;"); 933 verifyFormat("n * alignof char16;"); 934 verifyFormat("sizeof(char);"); 935 verifyFormat("alignof(char);"); 936 937 verifyFormat("return -1;"); 938 verifyFormat("switch (a) {\n" 939 "case -1:\n" 940 " break;\n" 941 "}"); 942} 943 944TEST_F(FormatTest, UndestandsOverloadedOperators) { 945 verifyFormat("bool operator<();"); 946 verifyFormat("bool operator>();"); 947 verifyFormat("bool operator=();"); 948 verifyFormat("bool operator==();"); 949 verifyFormat("bool operator!=();"); 950 verifyFormat("int operator+();"); 951 verifyFormat("int operator++();"); 952 verifyFormat("bool operator();"); 953 verifyFormat("bool operator()();"); 954 verifyFormat("bool operator[]();"); 955 verifyFormat("operator bool();"); 956 verifyFormat("operator SomeType<int>();"); 957 verifyFormat("void *operator new(std::size_t size);"); 958 verifyFormat("void *operator new[](std::size_t size);"); 959 verifyFormat("void operator delete(void *ptr);"); 960 verifyFormat("void operator delete[](void *ptr);"); 961} 962 963TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 964 verifyFormat("int *f(int *a) {}"); 965 verifyFormat("f(a, *a);"); 966 verifyFormat("f(*a);"); 967 verifyFormat("int a = b * 10;"); 968 verifyFormat("int a = 10 * b;"); 969 verifyFormat("int a = b * c;"); 970 verifyFormat("int a += b * c;"); 971 verifyFormat("int a -= b * c;"); 972 verifyFormat("int a *= b * c;"); 973 verifyFormat("int a /= b * c;"); 974 verifyFormat("int a = *b;"); 975 verifyFormat("int a = *b * c;"); 976 verifyFormat("int a = b * *c;"); 977 verifyFormat("int main(int argc, char **argv) {}"); 978 verifyFormat("return 10 * b;"); 979 verifyFormat("return *b * *c;"); 980 verifyFormat("return a & ~b;"); 981 verifyFormat("f(b ? *c : *d);"); 982 verifyFormat("int a = b ? *c : *d;"); 983 verifyFormat("*b = a;"); 984 verifyFormat("a * ~b;"); 985 verifyFormat("a * !b;"); 986 verifyFormat("a * +b;"); 987 verifyFormat("a * -b;"); 988 verifyFormat("a * ++b;"); 989 verifyFormat("a * --b;"); 990 verifyFormat("int *pa = (int *)&a;"); 991 992 verifyFormat("InvalidRegions[*R] = 0;"); 993 994 verifyFormat("A<int *> a;"); 995 verifyFormat("A<int **> a;"); 996 verifyFormat("A<int *, int *> a;"); 997 verifyFormat("A<int **, int **> a;"); 998 verifyFormat("Type *A = static_cast<Type *>(P);"); 999 verifyFormat("Type *A = (Type *)P;"); 1000 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 1001 1002 verifyFormat( 1003 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1005 1006 verifyGoogleFormat("int main(int argc, char** argv) {}"); 1007 verifyGoogleFormat("A<int*> a;"); 1008 verifyGoogleFormat("A<int**> a;"); 1009 verifyGoogleFormat("A<int*, int*> a;"); 1010 verifyGoogleFormat("A<int**, int**> a;"); 1011 verifyGoogleFormat("f(b ? *c : *d);"); 1012 verifyGoogleFormat("int a = b ? *c : *d;"); 1013} 1014 1015TEST_F(FormatTest, FormatsFunctionTypes) { 1016 // FIXME: Determine the cases that need a space after the return type and fix. 1017 verifyFormat("A<bool()> a;"); 1018 verifyFormat("A<SomeType()> a;"); 1019 verifyFormat("A<void(*)(int, std::string)> a;"); 1020 1021 verifyFormat("int(*func)(void *);"); 1022} 1023 1024TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { 1025 verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n" 1026 " int LoooooooooooooooongParam2) {}"); 1027 verifyFormat( 1028 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 1029 " SourceLocation L, IdentifierIn *II,\n" 1030 " Type *T) {}"); 1031} 1032 1033TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 1034 verifyFormat("(a)->b();"); 1035 verifyFormat("--a;"); 1036} 1037 1038TEST_F(FormatTest, HandlesIncludeDirectives) { 1039 EXPECT_EQ("#include <string>\n", format("#include <string>\n")); 1040 EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n")); 1041 EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); 1042 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); 1043 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); 1044 1045 EXPECT_EQ("#import <string>\n", format("#import <string>\n")); 1046 EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); 1047 EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); 1048 EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); 1049 EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); 1050} 1051 1052//===----------------------------------------------------------------------===// 1053// Error recovery tests. 1054//===----------------------------------------------------------------------===// 1055 1056TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 1057 verifyFormat("void f() { return } 42"); 1058} 1059 1060TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 1061 verifyFormat("int aaaaaaaa =\n" 1062 " // Overly long comment\n" 1063 " b;", getLLVMStyleWithColumns(20)); 1064 verifyFormat("function(\n" 1065 " ShortArgument,\n" 1066 " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20)); 1067} 1068 1069TEST_F(FormatTest, IncorrectAccessSpecifier) { 1070 verifyFormat("public:"); 1071 verifyFormat("class A {\n" 1072 "public\n" 1073 " void f() {}\n" 1074 "};"); 1075 verifyFormat("public\n" 1076 "int qwerty;"); 1077 verifyFormat("public\n" 1078 "B {}"); 1079 verifyFormat("public\n" 1080 "{}"); 1081 verifyFormat("public\n" 1082 "B {\n" 1083 " int x;\n" 1084 "}"); 1085} 1086 1087TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 1088 verifyFormat("{"); 1089} 1090 1091TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 1092 verifyFormat("do {}"); 1093 verifyFormat("do {}\n" 1094 "f();"); 1095 verifyFormat("do {}\n" 1096 "wheeee(fun);"); 1097 verifyFormat("do {\n" 1098 " f();\n" 1099 "}"); 1100} 1101 1102TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 1103 verifyFormat("namespace {\n" 1104 "class Foo { Foo ( }; } // comment"); 1105} 1106 1107TEST_F(FormatTest, IncorrectCodeErrorDetection) { 1108 EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); 1109 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 1110 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 1111 EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); 1112 1113 FormatStyle Style = getLLVMStyle(); 1114 Style.ColumnLimit = 10; 1115 EXPECT_EQ("{\n" 1116 " {\n" 1117 " breakme(\n" 1118 " qwe);\n" 1119 "}\n", format("{\n" 1120 " {\n" 1121 " breakme(qwe);\n" 1122 "}\n", Style)); 1123 1124} 1125 1126TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 1127 verifyFormat( 1128 "int x = {\n" 1129 " avariable,\n" 1130 " b(alongervariable)\n" 1131 "};", getLLVMStyleWithColumns(25)); 1132} 1133 1134TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { 1135 verifyFormat( 1136 "Aaa({\n" 1137 " int i;\n" 1138 "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1139 " ccccccccccccccccc));"); 1140} 1141 1142//===----------------------------------------------------------------------===// 1143// Objective-C tests. 1144//===----------------------------------------------------------------------===// 1145 1146TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 1147 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 1148 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 1149 format("-(NSUInteger)indexOfObject:(id)anObject;")); 1150 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 1151 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 1152 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 1153 format("-(NSInteger)Method3:(id)anObject;")); 1154 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 1155 format("-(NSInteger)Method4:(id)anObject;")); 1156 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 1157 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 1158 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 1159 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 1160 EXPECT_EQ( 1161 "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 1162 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 1163 1164 // Very long objectiveC method declaration. 1165 EXPECT_EQ( 1166 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " 1167 "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " 1168 "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " 1169 "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " 1170 "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " 1171 "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", 1172 format( 1173 "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " 1174 "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " 1175 "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " 1176 "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " 1177 "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " 1178 "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); 1179 1180 verifyFormat("- (int)sum:(vector<int>)numbers;"); 1181 verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;"); 1182 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 1183 // protocol lists (but not for template classes): 1184 //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 1185 1186 verifyFormat("- (int(*)())foo:(int(*)())f;"); 1187 verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;"); 1188 1189 // If there's no return type (very rare in practice!), LLVM and Google style 1190 // agree. 1191 verifyFormat("- foo:(int)f;"); 1192 verifyGoogleFormat("- foo:(int)foo;"); 1193} 1194 1195TEST_F(FormatTest, FormatObjCBlocks) { 1196 verifyFormat("int (^Block)(int, int);"); 1197 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); 1198} 1199 1200TEST_F(FormatTest, FormatObjCInterface) { 1201 // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875 1202 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 1203 "@public\n" 1204 " int field1;\n" 1205 "@protected\n" 1206 " int field2;\n" 1207 "@private\n" 1208 " int field3;\n" 1209 "@package\n" 1210 " int field4;\n" 1211 "}\n" 1212 "+ (id)init;\n" 1213 "@end"); 1214 1215 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 1216 " @public\n" 1217 " int field1;\n" 1218 " @protected\n" 1219 " int field2;\n" 1220 " @private\n" 1221 " int field3;\n" 1222 " @package\n" 1223 " int field4;\n" 1224 "}\n" 1225 "+(id) init;\n" 1226 "@end"); 1227 1228 verifyFormat("@interface Foo\n" 1229 "+ (id)init;\n" 1230 "// Look, a comment!\n" 1231 "- (int)answerWith:(int)i;\n" 1232 "@end"); 1233 1234 verifyFormat("@interface Foo\n" 1235 "@end\n" 1236 "@interface Bar\n" 1237 "@end"); 1238 1239 verifyFormat("@interface Foo : Bar\n" 1240 "+ (id)init;\n" 1241 "@end"); 1242 1243 verifyFormat("@interface Foo : Bar <Baz, Quux>\n" 1244 "+ (id)init;\n" 1245 "@end"); 1246 1247 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 1248 "+(id) init;\n" 1249 "@end"); 1250 1251 verifyFormat("@interface Foo (HackStuff)\n" 1252 "+ (id)init;\n" 1253 "@end"); 1254 1255 verifyFormat("@interface Foo ()\n" 1256 "+ (id)init;\n" 1257 "@end"); 1258 1259 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 1260 "+ (id)init;\n" 1261 "@end"); 1262 1263 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 1264 "+(id) init;\n" 1265 "@end"); 1266 1267 verifyFormat("@interface Foo {\n" 1268 " int _i;\n" 1269 "}\n" 1270 "+ (id)init;\n" 1271 "@end"); 1272 1273 verifyFormat("@interface Foo : Bar {\n" 1274 " int _i;\n" 1275 "}\n" 1276 "+ (id)init;\n" 1277 "@end"); 1278 1279 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 1280 " int _i;\n" 1281 "}\n" 1282 "+ (id)init;\n" 1283 "@end"); 1284 1285 verifyFormat("@interface Foo (HackStuff) {\n" 1286 " int _i;\n" 1287 "}\n" 1288 "+ (id)init;\n" 1289 "@end"); 1290 1291 verifyFormat("@interface Foo () {\n" 1292 " int _i;\n" 1293 "}\n" 1294 "+ (id)init;\n" 1295 "@end"); 1296 1297 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 1298 " int _i;\n" 1299 "}\n" 1300 "+ (id)init;\n" 1301 "@end"); 1302} 1303 1304TEST_F(FormatTest, FormatObjCImplementation) { 1305 verifyFormat("@implementation Foo : NSObject {\n" 1306 "@public\n" 1307 " int field1;\n" 1308 "@protected\n" 1309 " int field2;\n" 1310 "@private\n" 1311 " int field3;\n" 1312 "@package\n" 1313 " int field4;\n" 1314 "}\n" 1315 "+ (id)init {}\n" 1316 "@end"); 1317 1318 verifyGoogleFormat("@implementation Foo : NSObject {\n" 1319 " @public\n" 1320 " int field1;\n" 1321 " @protected\n" 1322 " int field2;\n" 1323 " @private\n" 1324 " int field3;\n" 1325 " @package\n" 1326 " int field4;\n" 1327 "}\n" 1328 "+(id) init {}\n" 1329 "@end"); 1330 1331 verifyFormat("@implementation Foo\n" 1332 "+ (id)init {\n" 1333 " if (true)\n" 1334 " return nil;\n" 1335 "}\n" 1336 "// Look, a comment!\n" 1337 "- (int)answerWith:(int)i {\n" 1338 " return i;\n" 1339 "}\n" 1340 "@end"); 1341 1342 verifyFormat("@implementation Foo\n" 1343 "@end\n" 1344 "@implementation Bar\n" 1345 "@end"); 1346 1347 verifyFormat("@implementation Foo : Bar\n" 1348 "+ (id)init {}\n" 1349 "@end"); 1350 1351 verifyFormat("@implementation Foo {\n" 1352 " int _i;\n" 1353 "}\n" 1354 "+ (id)init {}\n" 1355 "@end"); 1356 1357 verifyFormat("@implementation Foo : Bar {\n" 1358 " int _i;\n" 1359 "}\n" 1360 "+ (id)init {}\n" 1361 "@end"); 1362 1363 verifyFormat("@implementation Foo (HackStuff)\n" 1364 "+ (id)init {}\n" 1365 "@end"); 1366} 1367 1368TEST_F(FormatTest, FormatObjCProtocol) { 1369 verifyFormat("@protocol Foo\n" 1370 "@property(weak) id delegate;\n" 1371 "- (NSUInteger)numberOfThings;\n" 1372 "@end"); 1373 1374 verifyFormat("@protocol MyProtocol <NSObject>\n" 1375 "- (NSUInteger)numberOfThings;\n" 1376 "@end"); 1377 1378 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 1379 "-(NSUInteger) numberOfThings;\n" 1380 "@end"); 1381 1382 verifyFormat("@protocol Foo;\n" 1383 "@protocol Bar;\n"); 1384 1385 verifyFormat("@protocol Foo\n" 1386 "@end\n" 1387 "@protocol Bar\n" 1388 "@end"); 1389 1390 verifyFormat("@protocol myProtocol\n" 1391 "- (void)mandatoryWithInt:(int)i;\n" 1392 "@optional\n" 1393 "- (void)optional;\n" 1394 "@required\n" 1395 "- (void)required;\n" 1396 "@optional\n" 1397 "@property(assign) int madProp;\n" 1398 "@end\n"); 1399} 1400 1401TEST_F(FormatTest, ObjCAt) { 1402 verifyFormat("@autoreleasepool"); 1403 verifyFormat("@catch"); 1404 verifyFormat("@class"); 1405 verifyFormat("@compatibility_alias"); 1406 verifyFormat("@defs"); 1407 verifyFormat("@dynamic"); 1408 verifyFormat("@encode"); 1409 verifyFormat("@end"); 1410 verifyFormat("@finally"); 1411 verifyFormat("@implementation"); 1412 verifyFormat("@import"); 1413 verifyFormat("@interface"); 1414 verifyFormat("@optional"); 1415 verifyFormat("@package"); 1416 verifyFormat("@private"); 1417 verifyFormat("@property"); 1418 verifyFormat("@protected"); 1419 verifyFormat("@protocol"); 1420 verifyFormat("@public"); 1421 verifyFormat("@required"); 1422 verifyFormat("@selector"); 1423 verifyFormat("@synchronized"); 1424 verifyFormat("@synthesize"); 1425 verifyFormat("@throw"); 1426 verifyFormat("@try"); 1427 1428 verifyFormat("@\"String\""); 1429 verifyFormat("@1"); 1430 verifyFormat("@+4.8"); 1431 verifyFormat("@-4"); 1432 verifyFormat("@1LL"); 1433 verifyFormat("@.5"); 1434 verifyFormat("@'c'"); 1435 verifyFormat("@true"); 1436 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 1437 verifyFormat("@["); 1438 verifyFormat("@{"); 1439 1440 EXPECT_EQ("@interface", format("@ interface")); 1441 1442 // The precise formatting of this doesn't matter, nobody writes code like 1443 // this. 1444 verifyFormat("@ /*foo*/ interface"); 1445} 1446 1447TEST_F(FormatTest, ObjCSnippets) { 1448 // FIXME: Make the uncommented lines below pass. 1449 verifyFormat("@autoreleasepool {\n" 1450 " foo();\n" 1451 "}"); 1452 verifyFormat("@class Foo, Bar;"); 1453 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 1454 verifyFormat("@dynamic textColor;"); 1455 //verifyFormat("char *buf1 = @encode(int **);"); 1456 verifyFormat("Protocol *proto = @protocol(p1);"); 1457 //verifyFormat("SEL s = @selector(foo:);"); 1458 verifyFormat("@synchronized(self) {\n" 1459 " f();\n" 1460 "}"); 1461 1462 // FIXME: Some Apple code examples don't have spaces around '=' for 1463 // @synthesize, decide if that's desired or not in LLVM style. Google style 1464 // definitely wants spaces. 1465 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 1466 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 1467 1468 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 1469 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 1470 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 1471} 1472 1473} // end namespace tooling 1474} // end namespace clang 1475