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 StatusCheck { 34 SC_ExpectComplete, 35 SC_ExpectIncomplete, 36 SC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 StatusCheck CheckComplete = SC_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 FormattingAttemptStatus Status; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &Status); 48 if (CheckComplete != SC_DoNotCheck) { 49 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 50 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 51 << Code << "\n\n"; 52 } 53 ReplacementCount = Replaces.size(); 54 auto Result = applyAllReplacements(Code, Replaces); 55 EXPECT_TRUE(static_cast<bool>(Result)); 56 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 57 return *Result; 58 } 59 60 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 66 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 67 } 68 69 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 70 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 71 } 72 73 void verifyFormat(llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 76 if (Style.Language == FormatStyle::LK_Cpp) { 77 // Objective-C++ is a superset of C++, so everything checked for C++ 78 // needs to be checked for Objective-C++ as well. 79 FormatStyle ObjCStyle = Style; 80 ObjCStyle.Language = FormatStyle::LK_ObjC; 81 EXPECT_EQ(Code.str(), format(test::messUp(Code), ObjCStyle)); 82 } 83 } 84 85 void verifyIncompleteFormat(llvm::StringRef Code, 86 const FormatStyle &Style = getLLVMStyle()) { 87 EXPECT_EQ(Code.str(), 88 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 89 } 90 91 void verifyGoogleFormat(llvm::StringRef Code) { 92 verifyFormat(Code, getGoogleStyle()); 93 } 94 95 void verifyIndependentOfContext(llvm::StringRef text) { 96 verifyFormat(text); 97 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 98 } 99 100 /// \brief Verify that clang-format does not crash on the given input. 101 void verifyNoCrash(llvm::StringRef Code, 102 const FormatStyle &Style = getLLVMStyle()) { 103 format(Code, Style, SC_DoNotCheck); 104 } 105 106 int ReplacementCount; 107 }; 108 109 TEST_F(FormatTest, MessUp) { 110 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 111 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 112 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 113 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 114 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 115 } 116 117 //===----------------------------------------------------------------------===// 118 // Basic function tests. 119 //===----------------------------------------------------------------------===// 120 121 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 122 EXPECT_EQ(";", format(";")); 123 } 124 125 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 126 EXPECT_EQ("int i;", format(" int i;")); 127 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 128 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 129 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 130 } 131 132 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 133 EXPECT_EQ("int i;", format("int\ni;")); 134 } 135 136 TEST_F(FormatTest, FormatsNestedBlockStatements) { 137 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 138 } 139 140 TEST_F(FormatTest, FormatsNestedCall) { 141 verifyFormat("Method(f1, f2(f3));"); 142 verifyFormat("Method(f1(f2, f3()));"); 143 verifyFormat("Method(f1(f2, (f3())));"); 144 } 145 146 TEST_F(FormatTest, NestedNameSpecifiers) { 147 verifyFormat("vector<::Type> v;"); 148 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 149 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 150 verifyFormat("bool a = 2 < ::SomeFunction();"); 151 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 152 verifyFormat("some::string getName();"); 153 } 154 155 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 156 EXPECT_EQ("if (a) {\n" 157 " f();\n" 158 "}", 159 format("if(a){f();}")); 160 EXPECT_EQ(4, ReplacementCount); 161 EXPECT_EQ("if (a) {\n" 162 " f();\n" 163 "}", 164 format("if (a) {\n" 165 " f();\n" 166 "}")); 167 EXPECT_EQ(0, ReplacementCount); 168 EXPECT_EQ("/*\r\n" 169 "\r\n" 170 "*/\r\n", 171 format("/*\r\n" 172 "\r\n" 173 "*/\r\n")); 174 EXPECT_EQ(0, ReplacementCount); 175 } 176 177 TEST_F(FormatTest, RemovesEmptyLines) { 178 EXPECT_EQ("class C {\n" 179 " int i;\n" 180 "};", 181 format("class C {\n" 182 " int i;\n" 183 "\n" 184 "};")); 185 186 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 187 EXPECT_EQ("namespace N {\n" 188 "\n" 189 "int i;\n" 190 "}", 191 format("namespace N {\n" 192 "\n" 193 "int i;\n" 194 "}", 195 getGoogleStyle())); 196 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 197 "\n" 198 "int i;\n" 199 "}", 200 format("extern /**/ \"C\" /**/ {\n" 201 "\n" 202 "int i;\n" 203 "}", 204 getGoogleStyle())); 205 206 // ...but do keep inlining and removing empty lines for non-block extern "C" 207 // functions. 208 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 209 EXPECT_EQ("extern \"C\" int f() {\n" 210 " int i = 42;\n" 211 " return i;\n" 212 "}", 213 format("extern \"C\" int f() {\n" 214 "\n" 215 " int i = 42;\n" 216 " return i;\n" 217 "}", 218 getGoogleStyle())); 219 220 // Remove empty lines at the beginning and end of blocks. 221 EXPECT_EQ("void f() {\n" 222 "\n" 223 " if (a) {\n" 224 "\n" 225 " f();\n" 226 " }\n" 227 "}", 228 format("void f() {\n" 229 "\n" 230 " if (a) {\n" 231 "\n" 232 " f();\n" 233 "\n" 234 " }\n" 235 "\n" 236 "}", 237 getLLVMStyle())); 238 EXPECT_EQ("void f() {\n" 239 " if (a) {\n" 240 " f();\n" 241 " }\n" 242 "}", 243 format("void f() {\n" 244 "\n" 245 " if (a) {\n" 246 "\n" 247 " f();\n" 248 "\n" 249 " }\n" 250 "\n" 251 "}", 252 getGoogleStyle())); 253 254 // Don't remove empty lines in more complex control statements. 255 EXPECT_EQ("void f() {\n" 256 " if (a) {\n" 257 " f();\n" 258 "\n" 259 " } else if (b) {\n" 260 " f();\n" 261 " }\n" 262 "}", 263 format("void f() {\n" 264 " if (a) {\n" 265 " f();\n" 266 "\n" 267 " } else if (b) {\n" 268 " f();\n" 269 "\n" 270 " }\n" 271 "\n" 272 "}")); 273 274 // FIXME: This is slightly inconsistent. 275 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 276 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 277 EXPECT_EQ("namespace {\n" 278 "int i;\n" 279 "}", 280 format("namespace {\n" 281 "int i;\n" 282 "\n" 283 "}", LLVMWithNoNamespaceFix)); 284 EXPECT_EQ("namespace {\n" 285 "int i;\n" 286 "}", 287 format("namespace {\n" 288 "int i;\n" 289 "\n" 290 "}")); 291 EXPECT_EQ("namespace {\n" 292 "int i;\n" 293 "\n" 294 "} // namespace", 295 format("namespace {\n" 296 "int i;\n" 297 "\n" 298 "} // namespace")); 299 300 FormatStyle Style = getLLVMStyle(); 301 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 302 Style.MaxEmptyLinesToKeep = 2; 303 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 304 Style.BraceWrapping.AfterClass = true; 305 Style.BraceWrapping.AfterFunction = true; 306 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 307 308 EXPECT_EQ("class Foo\n" 309 "{\n" 310 " Foo() {}\n" 311 "\n" 312 " void funk() {}\n" 313 "};", 314 format("class Foo\n" 315 "{\n" 316 " Foo()\n" 317 " {\n" 318 " }\n" 319 "\n" 320 " void funk() {}\n" 321 "};", 322 Style)); 323 } 324 325 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 326 verifyFormat("x = (a) and (b);"); 327 verifyFormat("x = (a) or (b);"); 328 verifyFormat("x = (a) bitand (b);"); 329 verifyFormat("x = (a) bitor (b);"); 330 verifyFormat("x = (a) not_eq (b);"); 331 verifyFormat("x = (a) and_eq (b);"); 332 verifyFormat("x = (a) or_eq (b);"); 333 verifyFormat("x = (a) xor (b);"); 334 } 335 336 //===----------------------------------------------------------------------===// 337 // Tests for control statements. 338 //===----------------------------------------------------------------------===// 339 340 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 341 verifyFormat("if (true)\n f();\ng();"); 342 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 343 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 344 345 FormatStyle AllowsMergedIf = getLLVMStyle(); 346 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 347 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 348 verifyFormat("if (a)\n" 349 " // comment\n" 350 " f();", 351 AllowsMergedIf); 352 verifyFormat("{\n" 353 " if (a)\n" 354 " label:\n" 355 " f();\n" 356 "}", 357 AllowsMergedIf); 358 verifyFormat("#define A \\\n" 359 " if (a) \\\n" 360 " label: \\\n" 361 " f()", 362 AllowsMergedIf); 363 verifyFormat("if (a)\n" 364 " ;", 365 AllowsMergedIf); 366 verifyFormat("if (a)\n" 367 " if (b) return;", 368 AllowsMergedIf); 369 370 verifyFormat("if (a) // Can't merge this\n" 371 " f();\n", 372 AllowsMergedIf); 373 verifyFormat("if (a) /* still don't merge */\n" 374 " f();", 375 AllowsMergedIf); 376 verifyFormat("if (a) { // Never merge this\n" 377 " f();\n" 378 "}", 379 AllowsMergedIf); 380 verifyFormat("if (a) { /* Never merge this */\n" 381 " f();\n" 382 "}", 383 AllowsMergedIf); 384 385 AllowsMergedIf.ColumnLimit = 14; 386 verifyFormat("if (a) return;", AllowsMergedIf); 387 verifyFormat("if (aaaaaaaaa)\n" 388 " return;", 389 AllowsMergedIf); 390 391 AllowsMergedIf.ColumnLimit = 13; 392 verifyFormat("if (a)\n return;", AllowsMergedIf); 393 } 394 395 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 396 FormatStyle AllowsMergedLoops = getLLVMStyle(); 397 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 398 verifyFormat("while (true) continue;", AllowsMergedLoops); 399 verifyFormat("for (;;) continue;", AllowsMergedLoops); 400 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 401 verifyFormat("while (true)\n" 402 " ;", 403 AllowsMergedLoops); 404 verifyFormat("for (;;)\n" 405 " ;", 406 AllowsMergedLoops); 407 verifyFormat("for (;;)\n" 408 " for (;;) continue;", 409 AllowsMergedLoops); 410 verifyFormat("for (;;) // Can't merge this\n" 411 " continue;", 412 AllowsMergedLoops); 413 verifyFormat("for (;;) /* still don't merge */\n" 414 " continue;", 415 AllowsMergedLoops); 416 } 417 418 TEST_F(FormatTest, FormatShortBracedStatements) { 419 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 420 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 421 422 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 423 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 424 425 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 426 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 427 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 428 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 429 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 430 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 431 verifyFormat("if (true) { //\n" 432 " f();\n" 433 "}", 434 AllowSimpleBracedStatements); 435 verifyFormat("if (true) {\n" 436 " f();\n" 437 " f();\n" 438 "}", 439 AllowSimpleBracedStatements); 440 verifyFormat("if (true) {\n" 441 " f();\n" 442 "} else {\n" 443 " f();\n" 444 "}", 445 AllowSimpleBracedStatements); 446 447 verifyFormat("template <int> struct A2 {\n" 448 " struct B {};\n" 449 "};", 450 AllowSimpleBracedStatements); 451 452 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 453 verifyFormat("if (true) {\n" 454 " f();\n" 455 "}", 456 AllowSimpleBracedStatements); 457 verifyFormat("if (true) {\n" 458 " f();\n" 459 "} else {\n" 460 " f();\n" 461 "}", 462 AllowSimpleBracedStatements); 463 464 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 465 verifyFormat("while (true) {\n" 466 " f();\n" 467 "}", 468 AllowSimpleBracedStatements); 469 verifyFormat("for (;;) {\n" 470 " f();\n" 471 "}", 472 AllowSimpleBracedStatements); 473 } 474 475 TEST_F(FormatTest, ParseIfElse) { 476 verifyFormat("if (true)\n" 477 " if (true)\n" 478 " if (true)\n" 479 " f();\n" 480 " else\n" 481 " g();\n" 482 " else\n" 483 " h();\n" 484 "else\n" 485 " i();"); 486 verifyFormat("if (true)\n" 487 " if (true)\n" 488 " if (true) {\n" 489 " if (true)\n" 490 " f();\n" 491 " } else {\n" 492 " g();\n" 493 " }\n" 494 " else\n" 495 " h();\n" 496 "else {\n" 497 " i();\n" 498 "}"); 499 verifyFormat("void f() {\n" 500 " if (a) {\n" 501 " } else {\n" 502 " }\n" 503 "}"); 504 } 505 506 TEST_F(FormatTest, ElseIf) { 507 verifyFormat("if (a) {\n} else if (b) {\n}"); 508 verifyFormat("if (a)\n" 509 " f();\n" 510 "else if (b)\n" 511 " g();\n" 512 "else\n" 513 " h();"); 514 verifyFormat("if (a) {\n" 515 " f();\n" 516 "}\n" 517 "// or else ..\n" 518 "else {\n" 519 " g()\n" 520 "}"); 521 522 verifyFormat("if (a) {\n" 523 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 525 "}"); 526 verifyFormat("if (a) {\n" 527 "} else if (\n" 528 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 529 "}", 530 getLLVMStyleWithColumns(62)); 531 } 532 533 TEST_F(FormatTest, FormatsForLoop) { 534 verifyFormat( 535 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 536 " ++VeryVeryLongLoopVariable)\n" 537 " ;"); 538 verifyFormat("for (;;)\n" 539 " f();"); 540 verifyFormat("for (;;) {\n}"); 541 verifyFormat("for (;;) {\n" 542 " f();\n" 543 "}"); 544 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 545 546 verifyFormat( 547 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 548 " E = UnwrappedLines.end();\n" 549 " I != E; ++I) {\n}"); 550 551 verifyFormat( 552 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 553 " ++IIIII) {\n}"); 554 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 555 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 556 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 557 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 558 " I = FD->getDeclsInPrototypeScope().begin(),\n" 559 " E = FD->getDeclsInPrototypeScope().end();\n" 560 " I != E; ++I) {\n}"); 561 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 562 " I = Container.begin(),\n" 563 " E = Container.end();\n" 564 " I != E; ++I) {\n}", 565 getLLVMStyleWithColumns(76)); 566 567 verifyFormat( 568 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 570 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 572 " ++aaaaaaaaaaa) {\n}"); 573 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 574 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 575 " ++i) {\n}"); 576 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 577 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 578 "}"); 579 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 580 " aaaaaaaaaa);\n" 581 " iter; ++iter) {\n" 582 "}"); 583 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 585 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 586 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 587 588 FormatStyle NoBinPacking = getLLVMStyle(); 589 NoBinPacking.BinPackParameters = false; 590 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 591 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 592 " aaaaaaaaaaaaaaaa,\n" 593 " aaaaaaaaaaaaaaaa,\n" 594 " aaaaaaaaaaaaaaaa);\n" 595 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 596 "}", 597 NoBinPacking); 598 verifyFormat( 599 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 600 " E = UnwrappedLines.end();\n" 601 " I != E;\n" 602 " ++I) {\n}", 603 NoBinPacking); 604 } 605 606 TEST_F(FormatTest, RangeBasedForLoops) { 607 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 608 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 609 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 610 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 611 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 612 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 613 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 614 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 615 } 616 617 TEST_F(FormatTest, ForEachLoops) { 618 verifyFormat("void f() {\n" 619 " foreach (Item *item, itemlist) {}\n" 620 " Q_FOREACH (Item *item, itemlist) {}\n" 621 " BOOST_FOREACH (Item *item, itemlist) {}\n" 622 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 623 "}"); 624 625 // As function-like macros. 626 verifyFormat("#define foreach(x, y)\n" 627 "#define Q_FOREACH(x, y)\n" 628 "#define BOOST_FOREACH(x, y)\n" 629 "#define UNKNOWN_FOREACH(x, y)\n"); 630 631 // Not as function-like macros. 632 verifyFormat("#define foreach (x, y)\n" 633 "#define Q_FOREACH (x, y)\n" 634 "#define BOOST_FOREACH (x, y)\n" 635 "#define UNKNOWN_FOREACH (x, y)\n"); 636 } 637 638 TEST_F(FormatTest, FormatsWhileLoop) { 639 verifyFormat("while (true) {\n}"); 640 verifyFormat("while (true)\n" 641 " f();"); 642 verifyFormat("while () {\n}"); 643 verifyFormat("while () {\n" 644 " f();\n" 645 "}"); 646 } 647 648 TEST_F(FormatTest, FormatsDoWhile) { 649 verifyFormat("do {\n" 650 " do_something();\n" 651 "} while (something());"); 652 verifyFormat("do\n" 653 " do_something();\n" 654 "while (something());"); 655 } 656 657 TEST_F(FormatTest, FormatsSwitchStatement) { 658 verifyFormat("switch (x) {\n" 659 "case 1:\n" 660 " f();\n" 661 " break;\n" 662 "case kFoo:\n" 663 "case ns::kBar:\n" 664 "case kBaz:\n" 665 " break;\n" 666 "default:\n" 667 " g();\n" 668 " break;\n" 669 "}"); 670 verifyFormat("switch (x) {\n" 671 "case 1: {\n" 672 " f();\n" 673 " break;\n" 674 "}\n" 675 "case 2: {\n" 676 " break;\n" 677 "}\n" 678 "}"); 679 verifyFormat("switch (x) {\n" 680 "case 1: {\n" 681 " f();\n" 682 " {\n" 683 " g();\n" 684 " h();\n" 685 " }\n" 686 " break;\n" 687 "}\n" 688 "}"); 689 verifyFormat("switch (x) {\n" 690 "case 1: {\n" 691 " f();\n" 692 " if (foo) {\n" 693 " g();\n" 694 " h();\n" 695 " }\n" 696 " break;\n" 697 "}\n" 698 "}"); 699 verifyFormat("switch (x) {\n" 700 "case 1: {\n" 701 " f();\n" 702 " g();\n" 703 "} break;\n" 704 "}"); 705 verifyFormat("switch (test)\n" 706 " ;"); 707 verifyFormat("switch (x) {\n" 708 "default: {\n" 709 " // Do nothing.\n" 710 "}\n" 711 "}"); 712 verifyFormat("switch (x) {\n" 713 "// comment\n" 714 "// if 1, do f()\n" 715 "case 1:\n" 716 " f();\n" 717 "}"); 718 verifyFormat("switch (x) {\n" 719 "case 1:\n" 720 " // Do amazing stuff\n" 721 " {\n" 722 " f();\n" 723 " g();\n" 724 " }\n" 725 " break;\n" 726 "}"); 727 verifyFormat("#define A \\\n" 728 " switch (x) { \\\n" 729 " case a: \\\n" 730 " foo = b; \\\n" 731 " }", 732 getLLVMStyleWithColumns(20)); 733 verifyFormat("#define OPERATION_CASE(name) \\\n" 734 " case OP_name: \\\n" 735 " return operations::Operation##name\n", 736 getLLVMStyleWithColumns(40)); 737 verifyFormat("switch (x) {\n" 738 "case 1:;\n" 739 "default:;\n" 740 " int i;\n" 741 "}"); 742 743 verifyGoogleFormat("switch (x) {\n" 744 " case 1:\n" 745 " f();\n" 746 " break;\n" 747 " case kFoo:\n" 748 " case ns::kBar:\n" 749 " case kBaz:\n" 750 " break;\n" 751 " default:\n" 752 " g();\n" 753 " break;\n" 754 "}"); 755 verifyGoogleFormat("switch (x) {\n" 756 " case 1: {\n" 757 " f();\n" 758 " break;\n" 759 " }\n" 760 "}"); 761 verifyGoogleFormat("switch (test)\n" 762 " ;"); 763 764 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 765 " case OP_name: \\\n" 766 " return operations::Operation##name\n"); 767 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 768 " // Get the correction operation class.\n" 769 " switch (OpCode) {\n" 770 " CASE(Add);\n" 771 " CASE(Subtract);\n" 772 " default:\n" 773 " return operations::Unknown;\n" 774 " }\n" 775 "#undef OPERATION_CASE\n" 776 "}"); 777 verifyFormat("DEBUG({\n" 778 " switch (x) {\n" 779 " case A:\n" 780 " f();\n" 781 " break;\n" 782 " // On B:\n" 783 " case B:\n" 784 " g();\n" 785 " break;\n" 786 " }\n" 787 "});"); 788 verifyFormat("switch (a) {\n" 789 "case (b):\n" 790 " return;\n" 791 "}"); 792 793 verifyFormat("switch (a) {\n" 794 "case some_namespace::\n" 795 " some_constant:\n" 796 " return;\n" 797 "}", 798 getLLVMStyleWithColumns(34)); 799 } 800 801 TEST_F(FormatTest, CaseRanges) { 802 verifyFormat("switch (x) {\n" 803 "case 'A' ... 'Z':\n" 804 "case 1 ... 5:\n" 805 "case a ... b:\n" 806 " break;\n" 807 "}"); 808 } 809 810 TEST_F(FormatTest, ShortCaseLabels) { 811 FormatStyle Style = getLLVMStyle(); 812 Style.AllowShortCaseLabelsOnASingleLine = true; 813 verifyFormat("switch (a) {\n" 814 "case 1: x = 1; break;\n" 815 "case 2: return;\n" 816 "case 3:\n" 817 "case 4:\n" 818 "case 5: return;\n" 819 "case 6: // comment\n" 820 " return;\n" 821 "case 7:\n" 822 " // comment\n" 823 " return;\n" 824 "case 8:\n" 825 " x = 8; // comment\n" 826 " break;\n" 827 "default: y = 1; break;\n" 828 "}", 829 Style); 830 verifyFormat("switch (a) {\n" 831 "#if FOO\n" 832 "case 0: return 0;\n" 833 "#endif\n" 834 "}", 835 Style); 836 verifyFormat("switch (a) {\n" 837 "case 1: {\n" 838 "}\n" 839 "case 2: {\n" 840 " return;\n" 841 "}\n" 842 "case 3: {\n" 843 " x = 1;\n" 844 " return;\n" 845 "}\n" 846 "case 4:\n" 847 " if (x)\n" 848 " return;\n" 849 "}", 850 Style); 851 Style.ColumnLimit = 21; 852 verifyFormat("switch (a) {\n" 853 "case 1: x = 1; break;\n" 854 "case 2: return;\n" 855 "case 3:\n" 856 "case 4:\n" 857 "case 5: return;\n" 858 "default:\n" 859 " y = 1;\n" 860 " break;\n" 861 "}", 862 Style); 863 } 864 865 TEST_F(FormatTest, FormatsLabels) { 866 verifyFormat("void f() {\n" 867 " some_code();\n" 868 "test_label:\n" 869 " some_other_code();\n" 870 " {\n" 871 " some_more_code();\n" 872 " another_label:\n" 873 " some_more_code();\n" 874 " }\n" 875 "}"); 876 verifyFormat("{\n" 877 " some_code();\n" 878 "test_label:\n" 879 " some_other_code();\n" 880 "}"); 881 verifyFormat("{\n" 882 " some_code();\n" 883 "test_label:;\n" 884 " int i = 0;\n" 885 "}"); 886 } 887 888 //===----------------------------------------------------------------------===// 889 // Tests for classes, namespaces, etc. 890 //===----------------------------------------------------------------------===// 891 892 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 893 verifyFormat("class A {};"); 894 } 895 896 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 897 verifyFormat("class A {\n" 898 "public:\n" 899 "public: // comment\n" 900 "protected:\n" 901 "private:\n" 902 " void f() {}\n" 903 "};"); 904 verifyGoogleFormat("class A {\n" 905 " public:\n" 906 " protected:\n" 907 " private:\n" 908 " void f() {}\n" 909 "};"); 910 verifyFormat("class A {\n" 911 "public slots:\n" 912 " void f1() {}\n" 913 "public Q_SLOTS:\n" 914 " void f2() {}\n" 915 "protected slots:\n" 916 " void f3() {}\n" 917 "protected Q_SLOTS:\n" 918 " void f4() {}\n" 919 "private slots:\n" 920 " void f5() {}\n" 921 "private Q_SLOTS:\n" 922 " void f6() {}\n" 923 "signals:\n" 924 " void g1();\n" 925 "Q_SIGNALS:\n" 926 " void g2();\n" 927 "};"); 928 929 // Don't interpret 'signals' the wrong way. 930 verifyFormat("signals.set();"); 931 verifyFormat("for (Signals signals : f()) {\n}"); 932 verifyFormat("{\n" 933 " signals.set(); // This needs indentation.\n" 934 "}"); 935 verifyFormat("void f() {\n" 936 "label:\n" 937 " signals.baz();\n" 938 "}"); 939 } 940 941 TEST_F(FormatTest, SeparatesLogicalBlocks) { 942 EXPECT_EQ("class A {\n" 943 "public:\n" 944 " void f();\n" 945 "\n" 946 "private:\n" 947 " void g() {}\n" 948 " // test\n" 949 "protected:\n" 950 " int h;\n" 951 "};", 952 format("class A {\n" 953 "public:\n" 954 "void f();\n" 955 "private:\n" 956 "void g() {}\n" 957 "// test\n" 958 "protected:\n" 959 "int h;\n" 960 "};")); 961 EXPECT_EQ("class A {\n" 962 "protected:\n" 963 "public:\n" 964 " void f();\n" 965 "};", 966 format("class A {\n" 967 "protected:\n" 968 "\n" 969 "public:\n" 970 "\n" 971 " void f();\n" 972 "};")); 973 974 // Even ensure proper spacing inside macros. 975 EXPECT_EQ("#define B \\\n" 976 " class A { \\\n" 977 " protected: \\\n" 978 " public: \\\n" 979 " void f(); \\\n" 980 " };", 981 format("#define B \\\n" 982 " class A { \\\n" 983 " protected: \\\n" 984 " \\\n" 985 " public: \\\n" 986 " \\\n" 987 " void f(); \\\n" 988 " };", 989 getGoogleStyle())); 990 // But don't remove empty lines after macros ending in access specifiers. 991 EXPECT_EQ("#define A private:\n" 992 "\n" 993 "int i;", 994 format("#define A private:\n" 995 "\n" 996 "int i;")); 997 } 998 999 TEST_F(FormatTest, FormatsClasses) { 1000 verifyFormat("class A : public B {};"); 1001 verifyFormat("class A : public ::B {};"); 1002 1003 verifyFormat( 1004 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1005 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1006 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1007 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1008 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1009 verifyFormat( 1010 "class A : public B, public C, public D, public E, public F {};"); 1011 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1012 " public C,\n" 1013 " public D,\n" 1014 " public E,\n" 1015 " public F,\n" 1016 " public G {};"); 1017 1018 verifyFormat("class\n" 1019 " ReallyReallyLongClassName {\n" 1020 " int i;\n" 1021 "};", 1022 getLLVMStyleWithColumns(32)); 1023 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1024 " aaaaaaaaaaaaaaaa> {};"); 1025 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1026 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1027 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1028 verifyFormat("template <class R, class C>\n" 1029 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1030 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1031 verifyFormat("class ::A::B {};"); 1032 } 1033 1034 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1035 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1036 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1037 1038 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1039 verifyFormat("class MyClass\n" 1040 " : public X\n" 1041 " , public Y {};", 1042 StyleWithInheritanceBreak); 1043 } 1044 1045 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1046 verifyFormat("class A {\n} a, b;"); 1047 verifyFormat("struct A {\n} a, b;"); 1048 verifyFormat("union A {\n} a;"); 1049 } 1050 1051 TEST_F(FormatTest, FormatsEnum) { 1052 verifyFormat("enum {\n" 1053 " Zero,\n" 1054 " One = 1,\n" 1055 " Two = One + 1,\n" 1056 " Three = (One + Two),\n" 1057 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1058 " Five = (One, Two, Three, Four, 5)\n" 1059 "};"); 1060 verifyGoogleFormat("enum {\n" 1061 " Zero,\n" 1062 " One = 1,\n" 1063 " Two = One + 1,\n" 1064 " Three = (One + Two),\n" 1065 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1066 " Five = (One, Two, Three, Four, 5)\n" 1067 "};"); 1068 verifyFormat("enum Enum {};"); 1069 verifyFormat("enum {};"); 1070 verifyFormat("enum X E {} d;"); 1071 verifyFormat("enum __attribute__((...)) E {} d;"); 1072 verifyFormat("enum __declspec__((...)) E {} d;"); 1073 verifyFormat("enum {\n" 1074 " Bar = Foo<int, int>::value\n" 1075 "};", 1076 getLLVMStyleWithColumns(30)); 1077 1078 verifyFormat("enum ShortEnum { A, B, C };"); 1079 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1080 1081 EXPECT_EQ("enum KeepEmptyLines {\n" 1082 " ONE,\n" 1083 "\n" 1084 " TWO,\n" 1085 "\n" 1086 " THREE\n" 1087 "}", 1088 format("enum KeepEmptyLines {\n" 1089 " ONE,\n" 1090 "\n" 1091 " TWO,\n" 1092 "\n" 1093 "\n" 1094 " THREE\n" 1095 "}")); 1096 verifyFormat("enum E { // comment\n" 1097 " ONE,\n" 1098 " TWO\n" 1099 "};\n" 1100 "int i;"); 1101 // Not enums. 1102 verifyFormat("enum X f() {\n" 1103 " a();\n" 1104 " return 42;\n" 1105 "}"); 1106 verifyFormat("enum X Type::f() {\n" 1107 " a();\n" 1108 " return 42;\n" 1109 "}"); 1110 verifyFormat("enum ::X f() {\n" 1111 " a();\n" 1112 " return 42;\n" 1113 "}"); 1114 verifyFormat("enum ns::X f() {\n" 1115 " a();\n" 1116 " return 42;\n" 1117 "}"); 1118 } 1119 1120 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1121 verifyFormat("enum Type {\n" 1122 " One = 0; // These semicolons should be commas.\n" 1123 " Two = 1;\n" 1124 "};"); 1125 verifyFormat("namespace n {\n" 1126 "enum Type {\n" 1127 " One,\n" 1128 " Two, // missing };\n" 1129 " int i;\n" 1130 "}\n" 1131 "void g() {}"); 1132 } 1133 1134 TEST_F(FormatTest, FormatsEnumStruct) { 1135 verifyFormat("enum struct {\n" 1136 " Zero,\n" 1137 " One = 1,\n" 1138 " Two = One + 1,\n" 1139 " Three = (One + Two),\n" 1140 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1141 " Five = (One, Two, Three, Four, 5)\n" 1142 "};"); 1143 verifyFormat("enum struct Enum {};"); 1144 verifyFormat("enum struct {};"); 1145 verifyFormat("enum struct X E {} d;"); 1146 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1147 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1148 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1149 } 1150 1151 TEST_F(FormatTest, FormatsEnumClass) { 1152 verifyFormat("enum class {\n" 1153 " Zero,\n" 1154 " One = 1,\n" 1155 " Two = One + 1,\n" 1156 " Three = (One + Two),\n" 1157 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1158 " Five = (One, Two, Three, Four, 5)\n" 1159 "};"); 1160 verifyFormat("enum class Enum {};"); 1161 verifyFormat("enum class {};"); 1162 verifyFormat("enum class X E {} d;"); 1163 verifyFormat("enum class __attribute__((...)) E {} d;"); 1164 verifyFormat("enum class __declspec__((...)) E {} d;"); 1165 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1166 } 1167 1168 TEST_F(FormatTest, FormatsEnumTypes) { 1169 verifyFormat("enum X : int {\n" 1170 " A, // Force multiple lines.\n" 1171 " B\n" 1172 "};"); 1173 verifyFormat("enum X : int { A, B };"); 1174 verifyFormat("enum X : std::uint32_t { A, B };"); 1175 } 1176 1177 TEST_F(FormatTest, FormatsNSEnums) { 1178 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1179 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1180 " // Information about someDecentlyLongValue.\n" 1181 " someDecentlyLongValue,\n" 1182 " // Information about anotherDecentlyLongValue.\n" 1183 " anotherDecentlyLongValue,\n" 1184 " // Information about aThirdDecentlyLongValue.\n" 1185 " aThirdDecentlyLongValue\n" 1186 "};"); 1187 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1188 " a = 1,\n" 1189 " b = 2,\n" 1190 " c = 3,\n" 1191 "};"); 1192 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1193 " a = 1,\n" 1194 " b = 2,\n" 1195 " c = 3,\n" 1196 "};"); 1197 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1198 " a = 1,\n" 1199 " b = 2,\n" 1200 " c = 3,\n" 1201 "};"); 1202 } 1203 1204 TEST_F(FormatTest, FormatsBitfields) { 1205 verifyFormat("struct Bitfields {\n" 1206 " unsigned sClass : 8;\n" 1207 " unsigned ValueKind : 2;\n" 1208 "};"); 1209 verifyFormat("struct A {\n" 1210 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1211 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1212 "};"); 1213 verifyFormat("struct MyStruct {\n" 1214 " uchar data;\n" 1215 " uchar : 8;\n" 1216 " uchar : 8;\n" 1217 " uchar other;\n" 1218 "};"); 1219 } 1220 1221 TEST_F(FormatTest, FormatsNamespaces) { 1222 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1223 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1224 1225 verifyFormat("namespace some_namespace {\n" 1226 "class A {};\n" 1227 "void f() { f(); }\n" 1228 "}", 1229 LLVMWithNoNamespaceFix); 1230 verifyFormat("namespace {\n" 1231 "class A {};\n" 1232 "void f() { f(); }\n" 1233 "}", 1234 LLVMWithNoNamespaceFix); 1235 verifyFormat("inline namespace X {\n" 1236 "class A {};\n" 1237 "void f() { f(); }\n" 1238 "}", 1239 LLVMWithNoNamespaceFix); 1240 verifyFormat("using namespace some_namespace;\n" 1241 "class A {};\n" 1242 "void f() { f(); }", 1243 LLVMWithNoNamespaceFix); 1244 1245 // This code is more common than we thought; if we 1246 // layout this correctly the semicolon will go into 1247 // its own line, which is undesirable. 1248 verifyFormat("namespace {};", 1249 LLVMWithNoNamespaceFix); 1250 verifyFormat("namespace {\n" 1251 "class A {};\n" 1252 "};", 1253 LLVMWithNoNamespaceFix); 1254 1255 verifyFormat("namespace {\n" 1256 "int SomeVariable = 0; // comment\n" 1257 "} // namespace", 1258 LLVMWithNoNamespaceFix); 1259 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1260 "#define HEADER_GUARD\n" 1261 "namespace my_namespace {\n" 1262 "int i;\n" 1263 "} // my_namespace\n" 1264 "#endif // HEADER_GUARD", 1265 format("#ifndef HEADER_GUARD\n" 1266 " #define HEADER_GUARD\n" 1267 " namespace my_namespace {\n" 1268 "int i;\n" 1269 "} // my_namespace\n" 1270 "#endif // HEADER_GUARD", 1271 LLVMWithNoNamespaceFix)); 1272 1273 EXPECT_EQ("namespace A::B {\n" 1274 "class C {};\n" 1275 "}", 1276 format("namespace A::B {\n" 1277 "class C {};\n" 1278 "}", 1279 LLVMWithNoNamespaceFix)); 1280 1281 FormatStyle Style = getLLVMStyle(); 1282 Style.NamespaceIndentation = FormatStyle::NI_All; 1283 EXPECT_EQ("namespace out {\n" 1284 " int i;\n" 1285 " namespace in {\n" 1286 " int i;\n" 1287 " } // namespace in\n" 1288 "} // namespace out", 1289 format("namespace out {\n" 1290 "int i;\n" 1291 "namespace in {\n" 1292 "int i;\n" 1293 "} // namespace in\n" 1294 "} // namespace out", 1295 Style)); 1296 1297 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1298 EXPECT_EQ("namespace out {\n" 1299 "int i;\n" 1300 "namespace in {\n" 1301 " int i;\n" 1302 "} // namespace in\n" 1303 "} // namespace out", 1304 format("namespace out {\n" 1305 "int i;\n" 1306 "namespace in {\n" 1307 "int i;\n" 1308 "} // namespace in\n" 1309 "} // namespace out", 1310 Style)); 1311 } 1312 1313 TEST_F(FormatTest, FormatsCompactNamespaces) { 1314 FormatStyle Style = getLLVMStyle(); 1315 Style.CompactNamespaces = true; 1316 1317 verifyFormat("namespace A { namespace B {\n" 1318 "}} // namespace A::B", 1319 Style); 1320 1321 EXPECT_EQ("namespace out { namespace in {\n" 1322 "}} // namespace out::in", 1323 format("namespace out {\n" 1324 "namespace in {\n" 1325 "} // namespace in\n" 1326 "} // namespace out", 1327 Style)); 1328 1329 // Only namespaces which have both consecutive opening and end get compacted 1330 EXPECT_EQ("namespace out {\n" 1331 "namespace in1 {\n" 1332 "} // namespace in1\n" 1333 "namespace in2 {\n" 1334 "} // namespace in2\n" 1335 "} // namespace out", 1336 format("namespace out {\n" 1337 "namespace in1 {\n" 1338 "} // namespace in1\n" 1339 "namespace in2 {\n" 1340 "} // namespace in2\n" 1341 "} // namespace out", 1342 Style)); 1343 1344 EXPECT_EQ("namespace out {\n" 1345 "int i;\n" 1346 "namespace in {\n" 1347 "int j;\n" 1348 "} // namespace in\n" 1349 "int k;\n" 1350 "} // namespace out", 1351 format("namespace out { int i;\n" 1352 "namespace in { int j; } // namespace in\n" 1353 "int k; } // namespace out", 1354 Style)); 1355 1356 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1357 "}}} // namespace A::B::C\n", 1358 format("namespace A { namespace B {\n" 1359 "namespace C {\n" 1360 "}} // namespace B::C\n" 1361 "} // namespace A\n", 1362 Style)); 1363 1364 Style.ColumnLimit = 40; 1365 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1366 "namespace bbbbbbbbbb {\n" 1367 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1368 format("namespace aaaaaaaaaa {\n" 1369 "namespace bbbbbbbbbb {\n" 1370 "} // namespace bbbbbbbbbb\n" 1371 "} // namespace aaaaaaaaaa", 1372 Style)); 1373 1374 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1375 "namespace cccccc {\n" 1376 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1377 format("namespace aaaaaa {\n" 1378 "namespace bbbbbb {\n" 1379 "namespace cccccc {\n" 1380 "} // namespace cccccc\n" 1381 "} // namespace bbbbbb\n" 1382 "} // namespace aaaaaa", 1383 Style)); 1384 Style.ColumnLimit = 80; 1385 1386 // Extra semicolon after 'inner' closing brace prevents merging 1387 EXPECT_EQ("namespace out { namespace in {\n" 1388 "}; } // namespace out::in", 1389 format("namespace out {\n" 1390 "namespace in {\n" 1391 "}; // namespace in\n" 1392 "} // namespace out", 1393 Style)); 1394 1395 // Extra semicolon after 'outer' closing brace is conserved 1396 EXPECT_EQ("namespace out { namespace in {\n" 1397 "}}; // namespace out::in", 1398 format("namespace out {\n" 1399 "namespace in {\n" 1400 "} // namespace in\n" 1401 "}; // namespace out", 1402 Style)); 1403 1404 Style.NamespaceIndentation = FormatStyle::NI_All; 1405 EXPECT_EQ("namespace out { namespace in {\n" 1406 " int i;\n" 1407 "}} // namespace out::in", 1408 format("namespace out {\n" 1409 "namespace in {\n" 1410 "int i;\n" 1411 "} // namespace in\n" 1412 "} // namespace out", 1413 Style)); 1414 EXPECT_EQ("namespace out { namespace mid {\n" 1415 " namespace in {\n" 1416 " int j;\n" 1417 " } // namespace in\n" 1418 " int k;\n" 1419 "}} // namespace out::mid", 1420 format("namespace out { namespace mid {\n" 1421 "namespace in { int j; } // namespace in\n" 1422 "int k; }} // namespace out::mid", 1423 Style)); 1424 1425 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1426 EXPECT_EQ("namespace out { namespace in {\n" 1427 " int i;\n" 1428 "}} // namespace out::in", 1429 format("namespace out {\n" 1430 "namespace in {\n" 1431 "int i;\n" 1432 "} // namespace in\n" 1433 "} // namespace out", 1434 Style)); 1435 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1436 " int i;\n" 1437 "}}} // namespace out::mid::in", 1438 format("namespace out {\n" 1439 "namespace mid {\n" 1440 "namespace in {\n" 1441 "int i;\n" 1442 "} // namespace in\n" 1443 "} // namespace mid\n" 1444 "} // namespace out", 1445 Style)); 1446 } 1447 1448 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 1449 1450 TEST_F(FormatTest, FormatsInlineASM) { 1451 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1452 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1453 verifyFormat( 1454 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1455 " \"cpuid\\n\\t\"\n" 1456 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1457 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1458 " : \"a\"(value));"); 1459 EXPECT_EQ( 1460 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1461 " __asm {\n" 1462 " mov edx,[that] // vtable in edx\n" 1463 " mov eax,methodIndex\n" 1464 " call [edx][eax*4] // stdcall\n" 1465 " }\n" 1466 "}", 1467 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1468 " __asm {\n" 1469 " mov edx,[that] // vtable in edx\n" 1470 " mov eax,methodIndex\n" 1471 " call [edx][eax*4] // stdcall\n" 1472 " }\n" 1473 "}")); 1474 EXPECT_EQ("_asm {\n" 1475 " xor eax, eax;\n" 1476 " cpuid;\n" 1477 "}", 1478 format("_asm {\n" 1479 " xor eax, eax;\n" 1480 " cpuid;\n" 1481 "}")); 1482 verifyFormat("void function() {\n" 1483 " // comment\n" 1484 " asm(\"\");\n" 1485 "}"); 1486 EXPECT_EQ("__asm {\n" 1487 "}\n" 1488 "int i;", 1489 format("__asm {\n" 1490 "}\n" 1491 "int i;")); 1492 } 1493 1494 TEST_F(FormatTest, FormatTryCatch) { 1495 verifyFormat("try {\n" 1496 " throw a * b;\n" 1497 "} catch (int a) {\n" 1498 " // Do nothing.\n" 1499 "} catch (...) {\n" 1500 " exit(42);\n" 1501 "}"); 1502 1503 // Function-level try statements. 1504 verifyFormat("int f() try { return 4; } catch (...) {\n" 1505 " return 5;\n" 1506 "}"); 1507 verifyFormat("class A {\n" 1508 " int a;\n" 1509 " A() try : a(0) {\n" 1510 " } catch (...) {\n" 1511 " throw;\n" 1512 " }\n" 1513 "};\n"); 1514 1515 // Incomplete try-catch blocks. 1516 verifyIncompleteFormat("try {} catch ("); 1517 } 1518 1519 TEST_F(FormatTest, FormatSEHTryCatch) { 1520 verifyFormat("__try {\n" 1521 " int a = b * c;\n" 1522 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1523 " // Do nothing.\n" 1524 "}"); 1525 1526 verifyFormat("__try {\n" 1527 " int a = b * c;\n" 1528 "} __finally {\n" 1529 " // Do nothing.\n" 1530 "}"); 1531 1532 verifyFormat("DEBUG({\n" 1533 " __try {\n" 1534 " } __finally {\n" 1535 " }\n" 1536 "});\n"); 1537 } 1538 1539 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1540 verifyFormat("try {\n" 1541 " f();\n" 1542 "} catch {\n" 1543 " g();\n" 1544 "}"); 1545 verifyFormat("try {\n" 1546 " f();\n" 1547 "} catch (A a) MACRO(x) {\n" 1548 " g();\n" 1549 "} catch (B b) MACRO(x) {\n" 1550 " g();\n" 1551 "}"); 1552 } 1553 1554 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1555 FormatStyle Style = getLLVMStyle(); 1556 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1557 FormatStyle::BS_WebKit}) { 1558 Style.BreakBeforeBraces = BraceStyle; 1559 verifyFormat("try {\n" 1560 " // something\n" 1561 "} catch (...) {\n" 1562 " // something\n" 1563 "}", 1564 Style); 1565 } 1566 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1567 verifyFormat("try {\n" 1568 " // something\n" 1569 "}\n" 1570 "catch (...) {\n" 1571 " // something\n" 1572 "}", 1573 Style); 1574 verifyFormat("__try {\n" 1575 " // something\n" 1576 "}\n" 1577 "__finally {\n" 1578 " // something\n" 1579 "}", 1580 Style); 1581 verifyFormat("@try {\n" 1582 " // something\n" 1583 "}\n" 1584 "@finally {\n" 1585 " // something\n" 1586 "}", 1587 Style); 1588 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1589 verifyFormat("try\n" 1590 "{\n" 1591 " // something\n" 1592 "}\n" 1593 "catch (...)\n" 1594 "{\n" 1595 " // something\n" 1596 "}", 1597 Style); 1598 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 1599 verifyFormat("try\n" 1600 " {\n" 1601 " // something\n" 1602 " }\n" 1603 "catch (...)\n" 1604 " {\n" 1605 " // something\n" 1606 " }", 1607 Style); 1608 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1609 Style.BraceWrapping.BeforeCatch = true; 1610 verifyFormat("try {\n" 1611 " // something\n" 1612 "}\n" 1613 "catch (...) {\n" 1614 " // something\n" 1615 "}", 1616 Style); 1617 } 1618 1619 TEST_F(FormatTest, StaticInitializers) { 1620 verifyFormat("static SomeClass SC = {1, 'a'};"); 1621 1622 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1623 " 100000000, " 1624 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1625 1626 // Here, everything other than the "}" would fit on a line. 1627 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1628 " 10000000000000000000000000};"); 1629 EXPECT_EQ("S s = {a,\n" 1630 "\n" 1631 " b};", 1632 format("S s = {\n" 1633 " a,\n" 1634 "\n" 1635 " b\n" 1636 "};")); 1637 1638 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1639 // line. However, the formatting looks a bit off and this probably doesn't 1640 // happen often in practice. 1641 verifyFormat("static int Variable[1] = {\n" 1642 " {1000000000000000000000000000000000000}};", 1643 getLLVMStyleWithColumns(40)); 1644 } 1645 1646 TEST_F(FormatTest, DesignatedInitializers) { 1647 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1648 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1649 " .bbbbbbbbbb = 2,\n" 1650 " .cccccccccc = 3,\n" 1651 " .dddddddddd = 4,\n" 1652 " .eeeeeeeeee = 5};"); 1653 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1654 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1655 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1656 " .ccccccccccccccccccccccccccc = 3,\n" 1657 " .ddddddddddddddddddddddddddd = 4,\n" 1658 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1659 1660 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1661 } 1662 1663 TEST_F(FormatTest, NestedStaticInitializers) { 1664 verifyFormat("static A x = {{{}}};\n"); 1665 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 1666 " {init1, init2, init3, init4}}};", 1667 getLLVMStyleWithColumns(50)); 1668 1669 verifyFormat("somes Status::global_reps[3] = {\n" 1670 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1671 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1672 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 1673 getLLVMStyleWithColumns(60)); 1674 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 1675 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1676 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1677 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 1678 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 1679 " {rect.fRight - rect.fLeft, rect.fBottom - " 1680 "rect.fTop}};"); 1681 1682 verifyFormat( 1683 "SomeArrayOfSomeType a = {\n" 1684 " {{1, 2, 3},\n" 1685 " {1, 2, 3},\n" 1686 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 1687 " 333333333333333333333333333333},\n" 1688 " {1, 2, 3},\n" 1689 " {1, 2, 3}}};"); 1690 verifyFormat( 1691 "SomeArrayOfSomeType a = {\n" 1692 " {{1, 2, 3}},\n" 1693 " {{1, 2, 3}},\n" 1694 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 1695 " 333333333333333333333333333333}},\n" 1696 " {{1, 2, 3}},\n" 1697 " {{1, 2, 3}}};"); 1698 1699 verifyFormat("struct {\n" 1700 " unsigned bit;\n" 1701 " const char *const name;\n" 1702 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 1703 " {kOsWin, \"Windows\"},\n" 1704 " {kOsLinux, \"Linux\"},\n" 1705 " {kOsCrOS, \"Chrome OS\"}};"); 1706 verifyFormat("struct {\n" 1707 " unsigned bit;\n" 1708 " const char *const name;\n" 1709 "} kBitsToOs[] = {\n" 1710 " {kOsMac, \"Mac\"},\n" 1711 " {kOsWin, \"Windows\"},\n" 1712 " {kOsLinux, \"Linux\"},\n" 1713 " {kOsCrOS, \"Chrome OS\"},\n" 1714 "};"); 1715 } 1716 1717 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 1718 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1719 " \\\n" 1720 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 1721 } 1722 1723 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 1724 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 1725 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 1726 1727 // Do break defaulted and deleted functions. 1728 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 1729 " default;", 1730 getLLVMStyleWithColumns(40)); 1731 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 1732 " delete;", 1733 getLLVMStyleWithColumns(40)); 1734 } 1735 1736 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 1737 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 1738 getLLVMStyleWithColumns(40)); 1739 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 1740 getLLVMStyleWithColumns(40)); 1741 EXPECT_EQ("#define Q \\\n" 1742 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 1743 " \"aaaaaaaa.cpp\"", 1744 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 1745 getLLVMStyleWithColumns(40))); 1746 } 1747 1748 TEST_F(FormatTest, UnderstandsLinePPDirective) { 1749 EXPECT_EQ("# 123 \"A string literal\"", 1750 format(" # 123 \"A string literal\"")); 1751 } 1752 1753 TEST_F(FormatTest, LayoutUnknownPPDirective) { 1754 EXPECT_EQ("#;", format("#;")); 1755 verifyFormat("#\n;\n;\n;"); 1756 } 1757 1758 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 1759 EXPECT_EQ("#line 42 \"test\"\n", 1760 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 1761 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 1762 getLLVMStyleWithColumns(12))); 1763 } 1764 1765 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 1766 EXPECT_EQ("#line 42 \"test\"", 1767 format("# \\\n line \\\n 42 \\\n \"test\"")); 1768 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 1769 } 1770 1771 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 1772 verifyFormat("#define A \\x20"); 1773 verifyFormat("#define A \\ x20"); 1774 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 1775 verifyFormat("#define A ''"); 1776 verifyFormat("#define A ''qqq"); 1777 verifyFormat("#define A `qqq"); 1778 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 1779 EXPECT_EQ("const char *c = STRINGIFY(\n" 1780 "\\na : b);", 1781 format("const char * c = STRINGIFY(\n" 1782 "\\na : b);")); 1783 1784 verifyFormat("a\r\\"); 1785 verifyFormat("a\v\\"); 1786 verifyFormat("a\f\\"); 1787 } 1788 1789 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 1790 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 1791 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 1792 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 1793 // FIXME: We never break before the macro name. 1794 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 1795 1796 verifyFormat("#define A A\n#define A A"); 1797 verifyFormat("#define A(X) A\n#define A A"); 1798 1799 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 1800 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 1801 } 1802 1803 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 1804 EXPECT_EQ("// somecomment\n" 1805 "#include \"a.h\"\n" 1806 "#define A( \\\n" 1807 " A, B)\n" 1808 "#include \"b.h\"\n" 1809 "// somecomment\n", 1810 format(" // somecomment\n" 1811 " #include \"a.h\"\n" 1812 "#define A(A,\\\n" 1813 " B)\n" 1814 " #include \"b.h\"\n" 1815 " // somecomment\n", 1816 getLLVMStyleWithColumns(13))); 1817 } 1818 1819 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 1820 1821 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 1822 EXPECT_EQ("#define A \\\n" 1823 " c; \\\n" 1824 " e;\n" 1825 "f;", 1826 format("#define A c; e;\n" 1827 "f;", 1828 getLLVMStyleWithColumns(14))); 1829 } 1830 1831 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 1832 1833 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 1834 EXPECT_EQ("int x,\n" 1835 "#define A\n" 1836 " y;", 1837 format("int x,\n#define A\ny;")); 1838 } 1839 1840 TEST_F(FormatTest, HashInMacroDefinition) { 1841 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 1842 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 1843 verifyFormat("#define A \\\n" 1844 " { \\\n" 1845 " f(#c); \\\n" 1846 " }", 1847 getLLVMStyleWithColumns(11)); 1848 1849 verifyFormat("#define A(X) \\\n" 1850 " void function##X()", 1851 getLLVMStyleWithColumns(22)); 1852 1853 verifyFormat("#define A(a, b, c) \\\n" 1854 " void a##b##c()", 1855 getLLVMStyleWithColumns(22)); 1856 1857 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 1858 } 1859 1860 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 1861 EXPECT_EQ("#define A (x)", format("#define A (x)")); 1862 EXPECT_EQ("#define A(x)", format("#define A(x)")); 1863 } 1864 1865 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 1866 EXPECT_EQ("#define A b;", format("#define A \\\n" 1867 " \\\n" 1868 " b;", 1869 getLLVMStyleWithColumns(25))); 1870 EXPECT_EQ("#define A \\\n" 1871 " \\\n" 1872 " a; \\\n" 1873 " b;", 1874 format("#define A \\\n" 1875 " \\\n" 1876 " a; \\\n" 1877 " b;", 1878 getLLVMStyleWithColumns(11))); 1879 EXPECT_EQ("#define A \\\n" 1880 " a; \\\n" 1881 " \\\n" 1882 " b;", 1883 format("#define A \\\n" 1884 " a; \\\n" 1885 " \\\n" 1886 " b;", 1887 getLLVMStyleWithColumns(11))); 1888 } 1889 1890 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 1891 verifyIncompleteFormat("#define A :"); 1892 verifyFormat("#define SOMECASES \\\n" 1893 " case 1: \\\n" 1894 " case 2\n", 1895 getLLVMStyleWithColumns(20)); 1896 verifyFormat("#define MACRO(a) \\\n" 1897 " if (a) \\\n" 1898 " f(); \\\n" 1899 " else \\\n" 1900 " g()", 1901 getLLVMStyleWithColumns(18)); 1902 verifyFormat("#define A template <typename T>"); 1903 verifyIncompleteFormat("#define STR(x) #x\n" 1904 "f(STR(this_is_a_string_literal{));"); 1905 verifyFormat("#pragma omp threadprivate( \\\n" 1906 " y)), // expected-warning", 1907 getLLVMStyleWithColumns(28)); 1908 verifyFormat("#d, = };"); 1909 verifyFormat("#if \"a"); 1910 verifyIncompleteFormat("({\n" 1911 "#define b \\\n" 1912 " } \\\n" 1913 " a\n" 1914 "a", 1915 getLLVMStyleWithColumns(15)); 1916 verifyFormat("#define A \\\n" 1917 " { \\\n" 1918 " {\n" 1919 "#define B \\\n" 1920 " } \\\n" 1921 " }", 1922 getLLVMStyleWithColumns(15)); 1923 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 1924 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 1925 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 1926 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 1927 } 1928 1929 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 1930 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 1931 EXPECT_EQ("class A : public QObject {\n" 1932 " Q_OBJECT\n" 1933 "\n" 1934 " A() {}\n" 1935 "};", 1936 format("class A : public QObject {\n" 1937 " Q_OBJECT\n" 1938 "\n" 1939 " A() {\n}\n" 1940 "} ;")); 1941 EXPECT_EQ("MACRO\n" 1942 "/*static*/ int i;", 1943 format("MACRO\n" 1944 " /*static*/ int i;")); 1945 EXPECT_EQ("SOME_MACRO\n" 1946 "namespace {\n" 1947 "void f();\n" 1948 "} // namespace", 1949 format("SOME_MACRO\n" 1950 " namespace {\n" 1951 "void f( );\n" 1952 "} // namespace")); 1953 // Only if the identifier contains at least 5 characters. 1954 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 1955 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 1956 // Only if everything is upper case. 1957 EXPECT_EQ("class A : public QObject {\n" 1958 " Q_Object A() {}\n" 1959 "};", 1960 format("class A : public QObject {\n" 1961 " Q_Object\n" 1962 " A() {\n}\n" 1963 "} ;")); 1964 1965 // Only if the next line can actually start an unwrapped line. 1966 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 1967 format("SOME_WEIRD_LOG_MACRO\n" 1968 "<< SomeThing;")); 1969 1970 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 1971 "(n, buffers))\n", 1972 getChromiumStyle(FormatStyle::LK_Cpp)); 1973 } 1974 1975 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 1976 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 1977 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 1978 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 1979 "class X {};\n" 1980 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 1981 "int *createScopDetectionPass() { return 0; }", 1982 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 1983 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 1984 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 1985 " class X {};\n" 1986 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 1987 " int *createScopDetectionPass() { return 0; }")); 1988 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 1989 // braces, so that inner block is indented one level more. 1990 EXPECT_EQ("int q() {\n" 1991 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 1992 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 1993 " IPC_END_MESSAGE_MAP()\n" 1994 "}", 1995 format("int q() {\n" 1996 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 1997 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 1998 " IPC_END_MESSAGE_MAP()\n" 1999 "}")); 2000 2001 // Same inside macros. 2002 EXPECT_EQ("#define LIST(L) \\\n" 2003 " L(A) \\\n" 2004 " L(B) \\\n" 2005 " L(C)", 2006 format("#define LIST(L) \\\n" 2007 " L(A) \\\n" 2008 " L(B) \\\n" 2009 " L(C)", 2010 getGoogleStyle())); 2011 2012 // These must not be recognized as macros. 2013 EXPECT_EQ("int q() {\n" 2014 " f(x);\n" 2015 " f(x) {}\n" 2016 " f(x)->g();\n" 2017 " f(x)->*g();\n" 2018 " f(x).g();\n" 2019 " f(x) = x;\n" 2020 " f(x) += x;\n" 2021 " f(x) -= x;\n" 2022 " f(x) *= x;\n" 2023 " f(x) /= x;\n" 2024 " f(x) %= x;\n" 2025 " f(x) &= x;\n" 2026 " f(x) |= x;\n" 2027 " f(x) ^= x;\n" 2028 " f(x) >>= x;\n" 2029 " f(x) <<= x;\n" 2030 " f(x)[y].z();\n" 2031 " LOG(INFO) << x;\n" 2032 " ifstream(x) >> x;\n" 2033 "}\n", 2034 format("int q() {\n" 2035 " f(x)\n;\n" 2036 " f(x)\n {}\n" 2037 " f(x)\n->g();\n" 2038 " f(x)\n->*g();\n" 2039 " f(x)\n.g();\n" 2040 " f(x)\n = x;\n" 2041 " f(x)\n += x;\n" 2042 " f(x)\n -= x;\n" 2043 " f(x)\n *= x;\n" 2044 " f(x)\n /= x;\n" 2045 " f(x)\n %= x;\n" 2046 " f(x)\n &= x;\n" 2047 " f(x)\n |= x;\n" 2048 " f(x)\n ^= x;\n" 2049 " f(x)\n >>= x;\n" 2050 " f(x)\n <<= x;\n" 2051 " f(x)\n[y].z();\n" 2052 " LOG(INFO)\n << x;\n" 2053 " ifstream(x)\n >> x;\n" 2054 "}\n")); 2055 EXPECT_EQ("int q() {\n" 2056 " F(x)\n" 2057 " if (1) {\n" 2058 " }\n" 2059 " F(x)\n" 2060 " while (1) {\n" 2061 " }\n" 2062 " F(x)\n" 2063 " G(x);\n" 2064 " F(x)\n" 2065 " try {\n" 2066 " Q();\n" 2067 " } catch (...) {\n" 2068 " }\n" 2069 "}\n", 2070 format("int q() {\n" 2071 "F(x)\n" 2072 "if (1) {}\n" 2073 "F(x)\n" 2074 "while (1) {}\n" 2075 "F(x)\n" 2076 "G(x);\n" 2077 "F(x)\n" 2078 "try { Q(); } catch (...) {}\n" 2079 "}\n")); 2080 EXPECT_EQ("class A {\n" 2081 " A() : t(0) {}\n" 2082 " A(int i) noexcept() : {}\n" 2083 " A(X x)\n" // FIXME: function-level try blocks are broken. 2084 " try : t(0) {\n" 2085 " } catch (...) {\n" 2086 " }\n" 2087 "};", 2088 format("class A {\n" 2089 " A()\n : t(0) {}\n" 2090 " A(int i)\n noexcept() : {}\n" 2091 " A(X x)\n" 2092 " try : t(0) {} catch (...) {}\n" 2093 "};")); 2094 EXPECT_EQ("class SomeClass {\n" 2095 "public:\n" 2096 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2097 "};", 2098 format("class SomeClass {\n" 2099 "public:\n" 2100 " SomeClass()\n" 2101 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2102 "};")); 2103 EXPECT_EQ("class SomeClass {\n" 2104 "public:\n" 2105 " SomeClass()\n" 2106 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2107 "};", 2108 format("class SomeClass {\n" 2109 "public:\n" 2110 " SomeClass()\n" 2111 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2112 "};", 2113 getLLVMStyleWithColumns(40))); 2114 2115 verifyFormat("MACRO(>)"); 2116 } 2117 2118 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2119 verifyFormat("#define A \\\n" 2120 " f({ \\\n" 2121 " g(); \\\n" 2122 " });", 2123 getLLVMStyleWithColumns(11)); 2124 } 2125 2126 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2127 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2128 } 2129 2130 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2131 verifyFormat("{\n { a #c; }\n}"); 2132 } 2133 2134 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2135 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2136 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2137 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2138 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2139 } 2140 2141 TEST_F(FormatTest, EscapedNewlines) { 2142 EXPECT_EQ( 2143 "#define A \\\n int i; \\\n int j;", 2144 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2145 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2146 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2147 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2148 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2149 } 2150 2151 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2152 verifyFormat("#define A \\\n" 2153 " int v( \\\n" 2154 " a); \\\n" 2155 " int i;", 2156 getLLVMStyleWithColumns(11)); 2157 } 2158 2159 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2160 EXPECT_EQ( 2161 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2162 " \\\n" 2163 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2164 "\n" 2165 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2166 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2167 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2168 "\\\n" 2169 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2170 " \n" 2171 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2172 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2173 } 2174 2175 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2176 EXPECT_EQ("int\n" 2177 "#define A\n" 2178 " a;", 2179 format("int\n#define A\na;")); 2180 verifyFormat("functionCallTo(\n" 2181 " someOtherFunction(\n" 2182 " withSomeParameters, whichInSequence,\n" 2183 " areLongerThanALine(andAnotherCall,\n" 2184 "#define A B\n" 2185 " withMoreParamters,\n" 2186 " whichStronglyInfluenceTheLayout),\n" 2187 " andMoreParameters),\n" 2188 " trailing);", 2189 getLLVMStyleWithColumns(69)); 2190 verifyFormat("Foo::Foo()\n" 2191 "#ifdef BAR\n" 2192 " : baz(0)\n" 2193 "#endif\n" 2194 "{\n" 2195 "}"); 2196 verifyFormat("void f() {\n" 2197 " if (true)\n" 2198 "#ifdef A\n" 2199 " f(42);\n" 2200 " x();\n" 2201 "#else\n" 2202 " g();\n" 2203 " x();\n" 2204 "#endif\n" 2205 "}"); 2206 verifyFormat("void f(param1, param2,\n" 2207 " param3,\n" 2208 "#ifdef A\n" 2209 " param4(param5,\n" 2210 "#ifdef A1\n" 2211 " param6,\n" 2212 "#ifdef A2\n" 2213 " param7),\n" 2214 "#else\n" 2215 " param8),\n" 2216 " param9,\n" 2217 "#endif\n" 2218 " param10,\n" 2219 "#endif\n" 2220 " param11)\n" 2221 "#else\n" 2222 " param12)\n" 2223 "#endif\n" 2224 "{\n" 2225 " x();\n" 2226 "}", 2227 getLLVMStyleWithColumns(28)); 2228 verifyFormat("#if 1\n" 2229 "int i;"); 2230 verifyFormat("#if 1\n" 2231 "#endif\n" 2232 "#if 1\n" 2233 "#else\n" 2234 "#endif\n"); 2235 verifyFormat("DEBUG({\n" 2236 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2237 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2238 "});\n" 2239 "#if a\n" 2240 "#else\n" 2241 "#endif"); 2242 2243 verifyIncompleteFormat("void f(\n" 2244 "#if A\n" 2245 ");\n" 2246 "#else\n" 2247 "#endif"); 2248 } 2249 2250 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2251 verifyFormat("#endif\n" 2252 "#if B"); 2253 } 2254 2255 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2256 FormatStyle SingleLine = getLLVMStyle(); 2257 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2258 verifyFormat("#if 0\n" 2259 "#elif 1\n" 2260 "#endif\n" 2261 "void foo() {\n" 2262 " if (test) foo2();\n" 2263 "}", 2264 SingleLine); 2265 } 2266 2267 TEST_F(FormatTest, LayoutBlockInsideParens) { 2268 verifyFormat("functionCall({ int i; });"); 2269 verifyFormat("functionCall({\n" 2270 " int i;\n" 2271 " int j;\n" 2272 "});"); 2273 verifyFormat("functionCall(\n" 2274 " {\n" 2275 " int i;\n" 2276 " int j;\n" 2277 " },\n" 2278 " aaaa, bbbb, cccc);"); 2279 verifyFormat("functionA(functionB({\n" 2280 " int i;\n" 2281 " int j;\n" 2282 " }),\n" 2283 " aaaa, bbbb, cccc);"); 2284 verifyFormat("functionCall(\n" 2285 " {\n" 2286 " int i;\n" 2287 " int j;\n" 2288 " },\n" 2289 " aaaa, bbbb, // comment\n" 2290 " cccc);"); 2291 verifyFormat("functionA(functionB({\n" 2292 " int i;\n" 2293 " int j;\n" 2294 " }),\n" 2295 " aaaa, bbbb, // comment\n" 2296 " cccc);"); 2297 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2298 verifyFormat("functionCall(aaaa, bbbb, {\n" 2299 " int i;\n" 2300 " int j;\n" 2301 "});"); 2302 verifyFormat( 2303 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2304 " {\n" 2305 " int i; // break\n" 2306 " },\n" 2307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2308 " ccccccccccccccccc));"); 2309 verifyFormat("DEBUG({\n" 2310 " if (a)\n" 2311 " f();\n" 2312 "});"); 2313 } 2314 2315 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2316 EXPECT_EQ("SOME_MACRO { int i; }\n" 2317 "int i;", 2318 format(" SOME_MACRO {int i;} int i;")); 2319 } 2320 2321 TEST_F(FormatTest, LayoutNestedBlocks) { 2322 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2323 " struct s {\n" 2324 " int i;\n" 2325 " };\n" 2326 " s kBitsToOs[] = {{10}};\n" 2327 " for (int i = 0; i < 10; ++i)\n" 2328 " return;\n" 2329 "}"); 2330 verifyFormat("call(parameter, {\n" 2331 " something();\n" 2332 " // Comment using all columns.\n" 2333 " somethingelse();\n" 2334 "});", 2335 getLLVMStyleWithColumns(40)); 2336 verifyFormat("DEBUG( //\n" 2337 " { f(); }, a);"); 2338 verifyFormat("DEBUG( //\n" 2339 " {\n" 2340 " f(); //\n" 2341 " },\n" 2342 " a);"); 2343 2344 EXPECT_EQ("call(parameter, {\n" 2345 " something();\n" 2346 " // Comment too\n" 2347 " // looooooooooong.\n" 2348 " somethingElse();\n" 2349 "});", 2350 format("call(parameter, {\n" 2351 " something();\n" 2352 " // Comment too looooooooooong.\n" 2353 " somethingElse();\n" 2354 "});", 2355 getLLVMStyleWithColumns(29))); 2356 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2357 EXPECT_EQ("DEBUG({ // comment\n" 2358 " int i;\n" 2359 "});", 2360 format("DEBUG({ // comment\n" 2361 "int i;\n" 2362 "});")); 2363 EXPECT_EQ("DEBUG({\n" 2364 " int i;\n" 2365 "\n" 2366 " // comment\n" 2367 " int j;\n" 2368 "});", 2369 format("DEBUG({\n" 2370 " int i;\n" 2371 "\n" 2372 " // comment\n" 2373 " int j;\n" 2374 "});")); 2375 2376 verifyFormat("DEBUG({\n" 2377 " if (a)\n" 2378 " return;\n" 2379 "});"); 2380 verifyGoogleFormat("DEBUG({\n" 2381 " if (a) return;\n" 2382 "});"); 2383 FormatStyle Style = getGoogleStyle(); 2384 Style.ColumnLimit = 45; 2385 verifyFormat("Debug(aaaaa,\n" 2386 " {\n" 2387 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 2388 " },\n" 2389 " a);", 2390 Style); 2391 2392 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 2393 2394 verifyNoCrash("^{v^{a}}"); 2395 } 2396 2397 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 2398 EXPECT_EQ("#define MACRO() \\\n" 2399 " Debug(aaa, /* force line break */ \\\n" 2400 " { \\\n" 2401 " int i; \\\n" 2402 " int j; \\\n" 2403 " })", 2404 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 2405 " { int i; int j; })", 2406 getGoogleStyle())); 2407 2408 EXPECT_EQ("#define A \\\n" 2409 " [] { \\\n" 2410 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2411 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 2412 " }", 2413 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2414 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 2415 getGoogleStyle())); 2416 } 2417 2418 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2419 EXPECT_EQ("{}", format("{}")); 2420 verifyFormat("enum E {};"); 2421 verifyFormat("enum E {}"); 2422 } 2423 2424 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 2425 FormatStyle Style = getLLVMStyle(); 2426 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 2427 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 2428 verifyFormat("FOO_BEGIN\n" 2429 " FOO_ENTRY\n" 2430 "FOO_END", Style); 2431 verifyFormat("FOO_BEGIN\n" 2432 " NESTED_FOO_BEGIN\n" 2433 " NESTED_FOO_ENTRY\n" 2434 " NESTED_FOO_END\n" 2435 "FOO_END", Style); 2436 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 2437 " int x;\n" 2438 " x = 1;\n" 2439 "FOO_END(Baz)", Style); 2440 } 2441 2442 //===----------------------------------------------------------------------===// 2443 // Line break tests. 2444 //===----------------------------------------------------------------------===// 2445 2446 TEST_F(FormatTest, PreventConfusingIndents) { 2447 verifyFormat( 2448 "void f() {\n" 2449 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2450 " parameter, parameter, parameter)),\n" 2451 " SecondLongCall(parameter));\n" 2452 "}"); 2453 verifyFormat( 2454 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2455 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2457 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 2458 verifyFormat( 2459 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2460 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 2461 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 2462 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 2463 verifyFormat( 2464 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 2465 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 2466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 2467 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 2468 verifyFormat("int a = bbbb && ccc &&\n" 2469 " fffff(\n" 2470 "#define A Just forcing a new line\n" 2471 " ddd);"); 2472 } 2473 2474 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 2475 verifyFormat( 2476 "bool aaaaaaa =\n" 2477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 2478 " bbbbbbbb();"); 2479 verifyFormat( 2480 "bool aaaaaaa =\n" 2481 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 2482 " bbbbbbbb();"); 2483 2484 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 2486 " ccccccccc == ddddddddddd;"); 2487 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 2489 " ccccccccc == ddddddddddd;"); 2490 verifyFormat( 2491 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 2492 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 2493 " ccccccccc == ddddddddddd;"); 2494 2495 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2496 " aaaaaa) &&\n" 2497 " bbbbbb && cccccc;"); 2498 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2499 " aaaaaa) >>\n" 2500 " bbbbbb;"); 2501 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 2502 " SourceMgr.getSpellingColumnNumber(\n" 2503 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 2504 " 1);"); 2505 2506 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2507 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 2508 " cccccc) {\n}"); 2509 verifyFormat("b = a &&\n" 2510 " // Comment\n" 2511 " b.c && d;"); 2512 2513 // If the LHS of a comparison is not a binary expression itself, the 2514 // additional linebreak confuses many people. 2515 verifyFormat( 2516 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 2518 "}"); 2519 verifyFormat( 2520 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2521 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2522 "}"); 2523 verifyFormat( 2524 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 2525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2526 "}"); 2527 // Even explicit parentheses stress the precedence enough to make the 2528 // additional break unnecessary. 2529 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2531 "}"); 2532 // This cases is borderline, but with the indentation it is still readable. 2533 verifyFormat( 2534 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2535 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2536 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2537 "}", 2538 getLLVMStyleWithColumns(75)); 2539 2540 // If the LHS is a binary expression, we should still use the additional break 2541 // as otherwise the formatting hides the operator precedence. 2542 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2543 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2544 " 5) {\n" 2545 "}"); 2546 2547 FormatStyle OnePerLine = getLLVMStyle(); 2548 OnePerLine.BinPackParameters = false; 2549 verifyFormat( 2550 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 2553 OnePerLine); 2554 2555 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 2556 " .aaa(aaaaaaaaaaaaa) *\n" 2557 " aaaaaaa +\n" 2558 " aaaaaaa;", 2559 getLLVMStyleWithColumns(40)); 2560 } 2561 2562 TEST_F(FormatTest, ExpressionIndentation) { 2563 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2567 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 2568 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 2569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2570 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 2571 " ccccccccccccccccccccccccccccccccccccccccc;"); 2572 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2575 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2576 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2579 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2580 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2581 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2583 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2584 verifyFormat("if () {\n" 2585 "} else if (aaaaa && bbbbb > // break\n" 2586 " ccccc) {\n" 2587 "}"); 2588 verifyFormat("if () {\n" 2589 "} else if (aaaaa &&\n" 2590 " bbbbb > // break\n" 2591 " ccccc &&\n" 2592 " ddddd) {\n" 2593 "}"); 2594 2595 // Presence of a trailing comment used to change indentation of b. 2596 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 2597 " b;\n" 2598 "return aaaaaaaaaaaaaaaaaaa +\n" 2599 " b; //", 2600 getLLVMStyleWithColumns(30)); 2601 } 2602 2603 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 2604 // Not sure what the best system is here. Like this, the LHS can be found 2605 // immediately above an operator (everything with the same or a higher 2606 // indent). The RHS is aligned right of the operator and so compasses 2607 // everything until something with the same indent as the operator is found. 2608 // FIXME: Is this a good system? 2609 FormatStyle Style = getLLVMStyle(); 2610 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 2611 verifyFormat( 2612 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2613 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2614 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2615 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2616 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2617 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2618 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2619 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2620 " > ccccccccccccccccccccccccccccccccccccccccc;", 2621 Style); 2622 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2623 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2624 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2625 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2626 Style); 2627 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2628 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2629 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2630 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2631 Style); 2632 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2633 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2634 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2635 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2636 Style); 2637 verifyFormat("if () {\n" 2638 "} else if (aaaaa\n" 2639 " && bbbbb // break\n" 2640 " > ccccc) {\n" 2641 "}", 2642 Style); 2643 verifyFormat("return (a)\n" 2644 " // comment\n" 2645 " + b;", 2646 Style); 2647 verifyFormat( 2648 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2649 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2650 " + cc;", 2651 Style); 2652 2653 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2654 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 2655 Style); 2656 2657 // Forced by comments. 2658 verifyFormat( 2659 "unsigned ContentSize =\n" 2660 " sizeof(int16_t) // DWARF ARange version number\n" 2661 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 2662 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 2663 " + sizeof(int8_t); // Segment Size (in bytes)"); 2664 2665 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 2666 " == boost::fusion::at_c<1>(iiii).second;", 2667 Style); 2668 2669 Style.ColumnLimit = 60; 2670 verifyFormat("zzzzzzzzzz\n" 2671 " = bbbbbbbbbbbbbbbbb\n" 2672 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 2673 Style); 2674 } 2675 2676 TEST_F(FormatTest, EnforcedOperatorWraps) { 2677 // Here we'd like to wrap after the || operators, but a comment is forcing an 2678 // earlier wrap. 2679 verifyFormat("bool x = aaaaa //\n" 2680 " || bbbbb\n" 2681 " //\n" 2682 " || cccc;"); 2683 } 2684 2685 TEST_F(FormatTest, NoOperandAlignment) { 2686 FormatStyle Style = getLLVMStyle(); 2687 Style.AlignOperands = false; 2688 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 2689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 2691 Style); 2692 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2693 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2694 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2695 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2696 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2697 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2698 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2699 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2700 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2701 " > ccccccccccccccccccccccccccccccccccccccccc;", 2702 Style); 2703 2704 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2705 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2706 " + cc;", 2707 Style); 2708 verifyFormat("int a = aa\n" 2709 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2710 " * cccccccccccccccccccccccccccccccccccc;\n", 2711 Style); 2712 2713 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 2714 verifyFormat("return (a > b\n" 2715 " // comment1\n" 2716 " // comment2\n" 2717 " || c);", 2718 Style); 2719 } 2720 2721 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 2722 FormatStyle Style = getLLVMStyle(); 2723 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2724 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2726 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2727 Style); 2728 } 2729 2730 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 2731 FormatStyle Style = getLLVMStyle(); 2732 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2733 Style.BinPackArguments = false; 2734 Style.ColumnLimit = 40; 2735 verifyFormat("void test() {\n" 2736 " someFunction(\n" 2737 " this + argument + is + quite\n" 2738 " + long + so + it + gets + wrapped\n" 2739 " + but + remains + bin - packed);\n" 2740 "}", 2741 Style); 2742 verifyFormat("void test() {\n" 2743 " someFunction(arg1,\n" 2744 " this + argument + is\n" 2745 " + quite + long + so\n" 2746 " + it + gets + wrapped\n" 2747 " + but + remains + bin\n" 2748 " - packed,\n" 2749 " arg3);\n" 2750 "}", 2751 Style); 2752 verifyFormat("void test() {\n" 2753 " someFunction(\n" 2754 " arg1,\n" 2755 " this + argument + has\n" 2756 " + anotherFunc(nested,\n" 2757 " calls + whose\n" 2758 " + arguments\n" 2759 " + are + also\n" 2760 " + wrapped,\n" 2761 " in + addition)\n" 2762 " + to + being + bin - packed,\n" 2763 " arg3);\n" 2764 "}", 2765 Style); 2766 2767 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 2768 verifyFormat("void test() {\n" 2769 " someFunction(\n" 2770 " arg1,\n" 2771 " this + argument + has +\n" 2772 " anotherFunc(nested,\n" 2773 " calls + whose +\n" 2774 " arguments +\n" 2775 " are + also +\n" 2776 " wrapped,\n" 2777 " in + addition) +\n" 2778 " to + being + bin - packed,\n" 2779 " arg3);\n" 2780 "}", 2781 Style); 2782 } 2783 2784 TEST_F(FormatTest, ConstructorInitializers) { 2785 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 2786 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 2787 getLLVMStyleWithColumns(45)); 2788 verifyFormat("Constructor()\n" 2789 " : Inttializer(FitsOnTheLine) {}", 2790 getLLVMStyleWithColumns(44)); 2791 verifyFormat("Constructor()\n" 2792 " : Inttializer(FitsOnTheLine) {}", 2793 getLLVMStyleWithColumns(43)); 2794 2795 verifyFormat("template <typename T>\n" 2796 "Constructor() : Initializer(FitsOnTheLine) {}", 2797 getLLVMStyleWithColumns(45)); 2798 2799 verifyFormat( 2800 "SomeClass::Constructor()\n" 2801 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2802 2803 verifyFormat( 2804 "SomeClass::Constructor()\n" 2805 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2806 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 2807 verifyFormat( 2808 "SomeClass::Constructor()\n" 2809 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2810 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2811 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2812 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 2813 " : aaaaaaaaaa(aaaaaa) {}"); 2814 2815 verifyFormat("Constructor()\n" 2816 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2817 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2818 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2819 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 2820 2821 verifyFormat("Constructor()\n" 2822 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2823 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2824 2825 verifyFormat("Constructor(int Parameter = 0)\n" 2826 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 2827 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 2828 verifyFormat("Constructor()\n" 2829 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 2830 "}", 2831 getLLVMStyleWithColumns(60)); 2832 verifyFormat("Constructor()\n" 2833 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2834 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 2835 2836 // Here a line could be saved by splitting the second initializer onto two 2837 // lines, but that is not desirable. 2838 verifyFormat("Constructor()\n" 2839 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 2840 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 2841 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 2842 2843 FormatStyle OnePerLine = getLLVMStyle(); 2844 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 2845 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 2846 verifyFormat("SomeClass::Constructor()\n" 2847 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2848 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2849 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2850 OnePerLine); 2851 verifyFormat("SomeClass::Constructor()\n" 2852 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 2853 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2854 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2855 OnePerLine); 2856 verifyFormat("MyClass::MyClass(int var)\n" 2857 " : some_var_(var), // 4 space indent\n" 2858 " some_other_var_(var + 1) { // lined up\n" 2859 "}", 2860 OnePerLine); 2861 verifyFormat("Constructor()\n" 2862 " : aaaaa(aaaaaa),\n" 2863 " aaaaa(aaaaaa),\n" 2864 " aaaaa(aaaaaa),\n" 2865 " aaaaa(aaaaaa),\n" 2866 " aaaaa(aaaaaa) {}", 2867 OnePerLine); 2868 verifyFormat("Constructor()\n" 2869 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 2870 " aaaaaaaaaaaaaaaaaaaaaa) {}", 2871 OnePerLine); 2872 OnePerLine.BinPackParameters = false; 2873 verifyFormat( 2874 "Constructor()\n" 2875 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2876 " aaaaaaaaaaa().aaa(),\n" 2877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 2878 OnePerLine); 2879 OnePerLine.ColumnLimit = 60; 2880 verifyFormat("Constructor()\n" 2881 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 2882 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 2883 OnePerLine); 2884 2885 EXPECT_EQ("Constructor()\n" 2886 " : // Comment forcing unwanted break.\n" 2887 " aaaa(aaaa) {}", 2888 format("Constructor() :\n" 2889 " // Comment forcing unwanted break.\n" 2890 " aaaa(aaaa) {}")); 2891 } 2892 2893 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 2894 FormatStyle Style = getLLVMStyle(); 2895 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 2896 2897 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 2898 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 2899 getStyleWithColumns(Style, 45)); 2900 verifyFormat("Constructor() :\n" 2901 " Initializer(FitsOnTheLine) {}", 2902 getStyleWithColumns(Style, 44)); 2903 verifyFormat("Constructor() :\n" 2904 " Initializer(FitsOnTheLine) {}", 2905 getStyleWithColumns(Style, 43)); 2906 2907 verifyFormat("template <typename T>\n" 2908 "Constructor() : Initializer(FitsOnTheLine) {}", 2909 getStyleWithColumns(Style, 50)); 2910 2911 verifyFormat( 2912 "SomeClass::Constructor() :\n" 2913 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 2914 Style); 2915 2916 verifyFormat( 2917 "SomeClass::Constructor() :\n" 2918 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2919 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2920 Style); 2921 verifyFormat( 2922 "SomeClass::Constructor() :\n" 2923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2924 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 2925 Style); 2926 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2927 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 2928 " aaaaaaaaaa(aaaaaa) {}", 2929 Style); 2930 2931 verifyFormat("Constructor() :\n" 2932 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2933 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2934 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2935 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 2936 Style); 2937 2938 verifyFormat("Constructor() :\n" 2939 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 2941 Style); 2942 2943 verifyFormat("Constructor(int Parameter = 0) :\n" 2944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 2945 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 2946 Style); 2947 verifyFormat("Constructor() :\n" 2948 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 2949 "}", 2950 getStyleWithColumns(Style, 60)); 2951 verifyFormat("Constructor() :\n" 2952 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2953 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 2954 Style); 2955 2956 // Here a line could be saved by splitting the second initializer onto two 2957 // lines, but that is not desirable. 2958 verifyFormat("Constructor() :\n" 2959 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 2960 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 2961 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 2962 Style); 2963 2964 FormatStyle OnePerLine = Style; 2965 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 2966 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 2967 verifyFormat("SomeClass::Constructor() :\n" 2968 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2969 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2970 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2971 OnePerLine); 2972 verifyFormat("SomeClass::Constructor() :\n" 2973 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 2974 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2975 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 2976 OnePerLine); 2977 verifyFormat("MyClass::MyClass(int var) :\n" 2978 " some_var_(var), // 4 space indent\n" 2979 " some_other_var_(var + 1) { // lined up\n" 2980 "}", 2981 OnePerLine); 2982 verifyFormat("Constructor() :\n" 2983 " aaaaa(aaaaaa),\n" 2984 " aaaaa(aaaaaa),\n" 2985 " aaaaa(aaaaaa),\n" 2986 " aaaaa(aaaaaa),\n" 2987 " aaaaa(aaaaaa) {}", 2988 OnePerLine); 2989 verifyFormat("Constructor() :\n" 2990 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 2991 " aaaaaaaaaaaaaaaaaaaaaa) {}", 2992 OnePerLine); 2993 OnePerLine.BinPackParameters = false; 2994 verifyFormat( 2995 "Constructor() :\n" 2996 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2997 " aaaaaaaaaaa().aaa(),\n" 2998 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 2999 OnePerLine); 3000 OnePerLine.ColumnLimit = 60; 3001 verifyFormat("Constructor() :\n" 3002 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3003 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3004 OnePerLine); 3005 3006 EXPECT_EQ("Constructor() :\n" 3007 " // Comment forcing unwanted break.\n" 3008 " aaaa(aaaa) {}", 3009 format("Constructor() :\n" 3010 " // Comment forcing unwanted break.\n" 3011 " aaaa(aaaa) {}", 3012 Style)); 3013 3014 Style.ColumnLimit = 0; 3015 verifyFormat("SomeClass::Constructor() :\n" 3016 " a(a) {}", 3017 Style); 3018 verifyFormat("SomeClass::Constructor() noexcept :\n" 3019 " a(a) {}", 3020 Style); 3021 verifyFormat("SomeClass::Constructor() :\n" 3022 " a(a), b(b), c(c) {}", 3023 Style); 3024 verifyFormat("SomeClass::Constructor() :\n" 3025 " a(a) {\n" 3026 " foo();\n" 3027 " bar();\n" 3028 "}", 3029 Style); 3030 3031 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3032 verifyFormat("SomeClass::Constructor() :\n" 3033 " a(a), b(b), c(c) {\n" 3034 "}", 3035 Style); 3036 verifyFormat("SomeClass::Constructor() :\n" 3037 " a(a) {\n" 3038 "}", 3039 Style); 3040 3041 Style.ColumnLimit = 80; 3042 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3043 Style.ConstructorInitializerIndentWidth = 2; 3044 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3045 Style); 3046 verifyFormat("SomeClass::Constructor() :\n" 3047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3048 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3049 Style); 3050 } 3051 3052 TEST_F(FormatTest, MemoizationTests) { 3053 // This breaks if the memoization lookup does not take \c Indent and 3054 // \c LastSpace into account. 3055 verifyFormat( 3056 "extern CFRunLoopTimerRef\n" 3057 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3058 " CFTimeInterval interval, CFOptionFlags flags,\n" 3059 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3060 " CFRunLoopTimerContext *context) {}"); 3061 3062 // Deep nesting somewhat works around our memoization. 3063 verifyFormat( 3064 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3065 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3066 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3067 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3068 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3069 getLLVMStyleWithColumns(65)); 3070 verifyFormat( 3071 "aaaaa(\n" 3072 " aaaaa,\n" 3073 " aaaaa(\n" 3074 " aaaaa,\n" 3075 " aaaaa(\n" 3076 " aaaaa,\n" 3077 " aaaaa(\n" 3078 " aaaaa,\n" 3079 " aaaaa(\n" 3080 " aaaaa,\n" 3081 " aaaaa(\n" 3082 " aaaaa,\n" 3083 " aaaaa(\n" 3084 " aaaaa,\n" 3085 " aaaaa(\n" 3086 " aaaaa,\n" 3087 " aaaaa(\n" 3088 " aaaaa,\n" 3089 " aaaaa(\n" 3090 " aaaaa,\n" 3091 " aaaaa(\n" 3092 " aaaaa,\n" 3093 " aaaaa(\n" 3094 " aaaaa,\n" 3095 " aaaaa))))))))))));", 3096 getLLVMStyleWithColumns(65)); 3097 verifyFormat( 3098 "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" 3099 " a),\n" 3100 " a),\n" 3101 " a),\n" 3102 " a),\n" 3103 " a),\n" 3104 " a),\n" 3105 " a),\n" 3106 " a),\n" 3107 " a),\n" 3108 " a),\n" 3109 " a),\n" 3110 " a),\n" 3111 " a),\n" 3112 " a),\n" 3113 " a),\n" 3114 " a),\n" 3115 " a)", 3116 getLLVMStyleWithColumns(65)); 3117 3118 // This test takes VERY long when memoization is broken. 3119 FormatStyle OnePerLine = getLLVMStyle(); 3120 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3121 OnePerLine.BinPackParameters = false; 3122 std::string input = "Constructor()\n" 3123 " : aaaa(a,\n"; 3124 for (unsigned i = 0, e = 80; i != e; ++i) { 3125 input += " a,\n"; 3126 } 3127 input += " a) {}"; 3128 verifyFormat(input, OnePerLine); 3129 } 3130 3131 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3132 verifyFormat( 3133 "void f() {\n" 3134 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3135 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3136 " f();\n" 3137 "}"); 3138 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3139 " Intervals[i - 1].getRange().getLast()) {\n}"); 3140 } 3141 3142 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3143 // Principially, we break function declarations in a certain order: 3144 // 1) break amongst arguments. 3145 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3146 " Cccccccccccccc cccccccccccccc);"); 3147 verifyFormat("template <class TemplateIt>\n" 3148 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3149 " TemplateIt *stop) {}"); 3150 3151 // 2) break after return type. 3152 verifyFormat( 3153 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3154 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3155 getGoogleStyle()); 3156 3157 // 3) break after (. 3158 verifyFormat( 3159 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3160 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3161 getGoogleStyle()); 3162 3163 // 4) break before after nested name specifiers. 3164 verifyFormat( 3165 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3166 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3167 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3168 getGoogleStyle()); 3169 3170 // However, there are exceptions, if a sufficient amount of lines can be 3171 // saved. 3172 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3173 // more adjusting. 3174 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3175 " Cccccccccccccc cccccccccc,\n" 3176 " Cccccccccccccc cccccccccc,\n" 3177 " Cccccccccccccc cccccccccc,\n" 3178 " Cccccccccccccc cccccccccc);"); 3179 verifyFormat( 3180 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3181 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3182 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3183 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3184 getGoogleStyle()); 3185 verifyFormat( 3186 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3187 " Cccccccccccccc cccccccccc,\n" 3188 " Cccccccccccccc cccccccccc,\n" 3189 " Cccccccccccccc cccccccccc,\n" 3190 " Cccccccccccccc cccccccccc,\n" 3191 " Cccccccccccccc cccccccccc,\n" 3192 " Cccccccccccccc cccccccccc);"); 3193 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3194 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3195 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3196 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3197 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3198 3199 // Break after multi-line parameters. 3200 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3203 " bbbb bbbb);"); 3204 verifyFormat("void SomeLoooooooooooongFunction(\n" 3205 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3206 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3207 " int bbbbbbbbbbbbb);"); 3208 3209 // Treat overloaded operators like other functions. 3210 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3211 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3212 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3213 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3214 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3215 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3216 verifyGoogleFormat( 3217 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3218 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3219 verifyGoogleFormat( 3220 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3221 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3222 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3223 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3224 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3225 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3226 verifyGoogleFormat( 3227 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3228 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3229 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3230 verifyGoogleFormat( 3231 "template <typename T>\n" 3232 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3233 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3234 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3235 3236 FormatStyle Style = getLLVMStyle(); 3237 Style.PointerAlignment = FormatStyle::PAS_Left; 3238 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3239 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3240 Style); 3241 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3242 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3243 Style); 3244 } 3245 3246 TEST_F(FormatTest, TrailingReturnType) { 3247 verifyFormat("auto foo() -> int;\n"); 3248 verifyFormat("struct S {\n" 3249 " auto bar() const -> int;\n" 3250 "};"); 3251 verifyFormat("template <size_t Order, typename T>\n" 3252 "auto load_img(const std::string &filename)\n" 3253 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3254 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3255 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3256 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3257 verifyFormat("template <typename T>\n" 3258 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3259 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3260 3261 // Not trailing return types. 3262 verifyFormat("void f() { auto a = b->c(); }"); 3263 } 3264 3265 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3266 // Avoid breaking before trailing 'const' or other trailing annotations, if 3267 // they are not function-like. 3268 FormatStyle Style = getGoogleStyle(); 3269 Style.ColumnLimit = 47; 3270 verifyFormat("void someLongFunction(\n" 3271 " int someLoooooooooooooongParameter) const {\n}", 3272 getLLVMStyleWithColumns(47)); 3273 verifyFormat("LoooooongReturnType\n" 3274 "someLoooooooongFunction() const {}", 3275 getLLVMStyleWithColumns(47)); 3276 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3277 " const {}", 3278 Style); 3279 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3280 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3281 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3282 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3283 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3284 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3285 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3286 " aaaaaaaaaaa aaaaa) const override;"); 3287 verifyGoogleFormat( 3288 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3289 " const override;"); 3290 3291 // Even if the first parameter has to be wrapped. 3292 verifyFormat("void someLongFunction(\n" 3293 " int someLongParameter) const {}", 3294 getLLVMStyleWithColumns(46)); 3295 verifyFormat("void someLongFunction(\n" 3296 " int someLongParameter) const {}", 3297 Style); 3298 verifyFormat("void someLongFunction(\n" 3299 " int someLongParameter) override {}", 3300 Style); 3301 verifyFormat("void someLongFunction(\n" 3302 " int someLongParameter) OVERRIDE {}", 3303 Style); 3304 verifyFormat("void someLongFunction(\n" 3305 " int someLongParameter) final {}", 3306 Style); 3307 verifyFormat("void someLongFunction(\n" 3308 " int someLongParameter) FINAL {}", 3309 Style); 3310 verifyFormat("void someLongFunction(\n" 3311 " int parameter) const override {}", 3312 Style); 3313 3314 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3315 verifyFormat("void someLongFunction(\n" 3316 " int someLongParameter) const\n" 3317 "{\n" 3318 "}", 3319 Style); 3320 3321 // Unless these are unknown annotations. 3322 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3323 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3324 " LONG_AND_UGLY_ANNOTATION;"); 3325 3326 // Breaking before function-like trailing annotations is fine to keep them 3327 // close to their arguments. 3328 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3329 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3330 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3331 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3332 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3333 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3334 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3335 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3336 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3337 3338 verifyFormat( 3339 "void aaaaaaaaaaaaaaaaaa()\n" 3340 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3341 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3342 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3343 " __attribute__((unused));"); 3344 verifyGoogleFormat( 3345 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3346 " GUARDED_BY(aaaaaaaaaaaa);"); 3347 verifyGoogleFormat( 3348 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3349 " GUARDED_BY(aaaaaaaaaaaa);"); 3350 verifyGoogleFormat( 3351 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3352 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3353 verifyGoogleFormat( 3354 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3355 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3356 } 3357 3358 TEST_F(FormatTest, FunctionAnnotations) { 3359 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3360 "int OldFunction(const string ¶meter) {}"); 3361 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3362 "string OldFunction(const string ¶meter) {}"); 3363 verifyFormat("template <typename T>\n" 3364 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3365 "string OldFunction(const string ¶meter) {}"); 3366 3367 // Not function annotations. 3368 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3369 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3370 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3371 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3372 verifyFormat("MACRO(abc).function() // wrap\n" 3373 " << abc;"); 3374 verifyFormat("MACRO(abc)->function() // wrap\n" 3375 " << abc;"); 3376 verifyFormat("MACRO(abc)::function() // wrap\n" 3377 " << abc;"); 3378 } 3379 3380 TEST_F(FormatTest, BreaksDesireably) { 3381 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3382 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3383 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3384 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3386 "}"); 3387 3388 verifyFormat( 3389 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3391 3392 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3393 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3394 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3395 3396 verifyFormat( 3397 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3398 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3400 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 3402 3403 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3404 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3405 3406 verifyFormat( 3407 "void f() {\n" 3408 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 3409 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3410 "}"); 3411 verifyFormat( 3412 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3414 verifyFormat( 3415 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3417 verifyFormat( 3418 "aaaaaa(aaa,\n" 3419 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3421 " aaaa);"); 3422 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3425 3426 // Indent consistently independent of call expression and unary operator. 3427 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3428 " dddddddddddddddddddddddddddddd));"); 3429 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3430 " dddddddddddddddddddddddddddddd));"); 3431 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 3432 " dddddddddddddddddddddddddddddd));"); 3433 3434 // This test case breaks on an incorrect memoization, i.e. an optimization not 3435 // taking into account the StopAt value. 3436 verifyFormat( 3437 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3438 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3439 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3440 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3441 3442 verifyFormat("{\n {\n {\n" 3443 " Annotation.SpaceRequiredBefore =\n" 3444 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 3445 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 3446 " }\n }\n}"); 3447 3448 // Break on an outer level if there was a break on an inner level. 3449 EXPECT_EQ("f(g(h(a, // comment\n" 3450 " b, c),\n" 3451 " d, e),\n" 3452 " x, y);", 3453 format("f(g(h(a, // comment\n" 3454 " b, c), d, e), x, y);")); 3455 3456 // Prefer breaking similar line breaks. 3457 verifyFormat( 3458 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 3459 " NSTrackingMouseEnteredAndExited |\n" 3460 " NSTrackingActiveAlways;"); 3461 } 3462 3463 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 3464 FormatStyle NoBinPacking = getGoogleStyle(); 3465 NoBinPacking.BinPackParameters = false; 3466 NoBinPacking.BinPackArguments = true; 3467 verifyFormat("void f() {\n" 3468 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 3469 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3470 "}", 3471 NoBinPacking); 3472 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 3473 " int aaaaaaaaaaaaaaaaaaaa,\n" 3474 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3475 NoBinPacking); 3476 3477 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3478 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3479 " vector<int> bbbbbbbbbbbbbbb);", 3480 NoBinPacking); 3481 // FIXME: This behavior difference is probably not wanted. However, currently 3482 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 3483 // template arguments from BreakBeforeParameter being set because of the 3484 // one-per-line formatting. 3485 verifyFormat( 3486 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3487 " aaaaaaaaaa> aaaaaaaaaa);", 3488 NoBinPacking); 3489 verifyFormat( 3490 "void fffffffffff(\n" 3491 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 3492 " aaaaaaaaaa);"); 3493 } 3494 3495 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 3496 FormatStyle NoBinPacking = getGoogleStyle(); 3497 NoBinPacking.BinPackParameters = false; 3498 NoBinPacking.BinPackArguments = false; 3499 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 3500 " aaaaaaaaaaaaaaaaaaaa,\n" 3501 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 3502 NoBinPacking); 3503 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 3504 " aaaaaaaaaaaaa,\n" 3505 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 3506 NoBinPacking); 3507 verifyFormat( 3508 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3509 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3511 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3512 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 3513 NoBinPacking); 3514 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3515 " .aaaaaaaaaaaaaaaaaa();", 3516 NoBinPacking); 3517 verifyFormat("void f() {\n" 3518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3519 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 3520 "}", 3521 NoBinPacking); 3522 3523 verifyFormat( 3524 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3525 " aaaaaaaaaaaa,\n" 3526 " aaaaaaaaaaaa);", 3527 NoBinPacking); 3528 verifyFormat( 3529 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 3530 " ddddddddddddddddddddddddddddd),\n" 3531 " test);", 3532 NoBinPacking); 3533 3534 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3535 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 3536 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 3537 " aaaaaaaaaaaaaaaaaa;", 3538 NoBinPacking); 3539 verifyFormat("a(\"a\"\n" 3540 " \"a\",\n" 3541 " a);"); 3542 3543 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3544 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 3545 " aaaaaaaaa,\n" 3546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3547 NoBinPacking); 3548 verifyFormat( 3549 "void f() {\n" 3550 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3551 " .aaaaaaa();\n" 3552 "}", 3553 NoBinPacking); 3554 verifyFormat( 3555 "template <class SomeType, class SomeOtherType>\n" 3556 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 3557 NoBinPacking); 3558 } 3559 3560 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 3561 FormatStyle Style = getLLVMStyleWithColumns(15); 3562 Style.ExperimentalAutoDetectBinPacking = true; 3563 EXPECT_EQ("aaa(aaaa,\n" 3564 " aaaa,\n" 3565 " aaaa);\n" 3566 "aaa(aaaa,\n" 3567 " aaaa,\n" 3568 " aaaa);", 3569 format("aaa(aaaa,\n" // one-per-line 3570 " aaaa,\n" 3571 " aaaa );\n" 3572 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3573 Style)); 3574 EXPECT_EQ("aaa(aaaa, aaaa,\n" 3575 " aaaa);\n" 3576 "aaa(aaaa, aaaa,\n" 3577 " aaaa);", 3578 format("aaa(aaaa, aaaa,\n" // bin-packed 3579 " aaaa );\n" 3580 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3581 Style)); 3582 } 3583 3584 TEST_F(FormatTest, FormatsBuilderPattern) { 3585 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 3586 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 3587 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 3588 " .StartsWith(\".init\", ORDER_INIT)\n" 3589 " .StartsWith(\".fini\", ORDER_FINI)\n" 3590 " .StartsWith(\".hash\", ORDER_HASH)\n" 3591 " .Default(ORDER_TEXT);\n"); 3592 3593 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 3594 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 3595 verifyFormat( 3596 "aaaaaaa->aaaaaaa\n" 3597 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3598 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3599 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3600 verifyFormat( 3601 "aaaaaaa->aaaaaaa\n" 3602 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3603 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3604 verifyFormat( 3605 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 3606 " aaaaaaaaaaaaaa);"); 3607 verifyFormat( 3608 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 3609 " aaaaaa->aaaaaaaaaaaa()\n" 3610 " ->aaaaaaaaaaaaaaaa(\n" 3611 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3612 " ->aaaaaaaaaaaaaaaaa();"); 3613 verifyGoogleFormat( 3614 "void f() {\n" 3615 " someo->Add((new util::filetools::Handler(dir))\n" 3616 " ->OnEvent1(NewPermanentCallback(\n" 3617 " this, &HandlerHolderClass::EventHandlerCBA))\n" 3618 " ->OnEvent2(NewPermanentCallback(\n" 3619 " this, &HandlerHolderClass::EventHandlerCBB))\n" 3620 " ->OnEvent3(NewPermanentCallback(\n" 3621 " this, &HandlerHolderClass::EventHandlerCBC))\n" 3622 " ->OnEvent5(NewPermanentCallback(\n" 3623 " this, &HandlerHolderClass::EventHandlerCBD))\n" 3624 " ->OnEvent6(NewPermanentCallback(\n" 3625 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 3626 "}"); 3627 3628 verifyFormat( 3629 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 3630 verifyFormat("aaaaaaaaaaaaaaa()\n" 3631 " .aaaaaaaaaaaaaaa()\n" 3632 " .aaaaaaaaaaaaaaa()\n" 3633 " .aaaaaaaaaaaaaaa()\n" 3634 " .aaaaaaaaaaaaaaa();"); 3635 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3636 " .aaaaaaaaaaaaaaa()\n" 3637 " .aaaaaaaaaaaaaaa()\n" 3638 " .aaaaaaaaaaaaaaa();"); 3639 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3640 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3641 " .aaaaaaaaaaaaaaa();"); 3642 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 3643 " ->aaaaaaaaaaaaaae(0)\n" 3644 " ->aaaaaaaaaaaaaaa();"); 3645 3646 // Don't linewrap after very short segments. 3647 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3648 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3649 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3650 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3651 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3652 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3653 verifyFormat("aaa()\n" 3654 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3655 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3656 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3657 3658 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3659 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3660 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 3661 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3662 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 3664 3665 // Prefer not to break after empty parentheses. 3666 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 3667 " First->LastNewlineOffset);"); 3668 3669 // Prefer not to create "hanging" indents. 3670 verifyFormat( 3671 "return !soooooooooooooome_map\n" 3672 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3673 " .second;"); 3674 verifyFormat( 3675 "return aaaaaaaaaaaaaaaa\n" 3676 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 3677 " .aaaa(aaaaaaaaaaaaaa);"); 3678 // No hanging indent here. 3679 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 3680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3681 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 3682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3683 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 3684 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3685 getLLVMStyleWithColumns(60)); 3686 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 3687 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 3688 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3689 getLLVMStyleWithColumns(59)); 3690 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3691 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3692 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3693 } 3694 3695 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 3696 verifyFormat( 3697 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3698 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 3699 verifyFormat( 3700 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 3701 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 3702 3703 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3704 " ccccccccccccccccccccccccc) {\n}"); 3705 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 3706 " ccccccccccccccccccccccccc) {\n}"); 3707 3708 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3709 " ccccccccccccccccccccccccc) {\n}"); 3710 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 3711 " ccccccccccccccccccccccccc) {\n}"); 3712 3713 verifyFormat( 3714 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 3715 " ccccccccccccccccccccccccc) {\n}"); 3716 verifyFormat( 3717 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 3718 " ccccccccccccccccccccccccc) {\n}"); 3719 3720 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 3721 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 3722 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 3723 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3724 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 3725 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 3726 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 3727 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3728 3729 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 3730 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 3731 " aaaaaaaaaaaaaaa != aa) {\n}"); 3732 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 3733 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 3734 " aaaaaaaaaaaaaaa != aa) {\n}"); 3735 } 3736 3737 TEST_F(FormatTest, BreaksAfterAssignments) { 3738 verifyFormat( 3739 "unsigned Cost =\n" 3740 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 3741 " SI->getPointerAddressSpaceee());\n"); 3742 verifyFormat( 3743 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 3744 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 3745 3746 verifyFormat( 3747 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 3748 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 3749 verifyFormat("unsigned OriginalStartColumn =\n" 3750 " SourceMgr.getSpellingColumnNumber(\n" 3751 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 3752 " 1;"); 3753 } 3754 3755 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 3756 FormatStyle Style = getLLVMStyle(); 3757 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3758 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 3759 Style); 3760 3761 Style.PenaltyBreakAssignment = 20; 3762 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3763 " cccccccccccccccccccccccccc;", 3764 Style); 3765 } 3766 3767 TEST_F(FormatTest, AlignsAfterAssignments) { 3768 verifyFormat( 3769 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3770 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3771 verifyFormat( 3772 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3773 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3774 verifyFormat( 3775 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3776 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3777 verifyFormat( 3778 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3779 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3780 verifyFormat( 3781 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3782 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3783 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 3784 } 3785 3786 TEST_F(FormatTest, AlignsAfterReturn) { 3787 verifyFormat( 3788 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3789 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3790 verifyFormat( 3791 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3792 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3793 verifyFormat( 3794 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3795 " aaaaaaaaaaaaaaaaaaaaaa();"); 3796 verifyFormat( 3797 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3798 " aaaaaaaaaaaaaaaaaaaaaa());"); 3799 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3801 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 3803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3804 verifyFormat("return\n" 3805 " // true if code is one of a or b.\n" 3806 " code == a || code == b;"); 3807 } 3808 3809 TEST_F(FormatTest, AlignsAfterOpenBracket) { 3810 verifyFormat( 3811 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 3812 " aaaaaaaaa aaaaaaa) {}"); 3813 verifyFormat( 3814 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 3815 " aaaaaaaaaaa aaaaaaaaa);"); 3816 verifyFormat( 3817 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 3818 " aaaaaaaaaaaaaaaaaaaaa));"); 3819 FormatStyle Style = getLLVMStyle(); 3820 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3821 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3822 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 3823 Style); 3824 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 3825 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 3826 Style); 3827 verifyFormat("SomeLongVariableName->someFunction(\n" 3828 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 3829 Style); 3830 verifyFormat( 3831 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 3832 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3833 Style); 3834 verifyFormat( 3835 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 3836 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3837 Style); 3838 verifyFormat( 3839 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 3840 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 3841 Style); 3842 3843 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 3844 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 3845 " b));", 3846 Style); 3847 3848 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 3849 Style.BinPackArguments = false; 3850 Style.BinPackParameters = false; 3851 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3852 " aaaaaaaaaaa aaaaaaaa,\n" 3853 " aaaaaaaaa aaaaaaa,\n" 3854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3855 Style); 3856 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 3857 " aaaaaaaaaaa aaaaaaaaa,\n" 3858 " aaaaaaaaaaa aaaaaaaaa,\n" 3859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3860 Style); 3861 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 3862 " aaaaaaaaaaaaaaa,\n" 3863 " aaaaaaaaaaaaaaaaaaaaa,\n" 3864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 3865 Style); 3866 verifyFormat( 3867 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 3868 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 3869 Style); 3870 verifyFormat( 3871 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 3872 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 3873 Style); 3874 verifyFormat( 3875 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3876 " aaaaaaaaaaaaaaaaaaaaa(\n" 3877 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 3878 " aaaaaaaaaaaaaaaa);", 3879 Style); 3880 verifyFormat( 3881 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3882 " aaaaaaaaaaaaaaaaaaaaa(\n" 3883 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 3884 " aaaaaaaaaaaaaaaa);", 3885 Style); 3886 } 3887 3888 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 3889 FormatStyle Style = getLLVMStyleWithColumns(40); 3890 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3891 " bbbbbbbbbbbbbbbbbbbbbb);", 3892 Style); 3893 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 3894 Style.AlignOperands = false; 3895 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3896 " bbbbbbbbbbbbbbbbbbbbbb);", 3897 Style); 3898 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3899 Style.AlignOperands = true; 3900 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3901 " bbbbbbbbbbbbbbbbbbbbbb);", 3902 Style); 3903 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3904 Style.AlignOperands = false; 3905 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 3906 " bbbbbbbbbbbbbbbbbbbbbb);", 3907 Style); 3908 } 3909 3910 TEST_F(FormatTest, BreaksConditionalExpressions) { 3911 verifyFormat( 3912 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3913 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3914 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3915 verifyFormat( 3916 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 3917 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3918 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3919 verifyFormat( 3920 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3921 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3922 verifyFormat( 3923 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 3924 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3925 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3926 verifyFormat( 3927 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 3928 " : aaaaaaaaaaaaa);"); 3929 verifyFormat( 3930 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3931 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3932 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3933 " aaaaaaaaaaaaa);"); 3934 verifyFormat( 3935 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3936 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3937 " aaaaaaaaaaaaa);"); 3938 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3939 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3941 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3943 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3945 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3947 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3948 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3949 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3950 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3952 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3954 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3955 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3956 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3957 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3958 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 3959 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3960 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3961 " : aaaaaaaaaaaaaaaa;"); 3962 verifyFormat( 3963 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3964 " ? aaaaaaaaaaaaaaa\n" 3965 " : aaaaaaaaaaaaaaa;"); 3966 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 3967 " aaaaaaaaa\n" 3968 " ? b\n" 3969 " : c);"); 3970 verifyFormat("return aaaa == bbbb\n" 3971 " // comment\n" 3972 " ? aaaa\n" 3973 " : bbbb;"); 3974 verifyFormat("unsigned Indent =\n" 3975 " format(TheLine.First,\n" 3976 " IndentForLevel[TheLine.Level] >= 0\n" 3977 " ? IndentForLevel[TheLine.Level]\n" 3978 " : TheLine * 2,\n" 3979 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 3980 getLLVMStyleWithColumns(60)); 3981 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 3982 " ? aaaaaaaaaaaaaaa\n" 3983 " : bbbbbbbbbbbbbbb //\n" 3984 " ? ccccccccccccccc\n" 3985 " : ddddddddddddddd;"); 3986 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 3987 " ? aaaaaaaaaaaaaaa\n" 3988 " : (bbbbbbbbbbbbbbb //\n" 3989 " ? ccccccccccccccc\n" 3990 " : ddddddddddddddd);"); 3991 verifyFormat( 3992 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3993 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3994 " aaaaaaaaaaaaaaaaaaaaa +\n" 3995 " aaaaaaaaaaaaaaaaaaaaa\n" 3996 " : aaaaaaaaaa;"); 3997 verifyFormat( 3998 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3999 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4000 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4001 4002 FormatStyle NoBinPacking = getLLVMStyle(); 4003 NoBinPacking.BinPackArguments = false; 4004 verifyFormat( 4005 "void f() {\n" 4006 " g(aaa,\n" 4007 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4009 " ? aaaaaaaaaaaaaaa\n" 4010 " : aaaaaaaaaaaaaaa);\n" 4011 "}", 4012 NoBinPacking); 4013 verifyFormat( 4014 "void f() {\n" 4015 " g(aaa,\n" 4016 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4018 " ?: aaaaaaaaaaaaaaa);\n" 4019 "}", 4020 NoBinPacking); 4021 4022 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4023 " // comment.\n" 4024 " ccccccccccccccccccccccccccccccccccccccc\n" 4025 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4026 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4027 4028 // Assignments in conditional expressions. Apparently not uncommon :-(. 4029 verifyFormat("return a != b\n" 4030 " // comment\n" 4031 " ? a = b\n" 4032 " : a = b;"); 4033 verifyFormat("return a != b\n" 4034 " // comment\n" 4035 " ? a = a != b\n" 4036 " // comment\n" 4037 " ? a = b\n" 4038 " : a\n" 4039 " : a;\n"); 4040 verifyFormat("return a != b\n" 4041 " // comment\n" 4042 " ? a\n" 4043 " : a = a != b\n" 4044 " // comment\n" 4045 " ? a = b\n" 4046 " : a;"); 4047 } 4048 4049 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4050 FormatStyle Style = getLLVMStyle(); 4051 Style.BreakBeforeTernaryOperators = false; 4052 Style.ColumnLimit = 70; 4053 verifyFormat( 4054 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4057 Style); 4058 verifyFormat( 4059 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4060 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4061 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4062 Style); 4063 verifyFormat( 4064 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4066 Style); 4067 verifyFormat( 4068 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4069 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4071 Style); 4072 verifyFormat( 4073 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4074 " aaaaaaaaaaaaa);", 4075 Style); 4076 verifyFormat( 4077 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4078 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4079 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4080 " aaaaaaaaaaaaa);", 4081 Style); 4082 verifyFormat( 4083 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4084 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4085 " aaaaaaaaaaaaa);", 4086 Style); 4087 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4091 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4092 Style); 4093 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4095 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4099 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4100 Style); 4101 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4102 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4105 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4106 Style); 4107 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4108 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4109 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4110 Style); 4111 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4114 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4115 Style); 4116 verifyFormat( 4117 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4118 " aaaaaaaaaaaaaaa :\n" 4119 " aaaaaaaaaaaaaaa;", 4120 Style); 4121 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4122 " aaaaaaaaa ?\n" 4123 " b :\n" 4124 " c);", 4125 Style); 4126 verifyFormat("unsigned Indent =\n" 4127 " format(TheLine.First,\n" 4128 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4129 " IndentForLevel[TheLine.Level] :\n" 4130 " TheLine * 2,\n" 4131 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4132 Style); 4133 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4134 " aaaaaaaaaaaaaaa :\n" 4135 " bbbbbbbbbbbbbbb ? //\n" 4136 " ccccccccccccccc :\n" 4137 " ddddddddddddddd;", 4138 Style); 4139 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4140 " aaaaaaaaaaaaaaa :\n" 4141 " (bbbbbbbbbbbbbbb ? //\n" 4142 " ccccccccccccccc :\n" 4143 " ddddddddddddddd);", 4144 Style); 4145 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4146 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4147 " ccccccccccccccccccccccccccc;", 4148 Style); 4149 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4150 " aaaaa :\n" 4151 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4152 Style); 4153 } 4154 4155 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4156 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4157 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4158 verifyFormat("bool a = true, b = false;"); 4159 4160 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4162 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4163 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4164 verifyFormat( 4165 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4166 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4167 " d = e && f;"); 4168 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4169 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4170 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4171 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4172 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4173 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4174 4175 FormatStyle Style = getGoogleStyle(); 4176 Style.PointerAlignment = FormatStyle::PAS_Left; 4177 Style.DerivePointerAlignment = false; 4178 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4179 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4180 " *b = bbbbbbbbbbbbbbbbbbb;", 4181 Style); 4182 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4183 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4184 Style); 4185 verifyFormat("vector<int*> a, b;", Style); 4186 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4187 } 4188 4189 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4190 verifyFormat("arr[foo ? bar : baz];"); 4191 verifyFormat("f()[foo ? bar : baz];"); 4192 verifyFormat("(a + b)[foo ? bar : baz];"); 4193 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4194 } 4195 4196 TEST_F(FormatTest, AlignsStringLiterals) { 4197 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4198 " \"short literal\");"); 4199 verifyFormat( 4200 "looooooooooooooooooooooooongFunction(\n" 4201 " \"short literal\"\n" 4202 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4203 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4204 " \" string literals\",\n" 4205 " and, other, parameters);"); 4206 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4207 " \"5678\";", 4208 format("fun + \"1243\" /* comment */\n" 4209 " \"5678\";", 4210 getLLVMStyleWithColumns(28))); 4211 EXPECT_EQ( 4212 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4213 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4214 " \"aaaaaaaaaaaaaaaa\";", 4215 format("aaaaaa =" 4216 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4217 "aaaaaaaaaaaaaaaaaaaaa\" " 4218 "\"aaaaaaaaaaaaaaaa\";")); 4219 verifyFormat("a = a + \"a\"\n" 4220 " \"a\"\n" 4221 " \"a\";"); 4222 verifyFormat("f(\"a\", \"b\"\n" 4223 " \"c\");"); 4224 4225 verifyFormat( 4226 "#define LL_FORMAT \"ll\"\n" 4227 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4228 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4229 4230 verifyFormat("#define A(X) \\\n" 4231 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4232 " \"ccccc\"", 4233 getLLVMStyleWithColumns(23)); 4234 verifyFormat("#define A \"def\"\n" 4235 "f(\"abc\" A \"ghi\"\n" 4236 " \"jkl\");"); 4237 4238 verifyFormat("f(L\"a\"\n" 4239 " L\"b\");"); 4240 verifyFormat("#define A(X) \\\n" 4241 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4242 " L\"ccccc\"", 4243 getLLVMStyleWithColumns(25)); 4244 4245 verifyFormat("f(@\"a\"\n" 4246 " @\"b\");"); 4247 verifyFormat("NSString s = @\"a\"\n" 4248 " @\"b\"\n" 4249 " @\"c\";"); 4250 verifyFormat("NSString s = @\"a\"\n" 4251 " \"b\"\n" 4252 " \"c\";"); 4253 } 4254 4255 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4256 FormatStyle Style = getLLVMStyle(); 4257 // No declarations or definitions should be moved to own line. 4258 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4259 verifyFormat("class A {\n" 4260 " int f() { return 1; }\n" 4261 " int g();\n" 4262 "};\n" 4263 "int f() { return 1; }\n" 4264 "int g();\n", 4265 Style); 4266 4267 // All declarations and definitions should have the return type moved to its 4268 // own 4269 // line. 4270 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4271 verifyFormat("class E {\n" 4272 " int\n" 4273 " f() {\n" 4274 " return 1;\n" 4275 " }\n" 4276 " int\n" 4277 " g();\n" 4278 "};\n" 4279 "int\n" 4280 "f() {\n" 4281 " return 1;\n" 4282 "}\n" 4283 "int\n" 4284 "g();\n", 4285 Style); 4286 4287 // Top-level definitions, and no kinds of declarations should have the 4288 // return type moved to its own line. 4289 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4290 verifyFormat("class B {\n" 4291 " int f() { return 1; }\n" 4292 " int g();\n" 4293 "};\n" 4294 "int\n" 4295 "f() {\n" 4296 " return 1;\n" 4297 "}\n" 4298 "int g();\n", 4299 Style); 4300 4301 // Top-level definitions and declarations should have the return type moved 4302 // to its own line. 4303 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4304 verifyFormat("class C {\n" 4305 " int f() { return 1; }\n" 4306 " int g();\n" 4307 "};\n" 4308 "int\n" 4309 "f() {\n" 4310 " return 1;\n" 4311 "}\n" 4312 "int\n" 4313 "g();\n", 4314 Style); 4315 4316 // All definitions should have the return type moved to its own line, but no 4317 // kinds of declarations. 4318 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4319 verifyFormat("class D {\n" 4320 " int\n" 4321 " f() {\n" 4322 " return 1;\n" 4323 " }\n" 4324 " int g();\n" 4325 "};\n" 4326 "int\n" 4327 "f() {\n" 4328 " return 1;\n" 4329 "}\n" 4330 "int g();\n", 4331 Style); 4332 verifyFormat("const char *\n" 4333 "f(void) {\n" // Break here. 4334 " return \"\";\n" 4335 "}\n" 4336 "const char *bar(void);\n", // No break here. 4337 Style); 4338 verifyFormat("template <class T>\n" 4339 "T *\n" 4340 "f(T &c) {\n" // Break here. 4341 " return NULL;\n" 4342 "}\n" 4343 "template <class T> T *f(T &c);\n", // No break here. 4344 Style); 4345 verifyFormat("class C {\n" 4346 " int\n" 4347 " operator+() {\n" 4348 " return 1;\n" 4349 " }\n" 4350 " int\n" 4351 " operator()() {\n" 4352 " return 1;\n" 4353 " }\n" 4354 "};\n", 4355 Style); 4356 verifyFormat("void\n" 4357 "A::operator()() {}\n" 4358 "void\n" 4359 "A::operator>>() {}\n" 4360 "void\n" 4361 "A::operator+() {}\n", 4362 Style); 4363 verifyFormat("void *operator new(std::size_t s);", // No break here. 4364 Style); 4365 verifyFormat("void *\n" 4366 "operator new(std::size_t s) {}", 4367 Style); 4368 verifyFormat("void *\n" 4369 "operator delete[](void *ptr) {}", 4370 Style); 4371 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4372 verifyFormat("const char *\n" 4373 "f(void)\n" // Break here. 4374 "{\n" 4375 " return \"\";\n" 4376 "}\n" 4377 "const char *bar(void);\n", // No break here. 4378 Style); 4379 verifyFormat("template <class T>\n" 4380 "T *\n" // Problem here: no line break 4381 "f(T &c)\n" // Break here. 4382 "{\n" 4383 " return NULL;\n" 4384 "}\n" 4385 "template <class T> T *f(T &c);\n", // No break here. 4386 Style); 4387 } 4388 4389 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4390 FormatStyle NoBreak = getLLVMStyle(); 4391 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4392 FormatStyle Break = getLLVMStyle(); 4393 Break.AlwaysBreakBeforeMultilineStrings = true; 4394 verifyFormat("aaaa = \"bbbb\"\n" 4395 " \"cccc\";", 4396 NoBreak); 4397 verifyFormat("aaaa =\n" 4398 " \"bbbb\"\n" 4399 " \"cccc\";", 4400 Break); 4401 verifyFormat("aaaa(\"bbbb\"\n" 4402 " \"cccc\");", 4403 NoBreak); 4404 verifyFormat("aaaa(\n" 4405 " \"bbbb\"\n" 4406 " \"cccc\");", 4407 Break); 4408 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4409 " \"cccc\");", 4410 NoBreak); 4411 verifyFormat("aaaa(qqq,\n" 4412 " \"bbbb\"\n" 4413 " \"cccc\");", 4414 Break); 4415 verifyFormat("aaaa(qqq,\n" 4416 " L\"bbbb\"\n" 4417 " L\"cccc\");", 4418 Break); 4419 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4420 " \"bbbb\"));", 4421 Break); 4422 verifyFormat("string s = someFunction(\n" 4423 " \"abc\"\n" 4424 " \"abc\");", 4425 Break); 4426 4427 // As we break before unary operators, breaking right after them is bad. 4428 verifyFormat("string foo = abc ? \"x\"\n" 4429 " \"blah blah blah blah blah blah\"\n" 4430 " : \"y\";", 4431 Break); 4432 4433 // Don't break if there is no column gain. 4434 verifyFormat("f(\"aaaa\"\n" 4435 " \"bbbb\");", 4436 Break); 4437 4438 // Treat literals with escaped newlines like multi-line string literals. 4439 EXPECT_EQ("x = \"a\\\n" 4440 "b\\\n" 4441 "c\";", 4442 format("x = \"a\\\n" 4443 "b\\\n" 4444 "c\";", 4445 NoBreak)); 4446 EXPECT_EQ("xxxx =\n" 4447 " \"a\\\n" 4448 "b\\\n" 4449 "c\";", 4450 format("xxxx = \"a\\\n" 4451 "b\\\n" 4452 "c\";", 4453 Break)); 4454 4455 EXPECT_EQ("NSString *const kString =\n" 4456 " @\"aaaa\"\n" 4457 " @\"bbbb\";", 4458 format("NSString *const kString = @\"aaaa\"\n" 4459 "@\"bbbb\";", 4460 Break)); 4461 4462 Break.ColumnLimit = 0; 4463 verifyFormat("const char *hello = \"hello llvm\";", Break); 4464 } 4465 4466 TEST_F(FormatTest, AlignsPipes) { 4467 verifyFormat( 4468 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4469 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4470 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4471 verifyFormat( 4472 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 4473 " << aaaaaaaaaaaaaaaaaaaa;"); 4474 verifyFormat( 4475 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4476 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4477 verifyFormat( 4478 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4479 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4480 verifyFormat( 4481 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 4482 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 4483 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 4484 verifyFormat( 4485 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4486 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4487 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4488 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4489 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4491 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4492 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 4493 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 4494 verifyFormat( 4495 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4496 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4497 verifyFormat( 4498 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 4499 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4500 4501 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 4502 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 4503 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4505 " aaaaaaaaaaaaaaaaaaaaa)\n" 4506 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4507 verifyFormat("LOG_IF(aaa == //\n" 4508 " bbb)\n" 4509 " << a << b;"); 4510 4511 // But sometimes, breaking before the first "<<" is desirable. 4512 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4513 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 4514 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 4515 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4516 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4517 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 4518 " << BEF << IsTemplate << Description << E->getType();"); 4519 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4520 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4521 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4522 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4523 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4525 " << aaa;"); 4526 4527 verifyFormat( 4528 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4529 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4530 4531 // Incomplete string literal. 4532 EXPECT_EQ("llvm::errs() << \"\n" 4533 " << a;", 4534 format("llvm::errs() << \"\n<<a;")); 4535 4536 verifyFormat("void f() {\n" 4537 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 4538 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 4539 "}"); 4540 4541 // Handle 'endl'. 4542 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 4543 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4544 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4545 4546 // Handle '\n'. 4547 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 4548 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 4549 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 4550 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 4551 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 4552 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 4553 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 4554 } 4555 4556 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 4557 verifyFormat("return out << \"somepacket = {\\n\"\n" 4558 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 4559 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 4560 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 4561 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 4562 " << \"}\";"); 4563 4564 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4565 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4566 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 4567 verifyFormat( 4568 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 4569 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 4570 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 4571 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 4572 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 4573 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 4574 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4575 verifyFormat( 4576 "void f() {\n" 4577 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 4578 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4579 "}"); 4580 4581 // Breaking before the first "<<" is generally not desirable. 4582 verifyFormat( 4583 "llvm::errs()\n" 4584 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4585 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4586 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4587 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4588 getLLVMStyleWithColumns(70)); 4589 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4590 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4591 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4592 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4593 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4594 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4595 getLLVMStyleWithColumns(70)); 4596 4597 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 4598 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 4599 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 4600 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 4601 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 4602 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 4603 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 4604 " (aaaa + aaaa);", 4605 getLLVMStyleWithColumns(40)); 4606 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 4607 " (aaaaaaa + aaaaa));", 4608 getLLVMStyleWithColumns(40)); 4609 verifyFormat( 4610 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 4611 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 4612 " bbbbbbbbbbbbbbbbbbbbbbb);"); 4613 } 4614 4615 TEST_F(FormatTest, UnderstandsEquals) { 4616 verifyFormat( 4617 "aaaaaaaaaaaaaaaaa =\n" 4618 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4619 verifyFormat( 4620 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4621 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4622 verifyFormat( 4623 "if (a) {\n" 4624 " f();\n" 4625 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4627 "}"); 4628 4629 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4630 " 100000000 + 10000000) {\n}"); 4631 } 4632 4633 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 4634 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4635 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 4636 4637 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4638 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 4639 4640 verifyFormat( 4641 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 4642 " Parameter2);"); 4643 4644 verifyFormat( 4645 "ShortObject->shortFunction(\n" 4646 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 4647 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 4648 4649 verifyFormat("loooooooooooooongFunction(\n" 4650 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 4651 4652 verifyFormat( 4653 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 4654 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 4655 4656 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4657 " .WillRepeatedly(Return(SomeValue));"); 4658 verifyFormat("void f() {\n" 4659 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4660 " .Times(2)\n" 4661 " .WillRepeatedly(Return(SomeValue));\n" 4662 "}"); 4663 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 4664 " ccccccccccccccccccccccc);"); 4665 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4666 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4667 " .aaaaa(aaaaa),\n" 4668 " aaaaaaaaaaaaaaaaaaaaa);"); 4669 verifyFormat("void f() {\n" 4670 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4671 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 4672 "}"); 4673 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4675 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4676 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4677 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4678 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4679 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4680 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4681 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 4682 "}"); 4683 4684 // Here, it is not necessary to wrap at "." or "->". 4685 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 4686 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4687 verifyFormat( 4688 "aaaaaaaaaaa->aaaaaaaaa(\n" 4689 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4690 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 4691 4692 verifyFormat( 4693 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4694 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 4695 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 4696 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4697 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 4698 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4699 4700 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4701 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4702 " .a();"); 4703 4704 FormatStyle NoBinPacking = getLLVMStyle(); 4705 NoBinPacking.BinPackParameters = false; 4706 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4707 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4708 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 4709 " aaaaaaaaaaaaaaaaaaa,\n" 4710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4711 NoBinPacking); 4712 4713 // If there is a subsequent call, change to hanging indentation. 4714 verifyFormat( 4715 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4716 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 4717 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4718 verifyFormat( 4719 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4720 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 4721 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4723 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4724 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4726 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4727 } 4728 4729 TEST_F(FormatTest, WrapsTemplateDeclarations) { 4730 verifyFormat("template <typename T>\n" 4731 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4732 verifyFormat("template <typename T>\n" 4733 "// T should be one of {A, B}.\n" 4734 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4735 verifyFormat( 4736 "template <typename T>\n" 4737 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 4738 verifyFormat("template <typename T>\n" 4739 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 4740 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 4741 verifyFormat( 4742 "template <typename T>\n" 4743 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 4744 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 4745 verifyFormat( 4746 "template <typename T>\n" 4747 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 4748 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 4749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4750 verifyFormat("template <typename T>\n" 4751 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4752 " int aaaaaaaaaaaaaaaaaaaaaa);"); 4753 verifyFormat( 4754 "template <typename T1, typename T2 = char, typename T3 = char,\n" 4755 " typename T4 = char>\n" 4756 "void f();"); 4757 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 4758 " template <typename> class cccccccccccccccccccccc,\n" 4759 " typename ddddddddddddd>\n" 4760 "class C {};"); 4761 verifyFormat( 4762 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 4763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4764 4765 verifyFormat("void f() {\n" 4766 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 4767 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 4768 "}"); 4769 4770 verifyFormat("template <typename T> class C {};"); 4771 verifyFormat("template <typename T> void f();"); 4772 verifyFormat("template <typename T> void f() {}"); 4773 verifyFormat( 4774 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 4777 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4778 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4779 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 4780 " bbbbbbbbbbbbbbbbbbbbbbbb);", 4781 getLLVMStyleWithColumns(72)); 4782 EXPECT_EQ("static_cast<A< //\n" 4783 " B> *>(\n" 4784 "\n" 4785 ");", 4786 format("static_cast<A<//\n" 4787 " B>*>(\n" 4788 "\n" 4789 " );")); 4790 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4791 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 4792 4793 FormatStyle AlwaysBreak = getLLVMStyle(); 4794 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 4795 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 4796 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 4797 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 4798 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4799 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 4800 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 4801 verifyFormat("template <template <typename> class Fooooooo,\n" 4802 " template <typename> class Baaaaaaar>\n" 4803 "struct C {};", 4804 AlwaysBreak); 4805 verifyFormat("template <typename T> // T can be A, B or C.\n" 4806 "struct C {};", 4807 AlwaysBreak); 4808 verifyFormat("template <enum E> class A {\n" 4809 "public:\n" 4810 " E *f();\n" 4811 "};"); 4812 } 4813 4814 TEST_F(FormatTest, WrapsTemplateParameters) { 4815 FormatStyle Style = getLLVMStyle(); 4816 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4817 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4818 verifyFormat( 4819 "template <typename... a> struct q {};\n" 4820 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 4821 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 4822 " y;", 4823 Style); 4824 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4825 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4826 verifyFormat( 4827 "template <typename... a> struct r {};\n" 4828 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 4829 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 4830 " y;", 4831 Style); 4832 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4833 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4834 verifyFormat( 4835 "template <typename... a> struct s {};\n" 4836 "extern s<\n" 4837 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4838 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 4839 " y;", 4840 Style); 4841 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4842 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4843 verifyFormat( 4844 "template <typename... a> struct t {};\n" 4845 "extern t<\n" 4846 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4847 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 4848 " y;", 4849 Style); 4850 } 4851 4852 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 4853 verifyFormat( 4854 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4856 verifyFormat( 4857 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4860 4861 // FIXME: Should we have the extra indent after the second break? 4862 verifyFormat( 4863 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4866 4867 verifyFormat( 4868 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 4869 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 4870 4871 // Breaking at nested name specifiers is generally not desirable. 4872 verifyFormat( 4873 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4874 " aaaaaaaaaaaaaaaaaaaaaaa);"); 4875 4876 verifyFormat( 4877 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 4878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4879 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4880 " aaaaaaaaaaaaaaaaaaaaa);", 4881 getLLVMStyleWithColumns(74)); 4882 4883 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 4884 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4885 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4886 } 4887 4888 TEST_F(FormatTest, UnderstandsTemplateParameters) { 4889 verifyFormat("A<int> a;"); 4890 verifyFormat("A<A<A<int>>> a;"); 4891 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 4892 verifyFormat("bool x = a < 1 || 2 > a;"); 4893 verifyFormat("bool x = 5 < f<int>();"); 4894 verifyFormat("bool x = f<int>() > 5;"); 4895 verifyFormat("bool x = 5 < a<int>::x;"); 4896 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 4897 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 4898 4899 verifyGoogleFormat("A<A<int>> a;"); 4900 verifyGoogleFormat("A<A<A<int>>> a;"); 4901 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 4902 verifyGoogleFormat("A<A<int> > a;"); 4903 verifyGoogleFormat("A<A<A<int> > > a;"); 4904 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 4905 verifyGoogleFormat("A<::A<int>> a;"); 4906 verifyGoogleFormat("A<::A> a;"); 4907 verifyGoogleFormat("A< ::A> a;"); 4908 verifyGoogleFormat("A< ::A<int> > a;"); 4909 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 4910 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 4911 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 4912 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 4913 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 4914 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 4915 4916 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 4917 4918 verifyFormat("test >> a >> b;"); 4919 verifyFormat("test << a >> b;"); 4920 4921 verifyFormat("f<int>();"); 4922 verifyFormat("template <typename T> void f() {}"); 4923 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 4924 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 4925 "sizeof(char)>::type>;"); 4926 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 4927 verifyFormat("f(a.operator()<A>());"); 4928 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4929 " .template operator()<A>());", 4930 getLLVMStyleWithColumns(35)); 4931 4932 // Not template parameters. 4933 verifyFormat("return a < b && c > d;"); 4934 verifyFormat("void f() {\n" 4935 " while (a < b && c > d) {\n" 4936 " }\n" 4937 "}"); 4938 verifyFormat("template <typename... Types>\n" 4939 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 4940 4941 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 4943 getLLVMStyleWithColumns(60)); 4944 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 4945 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 4946 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 4947 } 4948 4949 TEST_F(FormatTest, BitshiftOperatorWidth) { 4950 EXPECT_EQ("int a = 1 << 2; /* foo\n" 4951 " bar */", 4952 format("int a=1<<2; /* foo\n" 4953 " bar */")); 4954 4955 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 4956 " bar */", 4957 format("int b =256>>1 ; /* foo\n" 4958 " bar */")); 4959 } 4960 4961 TEST_F(FormatTest, UnderstandsBinaryOperators) { 4962 verifyFormat("COMPARE(a, ==, b);"); 4963 verifyFormat("auto s = sizeof...(Ts) - 1;"); 4964 } 4965 4966 TEST_F(FormatTest, UnderstandsPointersToMembers) { 4967 verifyFormat("int A::*x;"); 4968 verifyFormat("int (S::*func)(void *);"); 4969 verifyFormat("void f() { int (S::*func)(void *); }"); 4970 verifyFormat("typedef bool *(Class::*Member)() const;"); 4971 verifyFormat("void f() {\n" 4972 " (a->*f)();\n" 4973 " a->*x;\n" 4974 " (a.*f)();\n" 4975 " ((*a).*f)();\n" 4976 " a.*x;\n" 4977 "}"); 4978 verifyFormat("void f() {\n" 4979 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 4980 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 4981 "}"); 4982 verifyFormat( 4983 "(aaaaaaaaaa->*bbbbbbb)(\n" 4984 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4985 FormatStyle Style = getLLVMStyle(); 4986 Style.PointerAlignment = FormatStyle::PAS_Left; 4987 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 4988 } 4989 4990 TEST_F(FormatTest, UnderstandsUnaryOperators) { 4991 verifyFormat("int a = -2;"); 4992 verifyFormat("f(-1, -2, -3);"); 4993 verifyFormat("a[-1] = 5;"); 4994 verifyFormat("int a = 5 + -2;"); 4995 verifyFormat("if (i == -1) {\n}"); 4996 verifyFormat("if (i != -1) {\n}"); 4997 verifyFormat("if (i > -1) {\n}"); 4998 verifyFormat("if (i < -1) {\n}"); 4999 verifyFormat("++(a->f());"); 5000 verifyFormat("--(a->f());"); 5001 verifyFormat("(a->f())++;"); 5002 verifyFormat("a[42]++;"); 5003 verifyFormat("if (!(a->f())) {\n}"); 5004 5005 verifyFormat("a-- > b;"); 5006 verifyFormat("b ? -a : c;"); 5007 verifyFormat("n * sizeof char16;"); 5008 verifyFormat("n * alignof char16;", getGoogleStyle()); 5009 verifyFormat("sizeof(char);"); 5010 verifyFormat("alignof(char);", getGoogleStyle()); 5011 5012 verifyFormat("return -1;"); 5013 verifyFormat("switch (a) {\n" 5014 "case -1:\n" 5015 " break;\n" 5016 "}"); 5017 verifyFormat("#define X -1"); 5018 verifyFormat("#define X -kConstant"); 5019 5020 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5021 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5022 5023 verifyFormat("int a = /* confusing comment */ -1;"); 5024 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5025 verifyFormat("int a = i /* confusing comment */++;"); 5026 } 5027 5028 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5029 verifyFormat("if (!aaaaaaaaaa( // break\n" 5030 " aaaaa)) {\n" 5031 "}"); 5032 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5033 " aaaaa));"); 5034 verifyFormat("*aaa = aaaaaaa( // break\n" 5035 " bbbbbb);"); 5036 } 5037 5038 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5039 verifyFormat("bool operator<();"); 5040 verifyFormat("bool operator>();"); 5041 verifyFormat("bool operator=();"); 5042 verifyFormat("bool operator==();"); 5043 verifyFormat("bool operator!=();"); 5044 verifyFormat("int operator+();"); 5045 verifyFormat("int operator++();"); 5046 verifyFormat("bool operator,();"); 5047 verifyFormat("bool operator();"); 5048 verifyFormat("bool operator()();"); 5049 verifyFormat("bool operator[]();"); 5050 verifyFormat("operator bool();"); 5051 verifyFormat("operator int();"); 5052 verifyFormat("operator void *();"); 5053 verifyFormat("operator SomeType<int>();"); 5054 verifyFormat("operator SomeType<int, int>();"); 5055 verifyFormat("operator SomeType<SomeType<int>>();"); 5056 verifyFormat("void *operator new(std::size_t size);"); 5057 verifyFormat("void *operator new[](std::size_t size);"); 5058 verifyFormat("void operator delete(void *ptr);"); 5059 verifyFormat("void operator delete[](void *ptr);"); 5060 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5061 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5062 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5063 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5064 5065 verifyFormat( 5066 "ostream &operator<<(ostream &OutputStream,\n" 5067 " SomeReallyLongType WithSomeReallyLongValue);"); 5068 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5069 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5070 " return left.group < right.group;\n" 5071 "}"); 5072 verifyFormat("SomeType &operator=(const SomeType &S);"); 5073 verifyFormat("f.template operator()<int>();"); 5074 5075 verifyGoogleFormat("operator void*();"); 5076 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5077 verifyGoogleFormat("operator ::A();"); 5078 5079 verifyFormat("using A::operator+;"); 5080 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5081 "int i;"); 5082 } 5083 5084 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5085 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5086 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5087 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5088 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5089 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5090 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5091 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5092 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5093 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5094 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5095 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5096 verifyFormat("SomeType MemberFunction(const Deleted &) const &;"); 5097 verifyFormat("template <typename T>\n" 5098 "void F(T) && = delete;", 5099 getGoogleStyle()); 5100 5101 FormatStyle AlignLeft = getLLVMStyle(); 5102 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5103 verifyFormat("void A::b() && {}", AlignLeft); 5104 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5105 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5106 AlignLeft); 5107 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5108 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5109 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5110 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5111 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5112 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5113 verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft); 5114 5115 FormatStyle Spaces = getLLVMStyle(); 5116 Spaces.SpacesInCStyleCastParentheses = true; 5117 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5118 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5119 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5120 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5121 5122 Spaces.SpacesInCStyleCastParentheses = false; 5123 Spaces.SpacesInParentheses = true; 5124 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5125 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5126 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5127 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5128 } 5129 5130 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5131 verifyFormat("void f() {\n" 5132 " A *a = new A;\n" 5133 " A *a = new (placement) A;\n" 5134 " delete a;\n" 5135 " delete (A *)a;\n" 5136 "}"); 5137 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5138 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5139 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5140 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5141 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5142 verifyFormat("delete[] h->p;"); 5143 } 5144 5145 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5146 verifyFormat("int *f(int *a) {}"); 5147 verifyFormat("int main(int argc, char **argv) {}"); 5148 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5149 verifyIndependentOfContext("f(a, *a);"); 5150 verifyFormat("void g() { f(*a); }"); 5151 verifyIndependentOfContext("int a = b * 10;"); 5152 verifyIndependentOfContext("int a = 10 * b;"); 5153 verifyIndependentOfContext("int a = b * c;"); 5154 verifyIndependentOfContext("int a += b * c;"); 5155 verifyIndependentOfContext("int a -= b * c;"); 5156 verifyIndependentOfContext("int a *= b * c;"); 5157 verifyIndependentOfContext("int a /= b * c;"); 5158 verifyIndependentOfContext("int a = *b;"); 5159 verifyIndependentOfContext("int a = *b * c;"); 5160 verifyIndependentOfContext("int a = b * *c;"); 5161 verifyIndependentOfContext("int a = b * (10);"); 5162 verifyIndependentOfContext("S << b * (10);"); 5163 verifyIndependentOfContext("return 10 * b;"); 5164 verifyIndependentOfContext("return *b * *c;"); 5165 verifyIndependentOfContext("return a & ~b;"); 5166 verifyIndependentOfContext("f(b ? *c : *d);"); 5167 verifyIndependentOfContext("int a = b ? *c : *d;"); 5168 verifyIndependentOfContext("*b = a;"); 5169 verifyIndependentOfContext("a * ~b;"); 5170 verifyIndependentOfContext("a * !b;"); 5171 verifyIndependentOfContext("a * +b;"); 5172 verifyIndependentOfContext("a * -b;"); 5173 verifyIndependentOfContext("a * ++b;"); 5174 verifyIndependentOfContext("a * --b;"); 5175 verifyIndependentOfContext("a[4] * b;"); 5176 verifyIndependentOfContext("a[a * a] = 1;"); 5177 verifyIndependentOfContext("f() * b;"); 5178 verifyIndependentOfContext("a * [self dostuff];"); 5179 verifyIndependentOfContext("int x = a * (a + b);"); 5180 verifyIndependentOfContext("(a *)(a + b);"); 5181 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5182 verifyIndependentOfContext("int *pa = (int *)&a;"); 5183 verifyIndependentOfContext("return sizeof(int **);"); 5184 verifyIndependentOfContext("return sizeof(int ******);"); 5185 verifyIndependentOfContext("return (int **&)a;"); 5186 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5187 verifyFormat("void f(Type (*parameter)[10]) {}"); 5188 verifyFormat("void f(Type (¶meter)[10]) {}"); 5189 verifyGoogleFormat("return sizeof(int**);"); 5190 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5191 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5192 verifyFormat("auto a = [](int **&, int ***) {};"); 5193 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5194 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5195 verifyFormat("[](const decltype(*a) &value) {}"); 5196 verifyFormat("decltype(a * b) F();"); 5197 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5198 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5199 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5200 verifyIndependentOfContext("int i{a * b};"); 5201 verifyIndependentOfContext("aaa && aaa->f();"); 5202 verifyIndependentOfContext("int x = ~*p;"); 5203 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5204 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5205 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5206 verifyFormat("void f() { f(a, c * d); }"); 5207 verifyFormat("void f() { f(new a(), c * d); }"); 5208 verifyFormat("void f(const MyOverride &override);"); 5209 verifyFormat("void f(const MyFinal &final);"); 5210 verifyIndependentOfContext("bool a = f() && override.f();"); 5211 verifyIndependentOfContext("bool a = f() && final.f();"); 5212 5213 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5214 5215 verifyIndependentOfContext("A<int *> a;"); 5216 verifyIndependentOfContext("A<int **> a;"); 5217 verifyIndependentOfContext("A<int *, int *> a;"); 5218 verifyIndependentOfContext("A<int *[]> a;"); 5219 verifyIndependentOfContext( 5220 "const char *const p = reinterpret_cast<const char *const>(q);"); 5221 verifyIndependentOfContext("A<int **, int **> a;"); 5222 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5223 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5224 verifyFormat("for (; a && b;) {\n}"); 5225 verifyFormat("bool foo = true && [] { return false; }();"); 5226 5227 verifyFormat( 5228 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5229 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5230 5231 verifyGoogleFormat("int const* a = &b;"); 5232 verifyGoogleFormat("**outparam = 1;"); 5233 verifyGoogleFormat("*outparam = a * b;"); 5234 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5235 verifyGoogleFormat("A<int*> a;"); 5236 verifyGoogleFormat("A<int**> a;"); 5237 verifyGoogleFormat("A<int*, int*> a;"); 5238 verifyGoogleFormat("A<int**, int**> a;"); 5239 verifyGoogleFormat("f(b ? *c : *d);"); 5240 verifyGoogleFormat("int a = b ? *c : *d;"); 5241 verifyGoogleFormat("Type* t = **x;"); 5242 verifyGoogleFormat("Type* t = *++*x;"); 5243 verifyGoogleFormat("*++*x;"); 5244 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5245 verifyGoogleFormat("Type* t = x++ * y;"); 5246 verifyGoogleFormat( 5247 "const char* const p = reinterpret_cast<const char* const>(q);"); 5248 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5249 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5250 verifyGoogleFormat("template <typename T>\n" 5251 "void f(int i = 0, SomeType** temps = NULL);"); 5252 5253 FormatStyle Left = getLLVMStyle(); 5254 Left.PointerAlignment = FormatStyle::PAS_Left; 5255 verifyFormat("x = *a(x) = *a(y);", Left); 5256 verifyFormat("for (;; *a = b) {\n}", Left); 5257 verifyFormat("return *this += 1;", Left); 5258 5259 verifyIndependentOfContext("a = *(x + y);"); 5260 verifyIndependentOfContext("a = &(x + y);"); 5261 verifyIndependentOfContext("*(x + y).call();"); 5262 verifyIndependentOfContext("&(x + y)->call();"); 5263 verifyFormat("void f() { &(*I).first; }"); 5264 5265 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5266 verifyFormat( 5267 "int *MyValues = {\n" 5268 " *A, // Operator detection might be confused by the '{'\n" 5269 " *BB // Operator detection might be confused by previous comment\n" 5270 "};"); 5271 5272 verifyIndependentOfContext("if (int *a = &b)"); 5273 verifyIndependentOfContext("if (int &a = *b)"); 5274 verifyIndependentOfContext("if (a & b[i])"); 5275 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5276 verifyIndependentOfContext("if (*b[i])"); 5277 verifyIndependentOfContext("if (int *a = (&b))"); 5278 verifyIndependentOfContext("while (int *a = &b)"); 5279 verifyIndependentOfContext("size = sizeof *a;"); 5280 verifyIndependentOfContext("if (a && (b = c))"); 5281 verifyFormat("void f() {\n" 5282 " for (const int &v : Values) {\n" 5283 " }\n" 5284 "}"); 5285 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5286 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5287 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5288 5289 verifyFormat("#define A (!a * b)"); 5290 verifyFormat("#define MACRO \\\n" 5291 " int *i = a * b; \\\n" 5292 " void f(a *b);", 5293 getLLVMStyleWithColumns(19)); 5294 5295 verifyIndependentOfContext("A = new SomeType *[Length];"); 5296 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5297 verifyIndependentOfContext("T **t = new T *;"); 5298 verifyIndependentOfContext("T **t = new T *();"); 5299 verifyGoogleFormat("A = new SomeType*[Length]();"); 5300 verifyGoogleFormat("A = new SomeType*[Length];"); 5301 verifyGoogleFormat("T** t = new T*;"); 5302 verifyGoogleFormat("T** t = new T*();"); 5303 5304 FormatStyle PointerLeft = getLLVMStyle(); 5305 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5306 verifyFormat("delete *x;", PointerLeft); 5307 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5308 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5309 verifyFormat("template <bool a, bool b> " 5310 "typename t::if<x && y>::type f() {}"); 5311 verifyFormat("template <int *y> f() {}"); 5312 verifyFormat("vector<int *> v;"); 5313 verifyFormat("vector<int *const> v;"); 5314 verifyFormat("vector<int *const **const *> v;"); 5315 verifyFormat("vector<int *volatile> v;"); 5316 verifyFormat("vector<a * b> v;"); 5317 verifyFormat("foo<b && false>();"); 5318 verifyFormat("foo<b & 1>();"); 5319 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5320 verifyFormat( 5321 "template <class T, class = typename std::enable_if<\n" 5322 " std::is_integral<T>::value &&\n" 5323 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5324 "void F();", 5325 getLLVMStyleWithColumns(70)); 5326 verifyFormat( 5327 "template <class T,\n" 5328 " class = typename std::enable_if<\n" 5329 " std::is_integral<T>::value &&\n" 5330 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 5331 " class U>\n" 5332 "void F();", 5333 getLLVMStyleWithColumns(70)); 5334 verifyFormat( 5335 "template <class T,\n" 5336 " class = typename ::std::enable_if<\n" 5337 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5338 "void F();", 5339 getGoogleStyleWithColumns(68)); 5340 5341 verifyIndependentOfContext("MACRO(int *i);"); 5342 verifyIndependentOfContext("MACRO(auto *a);"); 5343 verifyIndependentOfContext("MACRO(const A *a);"); 5344 verifyIndependentOfContext("MACRO(A *const a);"); 5345 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5346 verifyFormat("void f() { f(float{1}, a * a); }"); 5347 // FIXME: Is there a way to make this work? 5348 // verifyIndependentOfContext("MACRO(A *a);"); 5349 5350 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5351 verifyFormat("return options != nullptr && operator==(*options);"); 5352 5353 EXPECT_EQ("#define OP(x) \\\n" 5354 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5355 " return s << a.DebugString(); \\\n" 5356 " }", 5357 format("#define OP(x) \\\n" 5358 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5359 " return s << a.DebugString(); \\\n" 5360 " }", 5361 getLLVMStyleWithColumns(50))); 5362 5363 // FIXME: We cannot handle this case yet; we might be able to figure out that 5364 // foo<x> d > v; doesn't make sense. 5365 verifyFormat("foo<a<b && c> d> v;"); 5366 5367 FormatStyle PointerMiddle = getLLVMStyle(); 5368 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5369 verifyFormat("delete *x;", PointerMiddle); 5370 verifyFormat("int * x;", PointerMiddle); 5371 verifyFormat("template <int * y> f() {}", PointerMiddle); 5372 verifyFormat("int * f(int * a) {}", PointerMiddle); 5373 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5374 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5375 verifyFormat("A<int *> a;", PointerMiddle); 5376 verifyFormat("A<int **> a;", PointerMiddle); 5377 verifyFormat("A<int *, int *> a;", PointerMiddle); 5378 verifyFormat("A<int * []> a;", PointerMiddle); 5379 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5380 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5381 verifyFormat("T ** t = new T *;", PointerMiddle); 5382 5383 // Member function reference qualifiers aren't binary operators. 5384 verifyFormat("string // break\n" 5385 "operator()() & {}"); 5386 verifyFormat("string // break\n" 5387 "operator()() && {}"); 5388 verifyGoogleFormat("template <typename T>\n" 5389 "auto x() & -> int {}"); 5390 } 5391 5392 TEST_F(FormatTest, UnderstandsAttributes) { 5393 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5394 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5395 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5396 FormatStyle AfterType = getLLVMStyle(); 5397 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5398 verifyFormat("__attribute__((nodebug)) void\n" 5399 "foo() {}\n", 5400 AfterType); 5401 } 5402 5403 TEST_F(FormatTest, UnderstandsEllipsis) { 5404 verifyFormat("int printf(const char *fmt, ...);"); 5405 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5406 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5407 5408 FormatStyle PointersLeft = getLLVMStyle(); 5409 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5410 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5411 } 5412 5413 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5414 EXPECT_EQ("int *a;\n" 5415 "int *a;\n" 5416 "int *a;", 5417 format("int *a;\n" 5418 "int* a;\n" 5419 "int *a;", 5420 getGoogleStyle())); 5421 EXPECT_EQ("int* a;\n" 5422 "int* a;\n" 5423 "int* a;", 5424 format("int* a;\n" 5425 "int* a;\n" 5426 "int *a;", 5427 getGoogleStyle())); 5428 EXPECT_EQ("int *a;\n" 5429 "int *a;\n" 5430 "int *a;", 5431 format("int *a;\n" 5432 "int * a;\n" 5433 "int * a;", 5434 getGoogleStyle())); 5435 EXPECT_EQ("auto x = [] {\n" 5436 " int *a;\n" 5437 " int *a;\n" 5438 " int *a;\n" 5439 "};", 5440 format("auto x=[]{int *a;\n" 5441 "int * a;\n" 5442 "int * a;};", 5443 getGoogleStyle())); 5444 } 5445 5446 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5447 verifyFormat("int f(int &&a) {}"); 5448 verifyFormat("int f(int a, char &&b) {}"); 5449 verifyFormat("void f() { int &&a = b; }"); 5450 verifyGoogleFormat("int f(int a, char&& b) {}"); 5451 verifyGoogleFormat("void f() { int&& a = b; }"); 5452 5453 verifyIndependentOfContext("A<int &&> a;"); 5454 verifyIndependentOfContext("A<int &&, int &&> a;"); 5455 verifyGoogleFormat("A<int&&> a;"); 5456 verifyGoogleFormat("A<int&&, int&&> a;"); 5457 5458 // Not rvalue references: 5459 verifyFormat("template <bool B, bool C> class A {\n" 5460 " static_assert(B && C, \"Something is wrong\");\n" 5461 "};"); 5462 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5463 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5464 verifyFormat("#define A(a, b) (a && b)"); 5465 } 5466 5467 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5468 verifyFormat("void f() {\n" 5469 " x[aaaaaaaaa -\n" 5470 " b] = 23;\n" 5471 "}", 5472 getLLVMStyleWithColumns(15)); 5473 } 5474 5475 TEST_F(FormatTest, FormatsCasts) { 5476 verifyFormat("Type *A = static_cast<Type *>(P);"); 5477 verifyFormat("Type *A = (Type *)P;"); 5478 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5479 verifyFormat("int a = (int)(2.0f);"); 5480 verifyFormat("int a = (int)2.0f;"); 5481 verifyFormat("x[(int32)y];"); 5482 verifyFormat("x = (int32)y;"); 5483 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5484 verifyFormat("int a = (int)*b;"); 5485 verifyFormat("int a = (int)2.0f;"); 5486 verifyFormat("int a = (int)~0;"); 5487 verifyFormat("int a = (int)++a;"); 5488 verifyFormat("int a = (int)sizeof(int);"); 5489 verifyFormat("int a = (int)+2;"); 5490 verifyFormat("my_int a = (my_int)2.0f;"); 5491 verifyFormat("my_int a = (my_int)sizeof(int);"); 5492 verifyFormat("return (my_int)aaa;"); 5493 verifyFormat("#define x ((int)-1)"); 5494 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5495 verifyFormat("#define p(q) ((int *)&q)"); 5496 verifyFormat("fn(a)(b) + 1;"); 5497 5498 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5499 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5500 verifyFormat("my_int a = (my_int)~0;"); 5501 verifyFormat("my_int a = (my_int)++a;"); 5502 verifyFormat("my_int a = (my_int)-2;"); 5503 verifyFormat("my_int a = (my_int)1;"); 5504 verifyFormat("my_int a = (my_int *)1;"); 5505 verifyFormat("my_int a = (const my_int)-1;"); 5506 verifyFormat("my_int a = (const my_int *)-1;"); 5507 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5508 verifyFormat("my_int a = (ns::my_int)-2;"); 5509 verifyFormat("case (my_int)ONE:"); 5510 verifyFormat("auto x = (X)this;"); 5511 5512 // FIXME: single value wrapped with paren will be treated as cast. 5513 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5514 5515 verifyFormat("{ (void)F; }"); 5516 5517 // Don't break after a cast's 5518 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5519 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5520 " bbbbbbbbbbbbbbbbbbbbbb);"); 5521 5522 // These are not casts. 5523 verifyFormat("void f(int *) {}"); 5524 verifyFormat("f(foo)->b;"); 5525 verifyFormat("f(foo).b;"); 5526 verifyFormat("f(foo)(b);"); 5527 verifyFormat("f(foo)[b];"); 5528 verifyFormat("[](foo) { return 4; }(bar);"); 5529 verifyFormat("(*funptr)(foo)[4];"); 5530 verifyFormat("funptrs[4](foo)[4];"); 5531 verifyFormat("void f(int *);"); 5532 verifyFormat("void f(int *) = 0;"); 5533 verifyFormat("void f(SmallVector<int>) {}"); 5534 verifyFormat("void f(SmallVector<int>);"); 5535 verifyFormat("void f(SmallVector<int>) = 0;"); 5536 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5537 verifyFormat("int a = sizeof(int) * b;"); 5538 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5539 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5540 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5541 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5542 5543 // These are not casts, but at some point were confused with casts. 5544 verifyFormat("virtual void foo(int *) override;"); 5545 verifyFormat("virtual void foo(char &) const;"); 5546 verifyFormat("virtual void foo(int *a, char *) const;"); 5547 verifyFormat("int a = sizeof(int *) + b;"); 5548 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 5549 verifyFormat("bool b = f(g<int>) && c;"); 5550 verifyFormat("typedef void (*f)(int i) func;"); 5551 5552 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 5553 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5554 // FIXME: The indentation here is not ideal. 5555 verifyFormat( 5556 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5557 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 5558 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 5559 } 5560 5561 TEST_F(FormatTest, FormatsFunctionTypes) { 5562 verifyFormat("A<bool()> a;"); 5563 verifyFormat("A<SomeType()> a;"); 5564 verifyFormat("A<void (*)(int, std::string)> a;"); 5565 verifyFormat("A<void *(int)>;"); 5566 verifyFormat("void *(*a)(int *, SomeType *);"); 5567 verifyFormat("int (*func)(void *);"); 5568 verifyFormat("void f() { int (*func)(void *); }"); 5569 verifyFormat("template <class CallbackClass>\n" 5570 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 5571 5572 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 5573 verifyGoogleFormat("void* (*a)(int);"); 5574 verifyGoogleFormat( 5575 "template <class CallbackClass>\n" 5576 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 5577 5578 // Other constructs can look somewhat like function types: 5579 verifyFormat("A<sizeof(*x)> a;"); 5580 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 5581 verifyFormat("some_var = function(*some_pointer_var)[0];"); 5582 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 5583 verifyFormat("int x = f(&h)();"); 5584 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 5585 verifyFormat("std::function<\n" 5586 " LooooooooooongTemplatedType<\n" 5587 " SomeType>*(\n" 5588 " LooooooooooooooooongType type)>\n" 5589 " function;", 5590 getGoogleStyleWithColumns(40)); 5591 } 5592 5593 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 5594 verifyFormat("A (*foo_)[6];"); 5595 verifyFormat("vector<int> (*foo_)[6];"); 5596 } 5597 5598 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 5599 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5600 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5601 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 5602 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5603 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5604 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5605 5606 // Different ways of ()-initializiation. 5607 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5608 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 5609 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5610 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 5611 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5612 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 5613 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5614 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 5615 5616 // Lambdas should not confuse the variable declaration heuristic. 5617 verifyFormat("LooooooooooooooooongType\n" 5618 " variable(nullptr, [](A *a) {});", 5619 getLLVMStyleWithColumns(40)); 5620 } 5621 5622 TEST_F(FormatTest, BreaksLongDeclarations) { 5623 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 5624 " AnotherNameForTheLongType;"); 5625 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 5626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5627 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5628 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5629 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 5630 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5631 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5632 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5633 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 5634 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5635 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5636 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5637 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5638 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5639 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5640 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 5641 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5642 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 5643 FormatStyle Indented = getLLVMStyle(); 5644 Indented.IndentWrappedFunctionNames = true; 5645 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5646 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 5647 Indented); 5648 verifyFormat( 5649 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5650 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5651 Indented); 5652 verifyFormat( 5653 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5654 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5655 Indented); 5656 verifyFormat( 5657 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5658 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5659 Indented); 5660 5661 // FIXME: Without the comment, this breaks after "(". 5662 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 5663 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 5664 getGoogleStyle()); 5665 5666 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 5667 " int LoooooooooooooooooooongParam2) {}"); 5668 verifyFormat( 5669 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 5670 " SourceLocation L, IdentifierIn *II,\n" 5671 " Type *T) {}"); 5672 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 5673 "ReallyReaaallyLongFunctionName(\n" 5674 " const std::string &SomeParameter,\n" 5675 " const SomeType<string, SomeOtherTemplateParameter>\n" 5676 " &ReallyReallyLongParameterName,\n" 5677 " const SomeType<string, SomeOtherTemplateParameter>\n" 5678 " &AnotherLongParameterName) {}"); 5679 verifyFormat("template <typename A>\n" 5680 "SomeLoooooooooooooooooooooongType<\n" 5681 " typename some_namespace::SomeOtherType<A>::Type>\n" 5682 "Function() {}"); 5683 5684 verifyGoogleFormat( 5685 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 5686 " aaaaaaaaaaaaaaaaaaaaaaa;"); 5687 verifyGoogleFormat( 5688 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 5689 " SourceLocation L) {}"); 5690 verifyGoogleFormat( 5691 "some_namespace::LongReturnType\n" 5692 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 5693 " int first_long_parameter, int second_parameter) {}"); 5694 5695 verifyGoogleFormat("template <typename T>\n" 5696 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5697 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 5698 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5699 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 5700 5701 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5702 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5703 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5704 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5705 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5706 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5707 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5708 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 5710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5711 5712 verifyFormat("template <typename T> // Templates on own line.\n" 5713 "static int // Some comment.\n" 5714 "MyFunction(int a);", 5715 getLLVMStyle()); 5716 } 5717 5718 TEST_F(FormatTest, FormatsArrays) { 5719 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5720 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 5721 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 5722 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 5723 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 5724 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 5725 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5726 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5727 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5728 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 5729 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5730 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5731 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5732 verifyFormat( 5733 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 5734 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5735 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 5736 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 5737 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5738 5739 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 5740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 5741 verifyFormat( 5742 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 5743 " .aaaaaaa[0]\n" 5744 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5745 verifyFormat("a[::b::c];"); 5746 5747 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 5748 5749 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 5750 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 5751 } 5752 5753 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 5754 verifyFormat("(a)->b();"); 5755 verifyFormat("--a;"); 5756 } 5757 5758 TEST_F(FormatTest, HandlesIncludeDirectives) { 5759 verifyFormat("#include <string>\n" 5760 "#include <a/b/c.h>\n" 5761 "#include \"a/b/string\"\n" 5762 "#include \"string.h\"\n" 5763 "#include \"string.h\"\n" 5764 "#include <a-a>\n" 5765 "#include < path with space >\n" 5766 "#include_next <test.h>" 5767 "#include \"abc.h\" // this is included for ABC\n" 5768 "#include \"some long include\" // with a comment\n" 5769 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 5770 getLLVMStyleWithColumns(35)); 5771 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 5772 EXPECT_EQ("#include <a>", format("#include<a>")); 5773 5774 verifyFormat("#import <string>"); 5775 verifyFormat("#import <a/b/c.h>"); 5776 verifyFormat("#import \"a/b/string\""); 5777 verifyFormat("#import \"string.h\""); 5778 verifyFormat("#import \"string.h\""); 5779 verifyFormat("#if __has_include(<strstream>)\n" 5780 "#include <strstream>\n" 5781 "#endif"); 5782 5783 verifyFormat("#define MY_IMPORT <a/b>"); 5784 5785 verifyFormat("#if __has_include(<a/b>)"); 5786 verifyFormat("#if __has_include_next(<a/b>)"); 5787 verifyFormat("#define F __has_include(<a/b>)"); 5788 verifyFormat("#define F __has_include_next(<a/b>)"); 5789 5790 // Protocol buffer definition or missing "#". 5791 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 5792 getLLVMStyleWithColumns(30)); 5793 5794 FormatStyle Style = getLLVMStyle(); 5795 Style.AlwaysBreakBeforeMultilineStrings = true; 5796 Style.ColumnLimit = 0; 5797 verifyFormat("#import \"abc.h\"", Style); 5798 5799 // But 'import' might also be a regular C++ namespace. 5800 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5802 } 5803 5804 //===----------------------------------------------------------------------===// 5805 // Error recovery tests. 5806 //===----------------------------------------------------------------------===// 5807 5808 TEST_F(FormatTest, IncompleteParameterLists) { 5809 FormatStyle NoBinPacking = getLLVMStyle(); 5810 NoBinPacking.BinPackParameters = false; 5811 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 5812 " double *min_x,\n" 5813 " double *max_x,\n" 5814 " double *min_y,\n" 5815 " double *max_y,\n" 5816 " double *min_z,\n" 5817 " double *max_z, ) {}", 5818 NoBinPacking); 5819 } 5820 5821 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 5822 verifyFormat("void f() { return; }\n42"); 5823 verifyFormat("void f() {\n" 5824 " if (0)\n" 5825 " return;\n" 5826 "}\n" 5827 "42"); 5828 verifyFormat("void f() { return }\n42"); 5829 verifyFormat("void f() {\n" 5830 " if (0)\n" 5831 " return\n" 5832 "}\n" 5833 "42"); 5834 } 5835 5836 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 5837 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 5838 EXPECT_EQ("void f() {\n" 5839 " if (a)\n" 5840 " return\n" 5841 "}", 5842 format("void f ( ) { if ( a ) return }")); 5843 EXPECT_EQ("namespace N {\n" 5844 "void f()\n" 5845 "}", 5846 format("namespace N { void f() }")); 5847 EXPECT_EQ("namespace N {\n" 5848 "void f() {}\n" 5849 "void g()\n" 5850 "} // namespace N", 5851 format("namespace N { void f( ) { } void g( ) }")); 5852 } 5853 5854 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 5855 verifyFormat("int aaaaaaaa =\n" 5856 " // Overlylongcomment\n" 5857 " b;", 5858 getLLVMStyleWithColumns(20)); 5859 verifyFormat("function(\n" 5860 " ShortArgument,\n" 5861 " LoooooooooooongArgument);\n", 5862 getLLVMStyleWithColumns(20)); 5863 } 5864 5865 TEST_F(FormatTest, IncorrectAccessSpecifier) { 5866 verifyFormat("public:"); 5867 verifyFormat("class A {\n" 5868 "public\n" 5869 " void f() {}\n" 5870 "};"); 5871 verifyFormat("public\n" 5872 "int qwerty;"); 5873 verifyFormat("public\n" 5874 "B {}"); 5875 verifyFormat("public\n" 5876 "{}"); 5877 verifyFormat("public\n" 5878 "B { int x; }"); 5879 } 5880 5881 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 5882 verifyFormat("{"); 5883 verifyFormat("#})"); 5884 verifyNoCrash("(/**/[:!] ?[)."); 5885 } 5886 5887 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 5888 verifyFormat("do {\n}"); 5889 verifyFormat("do {\n}\n" 5890 "f();"); 5891 verifyFormat("do {\n}\n" 5892 "wheeee(fun);"); 5893 verifyFormat("do {\n" 5894 " f();\n" 5895 "}"); 5896 } 5897 5898 TEST_F(FormatTest, IncorrectCodeMissingParens) { 5899 verifyFormat("if {\n foo;\n foo();\n}"); 5900 verifyFormat("switch {\n foo;\n foo();\n}"); 5901 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 5902 verifyFormat("while {\n foo;\n foo();\n}"); 5903 verifyFormat("do {\n foo;\n foo();\n} while;"); 5904 } 5905 5906 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 5907 verifyIncompleteFormat("namespace {\n" 5908 "class Foo { Foo (\n" 5909 "};\n" 5910 "} // namespace"); 5911 } 5912 5913 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 5914 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 5915 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 5916 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 5917 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 5918 5919 EXPECT_EQ("{\n" 5920 " {\n" 5921 " breakme(\n" 5922 " qwe);\n" 5923 " }\n", 5924 format("{\n" 5925 " {\n" 5926 " breakme(qwe);\n" 5927 "}\n", 5928 getLLVMStyleWithColumns(10))); 5929 } 5930 5931 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 5932 verifyFormat("int x = {\n" 5933 " avariable,\n" 5934 " b(alongervariable)};", 5935 getLLVMStyleWithColumns(25)); 5936 } 5937 5938 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 5939 verifyFormat("return (a)(b){1, 2, 3};"); 5940 } 5941 5942 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 5943 verifyFormat("vector<int> x{1, 2, 3, 4};"); 5944 verifyFormat("vector<int> x{\n" 5945 " 1, 2, 3, 4,\n" 5946 "};"); 5947 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 5948 verifyFormat("f({1, 2});"); 5949 verifyFormat("auto v = Foo{-1};"); 5950 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 5951 verifyFormat("Class::Class : member{1, 2, 3} {}"); 5952 verifyFormat("new vector<int>{1, 2, 3};"); 5953 verifyFormat("new int[3]{1, 2, 3};"); 5954 verifyFormat("new int{1};"); 5955 verifyFormat("return {arg1, arg2};"); 5956 verifyFormat("return {arg1, SomeType{parameter}};"); 5957 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 5958 verifyFormat("new T{arg1, arg2};"); 5959 verifyFormat("f(MyMap[{composite, key}]);"); 5960 verifyFormat("class Class {\n" 5961 " T member = {arg1, arg2};\n" 5962 "};"); 5963 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 5964 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 5965 verifyFormat("int a = std::is_integral<int>{} + 0;"); 5966 5967 verifyFormat("int foo(int i) { return fo1{}(i); }"); 5968 verifyFormat("int foo(int i) { return fo1{}(i); }"); 5969 verifyFormat("auto i = decltype(x){};"); 5970 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 5971 verifyFormat("Node n{1, Node{1000}, //\n" 5972 " 2};"); 5973 verifyFormat("Aaaa aaaaaaa{\n" 5974 " {\n" 5975 " aaaa,\n" 5976 " },\n" 5977 "};"); 5978 verifyFormat("class C : public D {\n" 5979 " SomeClass SC{2};\n" 5980 "};"); 5981 verifyFormat("class C : public A {\n" 5982 " class D : public B {\n" 5983 " void f() { int i{2}; }\n" 5984 " };\n" 5985 "};"); 5986 verifyFormat("#define A {a, a},"); 5987 5988 // Cases where distinguising braced lists and blocks is hard. 5989 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 5990 verifyFormat("void f() {\n" 5991 " return; // comment\n" 5992 "}\n" 5993 "SomeType t;"); 5994 verifyFormat("void f() {\n" 5995 " if (a) {\n" 5996 " f();\n" 5997 " }\n" 5998 "}\n" 5999 "SomeType t;"); 6000 6001 // In combination with BinPackArguments = false. 6002 FormatStyle NoBinPacking = getLLVMStyle(); 6003 NoBinPacking.BinPackArguments = false; 6004 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6005 " bbbbb,\n" 6006 " ccccc,\n" 6007 " ddddd,\n" 6008 " eeeee,\n" 6009 " ffffff,\n" 6010 " ggggg,\n" 6011 " hhhhhh,\n" 6012 " iiiiii,\n" 6013 " jjjjjj,\n" 6014 " kkkkkk};", 6015 NoBinPacking); 6016 verifyFormat("const Aaaaaa aaaaa = {\n" 6017 " aaaaa,\n" 6018 " bbbbb,\n" 6019 " ccccc,\n" 6020 " ddddd,\n" 6021 " eeeee,\n" 6022 " ffffff,\n" 6023 " ggggg,\n" 6024 " hhhhhh,\n" 6025 " iiiiii,\n" 6026 " jjjjjj,\n" 6027 " kkkkkk,\n" 6028 "};", 6029 NoBinPacking); 6030 verifyFormat( 6031 "const Aaaaaa aaaaa = {\n" 6032 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6033 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6034 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6035 "};", 6036 NoBinPacking); 6037 6038 // FIXME: The alignment of these trailing comments might be bad. Then again, 6039 // this might be utterly useless in real code. 6040 verifyFormat("Constructor::Constructor()\n" 6041 " : some_value{ //\n" 6042 " aaaaaaa, //\n" 6043 " bbbbbbb} {}"); 6044 6045 // In braced lists, the first comment is always assumed to belong to the 6046 // first element. Thus, it can be moved to the next or previous line as 6047 // appropriate. 6048 EXPECT_EQ("function({// First element:\n" 6049 " 1,\n" 6050 " // Second element:\n" 6051 " 2});", 6052 format("function({\n" 6053 " // First element:\n" 6054 " 1,\n" 6055 " // Second element:\n" 6056 " 2});")); 6057 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6058 " // First element:\n" 6059 " 1,\n" 6060 " // Second element:\n" 6061 " 2};", 6062 format("std::vector<int> MyNumbers{// First element:\n" 6063 " 1,\n" 6064 " // Second element:\n" 6065 " 2};", 6066 getLLVMStyleWithColumns(30))); 6067 // A trailing comma should still lead to an enforced line break. 6068 EXPECT_EQ("vector<int> SomeVector = {\n" 6069 " // aaa\n" 6070 " 1, 2,\n" 6071 "};", 6072 format("vector<int> SomeVector = { // aaa\n" 6073 " 1, 2, };")); 6074 6075 FormatStyle ExtraSpaces = getLLVMStyle(); 6076 ExtraSpaces.Cpp11BracedListStyle = false; 6077 ExtraSpaces.ColumnLimit = 75; 6078 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6079 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6080 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6081 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6082 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6083 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6084 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6085 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6086 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6087 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6088 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6089 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6090 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6091 verifyFormat("class Class {\n" 6092 " T member = { arg1, arg2 };\n" 6093 "};", 6094 ExtraSpaces); 6095 verifyFormat( 6096 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6097 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6098 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6099 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6100 ExtraSpaces); 6101 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6102 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6103 ExtraSpaces); 6104 verifyFormat( 6105 "someFunction(OtherParam,\n" 6106 " BracedList{ // comment 1 (Forcing interesting break)\n" 6107 " param1, param2,\n" 6108 " // comment 2\n" 6109 " param3, param4 });", 6110 ExtraSpaces); 6111 verifyFormat( 6112 "std::this_thread::sleep_for(\n" 6113 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6114 ExtraSpaces); 6115 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6116 " aaaaaaa,\n" 6117 " aaaaaaaaaa,\n" 6118 " aaaaa,\n" 6119 " aaaaaaaaaaaaaaa,\n" 6120 " aaa,\n" 6121 " aaaaaaaaaa,\n" 6122 " a,\n" 6123 " aaaaaaaaaaaaaaaaaaaaa,\n" 6124 " aaaaaaaaaaaa,\n" 6125 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6126 " aaaaaaa,\n" 6127 " a};"); 6128 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6129 } 6130 6131 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6132 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6133 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6134 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6135 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6136 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6137 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6138 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6139 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6140 " 1, 22, 333, 4444, 55555, //\n" 6141 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6142 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6143 verifyFormat( 6144 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6145 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6146 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6147 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6148 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6149 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6150 " 7777777};"); 6151 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6152 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6153 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6154 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6155 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6156 " // Separating comment.\n" 6157 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6158 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6159 " // Leading comment\n" 6160 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6161 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6162 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6163 " 1, 1, 1, 1};", 6164 getLLVMStyleWithColumns(39)); 6165 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6166 " 1, 1, 1, 1};", 6167 getLLVMStyleWithColumns(38)); 6168 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6169 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6170 getLLVMStyleWithColumns(43)); 6171 verifyFormat( 6172 "static unsigned SomeValues[10][3] = {\n" 6173 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6174 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6175 verifyFormat("static auto fields = new vector<string>{\n" 6176 " \"aaaaaaaaaaaaa\",\n" 6177 " \"aaaaaaaaaaaaa\",\n" 6178 " \"aaaaaaaaaaaa\",\n" 6179 " \"aaaaaaaaaaaaaa\",\n" 6180 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6181 " \"aaaaaaaaaaaa\",\n" 6182 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6183 "};"); 6184 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6185 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6186 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6187 " 3, cccccccccccccccccccccc};", 6188 getLLVMStyleWithColumns(60)); 6189 6190 // Trailing commas. 6191 verifyFormat("vector<int> x = {\n" 6192 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6193 "};", 6194 getLLVMStyleWithColumns(39)); 6195 verifyFormat("vector<int> x = {\n" 6196 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6197 "};", 6198 getLLVMStyleWithColumns(39)); 6199 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6200 " 1, 1, 1, 1,\n" 6201 " /**/ /**/};", 6202 getLLVMStyleWithColumns(39)); 6203 6204 // Trailing comment in the first line. 6205 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6206 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6207 " 111111111, 222222222, 3333333333, 444444444, //\n" 6208 " 11111111, 22222222, 333333333, 44444444};"); 6209 // Trailing comment in the last line. 6210 verifyFormat("int aaaaa[] = {\n" 6211 " 1, 2, 3, // comment\n" 6212 " 4, 5, 6 // comment\n" 6213 "};"); 6214 6215 // With nested lists, we should either format one item per line or all nested 6216 // lists one on line. 6217 // FIXME: For some nested lists, we can do better. 6218 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6219 " {aaaaaaaaaaaaaaaaaaa},\n" 6220 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6221 " {aaaaaaaaaaaaaaaaa}};", 6222 getLLVMStyleWithColumns(60)); 6223 verifyFormat( 6224 "SomeStruct my_struct_array = {\n" 6225 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6226 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6227 " {aaa, aaa},\n" 6228 " {aaa, aaa},\n" 6229 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6230 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6231 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6232 6233 // No column layout should be used here. 6234 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6235 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6236 6237 verifyNoCrash("a<,"); 6238 6239 // No braced initializer here. 6240 verifyFormat("void f() {\n" 6241 " struct Dummy {};\n" 6242 " f(v);\n" 6243 "}"); 6244 6245 // Long lists should be formatted in columns even if they are nested. 6246 verifyFormat( 6247 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6248 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6249 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6250 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6251 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6252 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6253 6254 // Allow "single-column" layout even if that violates the column limit. There 6255 // isn't going to be a better way. 6256 verifyFormat("std::vector<int> a = {\n" 6257 " aaaaaaaa,\n" 6258 " aaaaaaaa,\n" 6259 " aaaaaaaa,\n" 6260 " aaaaaaaa,\n" 6261 " aaaaaaaaaa,\n" 6262 " aaaaaaaa,\n" 6263 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6264 getLLVMStyleWithColumns(30)); 6265 verifyFormat("vector<int> aaaa = {\n" 6266 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6267 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6268 " aaaaaa.aaaaaaa,\n" 6269 " aaaaaa.aaaaaaa,\n" 6270 " aaaaaa.aaaaaaa,\n" 6271 " aaaaaa.aaaaaaa,\n" 6272 "};"); 6273 6274 // Don't create hanging lists. 6275 verifyFormat("someFunction(Param, {List1, List2,\n" 6276 " List3});", 6277 getLLVMStyleWithColumns(35)); 6278 verifyFormat("someFunction(Param, Param,\n" 6279 " {List1, List2,\n" 6280 " List3});", 6281 getLLVMStyleWithColumns(35)); 6282 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6283 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6284 } 6285 6286 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6287 FormatStyle DoNotMerge = getLLVMStyle(); 6288 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6289 6290 verifyFormat("void f() { return 42; }"); 6291 verifyFormat("void f() {\n" 6292 " return 42;\n" 6293 "}", 6294 DoNotMerge); 6295 verifyFormat("void f() {\n" 6296 " // Comment\n" 6297 "}"); 6298 verifyFormat("{\n" 6299 "#error {\n" 6300 " int a;\n" 6301 "}"); 6302 verifyFormat("{\n" 6303 " int a;\n" 6304 "#error {\n" 6305 "}"); 6306 verifyFormat("void f() {} // comment"); 6307 verifyFormat("void f() { int a; } // comment"); 6308 verifyFormat("void f() {\n" 6309 "} // comment", 6310 DoNotMerge); 6311 verifyFormat("void f() {\n" 6312 " int a;\n" 6313 "} // comment", 6314 DoNotMerge); 6315 verifyFormat("void f() {\n" 6316 "} // comment", 6317 getLLVMStyleWithColumns(15)); 6318 6319 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6320 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6321 6322 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6323 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6324 verifyFormat("class C {\n" 6325 " C()\n" 6326 " : iiiiiiii(nullptr),\n" 6327 " kkkkkkk(nullptr),\n" 6328 " mmmmmmm(nullptr),\n" 6329 " nnnnnnn(nullptr) {}\n" 6330 "};", 6331 getGoogleStyle()); 6332 6333 FormatStyle NoColumnLimit = getLLVMStyle(); 6334 NoColumnLimit.ColumnLimit = 0; 6335 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6336 EXPECT_EQ("class C {\n" 6337 " A() : b(0) {}\n" 6338 "};", 6339 format("class C{A():b(0){}};", NoColumnLimit)); 6340 EXPECT_EQ("A()\n" 6341 " : b(0) {\n" 6342 "}", 6343 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6344 6345 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6346 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6347 FormatStyle::SFS_None; 6348 EXPECT_EQ("A()\n" 6349 " : b(0) {\n" 6350 "}", 6351 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6352 EXPECT_EQ("A()\n" 6353 " : b(0) {\n" 6354 "}", 6355 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6356 6357 verifyFormat("#define A \\\n" 6358 " void f() { \\\n" 6359 " int i; \\\n" 6360 " }", 6361 getLLVMStyleWithColumns(20)); 6362 verifyFormat("#define A \\\n" 6363 " void f() { int i; }", 6364 getLLVMStyleWithColumns(21)); 6365 verifyFormat("#define A \\\n" 6366 " void f() { \\\n" 6367 " int i; \\\n" 6368 " } \\\n" 6369 " int j;", 6370 getLLVMStyleWithColumns(22)); 6371 verifyFormat("#define A \\\n" 6372 " void f() { int i; } \\\n" 6373 " int j;", 6374 getLLVMStyleWithColumns(23)); 6375 } 6376 6377 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 6378 FormatStyle MergeEmptyOnly = getLLVMStyle(); 6379 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6380 verifyFormat("class C {\n" 6381 " int f() {}\n" 6382 "};", 6383 MergeEmptyOnly); 6384 verifyFormat("class C {\n" 6385 " int f() {\n" 6386 " return 42;\n" 6387 " }\n" 6388 "};", 6389 MergeEmptyOnly); 6390 verifyFormat("int f() {}", MergeEmptyOnly); 6391 verifyFormat("int f() {\n" 6392 " return 42;\n" 6393 "}", 6394 MergeEmptyOnly); 6395 6396 // Also verify behavior when BraceWrapping.AfterFunction = true 6397 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6398 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 6399 verifyFormat("int f() {}", MergeEmptyOnly); 6400 verifyFormat("class C {\n" 6401 " int f() {}\n" 6402 "};", 6403 MergeEmptyOnly); 6404 } 6405 6406 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6407 FormatStyle MergeInlineOnly = getLLVMStyle(); 6408 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6409 verifyFormat("class C {\n" 6410 " int f() { return 42; }\n" 6411 "};", 6412 MergeInlineOnly); 6413 verifyFormat("int f() {\n" 6414 " return 42;\n" 6415 "}", 6416 MergeInlineOnly); 6417 6418 // SFS_Inline implies SFS_Empty 6419 verifyFormat("class C {\n" 6420 " int f() {}\n" 6421 "};", 6422 MergeInlineOnly); 6423 verifyFormat("int f() {}", MergeInlineOnly); 6424 6425 // Also verify behavior when BraceWrapping.AfterFunction = true 6426 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6427 MergeInlineOnly.BraceWrapping.AfterFunction = true; 6428 verifyFormat("class C {\n" 6429 " int f() { return 42; }\n" 6430 "};", 6431 MergeInlineOnly); 6432 verifyFormat("int f()\n" 6433 "{\n" 6434 " return 42;\n" 6435 "}", 6436 MergeInlineOnly); 6437 6438 // SFS_Inline implies SFS_Empty 6439 verifyFormat("int f() {}", MergeInlineOnly); 6440 verifyFormat("class C {\n" 6441 " int f() {}\n" 6442 "};", 6443 MergeInlineOnly); 6444 } 6445 6446 TEST_F(FormatTest, SplitEmptyFunctionBody) { 6447 FormatStyle Style = getLLVMStyle(); 6448 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6449 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6450 Style.BraceWrapping.AfterFunction = true; 6451 Style.BraceWrapping.SplitEmptyFunctionBody = false; 6452 Style.ColumnLimit = 40; 6453 6454 verifyFormat("int f()\n" 6455 "{}", 6456 Style); 6457 verifyFormat("int f()\n" 6458 "{\n" 6459 " return 42;\n" 6460 "}", 6461 Style); 6462 verifyFormat("int f()\n" 6463 "{\n" 6464 " // some comment\n" 6465 "}", 6466 Style); 6467 6468 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6469 verifyFormat("int f() {}", Style); 6470 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6471 "{}", 6472 Style); 6473 verifyFormat("int f()\n" 6474 "{\n" 6475 " return 0;\n" 6476 "}", 6477 Style); 6478 6479 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6480 verifyFormat("class Foo {\n" 6481 " int f() {}\n" 6482 "};\n", 6483 Style); 6484 verifyFormat("class Foo {\n" 6485 " int f() { return 0; }\n" 6486 "};\n", 6487 Style); 6488 verifyFormat("class Foo {\n" 6489 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6490 " {}\n" 6491 "};\n", 6492 Style); 6493 verifyFormat("class Foo {\n" 6494 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6495 " {\n" 6496 " return 0;\n" 6497 " }\n" 6498 "};\n", 6499 Style); 6500 6501 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6502 verifyFormat("int f() {}", Style); 6503 verifyFormat("int f() { return 0; }", Style); 6504 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6505 "{}", 6506 Style); 6507 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6508 "{\n" 6509 " return 0;\n" 6510 "}", 6511 Style); 6512 } 6513 6514 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6515 // Elaborate type variable declarations. 6516 verifyFormat("struct foo a = {bar};\nint n;"); 6517 verifyFormat("class foo a = {bar};\nint n;"); 6518 verifyFormat("union foo a = {bar};\nint n;"); 6519 6520 // Elaborate types inside function definitions. 6521 verifyFormat("struct foo f() {}\nint n;"); 6522 verifyFormat("class foo f() {}\nint n;"); 6523 verifyFormat("union foo f() {}\nint n;"); 6524 6525 // Templates. 6526 verifyFormat("template <class X> void f() {}\nint n;"); 6527 verifyFormat("template <struct X> void f() {}\nint n;"); 6528 verifyFormat("template <union X> void f() {}\nint n;"); 6529 6530 // Actual definitions... 6531 verifyFormat("struct {\n} n;"); 6532 verifyFormat( 6533 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6534 verifyFormat("union Z {\n int n;\n} x;"); 6535 verifyFormat("class MACRO Z {\n} n;"); 6536 verifyFormat("class MACRO(X) Z {\n} n;"); 6537 verifyFormat("class __attribute__(X) Z {\n} n;"); 6538 verifyFormat("class __declspec(X) Z {\n} n;"); 6539 verifyFormat("class A##B##C {\n} n;"); 6540 verifyFormat("class alignas(16) Z {\n} n;"); 6541 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6542 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6543 6544 // Redefinition from nested context: 6545 verifyFormat("class A::B::C {\n} n;"); 6546 6547 // Template definitions. 6548 verifyFormat( 6549 "template <typename F>\n" 6550 "Matcher(const Matcher<F> &Other,\n" 6551 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6552 " !is_same<F, T>::value>::type * = 0)\n" 6553 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6554 6555 // FIXME: This is still incorrectly handled at the formatter side. 6556 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6557 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6558 6559 // FIXME: 6560 // This now gets parsed incorrectly as class definition. 6561 // verifyFormat("class A<int> f() {\n}\nint n;"); 6562 6563 // Elaborate types where incorrectly parsing the structural element would 6564 // break the indent. 6565 verifyFormat("if (true)\n" 6566 " class X x;\n" 6567 "else\n" 6568 " f();\n"); 6569 6570 // This is simply incomplete. Formatting is not important, but must not crash. 6571 verifyFormat("class A:"); 6572 } 6573 6574 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6575 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6576 format("#error Leave all white!!!!! space* alone!\n")); 6577 EXPECT_EQ( 6578 "#warning Leave all white!!!!! space* alone!\n", 6579 format("#warning Leave all white!!!!! space* alone!\n")); 6580 EXPECT_EQ("#error 1", format(" # error 1")); 6581 EXPECT_EQ("#warning 1", format(" # warning 1")); 6582 } 6583 6584 TEST_F(FormatTest, FormatHashIfExpressions) { 6585 verifyFormat("#if AAAA && BBBB"); 6586 verifyFormat("#if (AAAA && BBBB)"); 6587 verifyFormat("#elif (AAAA && BBBB)"); 6588 // FIXME: Come up with a better indentation for #elif. 6589 verifyFormat( 6590 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6591 " defined(BBBBBBBB)\n" 6592 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6593 " defined(BBBBBBBB)\n" 6594 "#endif", 6595 getLLVMStyleWithColumns(65)); 6596 } 6597 6598 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6599 FormatStyle AllowsMergedIf = getGoogleStyle(); 6600 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6601 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6602 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6603 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6604 EXPECT_EQ("if (true) return 42;", 6605 format("if (true)\nreturn 42;", AllowsMergedIf)); 6606 FormatStyle ShortMergedIf = AllowsMergedIf; 6607 ShortMergedIf.ColumnLimit = 25; 6608 verifyFormat("#define A \\\n" 6609 " if (true) return 42;", 6610 ShortMergedIf); 6611 verifyFormat("#define A \\\n" 6612 " f(); \\\n" 6613 " if (true)\n" 6614 "#define B", 6615 ShortMergedIf); 6616 verifyFormat("#define A \\\n" 6617 " f(); \\\n" 6618 " if (true)\n" 6619 "g();", 6620 ShortMergedIf); 6621 verifyFormat("{\n" 6622 "#ifdef A\n" 6623 " // Comment\n" 6624 " if (true) continue;\n" 6625 "#endif\n" 6626 " // Comment\n" 6627 " if (true) continue;\n" 6628 "}", 6629 ShortMergedIf); 6630 ShortMergedIf.ColumnLimit = 29; 6631 verifyFormat("#define A \\\n" 6632 " if (aaaaaaaaaa) return 1; \\\n" 6633 " return 2;", 6634 ShortMergedIf); 6635 ShortMergedIf.ColumnLimit = 28; 6636 verifyFormat("#define A \\\n" 6637 " if (aaaaaaaaaa) \\\n" 6638 " return 1; \\\n" 6639 " return 2;", 6640 ShortMergedIf); 6641 } 6642 6643 TEST_F(FormatTest, FormatStarDependingOnContext) { 6644 verifyFormat("void f(int *a);"); 6645 verifyFormat("void f() { f(fint * b); }"); 6646 verifyFormat("class A {\n void f(int *a);\n};"); 6647 verifyFormat("class A {\n int *a;\n};"); 6648 verifyFormat("namespace a {\n" 6649 "namespace b {\n" 6650 "class A {\n" 6651 " void f() {}\n" 6652 " int *a;\n" 6653 "};\n" 6654 "} // namespace b\n" 6655 "} // namespace a"); 6656 } 6657 6658 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 6659 verifyFormat("while"); 6660 verifyFormat("operator"); 6661 } 6662 6663 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 6664 // This code would be painfully slow to format if we didn't skip it. 6665 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 6666 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6667 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6668 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6669 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 6670 "A(1, 1)\n" 6671 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 6672 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6673 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6674 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6675 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6676 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6677 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6678 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6679 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 6680 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 6681 // Deeply nested part is untouched, rest is formatted. 6682 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 6683 format(std::string("int i;\n") + Code + "int j;\n", 6684 getLLVMStyle(), SC_ExpectIncomplete)); 6685 } 6686 6687 //===----------------------------------------------------------------------===// 6688 // Objective-C tests. 6689 //===----------------------------------------------------------------------===// 6690 6691 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 6692 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 6693 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 6694 format("-(NSUInteger)indexOfObject:(id)anObject;")); 6695 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 6696 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 6697 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 6698 format("-(NSInteger)Method3:(id)anObject;")); 6699 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 6700 format("-(NSInteger)Method4:(id)anObject;")); 6701 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 6702 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 6703 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 6704 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 6705 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6706 "forAllCells:(BOOL)flag;", 6707 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6708 "forAllCells:(BOOL)flag;")); 6709 6710 // Very long objectiveC method declaration. 6711 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 6712 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 6713 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 6714 " inRange:(NSRange)range\n" 6715 " outRange:(NSRange)out_range\n" 6716 " outRange1:(NSRange)out_range1\n" 6717 " outRange2:(NSRange)out_range2\n" 6718 " outRange3:(NSRange)out_range3\n" 6719 " outRange4:(NSRange)out_range4\n" 6720 " outRange5:(NSRange)out_range5\n" 6721 " outRange6:(NSRange)out_range6\n" 6722 " outRange7:(NSRange)out_range7\n" 6723 " outRange8:(NSRange)out_range8\n" 6724 " outRange9:(NSRange)out_range9;"); 6725 6726 // When the function name has to be wrapped. 6727 FormatStyle Style = getLLVMStyle(); 6728 Style.IndentWrappedFunctionNames = false; 6729 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6730 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6731 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6732 "}", 6733 Style); 6734 Style.IndentWrappedFunctionNames = true; 6735 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6736 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6737 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6738 "}", 6739 Style); 6740 6741 verifyFormat("- (int)sum:(vector<int>)numbers;"); 6742 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 6743 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 6744 // protocol lists (but not for template classes): 6745 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 6746 6747 verifyFormat("- (int (*)())foo:(int (*)())f;"); 6748 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 6749 6750 // If there's no return type (very rare in practice!), LLVM and Google style 6751 // agree. 6752 verifyFormat("- foo;"); 6753 verifyFormat("- foo:(int)f;"); 6754 verifyGoogleFormat("- foo:(int)foo;"); 6755 } 6756 6757 6758 TEST_F(FormatTest, BreaksStringLiterals) { 6759 EXPECT_EQ("\"some text \"\n" 6760 "\"other\";", 6761 format("\"some text other\";", getLLVMStyleWithColumns(12))); 6762 EXPECT_EQ("\"some text \"\n" 6763 "\"other\";", 6764 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 6765 EXPECT_EQ( 6766 "#define A \\\n" 6767 " \"some \" \\\n" 6768 " \"text \" \\\n" 6769 " \"other\";", 6770 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 6771 EXPECT_EQ( 6772 "#define A \\\n" 6773 " \"so \" \\\n" 6774 " \"text \" \\\n" 6775 " \"other\";", 6776 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 6777 6778 EXPECT_EQ("\"some text\"", 6779 format("\"some text\"", getLLVMStyleWithColumns(1))); 6780 EXPECT_EQ("\"some text\"", 6781 format("\"some text\"", getLLVMStyleWithColumns(11))); 6782 EXPECT_EQ("\"some \"\n" 6783 "\"text\"", 6784 format("\"some text\"", getLLVMStyleWithColumns(10))); 6785 EXPECT_EQ("\"some \"\n" 6786 "\"text\"", 6787 format("\"some text\"", getLLVMStyleWithColumns(7))); 6788 EXPECT_EQ("\"some\"\n" 6789 "\" tex\"\n" 6790 "\"t\"", 6791 format("\"some text\"", getLLVMStyleWithColumns(6))); 6792 EXPECT_EQ("\"some\"\n" 6793 "\" tex\"\n" 6794 "\" and\"", 6795 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 6796 EXPECT_EQ("\"some\"\n" 6797 "\"/tex\"\n" 6798 "\"/and\"", 6799 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 6800 6801 EXPECT_EQ("variable =\n" 6802 " \"long string \"\n" 6803 " \"literal\";", 6804 format("variable = \"long string literal\";", 6805 getLLVMStyleWithColumns(20))); 6806 6807 EXPECT_EQ("variable = f(\n" 6808 " \"long string \"\n" 6809 " \"literal\",\n" 6810 " short,\n" 6811 " loooooooooooooooooooong);", 6812 format("variable = f(\"long string literal\", short, " 6813 "loooooooooooooooooooong);", 6814 getLLVMStyleWithColumns(20))); 6815 6816 EXPECT_EQ( 6817 "f(g(\"long string \"\n" 6818 " \"literal\"),\n" 6819 " b);", 6820 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 6821 EXPECT_EQ("f(g(\"long string \"\n" 6822 " \"literal\",\n" 6823 " a),\n" 6824 " b);", 6825 format("f(g(\"long string literal\", a), b);", 6826 getLLVMStyleWithColumns(20))); 6827 EXPECT_EQ( 6828 "f(\"one two\".split(\n" 6829 " variable));", 6830 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 6831 EXPECT_EQ("f(\"one two three four five six \"\n" 6832 " \"seven\".split(\n" 6833 " really_looooong_variable));", 6834 format("f(\"one two three four five six seven\"." 6835 "split(really_looooong_variable));", 6836 getLLVMStyleWithColumns(33))); 6837 6838 EXPECT_EQ("f(\"some \"\n" 6839 " \"text\",\n" 6840 " other);", 6841 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 6842 6843 // Only break as a last resort. 6844 verifyFormat( 6845 "aaaaaaaaaaaaaaaaaaaa(\n" 6846 " aaaaaaaaaaaaaaaaaaaa,\n" 6847 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 6848 6849 EXPECT_EQ("\"splitmea\"\n" 6850 "\"trandomp\"\n" 6851 "\"oint\"", 6852 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 6853 6854 EXPECT_EQ("\"split/\"\n" 6855 "\"pathat/\"\n" 6856 "\"slashes\"", 6857 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 6858 6859 EXPECT_EQ("\"split/\"\n" 6860 "\"pathat/\"\n" 6861 "\"slashes\"", 6862 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 6863 EXPECT_EQ("\"split at \"\n" 6864 "\"spaces/at/\"\n" 6865 "\"slashes.at.any$\"\n" 6866 "\"non-alphanumeric%\"\n" 6867 "\"1111111111characte\"\n" 6868 "\"rs\"", 6869 format("\"split at " 6870 "spaces/at/" 6871 "slashes.at." 6872 "any$non-" 6873 "alphanumeric%" 6874 "1111111111characte" 6875 "rs\"", 6876 getLLVMStyleWithColumns(20))); 6877 6878 // Verify that splitting the strings understands 6879 // Style::AlwaysBreakBeforeMultilineStrings. 6880 EXPECT_EQ( 6881 "aaaaaaaaaaaa(\n" 6882 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 6883 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 6884 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 6885 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 6886 "aaaaaaaaaaaaaaaaaaaaaa\");", 6887 getGoogleStyle())); 6888 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 6889 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 6890 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 6891 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 6892 "aaaaaaaaaaaaaaaaaaaaaa\";", 6893 getGoogleStyle())); 6894 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 6895 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 6896 format("llvm::outs() << " 6897 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 6898 "aaaaaaaaaaaaaaaaaaa\";")); 6899 EXPECT_EQ("ffff(\n" 6900 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 6901 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 6902 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 6903 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 6904 getGoogleStyle())); 6905 6906 FormatStyle Style = getLLVMStyleWithColumns(12); 6907 Style.BreakStringLiterals = false; 6908 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 6909 6910 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 6911 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 6912 EXPECT_EQ("#define A \\\n" 6913 " \"some \" \\\n" 6914 " \"text \" \\\n" 6915 " \"other\";", 6916 format("#define A \"some text other\";", AlignLeft)); 6917 } 6918 6919 TEST_F(FormatTest, FullyRemoveEmptyLines) { 6920 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 6921 NoEmptyLines.MaxEmptyLinesToKeep = 0; 6922 EXPECT_EQ("int i = a(b());", 6923 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 6924 } 6925 6926 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 6927 EXPECT_EQ( 6928 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 6929 "(\n" 6930 " \"x\t\");", 6931 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 6932 "aaaaaaa(" 6933 "\"x\t\");")); 6934 } 6935 6936 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 6937 EXPECT_EQ( 6938 "u8\"utf8 string \"\n" 6939 "u8\"literal\";", 6940 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 6941 EXPECT_EQ( 6942 "u\"utf16 string \"\n" 6943 "u\"literal\";", 6944 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 6945 EXPECT_EQ( 6946 "U\"utf32 string \"\n" 6947 "U\"literal\";", 6948 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 6949 EXPECT_EQ("L\"wide string \"\n" 6950 "L\"literal\";", 6951 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 6952 EXPECT_EQ("@\"NSString \"\n" 6953 "@\"literal\";", 6954 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 6955 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 6956 6957 // This input makes clang-format try to split the incomplete unicode escape 6958 // sequence, which used to lead to a crasher. 6959 verifyNoCrash( 6960 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 6961 getLLVMStyleWithColumns(60)); 6962 } 6963 6964 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 6965 FormatStyle Style = getGoogleStyleWithColumns(15); 6966 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 6967 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 6968 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 6969 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 6970 EXPECT_EQ("u8R\"x(raw literal)x\";", 6971 format("u8R\"x(raw literal)x\";", Style)); 6972 } 6973 6974 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 6975 FormatStyle Style = getLLVMStyleWithColumns(20); 6976 EXPECT_EQ( 6977 "_T(\"aaaaaaaaaaaaaa\")\n" 6978 "_T(\"aaaaaaaaaaaaaa\")\n" 6979 "_T(\"aaaaaaaaaaaa\")", 6980 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 6981 EXPECT_EQ("f(x,\n" 6982 " _T(\"aaaaaaaaaaaa\")\n" 6983 " _T(\"aaa\"),\n" 6984 " z);", 6985 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 6986 6987 // FIXME: Handle embedded spaces in one iteration. 6988 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 6989 // "_T(\"aaaaaaaaaaaaa\")\n" 6990 // "_T(\"aaaaaaaaaaaaa\")\n" 6991 // "_T(\"a\")", 6992 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 6993 // getLLVMStyleWithColumns(20))); 6994 EXPECT_EQ( 6995 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 6996 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 6997 EXPECT_EQ("f(\n" 6998 "#if !TEST\n" 6999 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7000 "#endif\n" 7001 ");", 7002 format("f(\n" 7003 "#if !TEST\n" 7004 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7005 "#endif\n" 7006 ");")); 7007 EXPECT_EQ("f(\n" 7008 "\n" 7009 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7010 format("f(\n" 7011 "\n" 7012 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7013 } 7014 7015 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7016 // In a function call with two operands, the second can be broken with no line 7017 // break before it. 7018 EXPECT_EQ("func(a, \"long long \"\n" 7019 " \"long long\");", 7020 format("func(a, \"long long long long\");", 7021 getLLVMStyleWithColumns(24))); 7022 // In a function call with three operands, the second must be broken with a 7023 // line break before it. 7024 EXPECT_EQ("func(a,\n" 7025 " \"long long long \"\n" 7026 " \"long\",\n" 7027 " c);", 7028 format("func(a, \"long long long long\", c);", 7029 getLLVMStyleWithColumns(24))); 7030 // In a function call with three operands, the third must be broken with a 7031 // line break before it. 7032 EXPECT_EQ("func(a, b,\n" 7033 " \"long long long \"\n" 7034 " \"long\");", 7035 format("func(a, b, \"long long long long\");", 7036 getLLVMStyleWithColumns(24))); 7037 // In a function call with three operands, both the second and the third must 7038 // be broken with a line break before them. 7039 EXPECT_EQ("func(a,\n" 7040 " \"long long long \"\n" 7041 " \"long\",\n" 7042 " \"long long long \"\n" 7043 " \"long\");", 7044 format("func(a, \"long long long long\", \"long long long long\");", 7045 getLLVMStyleWithColumns(24))); 7046 // In a chain of << with two operands, the second can be broken with no line 7047 // break before it. 7048 EXPECT_EQ("a << \"line line \"\n" 7049 " \"line\";", 7050 format("a << \"line line line\";", 7051 getLLVMStyleWithColumns(20))); 7052 // In a chain of << with three operands, the second can be broken with no line 7053 // break before it. 7054 EXPECT_EQ("abcde << \"line \"\n" 7055 " \"line line\"\n" 7056 " << c;", 7057 format("abcde << \"line line line\" << c;", 7058 getLLVMStyleWithColumns(20))); 7059 // In a chain of << with three operands, the third must be broken with a line 7060 // break before it. 7061 EXPECT_EQ("a << b\n" 7062 " << \"line line \"\n" 7063 " \"line\";", 7064 format("a << b << \"line line line\";", 7065 getLLVMStyleWithColumns(20))); 7066 // In a chain of << with three operands, the second can be broken with no line 7067 // break before it and the third must be broken with a line break before it. 7068 EXPECT_EQ("abcd << \"line line \"\n" 7069 " \"line\"\n" 7070 " << \"line line \"\n" 7071 " \"line\";", 7072 format("abcd << \"line line line\" << \"line line line\";", 7073 getLLVMStyleWithColumns(20))); 7074 // In a chain of binary operators with two operands, the second can be broken 7075 // with no line break before it. 7076 EXPECT_EQ("abcd + \"line line \"\n" 7077 " \"line line\";", 7078 format("abcd + \"line line line line\";", 7079 getLLVMStyleWithColumns(20))); 7080 // In a chain of binary operators with three operands, the second must be 7081 // broken with a line break before it. 7082 EXPECT_EQ("abcd +\n" 7083 " \"line line \"\n" 7084 " \"line line\" +\n" 7085 " e;", 7086 format("abcd + \"line line line line\" + e;", 7087 getLLVMStyleWithColumns(20))); 7088 // In a function call with two operands, with AlignAfterOpenBracket enabled, 7089 // the first must be broken with a line break before it. 7090 FormatStyle Style = getLLVMStyleWithColumns(25); 7091 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7092 EXPECT_EQ("someFunction(\n" 7093 " \"long long long \"\n" 7094 " \"long\",\n" 7095 " a);", 7096 format("someFunction(\"long long long long\", a);", Style)); 7097 } 7098 7099 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7100 EXPECT_EQ( 7101 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7102 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7104 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7107 } 7108 7109 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7110 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7111 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7112 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7113 "multiline raw string literal xxxxxxxxxxxxxx\n" 7114 ")x\",\n" 7115 " a),\n" 7116 " b);", 7117 format("fffffffffff(g(R\"x(\n" 7118 "multiline raw string literal xxxxxxxxxxxxxx\n" 7119 ")x\", a), b);", 7120 getGoogleStyleWithColumns(20))); 7121 EXPECT_EQ("fffffffffff(\n" 7122 " g(R\"x(qqq\n" 7123 "multiline raw string literal xxxxxxxxxxxxxx\n" 7124 ")x\",\n" 7125 " a),\n" 7126 " b);", 7127 format("fffffffffff(g(R\"x(qqq\n" 7128 "multiline raw string literal xxxxxxxxxxxxxx\n" 7129 ")x\", a), b);", 7130 getGoogleStyleWithColumns(20))); 7131 7132 EXPECT_EQ("fffffffffff(R\"x(\n" 7133 "multiline raw string literal xxxxxxxxxxxxxx\n" 7134 ")x\");", 7135 format("fffffffffff(R\"x(\n" 7136 "multiline raw string literal xxxxxxxxxxxxxx\n" 7137 ")x\");", 7138 getGoogleStyleWithColumns(20))); 7139 EXPECT_EQ("fffffffffff(R\"x(\n" 7140 "multiline raw string literal xxxxxxxxxxxxxx\n" 7141 ")x\" + bbbbbb);", 7142 format("fffffffffff(R\"x(\n" 7143 "multiline raw string literal xxxxxxxxxxxxxx\n" 7144 ")x\" + bbbbbb);", 7145 getGoogleStyleWithColumns(20))); 7146 EXPECT_EQ("fffffffffff(\n" 7147 " R\"x(\n" 7148 "multiline raw string literal xxxxxxxxxxxxxx\n" 7149 ")x\" +\n" 7150 " bbbbbb);", 7151 format("fffffffffff(\n" 7152 " R\"x(\n" 7153 "multiline raw string literal xxxxxxxxxxxxxx\n" 7154 ")x\" + bbbbbb);", 7155 getGoogleStyleWithColumns(20))); 7156 } 7157 7158 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7159 verifyFormat("string a = \"unterminated;"); 7160 EXPECT_EQ("function(\"unterminated,\n" 7161 " OtherParameter);", 7162 format("function( \"unterminated,\n" 7163 " OtherParameter);")); 7164 } 7165 7166 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7167 FormatStyle Style = getLLVMStyle(); 7168 Style.Standard = FormatStyle::LS_Cpp03; 7169 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7170 format("#define x(_a) printf(\"foo\"_a);", Style)); 7171 } 7172 7173 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 7174 7175 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 7176 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 7177 " \"ddeeefff\");", 7178 format("someFunction(\"aaabbbcccdddeeefff\");", 7179 getLLVMStyleWithColumns(25))); 7180 EXPECT_EQ("someFunction1234567890(\n" 7181 " \"aaabbbcccdddeeefff\");", 7182 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7183 getLLVMStyleWithColumns(26))); 7184 EXPECT_EQ("someFunction1234567890(\n" 7185 " \"aaabbbcccdddeeeff\"\n" 7186 " \"f\");", 7187 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7188 getLLVMStyleWithColumns(25))); 7189 EXPECT_EQ("someFunction1234567890(\n" 7190 " \"aaabbbcccdddeeeff\"\n" 7191 " \"f\");", 7192 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7193 getLLVMStyleWithColumns(24))); 7194 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7195 " \"ddde \"\n" 7196 " \"efff\");", 7197 format("someFunction(\"aaabbbcc ddde efff\");", 7198 getLLVMStyleWithColumns(25))); 7199 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 7200 " \"ddeeefff\");", 7201 format("someFunction(\"aaabbbccc ddeeefff\");", 7202 getLLVMStyleWithColumns(25))); 7203 EXPECT_EQ("someFunction1234567890(\n" 7204 " \"aaabb \"\n" 7205 " \"cccdddeeefff\");", 7206 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 7207 getLLVMStyleWithColumns(25))); 7208 EXPECT_EQ("#define A \\\n" 7209 " string s = \\\n" 7210 " \"123456789\" \\\n" 7211 " \"0\"; \\\n" 7212 " int i;", 7213 format("#define A string s = \"1234567890\"; int i;", 7214 getLLVMStyleWithColumns(20))); 7215 // FIXME: Put additional penalties on breaking at non-whitespace locations. 7216 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7217 " \"dddeeeff\"\n" 7218 " \"f\");", 7219 format("someFunction(\"aaabbbcc dddeeefff\");", 7220 getLLVMStyleWithColumns(25))); 7221 } 7222 7223 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 7224 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 7225 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 7226 EXPECT_EQ("\"test\"\n" 7227 "\"\\n\"", 7228 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 7229 EXPECT_EQ("\"tes\\\\\"\n" 7230 "\"n\"", 7231 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 7232 EXPECT_EQ("\"\\\\\\\\\"\n" 7233 "\"\\n\"", 7234 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 7235 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 7236 EXPECT_EQ("\"\\uff01\"\n" 7237 "\"test\"", 7238 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 7239 EXPECT_EQ("\"\\Uff01ff02\"", 7240 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 7241 EXPECT_EQ("\"\\x000000000001\"\n" 7242 "\"next\"", 7243 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 7244 EXPECT_EQ("\"\\x000000000001next\"", 7245 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 7246 EXPECT_EQ("\"\\x000000000001\"", 7247 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 7248 EXPECT_EQ("\"test\"\n" 7249 "\"\\000000\"\n" 7250 "\"000001\"", 7251 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 7252 EXPECT_EQ("\"test\\000\"\n" 7253 "\"00000000\"\n" 7254 "\"1\"", 7255 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 7256 } 7257 7258 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 7259 verifyFormat("void f() {\n" 7260 " return g() {}\n" 7261 " void h() {}"); 7262 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 7263 "g();\n" 7264 "}"); 7265 } 7266 7267 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 7268 verifyFormat( 7269 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 7270 } 7271 7272 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 7273 verifyFormat("class X {\n" 7274 " void f() {\n" 7275 " }\n" 7276 "};", 7277 getLLVMStyleWithColumns(12)); 7278 } 7279 7280 TEST_F(FormatTest, ConfigurableIndentWidth) { 7281 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 7282 EightIndent.IndentWidth = 8; 7283 EightIndent.ContinuationIndentWidth = 8; 7284 verifyFormat("void f() {\n" 7285 " someFunction();\n" 7286 " if (true) {\n" 7287 " f();\n" 7288 " }\n" 7289 "}", 7290 EightIndent); 7291 verifyFormat("class X {\n" 7292 " void f() {\n" 7293 " }\n" 7294 "};", 7295 EightIndent); 7296 verifyFormat("int x[] = {\n" 7297 " call(),\n" 7298 " call()};", 7299 EightIndent); 7300 } 7301 7302 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 7303 verifyFormat("double\n" 7304 "f();", 7305 getLLVMStyleWithColumns(8)); 7306 } 7307 7308 TEST_F(FormatTest, ConfigurableUseOfTab) { 7309 FormatStyle Tab = getLLVMStyleWithColumns(42); 7310 Tab.IndentWidth = 8; 7311 Tab.UseTab = FormatStyle::UT_Always; 7312 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7313 7314 EXPECT_EQ("if (aaaaaaaa && // q\n" 7315 " bb)\t\t// w\n" 7316 "\t;", 7317 format("if (aaaaaaaa &&// q\n" 7318 "bb)// w\n" 7319 ";", 7320 Tab)); 7321 EXPECT_EQ("if (aaa && bbb) // w\n" 7322 "\t;", 7323 format("if(aaa&&bbb)// w\n" 7324 ";", 7325 Tab)); 7326 7327 verifyFormat("class X {\n" 7328 "\tvoid f() {\n" 7329 "\t\tsomeFunction(parameter1,\n" 7330 "\t\t\t parameter2);\n" 7331 "\t}\n" 7332 "};", 7333 Tab); 7334 verifyFormat("#define A \\\n" 7335 "\tvoid f() { \\\n" 7336 "\t\tsomeFunction( \\\n" 7337 "\t\t parameter1, \\\n" 7338 "\t\t parameter2); \\\n" 7339 "\t}", 7340 Tab); 7341 7342 Tab.TabWidth = 4; 7343 Tab.IndentWidth = 8; 7344 verifyFormat("class TabWidth4Indent8 {\n" 7345 "\t\tvoid f() {\n" 7346 "\t\t\t\tsomeFunction(parameter1,\n" 7347 "\t\t\t\t\t\t\t parameter2);\n" 7348 "\t\t}\n" 7349 "};", 7350 Tab); 7351 7352 Tab.TabWidth = 4; 7353 Tab.IndentWidth = 4; 7354 verifyFormat("class TabWidth4Indent4 {\n" 7355 "\tvoid f() {\n" 7356 "\t\tsomeFunction(parameter1,\n" 7357 "\t\t\t\t\t parameter2);\n" 7358 "\t}\n" 7359 "};", 7360 Tab); 7361 7362 Tab.TabWidth = 8; 7363 Tab.IndentWidth = 4; 7364 verifyFormat("class TabWidth8Indent4 {\n" 7365 " void f() {\n" 7366 "\tsomeFunction(parameter1,\n" 7367 "\t\t parameter2);\n" 7368 " }\n" 7369 "};", 7370 Tab); 7371 7372 Tab.TabWidth = 8; 7373 Tab.IndentWidth = 8; 7374 EXPECT_EQ("/*\n" 7375 "\t a\t\tcomment\n" 7376 "\t in multiple lines\n" 7377 " */", 7378 format(" /*\t \t \n" 7379 " \t \t a\t\tcomment\t \t\n" 7380 " \t \t in multiple lines\t\n" 7381 " \t */", 7382 Tab)); 7383 7384 Tab.UseTab = FormatStyle::UT_ForIndentation; 7385 verifyFormat("{\n" 7386 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7387 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7388 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7389 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7390 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7391 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7392 "};", 7393 Tab); 7394 verifyFormat("enum AA {\n" 7395 "\ta1, // Force multiple lines\n" 7396 "\ta2,\n" 7397 "\ta3\n" 7398 "};", 7399 Tab); 7400 EXPECT_EQ("if (aaaaaaaa && // q\n" 7401 " bb) // w\n" 7402 "\t;", 7403 format("if (aaaaaaaa &&// q\n" 7404 "bb)// w\n" 7405 ";", 7406 Tab)); 7407 verifyFormat("class X {\n" 7408 "\tvoid f() {\n" 7409 "\t\tsomeFunction(parameter1,\n" 7410 "\t\t parameter2);\n" 7411 "\t}\n" 7412 "};", 7413 Tab); 7414 verifyFormat("{\n" 7415 "\tQ(\n" 7416 "\t {\n" 7417 "\t\t int a;\n" 7418 "\t\t someFunction(aaaaaaaa,\n" 7419 "\t\t bbbbbbb);\n" 7420 "\t },\n" 7421 "\t p);\n" 7422 "}", 7423 Tab); 7424 EXPECT_EQ("{\n" 7425 "\t/* aaaa\n" 7426 "\t bbbb */\n" 7427 "}", 7428 format("{\n" 7429 "/* aaaa\n" 7430 " bbbb */\n" 7431 "}", 7432 Tab)); 7433 EXPECT_EQ("{\n" 7434 "\t/*\n" 7435 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7436 "\t bbbbbbbbbbbbb\n" 7437 "\t*/\n" 7438 "}", 7439 format("{\n" 7440 "/*\n" 7441 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7442 "*/\n" 7443 "}", 7444 Tab)); 7445 EXPECT_EQ("{\n" 7446 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7447 "\t// bbbbbbbbbbbbb\n" 7448 "}", 7449 format("{\n" 7450 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7451 "}", 7452 Tab)); 7453 EXPECT_EQ("{\n" 7454 "\t/*\n" 7455 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7456 "\t bbbbbbbbbbbbb\n" 7457 "\t*/\n" 7458 "}", 7459 format("{\n" 7460 "\t/*\n" 7461 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7462 "\t*/\n" 7463 "}", 7464 Tab)); 7465 EXPECT_EQ("{\n" 7466 "\t/*\n" 7467 "\n" 7468 "\t*/\n" 7469 "}", 7470 format("{\n" 7471 "\t/*\n" 7472 "\n" 7473 "\t*/\n" 7474 "}", 7475 Tab)); 7476 EXPECT_EQ("{\n" 7477 "\t/*\n" 7478 " asdf\n" 7479 "\t*/\n" 7480 "}", 7481 format("{\n" 7482 "\t/*\n" 7483 " asdf\n" 7484 "\t*/\n" 7485 "}", 7486 Tab)); 7487 7488 Tab.UseTab = FormatStyle::UT_Never; 7489 EXPECT_EQ("/*\n" 7490 " a\t\tcomment\n" 7491 " in multiple lines\n" 7492 " */", 7493 format(" /*\t \t \n" 7494 " \t \t a\t\tcomment\t \t\n" 7495 " \t \t in multiple lines\t\n" 7496 " \t */", 7497 Tab)); 7498 EXPECT_EQ("/* some\n" 7499 " comment */", 7500 format(" \t \t /* some\n" 7501 " \t \t comment */", 7502 Tab)); 7503 EXPECT_EQ("int a; /* some\n" 7504 " comment */", 7505 format(" \t \t int a; /* some\n" 7506 " \t \t comment */", 7507 Tab)); 7508 7509 EXPECT_EQ("int a; /* some\n" 7510 "comment */", 7511 format(" \t \t int\ta; /* some\n" 7512 " \t \t comment */", 7513 Tab)); 7514 EXPECT_EQ("f(\"\t\t\"); /* some\n" 7515 " comment */", 7516 format(" \t \t f(\"\t\t\"); /* some\n" 7517 " \t \t comment */", 7518 Tab)); 7519 EXPECT_EQ("{\n" 7520 " /*\n" 7521 " * Comment\n" 7522 " */\n" 7523 " int i;\n" 7524 "}", 7525 format("{\n" 7526 "\t/*\n" 7527 "\t * Comment\n" 7528 "\t */\n" 7529 "\t int i;\n" 7530 "}")); 7531 7532 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 7533 Tab.TabWidth = 8; 7534 Tab.IndentWidth = 8; 7535 EXPECT_EQ("if (aaaaaaaa && // q\n" 7536 " bb) // w\n" 7537 "\t;", 7538 format("if (aaaaaaaa &&// q\n" 7539 "bb)// w\n" 7540 ";", 7541 Tab)); 7542 EXPECT_EQ("if (aaa && bbb) // w\n" 7543 "\t;", 7544 format("if(aaa&&bbb)// w\n" 7545 ";", 7546 Tab)); 7547 verifyFormat("class X {\n" 7548 "\tvoid f() {\n" 7549 "\t\tsomeFunction(parameter1,\n" 7550 "\t\t\t parameter2);\n" 7551 "\t}\n" 7552 "};", 7553 Tab); 7554 verifyFormat("#define A \\\n" 7555 "\tvoid f() { \\\n" 7556 "\t\tsomeFunction( \\\n" 7557 "\t\t parameter1, \\\n" 7558 "\t\t parameter2); \\\n" 7559 "\t}", 7560 Tab); 7561 Tab.TabWidth = 4; 7562 Tab.IndentWidth = 8; 7563 verifyFormat("class TabWidth4Indent8 {\n" 7564 "\t\tvoid f() {\n" 7565 "\t\t\t\tsomeFunction(parameter1,\n" 7566 "\t\t\t\t\t\t\t parameter2);\n" 7567 "\t\t}\n" 7568 "};", 7569 Tab); 7570 Tab.TabWidth = 4; 7571 Tab.IndentWidth = 4; 7572 verifyFormat("class TabWidth4Indent4 {\n" 7573 "\tvoid f() {\n" 7574 "\t\tsomeFunction(parameter1,\n" 7575 "\t\t\t\t\t parameter2);\n" 7576 "\t}\n" 7577 "};", 7578 Tab); 7579 Tab.TabWidth = 8; 7580 Tab.IndentWidth = 4; 7581 verifyFormat("class TabWidth8Indent4 {\n" 7582 " void f() {\n" 7583 "\tsomeFunction(parameter1,\n" 7584 "\t\t parameter2);\n" 7585 " }\n" 7586 "};", 7587 Tab); 7588 Tab.TabWidth = 8; 7589 Tab.IndentWidth = 8; 7590 EXPECT_EQ("/*\n" 7591 "\t a\t\tcomment\n" 7592 "\t in multiple lines\n" 7593 " */", 7594 format(" /*\t \t \n" 7595 " \t \t a\t\tcomment\t \t\n" 7596 " \t \t in multiple lines\t\n" 7597 " \t */", 7598 Tab)); 7599 verifyFormat("{\n" 7600 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7601 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7602 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7603 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7604 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7605 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7606 "};", 7607 Tab); 7608 verifyFormat("enum AA {\n" 7609 "\ta1, // Force multiple lines\n" 7610 "\ta2,\n" 7611 "\ta3\n" 7612 "};", 7613 Tab); 7614 EXPECT_EQ("if (aaaaaaaa && // q\n" 7615 " bb) // w\n" 7616 "\t;", 7617 format("if (aaaaaaaa &&// q\n" 7618 "bb)// w\n" 7619 ";", 7620 Tab)); 7621 verifyFormat("class X {\n" 7622 "\tvoid f() {\n" 7623 "\t\tsomeFunction(parameter1,\n" 7624 "\t\t\t parameter2);\n" 7625 "\t}\n" 7626 "};", 7627 Tab); 7628 verifyFormat("{\n" 7629 "\tQ(\n" 7630 "\t {\n" 7631 "\t\t int a;\n" 7632 "\t\t someFunction(aaaaaaaa,\n" 7633 "\t\t\t\t bbbbbbb);\n" 7634 "\t },\n" 7635 "\t p);\n" 7636 "}", 7637 Tab); 7638 EXPECT_EQ("{\n" 7639 "\t/* aaaa\n" 7640 "\t bbbb */\n" 7641 "}", 7642 format("{\n" 7643 "/* aaaa\n" 7644 " bbbb */\n" 7645 "}", 7646 Tab)); 7647 EXPECT_EQ("{\n" 7648 "\t/*\n" 7649 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7650 "\t bbbbbbbbbbbbb\n" 7651 "\t*/\n" 7652 "}", 7653 format("{\n" 7654 "/*\n" 7655 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7656 "*/\n" 7657 "}", 7658 Tab)); 7659 EXPECT_EQ("{\n" 7660 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7661 "\t// bbbbbbbbbbbbb\n" 7662 "}", 7663 format("{\n" 7664 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7665 "}", 7666 Tab)); 7667 EXPECT_EQ("{\n" 7668 "\t/*\n" 7669 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7670 "\t bbbbbbbbbbbbb\n" 7671 "\t*/\n" 7672 "}", 7673 format("{\n" 7674 "\t/*\n" 7675 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7676 "\t*/\n" 7677 "}", 7678 Tab)); 7679 EXPECT_EQ("{\n" 7680 "\t/*\n" 7681 "\n" 7682 "\t*/\n" 7683 "}", 7684 format("{\n" 7685 "\t/*\n" 7686 "\n" 7687 "\t*/\n" 7688 "}", 7689 Tab)); 7690 EXPECT_EQ("{\n" 7691 "\t/*\n" 7692 " asdf\n" 7693 "\t*/\n" 7694 "}", 7695 format("{\n" 7696 "\t/*\n" 7697 " asdf\n" 7698 "\t*/\n" 7699 "}", 7700 Tab)); 7701 EXPECT_EQ("/*\n" 7702 "\t a\t\tcomment\n" 7703 "\t in multiple lines\n" 7704 " */", 7705 format(" /*\t \t \n" 7706 " \t \t a\t\tcomment\t \t\n" 7707 " \t \t in multiple lines\t\n" 7708 " \t */", 7709 Tab)); 7710 EXPECT_EQ("/* some\n" 7711 " comment */", 7712 format(" \t \t /* some\n" 7713 " \t \t comment */", 7714 Tab)); 7715 EXPECT_EQ("int a; /* some\n" 7716 " comment */", 7717 format(" \t \t int a; /* some\n" 7718 " \t \t comment */", 7719 Tab)); 7720 EXPECT_EQ("int a; /* some\n" 7721 "comment */", 7722 format(" \t \t int\ta; /* some\n" 7723 " \t \t comment */", 7724 Tab)); 7725 EXPECT_EQ("f(\"\t\t\"); /* some\n" 7726 " comment */", 7727 format(" \t \t f(\"\t\t\"); /* some\n" 7728 " \t \t comment */", 7729 Tab)); 7730 EXPECT_EQ("{\n" 7731 " /*\n" 7732 " * Comment\n" 7733 " */\n" 7734 " int i;\n" 7735 "}", 7736 format("{\n" 7737 "\t/*\n" 7738 "\t * Comment\n" 7739 "\t */\n" 7740 "\t int i;\n" 7741 "}")); 7742 Tab.AlignConsecutiveAssignments = true; 7743 Tab.AlignConsecutiveDeclarations = true; 7744 Tab.TabWidth = 4; 7745 Tab.IndentWidth = 4; 7746 verifyFormat("class Assign {\n" 7747 "\tvoid f() {\n" 7748 "\t\tint x = 123;\n" 7749 "\t\tint random = 4;\n" 7750 "\t\tstd::string alphabet =\n" 7751 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 7752 "\t}\n" 7753 "};", 7754 Tab); 7755 } 7756 7757 TEST_F(FormatTest, CalculatesOriginalColumn) { 7758 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7759 "q\"; /* some\n" 7760 " comment */", 7761 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7762 "q\"; /* some\n" 7763 " comment */", 7764 getLLVMStyle())); 7765 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 7766 "/* some\n" 7767 " comment */", 7768 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 7769 " /* some\n" 7770 " comment */", 7771 getLLVMStyle())); 7772 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7773 "qqq\n" 7774 "/* some\n" 7775 " comment */", 7776 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7777 "qqq\n" 7778 " /* some\n" 7779 " comment */", 7780 getLLVMStyle())); 7781 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7782 "wwww; /* some\n" 7783 " comment */", 7784 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 7785 "wwww; /* some\n" 7786 " comment */", 7787 getLLVMStyle())); 7788 } 7789 7790 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 7791 FormatStyle NoSpace = getLLVMStyle(); 7792 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 7793 7794 verifyFormat("while(true)\n" 7795 " continue;", 7796 NoSpace); 7797 verifyFormat("for(;;)\n" 7798 " continue;", 7799 NoSpace); 7800 verifyFormat("if(true)\n" 7801 " f();\n" 7802 "else if(true)\n" 7803 " f();", 7804 NoSpace); 7805 verifyFormat("do {\n" 7806 " do_something();\n" 7807 "} while(something());", 7808 NoSpace); 7809 verifyFormat("switch(x) {\n" 7810 "default:\n" 7811 " break;\n" 7812 "}", 7813 NoSpace); 7814 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 7815 verifyFormat("size_t x = sizeof(x);", NoSpace); 7816 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 7817 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 7818 verifyFormat("alignas(128) char a[128];", NoSpace); 7819 verifyFormat("size_t x = alignof(MyType);", NoSpace); 7820 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 7821 verifyFormat("int f() throw(Deprecated);", NoSpace); 7822 verifyFormat("typedef void (*cb)(int);", NoSpace); 7823 verifyFormat("T A::operator()();", NoSpace); 7824 verifyFormat("X A::operator++(T);", NoSpace); 7825 7826 FormatStyle Space = getLLVMStyle(); 7827 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 7828 7829 verifyFormat("int f ();", Space); 7830 verifyFormat("void f (int a, T b) {\n" 7831 " while (true)\n" 7832 " continue;\n" 7833 "}", 7834 Space); 7835 verifyFormat("if (true)\n" 7836 " f ();\n" 7837 "else if (true)\n" 7838 " f ();", 7839 Space); 7840 verifyFormat("do {\n" 7841 " do_something ();\n" 7842 "} while (something ());", 7843 Space); 7844 verifyFormat("switch (x) {\n" 7845 "default:\n" 7846 " break;\n" 7847 "}", 7848 Space); 7849 verifyFormat("A::A () : a (1) {}", Space); 7850 verifyFormat("void f () __attribute__ ((asdf));", Space); 7851 verifyFormat("*(&a + 1);\n" 7852 "&((&a)[1]);\n" 7853 "a[(b + c) * d];\n" 7854 "(((a + 1) * 2) + 3) * 4;", 7855 Space); 7856 verifyFormat("#define A(x) x", Space); 7857 verifyFormat("#define A (x) x", Space); 7858 verifyFormat("#if defined(x)\n" 7859 "#endif", 7860 Space); 7861 verifyFormat("auto i = std::make_unique<int> (5);", Space); 7862 verifyFormat("size_t x = sizeof (x);", Space); 7863 verifyFormat("auto f (int x) -> decltype (x);", Space); 7864 verifyFormat("int f (T x) noexcept (x.create ());", Space); 7865 verifyFormat("alignas (128) char a[128];", Space); 7866 verifyFormat("size_t x = alignof (MyType);", Space); 7867 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 7868 verifyFormat("int f () throw (Deprecated);", Space); 7869 verifyFormat("typedef void (*cb) (int);", Space); 7870 verifyFormat("T A::operator() ();", Space); 7871 verifyFormat("X A::operator++ (T);", Space); 7872 } 7873 7874 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 7875 FormatStyle Spaces = getLLVMStyle(); 7876 7877 Spaces.SpacesInParentheses = true; 7878 verifyFormat("call( x, y, z );", Spaces); 7879 verifyFormat("call();", Spaces); 7880 verifyFormat("std::function<void( int, int )> callback;", Spaces); 7881 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 7882 Spaces); 7883 verifyFormat("while ( (bool)1 )\n" 7884 " continue;", 7885 Spaces); 7886 verifyFormat("for ( ;; )\n" 7887 " continue;", 7888 Spaces); 7889 verifyFormat("if ( true )\n" 7890 " f();\n" 7891 "else if ( true )\n" 7892 " f();", 7893 Spaces); 7894 verifyFormat("do {\n" 7895 " do_something( (int)i );\n" 7896 "} while ( something() );", 7897 Spaces); 7898 verifyFormat("switch ( x ) {\n" 7899 "default:\n" 7900 " break;\n" 7901 "}", 7902 Spaces); 7903 7904 Spaces.SpacesInParentheses = false; 7905 Spaces.SpacesInCStyleCastParentheses = true; 7906 verifyFormat("Type *A = ( Type * )P;", Spaces); 7907 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 7908 verifyFormat("x = ( int32 )y;", Spaces); 7909 verifyFormat("int a = ( int )(2.0f);", Spaces); 7910 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 7911 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 7912 verifyFormat("#define x (( int )-1)", Spaces); 7913 7914 // Run the first set of tests again with: 7915 Spaces.SpacesInParentheses = false; 7916 Spaces.SpaceInEmptyParentheses = true; 7917 Spaces.SpacesInCStyleCastParentheses = true; 7918 verifyFormat("call(x, y, z);", Spaces); 7919 verifyFormat("call( );", Spaces); 7920 verifyFormat("std::function<void(int, int)> callback;", Spaces); 7921 verifyFormat("while (( bool )1)\n" 7922 " continue;", 7923 Spaces); 7924 verifyFormat("for (;;)\n" 7925 " continue;", 7926 Spaces); 7927 verifyFormat("if (true)\n" 7928 " f( );\n" 7929 "else if (true)\n" 7930 " f( );", 7931 Spaces); 7932 verifyFormat("do {\n" 7933 " do_something(( int )i);\n" 7934 "} while (something( ));", 7935 Spaces); 7936 verifyFormat("switch (x) {\n" 7937 "default:\n" 7938 " break;\n" 7939 "}", 7940 Spaces); 7941 7942 // Run the first set of tests again with: 7943 Spaces.SpaceAfterCStyleCast = true; 7944 verifyFormat("call(x, y, z);", Spaces); 7945 verifyFormat("call( );", Spaces); 7946 verifyFormat("std::function<void(int, int)> callback;", Spaces); 7947 verifyFormat("while (( bool ) 1)\n" 7948 " continue;", 7949 Spaces); 7950 verifyFormat("for (;;)\n" 7951 " continue;", 7952 Spaces); 7953 verifyFormat("if (true)\n" 7954 " f( );\n" 7955 "else if (true)\n" 7956 " f( );", 7957 Spaces); 7958 verifyFormat("do {\n" 7959 " do_something(( int ) i);\n" 7960 "} while (something( ));", 7961 Spaces); 7962 verifyFormat("switch (x) {\n" 7963 "default:\n" 7964 " break;\n" 7965 "}", 7966 Spaces); 7967 7968 // Run subset of tests again with: 7969 Spaces.SpacesInCStyleCastParentheses = false; 7970 Spaces.SpaceAfterCStyleCast = true; 7971 verifyFormat("while ((bool) 1)\n" 7972 " continue;", 7973 Spaces); 7974 verifyFormat("do {\n" 7975 " do_something((int) i);\n" 7976 "} while (something( ));", 7977 Spaces); 7978 } 7979 7980 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 7981 verifyFormat("int a[5];"); 7982 verifyFormat("a[3] += 42;"); 7983 7984 FormatStyle Spaces = getLLVMStyle(); 7985 Spaces.SpacesInSquareBrackets = true; 7986 // Lambdas unchanged. 7987 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 7988 verifyFormat("return [i, args...] {};", Spaces); 7989 7990 // Not lambdas. 7991 verifyFormat("int a[ 5 ];", Spaces); 7992 verifyFormat("a[ 3 ] += 42;", Spaces); 7993 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 7994 verifyFormat("double &operator[](int i) { return 0; }\n" 7995 "int i;", 7996 Spaces); 7997 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 7998 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 7999 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8000 } 8001 8002 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8003 verifyFormat("int a = 5;"); 8004 verifyFormat("a += 42;"); 8005 verifyFormat("a or_eq 8;"); 8006 8007 FormatStyle Spaces = getLLVMStyle(); 8008 Spaces.SpaceBeforeAssignmentOperators = false; 8009 verifyFormat("int a= 5;", Spaces); 8010 verifyFormat("a+= 42;", Spaces); 8011 verifyFormat("a or_eq 8;", Spaces); 8012 } 8013 8014 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8015 FormatStyle Alignment = getLLVMStyle(); 8016 Alignment.AlignConsecutiveAssignments = false; 8017 verifyFormat("int a = 5;\n" 8018 "int oneTwoThree = 123;", 8019 Alignment); 8020 verifyFormat("int a = 5;\n" 8021 "int oneTwoThree = 123;", 8022 Alignment); 8023 8024 Alignment.AlignConsecutiveAssignments = true; 8025 verifyFormat("int a = 5;\n" 8026 "int oneTwoThree = 123;", 8027 Alignment); 8028 verifyFormat("int a = method();\n" 8029 "int oneTwoThree = 133;", 8030 Alignment); 8031 verifyFormat("a &= 5;\n" 8032 "bcd *= 5;\n" 8033 "ghtyf += 5;\n" 8034 "dvfvdb -= 5;\n" 8035 "a /= 5;\n" 8036 "vdsvsv %= 5;\n" 8037 "sfdbddfbdfbb ^= 5;\n" 8038 "dvsdsv |= 5;\n" 8039 "int dsvvdvsdvvv = 123;", 8040 Alignment); 8041 verifyFormat("int i = 1, j = 10;\n" 8042 "something = 2000;", 8043 Alignment); 8044 verifyFormat("something = 2000;\n" 8045 "int i = 1, j = 10;\n", 8046 Alignment); 8047 verifyFormat("something = 2000;\n" 8048 "another = 911;\n" 8049 "int i = 1, j = 10;\n" 8050 "oneMore = 1;\n" 8051 "i = 2;", 8052 Alignment); 8053 verifyFormat("int a = 5;\n" 8054 "int one = 1;\n" 8055 "method();\n" 8056 "int oneTwoThree = 123;\n" 8057 "int oneTwo = 12;", 8058 Alignment); 8059 verifyFormat("int oneTwoThree = 123;\n" 8060 "int oneTwo = 12;\n" 8061 "method();\n", 8062 Alignment); 8063 verifyFormat("int oneTwoThree = 123; // comment\n" 8064 "int oneTwo = 12; // comment", 8065 Alignment); 8066 EXPECT_EQ("int a = 5;\n" 8067 "\n" 8068 "int oneTwoThree = 123;", 8069 format("int a = 5;\n" 8070 "\n" 8071 "int oneTwoThree= 123;", 8072 Alignment)); 8073 EXPECT_EQ("int a = 5;\n" 8074 "int one = 1;\n" 8075 "\n" 8076 "int oneTwoThree = 123;", 8077 format("int a = 5;\n" 8078 "int one = 1;\n" 8079 "\n" 8080 "int oneTwoThree = 123;", 8081 Alignment)); 8082 EXPECT_EQ("int a = 5;\n" 8083 "int one = 1;\n" 8084 "\n" 8085 "int oneTwoThree = 123;\n" 8086 "int oneTwo = 12;", 8087 format("int a = 5;\n" 8088 "int one = 1;\n" 8089 "\n" 8090 "int oneTwoThree = 123;\n" 8091 "int oneTwo = 12;", 8092 Alignment)); 8093 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8094 verifyFormat("#define A \\\n" 8095 " int aaaa = 12; \\\n" 8096 " int b = 23; \\\n" 8097 " int ccc = 234; \\\n" 8098 " int dddddddddd = 2345;", 8099 Alignment); 8100 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8101 verifyFormat("#define A \\\n" 8102 " int aaaa = 12; \\\n" 8103 " int b = 23; \\\n" 8104 " int ccc = 234; \\\n" 8105 " int dddddddddd = 2345;", 8106 Alignment); 8107 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8108 verifyFormat("#define A " 8109 " \\\n" 8110 " int aaaa = 12; " 8111 " \\\n" 8112 " int b = 23; " 8113 " \\\n" 8114 " int ccc = 234; " 8115 " \\\n" 8116 " int dddddddddd = 2345;", 8117 Alignment); 8118 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8119 "k = 4, int l = 5,\n" 8120 " int m = 6) {\n" 8121 " int j = 10;\n" 8122 " otherThing = 1;\n" 8123 "}", 8124 Alignment); 8125 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8126 " int i = 1;\n" 8127 " int j = 2;\n" 8128 " int big = 10000;\n" 8129 "}", 8130 Alignment); 8131 verifyFormat("class C {\n" 8132 "public:\n" 8133 " int i = 1;\n" 8134 " virtual void f() = 0;\n" 8135 "};", 8136 Alignment); 8137 verifyFormat("int i = 1;\n" 8138 "if (SomeType t = getSomething()) {\n" 8139 "}\n" 8140 "int j = 2;\n" 8141 "int big = 10000;", 8142 Alignment); 8143 verifyFormat("int j = 7;\n" 8144 "for (int k = 0; k < N; ++k) {\n" 8145 "}\n" 8146 "int j = 2;\n" 8147 "int big = 10000;\n" 8148 "}", 8149 Alignment); 8150 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8151 verifyFormat("int i = 1;\n" 8152 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8153 " = someLooooooooooooooooongFunction();\n" 8154 "int j = 2;", 8155 Alignment); 8156 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8157 verifyFormat("int i = 1;\n" 8158 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8159 " someLooooooooooooooooongFunction();\n" 8160 "int j = 2;", 8161 Alignment); 8162 8163 verifyFormat("auto lambda = []() {\n" 8164 " auto i = 0;\n" 8165 " return 0;\n" 8166 "};\n" 8167 "int i = 0;\n" 8168 "auto v = type{\n" 8169 " i = 1, //\n" 8170 " (i = 2), //\n" 8171 " i = 3 //\n" 8172 "};", 8173 Alignment); 8174 8175 verifyFormat( 8176 "int i = 1;\n" 8177 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8178 " loooooooooooooooooooooongParameterB);\n" 8179 "int j = 2;", 8180 Alignment); 8181 8182 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8183 " typename B = very_long_type_name_1,\n" 8184 " typename T_2 = very_long_type_name_2>\n" 8185 "auto foo() {}\n", 8186 Alignment); 8187 verifyFormat("int a, b = 1;\n" 8188 "int c = 2;\n" 8189 "int dd = 3;\n", 8190 Alignment); 8191 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8192 "float b[1][] = {{3.f}};\n", 8193 Alignment); 8194 verifyFormat("for (int i = 0; i < 1; i++)\n" 8195 " int x = 1;\n", 8196 Alignment); 8197 verifyFormat("for (i = 0; i < 1; i++)\n" 8198 " x = 1;\n" 8199 "y = 1;\n", 8200 Alignment); 8201 } 8202 8203 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8204 FormatStyle Alignment = getLLVMStyle(); 8205 Alignment.AlignConsecutiveDeclarations = false; 8206 verifyFormat("float const a = 5;\n" 8207 "int oneTwoThree = 123;", 8208 Alignment); 8209 verifyFormat("int a = 5;\n" 8210 "float const oneTwoThree = 123;", 8211 Alignment); 8212 8213 Alignment.AlignConsecutiveDeclarations = true; 8214 verifyFormat("float const a = 5;\n" 8215 "int oneTwoThree = 123;", 8216 Alignment); 8217 verifyFormat("int a = method();\n" 8218 "float const oneTwoThree = 133;", 8219 Alignment); 8220 verifyFormat("int i = 1, j = 10;\n" 8221 "something = 2000;", 8222 Alignment); 8223 verifyFormat("something = 2000;\n" 8224 "int i = 1, j = 10;\n", 8225 Alignment); 8226 verifyFormat("float something = 2000;\n" 8227 "double another = 911;\n" 8228 "int i = 1, j = 10;\n" 8229 "const int *oneMore = 1;\n" 8230 "unsigned i = 2;", 8231 Alignment); 8232 verifyFormat("float a = 5;\n" 8233 "int one = 1;\n" 8234 "method();\n" 8235 "const double oneTwoThree = 123;\n" 8236 "const unsigned int oneTwo = 12;", 8237 Alignment); 8238 verifyFormat("int oneTwoThree{0}; // comment\n" 8239 "unsigned oneTwo; // comment", 8240 Alignment); 8241 EXPECT_EQ("float const a = 5;\n" 8242 "\n" 8243 "int oneTwoThree = 123;", 8244 format("float const a = 5;\n" 8245 "\n" 8246 "int oneTwoThree= 123;", 8247 Alignment)); 8248 EXPECT_EQ("float a = 5;\n" 8249 "int one = 1;\n" 8250 "\n" 8251 "unsigned oneTwoThree = 123;", 8252 format("float a = 5;\n" 8253 "int one = 1;\n" 8254 "\n" 8255 "unsigned oneTwoThree = 123;", 8256 Alignment)); 8257 EXPECT_EQ("float a = 5;\n" 8258 "int one = 1;\n" 8259 "\n" 8260 "unsigned oneTwoThree = 123;\n" 8261 "int oneTwo = 12;", 8262 format("float a = 5;\n" 8263 "int one = 1;\n" 8264 "\n" 8265 "unsigned oneTwoThree = 123;\n" 8266 "int oneTwo = 12;", 8267 Alignment)); 8268 // Function prototype alignment 8269 verifyFormat("int a();\n" 8270 "double b();", 8271 Alignment); 8272 verifyFormat("int a(int x);\n" 8273 "double b();", 8274 Alignment); 8275 unsigned OldColumnLimit = Alignment.ColumnLimit; 8276 // We need to set ColumnLimit to zero, in order to stress nested alignments, 8277 // otherwise the function parameters will be re-flowed onto a single line. 8278 Alignment.ColumnLimit = 0; 8279 EXPECT_EQ("int a(int x,\n" 8280 " float y);\n" 8281 "double b(int x,\n" 8282 " double y);", 8283 format("int a(int x,\n" 8284 " float y);\n" 8285 "double b(int x,\n" 8286 " double y);", 8287 Alignment)); 8288 // This ensures that function parameters of function declarations are 8289 // correctly indented when their owning functions are indented. 8290 // The failure case here is for 'double y' to not be indented enough. 8291 EXPECT_EQ("double a(int x);\n" 8292 "int b(int y,\n" 8293 " double z);", 8294 format("double a(int x);\n" 8295 "int b(int y,\n" 8296 " double z);", 8297 Alignment)); 8298 // Set ColumnLimit low so that we induce wrapping immediately after 8299 // the function name and opening paren. 8300 Alignment.ColumnLimit = 13; 8301 verifyFormat("int function(\n" 8302 " int x,\n" 8303 " bool y);", 8304 Alignment); 8305 Alignment.ColumnLimit = OldColumnLimit; 8306 // Ensure function pointers don't screw up recursive alignment 8307 verifyFormat("int a(int x, void (*fp)(int y));\n" 8308 "double b();", 8309 Alignment); 8310 Alignment.AlignConsecutiveAssignments = true; 8311 // Ensure recursive alignment is broken by function braces, so that the 8312 // "a = 1" does not align with subsequent assignments inside the function 8313 // body. 8314 verifyFormat("int func(int a = 1) {\n" 8315 " int b = 2;\n" 8316 " int cc = 3;\n" 8317 "}", 8318 Alignment); 8319 verifyFormat("float something = 2000;\n" 8320 "double another = 911;\n" 8321 "int i = 1, j = 10;\n" 8322 "const int *oneMore = 1;\n" 8323 "unsigned i = 2;", 8324 Alignment); 8325 verifyFormat("int oneTwoThree = {0}; // comment\n" 8326 "unsigned oneTwo = 0; // comment", 8327 Alignment); 8328 // Make sure that scope is correctly tracked, in the absence of braces 8329 verifyFormat("for (int i = 0; i < n; i++)\n" 8330 " j = i;\n" 8331 "double x = 1;\n", 8332 Alignment); 8333 verifyFormat("if (int i = 0)\n" 8334 " j = i;\n" 8335 "double x = 1;\n", 8336 Alignment); 8337 // Ensure operator[] and operator() are comprehended 8338 verifyFormat("struct test {\n" 8339 " long long int foo();\n" 8340 " int operator[](int a);\n" 8341 " double bar();\n" 8342 "};\n", 8343 Alignment); 8344 verifyFormat("struct test {\n" 8345 " long long int foo();\n" 8346 " int operator()(int a);\n" 8347 " double bar();\n" 8348 "};\n", 8349 Alignment); 8350 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 8351 " int const i = 1;\n" 8352 " int * j = 2;\n" 8353 " int big = 10000;\n" 8354 "\n" 8355 " unsigned oneTwoThree = 123;\n" 8356 " int oneTwo = 12;\n" 8357 " method();\n" 8358 " float k = 2;\n" 8359 " int ll = 10000;\n" 8360 "}", 8361 format("void SomeFunction(int parameter= 0) {\n" 8362 " int const i= 1;\n" 8363 " int *j=2;\n" 8364 " int big = 10000;\n" 8365 "\n" 8366 "unsigned oneTwoThree =123;\n" 8367 "int oneTwo = 12;\n" 8368 " method();\n" 8369 "float k= 2;\n" 8370 "int ll=10000;\n" 8371 "}", 8372 Alignment)); 8373 Alignment.AlignConsecutiveAssignments = false; 8374 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8375 verifyFormat("#define A \\\n" 8376 " int aaaa = 12; \\\n" 8377 " float b = 23; \\\n" 8378 " const int ccc = 234; \\\n" 8379 " unsigned dddddddddd = 2345;", 8380 Alignment); 8381 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8382 verifyFormat("#define A \\\n" 8383 " int aaaa = 12; \\\n" 8384 " float b = 23; \\\n" 8385 " const int ccc = 234; \\\n" 8386 " unsigned dddddddddd = 2345;", 8387 Alignment); 8388 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8389 Alignment.ColumnLimit = 30; 8390 verifyFormat("#define A \\\n" 8391 " int aaaa = 12; \\\n" 8392 " float b = 23; \\\n" 8393 " const int ccc = 234; \\\n" 8394 " int dddddddddd = 2345;", 8395 Alignment); 8396 Alignment.ColumnLimit = 80; 8397 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8398 "k = 4, int l = 5,\n" 8399 " int m = 6) {\n" 8400 " const int j = 10;\n" 8401 " otherThing = 1;\n" 8402 "}", 8403 Alignment); 8404 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8405 " int const i = 1;\n" 8406 " int * j = 2;\n" 8407 " int big = 10000;\n" 8408 "}", 8409 Alignment); 8410 verifyFormat("class C {\n" 8411 "public:\n" 8412 " int i = 1;\n" 8413 " virtual void f() = 0;\n" 8414 "};", 8415 Alignment); 8416 verifyFormat("float i = 1;\n" 8417 "if (SomeType t = getSomething()) {\n" 8418 "}\n" 8419 "const unsigned j = 2;\n" 8420 "int big = 10000;", 8421 Alignment); 8422 verifyFormat("float j = 7;\n" 8423 "for (int k = 0; k < N; ++k) {\n" 8424 "}\n" 8425 "unsigned j = 2;\n" 8426 "int big = 10000;\n" 8427 "}", 8428 Alignment); 8429 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8430 verifyFormat("float i = 1;\n" 8431 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8432 " = someLooooooooooooooooongFunction();\n" 8433 "int j = 2;", 8434 Alignment); 8435 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8436 verifyFormat("int i = 1;\n" 8437 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8438 " someLooooooooooooooooongFunction();\n" 8439 "int j = 2;", 8440 Alignment); 8441 8442 Alignment.AlignConsecutiveAssignments = true; 8443 verifyFormat("auto lambda = []() {\n" 8444 " auto ii = 0;\n" 8445 " float j = 0;\n" 8446 " return 0;\n" 8447 "};\n" 8448 "int i = 0;\n" 8449 "float i2 = 0;\n" 8450 "auto v = type{\n" 8451 " i = 1, //\n" 8452 " (i = 2), //\n" 8453 " i = 3 //\n" 8454 "};", 8455 Alignment); 8456 Alignment.AlignConsecutiveAssignments = false; 8457 8458 verifyFormat( 8459 "int i = 1;\n" 8460 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8461 " loooooooooooooooooooooongParameterB);\n" 8462 "int j = 2;", 8463 Alignment); 8464 8465 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 8466 // We expect declarations and assignments to align, as long as it doesn't 8467 // exceed the column limit, starting a new alignment sequence whenever it 8468 // happens. 8469 Alignment.AlignConsecutiveAssignments = true; 8470 Alignment.ColumnLimit = 30; 8471 verifyFormat("float ii = 1;\n" 8472 "unsigned j = 2;\n" 8473 "int someVerylongVariable = 1;\n" 8474 "AnotherLongType ll = 123456;\n" 8475 "VeryVeryLongType k = 2;\n" 8476 "int myvar = 1;", 8477 Alignment); 8478 Alignment.ColumnLimit = 80; 8479 Alignment.AlignConsecutiveAssignments = false; 8480 8481 verifyFormat( 8482 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 8483 " typename LongType, typename B>\n" 8484 "auto foo() {}\n", 8485 Alignment); 8486 verifyFormat("float a, b = 1;\n" 8487 "int c = 2;\n" 8488 "int dd = 3;\n", 8489 Alignment); 8490 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8491 "float b[1][] = {{3.f}};\n", 8492 Alignment); 8493 Alignment.AlignConsecutiveAssignments = true; 8494 verifyFormat("float a, b = 1;\n" 8495 "int c = 2;\n" 8496 "int dd = 3;\n", 8497 Alignment); 8498 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8499 "float b[1][] = {{3.f}};\n", 8500 Alignment); 8501 Alignment.AlignConsecutiveAssignments = false; 8502 8503 Alignment.ColumnLimit = 30; 8504 Alignment.BinPackParameters = false; 8505 verifyFormat("void foo(float a,\n" 8506 " float b,\n" 8507 " int c,\n" 8508 " uint32_t *d) {\n" 8509 " int * e = 0;\n" 8510 " float f = 0;\n" 8511 " double g = 0;\n" 8512 "}\n" 8513 "void bar(ino_t a,\n" 8514 " int b,\n" 8515 " uint32_t *c,\n" 8516 " bool d) {}\n", 8517 Alignment); 8518 Alignment.BinPackParameters = true; 8519 Alignment.ColumnLimit = 80; 8520 } 8521 8522 TEST_F(FormatTest, LinuxBraceBreaking) { 8523 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8524 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8525 verifyFormat("namespace a\n" 8526 "{\n" 8527 "class A\n" 8528 "{\n" 8529 " void f()\n" 8530 " {\n" 8531 " if (true) {\n" 8532 " a();\n" 8533 " b();\n" 8534 " } else {\n" 8535 " a();\n" 8536 " }\n" 8537 " }\n" 8538 " void g() { return; }\n" 8539 "};\n" 8540 "struct B {\n" 8541 " int x;\n" 8542 "};\n" 8543 "}\n", 8544 LinuxBraceStyle); 8545 verifyFormat("enum X {\n" 8546 " Y = 0,\n" 8547 "}\n", 8548 LinuxBraceStyle); 8549 verifyFormat("struct S {\n" 8550 " int Type;\n" 8551 " union {\n" 8552 " int x;\n" 8553 " double y;\n" 8554 " } Value;\n" 8555 " class C\n" 8556 " {\n" 8557 " MyFavoriteType Value;\n" 8558 " } Class;\n" 8559 "}\n", 8560 LinuxBraceStyle); 8561 } 8562 8563 TEST_F(FormatTest, MozillaBraceBreaking) { 8564 FormatStyle MozillaBraceStyle = getLLVMStyle(); 8565 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 8566 MozillaBraceStyle.FixNamespaceComments = false; 8567 verifyFormat("namespace a {\n" 8568 "class A\n" 8569 "{\n" 8570 " void f()\n" 8571 " {\n" 8572 " if (true) {\n" 8573 " a();\n" 8574 " b();\n" 8575 " }\n" 8576 " }\n" 8577 " void g() { return; }\n" 8578 "};\n" 8579 "enum E\n" 8580 "{\n" 8581 " A,\n" 8582 " // foo\n" 8583 " B,\n" 8584 " C\n" 8585 "};\n" 8586 "struct B\n" 8587 "{\n" 8588 " int x;\n" 8589 "};\n" 8590 "}\n", 8591 MozillaBraceStyle); 8592 verifyFormat("struct S\n" 8593 "{\n" 8594 " int Type;\n" 8595 " union\n" 8596 " {\n" 8597 " int x;\n" 8598 " double y;\n" 8599 " } Value;\n" 8600 " class C\n" 8601 " {\n" 8602 " MyFavoriteType Value;\n" 8603 " } Class;\n" 8604 "}\n", 8605 MozillaBraceStyle); 8606 } 8607 8608 TEST_F(FormatTest, StroustrupBraceBreaking) { 8609 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 8610 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8611 verifyFormat("namespace a {\n" 8612 "class A {\n" 8613 " void f()\n" 8614 " {\n" 8615 " if (true) {\n" 8616 " a();\n" 8617 " b();\n" 8618 " }\n" 8619 " }\n" 8620 " void g() { return; }\n" 8621 "};\n" 8622 "struct B {\n" 8623 " int x;\n" 8624 "};\n" 8625 "} // namespace a\n", 8626 StroustrupBraceStyle); 8627 8628 verifyFormat("void foo()\n" 8629 "{\n" 8630 " if (a) {\n" 8631 " a();\n" 8632 " }\n" 8633 " else {\n" 8634 " b();\n" 8635 " }\n" 8636 "}\n", 8637 StroustrupBraceStyle); 8638 8639 verifyFormat("#ifdef _DEBUG\n" 8640 "int foo(int i = 0)\n" 8641 "#else\n" 8642 "int foo(int i = 5)\n" 8643 "#endif\n" 8644 "{\n" 8645 " return i;\n" 8646 "}", 8647 StroustrupBraceStyle); 8648 8649 verifyFormat("void foo() {}\n" 8650 "void bar()\n" 8651 "#ifdef _DEBUG\n" 8652 "{\n" 8653 " foo();\n" 8654 "}\n" 8655 "#else\n" 8656 "{\n" 8657 "}\n" 8658 "#endif", 8659 StroustrupBraceStyle); 8660 8661 verifyFormat("void foobar() { int i = 5; }\n" 8662 "#ifdef _DEBUG\n" 8663 "void bar() {}\n" 8664 "#else\n" 8665 "void bar() { foobar(); }\n" 8666 "#endif", 8667 StroustrupBraceStyle); 8668 } 8669 8670 TEST_F(FormatTest, AllmanBraceBreaking) { 8671 FormatStyle AllmanBraceStyle = getLLVMStyle(); 8672 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 8673 verifyFormat("namespace a\n" 8674 "{\n" 8675 "class A\n" 8676 "{\n" 8677 " void f()\n" 8678 " {\n" 8679 " if (true)\n" 8680 " {\n" 8681 " a();\n" 8682 " b();\n" 8683 " }\n" 8684 " }\n" 8685 " void g() { return; }\n" 8686 "};\n" 8687 "struct B\n" 8688 "{\n" 8689 " int x;\n" 8690 "};\n" 8691 "}", 8692 AllmanBraceStyle); 8693 8694 verifyFormat("void f()\n" 8695 "{\n" 8696 " if (true)\n" 8697 " {\n" 8698 " a();\n" 8699 " }\n" 8700 " else if (false)\n" 8701 " {\n" 8702 " b();\n" 8703 " }\n" 8704 " else\n" 8705 " {\n" 8706 " c();\n" 8707 " }\n" 8708 "}\n", 8709 AllmanBraceStyle); 8710 8711 verifyFormat("void f()\n" 8712 "{\n" 8713 " for (int i = 0; i < 10; ++i)\n" 8714 " {\n" 8715 " a();\n" 8716 " }\n" 8717 " while (false)\n" 8718 " {\n" 8719 " b();\n" 8720 " }\n" 8721 " do\n" 8722 " {\n" 8723 " c();\n" 8724 " } while (false)\n" 8725 "}\n", 8726 AllmanBraceStyle); 8727 8728 verifyFormat("void f(int a)\n" 8729 "{\n" 8730 " switch (a)\n" 8731 " {\n" 8732 " case 0:\n" 8733 " break;\n" 8734 " case 1:\n" 8735 " {\n" 8736 " break;\n" 8737 " }\n" 8738 " case 2:\n" 8739 " {\n" 8740 " }\n" 8741 " break;\n" 8742 " default:\n" 8743 " break;\n" 8744 " }\n" 8745 "}\n", 8746 AllmanBraceStyle); 8747 8748 verifyFormat("enum X\n" 8749 "{\n" 8750 " Y = 0,\n" 8751 "}\n", 8752 AllmanBraceStyle); 8753 verifyFormat("enum X\n" 8754 "{\n" 8755 " Y = 0\n" 8756 "}\n", 8757 AllmanBraceStyle); 8758 8759 verifyFormat("@interface BSApplicationController ()\n" 8760 "{\n" 8761 "@private\n" 8762 " id _extraIvar;\n" 8763 "}\n" 8764 "@end\n", 8765 AllmanBraceStyle); 8766 8767 verifyFormat("#ifdef _DEBUG\n" 8768 "int foo(int i = 0)\n" 8769 "#else\n" 8770 "int foo(int i = 5)\n" 8771 "#endif\n" 8772 "{\n" 8773 " return i;\n" 8774 "}", 8775 AllmanBraceStyle); 8776 8777 verifyFormat("void foo() {}\n" 8778 "void bar()\n" 8779 "#ifdef _DEBUG\n" 8780 "{\n" 8781 " foo();\n" 8782 "}\n" 8783 "#else\n" 8784 "{\n" 8785 "}\n" 8786 "#endif", 8787 AllmanBraceStyle); 8788 8789 verifyFormat("void foobar() { int i = 5; }\n" 8790 "#ifdef _DEBUG\n" 8791 "void bar() {}\n" 8792 "#else\n" 8793 "void bar() { foobar(); }\n" 8794 "#endif", 8795 AllmanBraceStyle); 8796 8797 // This shouldn't affect ObjC blocks.. 8798 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 8799 " // ...\n" 8800 " int i;\n" 8801 "}];", 8802 AllmanBraceStyle); 8803 verifyFormat("void (^block)(void) = ^{\n" 8804 " // ...\n" 8805 " int i;\n" 8806 "};", 8807 AllmanBraceStyle); 8808 // .. or dict literals. 8809 verifyFormat("void f()\n" 8810 "{\n" 8811 " // ...\n" 8812 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 8813 "}", 8814 AllmanBraceStyle); 8815 verifyFormat("void f()\n" 8816 "{\n" 8817 " // ...\n" 8818 " [object someMethod:@{a : @\"b\"}];\n" 8819 "}", 8820 AllmanBraceStyle); 8821 verifyFormat("int f()\n" 8822 "{ // comment\n" 8823 " return 42;\n" 8824 "}", 8825 AllmanBraceStyle); 8826 8827 AllmanBraceStyle.ColumnLimit = 19; 8828 verifyFormat("void f() { int i; }", AllmanBraceStyle); 8829 AllmanBraceStyle.ColumnLimit = 18; 8830 verifyFormat("void f()\n" 8831 "{\n" 8832 " int i;\n" 8833 "}", 8834 AllmanBraceStyle); 8835 AllmanBraceStyle.ColumnLimit = 80; 8836 8837 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 8838 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 8839 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 8840 verifyFormat("void f(bool b)\n" 8841 "{\n" 8842 " if (b)\n" 8843 " {\n" 8844 " return;\n" 8845 " }\n" 8846 "}\n", 8847 BreakBeforeBraceShortIfs); 8848 verifyFormat("void f(bool b)\n" 8849 "{\n" 8850 " if (b) return;\n" 8851 "}\n", 8852 BreakBeforeBraceShortIfs); 8853 verifyFormat("void f(bool b)\n" 8854 "{\n" 8855 " while (b)\n" 8856 " {\n" 8857 " return;\n" 8858 " }\n" 8859 "}\n", 8860 BreakBeforeBraceShortIfs); 8861 } 8862 8863 TEST_F(FormatTest, GNUBraceBreaking) { 8864 FormatStyle GNUBraceStyle = getLLVMStyle(); 8865 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 8866 verifyFormat("namespace a\n" 8867 "{\n" 8868 "class A\n" 8869 "{\n" 8870 " void f()\n" 8871 " {\n" 8872 " int a;\n" 8873 " {\n" 8874 " int b;\n" 8875 " }\n" 8876 " if (true)\n" 8877 " {\n" 8878 " a();\n" 8879 " b();\n" 8880 " }\n" 8881 " }\n" 8882 " void g() { return; }\n" 8883 "}\n" 8884 "}", 8885 GNUBraceStyle); 8886 8887 verifyFormat("void f()\n" 8888 "{\n" 8889 " if (true)\n" 8890 " {\n" 8891 " a();\n" 8892 " }\n" 8893 " else if (false)\n" 8894 " {\n" 8895 " b();\n" 8896 " }\n" 8897 " else\n" 8898 " {\n" 8899 " c();\n" 8900 " }\n" 8901 "}\n", 8902 GNUBraceStyle); 8903 8904 verifyFormat("void f()\n" 8905 "{\n" 8906 " for (int i = 0; i < 10; ++i)\n" 8907 " {\n" 8908 " a();\n" 8909 " }\n" 8910 " while (false)\n" 8911 " {\n" 8912 " b();\n" 8913 " }\n" 8914 " do\n" 8915 " {\n" 8916 " c();\n" 8917 " }\n" 8918 " while (false);\n" 8919 "}\n", 8920 GNUBraceStyle); 8921 8922 verifyFormat("void f(int a)\n" 8923 "{\n" 8924 " switch (a)\n" 8925 " {\n" 8926 " case 0:\n" 8927 " break;\n" 8928 " case 1:\n" 8929 " {\n" 8930 " break;\n" 8931 " }\n" 8932 " case 2:\n" 8933 " {\n" 8934 " }\n" 8935 " break;\n" 8936 " default:\n" 8937 " break;\n" 8938 " }\n" 8939 "}\n", 8940 GNUBraceStyle); 8941 8942 verifyFormat("enum X\n" 8943 "{\n" 8944 " Y = 0,\n" 8945 "}\n", 8946 GNUBraceStyle); 8947 8948 verifyFormat("@interface BSApplicationController ()\n" 8949 "{\n" 8950 "@private\n" 8951 " id _extraIvar;\n" 8952 "}\n" 8953 "@end\n", 8954 GNUBraceStyle); 8955 8956 verifyFormat("#ifdef _DEBUG\n" 8957 "int foo(int i = 0)\n" 8958 "#else\n" 8959 "int foo(int i = 5)\n" 8960 "#endif\n" 8961 "{\n" 8962 " return i;\n" 8963 "}", 8964 GNUBraceStyle); 8965 8966 verifyFormat("void foo() {}\n" 8967 "void bar()\n" 8968 "#ifdef _DEBUG\n" 8969 "{\n" 8970 " foo();\n" 8971 "}\n" 8972 "#else\n" 8973 "{\n" 8974 "}\n" 8975 "#endif", 8976 GNUBraceStyle); 8977 8978 verifyFormat("void foobar() { int i = 5; }\n" 8979 "#ifdef _DEBUG\n" 8980 "void bar() {}\n" 8981 "#else\n" 8982 "void bar() { foobar(); }\n" 8983 "#endif", 8984 GNUBraceStyle); 8985 } 8986 8987 TEST_F(FormatTest, WebKitBraceBreaking) { 8988 FormatStyle WebKitBraceStyle = getLLVMStyle(); 8989 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 8990 WebKitBraceStyle.FixNamespaceComments = false; 8991 verifyFormat("namespace a {\n" 8992 "class A {\n" 8993 " void f()\n" 8994 " {\n" 8995 " if (true) {\n" 8996 " a();\n" 8997 " b();\n" 8998 " }\n" 8999 " }\n" 9000 " void g() { return; }\n" 9001 "};\n" 9002 "enum E {\n" 9003 " A,\n" 9004 " // foo\n" 9005 " B,\n" 9006 " C\n" 9007 "};\n" 9008 "struct B {\n" 9009 " int x;\n" 9010 "};\n" 9011 "}\n", 9012 WebKitBraceStyle); 9013 verifyFormat("struct S {\n" 9014 " int Type;\n" 9015 " union {\n" 9016 " int x;\n" 9017 " double y;\n" 9018 " } Value;\n" 9019 " class C {\n" 9020 " MyFavoriteType Value;\n" 9021 " } Class;\n" 9022 "};\n", 9023 WebKitBraceStyle); 9024 } 9025 9026 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9027 verifyFormat("void f() {\n" 9028 " try {\n" 9029 " } catch (const Exception &e) {\n" 9030 " }\n" 9031 "}\n", 9032 getLLVMStyle()); 9033 } 9034 9035 TEST_F(FormatTest, UnderstandsPragmas) { 9036 verifyFormat("#pragma omp reduction(| : var)"); 9037 verifyFormat("#pragma omp reduction(+ : var)"); 9038 9039 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9040 "(including parentheses).", 9041 format("#pragma mark Any non-hyphenated or hyphenated string " 9042 "(including parentheses).")); 9043 } 9044 9045 TEST_F(FormatTest, UnderstandPragmaOption) { 9046 verifyFormat("#pragma option -C -A"); 9047 9048 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9049 } 9050 9051 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9052 for (size_t i = 1; i < Styles.size(); ++i) \ 9053 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9054 << " differs from Style #0" 9055 9056 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9057 SmallVector<FormatStyle, 3> Styles; 9058 Styles.resize(3); 9059 9060 Styles[0] = getLLVMStyle(); 9061 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9062 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9063 EXPECT_ALL_STYLES_EQUAL(Styles); 9064 9065 Styles[0] = getGoogleStyle(); 9066 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9067 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9068 EXPECT_ALL_STYLES_EQUAL(Styles); 9069 9070 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9071 EXPECT_TRUE( 9072 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9073 EXPECT_TRUE( 9074 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9075 EXPECT_ALL_STYLES_EQUAL(Styles); 9076 9077 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9078 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9079 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9080 EXPECT_ALL_STYLES_EQUAL(Styles); 9081 9082 Styles[0] = getMozillaStyle(); 9083 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9084 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9085 EXPECT_ALL_STYLES_EQUAL(Styles); 9086 9087 Styles[0] = getWebKitStyle(); 9088 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9089 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9090 EXPECT_ALL_STYLES_EQUAL(Styles); 9091 9092 Styles[0] = getGNUStyle(); 9093 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9094 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9095 EXPECT_ALL_STYLES_EQUAL(Styles); 9096 9097 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9098 } 9099 9100 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9101 SmallVector<FormatStyle, 8> Styles; 9102 Styles.resize(2); 9103 9104 Styles[0] = getGoogleStyle(); 9105 Styles[1] = getLLVMStyle(); 9106 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9107 EXPECT_ALL_STYLES_EQUAL(Styles); 9108 9109 Styles.resize(5); 9110 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9111 Styles[1] = getLLVMStyle(); 9112 Styles[1].Language = FormatStyle::LK_JavaScript; 9113 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9114 9115 Styles[2] = getLLVMStyle(); 9116 Styles[2].Language = FormatStyle::LK_JavaScript; 9117 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9118 "BasedOnStyle: Google", 9119 &Styles[2]) 9120 .value()); 9121 9122 Styles[3] = getLLVMStyle(); 9123 Styles[3].Language = FormatStyle::LK_JavaScript; 9124 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9125 "Language: JavaScript", 9126 &Styles[3]) 9127 .value()); 9128 9129 Styles[4] = getLLVMStyle(); 9130 Styles[4].Language = FormatStyle::LK_JavaScript; 9131 EXPECT_EQ(0, parseConfiguration("---\n" 9132 "BasedOnStyle: LLVM\n" 9133 "IndentWidth: 123\n" 9134 "---\n" 9135 "BasedOnStyle: Google\n" 9136 "Language: JavaScript", 9137 &Styles[4]) 9138 .value()); 9139 EXPECT_ALL_STYLES_EQUAL(Styles); 9140 } 9141 9142 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9143 Style.FIELD = false; \ 9144 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9145 EXPECT_TRUE(Style.FIELD); \ 9146 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9147 EXPECT_FALSE(Style.FIELD); 9148 9149 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9150 9151 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9152 Style.STRUCT.FIELD = false; \ 9153 EXPECT_EQ(0, \ 9154 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9155 .value()); \ 9156 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9157 EXPECT_EQ(0, \ 9158 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9159 .value()); \ 9160 EXPECT_FALSE(Style.STRUCT.FIELD); 9161 9162 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9163 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9164 9165 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9166 EXPECT_NE(VALUE, Style.FIELD); \ 9167 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9168 EXPECT_EQ(VALUE, Style.FIELD) 9169 9170 TEST_F(FormatTest, ParsesConfigurationBools) { 9171 FormatStyle Style = {}; 9172 Style.Language = FormatStyle::LK_Cpp; 9173 CHECK_PARSE_BOOL(AlignOperands); 9174 CHECK_PARSE_BOOL(AlignTrailingComments); 9175 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9176 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9177 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9178 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9179 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9180 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9181 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9182 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9183 CHECK_PARSE_BOOL(BinPackArguments); 9184 CHECK_PARSE_BOOL(BinPackParameters); 9185 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9186 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9187 CHECK_PARSE_BOOL(BreakStringLiterals); 9188 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 9189 CHECK_PARSE_BOOL(CompactNamespaces); 9190 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9191 CHECK_PARSE_BOOL(DerivePointerAlignment); 9192 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9193 CHECK_PARSE_BOOL(DisableFormat); 9194 CHECK_PARSE_BOOL(IndentCaseLabels); 9195 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9196 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9197 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9198 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9199 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9200 CHECK_PARSE_BOOL(ReflowComments); 9201 CHECK_PARSE_BOOL(SortIncludes); 9202 CHECK_PARSE_BOOL(SpacesInParentheses); 9203 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9204 CHECK_PARSE_BOOL(SpacesInAngles); 9205 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9206 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9207 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9208 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9209 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 9210 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9211 9212 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9213 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9214 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9215 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9216 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9217 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9218 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9219 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9220 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9221 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9222 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9223 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunctionBody); 9224 } 9225 9226 #undef CHECK_PARSE_BOOL 9227 9228 TEST_F(FormatTest, ParsesConfiguration) { 9229 FormatStyle Style = {}; 9230 Style.Language = FormatStyle::LK_Cpp; 9231 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9232 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9233 ConstructorInitializerIndentWidth, 1234u); 9234 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9235 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9236 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9237 CHECK_PARSE("PenaltyBreakAssignment: 1234", 9238 PenaltyBreakAssignment, 1234u); 9239 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9240 PenaltyBreakBeforeFirstCallParameter, 1234u); 9241 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9242 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9243 PenaltyReturnTypeOnItsOwnLine, 1234u); 9244 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9245 SpacesBeforeTrailingComments, 1234u); 9246 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9247 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9248 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 9249 9250 Style.PointerAlignment = FormatStyle::PAS_Middle; 9251 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9252 FormatStyle::PAS_Left); 9253 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9254 FormatStyle::PAS_Right); 9255 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9256 FormatStyle::PAS_Middle); 9257 // For backward compatibility: 9258 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9259 FormatStyle::PAS_Left); 9260 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9261 FormatStyle::PAS_Right); 9262 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9263 FormatStyle::PAS_Middle); 9264 9265 Style.Standard = FormatStyle::LS_Auto; 9266 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9267 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9268 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9269 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9270 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9271 9272 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9273 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9274 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9275 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9276 FormatStyle::BOS_None); 9277 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9278 FormatStyle::BOS_All); 9279 // For backward compatibility: 9280 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9281 FormatStyle::BOS_None); 9282 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9283 FormatStyle::BOS_All); 9284 9285 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 9286 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 9287 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 9288 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 9289 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 9290 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 9291 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 9292 // For backward compatibility: 9293 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 9294 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 9295 9296 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9297 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9298 FormatStyle::BAS_Align); 9299 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9300 FormatStyle::BAS_DontAlign); 9301 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9302 FormatStyle::BAS_AlwaysBreak); 9303 // For backward compatibility: 9304 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9305 FormatStyle::BAS_DontAlign); 9306 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9307 FormatStyle::BAS_Align); 9308 9309 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9310 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 9311 FormatStyle::ENAS_DontAlign); 9312 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 9313 FormatStyle::ENAS_Left); 9314 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 9315 FormatStyle::ENAS_Right); 9316 // For backward compatibility: 9317 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 9318 FormatStyle::ENAS_Left); 9319 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 9320 FormatStyle::ENAS_Right); 9321 9322 Style.UseTab = FormatStyle::UT_ForIndentation; 9323 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9324 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9325 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9326 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 9327 FormatStyle::UT_ForContinuationAndIndentation); 9328 // For backward compatibility: 9329 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9330 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9331 9332 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9333 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9334 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9335 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9336 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9337 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9338 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9339 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9340 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9341 // For backward compatibility: 9342 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9343 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9344 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9345 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9346 9347 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9348 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9349 FormatStyle::SBPO_Never); 9350 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9351 FormatStyle::SBPO_Always); 9352 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9353 FormatStyle::SBPO_ControlStatements); 9354 // For backward compatibility: 9355 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9356 FormatStyle::SBPO_Never); 9357 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9358 FormatStyle::SBPO_ControlStatements); 9359 9360 Style.ColumnLimit = 123; 9361 FormatStyle BaseStyle = getLLVMStyle(); 9362 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9363 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9364 9365 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9366 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9367 FormatStyle::BS_Attach); 9368 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9369 FormatStyle::BS_Linux); 9370 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 9371 FormatStyle::BS_Mozilla); 9372 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9373 FormatStyle::BS_Stroustrup); 9374 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9375 FormatStyle::BS_Allman); 9376 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9377 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 9378 FormatStyle::BS_WebKit); 9379 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 9380 FormatStyle::BS_Custom); 9381 9382 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9383 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 9384 FormatStyle::RTBS_None); 9385 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 9386 FormatStyle::RTBS_All); 9387 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 9388 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 9389 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 9390 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 9391 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 9392 AlwaysBreakAfterReturnType, 9393 FormatStyle::RTBS_TopLevelDefinitions); 9394 9395 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 9396 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 9397 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 9398 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 9399 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 9400 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 9401 AlwaysBreakAfterDefinitionReturnType, 9402 FormatStyle::DRTBS_TopLevel); 9403 9404 Style.NamespaceIndentation = FormatStyle::NI_All; 9405 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 9406 FormatStyle::NI_None); 9407 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 9408 FormatStyle::NI_Inner); 9409 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 9410 FormatStyle::NI_All); 9411 9412 // FIXME: This is required because parsing a configuration simply overwrites 9413 // the first N elements of the list instead of resetting it. 9414 Style.ForEachMacros.clear(); 9415 std::vector<std::string> BoostForeach; 9416 BoostForeach.push_back("BOOST_FOREACH"); 9417 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 9418 std::vector<std::string> BoostAndQForeach; 9419 BoostAndQForeach.push_back("BOOST_FOREACH"); 9420 BoostAndQForeach.push_back("Q_FOREACH"); 9421 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 9422 BoostAndQForeach); 9423 9424 Style.IncludeCategories.clear(); 9425 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 9426 {".*", 1}}; 9427 CHECK_PARSE("IncludeCategories:\n" 9428 " - Regex: abc/.*\n" 9429 " Priority: 2\n" 9430 " - Regex: .*\n" 9431 " Priority: 1", 9432 IncludeCategories, ExpectedCategories); 9433 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 9434 } 9435 9436 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 9437 FormatStyle Style = {}; 9438 Style.Language = FormatStyle::LK_Cpp; 9439 CHECK_PARSE("Language: Cpp\n" 9440 "IndentWidth: 12", 9441 IndentWidth, 12u); 9442 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 9443 "IndentWidth: 34", 9444 &Style), 9445 ParseError::Unsuitable); 9446 EXPECT_EQ(12u, Style.IndentWidth); 9447 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9448 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9449 9450 Style.Language = FormatStyle::LK_JavaScript; 9451 CHECK_PARSE("Language: JavaScript\n" 9452 "IndentWidth: 12", 9453 IndentWidth, 12u); 9454 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 9455 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 9456 "IndentWidth: 34", 9457 &Style), 9458 ParseError::Unsuitable); 9459 EXPECT_EQ(23u, Style.IndentWidth); 9460 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9461 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9462 9463 CHECK_PARSE("BasedOnStyle: LLVM\n" 9464 "IndentWidth: 67", 9465 IndentWidth, 67u); 9466 9467 CHECK_PARSE("---\n" 9468 "Language: JavaScript\n" 9469 "IndentWidth: 12\n" 9470 "---\n" 9471 "Language: Cpp\n" 9472 "IndentWidth: 34\n" 9473 "...\n", 9474 IndentWidth, 12u); 9475 9476 Style.Language = FormatStyle::LK_Cpp; 9477 CHECK_PARSE("---\n" 9478 "Language: JavaScript\n" 9479 "IndentWidth: 12\n" 9480 "---\n" 9481 "Language: Cpp\n" 9482 "IndentWidth: 34\n" 9483 "...\n", 9484 IndentWidth, 34u); 9485 CHECK_PARSE("---\n" 9486 "IndentWidth: 78\n" 9487 "---\n" 9488 "Language: JavaScript\n" 9489 "IndentWidth: 56\n" 9490 "...\n", 9491 IndentWidth, 78u); 9492 9493 Style.ColumnLimit = 123; 9494 Style.IndentWidth = 234; 9495 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 9496 Style.TabWidth = 345; 9497 EXPECT_FALSE(parseConfiguration("---\n" 9498 "IndentWidth: 456\n" 9499 "BreakBeforeBraces: Allman\n" 9500 "---\n" 9501 "Language: JavaScript\n" 9502 "IndentWidth: 111\n" 9503 "TabWidth: 111\n" 9504 "---\n" 9505 "Language: Cpp\n" 9506 "BreakBeforeBraces: Stroustrup\n" 9507 "TabWidth: 789\n" 9508 "...\n", 9509 &Style)); 9510 EXPECT_EQ(123u, Style.ColumnLimit); 9511 EXPECT_EQ(456u, Style.IndentWidth); 9512 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 9513 EXPECT_EQ(789u, Style.TabWidth); 9514 9515 EXPECT_EQ(parseConfiguration("---\n" 9516 "Language: JavaScript\n" 9517 "IndentWidth: 56\n" 9518 "---\n" 9519 "IndentWidth: 78\n" 9520 "...\n", 9521 &Style), 9522 ParseError::Error); 9523 EXPECT_EQ(parseConfiguration("---\n" 9524 "Language: JavaScript\n" 9525 "IndentWidth: 56\n" 9526 "---\n" 9527 "Language: JavaScript\n" 9528 "IndentWidth: 78\n" 9529 "...\n", 9530 &Style), 9531 ParseError::Error); 9532 9533 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9534 } 9535 9536 #undef CHECK_PARSE 9537 9538 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 9539 FormatStyle Style = {}; 9540 Style.Language = FormatStyle::LK_JavaScript; 9541 Style.BreakBeforeTernaryOperators = true; 9542 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9543 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9544 9545 Style.BreakBeforeTernaryOperators = true; 9546 EXPECT_EQ(0, parseConfiguration("---\n" 9547 "BasedOnStyle: Google\n" 9548 "---\n" 9549 "Language: JavaScript\n" 9550 "IndentWidth: 76\n" 9551 "...\n", 9552 &Style) 9553 .value()); 9554 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9555 EXPECT_EQ(76u, Style.IndentWidth); 9556 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9557 } 9558 9559 TEST_F(FormatTest, ConfigurationRoundTripTest) { 9560 FormatStyle Style = getLLVMStyle(); 9561 std::string YAML = configurationAsText(Style); 9562 FormatStyle ParsedStyle = {}; 9563 ParsedStyle.Language = FormatStyle::LK_Cpp; 9564 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 9565 EXPECT_EQ(Style, ParsedStyle); 9566 } 9567 9568 TEST_F(FormatTest, WorksFor8bitEncodings) { 9569 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 9570 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 9571 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 9572 "\"\xef\xee\xf0\xf3...\"", 9573 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 9574 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 9575 "\xef\xee\xf0\xf3...\"", 9576 getLLVMStyleWithColumns(12))); 9577 } 9578 9579 TEST_F(FormatTest, HandlesUTF8BOM) { 9580 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 9581 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 9582 format("\xef\xbb\xbf#include <iostream>")); 9583 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 9584 format("\xef\xbb\xbf\n#include <iostream>")); 9585 } 9586 9587 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 9588 #if !defined(_MSC_VER) 9589 9590 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 9591 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 9592 getLLVMStyleWithColumns(35)); 9593 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 9594 getLLVMStyleWithColumns(31)); 9595 verifyFormat("// Однажды в студёную зимнюю пору...", 9596 getLLVMStyleWithColumns(36)); 9597 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 9598 verifyFormat("/* Однажды в студёную зимнюю пору... */", 9599 getLLVMStyleWithColumns(39)); 9600 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 9601 getLLVMStyleWithColumns(35)); 9602 } 9603 9604 TEST_F(FormatTest, SplitsUTF8Strings) { 9605 // Non-printable characters' width is currently considered to be the length in 9606 // bytes in UTF8. The characters can be displayed in very different manner 9607 // (zero-width, single width with a substitution glyph, expanded to their code 9608 // (e.g. "<8d>"), so there's no single correct way to handle them. 9609 EXPECT_EQ("\"aaaaÄ\"\n" 9610 "\"\xc2\x8d\";", 9611 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9612 EXPECT_EQ("\"aaaaaaaÄ\"\n" 9613 "\"\xc2\x8d\";", 9614 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9615 EXPECT_EQ("\"Однажды, в \"\n" 9616 "\"студёную \"\n" 9617 "\"зимнюю \"\n" 9618 "\"пору,\"", 9619 format("\"Однажды, в студёную зимнюю пору,\"", 9620 getLLVMStyleWithColumns(13))); 9621 EXPECT_EQ( 9622 "\"一 二 三 \"\n" 9623 "\"四 五六 \"\n" 9624 "\"七 八 九 \"\n" 9625 "\"十\"", 9626 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 9627 EXPECT_EQ("\"一\t二 \"\n" 9628 "\"\t三 \"\n" 9629 "\"四 五\t六 \"\n" 9630 "\"\t七 \"\n" 9631 "\"八九十\tqq\"", 9632 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 9633 getLLVMStyleWithColumns(11))); 9634 9635 // UTF8 character in an escape sequence. 9636 EXPECT_EQ("\"aaaaaa\"\n" 9637 "\"\\\xC2\x8D\"", 9638 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 9639 } 9640 9641 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 9642 EXPECT_EQ("const char *sssss =\n" 9643 " \"一二三四五六七八\\\n" 9644 " 九 十\";", 9645 format("const char *sssss = \"一二三四五六七八\\\n" 9646 " 九 十\";", 9647 getLLVMStyleWithColumns(30))); 9648 } 9649 9650 TEST_F(FormatTest, SplitsUTF8LineComments) { 9651 EXPECT_EQ("// aaaaÄ\xc2\x8d", 9652 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 9653 EXPECT_EQ("// Я из лесу\n" 9654 "// вышел; был\n" 9655 "// сильный\n" 9656 "// мороз.", 9657 format("// Я из лесу вышел; был сильный мороз.", 9658 getLLVMStyleWithColumns(13))); 9659 EXPECT_EQ("// 一二三\n" 9660 "// 四五六七\n" 9661 "// 八 九\n" 9662 "// 十", 9663 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 9664 } 9665 9666 TEST_F(FormatTest, SplitsUTF8BlockComments) { 9667 EXPECT_EQ("/* Гляжу,\n" 9668 " * поднимается\n" 9669 " * медленно в\n" 9670 " * гору\n" 9671 " * Лошадка,\n" 9672 " * везущая\n" 9673 " * хворосту\n" 9674 " * воз. */", 9675 format("/* Гляжу, поднимается медленно в гору\n" 9676 " * Лошадка, везущая хворосту воз. */", 9677 getLLVMStyleWithColumns(13))); 9678 EXPECT_EQ( 9679 "/* 一二三\n" 9680 " * 四五六七\n" 9681 " * 八 九\n" 9682 " * 十 */", 9683 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 9684 EXPECT_EQ("/* \n" 9685 " * \n" 9686 " * - */", 9687 format("/* - */", getLLVMStyleWithColumns(12))); 9688 } 9689 9690 #endif // _MSC_VER 9691 9692 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 9693 FormatStyle Style = getLLVMStyle(); 9694 9695 Style.ConstructorInitializerIndentWidth = 4; 9696 verifyFormat( 9697 "SomeClass::Constructor()\n" 9698 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9699 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9700 Style); 9701 9702 Style.ConstructorInitializerIndentWidth = 2; 9703 verifyFormat( 9704 "SomeClass::Constructor()\n" 9705 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9706 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9707 Style); 9708 9709 Style.ConstructorInitializerIndentWidth = 0; 9710 verifyFormat( 9711 "SomeClass::Constructor()\n" 9712 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9713 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9714 Style); 9715 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9716 verifyFormat( 9717 "SomeLongTemplateVariableName<\n" 9718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 9719 Style); 9720 verifyFormat( 9721 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 9722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9723 Style); 9724 } 9725 9726 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 9727 FormatStyle Style = getLLVMStyle(); 9728 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 9729 Style.ConstructorInitializerIndentWidth = 4; 9730 verifyFormat("SomeClass::Constructor()\n" 9731 " : a(a)\n" 9732 " , b(b)\n" 9733 " , c(c) {}", 9734 Style); 9735 verifyFormat("SomeClass::Constructor()\n" 9736 " : a(a) {}", 9737 Style); 9738 9739 Style.ColumnLimit = 0; 9740 verifyFormat("SomeClass::Constructor()\n" 9741 " : a(a) {}", 9742 Style); 9743 verifyFormat("SomeClass::Constructor() noexcept\n" 9744 " : a(a) {}", 9745 Style); 9746 verifyFormat("SomeClass::Constructor()\n" 9747 " : a(a)\n" 9748 " , b(b)\n" 9749 " , c(c) {}", 9750 Style); 9751 verifyFormat("SomeClass::Constructor()\n" 9752 " : a(a) {\n" 9753 " foo();\n" 9754 " bar();\n" 9755 "}", 9756 Style); 9757 9758 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9759 verifyFormat("SomeClass::Constructor()\n" 9760 " : a(a)\n" 9761 " , b(b)\n" 9762 " , c(c) {\n}", 9763 Style); 9764 verifyFormat("SomeClass::Constructor()\n" 9765 " : a(a) {\n}", 9766 Style); 9767 9768 Style.ColumnLimit = 80; 9769 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 9770 Style.ConstructorInitializerIndentWidth = 2; 9771 verifyFormat("SomeClass::Constructor()\n" 9772 " : a(a)\n" 9773 " , b(b)\n" 9774 " , c(c) {}", 9775 Style); 9776 9777 Style.ConstructorInitializerIndentWidth = 0; 9778 verifyFormat("SomeClass::Constructor()\n" 9779 ": a(a)\n" 9780 ", b(b)\n" 9781 ", c(c) {}", 9782 Style); 9783 9784 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 9785 Style.ConstructorInitializerIndentWidth = 4; 9786 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 9787 verifyFormat( 9788 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 9789 Style); 9790 verifyFormat( 9791 "SomeClass::Constructor()\n" 9792 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 9793 Style); 9794 Style.ConstructorInitializerIndentWidth = 4; 9795 Style.ColumnLimit = 60; 9796 verifyFormat("SomeClass::Constructor()\n" 9797 " : aaaaaaaa(aaaaaaaa)\n" 9798 " , aaaaaaaa(aaaaaaaa)\n" 9799 " , aaaaaaaa(aaaaaaaa) {}", 9800 Style); 9801 } 9802 9803 TEST_F(FormatTest, Destructors) { 9804 verifyFormat("void F(int &i) { i.~int(); }"); 9805 verifyFormat("void F(int &i) { i->~int(); }"); 9806 } 9807 9808 TEST_F(FormatTest, FormatsWithWebKitStyle) { 9809 FormatStyle Style = getWebKitStyle(); 9810 9811 // Don't indent in outer namespaces. 9812 verifyFormat("namespace outer {\n" 9813 "int i;\n" 9814 "namespace inner {\n" 9815 " int i;\n" 9816 "} // namespace inner\n" 9817 "} // namespace outer\n" 9818 "namespace other_outer {\n" 9819 "int i;\n" 9820 "}", 9821 Style); 9822 9823 // Don't indent case labels. 9824 verifyFormat("switch (variable) {\n" 9825 "case 1:\n" 9826 "case 2:\n" 9827 " doSomething();\n" 9828 " break;\n" 9829 "default:\n" 9830 " ++variable;\n" 9831 "}", 9832 Style); 9833 9834 // Wrap before binary operators. 9835 EXPECT_EQ("void f()\n" 9836 "{\n" 9837 " if (aaaaaaaaaaaaaaaa\n" 9838 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 9839 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 9840 " return;\n" 9841 "}", 9842 format("void f() {\n" 9843 "if (aaaaaaaaaaaaaaaa\n" 9844 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 9845 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 9846 "return;\n" 9847 "}", 9848 Style)); 9849 9850 // Allow functions on a single line. 9851 verifyFormat("void f() { return; }", Style); 9852 9853 // Constructor initializers are formatted one per line with the "," on the 9854 // new line. 9855 verifyFormat("Constructor()\n" 9856 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9857 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 9858 " aaaaaaaaaaaaaa)\n" 9859 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 9860 "{\n" 9861 "}", 9862 Style); 9863 verifyFormat("SomeClass::Constructor()\n" 9864 " : a(a)\n" 9865 "{\n" 9866 "}", 9867 Style); 9868 EXPECT_EQ("SomeClass::Constructor()\n" 9869 " : a(a)\n" 9870 "{\n" 9871 "}", 9872 format("SomeClass::Constructor():a(a){}", Style)); 9873 verifyFormat("SomeClass::Constructor()\n" 9874 " : a(a)\n" 9875 " , b(b)\n" 9876 " , c(c)\n" 9877 "{\n" 9878 "}", 9879 Style); 9880 verifyFormat("SomeClass::Constructor()\n" 9881 " : a(a)\n" 9882 "{\n" 9883 " foo();\n" 9884 " bar();\n" 9885 "}", 9886 Style); 9887 9888 // Access specifiers should be aligned left. 9889 verifyFormat("class C {\n" 9890 "public:\n" 9891 " int i;\n" 9892 "};", 9893 Style); 9894 9895 // Do not align comments. 9896 verifyFormat("int a; // Do not\n" 9897 "double b; // align comments.", 9898 Style); 9899 9900 // Do not align operands. 9901 EXPECT_EQ("ASSERT(aaaa\n" 9902 " || bbbb);", 9903 format("ASSERT ( aaaa\n||bbbb);", Style)); 9904 9905 // Accept input's line breaks. 9906 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 9907 " || bbbbbbbbbbbbbbb) {\n" 9908 " i++;\n" 9909 "}", 9910 format("if (aaaaaaaaaaaaaaa\n" 9911 "|| bbbbbbbbbbbbbbb) { i++; }", 9912 Style)); 9913 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 9914 " i++;\n" 9915 "}", 9916 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 9917 9918 // Don't automatically break all macro definitions (llvm.org/PR17842). 9919 verifyFormat("#define aNumber 10", Style); 9920 // However, generally keep the line breaks that the user authored. 9921 EXPECT_EQ("#define aNumber \\\n" 9922 " 10", 9923 format("#define aNumber \\\n" 9924 " 10", 9925 Style)); 9926 9927 // Keep empty and one-element array literals on a single line. 9928 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 9929 " copyItems:YES];", 9930 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 9931 "copyItems:YES];", 9932 Style)); 9933 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 9934 " copyItems:YES];", 9935 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 9936 " copyItems:YES];", 9937 Style)); 9938 // FIXME: This does not seem right, there should be more indentation before 9939 // the array literal's entries. Nested blocks have the same problem. 9940 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 9941 " @\"a\",\n" 9942 " @\"a\"\n" 9943 "]\n" 9944 " copyItems:YES];", 9945 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 9946 " @\"a\",\n" 9947 " @\"a\"\n" 9948 " ]\n" 9949 " copyItems:YES];", 9950 Style)); 9951 EXPECT_EQ( 9952 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 9953 " copyItems:YES];", 9954 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 9955 " copyItems:YES];", 9956 Style)); 9957 9958 verifyFormat("[self.a b:c c:d];", Style); 9959 EXPECT_EQ("[self.a b:c\n" 9960 " c:d];", 9961 format("[self.a b:c\n" 9962 "c:d];", 9963 Style)); 9964 } 9965 9966 TEST_F(FormatTest, FormatsLambdas) { 9967 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 9968 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 9969 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 9970 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 9971 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 9972 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 9973 verifyFormat("int x = f(*+[] {});"); 9974 verifyFormat("void f() {\n" 9975 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 9976 "}\n"); 9977 verifyFormat("void f() {\n" 9978 " other(x.begin(), //\n" 9979 " x.end(), //\n" 9980 " [&](int, int) { return 1; });\n" 9981 "}\n"); 9982 verifyFormat("SomeFunction([]() { // A cool function...\n" 9983 " return 43;\n" 9984 "});"); 9985 EXPECT_EQ("SomeFunction([]() {\n" 9986 "#define A a\n" 9987 " return 43;\n" 9988 "});", 9989 format("SomeFunction([](){\n" 9990 "#define A a\n" 9991 "return 43;\n" 9992 "});")); 9993 verifyFormat("void f() {\n" 9994 " SomeFunction([](decltype(x), A *a) {});\n" 9995 "}"); 9996 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9997 " [](const aaaaaaaaaa &a) { return a; });"); 9998 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 9999 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10000 "});"); 10001 verifyFormat("Constructor()\n" 10002 " : Field([] { // comment\n" 10003 " int i;\n" 10004 " }) {}"); 10005 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10006 " return some_parameter.size();\n" 10007 "};"); 10008 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10009 " [](const string &s) { return s; };"); 10010 verifyFormat("int i = aaaaaa ? 1 //\n" 10011 " : [] {\n" 10012 " return 2; //\n" 10013 " }();"); 10014 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10015 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10016 " return x == 2; // force break\n" 10017 " });"); 10018 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10019 " [=](int iiiiiiiiiiii) {\n" 10020 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 10021 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 10022 " });", 10023 getLLVMStyleWithColumns(60)); 10024 verifyFormat("SomeFunction({[&] {\n" 10025 " // comment\n" 10026 " },\n" 10027 " [&] {\n" 10028 " // comment\n" 10029 " }});"); 10030 verifyFormat("SomeFunction({[&] {\n" 10031 " // comment\n" 10032 "}});"); 10033 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10034 " [&]() { return true; },\n" 10035 " aaaaa aaaaaaaaa);"); 10036 10037 // Lambdas with return types. 10038 verifyFormat("int c = []() -> int { return 2; }();\n"); 10039 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10040 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10041 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10042 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10043 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10044 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10045 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10046 verifyFormat("[a, a]() -> a<1> {};"); 10047 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10048 " int j) -> int {\n" 10049 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10050 "};"); 10051 verifyFormat( 10052 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10053 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10054 " return aaaaaaaaaaaaaaaaa;\n" 10055 " });", 10056 getLLVMStyleWithColumns(70)); 10057 verifyFormat("[]() //\n" 10058 " -> int {\n" 10059 " return 1; //\n" 10060 "};"); 10061 10062 // Multiple lambdas in the same parentheses change indentation rules. 10063 verifyFormat("SomeFunction(\n" 10064 " []() {\n" 10065 " int i = 42;\n" 10066 " return i;\n" 10067 " },\n" 10068 " []() {\n" 10069 " int j = 43;\n" 10070 " return j;\n" 10071 " });"); 10072 10073 // More complex introducers. 10074 verifyFormat("return [i, args...] {};"); 10075 10076 // Not lambdas. 10077 verifyFormat("constexpr char hello[]{\"hello\"};"); 10078 verifyFormat("double &operator[](int i) { return 0; }\n" 10079 "int i;"); 10080 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10081 verifyFormat("int i = a[a][a]->f();"); 10082 verifyFormat("int i = (*b)[a]->f();"); 10083 10084 // Other corner cases. 10085 verifyFormat("void f() {\n" 10086 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10087 " );\n" 10088 "}"); 10089 10090 // Lambdas created through weird macros. 10091 verifyFormat("void f() {\n" 10092 " MACRO((const AA &a) { return 1; });\n" 10093 " MACRO((AA &a) { return 1; });\n" 10094 "}"); 10095 10096 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10097 " doo_dah();\n" 10098 " doo_dah();\n" 10099 " })) {\n" 10100 "}"); 10101 verifyFormat("auto lambda = []() {\n" 10102 " int a = 2\n" 10103 "#if A\n" 10104 " + 2\n" 10105 "#endif\n" 10106 " ;\n" 10107 "};"); 10108 10109 // Lambdas with complex multiline introducers. 10110 verifyFormat( 10111 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10112 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 10113 " -> ::std::unordered_set<\n" 10114 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 10115 " //\n" 10116 " });"); 10117 } 10118 10119 TEST_F(FormatTest, FormatsBlocks) { 10120 FormatStyle ShortBlocks = getLLVMStyle(); 10121 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10122 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10123 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10124 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10125 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10126 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10127 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10128 10129 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10130 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10131 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10132 10133 verifyFormat("[operation setCompletionBlock:^{\n" 10134 " [self onOperationDone];\n" 10135 "}];"); 10136 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10137 " [self onOperationDone];\n" 10138 "}]};"); 10139 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10140 " f();\n" 10141 "}];"); 10142 verifyFormat("int a = [operation block:^int(int *i) {\n" 10143 " return 1;\n" 10144 "}];"); 10145 verifyFormat("[myObject doSomethingWith:arg1\n" 10146 " aaa:^int(int *a) {\n" 10147 " return 1;\n" 10148 " }\n" 10149 " bbb:f(a * bbbbbbbb)];"); 10150 10151 verifyFormat("[operation setCompletionBlock:^{\n" 10152 " [self.delegate newDataAvailable];\n" 10153 "}];", 10154 getLLVMStyleWithColumns(60)); 10155 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10156 " NSString *path = [self sessionFilePath];\n" 10157 " if (path) {\n" 10158 " // ...\n" 10159 " }\n" 10160 "});"); 10161 verifyFormat("[[SessionService sharedService]\n" 10162 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10163 " if (window) {\n" 10164 " [self windowDidLoad:window];\n" 10165 " } else {\n" 10166 " [self errorLoadingWindow];\n" 10167 " }\n" 10168 " }];"); 10169 verifyFormat("void (^largeBlock)(void) = ^{\n" 10170 " // ...\n" 10171 "};\n", 10172 getLLVMStyleWithColumns(40)); 10173 verifyFormat("[[SessionService sharedService]\n" 10174 " loadWindowWithCompletionBlock: //\n" 10175 " ^(SessionWindow *window) {\n" 10176 " if (window) {\n" 10177 " [self windowDidLoad:window];\n" 10178 " } else {\n" 10179 " [self errorLoadingWindow];\n" 10180 " }\n" 10181 " }];", 10182 getLLVMStyleWithColumns(60)); 10183 verifyFormat("[myObject doSomethingWith:arg1\n" 10184 " firstBlock:^(Foo *a) {\n" 10185 " // ...\n" 10186 " int i;\n" 10187 " }\n" 10188 " secondBlock:^(Bar *b) {\n" 10189 " // ...\n" 10190 " int i;\n" 10191 " }\n" 10192 " thirdBlock:^Foo(Bar *b) {\n" 10193 " // ...\n" 10194 " int i;\n" 10195 " }];"); 10196 verifyFormat("[myObject doSomethingWith:arg1\n" 10197 " firstBlock:-1\n" 10198 " secondBlock:^(Bar *b) {\n" 10199 " // ...\n" 10200 " int i;\n" 10201 " }];"); 10202 10203 verifyFormat("f(^{\n" 10204 " @autoreleasepool {\n" 10205 " if (a) {\n" 10206 " g();\n" 10207 " }\n" 10208 " }\n" 10209 "});"); 10210 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10211 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 10212 "};"); 10213 10214 FormatStyle FourIndent = getLLVMStyle(); 10215 FourIndent.ObjCBlockIndentWidth = 4; 10216 verifyFormat("[operation setCompletionBlock:^{\n" 10217 " [self onOperationDone];\n" 10218 "}];", 10219 FourIndent); 10220 } 10221 10222 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10223 FormatStyle ZeroColumn = getLLVMStyle(); 10224 ZeroColumn.ColumnLimit = 0; 10225 10226 verifyFormat("[[SessionService sharedService] " 10227 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10228 " if (window) {\n" 10229 " [self windowDidLoad:window];\n" 10230 " } else {\n" 10231 " [self errorLoadingWindow];\n" 10232 " }\n" 10233 "}];", 10234 ZeroColumn); 10235 EXPECT_EQ("[[SessionService sharedService]\n" 10236 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10237 " if (window) {\n" 10238 " [self windowDidLoad:window];\n" 10239 " } else {\n" 10240 " [self errorLoadingWindow];\n" 10241 " }\n" 10242 " }];", 10243 format("[[SessionService sharedService]\n" 10244 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10245 " if (window) {\n" 10246 " [self windowDidLoad:window];\n" 10247 " } else {\n" 10248 " [self errorLoadingWindow];\n" 10249 " }\n" 10250 "}];", 10251 ZeroColumn)); 10252 verifyFormat("[myObject doSomethingWith:arg1\n" 10253 " firstBlock:^(Foo *a) {\n" 10254 " // ...\n" 10255 " int i;\n" 10256 " }\n" 10257 " secondBlock:^(Bar *b) {\n" 10258 " // ...\n" 10259 " int i;\n" 10260 " }\n" 10261 " thirdBlock:^Foo(Bar *b) {\n" 10262 " // ...\n" 10263 " int i;\n" 10264 " }];", 10265 ZeroColumn); 10266 verifyFormat("f(^{\n" 10267 " @autoreleasepool {\n" 10268 " if (a) {\n" 10269 " g();\n" 10270 " }\n" 10271 " }\n" 10272 "});", 10273 ZeroColumn); 10274 verifyFormat("void (^largeBlock)(void) = ^{\n" 10275 " // ...\n" 10276 "};", 10277 ZeroColumn); 10278 10279 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10280 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10281 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10282 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10283 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10284 " int i;\n" 10285 "};", 10286 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10287 } 10288 10289 TEST_F(FormatTest, SupportsCRLF) { 10290 EXPECT_EQ("int a;\r\n" 10291 "int b;\r\n" 10292 "int c;\r\n", 10293 format("int a;\r\n" 10294 " int b;\r\n" 10295 " int c;\r\n", 10296 getLLVMStyle())); 10297 EXPECT_EQ("int a;\r\n" 10298 "int b;\r\n" 10299 "int c;\r\n", 10300 format("int a;\r\n" 10301 " int b;\n" 10302 " int c;\r\n", 10303 getLLVMStyle())); 10304 EXPECT_EQ("int a;\n" 10305 "int b;\n" 10306 "int c;\n", 10307 format("int a;\r\n" 10308 " int b;\n" 10309 " int c;\n", 10310 getLLVMStyle())); 10311 EXPECT_EQ("\"aaaaaaa \"\r\n" 10312 "\"bbbbbbb\";\r\n", 10313 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10314 EXPECT_EQ("#define A \\\r\n" 10315 " b; \\\r\n" 10316 " c; \\\r\n" 10317 " d;\r\n", 10318 format("#define A \\\r\n" 10319 " b; \\\r\n" 10320 " c; d; \r\n", 10321 getGoogleStyle())); 10322 10323 EXPECT_EQ("/*\r\n" 10324 "multi line block comments\r\n" 10325 "should not introduce\r\n" 10326 "an extra carriage return\r\n" 10327 "*/\r\n", 10328 format("/*\r\n" 10329 "multi line block comments\r\n" 10330 "should not introduce\r\n" 10331 "an extra carriage return\r\n" 10332 "*/\r\n")); 10333 } 10334 10335 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10336 verifyFormat("MY_CLASS(C) {\n" 10337 " int i;\n" 10338 " int j;\n" 10339 "};"); 10340 } 10341 10342 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10343 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10344 TwoIndent.ContinuationIndentWidth = 2; 10345 10346 EXPECT_EQ("int i =\n" 10347 " longFunction(\n" 10348 " arg);", 10349 format("int i = longFunction(arg);", TwoIndent)); 10350 10351 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10352 SixIndent.ContinuationIndentWidth = 6; 10353 10354 EXPECT_EQ("int i =\n" 10355 " longFunction(\n" 10356 " arg);", 10357 format("int i = longFunction(arg);", SixIndent)); 10358 } 10359 10360 TEST_F(FormatTest, SpacesInAngles) { 10361 FormatStyle Spaces = getLLVMStyle(); 10362 Spaces.SpacesInAngles = true; 10363 10364 verifyFormat("static_cast< int >(arg);", Spaces); 10365 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10366 verifyFormat("f< int, float >();", Spaces); 10367 verifyFormat("template <> g() {}", Spaces); 10368 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10369 verifyFormat("std::function< void(int, int) > fct;", Spaces); 10370 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 10371 Spaces); 10372 10373 Spaces.Standard = FormatStyle::LS_Cpp03; 10374 Spaces.SpacesInAngles = true; 10375 verifyFormat("A< A< int > >();", Spaces); 10376 10377 Spaces.SpacesInAngles = false; 10378 verifyFormat("A<A<int> >();", Spaces); 10379 10380 Spaces.Standard = FormatStyle::LS_Cpp11; 10381 Spaces.SpacesInAngles = true; 10382 verifyFormat("A< A< int > >();", Spaces); 10383 10384 Spaces.SpacesInAngles = false; 10385 verifyFormat("A<A<int>>();", Spaces); 10386 } 10387 10388 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 10389 FormatStyle Style = getLLVMStyle(); 10390 Style.SpaceAfterTemplateKeyword = false; 10391 verifyFormat("template<int> void foo();", Style); 10392 } 10393 10394 TEST_F(FormatTest, TripleAngleBrackets) { 10395 verifyFormat("f<<<1, 1>>>();"); 10396 verifyFormat("f<<<1, 1, 1, s>>>();"); 10397 verifyFormat("f<<<a, b, c, d>>>();"); 10398 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10399 verifyFormat("f<param><<<1, 1>>>();"); 10400 verifyFormat("f<1><<<1, 1>>>();"); 10401 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10402 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10403 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10404 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 10405 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 10406 } 10407 10408 TEST_F(FormatTest, MergeLessLessAtEnd) { 10409 verifyFormat("<<"); 10410 EXPECT_EQ("< < <", format("\\\n<<<")); 10411 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10412 "aaallvm::outs() <<"); 10413 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10414 "aaaallvm::outs()\n <<"); 10415 } 10416 10417 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10418 std::string code = "#if A\n" 10419 "#if B\n" 10420 "a.\n" 10421 "#endif\n" 10422 " a = 1;\n" 10423 "#else\n" 10424 "#endif\n" 10425 "#if C\n" 10426 "#else\n" 10427 "#endif\n"; 10428 EXPECT_EQ(code, format(code)); 10429 } 10430 10431 TEST_F(FormatTest, HandleConflictMarkers) { 10432 // Git/SVN conflict markers. 10433 EXPECT_EQ("int a;\n" 10434 "void f() {\n" 10435 " callme(some(parameter1,\n" 10436 "<<<<<<< text by the vcs\n" 10437 " parameter2),\n" 10438 "||||||| text by the vcs\n" 10439 " parameter2),\n" 10440 " parameter3,\n" 10441 "======= text by the vcs\n" 10442 " parameter2, parameter3),\n" 10443 ">>>>>>> text by the vcs\n" 10444 " otherparameter);\n", 10445 format("int a;\n" 10446 "void f() {\n" 10447 " callme(some(parameter1,\n" 10448 "<<<<<<< text by the vcs\n" 10449 " parameter2),\n" 10450 "||||||| text by the vcs\n" 10451 " parameter2),\n" 10452 " parameter3,\n" 10453 "======= text by the vcs\n" 10454 " parameter2,\n" 10455 " parameter3),\n" 10456 ">>>>>>> text by the vcs\n" 10457 " otherparameter);\n")); 10458 10459 // Perforce markers. 10460 EXPECT_EQ("void f() {\n" 10461 " function(\n" 10462 ">>>> text by the vcs\n" 10463 " parameter,\n" 10464 "==== text by the vcs\n" 10465 " parameter,\n" 10466 "==== text by the vcs\n" 10467 " parameter,\n" 10468 "<<<< text by the vcs\n" 10469 " parameter);\n", 10470 format("void f() {\n" 10471 " function(\n" 10472 ">>>> text by the vcs\n" 10473 " parameter,\n" 10474 "==== text by the vcs\n" 10475 " parameter,\n" 10476 "==== text by the vcs\n" 10477 " parameter,\n" 10478 "<<<< text by the vcs\n" 10479 " parameter);\n")); 10480 10481 EXPECT_EQ("<<<<<<<\n" 10482 "|||||||\n" 10483 "=======\n" 10484 ">>>>>>>", 10485 format("<<<<<<<\n" 10486 "|||||||\n" 10487 "=======\n" 10488 ">>>>>>>")); 10489 10490 EXPECT_EQ("<<<<<<<\n" 10491 "|||||||\n" 10492 "int i;\n" 10493 "=======\n" 10494 ">>>>>>>", 10495 format("<<<<<<<\n" 10496 "|||||||\n" 10497 "int i;\n" 10498 "=======\n" 10499 ">>>>>>>")); 10500 10501 // FIXME: Handle parsing of macros around conflict markers correctly: 10502 EXPECT_EQ("#define Macro \\\n" 10503 "<<<<<<<\n" 10504 "Something \\\n" 10505 "|||||||\n" 10506 "Else \\\n" 10507 "=======\n" 10508 "Other \\\n" 10509 ">>>>>>>\n" 10510 " End int i;\n", 10511 format("#define Macro \\\n" 10512 "<<<<<<<\n" 10513 " Something \\\n" 10514 "|||||||\n" 10515 " Else \\\n" 10516 "=======\n" 10517 " Other \\\n" 10518 ">>>>>>>\n" 10519 " End\n" 10520 "int i;\n")); 10521 } 10522 10523 TEST_F(FormatTest, DisableRegions) { 10524 EXPECT_EQ("int i;\n" 10525 "// clang-format off\n" 10526 " int j;\n" 10527 "// clang-format on\n" 10528 "int k;", 10529 format(" int i;\n" 10530 " // clang-format off\n" 10531 " int j;\n" 10532 " // clang-format on\n" 10533 " int k;")); 10534 EXPECT_EQ("int i;\n" 10535 "/* clang-format off */\n" 10536 " int j;\n" 10537 "/* clang-format on */\n" 10538 "int k;", 10539 format(" int i;\n" 10540 " /* clang-format off */\n" 10541 " int j;\n" 10542 " /* clang-format on */\n" 10543 " int k;")); 10544 10545 // Don't reflow comments within disabled regions. 10546 EXPECT_EQ( 10547 "// clang-format off\n" 10548 "// long long long long long long line\n" 10549 "/* clang-format on */\n" 10550 "/* long long long\n" 10551 " * long long long\n" 10552 " * line */\n" 10553 "int i;\n" 10554 "/* clang-format off */\n" 10555 "/* long long long long long long line */\n", 10556 format("// clang-format off\n" 10557 "// long long long long long long line\n" 10558 "/* clang-format on */\n" 10559 "/* long long long long long long line */\n" 10560 "int i;\n" 10561 "/* clang-format off */\n" 10562 "/* long long long long long long line */\n", 10563 getLLVMStyleWithColumns(20))); 10564 } 10565 10566 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 10567 format("? ) ="); 10568 verifyNoCrash("#define a\\\n /**/}"); 10569 } 10570 10571 TEST_F(FormatTest, FormatsTableGenCode) { 10572 FormatStyle Style = getLLVMStyle(); 10573 Style.Language = FormatStyle::LK_TableGen; 10574 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 10575 } 10576 10577 TEST_F(FormatTest, ArrayOfTemplates) { 10578 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 10579 format("auto a = new unique_ptr<int > [ 10];")); 10580 10581 FormatStyle Spaces = getLLVMStyle(); 10582 Spaces.SpacesInSquareBrackets = true; 10583 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 10584 format("auto a = new unique_ptr<int > [10];", Spaces)); 10585 } 10586 10587 TEST_F(FormatTest, ArrayAsTemplateType) { 10588 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 10589 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 10590 10591 FormatStyle Spaces = getLLVMStyle(); 10592 Spaces.SpacesInSquareBrackets = true; 10593 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 10594 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 10595 } 10596 10597 TEST_F(FormatTest, NoSpaceAfterSuper) { 10598 verifyFormat("__super::FooBar();"); 10599 } 10600 10601 TEST(FormatStyle, GetStyleOfFile) { 10602 vfs::InMemoryFileSystem FS; 10603 // Test 1: format file in the same directory. 10604 ASSERT_TRUE( 10605 FS.addFile("/a/.clang-format", 0, 10606 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 10607 ASSERT_TRUE( 10608 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10609 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 10610 ASSERT_TRUE((bool)Style1); 10611 ASSERT_EQ(*Style1, getLLVMStyle()); 10612 10613 // Test 2.1: fallback to default. 10614 ASSERT_TRUE( 10615 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10616 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 10617 ASSERT_TRUE((bool)Style2); 10618 ASSERT_EQ(*Style2, getMozillaStyle()); 10619 10620 // Test 2.2: no format on 'none' fallback style. 10621 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 10622 ASSERT_TRUE((bool)Style2); 10623 ASSERT_EQ(*Style2, getNoStyle()); 10624 10625 // Test 2.3: format if config is found with no based style while fallback is 10626 // 'none'. 10627 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 10628 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 10629 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 10630 ASSERT_TRUE((bool)Style2); 10631 ASSERT_EQ(*Style2, getLLVMStyle()); 10632 10633 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 10634 Style2 = getStyle("{}", "a.h", "none", "", &FS); 10635 ASSERT_TRUE((bool)Style2); 10636 ASSERT_EQ(*Style2, getLLVMStyle()); 10637 10638 // Test 3: format file in parent directory. 10639 ASSERT_TRUE( 10640 FS.addFile("/c/.clang-format", 0, 10641 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 10642 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 10643 llvm::MemoryBuffer::getMemBuffer("int i;"))); 10644 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 10645 ASSERT_TRUE((bool)Style3); 10646 ASSERT_EQ(*Style3, getGoogleStyle()); 10647 10648 // Test 4: error on invalid fallback style 10649 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 10650 ASSERT_FALSE((bool)Style4); 10651 llvm::consumeError(Style4.takeError()); 10652 10653 // Test 5: error on invalid yaml on command line 10654 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 10655 ASSERT_FALSE((bool)Style5); 10656 llvm::consumeError(Style5.takeError()); 10657 10658 // Test 6: error on invalid style 10659 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 10660 ASSERT_FALSE((bool)Style6); 10661 llvm::consumeError(Style6.takeError()); 10662 10663 // Test 7: found config file, error on parsing it 10664 ASSERT_TRUE( 10665 FS.addFile("/d/.clang-format", 0, 10666 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 10667 "InvalidKey: InvalidValue"))); 10668 ASSERT_TRUE( 10669 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 10670 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 10671 ASSERT_FALSE((bool)Style7); 10672 llvm::consumeError(Style7.takeError()); 10673 } 10674 10675 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 10676 // Column limit is 20. 10677 std::string Code = "Type *a =\n" 10678 " new Type();\n" 10679 "g(iiiii, 0, jjjjj,\n" 10680 " 0, kkkkk, 0, mm);\n" 10681 "int bad = format ;"; 10682 std::string Expected = "auto a = new Type();\n" 10683 "g(iiiii, nullptr,\n" 10684 " jjjjj, nullptr,\n" 10685 " kkkkk, nullptr,\n" 10686 " mm);\n" 10687 "int bad = format ;"; 10688 FileID ID = Context.createInMemoryFile("format.cpp", Code); 10689 tooling::Replacements Replaces = toReplacements( 10690 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 10691 "auto "), 10692 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 10693 "nullptr"), 10694 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 10695 "nullptr"), 10696 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 10697 "nullptr")}); 10698 10699 format::FormatStyle Style = format::getLLVMStyle(); 10700 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 10701 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 10702 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 10703 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 10704 auto Result = applyAllReplacements(Code, *FormattedReplaces); 10705 EXPECT_TRUE(static_cast<bool>(Result)); 10706 EXPECT_EQ(Expected, *Result); 10707 } 10708 10709 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 10710 std::string Code = "#include \"a.h\"\n" 10711 "#include \"c.h\"\n" 10712 "\n" 10713 "int main() {\n" 10714 " return 0;\n" 10715 "}"; 10716 std::string Expected = "#include \"a.h\"\n" 10717 "#include \"b.h\"\n" 10718 "#include \"c.h\"\n" 10719 "\n" 10720 "int main() {\n" 10721 " return 0;\n" 10722 "}"; 10723 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 10724 tooling::Replacements Replaces = toReplacements( 10725 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 10726 "#include \"b.h\"\n")}); 10727 10728 format::FormatStyle Style = format::getLLVMStyle(); 10729 Style.SortIncludes = true; 10730 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 10731 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 10732 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 10733 auto Result = applyAllReplacements(Code, *FormattedReplaces); 10734 EXPECT_TRUE(static_cast<bool>(Result)); 10735 EXPECT_EQ(Expected, *Result); 10736 } 10737 10738 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 10739 format::FormatStyle Style = format::getLLVMStyle(); 10740 Style.Standard = FormatStyle::LS_Cpp03; 10741 // cpp03 recognize this string as identifier u8 and literal character 'a' 10742 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 10743 } 10744 10745 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 10746 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 10747 // all modes, including C++11, C++14 and C++17 10748 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 10749 } 10750 10751 } // end namespace 10752 } // end namespace format 10753 } // end namespace clang 10754