FormatTest.cpp revision 154120cde4f56ed04261bab302fdbbed1a7f080b
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 LangOptions LangOpts; 30 LangOpts.CPlusPlus = 1; 31 Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts); 32 tooling::Replacements Replace = 33 reformat(Style, Lex, Context.Sources, Ranges); 34 EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); 35 return Context.getRewrittenText(ID); 36 } 37 38 std::string format(llvm::StringRef Code, 39 const FormatStyle &Style = getLLVMStyle()) { 40 return format(Code, 0, Code.size(), Style); 41 } 42 43 std::string messUp(llvm::StringRef Code) { 44 std::string MessedUp(Code.str()); 45 bool InComment = 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] != ' ') { 53 JustReplacedNewline = false; 54 } else if (MessedUp[i] == '\n') { 55 if (InComment) { 56 InComment = false; 57 } else { 58 JustReplacedNewline = true; 59 MessedUp[i] = ' '; 60 } 61 } 62 } 63 return MessedUp; 64 } 65 66 void verifyFormat(llvm::StringRef Code) { 67 EXPECT_EQ(Code.str(), format(messUp(Code))); 68 } 69 70 void verifyGoogleFormat(llvm::StringRef Code) { 71 EXPECT_EQ(Code.str(), format(messUp(Code), getGoogleStyle())); 72 } 73}; 74 75//===----------------------------------------------------------------------===// 76// Basic function tests. 77//===----------------------------------------------------------------------===// 78 79TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { 80 EXPECT_EQ(";", format(";")); 81} 82 83TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 84 EXPECT_EQ("int i;", format(" int i;")); 85 EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); 86 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 87 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 88} 89 90TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 91 EXPECT_EQ("int i;", format("int\ni;")); 92} 93 94TEST_F(FormatTest, FormatsNestedBlockStatements) { 95 EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}")); 96} 97 98TEST_F(FormatTest, FormatsNestedCall) { 99 verifyFormat("Method(f1, f2(f3));"); 100 verifyFormat("Method(f1(f2, f3()));"); 101} 102 103 104//===----------------------------------------------------------------------===// 105// Tests for control statements. 106//===----------------------------------------------------------------------===// 107 108TEST_F(FormatTest, FormatIfWithoutCompountStatement) { 109 verifyFormat("if (true)\n f();\ng();"); 110 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 111 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 112 verifyFormat("if (a)\n" 113 " // comment\n" 114 " f();"); 115} 116 117TEST_F(FormatTest, ParseIfElse) { 118 verifyFormat("if (true)\n" 119 " if (true)\n" 120 " if (true)\n" 121 " f();\n" 122 " else\n" 123 " g();\n" 124 " else\n" 125 " h();\n" 126 "else\n" 127 " i();"); 128 verifyFormat("if (true)\n" 129 " if (true)\n" 130 " if (true) {\n" 131 " if (true)\n" 132 " f();\n" 133 " } else {\n" 134 " g();\n" 135 " }\n" 136 " else\n" 137 " h();\n" 138 "else {\n" 139 " i();\n" 140 "}"); 141} 142 143TEST_F(FormatTest, ElseIf) { 144 verifyFormat("if (a) {\n" 145 "} else if (b) {\n" 146 "}"); 147 verifyFormat("if (a)\n" 148 " f();\n" 149 "else if (b)\n" 150 " g();\n" 151 "else\n" 152 " h();"); 153} 154 155TEST_F(FormatTest, FormatsForLoop) { 156 verifyFormat( 157 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 158 " ++VeryVeryLongLoopVariable)\n" 159 " ;"); 160 verifyFormat("for (;;)\n" 161 " f();"); 162 verifyFormat("for (;;) {\n" 163 "}"); 164 verifyFormat("for (;;) {\n" 165 " f();\n" 166 "}"); 167} 168 169TEST_F(FormatTest, FormatsWhileLoop) { 170 verifyFormat("while (true) {\n}"); 171 verifyFormat("while (true)\n" 172 " f();"); 173 verifyFormat("while () {\n" 174 "}"); 175 verifyFormat("while () {\n" 176 " f();\n" 177 "}"); 178} 179 180TEST_F(FormatTest, FormatsDoWhile) { 181 verifyFormat("do {\n" 182 " do_something();\n" 183 "} while (something());"); 184 verifyFormat("do\n" 185 " do_something();\n" 186 "while (something());"); 187} 188 189TEST_F(FormatTest, FormatsSwitchStatement) { 190 verifyFormat("switch (x) {\n" 191 "case 1:\n" 192 " f();\n" 193 " break;\n" 194 "case kFoo:\n" 195 "case ns::kBar:\n" 196 "case kBaz:\n" 197 " break;\n" 198 "default:\n" 199 " g();\n" 200 " break;\n" 201 "}"); 202 verifyFormat("switch (x) {\n" 203 "case 1: {\n" 204 " f();\n" 205 " break;\n" 206 "}\n" 207 "}"); 208 verifyFormat("switch (test)\n" 209 " ;"); 210 verifyGoogleFormat("switch (x) {\n" 211 " case 1:\n" 212 " f();\n" 213 " break;\n" 214 " case kFoo:\n" 215 " case ns::kBar:\n" 216 " case kBaz:\n" 217 " break;\n" 218 " default:\n" 219 " g();\n" 220 " break;\n" 221 "}"); 222 verifyGoogleFormat("switch (x) {\n" 223 " case 1: {\n" 224 " f();\n" 225 " break;\n" 226 " }\n" 227 "}"); 228 verifyGoogleFormat("switch (test)\n" 229 " ;"); 230} 231 232TEST_F(FormatTest, FormatsLabels) { 233 verifyFormat("void f() {\n" 234 " some_code();\n" 235 "test_label:\n" 236 " some_other_code();\n" 237 " {\n" 238 " some_more_code();\n" 239 " another_label:\n" 240 " some_more_code();\n" 241 " }\n" 242 "}"); 243 verifyFormat("some_code();\n" 244 "test_label:\n" 245 "some_other_code();"); 246} 247 248 249//===----------------------------------------------------------------------===// 250// Tests for comments. 251//===----------------------------------------------------------------------===// 252 253TEST_F(FormatTest, UnderstandsSingleLineComments) { 254 verifyFormat("// line 1\n" 255 "// line 2\n" 256 "void f() {\n}\n"); 257 258 verifyFormat("void f() {\n" 259 " // Doesn't do anything\n" 260 "}"); 261 262 verifyFormat("int i // This is a fancy variable\n" 263 " = 5;"); 264 265 verifyFormat("enum E {\n" 266 " // comment\n" 267 " VAL_A, // comment\n" 268 " VAL_B\n" 269 "};"); 270 271 verifyFormat( 272 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 273 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 274} 275 276TEST_F(FormatTest, UnderstandsMultiLineComments) { 277 verifyFormat("f(/*test=*/ true);"); 278} 279 280 281//===----------------------------------------------------------------------===// 282// Tests for classes, namespaces, etc. 283//===----------------------------------------------------------------------===// 284 285TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 286 verifyFormat("class A {\n};"); 287} 288 289TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 290 verifyFormat("class A {\n" 291 "public:\n" 292 "protected:\n" 293 "private:\n" 294 " void f() {\n" 295 " }\n" 296 "};"); 297 verifyGoogleFormat("class A {\n" 298 " public:\n" 299 " protected:\n" 300 " private:\n" 301 " void f() {\n" 302 " }\n" 303 "};"); 304} 305 306TEST_F(FormatTest, FormatsDerivedClass) { 307 verifyFormat("class A : public B {\n" 308 "};"); 309 verifyFormat("class A : public ::B {\n" 310 "};"); 311} 312 313TEST_F(FormatTest, FormatsEnum) { 314 verifyFormat("enum {\n" 315 " Zero,\n" 316 " One = 1,\n" 317 " Two = One + 1,\n" 318 " Three = (One + Two),\n" 319 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 320 " Five = (One, Two, Three, Four, 5)\n" 321 "};"); 322 verifyFormat("enum Enum {\n" 323 "};"); 324 verifyFormat("enum {\n" 325 "};"); 326} 327 328TEST_F(FormatTest, FormatsNamespaces) { 329 verifyFormat("namespace some_namespace {\n" 330 "class A {\n" 331 "};\n" 332 "void f() {\n" 333 " f();\n" 334 "}\n" 335 "}"); 336 verifyFormat("namespace {\n" 337 "class A {\n" 338 "};\n" 339 "void f() {\n" 340 " f();\n" 341 "}\n" 342 "}"); 343 verifyFormat("using namespace some_namespace;\n" 344 "class A {\n" 345 "};\n" 346 "void f() {\n" 347 " f();\n" 348 "}"); 349} 350 351TEST_F(FormatTest, StaticInitializers) { 352 verifyFormat("static SomeClass SC = { 1, 'a' };"); 353 354 // FIXME: Format like enums if the static initializer does not fit on a line. 355 verifyFormat( 356 "static SomeClass WithALoooooooooooooooooooongName = { 100000000,\n" 357 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };"); 358} 359 360//===----------------------------------------------------------------------===// 361// Line break tests. 362//===----------------------------------------------------------------------===// 363 364TEST_F(FormatTest, FormatsFunctionDefinition) { 365 verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," 366 " int h, int j, int f,\n" 367 " int c, int ddddddddddddd) {\n" 368 "}"); 369} 370 371TEST_F(FormatTest, FormatsAwesomeMethodCall) { 372 verifyFormat( 373 "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 374 " parameter, parameter, parameter)), SecondLongCall(parameter));"); 375} 376 377TEST_F(FormatTest, ConstructorInitializers) { 378 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}"); 379 380 verifyFormat( 381 "SomeClass::Constructor()\n" 382 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" 383 "}"); 384 385 verifyFormat( 386 "SomeClass::Constructor()\n" 387 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 388 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" 389 "}"); 390 391 verifyFormat("Constructor()\n" 392 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 393 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 394 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 395 " aaaaaaaaaaaaaaaaaaaaaaa() {\n" 396 "}"); 397 398 // Here a line could be saved by splitting the second initializer onto two 399 // lines, but that is not desireable. 400 verifyFormat("Constructor()\n" 401 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 402 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 403 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 404 "}"); 405 406 verifyGoogleFormat("MyClass::MyClass(int var)\n" 407 " : some_var_(var), // 4 space indent\n" 408 " some_other_var_(var + 1) { // lined up\n" 409 "}"); 410} 411 412TEST_F(FormatTest, BreaksAsHighAsPossible) { 413 verifyFormat( 414 "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 415 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 416 " f();"); 417} 418 419TEST_F(FormatTest, BreaksDesireably) { 420 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 421 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 422 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};"); 423 424 verifyFormat( 425 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 426 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 427 428 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 431 432 verifyFormat( 433 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 434 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 435 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 437 438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 439 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 440 441 // This test case breaks on an incorrect memoization, i.e. an optimization not 442 // taking into account the StopAt value. 443 verifyFormat( 444 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 445 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 446 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 447 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 448} 449 450TEST_F(FormatTest, AlignsStringLiterals) { 451 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 452 " \"short literal\");"); 453 verifyFormat( 454 "looooooooooooooooooooooooongFunction(\n" 455 " \"short literal\"\n" 456 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 457} 458 459TEST_F(FormatTest, AlignsPipes) { 460 verifyFormat( 461 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 462 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 463 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 464 verifyFormat( 465 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 466 " << aaaaaaaaaaaaaaaaaaaa;"); 467 verifyFormat( 468 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 469 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 470 verifyFormat( 471 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 472 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 473 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 474 verifyFormat( 475 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 476 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 477 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 478} 479 480TEST_F(FormatTest, UnderstandsEquals) { 481 verifyFormat( 482 "aaaaaaaaaaaaaaaaa =\n" 483 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 484 verifyFormat( 485 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 487 "}"); 488 verifyFormat( 489 "if (a) {\n" 490 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 492 "}"); 493 494 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 495 " 100000000 + 100000000) {\n}"); 496} 497 498TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 499 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 500 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 501 502 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 503 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 504 505 verifyFormat( 506 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 507 " Parameter2);"); 508 509 verifyFormat( 510 "ShortObject->shortFunction(\n" 511 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 512 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 513 514 verifyFormat("loooooooooooooongFunction(\n" 515 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 516 517 verifyFormat( 518 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 519 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 520 521 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 522 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 523 "}"); 524} 525 526TEST_F(FormatTest, UnderstandsTemplateParameters) { 527 verifyFormat("A<int> a;"); 528 verifyFormat("A<A<A<int> > > a;"); 529 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 530 verifyFormat("bool x = a < 1 || 2 > a;"); 531 verifyFormat("bool x = 5 < f<int>();"); 532 verifyFormat("bool x = f<int>() > 5;"); 533 verifyFormat("bool x = 5 < a<int>::x;"); 534 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 535 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 536 537 verifyGoogleFormat("A<A<int>> a;"); 538 verifyGoogleFormat("A<A<A<int>>> a;"); 539 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 540 541 verifyFormat("test >> a >> b;"); 542 verifyFormat("test << a >> b;"); 543 544 verifyFormat("f<int>();"); 545 verifyFormat("template <typename T> void f() {\n}"); 546} 547 548TEST_F(FormatTest, UndestandsUnaryOperators) { 549 verifyFormat("int a = -2;"); 550 verifyFormat("f(-1, -2, -3);"); 551 verifyFormat("a[-1] = 5;"); 552 verifyFormat("int a = 5 + -2;"); 553 verifyFormat("if (i == -1) {\n}"); 554 verifyFormat("if (i != -1) {\n}"); 555 verifyFormat("if (i > -1) {\n}"); 556 verifyFormat("if (i < -1) {\n}"); 557 verifyFormat("++(a->f());"); 558 verifyFormat("--(a->f());"); 559 verifyFormat("if (!(a->f())) {\n}"); 560} 561 562TEST_F(FormatTest, UndestandsOverloadedOperators) { 563 verifyFormat("bool operator<() {\n}"); 564} 565 566TEST_F(FormatTest, UnderstandsUsesOfStar) { 567 verifyFormat("int *f(int *a) {\n}"); 568 verifyFormat("f(a, *a);"); 569 verifyFormat("f(*a);"); 570 verifyFormat("int a = b * 10;"); 571 verifyFormat("int a = 10 * b;"); 572 verifyFormat("int a = b * c;"); 573 verifyFormat("int a += b * c;"); 574 verifyFormat("int a -= b * c;"); 575 verifyFormat("int a *= b * c;"); 576 verifyFormat("int a /= b * c;"); 577 verifyFormat("int a = *b;"); 578 verifyFormat("int a = *b * c;"); 579 verifyFormat("int a = b * *c;"); 580 verifyFormat("int main(int argc, char **argv) {\n}"); 581 582 // FIXME: Is this desired for LLVM? Fix if not. 583 verifyFormat("A<int *> a;"); 584 verifyFormat("A<int **> a;"); 585 verifyFormat("A<int *, int *> a;"); 586 verifyFormat("A<int **, int **> a;"); 587 588 verifyGoogleFormat("int main(int argc, char** argv) {\n}"); 589 verifyGoogleFormat("A<int*> a;"); 590 verifyGoogleFormat("A<int**> a;"); 591 verifyGoogleFormat("A<int*, int*> a;"); 592 verifyGoogleFormat("A<int**, int**> a;"); 593} 594 595TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 596 verifyFormat("(a)->b();"); 597 verifyFormat("--a;"); 598} 599 600TEST_F(FormatTest, HandlesIncludeDirectives) { 601 EXPECT_EQ("#include <string>\n", format("#include <string>\n")); 602 EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); 603 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); 604 EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); 605} 606 607 608//===----------------------------------------------------------------------===// 609// Error recovery tests. 610//===----------------------------------------------------------------------===// 611 612TEST_F(FormatTest, IncorrectAccessSpecifier) { 613 verifyFormat("public:"); 614 verifyFormat("class A {\n" 615 "public\n" 616 " void f() {\n" 617 " }\n" 618 "};"); 619 verifyFormat("public\n" 620 "int qwerty;"); 621 verifyFormat("public\n" 622 "B {\n" 623 "};"); 624 verifyFormat("public\n" 625 "{\n" 626 "};"); 627 verifyFormat("public\n" 628 "B {\n" 629 " int x;\n" 630 "};"); 631} 632 633TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 634 verifyFormat("{"); 635} 636 637TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 638 verifyFormat("do {\n" 639 "};"); 640 verifyFormat("do {\n" 641 "};\n" 642 "f();"); 643 verifyFormat("do {\n" 644 "}\n" 645 "wheeee(fun);"); 646 verifyFormat("do {\n" 647 " f();\n" 648 "};"); 649} 650 651TEST_F(FormatTest, IncorrectCodeErrorDetection) { 652 EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); 653 EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); 654 EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); 655 EXPECT_EQ("{\n {\n }\n }\n}\n", format("{\n {\n }\n }\n}\n")); 656 657 FormatStyle Style = getLLVMStyle(); 658 Style.ColumnLimit = 10; 659 EXPECT_EQ("{\n" 660 " {\n" 661 " breakme(\n" 662 " qwe);\n" 663 "}\n", format("{\n" 664 " {\n" 665 " breakme(qwe);\n" 666 "}\n", Style)); 667 668} 669 670TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 671 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 672 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 673 format("-(NSUInteger)indexOfObject:(id)anObject;")); 674 EXPECT_EQ("- (NSInteger)Mthod1;", 675 format("-(NSInteger)Mthod1;")); 676 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 677 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 678 format("-(NSInteger)Method3:(id)anObject;")); 679 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 680 format("-(NSInteger)Method4:(id)anObject;")); 681 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 682 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 683 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 684 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 685 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", 686 format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); 687} 688 689} // end namespace tooling 690} // end namespace clang 691