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 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 30 31 class FormatTest : public ::testing::Test { 32 protected: 33 enum IncompleteCheck { 34 IC_ExpectComplete, 35 IC_ExpectIncomplete, 36 IC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 IncompleteCheck CheckIncomplete = IC_ExpectComplete) { 42 DEBUG(llvm::errs() << "---\n"); 43 DEBUG(llvm::errs() << Code << "\n\n"); 44 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 45 bool IncompleteFormat = false; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 48 if (CheckIncomplete != IC_DoNotCheck) { 49 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete; 50 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n"; 51 } 52 ReplacementCount = Replaces.size(); 53 auto Result = applyAllReplacements(Code, Replaces); 54 EXPECT_TRUE(static_cast<bool>(Result)); 55 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 56 return *Result; 57 } 58 59 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 60 FormatStyle Style = getLLVMStyle(); 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 66 FormatStyle Style = getGoogleStyle(); 67 Style.ColumnLimit = ColumnLimit; 68 return Style; 69 } 70 71 void verifyFormat(llvm::StringRef Code, 72 const FormatStyle &Style = getLLVMStyle()) { 73 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 74 if (Style.Language == FormatStyle::LK_Cpp) { 75 // Objective-C++ is a superset of C++, so everything checked for C++ 76 // needs to be checked for Objective-C++ as well. 77 FormatStyle ObjCStyle = Style; 78 ObjCStyle.Language = FormatStyle::LK_ObjC; 79 EXPECT_EQ(Code.str(), format(test::messUp(Code), ObjCStyle)); 80 } 81 } 82 83 void verifyIncompleteFormat(llvm::StringRef Code, 84 const FormatStyle &Style = getLLVMStyle()) { 85 EXPECT_EQ(Code.str(), 86 format(test::messUp(Code), Style, IC_ExpectIncomplete)); 87 } 88 89 void verifyGoogleFormat(llvm::StringRef Code) { 90 verifyFormat(Code, getGoogleStyle()); 91 } 92 93 void verifyIndependentOfContext(llvm::StringRef text) { 94 verifyFormat(text); 95 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 96 } 97 98 /// \brief Verify that clang-format does not crash on the given input. 99 void verifyNoCrash(llvm::StringRef Code, 100 const FormatStyle &Style = getLLVMStyle()) { 101 format(Code, Style, IC_DoNotCheck); 102 } 103 104 int ReplacementCount; 105 }; 106 107 TEST_F(FormatTest, MessUp) { 108 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 109 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 110 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 111 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 112 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 113 } 114 115 //===----------------------------------------------------------------------===// 116 // Basic function tests. 117 //===----------------------------------------------------------------------===// 118 119 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 120 EXPECT_EQ(";", format(";")); 121 } 122 123 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 124 EXPECT_EQ("int i;", format(" int i;")); 125 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 126 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 127 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 128 } 129 130 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 131 EXPECT_EQ("int i;", format("int\ni;")); 132 } 133 134 TEST_F(FormatTest, FormatsNestedBlockStatements) { 135 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 136 } 137 138 TEST_F(FormatTest, FormatsNestedCall) { 139 verifyFormat("Method(f1, f2(f3));"); 140 verifyFormat("Method(f1(f2, f3()));"); 141 verifyFormat("Method(f1(f2, (f3())));"); 142 } 143 144 TEST_F(FormatTest, NestedNameSpecifiers) { 145 verifyFormat("vector<::Type> v;"); 146 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 147 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 148 verifyFormat("bool a = 2 < ::SomeFunction();"); 149 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 150 verifyFormat("some::string getName();"); 151 } 152 153 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 154 EXPECT_EQ("if (a) {\n" 155 " f();\n" 156 "}", 157 format("if(a){f();}")); 158 EXPECT_EQ(4, ReplacementCount); 159 EXPECT_EQ("if (a) {\n" 160 " f();\n" 161 "}", 162 format("if (a) {\n" 163 " f();\n" 164 "}")); 165 EXPECT_EQ(0, ReplacementCount); 166 EXPECT_EQ("/*\r\n" 167 "\r\n" 168 "*/\r\n", 169 format("/*\r\n" 170 "\r\n" 171 "*/\r\n")); 172 EXPECT_EQ(0, ReplacementCount); 173 } 174 175 TEST_F(FormatTest, RemovesEmptyLines) { 176 EXPECT_EQ("class C {\n" 177 " int i;\n" 178 "};", 179 format("class C {\n" 180 " int i;\n" 181 "\n" 182 "};")); 183 184 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 185 EXPECT_EQ("namespace N {\n" 186 "\n" 187 "int i;\n" 188 "}", 189 format("namespace N {\n" 190 "\n" 191 "int i;\n" 192 "}", 193 getGoogleStyle())); 194 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 195 "\n" 196 "int i;\n" 197 "}", 198 format("extern /**/ \"C\" /**/ {\n" 199 "\n" 200 "int i;\n" 201 "}", 202 getGoogleStyle())); 203 204 // ...but do keep inlining and removing empty lines for non-block extern "C" 205 // functions. 206 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 207 EXPECT_EQ("extern \"C\" int f() {\n" 208 " int i = 42;\n" 209 " return i;\n" 210 "}", 211 format("extern \"C\" int f() {\n" 212 "\n" 213 " int i = 42;\n" 214 " return i;\n" 215 "}", 216 getGoogleStyle())); 217 218 // Remove empty lines at the beginning and end of blocks. 219 EXPECT_EQ("void f() {\n" 220 "\n" 221 " if (a) {\n" 222 "\n" 223 " f();\n" 224 " }\n" 225 "}", 226 format("void f() {\n" 227 "\n" 228 " if (a) {\n" 229 "\n" 230 " f();\n" 231 "\n" 232 " }\n" 233 "\n" 234 "}", 235 getLLVMStyle())); 236 EXPECT_EQ("void f() {\n" 237 " if (a) {\n" 238 " f();\n" 239 " }\n" 240 "}", 241 format("void f() {\n" 242 "\n" 243 " if (a) {\n" 244 "\n" 245 " f();\n" 246 "\n" 247 " }\n" 248 "\n" 249 "}", 250 getGoogleStyle())); 251 252 // Don't remove empty lines in more complex control statements. 253 EXPECT_EQ("void f() {\n" 254 " if (a) {\n" 255 " f();\n" 256 "\n" 257 " } else if (b) {\n" 258 " f();\n" 259 " }\n" 260 "}", 261 format("void f() {\n" 262 " if (a) {\n" 263 " f();\n" 264 "\n" 265 " } else if (b) {\n" 266 " f();\n" 267 "\n" 268 " }\n" 269 "\n" 270 "}")); 271 272 // FIXME: This is slightly inconsistent. 273 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 274 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 275 EXPECT_EQ("namespace {\n" 276 "int i;\n" 277 "}", 278 format("namespace {\n" 279 "int i;\n" 280 "\n" 281 "}", LLVMWithNoNamespaceFix)); 282 EXPECT_EQ("namespace {\n" 283 "int i;\n" 284 "}", 285 format("namespace {\n" 286 "int i;\n" 287 "\n" 288 "}")); 289 EXPECT_EQ("namespace {\n" 290 "int i;\n" 291 "\n" 292 "} // namespace", 293 format("namespace {\n" 294 "int i;\n" 295 "\n" 296 "} // namespace")); 297 298 FormatStyle Style = getLLVMStyle(); 299 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 300 Style.MaxEmptyLinesToKeep = 2; 301 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 302 Style.BraceWrapping.AfterClass = true; 303 Style.BraceWrapping.AfterFunction = true; 304 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 305 306 EXPECT_EQ("class Foo\n" 307 "{\n" 308 " Foo() {}\n" 309 "\n" 310 " void funk() {}\n" 311 "};", 312 format("class Foo\n" 313 "{\n" 314 " Foo()\n" 315 " {\n" 316 " }\n" 317 "\n" 318 " void funk() {}\n" 319 "};", 320 Style)); 321 } 322 323 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 324 verifyFormat("x = (a) and (b);"); 325 verifyFormat("x = (a) or (b);"); 326 verifyFormat("x = (a) bitand (b);"); 327 verifyFormat("x = (a) bitor (b);"); 328 verifyFormat("x = (a) not_eq (b);"); 329 verifyFormat("x = (a) and_eq (b);"); 330 verifyFormat("x = (a) or_eq (b);"); 331 verifyFormat("x = (a) xor (b);"); 332 } 333 334 //===----------------------------------------------------------------------===// 335 // Tests for control statements. 336 //===----------------------------------------------------------------------===// 337 338 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 339 verifyFormat("if (true)\n f();\ng();"); 340 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 341 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 342 343 FormatStyle AllowsMergedIf = getLLVMStyle(); 344 AllowsMergedIf.AlignEscapedNewlinesLeft = true; 345 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 346 verifyFormat("if (a)\n" 347 " // comment\n" 348 " f();", 349 AllowsMergedIf); 350 verifyFormat("{\n" 351 " if (a)\n" 352 " label:\n" 353 " f();\n" 354 "}", 355 AllowsMergedIf); 356 verifyFormat("#define A \\\n" 357 " if (a) \\\n" 358 " label: \\\n" 359 " f()", 360 AllowsMergedIf); 361 verifyFormat("if (a)\n" 362 " ;", 363 AllowsMergedIf); 364 verifyFormat("if (a)\n" 365 " if (b) return;", 366 AllowsMergedIf); 367 368 verifyFormat("if (a) // Can't merge this\n" 369 " f();\n", 370 AllowsMergedIf); 371 verifyFormat("if (a) /* still don't merge */\n" 372 " f();", 373 AllowsMergedIf); 374 verifyFormat("if (a) { // Never merge this\n" 375 " f();\n" 376 "}", 377 AllowsMergedIf); 378 verifyFormat("if (a) { /* Never merge this */\n" 379 " f();\n" 380 "}", 381 AllowsMergedIf); 382 383 AllowsMergedIf.ColumnLimit = 14; 384 verifyFormat("if (a) return;", AllowsMergedIf); 385 verifyFormat("if (aaaaaaaaa)\n" 386 " return;", 387 AllowsMergedIf); 388 389 AllowsMergedIf.ColumnLimit = 13; 390 verifyFormat("if (a)\n return;", AllowsMergedIf); 391 } 392 393 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 394 FormatStyle AllowsMergedLoops = getLLVMStyle(); 395 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 396 verifyFormat("while (true) continue;", AllowsMergedLoops); 397 verifyFormat("for (;;) continue;", AllowsMergedLoops); 398 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 399 verifyFormat("while (true)\n" 400 " ;", 401 AllowsMergedLoops); 402 verifyFormat("for (;;)\n" 403 " ;", 404 AllowsMergedLoops); 405 verifyFormat("for (;;)\n" 406 " for (;;) continue;", 407 AllowsMergedLoops); 408 verifyFormat("for (;;) // Can't merge this\n" 409 " continue;", 410 AllowsMergedLoops); 411 verifyFormat("for (;;) /* still don't merge */\n" 412 " continue;", 413 AllowsMergedLoops); 414 } 415 416 TEST_F(FormatTest, FormatShortBracedStatements) { 417 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 418 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 419 420 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 421 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 422 423 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 424 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 425 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 426 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 427 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 428 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 429 verifyFormat("if (true) { //\n" 430 " f();\n" 431 "}", 432 AllowSimpleBracedStatements); 433 verifyFormat("if (true) {\n" 434 " f();\n" 435 " f();\n" 436 "}", 437 AllowSimpleBracedStatements); 438 verifyFormat("if (true) {\n" 439 " f();\n" 440 "} else {\n" 441 " f();\n" 442 "}", 443 AllowSimpleBracedStatements); 444 445 verifyFormat("template <int> struct A2 {\n" 446 " struct B {};\n" 447 "};", 448 AllowSimpleBracedStatements); 449 450 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 451 verifyFormat("if (true) {\n" 452 " f();\n" 453 "}", 454 AllowSimpleBracedStatements); 455 verifyFormat("if (true) {\n" 456 " f();\n" 457 "} else {\n" 458 " f();\n" 459 "}", 460 AllowSimpleBracedStatements); 461 462 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 463 verifyFormat("while (true) {\n" 464 " f();\n" 465 "}", 466 AllowSimpleBracedStatements); 467 verifyFormat("for (;;) {\n" 468 " f();\n" 469 "}", 470 AllowSimpleBracedStatements); 471 } 472 473 TEST_F(FormatTest, ParseIfElse) { 474 verifyFormat("if (true)\n" 475 " if (true)\n" 476 " if (true)\n" 477 " f();\n" 478 " else\n" 479 " g();\n" 480 " else\n" 481 " h();\n" 482 "else\n" 483 " i();"); 484 verifyFormat("if (true)\n" 485 " if (true)\n" 486 " if (true) {\n" 487 " if (true)\n" 488 " f();\n" 489 " } else {\n" 490 " g();\n" 491 " }\n" 492 " else\n" 493 " h();\n" 494 "else {\n" 495 " i();\n" 496 "}"); 497 verifyFormat("void f() {\n" 498 " if (a) {\n" 499 " } else {\n" 500 " }\n" 501 "}"); 502 } 503 504 TEST_F(FormatTest, ElseIf) { 505 verifyFormat("if (a) {\n} else if (b) {\n}"); 506 verifyFormat("if (a)\n" 507 " f();\n" 508 "else if (b)\n" 509 " g();\n" 510 "else\n" 511 " h();"); 512 verifyFormat("if (a) {\n" 513 " f();\n" 514 "}\n" 515 "// or else ..\n" 516 "else {\n" 517 " g()\n" 518 "}"); 519 520 verifyFormat("if (a) {\n" 521 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 523 "}"); 524 verifyFormat("if (a) {\n" 525 "} else if (\n" 526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 527 "}", 528 getLLVMStyleWithColumns(62)); 529 } 530 531 TEST_F(FormatTest, FormatsForLoop) { 532 verifyFormat( 533 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 534 " ++VeryVeryLongLoopVariable)\n" 535 " ;"); 536 verifyFormat("for (;;)\n" 537 " f();"); 538 verifyFormat("for (;;) {\n}"); 539 verifyFormat("for (;;) {\n" 540 " f();\n" 541 "}"); 542 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 543 544 verifyFormat( 545 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 546 " E = UnwrappedLines.end();\n" 547 " I != E; ++I) {\n}"); 548 549 verifyFormat( 550 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 551 " ++IIIII) {\n}"); 552 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 553 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 554 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 555 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 556 " I = FD->getDeclsInPrototypeScope().begin(),\n" 557 " E = FD->getDeclsInPrototypeScope().end();\n" 558 " I != E; ++I) {\n}"); 559 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 560 " I = Container.begin(),\n" 561 " E = Container.end();\n" 562 " I != E; ++I) {\n}", 563 getLLVMStyleWithColumns(76)); 564 565 verifyFormat( 566 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 568 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 570 " ++aaaaaaaaaaa) {\n}"); 571 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 572 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 573 " ++i) {\n}"); 574 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 575 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 576 "}"); 577 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 578 " aaaaaaaaaa);\n" 579 " iter; ++iter) {\n" 580 "}"); 581 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 583 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 584 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 585 586 FormatStyle NoBinPacking = getLLVMStyle(); 587 NoBinPacking.BinPackParameters = false; 588 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 589 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 590 " aaaaaaaaaaaaaaaa,\n" 591 " aaaaaaaaaaaaaaaa,\n" 592 " aaaaaaaaaaaaaaaa);\n" 593 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 594 "}", 595 NoBinPacking); 596 verifyFormat( 597 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 598 " E = UnwrappedLines.end();\n" 599 " I != E;\n" 600 " ++I) {\n}", 601 NoBinPacking); 602 } 603 604 TEST_F(FormatTest, RangeBasedForLoops) { 605 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 606 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 607 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 608 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 609 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 611 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 612 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 613 } 614 615 TEST_F(FormatTest, ForEachLoops) { 616 verifyFormat("void f() {\n" 617 " foreach (Item *item, itemlist) {}\n" 618 " Q_FOREACH (Item *item, itemlist) {}\n" 619 " BOOST_FOREACH (Item *item, itemlist) {}\n" 620 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 621 "}"); 622 623 // As function-like macros. 624 verifyFormat("#define foreach(x, y)\n" 625 "#define Q_FOREACH(x, y)\n" 626 "#define BOOST_FOREACH(x, y)\n" 627 "#define UNKNOWN_FOREACH(x, y)\n"); 628 629 // Not as function-like macros. 630 verifyFormat("#define foreach (x, y)\n" 631 "#define Q_FOREACH (x, y)\n" 632 "#define BOOST_FOREACH (x, y)\n" 633 "#define UNKNOWN_FOREACH (x, y)\n"); 634 } 635 636 TEST_F(FormatTest, FormatsWhileLoop) { 637 verifyFormat("while (true) {\n}"); 638 verifyFormat("while (true)\n" 639 " f();"); 640 verifyFormat("while () {\n}"); 641 verifyFormat("while () {\n" 642 " f();\n" 643 "}"); 644 } 645 646 TEST_F(FormatTest, FormatsDoWhile) { 647 verifyFormat("do {\n" 648 " do_something();\n" 649 "} while (something());"); 650 verifyFormat("do\n" 651 " do_something();\n" 652 "while (something());"); 653 } 654 655 TEST_F(FormatTest, FormatsSwitchStatement) { 656 verifyFormat("switch (x) {\n" 657 "case 1:\n" 658 " f();\n" 659 " break;\n" 660 "case kFoo:\n" 661 "case ns::kBar:\n" 662 "case kBaz:\n" 663 " break;\n" 664 "default:\n" 665 " g();\n" 666 " break;\n" 667 "}"); 668 verifyFormat("switch (x) {\n" 669 "case 1: {\n" 670 " f();\n" 671 " break;\n" 672 "}\n" 673 "case 2: {\n" 674 " break;\n" 675 "}\n" 676 "}"); 677 verifyFormat("switch (x) {\n" 678 "case 1: {\n" 679 " f();\n" 680 " {\n" 681 " g();\n" 682 " h();\n" 683 " }\n" 684 " break;\n" 685 "}\n" 686 "}"); 687 verifyFormat("switch (x) {\n" 688 "case 1: {\n" 689 " f();\n" 690 " if (foo) {\n" 691 " g();\n" 692 " h();\n" 693 " }\n" 694 " break;\n" 695 "}\n" 696 "}"); 697 verifyFormat("switch (x) {\n" 698 "case 1: {\n" 699 " f();\n" 700 " g();\n" 701 "} break;\n" 702 "}"); 703 verifyFormat("switch (test)\n" 704 " ;"); 705 verifyFormat("switch (x) {\n" 706 "default: {\n" 707 " // Do nothing.\n" 708 "}\n" 709 "}"); 710 verifyFormat("switch (x) {\n" 711 "// comment\n" 712 "// if 1, do f()\n" 713 "case 1:\n" 714 " f();\n" 715 "}"); 716 verifyFormat("switch (x) {\n" 717 "case 1:\n" 718 " // Do amazing stuff\n" 719 " {\n" 720 " f();\n" 721 " g();\n" 722 " }\n" 723 " break;\n" 724 "}"); 725 verifyFormat("#define A \\\n" 726 " switch (x) { \\\n" 727 " case a: \\\n" 728 " foo = b; \\\n" 729 " }", 730 getLLVMStyleWithColumns(20)); 731 verifyFormat("#define OPERATION_CASE(name) \\\n" 732 " case OP_name: \\\n" 733 " return operations::Operation##name\n", 734 getLLVMStyleWithColumns(40)); 735 verifyFormat("switch (x) {\n" 736 "case 1:;\n" 737 "default:;\n" 738 " int i;\n" 739 "}"); 740 741 verifyGoogleFormat("switch (x) {\n" 742 " case 1:\n" 743 " f();\n" 744 " break;\n" 745 " case kFoo:\n" 746 " case ns::kBar:\n" 747 " case kBaz:\n" 748 " break;\n" 749 " default:\n" 750 " g();\n" 751 " break;\n" 752 "}"); 753 verifyGoogleFormat("switch (x) {\n" 754 " case 1: {\n" 755 " f();\n" 756 " break;\n" 757 " }\n" 758 "}"); 759 verifyGoogleFormat("switch (test)\n" 760 " ;"); 761 762 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 763 " case OP_name: \\\n" 764 " return operations::Operation##name\n"); 765 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 766 " // Get the correction operation class.\n" 767 " switch (OpCode) {\n" 768 " CASE(Add);\n" 769 " CASE(Subtract);\n" 770 " default:\n" 771 " return operations::Unknown;\n" 772 " }\n" 773 "#undef OPERATION_CASE\n" 774 "}"); 775 verifyFormat("DEBUG({\n" 776 " switch (x) {\n" 777 " case A:\n" 778 " f();\n" 779 " break;\n" 780 " // On B:\n" 781 " case B:\n" 782 " g();\n" 783 " break;\n" 784 " }\n" 785 "});"); 786 verifyFormat("switch (a) {\n" 787 "case (b):\n" 788 " return;\n" 789 "}"); 790 791 verifyFormat("switch (a) {\n" 792 "case some_namespace::\n" 793 " some_constant:\n" 794 " return;\n" 795 "}", 796 getLLVMStyleWithColumns(34)); 797 } 798 799 TEST_F(FormatTest, CaseRanges) { 800 verifyFormat("switch (x) {\n" 801 "case 'A' ... 'Z':\n" 802 "case 1 ... 5:\n" 803 "case a ... b:\n" 804 " break;\n" 805 "}"); 806 } 807 808 TEST_F(FormatTest, ShortCaseLabels) { 809 FormatStyle Style = getLLVMStyle(); 810 Style.AllowShortCaseLabelsOnASingleLine = true; 811 verifyFormat("switch (a) {\n" 812 "case 1: x = 1; break;\n" 813 "case 2: return;\n" 814 "case 3:\n" 815 "case 4:\n" 816 "case 5: return;\n" 817 "case 6: // comment\n" 818 " return;\n" 819 "case 7:\n" 820 " // comment\n" 821 " return;\n" 822 "case 8:\n" 823 " x = 8; // comment\n" 824 " break;\n" 825 "default: y = 1; break;\n" 826 "}", 827 Style); 828 verifyFormat("switch (a) {\n" 829 "#if FOO\n" 830 "case 0: return 0;\n" 831 "#endif\n" 832 "}", 833 Style); 834 verifyFormat("switch (a) {\n" 835 "case 1: {\n" 836 "}\n" 837 "case 2: {\n" 838 " return;\n" 839 "}\n" 840 "case 3: {\n" 841 " x = 1;\n" 842 " return;\n" 843 "}\n" 844 "case 4:\n" 845 " if (x)\n" 846 " return;\n" 847 "}", 848 Style); 849 Style.ColumnLimit = 21; 850 verifyFormat("switch (a) {\n" 851 "case 1: x = 1; break;\n" 852 "case 2: return;\n" 853 "case 3:\n" 854 "case 4:\n" 855 "case 5: return;\n" 856 "default:\n" 857 " y = 1;\n" 858 " break;\n" 859 "}", 860 Style); 861 } 862 863 TEST_F(FormatTest, FormatsLabels) { 864 verifyFormat("void f() {\n" 865 " some_code();\n" 866 "test_label:\n" 867 " some_other_code();\n" 868 " {\n" 869 " some_more_code();\n" 870 " another_label:\n" 871 " some_more_code();\n" 872 " }\n" 873 "}"); 874 verifyFormat("{\n" 875 " some_code();\n" 876 "test_label:\n" 877 " some_other_code();\n" 878 "}"); 879 verifyFormat("{\n" 880 " some_code();\n" 881 "test_label:;\n" 882 " int i = 0;\n" 883 "}"); 884 } 885 886 //===----------------------------------------------------------------------===// 887 // Tests for classes, namespaces, etc. 888 //===----------------------------------------------------------------------===// 889 890 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 891 verifyFormat("class A {};"); 892 } 893 894 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 895 verifyFormat("class A {\n" 896 "public:\n" 897 "public: // comment\n" 898 "protected:\n" 899 "private:\n" 900 " void f() {}\n" 901 "};"); 902 verifyGoogleFormat("class A {\n" 903 " public:\n" 904 " protected:\n" 905 " private:\n" 906 " void f() {}\n" 907 "};"); 908 verifyFormat("class A {\n" 909 "public slots:\n" 910 " void f1() {}\n" 911 "public Q_SLOTS:\n" 912 " void f2() {}\n" 913 "protected slots:\n" 914 " void f3() {}\n" 915 "protected Q_SLOTS:\n" 916 " void f4() {}\n" 917 "private slots:\n" 918 " void f5() {}\n" 919 "private Q_SLOTS:\n" 920 " void f6() {}\n" 921 "signals:\n" 922 " void g1();\n" 923 "Q_SIGNALS:\n" 924 " void g2();\n" 925 "};"); 926 927 // Don't interpret 'signals' the wrong way. 928 verifyFormat("signals.set();"); 929 verifyFormat("for (Signals signals : f()) {\n}"); 930 verifyFormat("{\n" 931 " signals.set(); // This needs indentation.\n" 932 "}"); 933 verifyFormat("void f() {\n" 934 "label:\n" 935 " signals.baz();\n" 936 "}"); 937 } 938 939 TEST_F(FormatTest, SeparatesLogicalBlocks) { 940 EXPECT_EQ("class A {\n" 941 "public:\n" 942 " void f();\n" 943 "\n" 944 "private:\n" 945 " void g() {}\n" 946 " // test\n" 947 "protected:\n" 948 " int h;\n" 949 "};", 950 format("class A {\n" 951 "public:\n" 952 "void f();\n" 953 "private:\n" 954 "void g() {}\n" 955 "// test\n" 956 "protected:\n" 957 "int h;\n" 958 "};")); 959 EXPECT_EQ("class A {\n" 960 "protected:\n" 961 "public:\n" 962 " void f();\n" 963 "};", 964 format("class A {\n" 965 "protected:\n" 966 "\n" 967 "public:\n" 968 "\n" 969 " void f();\n" 970 "};")); 971 972 // Even ensure proper spacing inside macros. 973 EXPECT_EQ("#define B \\\n" 974 " class A { \\\n" 975 " protected: \\\n" 976 " public: \\\n" 977 " void f(); \\\n" 978 " };", 979 format("#define B \\\n" 980 " class A { \\\n" 981 " protected: \\\n" 982 " \\\n" 983 " public: \\\n" 984 " \\\n" 985 " void f(); \\\n" 986 " };", 987 getGoogleStyle())); 988 // But don't remove empty lines after macros ending in access specifiers. 989 EXPECT_EQ("#define A private:\n" 990 "\n" 991 "int i;", 992 format("#define A private:\n" 993 "\n" 994 "int i;")); 995 } 996 997 TEST_F(FormatTest, FormatsClasses) { 998 verifyFormat("class A : public B {};"); 999 verifyFormat("class A : public ::B {};"); 1000 1001 verifyFormat( 1002 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1003 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1004 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1005 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1006 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1007 verifyFormat( 1008 "class A : public B, public C, public D, public E, public F {};"); 1009 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1010 " public C,\n" 1011 " public D,\n" 1012 " public E,\n" 1013 " public F,\n" 1014 " public G {};"); 1015 1016 verifyFormat("class\n" 1017 " ReallyReallyLongClassName {\n" 1018 " int i;\n" 1019 "};", 1020 getLLVMStyleWithColumns(32)); 1021 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1022 " aaaaaaaaaaaaaaaa> {};"); 1023 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1024 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1025 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1026 verifyFormat("template <class R, class C>\n" 1027 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1028 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1029 verifyFormat("class ::A::B {};"); 1030 } 1031 1032 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1033 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1034 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1035 1036 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1037 verifyFormat("class MyClass\n" 1038 " : public X\n" 1039 " , public Y {};", 1040 StyleWithInheritanceBreak); 1041 } 1042 1043 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1044 verifyFormat("class A {\n} a, b;"); 1045 verifyFormat("struct A {\n} a, b;"); 1046 verifyFormat("union A {\n} a;"); 1047 } 1048 1049 TEST_F(FormatTest, FormatsEnum) { 1050 verifyFormat("enum {\n" 1051 " Zero,\n" 1052 " One = 1,\n" 1053 " Two = One + 1,\n" 1054 " Three = (One + Two),\n" 1055 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1056 " Five = (One, Two, Three, Four, 5)\n" 1057 "};"); 1058 verifyGoogleFormat("enum {\n" 1059 " Zero,\n" 1060 " One = 1,\n" 1061 " Two = One + 1,\n" 1062 " Three = (One + Two),\n" 1063 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1064 " Five = (One, Two, Three, Four, 5)\n" 1065 "};"); 1066 verifyFormat("enum Enum {};"); 1067 verifyFormat("enum {};"); 1068 verifyFormat("enum X E {} d;"); 1069 verifyFormat("enum __attribute__((...)) E {} d;"); 1070 verifyFormat("enum __declspec__((...)) E {} d;"); 1071 verifyFormat("enum {\n" 1072 " Bar = Foo<int, int>::value\n" 1073 "};", 1074 getLLVMStyleWithColumns(30)); 1075 1076 verifyFormat("enum ShortEnum { A, B, C };"); 1077 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1078 1079 EXPECT_EQ("enum KeepEmptyLines {\n" 1080 " ONE,\n" 1081 "\n" 1082 " TWO,\n" 1083 "\n" 1084 " THREE\n" 1085 "}", 1086 format("enum KeepEmptyLines {\n" 1087 " ONE,\n" 1088 "\n" 1089 " TWO,\n" 1090 "\n" 1091 "\n" 1092 " THREE\n" 1093 "}")); 1094 verifyFormat("enum E { // comment\n" 1095 " ONE,\n" 1096 " TWO\n" 1097 "};\n" 1098 "int i;"); 1099 // Not enums. 1100 verifyFormat("enum X f() {\n" 1101 " a();\n" 1102 " return 42;\n" 1103 "}"); 1104 verifyFormat("enum X Type::f() {\n" 1105 " a();\n" 1106 " return 42;\n" 1107 "}"); 1108 verifyFormat("enum ::X f() {\n" 1109 " a();\n" 1110 " return 42;\n" 1111 "}"); 1112 verifyFormat("enum ns::X f() {\n" 1113 " a();\n" 1114 " return 42;\n" 1115 "}"); 1116 } 1117 1118 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1119 verifyFormat("enum Type {\n" 1120 " One = 0; // These semicolons should be commas.\n" 1121 " Two = 1;\n" 1122 "};"); 1123 verifyFormat("namespace n {\n" 1124 "enum Type {\n" 1125 " One,\n" 1126 " Two, // missing };\n" 1127 " int i;\n" 1128 "}\n" 1129 "void g() {}"); 1130 } 1131 1132 TEST_F(FormatTest, FormatsEnumStruct) { 1133 verifyFormat("enum struct {\n" 1134 " Zero,\n" 1135 " One = 1,\n" 1136 " Two = One + 1,\n" 1137 " Three = (One + Two),\n" 1138 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1139 " Five = (One, Two, Three, Four, 5)\n" 1140 "};"); 1141 verifyFormat("enum struct Enum {};"); 1142 verifyFormat("enum struct {};"); 1143 verifyFormat("enum struct X E {} d;"); 1144 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1145 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1146 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1147 } 1148 1149 TEST_F(FormatTest, FormatsEnumClass) { 1150 verifyFormat("enum class {\n" 1151 " Zero,\n" 1152 " One = 1,\n" 1153 " Two = One + 1,\n" 1154 " Three = (One + Two),\n" 1155 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1156 " Five = (One, Two, Three, Four, 5)\n" 1157 "};"); 1158 verifyFormat("enum class Enum {};"); 1159 verifyFormat("enum class {};"); 1160 verifyFormat("enum class X E {} d;"); 1161 verifyFormat("enum class __attribute__((...)) E {} d;"); 1162 verifyFormat("enum class __declspec__((...)) E {} d;"); 1163 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1164 } 1165 1166 TEST_F(FormatTest, FormatsEnumTypes) { 1167 verifyFormat("enum X : int {\n" 1168 " A, // Force multiple lines.\n" 1169 " B\n" 1170 "};"); 1171 verifyFormat("enum X : int { A, B };"); 1172 verifyFormat("enum X : std::uint32_t { A, B };"); 1173 } 1174 1175 TEST_F(FormatTest, FormatsNSEnums) { 1176 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1177 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1178 " // Information about someDecentlyLongValue.\n" 1179 " someDecentlyLongValue,\n" 1180 " // Information about anotherDecentlyLongValue.\n" 1181 " anotherDecentlyLongValue,\n" 1182 " // Information about aThirdDecentlyLongValue.\n" 1183 " aThirdDecentlyLongValue\n" 1184 "};"); 1185 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1186 " a = 1,\n" 1187 " b = 2,\n" 1188 " c = 3,\n" 1189 "};"); 1190 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1191 " a = 1,\n" 1192 " b = 2,\n" 1193 " c = 3,\n" 1194 "};"); 1195 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1196 " a = 1,\n" 1197 " b = 2,\n" 1198 " c = 3,\n" 1199 "};"); 1200 } 1201 1202 TEST_F(FormatTest, FormatsBitfields) { 1203 verifyFormat("struct Bitfields {\n" 1204 " unsigned sClass : 8;\n" 1205 " unsigned ValueKind : 2;\n" 1206 "};"); 1207 verifyFormat("struct A {\n" 1208 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1209 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1210 "};"); 1211 verifyFormat("struct MyStruct {\n" 1212 " uchar data;\n" 1213 " uchar : 8;\n" 1214 " uchar : 8;\n" 1215 " uchar other;\n" 1216 "};"); 1217 } 1218 1219 TEST_F(FormatTest, FormatsNamespaces) { 1220 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1221 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1222 1223 verifyFormat("namespace some_namespace {\n" 1224 "class A {};\n" 1225 "void f() { f(); }\n" 1226 "}", 1227 LLVMWithNoNamespaceFix); 1228 verifyFormat("namespace {\n" 1229 "class A {};\n" 1230 "void f() { f(); }\n" 1231 "}", 1232 LLVMWithNoNamespaceFix); 1233 verifyFormat("inline namespace X {\n" 1234 "class A {};\n" 1235 "void f() { f(); }\n" 1236 "}", 1237 LLVMWithNoNamespaceFix); 1238 verifyFormat("using namespace some_namespace;\n" 1239 "class A {};\n" 1240 "void f() { f(); }", 1241 LLVMWithNoNamespaceFix); 1242 1243 // This code is more common than we thought; if we 1244 // layout this correctly the semicolon will go into 1245 // its own line, which is undesirable. 1246 verifyFormat("namespace {};", 1247 LLVMWithNoNamespaceFix); 1248 verifyFormat("namespace {\n" 1249 "class A {};\n" 1250 "};", 1251 LLVMWithNoNamespaceFix); 1252 1253 verifyFormat("namespace {\n" 1254 "int SomeVariable = 0; // comment\n" 1255 "} // namespace", 1256 LLVMWithNoNamespaceFix); 1257 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1258 "#define HEADER_GUARD\n" 1259 "namespace my_namespace {\n" 1260 "int i;\n" 1261 "} // my_namespace\n" 1262 "#endif // HEADER_GUARD", 1263 format("#ifndef HEADER_GUARD\n" 1264 " #define HEADER_GUARD\n" 1265 " namespace my_namespace {\n" 1266 "int i;\n" 1267 "} // my_namespace\n" 1268 "#endif // HEADER_GUARD", 1269 LLVMWithNoNamespaceFix)); 1270 1271 EXPECT_EQ("namespace A::B {\n" 1272 "class C {};\n" 1273 "}", 1274 format("namespace A::B {\n" 1275 "class C {};\n" 1276 "}", 1277 LLVMWithNoNamespaceFix)); 1278 1279 FormatStyle Style = getLLVMStyle(); 1280 Style.NamespaceIndentation = FormatStyle::NI_All; 1281 EXPECT_EQ("namespace out {\n" 1282 " int i;\n" 1283 " namespace in {\n" 1284 " int i;\n" 1285 " } // namespace in\n" 1286 "} // namespace out", 1287 format("namespace out {\n" 1288 "int i;\n" 1289 "namespace in {\n" 1290 "int i;\n" 1291 "} // namespace in\n" 1292 "} // namespace out", 1293 Style)); 1294 1295 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1296 EXPECT_EQ("namespace out {\n" 1297 "int i;\n" 1298 "namespace in {\n" 1299 " int i;\n" 1300 "} // namespace in\n" 1301 "} // namespace out", 1302 format("namespace out {\n" 1303 "int i;\n" 1304 "namespace in {\n" 1305 "int i;\n" 1306 "} // namespace in\n" 1307 "} // namespace out", 1308 Style)); 1309 } 1310 1311 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 1312 1313 TEST_F(FormatTest, FormatsInlineASM) { 1314 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1315 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1316 verifyFormat( 1317 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1318 " \"cpuid\\n\\t\"\n" 1319 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1320 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1321 " : \"a\"(value));"); 1322 EXPECT_EQ( 1323 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1324 " __asm {\n" 1325 " mov edx,[that] // vtable in edx\n" 1326 " mov eax,methodIndex\n" 1327 " call [edx][eax*4] // stdcall\n" 1328 " }\n" 1329 "}", 1330 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1331 " __asm {\n" 1332 " mov edx,[that] // vtable in edx\n" 1333 " mov eax,methodIndex\n" 1334 " call [edx][eax*4] // stdcall\n" 1335 " }\n" 1336 "}")); 1337 EXPECT_EQ("_asm {\n" 1338 " xor eax, eax;\n" 1339 " cpuid;\n" 1340 "}", 1341 format("_asm {\n" 1342 " xor eax, eax;\n" 1343 " cpuid;\n" 1344 "}")); 1345 verifyFormat("void function() {\n" 1346 " // comment\n" 1347 " asm(\"\");\n" 1348 "}"); 1349 EXPECT_EQ("__asm {\n" 1350 "}\n" 1351 "int i;", 1352 format("__asm {\n" 1353 "}\n" 1354 "int i;")); 1355 } 1356 1357 TEST_F(FormatTest, FormatTryCatch) { 1358 verifyFormat("try {\n" 1359 " throw a * b;\n" 1360 "} catch (int a) {\n" 1361 " // Do nothing.\n" 1362 "} catch (...) {\n" 1363 " exit(42);\n" 1364 "}"); 1365 1366 // Function-level try statements. 1367 verifyFormat("int f() try { return 4; } catch (...) {\n" 1368 " return 5;\n" 1369 "}"); 1370 verifyFormat("class A {\n" 1371 " int a;\n" 1372 " A() try : a(0) {\n" 1373 " } catch (...) {\n" 1374 " throw;\n" 1375 " }\n" 1376 "};\n"); 1377 1378 // Incomplete try-catch blocks. 1379 verifyIncompleteFormat("try {} catch ("); 1380 } 1381 1382 TEST_F(FormatTest, FormatSEHTryCatch) { 1383 verifyFormat("__try {\n" 1384 " int a = b * c;\n" 1385 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1386 " // Do nothing.\n" 1387 "}"); 1388 1389 verifyFormat("__try {\n" 1390 " int a = b * c;\n" 1391 "} __finally {\n" 1392 " // Do nothing.\n" 1393 "}"); 1394 1395 verifyFormat("DEBUG({\n" 1396 " __try {\n" 1397 " } __finally {\n" 1398 " }\n" 1399 "});\n"); 1400 } 1401 1402 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1403 verifyFormat("try {\n" 1404 " f();\n" 1405 "} catch {\n" 1406 " g();\n" 1407 "}"); 1408 verifyFormat("try {\n" 1409 " f();\n" 1410 "} catch (A a) MACRO(x) {\n" 1411 " g();\n" 1412 "} catch (B b) MACRO(x) {\n" 1413 " g();\n" 1414 "}"); 1415 } 1416 1417 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1418 FormatStyle Style = getLLVMStyle(); 1419 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1420 FormatStyle::BS_WebKit}) { 1421 Style.BreakBeforeBraces = BraceStyle; 1422 verifyFormat("try {\n" 1423 " // something\n" 1424 "} catch (...) {\n" 1425 " // something\n" 1426 "}", 1427 Style); 1428 } 1429 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1430 verifyFormat("try {\n" 1431 " // something\n" 1432 "}\n" 1433 "catch (...) {\n" 1434 " // something\n" 1435 "}", 1436 Style); 1437 verifyFormat("__try {\n" 1438 " // something\n" 1439 "}\n" 1440 "__finally {\n" 1441 " // something\n" 1442 "}", 1443 Style); 1444 verifyFormat("@try {\n" 1445 " // something\n" 1446 "}\n" 1447 "@finally {\n" 1448 " // something\n" 1449 "}", 1450 Style); 1451 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1452 verifyFormat("try\n" 1453 "{\n" 1454 " // something\n" 1455 "}\n" 1456 "catch (...)\n" 1457 "{\n" 1458 " // something\n" 1459 "}", 1460 Style); 1461 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 1462 verifyFormat("try\n" 1463 " {\n" 1464 " // something\n" 1465 " }\n" 1466 "catch (...)\n" 1467 " {\n" 1468 " // something\n" 1469 " }", 1470 Style); 1471 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1472 Style.BraceWrapping.BeforeCatch = true; 1473 verifyFormat("try {\n" 1474 " // something\n" 1475 "}\n" 1476 "catch (...) {\n" 1477 " // something\n" 1478 "}", 1479 Style); 1480 } 1481 1482 TEST_F(FormatTest, StaticInitializers) { 1483 verifyFormat("static SomeClass SC = {1, 'a'};"); 1484 1485 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1486 " 100000000, " 1487 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1488 1489 // Here, everything other than the "}" would fit on a line. 1490 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1491 " 10000000000000000000000000};"); 1492 EXPECT_EQ("S s = {a,\n" 1493 "\n" 1494 " b};", 1495 format("S s = {\n" 1496 " a,\n" 1497 "\n" 1498 " b\n" 1499 "};")); 1500 1501 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1502 // line. However, the formatting looks a bit off and this probably doesn't 1503 // happen often in practice. 1504 verifyFormat("static int Variable[1] = {\n" 1505 " {1000000000000000000000000000000000000}};", 1506 getLLVMStyleWithColumns(40)); 1507 } 1508 1509 TEST_F(FormatTest, DesignatedInitializers) { 1510 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1511 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1512 " .bbbbbbbbbb = 2,\n" 1513 " .cccccccccc = 3,\n" 1514 " .dddddddddd = 4,\n" 1515 " .eeeeeeeeee = 5};"); 1516 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1517 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1518 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1519 " .ccccccccccccccccccccccccccc = 3,\n" 1520 " .ddddddddddddddddddddddddddd = 4,\n" 1521 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1522 1523 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1524 } 1525 1526 TEST_F(FormatTest, NestedStaticInitializers) { 1527 verifyFormat("static A x = {{{}}};\n"); 1528 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 1529 " {init1, init2, init3, init4}}};", 1530 getLLVMStyleWithColumns(50)); 1531 1532 verifyFormat("somes Status::global_reps[3] = {\n" 1533 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1534 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1535 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 1536 getLLVMStyleWithColumns(60)); 1537 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 1538 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1539 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1540 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 1541 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 1542 " {rect.fRight - rect.fLeft, rect.fBottom - " 1543 "rect.fTop}};"); 1544 1545 verifyFormat( 1546 "SomeArrayOfSomeType a = {\n" 1547 " {{1, 2, 3},\n" 1548 " {1, 2, 3},\n" 1549 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 1550 " 333333333333333333333333333333},\n" 1551 " {1, 2, 3},\n" 1552 " {1, 2, 3}}};"); 1553 verifyFormat( 1554 "SomeArrayOfSomeType a = {\n" 1555 " {{1, 2, 3}},\n" 1556 " {{1, 2, 3}},\n" 1557 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 1558 " 333333333333333333333333333333}},\n" 1559 " {{1, 2, 3}},\n" 1560 " {{1, 2, 3}}};"); 1561 1562 verifyFormat("struct {\n" 1563 " unsigned bit;\n" 1564 " const char *const name;\n" 1565 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 1566 " {kOsWin, \"Windows\"},\n" 1567 " {kOsLinux, \"Linux\"},\n" 1568 " {kOsCrOS, \"Chrome OS\"}};"); 1569 verifyFormat("struct {\n" 1570 " unsigned bit;\n" 1571 " const char *const name;\n" 1572 "} kBitsToOs[] = {\n" 1573 " {kOsMac, \"Mac\"},\n" 1574 " {kOsWin, \"Windows\"},\n" 1575 " {kOsLinux, \"Linux\"},\n" 1576 " {kOsCrOS, \"Chrome OS\"},\n" 1577 "};"); 1578 } 1579 1580 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 1581 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1582 " \\\n" 1583 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 1584 } 1585 1586 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 1587 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 1588 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 1589 1590 // Do break defaulted and deleted functions. 1591 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 1592 " default;", 1593 getLLVMStyleWithColumns(40)); 1594 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 1595 " delete;", 1596 getLLVMStyleWithColumns(40)); 1597 } 1598 1599 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 1600 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 1601 getLLVMStyleWithColumns(40)); 1602 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 1603 getLLVMStyleWithColumns(40)); 1604 EXPECT_EQ("#define Q \\\n" 1605 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 1606 " \"aaaaaaaa.cpp\"", 1607 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 1608 getLLVMStyleWithColumns(40))); 1609 } 1610 1611 TEST_F(FormatTest, UnderstandsLinePPDirective) { 1612 EXPECT_EQ("# 123 \"A string literal\"", 1613 format(" # 123 \"A string literal\"")); 1614 } 1615 1616 TEST_F(FormatTest, LayoutUnknownPPDirective) { 1617 EXPECT_EQ("#;", format("#;")); 1618 verifyFormat("#\n;\n;\n;"); 1619 } 1620 1621 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 1622 EXPECT_EQ("#line 42 \"test\"\n", 1623 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 1624 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 1625 getLLVMStyleWithColumns(12))); 1626 } 1627 1628 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 1629 EXPECT_EQ("#line 42 \"test\"", 1630 format("# \\\n line \\\n 42 \\\n \"test\"")); 1631 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 1632 } 1633 1634 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 1635 verifyFormat("#define A \\x20"); 1636 verifyFormat("#define A \\ x20"); 1637 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 1638 verifyFormat("#define A ''"); 1639 verifyFormat("#define A ''qqq"); 1640 verifyFormat("#define A `qqq"); 1641 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 1642 EXPECT_EQ("const char *c = STRINGIFY(\n" 1643 "\\na : b);", 1644 format("const char * c = STRINGIFY(\n" 1645 "\\na : b);")); 1646 1647 verifyFormat("a\r\\"); 1648 verifyFormat("a\v\\"); 1649 verifyFormat("a\f\\"); 1650 } 1651 1652 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 1653 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 1654 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 1655 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 1656 // FIXME: We never break before the macro name. 1657 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 1658 1659 verifyFormat("#define A A\n#define A A"); 1660 verifyFormat("#define A(X) A\n#define A A"); 1661 1662 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 1663 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 1664 } 1665 1666 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 1667 EXPECT_EQ("// somecomment\n" 1668 "#include \"a.h\"\n" 1669 "#define A( \\\n" 1670 " A, B)\n" 1671 "#include \"b.h\"\n" 1672 "// somecomment\n", 1673 format(" // somecomment\n" 1674 " #include \"a.h\"\n" 1675 "#define A(A,\\\n" 1676 " B)\n" 1677 " #include \"b.h\"\n" 1678 " // somecomment\n", 1679 getLLVMStyleWithColumns(13))); 1680 } 1681 1682 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 1683 1684 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 1685 EXPECT_EQ("#define A \\\n" 1686 " c; \\\n" 1687 " e;\n" 1688 "f;", 1689 format("#define A c; e;\n" 1690 "f;", 1691 getLLVMStyleWithColumns(14))); 1692 } 1693 1694 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 1695 1696 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 1697 EXPECT_EQ("int x,\n" 1698 "#define A\n" 1699 " y;", 1700 format("int x,\n#define A\ny;")); 1701 } 1702 1703 TEST_F(FormatTest, HashInMacroDefinition) { 1704 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 1705 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 1706 verifyFormat("#define A \\\n" 1707 " { \\\n" 1708 " f(#c); \\\n" 1709 " }", 1710 getLLVMStyleWithColumns(11)); 1711 1712 verifyFormat("#define A(X) \\\n" 1713 " void function##X()", 1714 getLLVMStyleWithColumns(22)); 1715 1716 verifyFormat("#define A(a, b, c) \\\n" 1717 " void a##b##c()", 1718 getLLVMStyleWithColumns(22)); 1719 1720 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 1721 } 1722 1723 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 1724 EXPECT_EQ("#define A (x)", format("#define A (x)")); 1725 EXPECT_EQ("#define A(x)", format("#define A(x)")); 1726 } 1727 1728 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 1729 EXPECT_EQ("#define A b;", format("#define A \\\n" 1730 " \\\n" 1731 " b;", 1732 getLLVMStyleWithColumns(25))); 1733 EXPECT_EQ("#define A \\\n" 1734 " \\\n" 1735 " a; \\\n" 1736 " b;", 1737 format("#define A \\\n" 1738 " \\\n" 1739 " a; \\\n" 1740 " b;", 1741 getLLVMStyleWithColumns(11))); 1742 EXPECT_EQ("#define A \\\n" 1743 " a; \\\n" 1744 " \\\n" 1745 " b;", 1746 format("#define A \\\n" 1747 " a; \\\n" 1748 " \\\n" 1749 " b;", 1750 getLLVMStyleWithColumns(11))); 1751 } 1752 1753 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 1754 verifyIncompleteFormat("#define A :"); 1755 verifyFormat("#define SOMECASES \\\n" 1756 " case 1: \\\n" 1757 " case 2\n", 1758 getLLVMStyleWithColumns(20)); 1759 verifyFormat("#define MACRO(a) \\\n" 1760 " if (a) \\\n" 1761 " f(); \\\n" 1762 " else \\\n" 1763 " g()", 1764 getLLVMStyleWithColumns(18)); 1765 verifyFormat("#define A template <typename T>"); 1766 verifyIncompleteFormat("#define STR(x) #x\n" 1767 "f(STR(this_is_a_string_literal{));"); 1768 verifyFormat("#pragma omp threadprivate( \\\n" 1769 " y)), // expected-warning", 1770 getLLVMStyleWithColumns(28)); 1771 verifyFormat("#d, = };"); 1772 verifyFormat("#if \"a"); 1773 verifyIncompleteFormat("({\n" 1774 "#define b \\\n" 1775 " } \\\n" 1776 " a\n" 1777 "a", 1778 getLLVMStyleWithColumns(15)); 1779 verifyFormat("#define A \\\n" 1780 " { \\\n" 1781 " {\n" 1782 "#define B \\\n" 1783 " } \\\n" 1784 " }", 1785 getLLVMStyleWithColumns(15)); 1786 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 1787 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 1788 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 1789 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 1790 } 1791 1792 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 1793 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 1794 EXPECT_EQ("class A : public QObject {\n" 1795 " Q_OBJECT\n" 1796 "\n" 1797 " A() {}\n" 1798 "};", 1799 format("class A : public QObject {\n" 1800 " Q_OBJECT\n" 1801 "\n" 1802 " A() {\n}\n" 1803 "} ;")); 1804 EXPECT_EQ("MACRO\n" 1805 "/*static*/ int i;", 1806 format("MACRO\n" 1807 " /*static*/ int i;")); 1808 EXPECT_EQ("SOME_MACRO\n" 1809 "namespace {\n" 1810 "void f();\n" 1811 "} // namespace", 1812 format("SOME_MACRO\n" 1813 " namespace {\n" 1814 "void f( );\n" 1815 "} // namespace")); 1816 // Only if the identifier contains at least 5 characters. 1817 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 1818 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 1819 // Only if everything is upper case. 1820 EXPECT_EQ("class A : public QObject {\n" 1821 " Q_Object A() {}\n" 1822 "};", 1823 format("class A : public QObject {\n" 1824 " Q_Object\n" 1825 " A() {\n}\n" 1826 "} ;")); 1827 1828 // Only if the next line can actually start an unwrapped line. 1829 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 1830 format("SOME_WEIRD_LOG_MACRO\n" 1831 "<< SomeThing;")); 1832 1833 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 1834 "(n, buffers))\n", 1835 getChromiumStyle(FormatStyle::LK_Cpp)); 1836 } 1837 1838 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 1839 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 1840 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 1841 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 1842 "class X {};\n" 1843 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 1844 "int *createScopDetectionPass() { return 0; }", 1845 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 1846 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 1847 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 1848 " class X {};\n" 1849 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 1850 " int *createScopDetectionPass() { return 0; }")); 1851 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 1852 // braces, so that inner block is indented one level more. 1853 EXPECT_EQ("int q() {\n" 1854 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 1855 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 1856 " IPC_END_MESSAGE_MAP()\n" 1857 "}", 1858 format("int q() {\n" 1859 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 1860 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 1861 " IPC_END_MESSAGE_MAP()\n" 1862 "}")); 1863 1864 // Same inside macros. 1865 EXPECT_EQ("#define LIST(L) \\\n" 1866 " L(A) \\\n" 1867 " L(B) \\\n" 1868 " L(C)", 1869 format("#define LIST(L) \\\n" 1870 " L(A) \\\n" 1871 " L(B) \\\n" 1872 " L(C)", 1873 getGoogleStyle())); 1874 1875 // These must not be recognized as macros. 1876 EXPECT_EQ("int q() {\n" 1877 " f(x);\n" 1878 " f(x) {}\n" 1879 " f(x)->g();\n" 1880 " f(x)->*g();\n" 1881 " f(x).g();\n" 1882 " f(x) = x;\n" 1883 " f(x) += x;\n" 1884 " f(x) -= x;\n" 1885 " f(x) *= x;\n" 1886 " f(x) /= x;\n" 1887 " f(x) %= x;\n" 1888 " f(x) &= x;\n" 1889 " f(x) |= x;\n" 1890 " f(x) ^= x;\n" 1891 " f(x) >>= x;\n" 1892 " f(x) <<= x;\n" 1893 " f(x)[y].z();\n" 1894 " LOG(INFO) << x;\n" 1895 " ifstream(x) >> x;\n" 1896 "}\n", 1897 format("int q() {\n" 1898 " f(x)\n;\n" 1899 " f(x)\n {}\n" 1900 " f(x)\n->g();\n" 1901 " f(x)\n->*g();\n" 1902 " f(x)\n.g();\n" 1903 " f(x)\n = x;\n" 1904 " f(x)\n += x;\n" 1905 " f(x)\n -= x;\n" 1906 " f(x)\n *= x;\n" 1907 " f(x)\n /= x;\n" 1908 " f(x)\n %= x;\n" 1909 " f(x)\n &= x;\n" 1910 " f(x)\n |= x;\n" 1911 " f(x)\n ^= x;\n" 1912 " f(x)\n >>= x;\n" 1913 " f(x)\n <<= x;\n" 1914 " f(x)\n[y].z();\n" 1915 " LOG(INFO)\n << x;\n" 1916 " ifstream(x)\n >> x;\n" 1917 "}\n")); 1918 EXPECT_EQ("int q() {\n" 1919 " F(x)\n" 1920 " if (1) {\n" 1921 " }\n" 1922 " F(x)\n" 1923 " while (1) {\n" 1924 " }\n" 1925 " F(x)\n" 1926 " G(x);\n" 1927 " F(x)\n" 1928 " try {\n" 1929 " Q();\n" 1930 " } catch (...) {\n" 1931 " }\n" 1932 "}\n", 1933 format("int q() {\n" 1934 "F(x)\n" 1935 "if (1) {}\n" 1936 "F(x)\n" 1937 "while (1) {}\n" 1938 "F(x)\n" 1939 "G(x);\n" 1940 "F(x)\n" 1941 "try { Q(); } catch (...) {}\n" 1942 "}\n")); 1943 EXPECT_EQ("class A {\n" 1944 " A() : t(0) {}\n" 1945 " A(int i) noexcept() : {}\n" 1946 " A(X x)\n" // FIXME: function-level try blocks are broken. 1947 " try : t(0) {\n" 1948 " } catch (...) {\n" 1949 " }\n" 1950 "};", 1951 format("class A {\n" 1952 " A()\n : t(0) {}\n" 1953 " A(int i)\n noexcept() : {}\n" 1954 " A(X x)\n" 1955 " try : t(0) {} catch (...) {}\n" 1956 "};")); 1957 EXPECT_EQ("class SomeClass {\n" 1958 "public:\n" 1959 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 1960 "};", 1961 format("class SomeClass {\n" 1962 "public:\n" 1963 " SomeClass()\n" 1964 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 1965 "};")); 1966 EXPECT_EQ("class SomeClass {\n" 1967 "public:\n" 1968 " SomeClass()\n" 1969 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 1970 "};", 1971 format("class SomeClass {\n" 1972 "public:\n" 1973 " SomeClass()\n" 1974 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 1975 "};", 1976 getLLVMStyleWithColumns(40))); 1977 1978 verifyFormat("MACRO(>)"); 1979 } 1980 1981 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 1982 verifyFormat("#define A \\\n" 1983 " f({ \\\n" 1984 " g(); \\\n" 1985 " });", 1986 getLLVMStyleWithColumns(11)); 1987 } 1988 1989 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 1990 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 1991 } 1992 1993 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 1994 verifyFormat("{\n { a #c; }\n}"); 1995 } 1996 1997 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 1998 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 1999 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2000 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2001 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2002 } 2003 2004 TEST_F(FormatTest, EscapedNewlines) { 2005 EXPECT_EQ( 2006 "#define A \\\n int i; \\\n int j;", 2007 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2008 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2009 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2010 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2011 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2012 } 2013 2014 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2015 verifyFormat("#define A \\\n" 2016 " int v( \\\n" 2017 " a); \\\n" 2018 " int i;", 2019 getLLVMStyleWithColumns(11)); 2020 } 2021 2022 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2023 EXPECT_EQ( 2024 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2025 " \\\n" 2026 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2027 "\n" 2028 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2029 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2030 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2031 "\\\n" 2032 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2033 " \n" 2034 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2035 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2036 } 2037 2038 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2039 EXPECT_EQ("int\n" 2040 "#define A\n" 2041 " a;", 2042 format("int\n#define A\na;")); 2043 verifyFormat("functionCallTo(\n" 2044 " someOtherFunction(\n" 2045 " withSomeParameters, whichInSequence,\n" 2046 " areLongerThanALine(andAnotherCall,\n" 2047 "#define A B\n" 2048 " withMoreParamters,\n" 2049 " whichStronglyInfluenceTheLayout),\n" 2050 " andMoreParameters),\n" 2051 " trailing);", 2052 getLLVMStyleWithColumns(69)); 2053 verifyFormat("Foo::Foo()\n" 2054 "#ifdef BAR\n" 2055 " : baz(0)\n" 2056 "#endif\n" 2057 "{\n" 2058 "}"); 2059 verifyFormat("void f() {\n" 2060 " if (true)\n" 2061 "#ifdef A\n" 2062 " f(42);\n" 2063 " x();\n" 2064 "#else\n" 2065 " g();\n" 2066 " x();\n" 2067 "#endif\n" 2068 "}"); 2069 verifyFormat("void f(param1, param2,\n" 2070 " param3,\n" 2071 "#ifdef A\n" 2072 " param4(param5,\n" 2073 "#ifdef A1\n" 2074 " param6,\n" 2075 "#ifdef A2\n" 2076 " param7),\n" 2077 "#else\n" 2078 " param8),\n" 2079 " param9,\n" 2080 "#endif\n" 2081 " param10,\n" 2082 "#endif\n" 2083 " param11)\n" 2084 "#else\n" 2085 " param12)\n" 2086 "#endif\n" 2087 "{\n" 2088 " x();\n" 2089 "}", 2090 getLLVMStyleWithColumns(28)); 2091 verifyFormat("#if 1\n" 2092 "int i;"); 2093 verifyFormat("#if 1\n" 2094 "#endif\n" 2095 "#if 1\n" 2096 "#else\n" 2097 "#endif\n"); 2098 verifyFormat("DEBUG({\n" 2099 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2101 "});\n" 2102 "#if a\n" 2103 "#else\n" 2104 "#endif"); 2105 2106 verifyIncompleteFormat("void f(\n" 2107 "#if A\n" 2108 " );\n" 2109 "#else\n" 2110 "#endif"); 2111 } 2112 2113 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2114 verifyFormat("#endif\n" 2115 "#if B"); 2116 } 2117 2118 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2119 FormatStyle SingleLine = getLLVMStyle(); 2120 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2121 verifyFormat("#if 0\n" 2122 "#elif 1\n" 2123 "#endif\n" 2124 "void foo() {\n" 2125 " if (test) foo2();\n" 2126 "}", 2127 SingleLine); 2128 } 2129 2130 TEST_F(FormatTest, LayoutBlockInsideParens) { 2131 verifyFormat("functionCall({ int i; });"); 2132 verifyFormat("functionCall({\n" 2133 " int i;\n" 2134 " int j;\n" 2135 "});"); 2136 verifyFormat("functionCall(\n" 2137 " {\n" 2138 " int i;\n" 2139 " int j;\n" 2140 " },\n" 2141 " aaaa, bbbb, cccc);"); 2142 verifyFormat("functionA(functionB({\n" 2143 " int i;\n" 2144 " int j;\n" 2145 " }),\n" 2146 " aaaa, bbbb, cccc);"); 2147 verifyFormat("functionCall(\n" 2148 " {\n" 2149 " int i;\n" 2150 " int j;\n" 2151 " },\n" 2152 " aaaa, bbbb, // comment\n" 2153 " cccc);"); 2154 verifyFormat("functionA(functionB({\n" 2155 " int i;\n" 2156 " int j;\n" 2157 " }),\n" 2158 " aaaa, bbbb, // comment\n" 2159 " cccc);"); 2160 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2161 verifyFormat("functionCall(aaaa, bbbb, {\n" 2162 " int i;\n" 2163 " int j;\n" 2164 "});"); 2165 verifyFormat( 2166 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2167 " {\n" 2168 " int i; // break\n" 2169 " },\n" 2170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2171 " ccccccccccccccccc));"); 2172 verifyFormat("DEBUG({\n" 2173 " if (a)\n" 2174 " f();\n" 2175 "});"); 2176 } 2177 2178 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2179 EXPECT_EQ("SOME_MACRO { int i; }\n" 2180 "int i;", 2181 format(" SOME_MACRO {int i;} int i;")); 2182 } 2183 2184 TEST_F(FormatTest, LayoutNestedBlocks) { 2185 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2186 " struct s {\n" 2187 " int i;\n" 2188 " };\n" 2189 " s kBitsToOs[] = {{10}};\n" 2190 " for (int i = 0; i < 10; ++i)\n" 2191 " return;\n" 2192 "}"); 2193 verifyFormat("call(parameter, {\n" 2194 " something();\n" 2195 " // Comment using all columns.\n" 2196 " somethingelse();\n" 2197 "});", 2198 getLLVMStyleWithColumns(40)); 2199 verifyFormat("DEBUG( //\n" 2200 " { f(); }, a);"); 2201 verifyFormat("DEBUG( //\n" 2202 " {\n" 2203 " f(); //\n" 2204 " },\n" 2205 " a);"); 2206 2207 EXPECT_EQ("call(parameter, {\n" 2208 " something();\n" 2209 " // Comment too\n" 2210 " // looooooooooong.\n" 2211 " somethingElse();\n" 2212 "});", 2213 format("call(parameter, {\n" 2214 " something();\n" 2215 " // Comment too looooooooooong.\n" 2216 " somethingElse();\n" 2217 "});", 2218 getLLVMStyleWithColumns(29))); 2219 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2220 EXPECT_EQ("DEBUG({ // comment\n" 2221 " int i;\n" 2222 "});", 2223 format("DEBUG({ // comment\n" 2224 "int i;\n" 2225 "});")); 2226 EXPECT_EQ("DEBUG({\n" 2227 " int i;\n" 2228 "\n" 2229 " // comment\n" 2230 " int j;\n" 2231 "});", 2232 format("DEBUG({\n" 2233 " int i;\n" 2234 "\n" 2235 " // comment\n" 2236 " int j;\n" 2237 "});")); 2238 2239 verifyFormat("DEBUG({\n" 2240 " if (a)\n" 2241 " return;\n" 2242 "});"); 2243 verifyGoogleFormat("DEBUG({\n" 2244 " if (a) return;\n" 2245 "});"); 2246 FormatStyle Style = getGoogleStyle(); 2247 Style.ColumnLimit = 45; 2248 verifyFormat("Debug(aaaaa,\n" 2249 " {\n" 2250 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 2251 " },\n" 2252 " a);", 2253 Style); 2254 2255 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 2256 2257 verifyNoCrash("^{v^{a}}"); 2258 } 2259 2260 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 2261 EXPECT_EQ("#define MACRO() \\\n" 2262 " Debug(aaa, /* force line break */ \\\n" 2263 " { \\\n" 2264 " int i; \\\n" 2265 " int j; \\\n" 2266 " })", 2267 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 2268 " { int i; int j; })", 2269 getGoogleStyle())); 2270 2271 EXPECT_EQ("#define A \\\n" 2272 " [] { \\\n" 2273 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2274 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 2275 " }", 2276 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2277 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 2278 getGoogleStyle())); 2279 } 2280 2281 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2282 EXPECT_EQ("{}", format("{}")); 2283 verifyFormat("enum E {};"); 2284 verifyFormat("enum E {}"); 2285 } 2286 2287 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 2288 FormatStyle Style = getLLVMStyle(); 2289 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 2290 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 2291 verifyFormat("FOO_BEGIN\n" 2292 " FOO_ENTRY\n" 2293 "FOO_END", Style); 2294 verifyFormat("FOO_BEGIN\n" 2295 " NESTED_FOO_BEGIN\n" 2296 " NESTED_FOO_ENTRY\n" 2297 " NESTED_FOO_END\n" 2298 "FOO_END", Style); 2299 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 2300 " int x;\n" 2301 " x = 1;\n" 2302 "FOO_END(Baz)", Style); 2303 } 2304 2305 //===----------------------------------------------------------------------===// 2306 // Line break tests. 2307 //===----------------------------------------------------------------------===// 2308 2309 TEST_F(FormatTest, PreventConfusingIndents) { 2310 verifyFormat( 2311 "void f() {\n" 2312 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2313 " parameter, parameter, parameter)),\n" 2314 " SecondLongCall(parameter));\n" 2315 "}"); 2316 verifyFormat( 2317 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2318 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2319 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2320 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 2321 verifyFormat( 2322 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2323 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 2324 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 2325 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 2326 verifyFormat( 2327 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 2328 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 2329 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 2330 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 2331 verifyFormat("int a = bbbb && ccc &&\n" 2332 " fffff(\n" 2333 "#define A Just forcing a new line\n" 2334 " ddd);"); 2335 } 2336 2337 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 2338 verifyFormat( 2339 "bool aaaaaaa =\n" 2340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 2341 " bbbbbbbb();"); 2342 verifyFormat( 2343 "bool aaaaaaa =\n" 2344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 2345 " bbbbbbbb();"); 2346 2347 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2348 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 2349 " ccccccccc == ddddddddddd;"); 2350 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2351 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 2352 " ccccccccc == ddddddddddd;"); 2353 verifyFormat( 2354 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 2355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 2356 " ccccccccc == ddddddddddd;"); 2357 2358 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2359 " aaaaaa) &&\n" 2360 " bbbbbb && cccccc;"); 2361 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2362 " aaaaaa) >>\n" 2363 " bbbbbb;"); 2364 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 2365 " SourceMgr.getSpellingColumnNumber(\n" 2366 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 2367 " 1);"); 2368 2369 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2370 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 2371 " cccccc) {\n}"); 2372 verifyFormat("b = a &&\n" 2373 " // Comment\n" 2374 " b.c && d;"); 2375 2376 // If the LHS of a comparison is not a binary expression itself, the 2377 // additional linebreak confuses many people. 2378 verifyFormat( 2379 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 2381 "}"); 2382 verifyFormat( 2383 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2384 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2385 "}"); 2386 verifyFormat( 2387 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 2388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2389 "}"); 2390 // Even explicit parentheses stress the precedence enough to make the 2391 // additional break unnecessary. 2392 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2393 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2394 "}"); 2395 // This cases is borderline, but with the indentation it is still readable. 2396 verifyFormat( 2397 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2398 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2400 "}", 2401 getLLVMStyleWithColumns(75)); 2402 2403 // If the LHS is a binary expression, we should still use the additional break 2404 // as otherwise the formatting hides the operator precedence. 2405 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2407 " 5) {\n" 2408 "}"); 2409 2410 FormatStyle OnePerLine = getLLVMStyle(); 2411 OnePerLine.BinPackParameters = false; 2412 verifyFormat( 2413 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 2416 OnePerLine); 2417 2418 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 2419 " .aaa(aaaaaaaaaaaaa) *\n" 2420 " aaaaaaa +\n" 2421 " aaaaaaa;", 2422 getLLVMStyleWithColumns(40)); 2423 } 2424 2425 TEST_F(FormatTest, ExpressionIndentation) { 2426 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2430 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 2431 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 2432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 2434 " ccccccccccccccccccccccccccccccccccccccccc;"); 2435 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2438 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2439 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2442 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2443 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2446 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2447 verifyFormat("if () {\n" 2448 "} else if (aaaaa && bbbbb > // break\n" 2449 " ccccc) {\n" 2450 "}"); 2451 verifyFormat("if () {\n" 2452 "} else if (aaaaa &&\n" 2453 " bbbbb > // break\n" 2454 " ccccc &&\n" 2455 " ddddd) {\n" 2456 "}"); 2457 2458 // Presence of a trailing comment used to change indentation of b. 2459 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 2460 " b;\n" 2461 "return aaaaaaaaaaaaaaaaaaa +\n" 2462 " b; //", 2463 getLLVMStyleWithColumns(30)); 2464 } 2465 2466 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 2467 // Not sure what the best system is here. Like this, the LHS can be found 2468 // immediately above an operator (everything with the same or a higher 2469 // indent). The RHS is aligned right of the operator and so compasses 2470 // everything until something with the same indent as the operator is found. 2471 // FIXME: Is this a good system? 2472 FormatStyle Style = getLLVMStyle(); 2473 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 2474 verifyFormat( 2475 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2476 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2477 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2478 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2479 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2480 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2481 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2482 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2483 " > ccccccccccccccccccccccccccccccccccccccccc;", 2484 Style); 2485 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2486 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2487 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2488 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2489 Style); 2490 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2491 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2492 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2493 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2494 Style); 2495 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2496 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2497 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2498 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2499 Style); 2500 verifyFormat("if () {\n" 2501 "} else if (aaaaa\n" 2502 " && bbbbb // break\n" 2503 " > ccccc) {\n" 2504 "}", 2505 Style); 2506 verifyFormat("return (a)\n" 2507 " // comment\n" 2508 " + b;", 2509 Style); 2510 verifyFormat( 2511 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2512 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2513 " + cc;", 2514 Style); 2515 2516 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2517 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 2518 Style); 2519 2520 // Forced by comments. 2521 verifyFormat( 2522 "unsigned ContentSize =\n" 2523 " sizeof(int16_t) // DWARF ARange version number\n" 2524 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 2525 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 2526 " + sizeof(int8_t); // Segment Size (in bytes)"); 2527 2528 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 2529 " == boost::fusion::at_c<1>(iiii).second;", 2530 Style); 2531 2532 Style.ColumnLimit = 60; 2533 verifyFormat("zzzzzzzzzz\n" 2534 " = bbbbbbbbbbbbbbbbb\n" 2535 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 2536 Style); 2537 } 2538 2539 TEST_F(FormatTest, EnforcedOperatorWraps) { 2540 // Here we'd like to wrap after the || operators, but a comment is forcing an 2541 // earlier wrap. 2542 verifyFormat("bool x = aaaaa //\n" 2543 " || bbbbb\n" 2544 " //\n" 2545 " || cccc;"); 2546 } 2547 2548 TEST_F(FormatTest, NoOperandAlignment) { 2549 FormatStyle Style = getLLVMStyle(); 2550 Style.AlignOperands = false; 2551 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 2552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 2554 Style); 2555 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2556 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2557 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2558 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2559 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2560 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2561 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2562 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2563 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2564 " > ccccccccccccccccccccccccccccccccccccccccc;", 2565 Style); 2566 2567 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2568 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2569 " + cc;", 2570 Style); 2571 verifyFormat("int a = aa\n" 2572 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2573 " * cccccccccccccccccccccccccccccccccccc;\n", 2574 Style); 2575 2576 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 2577 verifyFormat("return (a > b\n" 2578 " // comment1\n" 2579 " // comment2\n" 2580 " || c);", 2581 Style); 2582 } 2583 2584 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 2585 FormatStyle Style = getLLVMStyle(); 2586 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2587 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2589 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2590 Style); 2591 } 2592 2593 TEST_F(FormatTest, ConstructorInitializers) { 2594 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 2595 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 2596 getLLVMStyleWithColumns(45)); 2597 verifyFormat("Constructor()\n" 2598 " : Inttializer(FitsOnTheLine) {}", 2599 getLLVMStyleWithColumns(44)); 2600 verifyFormat("Constructor()\n" 2601 " : Inttializer(FitsOnTheLine) {}", 2602 getLLVMStyleWithColumns(43)); 2603 2604 verifyFormat("template <typename T>\n" 2605 "Constructor() : Initializer(FitsOnTheLine) {}", 2606 getLLVMStyleWithColumns(45)); 2607 2608 verifyFormat( 2609 "SomeClass::Constructor()\n" 2610 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2611 2612 verifyFormat( 2613 "SomeClass::Constructor()\n" 2614 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2615 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 2616 verifyFormat( 2617 "SomeClass::Constructor()\n" 2618 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2619 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2620 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2621 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2622 " : aaaaaaaaaa(aaaaaa) {}"); 2623 2624 verifyFormat("Constructor()\n" 2625 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2626 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2627 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2628 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 2629 2630 verifyFormat("Constructor()\n" 2631 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2633 2634 verifyFormat("Constructor(int Parameter = 0)\n" 2635 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 2636 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 2637 verifyFormat("Constructor()\n" 2638 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 2639 "}", 2640 getLLVMStyleWithColumns(60)); 2641 verifyFormat("Constructor()\n" 2642 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2643 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 2644 2645 // Here a line could be saved by splitting the second initializer onto two 2646 // lines, but that is not desirable. 2647 verifyFormat("Constructor()\n" 2648 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 2649 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 2650 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2651 2652 FormatStyle OnePerLine = getLLVMStyle(); 2653 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 2654 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 2655 verifyFormat("SomeClass::Constructor()\n" 2656 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2657 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2658 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2659 OnePerLine); 2660 verifyFormat("SomeClass::Constructor()\n" 2661 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 2662 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2663 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2664 OnePerLine); 2665 verifyFormat("MyClass::MyClass(int var)\n" 2666 " : some_var_(var), // 4 space indent\n" 2667 " some_other_var_(var + 1) { // lined up\n" 2668 "}", 2669 OnePerLine); 2670 verifyFormat("Constructor()\n" 2671 " : aaaaa(aaaaaa),\n" 2672 " aaaaa(aaaaaa),\n" 2673 " aaaaa(aaaaaa),\n" 2674 " aaaaa(aaaaaa),\n" 2675 " aaaaa(aaaaaa) {}", 2676 OnePerLine); 2677 verifyFormat("Constructor()\n" 2678 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 2679 " aaaaaaaaaaaaaaaaaaaaaa) {}", 2680 OnePerLine); 2681 OnePerLine.BinPackParameters = false; 2682 verifyFormat( 2683 "Constructor()\n" 2684 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2685 " aaaaaaaaaaa().aaa(),\n" 2686 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 2687 OnePerLine); 2688 OnePerLine.ColumnLimit = 60; 2689 verifyFormat("Constructor()\n" 2690 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 2691 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 2692 OnePerLine); 2693 2694 EXPECT_EQ("Constructor()\n" 2695 " : // Comment forcing unwanted break.\n" 2696 " aaaa(aaaa) {}", 2697 format("Constructor() :\n" 2698 " // Comment forcing unwanted break.\n" 2699 " aaaa(aaaa) {}")); 2700 } 2701 2702 TEST_F(FormatTest, MemoizationTests) { 2703 // This breaks if the memoization lookup does not take \c Indent and 2704 // \c LastSpace into account. 2705 verifyFormat( 2706 "extern CFRunLoopTimerRef\n" 2707 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 2708 " CFTimeInterval interval, CFOptionFlags flags,\n" 2709 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 2710 " CFRunLoopTimerContext *context) {}"); 2711 2712 // Deep nesting somewhat works around our memoization. 2713 verifyFormat( 2714 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 2715 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 2716 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 2717 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 2718 " aaaaa())))))))))))))))))))))))))))))))))))))));", 2719 getLLVMStyleWithColumns(65)); 2720 verifyFormat( 2721 "aaaaa(\n" 2722 " aaaaa,\n" 2723 " aaaaa(\n" 2724 " aaaaa,\n" 2725 " aaaaa(\n" 2726 " aaaaa,\n" 2727 " aaaaa(\n" 2728 " aaaaa,\n" 2729 " aaaaa(\n" 2730 " aaaaa,\n" 2731 " aaaaa(\n" 2732 " aaaaa,\n" 2733 " aaaaa(\n" 2734 " aaaaa,\n" 2735 " aaaaa(\n" 2736 " aaaaa,\n" 2737 " aaaaa(\n" 2738 " aaaaa,\n" 2739 " aaaaa(\n" 2740 " aaaaa,\n" 2741 " aaaaa(\n" 2742 " aaaaa,\n" 2743 " aaaaa(\n" 2744 " aaaaa,\n" 2745 " aaaaa))))))))))));", 2746 getLLVMStyleWithColumns(65)); 2747 verifyFormat( 2748 "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n" 2749 " a),\n" 2750 " a),\n" 2751 " a),\n" 2752 " a),\n" 2753 " a),\n" 2754 " a),\n" 2755 " a),\n" 2756 " a),\n" 2757 " a),\n" 2758 " a),\n" 2759 " a),\n" 2760 " a),\n" 2761 " a),\n" 2762 " a),\n" 2763 " a),\n" 2764 " a),\n" 2765 " a)", 2766 getLLVMStyleWithColumns(65)); 2767 2768 // This test takes VERY long when memoization is broken. 2769 FormatStyle OnePerLine = getLLVMStyle(); 2770 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 2771 OnePerLine.BinPackParameters = false; 2772 std::string input = "Constructor()\n" 2773 " : aaaa(a,\n"; 2774 for (unsigned i = 0, e = 80; i != e; ++i) { 2775 input += " a,\n"; 2776 } 2777 input += " a) {}"; 2778 verifyFormat(input, OnePerLine); 2779 } 2780 2781 TEST_F(FormatTest, BreaksAsHighAsPossible) { 2782 verifyFormat( 2783 "void f() {\n" 2784 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 2785 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 2786 " f();\n" 2787 "}"); 2788 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 2789 " Intervals[i - 1].getRange().getLast()) {\n}"); 2790 } 2791 2792 TEST_F(FormatTest, BreaksFunctionDeclarations) { 2793 // Principially, we break function declarations in a certain order: 2794 // 1) break amongst arguments. 2795 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 2796 " Cccccccccccccc cccccccccccccc);"); 2797 verifyFormat("template <class TemplateIt>\n" 2798 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 2799 " TemplateIt *stop) {}"); 2800 2801 // 2) break after return type. 2802 verifyFormat( 2803 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2804 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 2805 getGoogleStyle()); 2806 2807 // 3) break after (. 2808 verifyFormat( 2809 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 2810 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 2811 getGoogleStyle()); 2812 2813 // 4) break before after nested name specifiers. 2814 verifyFormat( 2815 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2816 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 2817 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 2818 getGoogleStyle()); 2819 2820 // However, there are exceptions, if a sufficient amount of lines can be 2821 // saved. 2822 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 2823 // more adjusting. 2824 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 2825 " Cccccccccccccc cccccccccc,\n" 2826 " Cccccccccccccc cccccccccc,\n" 2827 " Cccccccccccccc cccccccccc,\n" 2828 " Cccccccccccccc cccccccccc);"); 2829 verifyFormat( 2830 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2831 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2832 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2833 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 2834 getGoogleStyle()); 2835 verifyFormat( 2836 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 2837 " Cccccccccccccc cccccccccc,\n" 2838 " Cccccccccccccc cccccccccc,\n" 2839 " Cccccccccccccc cccccccccc,\n" 2840 " Cccccccccccccc cccccccccc,\n" 2841 " Cccccccccccccc cccccccccc,\n" 2842 " Cccccccccccccc cccccccccc);"); 2843 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 2844 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2845 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2846 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 2847 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 2848 2849 // Break after multi-line parameters. 2850 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2853 " bbbb bbbb);"); 2854 verifyFormat("void SomeLoooooooooooongFunction(\n" 2855 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 2856 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2857 " int bbbbbbbbbbbbb);"); 2858 2859 // Treat overloaded operators like other functions. 2860 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 2861 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 2862 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 2863 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 2864 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 2865 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 2866 verifyGoogleFormat( 2867 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 2868 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 2869 verifyGoogleFormat( 2870 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 2871 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 2872 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2873 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 2874 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 2875 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 2876 verifyGoogleFormat( 2877 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 2878 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2879 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 2880 verifyGoogleFormat( 2881 "template <typename T>\n" 2882 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2883 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 2884 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 2885 2886 FormatStyle Style = getLLVMStyle(); 2887 Style.PointerAlignment = FormatStyle::PAS_Left; 2888 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2889 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 2890 Style); 2891 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 2892 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 2893 Style); 2894 } 2895 2896 TEST_F(FormatTest, TrailingReturnType) { 2897 verifyFormat("auto foo() -> int;\n"); 2898 verifyFormat("struct S {\n" 2899 " auto bar() const -> int;\n" 2900 "};"); 2901 verifyFormat("template <size_t Order, typename T>\n" 2902 "auto load_img(const std::string &filename)\n" 2903 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 2904 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 2905 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 2906 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 2907 verifyFormat("template <typename T>\n" 2908 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 2909 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 2910 2911 // Not trailing return types. 2912 verifyFormat("void f() { auto a = b->c(); }"); 2913 } 2914 2915 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 2916 // Avoid breaking before trailing 'const' or other trailing annotations, if 2917 // they are not function-like. 2918 FormatStyle Style = getGoogleStyle(); 2919 Style.ColumnLimit = 47; 2920 verifyFormat("void someLongFunction(\n" 2921 " int someLoooooooooooooongParameter) const {\n}", 2922 getLLVMStyleWithColumns(47)); 2923 verifyFormat("LoooooongReturnType\n" 2924 "someLoooooooongFunction() const {}", 2925 getLLVMStyleWithColumns(47)); 2926 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 2927 " const {}", 2928 Style); 2929 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 2930 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 2931 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 2932 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 2933 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 2934 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 2935 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 2936 " aaaaaaaaaaa aaaaa) const override;"); 2937 verifyGoogleFormat( 2938 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 2939 " const override;"); 2940 2941 // Even if the first parameter has to be wrapped. 2942 verifyFormat("void someLongFunction(\n" 2943 " int someLongParameter) const {}", 2944 getLLVMStyleWithColumns(46)); 2945 verifyFormat("void someLongFunction(\n" 2946 " int someLongParameter) const {}", 2947 Style); 2948 verifyFormat("void someLongFunction(\n" 2949 " int someLongParameter) override {}", 2950 Style); 2951 verifyFormat("void someLongFunction(\n" 2952 " int someLongParameter) OVERRIDE {}", 2953 Style); 2954 verifyFormat("void someLongFunction(\n" 2955 " int someLongParameter) final {}", 2956 Style); 2957 verifyFormat("void someLongFunction(\n" 2958 " int someLongParameter) FINAL {}", 2959 Style); 2960 verifyFormat("void someLongFunction(\n" 2961 " int parameter) const override {}", 2962 Style); 2963 2964 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2965 verifyFormat("void someLongFunction(\n" 2966 " int someLongParameter) const\n" 2967 "{\n" 2968 "}", 2969 Style); 2970 2971 // Unless these are unknown annotations. 2972 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 2973 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2974 " LONG_AND_UGLY_ANNOTATION;"); 2975 2976 // Breaking before function-like trailing annotations is fine to keep them 2977 // close to their arguments. 2978 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2979 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 2980 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 2981 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 2982 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 2983 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 2984 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 2985 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 2986 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 2987 2988 verifyFormat( 2989 "void aaaaaaaaaaaaaaaaaa()\n" 2990 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 2991 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 2992 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2993 " __attribute__((unused));"); 2994 verifyGoogleFormat( 2995 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2996 " GUARDED_BY(aaaaaaaaaaaa);"); 2997 verifyGoogleFormat( 2998 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2999 " GUARDED_BY(aaaaaaaaaaaa);"); 3000 verifyGoogleFormat( 3001 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3002 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3003 verifyGoogleFormat( 3004 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3005 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3006 } 3007 3008 TEST_F(FormatTest, FunctionAnnotations) { 3009 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3010 "int OldFunction(const string ¶meter) {}"); 3011 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3012 "string OldFunction(const string ¶meter) {}"); 3013 verifyFormat("template <typename T>\n" 3014 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3015 "string OldFunction(const string ¶meter) {}"); 3016 3017 // Not function annotations. 3018 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3019 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3020 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3021 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3022 verifyFormat("MACRO(abc).function() // wrap\n" 3023 " << abc;"); 3024 verifyFormat("MACRO(abc)->function() // wrap\n" 3025 " << abc;"); 3026 verifyFormat("MACRO(abc)::function() // wrap\n" 3027 " << abc;"); 3028 } 3029 3030 TEST_F(FormatTest, BreaksDesireably) { 3031 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3032 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3033 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3034 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3036 "}"); 3037 3038 verifyFormat( 3039 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3041 3042 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3045 3046 verifyFormat( 3047 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3048 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3050 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 3052 3053 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3054 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3055 3056 verifyFormat( 3057 "void f() {\n" 3058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 3059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3060 "}"); 3061 verifyFormat( 3062 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3064 verifyFormat( 3065 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3067 verifyFormat( 3068 "aaaaaa(aaa,\n" 3069 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3071 " aaaa);"); 3072 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3073 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3075 3076 // Indent consistently independent of call expression and unary operator. 3077 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3078 " dddddddddddddddddddddddddddddd));"); 3079 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3080 " dddddddddddddddddddddddddddddd));"); 3081 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 3082 " dddddddddddddddddddddddddddddd));"); 3083 3084 // This test case breaks on an incorrect memoization, i.e. an optimization not 3085 // taking into account the StopAt value. 3086 verifyFormat( 3087 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3088 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3089 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3090 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3091 3092 verifyFormat("{\n {\n {\n" 3093 " Annotation.SpaceRequiredBefore =\n" 3094 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 3095 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 3096 " }\n }\n}"); 3097 3098 // Break on an outer level if there was a break on an inner level. 3099 EXPECT_EQ("f(g(h(a, // comment\n" 3100 " b, c),\n" 3101 " d, e),\n" 3102 " x, y);", 3103 format("f(g(h(a, // comment\n" 3104 " b, c), d, e), x, y);")); 3105 3106 // Prefer breaking similar line breaks. 3107 verifyFormat( 3108 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 3109 " NSTrackingMouseEnteredAndExited |\n" 3110 " NSTrackingActiveAlways;"); 3111 } 3112 3113 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 3114 FormatStyle NoBinPacking = getGoogleStyle(); 3115 NoBinPacking.BinPackParameters = false; 3116 NoBinPacking.BinPackArguments = true; 3117 verifyFormat("void f() {\n" 3118 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 3119 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3120 "}", 3121 NoBinPacking); 3122 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 3123 " int aaaaaaaaaaaaaaaaaaaa,\n" 3124 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3125 NoBinPacking); 3126 3127 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3128 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3129 " vector<int> bbbbbbbbbbbbbbb);", 3130 NoBinPacking); 3131 // FIXME: This behavior difference is probably not wanted. However, currently 3132 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 3133 // template arguments from BreakBeforeParameter being set because of the 3134 // one-per-line formatting. 3135 verifyFormat( 3136 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3137 " aaaaaaaaaa> aaaaaaaaaa);", 3138 NoBinPacking); 3139 verifyFormat( 3140 "void fffffffffff(\n" 3141 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 3142 " aaaaaaaaaa);"); 3143 } 3144 3145 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 3146 FormatStyle NoBinPacking = getGoogleStyle(); 3147 NoBinPacking.BinPackParameters = false; 3148 NoBinPacking.BinPackArguments = false; 3149 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 3150 " aaaaaaaaaaaaaaaaaaaa,\n" 3151 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 3152 NoBinPacking); 3153 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 3154 " aaaaaaaaaaaaa,\n" 3155 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 3156 NoBinPacking); 3157 verifyFormat( 3158 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3159 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3161 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3162 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 3163 NoBinPacking); 3164 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3165 " .aaaaaaaaaaaaaaaaaa();", 3166 NoBinPacking); 3167 verifyFormat("void f() {\n" 3168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3169 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 3170 "}", 3171 NoBinPacking); 3172 3173 verifyFormat( 3174 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3175 " aaaaaaaaaaaa,\n" 3176 " aaaaaaaaaaaa);", 3177 NoBinPacking); 3178 verifyFormat( 3179 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 3180 " ddddddddddddddddddddddddddddd),\n" 3181 " test);", 3182 NoBinPacking); 3183 3184 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3185 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 3186 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 3187 " aaaaaaaaaaaaaaaaaa;", 3188 NoBinPacking); 3189 verifyFormat("a(\"a\"\n" 3190 " \"a\",\n" 3191 " a);"); 3192 3193 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3194 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 3195 " aaaaaaaaa,\n" 3196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3197 NoBinPacking); 3198 verifyFormat( 3199 "void f() {\n" 3200 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3201 " .aaaaaaa();\n" 3202 "}", 3203 NoBinPacking); 3204 verifyFormat( 3205 "template <class SomeType, class SomeOtherType>\n" 3206 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 3207 NoBinPacking); 3208 } 3209 3210 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 3211 FormatStyle Style = getLLVMStyleWithColumns(15); 3212 Style.ExperimentalAutoDetectBinPacking = true; 3213 EXPECT_EQ("aaa(aaaa,\n" 3214 " aaaa,\n" 3215 " aaaa);\n" 3216 "aaa(aaaa,\n" 3217 " aaaa,\n" 3218 " aaaa);", 3219 format("aaa(aaaa,\n" // one-per-line 3220 " aaaa,\n" 3221 " aaaa );\n" 3222 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3223 Style)); 3224 EXPECT_EQ("aaa(aaaa, aaaa,\n" 3225 " aaaa);\n" 3226 "aaa(aaaa, aaaa,\n" 3227 " aaaa);", 3228 format("aaa(aaaa, aaaa,\n" // bin-packed 3229 " aaaa );\n" 3230 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3231 Style)); 3232 } 3233 3234 TEST_F(FormatTest, FormatsBuilderPattern) { 3235 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 3236 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 3237 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 3238 " .StartsWith(\".init\", ORDER_INIT)\n" 3239 " .StartsWith(\".fini\", ORDER_FINI)\n" 3240 " .StartsWith(\".hash\", ORDER_HASH)\n" 3241 " .Default(ORDER_TEXT);\n"); 3242 3243 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 3244 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 3245 verifyFormat( 3246 "aaaaaaa->aaaaaaa\n" 3247 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3249 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3250 verifyFormat( 3251 "aaaaaaa->aaaaaaa\n" 3252 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3253 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3254 verifyFormat( 3255 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 3256 " aaaaaaaaaaaaaa);"); 3257 verifyFormat( 3258 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 3259 " aaaaaa->aaaaaaaaaaaa()\n" 3260 " ->aaaaaaaaaaaaaaaa(\n" 3261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3262 " ->aaaaaaaaaaaaaaaaa();"); 3263 verifyGoogleFormat( 3264 "void f() {\n" 3265 " someo->Add((new util::filetools::Handler(dir))\n" 3266 " ->OnEvent1(NewPermanentCallback(\n" 3267 " this, &HandlerHolderClass::EventHandlerCBA))\n" 3268 " ->OnEvent2(NewPermanentCallback(\n" 3269 " this, &HandlerHolderClass::EventHandlerCBB))\n" 3270 " ->OnEvent3(NewPermanentCallback(\n" 3271 " this, &HandlerHolderClass::EventHandlerCBC))\n" 3272 " ->OnEvent5(NewPermanentCallback(\n" 3273 " this, &HandlerHolderClass::EventHandlerCBD))\n" 3274 " ->OnEvent6(NewPermanentCallback(\n" 3275 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 3276 "}"); 3277 3278 verifyFormat( 3279 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 3280 verifyFormat("aaaaaaaaaaaaaaa()\n" 3281 " .aaaaaaaaaaaaaaa()\n" 3282 " .aaaaaaaaaaaaaaa()\n" 3283 " .aaaaaaaaaaaaaaa()\n" 3284 " .aaaaaaaaaaaaaaa();"); 3285 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3286 " .aaaaaaaaaaaaaaa()\n" 3287 " .aaaaaaaaaaaaaaa()\n" 3288 " .aaaaaaaaaaaaaaa();"); 3289 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3290 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3291 " .aaaaaaaaaaaaaaa();"); 3292 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 3293 " ->aaaaaaaaaaaaaae(0)\n" 3294 " ->aaaaaaaaaaaaaaa();"); 3295 3296 // Don't linewrap after very short segments. 3297 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3298 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3299 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3300 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3301 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3302 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3303 verifyFormat("aaa()\n" 3304 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3305 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3306 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3307 3308 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3309 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3310 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 3311 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3312 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 3314 3315 // Prefer not to break after empty parentheses. 3316 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 3317 " First->LastNewlineOffset);"); 3318 3319 // Prefer not to create "hanging" indents. 3320 verifyFormat( 3321 "return !soooooooooooooome_map\n" 3322 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3323 " .second;"); 3324 verifyFormat( 3325 "return aaaaaaaaaaaaaaaa\n" 3326 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 3327 " .aaaa(aaaaaaaaaaaaaa);"); 3328 // No hanging indent here. 3329 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 3330 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3331 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 3332 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3333 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 3334 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3335 getLLVMStyleWithColumns(60)); 3336 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 3337 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 3338 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3339 getLLVMStyleWithColumns(59)); 3340 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3342 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3343 } 3344 3345 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 3346 verifyFormat( 3347 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3348 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 3349 verifyFormat( 3350 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 3351 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 3352 3353 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3354 " ccccccccccccccccccccccccc) {\n}"); 3355 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 3356 " ccccccccccccccccccccccccc) {\n}"); 3357 3358 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3359 " ccccccccccccccccccccccccc) {\n}"); 3360 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 3361 " ccccccccccccccccccccccccc) {\n}"); 3362 3363 verifyFormat( 3364 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 3365 " ccccccccccccccccccccccccc) {\n}"); 3366 verifyFormat( 3367 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 3368 " ccccccccccccccccccccccccc) {\n}"); 3369 3370 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 3371 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 3372 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 3373 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3374 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 3375 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 3376 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 3377 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3378 3379 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 3380 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 3381 " aaaaaaaaaaaaaaa != aa) {\n}"); 3382 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 3383 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 3384 " aaaaaaaaaaaaaaa != aa) {\n}"); 3385 } 3386 3387 TEST_F(FormatTest, BreaksAfterAssignments) { 3388 verifyFormat( 3389 "unsigned Cost =\n" 3390 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 3391 " SI->getPointerAddressSpaceee());\n"); 3392 verifyFormat( 3393 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 3394 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 3395 3396 verifyFormat( 3397 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 3398 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 3399 verifyFormat("unsigned OriginalStartColumn =\n" 3400 " SourceMgr.getSpellingColumnNumber(\n" 3401 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 3402 " 1;"); 3403 } 3404 3405 TEST_F(FormatTest, AlignsAfterAssignments) { 3406 verifyFormat( 3407 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3408 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3409 verifyFormat( 3410 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3411 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3412 verifyFormat( 3413 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3414 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3415 verifyFormat( 3416 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3417 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3418 verifyFormat( 3419 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3420 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3421 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 3422 } 3423 3424 TEST_F(FormatTest, AlignsAfterReturn) { 3425 verifyFormat( 3426 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3427 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3428 verifyFormat( 3429 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3430 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3431 verifyFormat( 3432 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3433 " aaaaaaaaaaaaaaaaaaaaaa();"); 3434 verifyFormat( 3435 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3436 " aaaaaaaaaaaaaaaaaaaaaa());"); 3437 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3439 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 3441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3442 verifyFormat("return\n" 3443 " // true if code is one of a or b.\n" 3444 " code == a || code == b;"); 3445 } 3446 3447 TEST_F(FormatTest, AlignsAfterOpenBracket) { 3448 verifyFormat( 3449 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 3450 " aaaaaaaaa aaaaaaa) {}"); 3451 verifyFormat( 3452 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 3453 " aaaaaaaaaaa aaaaaaaaa);"); 3454 verifyFormat( 3455 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 3456 " aaaaaaaaaaaaaaaaaaaaa));"); 3457 FormatStyle Style = getLLVMStyle(); 3458 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3459 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3460 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 3461 Style); 3462 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 3463 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 3464 Style); 3465 verifyFormat("SomeLongVariableName->someFunction(\n" 3466 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 3467 Style); 3468 verifyFormat( 3469 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 3470 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3471 Style); 3472 verifyFormat( 3473 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 3474 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3475 Style); 3476 verifyFormat( 3477 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 3478 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 3479 Style); 3480 3481 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 3482 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 3483 " b));", 3484 Style); 3485 3486 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 3487 Style.BinPackArguments = false; 3488 Style.BinPackParameters = false; 3489 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3490 " aaaaaaaaaaa aaaaaaaa,\n" 3491 " aaaaaaaaa aaaaaaa,\n" 3492 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3493 Style); 3494 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 3495 " aaaaaaaaaaa aaaaaaaaa,\n" 3496 " aaaaaaaaaaa aaaaaaaaa,\n" 3497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3498 Style); 3499 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 3500 " aaaaaaaaaaaaaaa,\n" 3501 " aaaaaaaaaaaaaaaaaaaaa,\n" 3502 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 3503 Style); 3504 verifyFormat( 3505 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 3506 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 3507 Style); 3508 verifyFormat( 3509 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 3510 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 3511 Style); 3512 verifyFormat( 3513 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3514 " aaaaaaaaaaaaaaaaaaaaa(\n" 3515 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 3516 " aaaaaaaaaaaaaaaa);", 3517 Style); 3518 verifyFormat( 3519 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3520 " aaaaaaaaaaaaaaaaaaaaa(\n" 3521 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 3522 " aaaaaaaaaaaaaaaa);", 3523 Style); 3524 } 3525 3526 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 3527 FormatStyle Style = getLLVMStyleWithColumns(40); 3528 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3529 " bbbbbbbbbbbbbbbbbbbbbb);", 3530 Style); 3531 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 3532 Style.AlignOperands = false; 3533 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3534 " bbbbbbbbbbbbbbbbbbbbbb);", 3535 Style); 3536 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3537 Style.AlignOperands = true; 3538 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3539 " bbbbbbbbbbbbbbbbbbbbbb);", 3540 Style); 3541 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3542 Style.AlignOperands = false; 3543 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3544 " bbbbbbbbbbbbbbbbbbbbbb);", 3545 Style); 3546 } 3547 3548 TEST_F(FormatTest, BreaksConditionalExpressions) { 3549 verifyFormat( 3550 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3551 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3552 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3553 verifyFormat( 3554 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 3555 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3556 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3557 verifyFormat( 3558 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3559 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3560 verifyFormat( 3561 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 3562 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3563 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3564 verifyFormat( 3565 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 3566 " : aaaaaaaaaaaaa);"); 3567 verifyFormat( 3568 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3569 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3570 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3571 " aaaaaaaaaaaaa);"); 3572 verifyFormat( 3573 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3574 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3575 " aaaaaaaaaaaaa);"); 3576 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3577 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3579 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3581 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3583 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3585 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3587 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3588 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3589 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3590 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3592 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3593 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3594 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3595 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3596 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 3597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3598 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3599 " : aaaaaaaaaaaaaaaa;"); 3600 verifyFormat( 3601 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3602 " ? aaaaaaaaaaaaaaa\n" 3603 " : aaaaaaaaaaaaaaa;"); 3604 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 3605 " aaaaaaaaa\n" 3606 " ? b\n" 3607 " : c);"); 3608 verifyFormat("return aaaa == bbbb\n" 3609 " // comment\n" 3610 " ? aaaa\n" 3611 " : bbbb;"); 3612 verifyFormat("unsigned Indent =\n" 3613 " format(TheLine.First,\n" 3614 " IndentForLevel[TheLine.Level] >= 0\n" 3615 " ? IndentForLevel[TheLine.Level]\n" 3616 " : TheLine * 2,\n" 3617 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 3618 getLLVMStyleWithColumns(60)); 3619 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 3620 " ? aaaaaaaaaaaaaaa\n" 3621 " : bbbbbbbbbbbbbbb //\n" 3622 " ? ccccccccccccccc\n" 3623 " : ddddddddddddddd;"); 3624 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 3625 " ? aaaaaaaaaaaaaaa\n" 3626 " : (bbbbbbbbbbbbbbb //\n" 3627 " ? ccccccccccccccc\n" 3628 " : ddddddddddddddd);"); 3629 verifyFormat( 3630 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3631 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3632 " aaaaaaaaaaaaaaaaaaaaa +\n" 3633 " aaaaaaaaaaaaaaaaaaaaa\n" 3634 " : aaaaaaaaaa;"); 3635 verifyFormat( 3636 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3637 " : aaaaaaaaaaaaaaaaaaaaaa\n" 3638 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3639 3640 FormatStyle NoBinPacking = getLLVMStyle(); 3641 NoBinPacking.BinPackArguments = false; 3642 verifyFormat( 3643 "void f() {\n" 3644 " g(aaa,\n" 3645 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 3646 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3647 " ? aaaaaaaaaaaaaaa\n" 3648 " : aaaaaaaaaaaaaaa);\n" 3649 "}", 3650 NoBinPacking); 3651 verifyFormat( 3652 "void f() {\n" 3653 " g(aaa,\n" 3654 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 3655 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3656 " ?: aaaaaaaaaaaaaaa);\n" 3657 "}", 3658 NoBinPacking); 3659 3660 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 3661 " // comment.\n" 3662 " ccccccccccccccccccccccccccccccccccccccc\n" 3663 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3664 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 3665 3666 // Assignments in conditional expressions. Apparently not uncommon :-(. 3667 verifyFormat("return a != b\n" 3668 " // comment\n" 3669 " ? a = b\n" 3670 " : a = b;"); 3671 verifyFormat("return a != b\n" 3672 " // comment\n" 3673 " ? a = a != b\n" 3674 " // comment\n" 3675 " ? a = b\n" 3676 " : a\n" 3677 " : a;\n"); 3678 verifyFormat("return a != b\n" 3679 " // comment\n" 3680 " ? a\n" 3681 " : a = a != b\n" 3682 " // comment\n" 3683 " ? a = b\n" 3684 " : a;"); 3685 } 3686 3687 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 3688 FormatStyle Style = getLLVMStyle(); 3689 Style.BreakBeforeTernaryOperators = false; 3690 Style.ColumnLimit = 70; 3691 verifyFormat( 3692 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3693 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3694 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3695 Style); 3696 verifyFormat( 3697 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 3698 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3699 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3700 Style); 3701 verifyFormat( 3702 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3703 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3704 Style); 3705 verifyFormat( 3706 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 3707 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3709 Style); 3710 verifyFormat( 3711 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 3712 " aaaaaaaaaaaaa);", 3713 Style); 3714 verifyFormat( 3715 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3716 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3717 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3718 " aaaaaaaaaaaaa);", 3719 Style); 3720 verifyFormat( 3721 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3722 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3723 " aaaaaaaaaaaaa);", 3724 Style); 3725 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3726 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3730 Style); 3731 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3732 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3737 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3738 Style); 3739 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 3741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3743 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3744 Style); 3745 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3746 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3747 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3748 Style); 3749 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 3750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 3752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3753 Style); 3754 verifyFormat( 3755 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3756 " aaaaaaaaaaaaaaa :\n" 3757 " aaaaaaaaaaaaaaa;", 3758 Style); 3759 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 3760 " aaaaaaaaa ?\n" 3761 " b :\n" 3762 " c);", 3763 Style); 3764 verifyFormat("unsigned Indent =\n" 3765 " format(TheLine.First,\n" 3766 " IndentForLevel[TheLine.Level] >= 0 ?\n" 3767 " IndentForLevel[TheLine.Level] :\n" 3768 " TheLine * 2,\n" 3769 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 3770 Style); 3771 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 3772 " aaaaaaaaaaaaaaa :\n" 3773 " bbbbbbbbbbbbbbb ? //\n" 3774 " ccccccccccccccc :\n" 3775 " ddddddddddddddd;", 3776 Style); 3777 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 3778 " aaaaaaaaaaaaaaa :\n" 3779 " (bbbbbbbbbbbbbbb ? //\n" 3780 " ccccccccccccccc :\n" 3781 " ddddddddddddddd);", 3782 Style); 3783 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3784 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 3785 " ccccccccccccccccccccccccccc;", 3786 Style); 3787 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 3788 " aaaaa :\n" 3789 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 3790 Style); 3791 } 3792 3793 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 3794 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 3795 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 3796 verifyFormat("bool a = true, b = false;"); 3797 3798 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 3800 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 3801 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 3802 verifyFormat( 3803 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3804 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 3805 " d = e && f;"); 3806 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 3807 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 3808 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 3809 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 3810 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 3811 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 3812 3813 FormatStyle Style = getGoogleStyle(); 3814 Style.PointerAlignment = FormatStyle::PAS_Left; 3815 Style.DerivePointerAlignment = false; 3816 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3817 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 3818 " *b = bbbbbbbbbbbbbbbbbbb;", 3819 Style); 3820 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 3821 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 3822 Style); 3823 verifyFormat("vector<int*> a, b;", Style); 3824 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 3825 } 3826 3827 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 3828 verifyFormat("arr[foo ? bar : baz];"); 3829 verifyFormat("f()[foo ? bar : baz];"); 3830 verifyFormat("(a + b)[foo ? bar : baz];"); 3831 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 3832 } 3833 3834 TEST_F(FormatTest, AlignsStringLiterals) { 3835 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 3836 " \"short literal\");"); 3837 verifyFormat( 3838 "looooooooooooooooooooooooongFunction(\n" 3839 " \"short literal\"\n" 3840 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 3841 verifyFormat("someFunction(\"Always break between multi-line\"\n" 3842 " \" string literals\",\n" 3843 " and, other, parameters);"); 3844 EXPECT_EQ("fun + \"1243\" /* comment */\n" 3845 " \"5678\";", 3846 format("fun + \"1243\" /* comment */\n" 3847 " \"5678\";", 3848 getLLVMStyleWithColumns(28))); 3849 EXPECT_EQ( 3850 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 3851 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 3852 " \"aaaaaaaaaaaaaaaa\";", 3853 format("aaaaaa =" 3854 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 3855 "aaaaaaaaaaaaaaaaaaaaa\" " 3856 "\"aaaaaaaaaaaaaaaa\";")); 3857 verifyFormat("a = a + \"a\"\n" 3858 " \"a\"\n" 3859 " \"a\";"); 3860 verifyFormat("f(\"a\", \"b\"\n" 3861 " \"c\");"); 3862 3863 verifyFormat( 3864 "#define LL_FORMAT \"ll\"\n" 3865 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 3866 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 3867 3868 verifyFormat("#define A(X) \\\n" 3869 " \"aaaaa\" #X \"bbbbbb\" \\\n" 3870 " \"ccccc\"", 3871 getLLVMStyleWithColumns(23)); 3872 verifyFormat("#define A \"def\"\n" 3873 "f(\"abc\" A \"ghi\"\n" 3874 " \"jkl\");"); 3875 3876 verifyFormat("f(L\"a\"\n" 3877 " L\"b\");"); 3878 verifyFormat("#define A(X) \\\n" 3879 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 3880 " L\"ccccc\"", 3881 getLLVMStyleWithColumns(25)); 3882 3883 verifyFormat("f(@\"a\"\n" 3884 " @\"b\");"); 3885 verifyFormat("NSString s = @\"a\"\n" 3886 " @\"b\"\n" 3887 " @\"c\";"); 3888 verifyFormat("NSString s = @\"a\"\n" 3889 " \"b\"\n" 3890 " \"c\";"); 3891 } 3892 3893 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 3894 FormatStyle Style = getLLVMStyle(); 3895 // No declarations or definitions should be moved to own line. 3896 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 3897 verifyFormat("class A {\n" 3898 " int f() { return 1; }\n" 3899 " int g();\n" 3900 "};\n" 3901 "int f() { return 1; }\n" 3902 "int g();\n", 3903 Style); 3904 3905 // All declarations and definitions should have the return type moved to its 3906 // own 3907 // line. 3908 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 3909 verifyFormat("class E {\n" 3910 " int\n" 3911 " f() {\n" 3912 " return 1;\n" 3913 " }\n" 3914 " int\n" 3915 " g();\n" 3916 "};\n" 3917 "int\n" 3918 "f() {\n" 3919 " return 1;\n" 3920 "}\n" 3921 "int\n" 3922 "g();\n", 3923 Style); 3924 3925 // Top-level definitions, and no kinds of declarations should have the 3926 // return type moved to its own line. 3927 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 3928 verifyFormat("class B {\n" 3929 " int f() { return 1; }\n" 3930 " int g();\n" 3931 "};\n" 3932 "int\n" 3933 "f() {\n" 3934 " return 1;\n" 3935 "}\n" 3936 "int g();\n", 3937 Style); 3938 3939 // Top-level definitions and declarations should have the return type moved 3940 // to its own line. 3941 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 3942 verifyFormat("class C {\n" 3943 " int f() { return 1; }\n" 3944 " int g();\n" 3945 "};\n" 3946 "int\n" 3947 "f() {\n" 3948 " return 1;\n" 3949 "}\n" 3950 "int\n" 3951 "g();\n", 3952 Style); 3953 3954 // All definitions should have the return type moved to its own line, but no 3955 // kinds of declarations. 3956 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 3957 verifyFormat("class D {\n" 3958 " int\n" 3959 " f() {\n" 3960 " return 1;\n" 3961 " }\n" 3962 " int g();\n" 3963 "};\n" 3964 "int\n" 3965 "f() {\n" 3966 " return 1;\n" 3967 "}\n" 3968 "int g();\n", 3969 Style); 3970 verifyFormat("const char *\n" 3971 "f(void) {\n" // Break here. 3972 " return \"\";\n" 3973 "}\n" 3974 "const char *bar(void);\n", // No break here. 3975 Style); 3976 verifyFormat("template <class T>\n" 3977 "T *\n" 3978 "f(T &c) {\n" // Break here. 3979 " return NULL;\n" 3980 "}\n" 3981 "template <class T> T *f(T &c);\n", // No break here. 3982 Style); 3983 verifyFormat("class C {\n" 3984 " int\n" 3985 " operator+() {\n" 3986 " return 1;\n" 3987 " }\n" 3988 " int\n" 3989 " operator()() {\n" 3990 " return 1;\n" 3991 " }\n" 3992 "};\n", 3993 Style); 3994 verifyFormat("void\n" 3995 "A::operator()() {}\n" 3996 "void\n" 3997 "A::operator>>() {}\n" 3998 "void\n" 3999 "A::operator+() {}\n", 4000 Style); 4001 verifyFormat("void *operator new(std::size_t s);", // No break here. 4002 Style); 4003 verifyFormat("void *\n" 4004 "operator new(std::size_t s) {}", 4005 Style); 4006 verifyFormat("void *\n" 4007 "operator delete[](void *ptr) {}", 4008 Style); 4009 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4010 verifyFormat("const char *\n" 4011 "f(void)\n" // Break here. 4012 "{\n" 4013 " return \"\";\n" 4014 "}\n" 4015 "const char *bar(void);\n", // No break here. 4016 Style); 4017 verifyFormat("template <class T>\n" 4018 "T *\n" // Problem here: no line break 4019 "f(T &c)\n" // Break here. 4020 "{\n" 4021 " return NULL;\n" 4022 "}\n" 4023 "template <class T> T *f(T &c);\n", // No break here. 4024 Style); 4025 } 4026 4027 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4028 FormatStyle NoBreak = getLLVMStyle(); 4029 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4030 FormatStyle Break = getLLVMStyle(); 4031 Break.AlwaysBreakBeforeMultilineStrings = true; 4032 verifyFormat("aaaa = \"bbbb\"\n" 4033 " \"cccc\";", 4034 NoBreak); 4035 verifyFormat("aaaa =\n" 4036 " \"bbbb\"\n" 4037 " \"cccc\";", 4038 Break); 4039 verifyFormat("aaaa(\"bbbb\"\n" 4040 " \"cccc\");", 4041 NoBreak); 4042 verifyFormat("aaaa(\n" 4043 " \"bbbb\"\n" 4044 " \"cccc\");", 4045 Break); 4046 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4047 " \"cccc\");", 4048 NoBreak); 4049 verifyFormat("aaaa(qqq,\n" 4050 " \"bbbb\"\n" 4051 " \"cccc\");", 4052 Break); 4053 verifyFormat("aaaa(qqq,\n" 4054 " L\"bbbb\"\n" 4055 " L\"cccc\");", 4056 Break); 4057 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4058 " \"bbbb\"));", 4059 Break); 4060 verifyFormat("string s = someFunction(\n" 4061 " \"abc\"\n" 4062 " \"abc\");", 4063 Break); 4064 4065 // As we break before unary operators, breaking right after them is bad. 4066 verifyFormat("string foo = abc ? \"x\"\n" 4067 " \"blah blah blah blah blah blah\"\n" 4068 " : \"y\";", 4069 Break); 4070 4071 // Don't break if there is no column gain. 4072 verifyFormat("f(\"aaaa\"\n" 4073 " \"bbbb\");", 4074 Break); 4075 4076 // Treat literals with escaped newlines like multi-line string literals. 4077 EXPECT_EQ("x = \"a\\\n" 4078 "b\\\n" 4079 "c\";", 4080 format("x = \"a\\\n" 4081 "b\\\n" 4082 "c\";", 4083 NoBreak)); 4084 EXPECT_EQ("xxxx =\n" 4085 " \"a\\\n" 4086 "b\\\n" 4087 "c\";", 4088 format("xxxx = \"a\\\n" 4089 "b\\\n" 4090 "c\";", 4091 Break)); 4092 4093 // Exempt ObjC strings for now. 4094 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 4095 " @\"bbbb\";", 4096 format("NSString *const kString = @\"aaaa\"\n" 4097 "@\"bbbb\";", 4098 Break)); 4099 4100 Break.ColumnLimit = 0; 4101 verifyFormat("const char *hello = \"hello llvm\";", Break); 4102 } 4103 4104 TEST_F(FormatTest, AlignsPipes) { 4105 verifyFormat( 4106 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4107 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4108 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4109 verifyFormat( 4110 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 4111 " << aaaaaaaaaaaaaaaaaaaa;"); 4112 verifyFormat( 4113 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4114 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4115 verifyFormat( 4116 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4117 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4118 verifyFormat( 4119 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 4120 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 4121 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 4122 verifyFormat( 4123 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4124 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4125 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4126 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4128 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4129 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4130 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 4131 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 4132 verifyFormat( 4133 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4135 verifyFormat( 4136 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 4137 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4138 4139 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 4140 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 4141 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4143 " aaaaaaaaaaaaaaaaaaaaa)\n" 4144 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4145 verifyFormat("LOG_IF(aaa == //\n" 4146 " bbb)\n" 4147 " << a << b;"); 4148 4149 // But sometimes, breaking before the first "<<" is desirable. 4150 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4151 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 4152 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 4153 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4154 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4155 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 4156 " << BEF << IsTemplate << Description << E->getType();"); 4157 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4158 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4160 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4161 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4162 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4163 " << aaa;"); 4164 4165 verifyFormat( 4166 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4167 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4168 4169 // Incomplete string literal. 4170 EXPECT_EQ("llvm::errs() << \"\n" 4171 " << a;", 4172 format("llvm::errs() << \"\n<<a;")); 4173 4174 verifyFormat("void f() {\n" 4175 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 4176 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 4177 "}"); 4178 4179 // Handle 'endl'. 4180 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 4181 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4182 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4183 4184 // Handle '\n'. 4185 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 4186 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 4187 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 4188 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 4189 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 4190 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 4191 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 4192 } 4193 4194 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 4195 verifyFormat("return out << \"somepacket = {\\n\"\n" 4196 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 4197 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 4198 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 4199 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 4200 " << \"}\";"); 4201 4202 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4203 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4204 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 4205 verifyFormat( 4206 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 4207 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 4208 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 4209 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 4210 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 4211 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 4212 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4213 verifyFormat( 4214 "void f() {\n" 4215 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 4216 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4217 "}"); 4218 4219 // Breaking before the first "<<" is generally not desirable. 4220 verifyFormat( 4221 "llvm::errs()\n" 4222 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4223 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4224 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4225 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4226 getLLVMStyleWithColumns(70)); 4227 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4228 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4229 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4230 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4231 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4232 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4233 getLLVMStyleWithColumns(70)); 4234 4235 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 4236 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 4237 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 4238 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 4239 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 4240 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 4241 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 4242 " (aaaa + aaaa);", 4243 getLLVMStyleWithColumns(40)); 4244 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 4245 " (aaaaaaa + aaaaa));", 4246 getLLVMStyleWithColumns(40)); 4247 verifyFormat( 4248 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 4249 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 4250 " bbbbbbbbbbbbbbbbbbbbbbb);"); 4251 } 4252 4253 TEST_F(FormatTest, UnderstandsEquals) { 4254 verifyFormat( 4255 "aaaaaaaaaaaaaaaaa =\n" 4256 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4257 verifyFormat( 4258 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4259 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4260 verifyFormat( 4261 "if (a) {\n" 4262 " f();\n" 4263 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4264 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4265 "}"); 4266 4267 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4268 " 100000000 + 10000000) {\n}"); 4269 } 4270 4271 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 4272 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4273 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 4274 4275 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4276 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 4277 4278 verifyFormat( 4279 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 4280 " Parameter2);"); 4281 4282 verifyFormat( 4283 "ShortObject->shortFunction(\n" 4284 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 4285 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 4286 4287 verifyFormat("loooooooooooooongFunction(\n" 4288 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 4289 4290 verifyFormat( 4291 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 4292 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 4293 4294 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4295 " .WillRepeatedly(Return(SomeValue));"); 4296 verifyFormat("void f() {\n" 4297 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4298 " .Times(2)\n" 4299 " .WillRepeatedly(Return(SomeValue));\n" 4300 "}"); 4301 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 4302 " ccccccccccccccccccccccc);"); 4303 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4305 " .aaaaa(aaaaa),\n" 4306 " aaaaaaaaaaaaaaaaaaaaa);"); 4307 verifyFormat("void f() {\n" 4308 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4309 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 4310 "}"); 4311 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4312 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4313 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4314 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4315 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4316 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4317 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4318 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4319 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 4320 "}"); 4321 4322 // Here, it is not necessary to wrap at "." or "->". 4323 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 4324 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4325 verifyFormat( 4326 "aaaaaaaaaaa->aaaaaaaaa(\n" 4327 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4328 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 4329 4330 verifyFormat( 4331 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4332 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 4333 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 4334 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4335 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 4336 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4337 4338 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4340 " .a();"); 4341 4342 FormatStyle NoBinPacking = getLLVMStyle(); 4343 NoBinPacking.BinPackParameters = false; 4344 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4345 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4346 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 4347 " aaaaaaaaaaaaaaaaaaa,\n" 4348 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4349 NoBinPacking); 4350 4351 // If there is a subsequent call, change to hanging indentation. 4352 verifyFormat( 4353 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4354 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 4355 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4356 verifyFormat( 4357 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4358 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 4359 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4361 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4362 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4364 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4365 } 4366 4367 TEST_F(FormatTest, WrapsTemplateDeclarations) { 4368 verifyFormat("template <typename T>\n" 4369 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4370 verifyFormat("template <typename T>\n" 4371 "// T should be one of {A, B}.\n" 4372 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4373 verifyFormat( 4374 "template <typename T>\n" 4375 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 4376 verifyFormat("template <typename T>\n" 4377 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 4378 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 4379 verifyFormat( 4380 "template <typename T>\n" 4381 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 4382 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 4383 verifyFormat( 4384 "template <typename T>\n" 4385 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 4386 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 4387 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4388 verifyFormat("template <typename T>\n" 4389 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4390 " int aaaaaaaaaaaaaaaaaaaaaa);"); 4391 verifyFormat( 4392 "template <typename T1, typename T2 = char, typename T3 = char,\n" 4393 " typename T4 = char>\n" 4394 "void f();"); 4395 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 4396 " template <typename> class cccccccccccccccccccccc,\n" 4397 " typename ddddddddddddd>\n" 4398 "class C {};"); 4399 verifyFormat( 4400 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 4401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4402 4403 verifyFormat("void f() {\n" 4404 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 4405 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 4406 "}"); 4407 4408 verifyFormat("template <typename T> class C {};"); 4409 verifyFormat("template <typename T> void f();"); 4410 verifyFormat("template <typename T> void f() {}"); 4411 verifyFormat( 4412 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 4415 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 4418 " bbbbbbbbbbbbbbbbbbbbbbbb);", 4419 getLLVMStyleWithColumns(72)); 4420 EXPECT_EQ("static_cast<A< //\n" 4421 " B> *>(\n" 4422 "\n" 4423 " );", 4424 format("static_cast<A<//\n" 4425 " B>*>(\n" 4426 "\n" 4427 " );")); 4428 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4429 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 4430 4431 FormatStyle AlwaysBreak = getLLVMStyle(); 4432 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 4433 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 4434 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 4435 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 4436 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4437 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 4438 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 4439 verifyFormat("template <template <typename> class Fooooooo,\n" 4440 " template <typename> class Baaaaaaar>\n" 4441 "struct C {};", 4442 AlwaysBreak); 4443 verifyFormat("template <typename T> // T can be A, B or C.\n" 4444 "struct C {};", 4445 AlwaysBreak); 4446 verifyFormat("template <enum E> class A {\n" 4447 "public:\n" 4448 " E *f();\n" 4449 "};"); 4450 } 4451 4452 TEST_F(FormatTest, WrapsTemplateParameters) { 4453 FormatStyle Style = getLLVMStyle(); 4454 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4455 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4456 verifyFormat( 4457 "template <typename... a> struct q {};\n" 4458 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 4459 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 4460 " y;", 4461 Style); 4462 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4463 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4464 verifyFormat( 4465 "template <typename... a> struct r {};\n" 4466 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 4467 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 4468 " y;", 4469 Style); 4470 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4471 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4472 verifyFormat( 4473 "template <typename... a> struct s {};\n" 4474 "extern s<\n" 4475 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4476 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 4477 " y;", 4478 Style); 4479 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4480 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4481 verifyFormat( 4482 "template <typename... a> struct t {};\n" 4483 "extern t<\n" 4484 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4485 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 4486 " y;", 4487 Style); 4488 } 4489 4490 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 4491 verifyFormat( 4492 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4493 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4494 verifyFormat( 4495 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4496 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4498 4499 // FIXME: Should we have the extra indent after the second break? 4500 verifyFormat( 4501 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4502 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4503 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4504 4505 verifyFormat( 4506 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 4507 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 4508 4509 // Breaking at nested name specifiers is generally not desirable. 4510 verifyFormat( 4511 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4512 " aaaaaaaaaaaaaaaaaaaaaaa);"); 4513 4514 verifyFormat( 4515 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 4516 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4518 " aaaaaaaaaaaaaaaaaaaaa);", 4519 getLLVMStyleWithColumns(74)); 4520 4521 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4523 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4524 } 4525 4526 TEST_F(FormatTest, UnderstandsTemplateParameters) { 4527 verifyFormat("A<int> a;"); 4528 verifyFormat("A<A<A<int>>> a;"); 4529 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 4530 verifyFormat("bool x = a < 1 || 2 > a;"); 4531 verifyFormat("bool x = 5 < f<int>();"); 4532 verifyFormat("bool x = f<int>() > 5;"); 4533 verifyFormat("bool x = 5 < a<int>::x;"); 4534 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 4535 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 4536 4537 verifyGoogleFormat("A<A<int>> a;"); 4538 verifyGoogleFormat("A<A<A<int>>> a;"); 4539 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 4540 verifyGoogleFormat("A<A<int> > a;"); 4541 verifyGoogleFormat("A<A<A<int> > > a;"); 4542 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 4543 verifyGoogleFormat("A<::A<int>> a;"); 4544 verifyGoogleFormat("A<::A> a;"); 4545 verifyGoogleFormat("A< ::A> a;"); 4546 verifyGoogleFormat("A< ::A<int> > a;"); 4547 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 4548 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 4549 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 4550 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 4551 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 4552 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 4553 4554 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 4555 4556 verifyFormat("test >> a >> b;"); 4557 verifyFormat("test << a >> b;"); 4558 4559 verifyFormat("f<int>();"); 4560 verifyFormat("template <typename T> void f() {}"); 4561 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 4562 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 4563 "sizeof(char)>::type>;"); 4564 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 4565 verifyFormat("f(a.operator()<A>());"); 4566 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4567 " .template operator()<A>());", 4568 getLLVMStyleWithColumns(35)); 4569 4570 // Not template parameters. 4571 verifyFormat("return a < b && c > d;"); 4572 verifyFormat("void f() {\n" 4573 " while (a < b && c > d) {\n" 4574 " }\n" 4575 "}"); 4576 verifyFormat("template <typename... Types>\n" 4577 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 4578 4579 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 4581 getLLVMStyleWithColumns(60)); 4582 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 4583 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 4584 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 4585 } 4586 4587 TEST_F(FormatTest, BitshiftOperatorWidth) { 4588 EXPECT_EQ("int a = 1 << 2; /* foo\n" 4589 " bar */", 4590 format("int a=1<<2; /* foo\n" 4591 " bar */")); 4592 4593 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 4594 " bar */", 4595 format("int b =256>>1 ; /* foo\n" 4596 " bar */")); 4597 } 4598 4599 TEST_F(FormatTest, UnderstandsBinaryOperators) { 4600 verifyFormat("COMPARE(a, ==, b);"); 4601 verifyFormat("auto s = sizeof...(Ts) - 1;"); 4602 } 4603 4604 TEST_F(FormatTest, UnderstandsPointersToMembers) { 4605 verifyFormat("int A::*x;"); 4606 verifyFormat("int (S::*func)(void *);"); 4607 verifyFormat("void f() { int (S::*func)(void *); }"); 4608 verifyFormat("typedef bool *(Class::*Member)() const;"); 4609 verifyFormat("void f() {\n" 4610 " (a->*f)();\n" 4611 " a->*x;\n" 4612 " (a.*f)();\n" 4613 " ((*a).*f)();\n" 4614 " a.*x;\n" 4615 "}"); 4616 verifyFormat("void f() {\n" 4617 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 4618 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 4619 "}"); 4620 verifyFormat( 4621 "(aaaaaaaaaa->*bbbbbbb)(\n" 4622 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4623 FormatStyle Style = getLLVMStyle(); 4624 Style.PointerAlignment = FormatStyle::PAS_Left; 4625 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 4626 } 4627 4628 TEST_F(FormatTest, UnderstandsUnaryOperators) { 4629 verifyFormat("int a = -2;"); 4630 verifyFormat("f(-1, -2, -3);"); 4631 verifyFormat("a[-1] = 5;"); 4632 verifyFormat("int a = 5 + -2;"); 4633 verifyFormat("if (i == -1) {\n}"); 4634 verifyFormat("if (i != -1) {\n}"); 4635 verifyFormat("if (i > -1) {\n}"); 4636 verifyFormat("if (i < -1) {\n}"); 4637 verifyFormat("++(a->f());"); 4638 verifyFormat("--(a->f());"); 4639 verifyFormat("(a->f())++;"); 4640 verifyFormat("a[42]++;"); 4641 verifyFormat("if (!(a->f())) {\n}"); 4642 4643 verifyFormat("a-- > b;"); 4644 verifyFormat("b ? -a : c;"); 4645 verifyFormat("n * sizeof char16;"); 4646 verifyFormat("n * alignof char16;", getGoogleStyle()); 4647 verifyFormat("sizeof(char);"); 4648 verifyFormat("alignof(char);", getGoogleStyle()); 4649 4650 verifyFormat("return -1;"); 4651 verifyFormat("switch (a) {\n" 4652 "case -1:\n" 4653 " break;\n" 4654 "}"); 4655 verifyFormat("#define X -1"); 4656 verifyFormat("#define X -kConstant"); 4657 4658 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 4659 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 4660 4661 verifyFormat("int a = /* confusing comment */ -1;"); 4662 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 4663 verifyFormat("int a = i /* confusing comment */++;"); 4664 } 4665 4666 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 4667 verifyFormat("if (!aaaaaaaaaa( // break\n" 4668 " aaaaa)) {\n" 4669 "}"); 4670 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 4671 " aaaaa));"); 4672 verifyFormat("*aaa = aaaaaaa( // break\n" 4673 " bbbbbb);"); 4674 } 4675 4676 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 4677 verifyFormat("bool operator<();"); 4678 verifyFormat("bool operator>();"); 4679 verifyFormat("bool operator=();"); 4680 verifyFormat("bool operator==();"); 4681 verifyFormat("bool operator!=();"); 4682 verifyFormat("int operator+();"); 4683 verifyFormat("int operator++();"); 4684 verifyFormat("bool operator,();"); 4685 verifyFormat("bool operator();"); 4686 verifyFormat("bool operator()();"); 4687 verifyFormat("bool operator[]();"); 4688 verifyFormat("operator bool();"); 4689 verifyFormat("operator int();"); 4690 verifyFormat("operator void *();"); 4691 verifyFormat("operator SomeType<int>();"); 4692 verifyFormat("operator SomeType<int, int>();"); 4693 verifyFormat("operator SomeType<SomeType<int>>();"); 4694 verifyFormat("void *operator new(std::size_t size);"); 4695 verifyFormat("void *operator new[](std::size_t size);"); 4696 verifyFormat("void operator delete(void *ptr);"); 4697 verifyFormat("void operator delete[](void *ptr);"); 4698 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 4699 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 4700 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 4701 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 4702 4703 verifyFormat( 4704 "ostream &operator<<(ostream &OutputStream,\n" 4705 " SomeReallyLongType WithSomeReallyLongValue);"); 4706 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 4707 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 4708 " return left.group < right.group;\n" 4709 "}"); 4710 verifyFormat("SomeType &operator=(const SomeType &S);"); 4711 verifyFormat("f.template operator()<int>();"); 4712 4713 verifyGoogleFormat("operator void*();"); 4714 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 4715 verifyGoogleFormat("operator ::A();"); 4716 4717 verifyFormat("using A::operator+;"); 4718 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 4719 "int i;"); 4720 } 4721 4722 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 4723 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 4724 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 4725 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 4726 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 4727 verifyFormat("Deleted &operator=(const Deleted &) &;"); 4728 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 4729 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 4730 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 4731 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 4732 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 4733 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 4734 verifyFormat("SomeType MemberFunction(const Deleted &) const &;"); 4735 verifyFormat("template <typename T>\n" 4736 "void F(T) && = delete;", 4737 getGoogleStyle()); 4738 4739 FormatStyle AlignLeft = getLLVMStyle(); 4740 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 4741 verifyFormat("void A::b() && {}", AlignLeft); 4742 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 4743 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 4744 AlignLeft); 4745 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 4746 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 4747 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 4748 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 4749 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 4750 verifyFormat("auto Function(T) & -> void;", AlignLeft); 4751 verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft); 4752 4753 FormatStyle Spaces = getLLVMStyle(); 4754 Spaces.SpacesInCStyleCastParentheses = true; 4755 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 4756 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 4757 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 4758 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 4759 4760 Spaces.SpacesInCStyleCastParentheses = false; 4761 Spaces.SpacesInParentheses = true; 4762 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 4763 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 4764 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 4765 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 4766 } 4767 4768 TEST_F(FormatTest, UnderstandsNewAndDelete) { 4769 verifyFormat("void f() {\n" 4770 " A *a = new A;\n" 4771 " A *a = new (placement) A;\n" 4772 " delete a;\n" 4773 " delete (A *)a;\n" 4774 "}"); 4775 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 4776 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 4777 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4778 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 4779 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 4780 verifyFormat("delete[] h->p;"); 4781 } 4782 4783 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 4784 verifyFormat("int *f(int *a) {}"); 4785 verifyFormat("int main(int argc, char **argv) {}"); 4786 verifyFormat("Test::Test(int b) : a(b * b) {}"); 4787 verifyIndependentOfContext("f(a, *a);"); 4788 verifyFormat("void g() { f(*a); }"); 4789 verifyIndependentOfContext("int a = b * 10;"); 4790 verifyIndependentOfContext("int a = 10 * b;"); 4791 verifyIndependentOfContext("int a = b * c;"); 4792 verifyIndependentOfContext("int a += b * c;"); 4793 verifyIndependentOfContext("int a -= b * c;"); 4794 verifyIndependentOfContext("int a *= b * c;"); 4795 verifyIndependentOfContext("int a /= b * c;"); 4796 verifyIndependentOfContext("int a = *b;"); 4797 verifyIndependentOfContext("int a = *b * c;"); 4798 verifyIndependentOfContext("int a = b * *c;"); 4799 verifyIndependentOfContext("int a = b * (10);"); 4800 verifyIndependentOfContext("S << b * (10);"); 4801 verifyIndependentOfContext("return 10 * b;"); 4802 verifyIndependentOfContext("return *b * *c;"); 4803 verifyIndependentOfContext("return a & ~b;"); 4804 verifyIndependentOfContext("f(b ? *c : *d);"); 4805 verifyIndependentOfContext("int a = b ? *c : *d;"); 4806 verifyIndependentOfContext("*b = a;"); 4807 verifyIndependentOfContext("a * ~b;"); 4808 verifyIndependentOfContext("a * !b;"); 4809 verifyIndependentOfContext("a * +b;"); 4810 verifyIndependentOfContext("a * -b;"); 4811 verifyIndependentOfContext("a * ++b;"); 4812 verifyIndependentOfContext("a * --b;"); 4813 verifyIndependentOfContext("a[4] * b;"); 4814 verifyIndependentOfContext("a[a * a] = 1;"); 4815 verifyIndependentOfContext("f() * b;"); 4816 verifyIndependentOfContext("a * [self dostuff];"); 4817 verifyIndependentOfContext("int x = a * (a + b);"); 4818 verifyIndependentOfContext("(a *)(a + b);"); 4819 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 4820 verifyIndependentOfContext("int *pa = (int *)&a;"); 4821 verifyIndependentOfContext("return sizeof(int **);"); 4822 verifyIndependentOfContext("return sizeof(int ******);"); 4823 verifyIndependentOfContext("return (int **&)a;"); 4824 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 4825 verifyFormat("void f(Type (*parameter)[10]) {}"); 4826 verifyFormat("void f(Type (¶meter)[10]) {}"); 4827 verifyGoogleFormat("return sizeof(int**);"); 4828 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 4829 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 4830 verifyFormat("auto a = [](int **&, int ***) {};"); 4831 verifyFormat("auto PointerBinding = [](const char *S) {};"); 4832 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 4833 verifyFormat("[](const decltype(*a) &value) {}"); 4834 verifyFormat("decltype(a * b) F();"); 4835 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 4836 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 4837 verifyIndependentOfContext("typedef void (*f)(int *a);"); 4838 verifyIndependentOfContext("int i{a * b};"); 4839 verifyIndependentOfContext("aaa && aaa->f();"); 4840 verifyIndependentOfContext("int x = ~*p;"); 4841 verifyFormat("Constructor() : a(a), area(width * height) {}"); 4842 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 4843 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 4844 verifyFormat("void f() { f(a, c * d); }"); 4845 verifyFormat("void f() { f(new a(), c * d); }"); 4846 verifyFormat("void f(const MyOverride &override);"); 4847 verifyFormat("void f(const MyFinal &final);"); 4848 verifyIndependentOfContext("bool a = f() && override.f();"); 4849 verifyIndependentOfContext("bool a = f() && final.f();"); 4850 4851 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 4852 4853 verifyIndependentOfContext("A<int *> a;"); 4854 verifyIndependentOfContext("A<int **> a;"); 4855 verifyIndependentOfContext("A<int *, int *> a;"); 4856 verifyIndependentOfContext("A<int *[]> a;"); 4857 verifyIndependentOfContext( 4858 "const char *const p = reinterpret_cast<const char *const>(q);"); 4859 verifyIndependentOfContext("A<int **, int **> a;"); 4860 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 4861 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 4862 verifyFormat("for (; a && b;) {\n}"); 4863 verifyFormat("bool foo = true && [] { return false; }();"); 4864 4865 verifyFormat( 4866 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4868 4869 verifyGoogleFormat("int const* a = &b;"); 4870 verifyGoogleFormat("**outparam = 1;"); 4871 verifyGoogleFormat("*outparam = a * b;"); 4872 verifyGoogleFormat("int main(int argc, char** argv) {}"); 4873 verifyGoogleFormat("A<int*> a;"); 4874 verifyGoogleFormat("A<int**> a;"); 4875 verifyGoogleFormat("A<int*, int*> a;"); 4876 verifyGoogleFormat("A<int**, int**> a;"); 4877 verifyGoogleFormat("f(b ? *c : *d);"); 4878 verifyGoogleFormat("int a = b ? *c : *d;"); 4879 verifyGoogleFormat("Type* t = **x;"); 4880 verifyGoogleFormat("Type* t = *++*x;"); 4881 verifyGoogleFormat("*++*x;"); 4882 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 4883 verifyGoogleFormat("Type* t = x++ * y;"); 4884 verifyGoogleFormat( 4885 "const char* const p = reinterpret_cast<const char* const>(q);"); 4886 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 4887 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 4888 verifyGoogleFormat("template <typename T>\n" 4889 "void f(int i = 0, SomeType** temps = NULL);"); 4890 4891 FormatStyle Left = getLLVMStyle(); 4892 Left.PointerAlignment = FormatStyle::PAS_Left; 4893 verifyFormat("x = *a(x) = *a(y);", Left); 4894 verifyFormat("for (;; *a = b) {\n}", Left); 4895 verifyFormat("return *this += 1;", Left); 4896 4897 verifyIndependentOfContext("a = *(x + y);"); 4898 verifyIndependentOfContext("a = &(x + y);"); 4899 verifyIndependentOfContext("*(x + y).call();"); 4900 verifyIndependentOfContext("&(x + y)->call();"); 4901 verifyFormat("void f() { &(*I).first; }"); 4902 4903 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 4904 verifyFormat( 4905 "int *MyValues = {\n" 4906 " *A, // Operator detection might be confused by the '{'\n" 4907 " *BB // Operator detection might be confused by previous comment\n" 4908 "};"); 4909 4910 verifyIndependentOfContext("if (int *a = &b)"); 4911 verifyIndependentOfContext("if (int &a = *b)"); 4912 verifyIndependentOfContext("if (a & b[i])"); 4913 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 4914 verifyIndependentOfContext("if (*b[i])"); 4915 verifyIndependentOfContext("if (int *a = (&b))"); 4916 verifyIndependentOfContext("while (int *a = &b)"); 4917 verifyIndependentOfContext("size = sizeof *a;"); 4918 verifyIndependentOfContext("if (a && (b = c))"); 4919 verifyFormat("void f() {\n" 4920 " for (const int &v : Values) {\n" 4921 " }\n" 4922 "}"); 4923 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 4924 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 4925 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 4926 4927 verifyFormat("#define A (!a * b)"); 4928 verifyFormat("#define MACRO \\\n" 4929 " int *i = a * b; \\\n" 4930 " void f(a *b);", 4931 getLLVMStyleWithColumns(19)); 4932 4933 verifyIndependentOfContext("A = new SomeType *[Length];"); 4934 verifyIndependentOfContext("A = new SomeType *[Length]();"); 4935 verifyIndependentOfContext("T **t = new T *;"); 4936 verifyIndependentOfContext("T **t = new T *();"); 4937 verifyGoogleFormat("A = new SomeType*[Length]();"); 4938 verifyGoogleFormat("A = new SomeType*[Length];"); 4939 verifyGoogleFormat("T** t = new T*;"); 4940 verifyGoogleFormat("T** t = new T*();"); 4941 4942 FormatStyle PointerLeft = getLLVMStyle(); 4943 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 4944 verifyFormat("delete *x;", PointerLeft); 4945 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 4946 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 4947 verifyFormat("template <bool a, bool b> " 4948 "typename t::if<x && y>::type f() {}"); 4949 verifyFormat("template <int *y> f() {}"); 4950 verifyFormat("vector<int *> v;"); 4951 verifyFormat("vector<int *const> v;"); 4952 verifyFormat("vector<int *const **const *> v;"); 4953 verifyFormat("vector<int *volatile> v;"); 4954 verifyFormat("vector<a * b> v;"); 4955 verifyFormat("foo<b && false>();"); 4956 verifyFormat("foo<b & 1>();"); 4957 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 4958 verifyFormat( 4959 "template <class T, class = typename std::enable_if<\n" 4960 " std::is_integral<T>::value &&\n" 4961 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 4962 "void F();", 4963 getLLVMStyleWithColumns(70)); 4964 verifyFormat( 4965 "template <class T,\n" 4966 " class = typename std::enable_if<\n" 4967 " std::is_integral<T>::value &&\n" 4968 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 4969 " class U>\n" 4970 "void F();", 4971 getLLVMStyleWithColumns(70)); 4972 verifyFormat( 4973 "template <class T,\n" 4974 " class = typename ::std::enable_if<\n" 4975 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 4976 "void F();", 4977 getGoogleStyleWithColumns(68)); 4978 4979 verifyIndependentOfContext("MACRO(int *i);"); 4980 verifyIndependentOfContext("MACRO(auto *a);"); 4981 verifyIndependentOfContext("MACRO(const A *a);"); 4982 verifyIndependentOfContext("MACRO(A *const a);"); 4983 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 4984 verifyFormat("void f() { f(float{1}, a * a); }"); 4985 // FIXME: Is there a way to make this work? 4986 // verifyIndependentOfContext("MACRO(A *a);"); 4987 4988 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 4989 verifyFormat("return options != nullptr && operator==(*options);"); 4990 4991 EXPECT_EQ("#define OP(x) \\\n" 4992 " ostream &operator<<(ostream &s, const A &a) { \\\n" 4993 " return s << a.DebugString(); \\\n" 4994 " }", 4995 format("#define OP(x) \\\n" 4996 " ostream &operator<<(ostream &s, const A &a) { \\\n" 4997 " return s << a.DebugString(); \\\n" 4998 " }", 4999 getLLVMStyleWithColumns(50))); 5000 5001 // FIXME: We cannot handle this case yet; we might be able to figure out that 5002 // foo<x> d > v; doesn't make sense. 5003 verifyFormat("foo<a<b && c> d> v;"); 5004 5005 FormatStyle PointerMiddle = getLLVMStyle(); 5006 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5007 verifyFormat("delete *x;", PointerMiddle); 5008 verifyFormat("int * x;", PointerMiddle); 5009 verifyFormat("template <int * y> f() {}", PointerMiddle); 5010 verifyFormat("int * f(int * a) {}", PointerMiddle); 5011 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5012 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5013 verifyFormat("A<int *> a;", PointerMiddle); 5014 verifyFormat("A<int **> a;", PointerMiddle); 5015 verifyFormat("A<int *, int *> a;", PointerMiddle); 5016 verifyFormat("A<int * []> a;", PointerMiddle); 5017 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5018 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5019 verifyFormat("T ** t = new T *;", PointerMiddle); 5020 5021 // Member function reference qualifiers aren't binary operators. 5022 verifyFormat("string // break\n" 5023 "operator()() & {}"); 5024 verifyFormat("string // break\n" 5025 "operator()() && {}"); 5026 verifyGoogleFormat("template <typename T>\n" 5027 "auto x() & -> int {}"); 5028 } 5029 5030 TEST_F(FormatTest, UnderstandsAttributes) { 5031 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5032 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5033 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5034 FormatStyle AfterType = getLLVMStyle(); 5035 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5036 verifyFormat("__attribute__((nodebug)) void\n" 5037 "foo() {}\n", 5038 AfterType); 5039 } 5040 5041 TEST_F(FormatTest, UnderstandsEllipsis) { 5042 verifyFormat("int printf(const char *fmt, ...);"); 5043 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5044 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5045 5046 FormatStyle PointersLeft = getLLVMStyle(); 5047 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5048 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5049 } 5050 5051 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5052 EXPECT_EQ("int *a;\n" 5053 "int *a;\n" 5054 "int *a;", 5055 format("int *a;\n" 5056 "int* a;\n" 5057 "int *a;", 5058 getGoogleStyle())); 5059 EXPECT_EQ("int* a;\n" 5060 "int* a;\n" 5061 "int* a;", 5062 format("int* a;\n" 5063 "int* a;\n" 5064 "int *a;", 5065 getGoogleStyle())); 5066 EXPECT_EQ("int *a;\n" 5067 "int *a;\n" 5068 "int *a;", 5069 format("int *a;\n" 5070 "int * a;\n" 5071 "int * a;", 5072 getGoogleStyle())); 5073 EXPECT_EQ("auto x = [] {\n" 5074 " int *a;\n" 5075 " int *a;\n" 5076 " int *a;\n" 5077 "};", 5078 format("auto x=[]{int *a;\n" 5079 "int * a;\n" 5080 "int * a;};", 5081 getGoogleStyle())); 5082 } 5083 5084 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5085 verifyFormat("int f(int &&a) {}"); 5086 verifyFormat("int f(int a, char &&b) {}"); 5087 verifyFormat("void f() { int &&a = b; }"); 5088 verifyGoogleFormat("int f(int a, char&& b) {}"); 5089 verifyGoogleFormat("void f() { int&& a = b; }"); 5090 5091 verifyIndependentOfContext("A<int &&> a;"); 5092 verifyIndependentOfContext("A<int &&, int &&> a;"); 5093 verifyGoogleFormat("A<int&&> a;"); 5094 verifyGoogleFormat("A<int&&, int&&> a;"); 5095 5096 // Not rvalue references: 5097 verifyFormat("template <bool B, bool C> class A {\n" 5098 " static_assert(B && C, \"Something is wrong\");\n" 5099 "};"); 5100 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5101 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5102 verifyFormat("#define A(a, b) (a && b)"); 5103 } 5104 5105 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5106 verifyFormat("void f() {\n" 5107 " x[aaaaaaaaa -\n" 5108 " b] = 23;\n" 5109 "}", 5110 getLLVMStyleWithColumns(15)); 5111 } 5112 5113 TEST_F(FormatTest, FormatsCasts) { 5114 verifyFormat("Type *A = static_cast<Type *>(P);"); 5115 verifyFormat("Type *A = (Type *)P;"); 5116 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5117 verifyFormat("int a = (int)(2.0f);"); 5118 verifyFormat("int a = (int)2.0f;"); 5119 verifyFormat("x[(int32)y];"); 5120 verifyFormat("x = (int32)y;"); 5121 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5122 verifyFormat("int a = (int)*b;"); 5123 verifyFormat("int a = (int)2.0f;"); 5124 verifyFormat("int a = (int)~0;"); 5125 verifyFormat("int a = (int)++a;"); 5126 verifyFormat("int a = (int)sizeof(int);"); 5127 verifyFormat("int a = (int)+2;"); 5128 verifyFormat("my_int a = (my_int)2.0f;"); 5129 verifyFormat("my_int a = (my_int)sizeof(int);"); 5130 verifyFormat("return (my_int)aaa;"); 5131 verifyFormat("#define x ((int)-1)"); 5132 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5133 verifyFormat("#define p(q) ((int *)&q)"); 5134 verifyFormat("fn(a)(b) + 1;"); 5135 5136 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5137 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5138 verifyFormat("my_int a = (my_int)~0;"); 5139 verifyFormat("my_int a = (my_int)++a;"); 5140 verifyFormat("my_int a = (my_int)-2;"); 5141 verifyFormat("my_int a = (my_int)1;"); 5142 verifyFormat("my_int a = (my_int *)1;"); 5143 verifyFormat("my_int a = (const my_int)-1;"); 5144 verifyFormat("my_int a = (const my_int *)-1;"); 5145 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5146 verifyFormat("my_int a = (ns::my_int)-2;"); 5147 verifyFormat("case (my_int)ONE:"); 5148 verifyFormat("auto x = (X)this;"); 5149 5150 // FIXME: single value wrapped with paren will be treated as cast. 5151 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5152 5153 verifyFormat("{ (void)F; }"); 5154 5155 // Don't break after a cast's 5156 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5157 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5158 " bbbbbbbbbbbbbbbbbbbbbb);"); 5159 5160 // These are not casts. 5161 verifyFormat("void f(int *) {}"); 5162 verifyFormat("f(foo)->b;"); 5163 verifyFormat("f(foo).b;"); 5164 verifyFormat("f(foo)(b);"); 5165 verifyFormat("f(foo)[b];"); 5166 verifyFormat("[](foo) { return 4; }(bar);"); 5167 verifyFormat("(*funptr)(foo)[4];"); 5168 verifyFormat("funptrs[4](foo)[4];"); 5169 verifyFormat("void f(int *);"); 5170 verifyFormat("void f(int *) = 0;"); 5171 verifyFormat("void f(SmallVector<int>) {}"); 5172 verifyFormat("void f(SmallVector<int>);"); 5173 verifyFormat("void f(SmallVector<int>) = 0;"); 5174 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5175 verifyFormat("int a = sizeof(int) * b;"); 5176 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5177 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5178 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5179 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5180 5181 // These are not casts, but at some point were confused with casts. 5182 verifyFormat("virtual void foo(int *) override;"); 5183 verifyFormat("virtual void foo(char &) const;"); 5184 verifyFormat("virtual void foo(int *a, char *) const;"); 5185 verifyFormat("int a = sizeof(int *) + b;"); 5186 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 5187 verifyFormat("bool b = f(g<int>) && c;"); 5188 verifyFormat("typedef void (*f)(int i) func;"); 5189 5190 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 5191 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5192 // FIXME: The indentation here is not ideal. 5193 verifyFormat( 5194 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5195 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 5196 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 5197 } 5198 5199 TEST_F(FormatTest, FormatsFunctionTypes) { 5200 verifyFormat("A<bool()> a;"); 5201 verifyFormat("A<SomeType()> a;"); 5202 verifyFormat("A<void (*)(int, std::string)> a;"); 5203 verifyFormat("A<void *(int)>;"); 5204 verifyFormat("void *(*a)(int *, SomeType *);"); 5205 verifyFormat("int (*func)(void *);"); 5206 verifyFormat("void f() { int (*func)(void *); }"); 5207 verifyFormat("template <class CallbackClass>\n" 5208 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 5209 5210 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 5211 verifyGoogleFormat("void* (*a)(int);"); 5212 verifyGoogleFormat( 5213 "template <class CallbackClass>\n" 5214 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 5215 5216 // Other constructs can look somewhat like function types: 5217 verifyFormat("A<sizeof(*x)> a;"); 5218 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 5219 verifyFormat("some_var = function(*some_pointer_var)[0];"); 5220 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 5221 verifyFormat("int x = f(&h)();"); 5222 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 5223 } 5224 5225 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 5226 verifyFormat("A (*foo_)[6];"); 5227 verifyFormat("vector<int> (*foo_)[6];"); 5228 } 5229 5230 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 5231 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5232 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5233 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 5234 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5235 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5236 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5237 5238 // Different ways of ()-initializiation. 5239 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5240 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 5241 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5242 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 5243 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5244 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 5245 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5246 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 5247 5248 // Lambdas should not confuse the variable declaration heuristic. 5249 verifyFormat("LooooooooooooooooongType\n" 5250 " variable(nullptr, [](A *a) {});", 5251 getLLVMStyleWithColumns(40)); 5252 } 5253 5254 TEST_F(FormatTest, BreaksLongDeclarations) { 5255 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 5256 " AnotherNameForTheLongType;"); 5257 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 5258 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5259 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5260 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5261 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 5262 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5263 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5264 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5265 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 5266 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5267 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5268 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5269 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5270 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5271 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5272 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 5273 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5274 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 5275 FormatStyle Indented = getLLVMStyle(); 5276 Indented.IndentWrappedFunctionNames = true; 5277 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5278 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 5279 Indented); 5280 verifyFormat( 5281 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5282 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5283 Indented); 5284 verifyFormat( 5285 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5286 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5287 Indented); 5288 verifyFormat( 5289 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5290 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5291 Indented); 5292 5293 // FIXME: Without the comment, this breaks after "(". 5294 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 5295 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 5296 getGoogleStyle()); 5297 5298 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 5299 " int LoooooooooooooooooooongParam2) {}"); 5300 verifyFormat( 5301 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 5302 " SourceLocation L, IdentifierIn *II,\n" 5303 " Type *T) {}"); 5304 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 5305 "ReallyReaaallyLongFunctionName(\n" 5306 " const std::string &SomeParameter,\n" 5307 " const SomeType<string, SomeOtherTemplateParameter>\n" 5308 " &ReallyReallyLongParameterName,\n" 5309 " const SomeType<string, SomeOtherTemplateParameter>\n" 5310 " &AnotherLongParameterName) {}"); 5311 verifyFormat("template <typename A>\n" 5312 "SomeLoooooooooooooooooooooongType<\n" 5313 " typename some_namespace::SomeOtherType<A>::Type>\n" 5314 "Function() {}"); 5315 5316 verifyGoogleFormat( 5317 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 5318 " aaaaaaaaaaaaaaaaaaaaaaa;"); 5319 verifyGoogleFormat( 5320 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 5321 " SourceLocation L) {}"); 5322 verifyGoogleFormat( 5323 "some_namespace::LongReturnType\n" 5324 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 5325 " int first_long_parameter, int second_parameter) {}"); 5326 5327 verifyGoogleFormat("template <typename T>\n" 5328 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5329 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 5330 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5331 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 5332 5333 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5334 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5335 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5336 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5337 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5338 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5339 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5340 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 5342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5343 5344 verifyFormat("template <typename T> // Templates on own line.\n" 5345 "static int // Some comment.\n" 5346 "MyFunction(int a);", 5347 getLLVMStyle()); 5348 } 5349 5350 TEST_F(FormatTest, FormatsArrays) { 5351 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5352 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 5353 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 5354 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 5355 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 5356 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 5357 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5358 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5359 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5360 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 5361 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5362 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5363 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5364 verifyFormat( 5365 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 5366 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5367 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 5368 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 5369 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5370 5371 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 5372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 5373 verifyFormat( 5374 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 5375 " .aaaaaaa[0]\n" 5376 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5377 verifyFormat("a[::b::c];"); 5378 5379 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 5380 5381 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 5382 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 5383 } 5384 5385 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 5386 verifyFormat("(a)->b();"); 5387 verifyFormat("--a;"); 5388 } 5389 5390 TEST_F(FormatTest, HandlesIncludeDirectives) { 5391 verifyFormat("#include <string>\n" 5392 "#include <a/b/c.h>\n" 5393 "#include \"a/b/string\"\n" 5394 "#include \"string.h\"\n" 5395 "#include \"string.h\"\n" 5396 "#include <a-a>\n" 5397 "#include < path with space >\n" 5398 "#include_next <test.h>" 5399 "#include \"abc.h\" // this is included for ABC\n" 5400 "#include \"some long include\" // with a comment\n" 5401 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 5402 getLLVMStyleWithColumns(35)); 5403 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 5404 EXPECT_EQ("#include <a>", format("#include<a>")); 5405 5406 verifyFormat("#import <string>"); 5407 verifyFormat("#import <a/b/c.h>"); 5408 verifyFormat("#import \"a/b/string\""); 5409 verifyFormat("#import \"string.h\""); 5410 verifyFormat("#import \"string.h\""); 5411 verifyFormat("#if __has_include(<strstream>)\n" 5412 "#include <strstream>\n" 5413 "#endif"); 5414 5415 verifyFormat("#define MY_IMPORT <a/b>"); 5416 5417 verifyFormat("#if __has_include(<a/b>)"); 5418 verifyFormat("#if __has_include_next(<a/b>)"); 5419 verifyFormat("#define F __has_include(<a/b>)"); 5420 verifyFormat("#define F __has_include_next(<a/b>)"); 5421 5422 // Protocol buffer definition or missing "#". 5423 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 5424 getLLVMStyleWithColumns(30)); 5425 5426 FormatStyle Style = getLLVMStyle(); 5427 Style.AlwaysBreakBeforeMultilineStrings = true; 5428 Style.ColumnLimit = 0; 5429 verifyFormat("#import \"abc.h\"", Style); 5430 5431 // But 'import' might also be a regular C++ namespace. 5432 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5434 } 5435 5436 //===----------------------------------------------------------------------===// 5437 // Error recovery tests. 5438 //===----------------------------------------------------------------------===// 5439 5440 TEST_F(FormatTest, IncompleteParameterLists) { 5441 FormatStyle NoBinPacking = getLLVMStyle(); 5442 NoBinPacking.BinPackParameters = false; 5443 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 5444 " double *min_x,\n" 5445 " double *max_x,\n" 5446 " double *min_y,\n" 5447 " double *max_y,\n" 5448 " double *min_z,\n" 5449 " double *max_z, ) {}", 5450 NoBinPacking); 5451 } 5452 5453 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 5454 verifyFormat("void f() { return; }\n42"); 5455 verifyFormat("void f() {\n" 5456 " if (0)\n" 5457 " return;\n" 5458 "}\n" 5459 "42"); 5460 verifyFormat("void f() { return }\n42"); 5461 verifyFormat("void f() {\n" 5462 " if (0)\n" 5463 " return\n" 5464 "}\n" 5465 "42"); 5466 } 5467 5468 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 5469 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 5470 EXPECT_EQ("void f() {\n" 5471 " if (a)\n" 5472 " return\n" 5473 "}", 5474 format("void f ( ) { if ( a ) return }")); 5475 EXPECT_EQ("namespace N {\n" 5476 "void f()\n" 5477 "}", 5478 format("namespace N { void f() }")); 5479 EXPECT_EQ("namespace N {\n" 5480 "void f() {}\n" 5481 "void g()\n" 5482 "} // namespace N", 5483 format("namespace N { void f( ) { } void g( ) }")); 5484 } 5485 5486 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 5487 verifyFormat("int aaaaaaaa =\n" 5488 " // Overlylongcomment\n" 5489 " b;", 5490 getLLVMStyleWithColumns(20)); 5491 verifyFormat("function(\n" 5492 " ShortArgument,\n" 5493 " LoooooooooooongArgument);\n", 5494 getLLVMStyleWithColumns(20)); 5495 } 5496 5497 TEST_F(FormatTest, IncorrectAccessSpecifier) { 5498 verifyFormat("public:"); 5499 verifyFormat("class A {\n" 5500 "public\n" 5501 " void f() {}\n" 5502 "};"); 5503 verifyFormat("public\n" 5504 "int qwerty;"); 5505 verifyFormat("public\n" 5506 "B {}"); 5507 verifyFormat("public\n" 5508 "{}"); 5509 verifyFormat("public\n" 5510 "B { int x; }"); 5511 } 5512 5513 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 5514 verifyFormat("{"); 5515 verifyFormat("#})"); 5516 verifyNoCrash("(/**/[:!] ?[)."); 5517 } 5518 5519 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 5520 verifyFormat("do {\n}"); 5521 verifyFormat("do {\n}\n" 5522 "f();"); 5523 verifyFormat("do {\n}\n" 5524 "wheeee(fun);"); 5525 verifyFormat("do {\n" 5526 " f();\n" 5527 "}"); 5528 } 5529 5530 TEST_F(FormatTest, IncorrectCodeMissingParens) { 5531 verifyFormat("if {\n foo;\n foo();\n}"); 5532 verifyFormat("switch {\n foo;\n foo();\n}"); 5533 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 5534 verifyFormat("while {\n foo;\n foo();\n}"); 5535 verifyFormat("do {\n foo;\n foo();\n} while;"); 5536 } 5537 5538 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 5539 verifyIncompleteFormat("namespace {\n" 5540 "class Foo { Foo (\n" 5541 "};\n" 5542 "} // namespace"); 5543 } 5544 5545 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 5546 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 5547 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 5548 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 5549 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 5550 5551 EXPECT_EQ("{\n" 5552 " {\n" 5553 " breakme(\n" 5554 " qwe);\n" 5555 " }\n", 5556 format("{\n" 5557 " {\n" 5558 " breakme(qwe);\n" 5559 "}\n", 5560 getLLVMStyleWithColumns(10))); 5561 } 5562 5563 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 5564 verifyFormat("int x = {\n" 5565 " avariable,\n" 5566 " b(alongervariable)};", 5567 getLLVMStyleWithColumns(25)); 5568 } 5569 5570 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 5571 verifyFormat("return (a)(b){1, 2, 3};"); 5572 } 5573 5574 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 5575 verifyFormat("vector<int> x{1, 2, 3, 4};"); 5576 verifyFormat("vector<int> x{\n" 5577 " 1, 2, 3, 4,\n" 5578 "};"); 5579 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 5580 verifyFormat("f({1, 2});"); 5581 verifyFormat("auto v = Foo{-1};"); 5582 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 5583 verifyFormat("Class::Class : member{1, 2, 3} {}"); 5584 verifyFormat("new vector<int>{1, 2, 3};"); 5585 verifyFormat("new int[3]{1, 2, 3};"); 5586 verifyFormat("new int{1};"); 5587 verifyFormat("return {arg1, arg2};"); 5588 verifyFormat("return {arg1, SomeType{parameter}};"); 5589 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 5590 verifyFormat("new T{arg1, arg2};"); 5591 verifyFormat("f(MyMap[{composite, key}]);"); 5592 verifyFormat("class Class {\n" 5593 " T member = {arg1, arg2};\n" 5594 "};"); 5595 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 5596 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 5597 verifyFormat("int a = std::is_integral<int>{} + 0;"); 5598 5599 verifyFormat("int foo(int i) { return fo1{}(i); }"); 5600 verifyFormat("int foo(int i) { return fo1{}(i); }"); 5601 verifyFormat("auto i = decltype(x){};"); 5602 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 5603 verifyFormat("Node n{1, Node{1000}, //\n" 5604 " 2};"); 5605 verifyFormat("Aaaa aaaaaaa{\n" 5606 " {\n" 5607 " aaaa,\n" 5608 " },\n" 5609 "};"); 5610 verifyFormat("class C : public D {\n" 5611 " SomeClass SC{2};\n" 5612 "};"); 5613 verifyFormat("class C : public A {\n" 5614 " class D : public B {\n" 5615 " void f() { int i{2}; }\n" 5616 " };\n" 5617 "};"); 5618 verifyFormat("#define A {a, a},"); 5619 5620 // Cases where distinguising braced lists and blocks is hard. 5621 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 5622 verifyFormat("void f() {\n" 5623 " return; // comment\n" 5624 "}\n" 5625 "SomeType t;"); 5626 verifyFormat("void f() {\n" 5627 " if (a) {\n" 5628 " f();\n" 5629 " }\n" 5630 "}\n" 5631 "SomeType t;"); 5632 5633 // In combination with BinPackArguments = false. 5634 FormatStyle NoBinPacking = getLLVMStyle(); 5635 NoBinPacking.BinPackArguments = false; 5636 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 5637 " bbbbb,\n" 5638 " ccccc,\n" 5639 " ddddd,\n" 5640 " eeeee,\n" 5641 " ffffff,\n" 5642 " ggggg,\n" 5643 " hhhhhh,\n" 5644 " iiiiii,\n" 5645 " jjjjjj,\n" 5646 " kkkkkk};", 5647 NoBinPacking); 5648 verifyFormat("const Aaaaaa aaaaa = {\n" 5649 " aaaaa,\n" 5650 " bbbbb,\n" 5651 " ccccc,\n" 5652 " ddddd,\n" 5653 " eeeee,\n" 5654 " ffffff,\n" 5655 " ggggg,\n" 5656 " hhhhhh,\n" 5657 " iiiiii,\n" 5658 " jjjjjj,\n" 5659 " kkkkkk,\n" 5660 "};", 5661 NoBinPacking); 5662 verifyFormat( 5663 "const Aaaaaa aaaaa = {\n" 5664 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 5665 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 5666 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 5667 "};", 5668 NoBinPacking); 5669 5670 // FIXME: The alignment of these trailing comments might be bad. Then again, 5671 // this might be utterly useless in real code. 5672 verifyFormat("Constructor::Constructor()\n" 5673 " : some_value{ //\n" 5674 " aaaaaaa, //\n" 5675 " bbbbbbb} {}"); 5676 5677 // In braced lists, the first comment is always assumed to belong to the 5678 // first element. Thus, it can be moved to the next or previous line as 5679 // appropriate. 5680 EXPECT_EQ("function({// First element:\n" 5681 " 1,\n" 5682 " // Second element:\n" 5683 " 2});", 5684 format("function({\n" 5685 " // First element:\n" 5686 " 1,\n" 5687 " // Second element:\n" 5688 " 2});")); 5689 EXPECT_EQ("std::vector<int> MyNumbers{\n" 5690 " // First element:\n" 5691 " 1,\n" 5692 " // Second element:\n" 5693 " 2};", 5694 format("std::vector<int> MyNumbers{// First element:\n" 5695 " 1,\n" 5696 " // Second element:\n" 5697 " 2};", 5698 getLLVMStyleWithColumns(30))); 5699 // A trailing comma should still lead to an enforced line break. 5700 EXPECT_EQ("vector<int> SomeVector = {\n" 5701 " // aaa\n" 5702 " 1, 2,\n" 5703 "};", 5704 format("vector<int> SomeVector = { // aaa\n" 5705 " 1, 2, };")); 5706 5707 FormatStyle ExtraSpaces = getLLVMStyle(); 5708 ExtraSpaces.Cpp11BracedListStyle = false; 5709 ExtraSpaces.ColumnLimit = 75; 5710 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 5711 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 5712 verifyFormat("f({ 1, 2 });", ExtraSpaces); 5713 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 5714 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 5715 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 5716 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 5717 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 5718 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 5719 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 5720 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 5721 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 5722 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 5723 verifyFormat("class Class {\n" 5724 " T member = { arg1, arg2 };\n" 5725 "};", 5726 ExtraSpaces); 5727 verifyFormat( 5728 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5729 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 5730 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5731 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 5732 ExtraSpaces); 5733 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 5734 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 5735 ExtraSpaces); 5736 verifyFormat( 5737 "someFunction(OtherParam,\n" 5738 " BracedList{ // comment 1 (Forcing interesting break)\n" 5739 " param1, param2,\n" 5740 " // comment 2\n" 5741 " param3, param4 });", 5742 ExtraSpaces); 5743 verifyFormat( 5744 "std::this_thread::sleep_for(\n" 5745 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 5746 ExtraSpaces); 5747 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 5748 " aaaaaaa,\n" 5749 " aaaaaaaaaa,\n" 5750 " aaaaa,\n" 5751 " aaaaaaaaaaaaaaa,\n" 5752 " aaa,\n" 5753 " aaaaaaaaaa,\n" 5754 " a,\n" 5755 " aaaaaaaaaaaaaaaaaaaaa,\n" 5756 " aaaaaaaaaaaa,\n" 5757 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 5758 " aaaaaaa,\n" 5759 " a};"); 5760 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 5761 } 5762 5763 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 5764 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5765 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5766 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5767 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5768 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5769 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 5770 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 5771 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5772 " 1, 22, 333, 4444, 55555, //\n" 5773 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5774 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 5775 verifyFormat( 5776 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5777 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5778 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 5779 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 5780 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 5781 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 5782 " 7777777};"); 5783 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 5784 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 5785 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 5786 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 5787 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 5788 " // Separating comment.\n" 5789 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 5790 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 5791 " // Leading comment\n" 5792 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 5793 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 5794 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 5795 " 1, 1, 1, 1};", 5796 getLLVMStyleWithColumns(39)); 5797 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 5798 " 1, 1, 1, 1};", 5799 getLLVMStyleWithColumns(38)); 5800 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 5801 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 5802 getLLVMStyleWithColumns(43)); 5803 verifyFormat( 5804 "static unsigned SomeValues[10][3] = {\n" 5805 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 5806 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 5807 verifyFormat("static auto fields = new vector<string>{\n" 5808 " \"aaaaaaaaaaaaa\",\n" 5809 " \"aaaaaaaaaaaaa\",\n" 5810 " \"aaaaaaaaaaaa\",\n" 5811 " \"aaaaaaaaaaaaaa\",\n" 5812 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 5813 " \"aaaaaaaaaaaa\",\n" 5814 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 5815 "};"); 5816 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 5817 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 5818 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 5819 " 3, cccccccccccccccccccccc};", 5820 getLLVMStyleWithColumns(60)); 5821 5822 // Trailing commas. 5823 verifyFormat("vector<int> x = {\n" 5824 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 5825 "};", 5826 getLLVMStyleWithColumns(39)); 5827 verifyFormat("vector<int> x = {\n" 5828 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 5829 "};", 5830 getLLVMStyleWithColumns(39)); 5831 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 5832 " 1, 1, 1, 1,\n" 5833 " /**/ /**/};", 5834 getLLVMStyleWithColumns(39)); 5835 5836 // Trailing comment in the first line. 5837 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 5838 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 5839 " 111111111, 222222222, 3333333333, 444444444, //\n" 5840 " 11111111, 22222222, 333333333, 44444444};"); 5841 // Trailing comment in the last line. 5842 verifyFormat("int aaaaa[] = {\n" 5843 " 1, 2, 3, // comment\n" 5844 " 4, 5, 6 // comment\n" 5845 "};"); 5846 5847 // With nested lists, we should either format one item per line or all nested 5848 // lists one on line. 5849 // FIXME: For some nested lists, we can do better. 5850 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 5851 " {aaaaaaaaaaaaaaaaaaa},\n" 5852 " {aaaaaaaaaaaaaaaaaaaaa},\n" 5853 " {aaaaaaaaaaaaaaaaa}};", 5854 getLLVMStyleWithColumns(60)); 5855 verifyFormat( 5856 "SomeStruct my_struct_array = {\n" 5857 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 5858 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 5859 " {aaa, aaa},\n" 5860 " {aaa, aaa},\n" 5861 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 5862 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5863 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 5864 5865 // No column layout should be used here. 5866 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 5867 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 5868 5869 verifyNoCrash("a<,"); 5870 5871 // No braced initializer here. 5872 verifyFormat("void f() {\n" 5873 " struct Dummy {};\n" 5874 " f(v);\n" 5875 "}"); 5876 5877 // Long lists should be formatted in columns even if they are nested. 5878 verifyFormat( 5879 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5880 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5881 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5882 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5883 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 5884 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 5885 5886 // Allow "single-column" layout even if that violates the column limit. There 5887 // isn't going to be a better way. 5888 verifyFormat("std::vector<int> a = {\n" 5889 " aaaaaaaa,\n" 5890 " aaaaaaaa,\n" 5891 " aaaaaaaa,\n" 5892 " aaaaaaaa,\n" 5893 " aaaaaaaaaa,\n" 5894 " aaaaaaaa,\n" 5895 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 5896 getLLVMStyleWithColumns(30)); 5897 verifyFormat("vector<int> aaaa = {\n" 5898 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5899 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5900 " aaaaaa.aaaaaaa,\n" 5901 " aaaaaa.aaaaaaa,\n" 5902 " aaaaaa.aaaaaaa,\n" 5903 " aaaaaa.aaaaaaa,\n" 5904 "};"); 5905 5906 // Don't create hanging lists. 5907 verifyFormat("someFunction(Param, {List1, List2,\n" 5908 " List3});", 5909 getLLVMStyleWithColumns(35)); 5910 verifyFormat("someFunction(Param, Param,\n" 5911 " {List1, List2,\n" 5912 " List3});", 5913 getLLVMStyleWithColumns(35)); 5914 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 5915 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5916 } 5917 5918 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 5919 FormatStyle DoNotMerge = getLLVMStyle(); 5920 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 5921 5922 verifyFormat("void f() { return 42; }"); 5923 verifyFormat("void f() {\n" 5924 " return 42;\n" 5925 "}", 5926 DoNotMerge); 5927 verifyFormat("void f() {\n" 5928 " // Comment\n" 5929 "}"); 5930 verifyFormat("{\n" 5931 "#error {\n" 5932 " int a;\n" 5933 "}"); 5934 verifyFormat("{\n" 5935 " int a;\n" 5936 "#error {\n" 5937 "}"); 5938 verifyFormat("void f() {} // comment"); 5939 verifyFormat("void f() { int a; } // comment"); 5940 verifyFormat("void f() {\n" 5941 "} // comment", 5942 DoNotMerge); 5943 verifyFormat("void f() {\n" 5944 " int a;\n" 5945 "} // comment", 5946 DoNotMerge); 5947 verifyFormat("void f() {\n" 5948 "} // comment", 5949 getLLVMStyleWithColumns(15)); 5950 5951 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 5952 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 5953 5954 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 5955 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 5956 verifyFormat("class C {\n" 5957 " C()\n" 5958 " : iiiiiiii(nullptr),\n" 5959 " kkkkkkk(nullptr),\n" 5960 " mmmmmmm(nullptr),\n" 5961 " nnnnnnn(nullptr) {}\n" 5962 "};", 5963 getGoogleStyle()); 5964 5965 FormatStyle NoColumnLimit = getLLVMStyle(); 5966 NoColumnLimit.ColumnLimit = 0; 5967 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 5968 EXPECT_EQ("class C {\n" 5969 " A() : b(0) {}\n" 5970 "};", 5971 format("class C{A():b(0){}};", NoColumnLimit)); 5972 EXPECT_EQ("A()\n" 5973 " : b(0) {\n" 5974 "}", 5975 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 5976 5977 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 5978 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 5979 FormatStyle::SFS_None; 5980 EXPECT_EQ("A()\n" 5981 " : b(0) {\n" 5982 "}", 5983 format("A():b(0){}", DoNotMergeNoColumnLimit)); 5984 EXPECT_EQ("A()\n" 5985 " : b(0) {\n" 5986 "}", 5987 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 5988 5989 verifyFormat("#define A \\\n" 5990 " void f() { \\\n" 5991 " int i; \\\n" 5992 " }", 5993 getLLVMStyleWithColumns(20)); 5994 verifyFormat("#define A \\\n" 5995 " void f() { int i; }", 5996 getLLVMStyleWithColumns(21)); 5997 verifyFormat("#define A \\\n" 5998 " void f() { \\\n" 5999 " int i; \\\n" 6000 " } \\\n" 6001 " int j;", 6002 getLLVMStyleWithColumns(22)); 6003 verifyFormat("#define A \\\n" 6004 " void f() { int i; } \\\n" 6005 " int j;", 6006 getLLVMStyleWithColumns(23)); 6007 } 6008 6009 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6010 FormatStyle MergeInlineOnly = getLLVMStyle(); 6011 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6012 verifyFormat("class C {\n" 6013 " int f() { return 42; }\n" 6014 "};", 6015 MergeInlineOnly); 6016 verifyFormat("int f() {\n" 6017 " return 42;\n" 6018 "}", 6019 MergeInlineOnly); 6020 } 6021 6022 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6023 // Elaborate type variable declarations. 6024 verifyFormat("struct foo a = {bar};\nint n;"); 6025 verifyFormat("class foo a = {bar};\nint n;"); 6026 verifyFormat("union foo a = {bar};\nint n;"); 6027 6028 // Elaborate types inside function definitions. 6029 verifyFormat("struct foo f() {}\nint n;"); 6030 verifyFormat("class foo f() {}\nint n;"); 6031 verifyFormat("union foo f() {}\nint n;"); 6032 6033 // Templates. 6034 verifyFormat("template <class X> void f() {}\nint n;"); 6035 verifyFormat("template <struct X> void f() {}\nint n;"); 6036 verifyFormat("template <union X> void f() {}\nint n;"); 6037 6038 // Actual definitions... 6039 verifyFormat("struct {\n} n;"); 6040 verifyFormat( 6041 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6042 verifyFormat("union Z {\n int n;\n} x;"); 6043 verifyFormat("class MACRO Z {\n} n;"); 6044 verifyFormat("class MACRO(X) Z {\n} n;"); 6045 verifyFormat("class __attribute__(X) Z {\n} n;"); 6046 verifyFormat("class __declspec(X) Z {\n} n;"); 6047 verifyFormat("class A##B##C {\n} n;"); 6048 verifyFormat("class alignas(16) Z {\n} n;"); 6049 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6050 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6051 6052 // Redefinition from nested context: 6053 verifyFormat("class A::B::C {\n} n;"); 6054 6055 // Template definitions. 6056 verifyFormat( 6057 "template <typename F>\n" 6058 "Matcher(const Matcher<F> &Other,\n" 6059 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6060 " !is_same<F, T>::value>::type * = 0)\n" 6061 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6062 6063 // FIXME: This is still incorrectly handled at the formatter side. 6064 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6065 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6066 6067 // FIXME: 6068 // This now gets parsed incorrectly as class definition. 6069 // verifyFormat("class A<int> f() {\n}\nint n;"); 6070 6071 // Elaborate types where incorrectly parsing the structural element would 6072 // break the indent. 6073 verifyFormat("if (true)\n" 6074 " class X x;\n" 6075 "else\n" 6076 " f();\n"); 6077 6078 // This is simply incomplete. Formatting is not important, but must not crash. 6079 verifyFormat("class A:"); 6080 } 6081 6082 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6083 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6084 format("#error Leave all white!!!!! space* alone!\n")); 6085 EXPECT_EQ( 6086 "#warning Leave all white!!!!! space* alone!\n", 6087 format("#warning Leave all white!!!!! space* alone!\n")); 6088 EXPECT_EQ("#error 1", format(" # error 1")); 6089 EXPECT_EQ("#warning 1", format(" # warning 1")); 6090 } 6091 6092 TEST_F(FormatTest, FormatHashIfExpressions) { 6093 verifyFormat("#if AAAA && BBBB"); 6094 verifyFormat("#if (AAAA && BBBB)"); 6095 verifyFormat("#elif (AAAA && BBBB)"); 6096 // FIXME: Come up with a better indentation for #elif. 6097 verifyFormat( 6098 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6099 " defined(BBBBBBBB)\n" 6100 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6101 " defined(BBBBBBBB)\n" 6102 "#endif", 6103 getLLVMStyleWithColumns(65)); 6104 } 6105 6106 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6107 FormatStyle AllowsMergedIf = getGoogleStyle(); 6108 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6109 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6110 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6111 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6112 EXPECT_EQ("if (true) return 42;", 6113 format("if (true)\nreturn 42;", AllowsMergedIf)); 6114 FormatStyle ShortMergedIf = AllowsMergedIf; 6115 ShortMergedIf.ColumnLimit = 25; 6116 verifyFormat("#define A \\\n" 6117 " if (true) return 42;", 6118 ShortMergedIf); 6119 verifyFormat("#define A \\\n" 6120 " f(); \\\n" 6121 " if (true)\n" 6122 "#define B", 6123 ShortMergedIf); 6124 verifyFormat("#define A \\\n" 6125 " f(); \\\n" 6126 " if (true)\n" 6127 "g();", 6128 ShortMergedIf); 6129 verifyFormat("{\n" 6130 "#ifdef A\n" 6131 " // Comment\n" 6132 " if (true) continue;\n" 6133 "#endif\n" 6134 " // Comment\n" 6135 " if (true) continue;\n" 6136 "}", 6137 ShortMergedIf); 6138 ShortMergedIf.ColumnLimit = 29; 6139 verifyFormat("#define A \\\n" 6140 " if (aaaaaaaaaa) return 1; \\\n" 6141 " return 2;", 6142 ShortMergedIf); 6143 ShortMergedIf.ColumnLimit = 28; 6144 verifyFormat("#define A \\\n" 6145 " if (aaaaaaaaaa) \\\n" 6146 " return 1; \\\n" 6147 " return 2;", 6148 ShortMergedIf); 6149 } 6150 6151 TEST_F(FormatTest, FormatStarDependingOnContext) { 6152 verifyFormat("void f(int *a);"); 6153 verifyFormat("void f() { f(fint * b); }"); 6154 verifyFormat("class A {\n void f(int *a);\n};"); 6155 verifyFormat("class A {\n int *a;\n};"); 6156 verifyFormat("namespace a {\n" 6157 "namespace b {\n" 6158 "class A {\n" 6159 " void f() {}\n" 6160 " int *a;\n" 6161 "};\n" 6162 "} // namespace b\n" 6163 "} // namespace a"); 6164 } 6165 6166 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 6167 verifyFormat("while"); 6168 verifyFormat("operator"); 6169 } 6170 6171 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 6172 // This code would be painfully slow to format if we didn't skip it. 6173 std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x 6174 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6175 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6176 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6177 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6178 "A(1, 1)\n" 6179 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 6180 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6181 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6182 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6183 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6184 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6185 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6186 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6187 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6188 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 6189 // Deeply nested part is untouched, rest is formatted. 6190 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 6191 format(std::string("int i;\n") + Code + "int j;\n", 6192 getLLVMStyle(), IC_ExpectIncomplete)); 6193 } 6194 6195 //===----------------------------------------------------------------------===// 6196 // Objective-C tests. 6197 //===----------------------------------------------------------------------===// 6198 6199 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 6200 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 6201 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 6202 format("-(NSUInteger)indexOfObject:(id)anObject;")); 6203 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 6204 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 6205 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 6206 format("-(NSInteger)Method3:(id)anObject;")); 6207 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 6208 format("-(NSInteger)Method4:(id)anObject;")); 6209 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 6210 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 6211 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 6212 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 6213 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6214 "forAllCells:(BOOL)flag;", 6215 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6216 "forAllCells:(BOOL)flag;")); 6217 6218 // Very long objectiveC method declaration. 6219 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 6220 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 6221 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 6222 " inRange:(NSRange)range\n" 6223 " outRange:(NSRange)out_range\n" 6224 " outRange1:(NSRange)out_range1\n" 6225 " outRange2:(NSRange)out_range2\n" 6226 " outRange3:(NSRange)out_range3\n" 6227 " outRange4:(NSRange)out_range4\n" 6228 " outRange5:(NSRange)out_range5\n" 6229 " outRange6:(NSRange)out_range6\n" 6230 " outRange7:(NSRange)out_range7\n" 6231 " outRange8:(NSRange)out_range8\n" 6232 " outRange9:(NSRange)out_range9;"); 6233 6234 // When the function name has to be wrapped. 6235 FormatStyle Style = getLLVMStyle(); 6236 Style.IndentWrappedFunctionNames = false; 6237 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6238 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6239 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6240 "}", 6241 Style); 6242 Style.IndentWrappedFunctionNames = true; 6243 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6244 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6245 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6246 "}", 6247 Style); 6248 6249 verifyFormat("- (int)sum:(vector<int>)numbers;"); 6250 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 6251 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 6252 // protocol lists (but not for template classes): 6253 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 6254 6255 verifyFormat("- (int (*)())foo:(int (*)())f;"); 6256 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 6257 6258 // If there's no return type (very rare in practice!), LLVM and Google style 6259 // agree. 6260 verifyFormat("- foo;"); 6261 verifyFormat("- foo:(int)f;"); 6262 verifyGoogleFormat("- foo:(int)foo;"); 6263 } 6264 6265 6266 TEST_F(FormatTest, BreaksStringLiterals) { 6267 EXPECT_EQ("\"some text \"\n" 6268 "\"other\";", 6269 format("\"some text other\";", getLLVMStyleWithColumns(12))); 6270 EXPECT_EQ("\"some text \"\n" 6271 "\"other\";", 6272 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 6273 EXPECT_EQ( 6274 "#define A \\\n" 6275 " \"some \" \\\n" 6276 " \"text \" \\\n" 6277 " \"other\";", 6278 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 6279 EXPECT_EQ( 6280 "#define A \\\n" 6281 " \"so \" \\\n" 6282 " \"text \" \\\n" 6283 " \"other\";", 6284 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 6285 6286 EXPECT_EQ("\"some text\"", 6287 format("\"some text\"", getLLVMStyleWithColumns(1))); 6288 EXPECT_EQ("\"some text\"", 6289 format("\"some text\"", getLLVMStyleWithColumns(11))); 6290 EXPECT_EQ("\"some \"\n" 6291 "\"text\"", 6292 format("\"some text\"", getLLVMStyleWithColumns(10))); 6293 EXPECT_EQ("\"some \"\n" 6294 "\"text\"", 6295 format("\"some text\"", getLLVMStyleWithColumns(7))); 6296 EXPECT_EQ("\"some\"\n" 6297 "\" tex\"\n" 6298 "\"t\"", 6299 format("\"some text\"", getLLVMStyleWithColumns(6))); 6300 EXPECT_EQ("\"some\"\n" 6301 "\" tex\"\n" 6302 "\" and\"", 6303 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 6304 EXPECT_EQ("\"some\"\n" 6305 "\"/tex\"\n" 6306 "\"/and\"", 6307 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 6308 6309 EXPECT_EQ("variable =\n" 6310 " \"long string \"\n" 6311 " \"literal\";", 6312 format("variable = \"long string literal\";", 6313 getLLVMStyleWithColumns(20))); 6314 6315 EXPECT_EQ("variable = f(\n" 6316 " \"long string \"\n" 6317 " \"literal\",\n" 6318 " short,\n" 6319 " loooooooooooooooooooong);", 6320 format("variable = f(\"long string literal\", short, " 6321 "loooooooooooooooooooong);", 6322 getLLVMStyleWithColumns(20))); 6323 6324 EXPECT_EQ( 6325 "f(g(\"long string \"\n" 6326 " \"literal\"),\n" 6327 " b);", 6328 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 6329 EXPECT_EQ("f(g(\"long string \"\n" 6330 " \"literal\",\n" 6331 " a),\n" 6332 " b);", 6333 format("f(g(\"long string literal\", a), b);", 6334 getLLVMStyleWithColumns(20))); 6335 EXPECT_EQ( 6336 "f(\"one two\".split(\n" 6337 " variable));", 6338 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 6339 EXPECT_EQ("f(\"one two three four five six \"\n" 6340 " \"seven\".split(\n" 6341 " really_looooong_variable));", 6342 format("f(\"one two three four five six seven\"." 6343 "split(really_looooong_variable));", 6344 getLLVMStyleWithColumns(33))); 6345 6346 EXPECT_EQ("f(\"some \"\n" 6347 " \"text\",\n" 6348 " other);", 6349 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 6350 6351 // Only break as a last resort. 6352 verifyFormat( 6353 "aaaaaaaaaaaaaaaaaaaa(\n" 6354 " aaaaaaaaaaaaaaaaaaaa,\n" 6355 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 6356 6357 EXPECT_EQ("\"splitmea\"\n" 6358 "\"trandomp\"\n" 6359 "\"oint\"", 6360 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 6361 6362 EXPECT_EQ("\"split/\"\n" 6363 "\"pathat/\"\n" 6364 "\"slashes\"", 6365 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 6366 6367 EXPECT_EQ("\"split/\"\n" 6368 "\"pathat/\"\n" 6369 "\"slashes\"", 6370 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 6371 EXPECT_EQ("\"split at \"\n" 6372 "\"spaces/at/\"\n" 6373 "\"slashes.at.any$\"\n" 6374 "\"non-alphanumeric%\"\n" 6375 "\"1111111111characte\"\n" 6376 "\"rs\"", 6377 format("\"split at " 6378 "spaces/at/" 6379 "slashes.at." 6380 "any$non-" 6381 "alphanumeric%" 6382 "1111111111characte" 6383 "rs\"", 6384 getLLVMStyleWithColumns(20))); 6385 6386 // Verify that splitting the strings understands 6387 // Style::AlwaysBreakBeforeMultilineStrings. 6388 EXPECT_EQ( 6389 "aaaaaaaaaaaa(\n" 6390 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 6391 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 6392 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 6393 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 6394 "aaaaaaaaaaaaaaaaaaaaaa\");", 6395 getGoogleStyle())); 6396 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 6397 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 6398 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 6399 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 6400 "aaaaaaaaaaaaaaaaaaaaaa\";", 6401 getGoogleStyle())); 6402 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 6403 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 6404 format("llvm::outs() << " 6405 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 6406 "aaaaaaaaaaaaaaaaaaa\";")); 6407 EXPECT_EQ("ffff(\n" 6408 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 6409 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 6410 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 6411 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 6412 getGoogleStyle())); 6413 6414 FormatStyle Style = getLLVMStyleWithColumns(12); 6415 Style.BreakStringLiterals = false; 6416 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 6417 6418 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 6419 AlignLeft.AlignEscapedNewlinesLeft = true; 6420 EXPECT_EQ("#define A \\\n" 6421 " \"some \" \\\n" 6422 " \"text \" \\\n" 6423 " \"other\";", 6424 format("#define A \"some text other\";", AlignLeft)); 6425 } 6426 6427 TEST_F(FormatTest, FullyRemoveEmptyLines) { 6428 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 6429 NoEmptyLines.MaxEmptyLinesToKeep = 0; 6430 EXPECT_EQ("int i = a(b());", 6431 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 6432 } 6433 6434 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 6435 EXPECT_EQ( 6436 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 6437 "(\n" 6438 " \"x\t\");", 6439 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 6440 "aaaaaaa(" 6441 "\"x\t\");")); 6442 } 6443 6444 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 6445 EXPECT_EQ( 6446 "u8\"utf8 string \"\n" 6447 "u8\"literal\";", 6448 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 6449 EXPECT_EQ( 6450 "u\"utf16 string \"\n" 6451 "u\"literal\";", 6452 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 6453 EXPECT_EQ( 6454 "U\"utf32 string \"\n" 6455 "U\"literal\";", 6456 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 6457 EXPECT_EQ("L\"wide string \"\n" 6458 "L\"literal\";", 6459 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 6460 EXPECT_EQ("@\"NSString \"\n" 6461 "@\"literal\";", 6462 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 6463 6464 // This input makes clang-format try to split the incomplete unicode escape 6465 // sequence, which used to lead to a crasher. 6466 verifyNoCrash( 6467 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 6468 getLLVMStyleWithColumns(60)); 6469 } 6470 6471 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 6472 FormatStyle Style = getGoogleStyleWithColumns(15); 6473 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 6474 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 6475 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 6476 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 6477 EXPECT_EQ("u8R\"x(raw literal)x\";", 6478 format("u8R\"x(raw literal)x\";", Style)); 6479 } 6480 6481 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 6482 FormatStyle Style = getLLVMStyleWithColumns(20); 6483 EXPECT_EQ( 6484 "_T(\"aaaaaaaaaaaaaa\")\n" 6485 "_T(\"aaaaaaaaaaaaaa\")\n" 6486 "_T(\"aaaaaaaaaaaa\")", 6487 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 6488 EXPECT_EQ("f(x,\n" 6489 " _T(\"aaaaaaaaaaaa\")\n" 6490 " _T(\"aaa\"),\n" 6491 " z);", 6492 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 6493 6494 // FIXME: Handle embedded spaces in one iteration. 6495 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 6496 // "_T(\"aaaaaaaaaaaaa\")\n" 6497 // "_T(\"aaaaaaaaaaaaa\")\n" 6498 // "_T(\"a\")", 6499 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 6500 // getLLVMStyleWithColumns(20))); 6501 EXPECT_EQ( 6502 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 6503 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 6504 EXPECT_EQ("f(\n" 6505 "#if !TEST\n" 6506 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 6507 "#endif\n" 6508 " );", 6509 format("f(\n" 6510 "#if !TEST\n" 6511 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 6512 "#endif\n" 6513 ");")); 6514 EXPECT_EQ("f(\n" 6515 "\n" 6516 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 6517 format("f(\n" 6518 "\n" 6519 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 6520 } 6521 6522 TEST_F(FormatTest, BreaksStringLiteralOperands) { 6523 // In a function call with two operands, the second can be broken with no line 6524 // break before it. 6525 EXPECT_EQ("func(a, \"long long \"\n" 6526 " \"long long\");", 6527 format("func(a, \"long long long long\");", 6528 getLLVMStyleWithColumns(24))); 6529 // In a function call with three operands, the second must be broken with a 6530 // line break before it. 6531 EXPECT_EQ("func(a,\n" 6532 " \"long long long \"\n" 6533 " \"long\",\n" 6534 " c);", 6535 format("func(a, \"long long long long\", c);", 6536 getLLVMStyleWithColumns(24))); 6537 // In a function call with three operands, the third must be broken with a 6538 // line break before it. 6539 EXPECT_EQ("func(a, b,\n" 6540 " \"long long long \"\n" 6541 " \"long\");", 6542 format("func(a, b, \"long long long long\");", 6543 getLLVMStyleWithColumns(24))); 6544 // In a function call with three operands, both the second and the third must 6545 // be broken with a line break before them. 6546 EXPECT_EQ("func(a,\n" 6547 " \"long long long \"\n" 6548 " \"long\",\n" 6549 " \"long long long \"\n" 6550 " \"long\");", 6551 format("func(a, \"long long long long\", \"long long long long\");", 6552 getLLVMStyleWithColumns(24))); 6553 // In a chain of << with two operands, the second can be broken with no line 6554 // break before it. 6555 EXPECT_EQ("a << \"line line \"\n" 6556 " \"line\";", 6557 format("a << \"line line line\";", 6558 getLLVMStyleWithColumns(20))); 6559 // In a chain of << with three operands, the second can be broken with no line 6560 // break before it. 6561 EXPECT_EQ("abcde << \"line \"\n" 6562 " \"line line\"\n" 6563 " << c;", 6564 format("abcde << \"line line line\" << c;", 6565 getLLVMStyleWithColumns(20))); 6566 // In a chain of << with three operands, the third must be broken with a line 6567 // break before it. 6568 EXPECT_EQ("a << b\n" 6569 " << \"line line \"\n" 6570 " \"line\";", 6571 format("a << b << \"line line line\";", 6572 getLLVMStyleWithColumns(20))); 6573 // In a chain of << with three operands, the second can be broken with no line 6574 // break before it and the third must be broken with a line break before it. 6575 EXPECT_EQ("abcd << \"line line \"\n" 6576 " \"line\"\n" 6577 " << \"line line \"\n" 6578 " \"line\";", 6579 format("abcd << \"line line line\" << \"line line line\";", 6580 getLLVMStyleWithColumns(20))); 6581 // In a chain of binary operators with two operands, the second can be broken 6582 // with no line break before it. 6583 EXPECT_EQ("abcd + \"line line \"\n" 6584 " \"line line\";", 6585 format("abcd + \"line line line line\";", 6586 getLLVMStyleWithColumns(20))); 6587 // In a chain of binary operators with three operands, the second must be 6588 // broken with a line break before it. 6589 EXPECT_EQ("abcd +\n" 6590 " \"line line \"\n" 6591 " \"line line\" +\n" 6592 " e;", 6593 format("abcd + \"line line line line\" + e;", 6594 getLLVMStyleWithColumns(20))); 6595 // In a function call with two operands, with AlignAfterOpenBracket enabled, 6596 // the first must be broken with a line break before it. 6597 FormatStyle Style = getLLVMStyleWithColumns(25); 6598 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6599 EXPECT_EQ("someFunction(\n" 6600 " \"long long long \"\n" 6601 " \"long\",\n" 6602 " a);", 6603 format("someFunction(\"long long long long\", a);", Style)); 6604 } 6605 6606 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 6607 EXPECT_EQ( 6608 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 6609 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 6610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 6611 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 6612 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 6613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 6614 } 6615 6616 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 6617 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 6618 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 6619 EXPECT_EQ("fffffffffff(g(R\"x(\n" 6620 "multiline raw string literal xxxxxxxxxxxxxx\n" 6621 ")x\",\n" 6622 " a),\n" 6623 " b);", 6624 format("fffffffffff(g(R\"x(\n" 6625 "multiline raw string literal xxxxxxxxxxxxxx\n" 6626 ")x\", a), b);", 6627 getGoogleStyleWithColumns(20))); 6628 EXPECT_EQ("fffffffffff(\n" 6629 " g(R\"x(qqq\n" 6630 "multiline raw string literal xxxxxxxxxxxxxx\n" 6631 ")x\",\n" 6632 " a),\n" 6633 " b);", 6634 format("fffffffffff(g(R\"x(qqq\n" 6635 "multiline raw string literal xxxxxxxxxxxxxx\n" 6636 ")x\", a), b);", 6637 getGoogleStyleWithColumns(20))); 6638 6639 EXPECT_EQ("fffffffffff(R\"x(\n" 6640 "multiline raw string literal xxxxxxxxxxxxxx\n" 6641 ")x\");", 6642 format("fffffffffff(R\"x(\n" 6643 "multiline raw string literal xxxxxxxxxxxxxx\n" 6644 ")x\");", 6645 getGoogleStyleWithColumns(20))); 6646 EXPECT_EQ("fffffffffff(R\"x(\n" 6647 "multiline raw string literal xxxxxxxxxxxxxx\n" 6648 ")x\" + bbbbbb);", 6649 format("fffffffffff(R\"x(\n" 6650 "multiline raw string literal xxxxxxxxxxxxxx\n" 6651 ")x\" + bbbbbb);", 6652 getGoogleStyleWithColumns(20))); 6653 EXPECT_EQ("fffffffffff(\n" 6654 " R\"x(\n" 6655 "multiline raw string literal xxxxxxxxxxxxxx\n" 6656 ")x\" +\n" 6657 " bbbbbb);", 6658 format("fffffffffff(\n" 6659 " R\"x(\n" 6660 "multiline raw string literal xxxxxxxxxxxxxx\n" 6661 ")x\" + bbbbbb);", 6662 getGoogleStyleWithColumns(20))); 6663 } 6664 6665 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 6666 verifyFormat("string a = \"unterminated;"); 6667 EXPECT_EQ("function(\"unterminated,\n" 6668 " OtherParameter);", 6669 format("function( \"unterminated,\n" 6670 " OtherParameter);")); 6671 } 6672 6673 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 6674 FormatStyle Style = getLLVMStyle(); 6675 Style.Standard = FormatStyle::LS_Cpp03; 6676 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 6677 format("#define x(_a) printf(\"foo\"_a);", Style)); 6678 } 6679 6680 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 6681 6682 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 6683 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 6684 " \"ddeeefff\");", 6685 format("someFunction(\"aaabbbcccdddeeefff\");", 6686 getLLVMStyleWithColumns(25))); 6687 EXPECT_EQ("someFunction1234567890(\n" 6688 " \"aaabbbcccdddeeefff\");", 6689 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 6690 getLLVMStyleWithColumns(26))); 6691 EXPECT_EQ("someFunction1234567890(\n" 6692 " \"aaabbbcccdddeeeff\"\n" 6693 " \"f\");", 6694 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 6695 getLLVMStyleWithColumns(25))); 6696 EXPECT_EQ("someFunction1234567890(\n" 6697 " \"aaabbbcccdddeeeff\"\n" 6698 " \"f\");", 6699 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 6700 getLLVMStyleWithColumns(24))); 6701 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 6702 " \"ddde \"\n" 6703 " \"efff\");", 6704 format("someFunction(\"aaabbbcc ddde efff\");", 6705 getLLVMStyleWithColumns(25))); 6706 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 6707 " \"ddeeefff\");", 6708 format("someFunction(\"aaabbbccc ddeeefff\");", 6709 getLLVMStyleWithColumns(25))); 6710 EXPECT_EQ("someFunction1234567890(\n" 6711 " \"aaabb \"\n" 6712 " \"cccdddeeefff\");", 6713 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 6714 getLLVMStyleWithColumns(25))); 6715 EXPECT_EQ("#define A \\\n" 6716 " string s = \\\n" 6717 " \"123456789\" \\\n" 6718 " \"0\"; \\\n" 6719 " int i;", 6720 format("#define A string s = \"1234567890\"; int i;", 6721 getLLVMStyleWithColumns(20))); 6722 // FIXME: Put additional penalties on breaking at non-whitespace locations. 6723 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 6724 " \"dddeeeff\"\n" 6725 " \"f\");", 6726 format("someFunction(\"aaabbbcc dddeeefff\");", 6727 getLLVMStyleWithColumns(25))); 6728 } 6729 6730 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 6731 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 6732 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 6733 EXPECT_EQ("\"test\"\n" 6734 "\"\\n\"", 6735 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 6736 EXPECT_EQ("\"tes\\\\\"\n" 6737 "\"n\"", 6738 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 6739 EXPECT_EQ("\"\\\\\\\\\"\n" 6740 "\"\\n\"", 6741 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 6742 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 6743 EXPECT_EQ("\"\\uff01\"\n" 6744 "\"test\"", 6745 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 6746 EXPECT_EQ("\"\\Uff01ff02\"", 6747 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 6748 EXPECT_EQ("\"\\x000000000001\"\n" 6749 "\"next\"", 6750 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 6751 EXPECT_EQ("\"\\x000000000001next\"", 6752 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 6753 EXPECT_EQ("\"\\x000000000001\"", 6754 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 6755 EXPECT_EQ("\"test\"\n" 6756 "\"\\000000\"\n" 6757 "\"000001\"", 6758 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 6759 EXPECT_EQ("\"test\\000\"\n" 6760 "\"00000000\"\n" 6761 "\"1\"", 6762 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 6763 } 6764 6765 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 6766 verifyFormat("void f() {\n" 6767 " return g() {}\n" 6768 " void h() {}"); 6769 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 6770 "g();\n" 6771 "}"); 6772 } 6773 6774 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 6775 verifyFormat( 6776 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 6777 } 6778 6779 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 6780 verifyFormat("class X {\n" 6781 " void f() {\n" 6782 " }\n" 6783 "};", 6784 getLLVMStyleWithColumns(12)); 6785 } 6786 6787 TEST_F(FormatTest, ConfigurableIndentWidth) { 6788 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 6789 EightIndent.IndentWidth = 8; 6790 EightIndent.ContinuationIndentWidth = 8; 6791 verifyFormat("void f() {\n" 6792 " someFunction();\n" 6793 " if (true) {\n" 6794 " f();\n" 6795 " }\n" 6796 "}", 6797 EightIndent); 6798 verifyFormat("class X {\n" 6799 " void f() {\n" 6800 " }\n" 6801 "};", 6802 EightIndent); 6803 verifyFormat("int x[] = {\n" 6804 " call(),\n" 6805 " call()};", 6806 EightIndent); 6807 } 6808 6809 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 6810 verifyFormat("double\n" 6811 "f();", 6812 getLLVMStyleWithColumns(8)); 6813 } 6814 6815 TEST_F(FormatTest, ConfigurableUseOfTab) { 6816 FormatStyle Tab = getLLVMStyleWithColumns(42); 6817 Tab.IndentWidth = 8; 6818 Tab.UseTab = FormatStyle::UT_Always; 6819 Tab.AlignEscapedNewlinesLeft = true; 6820 6821 EXPECT_EQ("if (aaaaaaaa && // q\n" 6822 " bb)\t\t// w\n" 6823 "\t;", 6824 format("if (aaaaaaaa &&// q\n" 6825 "bb)// w\n" 6826 ";", 6827 Tab)); 6828 EXPECT_EQ("if (aaa && bbb) // w\n" 6829 "\t;", 6830 format("if(aaa&&bbb)// w\n" 6831 ";", 6832 Tab)); 6833 6834 verifyFormat("class X {\n" 6835 "\tvoid f() {\n" 6836 "\t\tsomeFunction(parameter1,\n" 6837 "\t\t\t parameter2);\n" 6838 "\t}\n" 6839 "};", 6840 Tab); 6841 verifyFormat("#define A \\\n" 6842 "\tvoid f() { \\\n" 6843 "\t\tsomeFunction( \\\n" 6844 "\t\t parameter1, \\\n" 6845 "\t\t parameter2); \\\n" 6846 "\t}", 6847 Tab); 6848 6849 Tab.TabWidth = 4; 6850 Tab.IndentWidth = 8; 6851 verifyFormat("class TabWidth4Indent8 {\n" 6852 "\t\tvoid f() {\n" 6853 "\t\t\t\tsomeFunction(parameter1,\n" 6854 "\t\t\t\t\t\t\t parameter2);\n" 6855 "\t\t}\n" 6856 "};", 6857 Tab); 6858 6859 Tab.TabWidth = 4; 6860 Tab.IndentWidth = 4; 6861 verifyFormat("class TabWidth4Indent4 {\n" 6862 "\tvoid f() {\n" 6863 "\t\tsomeFunction(parameter1,\n" 6864 "\t\t\t\t\t parameter2);\n" 6865 "\t}\n" 6866 "};", 6867 Tab); 6868 6869 Tab.TabWidth = 8; 6870 Tab.IndentWidth = 4; 6871 verifyFormat("class TabWidth8Indent4 {\n" 6872 " void f() {\n" 6873 "\tsomeFunction(parameter1,\n" 6874 "\t\t parameter2);\n" 6875 " }\n" 6876 "};", 6877 Tab); 6878 6879 Tab.TabWidth = 8; 6880 Tab.IndentWidth = 8; 6881 EXPECT_EQ("/*\n" 6882 "\t a\t\tcomment\n" 6883 "\t in multiple lines\n" 6884 " */", 6885 format(" /*\t \t \n" 6886 " \t \t a\t\tcomment\t \t\n" 6887 " \t \t in multiple lines\t\n" 6888 " \t */", 6889 Tab)); 6890 6891 Tab.UseTab = FormatStyle::UT_ForIndentation; 6892 verifyFormat("{\n" 6893 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6894 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6895 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6896 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6897 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6898 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 6899 "};", 6900 Tab); 6901 verifyFormat("enum AA {\n" 6902 "\ta1, // Force multiple lines\n" 6903 "\ta2,\n" 6904 "\ta3\n" 6905 "};", 6906 Tab); 6907 EXPECT_EQ("if (aaaaaaaa && // q\n" 6908 " bb) // w\n" 6909 "\t;", 6910 format("if (aaaaaaaa &&// q\n" 6911 "bb)// w\n" 6912 ";", 6913 Tab)); 6914 verifyFormat("class X {\n" 6915 "\tvoid f() {\n" 6916 "\t\tsomeFunction(parameter1,\n" 6917 "\t\t parameter2);\n" 6918 "\t}\n" 6919 "};", 6920 Tab); 6921 verifyFormat("{\n" 6922 "\tQ(\n" 6923 "\t {\n" 6924 "\t\t int a;\n" 6925 "\t\t someFunction(aaaaaaaa,\n" 6926 "\t\t bbbbbbb);\n" 6927 "\t },\n" 6928 "\t p);\n" 6929 "}", 6930 Tab); 6931 EXPECT_EQ("{\n" 6932 "\t/* aaaa\n" 6933 "\t bbbb */\n" 6934 "}", 6935 format("{\n" 6936 "/* aaaa\n" 6937 " bbbb */\n" 6938 "}", 6939 Tab)); 6940 EXPECT_EQ("{\n" 6941 "\t/*\n" 6942 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6943 "\t bbbbbbbbbbbbb\n" 6944 "\t*/\n" 6945 "}", 6946 format("{\n" 6947 "/*\n" 6948 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 6949 "*/\n" 6950 "}", 6951 Tab)); 6952 EXPECT_EQ("{\n" 6953 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6954 "\t// bbbbbbbbbbbbb\n" 6955 "}", 6956 format("{\n" 6957 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 6958 "}", 6959 Tab)); 6960 EXPECT_EQ("{\n" 6961 "\t/*\n" 6962 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6963 "\t bbbbbbbbbbbbb\n" 6964 "\t*/\n" 6965 "}", 6966 format("{\n" 6967 "\t/*\n" 6968 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 6969 "\t*/\n" 6970 "}", 6971 Tab)); 6972 EXPECT_EQ("{\n" 6973 "\t/*\n" 6974 "\n" 6975 "\t*/\n" 6976 "}", 6977 format("{\n" 6978 "\t/*\n" 6979 "\n" 6980 "\t*/\n" 6981 "}", 6982 Tab)); 6983 EXPECT_EQ("{\n" 6984 "\t/*\n" 6985 " asdf\n" 6986 "\t*/\n" 6987 "}", 6988 format("{\n" 6989 "\t/*\n" 6990 " asdf\n" 6991 "\t*/\n" 6992 "}", 6993 Tab)); 6994 6995 Tab.UseTab = FormatStyle::UT_Never; 6996 EXPECT_EQ("/*\n" 6997 " a\t\tcomment\n" 6998 " in multiple lines\n" 6999 " */", 7000 format(" /*\t \t \n" 7001 " \t \t a\t\tcomment\t \t\n" 7002 " \t \t in multiple lines\t\n" 7003 " \t */", 7004 Tab)); 7005 EXPECT_EQ("/* some\n" 7006 " comment */", 7007 format(" \t \t /* some\n" 7008 " \t \t comment */", 7009 Tab)); 7010 EXPECT_EQ("int a; /* some\n" 7011 " comment */", 7012 format(" \t \t int a; /* some\n" 7013 " \t \t comment */", 7014 Tab)); 7015 7016 EXPECT_EQ("int a; /* some\n" 7017 "comment */", 7018 format(" \t \t int\ta; /* some\n" 7019 " \t \t comment */", 7020 Tab)); 7021 EXPECT_EQ("f(\"\t\t\"); /* some\n" 7022 " comment */", 7023 format(" \t \t f(\"\t\t\"); /* some\n" 7024 " \t \t comment */", 7025 Tab)); 7026 EXPECT_EQ("{\n" 7027 " /*\n" 7028 " * Comment\n" 7029 " */\n" 7030 " int i;\n" 7031 "}", 7032 format("{\n" 7033 "\t/*\n" 7034 "\t * Comment\n" 7035 "\t */\n" 7036 "\t int i;\n" 7037 "}")); 7038 7039 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 7040 Tab.TabWidth = 8; 7041 Tab.IndentWidth = 8; 7042 EXPECT_EQ("if (aaaaaaaa && // q\n" 7043 " bb) // w\n" 7044 "\t;", 7045 format("if (aaaaaaaa &&// q\n" 7046 "bb)// w\n" 7047 ";", 7048 Tab)); 7049 EXPECT_EQ("if (aaa && bbb) // w\n" 7050 "\t;", 7051 format("if(aaa&&bbb)// w\n" 7052 ";", 7053 Tab)); 7054 verifyFormat("class X {\n" 7055 "\tvoid f() {\n" 7056 "\t\tsomeFunction(parameter1,\n" 7057 "\t\t\t parameter2);\n" 7058 "\t}\n" 7059 "};", 7060 Tab); 7061 verifyFormat("#define A \\\n" 7062 "\tvoid f() { \\\n" 7063 "\t\tsomeFunction( \\\n" 7064 "\t\t parameter1, \\\n" 7065 "\t\t parameter2); \\\n" 7066 "\t}", 7067 Tab); 7068 Tab.TabWidth = 4; 7069 Tab.IndentWidth = 8; 7070 verifyFormat("class TabWidth4Indent8 {\n" 7071 "\t\tvoid f() {\n" 7072 "\t\t\t\tsomeFunction(parameter1,\n" 7073 "\t\t\t\t\t\t\t parameter2);\n" 7074 "\t\t}\n" 7075 "};", 7076 Tab); 7077 Tab.TabWidth = 4; 7078 Tab.IndentWidth = 4; 7079 verifyFormat("class TabWidth4Indent4 {\n" 7080 "\tvoid f() {\n" 7081 "\t\tsomeFunction(parameter1,\n" 7082 "\t\t\t\t\t parameter2);\n" 7083 "\t}\n" 7084 "};", 7085 Tab); 7086 Tab.TabWidth = 8; 7087 Tab.IndentWidth = 4; 7088 verifyFormat("class TabWidth8Indent4 {\n" 7089 " void f() {\n" 7090 "\tsomeFunction(parameter1,\n" 7091 "\t\t parameter2);\n" 7092 " }\n" 7093 "};", 7094 Tab); 7095 Tab.TabWidth = 8; 7096 Tab.IndentWidth = 8; 7097 EXPECT_EQ("/*\n" 7098 "\t a\t\tcomment\n" 7099 "\t in multiple lines\n" 7100 " */", 7101 format(" /*\t \t \n" 7102 " \t \t a\t\tcomment\t \t\n" 7103 " \t \t in multiple lines\t\n" 7104 " \t */", 7105 Tab)); 7106 verifyFormat("{\n" 7107 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7108 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7109 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7110 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7111 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7112 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7113 "};", 7114 Tab); 7115 verifyFormat("enum AA {\n" 7116 "\ta1, // Force multiple lines\n" 7117 "\ta2,\n" 7118 "\ta3\n" 7119 "};", 7120 Tab); 7121 EXPECT_EQ("if (aaaaaaaa && // q\n" 7122 " bb) // w\n" 7123 "\t;", 7124 format("if (aaaaaaaa &&// q\n" 7125 "bb)// w\n" 7126 ";", 7127 Tab)); 7128 verifyFormat("class X {\n" 7129 "\tvoid f() {\n" 7130 "\t\tsomeFunction(parameter1,\n" 7131 "\t\t\t parameter2);\n" 7132 "\t}\n" 7133 "};", 7134 Tab); 7135 verifyFormat("{\n" 7136 "\tQ(\n" 7137 "\t {\n" 7138 "\t\t int a;\n" 7139 "\t\t someFunction(aaaaaaaa,\n" 7140 "\t\t\t\t bbbbbbb);\n" 7141 "\t },\n" 7142 "\t p);\n" 7143 "}", 7144 Tab); 7145 EXPECT_EQ("{\n" 7146 "\t/* aaaa\n" 7147 "\t bbbb */\n" 7148 "}", 7149 format("{\n" 7150 "/* aaaa\n" 7151 " bbbb */\n" 7152 "}", 7153 Tab)); 7154 EXPECT_EQ("{\n" 7155 "\t/*\n" 7156 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7157 "\t bbbbbbbbbbbbb\n" 7158 "\t*/\n" 7159 "}", 7160 format("{\n" 7161 "/*\n" 7162 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7163 "*/\n" 7164 "}", 7165 Tab)); 7166 EXPECT_EQ("{\n" 7167 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7168 "\t// bbbbbbbbbbbbb\n" 7169 "}", 7170 format("{\n" 7171 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7172 "}", 7173 Tab)); 7174 EXPECT_EQ("{\n" 7175 "\t/*\n" 7176 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7177 "\t bbbbbbbbbbbbb\n" 7178 "\t*/\n" 7179 "}", 7180 format("{\n" 7181 "\t/*\n" 7182 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7183 "\t*/\n" 7184 "}", 7185 Tab)); 7186 EXPECT_EQ("{\n" 7187 "\t/*\n" 7188 "\n" 7189 "\t*/\n" 7190 "}", 7191 format("{\n" 7192 "\t/*\n" 7193 "\n" 7194 "\t*/\n" 7195 "}", 7196 Tab)); 7197 EXPECT_EQ("{\n" 7198 "\t/*\n" 7199 " asdf\n" 7200 "\t*/\n" 7201 "}", 7202 format("{\n" 7203 "\t/*\n" 7204 " asdf\n" 7205 "\t*/\n" 7206 "}", 7207 Tab)); 7208 EXPECT_EQ("/*\n" 7209 "\t a\t\tcomment\n" 7210 "\t in multiple lines\n" 7211 " */", 7212 format(" /*\t \t \n" 7213 " \t \t a\t\tcomment\t \t\n" 7214 " \t \t in multiple lines\t\n" 7215 " \t */", 7216 Tab)); 7217 EXPECT_EQ("/* some\n" 7218 " comment */", 7219 format(" \t \t /* some\n" 7220 " \t \t comment */", 7221 Tab)); 7222 EXPECT_EQ("int a; /* some\n" 7223 " comment */", 7224 format(" \t \t int a; /* some\n" 7225 " \t \t comment */", 7226 Tab)); 7227 EXPECT_EQ("int a; /* some\n" 7228 "comment */", 7229 format(" \t \t int\ta; /* some\n" 7230 " \t \t comment */", 7231 Tab)); 7232 EXPECT_EQ("f(\"\t\t\"); /* some\n" 7233 " comment */", 7234 format(" \t \t f(\"\t\t\"); /* some\n" 7235 " \t \t comment */", 7236 Tab)); 7237 EXPECT_EQ("{\n" 7238 " /*\n" 7239 " * Comment\n" 7240 " */\n" 7241 " int i;\n" 7242 "}", 7243 format("{\n" 7244 "\t/*\n" 7245 "\t * Comment\n" 7246 "\t */\n" 7247 "\t int i;\n" 7248 "}")); 7249 Tab.AlignConsecutiveAssignments = true; 7250 Tab.AlignConsecutiveDeclarations = true; 7251 Tab.TabWidth = 4; 7252 Tab.IndentWidth = 4; 7253 verifyFormat("class Assign {\n" 7254 "\tvoid f() {\n" 7255 "\t\tint x = 123;\n" 7256 "\t\tint random = 4;\n" 7257 "\t\tstd::string alphabet =\n" 7258 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 7259 "\t}\n" 7260 "};", 7261 Tab); 7262 } 7263 7264 TEST_F(FormatTest, CalculatesOriginalColumn) { 7265 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7266 "q\"; /* some\n" 7267 " comment */", 7268 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7269 "q\"; /* some\n" 7270 " comment */", 7271 getLLVMStyle())); 7272 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 7273 "/* some\n" 7274 " comment */", 7275 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 7276 " /* some\n" 7277 " comment */", 7278 getLLVMStyle())); 7279 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7280 "qqq\n" 7281 "/* some\n" 7282 " comment */", 7283 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7284 "qqq\n" 7285 " /* some\n" 7286 " comment */", 7287 getLLVMStyle())); 7288 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7289 "wwww; /* some\n" 7290 " comment */", 7291 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7292 "wwww; /* some\n" 7293 " comment */", 7294 getLLVMStyle())); 7295 } 7296 7297 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 7298 FormatStyle NoSpace = getLLVMStyle(); 7299 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 7300 7301 verifyFormat("while(true)\n" 7302 " continue;", 7303 NoSpace); 7304 verifyFormat("for(;;)\n" 7305 " continue;", 7306 NoSpace); 7307 verifyFormat("if(true)\n" 7308 " f();\n" 7309 "else if(true)\n" 7310 " f();", 7311 NoSpace); 7312 verifyFormat("do {\n" 7313 " do_something();\n" 7314 "} while(something());", 7315 NoSpace); 7316 verifyFormat("switch(x) {\n" 7317 "default:\n" 7318 " break;\n" 7319 "}", 7320 NoSpace); 7321 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 7322 verifyFormat("size_t x = sizeof(x);", NoSpace); 7323 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 7324 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 7325 verifyFormat("alignas(128) char a[128];", NoSpace); 7326 verifyFormat("size_t x = alignof(MyType);", NoSpace); 7327 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 7328 verifyFormat("int f() throw(Deprecated);", NoSpace); 7329 verifyFormat("typedef void (*cb)(int);", NoSpace); 7330 verifyFormat("T A::operator()();", NoSpace); 7331 verifyFormat("X A::operator++(T);", NoSpace); 7332 7333 FormatStyle Space = getLLVMStyle(); 7334 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 7335 7336 verifyFormat("int f ();", Space); 7337 verifyFormat("void f (int a, T b) {\n" 7338 " while (true)\n" 7339 " continue;\n" 7340 "}", 7341 Space); 7342 verifyFormat("if (true)\n" 7343 " f ();\n" 7344 "else if (true)\n" 7345 " f ();", 7346 Space); 7347 verifyFormat("do {\n" 7348 " do_something ();\n" 7349 "} while (something ());", 7350 Space); 7351 verifyFormat("switch (x) {\n" 7352 "default:\n" 7353 " break;\n" 7354 "}", 7355 Space); 7356 verifyFormat("A::A () : a (1) {}", Space); 7357 verifyFormat("void f () __attribute__ ((asdf));", Space); 7358 verifyFormat("*(&a + 1);\n" 7359 "&((&a)[1]);\n" 7360 "a[(b + c) * d];\n" 7361 "(((a + 1) * 2) + 3) * 4;", 7362 Space); 7363 verifyFormat("#define A(x) x", Space); 7364 verifyFormat("#define A (x) x", Space); 7365 verifyFormat("#if defined(x)\n" 7366 "#endif", 7367 Space); 7368 verifyFormat("auto i = std::make_unique<int> (5);", Space); 7369 verifyFormat("size_t x = sizeof (x);", Space); 7370 verifyFormat("auto f (int x) -> decltype (x);", Space); 7371 verifyFormat("int f (T x) noexcept (x.create ());", Space); 7372 verifyFormat("alignas (128) char a[128];", Space); 7373 verifyFormat("size_t x = alignof (MyType);", Space); 7374 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 7375 verifyFormat("int f () throw (Deprecated);", Space); 7376 verifyFormat("typedef void (*cb) (int);", Space); 7377 verifyFormat("T A::operator() ();", Space); 7378 verifyFormat("X A::operator++ (T);", Space); 7379 } 7380 7381 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 7382 FormatStyle Spaces = getLLVMStyle(); 7383 7384 Spaces.SpacesInParentheses = true; 7385 verifyFormat("call( x, y, z );", Spaces); 7386 verifyFormat("call();", Spaces); 7387 verifyFormat("std::function<void( int, int )> callback;", Spaces); 7388 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 7389 Spaces); 7390 verifyFormat("while ( (bool)1 )\n" 7391 " continue;", 7392 Spaces); 7393 verifyFormat("for ( ;; )\n" 7394 " continue;", 7395 Spaces); 7396 verifyFormat("if ( true )\n" 7397 " f();\n" 7398 "else if ( true )\n" 7399 " f();", 7400 Spaces); 7401 verifyFormat("do {\n" 7402 " do_something( (int)i );\n" 7403 "} while ( something() );", 7404 Spaces); 7405 verifyFormat("switch ( x ) {\n" 7406 "default:\n" 7407 " break;\n" 7408 "}", 7409 Spaces); 7410 7411 Spaces.SpacesInParentheses = false; 7412 Spaces.SpacesInCStyleCastParentheses = true; 7413 verifyFormat("Type *A = ( Type * )P;", Spaces); 7414 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 7415 verifyFormat("x = ( int32 )y;", Spaces); 7416 verifyFormat("int a = ( int )(2.0f);", Spaces); 7417 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 7418 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 7419 verifyFormat("#define x (( int )-1)", Spaces); 7420 7421 // Run the first set of tests again with: 7422 Spaces.SpacesInParentheses = false; 7423 Spaces.SpaceInEmptyParentheses = true; 7424 Spaces.SpacesInCStyleCastParentheses = true; 7425 verifyFormat("call(x, y, z);", Spaces); 7426 verifyFormat("call( );", Spaces); 7427 verifyFormat("std::function<void(int, int)> callback;", Spaces); 7428 verifyFormat("while (( bool )1)\n" 7429 " continue;", 7430 Spaces); 7431 verifyFormat("for (;;)\n" 7432 " continue;", 7433 Spaces); 7434 verifyFormat("if (true)\n" 7435 " f( );\n" 7436 "else if (true)\n" 7437 " f( );", 7438 Spaces); 7439 verifyFormat("do {\n" 7440 " do_something(( int )i);\n" 7441 "} while (something( ));", 7442 Spaces); 7443 verifyFormat("switch (x) {\n" 7444 "default:\n" 7445 " break;\n" 7446 "}", 7447 Spaces); 7448 7449 // Run the first set of tests again with: 7450 Spaces.SpaceAfterCStyleCast = true; 7451 verifyFormat("call(x, y, z);", Spaces); 7452 verifyFormat("call( );", Spaces); 7453 verifyFormat("std::function<void(int, int)> callback;", Spaces); 7454 verifyFormat("while (( bool ) 1)\n" 7455 " continue;", 7456 Spaces); 7457 verifyFormat("for (;;)\n" 7458 " continue;", 7459 Spaces); 7460 verifyFormat("if (true)\n" 7461 " f( );\n" 7462 "else if (true)\n" 7463 " f( );", 7464 Spaces); 7465 verifyFormat("do {\n" 7466 " do_something(( int ) i);\n" 7467 "} while (something( ));", 7468 Spaces); 7469 verifyFormat("switch (x) {\n" 7470 "default:\n" 7471 " break;\n" 7472 "}", 7473 Spaces); 7474 7475 // Run subset of tests again with: 7476 Spaces.SpacesInCStyleCastParentheses = false; 7477 Spaces.SpaceAfterCStyleCast = true; 7478 verifyFormat("while ((bool) 1)\n" 7479 " continue;", 7480 Spaces); 7481 verifyFormat("do {\n" 7482 " do_something((int) i);\n" 7483 "} while (something( ));", 7484 Spaces); 7485 } 7486 7487 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 7488 verifyFormat("int a[5];"); 7489 verifyFormat("a[3] += 42;"); 7490 7491 FormatStyle Spaces = getLLVMStyle(); 7492 Spaces.SpacesInSquareBrackets = true; 7493 // Lambdas unchanged. 7494 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 7495 verifyFormat("return [i, args...] {};", Spaces); 7496 7497 // Not lambdas. 7498 verifyFormat("int a[ 5 ];", Spaces); 7499 verifyFormat("a[ 3 ] += 42;", Spaces); 7500 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 7501 verifyFormat("double &operator[](int i) { return 0; }\n" 7502 "int i;", 7503 Spaces); 7504 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 7505 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 7506 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 7507 } 7508 7509 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 7510 verifyFormat("int a = 5;"); 7511 verifyFormat("a += 42;"); 7512 verifyFormat("a or_eq 8;"); 7513 7514 FormatStyle Spaces = getLLVMStyle(); 7515 Spaces.SpaceBeforeAssignmentOperators = false; 7516 verifyFormat("int a= 5;", Spaces); 7517 verifyFormat("a+= 42;", Spaces); 7518 verifyFormat("a or_eq 8;", Spaces); 7519 } 7520 7521 TEST_F(FormatTest, AlignConsecutiveAssignments) { 7522 FormatStyle Alignment = getLLVMStyle(); 7523 Alignment.AlignConsecutiveAssignments = false; 7524 verifyFormat("int a = 5;\n" 7525 "int oneTwoThree = 123;", 7526 Alignment); 7527 verifyFormat("int a = 5;\n" 7528 "int oneTwoThree = 123;", 7529 Alignment); 7530 7531 Alignment.AlignConsecutiveAssignments = true; 7532 verifyFormat("int a = 5;\n" 7533 "int oneTwoThree = 123;", 7534 Alignment); 7535 verifyFormat("int a = method();\n" 7536 "int oneTwoThree = 133;", 7537 Alignment); 7538 verifyFormat("a &= 5;\n" 7539 "bcd *= 5;\n" 7540 "ghtyf += 5;\n" 7541 "dvfvdb -= 5;\n" 7542 "a /= 5;\n" 7543 "vdsvsv %= 5;\n" 7544 "sfdbddfbdfbb ^= 5;\n" 7545 "dvsdsv |= 5;\n" 7546 "int dsvvdvsdvvv = 123;", 7547 Alignment); 7548 verifyFormat("int i = 1, j = 10;\n" 7549 "something = 2000;", 7550 Alignment); 7551 verifyFormat("something = 2000;\n" 7552 "int i = 1, j = 10;\n", 7553 Alignment); 7554 verifyFormat("something = 2000;\n" 7555 "another = 911;\n" 7556 "int i = 1, j = 10;\n" 7557 "oneMore = 1;\n" 7558 "i = 2;", 7559 Alignment); 7560 verifyFormat("int a = 5;\n" 7561 "int one = 1;\n" 7562 "method();\n" 7563 "int oneTwoThree = 123;\n" 7564 "int oneTwo = 12;", 7565 Alignment); 7566 verifyFormat("int oneTwoThree = 123;\n" 7567 "int oneTwo = 12;\n" 7568 "method();\n", 7569 Alignment); 7570 verifyFormat("int oneTwoThree = 123; // comment\n" 7571 "int oneTwo = 12; // comment", 7572 Alignment); 7573 EXPECT_EQ("int a = 5;\n" 7574 "\n" 7575 "int oneTwoThree = 123;", 7576 format("int a = 5;\n" 7577 "\n" 7578 "int oneTwoThree= 123;", 7579 Alignment)); 7580 EXPECT_EQ("int a = 5;\n" 7581 "int one = 1;\n" 7582 "\n" 7583 "int oneTwoThree = 123;", 7584 format("int a = 5;\n" 7585 "int one = 1;\n" 7586 "\n" 7587 "int oneTwoThree = 123;", 7588 Alignment)); 7589 EXPECT_EQ("int a = 5;\n" 7590 "int one = 1;\n" 7591 "\n" 7592 "int oneTwoThree = 123;\n" 7593 "int oneTwo = 12;", 7594 format("int a = 5;\n" 7595 "int one = 1;\n" 7596 "\n" 7597 "int oneTwoThree = 123;\n" 7598 "int oneTwo = 12;", 7599 Alignment)); 7600 Alignment.AlignEscapedNewlinesLeft = true; 7601 verifyFormat("#define A \\\n" 7602 " int aaaa = 12; \\\n" 7603 " int b = 23; \\\n" 7604 " int ccc = 234; \\\n" 7605 " int dddddddddd = 2345;", 7606 Alignment); 7607 Alignment.AlignEscapedNewlinesLeft = false; 7608 verifyFormat("#define A " 7609 " \\\n" 7610 " int aaaa = 12; " 7611 " \\\n" 7612 " int b = 23; " 7613 " \\\n" 7614 " int ccc = 234; " 7615 " \\\n" 7616 " int dddddddddd = 2345;", 7617 Alignment); 7618 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 7619 "k = 4, int l = 5,\n" 7620 " int m = 6) {\n" 7621 " int j = 10;\n" 7622 " otherThing = 1;\n" 7623 "}", 7624 Alignment); 7625 verifyFormat("void SomeFunction(int parameter = 0) {\n" 7626 " int i = 1;\n" 7627 " int j = 2;\n" 7628 " int big = 10000;\n" 7629 "}", 7630 Alignment); 7631 verifyFormat("class C {\n" 7632 "public:\n" 7633 " int i = 1;\n" 7634 " virtual void f() = 0;\n" 7635 "};", 7636 Alignment); 7637 verifyFormat("int i = 1;\n" 7638 "if (SomeType t = getSomething()) {\n" 7639 "}\n" 7640 "int j = 2;\n" 7641 "int big = 10000;", 7642 Alignment); 7643 verifyFormat("int j = 7;\n" 7644 "for (int k = 0; k < N; ++k) {\n" 7645 "}\n" 7646 "int j = 2;\n" 7647 "int big = 10000;\n" 7648 "}", 7649 Alignment); 7650 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7651 verifyFormat("int i = 1;\n" 7652 "LooooooooooongType loooooooooooooooooooooongVariable\n" 7653 " = someLooooooooooooooooongFunction();\n" 7654 "int j = 2;", 7655 Alignment); 7656 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7657 verifyFormat("int i = 1;\n" 7658 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 7659 " someLooooooooooooooooongFunction();\n" 7660 "int j = 2;", 7661 Alignment); 7662 7663 verifyFormat("auto lambda = []() {\n" 7664 " auto i = 0;\n" 7665 " return 0;\n" 7666 "};\n" 7667 "int i = 0;\n" 7668 "auto v = type{\n" 7669 " i = 1, //\n" 7670 " (i = 2), //\n" 7671 " i = 3 //\n" 7672 "};", 7673 Alignment); 7674 7675 verifyFormat( 7676 "int i = 1;\n" 7677 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 7678 " loooooooooooooooooooooongParameterB);\n" 7679 "int j = 2;", 7680 Alignment); 7681 7682 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 7683 " typename B = very_long_type_name_1,\n" 7684 " typename T_2 = very_long_type_name_2>\n" 7685 "auto foo() {}\n", 7686 Alignment); 7687 verifyFormat("int a, b = 1;\n" 7688 "int c = 2;\n" 7689 "int dd = 3;\n", 7690 Alignment); 7691 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 7692 "float b[1][] = {{3.f}};\n", 7693 Alignment); 7694 verifyFormat("for (int i = 0; i < 1; i++)\n" 7695 " int x = 1;\n", 7696 Alignment); 7697 verifyFormat("for (i = 0; i < 1; i++)\n" 7698 " x = 1;\n" 7699 "y = 1;\n", 7700 Alignment); 7701 } 7702 7703 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 7704 FormatStyle Alignment = getLLVMStyle(); 7705 Alignment.AlignConsecutiveDeclarations = false; 7706 verifyFormat("float const a = 5;\n" 7707 "int oneTwoThree = 123;", 7708 Alignment); 7709 verifyFormat("int a = 5;\n" 7710 "float const oneTwoThree = 123;", 7711 Alignment); 7712 7713 Alignment.AlignConsecutiveDeclarations = true; 7714 verifyFormat("float const a = 5;\n" 7715 "int oneTwoThree = 123;", 7716 Alignment); 7717 verifyFormat("int a = method();\n" 7718 "float const oneTwoThree = 133;", 7719 Alignment); 7720 verifyFormat("int i = 1, j = 10;\n" 7721 "something = 2000;", 7722 Alignment); 7723 verifyFormat("something = 2000;\n" 7724 "int i = 1, j = 10;\n", 7725 Alignment); 7726 verifyFormat("float something = 2000;\n" 7727 "double another = 911;\n" 7728 "int i = 1, j = 10;\n" 7729 "const int *oneMore = 1;\n" 7730 "unsigned i = 2;", 7731 Alignment); 7732 verifyFormat("float a = 5;\n" 7733 "int one = 1;\n" 7734 "method();\n" 7735 "const double oneTwoThree = 123;\n" 7736 "const unsigned int oneTwo = 12;", 7737 Alignment); 7738 verifyFormat("int oneTwoThree{0}; // comment\n" 7739 "unsigned oneTwo; // comment", 7740 Alignment); 7741 EXPECT_EQ("float const a = 5;\n" 7742 "\n" 7743 "int oneTwoThree = 123;", 7744 format("float const a = 5;\n" 7745 "\n" 7746 "int oneTwoThree= 123;", 7747 Alignment)); 7748 EXPECT_EQ("float a = 5;\n" 7749 "int one = 1;\n" 7750 "\n" 7751 "unsigned oneTwoThree = 123;", 7752 format("float a = 5;\n" 7753 "int one = 1;\n" 7754 "\n" 7755 "unsigned oneTwoThree = 123;", 7756 Alignment)); 7757 EXPECT_EQ("float a = 5;\n" 7758 "int one = 1;\n" 7759 "\n" 7760 "unsigned oneTwoThree = 123;\n" 7761 "int oneTwo = 12;", 7762 format("float a = 5;\n" 7763 "int one = 1;\n" 7764 "\n" 7765 "unsigned oneTwoThree = 123;\n" 7766 "int oneTwo = 12;", 7767 Alignment)); 7768 // Function prototype alignment 7769 verifyFormat("int a();\n" 7770 "double b();", 7771 Alignment); 7772 verifyFormat("int a(int x);\n" 7773 "double b();", 7774 Alignment); 7775 unsigned OldColumnLimit = Alignment.ColumnLimit; 7776 // We need to set ColumnLimit to zero, in order to stress nested alignments, 7777 // otherwise the function parameters will be re-flowed onto a single line. 7778 Alignment.ColumnLimit = 0; 7779 EXPECT_EQ("int a(int x,\n" 7780 " float y);\n" 7781 "double b(int x,\n" 7782 " double y);", 7783 format("int a(int x,\n" 7784 " float y);\n" 7785 "double b(int x,\n" 7786 " double y);", 7787 Alignment)); 7788 // This ensures that function parameters of function declarations are 7789 // correctly indented when their owning functions are indented. 7790 // The failure case here is for 'double y' to not be indented enough. 7791 EXPECT_EQ("double a(int x);\n" 7792 "int b(int y,\n" 7793 " double z);", 7794 format("double a(int x);\n" 7795 "int b(int y,\n" 7796 " double z);", 7797 Alignment)); 7798 // Set ColumnLimit low so that we induce wrapping immediately after 7799 // the function name and opening paren. 7800 Alignment.ColumnLimit = 13; 7801 verifyFormat("int function(\n" 7802 " int x,\n" 7803 " bool y);", 7804 Alignment); 7805 Alignment.ColumnLimit = OldColumnLimit; 7806 // Ensure function pointers don't screw up recursive alignment 7807 verifyFormat("int a(int x, void (*fp)(int y));\n" 7808 "double b();", 7809 Alignment); 7810 Alignment.AlignConsecutiveAssignments = true; 7811 // Ensure recursive alignment is broken by function braces, so that the 7812 // "a = 1" does not align with subsequent assignments inside the function 7813 // body. 7814 verifyFormat("int func(int a = 1) {\n" 7815 " int b = 2;\n" 7816 " int cc = 3;\n" 7817 "}", 7818 Alignment); 7819 verifyFormat("float something = 2000;\n" 7820 "double another = 911;\n" 7821 "int i = 1, j = 10;\n" 7822 "const int *oneMore = 1;\n" 7823 "unsigned i = 2;", 7824 Alignment); 7825 verifyFormat("int oneTwoThree = {0}; // comment\n" 7826 "unsigned oneTwo = 0; // comment", 7827 Alignment); 7828 // Make sure that scope is correctly tracked, in the absence of braces 7829 verifyFormat("for (int i = 0; i < n; i++)\n" 7830 " j = i;\n" 7831 "double x = 1;\n", 7832 Alignment); 7833 verifyFormat("if (int i = 0)\n" 7834 " j = i;\n" 7835 "double x = 1;\n", 7836 Alignment); 7837 // Ensure operator[] and operator() are comprehended 7838 verifyFormat("struct test {\n" 7839 " long long int foo();\n" 7840 " int operator[](int a);\n" 7841 " double bar();\n" 7842 "};\n", 7843 Alignment); 7844 verifyFormat("struct test {\n" 7845 " long long int foo();\n" 7846 " int operator()(int a);\n" 7847 " double bar();\n" 7848 "};\n", 7849 Alignment); 7850 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 7851 " int const i = 1;\n" 7852 " int * j = 2;\n" 7853 " int big = 10000;\n" 7854 "\n" 7855 " unsigned oneTwoThree = 123;\n" 7856 " int oneTwo = 12;\n" 7857 " method();\n" 7858 " float k = 2;\n" 7859 " int ll = 10000;\n" 7860 "}", 7861 format("void SomeFunction(int parameter= 0) {\n" 7862 " int const i= 1;\n" 7863 " int *j=2;\n" 7864 " int big = 10000;\n" 7865 "\n" 7866 "unsigned oneTwoThree =123;\n" 7867 "int oneTwo = 12;\n" 7868 " method();\n" 7869 "float k= 2;\n" 7870 "int ll=10000;\n" 7871 "}", 7872 Alignment)); 7873 Alignment.AlignConsecutiveAssignments = false; 7874 Alignment.AlignEscapedNewlinesLeft = true; 7875 verifyFormat("#define A \\\n" 7876 " int aaaa = 12; \\\n" 7877 " float b = 23; \\\n" 7878 " const int ccc = 234; \\\n" 7879 " unsigned dddddddddd = 2345;", 7880 Alignment); 7881 Alignment.AlignEscapedNewlinesLeft = false; 7882 Alignment.ColumnLimit = 30; 7883 verifyFormat("#define A \\\n" 7884 " int aaaa = 12; \\\n" 7885 " float b = 23; \\\n" 7886 " const int ccc = 234; \\\n" 7887 " int dddddddddd = 2345;", 7888 Alignment); 7889 Alignment.ColumnLimit = 80; 7890 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 7891 "k = 4, int l = 5,\n" 7892 " int m = 6) {\n" 7893 " const int j = 10;\n" 7894 " otherThing = 1;\n" 7895 "}", 7896 Alignment); 7897 verifyFormat("void SomeFunction(int parameter = 0) {\n" 7898 " int const i = 1;\n" 7899 " int * j = 2;\n" 7900 " int big = 10000;\n" 7901 "}", 7902 Alignment); 7903 verifyFormat("class C {\n" 7904 "public:\n" 7905 " int i = 1;\n" 7906 " virtual void f() = 0;\n" 7907 "};", 7908 Alignment); 7909 verifyFormat("float i = 1;\n" 7910 "if (SomeType t = getSomething()) {\n" 7911 "}\n" 7912 "const unsigned j = 2;\n" 7913 "int big = 10000;", 7914 Alignment); 7915 verifyFormat("float j = 7;\n" 7916 "for (int k = 0; k < N; ++k) {\n" 7917 "}\n" 7918 "unsigned j = 2;\n" 7919 "int big = 10000;\n" 7920 "}", 7921 Alignment); 7922 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7923 verifyFormat("float i = 1;\n" 7924 "LooooooooooongType loooooooooooooooooooooongVariable\n" 7925 " = someLooooooooooooooooongFunction();\n" 7926 "int j = 2;", 7927 Alignment); 7928 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7929 verifyFormat("int i = 1;\n" 7930 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 7931 " someLooooooooooooooooongFunction();\n" 7932 "int j = 2;", 7933 Alignment); 7934 7935 Alignment.AlignConsecutiveAssignments = true; 7936 verifyFormat("auto lambda = []() {\n" 7937 " auto ii = 0;\n" 7938 " float j = 0;\n" 7939 " return 0;\n" 7940 "};\n" 7941 "int i = 0;\n" 7942 "float i2 = 0;\n" 7943 "auto v = type{\n" 7944 " i = 1, //\n" 7945 " (i = 2), //\n" 7946 " i = 3 //\n" 7947 "};", 7948 Alignment); 7949 Alignment.AlignConsecutiveAssignments = false; 7950 7951 verifyFormat( 7952 "int i = 1;\n" 7953 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 7954 " loooooooooooooooooooooongParameterB);\n" 7955 "int j = 2;", 7956 Alignment); 7957 7958 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 7959 // We expect declarations and assignments to align, as long as it doesn't 7960 // exceed the column limit, starting a new alignment sequence whenever it 7961 // happens. 7962 Alignment.AlignConsecutiveAssignments = true; 7963 Alignment.ColumnLimit = 30; 7964 verifyFormat("float ii = 1;\n" 7965 "unsigned j = 2;\n" 7966 "int someVerylongVariable = 1;\n" 7967 "AnotherLongType ll = 123456;\n" 7968 "VeryVeryLongType k = 2;\n" 7969 "int myvar = 1;", 7970 Alignment); 7971 Alignment.ColumnLimit = 80; 7972 Alignment.AlignConsecutiveAssignments = false; 7973 7974 verifyFormat( 7975 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 7976 " typename LongType, typename B>\n" 7977 "auto foo() {}\n", 7978 Alignment); 7979 verifyFormat("float a, b = 1;\n" 7980 "int c = 2;\n" 7981 "int dd = 3;\n", 7982 Alignment); 7983 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 7984 "float b[1][] = {{3.f}};\n", 7985 Alignment); 7986 Alignment.AlignConsecutiveAssignments = true; 7987 verifyFormat("float a, b = 1;\n" 7988 "int c = 2;\n" 7989 "int dd = 3;\n", 7990 Alignment); 7991 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 7992 "float b[1][] = {{3.f}};\n", 7993 Alignment); 7994 Alignment.AlignConsecutiveAssignments = false; 7995 7996 Alignment.ColumnLimit = 30; 7997 Alignment.BinPackParameters = false; 7998 verifyFormat("void foo(float a,\n" 7999 " float b,\n" 8000 " int c,\n" 8001 " uint32_t *d) {\n" 8002 " int * e = 0;\n" 8003 " float f = 0;\n" 8004 " double g = 0;\n" 8005 "}\n" 8006 "void bar(ino_t a,\n" 8007 " int b,\n" 8008 " uint32_t *c,\n" 8009 " bool d) {}\n", 8010 Alignment); 8011 Alignment.BinPackParameters = true; 8012 Alignment.ColumnLimit = 80; 8013 } 8014 8015 TEST_F(FormatTest, LinuxBraceBreaking) { 8016 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8017 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8018 verifyFormat("namespace a\n" 8019 "{\n" 8020 "class A\n" 8021 "{\n" 8022 " void f()\n" 8023 " {\n" 8024 " if (true) {\n" 8025 " a();\n" 8026 " b();\n" 8027 " } else {\n" 8028 " a();\n" 8029 " }\n" 8030 " }\n" 8031 " void g() { return; }\n" 8032 "};\n" 8033 "struct B {\n" 8034 " int x;\n" 8035 "};\n" 8036 "}\n", 8037 LinuxBraceStyle); 8038 verifyFormat("enum X {\n" 8039 " Y = 0,\n" 8040 "}\n", 8041 LinuxBraceStyle); 8042 verifyFormat("struct S {\n" 8043 " int Type;\n" 8044 " union {\n" 8045 " int x;\n" 8046 " double y;\n" 8047 " } Value;\n" 8048 " class C\n" 8049 " {\n" 8050 " MyFavoriteType Value;\n" 8051 " } Class;\n" 8052 "}\n", 8053 LinuxBraceStyle); 8054 } 8055 8056 TEST_F(FormatTest, MozillaBraceBreaking) { 8057 FormatStyle MozillaBraceStyle = getLLVMStyle(); 8058 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 8059 MozillaBraceStyle.FixNamespaceComments = false; 8060 verifyFormat("namespace a {\n" 8061 "class A\n" 8062 "{\n" 8063 " void f()\n" 8064 " {\n" 8065 " if (true) {\n" 8066 " a();\n" 8067 " b();\n" 8068 " }\n" 8069 " }\n" 8070 " void g() { return; }\n" 8071 "};\n" 8072 "enum E\n" 8073 "{\n" 8074 " A,\n" 8075 " // foo\n" 8076 " B,\n" 8077 " C\n" 8078 "};\n" 8079 "struct B\n" 8080 "{\n" 8081 " int x;\n" 8082 "};\n" 8083 "}\n", 8084 MozillaBraceStyle); 8085 verifyFormat("struct S\n" 8086 "{\n" 8087 " int Type;\n" 8088 " union\n" 8089 " {\n" 8090 " int x;\n" 8091 " double y;\n" 8092 " } Value;\n" 8093 " class C\n" 8094 " {\n" 8095 " MyFavoriteType Value;\n" 8096 " } Class;\n" 8097 "}\n", 8098 MozillaBraceStyle); 8099 } 8100 8101 TEST_F(FormatTest, StroustrupBraceBreaking) { 8102 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 8103 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8104 verifyFormat("namespace a {\n" 8105 "class A {\n" 8106 " void f()\n" 8107 " {\n" 8108 " if (true) {\n" 8109 " a();\n" 8110 " b();\n" 8111 " }\n" 8112 " }\n" 8113 " void g() { return; }\n" 8114 "};\n" 8115 "struct B {\n" 8116 " int x;\n" 8117 "};\n" 8118 "} // namespace a\n", 8119 StroustrupBraceStyle); 8120 8121 verifyFormat("void foo()\n" 8122 "{\n" 8123 " if (a) {\n" 8124 " a();\n" 8125 " }\n" 8126 " else {\n" 8127 " b();\n" 8128 " }\n" 8129 "}\n", 8130 StroustrupBraceStyle); 8131 8132 verifyFormat("#ifdef _DEBUG\n" 8133 "int foo(int i = 0)\n" 8134 "#else\n" 8135 "int foo(int i = 5)\n" 8136 "#endif\n" 8137 "{\n" 8138 " return i;\n" 8139 "}", 8140 StroustrupBraceStyle); 8141 8142 verifyFormat("void foo() {}\n" 8143 "void bar()\n" 8144 "#ifdef _DEBUG\n" 8145 "{\n" 8146 " foo();\n" 8147 "}\n" 8148 "#else\n" 8149 "{\n" 8150 "}\n" 8151 "#endif", 8152 StroustrupBraceStyle); 8153 8154 verifyFormat("void foobar() { int i = 5; }\n" 8155 "#ifdef _DEBUG\n" 8156 "void bar() {}\n" 8157 "#else\n" 8158 "void bar() { foobar(); }\n" 8159 "#endif", 8160 StroustrupBraceStyle); 8161 } 8162 8163 TEST_F(FormatTest, AllmanBraceBreaking) { 8164 FormatStyle AllmanBraceStyle = getLLVMStyle(); 8165 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 8166 verifyFormat("namespace a\n" 8167 "{\n" 8168 "class A\n" 8169 "{\n" 8170 " void f()\n" 8171 " {\n" 8172 " if (true)\n" 8173 " {\n" 8174 " a();\n" 8175 " b();\n" 8176 " }\n" 8177 " }\n" 8178 " void g() { return; }\n" 8179 "};\n" 8180 "struct B\n" 8181 "{\n" 8182 " int x;\n" 8183 "};\n" 8184 "}", 8185 AllmanBraceStyle); 8186 8187 verifyFormat("void f()\n" 8188 "{\n" 8189 " if (true)\n" 8190 " {\n" 8191 " a();\n" 8192 " }\n" 8193 " else if (false)\n" 8194 " {\n" 8195 " b();\n" 8196 " }\n" 8197 " else\n" 8198 " {\n" 8199 " c();\n" 8200 " }\n" 8201 "}\n", 8202 AllmanBraceStyle); 8203 8204 verifyFormat("void f()\n" 8205 "{\n" 8206 " for (int i = 0; i < 10; ++i)\n" 8207 " {\n" 8208 " a();\n" 8209 " }\n" 8210 " while (false)\n" 8211 " {\n" 8212 " b();\n" 8213 " }\n" 8214 " do\n" 8215 " {\n" 8216 " c();\n" 8217 " } while (false)\n" 8218 "}\n", 8219 AllmanBraceStyle); 8220 8221 verifyFormat("void f(int a)\n" 8222 "{\n" 8223 " switch (a)\n" 8224 " {\n" 8225 " case 0:\n" 8226 " break;\n" 8227 " case 1:\n" 8228 " {\n" 8229 " break;\n" 8230 " }\n" 8231 " case 2:\n" 8232 " {\n" 8233 " }\n" 8234 " break;\n" 8235 " default:\n" 8236 " break;\n" 8237 " }\n" 8238 "}\n", 8239 AllmanBraceStyle); 8240 8241 verifyFormat("enum X\n" 8242 "{\n" 8243 " Y = 0,\n" 8244 "}\n", 8245 AllmanBraceStyle); 8246 verifyFormat("enum X\n" 8247 "{\n" 8248 " Y = 0\n" 8249 "}\n", 8250 AllmanBraceStyle); 8251 8252 verifyFormat("@interface BSApplicationController ()\n" 8253 "{\n" 8254 "@private\n" 8255 " id _extraIvar;\n" 8256 "}\n" 8257 "@end\n", 8258 AllmanBraceStyle); 8259 8260 verifyFormat("#ifdef _DEBUG\n" 8261 "int foo(int i = 0)\n" 8262 "#else\n" 8263 "int foo(int i = 5)\n" 8264 "#endif\n" 8265 "{\n" 8266 " return i;\n" 8267 "}", 8268 AllmanBraceStyle); 8269 8270 verifyFormat("void foo() {}\n" 8271 "void bar()\n" 8272 "#ifdef _DEBUG\n" 8273 "{\n" 8274 " foo();\n" 8275 "}\n" 8276 "#else\n" 8277 "{\n" 8278 "}\n" 8279 "#endif", 8280 AllmanBraceStyle); 8281 8282 verifyFormat("void foobar() { int i = 5; }\n" 8283 "#ifdef _DEBUG\n" 8284 "void bar() {}\n" 8285 "#else\n" 8286 "void bar() { foobar(); }\n" 8287 "#endif", 8288 AllmanBraceStyle); 8289 8290 // This shouldn't affect ObjC blocks.. 8291 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 8292 " // ...\n" 8293 " int i;\n" 8294 "}];", 8295 AllmanBraceStyle); 8296 verifyFormat("void (^block)(void) = ^{\n" 8297 " // ...\n" 8298 " int i;\n" 8299 "};", 8300 AllmanBraceStyle); 8301 // .. or dict literals. 8302 verifyFormat("void f()\n" 8303 "{\n" 8304 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 8305 "}", 8306 AllmanBraceStyle); 8307 verifyFormat("int f()\n" 8308 "{ // comment\n" 8309 " return 42;\n" 8310 "}", 8311 AllmanBraceStyle); 8312 8313 AllmanBraceStyle.ColumnLimit = 19; 8314 verifyFormat("void f() { int i; }", AllmanBraceStyle); 8315 AllmanBraceStyle.ColumnLimit = 18; 8316 verifyFormat("void f()\n" 8317 "{\n" 8318 " int i;\n" 8319 "}", 8320 AllmanBraceStyle); 8321 AllmanBraceStyle.ColumnLimit = 80; 8322 8323 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 8324 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 8325 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 8326 verifyFormat("void f(bool b)\n" 8327 "{\n" 8328 " if (b)\n" 8329 " {\n" 8330 " return;\n" 8331 " }\n" 8332 "}\n", 8333 BreakBeforeBraceShortIfs); 8334 verifyFormat("void f(bool b)\n" 8335 "{\n" 8336 " if (b) return;\n" 8337 "}\n", 8338 BreakBeforeBraceShortIfs); 8339 verifyFormat("void f(bool b)\n" 8340 "{\n" 8341 " while (b)\n" 8342 " {\n" 8343 " return;\n" 8344 " }\n" 8345 "}\n", 8346 BreakBeforeBraceShortIfs); 8347 } 8348 8349 TEST_F(FormatTest, GNUBraceBreaking) { 8350 FormatStyle GNUBraceStyle = getLLVMStyle(); 8351 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 8352 verifyFormat("namespace a\n" 8353 "{\n" 8354 "class A\n" 8355 "{\n" 8356 " void f()\n" 8357 " {\n" 8358 " int a;\n" 8359 " {\n" 8360 " int b;\n" 8361 " }\n" 8362 " if (true)\n" 8363 " {\n" 8364 " a();\n" 8365 " b();\n" 8366 " }\n" 8367 " }\n" 8368 " void g() { return; }\n" 8369 "}\n" 8370 "}", 8371 GNUBraceStyle); 8372 8373 verifyFormat("void f()\n" 8374 "{\n" 8375 " if (true)\n" 8376 " {\n" 8377 " a();\n" 8378 " }\n" 8379 " else if (false)\n" 8380 " {\n" 8381 " b();\n" 8382 " }\n" 8383 " else\n" 8384 " {\n" 8385 " c();\n" 8386 " }\n" 8387 "}\n", 8388 GNUBraceStyle); 8389 8390 verifyFormat("void f()\n" 8391 "{\n" 8392 " for (int i = 0; i < 10; ++i)\n" 8393 " {\n" 8394 " a();\n" 8395 " }\n" 8396 " while (false)\n" 8397 " {\n" 8398 " b();\n" 8399 " }\n" 8400 " do\n" 8401 " {\n" 8402 " c();\n" 8403 " }\n" 8404 " while (false);\n" 8405 "}\n", 8406 GNUBraceStyle); 8407 8408 verifyFormat("void f(int a)\n" 8409 "{\n" 8410 " switch (a)\n" 8411 " {\n" 8412 " case 0:\n" 8413 " break;\n" 8414 " case 1:\n" 8415 " {\n" 8416 " break;\n" 8417 " }\n" 8418 " case 2:\n" 8419 " {\n" 8420 " }\n" 8421 " break;\n" 8422 " default:\n" 8423 " break;\n" 8424 " }\n" 8425 "}\n", 8426 GNUBraceStyle); 8427 8428 verifyFormat("enum X\n" 8429 "{\n" 8430 " Y = 0,\n" 8431 "}\n", 8432 GNUBraceStyle); 8433 8434 verifyFormat("@interface BSApplicationController ()\n" 8435 "{\n" 8436 "@private\n" 8437 " id _extraIvar;\n" 8438 "}\n" 8439 "@end\n", 8440 GNUBraceStyle); 8441 8442 verifyFormat("#ifdef _DEBUG\n" 8443 "int foo(int i = 0)\n" 8444 "#else\n" 8445 "int foo(int i = 5)\n" 8446 "#endif\n" 8447 "{\n" 8448 " return i;\n" 8449 "}", 8450 GNUBraceStyle); 8451 8452 verifyFormat("void foo() {}\n" 8453 "void bar()\n" 8454 "#ifdef _DEBUG\n" 8455 "{\n" 8456 " foo();\n" 8457 "}\n" 8458 "#else\n" 8459 "{\n" 8460 "}\n" 8461 "#endif", 8462 GNUBraceStyle); 8463 8464 verifyFormat("void foobar() { int i = 5; }\n" 8465 "#ifdef _DEBUG\n" 8466 "void bar() {}\n" 8467 "#else\n" 8468 "void bar() { foobar(); }\n" 8469 "#endif", 8470 GNUBraceStyle); 8471 } 8472 8473 TEST_F(FormatTest, WebKitBraceBreaking) { 8474 FormatStyle WebKitBraceStyle = getLLVMStyle(); 8475 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 8476 WebKitBraceStyle.FixNamespaceComments = false; 8477 verifyFormat("namespace a {\n" 8478 "class A {\n" 8479 " void f()\n" 8480 " {\n" 8481 " if (true) {\n" 8482 " a();\n" 8483 " b();\n" 8484 " }\n" 8485 " }\n" 8486 " void g() { return; }\n" 8487 "};\n" 8488 "enum E {\n" 8489 " A,\n" 8490 " // foo\n" 8491 " B,\n" 8492 " C\n" 8493 "};\n" 8494 "struct B {\n" 8495 " int x;\n" 8496 "};\n" 8497 "}\n", 8498 WebKitBraceStyle); 8499 verifyFormat("struct S {\n" 8500 " int Type;\n" 8501 " union {\n" 8502 " int x;\n" 8503 " double y;\n" 8504 " } Value;\n" 8505 " class C {\n" 8506 " MyFavoriteType Value;\n" 8507 " } Class;\n" 8508 "};\n", 8509 WebKitBraceStyle); 8510 } 8511 8512 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 8513 verifyFormat("void f() {\n" 8514 " try {\n" 8515 " } catch (const Exception &e) {\n" 8516 " }\n" 8517 "}\n", 8518 getLLVMStyle()); 8519 } 8520 8521 TEST_F(FormatTest, UnderstandsPragmas) { 8522 verifyFormat("#pragma omp reduction(| : var)"); 8523 verifyFormat("#pragma omp reduction(+ : var)"); 8524 8525 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 8526 "(including parentheses).", 8527 format("#pragma mark Any non-hyphenated or hyphenated string " 8528 "(including parentheses).")); 8529 } 8530 8531 TEST_F(FormatTest, UnderstandPragmaOption) { 8532 verifyFormat("#pragma option -C -A"); 8533 8534 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 8535 } 8536 8537 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 8538 for (size_t i = 1; i < Styles.size(); ++i) \ 8539 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 8540 << " differs from Style #0" 8541 8542 TEST_F(FormatTest, GetsPredefinedStyleByName) { 8543 SmallVector<FormatStyle, 3> Styles; 8544 Styles.resize(3); 8545 8546 Styles[0] = getLLVMStyle(); 8547 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 8548 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 8549 EXPECT_ALL_STYLES_EQUAL(Styles); 8550 8551 Styles[0] = getGoogleStyle(); 8552 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 8553 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 8554 EXPECT_ALL_STYLES_EQUAL(Styles); 8555 8556 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 8557 EXPECT_TRUE( 8558 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 8559 EXPECT_TRUE( 8560 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 8561 EXPECT_ALL_STYLES_EQUAL(Styles); 8562 8563 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 8564 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 8565 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 8566 EXPECT_ALL_STYLES_EQUAL(Styles); 8567 8568 Styles[0] = getMozillaStyle(); 8569 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 8570 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 8571 EXPECT_ALL_STYLES_EQUAL(Styles); 8572 8573 Styles[0] = getWebKitStyle(); 8574 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 8575 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 8576 EXPECT_ALL_STYLES_EQUAL(Styles); 8577 8578 Styles[0] = getGNUStyle(); 8579 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 8580 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 8581 EXPECT_ALL_STYLES_EQUAL(Styles); 8582 8583 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 8584 } 8585 8586 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 8587 SmallVector<FormatStyle, 8> Styles; 8588 Styles.resize(2); 8589 8590 Styles[0] = getGoogleStyle(); 8591 Styles[1] = getLLVMStyle(); 8592 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 8593 EXPECT_ALL_STYLES_EQUAL(Styles); 8594 8595 Styles.resize(5); 8596 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 8597 Styles[1] = getLLVMStyle(); 8598 Styles[1].Language = FormatStyle::LK_JavaScript; 8599 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 8600 8601 Styles[2] = getLLVMStyle(); 8602 Styles[2].Language = FormatStyle::LK_JavaScript; 8603 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 8604 "BasedOnStyle: Google", 8605 &Styles[2]) 8606 .value()); 8607 8608 Styles[3] = getLLVMStyle(); 8609 Styles[3].Language = FormatStyle::LK_JavaScript; 8610 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 8611 "Language: JavaScript", 8612 &Styles[3]) 8613 .value()); 8614 8615 Styles[4] = getLLVMStyle(); 8616 Styles[4].Language = FormatStyle::LK_JavaScript; 8617 EXPECT_EQ(0, parseConfiguration("---\n" 8618 "BasedOnStyle: LLVM\n" 8619 "IndentWidth: 123\n" 8620 "---\n" 8621 "BasedOnStyle: Google\n" 8622 "Language: JavaScript", 8623 &Styles[4]) 8624 .value()); 8625 EXPECT_ALL_STYLES_EQUAL(Styles); 8626 } 8627 8628 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 8629 Style.FIELD = false; \ 8630 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 8631 EXPECT_TRUE(Style.FIELD); \ 8632 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 8633 EXPECT_FALSE(Style.FIELD); 8634 8635 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 8636 8637 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 8638 Style.STRUCT.FIELD = false; \ 8639 EXPECT_EQ(0, \ 8640 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 8641 .value()); \ 8642 EXPECT_TRUE(Style.STRUCT.FIELD); \ 8643 EXPECT_EQ(0, \ 8644 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 8645 .value()); \ 8646 EXPECT_FALSE(Style.STRUCT.FIELD); 8647 8648 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 8649 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 8650 8651 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 8652 EXPECT_NE(VALUE, Style.FIELD); \ 8653 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 8654 EXPECT_EQ(VALUE, Style.FIELD) 8655 8656 TEST_F(FormatTest, ParsesConfigurationBools) { 8657 FormatStyle Style = {}; 8658 Style.Language = FormatStyle::LK_Cpp; 8659 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 8660 CHECK_PARSE_BOOL(AlignOperands); 8661 CHECK_PARSE_BOOL(AlignTrailingComments); 8662 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 8663 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 8664 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 8665 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 8666 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 8667 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 8668 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 8669 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 8670 CHECK_PARSE_BOOL(BinPackArguments); 8671 CHECK_PARSE_BOOL(BinPackParameters); 8672 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 8673 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 8674 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 8675 CHECK_PARSE_BOOL(BreakStringLiterals); 8676 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 8677 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 8678 CHECK_PARSE_BOOL(DerivePointerAlignment); 8679 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 8680 CHECK_PARSE_BOOL(DisableFormat); 8681 CHECK_PARSE_BOOL(IndentCaseLabels); 8682 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 8683 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 8684 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 8685 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 8686 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 8687 CHECK_PARSE_BOOL(ReflowComments); 8688 CHECK_PARSE_BOOL(SortIncludes); 8689 CHECK_PARSE_BOOL(SpacesInParentheses); 8690 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 8691 CHECK_PARSE_BOOL(SpacesInAngles); 8692 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 8693 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 8694 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 8695 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 8696 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 8697 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 8698 8699 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 8700 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 8701 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 8702 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 8703 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 8704 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 8705 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 8706 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 8707 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 8708 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 8709 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 8710 } 8711 8712 #undef CHECK_PARSE_BOOL 8713 8714 TEST_F(FormatTest, ParsesConfiguration) { 8715 FormatStyle Style = {}; 8716 Style.Language = FormatStyle::LK_Cpp; 8717 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 8718 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 8719 ConstructorInitializerIndentWidth, 1234u); 8720 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 8721 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 8722 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 8723 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 8724 PenaltyBreakBeforeFirstCallParameter, 1234u); 8725 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 8726 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 8727 PenaltyReturnTypeOnItsOwnLine, 1234u); 8728 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 8729 SpacesBeforeTrailingComments, 1234u); 8730 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 8731 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 8732 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 8733 8734 Style.PointerAlignment = FormatStyle::PAS_Middle; 8735 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 8736 FormatStyle::PAS_Left); 8737 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 8738 FormatStyle::PAS_Right); 8739 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 8740 FormatStyle::PAS_Middle); 8741 // For backward compatibility: 8742 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 8743 FormatStyle::PAS_Left); 8744 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 8745 FormatStyle::PAS_Right); 8746 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 8747 FormatStyle::PAS_Middle); 8748 8749 Style.Standard = FormatStyle::LS_Auto; 8750 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 8751 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 8752 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 8753 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 8754 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 8755 8756 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8757 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 8758 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 8759 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 8760 FormatStyle::BOS_None); 8761 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 8762 FormatStyle::BOS_All); 8763 // For backward compatibility: 8764 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 8765 FormatStyle::BOS_None); 8766 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 8767 FormatStyle::BOS_All); 8768 8769 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8770 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 8771 FormatStyle::BAS_Align); 8772 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 8773 FormatStyle::BAS_DontAlign); 8774 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 8775 FormatStyle::BAS_AlwaysBreak); 8776 // For backward compatibility: 8777 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 8778 FormatStyle::BAS_DontAlign); 8779 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 8780 FormatStyle::BAS_Align); 8781 8782 Style.UseTab = FormatStyle::UT_ForIndentation; 8783 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 8784 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 8785 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 8786 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 8787 FormatStyle::UT_ForContinuationAndIndentation); 8788 // For backward compatibility: 8789 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 8790 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 8791 8792 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 8793 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 8794 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 8795 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 8796 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 8797 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 8798 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 8799 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 8800 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 8801 // For backward compatibility: 8802 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 8803 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 8804 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 8805 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 8806 8807 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 8808 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 8809 FormatStyle::SBPO_Never); 8810 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 8811 FormatStyle::SBPO_Always); 8812 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 8813 FormatStyle::SBPO_ControlStatements); 8814 // For backward compatibility: 8815 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 8816 FormatStyle::SBPO_Never); 8817 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 8818 FormatStyle::SBPO_ControlStatements); 8819 8820 Style.ColumnLimit = 123; 8821 FormatStyle BaseStyle = getLLVMStyle(); 8822 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 8823 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 8824 8825 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8826 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 8827 FormatStyle::BS_Attach); 8828 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 8829 FormatStyle::BS_Linux); 8830 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 8831 FormatStyle::BS_Mozilla); 8832 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 8833 FormatStyle::BS_Stroustrup); 8834 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 8835 FormatStyle::BS_Allman); 8836 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 8837 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 8838 FormatStyle::BS_WebKit); 8839 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 8840 FormatStyle::BS_Custom); 8841 8842 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8843 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 8844 FormatStyle::RTBS_None); 8845 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 8846 FormatStyle::RTBS_All); 8847 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 8848 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 8849 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 8850 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 8851 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 8852 AlwaysBreakAfterReturnType, 8853 FormatStyle::RTBS_TopLevelDefinitions); 8854 8855 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 8856 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 8857 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 8858 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 8859 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 8860 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 8861 AlwaysBreakAfterDefinitionReturnType, 8862 FormatStyle::DRTBS_TopLevel); 8863 8864 Style.NamespaceIndentation = FormatStyle::NI_All; 8865 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 8866 FormatStyle::NI_None); 8867 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 8868 FormatStyle::NI_Inner); 8869 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 8870 FormatStyle::NI_All); 8871 8872 // FIXME: This is required because parsing a configuration simply overwrites 8873 // the first N elements of the list instead of resetting it. 8874 Style.ForEachMacros.clear(); 8875 std::vector<std::string> BoostForeach; 8876 BoostForeach.push_back("BOOST_FOREACH"); 8877 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 8878 std::vector<std::string> BoostAndQForeach; 8879 BoostAndQForeach.push_back("BOOST_FOREACH"); 8880 BoostAndQForeach.push_back("Q_FOREACH"); 8881 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 8882 BoostAndQForeach); 8883 8884 Style.IncludeCategories.clear(); 8885 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 8886 {".*", 1}}; 8887 CHECK_PARSE("IncludeCategories:\n" 8888 " - Regex: abc/.*\n" 8889 " Priority: 2\n" 8890 " - Regex: .*\n" 8891 " Priority: 1", 8892 IncludeCategories, ExpectedCategories); 8893 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 8894 } 8895 8896 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 8897 FormatStyle Style = {}; 8898 Style.Language = FormatStyle::LK_Cpp; 8899 CHECK_PARSE("Language: Cpp\n" 8900 "IndentWidth: 12", 8901 IndentWidth, 12u); 8902 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 8903 "IndentWidth: 34", 8904 &Style), 8905 ParseError::Unsuitable); 8906 EXPECT_EQ(12u, Style.IndentWidth); 8907 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 8908 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 8909 8910 Style.Language = FormatStyle::LK_JavaScript; 8911 CHECK_PARSE("Language: JavaScript\n" 8912 "IndentWidth: 12", 8913 IndentWidth, 12u); 8914 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 8915 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 8916 "IndentWidth: 34", 8917 &Style), 8918 ParseError::Unsuitable); 8919 EXPECT_EQ(23u, Style.IndentWidth); 8920 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 8921 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 8922 8923 CHECK_PARSE("BasedOnStyle: LLVM\n" 8924 "IndentWidth: 67", 8925 IndentWidth, 67u); 8926 8927 CHECK_PARSE("---\n" 8928 "Language: JavaScript\n" 8929 "IndentWidth: 12\n" 8930 "---\n" 8931 "Language: Cpp\n" 8932 "IndentWidth: 34\n" 8933 "...\n", 8934 IndentWidth, 12u); 8935 8936 Style.Language = FormatStyle::LK_Cpp; 8937 CHECK_PARSE("---\n" 8938 "Language: JavaScript\n" 8939 "IndentWidth: 12\n" 8940 "---\n" 8941 "Language: Cpp\n" 8942 "IndentWidth: 34\n" 8943 "...\n", 8944 IndentWidth, 34u); 8945 CHECK_PARSE("---\n" 8946 "IndentWidth: 78\n" 8947 "---\n" 8948 "Language: JavaScript\n" 8949 "IndentWidth: 56\n" 8950 "...\n", 8951 IndentWidth, 78u); 8952 8953 Style.ColumnLimit = 123; 8954 Style.IndentWidth = 234; 8955 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 8956 Style.TabWidth = 345; 8957 EXPECT_FALSE(parseConfiguration("---\n" 8958 "IndentWidth: 456\n" 8959 "BreakBeforeBraces: Allman\n" 8960 "---\n" 8961 "Language: JavaScript\n" 8962 "IndentWidth: 111\n" 8963 "TabWidth: 111\n" 8964 "---\n" 8965 "Language: Cpp\n" 8966 "BreakBeforeBraces: Stroustrup\n" 8967 "TabWidth: 789\n" 8968 "...\n", 8969 &Style)); 8970 EXPECT_EQ(123u, Style.ColumnLimit); 8971 EXPECT_EQ(456u, Style.IndentWidth); 8972 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 8973 EXPECT_EQ(789u, Style.TabWidth); 8974 8975 EXPECT_EQ(parseConfiguration("---\n" 8976 "Language: JavaScript\n" 8977 "IndentWidth: 56\n" 8978 "---\n" 8979 "IndentWidth: 78\n" 8980 "...\n", 8981 &Style), 8982 ParseError::Error); 8983 EXPECT_EQ(parseConfiguration("---\n" 8984 "Language: JavaScript\n" 8985 "IndentWidth: 56\n" 8986 "---\n" 8987 "Language: JavaScript\n" 8988 "IndentWidth: 78\n" 8989 "...\n", 8990 &Style), 8991 ParseError::Error); 8992 8993 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 8994 } 8995 8996 #undef CHECK_PARSE 8997 8998 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 8999 FormatStyle Style = {}; 9000 Style.Language = FormatStyle::LK_JavaScript; 9001 Style.BreakBeforeTernaryOperators = true; 9002 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9003 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9004 9005 Style.BreakBeforeTernaryOperators = true; 9006 EXPECT_EQ(0, parseConfiguration("---\n" 9007 "BasedOnStyle: Google\n" 9008 "---\n" 9009 "Language: JavaScript\n" 9010 "IndentWidth: 76\n" 9011 "...\n", 9012 &Style) 9013 .value()); 9014 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9015 EXPECT_EQ(76u, Style.IndentWidth); 9016 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9017 } 9018 9019 TEST_F(FormatTest, ConfigurationRoundTripTest) { 9020 FormatStyle Style = getLLVMStyle(); 9021 std::string YAML = configurationAsText(Style); 9022 FormatStyle ParsedStyle = {}; 9023 ParsedStyle.Language = FormatStyle::LK_Cpp; 9024 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 9025 EXPECT_EQ(Style, ParsedStyle); 9026 } 9027 9028 TEST_F(FormatTest, WorksFor8bitEncodings) { 9029 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 9030 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 9031 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 9032 "\"\xef\xee\xf0\xf3...\"", 9033 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 9034 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 9035 "\xef\xee\xf0\xf3...\"", 9036 getLLVMStyleWithColumns(12))); 9037 } 9038 9039 TEST_F(FormatTest, HandlesUTF8BOM) { 9040 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 9041 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 9042 format("\xef\xbb\xbf#include <iostream>")); 9043 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 9044 format("\xef\xbb\xbf\n#include <iostream>")); 9045 } 9046 9047 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 9048 #if !defined(_MSC_VER) 9049 9050 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 9051 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 9052 getLLVMStyleWithColumns(35)); 9053 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 9054 getLLVMStyleWithColumns(31)); 9055 verifyFormat("// Однажды в студёную зимнюю пору...", 9056 getLLVMStyleWithColumns(36)); 9057 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 9058 verifyFormat("/* Однажды в студёную зимнюю пору... */", 9059 getLLVMStyleWithColumns(39)); 9060 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 9061 getLLVMStyleWithColumns(35)); 9062 } 9063 9064 TEST_F(FormatTest, SplitsUTF8Strings) { 9065 // Non-printable characters' width is currently considered to be the length in 9066 // bytes in UTF8. The characters can be displayed in very different manner 9067 // (zero-width, single width with a substitution glyph, expanded to their code 9068 // (e.g. "<8d>"), so there's no single correct way to handle them. 9069 EXPECT_EQ("\"aaaaÄ\"\n" 9070 "\"\xc2\x8d\";", 9071 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9072 EXPECT_EQ("\"aaaaaaaÄ\"\n" 9073 "\"\xc2\x8d\";", 9074 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9075 EXPECT_EQ("\"Однажды, в \"\n" 9076 "\"студёную \"\n" 9077 "\"зимнюю \"\n" 9078 "\"пору,\"", 9079 format("\"Однажды, в студёную зимнюю пору,\"", 9080 getLLVMStyleWithColumns(13))); 9081 EXPECT_EQ( 9082 "\"一 二 三 \"\n" 9083 "\"四 五六 \"\n" 9084 "\"七 八 九 \"\n" 9085 "\"十\"", 9086 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 9087 EXPECT_EQ("\"一\t二 \"\n" 9088 "\"\t三 \"\n" 9089 "\"四 五\t六 \"\n" 9090 "\"\t七 \"\n" 9091 "\"八九十\tqq\"", 9092 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 9093 getLLVMStyleWithColumns(11))); 9094 9095 // UTF8 character in an escape sequence. 9096 EXPECT_EQ("\"aaaaaa\"\n" 9097 "\"\\\xC2\x8D\"", 9098 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 9099 } 9100 9101 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 9102 EXPECT_EQ("const char *sssss =\n" 9103 " \"一二三四五六七八\\\n" 9104 " 九 十\";", 9105 format("const char *sssss = \"一二三四五六七八\\\n" 9106 " 九 十\";", 9107 getLLVMStyleWithColumns(30))); 9108 } 9109 9110 TEST_F(FormatTest, SplitsUTF8LineComments) { 9111 EXPECT_EQ("// aaaaÄ\xc2\x8d", 9112 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 9113 EXPECT_EQ("// Я из лесу\n" 9114 "// вышел; был\n" 9115 "// сильный\n" 9116 "// мороз.", 9117 format("// Я из лесу вышел; был сильный мороз.", 9118 getLLVMStyleWithColumns(13))); 9119 EXPECT_EQ("// 一二三\n" 9120 "// 四五六七\n" 9121 "// 八 九\n" 9122 "// 十", 9123 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 9124 } 9125 9126 TEST_F(FormatTest, SplitsUTF8BlockComments) { 9127 EXPECT_EQ("/* Гляжу,\n" 9128 " * поднимается\n" 9129 " * медленно в\n" 9130 " * гору\n" 9131 " * Лошадка,\n" 9132 " * везущая\n" 9133 " * хворосту\n" 9134 " * воз. */", 9135 format("/* Гляжу, поднимается медленно в гору\n" 9136 " * Лошадка, везущая хворосту воз. */", 9137 getLLVMStyleWithColumns(13))); 9138 EXPECT_EQ( 9139 "/* 一二三\n" 9140 " * 四五六七\n" 9141 " * 八 九\n" 9142 " * 十 */", 9143 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 9144 EXPECT_EQ("/* \n" 9145 " * \n" 9146 " * - */", 9147 format("/* - */", getLLVMStyleWithColumns(12))); 9148 } 9149 9150 #endif // _MSC_VER 9151 9152 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 9153 FormatStyle Style = getLLVMStyle(); 9154 9155 Style.ConstructorInitializerIndentWidth = 4; 9156 verifyFormat( 9157 "SomeClass::Constructor()\n" 9158 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9159 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9160 Style); 9161 9162 Style.ConstructorInitializerIndentWidth = 2; 9163 verifyFormat( 9164 "SomeClass::Constructor()\n" 9165 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9166 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9167 Style); 9168 9169 Style.ConstructorInitializerIndentWidth = 0; 9170 verifyFormat( 9171 "SomeClass::Constructor()\n" 9172 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9173 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9174 Style); 9175 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9176 verifyFormat( 9177 "SomeLongTemplateVariableName<\n" 9178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 9179 Style); 9180 verifyFormat( 9181 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 9182 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9183 Style); 9184 } 9185 9186 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 9187 FormatStyle Style = getLLVMStyle(); 9188 Style.BreakConstructorInitializersBeforeComma = true; 9189 Style.ConstructorInitializerIndentWidth = 4; 9190 verifyFormat("SomeClass::Constructor()\n" 9191 " : a(a)\n" 9192 " , b(b)\n" 9193 " , c(c) {}", 9194 Style); 9195 verifyFormat("SomeClass::Constructor()\n" 9196 " : a(a) {}", 9197 Style); 9198 9199 Style.ColumnLimit = 0; 9200 verifyFormat("SomeClass::Constructor()\n" 9201 " : a(a) {}", 9202 Style); 9203 verifyFormat("SomeClass::Constructor() noexcept\n" 9204 " : a(a) {}", 9205 Style); 9206 verifyFormat("SomeClass::Constructor()\n" 9207 " : a(a)\n" 9208 " , b(b)\n" 9209 " , c(c) {}", 9210 Style); 9211 verifyFormat("SomeClass::Constructor()\n" 9212 " : a(a) {\n" 9213 " foo();\n" 9214 " bar();\n" 9215 "}", 9216 Style); 9217 9218 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9219 verifyFormat("SomeClass::Constructor()\n" 9220 " : a(a)\n" 9221 " , b(b)\n" 9222 " , c(c) {\n}", 9223 Style); 9224 verifyFormat("SomeClass::Constructor()\n" 9225 " : a(a) {\n}", 9226 Style); 9227 9228 Style.ColumnLimit = 80; 9229 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 9230 Style.ConstructorInitializerIndentWidth = 2; 9231 verifyFormat("SomeClass::Constructor()\n" 9232 " : a(a)\n" 9233 " , b(b)\n" 9234 " , c(c) {}", 9235 Style); 9236 9237 Style.ConstructorInitializerIndentWidth = 0; 9238 verifyFormat("SomeClass::Constructor()\n" 9239 ": a(a)\n" 9240 ", b(b)\n" 9241 ", c(c) {}", 9242 Style); 9243 9244 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 9245 Style.ConstructorInitializerIndentWidth = 4; 9246 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 9247 verifyFormat( 9248 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 9249 Style); 9250 verifyFormat( 9251 "SomeClass::Constructor()\n" 9252 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 9253 Style); 9254 Style.ConstructorInitializerIndentWidth = 4; 9255 Style.ColumnLimit = 60; 9256 verifyFormat("SomeClass::Constructor()\n" 9257 " : aaaaaaaa(aaaaaaaa)\n" 9258 " , aaaaaaaa(aaaaaaaa)\n" 9259 " , aaaaaaaa(aaaaaaaa) {}", 9260 Style); 9261 } 9262 9263 TEST_F(FormatTest, Destructors) { 9264 verifyFormat("void F(int &i) { i.~int(); }"); 9265 verifyFormat("void F(int &i) { i->~int(); }"); 9266 } 9267 9268 TEST_F(FormatTest, FormatsWithWebKitStyle) { 9269 FormatStyle Style = getWebKitStyle(); 9270 9271 // Don't indent in outer namespaces. 9272 verifyFormat("namespace outer {\n" 9273 "int i;\n" 9274 "namespace inner {\n" 9275 " int i;\n" 9276 "} // namespace inner\n" 9277 "} // namespace outer\n" 9278 "namespace other_outer {\n" 9279 "int i;\n" 9280 "}", 9281 Style); 9282 9283 // Don't indent case labels. 9284 verifyFormat("switch (variable) {\n" 9285 "case 1:\n" 9286 "case 2:\n" 9287 " doSomething();\n" 9288 " break;\n" 9289 "default:\n" 9290 " ++variable;\n" 9291 "}", 9292 Style); 9293 9294 // Wrap before binary operators. 9295 EXPECT_EQ("void f()\n" 9296 "{\n" 9297 " if (aaaaaaaaaaaaaaaa\n" 9298 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 9299 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 9300 " return;\n" 9301 "}", 9302 format("void f() {\n" 9303 "if (aaaaaaaaaaaaaaaa\n" 9304 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 9305 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 9306 "return;\n" 9307 "}", 9308 Style)); 9309 9310 // Allow functions on a single line. 9311 verifyFormat("void f() { return; }", Style); 9312 9313 // Constructor initializers are formatted one per line with the "," on the 9314 // new line. 9315 verifyFormat("Constructor()\n" 9316 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9317 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 9318 " aaaaaaaaaaaaaa)\n" 9319 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 9320 "{\n" 9321 "}", 9322 Style); 9323 verifyFormat("SomeClass::Constructor()\n" 9324 " : a(a)\n" 9325 "{\n" 9326 "}", 9327 Style); 9328 EXPECT_EQ("SomeClass::Constructor()\n" 9329 " : a(a)\n" 9330 "{\n" 9331 "}", 9332 format("SomeClass::Constructor():a(a){}", Style)); 9333 verifyFormat("SomeClass::Constructor()\n" 9334 " : a(a)\n" 9335 " , b(b)\n" 9336 " , c(c)\n" 9337 "{\n" 9338 "}", 9339 Style); 9340 verifyFormat("SomeClass::Constructor()\n" 9341 " : a(a)\n" 9342 "{\n" 9343 " foo();\n" 9344 " bar();\n" 9345 "}", 9346 Style); 9347 9348 // Access specifiers should be aligned left. 9349 verifyFormat("class C {\n" 9350 "public:\n" 9351 " int i;\n" 9352 "};", 9353 Style); 9354 9355 // Do not align comments. 9356 verifyFormat("int a; // Do not\n" 9357 "double b; // align comments.", 9358 Style); 9359 9360 // Do not align operands. 9361 EXPECT_EQ("ASSERT(aaaa\n" 9362 " || bbbb);", 9363 format("ASSERT ( aaaa\n||bbbb);", Style)); 9364 9365 // Accept input's line breaks. 9366 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 9367 " || bbbbbbbbbbbbbbb) {\n" 9368 " i++;\n" 9369 "}", 9370 format("if (aaaaaaaaaaaaaaa\n" 9371 "|| bbbbbbbbbbbbbbb) { i++; }", 9372 Style)); 9373 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 9374 " i++;\n" 9375 "}", 9376 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 9377 9378 // Don't automatically break all macro definitions (llvm.org/PR17842). 9379 verifyFormat("#define aNumber 10", Style); 9380 // However, generally keep the line breaks that the user authored. 9381 EXPECT_EQ("#define aNumber \\\n" 9382 " 10", 9383 format("#define aNumber \\\n" 9384 " 10", 9385 Style)); 9386 9387 // Keep empty and one-element array literals on a single line. 9388 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 9389 " copyItems:YES];", 9390 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 9391 "copyItems:YES];", 9392 Style)); 9393 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 9394 " copyItems:YES];", 9395 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 9396 " copyItems:YES];", 9397 Style)); 9398 // FIXME: This does not seem right, there should be more indentation before 9399 // the array literal's entries. Nested blocks have the same problem. 9400 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 9401 " @\"a\",\n" 9402 " @\"a\"\n" 9403 "]\n" 9404 " copyItems:YES];", 9405 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 9406 " @\"a\",\n" 9407 " @\"a\"\n" 9408 " ]\n" 9409 " copyItems:YES];", 9410 Style)); 9411 EXPECT_EQ( 9412 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 9413 " copyItems:YES];", 9414 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 9415 " copyItems:YES];", 9416 Style)); 9417 9418 verifyFormat("[self.a b:c c:d];", Style); 9419 EXPECT_EQ("[self.a b:c\n" 9420 " c:d];", 9421 format("[self.a b:c\n" 9422 "c:d];", 9423 Style)); 9424 } 9425 9426 TEST_F(FormatTest, FormatsLambdas) { 9427 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 9428 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 9429 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 9430 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 9431 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 9432 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 9433 verifyFormat("int x = f(*+[] {});"); 9434 verifyFormat("void f() {\n" 9435 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 9436 "}\n"); 9437 verifyFormat("void f() {\n" 9438 " other(x.begin(), //\n" 9439 " x.end(), //\n" 9440 " [&](int, int) { return 1; });\n" 9441 "}\n"); 9442 verifyFormat("SomeFunction([]() { // A cool function...\n" 9443 " return 43;\n" 9444 "});"); 9445 EXPECT_EQ("SomeFunction([]() {\n" 9446 "#define A a\n" 9447 " return 43;\n" 9448 "});", 9449 format("SomeFunction([](){\n" 9450 "#define A a\n" 9451 "return 43;\n" 9452 "});")); 9453 verifyFormat("void f() {\n" 9454 " SomeFunction([](decltype(x), A *a) {});\n" 9455 "}"); 9456 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9457 " [](const aaaaaaaaaa &a) { return a; });"); 9458 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 9459 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 9460 "});"); 9461 verifyFormat("Constructor()\n" 9462 " : Field([] { // comment\n" 9463 " int i;\n" 9464 " }) {}"); 9465 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 9466 " return some_parameter.size();\n" 9467 "};"); 9468 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 9469 " [](const string &s) { return s; };"); 9470 verifyFormat("int i = aaaaaa ? 1 //\n" 9471 " : [] {\n" 9472 " return 2; //\n" 9473 " }();"); 9474 verifyFormat("llvm::errs() << \"number of twos is \"\n" 9475 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 9476 " return x == 2; // force break\n" 9477 " });"); 9478 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9479 " [=](int iiiiiiiiiiii) {\n" 9480 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 9481 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 9482 " });", 9483 getLLVMStyleWithColumns(60)); 9484 verifyFormat("SomeFunction({[&] {\n" 9485 " // comment\n" 9486 " },\n" 9487 " [&] {\n" 9488 " // comment\n" 9489 " }});"); 9490 verifyFormat("SomeFunction({[&] {\n" 9491 " // comment\n" 9492 "}});"); 9493 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 9494 " [&]() { return true; },\n" 9495 " aaaaa aaaaaaaaa);"); 9496 9497 // Lambdas with return types. 9498 verifyFormat("int c = []() -> int { return 2; }();\n"); 9499 verifyFormat("int c = []() -> int * { return 2; }();\n"); 9500 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 9501 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 9502 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 9503 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 9504 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 9505 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 9506 verifyFormat("[a, a]() -> a<1> {};"); 9507 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 9508 " int j) -> int {\n" 9509 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 9510 "};"); 9511 verifyFormat( 9512 "aaaaaaaaaaaaaaaaaaaaaa(\n" 9513 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 9514 " return aaaaaaaaaaaaaaaaa;\n" 9515 " });", 9516 getLLVMStyleWithColumns(70)); 9517 verifyFormat("[]() //\n" 9518 " -> int {\n" 9519 " return 1; //\n" 9520 "};"); 9521 9522 // Multiple lambdas in the same parentheses change indentation rules. 9523 verifyFormat("SomeFunction(\n" 9524 " []() {\n" 9525 " int i = 42;\n" 9526 " return i;\n" 9527 " },\n" 9528 " []() {\n" 9529 " int j = 43;\n" 9530 " return j;\n" 9531 " });"); 9532 9533 // More complex introducers. 9534 verifyFormat("return [i, args...] {};"); 9535 9536 // Not lambdas. 9537 verifyFormat("constexpr char hello[]{\"hello\"};"); 9538 verifyFormat("double &operator[](int i) { return 0; }\n" 9539 "int i;"); 9540 verifyFormat("std::unique_ptr<int[]> foo() {}"); 9541 verifyFormat("int i = a[a][a]->f();"); 9542 verifyFormat("int i = (*b)[a]->f();"); 9543 9544 // Other corner cases. 9545 verifyFormat("void f() {\n" 9546 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 9547 " );\n" 9548 "}"); 9549 9550 // Lambdas created through weird macros. 9551 verifyFormat("void f() {\n" 9552 " MACRO((const AA &a) { return 1; });\n" 9553 " MACRO((AA &a) { return 1; });\n" 9554 "}"); 9555 9556 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 9557 " doo_dah();\n" 9558 " doo_dah();\n" 9559 " })) {\n" 9560 "}"); 9561 verifyFormat("auto lambda = []() {\n" 9562 " int a = 2\n" 9563 "#if A\n" 9564 " + 2\n" 9565 "#endif\n" 9566 " ;\n" 9567 "};"); 9568 9569 // Lambdas with complex multiline introducers. 9570 verifyFormat( 9571 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9572 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 9573 " -> ::std::unordered_set<\n" 9574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 9575 " //\n" 9576 " });"); 9577 } 9578 9579 TEST_F(FormatTest, FormatsBlocks) { 9580 FormatStyle ShortBlocks = getLLVMStyle(); 9581 ShortBlocks.AllowShortBlocksOnASingleLine = true; 9582 verifyFormat("int (^Block)(int, int);", ShortBlocks); 9583 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 9584 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 9585 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 9586 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 9587 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 9588 9589 verifyFormat("foo(^{ bar(); });", ShortBlocks); 9590 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 9591 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 9592 9593 verifyFormat("[operation setCompletionBlock:^{\n" 9594 " [self onOperationDone];\n" 9595 "}];"); 9596 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 9597 " [self onOperationDone];\n" 9598 "}]};"); 9599 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 9600 " f();\n" 9601 "}];"); 9602 verifyFormat("int a = [operation block:^int(int *i) {\n" 9603 " return 1;\n" 9604 "}];"); 9605 verifyFormat("[myObject doSomethingWith:arg1\n" 9606 " aaa:^int(int *a) {\n" 9607 " return 1;\n" 9608 " }\n" 9609 " bbb:f(a * bbbbbbbb)];"); 9610 9611 verifyFormat("[operation setCompletionBlock:^{\n" 9612 " [self.delegate newDataAvailable];\n" 9613 "}];", 9614 getLLVMStyleWithColumns(60)); 9615 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 9616 " NSString *path = [self sessionFilePath];\n" 9617 " if (path) {\n" 9618 " // ...\n" 9619 " }\n" 9620 "});"); 9621 verifyFormat("[[SessionService sharedService]\n" 9622 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 9623 " if (window) {\n" 9624 " [self windowDidLoad:window];\n" 9625 " } else {\n" 9626 " [self errorLoadingWindow];\n" 9627 " }\n" 9628 " }];"); 9629 verifyFormat("void (^largeBlock)(void) = ^{\n" 9630 " // ...\n" 9631 "};\n", 9632 getLLVMStyleWithColumns(40)); 9633 verifyFormat("[[SessionService sharedService]\n" 9634 " loadWindowWithCompletionBlock: //\n" 9635 " ^(SessionWindow *window) {\n" 9636 " if (window) {\n" 9637 " [self windowDidLoad:window];\n" 9638 " } else {\n" 9639 " [self errorLoadingWindow];\n" 9640 " }\n" 9641 " }];", 9642 getLLVMStyleWithColumns(60)); 9643 verifyFormat("[myObject doSomethingWith:arg1\n" 9644 " firstBlock:^(Foo *a) {\n" 9645 " // ...\n" 9646 " int i;\n" 9647 " }\n" 9648 " secondBlock:^(Bar *b) {\n" 9649 " // ...\n" 9650 " int i;\n" 9651 " }\n" 9652 " thirdBlock:^Foo(Bar *b) {\n" 9653 " // ...\n" 9654 " int i;\n" 9655 " }];"); 9656 verifyFormat("[myObject doSomethingWith:arg1\n" 9657 " firstBlock:-1\n" 9658 " secondBlock:^(Bar *b) {\n" 9659 " // ...\n" 9660 " int i;\n" 9661 " }];"); 9662 9663 verifyFormat("f(^{\n" 9664 " @autoreleasepool {\n" 9665 " if (a) {\n" 9666 " g();\n" 9667 " }\n" 9668 " }\n" 9669 "});"); 9670 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 9671 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 9672 "};"); 9673 9674 FormatStyle FourIndent = getLLVMStyle(); 9675 FourIndent.ObjCBlockIndentWidth = 4; 9676 verifyFormat("[operation setCompletionBlock:^{\n" 9677 " [self onOperationDone];\n" 9678 "}];", 9679 FourIndent); 9680 } 9681 9682 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 9683 FormatStyle ZeroColumn = getLLVMStyle(); 9684 ZeroColumn.ColumnLimit = 0; 9685 9686 verifyFormat("[[SessionService sharedService] " 9687 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 9688 " if (window) {\n" 9689 " [self windowDidLoad:window];\n" 9690 " } else {\n" 9691 " [self errorLoadingWindow];\n" 9692 " }\n" 9693 "}];", 9694 ZeroColumn); 9695 EXPECT_EQ("[[SessionService sharedService]\n" 9696 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 9697 " if (window) {\n" 9698 " [self windowDidLoad:window];\n" 9699 " } else {\n" 9700 " [self errorLoadingWindow];\n" 9701 " }\n" 9702 " }];", 9703 format("[[SessionService sharedService]\n" 9704 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 9705 " if (window) {\n" 9706 " [self windowDidLoad:window];\n" 9707 " } else {\n" 9708 " [self errorLoadingWindow];\n" 9709 " }\n" 9710 "}];", 9711 ZeroColumn)); 9712 verifyFormat("[myObject doSomethingWith:arg1\n" 9713 " firstBlock:^(Foo *a) {\n" 9714 " // ...\n" 9715 " int i;\n" 9716 " }\n" 9717 " secondBlock:^(Bar *b) {\n" 9718 " // ...\n" 9719 " int i;\n" 9720 " }\n" 9721 " thirdBlock:^Foo(Bar *b) {\n" 9722 " // ...\n" 9723 " int i;\n" 9724 " }];", 9725 ZeroColumn); 9726 verifyFormat("f(^{\n" 9727 " @autoreleasepool {\n" 9728 " if (a) {\n" 9729 " g();\n" 9730 " }\n" 9731 " }\n" 9732 "});", 9733 ZeroColumn); 9734 verifyFormat("void (^largeBlock)(void) = ^{\n" 9735 " // ...\n" 9736 "};", 9737 ZeroColumn); 9738 9739 ZeroColumn.AllowShortBlocksOnASingleLine = true; 9740 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 9741 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 9742 ZeroColumn.AllowShortBlocksOnASingleLine = false; 9743 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 9744 " int i;\n" 9745 "};", 9746 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 9747 } 9748 9749 TEST_F(FormatTest, SupportsCRLF) { 9750 EXPECT_EQ("int a;\r\n" 9751 "int b;\r\n" 9752 "int c;\r\n", 9753 format("int a;\r\n" 9754 " int b;\r\n" 9755 " int c;\r\n", 9756 getLLVMStyle())); 9757 EXPECT_EQ("int a;\r\n" 9758 "int b;\r\n" 9759 "int c;\r\n", 9760 format("int a;\r\n" 9761 " int b;\n" 9762 " int c;\r\n", 9763 getLLVMStyle())); 9764 EXPECT_EQ("int a;\n" 9765 "int b;\n" 9766 "int c;\n", 9767 format("int a;\r\n" 9768 " int b;\n" 9769 " int c;\n", 9770 getLLVMStyle())); 9771 EXPECT_EQ("\"aaaaaaa \"\r\n" 9772 "\"bbbbbbb\";\r\n", 9773 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 9774 EXPECT_EQ("#define A \\\r\n" 9775 " b; \\\r\n" 9776 " c; \\\r\n" 9777 " d;\r\n", 9778 format("#define A \\\r\n" 9779 " b; \\\r\n" 9780 " c; d; \r\n", 9781 getGoogleStyle())); 9782 9783 EXPECT_EQ("/*\r\n" 9784 "multi line block comments\r\n" 9785 "should not introduce\r\n" 9786 "an extra carriage return\r\n" 9787 "*/\r\n", 9788 format("/*\r\n" 9789 "multi line block comments\r\n" 9790 "should not introduce\r\n" 9791 "an extra carriage return\r\n" 9792 "*/\r\n")); 9793 } 9794 9795 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 9796 verifyFormat("MY_CLASS(C) {\n" 9797 " int i;\n" 9798 " int j;\n" 9799 "};"); 9800 } 9801 9802 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 9803 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 9804 TwoIndent.ContinuationIndentWidth = 2; 9805 9806 EXPECT_EQ("int i =\n" 9807 " longFunction(\n" 9808 " arg);", 9809 format("int i = longFunction(arg);", TwoIndent)); 9810 9811 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 9812 SixIndent.ContinuationIndentWidth = 6; 9813 9814 EXPECT_EQ("int i =\n" 9815 " longFunction(\n" 9816 " arg);", 9817 format("int i = longFunction(arg);", SixIndent)); 9818 } 9819 9820 TEST_F(FormatTest, SpacesInAngles) { 9821 FormatStyle Spaces = getLLVMStyle(); 9822 Spaces.SpacesInAngles = true; 9823 9824 verifyFormat("static_cast< int >(arg);", Spaces); 9825 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 9826 verifyFormat("f< int, float >();", Spaces); 9827 verifyFormat("template <> g() {}", Spaces); 9828 verifyFormat("template < std::vector< int > > f() {}", Spaces); 9829 verifyFormat("std::function< void(int, int) > fct;", Spaces); 9830 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 9831 Spaces); 9832 9833 Spaces.Standard = FormatStyle::LS_Cpp03; 9834 Spaces.SpacesInAngles = true; 9835 verifyFormat("A< A< int > >();", Spaces); 9836 9837 Spaces.SpacesInAngles = false; 9838 verifyFormat("A<A<int> >();", Spaces); 9839 9840 Spaces.Standard = FormatStyle::LS_Cpp11; 9841 Spaces.SpacesInAngles = true; 9842 verifyFormat("A< A< int > >();", Spaces); 9843 9844 Spaces.SpacesInAngles = false; 9845 verifyFormat("A<A<int>>();", Spaces); 9846 } 9847 9848 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 9849 FormatStyle Style = getLLVMStyle(); 9850 Style.SpaceAfterTemplateKeyword = false; 9851 verifyFormat("template<int> void foo();", Style); 9852 } 9853 9854 TEST_F(FormatTest, TripleAngleBrackets) { 9855 verifyFormat("f<<<1, 1>>>();"); 9856 verifyFormat("f<<<1, 1, 1, s>>>();"); 9857 verifyFormat("f<<<a, b, c, d>>>();"); 9858 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 9859 verifyFormat("f<param><<<1, 1>>>();"); 9860 verifyFormat("f<1><<<1, 1>>>();"); 9861 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 9862 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 9863 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 9864 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 9865 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 9866 } 9867 9868 TEST_F(FormatTest, MergeLessLessAtEnd) { 9869 verifyFormat("<<"); 9870 EXPECT_EQ("< < <", format("\\\n<<<")); 9871 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 9872 "aaallvm::outs() <<"); 9873 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 9874 "aaaallvm::outs()\n <<"); 9875 } 9876 9877 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 9878 std::string code = "#if A\n" 9879 "#if B\n" 9880 "a.\n" 9881 "#endif\n" 9882 " a = 1;\n" 9883 "#else\n" 9884 "#endif\n" 9885 "#if C\n" 9886 "#else\n" 9887 "#endif\n"; 9888 EXPECT_EQ(code, format(code)); 9889 } 9890 9891 TEST_F(FormatTest, HandleConflictMarkers) { 9892 // Git/SVN conflict markers. 9893 EXPECT_EQ("int a;\n" 9894 "void f() {\n" 9895 " callme(some(parameter1,\n" 9896 "<<<<<<< text by the vcs\n" 9897 " parameter2),\n" 9898 "||||||| text by the vcs\n" 9899 " parameter2),\n" 9900 " parameter3,\n" 9901 "======= text by the vcs\n" 9902 " parameter2, parameter3),\n" 9903 ">>>>>>> text by the vcs\n" 9904 " otherparameter);\n", 9905 format("int a;\n" 9906 "void f() {\n" 9907 " callme(some(parameter1,\n" 9908 "<<<<<<< text by the vcs\n" 9909 " parameter2),\n" 9910 "||||||| text by the vcs\n" 9911 " parameter2),\n" 9912 " parameter3,\n" 9913 "======= text by the vcs\n" 9914 " parameter2,\n" 9915 " parameter3),\n" 9916 ">>>>>>> text by the vcs\n" 9917 " otherparameter);\n")); 9918 9919 // Perforce markers. 9920 EXPECT_EQ("void f() {\n" 9921 " function(\n" 9922 ">>>> text by the vcs\n" 9923 " parameter,\n" 9924 "==== text by the vcs\n" 9925 " parameter,\n" 9926 "==== text by the vcs\n" 9927 " parameter,\n" 9928 "<<<< text by the vcs\n" 9929 " parameter);\n", 9930 format("void f() {\n" 9931 " function(\n" 9932 ">>>> text by the vcs\n" 9933 " parameter,\n" 9934 "==== text by the vcs\n" 9935 " parameter,\n" 9936 "==== text by the vcs\n" 9937 " parameter,\n" 9938 "<<<< text by the vcs\n" 9939 " parameter);\n")); 9940 9941 EXPECT_EQ("<<<<<<<\n" 9942 "|||||||\n" 9943 "=======\n" 9944 ">>>>>>>", 9945 format("<<<<<<<\n" 9946 "|||||||\n" 9947 "=======\n" 9948 ">>>>>>>")); 9949 9950 EXPECT_EQ("<<<<<<<\n" 9951 "|||||||\n" 9952 "int i;\n" 9953 "=======\n" 9954 ">>>>>>>", 9955 format("<<<<<<<\n" 9956 "|||||||\n" 9957 "int i;\n" 9958 "=======\n" 9959 ">>>>>>>")); 9960 9961 // FIXME: Handle parsing of macros around conflict markers correctly: 9962 EXPECT_EQ("#define Macro \\\n" 9963 "<<<<<<<\n" 9964 "Something \\\n" 9965 "|||||||\n" 9966 "Else \\\n" 9967 "=======\n" 9968 "Other \\\n" 9969 ">>>>>>>\n" 9970 " End int i;\n", 9971 format("#define Macro \\\n" 9972 "<<<<<<<\n" 9973 " Something \\\n" 9974 "|||||||\n" 9975 " Else \\\n" 9976 "=======\n" 9977 " Other \\\n" 9978 ">>>>>>>\n" 9979 " End\n" 9980 "int i;\n")); 9981 } 9982 9983 TEST_F(FormatTest, DisableRegions) { 9984 EXPECT_EQ("int i;\n" 9985 "// clang-format off\n" 9986 " int j;\n" 9987 "// clang-format on\n" 9988 "int k;", 9989 format(" int i;\n" 9990 " // clang-format off\n" 9991 " int j;\n" 9992 " // clang-format on\n" 9993 " int k;")); 9994 EXPECT_EQ("int i;\n" 9995 "/* clang-format off */\n" 9996 " int j;\n" 9997 "/* clang-format on */\n" 9998 "int k;", 9999 format(" int i;\n" 10000 " /* clang-format off */\n" 10001 " int j;\n" 10002 " /* clang-format on */\n" 10003 " int k;")); 10004 10005 // Don't reflow comments within disabled regions. 10006 EXPECT_EQ( 10007 "// clang-format off\n" 10008 "// long long long long long long line\n" 10009 "/* clang-format on */\n" 10010 "/* long long long\n" 10011 " * long long long\n" 10012 " * line */\n" 10013 "int i;\n" 10014 "/* clang-format off */\n" 10015 "/* long long long long long long line */\n", 10016 format("// clang-format off\n" 10017 "// long long long long long long line\n" 10018 "/* clang-format on */\n" 10019 "/* long long long long long long line */\n" 10020 "int i;\n" 10021 "/* clang-format off */\n" 10022 "/* long long long long long long line */\n", 10023 getLLVMStyleWithColumns(20))); 10024 } 10025 10026 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 10027 format("? ) ="); 10028 verifyNoCrash("#define a\\\n /**/}"); 10029 } 10030 10031 TEST_F(FormatTest, FormatsTableGenCode) { 10032 FormatStyle Style = getLLVMStyle(); 10033 Style.Language = FormatStyle::LK_TableGen; 10034 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 10035 } 10036 10037 TEST_F(FormatTest, ArrayOfTemplates) { 10038 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 10039 format("auto a = new unique_ptr<int > [ 10];")); 10040 10041 FormatStyle Spaces = getLLVMStyle(); 10042 Spaces.SpacesInSquareBrackets = true; 10043 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 10044 format("auto a = new unique_ptr<int > [10];", Spaces)); 10045 } 10046 10047 TEST_F(FormatTest, ArrayAsTemplateType) { 10048 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 10049 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 10050 10051 FormatStyle Spaces = getLLVMStyle(); 10052 Spaces.SpacesInSquareBrackets = true; 10053 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 10054 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 10055 } 10056 10057 TEST_F(FormatTest, NoSpaceAfterSuper) { 10058 verifyFormat("__super::FooBar();"); 10059 } 10060 10061 TEST(FormatStyle, GetStyleOfFile) { 10062 vfs::InMemoryFileSystem FS; 10063 // Test 1: format file in the same directory. 10064 ASSERT_TRUE( 10065 FS.addFile("/a/.clang-format", 0, 10066 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 10067 ASSERT_TRUE( 10068 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10069 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 10070 ASSERT_TRUE((bool)Style1); 10071 ASSERT_EQ(*Style1, getLLVMStyle()); 10072 10073 // Test 2.1: fallback to default. 10074 ASSERT_TRUE( 10075 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10076 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 10077 ASSERT_TRUE((bool)Style2); 10078 ASSERT_EQ(*Style2, getMozillaStyle()); 10079 10080 // Test 2.2: no format on 'none' fallback style. 10081 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 10082 ASSERT_TRUE((bool)Style2); 10083 ASSERT_EQ(*Style2, getNoStyle()); 10084 10085 // Test 2.3: format if config is found with no based style while fallback is 10086 // 'none'. 10087 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 10088 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 10089 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 10090 ASSERT_TRUE((bool)Style2); 10091 ASSERT_EQ(*Style2, getLLVMStyle()); 10092 10093 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 10094 Style2 = getStyle("{}", "a.h", "none", "", &FS); 10095 ASSERT_TRUE((bool)Style2); 10096 ASSERT_EQ(*Style2, getLLVMStyle()); 10097 10098 // Test 3: format file in parent directory. 10099 ASSERT_TRUE( 10100 FS.addFile("/c/.clang-format", 0, 10101 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 10102 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 10103 llvm::MemoryBuffer::getMemBuffer("int i;"))); 10104 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 10105 ASSERT_TRUE((bool)Style3); 10106 ASSERT_EQ(*Style3, getGoogleStyle()); 10107 10108 // Test 4: error on invalid fallback style 10109 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 10110 ASSERT_FALSE((bool)Style4); 10111 llvm::consumeError(Style4.takeError()); 10112 10113 // Test 5: error on invalid yaml on command line 10114 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 10115 ASSERT_FALSE((bool)Style5); 10116 llvm::consumeError(Style5.takeError()); 10117 10118 // Test 6: error on invalid style 10119 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 10120 ASSERT_FALSE((bool)Style6); 10121 llvm::consumeError(Style6.takeError()); 10122 10123 // Test 7: found config file, error on parsing it 10124 ASSERT_TRUE( 10125 FS.addFile("/d/.clang-format", 0, 10126 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 10127 "InvalidKey: InvalidValue"))); 10128 ASSERT_TRUE( 10129 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10130 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 10131 ASSERT_FALSE((bool)Style7); 10132 llvm::consumeError(Style7.takeError()); 10133 } 10134 10135 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 10136 // Column limit is 20. 10137 std::string Code = "Type *a =\n" 10138 " new Type();\n" 10139 "g(iiiii, 0, jjjjj,\n" 10140 " 0, kkkkk, 0, mm);\n" 10141 "int bad = format ;"; 10142 std::string Expected = "auto a = new Type();\n" 10143 "g(iiiii, nullptr,\n" 10144 " jjjjj, nullptr,\n" 10145 " kkkkk, nullptr,\n" 10146 " mm);\n" 10147 "int bad = format ;"; 10148 FileID ID = Context.createInMemoryFile("format.cpp", Code); 10149 tooling::Replacements Replaces = toReplacements( 10150 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 10151 "auto "), 10152 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 10153 "nullptr"), 10154 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 10155 "nullptr"), 10156 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 10157 "nullptr")}); 10158 10159 format::FormatStyle Style = format::getLLVMStyle(); 10160 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 10161 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 10162 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 10163 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 10164 auto Result = applyAllReplacements(Code, *FormattedReplaces); 10165 EXPECT_TRUE(static_cast<bool>(Result)); 10166 EXPECT_EQ(Expected, *Result); 10167 } 10168 10169 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 10170 std::string Code = "#include \"a.h\"\n" 10171 "#include \"c.h\"\n" 10172 "\n" 10173 "int main() {\n" 10174 " return 0;\n" 10175 "}"; 10176 std::string Expected = "#include \"a.h\"\n" 10177 "#include \"b.h\"\n" 10178 "#include \"c.h\"\n" 10179 "\n" 10180 "int main() {\n" 10181 " return 0;\n" 10182 "}"; 10183 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 10184 tooling::Replacements Replaces = toReplacements( 10185 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 10186 "#include \"b.h\"\n")}); 10187 10188 format::FormatStyle Style = format::getLLVMStyle(); 10189 Style.SortIncludes = true; 10190 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 10191 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 10192 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 10193 auto Result = applyAllReplacements(Code, *FormattedReplaces); 10194 EXPECT_TRUE(static_cast<bool>(Result)); 10195 EXPECT_EQ(Expected, *Result); 10196 } 10197 10198 } // end namespace 10199 } // end namespace format 10200 } // end namespace clang 10201