1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "clang/Frontend/TextDiagnosticPrinter.h" 15 #include "llvm/Support/Debug.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include "gtest/gtest.h" 18 19 #define DEBUG_TYPE "format-test" 20 21 using clang::tooling::ReplacementTest; 22 using clang::tooling::toReplacements; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { 33 SC_ExpectComplete, 34 SC_ExpectIncomplete, 35 SC_DoNotCheck 36 }; 37 38 std::string format(llvm::StringRef Code, 39 const FormatStyle &Style = getLLVMStyle(), 40 StatusCheck CheckComplete = SC_ExpectComplete) { 41 LLVM_DEBUG(llvm::errs() << "---\n"); 42 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 43 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 44 FormattingAttemptStatus Status; 45 tooling::Replacements Replaces = 46 reformat(Style, Code, Ranges, "<stdin>", &Status); 47 if (CheckComplete != SC_DoNotCheck) { 48 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 49 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 50 << Code << "\n\n"; 51 } 52 ReplacementCount = Replaces.size(); 53 auto Result = applyAllReplacements(Code, Replaces); 54 EXPECT_TRUE(static_cast<bool>(Result)); 55 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 56 return *Result; 57 } 58 59 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 60 Style.ColumnLimit = ColumnLimit; 61 return Style; 62 } 63 64 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 66 } 67 68 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 69 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 70 } 71 72 void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, 73 const FormatStyle &Style = getLLVMStyle()) { 74 EXPECT_EQ(Expected.str(), format(Expected, Style)) 75 << "Expected code is not stable"; 76 EXPECT_EQ(Expected.str(), format(Code, Style)); 77 if (Style.Language == FormatStyle::LK_Cpp) { 78 // Objective-C++ is a superset of C++, so everything checked for C++ 79 // needs to be checked for Objective-C++ as well. 80 FormatStyle ObjCStyle = Style; 81 ObjCStyle.Language = FormatStyle::LK_ObjC; 82 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 83 } 84 } 85 86 void verifyFormat(llvm::StringRef Code, 87 const FormatStyle &Style = getLLVMStyle()) { 88 verifyFormat(Code, test::messUp(Code), Style); 89 } 90 91 void verifyIncompleteFormat(llvm::StringRef Code, 92 const FormatStyle &Style = getLLVMStyle()) { 93 EXPECT_EQ(Code.str(), 94 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 95 } 96 97 void verifyGoogleFormat(llvm::StringRef Code) { 98 verifyFormat(Code, getGoogleStyle()); 99 } 100 101 void verifyIndependentOfContext(llvm::StringRef text) { 102 verifyFormat(text); 103 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 104 } 105 106 /// \brief Verify that clang-format does not crash on the given input. 107 void verifyNoCrash(llvm::StringRef Code, 108 const FormatStyle &Style = getLLVMStyle()) { 109 format(Code, Style, SC_DoNotCheck); 110 } 111 112 int ReplacementCount; 113 }; 114 115 TEST_F(FormatTest, MessUp) { 116 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 117 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 118 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 119 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 120 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 121 } 122 123 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 124 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 125 } 126 127 TEST_F(FormatTest, LLVMStyleOverride) { 128 EXPECT_EQ(FormatStyle::LK_Proto, 129 getLLVMStyle(FormatStyle::LK_Proto).Language); 130 } 131 132 //===----------------------------------------------------------------------===// 133 // Basic function tests. 134 //===----------------------------------------------------------------------===// 135 136 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 137 EXPECT_EQ(";", format(";")); 138 } 139 140 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 141 EXPECT_EQ("int i;", format(" int i;")); 142 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 143 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 144 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 145 } 146 147 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 148 EXPECT_EQ("int i;", format("int\ni;")); 149 } 150 151 TEST_F(FormatTest, FormatsNestedBlockStatements) { 152 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 153 } 154 155 TEST_F(FormatTest, FormatsNestedCall) { 156 verifyFormat("Method(f1, f2(f3));"); 157 verifyFormat("Method(f1(f2, f3()));"); 158 verifyFormat("Method(f1(f2, (f3())));"); 159 } 160 161 TEST_F(FormatTest, NestedNameSpecifiers) { 162 verifyFormat("vector<::Type> v;"); 163 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 164 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 165 verifyFormat("bool a = 2 < ::SomeFunction();"); 166 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 167 verifyFormat("some::string getName();"); 168 } 169 170 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 171 EXPECT_EQ("if (a) {\n" 172 " f();\n" 173 "}", 174 format("if(a){f();}")); 175 EXPECT_EQ(4, ReplacementCount); 176 EXPECT_EQ("if (a) {\n" 177 " f();\n" 178 "}", 179 format("if (a) {\n" 180 " f();\n" 181 "}")); 182 EXPECT_EQ(0, ReplacementCount); 183 EXPECT_EQ("/*\r\n" 184 "\r\n" 185 "*/\r\n", 186 format("/*\r\n" 187 "\r\n" 188 "*/\r\n")); 189 EXPECT_EQ(0, ReplacementCount); 190 } 191 192 TEST_F(FormatTest, RemovesEmptyLines) { 193 EXPECT_EQ("class C {\n" 194 " int i;\n" 195 "};", 196 format("class C {\n" 197 " int i;\n" 198 "\n" 199 "};")); 200 201 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 202 EXPECT_EQ("namespace N {\n" 203 "\n" 204 "int i;\n" 205 "}", 206 format("namespace N {\n" 207 "\n" 208 "int i;\n" 209 "}", 210 getGoogleStyle())); 211 EXPECT_EQ("/* something */ namespace N {\n" 212 "\n" 213 "int i;\n" 214 "}", 215 format("/* something */ namespace N {\n" 216 "\n" 217 "int i;\n" 218 "}", 219 getGoogleStyle())); 220 EXPECT_EQ("inline namespace N {\n" 221 "\n" 222 "int i;\n" 223 "}", 224 format("inline namespace N {\n" 225 "\n" 226 "int i;\n" 227 "}", 228 getGoogleStyle())); 229 EXPECT_EQ("/* something */ inline namespace N {\n" 230 "\n" 231 "int i;\n" 232 "}", 233 format("/* something */ inline namespace N {\n" 234 "\n" 235 "int i;\n" 236 "}", 237 getGoogleStyle())); 238 EXPECT_EQ("export namespace N {\n" 239 "\n" 240 "int i;\n" 241 "}", 242 format("export namespace N {\n" 243 "\n" 244 "int i;\n" 245 "}", 246 getGoogleStyle())); 247 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 248 "\n" 249 "int i;\n" 250 "}", 251 format("extern /**/ \"C\" /**/ {\n" 252 "\n" 253 "int i;\n" 254 "}", 255 getGoogleStyle())); 256 257 // ...but do keep inlining and removing empty lines for non-block extern "C" 258 // functions. 259 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 260 EXPECT_EQ("extern \"C\" int f() {\n" 261 " int i = 42;\n" 262 " return i;\n" 263 "}", 264 format("extern \"C\" int f() {\n" 265 "\n" 266 " int i = 42;\n" 267 " return i;\n" 268 "}", 269 getGoogleStyle())); 270 271 // Remove empty lines at the beginning and end of blocks. 272 EXPECT_EQ("void f() {\n" 273 "\n" 274 " if (a) {\n" 275 "\n" 276 " f();\n" 277 " }\n" 278 "}", 279 format("void f() {\n" 280 "\n" 281 " if (a) {\n" 282 "\n" 283 " f();\n" 284 "\n" 285 " }\n" 286 "\n" 287 "}", 288 getLLVMStyle())); 289 EXPECT_EQ("void f() {\n" 290 " if (a) {\n" 291 " f();\n" 292 " }\n" 293 "}", 294 format("void f() {\n" 295 "\n" 296 " if (a) {\n" 297 "\n" 298 " f();\n" 299 "\n" 300 " }\n" 301 "\n" 302 "}", 303 getGoogleStyle())); 304 305 // Don't remove empty lines in more complex control statements. 306 EXPECT_EQ("void f() {\n" 307 " if (a) {\n" 308 " f();\n" 309 "\n" 310 " } else if (b) {\n" 311 " f();\n" 312 " }\n" 313 "}", 314 format("void f() {\n" 315 " if (a) {\n" 316 " f();\n" 317 "\n" 318 " } else if (b) {\n" 319 " f();\n" 320 "\n" 321 " }\n" 322 "\n" 323 "}")); 324 325 // Don't remove empty lines before namespace endings. 326 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 327 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 328 EXPECT_EQ("namespace {\n" 329 "int i;\n" 330 "\n" 331 "}", 332 format("namespace {\n" 333 "int i;\n" 334 "\n" 335 "}", LLVMWithNoNamespaceFix)); 336 EXPECT_EQ("namespace {\n" 337 "int i;\n" 338 "}", 339 format("namespace {\n" 340 "int i;\n" 341 "}", LLVMWithNoNamespaceFix)); 342 EXPECT_EQ("namespace {\n" 343 "int i;\n" 344 "\n" 345 "};", 346 format("namespace {\n" 347 "int i;\n" 348 "\n" 349 "};", LLVMWithNoNamespaceFix)); 350 EXPECT_EQ("namespace {\n" 351 "int i;\n" 352 "};", 353 format("namespace {\n" 354 "int i;\n" 355 "};", LLVMWithNoNamespaceFix)); 356 EXPECT_EQ("namespace {\n" 357 "int i;\n" 358 "\n" 359 "}", 360 format("namespace {\n" 361 "int i;\n" 362 "\n" 363 "}")); 364 EXPECT_EQ("namespace {\n" 365 "int i;\n" 366 "\n" 367 "} // namespace", 368 format("namespace {\n" 369 "int i;\n" 370 "\n" 371 "} // namespace")); 372 373 FormatStyle Style = getLLVMStyle(); 374 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 375 Style.MaxEmptyLinesToKeep = 2; 376 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 377 Style.BraceWrapping.AfterClass = true; 378 Style.BraceWrapping.AfterFunction = true; 379 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 380 381 EXPECT_EQ("class Foo\n" 382 "{\n" 383 " Foo() {}\n" 384 "\n" 385 " void funk() {}\n" 386 "};", 387 format("class Foo\n" 388 "{\n" 389 " Foo()\n" 390 " {\n" 391 " }\n" 392 "\n" 393 " void funk() {}\n" 394 "};", 395 Style)); 396 } 397 398 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 399 verifyFormat("x = (a) and (b);"); 400 verifyFormat("x = (a) or (b);"); 401 verifyFormat("x = (a) bitand (b);"); 402 verifyFormat("x = (a) bitor (b);"); 403 verifyFormat("x = (a) not_eq (b);"); 404 verifyFormat("x = (a) and_eq (b);"); 405 verifyFormat("x = (a) or_eq (b);"); 406 verifyFormat("x = (a) xor (b);"); 407 } 408 409 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 410 verifyFormat("x = compl(a);"); 411 verifyFormat("x = not(a);"); 412 verifyFormat("x = bitand(a);"); 413 // Unary operator must not be merged with the next identifier 414 verifyFormat("x = compl a;"); 415 verifyFormat("x = not a;"); 416 verifyFormat("x = bitand a;"); 417 } 418 419 //===----------------------------------------------------------------------===// 420 // Tests for control statements. 421 //===----------------------------------------------------------------------===// 422 423 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 424 verifyFormat("if (true)\n f();\ng();"); 425 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 426 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 427 verifyFormat("if constexpr (true)\n" 428 " f();\ng();"); 429 verifyFormat("if constexpr (a)\n" 430 " if constexpr (b)\n" 431 " if constexpr (c)\n" 432 " g();\n" 433 "h();"); 434 verifyFormat("if constexpr (a)\n" 435 " if constexpr (b) {\n" 436 " f();\n" 437 " }\n" 438 "g();"); 439 440 FormatStyle AllowsMergedIf = getLLVMStyle(); 441 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 442 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 443 FormatStyle::SIS_WithoutElse; 444 verifyFormat("if (a)\n" 445 " // comment\n" 446 " f();", 447 AllowsMergedIf); 448 verifyFormat("{\n" 449 " if (a)\n" 450 " label:\n" 451 " f();\n" 452 "}", 453 AllowsMergedIf); 454 verifyFormat("#define A \\\n" 455 " if (a) \\\n" 456 " label: \\\n" 457 " f()", 458 AllowsMergedIf); 459 verifyFormat("if (a)\n" 460 " ;", 461 AllowsMergedIf); 462 verifyFormat("if (a)\n" 463 " if (b) return;", 464 AllowsMergedIf); 465 466 verifyFormat("if (a) // Can't merge this\n" 467 " f();\n", 468 AllowsMergedIf); 469 verifyFormat("if (a) /* still don't merge */\n" 470 " f();", 471 AllowsMergedIf); 472 verifyFormat("if (a) { // Never merge this\n" 473 " f();\n" 474 "}", 475 AllowsMergedIf); 476 verifyFormat("if (a) { /* Never merge this */\n" 477 " f();\n" 478 "}", 479 AllowsMergedIf); 480 481 AllowsMergedIf.ColumnLimit = 14; 482 verifyFormat("if (a) return;", AllowsMergedIf); 483 verifyFormat("if (aaaaaaaaa)\n" 484 " return;", 485 AllowsMergedIf); 486 487 AllowsMergedIf.ColumnLimit = 13; 488 verifyFormat("if (a)\n return;", AllowsMergedIf); 489 } 490 491 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 492 FormatStyle AllowsMergedIf = getLLVMStyle(); 493 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 494 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 495 FormatStyle::SIS_WithoutElse; 496 verifyFormat("if (a)\n" 497 " f();\n" 498 "else {\n" 499 " g();\n" 500 "}", 501 AllowsMergedIf); 502 verifyFormat("if (a)\n" 503 " f();\n" 504 "else\n" 505 " g();\n", 506 AllowsMergedIf); 507 508 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always; 509 510 verifyFormat("if (a) f();\n" 511 "else {\n" 512 " g();\n" 513 "}", 514 AllowsMergedIf); 515 verifyFormat("if (a) f();\n" 516 "else {\n" 517 " if (a) f();\n" 518 " else {\n" 519 " g();\n" 520 " }\n" 521 " g();\n" 522 "}", 523 AllowsMergedIf); 524 } 525 526 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 527 FormatStyle AllowsMergedLoops = getLLVMStyle(); 528 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 529 verifyFormat("while (true) continue;", AllowsMergedLoops); 530 verifyFormat("for (;;) continue;", AllowsMergedLoops); 531 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 532 verifyFormat("while (true)\n" 533 " ;", 534 AllowsMergedLoops); 535 verifyFormat("for (;;)\n" 536 " ;", 537 AllowsMergedLoops); 538 verifyFormat("for (;;)\n" 539 " for (;;) continue;", 540 AllowsMergedLoops); 541 verifyFormat("for (;;) // Can't merge this\n" 542 " continue;", 543 AllowsMergedLoops); 544 verifyFormat("for (;;) /* still don't merge */\n" 545 " continue;", 546 AllowsMergedLoops); 547 } 548 549 TEST_F(FormatTest, FormatShortBracedStatements) { 550 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 551 AllowSimpleBracedStatements.ColumnLimit = 40; 552 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 553 554 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 555 FormatStyle::SIS_WithoutElse; 556 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 557 558 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 559 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 560 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 561 562 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 563 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 564 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 565 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 566 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 567 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 568 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 569 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 570 verifyFormat("if (true) {\n" 571 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 572 "}", 573 AllowSimpleBracedStatements); 574 verifyFormat("if (true) { //\n" 575 " f();\n" 576 "}", 577 AllowSimpleBracedStatements); 578 verifyFormat("if (true) {\n" 579 " f();\n" 580 " f();\n" 581 "}", 582 AllowSimpleBracedStatements); 583 verifyFormat("if (true) {\n" 584 " f();\n" 585 "} else {\n" 586 " f();\n" 587 "}", 588 AllowSimpleBracedStatements); 589 590 verifyFormat("struct A2 {\n" 591 " int X;\n" 592 "};", 593 AllowSimpleBracedStatements); 594 verifyFormat("typedef struct A2 {\n" 595 " int X;\n" 596 "} A2_t;", 597 AllowSimpleBracedStatements); 598 verifyFormat("template <int> struct A2 {\n" 599 " struct B {};\n" 600 "};", 601 AllowSimpleBracedStatements); 602 603 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 604 FormatStyle::SIS_Never; 605 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 606 verifyFormat("if (true) {\n" 607 " f();\n" 608 "}", 609 AllowSimpleBracedStatements); 610 verifyFormat("if (true) {\n" 611 " f();\n" 612 "} else {\n" 613 " f();\n" 614 "}", 615 AllowSimpleBracedStatements); 616 617 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 618 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 619 verifyFormat("while (true) {\n" 620 " f();\n" 621 "}", 622 AllowSimpleBracedStatements); 623 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 624 verifyFormat("for (;;) {\n" 625 " f();\n" 626 "}", 627 AllowSimpleBracedStatements); 628 629 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 630 FormatStyle::SIS_WithoutElse; 631 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 632 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 633 634 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 635 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 636 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 637 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 638 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 639 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 640 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 641 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 642 verifyFormat("if (true)\n" 643 "{\n" 644 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 645 "}", 646 AllowSimpleBracedStatements); 647 verifyFormat("if (true)\n" 648 "{ //\n" 649 " f();\n" 650 "}", 651 AllowSimpleBracedStatements); 652 verifyFormat("if (true)\n" 653 "{\n" 654 " f();\n" 655 " f();\n" 656 "}", 657 AllowSimpleBracedStatements); 658 verifyFormat("if (true)\n" 659 "{\n" 660 " f();\n" 661 "} else\n" 662 "{\n" 663 " f();\n" 664 "}", 665 AllowSimpleBracedStatements); 666 667 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 668 FormatStyle::SIS_Never; 669 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 670 verifyFormat("if (true)\n" 671 "{\n" 672 " f();\n" 673 "}", 674 AllowSimpleBracedStatements); 675 verifyFormat("if (true)\n" 676 "{\n" 677 " f();\n" 678 "} else\n" 679 "{\n" 680 " f();\n" 681 "}", 682 AllowSimpleBracedStatements); 683 684 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 685 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 686 verifyFormat("while (true)\n" 687 "{\n" 688 " f();\n" 689 "}", 690 AllowSimpleBracedStatements); 691 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 692 verifyFormat("for (;;)\n" 693 "{\n" 694 " f();\n" 695 "}", 696 AllowSimpleBracedStatements); 697 } 698 699 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 700 FormatStyle Style = getLLVMStyleWithColumns(60); 701 Style.AllowShortBlocksOnASingleLine = true; 702 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 703 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 704 EXPECT_EQ("#define A \\\n" 705 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 706 " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" 707 "X;", 708 format("#define A \\\n" 709 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 710 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 711 " }\n" 712 "X;", 713 Style)); 714 } 715 716 TEST_F(FormatTest, ParseIfElse) { 717 verifyFormat("if (true)\n" 718 " if (true)\n" 719 " if (true)\n" 720 " f();\n" 721 " else\n" 722 " g();\n" 723 " else\n" 724 " h();\n" 725 "else\n" 726 " i();"); 727 verifyFormat("if (true)\n" 728 " if (true)\n" 729 " if (true) {\n" 730 " if (true)\n" 731 " f();\n" 732 " } else {\n" 733 " g();\n" 734 " }\n" 735 " else\n" 736 " h();\n" 737 "else {\n" 738 " i();\n" 739 "}"); 740 verifyFormat("if (true)\n" 741 " if constexpr (true)\n" 742 " if (true) {\n" 743 " if constexpr (true)\n" 744 " f();\n" 745 " } else {\n" 746 " g();\n" 747 " }\n" 748 " else\n" 749 " h();\n" 750 "else {\n" 751 " i();\n" 752 "}"); 753 verifyFormat("void f() {\n" 754 " if (a) {\n" 755 " } else {\n" 756 " }\n" 757 "}"); 758 } 759 760 TEST_F(FormatTest, ElseIf) { 761 verifyFormat("if (a) {\n} else if (b) {\n}"); 762 verifyFormat("if (a)\n" 763 " f();\n" 764 "else if (b)\n" 765 " g();\n" 766 "else\n" 767 " h();"); 768 verifyFormat("if constexpr (a)\n" 769 " f();\n" 770 "else if constexpr (b)\n" 771 " g();\n" 772 "else\n" 773 " h();"); 774 verifyFormat("if (a) {\n" 775 " f();\n" 776 "}\n" 777 "// or else ..\n" 778 "else {\n" 779 " g()\n" 780 "}"); 781 782 verifyFormat("if (a) {\n" 783 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 785 "}"); 786 verifyFormat("if (a) {\n" 787 "} else if (\n" 788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 789 "}", 790 getLLVMStyleWithColumns(62)); 791 verifyFormat("if (a) {\n" 792 "} else if constexpr (\n" 793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 794 "}", 795 getLLVMStyleWithColumns(62)); 796 } 797 798 TEST_F(FormatTest, FormatsForLoop) { 799 verifyFormat( 800 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 801 " ++VeryVeryLongLoopVariable)\n" 802 " ;"); 803 verifyFormat("for (;;)\n" 804 " f();"); 805 verifyFormat("for (;;) {\n}"); 806 verifyFormat("for (;;) {\n" 807 " f();\n" 808 "}"); 809 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 810 811 verifyFormat( 812 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 813 " E = UnwrappedLines.end();\n" 814 " I != E; ++I) {\n}"); 815 816 verifyFormat( 817 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 818 " ++IIIII) {\n}"); 819 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 820 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 821 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 822 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 823 " I = FD->getDeclsInPrototypeScope().begin(),\n" 824 " E = FD->getDeclsInPrototypeScope().end();\n" 825 " I != E; ++I) {\n}"); 826 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 827 " I = Container.begin(),\n" 828 " E = Container.end();\n" 829 " I != E; ++I) {\n}", 830 getLLVMStyleWithColumns(76)); 831 832 verifyFormat( 833 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 835 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 836 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 837 " ++aaaaaaaaaaa) {\n}"); 838 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 839 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 840 " ++i) {\n}"); 841 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 842 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 843 "}"); 844 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 845 " aaaaaaaaaa);\n" 846 " iter; ++iter) {\n" 847 "}"); 848 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 850 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 851 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 852 853 // These should not be formatted as Objective-C for-in loops. 854 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 855 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 856 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 857 verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 858 859 FormatStyle NoBinPacking = getLLVMStyle(); 860 NoBinPacking.BinPackParameters = false; 861 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 862 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 863 " aaaaaaaaaaaaaaaa,\n" 864 " aaaaaaaaaaaaaaaa,\n" 865 " aaaaaaaaaaaaaaaa);\n" 866 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 867 "}", 868 NoBinPacking); 869 verifyFormat( 870 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 871 " E = UnwrappedLines.end();\n" 872 " I != E;\n" 873 " ++I) {\n}", 874 NoBinPacking); 875 876 FormatStyle AlignLeft = getLLVMStyle(); 877 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 878 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 879 } 880 881 TEST_F(FormatTest, RangeBasedForLoops) { 882 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 884 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 885 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 886 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 888 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 889 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 890 } 891 892 TEST_F(FormatTest, ForEachLoops) { 893 verifyFormat("void f() {\n" 894 " foreach (Item *item, itemlist) {}\n" 895 " Q_FOREACH (Item *item, itemlist) {}\n" 896 " BOOST_FOREACH (Item *item, itemlist) {}\n" 897 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 898 "}"); 899 900 // As function-like macros. 901 verifyFormat("#define foreach(x, y)\n" 902 "#define Q_FOREACH(x, y)\n" 903 "#define BOOST_FOREACH(x, y)\n" 904 "#define UNKNOWN_FOREACH(x, y)\n"); 905 906 // Not as function-like macros. 907 verifyFormat("#define foreach (x, y)\n" 908 "#define Q_FOREACH (x, y)\n" 909 "#define BOOST_FOREACH (x, y)\n" 910 "#define UNKNOWN_FOREACH (x, y)\n"); 911 } 912 913 TEST_F(FormatTest, FormatsWhileLoop) { 914 verifyFormat("while (true) {\n}"); 915 verifyFormat("while (true)\n" 916 " f();"); 917 verifyFormat("while () {\n}"); 918 verifyFormat("while () {\n" 919 " f();\n" 920 "}"); 921 } 922 923 TEST_F(FormatTest, FormatsDoWhile) { 924 verifyFormat("do {\n" 925 " do_something();\n" 926 "} while (something());"); 927 verifyFormat("do\n" 928 " do_something();\n" 929 "while (something());"); 930 } 931 932 TEST_F(FormatTest, FormatsSwitchStatement) { 933 verifyFormat("switch (x) {\n" 934 "case 1:\n" 935 " f();\n" 936 " break;\n" 937 "case kFoo:\n" 938 "case ns::kBar:\n" 939 "case kBaz:\n" 940 " break;\n" 941 "default:\n" 942 " g();\n" 943 " break;\n" 944 "}"); 945 verifyFormat("switch (x) {\n" 946 "case 1: {\n" 947 " f();\n" 948 " break;\n" 949 "}\n" 950 "case 2: {\n" 951 " break;\n" 952 "}\n" 953 "}"); 954 verifyFormat("switch (x) {\n" 955 "case 1: {\n" 956 " f();\n" 957 " {\n" 958 " g();\n" 959 " h();\n" 960 " }\n" 961 " break;\n" 962 "}\n" 963 "}"); 964 verifyFormat("switch (x) {\n" 965 "case 1: {\n" 966 " f();\n" 967 " if (foo) {\n" 968 " g();\n" 969 " h();\n" 970 " }\n" 971 " break;\n" 972 "}\n" 973 "}"); 974 verifyFormat("switch (x) {\n" 975 "case 1: {\n" 976 " f();\n" 977 " g();\n" 978 "} break;\n" 979 "}"); 980 verifyFormat("switch (test)\n" 981 " ;"); 982 verifyFormat("switch (x) {\n" 983 "default: {\n" 984 " // Do nothing.\n" 985 "}\n" 986 "}"); 987 verifyFormat("switch (x) {\n" 988 "// comment\n" 989 "// if 1, do f()\n" 990 "case 1:\n" 991 " f();\n" 992 "}"); 993 verifyFormat("switch (x) {\n" 994 "case 1:\n" 995 " // Do amazing stuff\n" 996 " {\n" 997 " f();\n" 998 " g();\n" 999 " }\n" 1000 " break;\n" 1001 "}"); 1002 verifyFormat("#define A \\\n" 1003 " switch (x) { \\\n" 1004 " case a: \\\n" 1005 " foo = b; \\\n" 1006 " }", 1007 getLLVMStyleWithColumns(20)); 1008 verifyFormat("#define OPERATION_CASE(name) \\\n" 1009 " case OP_name: \\\n" 1010 " return operations::Operation##name\n", 1011 getLLVMStyleWithColumns(40)); 1012 verifyFormat("switch (x) {\n" 1013 "case 1:;\n" 1014 "default:;\n" 1015 " int i;\n" 1016 "}"); 1017 1018 verifyGoogleFormat("switch (x) {\n" 1019 " case 1:\n" 1020 " f();\n" 1021 " break;\n" 1022 " case kFoo:\n" 1023 " case ns::kBar:\n" 1024 " case kBaz:\n" 1025 " break;\n" 1026 " default:\n" 1027 " g();\n" 1028 " break;\n" 1029 "}"); 1030 verifyGoogleFormat("switch (x) {\n" 1031 " case 1: {\n" 1032 " f();\n" 1033 " break;\n" 1034 " }\n" 1035 "}"); 1036 verifyGoogleFormat("switch (test)\n" 1037 " ;"); 1038 1039 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 1040 " case OP_name: \\\n" 1041 " return operations::Operation##name\n"); 1042 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 1043 " // Get the correction operation class.\n" 1044 " switch (OpCode) {\n" 1045 " CASE(Add);\n" 1046 " CASE(Subtract);\n" 1047 " default:\n" 1048 " return operations::Unknown;\n" 1049 " }\n" 1050 "#undef OPERATION_CASE\n" 1051 "}"); 1052 verifyFormat("DEBUG({\n" 1053 " switch (x) {\n" 1054 " case A:\n" 1055 " f();\n" 1056 " break;\n" 1057 " // fallthrough\n" 1058 " case B:\n" 1059 " g();\n" 1060 " break;\n" 1061 " }\n" 1062 "});"); 1063 EXPECT_EQ("DEBUG({\n" 1064 " switch (x) {\n" 1065 " case A:\n" 1066 " f();\n" 1067 " break;\n" 1068 " // On B:\n" 1069 " case B:\n" 1070 " g();\n" 1071 " break;\n" 1072 " }\n" 1073 "});", 1074 format("DEBUG({\n" 1075 " switch (x) {\n" 1076 " case A:\n" 1077 " f();\n" 1078 " break;\n" 1079 " // On B:\n" 1080 " case B:\n" 1081 " g();\n" 1082 " break;\n" 1083 " }\n" 1084 "});", 1085 getLLVMStyle())); 1086 EXPECT_EQ("switch (n) {\n" 1087 "case 0: {\n" 1088 " return false;\n" 1089 "}\n" 1090 "default: {\n" 1091 " return true;\n" 1092 "}\n" 1093 "}", 1094 format("switch (n)\n" 1095 "{\n" 1096 "case 0: {\n" 1097 " return false;\n" 1098 "}\n" 1099 "default: {\n" 1100 " return true;\n" 1101 "}\n" 1102 "}", 1103 getLLVMStyle())); 1104 verifyFormat("switch (a) {\n" 1105 "case (b):\n" 1106 " return;\n" 1107 "}"); 1108 1109 verifyFormat("switch (a) {\n" 1110 "case some_namespace::\n" 1111 " some_constant:\n" 1112 " return;\n" 1113 "}", 1114 getLLVMStyleWithColumns(34)); 1115 1116 FormatStyle Style = getLLVMStyle(); 1117 Style.IndentCaseLabels = true; 1118 Style.AllowShortBlocksOnASingleLine = false; 1119 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1120 Style.BraceWrapping.AfterControlStatement = true; 1121 EXPECT_EQ("switch (n)\n" 1122 "{\n" 1123 " case 0:\n" 1124 " {\n" 1125 " return false;\n" 1126 " }\n" 1127 " default:\n" 1128 " {\n" 1129 " return true;\n" 1130 " }\n" 1131 "}", 1132 format("switch (n) {\n" 1133 " case 0: {\n" 1134 " return false;\n" 1135 " }\n" 1136 " default: {\n" 1137 " return true;\n" 1138 " }\n" 1139 "}", 1140 Style)); 1141 } 1142 1143 TEST_F(FormatTest, CaseRanges) { 1144 verifyFormat("switch (x) {\n" 1145 "case 'A' ... 'Z':\n" 1146 "case 1 ... 5:\n" 1147 "case a ... b:\n" 1148 " break;\n" 1149 "}"); 1150 } 1151 1152 TEST_F(FormatTest, ShortCaseLabels) { 1153 FormatStyle Style = getLLVMStyle(); 1154 Style.AllowShortCaseLabelsOnASingleLine = true; 1155 verifyFormat("switch (a) {\n" 1156 "case 1: x = 1; break;\n" 1157 "case 2: return;\n" 1158 "case 3:\n" 1159 "case 4:\n" 1160 "case 5: return;\n" 1161 "case 6: // comment\n" 1162 " return;\n" 1163 "case 7:\n" 1164 " // comment\n" 1165 " return;\n" 1166 "case 8:\n" 1167 " x = 8; // comment\n" 1168 " break;\n" 1169 "default: y = 1; break;\n" 1170 "}", 1171 Style); 1172 verifyFormat("switch (a) {\n" 1173 "case 0: return; // comment\n" 1174 "case 1: break; // comment\n" 1175 "case 2: return;\n" 1176 "// comment\n" 1177 "case 3: return;\n" 1178 "// comment 1\n" 1179 "// comment 2\n" 1180 "// comment 3\n" 1181 "case 4: break; /* comment */\n" 1182 "case 5:\n" 1183 " // comment\n" 1184 " break;\n" 1185 "case 6: /* comment */ x = 1; break;\n" 1186 "case 7: x = /* comment */ 1; break;\n" 1187 "case 8:\n" 1188 " x = 1; /* comment */\n" 1189 " break;\n" 1190 "case 9:\n" 1191 " break; // comment line 1\n" 1192 " // comment line 2\n" 1193 "}", 1194 Style); 1195 EXPECT_EQ("switch (a) {\n" 1196 "case 1:\n" 1197 " x = 8;\n" 1198 " // fall through\n" 1199 "case 2: x = 8;\n" 1200 "// comment\n" 1201 "case 3:\n" 1202 " return; /* comment line 1\n" 1203 " * comment line 2 */\n" 1204 "case 4: i = 8;\n" 1205 "// something else\n" 1206 "#if FOO\n" 1207 "case 5: break;\n" 1208 "#endif\n" 1209 "}", 1210 format("switch (a) {\n" 1211 "case 1: x = 8;\n" 1212 " // fall through\n" 1213 "case 2:\n" 1214 " x = 8;\n" 1215 "// comment\n" 1216 "case 3:\n" 1217 " return; /* comment line 1\n" 1218 " * comment line 2 */\n" 1219 "case 4:\n" 1220 " i = 8;\n" 1221 "// something else\n" 1222 "#if FOO\n" 1223 "case 5: break;\n" 1224 "#endif\n" 1225 "}", 1226 Style)); 1227 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1228 " return; // long long long long long long long long long long long long comment\n" 1229 " // line\n" "}", 1230 format("switch (a) {\n" 1231 "case 0: return; // long long long long long long long long long long long long comment line\n" 1232 "}", 1233 Style)); 1234 EXPECT_EQ("switch (a) {\n" 1235 "case 0:\n" 1236 " return; /* long long long long long long long long long long long long comment\n" 1237 " line */\n" 1238 "}", 1239 format("switch (a) {\n" 1240 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1241 "}", 1242 Style)); 1243 verifyFormat("switch (a) {\n" 1244 "#if FOO\n" 1245 "case 0: return 0;\n" 1246 "#endif\n" 1247 "}", 1248 Style); 1249 verifyFormat("switch (a) {\n" 1250 "case 1: {\n" 1251 "}\n" 1252 "case 2: {\n" 1253 " return;\n" 1254 "}\n" 1255 "case 3: {\n" 1256 " x = 1;\n" 1257 " return;\n" 1258 "}\n" 1259 "case 4:\n" 1260 " if (x)\n" 1261 " return;\n" 1262 "}", 1263 Style); 1264 Style.ColumnLimit = 21; 1265 verifyFormat("switch (a) {\n" 1266 "case 1: x = 1; break;\n" 1267 "case 2: return;\n" 1268 "case 3:\n" 1269 "case 4:\n" 1270 "case 5: return;\n" 1271 "default:\n" 1272 " y = 1;\n" 1273 " break;\n" 1274 "}", 1275 Style); 1276 Style.ColumnLimit = 80; 1277 Style.AllowShortCaseLabelsOnASingleLine = false; 1278 Style.IndentCaseLabels = true; 1279 EXPECT_EQ("switch (n) {\n" 1280 " default /*comments*/:\n" 1281 " return true;\n" 1282 " case 0:\n" 1283 " return false;\n" 1284 "}", 1285 format("switch (n) {\n" 1286 "default/*comments*/:\n" 1287 " return true;\n" 1288 "case 0:\n" 1289 " return false;\n" 1290 "}", 1291 Style)); 1292 Style.AllowShortCaseLabelsOnASingleLine = true; 1293 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1294 Style.BraceWrapping.AfterControlStatement = true; 1295 EXPECT_EQ("switch (n)\n" 1296 "{\n" 1297 " case 0:\n" 1298 " {\n" 1299 " return false;\n" 1300 " }\n" 1301 " default:\n" 1302 " {\n" 1303 " return true;\n" 1304 " }\n" 1305 "}", 1306 format("switch (n) {\n" 1307 " case 0: {\n" 1308 " return false;\n" 1309 " }\n" 1310 " default:\n" 1311 " {\n" 1312 " return true;\n" 1313 " }\n" 1314 "}", 1315 Style)); 1316 } 1317 1318 TEST_F(FormatTest, FormatsLabels) { 1319 verifyFormat("void f() {\n" 1320 " some_code();\n" 1321 "test_label:\n" 1322 " some_other_code();\n" 1323 " {\n" 1324 " some_more_code();\n" 1325 " another_label:\n" 1326 " some_more_code();\n" 1327 " }\n" 1328 "}"); 1329 verifyFormat("{\n" 1330 " some_code();\n" 1331 "test_label:\n" 1332 " some_other_code();\n" 1333 "}"); 1334 verifyFormat("{\n" 1335 " some_code();\n" 1336 "test_label:;\n" 1337 " int i = 0;\n" 1338 "}"); 1339 } 1340 1341 //===----------------------------------------------------------------------===// 1342 // Tests for classes, namespaces, etc. 1343 //===----------------------------------------------------------------------===// 1344 1345 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1346 verifyFormat("class A {};"); 1347 } 1348 1349 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1350 verifyFormat("class A {\n" 1351 "public:\n" 1352 "public: // comment\n" 1353 "protected:\n" 1354 "private:\n" 1355 " void f() {}\n" 1356 "};"); 1357 verifyFormat("export class A {\n" 1358 "public:\n" 1359 "public: // comment\n" 1360 "protected:\n" 1361 "private:\n" 1362 " void f() {}\n" 1363 "};"); 1364 verifyGoogleFormat("class A {\n" 1365 " public:\n" 1366 " protected:\n" 1367 " private:\n" 1368 " void f() {}\n" 1369 "};"); 1370 verifyGoogleFormat("export class A {\n" 1371 " public:\n" 1372 " protected:\n" 1373 " private:\n" 1374 " void f() {}\n" 1375 "};"); 1376 verifyFormat("class A {\n" 1377 "public slots:\n" 1378 " void f1() {}\n" 1379 "public Q_SLOTS:\n" 1380 " void f2() {}\n" 1381 "protected slots:\n" 1382 " void f3() {}\n" 1383 "protected Q_SLOTS:\n" 1384 " void f4() {}\n" 1385 "private slots:\n" 1386 " void f5() {}\n" 1387 "private Q_SLOTS:\n" 1388 " void f6() {}\n" 1389 "signals:\n" 1390 " void g1();\n" 1391 "Q_SIGNALS:\n" 1392 " void g2();\n" 1393 "};"); 1394 1395 // Don't interpret 'signals' the wrong way. 1396 verifyFormat("signals.set();"); 1397 verifyFormat("for (Signals signals : f()) {\n}"); 1398 verifyFormat("{\n" 1399 " signals.set(); // This needs indentation.\n" 1400 "}"); 1401 verifyFormat("void f() {\n" 1402 "label:\n" 1403 " signals.baz();\n" 1404 "}"); 1405 } 1406 1407 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1408 EXPECT_EQ("class A {\n" 1409 "public:\n" 1410 " void f();\n" 1411 "\n" 1412 "private:\n" 1413 " void g() {}\n" 1414 " // test\n" 1415 "protected:\n" 1416 " int h;\n" 1417 "};", 1418 format("class A {\n" 1419 "public:\n" 1420 "void f();\n" 1421 "private:\n" 1422 "void g() {}\n" 1423 "// test\n" 1424 "protected:\n" 1425 "int h;\n" 1426 "};")); 1427 EXPECT_EQ("class A {\n" 1428 "protected:\n" 1429 "public:\n" 1430 " void f();\n" 1431 "};", 1432 format("class A {\n" 1433 "protected:\n" 1434 "\n" 1435 "public:\n" 1436 "\n" 1437 " void f();\n" 1438 "};")); 1439 1440 // Even ensure proper spacing inside macros. 1441 EXPECT_EQ("#define B \\\n" 1442 " class A { \\\n" 1443 " protected: \\\n" 1444 " public: \\\n" 1445 " void f(); \\\n" 1446 " };", 1447 format("#define B \\\n" 1448 " class A { \\\n" 1449 " protected: \\\n" 1450 " \\\n" 1451 " public: \\\n" 1452 " \\\n" 1453 " void f(); \\\n" 1454 " };", 1455 getGoogleStyle())); 1456 // But don't remove empty lines after macros ending in access specifiers. 1457 EXPECT_EQ("#define A private:\n" 1458 "\n" 1459 "int i;", 1460 format("#define A private:\n" 1461 "\n" 1462 "int i;")); 1463 } 1464 1465 TEST_F(FormatTest, FormatsClasses) { 1466 verifyFormat("class A : public B {};"); 1467 verifyFormat("class A : public ::B {};"); 1468 1469 verifyFormat( 1470 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1471 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1472 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1473 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1474 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1475 verifyFormat( 1476 "class A : public B, public C, public D, public E, public F {};"); 1477 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1478 " public C,\n" 1479 " public D,\n" 1480 " public E,\n" 1481 " public F,\n" 1482 " public G {};"); 1483 1484 verifyFormat("class\n" 1485 " ReallyReallyLongClassName {\n" 1486 " int i;\n" 1487 "};", 1488 getLLVMStyleWithColumns(32)); 1489 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1490 " aaaaaaaaaaaaaaaa> {};"); 1491 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1492 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1493 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1494 verifyFormat("template <class R, class C>\n" 1495 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1496 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1497 verifyFormat("class ::A::B {};"); 1498 } 1499 1500 TEST_F(FormatTest, BreakInheritanceStyle) { 1501 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 1502 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 1503 FormatStyle::BILS_BeforeComma; 1504 verifyFormat("class MyClass : public X {};", 1505 StyleWithInheritanceBreakBeforeComma); 1506 verifyFormat("class MyClass\n" 1507 " : public X\n" 1508 " , public Y {};", 1509 StyleWithInheritanceBreakBeforeComma); 1510 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 1511 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 1512 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1513 StyleWithInheritanceBreakBeforeComma); 1514 verifyFormat("struct aaaaaaaaaaaaa\n" 1515 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 1516 " aaaaaaaaaaaaaaaa> {};", 1517 StyleWithInheritanceBreakBeforeComma); 1518 1519 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 1520 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 1521 FormatStyle::BILS_AfterColon; 1522 verifyFormat("class MyClass : public X {};", 1523 StyleWithInheritanceBreakAfterColon); 1524 verifyFormat("class MyClass : public X, public Y {};", 1525 StyleWithInheritanceBreakAfterColon); 1526 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 1527 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1528 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1529 StyleWithInheritanceBreakAfterColon); 1530 verifyFormat("struct aaaaaaaaaaaaa :\n" 1531 " public aaaaaaaaaaaaaaaaaaa< // break\n" 1532 " aaaaaaaaaaaaaaaa> {};", 1533 StyleWithInheritanceBreakAfterColon); 1534 } 1535 1536 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1537 verifyFormat("class A {\n} a, b;"); 1538 verifyFormat("struct A {\n} a, b;"); 1539 verifyFormat("union A {\n} a;"); 1540 } 1541 1542 TEST_F(FormatTest, FormatsEnum) { 1543 verifyFormat("enum {\n" 1544 " Zero,\n" 1545 " One = 1,\n" 1546 " Two = One + 1,\n" 1547 " Three = (One + Two),\n" 1548 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1549 " Five = (One, Two, Three, Four, 5)\n" 1550 "};"); 1551 verifyGoogleFormat("enum {\n" 1552 " Zero,\n" 1553 " One = 1,\n" 1554 " Two = One + 1,\n" 1555 " Three = (One + Two),\n" 1556 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1557 " Five = (One, Two, Three, Four, 5)\n" 1558 "};"); 1559 verifyFormat("enum Enum {};"); 1560 verifyFormat("enum {};"); 1561 verifyFormat("enum X E {} d;"); 1562 verifyFormat("enum __attribute__((...)) E {} d;"); 1563 verifyFormat("enum __declspec__((...)) E {} d;"); 1564 verifyFormat("enum {\n" 1565 " Bar = Foo<int, int>::value\n" 1566 "};", 1567 getLLVMStyleWithColumns(30)); 1568 1569 verifyFormat("enum ShortEnum { A, B, C };"); 1570 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1571 1572 EXPECT_EQ("enum KeepEmptyLines {\n" 1573 " ONE,\n" 1574 "\n" 1575 " TWO,\n" 1576 "\n" 1577 " THREE\n" 1578 "}", 1579 format("enum KeepEmptyLines {\n" 1580 " ONE,\n" 1581 "\n" 1582 " TWO,\n" 1583 "\n" 1584 "\n" 1585 " THREE\n" 1586 "}")); 1587 verifyFormat("enum E { // comment\n" 1588 " ONE,\n" 1589 " TWO\n" 1590 "};\n" 1591 "int i;"); 1592 // Not enums. 1593 verifyFormat("enum X f() {\n" 1594 " a();\n" 1595 " return 42;\n" 1596 "}"); 1597 verifyFormat("enum X Type::f() {\n" 1598 " a();\n" 1599 " return 42;\n" 1600 "}"); 1601 verifyFormat("enum ::X f() {\n" 1602 " a();\n" 1603 " return 42;\n" 1604 "}"); 1605 verifyFormat("enum ns::X f() {\n" 1606 " a();\n" 1607 " return 42;\n" 1608 "}"); 1609 } 1610 1611 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1612 verifyFormat("enum Type {\n" 1613 " One = 0; // These semicolons should be commas.\n" 1614 " Two = 1;\n" 1615 "};"); 1616 verifyFormat("namespace n {\n" 1617 "enum Type {\n" 1618 " One,\n" 1619 " Two, // missing };\n" 1620 " int i;\n" 1621 "}\n" 1622 "void g() {}"); 1623 } 1624 1625 TEST_F(FormatTest, FormatsEnumStruct) { 1626 verifyFormat("enum struct {\n" 1627 " Zero,\n" 1628 " One = 1,\n" 1629 " Two = One + 1,\n" 1630 " Three = (One + Two),\n" 1631 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1632 " Five = (One, Two, Three, Four, 5)\n" 1633 "};"); 1634 verifyFormat("enum struct Enum {};"); 1635 verifyFormat("enum struct {};"); 1636 verifyFormat("enum struct X E {} d;"); 1637 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1638 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1639 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1640 } 1641 1642 TEST_F(FormatTest, FormatsEnumClass) { 1643 verifyFormat("enum class {\n" 1644 " Zero,\n" 1645 " One = 1,\n" 1646 " Two = One + 1,\n" 1647 " Three = (One + Two),\n" 1648 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1649 " Five = (One, Two, Three, Four, 5)\n" 1650 "};"); 1651 verifyFormat("enum class Enum {};"); 1652 verifyFormat("enum class {};"); 1653 verifyFormat("enum class X E {} d;"); 1654 verifyFormat("enum class __attribute__((...)) E {} d;"); 1655 verifyFormat("enum class __declspec__((...)) E {} d;"); 1656 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1657 } 1658 1659 TEST_F(FormatTest, FormatsEnumTypes) { 1660 verifyFormat("enum X : int {\n" 1661 " A, // Force multiple lines.\n" 1662 " B\n" 1663 "};"); 1664 verifyFormat("enum X : int { A, B };"); 1665 verifyFormat("enum X : std::uint32_t { A, B };"); 1666 } 1667 1668 TEST_F(FormatTest, FormatsTypedefEnum) { 1669 FormatStyle Style = getLLVMStyle(); 1670 Style.ColumnLimit = 40; 1671 verifyFormat("typedef enum {} EmptyEnum;"); 1672 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1673 verifyFormat("typedef enum {\n" 1674 " ZERO = 0,\n" 1675 " ONE = 1,\n" 1676 " TWO = 2,\n" 1677 " THREE = 3\n" 1678 "} LongEnum;", 1679 Style); 1680 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1681 Style.BraceWrapping.AfterEnum = true; 1682 verifyFormat("typedef enum {} EmptyEnum;"); 1683 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1684 verifyFormat("typedef enum\n" 1685 "{\n" 1686 " ZERO = 0,\n" 1687 " ONE = 1,\n" 1688 " TWO = 2,\n" 1689 " THREE = 3\n" 1690 "} LongEnum;", 1691 Style); 1692 } 1693 1694 TEST_F(FormatTest, FormatsNSEnums) { 1695 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1696 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1697 " // Information about someDecentlyLongValue.\n" 1698 " someDecentlyLongValue,\n" 1699 " // Information about anotherDecentlyLongValue.\n" 1700 " anotherDecentlyLongValue,\n" 1701 " // Information about aThirdDecentlyLongValue.\n" 1702 " aThirdDecentlyLongValue\n" 1703 "};"); 1704 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1705 " a = 1,\n" 1706 " b = 2,\n" 1707 " c = 3,\n" 1708 "};"); 1709 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1710 " a = 1,\n" 1711 " b = 2,\n" 1712 " c = 3,\n" 1713 "};"); 1714 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1715 " a = 1,\n" 1716 " b = 2,\n" 1717 " c = 3,\n" 1718 "};"); 1719 } 1720 1721 TEST_F(FormatTest, FormatsBitfields) { 1722 verifyFormat("struct Bitfields {\n" 1723 " unsigned sClass : 8;\n" 1724 " unsigned ValueKind : 2;\n" 1725 "};"); 1726 verifyFormat("struct A {\n" 1727 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1728 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1729 "};"); 1730 verifyFormat("struct MyStruct {\n" 1731 " uchar data;\n" 1732 " uchar : 8;\n" 1733 " uchar : 8;\n" 1734 " uchar other;\n" 1735 "};"); 1736 } 1737 1738 TEST_F(FormatTest, FormatsNamespaces) { 1739 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1740 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1741 1742 verifyFormat("namespace some_namespace {\n" 1743 "class A {};\n" 1744 "void f() { f(); }\n" 1745 "}", 1746 LLVMWithNoNamespaceFix); 1747 verifyFormat("/* something */ namespace some_namespace {\n" 1748 "class A {};\n" 1749 "void f() { f(); }\n" 1750 "}", 1751 LLVMWithNoNamespaceFix); 1752 verifyFormat("namespace {\n" 1753 "class A {};\n" 1754 "void f() { f(); }\n" 1755 "}", 1756 LLVMWithNoNamespaceFix); 1757 verifyFormat("/* something */ namespace {\n" 1758 "class A {};\n" 1759 "void f() { f(); }\n" 1760 "}", 1761 LLVMWithNoNamespaceFix); 1762 verifyFormat("inline namespace X {\n" 1763 "class A {};\n" 1764 "void f() { f(); }\n" 1765 "}", 1766 LLVMWithNoNamespaceFix); 1767 verifyFormat("/* something */ inline namespace X {\n" 1768 "class A {};\n" 1769 "void f() { f(); }\n" 1770 "}", 1771 LLVMWithNoNamespaceFix); 1772 verifyFormat("export namespace X {\n" 1773 "class A {};\n" 1774 "void f() { f(); }\n" 1775 "}", 1776 LLVMWithNoNamespaceFix); 1777 verifyFormat("using namespace some_namespace;\n" 1778 "class A {};\n" 1779 "void f() { f(); }", 1780 LLVMWithNoNamespaceFix); 1781 1782 // This code is more common than we thought; if we 1783 // layout this correctly the semicolon will go into 1784 // its own line, which is undesirable. 1785 verifyFormat("namespace {};", 1786 LLVMWithNoNamespaceFix); 1787 verifyFormat("namespace {\n" 1788 "class A {};\n" 1789 "};", 1790 LLVMWithNoNamespaceFix); 1791 1792 verifyFormat("namespace {\n" 1793 "int SomeVariable = 0; // comment\n" 1794 "} // namespace", 1795 LLVMWithNoNamespaceFix); 1796 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1797 "#define HEADER_GUARD\n" 1798 "namespace my_namespace {\n" 1799 "int i;\n" 1800 "} // my_namespace\n" 1801 "#endif // HEADER_GUARD", 1802 format("#ifndef HEADER_GUARD\n" 1803 " #define HEADER_GUARD\n" 1804 " namespace my_namespace {\n" 1805 "int i;\n" 1806 "} // my_namespace\n" 1807 "#endif // HEADER_GUARD", 1808 LLVMWithNoNamespaceFix)); 1809 1810 EXPECT_EQ("namespace A::B {\n" 1811 "class C {};\n" 1812 "}", 1813 format("namespace A::B {\n" 1814 "class C {};\n" 1815 "}", 1816 LLVMWithNoNamespaceFix)); 1817 1818 FormatStyle Style = getLLVMStyle(); 1819 Style.NamespaceIndentation = FormatStyle::NI_All; 1820 EXPECT_EQ("namespace out {\n" 1821 " int i;\n" 1822 " namespace in {\n" 1823 " int i;\n" 1824 " } // namespace in\n" 1825 "} // namespace out", 1826 format("namespace out {\n" 1827 "int i;\n" 1828 "namespace in {\n" 1829 "int i;\n" 1830 "} // namespace in\n" 1831 "} // namespace out", 1832 Style)); 1833 1834 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1835 EXPECT_EQ("namespace out {\n" 1836 "int i;\n" 1837 "namespace in {\n" 1838 " int i;\n" 1839 "} // namespace in\n" 1840 "} // namespace out", 1841 format("namespace out {\n" 1842 "int i;\n" 1843 "namespace in {\n" 1844 "int i;\n" 1845 "} // namespace in\n" 1846 "} // namespace out", 1847 Style)); 1848 } 1849 1850 TEST_F(FormatTest, FormatsCompactNamespaces) { 1851 FormatStyle Style = getLLVMStyle(); 1852 Style.CompactNamespaces = true; 1853 1854 verifyFormat("namespace A { namespace B {\n" 1855 "}} // namespace A::B", 1856 Style); 1857 1858 EXPECT_EQ("namespace out { namespace in {\n" 1859 "}} // namespace out::in", 1860 format("namespace out {\n" 1861 "namespace in {\n" 1862 "} // namespace in\n" 1863 "} // namespace out", 1864 Style)); 1865 1866 // Only namespaces which have both consecutive opening and end get compacted 1867 EXPECT_EQ("namespace out {\n" 1868 "namespace in1 {\n" 1869 "} // namespace in1\n" 1870 "namespace in2 {\n" 1871 "} // namespace in2\n" 1872 "} // namespace out", 1873 format("namespace out {\n" 1874 "namespace in1 {\n" 1875 "} // namespace in1\n" 1876 "namespace in2 {\n" 1877 "} // namespace in2\n" 1878 "} // namespace out", 1879 Style)); 1880 1881 EXPECT_EQ("namespace out {\n" 1882 "int i;\n" 1883 "namespace in {\n" 1884 "int j;\n" 1885 "} // namespace in\n" 1886 "int k;\n" 1887 "} // namespace out", 1888 format("namespace out { int i;\n" 1889 "namespace in { int j; } // namespace in\n" 1890 "int k; } // namespace out", 1891 Style)); 1892 1893 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1894 "}}} // namespace A::B::C\n", 1895 format("namespace A { namespace B {\n" 1896 "namespace C {\n" 1897 "}} // namespace B::C\n" 1898 "} // namespace A\n", 1899 Style)); 1900 1901 Style.ColumnLimit = 40; 1902 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1903 "namespace bbbbbbbbbb {\n" 1904 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1905 format("namespace aaaaaaaaaa {\n" 1906 "namespace bbbbbbbbbb {\n" 1907 "} // namespace bbbbbbbbbb\n" 1908 "} // namespace aaaaaaaaaa", 1909 Style)); 1910 1911 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1912 "namespace cccccc {\n" 1913 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1914 format("namespace aaaaaa {\n" 1915 "namespace bbbbbb {\n" 1916 "namespace cccccc {\n" 1917 "} // namespace cccccc\n" 1918 "} // namespace bbbbbb\n" 1919 "} // namespace aaaaaa", 1920 Style)); 1921 Style.ColumnLimit = 80; 1922 1923 // Extra semicolon after 'inner' closing brace prevents merging 1924 EXPECT_EQ("namespace out { namespace in {\n" 1925 "}; } // namespace out::in", 1926 format("namespace out {\n" 1927 "namespace in {\n" 1928 "}; // namespace in\n" 1929 "} // namespace out", 1930 Style)); 1931 1932 // Extra semicolon after 'outer' closing brace is conserved 1933 EXPECT_EQ("namespace out { namespace in {\n" 1934 "}}; // namespace out::in", 1935 format("namespace out {\n" 1936 "namespace in {\n" 1937 "} // namespace in\n" 1938 "}; // namespace out", 1939 Style)); 1940 1941 Style.NamespaceIndentation = FormatStyle::NI_All; 1942 EXPECT_EQ("namespace out { namespace in {\n" 1943 " int i;\n" 1944 "}} // namespace out::in", 1945 format("namespace out {\n" 1946 "namespace in {\n" 1947 "int i;\n" 1948 "} // namespace in\n" 1949 "} // namespace out", 1950 Style)); 1951 EXPECT_EQ("namespace out { namespace mid {\n" 1952 " namespace in {\n" 1953 " int j;\n" 1954 " } // namespace in\n" 1955 " int k;\n" 1956 "}} // namespace out::mid", 1957 format("namespace out { namespace mid {\n" 1958 "namespace in { int j; } // namespace in\n" 1959 "int k; }} // namespace out::mid", 1960 Style)); 1961 1962 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1963 EXPECT_EQ("namespace out { namespace in {\n" 1964 " int i;\n" 1965 "}} // namespace out::in", 1966 format("namespace out {\n" 1967 "namespace in {\n" 1968 "int i;\n" 1969 "} // namespace in\n" 1970 "} // namespace out", 1971 Style)); 1972 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1973 " int i;\n" 1974 "}}} // namespace out::mid::in", 1975 format("namespace out {\n" 1976 "namespace mid {\n" 1977 "namespace in {\n" 1978 "int i;\n" 1979 "} // namespace in\n" 1980 "} // namespace mid\n" 1981 "} // namespace out", 1982 Style)); 1983 } 1984 1985 TEST_F(FormatTest, FormatsExternC) { 1986 verifyFormat("extern \"C\" {\nint a;"); 1987 verifyFormat("extern \"C\" {}"); 1988 verifyFormat("extern \"C\" {\n" 1989 "int foo();\n" 1990 "}"); 1991 verifyFormat("extern \"C\" int foo() {}"); 1992 verifyFormat("extern \"C\" int foo();"); 1993 verifyFormat("extern \"C\" int foo() {\n" 1994 " int i = 42;\n" 1995 " return i;\n" 1996 "}"); 1997 1998 FormatStyle Style = getLLVMStyle(); 1999 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2000 Style.BraceWrapping.AfterFunction = true; 2001 verifyFormat("extern \"C\" int foo() {}", Style); 2002 verifyFormat("extern \"C\" int foo();", Style); 2003 verifyFormat("extern \"C\" int foo()\n" 2004 "{\n" 2005 " int i = 42;\n" 2006 " return i;\n" 2007 "}", 2008 Style); 2009 2010 Style.BraceWrapping.AfterExternBlock = true; 2011 Style.BraceWrapping.SplitEmptyRecord = false; 2012 verifyFormat("extern \"C\"\n" 2013 "{}", 2014 Style); 2015 verifyFormat("extern \"C\"\n" 2016 "{\n" 2017 " int foo();\n" 2018 "}", 2019 Style); 2020 } 2021 2022 TEST_F(FormatTest, FormatsInlineASM) { 2023 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2024 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2025 verifyFormat( 2026 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2027 " \"cpuid\\n\\t\"\n" 2028 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2029 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2030 " : \"a\"(value));"); 2031 EXPECT_EQ( 2032 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2033 " __asm {\n" 2034 " mov edx,[that] // vtable in edx\n" 2035 " mov eax,methodIndex\n" 2036 " call [edx][eax*4] // stdcall\n" 2037 " }\n" 2038 "}", 2039 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2040 " __asm {\n" 2041 " mov edx,[that] // vtable in edx\n" 2042 " mov eax,methodIndex\n" 2043 " call [edx][eax*4] // stdcall\n" 2044 " }\n" 2045 "}")); 2046 EXPECT_EQ("_asm {\n" 2047 " xor eax, eax;\n" 2048 " cpuid;\n" 2049 "}", 2050 format("_asm {\n" 2051 " xor eax, eax;\n" 2052 " cpuid;\n" 2053 "}")); 2054 verifyFormat("void function() {\n" 2055 " // comment\n" 2056 " asm(\"\");\n" 2057 "}"); 2058 EXPECT_EQ("__asm {\n" 2059 "}\n" 2060 "int i;", 2061 format("__asm {\n" 2062 "}\n" 2063 "int i;")); 2064 } 2065 2066 TEST_F(FormatTest, FormatTryCatch) { 2067 verifyFormat("try {\n" 2068 " throw a * b;\n" 2069 "} catch (int a) {\n" 2070 " // Do nothing.\n" 2071 "} catch (...) {\n" 2072 " exit(42);\n" 2073 "}"); 2074 2075 // Function-level try statements. 2076 verifyFormat("int f() try { return 4; } catch (...) {\n" 2077 " return 5;\n" 2078 "}"); 2079 verifyFormat("class A {\n" 2080 " int a;\n" 2081 " A() try : a(0) {\n" 2082 " } catch (...) {\n" 2083 " throw;\n" 2084 " }\n" 2085 "};\n"); 2086 2087 // Incomplete try-catch blocks. 2088 verifyIncompleteFormat("try {} catch ("); 2089 } 2090 2091 TEST_F(FormatTest, FormatSEHTryCatch) { 2092 verifyFormat("__try {\n" 2093 " int a = b * c;\n" 2094 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2095 " // Do nothing.\n" 2096 "}"); 2097 2098 verifyFormat("__try {\n" 2099 " int a = b * c;\n" 2100 "} __finally {\n" 2101 " // Do nothing.\n" 2102 "}"); 2103 2104 verifyFormat("DEBUG({\n" 2105 " __try {\n" 2106 " } __finally {\n" 2107 " }\n" 2108 "});\n"); 2109 } 2110 2111 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2112 verifyFormat("try {\n" 2113 " f();\n" 2114 "} catch {\n" 2115 " g();\n" 2116 "}"); 2117 verifyFormat("try {\n" 2118 " f();\n" 2119 "} catch (A a) MACRO(x) {\n" 2120 " g();\n" 2121 "} catch (B b) MACRO(x) {\n" 2122 " g();\n" 2123 "}"); 2124 } 2125 2126 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2127 FormatStyle Style = getLLVMStyle(); 2128 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2129 FormatStyle::BS_WebKit}) { 2130 Style.BreakBeforeBraces = BraceStyle; 2131 verifyFormat("try {\n" 2132 " // something\n" 2133 "} catch (...) {\n" 2134 " // something\n" 2135 "}", 2136 Style); 2137 } 2138 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2139 verifyFormat("try {\n" 2140 " // something\n" 2141 "}\n" 2142 "catch (...) {\n" 2143 " // something\n" 2144 "}", 2145 Style); 2146 verifyFormat("__try {\n" 2147 " // something\n" 2148 "}\n" 2149 "__finally {\n" 2150 " // something\n" 2151 "}", 2152 Style); 2153 verifyFormat("@try {\n" 2154 " // something\n" 2155 "}\n" 2156 "@finally {\n" 2157 " // something\n" 2158 "}", 2159 Style); 2160 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2161 verifyFormat("try\n" 2162 "{\n" 2163 " // something\n" 2164 "}\n" 2165 "catch (...)\n" 2166 "{\n" 2167 " // something\n" 2168 "}", 2169 Style); 2170 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2171 verifyFormat("try\n" 2172 " {\n" 2173 " // something\n" 2174 " }\n" 2175 "catch (...)\n" 2176 " {\n" 2177 " // something\n" 2178 " }", 2179 Style); 2180 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2181 Style.BraceWrapping.BeforeCatch = true; 2182 verifyFormat("try {\n" 2183 " // something\n" 2184 "}\n" 2185 "catch (...) {\n" 2186 " // something\n" 2187 "}", 2188 Style); 2189 } 2190 2191 TEST_F(FormatTest, StaticInitializers) { 2192 verifyFormat("static SomeClass SC = {1, 'a'};"); 2193 2194 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2195 " 100000000, " 2196 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2197 2198 // Here, everything other than the "}" would fit on a line. 2199 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2200 " 10000000000000000000000000};"); 2201 EXPECT_EQ("S s = {a,\n" 2202 "\n" 2203 " b};", 2204 format("S s = {\n" 2205 " a,\n" 2206 "\n" 2207 " b\n" 2208 "};")); 2209 2210 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2211 // line. However, the formatting looks a bit off and this probably doesn't 2212 // happen often in practice. 2213 verifyFormat("static int Variable[1] = {\n" 2214 " {1000000000000000000000000000000000000}};", 2215 getLLVMStyleWithColumns(40)); 2216 } 2217 2218 TEST_F(FormatTest, DesignatedInitializers) { 2219 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2220 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2221 " .bbbbbbbbbb = 2,\n" 2222 " .cccccccccc = 3,\n" 2223 " .dddddddddd = 4,\n" 2224 " .eeeeeeeeee = 5};"); 2225 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2226 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2227 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2228 " .ccccccccccccccccccccccccccc = 3,\n" 2229 " .ddddddddddddddddddddddddddd = 4,\n" 2230 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2231 2232 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2233 2234 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 2235 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 2236 " [2] = bbbbbbbbbb,\n" 2237 " [3] = cccccccccc,\n" 2238 " [4] = dddddddddd,\n" 2239 " [5] = eeeeeeeeee};"); 2240 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2241 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2242 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2243 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 2244 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 2245 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 2246 } 2247 2248 TEST_F(FormatTest, NestedStaticInitializers) { 2249 verifyFormat("static A x = {{{}}};\n"); 2250 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2251 " {init1, init2, init3, init4}}};", 2252 getLLVMStyleWithColumns(50)); 2253 2254 verifyFormat("somes Status::global_reps[3] = {\n" 2255 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2256 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2257 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2258 getLLVMStyleWithColumns(60)); 2259 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2260 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2261 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2262 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2263 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2264 " {rect.fRight - rect.fLeft, rect.fBottom - " 2265 "rect.fTop}};"); 2266 2267 verifyFormat( 2268 "SomeArrayOfSomeType a = {\n" 2269 " {{1, 2, 3},\n" 2270 " {1, 2, 3},\n" 2271 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2272 " 333333333333333333333333333333},\n" 2273 " {1, 2, 3},\n" 2274 " {1, 2, 3}}};"); 2275 verifyFormat( 2276 "SomeArrayOfSomeType a = {\n" 2277 " {{1, 2, 3}},\n" 2278 " {{1, 2, 3}},\n" 2279 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2280 " 333333333333333333333333333333}},\n" 2281 " {{1, 2, 3}},\n" 2282 " {{1, 2, 3}}};"); 2283 2284 verifyFormat("struct {\n" 2285 " unsigned bit;\n" 2286 " const char *const name;\n" 2287 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2288 " {kOsWin, \"Windows\"},\n" 2289 " {kOsLinux, \"Linux\"},\n" 2290 " {kOsCrOS, \"Chrome OS\"}};"); 2291 verifyFormat("struct {\n" 2292 " unsigned bit;\n" 2293 " const char *const name;\n" 2294 "} kBitsToOs[] = {\n" 2295 " {kOsMac, \"Mac\"},\n" 2296 " {kOsWin, \"Windows\"},\n" 2297 " {kOsLinux, \"Linux\"},\n" 2298 " {kOsCrOS, \"Chrome OS\"},\n" 2299 "};"); 2300 } 2301 2302 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2303 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2304 " \\\n" 2305 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2306 } 2307 2308 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2309 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2310 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2311 2312 // Do break defaulted and deleted functions. 2313 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2314 " default;", 2315 getLLVMStyleWithColumns(40)); 2316 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2317 " delete;", 2318 getLLVMStyleWithColumns(40)); 2319 } 2320 2321 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2322 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2323 getLLVMStyleWithColumns(40)); 2324 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2325 getLLVMStyleWithColumns(40)); 2326 EXPECT_EQ("#define Q \\\n" 2327 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2328 " \"aaaaaaaa.cpp\"", 2329 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2330 getLLVMStyleWithColumns(40))); 2331 } 2332 2333 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2334 EXPECT_EQ("# 123 \"A string literal\"", 2335 format(" # 123 \"A string literal\"")); 2336 } 2337 2338 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2339 EXPECT_EQ("#;", format("#;")); 2340 verifyFormat("#\n;\n;\n;"); 2341 } 2342 2343 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2344 EXPECT_EQ("#line 42 \"test\"\n", 2345 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2346 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2347 getLLVMStyleWithColumns(12))); 2348 } 2349 2350 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2351 EXPECT_EQ("#line 42 \"test\"", 2352 format("# \\\n line \\\n 42 \\\n \"test\"")); 2353 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2354 } 2355 2356 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2357 verifyFormat("#define A \\x20"); 2358 verifyFormat("#define A \\ x20"); 2359 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2360 verifyFormat("#define A ''"); 2361 verifyFormat("#define A ''qqq"); 2362 verifyFormat("#define A `qqq"); 2363 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2364 EXPECT_EQ("const char *c = STRINGIFY(\n" 2365 "\\na : b);", 2366 format("const char * c = STRINGIFY(\n" 2367 "\\na : b);")); 2368 2369 verifyFormat("a\r\\"); 2370 verifyFormat("a\v\\"); 2371 verifyFormat("a\f\\"); 2372 } 2373 2374 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2375 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2376 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2377 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2378 // FIXME: We never break before the macro name. 2379 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2380 2381 verifyFormat("#define A A\n#define A A"); 2382 verifyFormat("#define A(X) A\n#define A A"); 2383 2384 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2385 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2386 } 2387 2388 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2389 EXPECT_EQ("// somecomment\n" 2390 "#include \"a.h\"\n" 2391 "#define A( \\\n" 2392 " A, B)\n" 2393 "#include \"b.h\"\n" 2394 "// somecomment\n", 2395 format(" // somecomment\n" 2396 " #include \"a.h\"\n" 2397 "#define A(A,\\\n" 2398 " B)\n" 2399 " #include \"b.h\"\n" 2400 " // somecomment\n", 2401 getLLVMStyleWithColumns(13))); 2402 } 2403 2404 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2405 2406 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2407 EXPECT_EQ("#define A \\\n" 2408 " c; \\\n" 2409 " e;\n" 2410 "f;", 2411 format("#define A c; e;\n" 2412 "f;", 2413 getLLVMStyleWithColumns(14))); 2414 } 2415 2416 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2417 2418 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2419 EXPECT_EQ("int x,\n" 2420 "#define A\n" 2421 " y;", 2422 format("int x,\n#define A\ny;")); 2423 } 2424 2425 TEST_F(FormatTest, HashInMacroDefinition) { 2426 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2427 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2428 verifyFormat("#define A \\\n" 2429 " { \\\n" 2430 " f(#c); \\\n" 2431 " }", 2432 getLLVMStyleWithColumns(11)); 2433 2434 verifyFormat("#define A(X) \\\n" 2435 " void function##X()", 2436 getLLVMStyleWithColumns(22)); 2437 2438 verifyFormat("#define A(a, b, c) \\\n" 2439 " void a##b##c()", 2440 getLLVMStyleWithColumns(22)); 2441 2442 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2443 } 2444 2445 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2446 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2447 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2448 } 2449 2450 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2451 EXPECT_EQ("#define A b;", format("#define A \\\n" 2452 " \\\n" 2453 " b;", 2454 getLLVMStyleWithColumns(25))); 2455 EXPECT_EQ("#define A \\\n" 2456 " \\\n" 2457 " a; \\\n" 2458 " b;", 2459 format("#define A \\\n" 2460 " \\\n" 2461 " a; \\\n" 2462 " b;", 2463 getLLVMStyleWithColumns(11))); 2464 EXPECT_EQ("#define A \\\n" 2465 " a; \\\n" 2466 " \\\n" 2467 " b;", 2468 format("#define A \\\n" 2469 " a; \\\n" 2470 " \\\n" 2471 " b;", 2472 getLLVMStyleWithColumns(11))); 2473 } 2474 2475 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2476 verifyIncompleteFormat("#define A :"); 2477 verifyFormat("#define SOMECASES \\\n" 2478 " case 1: \\\n" 2479 " case 2\n", 2480 getLLVMStyleWithColumns(20)); 2481 verifyFormat("#define MACRO(a) \\\n" 2482 " if (a) \\\n" 2483 " f(); \\\n" 2484 " else \\\n" 2485 " g()", 2486 getLLVMStyleWithColumns(18)); 2487 verifyFormat("#define A template <typename T>"); 2488 verifyIncompleteFormat("#define STR(x) #x\n" 2489 "f(STR(this_is_a_string_literal{));"); 2490 verifyFormat("#pragma omp threadprivate( \\\n" 2491 " y)), // expected-warning", 2492 getLLVMStyleWithColumns(28)); 2493 verifyFormat("#d, = };"); 2494 verifyFormat("#if \"a"); 2495 verifyIncompleteFormat("({\n" 2496 "#define b \\\n" 2497 " } \\\n" 2498 " a\n" 2499 "a", 2500 getLLVMStyleWithColumns(15)); 2501 verifyFormat("#define A \\\n" 2502 " { \\\n" 2503 " {\n" 2504 "#define B \\\n" 2505 " } \\\n" 2506 " }", 2507 getLLVMStyleWithColumns(15)); 2508 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2509 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2510 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2511 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2512 } 2513 2514 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2515 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2516 EXPECT_EQ("class A : public QObject {\n" 2517 " Q_OBJECT\n" 2518 "\n" 2519 " A() {}\n" 2520 "};", 2521 format("class A : public QObject {\n" 2522 " Q_OBJECT\n" 2523 "\n" 2524 " A() {\n}\n" 2525 "} ;")); 2526 EXPECT_EQ("MACRO\n" 2527 "/*static*/ int i;", 2528 format("MACRO\n" 2529 " /*static*/ int i;")); 2530 EXPECT_EQ("SOME_MACRO\n" 2531 "namespace {\n" 2532 "void f();\n" 2533 "} // namespace", 2534 format("SOME_MACRO\n" 2535 " namespace {\n" 2536 "void f( );\n" 2537 "} // namespace")); 2538 // Only if the identifier contains at least 5 characters. 2539 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2540 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2541 // Only if everything is upper case. 2542 EXPECT_EQ("class A : public QObject {\n" 2543 " Q_Object A() {}\n" 2544 "};", 2545 format("class A : public QObject {\n" 2546 " Q_Object\n" 2547 " A() {\n}\n" 2548 "} ;")); 2549 2550 // Only if the next line can actually start an unwrapped line. 2551 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2552 format("SOME_WEIRD_LOG_MACRO\n" 2553 "<< SomeThing;")); 2554 2555 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2556 "(n, buffers))\n", 2557 getChromiumStyle(FormatStyle::LK_Cpp)); 2558 } 2559 2560 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2561 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2562 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2563 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2564 "class X {};\n" 2565 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2566 "int *createScopDetectionPass() { return 0; }", 2567 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2568 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2569 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2570 " class X {};\n" 2571 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2572 " int *createScopDetectionPass() { return 0; }")); 2573 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2574 // braces, so that inner block is indented one level more. 2575 EXPECT_EQ("int q() {\n" 2576 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2577 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2578 " IPC_END_MESSAGE_MAP()\n" 2579 "}", 2580 format("int q() {\n" 2581 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2582 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2583 " IPC_END_MESSAGE_MAP()\n" 2584 "}")); 2585 2586 // Same inside macros. 2587 EXPECT_EQ("#define LIST(L) \\\n" 2588 " L(A) \\\n" 2589 " L(B) \\\n" 2590 " L(C)", 2591 format("#define LIST(L) \\\n" 2592 " L(A) \\\n" 2593 " L(B) \\\n" 2594 " L(C)", 2595 getGoogleStyle())); 2596 2597 // These must not be recognized as macros. 2598 EXPECT_EQ("int q() {\n" 2599 " f(x);\n" 2600 " f(x) {}\n" 2601 " f(x)->g();\n" 2602 " f(x)->*g();\n" 2603 " f(x).g();\n" 2604 " f(x) = x;\n" 2605 " f(x) += x;\n" 2606 " f(x) -= x;\n" 2607 " f(x) *= x;\n" 2608 " f(x) /= x;\n" 2609 " f(x) %= x;\n" 2610 " f(x) &= x;\n" 2611 " f(x) |= x;\n" 2612 " f(x) ^= x;\n" 2613 " f(x) >>= x;\n" 2614 " f(x) <<= x;\n" 2615 " f(x)[y].z();\n" 2616 " LOG(INFO) << x;\n" 2617 " ifstream(x) >> x;\n" 2618 "}\n", 2619 format("int q() {\n" 2620 " f(x)\n;\n" 2621 " f(x)\n {}\n" 2622 " f(x)\n->g();\n" 2623 " f(x)\n->*g();\n" 2624 " f(x)\n.g();\n" 2625 " f(x)\n = x;\n" 2626 " f(x)\n += x;\n" 2627 " f(x)\n -= x;\n" 2628 " f(x)\n *= x;\n" 2629 " f(x)\n /= x;\n" 2630 " f(x)\n %= x;\n" 2631 " f(x)\n &= x;\n" 2632 " f(x)\n |= x;\n" 2633 " f(x)\n ^= x;\n" 2634 " f(x)\n >>= x;\n" 2635 " f(x)\n <<= x;\n" 2636 " f(x)\n[y].z();\n" 2637 " LOG(INFO)\n << x;\n" 2638 " ifstream(x)\n >> x;\n" 2639 "}\n")); 2640 EXPECT_EQ("int q() {\n" 2641 " F(x)\n" 2642 " if (1) {\n" 2643 " }\n" 2644 " F(x)\n" 2645 " while (1) {\n" 2646 " }\n" 2647 " F(x)\n" 2648 " G(x);\n" 2649 " F(x)\n" 2650 " try {\n" 2651 " Q();\n" 2652 " } catch (...) {\n" 2653 " }\n" 2654 "}\n", 2655 format("int q() {\n" 2656 "F(x)\n" 2657 "if (1) {}\n" 2658 "F(x)\n" 2659 "while (1) {}\n" 2660 "F(x)\n" 2661 "G(x);\n" 2662 "F(x)\n" 2663 "try { Q(); } catch (...) {}\n" 2664 "}\n")); 2665 EXPECT_EQ("class A {\n" 2666 " A() : t(0) {}\n" 2667 " A(int i) noexcept() : {}\n" 2668 " A(X x)\n" // FIXME: function-level try blocks are broken. 2669 " try : t(0) {\n" 2670 " } catch (...) {\n" 2671 " }\n" 2672 "};", 2673 format("class A {\n" 2674 " A()\n : t(0) {}\n" 2675 " A(int i)\n noexcept() : {}\n" 2676 " A(X x)\n" 2677 " try : t(0) {} catch (...) {}\n" 2678 "};")); 2679 FormatStyle Style = getLLVMStyle(); 2680 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2681 Style.BraceWrapping.AfterControlStatement = true; 2682 Style.BraceWrapping.AfterFunction = true; 2683 EXPECT_EQ("void f()\n" 2684 "try\n" 2685 "{\n" 2686 "}", 2687 format("void f() try {\n" 2688 "}", Style)); 2689 EXPECT_EQ("class SomeClass {\n" 2690 "public:\n" 2691 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2692 "};", 2693 format("class SomeClass {\n" 2694 "public:\n" 2695 " SomeClass()\n" 2696 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2697 "};")); 2698 EXPECT_EQ("class SomeClass {\n" 2699 "public:\n" 2700 " SomeClass()\n" 2701 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2702 "};", 2703 format("class SomeClass {\n" 2704 "public:\n" 2705 " SomeClass()\n" 2706 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2707 "};", 2708 getLLVMStyleWithColumns(40))); 2709 2710 verifyFormat("MACRO(>)"); 2711 2712 // Some macros contain an implicit semicolon. 2713 Style = getLLVMStyle(); 2714 Style.StatementMacros.push_back("FOO"); 2715 verifyFormat("FOO(a) int b = 0;"); 2716 verifyFormat("FOO(a)\n" 2717 "int b = 0;", 2718 Style); 2719 verifyFormat("FOO(a);\n" 2720 "int b = 0;", 2721 Style); 2722 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 2723 "int b = 0;", 2724 Style); 2725 verifyFormat("FOO()\n" 2726 "int b = 0;", 2727 Style); 2728 verifyFormat("FOO\n" 2729 "int b = 0;", 2730 Style); 2731 verifyFormat("void f() {\n" 2732 " FOO(a)\n" 2733 " return a;\n" 2734 "}", 2735 Style); 2736 verifyFormat("FOO(a)\n" 2737 "FOO(b)", 2738 Style); 2739 verifyFormat("int a = 0;\n" 2740 "FOO(b)\n" 2741 "int c = 0;", 2742 Style); 2743 verifyFormat("int a = 0;\n" 2744 "int x = FOO(a)\n" 2745 "int b = 0;", 2746 Style); 2747 verifyFormat("void foo(int a) { FOO(a) }\n" 2748 "uint32_t bar() {}", 2749 Style); 2750 } 2751 2752 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2753 verifyFormat("#define A \\\n" 2754 " f({ \\\n" 2755 " g(); \\\n" 2756 " });", 2757 getLLVMStyleWithColumns(11)); 2758 } 2759 2760 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2761 FormatStyle Style = getLLVMStyle(); 2762 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2763 Style.ColumnLimit = 40; 2764 verifyFormat("#ifdef _WIN32\n" 2765 "#define A 0\n" 2766 "#ifdef VAR2\n" 2767 "#define B 1\n" 2768 "#include <someheader.h>\n" 2769 "#define MACRO \\\n" 2770 " some_very_long_func_aaaaaaaaaa();\n" 2771 "#endif\n" 2772 "#else\n" 2773 "#define A 1\n" 2774 "#endif", 2775 Style); 2776 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2777 verifyFormat("#ifdef _WIN32\n" 2778 "# define A 0\n" 2779 "# ifdef VAR2\n" 2780 "# define B 1\n" 2781 "# include <someheader.h>\n" 2782 "# define MACRO \\\n" 2783 " some_very_long_func_aaaaaaaaaa();\n" 2784 "# endif\n" 2785 "#else\n" 2786 "# define A 1\n" 2787 "#endif", 2788 Style); 2789 verifyFormat("#if A\n" 2790 "# define MACRO \\\n" 2791 " void a(int x) { \\\n" 2792 " b(); \\\n" 2793 " c(); \\\n" 2794 " d(); \\\n" 2795 " e(); \\\n" 2796 " f(); \\\n" 2797 " }\n" 2798 "#endif", 2799 Style); 2800 // Comments before include guard. 2801 verifyFormat("// file comment\n" 2802 "// file comment\n" 2803 "#ifndef HEADER_H\n" 2804 "#define HEADER_H\n" 2805 "code();\n" 2806 "#endif", 2807 Style); 2808 // Test with include guards. 2809 verifyFormat("#ifndef HEADER_H\n" 2810 "#define HEADER_H\n" 2811 "code();\n" 2812 "#endif", 2813 Style); 2814 // Include guards must have a #define with the same variable immediately 2815 // after #ifndef. 2816 verifyFormat("#ifndef NOT_GUARD\n" 2817 "# define FOO\n" 2818 "code();\n" 2819 "#endif", 2820 Style); 2821 2822 // Include guards must cover the entire file. 2823 verifyFormat("code();\n" 2824 "code();\n" 2825 "#ifndef NOT_GUARD\n" 2826 "# define NOT_GUARD\n" 2827 "code();\n" 2828 "#endif", 2829 Style); 2830 verifyFormat("#ifndef NOT_GUARD\n" 2831 "# define NOT_GUARD\n" 2832 "code();\n" 2833 "#endif\n" 2834 "code();", 2835 Style); 2836 // Test with trailing blank lines. 2837 verifyFormat("#ifndef HEADER_H\n" 2838 "#define HEADER_H\n" 2839 "code();\n" 2840 "#endif\n", 2841 Style); 2842 // Include guards don't have #else. 2843 verifyFormat("#ifndef NOT_GUARD\n" 2844 "# define NOT_GUARD\n" 2845 "code();\n" 2846 "#else\n" 2847 "#endif", 2848 Style); 2849 verifyFormat("#ifndef NOT_GUARD\n" 2850 "# define NOT_GUARD\n" 2851 "code();\n" 2852 "#elif FOO\n" 2853 "#endif", 2854 Style); 2855 // Non-identifier #define after potential include guard. 2856 verifyFormat("#ifndef FOO\n" 2857 "# define 1\n" 2858 "#endif\n", 2859 Style); 2860 // #if closes past last non-preprocessor line. 2861 verifyFormat("#ifndef FOO\n" 2862 "#define FOO\n" 2863 "#if 1\n" 2864 "int i;\n" 2865 "# define A 0\n" 2866 "#endif\n" 2867 "#endif\n", 2868 Style); 2869 // FIXME: This doesn't handle the case where there's code between the 2870 // #ifndef and #define but all other conditions hold. This is because when 2871 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 2872 // previous code line yet, so we can't detect it. 2873 EXPECT_EQ("#ifndef NOT_GUARD\n" 2874 "code();\n" 2875 "#define NOT_GUARD\n" 2876 "code();\n" 2877 "#endif", 2878 format("#ifndef NOT_GUARD\n" 2879 "code();\n" 2880 "# define NOT_GUARD\n" 2881 "code();\n" 2882 "#endif", 2883 Style)); 2884 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 2885 // be outside an include guard. Examples are #pragma once and 2886 // #pragma GCC diagnostic, or anything else that does not change the meaning 2887 // of the file if it's included multiple times. 2888 EXPECT_EQ("#ifdef WIN32\n" 2889 "# pragma once\n" 2890 "#endif\n" 2891 "#ifndef HEADER_H\n" 2892 "# define HEADER_H\n" 2893 "code();\n" 2894 "#endif", 2895 format("#ifdef WIN32\n" 2896 "# pragma once\n" 2897 "#endif\n" 2898 "#ifndef HEADER_H\n" 2899 "#define HEADER_H\n" 2900 "code();\n" 2901 "#endif", 2902 Style)); 2903 // FIXME: This does not detect when there is a single non-preprocessor line 2904 // in front of an include-guard-like structure where other conditions hold 2905 // because ScopedLineState hides the line. 2906 EXPECT_EQ("code();\n" 2907 "#ifndef HEADER_H\n" 2908 "#define HEADER_H\n" 2909 "code();\n" 2910 "#endif", 2911 format("code();\n" 2912 "#ifndef HEADER_H\n" 2913 "# define HEADER_H\n" 2914 "code();\n" 2915 "#endif", 2916 Style)); 2917 // Keep comments aligned with #, otherwise indent comments normally. These 2918 // tests cannot use verifyFormat because messUp manipulates leading 2919 // whitespace. 2920 { 2921 const char *Expected = "" 2922 "void f() {\n" 2923 "#if 1\n" 2924 "// Preprocessor aligned.\n" 2925 "# define A 0\n" 2926 " // Code. Separated by blank line.\n" 2927 "\n" 2928 "# define B 0\n" 2929 " // Code. Not aligned with #\n" 2930 "# define C 0\n" 2931 "#endif"; 2932 const char *ToFormat = "" 2933 "void f() {\n" 2934 "#if 1\n" 2935 "// Preprocessor aligned.\n" 2936 "# define A 0\n" 2937 "// Code. Separated by blank line.\n" 2938 "\n" 2939 "# define B 0\n" 2940 " // Code. Not aligned with #\n" 2941 "# define C 0\n" 2942 "#endif"; 2943 EXPECT_EQ(Expected, format(ToFormat, Style)); 2944 EXPECT_EQ(Expected, format(Expected, Style)); 2945 } 2946 // Keep block quotes aligned. 2947 { 2948 const char *Expected = "" 2949 "void f() {\n" 2950 "#if 1\n" 2951 "/* Preprocessor aligned. */\n" 2952 "# define A 0\n" 2953 " /* Code. Separated by blank line. */\n" 2954 "\n" 2955 "# define B 0\n" 2956 " /* Code. Not aligned with # */\n" 2957 "# define C 0\n" 2958 "#endif"; 2959 const char *ToFormat = "" 2960 "void f() {\n" 2961 "#if 1\n" 2962 "/* Preprocessor aligned. */\n" 2963 "# define A 0\n" 2964 "/* Code. Separated by blank line. */\n" 2965 "\n" 2966 "# define B 0\n" 2967 " /* Code. Not aligned with # */\n" 2968 "# define C 0\n" 2969 "#endif"; 2970 EXPECT_EQ(Expected, format(ToFormat, Style)); 2971 EXPECT_EQ(Expected, format(Expected, Style)); 2972 } 2973 // Keep comments aligned with un-indented directives. 2974 { 2975 const char *Expected = "" 2976 "void f() {\n" 2977 "// Preprocessor aligned.\n" 2978 "#define A 0\n" 2979 " // Code. Separated by blank line.\n" 2980 "\n" 2981 "#define B 0\n" 2982 " // Code. Not aligned with #\n" 2983 "#define C 0\n"; 2984 const char *ToFormat = "" 2985 "void f() {\n" 2986 "// Preprocessor aligned.\n" 2987 "#define A 0\n" 2988 "// Code. Separated by blank line.\n" 2989 "\n" 2990 "#define B 0\n" 2991 " // Code. Not aligned with #\n" 2992 "#define C 0\n"; 2993 EXPECT_EQ(Expected, format(ToFormat, Style)); 2994 EXPECT_EQ(Expected, format(Expected, Style)); 2995 } 2996 // Test AfterHash with tabs. 2997 { 2998 FormatStyle Tabbed = Style; 2999 Tabbed.UseTab = FormatStyle::UT_Always; 3000 Tabbed.IndentWidth = 8; 3001 Tabbed.TabWidth = 8; 3002 verifyFormat("#ifdef _WIN32\n" 3003 "#\tdefine A 0\n" 3004 "#\tifdef VAR2\n" 3005 "#\t\tdefine B 1\n" 3006 "#\t\tinclude <someheader.h>\n" 3007 "#\t\tdefine MACRO \\\n" 3008 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 3009 "#\tendif\n" 3010 "#else\n" 3011 "#\tdefine A 1\n" 3012 "#endif", 3013 Tabbed); 3014 } 3015 3016 // Regression test: Multiline-macro inside include guards. 3017 verifyFormat("#ifndef HEADER_H\n" 3018 "#define HEADER_H\n" 3019 "#define A() \\\n" 3020 " int i; \\\n" 3021 " int j;\n" 3022 "#endif // HEADER_H", 3023 getLLVMStyleWithColumns(20)); 3024 3025 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 3026 // Basic before hash indent tests 3027 verifyFormat("#ifdef _WIN32\n" 3028 " #define A 0\n" 3029 " #ifdef VAR2\n" 3030 " #define B 1\n" 3031 " #include <someheader.h>\n" 3032 " #define MACRO \\\n" 3033 " some_very_long_func_aaaaaaaaaa();\n" 3034 " #endif\n" 3035 "#else\n" 3036 " #define A 1\n" 3037 "#endif", 3038 Style); 3039 verifyFormat("#if A\n" 3040 " #define MACRO \\\n" 3041 " void a(int x) { \\\n" 3042 " b(); \\\n" 3043 " c(); \\\n" 3044 " d(); \\\n" 3045 " e(); \\\n" 3046 " f(); \\\n" 3047 " }\n" 3048 "#endif", 3049 Style); 3050 // Keep comments aligned with indented directives. These 3051 // tests cannot use verifyFormat because messUp manipulates leading 3052 // whitespace. 3053 { 3054 const char *Expected = "void f() {\n" 3055 "// Aligned to preprocessor.\n" 3056 "#if 1\n" 3057 " // Aligned to code.\n" 3058 " int a;\n" 3059 " #if 1\n" 3060 " // Aligned to preprocessor.\n" 3061 " #define A 0\n" 3062 " // Aligned to code.\n" 3063 " int b;\n" 3064 " #endif\n" 3065 "#endif\n" 3066 "}"; 3067 const char *ToFormat = "void f() {\n" 3068 "// Aligned to preprocessor.\n" 3069 "#if 1\n" 3070 "// Aligned to code.\n" 3071 "int a;\n" 3072 "#if 1\n" 3073 "// Aligned to preprocessor.\n" 3074 "#define A 0\n" 3075 "// Aligned to code.\n" 3076 "int b;\n" 3077 "#endif\n" 3078 "#endif\n" 3079 "}"; 3080 EXPECT_EQ(Expected, format(ToFormat, Style)); 3081 EXPECT_EQ(Expected, format(Expected, Style)); 3082 } 3083 { 3084 const char *Expected = "void f() {\n" 3085 "/* Aligned to preprocessor. */\n" 3086 "#if 1\n" 3087 " /* Aligned to code. */\n" 3088 " int a;\n" 3089 " #if 1\n" 3090 " /* Aligned to preprocessor. */\n" 3091 " #define A 0\n" 3092 " /* Aligned to code. */\n" 3093 " int b;\n" 3094 " #endif\n" 3095 "#endif\n" 3096 "}"; 3097 const char *ToFormat = "void f() {\n" 3098 "/* Aligned to preprocessor. */\n" 3099 "#if 1\n" 3100 "/* Aligned to code. */\n" 3101 "int a;\n" 3102 "#if 1\n" 3103 "/* Aligned to preprocessor. */\n" 3104 "#define A 0\n" 3105 "/* Aligned to code. */\n" 3106 "int b;\n" 3107 "#endif\n" 3108 "#endif\n" 3109 "}"; 3110 EXPECT_EQ(Expected, format(ToFormat, Style)); 3111 EXPECT_EQ(Expected, format(Expected, Style)); 3112 } 3113 3114 // Test single comment before preprocessor 3115 verifyFormat("// Comment\n" 3116 "\n" 3117 "#if 1\n" 3118 "#endif", 3119 Style); 3120 } 3121 3122 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3123 verifyFormat("{\n { a #c; }\n}"); 3124 } 3125 3126 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3127 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3128 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3129 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3130 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3131 } 3132 3133 TEST_F(FormatTest, EscapedNewlines) { 3134 FormatStyle Narrow = getLLVMStyleWithColumns(11); 3135 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 3136 format("#define A \\\nint i;\\\n int j;", Narrow)); 3137 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3138 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3139 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 3140 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3141 3142 FormatStyle AlignLeft = getLLVMStyle(); 3143 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 3144 EXPECT_EQ("#define MACRO(x) \\\n" 3145 "private: \\\n" 3146 " int x(int a);\n", 3147 format("#define MACRO(x) \\\n" 3148 "private: \\\n" 3149 " int x(int a);\n", 3150 AlignLeft)); 3151 3152 // CRLF line endings 3153 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 3154 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 3155 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 3156 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3157 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 3158 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 3159 EXPECT_EQ("#define MACRO(x) \\\r\n" 3160 "private: \\\r\n" 3161 " int x(int a);\r\n", 3162 format("#define MACRO(x) \\\r\n" 3163 "private: \\\r\n" 3164 " int x(int a);\r\n", 3165 AlignLeft)); 3166 3167 FormatStyle DontAlign = getLLVMStyle(); 3168 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 3169 DontAlign.MaxEmptyLinesToKeep = 3; 3170 // FIXME: can't use verifyFormat here because the newline before 3171 // "public:" is not inserted the first time it's reformatted 3172 EXPECT_EQ("#define A \\\n" 3173 " class Foo { \\\n" 3174 " void bar(); \\\n" 3175 "\\\n" 3176 "\\\n" 3177 "\\\n" 3178 " public: \\\n" 3179 " void baz(); \\\n" 3180 " };", 3181 format("#define A \\\n" 3182 " class Foo { \\\n" 3183 " void bar(); \\\n" 3184 "\\\n" 3185 "\\\n" 3186 "\\\n" 3187 " public: \\\n" 3188 " void baz(); \\\n" 3189 " };", 3190 DontAlign)); 3191 } 3192 3193 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3194 verifyFormat("#define A \\\n" 3195 " int v( \\\n" 3196 " a); \\\n" 3197 " int i;", 3198 getLLVMStyleWithColumns(11)); 3199 } 3200 3201 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3202 EXPECT_EQ( 3203 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3204 " \\\n" 3205 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3206 "\n" 3207 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3208 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3209 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3210 "\\\n" 3211 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3212 " \n" 3213 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3214 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3215 } 3216 3217 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3218 EXPECT_EQ("int\n" 3219 "#define A\n" 3220 " a;", 3221 format("int\n#define A\na;")); 3222 verifyFormat("functionCallTo(\n" 3223 " someOtherFunction(\n" 3224 " withSomeParameters, whichInSequence,\n" 3225 " areLongerThanALine(andAnotherCall,\n" 3226 "#define A B\n" 3227 " withMoreParamters,\n" 3228 " whichStronglyInfluenceTheLayout),\n" 3229 " andMoreParameters),\n" 3230 " trailing);", 3231 getLLVMStyleWithColumns(69)); 3232 verifyFormat("Foo::Foo()\n" 3233 "#ifdef BAR\n" 3234 " : baz(0)\n" 3235 "#endif\n" 3236 "{\n" 3237 "}"); 3238 verifyFormat("void f() {\n" 3239 " if (true)\n" 3240 "#ifdef A\n" 3241 " f(42);\n" 3242 " x();\n" 3243 "#else\n" 3244 " g();\n" 3245 " x();\n" 3246 "#endif\n" 3247 "}"); 3248 verifyFormat("void f(param1, param2,\n" 3249 " param3,\n" 3250 "#ifdef A\n" 3251 " param4(param5,\n" 3252 "#ifdef A1\n" 3253 " param6,\n" 3254 "#ifdef A2\n" 3255 " param7),\n" 3256 "#else\n" 3257 " param8),\n" 3258 " param9,\n" 3259 "#endif\n" 3260 " param10,\n" 3261 "#endif\n" 3262 " param11)\n" 3263 "#else\n" 3264 " param12)\n" 3265 "#endif\n" 3266 "{\n" 3267 " x();\n" 3268 "}", 3269 getLLVMStyleWithColumns(28)); 3270 verifyFormat("#if 1\n" 3271 "int i;"); 3272 verifyFormat("#if 1\n" 3273 "#endif\n" 3274 "#if 1\n" 3275 "#else\n" 3276 "#endif\n"); 3277 verifyFormat("DEBUG({\n" 3278 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3280 "});\n" 3281 "#if a\n" 3282 "#else\n" 3283 "#endif"); 3284 3285 verifyIncompleteFormat("void f(\n" 3286 "#if A\n" 3287 ");\n" 3288 "#else\n" 3289 "#endif"); 3290 } 3291 3292 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3293 verifyFormat("#endif\n" 3294 "#if B"); 3295 } 3296 3297 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3298 FormatStyle SingleLine = getLLVMStyle(); 3299 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 3300 verifyFormat("#if 0\n" 3301 "#elif 1\n" 3302 "#endif\n" 3303 "void foo() {\n" 3304 " if (test) foo2();\n" 3305 "}", 3306 SingleLine); 3307 } 3308 3309 TEST_F(FormatTest, LayoutBlockInsideParens) { 3310 verifyFormat("functionCall({ int i; });"); 3311 verifyFormat("functionCall({\n" 3312 " int i;\n" 3313 " int j;\n" 3314 "});"); 3315 verifyFormat("functionCall(\n" 3316 " {\n" 3317 " int i;\n" 3318 " int j;\n" 3319 " },\n" 3320 " aaaa, bbbb, cccc);"); 3321 verifyFormat("functionA(functionB({\n" 3322 " int i;\n" 3323 " int j;\n" 3324 " }),\n" 3325 " aaaa, bbbb, cccc);"); 3326 verifyFormat("functionCall(\n" 3327 " {\n" 3328 " int i;\n" 3329 " int j;\n" 3330 " },\n" 3331 " aaaa, bbbb, // comment\n" 3332 " cccc);"); 3333 verifyFormat("functionA(functionB({\n" 3334 " int i;\n" 3335 " int j;\n" 3336 " }),\n" 3337 " aaaa, bbbb, // comment\n" 3338 " cccc);"); 3339 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3340 verifyFormat("functionCall(aaaa, bbbb, {\n" 3341 " int i;\n" 3342 " int j;\n" 3343 "});"); 3344 verifyFormat( 3345 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3346 " {\n" 3347 " int i; // break\n" 3348 " },\n" 3349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3350 " ccccccccccccccccc));"); 3351 verifyFormat("DEBUG({\n" 3352 " if (a)\n" 3353 " f();\n" 3354 "});"); 3355 } 3356 3357 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3358 EXPECT_EQ("SOME_MACRO { int i; }\n" 3359 "int i;", 3360 format(" SOME_MACRO {int i;} int i;")); 3361 } 3362 3363 TEST_F(FormatTest, LayoutNestedBlocks) { 3364 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3365 " struct s {\n" 3366 " int i;\n" 3367 " };\n" 3368 " s kBitsToOs[] = {{10}};\n" 3369 " for (int i = 0; i < 10; ++i)\n" 3370 " return;\n" 3371 "}"); 3372 verifyFormat("call(parameter, {\n" 3373 " something();\n" 3374 " // Comment using all columns.\n" 3375 " somethingelse();\n" 3376 "});", 3377 getLLVMStyleWithColumns(40)); 3378 verifyFormat("DEBUG( //\n" 3379 " { f(); }, a);"); 3380 verifyFormat("DEBUG( //\n" 3381 " {\n" 3382 " f(); //\n" 3383 " },\n" 3384 " a);"); 3385 3386 EXPECT_EQ("call(parameter, {\n" 3387 " something();\n" 3388 " // Comment too\n" 3389 " // looooooooooong.\n" 3390 " somethingElse();\n" 3391 "});", 3392 format("call(parameter, {\n" 3393 " something();\n" 3394 " // Comment too looooooooooong.\n" 3395 " somethingElse();\n" 3396 "});", 3397 getLLVMStyleWithColumns(29))); 3398 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3399 EXPECT_EQ("DEBUG({ // comment\n" 3400 " int i;\n" 3401 "});", 3402 format("DEBUG({ // comment\n" 3403 "int i;\n" 3404 "});")); 3405 EXPECT_EQ("DEBUG({\n" 3406 " int i;\n" 3407 "\n" 3408 " // comment\n" 3409 " int j;\n" 3410 "});", 3411 format("DEBUG({\n" 3412 " int i;\n" 3413 "\n" 3414 " // comment\n" 3415 " int j;\n" 3416 "});")); 3417 3418 verifyFormat("DEBUG({\n" 3419 " if (a)\n" 3420 " return;\n" 3421 "});"); 3422 verifyGoogleFormat("DEBUG({\n" 3423 " if (a) return;\n" 3424 "});"); 3425 FormatStyle Style = getGoogleStyle(); 3426 Style.ColumnLimit = 45; 3427 verifyFormat("Debug(\n" 3428 " aaaaa,\n" 3429 " {\n" 3430 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3431 " },\n" 3432 " a);", 3433 Style); 3434 3435 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3436 3437 verifyNoCrash("^{v^{a}}"); 3438 } 3439 3440 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3441 EXPECT_EQ("#define MACRO() \\\n" 3442 " Debug(aaa, /* force line break */ \\\n" 3443 " { \\\n" 3444 " int i; \\\n" 3445 " int j; \\\n" 3446 " })", 3447 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3448 " { int i; int j; })", 3449 getGoogleStyle())); 3450 3451 EXPECT_EQ("#define A \\\n" 3452 " [] { \\\n" 3453 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3454 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3455 " }", 3456 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3457 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3458 getGoogleStyle())); 3459 } 3460 3461 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3462 EXPECT_EQ("{}", format("{}")); 3463 verifyFormat("enum E {};"); 3464 verifyFormat("enum E {}"); 3465 } 3466 3467 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3468 FormatStyle Style = getLLVMStyle(); 3469 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3470 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3471 verifyFormat("FOO_BEGIN\n" 3472 " FOO_ENTRY\n" 3473 "FOO_END", Style); 3474 verifyFormat("FOO_BEGIN\n" 3475 " NESTED_FOO_BEGIN\n" 3476 " NESTED_FOO_ENTRY\n" 3477 " NESTED_FOO_END\n" 3478 "FOO_END", Style); 3479 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3480 " int x;\n" 3481 " x = 1;\n" 3482 "FOO_END(Baz)", Style); 3483 } 3484 3485 //===----------------------------------------------------------------------===// 3486 // Line break tests. 3487 //===----------------------------------------------------------------------===// 3488 3489 TEST_F(FormatTest, PreventConfusingIndents) { 3490 verifyFormat( 3491 "void f() {\n" 3492 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3493 " parameter, parameter, parameter)),\n" 3494 " SecondLongCall(parameter));\n" 3495 "}"); 3496 verifyFormat( 3497 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3498 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3499 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3500 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3501 verifyFormat( 3502 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3503 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3504 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3505 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3506 verifyFormat( 3507 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3508 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3510 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3511 verifyFormat("int a = bbbb && ccc &&\n" 3512 " fffff(\n" 3513 "#define A Just forcing a new line\n" 3514 " ddd);"); 3515 } 3516 3517 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3518 verifyFormat( 3519 "bool aaaaaaa =\n" 3520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3521 " bbbbbbbb();"); 3522 verifyFormat( 3523 "bool aaaaaaa =\n" 3524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3525 " bbbbbbbb();"); 3526 3527 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3528 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3529 " ccccccccc == ddddddddddd;"); 3530 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3532 " ccccccccc == ddddddddddd;"); 3533 verifyFormat( 3534 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3536 " ccccccccc == ddddddddddd;"); 3537 3538 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3539 " aaaaaa) &&\n" 3540 " bbbbbb && cccccc;"); 3541 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3542 " aaaaaa) >>\n" 3543 " bbbbbb;"); 3544 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3545 " SourceMgr.getSpellingColumnNumber(\n" 3546 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3547 " 1);"); 3548 3549 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3550 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3551 " cccccc) {\n}"); 3552 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3553 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3554 " cccccc) {\n}"); 3555 verifyFormat("b = a &&\n" 3556 " // Comment\n" 3557 " b.c && d;"); 3558 3559 // If the LHS of a comparison is not a binary expression itself, the 3560 // additional linebreak confuses many people. 3561 verifyFormat( 3562 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3564 "}"); 3565 verifyFormat( 3566 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3568 "}"); 3569 verifyFormat( 3570 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3572 "}"); 3573 verifyFormat( 3574 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3576 "}"); 3577 // Even explicit parentheses stress the precedence enough to make the 3578 // additional break unnecessary. 3579 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3581 "}"); 3582 // This cases is borderline, but with the indentation it is still readable. 3583 verifyFormat( 3584 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3585 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3587 "}", 3588 getLLVMStyleWithColumns(75)); 3589 3590 // If the LHS is a binary expression, we should still use the additional break 3591 // as otherwise the formatting hides the operator precedence. 3592 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3594 " 5) {\n" 3595 "}"); 3596 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3598 " 5) {\n" 3599 "}"); 3600 3601 FormatStyle OnePerLine = getLLVMStyle(); 3602 OnePerLine.BinPackParameters = false; 3603 verifyFormat( 3604 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3606 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3607 OnePerLine); 3608 3609 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3610 " .aaa(aaaaaaaaaaaaa) *\n" 3611 " aaaaaaa +\n" 3612 " aaaaaaa;", 3613 getLLVMStyleWithColumns(40)); 3614 } 3615 3616 TEST_F(FormatTest, ExpressionIndentation) { 3617 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3618 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3619 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3621 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3622 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3625 " ccccccccccccccccccccccccccccccccccccccccc;"); 3626 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3629 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3630 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3633 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3634 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3635 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3637 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3638 verifyFormat("if () {\n" 3639 "} else if (aaaaa && bbbbb > // break\n" 3640 " ccccc) {\n" 3641 "}"); 3642 verifyFormat("if () {\n" 3643 "} else if (aaaaa &&\n" 3644 " bbbbb > // break\n" 3645 " ccccc &&\n" 3646 " ddddd) {\n" 3647 "}"); 3648 3649 // Presence of a trailing comment used to change indentation of b. 3650 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3651 " b;\n" 3652 "return aaaaaaaaaaaaaaaaaaa +\n" 3653 " b; //", 3654 getLLVMStyleWithColumns(30)); 3655 } 3656 3657 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3658 // Not sure what the best system is here. Like this, the LHS can be found 3659 // immediately above an operator (everything with the same or a higher 3660 // indent). The RHS is aligned right of the operator and so compasses 3661 // everything until something with the same indent as the operator is found. 3662 // FIXME: Is this a good system? 3663 FormatStyle Style = getLLVMStyle(); 3664 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3665 verifyFormat( 3666 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3667 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3668 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3669 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3670 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3671 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3672 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3673 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3674 " > ccccccccccccccccccccccccccccccccccccccccc;", 3675 Style); 3676 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3677 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3678 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3679 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3680 Style); 3681 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3682 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3683 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3684 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3685 Style); 3686 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3687 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3688 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3689 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3690 Style); 3691 verifyFormat("if () {\n" 3692 "} else if (aaaaa\n" 3693 " && bbbbb // break\n" 3694 " > ccccc) {\n" 3695 "}", 3696 Style); 3697 verifyFormat("return (a)\n" 3698 " // comment\n" 3699 " + b;", 3700 Style); 3701 verifyFormat( 3702 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3703 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3704 " + cc;", 3705 Style); 3706 3707 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3708 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3709 Style); 3710 3711 // Forced by comments. 3712 verifyFormat( 3713 "unsigned ContentSize =\n" 3714 " sizeof(int16_t) // DWARF ARange version number\n" 3715 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3716 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3717 " + sizeof(int8_t); // Segment Size (in bytes)"); 3718 3719 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3720 " == boost::fusion::at_c<1>(iiii).second;", 3721 Style); 3722 3723 Style.ColumnLimit = 60; 3724 verifyFormat("zzzzzzzzzz\n" 3725 " = bbbbbbbbbbbbbbbbb\n" 3726 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3727 Style); 3728 3729 Style.ColumnLimit = 80; 3730 Style.IndentWidth = 4; 3731 Style.TabWidth = 4; 3732 Style.UseTab = FormatStyle::UT_Always; 3733 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3734 Style.AlignOperands = false; 3735 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 3736 "\t&& (someOtherLongishConditionPart1\n" 3737 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 3738 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);", 3739 Style)); 3740 } 3741 3742 TEST_F(FormatTest, EnforcedOperatorWraps) { 3743 // Here we'd like to wrap after the || operators, but a comment is forcing an 3744 // earlier wrap. 3745 verifyFormat("bool x = aaaaa //\n" 3746 " || bbbbb\n" 3747 " //\n" 3748 " || cccc;"); 3749 } 3750 3751 TEST_F(FormatTest, NoOperandAlignment) { 3752 FormatStyle Style = getLLVMStyle(); 3753 Style.AlignOperands = false; 3754 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3755 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3757 Style); 3758 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3759 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3760 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3761 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3762 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3763 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3764 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3765 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3766 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3767 " > ccccccccccccccccccccccccccccccccccccccccc;", 3768 Style); 3769 3770 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3771 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3772 " + cc;", 3773 Style); 3774 verifyFormat("int a = aa\n" 3775 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3776 " * cccccccccccccccccccccccccccccccccccc;\n", 3777 Style); 3778 3779 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3780 verifyFormat("return (a > b\n" 3781 " // comment1\n" 3782 " // comment2\n" 3783 " || c);", 3784 Style); 3785 } 3786 3787 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3788 FormatStyle Style = getLLVMStyle(); 3789 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3790 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3792 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3793 Style); 3794 } 3795 3796 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3797 FormatStyle Style = getLLVMStyle(); 3798 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3799 Style.BinPackArguments = false; 3800 Style.ColumnLimit = 40; 3801 verifyFormat("void test() {\n" 3802 " someFunction(\n" 3803 " this + argument + is + quite\n" 3804 " + long + so + it + gets + wrapped\n" 3805 " + but + remains + bin - packed);\n" 3806 "}", 3807 Style); 3808 verifyFormat("void test() {\n" 3809 " someFunction(arg1,\n" 3810 " this + argument + is\n" 3811 " + quite + long + so\n" 3812 " + it + gets + wrapped\n" 3813 " + but + remains + bin\n" 3814 " - packed,\n" 3815 " arg3);\n" 3816 "}", 3817 Style); 3818 verifyFormat("void test() {\n" 3819 " someFunction(\n" 3820 " arg1,\n" 3821 " this + argument + has\n" 3822 " + anotherFunc(nested,\n" 3823 " calls + whose\n" 3824 " + arguments\n" 3825 " + are + also\n" 3826 " + wrapped,\n" 3827 " in + addition)\n" 3828 " + to + being + bin - packed,\n" 3829 " arg3);\n" 3830 "}", 3831 Style); 3832 3833 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3834 verifyFormat("void test() {\n" 3835 " someFunction(\n" 3836 " arg1,\n" 3837 " this + argument + has +\n" 3838 " anotherFunc(nested,\n" 3839 " calls + whose +\n" 3840 " arguments +\n" 3841 " are + also +\n" 3842 " wrapped,\n" 3843 " in + addition) +\n" 3844 " to + being + bin - packed,\n" 3845 " arg3);\n" 3846 "}", 3847 Style); 3848 } 3849 3850 TEST_F(FormatTest, ConstructorInitializers) { 3851 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3852 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3853 getLLVMStyleWithColumns(45)); 3854 verifyFormat("Constructor()\n" 3855 " : Inttializer(FitsOnTheLine) {}", 3856 getLLVMStyleWithColumns(44)); 3857 verifyFormat("Constructor()\n" 3858 " : Inttializer(FitsOnTheLine) {}", 3859 getLLVMStyleWithColumns(43)); 3860 3861 verifyFormat("template <typename T>\n" 3862 "Constructor() : Initializer(FitsOnTheLine) {}", 3863 getLLVMStyleWithColumns(45)); 3864 3865 verifyFormat( 3866 "SomeClass::Constructor()\n" 3867 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3868 3869 verifyFormat( 3870 "SomeClass::Constructor()\n" 3871 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3872 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3873 verifyFormat( 3874 "SomeClass::Constructor()\n" 3875 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3876 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3877 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3878 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3879 " : aaaaaaaaaa(aaaaaa) {}"); 3880 3881 verifyFormat("Constructor()\n" 3882 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3883 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3884 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3885 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3886 3887 verifyFormat("Constructor()\n" 3888 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3890 3891 verifyFormat("Constructor(int Parameter = 0)\n" 3892 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3893 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3894 verifyFormat("Constructor()\n" 3895 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3896 "}", 3897 getLLVMStyleWithColumns(60)); 3898 verifyFormat("Constructor()\n" 3899 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3900 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3901 3902 // Here a line could be saved by splitting the second initializer onto two 3903 // lines, but that is not desirable. 3904 verifyFormat("Constructor()\n" 3905 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3906 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3907 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3908 3909 FormatStyle OnePerLine = getLLVMStyle(); 3910 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3911 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3912 verifyFormat("SomeClass::Constructor()\n" 3913 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3914 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3915 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3916 OnePerLine); 3917 verifyFormat("SomeClass::Constructor()\n" 3918 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3919 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3920 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3921 OnePerLine); 3922 verifyFormat("MyClass::MyClass(int var)\n" 3923 " : some_var_(var), // 4 space indent\n" 3924 " some_other_var_(var + 1) { // lined up\n" 3925 "}", 3926 OnePerLine); 3927 verifyFormat("Constructor()\n" 3928 " : aaaaa(aaaaaa),\n" 3929 " aaaaa(aaaaaa),\n" 3930 " aaaaa(aaaaaa),\n" 3931 " aaaaa(aaaaaa),\n" 3932 " aaaaa(aaaaaa) {}", 3933 OnePerLine); 3934 verifyFormat("Constructor()\n" 3935 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3936 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3937 OnePerLine); 3938 OnePerLine.BinPackParameters = false; 3939 verifyFormat( 3940 "Constructor()\n" 3941 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3942 " aaaaaaaaaaa().aaa(),\n" 3943 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3944 OnePerLine); 3945 OnePerLine.ColumnLimit = 60; 3946 verifyFormat("Constructor()\n" 3947 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3948 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3949 OnePerLine); 3950 3951 EXPECT_EQ("Constructor()\n" 3952 " : // Comment forcing unwanted break.\n" 3953 " aaaa(aaaa) {}", 3954 format("Constructor() :\n" 3955 " // Comment forcing unwanted break.\n" 3956 " aaaa(aaaa) {}")); 3957 } 3958 3959 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 3960 FormatStyle Style = getLLVMStyle(); 3961 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 3962 Style.ColumnLimit = 60; 3963 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3964 Style.AllowAllConstructorInitializersOnNextLine = true; 3965 Style.BinPackParameters = false; 3966 3967 for (int i = 0; i < 4; ++i) { 3968 // Test all combinations of parameters that should not have an effect. 3969 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 3970 Style.AllowAllArgumentsOnNextLine = i & 2; 3971 3972 Style.AllowAllConstructorInitializersOnNextLine = true; 3973 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 3974 verifyFormat("Constructor()\n" 3975 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 3976 Style); 3977 verifyFormat("Constructor() : a(a), b(b) {}", Style); 3978 3979 Style.AllowAllConstructorInitializersOnNextLine = false; 3980 verifyFormat("Constructor()\n" 3981 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 3982 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 3983 Style); 3984 verifyFormat("Constructor() : a(a), b(b) {}", Style); 3985 3986 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 3987 Style.AllowAllConstructorInitializersOnNextLine = true; 3988 verifyFormat("Constructor()\n" 3989 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 3990 Style); 3991 3992 Style.AllowAllConstructorInitializersOnNextLine = false; 3993 verifyFormat("Constructor()\n" 3994 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3995 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 3996 Style); 3997 3998 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3999 Style.AllowAllConstructorInitializersOnNextLine = true; 4000 verifyFormat("Constructor() :\n" 4001 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4002 Style); 4003 4004 Style.AllowAllConstructorInitializersOnNextLine = false; 4005 verifyFormat("Constructor() :\n" 4006 " aaaaaaaaaaaaaaaaaa(a),\n" 4007 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4008 Style); 4009 } 4010 4011 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 4012 // AllowAllConstructorInitializersOnNextLine in all 4013 // BreakConstructorInitializers modes 4014 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 4015 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4016 Style.AllowAllConstructorInitializersOnNextLine = false; 4017 verifyFormat("SomeClassWithALongName::Constructor(\n" 4018 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 4019 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4020 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4021 Style); 4022 4023 Style.AllowAllConstructorInitializersOnNextLine = true; 4024 verifyFormat("SomeClassWithALongName::Constructor(\n" 4025 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4026 " int bbbbbbbbbbbbb,\n" 4027 " int cccccccccccccccc)\n" 4028 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4029 Style); 4030 4031 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4032 Style.AllowAllConstructorInitializersOnNextLine = false; 4033 verifyFormat("SomeClassWithALongName::Constructor(\n" 4034 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4035 " int bbbbbbbbbbbbb)\n" 4036 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4037 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4038 Style); 4039 4040 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 4041 4042 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4043 verifyFormat("SomeClassWithALongName::Constructor(\n" 4044 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 4045 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4046 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4047 Style); 4048 4049 Style.AllowAllConstructorInitializersOnNextLine = true; 4050 verifyFormat("SomeClassWithALongName::Constructor(\n" 4051 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4052 " int bbbbbbbbbbbbb,\n" 4053 " int cccccccccccccccc)\n" 4054 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4055 Style); 4056 4057 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4058 Style.AllowAllConstructorInitializersOnNextLine = false; 4059 verifyFormat("SomeClassWithALongName::Constructor(\n" 4060 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4061 " int bbbbbbbbbbbbb)\n" 4062 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4063 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4064 Style); 4065 4066 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 4067 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4068 verifyFormat("SomeClassWithALongName::Constructor(\n" 4069 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 4070 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4071 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4072 Style); 4073 4074 Style.AllowAllConstructorInitializersOnNextLine = true; 4075 verifyFormat("SomeClassWithALongName::Constructor(\n" 4076 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4077 " int bbbbbbbbbbbbb,\n" 4078 " int cccccccccccccccc) :\n" 4079 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4080 Style); 4081 4082 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4083 Style.AllowAllConstructorInitializersOnNextLine = false; 4084 verifyFormat("SomeClassWithALongName::Constructor(\n" 4085 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4086 " int bbbbbbbbbbbbb) :\n" 4087 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4088 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4089 Style); 4090 } 4091 4092 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 4093 FormatStyle Style = getLLVMStyle(); 4094 Style.ColumnLimit = 60; 4095 Style.BinPackArguments = false; 4096 for (int i = 0; i < 4; ++i) { 4097 // Test all combinations of parameters that should not have an effect. 4098 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 4099 Style.AllowAllConstructorInitializersOnNextLine = i & 2; 4100 4101 Style.AllowAllArgumentsOnNextLine = true; 4102 verifyFormat("void foo() {\n" 4103 " FunctionCallWithReallyLongName(\n" 4104 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 4105 "}", 4106 Style); 4107 Style.AllowAllArgumentsOnNextLine = false; 4108 verifyFormat("void foo() {\n" 4109 " FunctionCallWithReallyLongName(\n" 4110 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4111 " bbbbbbbbbbbb);\n" 4112 "}", 4113 Style); 4114 4115 Style.AllowAllArgumentsOnNextLine = true; 4116 verifyFormat("void foo() {\n" 4117 " auto VariableWithReallyLongName = {\n" 4118 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 4119 "}", 4120 Style); 4121 Style.AllowAllArgumentsOnNextLine = false; 4122 verifyFormat("void foo() {\n" 4123 " auto VariableWithReallyLongName = {\n" 4124 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4125 " bbbbbbbbbbbb};\n" 4126 "}", 4127 Style); 4128 } 4129 4130 // This parameter should not affect declarations. 4131 Style.BinPackParameters = false; 4132 Style.AllowAllArgumentsOnNextLine = false; 4133 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4134 verifyFormat("void FunctionCallWithReallyLongName(\n" 4135 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 4136 Style); 4137 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4138 verifyFormat("void FunctionCallWithReallyLongName(\n" 4139 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 4140 " int bbbbbbbbbbbb);", 4141 Style); 4142 } 4143 4144 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 4145 FormatStyle Style = getLLVMStyle(); 4146 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 4147 4148 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 4149 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 4150 getStyleWithColumns(Style, 45)); 4151 verifyFormat("Constructor() :\n" 4152 " Initializer(FitsOnTheLine) {}", 4153 getStyleWithColumns(Style, 44)); 4154 verifyFormat("Constructor() :\n" 4155 " Initializer(FitsOnTheLine) {}", 4156 getStyleWithColumns(Style, 43)); 4157 4158 verifyFormat("template <typename T>\n" 4159 "Constructor() : Initializer(FitsOnTheLine) {}", 4160 getStyleWithColumns(Style, 50)); 4161 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4162 verifyFormat( 4163 "SomeClass::Constructor() :\n" 4164 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 4165 Style); 4166 4167 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 4168 verifyFormat( 4169 "SomeClass::Constructor() :\n" 4170 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 4171 Style); 4172 4173 verifyFormat( 4174 "SomeClass::Constructor() :\n" 4175 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4176 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4177 Style); 4178 verifyFormat( 4179 "SomeClass::Constructor() :\n" 4180 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4181 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 4182 Style); 4183 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4184 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4185 " aaaaaaaaaa(aaaaaa) {}", 4186 Style); 4187 4188 verifyFormat("Constructor() :\n" 4189 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4190 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4191 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4192 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 4193 Style); 4194 4195 verifyFormat("Constructor() :\n" 4196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4197 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4198 Style); 4199 4200 verifyFormat("Constructor(int Parameter = 0) :\n" 4201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 4202 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 4203 Style); 4204 verifyFormat("Constructor() :\n" 4205 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 4206 "}", 4207 getStyleWithColumns(Style, 60)); 4208 verifyFormat("Constructor() :\n" 4209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4210 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 4211 Style); 4212 4213 // Here a line could be saved by splitting the second initializer onto two 4214 // lines, but that is not desirable. 4215 verifyFormat("Constructor() :\n" 4216 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 4217 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 4218 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4219 Style); 4220 4221 FormatStyle OnePerLine = Style; 4222 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4223 OnePerLine.AllowAllConstructorInitializersOnNextLine = false; 4224 verifyFormat("SomeClass::Constructor() :\n" 4225 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4226 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4227 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4228 OnePerLine); 4229 verifyFormat("SomeClass::Constructor() :\n" 4230 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 4231 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4232 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4233 OnePerLine); 4234 verifyFormat("MyClass::MyClass(int var) :\n" 4235 " some_var_(var), // 4 space indent\n" 4236 " some_other_var_(var + 1) { // lined up\n" 4237 "}", 4238 OnePerLine); 4239 verifyFormat("Constructor() :\n" 4240 " aaaaa(aaaaaa),\n" 4241 " aaaaa(aaaaaa),\n" 4242 " aaaaa(aaaaaa),\n" 4243 " aaaaa(aaaaaa),\n" 4244 " aaaaa(aaaaaa) {}", 4245 OnePerLine); 4246 verifyFormat("Constructor() :\n" 4247 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4248 " aaaaaaaaaaaaaaaaaaaaaa) {}", 4249 OnePerLine); 4250 OnePerLine.BinPackParameters = false; 4251 verifyFormat( 4252 "Constructor() :\n" 4253 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4254 " aaaaaaaaaaa().aaa(),\n" 4255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4256 OnePerLine); 4257 OnePerLine.ColumnLimit = 60; 4258 verifyFormat("Constructor() :\n" 4259 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4260 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 4261 OnePerLine); 4262 4263 EXPECT_EQ("Constructor() :\n" 4264 " // Comment forcing unwanted break.\n" 4265 " aaaa(aaaa) {}", 4266 format("Constructor() :\n" 4267 " // Comment forcing unwanted break.\n" 4268 " aaaa(aaaa) {}", 4269 Style)); 4270 4271 Style.ColumnLimit = 0; 4272 verifyFormat("SomeClass::Constructor() :\n" 4273 " a(a) {}", 4274 Style); 4275 verifyFormat("SomeClass::Constructor() noexcept :\n" 4276 " a(a) {}", 4277 Style); 4278 verifyFormat("SomeClass::Constructor() :\n" 4279 " a(a), b(b), c(c) {}", 4280 Style); 4281 verifyFormat("SomeClass::Constructor() :\n" 4282 " a(a) {\n" 4283 " foo();\n" 4284 " bar();\n" 4285 "}", 4286 Style); 4287 4288 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 4289 verifyFormat("SomeClass::Constructor() :\n" 4290 " a(a), b(b), c(c) {\n" 4291 "}", 4292 Style); 4293 verifyFormat("SomeClass::Constructor() :\n" 4294 " a(a) {\n" 4295 "}", 4296 Style); 4297 4298 Style.ColumnLimit = 80; 4299 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 4300 Style.ConstructorInitializerIndentWidth = 2; 4301 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 4302 Style); 4303 verifyFormat("SomeClass::Constructor() :\n" 4304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4305 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 4306 Style); 4307 4308 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well 4309 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 4310 verifyFormat("class SomeClass\n" 4311 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4312 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 4313 Style); 4314 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 4315 verifyFormat("class SomeClass\n" 4316 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4317 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 4318 Style); 4319 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 4320 verifyFormat("class SomeClass :\n" 4321 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4322 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 4323 Style); 4324 } 4325 4326 #ifndef EXPENSIVE_CHECKS 4327 // Expensive checks enables libstdc++ checking which includes validating the 4328 // state of ranges used in std::priority_queue - this blows out the 4329 // runtime/scalability of the function and makes this test unacceptably slow. 4330 TEST_F(FormatTest, MemoizationTests) { 4331 // This breaks if the memoization lookup does not take \c Indent and 4332 // \c LastSpace into account. 4333 verifyFormat( 4334 "extern CFRunLoopTimerRef\n" 4335 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 4336 " CFTimeInterval interval, CFOptionFlags flags,\n" 4337 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 4338 " CFRunLoopTimerContext *context) {}"); 4339 4340 // Deep nesting somewhat works around our memoization. 4341 verifyFormat( 4342 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4343 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4344 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4345 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4346 " aaaaa())))))))))))))))))))))))))))))))))))))));", 4347 getLLVMStyleWithColumns(65)); 4348 verifyFormat( 4349 "aaaaa(\n" 4350 " aaaaa,\n" 4351 " aaaaa(\n" 4352 " aaaaa,\n" 4353 " aaaaa(\n" 4354 " aaaaa,\n" 4355 " aaaaa(\n" 4356 " aaaaa,\n" 4357 " aaaaa(\n" 4358 " aaaaa,\n" 4359 " aaaaa(\n" 4360 " aaaaa,\n" 4361 " aaaaa(\n" 4362 " aaaaa,\n" 4363 " aaaaa(\n" 4364 " aaaaa,\n" 4365 " aaaaa(\n" 4366 " aaaaa,\n" 4367 " aaaaa(\n" 4368 " aaaaa,\n" 4369 " aaaaa(\n" 4370 " aaaaa,\n" 4371 " aaaaa(\n" 4372 " aaaaa,\n" 4373 " aaaaa))))))))))));", 4374 getLLVMStyleWithColumns(65)); 4375 verifyFormat( 4376 "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" 4377 " a),\n" 4378 " a),\n" 4379 " a),\n" 4380 " a),\n" 4381 " a),\n" 4382 " a),\n" 4383 " a),\n" 4384 " a),\n" 4385 " a),\n" 4386 " a),\n" 4387 " a),\n" 4388 " a),\n" 4389 " a),\n" 4390 " a),\n" 4391 " a),\n" 4392 " a),\n" 4393 " a)", 4394 getLLVMStyleWithColumns(65)); 4395 4396 // This test takes VERY long when memoization is broken. 4397 FormatStyle OnePerLine = getLLVMStyle(); 4398 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4399 OnePerLine.BinPackParameters = false; 4400 std::string input = "Constructor()\n" 4401 " : aaaa(a,\n"; 4402 for (unsigned i = 0, e = 80; i != e; ++i) { 4403 input += " a,\n"; 4404 } 4405 input += " a) {}"; 4406 verifyFormat(input, OnePerLine); 4407 } 4408 #endif 4409 4410 TEST_F(FormatTest, BreaksAsHighAsPossible) { 4411 verifyFormat( 4412 "void f() {\n" 4413 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 4414 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 4415 " f();\n" 4416 "}"); 4417 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 4418 " Intervals[i - 1].getRange().getLast()) {\n}"); 4419 } 4420 4421 TEST_F(FormatTest, BreaksFunctionDeclarations) { 4422 // Principially, we break function declarations in a certain order: 4423 // 1) break amongst arguments. 4424 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 4425 " Cccccccccccccc cccccccccccccc);"); 4426 verifyFormat("template <class TemplateIt>\n" 4427 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 4428 " TemplateIt *stop) {}"); 4429 4430 // 2) break after return type. 4431 verifyFormat( 4432 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4433 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 4434 getGoogleStyle()); 4435 4436 // 3) break after (. 4437 verifyFormat( 4438 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 4439 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 4440 getGoogleStyle()); 4441 4442 // 4) break before after nested name specifiers. 4443 verifyFormat( 4444 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4445 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 4446 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 4447 getGoogleStyle()); 4448 4449 // However, there are exceptions, if a sufficient amount of lines can be 4450 // saved. 4451 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 4452 // more adjusting. 4453 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4454 " Cccccccccccccc cccccccccc,\n" 4455 " Cccccccccccccc cccccccccc,\n" 4456 " Cccccccccccccc cccccccccc,\n" 4457 " Cccccccccccccc cccccccccc);"); 4458 verifyFormat( 4459 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4460 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4461 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4462 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 4463 getGoogleStyle()); 4464 verifyFormat( 4465 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4466 " Cccccccccccccc cccccccccc,\n" 4467 " Cccccccccccccc cccccccccc,\n" 4468 " Cccccccccccccc cccccccccc,\n" 4469 " Cccccccccccccc cccccccccc,\n" 4470 " Cccccccccccccc cccccccccc,\n" 4471 " Cccccccccccccc cccccccccc);"); 4472 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4473 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4474 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4475 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4476 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 4477 4478 // Break after multi-line parameters. 4479 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4481 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4482 " bbbb bbbb);"); 4483 verifyFormat("void SomeLoooooooooooongFunction(\n" 4484 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 4485 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4486 " int bbbbbbbbbbbbb);"); 4487 4488 // Treat overloaded operators like other functions. 4489 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4490 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 4491 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4492 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 4493 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4494 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 4495 verifyGoogleFormat( 4496 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 4497 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4498 verifyGoogleFormat( 4499 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 4500 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4501 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4502 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4503 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 4504 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4505 verifyGoogleFormat( 4506 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 4507 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4508 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 4509 verifyGoogleFormat( 4510 "template <typename T>\n" 4511 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4512 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 4513 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 4514 4515 FormatStyle Style = getLLVMStyle(); 4516 Style.PointerAlignment = FormatStyle::PAS_Left; 4517 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4518 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 4519 Style); 4520 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 4521 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4522 Style); 4523 } 4524 4525 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 4526 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 4527 // Prefer keeping `::` followed by `operator` together. 4528 EXPECT_EQ("const aaaa::bbbbbbb &\n" 4529 "ccccccccc::operator++() {\n" 4530 " stuff();\n" 4531 "}", 4532 format("const aaaa::bbbbbbb\n" 4533 "&ccccccccc::operator++() { stuff(); }", 4534 getLLVMStyleWithColumns(40))); 4535 } 4536 4537 TEST_F(FormatTest, TrailingReturnType) { 4538 verifyFormat("auto foo() -> int;\n"); 4539 verifyFormat("struct S {\n" 4540 " auto bar() const -> int;\n" 4541 "};"); 4542 verifyFormat("template <size_t Order, typename T>\n" 4543 "auto load_img(const std::string &filename)\n" 4544 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 4545 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 4546 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 4547 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 4548 verifyFormat("template <typename T>\n" 4549 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 4550 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 4551 4552 // Not trailing return types. 4553 verifyFormat("void f() { auto a = b->c(); }"); 4554 } 4555 4556 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 4557 // Avoid breaking before trailing 'const' or other trailing annotations, if 4558 // they are not function-like. 4559 FormatStyle Style = getGoogleStyle(); 4560 Style.ColumnLimit = 47; 4561 verifyFormat("void someLongFunction(\n" 4562 " int someLoooooooooooooongParameter) const {\n}", 4563 getLLVMStyleWithColumns(47)); 4564 verifyFormat("LoooooongReturnType\n" 4565 "someLoooooooongFunction() const {}", 4566 getLLVMStyleWithColumns(47)); 4567 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 4568 " const {}", 4569 Style); 4570 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4571 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 4572 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4573 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 4574 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4575 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 4576 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 4577 " aaaaaaaaaaa aaaaa) const override;"); 4578 verifyGoogleFormat( 4579 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4580 " const override;"); 4581 4582 // Even if the first parameter has to be wrapped. 4583 verifyFormat("void someLongFunction(\n" 4584 " int someLongParameter) const {}", 4585 getLLVMStyleWithColumns(46)); 4586 verifyFormat("void someLongFunction(\n" 4587 " int someLongParameter) const {}", 4588 Style); 4589 verifyFormat("void someLongFunction(\n" 4590 " int someLongParameter) override {}", 4591 Style); 4592 verifyFormat("void someLongFunction(\n" 4593 " int someLongParameter) OVERRIDE {}", 4594 Style); 4595 verifyFormat("void someLongFunction(\n" 4596 " int someLongParameter) final {}", 4597 Style); 4598 verifyFormat("void someLongFunction(\n" 4599 " int someLongParameter) FINAL {}", 4600 Style); 4601 verifyFormat("void someLongFunction(\n" 4602 " int parameter) const override {}", 4603 Style); 4604 4605 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4606 verifyFormat("void someLongFunction(\n" 4607 " int someLongParameter) const\n" 4608 "{\n" 4609 "}", 4610 Style); 4611 4612 // Unless these are unknown annotations. 4613 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 4614 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4615 " LONG_AND_UGLY_ANNOTATION;"); 4616 4617 // Breaking before function-like trailing annotations is fine to keep them 4618 // close to their arguments. 4619 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4620 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4621 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4622 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4623 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4624 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4625 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4626 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4627 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4628 4629 verifyFormat( 4630 "void aaaaaaaaaaaaaaaaaa()\n" 4631 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4632 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4633 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4634 " __attribute__((unused));"); 4635 verifyGoogleFormat( 4636 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4637 " GUARDED_BY(aaaaaaaaaaaa);"); 4638 verifyGoogleFormat( 4639 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4640 " GUARDED_BY(aaaaaaaaaaaa);"); 4641 verifyGoogleFormat( 4642 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4643 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4644 verifyGoogleFormat( 4645 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4646 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4647 } 4648 4649 TEST_F(FormatTest, FunctionAnnotations) { 4650 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4651 "int OldFunction(const string ¶meter) {}"); 4652 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4653 "string OldFunction(const string ¶meter) {}"); 4654 verifyFormat("template <typename T>\n" 4655 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4656 "string OldFunction(const string ¶meter) {}"); 4657 4658 // Not function annotations. 4659 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4660 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4661 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4662 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4663 verifyFormat("MACRO(abc).function() // wrap\n" 4664 " << abc;"); 4665 verifyFormat("MACRO(abc)->function() // wrap\n" 4666 " << abc;"); 4667 verifyFormat("MACRO(abc)::function() // wrap\n" 4668 " << abc;"); 4669 } 4670 4671 TEST_F(FormatTest, BreaksDesireably) { 4672 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4673 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4674 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4675 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4677 "}"); 4678 4679 verifyFormat( 4680 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4682 4683 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4684 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4686 4687 verifyFormat( 4688 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4689 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4691 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4693 4694 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4695 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4696 4697 verifyFormat( 4698 "void f() {\n" 4699 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4701 "}"); 4702 verifyFormat( 4703 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4705 verifyFormat( 4706 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4707 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4708 verifyFormat( 4709 "aaaaaa(aaa,\n" 4710 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4712 " aaaa);"); 4713 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4716 4717 // Indent consistently independent of call expression and unary operator. 4718 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4719 " dddddddddddddddddddddddddddddd));"); 4720 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4721 " dddddddddddddddddddddddddddddd));"); 4722 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4723 " dddddddddddddddddddddddddddddd));"); 4724 4725 // This test case breaks on an incorrect memoization, i.e. an optimization not 4726 // taking into account the StopAt value. 4727 verifyFormat( 4728 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4729 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4730 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4731 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4732 4733 verifyFormat("{\n {\n {\n" 4734 " Annotation.SpaceRequiredBefore =\n" 4735 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4736 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4737 " }\n }\n}"); 4738 4739 // Break on an outer level if there was a break on an inner level. 4740 EXPECT_EQ("f(g(h(a, // comment\n" 4741 " b, c),\n" 4742 " d, e),\n" 4743 " x, y);", 4744 format("f(g(h(a, // comment\n" 4745 " b, c), d, e), x, y);")); 4746 4747 // Prefer breaking similar line breaks. 4748 verifyFormat( 4749 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4750 " NSTrackingMouseEnteredAndExited |\n" 4751 " NSTrackingActiveAlways;"); 4752 } 4753 4754 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4755 FormatStyle NoBinPacking = getGoogleStyle(); 4756 NoBinPacking.BinPackParameters = false; 4757 NoBinPacking.BinPackArguments = true; 4758 verifyFormat("void f() {\n" 4759 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4761 "}", 4762 NoBinPacking); 4763 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4764 " int aaaaaaaaaaaaaaaaaaaa,\n" 4765 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4766 NoBinPacking); 4767 4768 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4769 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4770 " vector<int> bbbbbbbbbbbbbbb);", 4771 NoBinPacking); 4772 // FIXME: This behavior difference is probably not wanted. However, currently 4773 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4774 // template arguments from BreakBeforeParameter being set because of the 4775 // one-per-line formatting. 4776 verifyFormat( 4777 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4778 " aaaaaaaaaa> aaaaaaaaaa);", 4779 NoBinPacking); 4780 verifyFormat( 4781 "void fffffffffff(\n" 4782 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4783 " aaaaaaaaaa);"); 4784 } 4785 4786 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4787 FormatStyle NoBinPacking = getGoogleStyle(); 4788 NoBinPacking.BinPackParameters = false; 4789 NoBinPacking.BinPackArguments = false; 4790 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4791 " aaaaaaaaaaaaaaaaaaaa,\n" 4792 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4793 NoBinPacking); 4794 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4795 " aaaaaaaaaaaaa,\n" 4796 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4797 NoBinPacking); 4798 verifyFormat( 4799 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4800 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4802 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4804 NoBinPacking); 4805 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4806 " .aaaaaaaaaaaaaaaaaa();", 4807 NoBinPacking); 4808 verifyFormat("void f() {\n" 4809 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4810 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4811 "}", 4812 NoBinPacking); 4813 4814 verifyFormat( 4815 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4816 " aaaaaaaaaaaa,\n" 4817 " aaaaaaaaaaaa);", 4818 NoBinPacking); 4819 verifyFormat( 4820 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4821 " ddddddddddddddddddddddddddddd),\n" 4822 " test);", 4823 NoBinPacking); 4824 4825 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4826 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4827 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4828 " aaaaaaaaaaaaaaaaaa;", 4829 NoBinPacking); 4830 verifyFormat("a(\"a\"\n" 4831 " \"a\",\n" 4832 " a);"); 4833 4834 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4835 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4836 " aaaaaaaaa,\n" 4837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4838 NoBinPacking); 4839 verifyFormat( 4840 "void f() {\n" 4841 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4842 " .aaaaaaa();\n" 4843 "}", 4844 NoBinPacking); 4845 verifyFormat( 4846 "template <class SomeType, class SomeOtherType>\n" 4847 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4848 NoBinPacking); 4849 } 4850 4851 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4852 FormatStyle Style = getLLVMStyleWithColumns(15); 4853 Style.ExperimentalAutoDetectBinPacking = true; 4854 EXPECT_EQ("aaa(aaaa,\n" 4855 " aaaa,\n" 4856 " aaaa);\n" 4857 "aaa(aaaa,\n" 4858 " aaaa,\n" 4859 " aaaa);", 4860 format("aaa(aaaa,\n" // one-per-line 4861 " aaaa,\n" 4862 " aaaa );\n" 4863 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4864 Style)); 4865 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4866 " aaaa);\n" 4867 "aaa(aaaa, aaaa,\n" 4868 " aaaa);", 4869 format("aaa(aaaa, aaaa,\n" // bin-packed 4870 " aaaa );\n" 4871 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4872 Style)); 4873 } 4874 4875 TEST_F(FormatTest, FormatsBuilderPattern) { 4876 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4877 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4878 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4879 " .StartsWith(\".init\", ORDER_INIT)\n" 4880 " .StartsWith(\".fini\", ORDER_FINI)\n" 4881 " .StartsWith(\".hash\", ORDER_HASH)\n" 4882 " .Default(ORDER_TEXT);\n"); 4883 4884 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4885 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4886 verifyFormat( 4887 "aaaaaaa->aaaaaaa\n" 4888 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4890 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4891 verifyFormat( 4892 "aaaaaaa->aaaaaaa\n" 4893 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4894 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4895 verifyFormat( 4896 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4897 " aaaaaaaaaaaaaa);"); 4898 verifyFormat( 4899 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4900 " aaaaaa->aaaaaaaaaaaa()\n" 4901 " ->aaaaaaaaaaaaaaaa(\n" 4902 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4903 " ->aaaaaaaaaaaaaaaaa();"); 4904 verifyGoogleFormat( 4905 "void f() {\n" 4906 " someo->Add((new util::filetools::Handler(dir))\n" 4907 " ->OnEvent1(NewPermanentCallback(\n" 4908 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4909 " ->OnEvent2(NewPermanentCallback(\n" 4910 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4911 " ->OnEvent3(NewPermanentCallback(\n" 4912 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4913 " ->OnEvent5(NewPermanentCallback(\n" 4914 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4915 " ->OnEvent6(NewPermanentCallback(\n" 4916 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4917 "}"); 4918 4919 verifyFormat( 4920 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4921 verifyFormat("aaaaaaaaaaaaaaa()\n" 4922 " .aaaaaaaaaaaaaaa()\n" 4923 " .aaaaaaaaaaaaaaa()\n" 4924 " .aaaaaaaaaaaaaaa()\n" 4925 " .aaaaaaaaaaaaaaa();"); 4926 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4927 " .aaaaaaaaaaaaaaa()\n" 4928 " .aaaaaaaaaaaaaaa()\n" 4929 " .aaaaaaaaaaaaaaa();"); 4930 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4931 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4932 " .aaaaaaaaaaaaaaa();"); 4933 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4934 " ->aaaaaaaaaaaaaae(0)\n" 4935 " ->aaaaaaaaaaaaaaa();"); 4936 4937 // Don't linewrap after very short segments. 4938 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4939 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4940 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4941 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4942 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4943 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4944 verifyFormat("aaa()\n" 4945 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4946 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4947 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4948 4949 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4950 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4951 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4952 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4953 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4954 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4955 4956 // Prefer not to break after empty parentheses. 4957 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4958 " First->LastNewlineOffset);"); 4959 4960 // Prefer not to create "hanging" indents. 4961 verifyFormat( 4962 "return !soooooooooooooome_map\n" 4963 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4964 " .second;"); 4965 verifyFormat( 4966 "return aaaaaaaaaaaaaaaa\n" 4967 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4968 " .aaaa(aaaaaaaaaaaaaa);"); 4969 // No hanging indent here. 4970 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4972 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4973 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4974 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4975 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4976 getLLVMStyleWithColumns(60)); 4977 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4978 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4979 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4980 getLLVMStyleWithColumns(59)); 4981 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4982 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4983 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4984 4985 // Dont break if only closing statements before member call 4986 verifyFormat("test() {\n" 4987 " ([]() -> {\n" 4988 " int b = 32;\n" 4989 " return 3;\n" 4990 " }).foo();\n" 4991 "}"); 4992 verifyFormat("test() {\n" 4993 " (\n" 4994 " []() -> {\n" 4995 " int b = 32;\n" 4996 " return 3;\n" 4997 " },\n" 4998 " foo, bar)\n" 4999 " .foo();\n" 5000 "}"); 5001 verifyFormat("test() {\n" 5002 " ([]() -> {\n" 5003 " int b = 32;\n" 5004 " return 3;\n" 5005 " })\n" 5006 " .foo()\n" 5007 " .bar();\n" 5008 "}"); 5009 verifyFormat("test() {\n" 5010 " ([]() -> {\n" 5011 " int b = 32;\n" 5012 " return 3;\n" 5013 " })\n" 5014 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 5015 " \"bbbb\");\n" 5016 "}", 5017 getLLVMStyleWithColumns(30)); 5018 } 5019 5020 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 5021 verifyFormat( 5022 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5023 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 5024 verifyFormat( 5025 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 5026 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 5027 5028 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 5029 " ccccccccccccccccccccccccc) {\n}"); 5030 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 5031 " ccccccccccccccccccccccccc) {\n}"); 5032 5033 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 5034 " ccccccccccccccccccccccccc) {\n}"); 5035 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 5036 " ccccccccccccccccccccccccc) {\n}"); 5037 5038 verifyFormat( 5039 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 5040 " ccccccccccccccccccccccccc) {\n}"); 5041 verifyFormat( 5042 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 5043 " ccccccccccccccccccccccccc) {\n}"); 5044 5045 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 5046 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 5047 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 5048 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 5049 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 5050 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 5051 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 5052 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 5053 5054 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 5055 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 5056 " aaaaaaaaaaaaaaa != aa) {\n}"); 5057 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 5058 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 5059 " aaaaaaaaaaaaaaa != aa) {\n}"); 5060 } 5061 5062 TEST_F(FormatTest, BreaksAfterAssignments) { 5063 verifyFormat( 5064 "unsigned Cost =\n" 5065 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 5066 " SI->getPointerAddressSpaceee());\n"); 5067 verifyFormat( 5068 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 5069 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 5070 5071 verifyFormat( 5072 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 5073 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 5074 verifyFormat("unsigned OriginalStartColumn =\n" 5075 " SourceMgr.getSpellingColumnNumber(\n" 5076 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 5077 " 1;"); 5078 } 5079 5080 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 5081 FormatStyle Style = getLLVMStyle(); 5082 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5083 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 5084 Style); 5085 5086 Style.PenaltyBreakAssignment = 20; 5087 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5088 " cccccccccccccccccccccccccc;", 5089 Style); 5090 } 5091 5092 TEST_F(FormatTest, AlignsAfterAssignments) { 5093 verifyFormat( 5094 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5095 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5096 verifyFormat( 5097 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5098 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5099 verifyFormat( 5100 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5101 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5102 verifyFormat( 5103 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5104 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 5105 verifyFormat( 5106 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 5107 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 5108 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 5109 } 5110 5111 TEST_F(FormatTest, AlignsAfterReturn) { 5112 verifyFormat( 5113 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5114 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5115 verifyFormat( 5116 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5117 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 5118 verifyFormat( 5119 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 5120 " aaaaaaaaaaaaaaaaaaaaaa();"); 5121 verifyFormat( 5122 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 5123 " aaaaaaaaaaaaaaaaaaaaaa());"); 5124 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5126 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 5128 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5129 verifyFormat("return\n" 5130 " // true if code is one of a or b.\n" 5131 " code == a || code == b;"); 5132 } 5133 5134 TEST_F(FormatTest, AlignsAfterOpenBracket) { 5135 verifyFormat( 5136 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 5137 " aaaaaaaaa aaaaaaa) {}"); 5138 verifyFormat( 5139 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 5140 " aaaaaaaaaaa aaaaaaaaa);"); 5141 verifyFormat( 5142 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 5143 " aaaaaaaaaaaaaaaaaaaaa));"); 5144 FormatStyle Style = getLLVMStyle(); 5145 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5146 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5147 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 5148 Style); 5149 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 5150 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 5151 Style); 5152 verifyFormat("SomeLongVariableName->someFunction(\n" 5153 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 5154 Style); 5155 verifyFormat( 5156 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 5157 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5158 Style); 5159 verifyFormat( 5160 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 5161 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5162 Style); 5163 verifyFormat( 5164 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 5165 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 5166 Style); 5167 5168 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 5169 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 5170 " b));", 5171 Style); 5172 5173 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5174 Style.BinPackArguments = false; 5175 Style.BinPackParameters = false; 5176 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5177 " aaaaaaaaaaa aaaaaaaa,\n" 5178 " aaaaaaaaa aaaaaaa,\n" 5179 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5180 Style); 5181 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 5182 " aaaaaaaaaaa aaaaaaaaa,\n" 5183 " aaaaaaaaaaa aaaaaaaaa,\n" 5184 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5185 Style); 5186 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 5187 " aaaaaaaaaaaaaaa,\n" 5188 " aaaaaaaaaaaaaaaaaaaaa,\n" 5189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 5190 Style); 5191 verifyFormat( 5192 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 5193 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 5194 Style); 5195 verifyFormat( 5196 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 5197 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 5198 Style); 5199 verifyFormat( 5200 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5201 " aaaaaaaaaaaaaaaaaaaaa(\n" 5202 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 5203 " aaaaaaaaaaaaaaaa);", 5204 Style); 5205 verifyFormat( 5206 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5207 " aaaaaaaaaaaaaaaaaaaaa(\n" 5208 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 5209 " aaaaaaaaaaaaaaaa);", 5210 Style); 5211 } 5212 5213 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 5214 FormatStyle Style = getLLVMStyleWithColumns(40); 5215 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5216 " bbbbbbbbbbbbbbbbbbbbbb);", 5217 Style); 5218 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5219 Style.AlignOperands = false; 5220 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5221 " bbbbbbbbbbbbbbbbbbbbbb);", 5222 Style); 5223 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5224 Style.AlignOperands = true; 5225 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5226 " bbbbbbbbbbbbbbbbbbbbbb);", 5227 Style); 5228 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5229 Style.AlignOperands = false; 5230 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5231 " bbbbbbbbbbbbbbbbbbbbbb);", 5232 Style); 5233 } 5234 5235 TEST_F(FormatTest, BreaksConditionalExpressions) { 5236 verifyFormat( 5237 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5238 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5239 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5240 verifyFormat( 5241 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5242 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5243 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5244 verifyFormat( 5245 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5246 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5247 verifyFormat( 5248 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 5249 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5250 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5251 verifyFormat( 5252 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 5253 " : aaaaaaaaaaaaa);"); 5254 verifyFormat( 5255 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5256 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5257 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5258 " aaaaaaaaaaaaa);"); 5259 verifyFormat( 5260 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5261 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5262 " aaaaaaaaaaaaa);"); 5263 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5264 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5265 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5266 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5267 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5268 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5270 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5271 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5272 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5274 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5275 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5277 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5279 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5280 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5281 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5282 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5283 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5284 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5285 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5286 " : aaaaaaaaaaaaaaaa;"); 5287 verifyFormat( 5288 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5289 " ? aaaaaaaaaaaaaaa\n" 5290 " : aaaaaaaaaaaaaaa;"); 5291 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5292 " aaaaaaaaa\n" 5293 " ? b\n" 5294 " : c);"); 5295 verifyFormat("return aaaa == bbbb\n" 5296 " // comment\n" 5297 " ? aaaa\n" 5298 " : bbbb;"); 5299 verifyFormat("unsigned Indent =\n" 5300 " format(TheLine.First,\n" 5301 " IndentForLevel[TheLine.Level] >= 0\n" 5302 " ? IndentForLevel[TheLine.Level]\n" 5303 " : TheLine * 2,\n" 5304 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5305 getLLVMStyleWithColumns(60)); 5306 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 5307 " ? aaaaaaaaaaaaaaa\n" 5308 " : bbbbbbbbbbbbbbb //\n" 5309 " ? ccccccccccccccc\n" 5310 " : ddddddddddddddd;"); 5311 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 5312 " ? aaaaaaaaaaaaaaa\n" 5313 " : (bbbbbbbbbbbbbbb //\n" 5314 " ? ccccccccccccccc\n" 5315 " : ddddddddddddddd);"); 5316 verifyFormat( 5317 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5318 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5319 " aaaaaaaaaaaaaaaaaaaaa +\n" 5320 " aaaaaaaaaaaaaaaaaaaaa\n" 5321 " : aaaaaaaaaa;"); 5322 verifyFormat( 5323 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5324 " : aaaaaaaaaaaaaaaaaaaaaa\n" 5325 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5326 5327 FormatStyle NoBinPacking = getLLVMStyle(); 5328 NoBinPacking.BinPackArguments = false; 5329 verifyFormat( 5330 "void f() {\n" 5331 " g(aaa,\n" 5332 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 5333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5334 " ? aaaaaaaaaaaaaaa\n" 5335 " : aaaaaaaaaaaaaaa);\n" 5336 "}", 5337 NoBinPacking); 5338 verifyFormat( 5339 "void f() {\n" 5340 " g(aaa,\n" 5341 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 5342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5343 " ?: aaaaaaaaaaaaaaa);\n" 5344 "}", 5345 NoBinPacking); 5346 5347 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 5348 " // comment.\n" 5349 " ccccccccccccccccccccccccccccccccccccccc\n" 5350 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5351 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 5352 5353 // Assignments in conditional expressions. Apparently not uncommon :-(. 5354 verifyFormat("return a != b\n" 5355 " // comment\n" 5356 " ? a = b\n" 5357 " : a = b;"); 5358 verifyFormat("return a != b\n" 5359 " // comment\n" 5360 " ? a = a != b\n" 5361 " // comment\n" 5362 " ? a = b\n" 5363 " : a\n" 5364 " : a;\n"); 5365 verifyFormat("return a != b\n" 5366 " // comment\n" 5367 " ? a\n" 5368 " : a = a != b\n" 5369 " // comment\n" 5370 " ? a = b\n" 5371 " : a;"); 5372 } 5373 5374 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 5375 FormatStyle Style = getLLVMStyle(); 5376 Style.BreakBeforeTernaryOperators = false; 5377 Style.ColumnLimit = 70; 5378 verifyFormat( 5379 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5382 Style); 5383 verifyFormat( 5384 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5385 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5387 Style); 5388 verifyFormat( 5389 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5391 Style); 5392 verifyFormat( 5393 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 5394 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5395 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5396 Style); 5397 verifyFormat( 5398 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 5399 " aaaaaaaaaaaaa);", 5400 Style); 5401 verifyFormat( 5402 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5403 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5404 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5405 " aaaaaaaaaaaaa);", 5406 Style); 5407 verifyFormat( 5408 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5409 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5410 " aaaaaaaaaaaaa);", 5411 Style); 5412 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5417 Style); 5418 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5424 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5425 Style); 5426 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 5428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5430 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5431 Style); 5432 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5433 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5434 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5435 Style); 5436 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5440 Style); 5441 verifyFormat( 5442 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5443 " aaaaaaaaaaaaaaa :\n" 5444 " aaaaaaaaaaaaaaa;", 5445 Style); 5446 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5447 " aaaaaaaaa ?\n" 5448 " b :\n" 5449 " c);", 5450 Style); 5451 verifyFormat("unsigned Indent =\n" 5452 " format(TheLine.First,\n" 5453 " IndentForLevel[TheLine.Level] >= 0 ?\n" 5454 " IndentForLevel[TheLine.Level] :\n" 5455 " TheLine * 2,\n" 5456 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5457 Style); 5458 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5459 " aaaaaaaaaaaaaaa :\n" 5460 " bbbbbbbbbbbbbbb ? //\n" 5461 " ccccccccccccccc :\n" 5462 " ddddddddddddddd;", 5463 Style); 5464 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5465 " aaaaaaaaaaaaaaa :\n" 5466 " (bbbbbbbbbbbbbbb ? //\n" 5467 " ccccccccccccccc :\n" 5468 " ddddddddddddddd);", 5469 Style); 5470 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5471 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 5472 " ccccccccccccccccccccccccccc;", 5473 Style); 5474 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5475 " aaaaa :\n" 5476 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 5477 Style); 5478 } 5479 5480 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 5481 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 5482 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 5483 verifyFormat("bool a = true, b = false;"); 5484 5485 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 5487 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 5488 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 5489 verifyFormat( 5490 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5491 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 5492 " d = e && f;"); 5493 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 5494 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 5495 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5496 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 5497 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 5498 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 5499 5500 FormatStyle Style = getGoogleStyle(); 5501 Style.PointerAlignment = FormatStyle::PAS_Left; 5502 Style.DerivePointerAlignment = false; 5503 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5504 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 5505 " *b = bbbbbbbbbbbbbbbbbbb;", 5506 Style); 5507 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5508 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 5509 Style); 5510 verifyFormat("vector<int*> a, b;", Style); 5511 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 5512 } 5513 5514 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 5515 verifyFormat("arr[foo ? bar : baz];"); 5516 verifyFormat("f()[foo ? bar : baz];"); 5517 verifyFormat("(a + b)[foo ? bar : baz];"); 5518 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 5519 } 5520 5521 TEST_F(FormatTest, AlignsStringLiterals) { 5522 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 5523 " \"short literal\");"); 5524 verifyFormat( 5525 "looooooooooooooooooooooooongFunction(\n" 5526 " \"short literal\"\n" 5527 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 5528 verifyFormat("someFunction(\"Always break between multi-line\"\n" 5529 " \" string literals\",\n" 5530 " and, other, parameters);"); 5531 EXPECT_EQ("fun + \"1243\" /* comment */\n" 5532 " \"5678\";", 5533 format("fun + \"1243\" /* comment */\n" 5534 " \"5678\";", 5535 getLLVMStyleWithColumns(28))); 5536 EXPECT_EQ( 5537 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 5538 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 5539 " \"aaaaaaaaaaaaaaaa\";", 5540 format("aaaaaa =" 5541 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 5542 "aaaaaaaaaaaaaaaaaaaaa\" " 5543 "\"aaaaaaaaaaaaaaaa\";")); 5544 verifyFormat("a = a + \"a\"\n" 5545 " \"a\"\n" 5546 " \"a\";"); 5547 verifyFormat("f(\"a\", \"b\"\n" 5548 " \"c\");"); 5549 5550 verifyFormat( 5551 "#define LL_FORMAT \"ll\"\n" 5552 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 5553 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 5554 5555 verifyFormat("#define A(X) \\\n" 5556 " \"aaaaa\" #X \"bbbbbb\" \\\n" 5557 " \"ccccc\"", 5558 getLLVMStyleWithColumns(23)); 5559 verifyFormat("#define A \"def\"\n" 5560 "f(\"abc\" A \"ghi\"\n" 5561 " \"jkl\");"); 5562 5563 verifyFormat("f(L\"a\"\n" 5564 " L\"b\");"); 5565 verifyFormat("#define A(X) \\\n" 5566 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 5567 " L\"ccccc\"", 5568 getLLVMStyleWithColumns(25)); 5569 5570 verifyFormat("f(@\"a\"\n" 5571 " @\"b\");"); 5572 verifyFormat("NSString s = @\"a\"\n" 5573 " @\"b\"\n" 5574 " @\"c\";"); 5575 verifyFormat("NSString s = @\"a\"\n" 5576 " \"b\"\n" 5577 " \"c\";"); 5578 } 5579 5580 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 5581 FormatStyle Style = getLLVMStyle(); 5582 // No declarations or definitions should be moved to own line. 5583 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 5584 verifyFormat("class A {\n" 5585 " int f() { return 1; }\n" 5586 " int g();\n" 5587 "};\n" 5588 "int f() { return 1; }\n" 5589 "int g();\n", 5590 Style); 5591 5592 // All declarations and definitions should have the return type moved to its 5593 // own 5594 // line. 5595 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 5596 verifyFormat("class E {\n" 5597 " int\n" 5598 " f() {\n" 5599 " return 1;\n" 5600 " }\n" 5601 " int\n" 5602 " g();\n" 5603 "};\n" 5604 "int\n" 5605 "f() {\n" 5606 " return 1;\n" 5607 "}\n" 5608 "int\n" 5609 "g();\n", 5610 Style); 5611 5612 // Top-level definitions, and no kinds of declarations should have the 5613 // return type moved to its own line. 5614 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 5615 verifyFormat("class B {\n" 5616 " int f() { return 1; }\n" 5617 " int g();\n" 5618 "};\n" 5619 "int\n" 5620 "f() {\n" 5621 " return 1;\n" 5622 "}\n" 5623 "int g();\n", 5624 Style); 5625 5626 // Top-level definitions and declarations should have the return type moved 5627 // to its own line. 5628 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 5629 verifyFormat("class C {\n" 5630 " int f() { return 1; }\n" 5631 " int g();\n" 5632 "};\n" 5633 "int\n" 5634 "f() {\n" 5635 " return 1;\n" 5636 "}\n" 5637 "int\n" 5638 "g();\n", 5639 Style); 5640 5641 // All definitions should have the return type moved to its own line, but no 5642 // kinds of declarations. 5643 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5644 verifyFormat("class D {\n" 5645 " int\n" 5646 " f() {\n" 5647 " return 1;\n" 5648 " }\n" 5649 " int g();\n" 5650 "};\n" 5651 "int\n" 5652 "f() {\n" 5653 " return 1;\n" 5654 "}\n" 5655 "int g();\n", 5656 Style); 5657 verifyFormat("const char *\n" 5658 "f(void) {\n" // Break here. 5659 " return \"\";\n" 5660 "}\n" 5661 "const char *bar(void);\n", // No break here. 5662 Style); 5663 verifyFormat("template <class T>\n" 5664 "T *\n" 5665 "f(T &c) {\n" // Break here. 5666 " return NULL;\n" 5667 "}\n" 5668 "template <class T> T *f(T &c);\n", // No break here. 5669 Style); 5670 verifyFormat("class C {\n" 5671 " int\n" 5672 " operator+() {\n" 5673 " return 1;\n" 5674 " }\n" 5675 " int\n" 5676 " operator()() {\n" 5677 " return 1;\n" 5678 " }\n" 5679 "};\n", 5680 Style); 5681 verifyFormat("void\n" 5682 "A::operator()() {}\n" 5683 "void\n" 5684 "A::operator>>() {}\n" 5685 "void\n" 5686 "A::operator+() {}\n", 5687 Style); 5688 verifyFormat("void *operator new(std::size_t s);", // No break here. 5689 Style); 5690 verifyFormat("void *\n" 5691 "operator new(std::size_t s) {}", 5692 Style); 5693 verifyFormat("void *\n" 5694 "operator delete[](void *ptr) {}", 5695 Style); 5696 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5697 verifyFormat("const char *\n" 5698 "f(void)\n" // Break here. 5699 "{\n" 5700 " return \"\";\n" 5701 "}\n" 5702 "const char *bar(void);\n", // No break here. 5703 Style); 5704 verifyFormat("template <class T>\n" 5705 "T *\n" // Problem here: no line break 5706 "f(T &c)\n" // Break here. 5707 "{\n" 5708 " return NULL;\n" 5709 "}\n" 5710 "template <class T> T *f(T &c);\n", // No break here. 5711 Style); 5712 } 5713 5714 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5715 FormatStyle NoBreak = getLLVMStyle(); 5716 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5717 FormatStyle Break = getLLVMStyle(); 5718 Break.AlwaysBreakBeforeMultilineStrings = true; 5719 verifyFormat("aaaa = \"bbbb\"\n" 5720 " \"cccc\";", 5721 NoBreak); 5722 verifyFormat("aaaa =\n" 5723 " \"bbbb\"\n" 5724 " \"cccc\";", 5725 Break); 5726 verifyFormat("aaaa(\"bbbb\"\n" 5727 " \"cccc\");", 5728 NoBreak); 5729 verifyFormat("aaaa(\n" 5730 " \"bbbb\"\n" 5731 " \"cccc\");", 5732 Break); 5733 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5734 " \"cccc\");", 5735 NoBreak); 5736 verifyFormat("aaaa(qqq,\n" 5737 " \"bbbb\"\n" 5738 " \"cccc\");", 5739 Break); 5740 verifyFormat("aaaa(qqq,\n" 5741 " L\"bbbb\"\n" 5742 " L\"cccc\");", 5743 Break); 5744 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5745 " \"bbbb\"));", 5746 Break); 5747 verifyFormat("string s = someFunction(\n" 5748 " \"abc\"\n" 5749 " \"abc\");", 5750 Break); 5751 5752 // As we break before unary operators, breaking right after them is bad. 5753 verifyFormat("string foo = abc ? \"x\"\n" 5754 " \"blah blah blah blah blah blah\"\n" 5755 " : \"y\";", 5756 Break); 5757 5758 // Don't break if there is no column gain. 5759 verifyFormat("f(\"aaaa\"\n" 5760 " \"bbbb\");", 5761 Break); 5762 5763 // Treat literals with escaped newlines like multi-line string literals. 5764 EXPECT_EQ("x = \"a\\\n" 5765 "b\\\n" 5766 "c\";", 5767 format("x = \"a\\\n" 5768 "b\\\n" 5769 "c\";", 5770 NoBreak)); 5771 EXPECT_EQ("xxxx =\n" 5772 " \"a\\\n" 5773 "b\\\n" 5774 "c\";", 5775 format("xxxx = \"a\\\n" 5776 "b\\\n" 5777 "c\";", 5778 Break)); 5779 5780 EXPECT_EQ("NSString *const kString =\n" 5781 " @\"aaaa\"\n" 5782 " @\"bbbb\";", 5783 format("NSString *const kString = @\"aaaa\"\n" 5784 "@\"bbbb\";", 5785 Break)); 5786 5787 Break.ColumnLimit = 0; 5788 verifyFormat("const char *hello = \"hello llvm\";", Break); 5789 } 5790 5791 TEST_F(FormatTest, AlignsPipes) { 5792 verifyFormat( 5793 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5794 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5795 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5796 verifyFormat( 5797 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5798 " << aaaaaaaaaaaaaaaaaaaa;"); 5799 verifyFormat( 5800 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5801 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5802 verifyFormat( 5803 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5804 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5805 verifyFormat( 5806 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5807 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5808 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5809 verifyFormat( 5810 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5811 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5812 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5813 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5816 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5817 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5818 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5819 verifyFormat( 5820 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5821 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5822 verifyFormat( 5823 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5824 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5825 5826 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5827 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5828 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5830 " aaaaaaaaaaaaaaaaaaaaa)\n" 5831 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5832 verifyFormat("LOG_IF(aaa == //\n" 5833 " bbb)\n" 5834 " << a << b;"); 5835 5836 // But sometimes, breaking before the first "<<" is desirable. 5837 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5838 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5839 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5840 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5841 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5842 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5843 " << BEF << IsTemplate << Description << E->getType();"); 5844 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5845 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5846 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5847 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5848 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5850 " << aaa;"); 5851 5852 verifyFormat( 5853 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5854 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5855 5856 // Incomplete string literal. 5857 EXPECT_EQ("llvm::errs() << \"\n" 5858 " << a;", 5859 format("llvm::errs() << \"\n<<a;")); 5860 5861 verifyFormat("void f() {\n" 5862 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5863 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5864 "}"); 5865 5866 // Handle 'endl'. 5867 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5868 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5869 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5870 5871 // Handle '\n'. 5872 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5873 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5874 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5875 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5876 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5877 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5878 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5879 } 5880 5881 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5882 verifyFormat("return out << \"somepacket = {\\n\"\n" 5883 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5884 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5885 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5886 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5887 " << \"}\";"); 5888 5889 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5890 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5891 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5892 verifyFormat( 5893 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5894 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5895 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5896 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5897 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5898 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5899 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5900 verifyFormat( 5901 "void f() {\n" 5902 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5903 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5904 "}"); 5905 5906 // Breaking before the first "<<" is generally not desirable. 5907 verifyFormat( 5908 "llvm::errs()\n" 5909 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5910 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5911 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5912 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5913 getLLVMStyleWithColumns(70)); 5914 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5915 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5916 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5917 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5918 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5919 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5920 getLLVMStyleWithColumns(70)); 5921 5922 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5923 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5924 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5925 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5926 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5927 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5928 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5929 " (aaaa + aaaa);", 5930 getLLVMStyleWithColumns(40)); 5931 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5932 " (aaaaaaa + aaaaa));", 5933 getLLVMStyleWithColumns(40)); 5934 verifyFormat( 5935 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5936 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5937 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5938 } 5939 5940 TEST_F(FormatTest, UnderstandsEquals) { 5941 verifyFormat( 5942 "aaaaaaaaaaaaaaaaa =\n" 5943 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5944 verifyFormat( 5945 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5947 verifyFormat( 5948 "if (a) {\n" 5949 " f();\n" 5950 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5952 "}"); 5953 5954 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5955 " 100000000 + 10000000) {\n}"); 5956 } 5957 5958 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5959 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5960 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5961 5962 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5963 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5964 5965 verifyFormat( 5966 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5967 " Parameter2);"); 5968 5969 verifyFormat( 5970 "ShortObject->shortFunction(\n" 5971 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5972 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5973 5974 verifyFormat("loooooooooooooongFunction(\n" 5975 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5976 5977 verifyFormat( 5978 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5979 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5980 5981 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5982 " .WillRepeatedly(Return(SomeValue));"); 5983 verifyFormat("void f() {\n" 5984 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5985 " .Times(2)\n" 5986 " .WillRepeatedly(Return(SomeValue));\n" 5987 "}"); 5988 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5989 " ccccccccccccccccccccccc);"); 5990 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5991 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5992 " .aaaaa(aaaaa),\n" 5993 " aaaaaaaaaaaaaaaaaaaaa);"); 5994 verifyFormat("void f() {\n" 5995 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5996 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5997 "}"); 5998 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6000 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6001 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6002 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6003 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6004 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6005 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6006 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 6007 "}"); 6008 6009 // Here, it is not necessary to wrap at "." or "->". 6010 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 6011 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 6012 verifyFormat( 6013 "aaaaaaaaaaa->aaaaaaaaa(\n" 6014 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6015 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 6016 6017 verifyFormat( 6018 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6019 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 6020 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 6021 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 6022 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 6023 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 6024 6025 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6026 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6027 " .a();"); 6028 6029 FormatStyle NoBinPacking = getLLVMStyle(); 6030 NoBinPacking.BinPackParameters = false; 6031 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 6032 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 6033 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 6034 " aaaaaaaaaaaaaaaaaaa,\n" 6035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6036 NoBinPacking); 6037 6038 // If there is a subsequent call, change to hanging indentation. 6039 verifyFormat( 6040 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6041 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 6042 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6043 verifyFormat( 6044 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6045 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 6046 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6048 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6049 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6051 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 6052 } 6053 6054 TEST_F(FormatTest, WrapsTemplateDeclarations) { 6055 verifyFormat("template <typename T>\n" 6056 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 6057 verifyFormat("template <typename T>\n" 6058 "// T should be one of {A, B}.\n" 6059 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 6060 verifyFormat( 6061 "template <typename T>\n" 6062 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 6063 verifyFormat("template <typename T>\n" 6064 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 6065 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 6066 verifyFormat( 6067 "template <typename T>\n" 6068 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 6069 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 6070 verifyFormat( 6071 "template <typename T>\n" 6072 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 6073 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 6074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6075 verifyFormat("template <typename T>\n" 6076 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6077 " int aaaaaaaaaaaaaaaaaaaaaa);"); 6078 verifyFormat( 6079 "template <typename T1, typename T2 = char, typename T3 = char,\n" 6080 " typename T4 = char>\n" 6081 "void f();"); 6082 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 6083 " template <typename> class cccccccccccccccccccccc,\n" 6084 " typename ddddddddddddd>\n" 6085 "class C {};"); 6086 verifyFormat( 6087 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 6088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6089 6090 verifyFormat("void f() {\n" 6091 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 6092 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 6093 "}"); 6094 6095 verifyFormat("template <typename T> class C {};"); 6096 verifyFormat("template <typename T> void f();"); 6097 verifyFormat("template <typename T> void f() {}"); 6098 verifyFormat( 6099 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 6100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 6102 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 6103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 6105 " bbbbbbbbbbbbbbbbbbbbbbbb);", 6106 getLLVMStyleWithColumns(72)); 6107 EXPECT_EQ("static_cast<A< //\n" 6108 " B> *>(\n" 6109 "\n" 6110 ");", 6111 format("static_cast<A<//\n" 6112 " B>*>(\n" 6113 "\n" 6114 " );")); 6115 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6116 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 6117 6118 FormatStyle AlwaysBreak = getLLVMStyle(); 6119 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 6120 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 6121 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 6122 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 6123 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6124 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 6125 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 6126 verifyFormat("template <template <typename> class Fooooooo,\n" 6127 " template <typename> class Baaaaaaar>\n" 6128 "struct C {};", 6129 AlwaysBreak); 6130 verifyFormat("template <typename T> // T can be A, B or C.\n" 6131 "struct C {};", 6132 AlwaysBreak); 6133 verifyFormat("template <enum E> class A {\n" 6134 "public:\n" 6135 " E *f();\n" 6136 "};"); 6137 6138 FormatStyle NeverBreak = getLLVMStyle(); 6139 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 6140 verifyFormat("template <typename T> class C {};", NeverBreak); 6141 verifyFormat("template <typename T> void f();", NeverBreak); 6142 verifyFormat("template <typename T> void f() {}", NeverBreak); 6143 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 6144 NeverBreak); 6145 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6146 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 6147 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 6148 NeverBreak); 6149 verifyFormat("template <template <typename> class Fooooooo,\n" 6150 " template <typename> class Baaaaaaar>\n" 6151 "struct C {};", 6152 NeverBreak); 6153 verifyFormat("template <typename T> // T can be A, B or C.\n" 6154 "struct C {};", 6155 NeverBreak); 6156 verifyFormat("template <enum E> class A {\n" 6157 "public:\n" 6158 " E *f();\n" 6159 "};", NeverBreak); 6160 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 6161 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 6162 NeverBreak); 6163 } 6164 6165 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 6166 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 6167 Style.ColumnLimit = 60; 6168 EXPECT_EQ("// Baseline - no comments.\n" 6169 "template <\n" 6170 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 6171 "void f() {}", 6172 format("// Baseline - no comments.\n" 6173 "template <\n" 6174 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 6175 "void f() {}", 6176 Style)); 6177 6178 EXPECT_EQ("template <\n" 6179 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6180 "void f() {}", 6181 format("template <\n" 6182 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6183 "void f() {}", 6184 Style)); 6185 6186 EXPECT_EQ( 6187 "template <\n" 6188 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 6189 "void f() {}", 6190 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 6191 "void f() {}", 6192 Style)); 6193 6194 EXPECT_EQ( 6195 "template <\n" 6196 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6197 " // multiline\n" 6198 "void f() {}", 6199 format("template <\n" 6200 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6201 " // multiline\n" 6202 "void f() {}", 6203 Style)); 6204 6205 EXPECT_EQ( 6206 "template <typename aaaaaaaaaa<\n" 6207 " bbbbbbbbbbbb>::value> // trailing loooong\n" 6208 "void f() {}", 6209 format( 6210 "template <\n" 6211 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 6212 "void f() {}", 6213 Style)); 6214 } 6215 6216 TEST_F(FormatTest, WrapsTemplateParameters) { 6217 FormatStyle Style = getLLVMStyle(); 6218 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6219 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6220 verifyFormat( 6221 "template <typename... a> struct q {};\n" 6222 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 6223 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 6224 " y;", 6225 Style); 6226 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6227 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6228 verifyFormat( 6229 "template <typename... a> struct r {};\n" 6230 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 6231 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 6232 " y;", 6233 Style); 6234 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6235 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6236 verifyFormat( 6237 "template <typename... a> struct s {};\n" 6238 "extern s<\n" 6239 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6240 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 6241 " y;", 6242 Style); 6243 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6244 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6245 verifyFormat( 6246 "template <typename... a> struct t {};\n" 6247 "extern t<\n" 6248 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6249 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 6250 " y;", 6251 Style); 6252 } 6253 6254 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 6255 verifyFormat( 6256 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6257 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6258 verifyFormat( 6259 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 6262 6263 // FIXME: Should we have the extra indent after the second break? 6264 verifyFormat( 6265 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6267 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6268 6269 verifyFormat( 6270 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 6271 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 6272 6273 // Breaking at nested name specifiers is generally not desirable. 6274 verifyFormat( 6275 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6276 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6277 6278 verifyFormat( 6279 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 6280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6282 " aaaaaaaaaaaaaaaaaaaaa);", 6283 getLLVMStyleWithColumns(74)); 6284 6285 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6286 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6287 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6288 } 6289 6290 TEST_F(FormatTest, UnderstandsTemplateParameters) { 6291 verifyFormat("A<int> a;"); 6292 verifyFormat("A<A<A<int>>> a;"); 6293 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 6294 verifyFormat("bool x = a < 1 || 2 > a;"); 6295 verifyFormat("bool x = 5 < f<int>();"); 6296 verifyFormat("bool x = f<int>() > 5;"); 6297 verifyFormat("bool x = 5 < a<int>::x;"); 6298 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 6299 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 6300 6301 verifyGoogleFormat("A<A<int>> a;"); 6302 verifyGoogleFormat("A<A<A<int>>> a;"); 6303 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 6304 verifyGoogleFormat("A<A<int> > a;"); 6305 verifyGoogleFormat("A<A<A<int> > > a;"); 6306 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 6307 verifyGoogleFormat("A<::A<int>> a;"); 6308 verifyGoogleFormat("A<::A> a;"); 6309 verifyGoogleFormat("A< ::A> a;"); 6310 verifyGoogleFormat("A< ::A<int> > a;"); 6311 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 6312 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 6313 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 6314 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 6315 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 6316 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 6317 6318 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 6319 6320 verifyFormat("test >> a >> b;"); 6321 verifyFormat("test << a >> b;"); 6322 6323 verifyFormat("f<int>();"); 6324 verifyFormat("template <typename T> void f() {}"); 6325 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 6326 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 6327 "sizeof(char)>::type>;"); 6328 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 6329 verifyFormat("f(a.operator()<A>());"); 6330 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6331 " .template operator()<A>());", 6332 getLLVMStyleWithColumns(35)); 6333 6334 // Not template parameters. 6335 verifyFormat("return a < b && c > d;"); 6336 verifyFormat("void f() {\n" 6337 " while (a < b && c > d) {\n" 6338 " }\n" 6339 "}"); 6340 verifyFormat("template <typename... Types>\n" 6341 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 6342 6343 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 6345 getLLVMStyleWithColumns(60)); 6346 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 6347 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 6348 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 6349 } 6350 6351 TEST_F(FormatTest, BitshiftOperatorWidth) { 6352 EXPECT_EQ("int a = 1 << 2; /* foo\n" 6353 " bar */", 6354 format("int a=1<<2; /* foo\n" 6355 " bar */")); 6356 6357 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 6358 " bar */", 6359 format("int b =256>>1 ; /* foo\n" 6360 " bar */")); 6361 } 6362 6363 TEST_F(FormatTest, UnderstandsBinaryOperators) { 6364 verifyFormat("COMPARE(a, ==, b);"); 6365 verifyFormat("auto s = sizeof...(Ts) - 1;"); 6366 } 6367 6368 TEST_F(FormatTest, UnderstandsPointersToMembers) { 6369 verifyFormat("int A::*x;"); 6370 verifyFormat("int (S::*func)(void *);"); 6371 verifyFormat("void f() { int (S::*func)(void *); }"); 6372 verifyFormat("typedef bool *(Class::*Member)() const;"); 6373 verifyFormat("void f() {\n" 6374 " (a->*f)();\n" 6375 " a->*x;\n" 6376 " (a.*f)();\n" 6377 " ((*a).*f)();\n" 6378 " a.*x;\n" 6379 "}"); 6380 verifyFormat("void f() {\n" 6381 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6382 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 6383 "}"); 6384 verifyFormat( 6385 "(aaaaaaaaaa->*bbbbbbb)(\n" 6386 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6387 FormatStyle Style = getLLVMStyle(); 6388 Style.PointerAlignment = FormatStyle::PAS_Left; 6389 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 6390 } 6391 6392 TEST_F(FormatTest, UnderstandsUnaryOperators) { 6393 verifyFormat("int a = -2;"); 6394 verifyFormat("f(-1, -2, -3);"); 6395 verifyFormat("a[-1] = 5;"); 6396 verifyFormat("int a = 5 + -2;"); 6397 verifyFormat("if (i == -1) {\n}"); 6398 verifyFormat("if (i != -1) {\n}"); 6399 verifyFormat("if (i > -1) {\n}"); 6400 verifyFormat("if (i < -1) {\n}"); 6401 verifyFormat("++(a->f());"); 6402 verifyFormat("--(a->f());"); 6403 verifyFormat("(a->f())++;"); 6404 verifyFormat("a[42]++;"); 6405 verifyFormat("if (!(a->f())) {\n}"); 6406 verifyFormat("if (!+i) {\n}"); 6407 verifyFormat("~&a;"); 6408 6409 verifyFormat("a-- > b;"); 6410 verifyFormat("b ? -a : c;"); 6411 verifyFormat("n * sizeof char16;"); 6412 verifyFormat("n * alignof char16;", getGoogleStyle()); 6413 verifyFormat("sizeof(char);"); 6414 verifyFormat("alignof(char);", getGoogleStyle()); 6415 6416 verifyFormat("return -1;"); 6417 verifyFormat("switch (a) {\n" 6418 "case -1:\n" 6419 " break;\n" 6420 "}"); 6421 verifyFormat("#define X -1"); 6422 verifyFormat("#define X -kConstant"); 6423 6424 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 6425 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 6426 6427 verifyFormat("int a = /* confusing comment */ -1;"); 6428 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 6429 verifyFormat("int a = i /* confusing comment */++;"); 6430 } 6431 6432 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 6433 verifyFormat("if (!aaaaaaaaaa( // break\n" 6434 " aaaaa)) {\n" 6435 "}"); 6436 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 6437 " aaaaa));"); 6438 verifyFormat("*aaa = aaaaaaa( // break\n" 6439 " bbbbbb);"); 6440 } 6441 6442 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 6443 verifyFormat("bool operator<();"); 6444 verifyFormat("bool operator>();"); 6445 verifyFormat("bool operator=();"); 6446 verifyFormat("bool operator==();"); 6447 verifyFormat("bool operator!=();"); 6448 verifyFormat("int operator+();"); 6449 verifyFormat("int operator++();"); 6450 verifyFormat("int operator++(int) volatile noexcept;"); 6451 verifyFormat("bool operator,();"); 6452 verifyFormat("bool operator();"); 6453 verifyFormat("bool operator()();"); 6454 verifyFormat("bool operator[]();"); 6455 verifyFormat("operator bool();"); 6456 verifyFormat("operator int();"); 6457 verifyFormat("operator void *();"); 6458 verifyFormat("operator SomeType<int>();"); 6459 verifyFormat("operator SomeType<int, int>();"); 6460 verifyFormat("operator SomeType<SomeType<int>>();"); 6461 verifyFormat("void *operator new(std::size_t size);"); 6462 verifyFormat("void *operator new[](std::size_t size);"); 6463 verifyFormat("void operator delete(void *ptr);"); 6464 verifyFormat("void operator delete[](void *ptr);"); 6465 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 6466 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 6467 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 6468 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 6469 6470 verifyFormat( 6471 "ostream &operator<<(ostream &OutputStream,\n" 6472 " SomeReallyLongType WithSomeReallyLongValue);"); 6473 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 6474 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 6475 " return left.group < right.group;\n" 6476 "}"); 6477 verifyFormat("SomeType &operator=(const SomeType &S);"); 6478 verifyFormat("f.template operator()<int>();"); 6479 6480 verifyGoogleFormat("operator void*();"); 6481 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 6482 verifyGoogleFormat("operator ::A();"); 6483 6484 verifyFormat("using A::operator+;"); 6485 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 6486 "int i;"); 6487 } 6488 6489 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 6490 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 6491 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 6492 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 6493 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 6494 verifyFormat("Deleted &operator=(const Deleted &) &;"); 6495 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 6496 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 6497 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 6498 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 6499 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 6500 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 6501 verifyFormat("void Fn(T const &) const &;"); 6502 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 6503 verifyFormat("template <typename T>\n" 6504 "void F(T) && = delete;", 6505 getGoogleStyle()); 6506 6507 FormatStyle AlignLeft = getLLVMStyle(); 6508 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 6509 verifyFormat("void A::b() && {}", AlignLeft); 6510 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 6511 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 6512 AlignLeft); 6513 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 6514 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 6515 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 6516 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 6517 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 6518 verifyFormat("auto Function(T) & -> void;", AlignLeft); 6519 verifyFormat("void Fn(T const&) const&;", AlignLeft); 6520 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 6521 6522 FormatStyle Spaces = getLLVMStyle(); 6523 Spaces.SpacesInCStyleCastParentheses = true; 6524 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 6525 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 6526 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 6527 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 6528 6529 Spaces.SpacesInCStyleCastParentheses = false; 6530 Spaces.SpacesInParentheses = true; 6531 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 6532 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 6533 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 6534 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 6535 } 6536 6537 TEST_F(FormatTest, UnderstandsNewAndDelete) { 6538 verifyFormat("void f() {\n" 6539 " A *a = new A;\n" 6540 " A *a = new (placement) A;\n" 6541 " delete a;\n" 6542 " delete (A *)a;\n" 6543 "}"); 6544 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6545 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6546 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6547 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6548 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6549 verifyFormat("delete[] h->p;"); 6550 } 6551 6552 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 6553 verifyFormat("int *f(int *a) {}"); 6554 verifyFormat("int main(int argc, char **argv) {}"); 6555 verifyFormat("Test::Test(int b) : a(b * b) {}"); 6556 verifyIndependentOfContext("f(a, *a);"); 6557 verifyFormat("void g() { f(*a); }"); 6558 verifyIndependentOfContext("int a = b * 10;"); 6559 verifyIndependentOfContext("int a = 10 * b;"); 6560 verifyIndependentOfContext("int a = b * c;"); 6561 verifyIndependentOfContext("int a += b * c;"); 6562 verifyIndependentOfContext("int a -= b * c;"); 6563 verifyIndependentOfContext("int a *= b * c;"); 6564 verifyIndependentOfContext("int a /= b * c;"); 6565 verifyIndependentOfContext("int a = *b;"); 6566 verifyIndependentOfContext("int a = *b * c;"); 6567 verifyIndependentOfContext("int a = b * *c;"); 6568 verifyIndependentOfContext("int a = b * (10);"); 6569 verifyIndependentOfContext("S << b * (10);"); 6570 verifyIndependentOfContext("return 10 * b;"); 6571 verifyIndependentOfContext("return *b * *c;"); 6572 verifyIndependentOfContext("return a & ~b;"); 6573 verifyIndependentOfContext("f(b ? *c : *d);"); 6574 verifyIndependentOfContext("int a = b ? *c : *d;"); 6575 verifyIndependentOfContext("*b = a;"); 6576 verifyIndependentOfContext("a * ~b;"); 6577 verifyIndependentOfContext("a * !b;"); 6578 verifyIndependentOfContext("a * +b;"); 6579 verifyIndependentOfContext("a * -b;"); 6580 verifyIndependentOfContext("a * ++b;"); 6581 verifyIndependentOfContext("a * --b;"); 6582 verifyIndependentOfContext("a[4] * b;"); 6583 verifyIndependentOfContext("a[a * a] = 1;"); 6584 verifyIndependentOfContext("f() * b;"); 6585 verifyIndependentOfContext("a * [self dostuff];"); 6586 verifyIndependentOfContext("int x = a * (a + b);"); 6587 verifyIndependentOfContext("(a *)(a + b);"); 6588 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 6589 verifyIndependentOfContext("int *pa = (int *)&a;"); 6590 verifyIndependentOfContext("return sizeof(int **);"); 6591 verifyIndependentOfContext("return sizeof(int ******);"); 6592 verifyIndependentOfContext("return (int **&)a;"); 6593 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 6594 verifyFormat("void f(Type (*parameter)[10]) {}"); 6595 verifyFormat("void f(Type (¶meter)[10]) {}"); 6596 verifyGoogleFormat("return sizeof(int**);"); 6597 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 6598 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 6599 verifyFormat("auto a = [](int **&, int ***) {};"); 6600 verifyFormat("auto PointerBinding = [](const char *S) {};"); 6601 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 6602 verifyFormat("[](const decltype(*a) &value) {}"); 6603 verifyFormat("decltype(a * b) F();"); 6604 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 6605 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 6606 verifyIndependentOfContext("typedef void (*f)(int *a);"); 6607 verifyIndependentOfContext("int i{a * b};"); 6608 verifyIndependentOfContext("aaa && aaa->f();"); 6609 verifyIndependentOfContext("int x = ~*p;"); 6610 verifyFormat("Constructor() : a(a), area(width * height) {}"); 6611 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 6612 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 6613 verifyFormat("void f() { f(a, c * d); }"); 6614 verifyFormat("void f() { f(new a(), c * d); }"); 6615 verifyFormat("void f(const MyOverride &override);"); 6616 verifyFormat("void f(const MyFinal &final);"); 6617 verifyIndependentOfContext("bool a = f() && override.f();"); 6618 verifyIndependentOfContext("bool a = f() && final.f();"); 6619 6620 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 6621 6622 verifyIndependentOfContext("A<int *> a;"); 6623 verifyIndependentOfContext("A<int **> a;"); 6624 verifyIndependentOfContext("A<int *, int *> a;"); 6625 verifyIndependentOfContext("A<int *[]> a;"); 6626 verifyIndependentOfContext( 6627 "const char *const p = reinterpret_cast<const char *const>(q);"); 6628 verifyIndependentOfContext("A<int **, int **> a;"); 6629 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 6630 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 6631 verifyFormat("for (; a && b;) {\n}"); 6632 verifyFormat("bool foo = true && [] { return false; }();"); 6633 6634 verifyFormat( 6635 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6637 6638 verifyGoogleFormat("int const* a = &b;"); 6639 verifyGoogleFormat("**outparam = 1;"); 6640 verifyGoogleFormat("*outparam = a * b;"); 6641 verifyGoogleFormat("int main(int argc, char** argv) {}"); 6642 verifyGoogleFormat("A<int*> a;"); 6643 verifyGoogleFormat("A<int**> a;"); 6644 verifyGoogleFormat("A<int*, int*> a;"); 6645 verifyGoogleFormat("A<int**, int**> a;"); 6646 verifyGoogleFormat("f(b ? *c : *d);"); 6647 verifyGoogleFormat("int a = b ? *c : *d;"); 6648 verifyGoogleFormat("Type* t = **x;"); 6649 verifyGoogleFormat("Type* t = *++*x;"); 6650 verifyGoogleFormat("*++*x;"); 6651 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 6652 verifyGoogleFormat("Type* t = x++ * y;"); 6653 verifyGoogleFormat( 6654 "const char* const p = reinterpret_cast<const char* const>(q);"); 6655 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 6656 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 6657 verifyGoogleFormat("template <typename T>\n" 6658 "void f(int i = 0, SomeType** temps = NULL);"); 6659 6660 FormatStyle Left = getLLVMStyle(); 6661 Left.PointerAlignment = FormatStyle::PAS_Left; 6662 verifyFormat("x = *a(x) = *a(y);", Left); 6663 verifyFormat("for (;; *a = b) {\n}", Left); 6664 verifyFormat("return *this += 1;", Left); 6665 verifyFormat("throw *x;", Left); 6666 verifyFormat("delete *x;", Left); 6667 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 6668 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 6669 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 6670 6671 verifyIndependentOfContext("a = *(x + y);"); 6672 verifyIndependentOfContext("a = &(x + y);"); 6673 verifyIndependentOfContext("*(x + y).call();"); 6674 verifyIndependentOfContext("&(x + y)->call();"); 6675 verifyFormat("void f() { &(*I).first; }"); 6676 6677 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 6678 verifyFormat( 6679 "int *MyValues = {\n" 6680 " *A, // Operator detection might be confused by the '{'\n" 6681 " *BB // Operator detection might be confused by previous comment\n" 6682 "};"); 6683 6684 verifyIndependentOfContext("if (int *a = &b)"); 6685 verifyIndependentOfContext("if (int &a = *b)"); 6686 verifyIndependentOfContext("if (a & b[i])"); 6687 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 6688 verifyIndependentOfContext("if (*b[i])"); 6689 verifyIndependentOfContext("if (int *a = (&b))"); 6690 verifyIndependentOfContext("while (int *a = &b)"); 6691 verifyIndependentOfContext("size = sizeof *a;"); 6692 verifyIndependentOfContext("if (a && (b = c))"); 6693 verifyFormat("void f() {\n" 6694 " for (const int &v : Values) {\n" 6695 " }\n" 6696 "}"); 6697 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 6698 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 6699 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 6700 6701 verifyFormat("#define A (!a * b)"); 6702 verifyFormat("#define MACRO \\\n" 6703 " int *i = a * b; \\\n" 6704 " void f(a *b);", 6705 getLLVMStyleWithColumns(19)); 6706 6707 verifyIndependentOfContext("A = new SomeType *[Length];"); 6708 verifyIndependentOfContext("A = new SomeType *[Length]();"); 6709 verifyIndependentOfContext("T **t = new T *;"); 6710 verifyIndependentOfContext("T **t = new T *();"); 6711 verifyGoogleFormat("A = new SomeType*[Length]();"); 6712 verifyGoogleFormat("A = new SomeType*[Length];"); 6713 verifyGoogleFormat("T** t = new T*;"); 6714 verifyGoogleFormat("T** t = new T*();"); 6715 6716 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 6717 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 6718 verifyFormat("template <bool a, bool b> " 6719 "typename t::if<x && y>::type f() {}"); 6720 verifyFormat("template <int *y> f() {}"); 6721 verifyFormat("vector<int *> v;"); 6722 verifyFormat("vector<int *const> v;"); 6723 verifyFormat("vector<int *const **const *> v;"); 6724 verifyFormat("vector<int *volatile> v;"); 6725 verifyFormat("vector<a * b> v;"); 6726 verifyFormat("foo<b && false>();"); 6727 verifyFormat("foo<b & 1>();"); 6728 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 6729 verifyFormat( 6730 "template <class T, class = typename std::enable_if<\n" 6731 " std::is_integral<T>::value &&\n" 6732 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 6733 "void F();", 6734 getLLVMStyleWithColumns(70)); 6735 verifyFormat( 6736 "template <class T,\n" 6737 " class = typename std::enable_if<\n" 6738 " std::is_integral<T>::value &&\n" 6739 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 6740 " class U>\n" 6741 "void F();", 6742 getLLVMStyleWithColumns(70)); 6743 verifyFormat( 6744 "template <class T,\n" 6745 " class = typename ::std::enable_if<\n" 6746 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 6747 "void F();", 6748 getGoogleStyleWithColumns(68)); 6749 6750 verifyIndependentOfContext("MACRO(int *i);"); 6751 verifyIndependentOfContext("MACRO(auto *a);"); 6752 verifyIndependentOfContext("MACRO(const A *a);"); 6753 verifyIndependentOfContext("MACRO(A *const a);"); 6754 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 6755 verifyFormat("void f() { f(float{1}, a * a); }"); 6756 // FIXME: Is there a way to make this work? 6757 // verifyIndependentOfContext("MACRO(A *a);"); 6758 6759 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 6760 verifyFormat("return options != nullptr && operator==(*options);"); 6761 6762 EXPECT_EQ("#define OP(x) \\\n" 6763 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6764 " return s << a.DebugString(); \\\n" 6765 " }", 6766 format("#define OP(x) \\\n" 6767 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6768 " return s << a.DebugString(); \\\n" 6769 " }", 6770 getLLVMStyleWithColumns(50))); 6771 6772 // FIXME: We cannot handle this case yet; we might be able to figure out that 6773 // foo<x> d > v; doesn't make sense. 6774 verifyFormat("foo<a<b && c> d> v;"); 6775 6776 FormatStyle PointerMiddle = getLLVMStyle(); 6777 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 6778 verifyFormat("delete *x;", PointerMiddle); 6779 verifyFormat("int * x;", PointerMiddle); 6780 verifyFormat("int *[] x;", PointerMiddle); 6781 verifyFormat("template <int * y> f() {}", PointerMiddle); 6782 verifyFormat("int * f(int * a) {}", PointerMiddle); 6783 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 6784 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 6785 verifyFormat("A<int *> a;", PointerMiddle); 6786 verifyFormat("A<int **> a;", PointerMiddle); 6787 verifyFormat("A<int *, int *> a;", PointerMiddle); 6788 verifyFormat("A<int *[]> a;", PointerMiddle); 6789 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 6790 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 6791 verifyFormat("T ** t = new T *;", PointerMiddle); 6792 6793 // Member function reference qualifiers aren't binary operators. 6794 verifyFormat("string // break\n" 6795 "operator()() & {}"); 6796 verifyFormat("string // break\n" 6797 "operator()() && {}"); 6798 verifyGoogleFormat("template <typename T>\n" 6799 "auto x() & -> int {}"); 6800 } 6801 6802 TEST_F(FormatTest, UnderstandsAttributes) { 6803 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 6804 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 6805 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6806 FormatStyle AfterType = getLLVMStyle(); 6807 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 6808 verifyFormat("__attribute__((nodebug)) void\n" 6809 "foo() {}\n", 6810 AfterType); 6811 } 6812 6813 TEST_F(FormatTest, UnderstandsSquareAttributes) { 6814 verifyFormat("SomeType s [[unused]] (InitValue);"); 6815 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 6816 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 6817 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 6818 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 6819 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6820 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6821 6822 // Make sure we do not mistake attributes for array subscripts. 6823 verifyFormat("int a() {}\n" 6824 "[[unused]] int b() {}\n"); 6825 verifyFormat("NSArray *arr;\n" 6826 "arr[[Foo() bar]];"); 6827 6828 // On the other hand, we still need to correctly find array subscripts. 6829 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 6830 6831 // Make sure we do not parse attributes as lambda introducers. 6832 FormatStyle MultiLineFunctions = getLLVMStyle(); 6833 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6834 verifyFormat("[[unused]] int b() {\n" 6835 " return 42;\n" 6836 "}\n", 6837 MultiLineFunctions); 6838 } 6839 6840 TEST_F(FormatTest, UnderstandsEllipsis) { 6841 verifyFormat("int printf(const char *fmt, ...);"); 6842 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 6843 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 6844 6845 FormatStyle PointersLeft = getLLVMStyle(); 6846 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 6847 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 6848 } 6849 6850 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 6851 EXPECT_EQ("int *a;\n" 6852 "int *a;\n" 6853 "int *a;", 6854 format("int *a;\n" 6855 "int* a;\n" 6856 "int *a;", 6857 getGoogleStyle())); 6858 EXPECT_EQ("int* a;\n" 6859 "int* a;\n" 6860 "int* a;", 6861 format("int* a;\n" 6862 "int* a;\n" 6863 "int *a;", 6864 getGoogleStyle())); 6865 EXPECT_EQ("int *a;\n" 6866 "int *a;\n" 6867 "int *a;", 6868 format("int *a;\n" 6869 "int * a;\n" 6870 "int * a;", 6871 getGoogleStyle())); 6872 EXPECT_EQ("auto x = [] {\n" 6873 " int *a;\n" 6874 " int *a;\n" 6875 " int *a;\n" 6876 "};", 6877 format("auto x=[]{int *a;\n" 6878 "int * a;\n" 6879 "int * a;};", 6880 getGoogleStyle())); 6881 } 6882 6883 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6884 verifyFormat("int f(int &&a) {}"); 6885 verifyFormat("int f(int a, char &&b) {}"); 6886 verifyFormat("void f() { int &&a = b; }"); 6887 verifyGoogleFormat("int f(int a, char&& b) {}"); 6888 verifyGoogleFormat("void f() { int&& a = b; }"); 6889 6890 verifyIndependentOfContext("A<int &&> a;"); 6891 verifyIndependentOfContext("A<int &&, int &&> a;"); 6892 verifyGoogleFormat("A<int&&> a;"); 6893 verifyGoogleFormat("A<int&&, int&&> a;"); 6894 6895 // Not rvalue references: 6896 verifyFormat("template <bool B, bool C> class A {\n" 6897 " static_assert(B && C, \"Something is wrong\");\n" 6898 "};"); 6899 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6900 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6901 verifyFormat("#define A(a, b) (a && b)"); 6902 } 6903 6904 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6905 verifyFormat("void f() {\n" 6906 " x[aaaaaaaaa -\n" 6907 " b] = 23;\n" 6908 "}", 6909 getLLVMStyleWithColumns(15)); 6910 } 6911 6912 TEST_F(FormatTest, FormatsCasts) { 6913 verifyFormat("Type *A = static_cast<Type *>(P);"); 6914 verifyFormat("Type *A = (Type *)P;"); 6915 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6916 verifyFormat("int a = (int)(2.0f);"); 6917 verifyFormat("int a = (int)2.0f;"); 6918 verifyFormat("x[(int32)y];"); 6919 verifyFormat("x = (int32)y;"); 6920 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6921 verifyFormat("int a = (int)*b;"); 6922 verifyFormat("int a = (int)2.0f;"); 6923 verifyFormat("int a = (int)~0;"); 6924 verifyFormat("int a = (int)++a;"); 6925 verifyFormat("int a = (int)sizeof(int);"); 6926 verifyFormat("int a = (int)+2;"); 6927 verifyFormat("my_int a = (my_int)2.0f;"); 6928 verifyFormat("my_int a = (my_int)sizeof(int);"); 6929 verifyFormat("return (my_int)aaa;"); 6930 verifyFormat("#define x ((int)-1)"); 6931 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6932 verifyFormat("#define p(q) ((int *)&q)"); 6933 verifyFormat("fn(a)(b) + 1;"); 6934 6935 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6936 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6937 verifyFormat("my_int a = (my_int)~0;"); 6938 verifyFormat("my_int a = (my_int)++a;"); 6939 verifyFormat("my_int a = (my_int)-2;"); 6940 verifyFormat("my_int a = (my_int)1;"); 6941 verifyFormat("my_int a = (my_int *)1;"); 6942 verifyFormat("my_int a = (const my_int)-1;"); 6943 verifyFormat("my_int a = (const my_int *)-1;"); 6944 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6945 verifyFormat("my_int a = (ns::my_int)-2;"); 6946 verifyFormat("case (my_int)ONE:"); 6947 verifyFormat("auto x = (X)this;"); 6948 6949 // FIXME: single value wrapped with paren will be treated as cast. 6950 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6951 6952 verifyFormat("{ (void)F; }"); 6953 6954 // Don't break after a cast's 6955 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6956 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6957 " bbbbbbbbbbbbbbbbbbbbbb);"); 6958 6959 // These are not casts. 6960 verifyFormat("void f(int *) {}"); 6961 verifyFormat("f(foo)->b;"); 6962 verifyFormat("f(foo).b;"); 6963 verifyFormat("f(foo)(b);"); 6964 verifyFormat("f(foo)[b];"); 6965 verifyFormat("[](foo) { return 4; }(bar);"); 6966 verifyFormat("(*funptr)(foo)[4];"); 6967 verifyFormat("funptrs[4](foo)[4];"); 6968 verifyFormat("void f(int *);"); 6969 verifyFormat("void f(int *) = 0;"); 6970 verifyFormat("void f(SmallVector<int>) {}"); 6971 verifyFormat("void f(SmallVector<int>);"); 6972 verifyFormat("void f(SmallVector<int>) = 0;"); 6973 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6974 verifyFormat("int a = sizeof(int) * b;"); 6975 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6976 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6977 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6978 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6979 6980 // These are not casts, but at some point were confused with casts. 6981 verifyFormat("virtual void foo(int *) override;"); 6982 verifyFormat("virtual void foo(char &) const;"); 6983 verifyFormat("virtual void foo(int *a, char *) const;"); 6984 verifyFormat("int a = sizeof(int *) + b;"); 6985 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6986 verifyFormat("bool b = f(g<int>) && c;"); 6987 verifyFormat("typedef void (*f)(int i) func;"); 6988 6989 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6990 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6991 // FIXME: The indentation here is not ideal. 6992 verifyFormat( 6993 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6994 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6995 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6996 } 6997 6998 TEST_F(FormatTest, FormatsFunctionTypes) { 6999 verifyFormat("A<bool()> a;"); 7000 verifyFormat("A<SomeType()> a;"); 7001 verifyFormat("A<void (*)(int, std::string)> a;"); 7002 verifyFormat("A<void *(int)>;"); 7003 verifyFormat("void *(*a)(int *, SomeType *);"); 7004 verifyFormat("int (*func)(void *);"); 7005 verifyFormat("void f() { int (*func)(void *); }"); 7006 verifyFormat("template <class CallbackClass>\n" 7007 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 7008 7009 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 7010 verifyGoogleFormat("void* (*a)(int);"); 7011 verifyGoogleFormat( 7012 "template <class CallbackClass>\n" 7013 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 7014 7015 // Other constructs can look somewhat like function types: 7016 verifyFormat("A<sizeof(*x)> a;"); 7017 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 7018 verifyFormat("some_var = function(*some_pointer_var)[0];"); 7019 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 7020 verifyFormat("int x = f(&h)();"); 7021 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 7022 verifyFormat("std::function<\n" 7023 " LooooooooooongTemplatedType<\n" 7024 " SomeType>*(\n" 7025 " LooooooooooooooooongType type)>\n" 7026 " function;", 7027 getGoogleStyleWithColumns(40)); 7028 } 7029 7030 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 7031 verifyFormat("A (*foo_)[6];"); 7032 verifyFormat("vector<int> (*foo_)[6];"); 7033 } 7034 7035 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 7036 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7037 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 7038 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 7039 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 7040 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7041 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 7042 7043 // Different ways of ()-initializiation. 7044 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7045 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 7046 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7047 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 7048 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7049 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 7050 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7051 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 7052 7053 // Lambdas should not confuse the variable declaration heuristic. 7054 verifyFormat("LooooooooooooooooongType\n" 7055 " variable(nullptr, [](A *a) {});", 7056 getLLVMStyleWithColumns(40)); 7057 } 7058 7059 TEST_F(FormatTest, BreaksLongDeclarations) { 7060 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 7061 " AnotherNameForTheLongType;"); 7062 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 7063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7064 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7065 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 7066 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 7067 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 7068 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7069 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7070 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 7071 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7072 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 7073 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7074 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 7075 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7076 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7077 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 7078 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7079 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 7080 FormatStyle Indented = getLLVMStyle(); 7081 Indented.IndentWrappedFunctionNames = true; 7082 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7083 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 7084 Indented); 7085 verifyFormat( 7086 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7087 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 7088 Indented); 7089 verifyFormat( 7090 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 7091 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 7092 Indented); 7093 verifyFormat( 7094 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 7095 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 7096 Indented); 7097 7098 // FIXME: Without the comment, this breaks after "(". 7099 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 7100 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 7101 getGoogleStyle()); 7102 7103 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 7104 " int LoooooooooooooooooooongParam2) {}"); 7105 verifyFormat( 7106 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 7107 " SourceLocation L, IdentifierIn *II,\n" 7108 " Type *T) {}"); 7109 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 7110 "ReallyReaaallyLongFunctionName(\n" 7111 " const std::string &SomeParameter,\n" 7112 " const SomeType<string, SomeOtherTemplateParameter>\n" 7113 " &ReallyReallyLongParameterName,\n" 7114 " const SomeType<string, SomeOtherTemplateParameter>\n" 7115 " &AnotherLongParameterName) {}"); 7116 verifyFormat("template <typename A>\n" 7117 "SomeLoooooooooooooooooooooongType<\n" 7118 " typename some_namespace::SomeOtherType<A>::Type>\n" 7119 "Function() {}"); 7120 7121 verifyGoogleFormat( 7122 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 7123 " aaaaaaaaaaaaaaaaaaaaaaa;"); 7124 verifyGoogleFormat( 7125 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 7126 " SourceLocation L) {}"); 7127 verifyGoogleFormat( 7128 "some_namespace::LongReturnType\n" 7129 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 7130 " int first_long_parameter, int second_parameter) {}"); 7131 7132 verifyGoogleFormat("template <typename T>\n" 7133 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7134 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 7135 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7136 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 7137 7138 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 7139 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7140 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7141 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7142 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7143 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 7144 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7145 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 7147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7148 7149 verifyFormat("template <typename T> // Templates on own line.\n" 7150 "static int // Some comment.\n" 7151 "MyFunction(int a);", 7152 getLLVMStyle()); 7153 } 7154 7155 TEST_F(FormatTest, FormatsArrays) { 7156 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7157 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 7158 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 7159 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 7160 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7161 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 7162 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7163 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 7164 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7165 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 7166 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7167 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7168 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 7169 verifyFormat( 7170 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 7171 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7172 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7173 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 7174 " .aaaaaaaaaaaaaaaaaaaaaa();"); 7175 7176 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 7177 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 7178 verifyFormat( 7179 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 7180 " .aaaaaaa[0]\n" 7181 " .aaaaaaaaaaaaaaaaaaaaaa();"); 7182 verifyFormat("a[::b::c];"); 7183 7184 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 7185 7186 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 7187 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 7188 } 7189 7190 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 7191 verifyFormat("(a)->b();"); 7192 verifyFormat("--a;"); 7193 } 7194 7195 TEST_F(FormatTest, HandlesIncludeDirectives) { 7196 verifyFormat("#include <string>\n" 7197 "#include <a/b/c.h>\n" 7198 "#include \"a/b/string\"\n" 7199 "#include \"string.h\"\n" 7200 "#include \"string.h\"\n" 7201 "#include <a-a>\n" 7202 "#include < path with space >\n" 7203 "#include_next <test.h>" 7204 "#include \"abc.h\" // this is included for ABC\n" 7205 "#include \"some long include\" // with a comment\n" 7206 "#include \"some very long include path\"\n" 7207 "#include <some/very/long/include/path>\n", 7208 getLLVMStyleWithColumns(35)); 7209 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 7210 EXPECT_EQ("#include <a>", format("#include<a>")); 7211 7212 verifyFormat("#import <string>"); 7213 verifyFormat("#import <a/b/c.h>"); 7214 verifyFormat("#import \"a/b/string\""); 7215 verifyFormat("#import \"string.h\""); 7216 verifyFormat("#import \"string.h\""); 7217 verifyFormat("#if __has_include(<strstream>)\n" 7218 "#include <strstream>\n" 7219 "#endif"); 7220 7221 verifyFormat("#define MY_IMPORT <a/b>"); 7222 7223 verifyFormat("#if __has_include(<a/b>)"); 7224 verifyFormat("#if __has_include_next(<a/b>)"); 7225 verifyFormat("#define F __has_include(<a/b>)"); 7226 verifyFormat("#define F __has_include_next(<a/b>)"); 7227 7228 // Protocol buffer definition or missing "#". 7229 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 7230 getLLVMStyleWithColumns(30)); 7231 7232 FormatStyle Style = getLLVMStyle(); 7233 Style.AlwaysBreakBeforeMultilineStrings = true; 7234 Style.ColumnLimit = 0; 7235 verifyFormat("#import \"abc.h\"", Style); 7236 7237 // But 'import' might also be a regular C++ namespace. 7238 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7239 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7240 } 7241 7242 //===----------------------------------------------------------------------===// 7243 // Error recovery tests. 7244 //===----------------------------------------------------------------------===// 7245 7246 TEST_F(FormatTest, IncompleteParameterLists) { 7247 FormatStyle NoBinPacking = getLLVMStyle(); 7248 NoBinPacking.BinPackParameters = false; 7249 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 7250 " double *min_x,\n" 7251 " double *max_x,\n" 7252 " double *min_y,\n" 7253 " double *max_y,\n" 7254 " double *min_z,\n" 7255 " double *max_z, ) {}", 7256 NoBinPacking); 7257 } 7258 7259 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 7260 verifyFormat("void f() { return; }\n42"); 7261 verifyFormat("void f() {\n" 7262 " if (0)\n" 7263 " return;\n" 7264 "}\n" 7265 "42"); 7266 verifyFormat("void f() { return }\n42"); 7267 verifyFormat("void f() {\n" 7268 " if (0)\n" 7269 " return\n" 7270 "}\n" 7271 "42"); 7272 } 7273 7274 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 7275 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 7276 EXPECT_EQ("void f() {\n" 7277 " if (a)\n" 7278 " return\n" 7279 "}", 7280 format("void f ( ) { if ( a ) return }")); 7281 EXPECT_EQ("namespace N {\n" 7282 "void f()\n" 7283 "}", 7284 format("namespace N { void f() }")); 7285 EXPECT_EQ("namespace N {\n" 7286 "void f() {}\n" 7287 "void g()\n" 7288 "} // namespace N", 7289 format("namespace N { void f( ) { } void g( ) }")); 7290 } 7291 7292 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 7293 verifyFormat("int aaaaaaaa =\n" 7294 " // Overlylongcomment\n" 7295 " b;", 7296 getLLVMStyleWithColumns(20)); 7297 verifyFormat("function(\n" 7298 " ShortArgument,\n" 7299 " LoooooooooooongArgument);\n", 7300 getLLVMStyleWithColumns(20)); 7301 } 7302 7303 TEST_F(FormatTest, IncorrectAccessSpecifier) { 7304 verifyFormat("public:"); 7305 verifyFormat("class A {\n" 7306 "public\n" 7307 " void f() {}\n" 7308 "};"); 7309 verifyFormat("public\n" 7310 "int qwerty;"); 7311 verifyFormat("public\n" 7312 "B {}"); 7313 verifyFormat("public\n" 7314 "{}"); 7315 verifyFormat("public\n" 7316 "B { int x; }"); 7317 } 7318 7319 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 7320 verifyFormat("{"); 7321 verifyFormat("#})"); 7322 verifyNoCrash("(/**/[:!] ?[)."); 7323 } 7324 7325 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 7326 // Found by oss-fuzz: 7327 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 7328 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 7329 Style.ColumnLimit = 60; 7330 verifyNoCrash( 7331 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 7332 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 7333 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 7334 Style); 7335 } 7336 7337 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 7338 verifyFormat("do {\n}"); 7339 verifyFormat("do {\n}\n" 7340 "f();"); 7341 verifyFormat("do {\n}\n" 7342 "wheeee(fun);"); 7343 verifyFormat("do {\n" 7344 " f();\n" 7345 "}"); 7346 } 7347 7348 TEST_F(FormatTest, IncorrectCodeMissingParens) { 7349 verifyFormat("if {\n foo;\n foo();\n}"); 7350 verifyFormat("switch {\n foo;\n foo();\n}"); 7351 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 7352 verifyFormat("while {\n foo;\n foo();\n}"); 7353 verifyFormat("do {\n foo;\n foo();\n} while;"); 7354 } 7355 7356 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 7357 verifyIncompleteFormat("namespace {\n" 7358 "class Foo { Foo (\n" 7359 "};\n" 7360 "} // namespace"); 7361 } 7362 7363 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 7364 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 7365 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 7366 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 7367 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 7368 7369 EXPECT_EQ("{\n" 7370 " {\n" 7371 " breakme(\n" 7372 " qwe);\n" 7373 " }\n", 7374 format("{\n" 7375 " {\n" 7376 " breakme(qwe);\n" 7377 "}\n", 7378 getLLVMStyleWithColumns(10))); 7379 } 7380 7381 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 7382 verifyFormat("int x = {\n" 7383 " avariable,\n" 7384 " b(alongervariable)};", 7385 getLLVMStyleWithColumns(25)); 7386 } 7387 7388 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 7389 verifyFormat("return (a)(b){1, 2, 3};"); 7390 } 7391 7392 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 7393 verifyFormat("vector<int> x{1, 2, 3, 4};"); 7394 verifyFormat("vector<int> x{\n" 7395 " 1,\n" 7396 " 2,\n" 7397 " 3,\n" 7398 " 4,\n" 7399 "};"); 7400 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 7401 verifyFormat("f({1, 2});"); 7402 verifyFormat("auto v = Foo{-1};"); 7403 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 7404 verifyFormat("Class::Class : member{1, 2, 3} {}"); 7405 verifyFormat("new vector<int>{1, 2, 3};"); 7406 verifyFormat("new int[3]{1, 2, 3};"); 7407 verifyFormat("new int{1};"); 7408 verifyFormat("return {arg1, arg2};"); 7409 verifyFormat("return {arg1, SomeType{parameter}};"); 7410 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 7411 verifyFormat("new T{arg1, arg2};"); 7412 verifyFormat("f(MyMap[{composite, key}]);"); 7413 verifyFormat("class Class {\n" 7414 " T member = {arg1, arg2};\n" 7415 "};"); 7416 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 7417 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 7418 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 7419 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 7420 verifyFormat("int a = std::is_integral<int>{} + 0;"); 7421 7422 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7423 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7424 verifyFormat("auto i = decltype(x){};"); 7425 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 7426 verifyFormat("Node n{1, Node{1000}, //\n" 7427 " 2};"); 7428 verifyFormat("Aaaa aaaaaaa{\n" 7429 " {\n" 7430 " aaaa,\n" 7431 " },\n" 7432 "};"); 7433 verifyFormat("class C : public D {\n" 7434 " SomeClass SC{2};\n" 7435 "};"); 7436 verifyFormat("class C : public A {\n" 7437 " class D : public B {\n" 7438 " void f() { int i{2}; }\n" 7439 " };\n" 7440 "};"); 7441 verifyFormat("#define A {a, a},"); 7442 7443 // Avoid breaking between equal sign and opening brace 7444 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 7445 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 7446 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 7447 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 7448 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 7449 " {\"ccccccccccccccccccccc\", 2}};", 7450 AvoidBreakingFirstArgument); 7451 7452 // Binpacking only if there is no trailing comma 7453 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 7454 " cccccccccc, dddddddddd};", 7455 getLLVMStyleWithColumns(50)); 7456 verifyFormat("const Aaaaaa aaaaa = {\n" 7457 " aaaaaaaaaaa,\n" 7458 " bbbbbbbbbbb,\n" 7459 " ccccccccccc,\n" 7460 " ddddddddddd,\n" 7461 "};", getLLVMStyleWithColumns(50)); 7462 7463 // Cases where distinguising braced lists and blocks is hard. 7464 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 7465 verifyFormat("void f() {\n" 7466 " return; // comment\n" 7467 "}\n" 7468 "SomeType t;"); 7469 verifyFormat("void f() {\n" 7470 " if (a) {\n" 7471 " f();\n" 7472 " }\n" 7473 "}\n" 7474 "SomeType t;"); 7475 7476 // In combination with BinPackArguments = false. 7477 FormatStyle NoBinPacking = getLLVMStyle(); 7478 NoBinPacking.BinPackArguments = false; 7479 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 7480 " bbbbb,\n" 7481 " ccccc,\n" 7482 " ddddd,\n" 7483 " eeeee,\n" 7484 " ffffff,\n" 7485 " ggggg,\n" 7486 " hhhhhh,\n" 7487 " iiiiii,\n" 7488 " jjjjjj,\n" 7489 " kkkkkk};", 7490 NoBinPacking); 7491 verifyFormat("const Aaaaaa aaaaa = {\n" 7492 " aaaaa,\n" 7493 " bbbbb,\n" 7494 " ccccc,\n" 7495 " ddddd,\n" 7496 " eeeee,\n" 7497 " ffffff,\n" 7498 " ggggg,\n" 7499 " hhhhhh,\n" 7500 " iiiiii,\n" 7501 " jjjjjj,\n" 7502 " kkkkkk,\n" 7503 "};", 7504 NoBinPacking); 7505 verifyFormat( 7506 "const Aaaaaa aaaaa = {\n" 7507 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 7508 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 7509 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 7510 "};", 7511 NoBinPacking); 7512 7513 // FIXME: The alignment of these trailing comments might be bad. Then again, 7514 // this might be utterly useless in real code. 7515 verifyFormat("Constructor::Constructor()\n" 7516 " : some_value{ //\n" 7517 " aaaaaaa, //\n" 7518 " bbbbbbb} {}"); 7519 7520 // In braced lists, the first comment is always assumed to belong to the 7521 // first element. Thus, it can be moved to the next or previous line as 7522 // appropriate. 7523 EXPECT_EQ("function({// First element:\n" 7524 " 1,\n" 7525 " // Second element:\n" 7526 " 2});", 7527 format("function({\n" 7528 " // First element:\n" 7529 " 1,\n" 7530 " // Second element:\n" 7531 " 2});")); 7532 EXPECT_EQ("std::vector<int> MyNumbers{\n" 7533 " // First element:\n" 7534 " 1,\n" 7535 " // Second element:\n" 7536 " 2};", 7537 format("std::vector<int> MyNumbers{// First element:\n" 7538 " 1,\n" 7539 " // Second element:\n" 7540 " 2};", 7541 getLLVMStyleWithColumns(30))); 7542 // A trailing comma should still lead to an enforced line break and no 7543 // binpacking. 7544 EXPECT_EQ("vector<int> SomeVector = {\n" 7545 " // aaa\n" 7546 " 1,\n" 7547 " 2,\n" 7548 "};", 7549 format("vector<int> SomeVector = { // aaa\n" 7550 " 1, 2, };")); 7551 7552 FormatStyle ExtraSpaces = getLLVMStyle(); 7553 ExtraSpaces.Cpp11BracedListStyle = false; 7554 ExtraSpaces.ColumnLimit = 75; 7555 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 7556 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 7557 verifyFormat("f({ 1, 2 });", ExtraSpaces); 7558 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 7559 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 7560 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 7561 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 7562 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 7563 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 7564 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 7565 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 7566 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 7567 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 7568 verifyFormat("class Class {\n" 7569 " T member = { arg1, arg2 };\n" 7570 "};", 7571 ExtraSpaces); 7572 verifyFormat( 7573 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7574 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 7575 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 7576 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 7577 ExtraSpaces); 7578 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 7579 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 7580 ExtraSpaces); 7581 verifyFormat( 7582 "someFunction(OtherParam,\n" 7583 " BracedList{ // comment 1 (Forcing interesting break)\n" 7584 " param1, param2,\n" 7585 " // comment 2\n" 7586 " param3, param4 });", 7587 ExtraSpaces); 7588 verifyFormat( 7589 "std::this_thread::sleep_for(\n" 7590 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 7591 ExtraSpaces); 7592 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 7593 " aaaaaaa,\n" 7594 " aaaaaaaaaa,\n" 7595 " aaaaa,\n" 7596 " aaaaaaaaaaaaaaa,\n" 7597 " aaa,\n" 7598 " aaaaaaaaaa,\n" 7599 " a,\n" 7600 " aaaaaaaaaaaaaaaaaaaaa,\n" 7601 " aaaaaaaaaaaa,\n" 7602 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 7603 " aaaaaaa,\n" 7604 " a};"); 7605 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 7606 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 7607 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 7608 7609 // Avoid breaking between initializer/equal sign and opening brace 7610 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 7611 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 7612 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7613 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7614 " { \"ccccccccccccccccccccc\", 2 }\n" 7615 "};", 7616 ExtraSpaces); 7617 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 7618 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7619 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7620 " { \"ccccccccccccccccccccc\", 2 }\n" 7621 "};", 7622 ExtraSpaces); 7623 7624 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 7625 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 7626 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 7627 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 7628 } 7629 7630 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 7631 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7632 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7633 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7634 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7635 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7636 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7637 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 7638 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7639 " 1, 22, 333, 4444, 55555, //\n" 7640 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7641 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7642 verifyFormat( 7643 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7644 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7645 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 7646 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7647 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7648 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7649 " 7777777};"); 7650 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7651 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7652 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7653 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7654 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7655 " // Separating comment.\n" 7656 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7657 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7658 " // Leading comment\n" 7659 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7660 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7661 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7662 " 1, 1, 1, 1};", 7663 getLLVMStyleWithColumns(39)); 7664 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7665 " 1, 1, 1, 1};", 7666 getLLVMStyleWithColumns(38)); 7667 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 7668 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 7669 getLLVMStyleWithColumns(43)); 7670 verifyFormat( 7671 "static unsigned SomeValues[10][3] = {\n" 7672 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 7673 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 7674 verifyFormat("static auto fields = new vector<string>{\n" 7675 " \"aaaaaaaaaaaaa\",\n" 7676 " \"aaaaaaaaaaaaa\",\n" 7677 " \"aaaaaaaaaaaa\",\n" 7678 " \"aaaaaaaaaaaaaa\",\n" 7679 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7680 " \"aaaaaaaaaaaa\",\n" 7681 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7682 "};"); 7683 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 7684 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 7685 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 7686 " 3, cccccccccccccccccccccc};", 7687 getLLVMStyleWithColumns(60)); 7688 7689 // Trailing commas. 7690 verifyFormat("vector<int> x = {\n" 7691 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 7692 "};", 7693 getLLVMStyleWithColumns(39)); 7694 verifyFormat("vector<int> x = {\n" 7695 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 7696 "};", 7697 getLLVMStyleWithColumns(39)); 7698 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7699 " 1, 1, 1, 1,\n" 7700 " /**/ /**/};", 7701 getLLVMStyleWithColumns(39)); 7702 7703 // Trailing comment in the first line. 7704 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 7705 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 7706 " 111111111, 222222222, 3333333333, 444444444, //\n" 7707 " 11111111, 22222222, 333333333, 44444444};"); 7708 // Trailing comment in the last line. 7709 verifyFormat("int aaaaa[] = {\n" 7710 " 1, 2, 3, // comment\n" 7711 " 4, 5, 6 // comment\n" 7712 "};"); 7713 7714 // With nested lists, we should either format one item per line or all nested 7715 // lists one on line. 7716 // FIXME: For some nested lists, we can do better. 7717 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 7718 " {aaaaaaaaaaaaaaaaaaa},\n" 7719 " {aaaaaaaaaaaaaaaaaaaaa},\n" 7720 " {aaaaaaaaaaaaaaaaa}};", 7721 getLLVMStyleWithColumns(60)); 7722 verifyFormat( 7723 "SomeStruct my_struct_array = {\n" 7724 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 7725 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 7726 " {aaa, aaa},\n" 7727 " {aaa, aaa},\n" 7728 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 7729 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7730 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 7731 7732 // No column layout should be used here. 7733 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 7734 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 7735 7736 verifyNoCrash("a<,"); 7737 7738 // No braced initializer here. 7739 verifyFormat("void f() {\n" 7740 " struct Dummy {};\n" 7741 " f(v);\n" 7742 "}"); 7743 7744 // Long lists should be formatted in columns even if they are nested. 7745 verifyFormat( 7746 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7747 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7748 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7749 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7750 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7751 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 7752 7753 // Allow "single-column" layout even if that violates the column limit. There 7754 // isn't going to be a better way. 7755 verifyFormat("std::vector<int> a = {\n" 7756 " aaaaaaaa,\n" 7757 " aaaaaaaa,\n" 7758 " aaaaaaaa,\n" 7759 " aaaaaaaa,\n" 7760 " aaaaaaaaaa,\n" 7761 " aaaaaaaa,\n" 7762 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 7763 getLLVMStyleWithColumns(30)); 7764 verifyFormat("vector<int> aaaa = {\n" 7765 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7766 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7767 " aaaaaa.aaaaaaa,\n" 7768 " aaaaaa.aaaaaaa,\n" 7769 " aaaaaa.aaaaaaa,\n" 7770 " aaaaaa.aaaaaaa,\n" 7771 "};"); 7772 7773 // Don't create hanging lists. 7774 verifyFormat("someFunction(Param, {List1, List2,\n" 7775 " List3});", 7776 getLLVMStyleWithColumns(35)); 7777 verifyFormat("someFunction(Param, Param,\n" 7778 " {List1, List2,\n" 7779 " List3});", 7780 getLLVMStyleWithColumns(35)); 7781 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 7782 " aaaaaaaaaaaaaaaaaaaaaaa);"); 7783 } 7784 7785 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 7786 FormatStyle DoNotMerge = getLLVMStyle(); 7787 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7788 7789 verifyFormat("void f() { return 42; }"); 7790 verifyFormat("void f() {\n" 7791 " return 42;\n" 7792 "}", 7793 DoNotMerge); 7794 verifyFormat("void f() {\n" 7795 " // Comment\n" 7796 "}"); 7797 verifyFormat("{\n" 7798 "#error {\n" 7799 " int a;\n" 7800 "}"); 7801 verifyFormat("{\n" 7802 " int a;\n" 7803 "#error {\n" 7804 "}"); 7805 verifyFormat("void f() {} // comment"); 7806 verifyFormat("void f() { int a; } // comment"); 7807 verifyFormat("void f() {\n" 7808 "} // comment", 7809 DoNotMerge); 7810 verifyFormat("void f() {\n" 7811 " int a;\n" 7812 "} // comment", 7813 DoNotMerge); 7814 verifyFormat("void f() {\n" 7815 "} // comment", 7816 getLLVMStyleWithColumns(15)); 7817 7818 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 7819 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 7820 7821 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 7822 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 7823 verifyFormat("class C {\n" 7824 " C()\n" 7825 " : iiiiiiii(nullptr),\n" 7826 " kkkkkkk(nullptr),\n" 7827 " mmmmmmm(nullptr),\n" 7828 " nnnnnnn(nullptr) {}\n" 7829 "};", 7830 getGoogleStyle()); 7831 7832 FormatStyle NoColumnLimit = getLLVMStyle(); 7833 NoColumnLimit.ColumnLimit = 0; 7834 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 7835 EXPECT_EQ("class C {\n" 7836 " A() : b(0) {}\n" 7837 "};", 7838 format("class C{A():b(0){}};", NoColumnLimit)); 7839 EXPECT_EQ("A()\n" 7840 " : b(0) {\n" 7841 "}", 7842 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 7843 7844 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 7845 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 7846 FormatStyle::SFS_None; 7847 EXPECT_EQ("A()\n" 7848 " : b(0) {\n" 7849 "}", 7850 format("A():b(0){}", DoNotMergeNoColumnLimit)); 7851 EXPECT_EQ("A()\n" 7852 " : b(0) {\n" 7853 "}", 7854 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 7855 7856 verifyFormat("#define A \\\n" 7857 " void f() { \\\n" 7858 " int i; \\\n" 7859 " }", 7860 getLLVMStyleWithColumns(20)); 7861 verifyFormat("#define A \\\n" 7862 " void f() { int i; }", 7863 getLLVMStyleWithColumns(21)); 7864 verifyFormat("#define A \\\n" 7865 " void f() { \\\n" 7866 " int i; \\\n" 7867 " } \\\n" 7868 " int j;", 7869 getLLVMStyleWithColumns(22)); 7870 verifyFormat("#define A \\\n" 7871 " void f() { int i; } \\\n" 7872 " int j;", 7873 getLLVMStyleWithColumns(23)); 7874 } 7875 7876 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 7877 FormatStyle MergeEmptyOnly = getLLVMStyle(); 7878 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7879 verifyFormat("class C {\n" 7880 " int f() {}\n" 7881 "};", 7882 MergeEmptyOnly); 7883 verifyFormat("class C {\n" 7884 " int f() {\n" 7885 " return 42;\n" 7886 " }\n" 7887 "};", 7888 MergeEmptyOnly); 7889 verifyFormat("int f() {}", MergeEmptyOnly); 7890 verifyFormat("int f() {\n" 7891 " return 42;\n" 7892 "}", 7893 MergeEmptyOnly); 7894 7895 // Also verify behavior when BraceWrapping.AfterFunction = true 7896 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7897 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 7898 verifyFormat("int f() {}", MergeEmptyOnly); 7899 verifyFormat("class C {\n" 7900 " int f() {}\n" 7901 "};", 7902 MergeEmptyOnly); 7903 } 7904 7905 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 7906 FormatStyle MergeInlineOnly = getLLVMStyle(); 7907 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7908 verifyFormat("class C {\n" 7909 " int f() { return 42; }\n" 7910 "};", 7911 MergeInlineOnly); 7912 verifyFormat("int f() {\n" 7913 " return 42;\n" 7914 "}", 7915 MergeInlineOnly); 7916 7917 // SFS_Inline implies SFS_Empty 7918 verifyFormat("class C {\n" 7919 " int f() {}\n" 7920 "};", 7921 MergeInlineOnly); 7922 verifyFormat("int f() {}", MergeInlineOnly); 7923 7924 // Also verify behavior when BraceWrapping.AfterFunction = true 7925 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7926 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7927 verifyFormat("class C {\n" 7928 " int f() { return 42; }\n" 7929 "};", 7930 MergeInlineOnly); 7931 verifyFormat("int f()\n" 7932 "{\n" 7933 " return 42;\n" 7934 "}", 7935 MergeInlineOnly); 7936 7937 // SFS_Inline implies SFS_Empty 7938 verifyFormat("int f() {}", MergeInlineOnly); 7939 verifyFormat("class C {\n" 7940 " int f() {}\n" 7941 "};", 7942 MergeInlineOnly); 7943 } 7944 7945 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 7946 FormatStyle MergeInlineOnly = getLLVMStyle(); 7947 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 7948 FormatStyle::SFS_InlineOnly; 7949 verifyFormat("class C {\n" 7950 " int f() { return 42; }\n" 7951 "};", 7952 MergeInlineOnly); 7953 verifyFormat("int f() {\n" 7954 " return 42;\n" 7955 "}", 7956 MergeInlineOnly); 7957 7958 // SFS_InlineOnly does not imply SFS_Empty 7959 verifyFormat("class C {\n" 7960 " int f() {}\n" 7961 "};", 7962 MergeInlineOnly); 7963 verifyFormat("int f() {\n" 7964 "}", 7965 MergeInlineOnly); 7966 7967 // Also verify behavior when BraceWrapping.AfterFunction = true 7968 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7969 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7970 verifyFormat("class C {\n" 7971 " int f() { return 42; }\n" 7972 "};", 7973 MergeInlineOnly); 7974 verifyFormat("int f()\n" 7975 "{\n" 7976 " return 42;\n" 7977 "}", 7978 MergeInlineOnly); 7979 7980 // SFS_InlineOnly does not imply SFS_Empty 7981 verifyFormat("int f()\n" 7982 "{\n" 7983 "}", 7984 MergeInlineOnly); 7985 verifyFormat("class C {\n" 7986 " int f() {}\n" 7987 "};", 7988 MergeInlineOnly); 7989 } 7990 7991 TEST_F(FormatTest, SplitEmptyFunction) { 7992 FormatStyle Style = getLLVMStyle(); 7993 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7994 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7995 Style.BraceWrapping.AfterFunction = true; 7996 Style.BraceWrapping.SplitEmptyFunction = false; 7997 Style.ColumnLimit = 40; 7998 7999 verifyFormat("int f()\n" 8000 "{}", 8001 Style); 8002 verifyFormat("int f()\n" 8003 "{\n" 8004 " return 42;\n" 8005 "}", 8006 Style); 8007 verifyFormat("int f()\n" 8008 "{\n" 8009 " // some comment\n" 8010 "}", 8011 Style); 8012 8013 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 8014 verifyFormat("int f() {}", Style); 8015 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8016 "{}", 8017 Style); 8018 verifyFormat("int f()\n" 8019 "{\n" 8020 " return 0;\n" 8021 "}", 8022 Style); 8023 8024 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 8025 verifyFormat("class Foo {\n" 8026 " int f() {}\n" 8027 "};\n", 8028 Style); 8029 verifyFormat("class Foo {\n" 8030 " int f() { return 0; }\n" 8031 "};\n", 8032 Style); 8033 verifyFormat("class Foo {\n" 8034 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8035 " {}\n" 8036 "};\n", 8037 Style); 8038 verifyFormat("class Foo {\n" 8039 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8040 " {\n" 8041 " return 0;\n" 8042 " }\n" 8043 "};\n", 8044 Style); 8045 8046 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 8047 verifyFormat("int f() {}", Style); 8048 verifyFormat("int f() { return 0; }", Style); 8049 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8050 "{}", 8051 Style); 8052 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8053 "{\n" 8054 " return 0;\n" 8055 "}", 8056 Style); 8057 } 8058 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 8059 FormatStyle Style = getLLVMStyle(); 8060 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 8061 verifyFormat("#ifdef A\n" 8062 "int f() {}\n" 8063 "#else\n" 8064 "int g() {}\n" 8065 "#endif", 8066 Style); 8067 } 8068 8069 TEST_F(FormatTest, SplitEmptyClass) { 8070 FormatStyle Style = getLLVMStyle(); 8071 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8072 Style.BraceWrapping.AfterClass = true; 8073 Style.BraceWrapping.SplitEmptyRecord = false; 8074 8075 verifyFormat("class Foo\n" 8076 "{};", 8077 Style); 8078 verifyFormat("/* something */ class Foo\n" 8079 "{};", 8080 Style); 8081 verifyFormat("template <typename X> class Foo\n" 8082 "{};", 8083 Style); 8084 verifyFormat("class Foo\n" 8085 "{\n" 8086 " Foo();\n" 8087 "};", 8088 Style); 8089 verifyFormat("typedef class Foo\n" 8090 "{\n" 8091 "} Foo_t;", 8092 Style); 8093 } 8094 8095 TEST_F(FormatTest, SplitEmptyStruct) { 8096 FormatStyle Style = getLLVMStyle(); 8097 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8098 Style.BraceWrapping.AfterStruct = true; 8099 Style.BraceWrapping.SplitEmptyRecord = false; 8100 8101 verifyFormat("struct Foo\n" 8102 "{};", 8103 Style); 8104 verifyFormat("/* something */ struct Foo\n" 8105 "{};", 8106 Style); 8107 verifyFormat("template <typename X> struct Foo\n" 8108 "{};", 8109 Style); 8110 verifyFormat("struct Foo\n" 8111 "{\n" 8112 " Foo();\n" 8113 "};", 8114 Style); 8115 verifyFormat("typedef struct Foo\n" 8116 "{\n" 8117 "} Foo_t;", 8118 Style); 8119 //typedef struct Bar {} Bar_t; 8120 } 8121 8122 TEST_F(FormatTest, SplitEmptyUnion) { 8123 FormatStyle Style = getLLVMStyle(); 8124 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8125 Style.BraceWrapping.AfterUnion = true; 8126 Style.BraceWrapping.SplitEmptyRecord = false; 8127 8128 verifyFormat("union Foo\n" 8129 "{};", 8130 Style); 8131 verifyFormat("/* something */ union Foo\n" 8132 "{};", 8133 Style); 8134 verifyFormat("union Foo\n" 8135 "{\n" 8136 " A,\n" 8137 "};", 8138 Style); 8139 verifyFormat("typedef union Foo\n" 8140 "{\n" 8141 "} Foo_t;", 8142 Style); 8143 } 8144 8145 TEST_F(FormatTest, SplitEmptyNamespace) { 8146 FormatStyle Style = getLLVMStyle(); 8147 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8148 Style.BraceWrapping.AfterNamespace = true; 8149 Style.BraceWrapping.SplitEmptyNamespace = false; 8150 8151 verifyFormat("namespace Foo\n" 8152 "{};", 8153 Style); 8154 verifyFormat("/* something */ namespace Foo\n" 8155 "{};", 8156 Style); 8157 verifyFormat("inline namespace Foo\n" 8158 "{};", 8159 Style); 8160 verifyFormat("/* something */ inline namespace Foo\n" 8161 "{};", 8162 Style); 8163 verifyFormat("export namespace Foo\n" 8164 "{};", 8165 Style); 8166 verifyFormat("namespace Foo\n" 8167 "{\n" 8168 "void Bar();\n" 8169 "};", 8170 Style); 8171 } 8172 8173 TEST_F(FormatTest, NeverMergeShortRecords) { 8174 FormatStyle Style = getLLVMStyle(); 8175 8176 verifyFormat("class Foo {\n" 8177 " Foo();\n" 8178 "};", 8179 Style); 8180 verifyFormat("typedef class Foo {\n" 8181 " Foo();\n" 8182 "} Foo_t;", 8183 Style); 8184 verifyFormat("struct Foo {\n" 8185 " Foo();\n" 8186 "};", 8187 Style); 8188 verifyFormat("typedef struct Foo {\n" 8189 " Foo();\n" 8190 "} Foo_t;", 8191 Style); 8192 verifyFormat("union Foo {\n" 8193 " A,\n" 8194 "};", 8195 Style); 8196 verifyFormat("typedef union Foo {\n" 8197 " A,\n" 8198 "} Foo_t;", 8199 Style); 8200 verifyFormat("namespace Foo {\n" 8201 "void Bar();\n" 8202 "};", 8203 Style); 8204 8205 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8206 Style.BraceWrapping.AfterClass = true; 8207 Style.BraceWrapping.AfterStruct = true; 8208 Style.BraceWrapping.AfterUnion = true; 8209 Style.BraceWrapping.AfterNamespace = true; 8210 verifyFormat("class Foo\n" 8211 "{\n" 8212 " Foo();\n" 8213 "};", 8214 Style); 8215 verifyFormat("typedef class Foo\n" 8216 "{\n" 8217 " Foo();\n" 8218 "} Foo_t;", 8219 Style); 8220 verifyFormat("struct Foo\n" 8221 "{\n" 8222 " Foo();\n" 8223 "};", 8224 Style); 8225 verifyFormat("typedef struct Foo\n" 8226 "{\n" 8227 " Foo();\n" 8228 "} Foo_t;", 8229 Style); 8230 verifyFormat("union Foo\n" 8231 "{\n" 8232 " A,\n" 8233 "};", 8234 Style); 8235 verifyFormat("typedef union Foo\n" 8236 "{\n" 8237 " A,\n" 8238 "} Foo_t;", 8239 Style); 8240 verifyFormat("namespace Foo\n" 8241 "{\n" 8242 "void Bar();\n" 8243 "};", 8244 Style); 8245 } 8246 8247 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 8248 // Elaborate type variable declarations. 8249 verifyFormat("struct foo a = {bar};\nint n;"); 8250 verifyFormat("class foo a = {bar};\nint n;"); 8251 verifyFormat("union foo a = {bar};\nint n;"); 8252 8253 // Elaborate types inside function definitions. 8254 verifyFormat("struct foo f() {}\nint n;"); 8255 verifyFormat("class foo f() {}\nint n;"); 8256 verifyFormat("union foo f() {}\nint n;"); 8257 8258 // Templates. 8259 verifyFormat("template <class X> void f() {}\nint n;"); 8260 verifyFormat("template <struct X> void f() {}\nint n;"); 8261 verifyFormat("template <union X> void f() {}\nint n;"); 8262 8263 // Actual definitions... 8264 verifyFormat("struct {\n} n;"); 8265 verifyFormat( 8266 "template <template <class T, class Y>, class Z> class X {\n} n;"); 8267 verifyFormat("union Z {\n int n;\n} x;"); 8268 verifyFormat("class MACRO Z {\n} n;"); 8269 verifyFormat("class MACRO(X) Z {\n} n;"); 8270 verifyFormat("class __attribute__(X) Z {\n} n;"); 8271 verifyFormat("class __declspec(X) Z {\n} n;"); 8272 verifyFormat("class A##B##C {\n} n;"); 8273 verifyFormat("class alignas(16) Z {\n} n;"); 8274 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 8275 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 8276 8277 // Redefinition from nested context: 8278 verifyFormat("class A::B::C {\n} n;"); 8279 8280 // Template definitions. 8281 verifyFormat( 8282 "template <typename F>\n" 8283 "Matcher(const Matcher<F> &Other,\n" 8284 " typename enable_if_c<is_base_of<F, T>::value &&\n" 8285 " !is_same<F, T>::value>::type * = 0)\n" 8286 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 8287 8288 // FIXME: This is still incorrectly handled at the formatter side. 8289 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 8290 verifyFormat("int i = SomeFunction(a<b, a> b);"); 8291 8292 // FIXME: 8293 // This now gets parsed incorrectly as class definition. 8294 // verifyFormat("class A<int> f() {\n}\nint n;"); 8295 8296 // Elaborate types where incorrectly parsing the structural element would 8297 // break the indent. 8298 verifyFormat("if (true)\n" 8299 " class X x;\n" 8300 "else\n" 8301 " f();\n"); 8302 8303 // This is simply incomplete. Formatting is not important, but must not crash. 8304 verifyFormat("class A:"); 8305 } 8306 8307 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 8308 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 8309 format("#error Leave all white!!!!! space* alone!\n")); 8310 EXPECT_EQ( 8311 "#warning Leave all white!!!!! space* alone!\n", 8312 format("#warning Leave all white!!!!! space* alone!\n")); 8313 EXPECT_EQ("#error 1", format(" # error 1")); 8314 EXPECT_EQ("#warning 1", format(" # warning 1")); 8315 } 8316 8317 TEST_F(FormatTest, FormatHashIfExpressions) { 8318 verifyFormat("#if AAAA && BBBB"); 8319 verifyFormat("#if (AAAA && BBBB)"); 8320 verifyFormat("#elif (AAAA && BBBB)"); 8321 // FIXME: Come up with a better indentation for #elif. 8322 verifyFormat( 8323 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 8324 " defined(BBBBBBBB)\n" 8325 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 8326 " defined(BBBBBBBB)\n" 8327 "#endif", 8328 getLLVMStyleWithColumns(65)); 8329 } 8330 8331 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 8332 FormatStyle AllowsMergedIf = getGoogleStyle(); 8333 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 8334 FormatStyle::SIS_WithoutElse; 8335 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 8336 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 8337 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 8338 EXPECT_EQ("if (true) return 42;", 8339 format("if (true)\nreturn 42;", AllowsMergedIf)); 8340 FormatStyle ShortMergedIf = AllowsMergedIf; 8341 ShortMergedIf.ColumnLimit = 25; 8342 verifyFormat("#define A \\\n" 8343 " if (true) return 42;", 8344 ShortMergedIf); 8345 verifyFormat("#define A \\\n" 8346 " f(); \\\n" 8347 " if (true)\n" 8348 "#define B", 8349 ShortMergedIf); 8350 verifyFormat("#define A \\\n" 8351 " f(); \\\n" 8352 " if (true)\n" 8353 "g();", 8354 ShortMergedIf); 8355 verifyFormat("{\n" 8356 "#ifdef A\n" 8357 " // Comment\n" 8358 " if (true) continue;\n" 8359 "#endif\n" 8360 " // Comment\n" 8361 " if (true) continue;\n" 8362 "}", 8363 ShortMergedIf); 8364 ShortMergedIf.ColumnLimit = 33; 8365 verifyFormat("#define A \\\n" 8366 " if constexpr (true) return 42;", 8367 ShortMergedIf); 8368 ShortMergedIf.ColumnLimit = 29; 8369 verifyFormat("#define A \\\n" 8370 " if (aaaaaaaaaa) return 1; \\\n" 8371 " return 2;", 8372 ShortMergedIf); 8373 ShortMergedIf.ColumnLimit = 28; 8374 verifyFormat("#define A \\\n" 8375 " if (aaaaaaaaaa) \\\n" 8376 " return 1; \\\n" 8377 " return 2;", 8378 ShortMergedIf); 8379 verifyFormat("#define A \\\n" 8380 " if constexpr (aaaaaaa) \\\n" 8381 " return 1; \\\n" 8382 " return 2;", 8383 ShortMergedIf); 8384 } 8385 8386 TEST_F(FormatTest, FormatStarDependingOnContext) { 8387 verifyFormat("void f(int *a);"); 8388 verifyFormat("void f() { f(fint * b); }"); 8389 verifyFormat("class A {\n void f(int *a);\n};"); 8390 verifyFormat("class A {\n int *a;\n};"); 8391 verifyFormat("namespace a {\n" 8392 "namespace b {\n" 8393 "class A {\n" 8394 " void f() {}\n" 8395 " int *a;\n" 8396 "};\n" 8397 "} // namespace b\n" 8398 "} // namespace a"); 8399 } 8400 8401 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 8402 verifyFormat("while"); 8403 verifyFormat("operator"); 8404 } 8405 8406 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 8407 // This code would be painfully slow to format if we didn't skip it. 8408 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 8409 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8410 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8411 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8412 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8413 "A(1, 1)\n" 8414 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 8415 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8416 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8417 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8418 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8419 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8420 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8421 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8422 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8423 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 8424 // Deeply nested part is untouched, rest is formatted. 8425 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 8426 format(std::string("int i;\n") + Code + "int j;\n", 8427 getLLVMStyle(), SC_ExpectIncomplete)); 8428 } 8429 8430 //===----------------------------------------------------------------------===// 8431 // Objective-C tests. 8432 //===----------------------------------------------------------------------===// 8433 8434 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 8435 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 8436 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 8437 format("-(NSUInteger)indexOfObject:(id)anObject;")); 8438 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 8439 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 8440 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 8441 format("-(NSInteger)Method3:(id)anObject;")); 8442 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 8443 format("-(NSInteger)Method4:(id)anObject;")); 8444 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 8445 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 8446 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 8447 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 8448 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8449 "forAllCells:(BOOL)flag;", 8450 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8451 "forAllCells:(BOOL)flag;")); 8452 8453 // Very long objectiveC method declaration. 8454 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 8455 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 8456 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 8457 " inRange:(NSRange)range\n" 8458 " outRange:(NSRange)out_range\n" 8459 " outRange1:(NSRange)out_range1\n" 8460 " outRange2:(NSRange)out_range2\n" 8461 " outRange3:(NSRange)out_range3\n" 8462 " outRange4:(NSRange)out_range4\n" 8463 " outRange5:(NSRange)out_range5\n" 8464 " outRange6:(NSRange)out_range6\n" 8465 " outRange7:(NSRange)out_range7\n" 8466 " outRange8:(NSRange)out_range8\n" 8467 " outRange9:(NSRange)out_range9;"); 8468 8469 // When the function name has to be wrapped. 8470 FormatStyle Style = getLLVMStyle(); 8471 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 8472 // and always indents instead. 8473 Style.IndentWrappedFunctionNames = false; 8474 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8475 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 8476 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 8477 "}", 8478 Style); 8479 Style.IndentWrappedFunctionNames = true; 8480 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8481 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 8482 " anotherName:(NSString)dddddddddddddd {\n" 8483 "}", 8484 Style); 8485 8486 verifyFormat("- (int)sum:(vector<int>)numbers;"); 8487 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 8488 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 8489 // protocol lists (but not for template classes): 8490 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 8491 8492 verifyFormat("- (int (*)())foo:(int (*)())f;"); 8493 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 8494 8495 // If there's no return type (very rare in practice!), LLVM and Google style 8496 // agree. 8497 verifyFormat("- foo;"); 8498 verifyFormat("- foo:(int)f;"); 8499 verifyGoogleFormat("- foo:(int)foo;"); 8500 } 8501 8502 8503 TEST_F(FormatTest, BreaksStringLiterals) { 8504 EXPECT_EQ("\"some text \"\n" 8505 "\"other\";", 8506 format("\"some text other\";", getLLVMStyleWithColumns(12))); 8507 EXPECT_EQ("\"some text \"\n" 8508 "\"other\";", 8509 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 8510 EXPECT_EQ( 8511 "#define A \\\n" 8512 " \"some \" \\\n" 8513 " \"text \" \\\n" 8514 " \"other\";", 8515 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 8516 EXPECT_EQ( 8517 "#define A \\\n" 8518 " \"so \" \\\n" 8519 " \"text \" \\\n" 8520 " \"other\";", 8521 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 8522 8523 EXPECT_EQ("\"some text\"", 8524 format("\"some text\"", getLLVMStyleWithColumns(1))); 8525 EXPECT_EQ("\"some text\"", 8526 format("\"some text\"", getLLVMStyleWithColumns(11))); 8527 EXPECT_EQ("\"some \"\n" 8528 "\"text\"", 8529 format("\"some text\"", getLLVMStyleWithColumns(10))); 8530 EXPECT_EQ("\"some \"\n" 8531 "\"text\"", 8532 format("\"some text\"", getLLVMStyleWithColumns(7))); 8533 EXPECT_EQ("\"some\"\n" 8534 "\" tex\"\n" 8535 "\"t\"", 8536 format("\"some text\"", getLLVMStyleWithColumns(6))); 8537 EXPECT_EQ("\"some\"\n" 8538 "\" tex\"\n" 8539 "\" and\"", 8540 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 8541 EXPECT_EQ("\"some\"\n" 8542 "\"/tex\"\n" 8543 "\"/and\"", 8544 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 8545 8546 EXPECT_EQ("variable =\n" 8547 " \"long string \"\n" 8548 " \"literal\";", 8549 format("variable = \"long string literal\";", 8550 getLLVMStyleWithColumns(20))); 8551 8552 EXPECT_EQ("variable = f(\n" 8553 " \"long string \"\n" 8554 " \"literal\",\n" 8555 " short,\n" 8556 " loooooooooooooooooooong);", 8557 format("variable = f(\"long string literal\", short, " 8558 "loooooooooooooooooooong);", 8559 getLLVMStyleWithColumns(20))); 8560 8561 EXPECT_EQ( 8562 "f(g(\"long string \"\n" 8563 " \"literal\"),\n" 8564 " b);", 8565 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 8566 EXPECT_EQ("f(g(\"long string \"\n" 8567 " \"literal\",\n" 8568 " a),\n" 8569 " b);", 8570 format("f(g(\"long string literal\", a), b);", 8571 getLLVMStyleWithColumns(20))); 8572 EXPECT_EQ( 8573 "f(\"one two\".split(\n" 8574 " variable));", 8575 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 8576 EXPECT_EQ("f(\"one two three four five six \"\n" 8577 " \"seven\".split(\n" 8578 " really_looooong_variable));", 8579 format("f(\"one two three four five six seven\"." 8580 "split(really_looooong_variable));", 8581 getLLVMStyleWithColumns(33))); 8582 8583 EXPECT_EQ("f(\"some \"\n" 8584 " \"text\",\n" 8585 " other);", 8586 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 8587 8588 // Only break as a last resort. 8589 verifyFormat( 8590 "aaaaaaaaaaaaaaaaaaaa(\n" 8591 " aaaaaaaaaaaaaaaaaaaa,\n" 8592 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 8593 8594 EXPECT_EQ("\"splitmea\"\n" 8595 "\"trandomp\"\n" 8596 "\"oint\"", 8597 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 8598 8599 EXPECT_EQ("\"split/\"\n" 8600 "\"pathat/\"\n" 8601 "\"slashes\"", 8602 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8603 8604 EXPECT_EQ("\"split/\"\n" 8605 "\"pathat/\"\n" 8606 "\"slashes\"", 8607 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8608 EXPECT_EQ("\"split at \"\n" 8609 "\"spaces/at/\"\n" 8610 "\"slashes.at.any$\"\n" 8611 "\"non-alphanumeric%\"\n" 8612 "\"1111111111characte\"\n" 8613 "\"rs\"", 8614 format("\"split at " 8615 "spaces/at/" 8616 "slashes.at." 8617 "any$non-" 8618 "alphanumeric%" 8619 "1111111111characte" 8620 "rs\"", 8621 getLLVMStyleWithColumns(20))); 8622 8623 // Verify that splitting the strings understands 8624 // Style::AlwaysBreakBeforeMultilineStrings. 8625 EXPECT_EQ( 8626 "aaaaaaaaaaaa(\n" 8627 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 8628 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 8629 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 8630 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8631 "aaaaaaaaaaaaaaaaaaaaaa\");", 8632 getGoogleStyle())); 8633 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8634 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8635 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8636 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8637 "aaaaaaaaaaaaaaaaaaaaaa\";", 8638 getGoogleStyle())); 8639 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8640 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8641 format("llvm::outs() << " 8642 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8643 "aaaaaaaaaaaaaaaaaaa\";")); 8644 EXPECT_EQ("ffff(\n" 8645 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8646 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8647 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8648 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8649 getGoogleStyle())); 8650 8651 FormatStyle Style = getLLVMStyleWithColumns(12); 8652 Style.BreakStringLiterals = false; 8653 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8654 8655 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8656 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8657 EXPECT_EQ("#define A \\\n" 8658 " \"some \" \\\n" 8659 " \"text \" \\\n" 8660 " \"other\";", 8661 format("#define A \"some text other\";", AlignLeft)); 8662 } 8663 8664 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 8665 EXPECT_EQ("C a = \"some more \"\n" 8666 " \"text\";", 8667 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 8668 } 8669 8670 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8671 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8672 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8673 EXPECT_EQ("int i = a(b());", 8674 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8675 } 8676 8677 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8678 EXPECT_EQ( 8679 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8680 "(\n" 8681 " \"x\t\");", 8682 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8683 "aaaaaaa(" 8684 "\"x\t\");")); 8685 } 8686 8687 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8688 EXPECT_EQ( 8689 "u8\"utf8 string \"\n" 8690 "u8\"literal\";", 8691 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8692 EXPECT_EQ( 8693 "u\"utf16 string \"\n" 8694 "u\"literal\";", 8695 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8696 EXPECT_EQ( 8697 "U\"utf32 string \"\n" 8698 "U\"literal\";", 8699 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8700 EXPECT_EQ("L\"wide string \"\n" 8701 "L\"literal\";", 8702 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8703 EXPECT_EQ("@\"NSString \"\n" 8704 "@\"literal\";", 8705 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8706 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 8707 8708 // This input makes clang-format try to split the incomplete unicode escape 8709 // sequence, which used to lead to a crasher. 8710 verifyNoCrash( 8711 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8712 getLLVMStyleWithColumns(60)); 8713 } 8714 8715 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8716 FormatStyle Style = getGoogleStyleWithColumns(15); 8717 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8718 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8719 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8720 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8721 EXPECT_EQ("u8R\"x(raw literal)x\";", 8722 format("u8R\"x(raw literal)x\";", Style)); 8723 } 8724 8725 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8726 FormatStyle Style = getLLVMStyleWithColumns(20); 8727 EXPECT_EQ( 8728 "_T(\"aaaaaaaaaaaaaa\")\n" 8729 "_T(\"aaaaaaaaaaaaaa\")\n" 8730 "_T(\"aaaaaaaaaaaa\")", 8731 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8732 EXPECT_EQ("f(x,\n" 8733 " _T(\"aaaaaaaaaaaa\")\n" 8734 " _T(\"aaa\"),\n" 8735 " z);", 8736 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8737 8738 // FIXME: Handle embedded spaces in one iteration. 8739 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8740 // "_T(\"aaaaaaaaaaaaa\")\n" 8741 // "_T(\"aaaaaaaaaaaaa\")\n" 8742 // "_T(\"a\")", 8743 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8744 // getLLVMStyleWithColumns(20))); 8745 EXPECT_EQ( 8746 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8747 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8748 EXPECT_EQ("f(\n" 8749 "#if !TEST\n" 8750 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8751 "#endif\n" 8752 ");", 8753 format("f(\n" 8754 "#if !TEST\n" 8755 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8756 "#endif\n" 8757 ");")); 8758 EXPECT_EQ("f(\n" 8759 "\n" 8760 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8761 format("f(\n" 8762 "\n" 8763 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8764 } 8765 8766 TEST_F(FormatTest, BreaksStringLiteralOperands) { 8767 // In a function call with two operands, the second can be broken with no line 8768 // break before it. 8769 EXPECT_EQ("func(a, \"long long \"\n" 8770 " \"long long\");", 8771 format("func(a, \"long long long long\");", 8772 getLLVMStyleWithColumns(24))); 8773 // In a function call with three operands, the second must be broken with a 8774 // line break before it. 8775 EXPECT_EQ("func(a,\n" 8776 " \"long long long \"\n" 8777 " \"long\",\n" 8778 " c);", 8779 format("func(a, \"long long long long\", c);", 8780 getLLVMStyleWithColumns(24))); 8781 // In a function call with three operands, the third must be broken with a 8782 // line break before it. 8783 EXPECT_EQ("func(a, b,\n" 8784 " \"long long long \"\n" 8785 " \"long\");", 8786 format("func(a, b, \"long long long long\");", 8787 getLLVMStyleWithColumns(24))); 8788 // In a function call with three operands, both the second and the third must 8789 // be broken with a line break before them. 8790 EXPECT_EQ("func(a,\n" 8791 " \"long long long \"\n" 8792 " \"long\",\n" 8793 " \"long long long \"\n" 8794 " \"long\");", 8795 format("func(a, \"long long long long\", \"long long long long\");", 8796 getLLVMStyleWithColumns(24))); 8797 // In a chain of << with two operands, the second can be broken with no line 8798 // break before it. 8799 EXPECT_EQ("a << \"line line \"\n" 8800 " \"line\";", 8801 format("a << \"line line line\";", 8802 getLLVMStyleWithColumns(20))); 8803 // In a chain of << with three operands, the second can be broken with no line 8804 // break before it. 8805 EXPECT_EQ("abcde << \"line \"\n" 8806 " \"line line\"\n" 8807 " << c;", 8808 format("abcde << \"line line line\" << c;", 8809 getLLVMStyleWithColumns(20))); 8810 // In a chain of << with three operands, the third must be broken with a line 8811 // break before it. 8812 EXPECT_EQ("a << b\n" 8813 " << \"line line \"\n" 8814 " \"line\";", 8815 format("a << b << \"line line line\";", 8816 getLLVMStyleWithColumns(20))); 8817 // In a chain of << with three operands, the second can be broken with no line 8818 // break before it and the third must be broken with a line break before it. 8819 EXPECT_EQ("abcd << \"line line \"\n" 8820 " \"line\"\n" 8821 " << \"line line \"\n" 8822 " \"line\";", 8823 format("abcd << \"line line line\" << \"line line line\";", 8824 getLLVMStyleWithColumns(20))); 8825 // In a chain of binary operators with two operands, the second can be broken 8826 // with no line break before it. 8827 EXPECT_EQ("abcd + \"line line \"\n" 8828 " \"line line\";", 8829 format("abcd + \"line line line line\";", 8830 getLLVMStyleWithColumns(20))); 8831 // In a chain of binary operators with three operands, the second must be 8832 // broken with a line break before it. 8833 EXPECT_EQ("abcd +\n" 8834 " \"line line \"\n" 8835 " \"line line\" +\n" 8836 " e;", 8837 format("abcd + \"line line line line\" + e;", 8838 getLLVMStyleWithColumns(20))); 8839 // In a function call with two operands, with AlignAfterOpenBracket enabled, 8840 // the first must be broken with a line break before it. 8841 FormatStyle Style = getLLVMStyleWithColumns(25); 8842 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8843 EXPECT_EQ("someFunction(\n" 8844 " \"long long long \"\n" 8845 " \"long\",\n" 8846 " a);", 8847 format("someFunction(\"long long long long\", a);", Style)); 8848 } 8849 8850 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8851 EXPECT_EQ( 8852 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8855 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8856 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8858 } 8859 8860 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8861 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8862 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8863 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8864 "multiline raw string literal xxxxxxxxxxxxxx\n" 8865 ")x\",\n" 8866 " a),\n" 8867 " b);", 8868 format("fffffffffff(g(R\"x(\n" 8869 "multiline raw string literal xxxxxxxxxxxxxx\n" 8870 ")x\", a), b);", 8871 getGoogleStyleWithColumns(20))); 8872 EXPECT_EQ("fffffffffff(\n" 8873 " g(R\"x(qqq\n" 8874 "multiline raw string literal xxxxxxxxxxxxxx\n" 8875 ")x\",\n" 8876 " a),\n" 8877 " b);", 8878 format("fffffffffff(g(R\"x(qqq\n" 8879 "multiline raw string literal xxxxxxxxxxxxxx\n" 8880 ")x\", a), b);", 8881 getGoogleStyleWithColumns(20))); 8882 8883 EXPECT_EQ("fffffffffff(R\"x(\n" 8884 "multiline raw string literal xxxxxxxxxxxxxx\n" 8885 ")x\");", 8886 format("fffffffffff(R\"x(\n" 8887 "multiline raw string literal xxxxxxxxxxxxxx\n" 8888 ")x\");", 8889 getGoogleStyleWithColumns(20))); 8890 EXPECT_EQ("fffffffffff(R\"x(\n" 8891 "multiline raw string literal xxxxxxxxxxxxxx\n" 8892 ")x\" + bbbbbb);", 8893 format("fffffffffff(R\"x(\n" 8894 "multiline raw string literal xxxxxxxxxxxxxx\n" 8895 ")x\" + bbbbbb);", 8896 getGoogleStyleWithColumns(20))); 8897 EXPECT_EQ("fffffffffff(\n" 8898 " R\"x(\n" 8899 "multiline raw string literal xxxxxxxxxxxxxx\n" 8900 ")x\" +\n" 8901 " bbbbbb);", 8902 format("fffffffffff(\n" 8903 " R\"x(\n" 8904 "multiline raw string literal xxxxxxxxxxxxxx\n" 8905 ")x\" + bbbbbb);", 8906 getGoogleStyleWithColumns(20))); 8907 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 8908 format("fffffffffff(\n" 8909 " R\"(single line raw string)\" + bbbbbb);")); 8910 } 8911 8912 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8913 verifyFormat("string a = \"unterminated;"); 8914 EXPECT_EQ("function(\"unterminated,\n" 8915 " OtherParameter);", 8916 format("function( \"unterminated,\n" 8917 " OtherParameter);")); 8918 } 8919 8920 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8921 FormatStyle Style = getLLVMStyle(); 8922 Style.Standard = FormatStyle::LS_Cpp03; 8923 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8924 format("#define x(_a) printf(\"foo\"_a);", Style)); 8925 } 8926 8927 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8928 8929 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8930 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8931 " \"ddeeefff\");", 8932 format("someFunction(\"aaabbbcccdddeeefff\");", 8933 getLLVMStyleWithColumns(25))); 8934 EXPECT_EQ("someFunction1234567890(\n" 8935 " \"aaabbbcccdddeeefff\");", 8936 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8937 getLLVMStyleWithColumns(26))); 8938 EXPECT_EQ("someFunction1234567890(\n" 8939 " \"aaabbbcccdddeeeff\"\n" 8940 " \"f\");", 8941 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8942 getLLVMStyleWithColumns(25))); 8943 EXPECT_EQ("someFunction1234567890(\n" 8944 " \"aaabbbcccdddeeeff\"\n" 8945 " \"f\");", 8946 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8947 getLLVMStyleWithColumns(24))); 8948 EXPECT_EQ("someFunction(\n" 8949 " \"aaabbbcc ddde \"\n" 8950 " \"efff\");", 8951 format("someFunction(\"aaabbbcc ddde efff\");", 8952 getLLVMStyleWithColumns(25))); 8953 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8954 " \"ddeeefff\");", 8955 format("someFunction(\"aaabbbccc ddeeefff\");", 8956 getLLVMStyleWithColumns(25))); 8957 EXPECT_EQ("someFunction1234567890(\n" 8958 " \"aaabb \"\n" 8959 " \"cccdddeeefff\");", 8960 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8961 getLLVMStyleWithColumns(25))); 8962 EXPECT_EQ("#define A \\\n" 8963 " string s = \\\n" 8964 " \"123456789\" \\\n" 8965 " \"0\"; \\\n" 8966 " int i;", 8967 format("#define A string s = \"1234567890\"; int i;", 8968 getLLVMStyleWithColumns(20))); 8969 EXPECT_EQ("someFunction(\n" 8970 " \"aaabbbcc \"\n" 8971 " \"dddeeefff\");", 8972 format("someFunction(\"aaabbbcc dddeeefff\");", 8973 getLLVMStyleWithColumns(25))); 8974 } 8975 8976 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8977 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8978 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8979 EXPECT_EQ("\"test\"\n" 8980 "\"\\n\"", 8981 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8982 EXPECT_EQ("\"tes\\\\\"\n" 8983 "\"n\"", 8984 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8985 EXPECT_EQ("\"\\\\\\\\\"\n" 8986 "\"\\n\"", 8987 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8988 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8989 EXPECT_EQ("\"\\uff01\"\n" 8990 "\"test\"", 8991 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8992 EXPECT_EQ("\"\\Uff01ff02\"", 8993 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8994 EXPECT_EQ("\"\\x000000000001\"\n" 8995 "\"next\"", 8996 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8997 EXPECT_EQ("\"\\x000000000001next\"", 8998 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8999 EXPECT_EQ("\"\\x000000000001\"", 9000 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 9001 EXPECT_EQ("\"test\"\n" 9002 "\"\\000000\"\n" 9003 "\"000001\"", 9004 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 9005 EXPECT_EQ("\"test\\000\"\n" 9006 "\"00000000\"\n" 9007 "\"1\"", 9008 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 9009 } 9010 9011 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 9012 verifyFormat("void f() {\n" 9013 " return g() {}\n" 9014 " void h() {}"); 9015 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 9016 "g();\n" 9017 "}"); 9018 } 9019 9020 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 9021 verifyFormat( 9022 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 9023 } 9024 9025 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 9026 verifyFormat("class X {\n" 9027 " void f() {\n" 9028 " }\n" 9029 "};", 9030 getLLVMStyleWithColumns(12)); 9031 } 9032 9033 TEST_F(FormatTest, ConfigurableIndentWidth) { 9034 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 9035 EightIndent.IndentWidth = 8; 9036 EightIndent.ContinuationIndentWidth = 8; 9037 verifyFormat("void f() {\n" 9038 " someFunction();\n" 9039 " if (true) {\n" 9040 " f();\n" 9041 " }\n" 9042 "}", 9043 EightIndent); 9044 verifyFormat("class X {\n" 9045 " void f() {\n" 9046 " }\n" 9047 "};", 9048 EightIndent); 9049 verifyFormat("int x[] = {\n" 9050 " call(),\n" 9051 " call()};", 9052 EightIndent); 9053 } 9054 9055 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 9056 verifyFormat("double\n" 9057 "f();", 9058 getLLVMStyleWithColumns(8)); 9059 } 9060 9061 TEST_F(FormatTest, ConfigurableUseOfTab) { 9062 FormatStyle Tab = getLLVMStyleWithColumns(42); 9063 Tab.IndentWidth = 8; 9064 Tab.UseTab = FormatStyle::UT_Always; 9065 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9066 9067 EXPECT_EQ("if (aaaaaaaa && // q\n" 9068 " bb)\t\t// w\n" 9069 "\t;", 9070 format("if (aaaaaaaa &&// q\n" 9071 "bb)// w\n" 9072 ";", 9073 Tab)); 9074 EXPECT_EQ("if (aaa && bbb) // w\n" 9075 "\t;", 9076 format("if(aaa&&bbb)// w\n" 9077 ";", 9078 Tab)); 9079 9080 verifyFormat("class X {\n" 9081 "\tvoid f() {\n" 9082 "\t\tsomeFunction(parameter1,\n" 9083 "\t\t\t parameter2);\n" 9084 "\t}\n" 9085 "};", 9086 Tab); 9087 verifyFormat("#define A \\\n" 9088 "\tvoid f() { \\\n" 9089 "\t\tsomeFunction( \\\n" 9090 "\t\t parameter1, \\\n" 9091 "\t\t parameter2); \\\n" 9092 "\t}", 9093 Tab); 9094 verifyFormat("int a;\t // x\n" 9095 "int bbbbbbbb; // x\n", 9096 Tab); 9097 9098 Tab.TabWidth = 4; 9099 Tab.IndentWidth = 8; 9100 verifyFormat("class TabWidth4Indent8 {\n" 9101 "\t\tvoid f() {\n" 9102 "\t\t\t\tsomeFunction(parameter1,\n" 9103 "\t\t\t\t\t\t\t parameter2);\n" 9104 "\t\t}\n" 9105 "};", 9106 Tab); 9107 9108 Tab.TabWidth = 4; 9109 Tab.IndentWidth = 4; 9110 verifyFormat("class TabWidth4Indent4 {\n" 9111 "\tvoid f() {\n" 9112 "\t\tsomeFunction(parameter1,\n" 9113 "\t\t\t\t\t parameter2);\n" 9114 "\t}\n" 9115 "};", 9116 Tab); 9117 9118 Tab.TabWidth = 8; 9119 Tab.IndentWidth = 4; 9120 verifyFormat("class TabWidth8Indent4 {\n" 9121 " void f() {\n" 9122 "\tsomeFunction(parameter1,\n" 9123 "\t\t parameter2);\n" 9124 " }\n" 9125 "};", 9126 Tab); 9127 9128 Tab.TabWidth = 8; 9129 Tab.IndentWidth = 8; 9130 EXPECT_EQ("/*\n" 9131 "\t a\t\tcomment\n" 9132 "\t in multiple lines\n" 9133 " */", 9134 format(" /*\t \t \n" 9135 " \t \t a\t\tcomment\t \t\n" 9136 " \t \t in multiple lines\t\n" 9137 " \t */", 9138 Tab)); 9139 9140 Tab.UseTab = FormatStyle::UT_ForIndentation; 9141 verifyFormat("{\n" 9142 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9143 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9144 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9145 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9146 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9147 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9148 "};", 9149 Tab); 9150 verifyFormat("enum AA {\n" 9151 "\ta1, // Force multiple lines\n" 9152 "\ta2,\n" 9153 "\ta3\n" 9154 "};", 9155 Tab); 9156 EXPECT_EQ("if (aaaaaaaa && // q\n" 9157 " bb) // w\n" 9158 "\t;", 9159 format("if (aaaaaaaa &&// q\n" 9160 "bb)// w\n" 9161 ";", 9162 Tab)); 9163 verifyFormat("class X {\n" 9164 "\tvoid f() {\n" 9165 "\t\tsomeFunction(parameter1,\n" 9166 "\t\t parameter2);\n" 9167 "\t}\n" 9168 "};", 9169 Tab); 9170 verifyFormat("{\n" 9171 "\tQ(\n" 9172 "\t {\n" 9173 "\t\t int a;\n" 9174 "\t\t someFunction(aaaaaaaa,\n" 9175 "\t\t bbbbbbb);\n" 9176 "\t },\n" 9177 "\t p);\n" 9178 "}", 9179 Tab); 9180 EXPECT_EQ("{\n" 9181 "\t/* aaaa\n" 9182 "\t bbbb */\n" 9183 "}", 9184 format("{\n" 9185 "/* aaaa\n" 9186 " bbbb */\n" 9187 "}", 9188 Tab)); 9189 EXPECT_EQ("{\n" 9190 "\t/*\n" 9191 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9192 "\t bbbbbbbbbbbbb\n" 9193 "\t*/\n" 9194 "}", 9195 format("{\n" 9196 "/*\n" 9197 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9198 "*/\n" 9199 "}", 9200 Tab)); 9201 EXPECT_EQ("{\n" 9202 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9203 "\t// bbbbbbbbbbbbb\n" 9204 "}", 9205 format("{\n" 9206 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9207 "}", 9208 Tab)); 9209 EXPECT_EQ("{\n" 9210 "\t/*\n" 9211 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9212 "\t bbbbbbbbbbbbb\n" 9213 "\t*/\n" 9214 "}", 9215 format("{\n" 9216 "\t/*\n" 9217 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9218 "\t*/\n" 9219 "}", 9220 Tab)); 9221 EXPECT_EQ("{\n" 9222 "\t/*\n" 9223 "\n" 9224 "\t*/\n" 9225 "}", 9226 format("{\n" 9227 "\t/*\n" 9228 "\n" 9229 "\t*/\n" 9230 "}", 9231 Tab)); 9232 EXPECT_EQ("{\n" 9233 "\t/*\n" 9234 " asdf\n" 9235 "\t*/\n" 9236 "}", 9237 format("{\n" 9238 "\t/*\n" 9239 " asdf\n" 9240 "\t*/\n" 9241 "}", 9242 Tab)); 9243 9244 Tab.UseTab = FormatStyle::UT_Never; 9245 EXPECT_EQ("/*\n" 9246 " a\t\tcomment\n" 9247 " in multiple lines\n" 9248 " */", 9249 format(" /*\t \t \n" 9250 " \t \t a\t\tcomment\t \t\n" 9251 " \t \t in multiple lines\t\n" 9252 " \t */", 9253 Tab)); 9254 EXPECT_EQ("/* some\n" 9255 " comment */", 9256 format(" \t \t /* some\n" 9257 " \t \t comment */", 9258 Tab)); 9259 EXPECT_EQ("int a; /* some\n" 9260 " comment */", 9261 format(" \t \t int a; /* some\n" 9262 " \t \t comment */", 9263 Tab)); 9264 9265 EXPECT_EQ("int a; /* some\n" 9266 "comment */", 9267 format(" \t \t int\ta; /* some\n" 9268 " \t \t comment */", 9269 Tab)); 9270 EXPECT_EQ("f(\"\t\t\"); /* some\n" 9271 " comment */", 9272 format(" \t \t f(\"\t\t\"); /* some\n" 9273 " \t \t comment */", 9274 Tab)); 9275 EXPECT_EQ("{\n" 9276 " /*\n" 9277 " * Comment\n" 9278 " */\n" 9279 " int i;\n" 9280 "}", 9281 format("{\n" 9282 "\t/*\n" 9283 "\t * Comment\n" 9284 "\t */\n" 9285 "\t int i;\n" 9286 "}")); 9287 9288 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 9289 Tab.TabWidth = 8; 9290 Tab.IndentWidth = 8; 9291 EXPECT_EQ("if (aaaaaaaa && // q\n" 9292 " bb) // w\n" 9293 "\t;", 9294 format("if (aaaaaaaa &&// q\n" 9295 "bb)// w\n" 9296 ";", 9297 Tab)); 9298 EXPECT_EQ("if (aaa && bbb) // w\n" 9299 "\t;", 9300 format("if(aaa&&bbb)// w\n" 9301 ";", 9302 Tab)); 9303 verifyFormat("class X {\n" 9304 "\tvoid f() {\n" 9305 "\t\tsomeFunction(parameter1,\n" 9306 "\t\t\t parameter2);\n" 9307 "\t}\n" 9308 "};", 9309 Tab); 9310 verifyFormat("#define A \\\n" 9311 "\tvoid f() { \\\n" 9312 "\t\tsomeFunction( \\\n" 9313 "\t\t parameter1, \\\n" 9314 "\t\t parameter2); \\\n" 9315 "\t}", 9316 Tab); 9317 Tab.TabWidth = 4; 9318 Tab.IndentWidth = 8; 9319 verifyFormat("class TabWidth4Indent8 {\n" 9320 "\t\tvoid f() {\n" 9321 "\t\t\t\tsomeFunction(parameter1,\n" 9322 "\t\t\t\t\t\t\t parameter2);\n" 9323 "\t\t}\n" 9324 "};", 9325 Tab); 9326 Tab.TabWidth = 4; 9327 Tab.IndentWidth = 4; 9328 verifyFormat("class TabWidth4Indent4 {\n" 9329 "\tvoid f() {\n" 9330 "\t\tsomeFunction(parameter1,\n" 9331 "\t\t\t\t\t parameter2);\n" 9332 "\t}\n" 9333 "};", 9334 Tab); 9335 Tab.TabWidth = 8; 9336 Tab.IndentWidth = 4; 9337 verifyFormat("class TabWidth8Indent4 {\n" 9338 " void f() {\n" 9339 "\tsomeFunction(parameter1,\n" 9340 "\t\t parameter2);\n" 9341 " }\n" 9342 "};", 9343 Tab); 9344 Tab.TabWidth = 8; 9345 Tab.IndentWidth = 8; 9346 EXPECT_EQ("/*\n" 9347 "\t a\t\tcomment\n" 9348 "\t in multiple lines\n" 9349 " */", 9350 format(" /*\t \t \n" 9351 " \t \t a\t\tcomment\t \t\n" 9352 " \t \t in multiple lines\t\n" 9353 " \t */", 9354 Tab)); 9355 verifyFormat("{\n" 9356 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9357 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9358 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9359 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9360 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9361 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9362 "};", 9363 Tab); 9364 verifyFormat("enum AA {\n" 9365 "\ta1, // Force multiple lines\n" 9366 "\ta2,\n" 9367 "\ta3\n" 9368 "};", 9369 Tab); 9370 EXPECT_EQ("if (aaaaaaaa && // q\n" 9371 " bb) // w\n" 9372 "\t;", 9373 format("if (aaaaaaaa &&// q\n" 9374 "bb)// w\n" 9375 ";", 9376 Tab)); 9377 verifyFormat("class X {\n" 9378 "\tvoid f() {\n" 9379 "\t\tsomeFunction(parameter1,\n" 9380 "\t\t\t parameter2);\n" 9381 "\t}\n" 9382 "};", 9383 Tab); 9384 verifyFormat("{\n" 9385 "\tQ(\n" 9386 "\t {\n" 9387 "\t\t int a;\n" 9388 "\t\t someFunction(aaaaaaaa,\n" 9389 "\t\t\t\t bbbbbbb);\n" 9390 "\t },\n" 9391 "\t p);\n" 9392 "}", 9393 Tab); 9394 EXPECT_EQ("{\n" 9395 "\t/* aaaa\n" 9396 "\t bbbb */\n" 9397 "}", 9398 format("{\n" 9399 "/* aaaa\n" 9400 " bbbb */\n" 9401 "}", 9402 Tab)); 9403 EXPECT_EQ("{\n" 9404 "\t/*\n" 9405 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9406 "\t bbbbbbbbbbbbb\n" 9407 "\t*/\n" 9408 "}", 9409 format("{\n" 9410 "/*\n" 9411 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9412 "*/\n" 9413 "}", 9414 Tab)); 9415 EXPECT_EQ("{\n" 9416 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9417 "\t// bbbbbbbbbbbbb\n" 9418 "}", 9419 format("{\n" 9420 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9421 "}", 9422 Tab)); 9423 EXPECT_EQ("{\n" 9424 "\t/*\n" 9425 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9426 "\t bbbbbbbbbbbbb\n" 9427 "\t*/\n" 9428 "}", 9429 format("{\n" 9430 "\t/*\n" 9431 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9432 "\t*/\n" 9433 "}", 9434 Tab)); 9435 EXPECT_EQ("{\n" 9436 "\t/*\n" 9437 "\n" 9438 "\t*/\n" 9439 "}", 9440 format("{\n" 9441 "\t/*\n" 9442 "\n" 9443 "\t*/\n" 9444 "}", 9445 Tab)); 9446 EXPECT_EQ("{\n" 9447 "\t/*\n" 9448 " asdf\n" 9449 "\t*/\n" 9450 "}", 9451 format("{\n" 9452 "\t/*\n" 9453 " asdf\n" 9454 "\t*/\n" 9455 "}", 9456 Tab)); 9457 EXPECT_EQ("/*\n" 9458 "\t a\t\tcomment\n" 9459 "\t in multiple lines\n" 9460 " */", 9461 format(" /*\t \t \n" 9462 " \t \t a\t\tcomment\t \t\n" 9463 " \t \t in multiple lines\t\n" 9464 " \t */", 9465 Tab)); 9466 EXPECT_EQ("/* some\n" 9467 " comment */", 9468 format(" \t \t /* some\n" 9469 " \t \t comment */", 9470 Tab)); 9471 EXPECT_EQ("int a; /* some\n" 9472 " comment */", 9473 format(" \t \t int a; /* some\n" 9474 " \t \t comment */", 9475 Tab)); 9476 EXPECT_EQ("int a; /* some\n" 9477 "comment */", 9478 format(" \t \t int\ta; /* some\n" 9479 " \t \t comment */", 9480 Tab)); 9481 EXPECT_EQ("f(\"\t\t\"); /* some\n" 9482 " comment */", 9483 format(" \t \t f(\"\t\t\"); /* some\n" 9484 " \t \t comment */", 9485 Tab)); 9486 EXPECT_EQ("{\n" 9487 " /*\n" 9488 " * Comment\n" 9489 " */\n" 9490 " int i;\n" 9491 "}", 9492 format("{\n" 9493 "\t/*\n" 9494 "\t * Comment\n" 9495 "\t */\n" 9496 "\t int i;\n" 9497 "}")); 9498 Tab.AlignConsecutiveAssignments = true; 9499 Tab.AlignConsecutiveDeclarations = true; 9500 Tab.TabWidth = 4; 9501 Tab.IndentWidth = 4; 9502 verifyFormat("class Assign {\n" 9503 "\tvoid f() {\n" 9504 "\t\tint x = 123;\n" 9505 "\t\tint random = 4;\n" 9506 "\t\tstd::string alphabet =\n" 9507 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 9508 "\t}\n" 9509 "};", 9510 Tab); 9511 } 9512 9513 TEST_F(FormatTest, CalculatesOriginalColumn) { 9514 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9515 "q\"; /* some\n" 9516 " comment */", 9517 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9518 "q\"; /* some\n" 9519 " comment */", 9520 getLLVMStyle())); 9521 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9522 "/* some\n" 9523 " comment */", 9524 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9525 " /* some\n" 9526 " comment */", 9527 getLLVMStyle())); 9528 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9529 "qqq\n" 9530 "/* some\n" 9531 " comment */", 9532 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9533 "qqq\n" 9534 " /* some\n" 9535 " comment */", 9536 getLLVMStyle())); 9537 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9538 "wwww; /* some\n" 9539 " comment */", 9540 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9541 "wwww; /* some\n" 9542 " comment */", 9543 getLLVMStyle())); 9544 } 9545 9546 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 9547 FormatStyle NoSpace = getLLVMStyle(); 9548 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 9549 9550 verifyFormat("while(true)\n" 9551 " continue;", 9552 NoSpace); 9553 verifyFormat("for(;;)\n" 9554 " continue;", 9555 NoSpace); 9556 verifyFormat("if(true)\n" 9557 " f();\n" 9558 "else if(true)\n" 9559 " f();", 9560 NoSpace); 9561 verifyFormat("do {\n" 9562 " do_something();\n" 9563 "} while(something());", 9564 NoSpace); 9565 verifyFormat("switch(x) {\n" 9566 "default:\n" 9567 " break;\n" 9568 "}", 9569 NoSpace); 9570 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 9571 verifyFormat("size_t x = sizeof(x);", NoSpace); 9572 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 9573 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 9574 verifyFormat("alignas(128) char a[128];", NoSpace); 9575 verifyFormat("size_t x = alignof(MyType);", NoSpace); 9576 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 9577 verifyFormat("int f() throw(Deprecated);", NoSpace); 9578 verifyFormat("typedef void (*cb)(int);", NoSpace); 9579 verifyFormat("T A::operator()();", NoSpace); 9580 verifyFormat("X A::operator++(T);", NoSpace); 9581 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 9582 9583 FormatStyle Space = getLLVMStyle(); 9584 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 9585 9586 verifyFormat("int f ();", Space); 9587 verifyFormat("void f (int a, T b) {\n" 9588 " while (true)\n" 9589 " continue;\n" 9590 "}", 9591 Space); 9592 verifyFormat("if (true)\n" 9593 " f ();\n" 9594 "else if (true)\n" 9595 " f ();", 9596 Space); 9597 verifyFormat("do {\n" 9598 " do_something ();\n" 9599 "} while (something ());", 9600 Space); 9601 verifyFormat("switch (x) {\n" 9602 "default:\n" 9603 " break;\n" 9604 "}", 9605 Space); 9606 verifyFormat("A::A () : a (1) {}", Space); 9607 verifyFormat("void f () __attribute__ ((asdf));", Space); 9608 verifyFormat("*(&a + 1);\n" 9609 "&((&a)[1]);\n" 9610 "a[(b + c) * d];\n" 9611 "(((a + 1) * 2) + 3) * 4;", 9612 Space); 9613 verifyFormat("#define A(x) x", Space); 9614 verifyFormat("#define A (x) x", Space); 9615 verifyFormat("#if defined(x)\n" 9616 "#endif", 9617 Space); 9618 verifyFormat("auto i = std::make_unique<int> (5);", Space); 9619 verifyFormat("size_t x = sizeof (x);", Space); 9620 verifyFormat("auto f (int x) -> decltype (x);", Space); 9621 verifyFormat("int f (T x) noexcept (x.create ());", Space); 9622 verifyFormat("alignas (128) char a[128];", Space); 9623 verifyFormat("size_t x = alignof (MyType);", Space); 9624 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 9625 verifyFormat("int f () throw (Deprecated);", Space); 9626 verifyFormat("typedef void (*cb) (int);", Space); 9627 verifyFormat("T A::operator() ();", Space); 9628 verifyFormat("X A::operator++ (T);", Space); 9629 verifyFormat("auto lambda = [] () { return 0; };", Space); 9630 verifyFormat("int x = int (y);", Space); 9631 9632 FormatStyle SomeSpace = getLLVMStyle(); 9633 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 9634 9635 verifyFormat("[]() -> float {}", SomeSpace); 9636 verifyFormat("[] (auto foo) {}", SomeSpace); 9637 verifyFormat("[foo]() -> int {}", SomeSpace); 9638 verifyFormat("int f();", SomeSpace); 9639 verifyFormat("void f (int a, T b) {\n" 9640 " while (true)\n" 9641 " continue;\n" 9642 "}", 9643 SomeSpace); 9644 verifyFormat("if (true)\n" 9645 " f();\n" 9646 "else if (true)\n" 9647 " f();", 9648 SomeSpace); 9649 verifyFormat("do {\n" 9650 " do_something();\n" 9651 "} while (something());", 9652 SomeSpace); 9653 verifyFormat("switch (x) {\n" 9654 "default:\n" 9655 " break;\n" 9656 "}", 9657 SomeSpace); 9658 verifyFormat("A::A() : a (1) {}", SomeSpace); 9659 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 9660 verifyFormat("*(&a + 1);\n" 9661 "&((&a)[1]);\n" 9662 "a[(b + c) * d];\n" 9663 "(((a + 1) * 2) + 3) * 4;", 9664 SomeSpace); 9665 verifyFormat("#define A(x) x", SomeSpace); 9666 verifyFormat("#define A (x) x", SomeSpace); 9667 verifyFormat("#if defined(x)\n" 9668 "#endif", 9669 SomeSpace); 9670 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 9671 verifyFormat("size_t x = sizeof (x);", SomeSpace); 9672 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 9673 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 9674 verifyFormat("alignas (128) char a[128];", SomeSpace); 9675 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 9676 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 9677 SomeSpace); 9678 verifyFormat("int f() throw (Deprecated);", SomeSpace); 9679 verifyFormat("typedef void (*cb) (int);", SomeSpace); 9680 verifyFormat("T A::operator()();", SomeSpace); 9681 verifyFormat("X A::operator++ (T);", SomeSpace); 9682 verifyFormat("int x = int (y);", SomeSpace); 9683 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 9684 } 9685 9686 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 9687 FormatStyle Spaces = getLLVMStyle(); 9688 9689 Spaces.SpacesInParentheses = true; 9690 verifyFormat("do_something( ::globalVar );", Spaces); 9691 verifyFormat("call( x, y, z );", Spaces); 9692 verifyFormat("call();", Spaces); 9693 verifyFormat("std::function<void( int, int )> callback;", Spaces); 9694 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 9695 Spaces); 9696 verifyFormat("while ( (bool)1 )\n" 9697 " continue;", 9698 Spaces); 9699 verifyFormat("for ( ;; )\n" 9700 " continue;", 9701 Spaces); 9702 verifyFormat("if ( true )\n" 9703 " f();\n" 9704 "else if ( true )\n" 9705 " f();", 9706 Spaces); 9707 verifyFormat("do {\n" 9708 " do_something( (int)i );\n" 9709 "} while ( something() );", 9710 Spaces); 9711 verifyFormat("switch ( x ) {\n" 9712 "default:\n" 9713 " break;\n" 9714 "}", 9715 Spaces); 9716 9717 Spaces.SpacesInParentheses = false; 9718 Spaces.SpacesInCStyleCastParentheses = true; 9719 verifyFormat("Type *A = ( Type * )P;", Spaces); 9720 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 9721 verifyFormat("x = ( int32 )y;", Spaces); 9722 verifyFormat("int a = ( int )(2.0f);", Spaces); 9723 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 9724 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 9725 verifyFormat("#define x (( int )-1)", Spaces); 9726 9727 // Run the first set of tests again with: 9728 Spaces.SpacesInParentheses = false; 9729 Spaces.SpaceInEmptyParentheses = true; 9730 Spaces.SpacesInCStyleCastParentheses = true; 9731 verifyFormat("call(x, y, z);", Spaces); 9732 verifyFormat("call( );", Spaces); 9733 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9734 verifyFormat("while (( bool )1)\n" 9735 " continue;", 9736 Spaces); 9737 verifyFormat("for (;;)\n" 9738 " continue;", 9739 Spaces); 9740 verifyFormat("if (true)\n" 9741 " f( );\n" 9742 "else if (true)\n" 9743 " f( );", 9744 Spaces); 9745 verifyFormat("do {\n" 9746 " do_something(( int )i);\n" 9747 "} while (something( ));", 9748 Spaces); 9749 verifyFormat("switch (x) {\n" 9750 "default:\n" 9751 " break;\n" 9752 "}", 9753 Spaces); 9754 9755 // Run the first set of tests again with: 9756 Spaces.SpaceAfterCStyleCast = true; 9757 verifyFormat("call(x, y, z);", Spaces); 9758 verifyFormat("call( );", Spaces); 9759 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9760 verifyFormat("while (( bool ) 1)\n" 9761 " continue;", 9762 Spaces); 9763 verifyFormat("for (;;)\n" 9764 " continue;", 9765 Spaces); 9766 verifyFormat("if (true)\n" 9767 " f( );\n" 9768 "else if (true)\n" 9769 " f( );", 9770 Spaces); 9771 verifyFormat("do {\n" 9772 " do_something(( int ) i);\n" 9773 "} while (something( ));", 9774 Spaces); 9775 verifyFormat("switch (x) {\n" 9776 "default:\n" 9777 " break;\n" 9778 "}", 9779 Spaces); 9780 9781 // Run subset of tests again with: 9782 Spaces.SpacesInCStyleCastParentheses = false; 9783 Spaces.SpaceAfterCStyleCast = true; 9784 verifyFormat("while ((bool) 1)\n" 9785 " continue;", 9786 Spaces); 9787 verifyFormat("do {\n" 9788 " do_something((int) i);\n" 9789 "} while (something( ));", 9790 Spaces); 9791 } 9792 9793 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 9794 verifyFormat("int a[5];"); 9795 verifyFormat("a[3] += 42;"); 9796 9797 FormatStyle Spaces = getLLVMStyle(); 9798 Spaces.SpacesInSquareBrackets = true; 9799 // Lambdas unchanged. 9800 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 9801 verifyFormat("return [i, args...] {};", Spaces); 9802 9803 // Not lambdas. 9804 verifyFormat("int a[ 5 ];", Spaces); 9805 verifyFormat("a[ 3 ] += 42;", Spaces); 9806 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 9807 verifyFormat("double &operator[](int i) { return 0; }\n" 9808 "int i;", 9809 Spaces); 9810 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 9811 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 9812 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 9813 } 9814 9815 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 9816 verifyFormat("int a = 5;"); 9817 verifyFormat("a += 42;"); 9818 verifyFormat("a or_eq 8;"); 9819 9820 FormatStyle Spaces = getLLVMStyle(); 9821 Spaces.SpaceBeforeAssignmentOperators = false; 9822 verifyFormat("int a= 5;", Spaces); 9823 verifyFormat("a+= 42;", Spaces); 9824 verifyFormat("a or_eq 8;", Spaces); 9825 } 9826 9827 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 9828 verifyFormat("class Foo : public Bar {};"); 9829 verifyFormat("Foo::Foo() : foo(1) {}"); 9830 verifyFormat("for (auto a : b) {\n}"); 9831 verifyFormat("int x = a ? b : c;"); 9832 verifyFormat("{\n" 9833 "label0:\n" 9834 " int x = 0;\n" 9835 "}"); 9836 verifyFormat("switch (x) {\n" 9837 "case 1:\n" 9838 "default:\n" 9839 "}"); 9840 9841 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 9842 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 9843 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 9844 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 9845 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 9846 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 9847 verifyFormat("{\n" 9848 "label1:\n" 9849 " int x = 0;\n" 9850 "}", 9851 CtorInitializerStyle); 9852 verifyFormat("switch (x) {\n" 9853 "case 1:\n" 9854 "default:\n" 9855 "}", 9856 CtorInitializerStyle); 9857 CtorInitializerStyle.BreakConstructorInitializers = 9858 FormatStyle::BCIS_AfterColon; 9859 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 9860 " aaaaaaaaaaaaaaaa(1),\n" 9861 " bbbbbbbbbbbbbbbb(2) {}", 9862 CtorInitializerStyle); 9863 CtorInitializerStyle.BreakConstructorInitializers = 9864 FormatStyle::BCIS_BeforeComma; 9865 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9866 " : aaaaaaaaaaaaaaaa(1)\n" 9867 " , bbbbbbbbbbbbbbbb(2) {}", 9868 CtorInitializerStyle); 9869 CtorInitializerStyle.BreakConstructorInitializers = 9870 FormatStyle::BCIS_BeforeColon; 9871 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9872 " : aaaaaaaaaaaaaaaa(1),\n" 9873 " bbbbbbbbbbbbbbbb(2) {}", 9874 CtorInitializerStyle); 9875 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 9876 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9877 ": aaaaaaaaaaaaaaaa(1),\n" 9878 " bbbbbbbbbbbbbbbb(2) {}", 9879 CtorInitializerStyle); 9880 9881 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 9882 InheritanceStyle.SpaceBeforeInheritanceColon = false; 9883 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 9884 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 9885 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 9886 verifyFormat("int x = a ? b : c;", InheritanceStyle); 9887 verifyFormat("{\n" 9888 "label2:\n" 9889 " int x = 0;\n" 9890 "}", 9891 InheritanceStyle); 9892 verifyFormat("switch (x) {\n" 9893 "case 1:\n" 9894 "default:\n" 9895 "}", 9896 InheritanceStyle); 9897 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 9898 verifyFormat("class Foooooooooooooooooooooo:\n" 9899 " public aaaaaaaaaaaaaaaaaa,\n" 9900 " public bbbbbbbbbbbbbbbbbb {\n" 9901 "}", 9902 InheritanceStyle); 9903 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 9904 verifyFormat("class Foooooooooooooooooooooo\n" 9905 " : public aaaaaaaaaaaaaaaaaa\n" 9906 " , public bbbbbbbbbbbbbbbbbb {\n" 9907 "}", 9908 InheritanceStyle); 9909 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 9910 verifyFormat("class Foooooooooooooooooooooo\n" 9911 " : public aaaaaaaaaaaaaaaaaa,\n" 9912 " public bbbbbbbbbbbbbbbbbb {\n" 9913 "}", 9914 InheritanceStyle); 9915 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 9916 verifyFormat("class Foooooooooooooooooooooo\n" 9917 ": public aaaaaaaaaaaaaaaaaa,\n" 9918 " public bbbbbbbbbbbbbbbbbb {}", 9919 InheritanceStyle); 9920 9921 FormatStyle ForLoopStyle = getLLVMStyle(); 9922 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 9923 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 9924 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 9925 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 9926 verifyFormat("int x = a ? b : c;", ForLoopStyle); 9927 verifyFormat("{\n" 9928 "label2:\n" 9929 " int x = 0;\n" 9930 "}", 9931 ForLoopStyle); 9932 verifyFormat("switch (x) {\n" 9933 "case 1:\n" 9934 "default:\n" 9935 "}", 9936 ForLoopStyle); 9937 9938 FormatStyle NoSpaceStyle = getLLVMStyle(); 9939 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 9940 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 9941 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 9942 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 9943 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 9944 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 9945 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 9946 verifyFormat("{\n" 9947 "label3:\n" 9948 " int x = 0;\n" 9949 "}", 9950 NoSpaceStyle); 9951 verifyFormat("switch (x) {\n" 9952 "case 1:\n" 9953 "default:\n" 9954 "}", 9955 NoSpaceStyle); 9956 } 9957 9958 TEST_F(FormatTest, AlignConsecutiveAssignments) { 9959 FormatStyle Alignment = getLLVMStyle(); 9960 Alignment.AlignConsecutiveAssignments = false; 9961 verifyFormat("int a = 5;\n" 9962 "int oneTwoThree = 123;", 9963 Alignment); 9964 verifyFormat("int a = 5;\n" 9965 "int oneTwoThree = 123;", 9966 Alignment); 9967 9968 Alignment.AlignConsecutiveAssignments = true; 9969 verifyFormat("int a = 5;\n" 9970 "int oneTwoThree = 123;", 9971 Alignment); 9972 verifyFormat("int a = method();\n" 9973 "int oneTwoThree = 133;", 9974 Alignment); 9975 verifyFormat("a &= 5;\n" 9976 "bcd *= 5;\n" 9977 "ghtyf += 5;\n" 9978 "dvfvdb -= 5;\n" 9979 "a /= 5;\n" 9980 "vdsvsv %= 5;\n" 9981 "sfdbddfbdfbb ^= 5;\n" 9982 "dvsdsv |= 5;\n" 9983 "int dsvvdvsdvvv = 123;", 9984 Alignment); 9985 verifyFormat("int i = 1, j = 10;\n" 9986 "something = 2000;", 9987 Alignment); 9988 verifyFormat("something = 2000;\n" 9989 "int i = 1, j = 10;\n", 9990 Alignment); 9991 verifyFormat("something = 2000;\n" 9992 "another = 911;\n" 9993 "int i = 1, j = 10;\n" 9994 "oneMore = 1;\n" 9995 "i = 2;", 9996 Alignment); 9997 verifyFormat("int a = 5;\n" 9998 "int one = 1;\n" 9999 "method();\n" 10000 "int oneTwoThree = 123;\n" 10001 "int oneTwo = 12;", 10002 Alignment); 10003 verifyFormat("int oneTwoThree = 123;\n" 10004 "int oneTwo = 12;\n" 10005 "method();\n", 10006 Alignment); 10007 verifyFormat("int oneTwoThree = 123; // comment\n" 10008 "int oneTwo = 12; // comment", 10009 Alignment); 10010 EXPECT_EQ("int a = 5;\n" 10011 "\n" 10012 "int oneTwoThree = 123;", 10013 format("int a = 5;\n" 10014 "\n" 10015 "int oneTwoThree= 123;", 10016 Alignment)); 10017 EXPECT_EQ("int a = 5;\n" 10018 "int one = 1;\n" 10019 "\n" 10020 "int oneTwoThree = 123;", 10021 format("int a = 5;\n" 10022 "int one = 1;\n" 10023 "\n" 10024 "int oneTwoThree = 123;", 10025 Alignment)); 10026 EXPECT_EQ("int a = 5;\n" 10027 "int one = 1;\n" 10028 "\n" 10029 "int oneTwoThree = 123;\n" 10030 "int oneTwo = 12;", 10031 format("int a = 5;\n" 10032 "int one = 1;\n" 10033 "\n" 10034 "int oneTwoThree = 123;\n" 10035 "int oneTwo = 12;", 10036 Alignment)); 10037 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 10038 verifyFormat("#define A \\\n" 10039 " int aaaa = 12; \\\n" 10040 " int b = 23; \\\n" 10041 " int ccc = 234; \\\n" 10042 " int dddddddddd = 2345;", 10043 Alignment); 10044 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10045 verifyFormat("#define A \\\n" 10046 " int aaaa = 12; \\\n" 10047 " int b = 23; \\\n" 10048 " int ccc = 234; \\\n" 10049 " int dddddddddd = 2345;", 10050 Alignment); 10051 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 10052 verifyFormat("#define A " 10053 " \\\n" 10054 " int aaaa = 12; " 10055 " \\\n" 10056 " int b = 23; " 10057 " \\\n" 10058 " int ccc = 234; " 10059 " \\\n" 10060 " int dddddddddd = 2345;", 10061 Alignment); 10062 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 10063 "k = 4, int l = 5,\n" 10064 " int m = 6) {\n" 10065 " int j = 10;\n" 10066 " otherThing = 1;\n" 10067 "}", 10068 Alignment); 10069 verifyFormat("void SomeFunction(int parameter = 0) {\n" 10070 " int i = 1;\n" 10071 " int j = 2;\n" 10072 " int big = 10000;\n" 10073 "}", 10074 Alignment); 10075 verifyFormat("class C {\n" 10076 "public:\n" 10077 " int i = 1;\n" 10078 " virtual void f() = 0;\n" 10079 "};", 10080 Alignment); 10081 verifyFormat("int i = 1;\n" 10082 "if (SomeType t = getSomething()) {\n" 10083 "}\n" 10084 "int j = 2;\n" 10085 "int big = 10000;", 10086 Alignment); 10087 verifyFormat("int j = 7;\n" 10088 "for (int k = 0; k < N; ++k) {\n" 10089 "}\n" 10090 "int j = 2;\n" 10091 "int big = 10000;\n" 10092 "}", 10093 Alignment); 10094 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10095 verifyFormat("int i = 1;\n" 10096 "LooooooooooongType loooooooooooooooooooooongVariable\n" 10097 " = someLooooooooooooooooongFunction();\n" 10098 "int j = 2;", 10099 Alignment); 10100 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 10101 verifyFormat("int i = 1;\n" 10102 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 10103 " someLooooooooooooooooongFunction();\n" 10104 "int j = 2;", 10105 Alignment); 10106 10107 verifyFormat("auto lambda = []() {\n" 10108 " auto i = 0;\n" 10109 " return 0;\n" 10110 "};\n" 10111 "int i = 0;\n" 10112 "auto v = type{\n" 10113 " i = 1, //\n" 10114 " (i = 2), //\n" 10115 " i = 3 //\n" 10116 "};", 10117 Alignment); 10118 10119 verifyFormat( 10120 "int i = 1;\n" 10121 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 10122 " loooooooooooooooooooooongParameterB);\n" 10123 "int j = 2;", 10124 Alignment); 10125 10126 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 10127 " typename B = very_long_type_name_1,\n" 10128 " typename T_2 = very_long_type_name_2>\n" 10129 "auto foo() {}\n", 10130 Alignment); 10131 verifyFormat("int a, b = 1;\n" 10132 "int c = 2;\n" 10133 "int dd = 3;\n", 10134 Alignment); 10135 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10136 "float b[1][] = {{3.f}};\n", 10137 Alignment); 10138 verifyFormat("for (int i = 0; i < 1; i++)\n" 10139 " int x = 1;\n", 10140 Alignment); 10141 verifyFormat("for (i = 0; i < 1; i++)\n" 10142 " x = 1;\n" 10143 "y = 1;\n", 10144 Alignment); 10145 } 10146 10147 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 10148 FormatStyle Alignment = getLLVMStyle(); 10149 Alignment.AlignConsecutiveDeclarations = false; 10150 verifyFormat("float const a = 5;\n" 10151 "int oneTwoThree = 123;", 10152 Alignment); 10153 verifyFormat("int a = 5;\n" 10154 "float const oneTwoThree = 123;", 10155 Alignment); 10156 10157 Alignment.AlignConsecutiveDeclarations = true; 10158 verifyFormat("float const a = 5;\n" 10159 "int oneTwoThree = 123;", 10160 Alignment); 10161 verifyFormat("int a = method();\n" 10162 "float const oneTwoThree = 133;", 10163 Alignment); 10164 verifyFormat("int i = 1, j = 10;\n" 10165 "something = 2000;", 10166 Alignment); 10167 verifyFormat("something = 2000;\n" 10168 "int i = 1, j = 10;\n", 10169 Alignment); 10170 verifyFormat("float something = 2000;\n" 10171 "double another = 911;\n" 10172 "int i = 1, j = 10;\n" 10173 "const int *oneMore = 1;\n" 10174 "unsigned i = 2;", 10175 Alignment); 10176 verifyFormat("float a = 5;\n" 10177 "int one = 1;\n" 10178 "method();\n" 10179 "const double oneTwoThree = 123;\n" 10180 "const unsigned int oneTwo = 12;", 10181 Alignment); 10182 verifyFormat("int oneTwoThree{0}; // comment\n" 10183 "unsigned oneTwo; // comment", 10184 Alignment); 10185 EXPECT_EQ("float const a = 5;\n" 10186 "\n" 10187 "int oneTwoThree = 123;", 10188 format("float const a = 5;\n" 10189 "\n" 10190 "int oneTwoThree= 123;", 10191 Alignment)); 10192 EXPECT_EQ("float a = 5;\n" 10193 "int one = 1;\n" 10194 "\n" 10195 "unsigned oneTwoThree = 123;", 10196 format("float a = 5;\n" 10197 "int one = 1;\n" 10198 "\n" 10199 "unsigned oneTwoThree = 123;", 10200 Alignment)); 10201 EXPECT_EQ("float a = 5;\n" 10202 "int one = 1;\n" 10203 "\n" 10204 "unsigned oneTwoThree = 123;\n" 10205 "int oneTwo = 12;", 10206 format("float a = 5;\n" 10207 "int one = 1;\n" 10208 "\n" 10209 "unsigned oneTwoThree = 123;\n" 10210 "int oneTwo = 12;", 10211 Alignment)); 10212 // Function prototype alignment 10213 verifyFormat("int a();\n" 10214 "double b();", 10215 Alignment); 10216 verifyFormat("int a(int x);\n" 10217 "double b();", 10218 Alignment); 10219 unsigned OldColumnLimit = Alignment.ColumnLimit; 10220 // We need to set ColumnLimit to zero, in order to stress nested alignments, 10221 // otherwise the function parameters will be re-flowed onto a single line. 10222 Alignment.ColumnLimit = 0; 10223 EXPECT_EQ("int a(int x,\n" 10224 " float y);\n" 10225 "double b(int x,\n" 10226 " double y);", 10227 format("int a(int x,\n" 10228 " float y);\n" 10229 "double b(int x,\n" 10230 " double y);", 10231 Alignment)); 10232 // This ensures that function parameters of function declarations are 10233 // correctly indented when their owning functions are indented. 10234 // The failure case here is for 'double y' to not be indented enough. 10235 EXPECT_EQ("double a(int x);\n" 10236 "int b(int y,\n" 10237 " double z);", 10238 format("double a(int x);\n" 10239 "int b(int y,\n" 10240 " double z);", 10241 Alignment)); 10242 // Set ColumnLimit low so that we induce wrapping immediately after 10243 // the function name and opening paren. 10244 Alignment.ColumnLimit = 13; 10245 verifyFormat("int function(\n" 10246 " int x,\n" 10247 " bool y);", 10248 Alignment); 10249 Alignment.ColumnLimit = OldColumnLimit; 10250 // Ensure function pointers don't screw up recursive alignment 10251 verifyFormat("int a(int x, void (*fp)(int y));\n" 10252 "double b();", 10253 Alignment); 10254 Alignment.AlignConsecutiveAssignments = true; 10255 // Ensure recursive alignment is broken by function braces, so that the 10256 // "a = 1" does not align with subsequent assignments inside the function 10257 // body. 10258 verifyFormat("int func(int a = 1) {\n" 10259 " int b = 2;\n" 10260 " int cc = 3;\n" 10261 "}", 10262 Alignment); 10263 verifyFormat("float something = 2000;\n" 10264 "double another = 911;\n" 10265 "int i = 1, j = 10;\n" 10266 "const int *oneMore = 1;\n" 10267 "unsigned i = 2;", 10268 Alignment); 10269 verifyFormat("int oneTwoThree = {0}; // comment\n" 10270 "unsigned oneTwo = 0; // comment", 10271 Alignment); 10272 // Make sure that scope is correctly tracked, in the absence of braces 10273 verifyFormat("for (int i = 0; i < n; i++)\n" 10274 " j = i;\n" 10275 "double x = 1;\n", 10276 Alignment); 10277 verifyFormat("if (int i = 0)\n" 10278 " j = i;\n" 10279 "double x = 1;\n", 10280 Alignment); 10281 // Ensure operator[] and operator() are comprehended 10282 verifyFormat("struct test {\n" 10283 " long long int foo();\n" 10284 " int operator[](int a);\n" 10285 " double bar();\n" 10286 "};\n", 10287 Alignment); 10288 verifyFormat("struct test {\n" 10289 " long long int foo();\n" 10290 " int operator()(int a);\n" 10291 " double bar();\n" 10292 "};\n", 10293 Alignment); 10294 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 10295 " int const i = 1;\n" 10296 " int * j = 2;\n" 10297 " int big = 10000;\n" 10298 "\n" 10299 " unsigned oneTwoThree = 123;\n" 10300 " int oneTwo = 12;\n" 10301 " method();\n" 10302 " float k = 2;\n" 10303 " int ll = 10000;\n" 10304 "}", 10305 format("void SomeFunction(int parameter= 0) {\n" 10306 " int const i= 1;\n" 10307 " int *j=2;\n" 10308 " int big = 10000;\n" 10309 "\n" 10310 "unsigned oneTwoThree =123;\n" 10311 "int oneTwo = 12;\n" 10312 " method();\n" 10313 "float k= 2;\n" 10314 "int ll=10000;\n" 10315 "}", 10316 Alignment)); 10317 Alignment.AlignConsecutiveAssignments = false; 10318 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 10319 verifyFormat("#define A \\\n" 10320 " int aaaa = 12; \\\n" 10321 " float b = 23; \\\n" 10322 " const int ccc = 234; \\\n" 10323 " unsigned dddddddddd = 2345;", 10324 Alignment); 10325 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10326 verifyFormat("#define A \\\n" 10327 " int aaaa = 12; \\\n" 10328 " float b = 23; \\\n" 10329 " const int ccc = 234; \\\n" 10330 " unsigned dddddddddd = 2345;", 10331 Alignment); 10332 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 10333 Alignment.ColumnLimit = 30; 10334 verifyFormat("#define A \\\n" 10335 " int aaaa = 12; \\\n" 10336 " float b = 23; \\\n" 10337 " const int ccc = 234; \\\n" 10338 " int dddddddddd = 2345;", 10339 Alignment); 10340 Alignment.ColumnLimit = 80; 10341 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 10342 "k = 4, int l = 5,\n" 10343 " int m = 6) {\n" 10344 " const int j = 10;\n" 10345 " otherThing = 1;\n" 10346 "}", 10347 Alignment); 10348 verifyFormat("void SomeFunction(int parameter = 0) {\n" 10349 " int const i = 1;\n" 10350 " int * j = 2;\n" 10351 " int big = 10000;\n" 10352 "}", 10353 Alignment); 10354 verifyFormat("class C {\n" 10355 "public:\n" 10356 " int i = 1;\n" 10357 " virtual void f() = 0;\n" 10358 "};", 10359 Alignment); 10360 verifyFormat("float i = 1;\n" 10361 "if (SomeType t = getSomething()) {\n" 10362 "}\n" 10363 "const unsigned j = 2;\n" 10364 "int big = 10000;", 10365 Alignment); 10366 verifyFormat("float j = 7;\n" 10367 "for (int k = 0; k < N; ++k) {\n" 10368 "}\n" 10369 "unsigned j = 2;\n" 10370 "int big = 10000;\n" 10371 "}", 10372 Alignment); 10373 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10374 verifyFormat("float i = 1;\n" 10375 "LooooooooooongType loooooooooooooooooooooongVariable\n" 10376 " = someLooooooooooooooooongFunction();\n" 10377 "int j = 2;", 10378 Alignment); 10379 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 10380 verifyFormat("int i = 1;\n" 10381 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 10382 " someLooooooooooooooooongFunction();\n" 10383 "int j = 2;", 10384 Alignment); 10385 10386 Alignment.AlignConsecutiveAssignments = true; 10387 verifyFormat("auto lambda = []() {\n" 10388 " auto ii = 0;\n" 10389 " float j = 0;\n" 10390 " return 0;\n" 10391 "};\n" 10392 "int i = 0;\n" 10393 "float i2 = 0;\n" 10394 "auto v = type{\n" 10395 " i = 1, //\n" 10396 " (i = 2), //\n" 10397 " i = 3 //\n" 10398 "};", 10399 Alignment); 10400 Alignment.AlignConsecutiveAssignments = false; 10401 10402 verifyFormat( 10403 "int i = 1;\n" 10404 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 10405 " loooooooooooooooooooooongParameterB);\n" 10406 "int j = 2;", 10407 Alignment); 10408 10409 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 10410 // We expect declarations and assignments to align, as long as it doesn't 10411 // exceed the column limit, starting a new alignment sequence whenever it 10412 // happens. 10413 Alignment.AlignConsecutiveAssignments = true; 10414 Alignment.ColumnLimit = 30; 10415 verifyFormat("float ii = 1;\n" 10416 "unsigned j = 2;\n" 10417 "int someVerylongVariable = 1;\n" 10418 "AnotherLongType ll = 123456;\n" 10419 "VeryVeryLongType k = 2;\n" 10420 "int myvar = 1;", 10421 Alignment); 10422 Alignment.ColumnLimit = 80; 10423 Alignment.AlignConsecutiveAssignments = false; 10424 10425 verifyFormat( 10426 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 10427 " typename LongType, typename B>\n" 10428 "auto foo() {}\n", 10429 Alignment); 10430 verifyFormat("float a, b = 1;\n" 10431 "int c = 2;\n" 10432 "int dd = 3;\n", 10433 Alignment); 10434 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10435 "float b[1][] = {{3.f}};\n", 10436 Alignment); 10437 Alignment.AlignConsecutiveAssignments = true; 10438 verifyFormat("float a, b = 1;\n" 10439 "int c = 2;\n" 10440 "int dd = 3;\n", 10441 Alignment); 10442 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10443 "float b[1][] = {{3.f}};\n", 10444 Alignment); 10445 Alignment.AlignConsecutiveAssignments = false; 10446 10447 Alignment.ColumnLimit = 30; 10448 Alignment.BinPackParameters = false; 10449 verifyFormat("void foo(float a,\n" 10450 " float b,\n" 10451 " int c,\n" 10452 " uint32_t *d) {\n" 10453 " int * e = 0;\n" 10454 " float f = 0;\n" 10455 " double g = 0;\n" 10456 "}\n" 10457 "void bar(ino_t a,\n" 10458 " int b,\n" 10459 " uint32_t *c,\n" 10460 " bool d) {}\n", 10461 Alignment); 10462 Alignment.BinPackParameters = true; 10463 Alignment.ColumnLimit = 80; 10464 10465 // Bug 33507 10466 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 10467 verifyFormat( 10468 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 10469 " static const Version verVs2017;\n" 10470 " return true;\n" 10471 "});\n", 10472 Alignment); 10473 Alignment.PointerAlignment = FormatStyle::PAS_Right; 10474 10475 // See llvm.org/PR35641 10476 Alignment.AlignConsecutiveDeclarations = true; 10477 verifyFormat("int func() { //\n" 10478 " int b;\n" 10479 " unsigned c;\n" 10480 "}", 10481 Alignment); 10482 } 10483 10484 TEST_F(FormatTest, LinuxBraceBreaking) { 10485 FormatStyle LinuxBraceStyle = getLLVMStyle(); 10486 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 10487 verifyFormat("namespace a\n" 10488 "{\n" 10489 "class A\n" 10490 "{\n" 10491 " void f()\n" 10492 " {\n" 10493 " if (true) {\n" 10494 " a();\n" 10495 " b();\n" 10496 " } else {\n" 10497 " a();\n" 10498 " }\n" 10499 " }\n" 10500 " void g() { return; }\n" 10501 "};\n" 10502 "struct B {\n" 10503 " int x;\n" 10504 "};\n" 10505 "} // namespace a\n", 10506 LinuxBraceStyle); 10507 verifyFormat("enum X {\n" 10508 " Y = 0,\n" 10509 "}\n", 10510 LinuxBraceStyle); 10511 verifyFormat("struct S {\n" 10512 " int Type;\n" 10513 " union {\n" 10514 " int x;\n" 10515 " double y;\n" 10516 " } Value;\n" 10517 " class C\n" 10518 " {\n" 10519 " MyFavoriteType Value;\n" 10520 " } Class;\n" 10521 "}\n", 10522 LinuxBraceStyle); 10523 } 10524 10525 TEST_F(FormatTest, MozillaBraceBreaking) { 10526 FormatStyle MozillaBraceStyle = getLLVMStyle(); 10527 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 10528 MozillaBraceStyle.FixNamespaceComments = false; 10529 verifyFormat("namespace a {\n" 10530 "class A\n" 10531 "{\n" 10532 " void f()\n" 10533 " {\n" 10534 " if (true) {\n" 10535 " a();\n" 10536 " b();\n" 10537 " }\n" 10538 " }\n" 10539 " void g() { return; }\n" 10540 "};\n" 10541 "enum E\n" 10542 "{\n" 10543 " A,\n" 10544 " // foo\n" 10545 " B,\n" 10546 " C\n" 10547 "};\n" 10548 "struct B\n" 10549 "{\n" 10550 " int x;\n" 10551 "};\n" 10552 "}\n", 10553 MozillaBraceStyle); 10554 verifyFormat("struct S\n" 10555 "{\n" 10556 " int Type;\n" 10557 " union\n" 10558 " {\n" 10559 " int x;\n" 10560 " double y;\n" 10561 " } Value;\n" 10562 " class C\n" 10563 " {\n" 10564 " MyFavoriteType Value;\n" 10565 " } Class;\n" 10566 "}\n", 10567 MozillaBraceStyle); 10568 } 10569 10570 TEST_F(FormatTest, StroustrupBraceBreaking) { 10571 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 10572 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10573 verifyFormat("namespace a {\n" 10574 "class A {\n" 10575 " void f()\n" 10576 " {\n" 10577 " if (true) {\n" 10578 " a();\n" 10579 " b();\n" 10580 " }\n" 10581 " }\n" 10582 " void g() { return; }\n" 10583 "};\n" 10584 "struct B {\n" 10585 " int x;\n" 10586 "};\n" 10587 "} // namespace a\n", 10588 StroustrupBraceStyle); 10589 10590 verifyFormat("void foo()\n" 10591 "{\n" 10592 " if (a) {\n" 10593 " a();\n" 10594 " }\n" 10595 " else {\n" 10596 " b();\n" 10597 " }\n" 10598 "}\n", 10599 StroustrupBraceStyle); 10600 10601 verifyFormat("#ifdef _DEBUG\n" 10602 "int foo(int i = 0)\n" 10603 "#else\n" 10604 "int foo(int i = 5)\n" 10605 "#endif\n" 10606 "{\n" 10607 " return i;\n" 10608 "}", 10609 StroustrupBraceStyle); 10610 10611 verifyFormat("void foo() {}\n" 10612 "void bar()\n" 10613 "#ifdef _DEBUG\n" 10614 "{\n" 10615 " foo();\n" 10616 "}\n" 10617 "#else\n" 10618 "{\n" 10619 "}\n" 10620 "#endif", 10621 StroustrupBraceStyle); 10622 10623 verifyFormat("void foobar() { int i = 5; }\n" 10624 "#ifdef _DEBUG\n" 10625 "void bar() {}\n" 10626 "#else\n" 10627 "void bar() { foobar(); }\n" 10628 "#endif", 10629 StroustrupBraceStyle); 10630 } 10631 10632 TEST_F(FormatTest, AllmanBraceBreaking) { 10633 FormatStyle AllmanBraceStyle = getLLVMStyle(); 10634 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 10635 10636 EXPECT_EQ("namespace a\n" 10637 "{\n" 10638 "void f();\n" 10639 "void g();\n" 10640 "} // namespace a\n", 10641 format("namespace a\n" 10642 "{\n" 10643 "void f();\n" 10644 "void g();\n" 10645 "}\n", 10646 AllmanBraceStyle)); 10647 10648 verifyFormat("namespace a\n" 10649 "{\n" 10650 "class A\n" 10651 "{\n" 10652 " void f()\n" 10653 " {\n" 10654 " if (true)\n" 10655 " {\n" 10656 " a();\n" 10657 " b();\n" 10658 " }\n" 10659 " }\n" 10660 " void g() { return; }\n" 10661 "};\n" 10662 "struct B\n" 10663 "{\n" 10664 " int x;\n" 10665 "};\n" 10666 "} // namespace a", 10667 AllmanBraceStyle); 10668 10669 verifyFormat("void f()\n" 10670 "{\n" 10671 " if (true)\n" 10672 " {\n" 10673 " a();\n" 10674 " }\n" 10675 " else if (false)\n" 10676 " {\n" 10677 " b();\n" 10678 " }\n" 10679 " else\n" 10680 " {\n" 10681 " c();\n" 10682 " }\n" 10683 "}\n", 10684 AllmanBraceStyle); 10685 10686 verifyFormat("void f()\n" 10687 "{\n" 10688 " for (int i = 0; i < 10; ++i)\n" 10689 " {\n" 10690 " a();\n" 10691 " }\n" 10692 " while (false)\n" 10693 " {\n" 10694 " b();\n" 10695 " }\n" 10696 " do\n" 10697 " {\n" 10698 " c();\n" 10699 " } while (false)\n" 10700 "}\n", 10701 AllmanBraceStyle); 10702 10703 verifyFormat("void f(int a)\n" 10704 "{\n" 10705 " switch (a)\n" 10706 " {\n" 10707 " case 0:\n" 10708 " break;\n" 10709 " case 1:\n" 10710 " {\n" 10711 " break;\n" 10712 " }\n" 10713 " case 2:\n" 10714 " {\n" 10715 " }\n" 10716 " break;\n" 10717 " default:\n" 10718 " break;\n" 10719 " }\n" 10720 "}\n", 10721 AllmanBraceStyle); 10722 10723 verifyFormat("enum X\n" 10724 "{\n" 10725 " Y = 0,\n" 10726 "}\n", 10727 AllmanBraceStyle); 10728 verifyFormat("enum X\n" 10729 "{\n" 10730 " Y = 0\n" 10731 "}\n", 10732 AllmanBraceStyle); 10733 10734 verifyFormat("@interface BSApplicationController ()\n" 10735 "{\n" 10736 "@private\n" 10737 " id _extraIvar;\n" 10738 "}\n" 10739 "@end\n", 10740 AllmanBraceStyle); 10741 10742 verifyFormat("#ifdef _DEBUG\n" 10743 "int foo(int i = 0)\n" 10744 "#else\n" 10745 "int foo(int i = 5)\n" 10746 "#endif\n" 10747 "{\n" 10748 " return i;\n" 10749 "}", 10750 AllmanBraceStyle); 10751 10752 verifyFormat("void foo() {}\n" 10753 "void bar()\n" 10754 "#ifdef _DEBUG\n" 10755 "{\n" 10756 " foo();\n" 10757 "}\n" 10758 "#else\n" 10759 "{\n" 10760 "}\n" 10761 "#endif", 10762 AllmanBraceStyle); 10763 10764 verifyFormat("void foobar() { int i = 5; }\n" 10765 "#ifdef _DEBUG\n" 10766 "void bar() {}\n" 10767 "#else\n" 10768 "void bar() { foobar(); }\n" 10769 "#endif", 10770 AllmanBraceStyle); 10771 10772 // This shouldn't affect ObjC blocks.. 10773 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 10774 " // ...\n" 10775 " int i;\n" 10776 "}];", 10777 AllmanBraceStyle); 10778 verifyFormat("void (^block)(void) = ^{\n" 10779 " // ...\n" 10780 " int i;\n" 10781 "};", 10782 AllmanBraceStyle); 10783 // .. or dict literals. 10784 verifyFormat("void f()\n" 10785 "{\n" 10786 " // ...\n" 10787 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 10788 "}", 10789 AllmanBraceStyle); 10790 verifyFormat("void f()\n" 10791 "{\n" 10792 " // ...\n" 10793 " [object someMethod:@{a : @\"b\"}];\n" 10794 "}", 10795 AllmanBraceStyle); 10796 verifyFormat("int f()\n" 10797 "{ // comment\n" 10798 " return 42;\n" 10799 "}", 10800 AllmanBraceStyle); 10801 10802 AllmanBraceStyle.ColumnLimit = 19; 10803 verifyFormat("void f() { int i; }", AllmanBraceStyle); 10804 AllmanBraceStyle.ColumnLimit = 18; 10805 verifyFormat("void f()\n" 10806 "{\n" 10807 " int i;\n" 10808 "}", 10809 AllmanBraceStyle); 10810 AllmanBraceStyle.ColumnLimit = 80; 10811 10812 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 10813 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 10814 FormatStyle::SIS_WithoutElse; 10815 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 10816 verifyFormat("void f(bool b)\n" 10817 "{\n" 10818 " if (b)\n" 10819 " {\n" 10820 " return;\n" 10821 " }\n" 10822 "}\n", 10823 BreakBeforeBraceShortIfs); 10824 verifyFormat("void f(bool b)\n" 10825 "{\n" 10826 " if constexpr (b)\n" 10827 " {\n" 10828 " return;\n" 10829 " }\n" 10830 "}\n", 10831 BreakBeforeBraceShortIfs); 10832 verifyFormat("void f(bool b)\n" 10833 "{\n" 10834 " if (b) return;\n" 10835 "}\n", 10836 BreakBeforeBraceShortIfs); 10837 verifyFormat("void f(bool b)\n" 10838 "{\n" 10839 " if constexpr (b) return;\n" 10840 "}\n", 10841 BreakBeforeBraceShortIfs); 10842 verifyFormat("void f(bool b)\n" 10843 "{\n" 10844 " while (b)\n" 10845 " {\n" 10846 " return;\n" 10847 " }\n" 10848 "}\n", 10849 BreakBeforeBraceShortIfs); 10850 } 10851 10852 TEST_F(FormatTest, GNUBraceBreaking) { 10853 FormatStyle GNUBraceStyle = getLLVMStyle(); 10854 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 10855 verifyFormat("namespace a\n" 10856 "{\n" 10857 "class A\n" 10858 "{\n" 10859 " void f()\n" 10860 " {\n" 10861 " int a;\n" 10862 " {\n" 10863 " int b;\n" 10864 " }\n" 10865 " if (true)\n" 10866 " {\n" 10867 " a();\n" 10868 " b();\n" 10869 " }\n" 10870 " }\n" 10871 " void g() { return; }\n" 10872 "}\n" 10873 "} // namespace a", 10874 GNUBraceStyle); 10875 10876 verifyFormat("void f()\n" 10877 "{\n" 10878 " if (true)\n" 10879 " {\n" 10880 " a();\n" 10881 " }\n" 10882 " else if (false)\n" 10883 " {\n" 10884 " b();\n" 10885 " }\n" 10886 " else\n" 10887 " {\n" 10888 " c();\n" 10889 " }\n" 10890 "}\n", 10891 GNUBraceStyle); 10892 10893 verifyFormat("void f()\n" 10894 "{\n" 10895 " for (int i = 0; i < 10; ++i)\n" 10896 " {\n" 10897 " a();\n" 10898 " }\n" 10899 " while (false)\n" 10900 " {\n" 10901 " b();\n" 10902 " }\n" 10903 " do\n" 10904 " {\n" 10905 " c();\n" 10906 " }\n" 10907 " while (false);\n" 10908 "}\n", 10909 GNUBraceStyle); 10910 10911 verifyFormat("void f(int a)\n" 10912 "{\n" 10913 " switch (a)\n" 10914 " {\n" 10915 " case 0:\n" 10916 " break;\n" 10917 " case 1:\n" 10918 " {\n" 10919 " break;\n" 10920 " }\n" 10921 " case 2:\n" 10922 " {\n" 10923 " }\n" 10924 " break;\n" 10925 " default:\n" 10926 " break;\n" 10927 " }\n" 10928 "}\n", 10929 GNUBraceStyle); 10930 10931 verifyFormat("enum X\n" 10932 "{\n" 10933 " Y = 0,\n" 10934 "}\n", 10935 GNUBraceStyle); 10936 10937 verifyFormat("@interface BSApplicationController ()\n" 10938 "{\n" 10939 "@private\n" 10940 " id _extraIvar;\n" 10941 "}\n" 10942 "@end\n", 10943 GNUBraceStyle); 10944 10945 verifyFormat("#ifdef _DEBUG\n" 10946 "int foo(int i = 0)\n" 10947 "#else\n" 10948 "int foo(int i = 5)\n" 10949 "#endif\n" 10950 "{\n" 10951 " return i;\n" 10952 "}", 10953 GNUBraceStyle); 10954 10955 verifyFormat("void foo() {}\n" 10956 "void bar()\n" 10957 "#ifdef _DEBUG\n" 10958 "{\n" 10959 " foo();\n" 10960 "}\n" 10961 "#else\n" 10962 "{\n" 10963 "}\n" 10964 "#endif", 10965 GNUBraceStyle); 10966 10967 verifyFormat("void foobar() { int i = 5; }\n" 10968 "#ifdef _DEBUG\n" 10969 "void bar() {}\n" 10970 "#else\n" 10971 "void bar() { foobar(); }\n" 10972 "#endif", 10973 GNUBraceStyle); 10974 } 10975 10976 TEST_F(FormatTest, WebKitBraceBreaking) { 10977 FormatStyle WebKitBraceStyle = getLLVMStyle(); 10978 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 10979 WebKitBraceStyle.FixNamespaceComments = false; 10980 verifyFormat("namespace a {\n" 10981 "class A {\n" 10982 " void f()\n" 10983 " {\n" 10984 " if (true) {\n" 10985 " a();\n" 10986 " b();\n" 10987 " }\n" 10988 " }\n" 10989 " void g() { return; }\n" 10990 "};\n" 10991 "enum E {\n" 10992 " A,\n" 10993 " // foo\n" 10994 " B,\n" 10995 " C\n" 10996 "};\n" 10997 "struct B {\n" 10998 " int x;\n" 10999 "};\n" 11000 "}\n", 11001 WebKitBraceStyle); 11002 verifyFormat("struct S {\n" 11003 " int Type;\n" 11004 " union {\n" 11005 " int x;\n" 11006 " double y;\n" 11007 " } Value;\n" 11008 " class C {\n" 11009 " MyFavoriteType Value;\n" 11010 " } Class;\n" 11011 "};\n", 11012 WebKitBraceStyle); 11013 } 11014 11015 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 11016 verifyFormat("void f() {\n" 11017 " try {\n" 11018 " } catch (const Exception &e) {\n" 11019 " }\n" 11020 "}\n", 11021 getLLVMStyle()); 11022 } 11023 11024 TEST_F(FormatTest, UnderstandsPragmas) { 11025 verifyFormat("#pragma omp reduction(| : var)"); 11026 verifyFormat("#pragma omp reduction(+ : var)"); 11027 11028 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 11029 "(including parentheses).", 11030 format("#pragma mark Any non-hyphenated or hyphenated string " 11031 "(including parentheses).")); 11032 } 11033 11034 TEST_F(FormatTest, UnderstandPragmaOption) { 11035 verifyFormat("#pragma option -C -A"); 11036 11037 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 11038 } 11039 11040 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 11041 FormatStyle Style = getLLVMStyle(); 11042 Style.ColumnLimit = 20; 11043 11044 verifyFormat("int a; // the\n" 11045 " // comment", Style); 11046 EXPECT_EQ("int a; /* first line\n" 11047 " * second\n" 11048 " * line third\n" 11049 " * line\n" 11050 " */", 11051 format("int a; /* first line\n" 11052 " * second\n" 11053 " * line third\n" 11054 " * line\n" 11055 " */", 11056 Style)); 11057 EXPECT_EQ("int a; // first line\n" 11058 " // second\n" 11059 " // line third\n" 11060 " // line", 11061 format("int a; // first line\n" 11062 " // second line\n" 11063 " // third line", 11064 Style)); 11065 11066 Style.PenaltyExcessCharacter = 90; 11067 verifyFormat("int a; // the comment", Style); 11068 EXPECT_EQ("int a; // the comment\n" 11069 " // aaa", 11070 format("int a; // the comment aaa", Style)); 11071 EXPECT_EQ("int a; /* first line\n" 11072 " * second line\n" 11073 " * third line\n" 11074 " */", 11075 format("int a; /* first line\n" 11076 " * second line\n" 11077 " * third line\n" 11078 " */", 11079 Style)); 11080 EXPECT_EQ("int a; // first line\n" 11081 " // second line\n" 11082 " // third line", 11083 format("int a; // first line\n" 11084 " // second line\n" 11085 " // third line", 11086 Style)); 11087 // FIXME: Investigate why this is not getting the same layout as the test 11088 // above. 11089 EXPECT_EQ("int a; /* first line\n" 11090 " * second line\n" 11091 " * third line\n" 11092 " */", 11093 format("int a; /* first line second line third line" 11094 "\n*/", 11095 Style)); 11096 11097 EXPECT_EQ("// foo bar baz bazfoo\n" 11098 "// foo bar foo bar\n", 11099 format("// foo bar baz bazfoo\n" 11100 "// foo bar foo bar\n", 11101 Style)); 11102 EXPECT_EQ("// foo bar baz bazfoo\n" 11103 "// foo bar foo bar\n", 11104 format("// foo bar baz bazfoo\n" 11105 "// foo bar foo bar\n", 11106 Style)); 11107 11108 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 11109 // next one. 11110 EXPECT_EQ("// foo bar baz bazfoo\n" 11111 "// bar foo bar\n", 11112 format("// foo bar baz bazfoo bar\n" 11113 "// foo bar\n", 11114 Style)); 11115 11116 EXPECT_EQ("// foo bar baz bazfoo\n" 11117 "// foo bar baz bazfoo\n" 11118 "// bar foo bar\n", 11119 format("// foo bar baz bazfoo\n" 11120 "// foo bar baz bazfoo bar\n" 11121 "// foo bar\n", 11122 Style)); 11123 11124 EXPECT_EQ("// foo bar baz bazfoo\n" 11125 "// foo bar baz bazfoo\n" 11126 "// bar foo bar\n", 11127 format("// foo bar baz bazfoo\n" 11128 "// foo bar baz bazfoo bar\n" 11129 "// foo bar\n", 11130 Style)); 11131 11132 // Make sure we do not keep protruding characters if strict mode reflow is 11133 // cheaper than keeping protruding characters. 11134 Style.ColumnLimit = 21; 11135 EXPECT_EQ("// foo foo foo foo\n" 11136 "// foo foo foo foo\n" 11137 "// foo foo foo foo\n", 11138 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 11139 Style)); 11140 11141 EXPECT_EQ("int a = /* long block\n" 11142 " comment */\n" 11143 " 42;", 11144 format("int a = /* long block comment */ 42;", Style)); 11145 } 11146 11147 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 11148 for (size_t i = 1; i < Styles.size(); ++i) \ 11149 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 11150 << " differs from Style #0" 11151 11152 TEST_F(FormatTest, GetsPredefinedStyleByName) { 11153 SmallVector<FormatStyle, 3> Styles; 11154 Styles.resize(3); 11155 11156 Styles[0] = getLLVMStyle(); 11157 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 11158 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 11159 EXPECT_ALL_STYLES_EQUAL(Styles); 11160 11161 Styles[0] = getGoogleStyle(); 11162 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 11163 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 11164 EXPECT_ALL_STYLES_EQUAL(Styles); 11165 11166 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 11167 EXPECT_TRUE( 11168 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 11169 EXPECT_TRUE( 11170 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 11171 EXPECT_ALL_STYLES_EQUAL(Styles); 11172 11173 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 11174 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 11175 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 11176 EXPECT_ALL_STYLES_EQUAL(Styles); 11177 11178 Styles[0] = getMozillaStyle(); 11179 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 11180 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 11181 EXPECT_ALL_STYLES_EQUAL(Styles); 11182 11183 Styles[0] = getWebKitStyle(); 11184 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 11185 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 11186 EXPECT_ALL_STYLES_EQUAL(Styles); 11187 11188 Styles[0] = getGNUStyle(); 11189 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 11190 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 11191 EXPECT_ALL_STYLES_EQUAL(Styles); 11192 11193 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 11194 } 11195 11196 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 11197 SmallVector<FormatStyle, 8> Styles; 11198 Styles.resize(2); 11199 11200 Styles[0] = getGoogleStyle(); 11201 Styles[1] = getLLVMStyle(); 11202 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 11203 EXPECT_ALL_STYLES_EQUAL(Styles); 11204 11205 Styles.resize(5); 11206 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 11207 Styles[1] = getLLVMStyle(); 11208 Styles[1].Language = FormatStyle::LK_JavaScript; 11209 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 11210 11211 Styles[2] = getLLVMStyle(); 11212 Styles[2].Language = FormatStyle::LK_JavaScript; 11213 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 11214 "BasedOnStyle: Google", 11215 &Styles[2]) 11216 .value()); 11217 11218 Styles[3] = getLLVMStyle(); 11219 Styles[3].Language = FormatStyle::LK_JavaScript; 11220 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 11221 "Language: JavaScript", 11222 &Styles[3]) 11223 .value()); 11224 11225 Styles[4] = getLLVMStyle(); 11226 Styles[4].Language = FormatStyle::LK_JavaScript; 11227 EXPECT_EQ(0, parseConfiguration("---\n" 11228 "BasedOnStyle: LLVM\n" 11229 "IndentWidth: 123\n" 11230 "---\n" 11231 "BasedOnStyle: Google\n" 11232 "Language: JavaScript", 11233 &Styles[4]) 11234 .value()); 11235 EXPECT_ALL_STYLES_EQUAL(Styles); 11236 } 11237 11238 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 11239 Style.FIELD = false; \ 11240 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 11241 EXPECT_TRUE(Style.FIELD); \ 11242 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 11243 EXPECT_FALSE(Style.FIELD); 11244 11245 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 11246 11247 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 11248 Style.STRUCT.FIELD = false; \ 11249 EXPECT_EQ(0, \ 11250 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 11251 .value()); \ 11252 EXPECT_TRUE(Style.STRUCT.FIELD); \ 11253 EXPECT_EQ(0, \ 11254 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 11255 .value()); \ 11256 EXPECT_FALSE(Style.STRUCT.FIELD); 11257 11258 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 11259 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 11260 11261 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 11262 EXPECT_NE(VALUE, Style.FIELD); \ 11263 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 11264 EXPECT_EQ(VALUE, Style.FIELD) 11265 11266 TEST_F(FormatTest, ParsesConfigurationBools) { 11267 FormatStyle Style = {}; 11268 Style.Language = FormatStyle::LK_Cpp; 11269 CHECK_PARSE_BOOL(AlignOperands); 11270 CHECK_PARSE_BOOL(AlignTrailingComments); 11271 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 11272 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 11273 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 11274 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 11275 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 11276 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 11277 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 11278 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 11279 CHECK_PARSE_BOOL(BinPackArguments); 11280 CHECK_PARSE_BOOL(BinPackParameters); 11281 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 11282 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 11283 CHECK_PARSE_BOOL(BreakStringLiterals); 11284 CHECK_PARSE_BOOL(CompactNamespaces); 11285 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 11286 CHECK_PARSE_BOOL(DerivePointerAlignment); 11287 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 11288 CHECK_PARSE_BOOL(DisableFormat); 11289 CHECK_PARSE_BOOL(IndentCaseLabels); 11290 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 11291 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 11292 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 11293 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 11294 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 11295 CHECK_PARSE_BOOL(ReflowComments); 11296 CHECK_PARSE_BOOL(SortIncludes); 11297 CHECK_PARSE_BOOL(SortUsingDeclarations); 11298 CHECK_PARSE_BOOL(SpacesInParentheses); 11299 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 11300 CHECK_PARSE_BOOL(SpacesInAngles); 11301 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 11302 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 11303 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 11304 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 11305 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 11306 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 11307 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 11308 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 11309 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 11310 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 11311 11312 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 11313 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 11314 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 11315 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 11316 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 11317 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 11318 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 11319 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 11320 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 11321 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 11322 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 11323 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 11324 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 11325 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 11326 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 11327 } 11328 11329 #undef CHECK_PARSE_BOOL 11330 11331 TEST_F(FormatTest, ParsesConfiguration) { 11332 FormatStyle Style = {}; 11333 Style.Language = FormatStyle::LK_Cpp; 11334 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 11335 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 11336 ConstructorInitializerIndentWidth, 1234u); 11337 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 11338 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 11339 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 11340 CHECK_PARSE("PenaltyBreakAssignment: 1234", 11341 PenaltyBreakAssignment, 1234u); 11342 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 11343 PenaltyBreakBeforeFirstCallParameter, 1234u); 11344 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 11345 PenaltyBreakTemplateDeclaration, 1234u); 11346 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 11347 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 11348 PenaltyReturnTypeOnItsOwnLine, 1234u); 11349 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 11350 SpacesBeforeTrailingComments, 1234u); 11351 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 11352 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 11353 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 11354 11355 Style.PointerAlignment = FormatStyle::PAS_Middle; 11356 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 11357 FormatStyle::PAS_Left); 11358 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 11359 FormatStyle::PAS_Right); 11360 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 11361 FormatStyle::PAS_Middle); 11362 // For backward compatibility: 11363 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 11364 FormatStyle::PAS_Left); 11365 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 11366 FormatStyle::PAS_Right); 11367 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 11368 FormatStyle::PAS_Middle); 11369 11370 Style.Standard = FormatStyle::LS_Auto; 11371 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 11372 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 11373 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 11374 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 11375 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 11376 11377 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 11378 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 11379 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 11380 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 11381 FormatStyle::BOS_None); 11382 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 11383 FormatStyle::BOS_All); 11384 // For backward compatibility: 11385 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 11386 FormatStyle::BOS_None); 11387 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 11388 FormatStyle::BOS_All); 11389 11390 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 11391 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 11392 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 11393 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 11394 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 11395 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 11396 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 11397 // For backward compatibility: 11398 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 11399 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 11400 11401 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 11402 CHECK_PARSE("BreakInheritanceList: BeforeComma", 11403 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 11404 CHECK_PARSE("BreakInheritanceList: AfterColon", 11405 BreakInheritanceList, FormatStyle::BILS_AfterColon); 11406 CHECK_PARSE("BreakInheritanceList: BeforeColon", 11407 BreakInheritanceList, FormatStyle::BILS_BeforeColon); 11408 // For backward compatibility: 11409 CHECK_PARSE("BreakBeforeInheritanceComma: true", 11410 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 11411 11412 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11413 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 11414 FormatStyle::BAS_Align); 11415 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 11416 FormatStyle::BAS_DontAlign); 11417 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 11418 FormatStyle::BAS_AlwaysBreak); 11419 // For backward compatibility: 11420 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 11421 FormatStyle::BAS_DontAlign); 11422 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 11423 FormatStyle::BAS_Align); 11424 11425 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 11426 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 11427 FormatStyle::ENAS_DontAlign); 11428 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 11429 FormatStyle::ENAS_Left); 11430 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 11431 FormatStyle::ENAS_Right); 11432 // For backward compatibility: 11433 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 11434 FormatStyle::ENAS_Left); 11435 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 11436 FormatStyle::ENAS_Right); 11437 11438 Style.UseTab = FormatStyle::UT_ForIndentation; 11439 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 11440 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 11441 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 11442 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 11443 FormatStyle::UT_ForContinuationAndIndentation); 11444 // For backward compatibility: 11445 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 11446 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 11447 11448 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11449 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 11450 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11451 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 11452 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 11453 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 11454 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 11455 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 11456 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11457 // For backward compatibility: 11458 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 11459 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11460 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 11461 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11462 11463 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 11464 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 11465 FormatStyle::SBPO_Never); 11466 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 11467 FormatStyle::SBPO_Always); 11468 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 11469 FormatStyle::SBPO_ControlStatements); 11470 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 11471 FormatStyle::SBPO_NonEmptyParentheses); 11472 // For backward compatibility: 11473 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 11474 FormatStyle::SBPO_Never); 11475 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 11476 FormatStyle::SBPO_ControlStatements); 11477 11478 Style.ColumnLimit = 123; 11479 FormatStyle BaseStyle = getLLVMStyle(); 11480 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 11481 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 11482 11483 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 11484 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 11485 FormatStyle::BS_Attach); 11486 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 11487 FormatStyle::BS_Linux); 11488 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 11489 FormatStyle::BS_Mozilla); 11490 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 11491 FormatStyle::BS_Stroustrup); 11492 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 11493 FormatStyle::BS_Allman); 11494 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 11495 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 11496 FormatStyle::BS_WebKit); 11497 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 11498 FormatStyle::BS_Custom); 11499 11500 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 11501 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 11502 FormatStyle::RTBS_None); 11503 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 11504 FormatStyle::RTBS_All); 11505 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 11506 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 11507 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 11508 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 11509 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 11510 AlwaysBreakAfterReturnType, 11511 FormatStyle::RTBS_TopLevelDefinitions); 11512 11513 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11514 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations, 11515 FormatStyle::BTDS_No); 11516 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations, 11517 FormatStyle::BTDS_MultiLine); 11518 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations, 11519 FormatStyle::BTDS_Yes); 11520 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations, 11521 FormatStyle::BTDS_MultiLine); 11522 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations, 11523 FormatStyle::BTDS_Yes); 11524 11525 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 11526 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 11527 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 11528 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 11529 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 11530 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 11531 AlwaysBreakAfterDefinitionReturnType, 11532 FormatStyle::DRTBS_TopLevel); 11533 11534 Style.NamespaceIndentation = FormatStyle::NI_All; 11535 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 11536 FormatStyle::NI_None); 11537 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 11538 FormatStyle::NI_Inner); 11539 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 11540 FormatStyle::NI_All); 11541 11542 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always; 11543 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 11544 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 11545 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 11546 AllowShortIfStatementsOnASingleLine, 11547 FormatStyle::SIS_WithoutElse); 11548 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 11549 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Always); 11550 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 11551 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 11552 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 11553 AllowShortIfStatementsOnASingleLine, 11554 FormatStyle::SIS_WithoutElse); 11555 11556 // FIXME: This is required because parsing a configuration simply overwrites 11557 // the first N elements of the list instead of resetting it. 11558 Style.ForEachMacros.clear(); 11559 std::vector<std::string> BoostForeach; 11560 BoostForeach.push_back("BOOST_FOREACH"); 11561 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 11562 std::vector<std::string> BoostAndQForeach; 11563 BoostAndQForeach.push_back("BOOST_FOREACH"); 11564 BoostAndQForeach.push_back("Q_FOREACH"); 11565 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 11566 BoostAndQForeach); 11567 11568 Style.StatementMacros.clear(); 11569 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 11570 std::vector<std::string>{"QUNUSED"}); 11571 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 11572 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 11573 11574 Style.IncludeStyle.IncludeCategories.clear(); 11575 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 11576 {"abc/.*", 2}, {".*", 1}}; 11577 CHECK_PARSE("IncludeCategories:\n" 11578 " - Regex: abc/.*\n" 11579 " Priority: 2\n" 11580 " - Regex: .*\n" 11581 " Priority: 1", 11582 IncludeStyle.IncludeCategories, ExpectedCategories); 11583 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 11584 "abc$"); 11585 11586 Style.RawStringFormats.clear(); 11587 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 11588 { 11589 FormatStyle::LK_TextProto, 11590 {"pb", "proto"}, 11591 {"PARSE_TEXT_PROTO"}, 11592 /*CanonicalDelimiter=*/"", 11593 "llvm", 11594 }, 11595 { 11596 FormatStyle::LK_Cpp, 11597 {"cc", "cpp"}, 11598 {"C_CODEBLOCK", "CPPEVAL"}, 11599 /*CanonicalDelimiter=*/"cc", 11600 /*BasedOnStyle=*/"", 11601 }, 11602 }; 11603 11604 CHECK_PARSE("RawStringFormats:\n" 11605 " - Language: TextProto\n" 11606 " Delimiters:\n" 11607 " - 'pb'\n" 11608 " - 'proto'\n" 11609 " EnclosingFunctions:\n" 11610 " - 'PARSE_TEXT_PROTO'\n" 11611 " BasedOnStyle: llvm\n" 11612 " - Language: Cpp\n" 11613 " Delimiters:\n" 11614 " - 'cc'\n" 11615 " - 'cpp'\n" 11616 " EnclosingFunctions:\n" 11617 " - 'C_CODEBLOCK'\n" 11618 " - 'CPPEVAL'\n" 11619 " CanonicalDelimiter: 'cc'", 11620 RawStringFormats, ExpectedRawStringFormats); 11621 } 11622 11623 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 11624 FormatStyle Style = {}; 11625 Style.Language = FormatStyle::LK_Cpp; 11626 CHECK_PARSE("Language: Cpp\n" 11627 "IndentWidth: 12", 11628 IndentWidth, 12u); 11629 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 11630 "IndentWidth: 34", 11631 &Style), 11632 ParseError::Unsuitable); 11633 EXPECT_EQ(12u, Style.IndentWidth); 11634 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 11635 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 11636 11637 Style.Language = FormatStyle::LK_JavaScript; 11638 CHECK_PARSE("Language: JavaScript\n" 11639 "IndentWidth: 12", 11640 IndentWidth, 12u); 11641 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 11642 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 11643 "IndentWidth: 34", 11644 &Style), 11645 ParseError::Unsuitable); 11646 EXPECT_EQ(23u, Style.IndentWidth); 11647 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 11648 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 11649 11650 CHECK_PARSE("BasedOnStyle: LLVM\n" 11651 "IndentWidth: 67", 11652 IndentWidth, 67u); 11653 11654 CHECK_PARSE("---\n" 11655 "Language: JavaScript\n" 11656 "IndentWidth: 12\n" 11657 "---\n" 11658 "Language: Cpp\n" 11659 "IndentWidth: 34\n" 11660 "...\n", 11661 IndentWidth, 12u); 11662 11663 Style.Language = FormatStyle::LK_Cpp; 11664 CHECK_PARSE("---\n" 11665 "Language: JavaScript\n" 11666 "IndentWidth: 12\n" 11667 "---\n" 11668 "Language: Cpp\n" 11669 "IndentWidth: 34\n" 11670 "...\n", 11671 IndentWidth, 34u); 11672 CHECK_PARSE("---\n" 11673 "IndentWidth: 78\n" 11674 "---\n" 11675 "Language: JavaScript\n" 11676 "IndentWidth: 56\n" 11677 "...\n", 11678 IndentWidth, 78u); 11679 11680 Style.ColumnLimit = 123; 11681 Style.IndentWidth = 234; 11682 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 11683 Style.TabWidth = 345; 11684 EXPECT_FALSE(parseConfiguration("---\n" 11685 "IndentWidth: 456\n" 11686 "BreakBeforeBraces: Allman\n" 11687 "---\n" 11688 "Language: JavaScript\n" 11689 "IndentWidth: 111\n" 11690 "TabWidth: 111\n" 11691 "---\n" 11692 "Language: Cpp\n" 11693 "BreakBeforeBraces: Stroustrup\n" 11694 "TabWidth: 789\n" 11695 "...\n", 11696 &Style)); 11697 EXPECT_EQ(123u, Style.ColumnLimit); 11698 EXPECT_EQ(456u, Style.IndentWidth); 11699 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 11700 EXPECT_EQ(789u, Style.TabWidth); 11701 11702 EXPECT_EQ(parseConfiguration("---\n" 11703 "Language: JavaScript\n" 11704 "IndentWidth: 56\n" 11705 "---\n" 11706 "IndentWidth: 78\n" 11707 "...\n", 11708 &Style), 11709 ParseError::Error); 11710 EXPECT_EQ(parseConfiguration("---\n" 11711 "Language: JavaScript\n" 11712 "IndentWidth: 56\n" 11713 "---\n" 11714 "Language: JavaScript\n" 11715 "IndentWidth: 78\n" 11716 "...\n", 11717 &Style), 11718 ParseError::Error); 11719 11720 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 11721 } 11722 11723 #undef CHECK_PARSE 11724 11725 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 11726 FormatStyle Style = {}; 11727 Style.Language = FormatStyle::LK_JavaScript; 11728 Style.BreakBeforeTernaryOperators = true; 11729 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 11730 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 11731 11732 Style.BreakBeforeTernaryOperators = true; 11733 EXPECT_EQ(0, parseConfiguration("---\n" 11734 "BasedOnStyle: Google\n" 11735 "---\n" 11736 "Language: JavaScript\n" 11737 "IndentWidth: 76\n" 11738 "...\n", 11739 &Style) 11740 .value()); 11741 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 11742 EXPECT_EQ(76u, Style.IndentWidth); 11743 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 11744 } 11745 11746 TEST_F(FormatTest, ConfigurationRoundTripTest) { 11747 FormatStyle Style = getLLVMStyle(); 11748 std::string YAML = configurationAsText(Style); 11749 FormatStyle ParsedStyle = {}; 11750 ParsedStyle.Language = FormatStyle::LK_Cpp; 11751 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 11752 EXPECT_EQ(Style, ParsedStyle); 11753 } 11754 11755 TEST_F(FormatTest, WorksFor8bitEncodings) { 11756 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 11757 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 11758 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 11759 "\"\xef\xee\xf0\xf3...\"", 11760 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 11761 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 11762 "\xef\xee\xf0\xf3...\"", 11763 getLLVMStyleWithColumns(12))); 11764 } 11765 11766 TEST_F(FormatTest, HandlesUTF8BOM) { 11767 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 11768 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 11769 format("\xef\xbb\xbf#include <iostream>")); 11770 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 11771 format("\xef\xbb\xbf\n#include <iostream>")); 11772 } 11773 11774 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 11775 #if !defined(_MSC_VER) 11776 11777 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 11778 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 11779 getLLVMStyleWithColumns(35)); 11780 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 11781 getLLVMStyleWithColumns(31)); 11782 verifyFormat("// Однажды в студёную зимнюю пору...", 11783 getLLVMStyleWithColumns(36)); 11784 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 11785 verifyFormat("/* Однажды в студёную зимнюю пору... */", 11786 getLLVMStyleWithColumns(39)); 11787 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 11788 getLLVMStyleWithColumns(35)); 11789 } 11790 11791 TEST_F(FormatTest, SplitsUTF8Strings) { 11792 // Non-printable characters' width is currently considered to be the length in 11793 // bytes in UTF8. The characters can be displayed in very different manner 11794 // (zero-width, single width with a substitution glyph, expanded to their code 11795 // (e.g. "<8d>"), so there's no single correct way to handle them. 11796 EXPECT_EQ("\"aaaaÄ\"\n" 11797 "\"\xc2\x8d\";", 11798 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 11799 EXPECT_EQ("\"aaaaaaaÄ\"\n" 11800 "\"\xc2\x8d\";", 11801 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 11802 EXPECT_EQ("\"Однажды, в \"\n" 11803 "\"студёную \"\n" 11804 "\"зимнюю \"\n" 11805 "\"пору,\"", 11806 format("\"Однажды, в студёную зимнюю пору,\"", 11807 getLLVMStyleWithColumns(13))); 11808 EXPECT_EQ( 11809 "\"一 二 三 \"\n" 11810 "\"四 五六 \"\n" 11811 "\"七 八 九 \"\n" 11812 "\"十\"", 11813 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 11814 EXPECT_EQ("\"一\t\"\n" 11815 "\"二 \t\"\n" 11816 "\"三 四 \"\n" 11817 "\"五\t\"\n" 11818 "\"六 \t\"\n" 11819 "\"七 \"\n" 11820 "\"八九十\tqq\"", 11821 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 11822 getLLVMStyleWithColumns(11))); 11823 11824 // UTF8 character in an escape sequence. 11825 EXPECT_EQ("\"aaaaaa\"\n" 11826 "\"\\\xC2\x8D\"", 11827 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 11828 } 11829 11830 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 11831 EXPECT_EQ("const char *sssss =\n" 11832 " \"一二三四五六七八\\\n" 11833 " 九 十\";", 11834 format("const char *sssss = \"一二三四五六七八\\\n" 11835 " 九 十\";", 11836 getLLVMStyleWithColumns(30))); 11837 } 11838 11839 TEST_F(FormatTest, SplitsUTF8LineComments) { 11840 EXPECT_EQ("// aaaaÄ\xc2\x8d", 11841 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 11842 EXPECT_EQ("// Я из лесу\n" 11843 "// вышел; был\n" 11844 "// сильный\n" 11845 "// мороз.", 11846 format("// Я из лесу вышел; был сильный мороз.", 11847 getLLVMStyleWithColumns(13))); 11848 EXPECT_EQ("// 一二三\n" 11849 "// 四五六七\n" 11850 "// 八 九\n" 11851 "// 十", 11852 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 11853 } 11854 11855 TEST_F(FormatTest, SplitsUTF8BlockComments) { 11856 EXPECT_EQ("/* Гляжу,\n" 11857 " * поднимается\n" 11858 " * медленно в\n" 11859 " * гору\n" 11860 " * Лошадка,\n" 11861 " * везущая\n" 11862 " * хворосту\n" 11863 " * воз. */", 11864 format("/* Гляжу, поднимается медленно в гору\n" 11865 " * Лошадка, везущая хворосту воз. */", 11866 getLLVMStyleWithColumns(13))); 11867 EXPECT_EQ( 11868 "/* 一二三\n" 11869 " * 四五六七\n" 11870 " * 八 九\n" 11871 " * 十 */", 11872 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 11873 EXPECT_EQ("/* \n" 11874 " * \n" 11875 " * - */", 11876 format("/* - */", getLLVMStyleWithColumns(12))); 11877 } 11878 11879 #endif // _MSC_VER 11880 11881 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 11882 FormatStyle Style = getLLVMStyle(); 11883 11884 Style.ConstructorInitializerIndentWidth = 4; 11885 verifyFormat( 11886 "SomeClass::Constructor()\n" 11887 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11888 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11889 Style); 11890 11891 Style.ConstructorInitializerIndentWidth = 2; 11892 verifyFormat( 11893 "SomeClass::Constructor()\n" 11894 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11895 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11896 Style); 11897 11898 Style.ConstructorInitializerIndentWidth = 0; 11899 verifyFormat( 11900 "SomeClass::Constructor()\n" 11901 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11902 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11903 Style); 11904 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11905 verifyFormat( 11906 "SomeLongTemplateVariableName<\n" 11907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 11908 Style); 11909 verifyFormat( 11910 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 11911 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 11912 Style); 11913 } 11914 11915 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 11916 FormatStyle Style = getLLVMStyle(); 11917 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 11918 Style.ConstructorInitializerIndentWidth = 4; 11919 verifyFormat("SomeClass::Constructor()\n" 11920 " : a(a)\n" 11921 " , b(b)\n" 11922 " , c(c) {}", 11923 Style); 11924 verifyFormat("SomeClass::Constructor()\n" 11925 " : a(a) {}", 11926 Style); 11927 11928 Style.ColumnLimit = 0; 11929 verifyFormat("SomeClass::Constructor()\n" 11930 " : a(a) {}", 11931 Style); 11932 verifyFormat("SomeClass::Constructor() noexcept\n" 11933 " : a(a) {}", 11934 Style); 11935 verifyFormat("SomeClass::Constructor()\n" 11936 " : a(a)\n" 11937 " , b(b)\n" 11938 " , c(c) {}", 11939 Style); 11940 verifyFormat("SomeClass::Constructor()\n" 11941 " : a(a) {\n" 11942 " foo();\n" 11943 " bar();\n" 11944 "}", 11945 Style); 11946 11947 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11948 verifyFormat("SomeClass::Constructor()\n" 11949 " : a(a)\n" 11950 " , b(b)\n" 11951 " , c(c) {\n}", 11952 Style); 11953 verifyFormat("SomeClass::Constructor()\n" 11954 " : a(a) {\n}", 11955 Style); 11956 11957 Style.ColumnLimit = 80; 11958 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11959 Style.ConstructorInitializerIndentWidth = 2; 11960 verifyFormat("SomeClass::Constructor()\n" 11961 " : a(a)\n" 11962 " , b(b)\n" 11963 " , c(c) {}", 11964 Style); 11965 11966 Style.ConstructorInitializerIndentWidth = 0; 11967 verifyFormat("SomeClass::Constructor()\n" 11968 ": a(a)\n" 11969 ", b(b)\n" 11970 ", c(c) {}", 11971 Style); 11972 11973 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 11974 Style.ConstructorInitializerIndentWidth = 4; 11975 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 11976 verifyFormat( 11977 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 11978 Style); 11979 verifyFormat( 11980 "SomeClass::Constructor()\n" 11981 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 11982 Style); 11983 Style.ConstructorInitializerIndentWidth = 4; 11984 Style.ColumnLimit = 60; 11985 verifyFormat("SomeClass::Constructor()\n" 11986 " : aaaaaaaa(aaaaaaaa)\n" 11987 " , aaaaaaaa(aaaaaaaa)\n" 11988 " , aaaaaaaa(aaaaaaaa) {}", 11989 Style); 11990 } 11991 11992 TEST_F(FormatTest, Destructors) { 11993 verifyFormat("void F(int &i) { i.~int(); }"); 11994 verifyFormat("void F(int &i) { i->~int(); }"); 11995 } 11996 11997 TEST_F(FormatTest, FormatsWithWebKitStyle) { 11998 FormatStyle Style = getWebKitStyle(); 11999 12000 // Don't indent in outer namespaces. 12001 verifyFormat("namespace outer {\n" 12002 "int i;\n" 12003 "namespace inner {\n" 12004 " int i;\n" 12005 "} // namespace inner\n" 12006 "} // namespace outer\n" 12007 "namespace other_outer {\n" 12008 "int i;\n" 12009 "}", 12010 Style); 12011 12012 // Don't indent case labels. 12013 verifyFormat("switch (variable) {\n" 12014 "case 1:\n" 12015 "case 2:\n" 12016 " doSomething();\n" 12017 " break;\n" 12018 "default:\n" 12019 " ++variable;\n" 12020 "}", 12021 Style); 12022 12023 // Wrap before binary operators. 12024 EXPECT_EQ("void f()\n" 12025 "{\n" 12026 " if (aaaaaaaaaaaaaaaa\n" 12027 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 12028 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 12029 " return;\n" 12030 "}", 12031 format("void f() {\n" 12032 "if (aaaaaaaaaaaaaaaa\n" 12033 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 12034 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 12035 "return;\n" 12036 "}", 12037 Style)); 12038 12039 // Allow functions on a single line. 12040 verifyFormat("void f() { return; }", Style); 12041 12042 // Constructor initializers are formatted one per line with the "," on the 12043 // new line. 12044 verifyFormat("Constructor()\n" 12045 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 12046 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 12047 " aaaaaaaaaaaaaa)\n" 12048 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 12049 "{\n" 12050 "}", 12051 Style); 12052 verifyFormat("SomeClass::Constructor()\n" 12053 " : a(a)\n" 12054 "{\n" 12055 "}", 12056 Style); 12057 EXPECT_EQ("SomeClass::Constructor()\n" 12058 " : a(a)\n" 12059 "{\n" 12060 "}", 12061 format("SomeClass::Constructor():a(a){}", Style)); 12062 verifyFormat("SomeClass::Constructor()\n" 12063 " : a(a)\n" 12064 " , b(b)\n" 12065 " , c(c)\n" 12066 "{\n" 12067 "}", 12068 Style); 12069 verifyFormat("SomeClass::Constructor()\n" 12070 " : a(a)\n" 12071 "{\n" 12072 " foo();\n" 12073 " bar();\n" 12074 "}", 12075 Style); 12076 12077 // Access specifiers should be aligned left. 12078 verifyFormat("class C {\n" 12079 "public:\n" 12080 " int i;\n" 12081 "};", 12082 Style); 12083 12084 // Do not align comments. 12085 verifyFormat("int a; // Do not\n" 12086 "double b; // align comments.", 12087 Style); 12088 12089 // Do not align operands. 12090 EXPECT_EQ("ASSERT(aaaa\n" 12091 " || bbbb);", 12092 format("ASSERT ( aaaa\n||bbbb);", Style)); 12093 12094 // Accept input's line breaks. 12095 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 12096 " || bbbbbbbbbbbbbbb) {\n" 12097 " i++;\n" 12098 "}", 12099 format("if (aaaaaaaaaaaaaaa\n" 12100 "|| bbbbbbbbbbbbbbb) { i++; }", 12101 Style)); 12102 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 12103 " i++;\n" 12104 "}", 12105 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 12106 12107 // Don't automatically break all macro definitions (llvm.org/PR17842). 12108 verifyFormat("#define aNumber 10", Style); 12109 // However, generally keep the line breaks that the user authored. 12110 EXPECT_EQ("#define aNumber \\\n" 12111 " 10", 12112 format("#define aNumber \\\n" 12113 " 10", 12114 Style)); 12115 12116 // Keep empty and one-element array literals on a single line. 12117 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 12118 " copyItems:YES];", 12119 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 12120 "copyItems:YES];", 12121 Style)); 12122 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 12123 " copyItems:YES];", 12124 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 12125 " copyItems:YES];", 12126 Style)); 12127 // FIXME: This does not seem right, there should be more indentation before 12128 // the array literal's entries. Nested blocks have the same problem. 12129 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 12130 " @\"a\",\n" 12131 " @\"a\"\n" 12132 "]\n" 12133 " copyItems:YES];", 12134 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 12135 " @\"a\",\n" 12136 " @\"a\"\n" 12137 " ]\n" 12138 " copyItems:YES];", 12139 Style)); 12140 EXPECT_EQ( 12141 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 12142 " copyItems:YES];", 12143 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 12144 " copyItems:YES];", 12145 Style)); 12146 12147 verifyFormat("[self.a b:c c:d];", Style); 12148 EXPECT_EQ("[self.a b:c\n" 12149 " c:d];", 12150 format("[self.a b:c\n" 12151 "c:d];", 12152 Style)); 12153 } 12154 12155 TEST_F(FormatTest, FormatsLambdas) { 12156 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 12157 verifyFormat( 12158 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 12159 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 12160 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 12161 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 12162 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 12163 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 12164 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 12165 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 12166 verifyFormat("int x = f(*+[] {});"); 12167 verifyFormat("void f() {\n" 12168 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 12169 "}\n"); 12170 verifyFormat("void f() {\n" 12171 " other(x.begin(), //\n" 12172 " x.end(), //\n" 12173 " [&](int, int) { return 1; });\n" 12174 "}\n"); 12175 verifyFormat("void f() {\n" 12176 " other.other.other.other.other(\n" 12177 " x.begin(), x.end(),\n" 12178 " [something, rather](int, int, int, int, int, int, int) { return 1; });\n" 12179 "}\n"); 12180 verifyFormat("void f() {\n" 12181 " other.other.other.other.other(\n" 12182 " x.begin(), x.end(),\n" 12183 " [something, rather](int, int, int, int, int, int, int) {\n" 12184 " //\n" 12185 " });\n" 12186 "}\n"); 12187 verifyFormat("SomeFunction([]() { // A cool function...\n" 12188 " return 43;\n" 12189 "});"); 12190 EXPECT_EQ("SomeFunction([]() {\n" 12191 "#define A a\n" 12192 " return 43;\n" 12193 "});", 12194 format("SomeFunction([](){\n" 12195 "#define A a\n" 12196 "return 43;\n" 12197 "});")); 12198 verifyFormat("void f() {\n" 12199 " SomeFunction([](decltype(x), A *a) {});\n" 12200 "}"); 12201 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12202 " [](const aaaaaaaaaa &a) { return a; });"); 12203 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 12204 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 12205 "});"); 12206 verifyFormat("Constructor()\n" 12207 " : Field([] { // comment\n" 12208 " int i;\n" 12209 " }) {}"); 12210 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 12211 " return some_parameter.size();\n" 12212 "};"); 12213 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 12214 " [](const string &s) { return s; };"); 12215 verifyFormat("int i = aaaaaa ? 1 //\n" 12216 " : [] {\n" 12217 " return 2; //\n" 12218 " }();"); 12219 verifyFormat("llvm::errs() << \"number of twos is \"\n" 12220 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 12221 " return x == 2; // force break\n" 12222 " });"); 12223 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12224 " [=](int iiiiiiiiiiii) {\n" 12225 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 12226 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 12227 " });", 12228 getLLVMStyleWithColumns(60)); 12229 verifyFormat("SomeFunction({[&] {\n" 12230 " // comment\n" 12231 " },\n" 12232 " [&] {\n" 12233 " // comment\n" 12234 " }});"); 12235 verifyFormat("SomeFunction({[&] {\n" 12236 " // comment\n" 12237 "}});"); 12238 verifyFormat("virtual aaaaaaaaaaaaaaaa(\n" 12239 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 12240 " aaaaa aaaaaaaaa);"); 12241 12242 // Lambdas with return types. 12243 verifyFormat("int c = []() -> int { return 2; }();\n"); 12244 verifyFormat("int c = []() -> int * { return 2; }();\n"); 12245 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 12246 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 12247 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 12248 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 12249 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 12250 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 12251 verifyFormat("[a, a]() -> a<1> {};"); 12252 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 12253 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 12254 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 12255 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 12256 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 12257 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 12258 verifyFormat("[]() -> foo<!5> { return {}; };"); 12259 verifyFormat("[]() -> foo<~5> { return {}; };"); 12260 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 12261 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 12262 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 12263 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 12264 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 12265 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 12266 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 12267 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 12268 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 12269 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 12270 verifyFormat("namespace bar {\n" 12271 "// broken:\n" 12272 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 12273 "} // namespace bar"); 12274 verifyFormat("namespace bar {\n" 12275 "// broken:\n" 12276 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 12277 "} // namespace bar"); 12278 verifyFormat("namespace bar {\n" 12279 "// broken:\n" 12280 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 12281 "} // namespace bar"); 12282 verifyFormat("namespace bar {\n" 12283 "// broken:\n" 12284 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 12285 "} // namespace bar"); 12286 verifyFormat("namespace bar {\n" 12287 "// broken:\n" 12288 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 12289 "} // namespace bar"); 12290 verifyFormat("namespace bar {\n" 12291 "// broken:\n" 12292 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 12293 "} // namespace bar"); 12294 verifyFormat("namespace bar {\n" 12295 "// broken:\n" 12296 "auto foo{[]() -> foo<!5> { return {}; }};\n" 12297 "} // namespace bar"); 12298 verifyFormat("namespace bar {\n" 12299 "// broken:\n" 12300 "auto foo{[]() -> foo<~5> { return {}; }};\n" 12301 "} // namespace bar"); 12302 verifyFormat("namespace bar {\n" 12303 "// broken:\n" 12304 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 12305 "} // namespace bar"); 12306 verifyFormat("namespace bar {\n" 12307 "// broken:\n" 12308 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 12309 "} // namespace bar"); 12310 verifyFormat("namespace bar {\n" 12311 "// broken:\n" 12312 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 12313 "} // namespace bar"); 12314 verifyFormat("namespace bar {\n" 12315 "// broken:\n" 12316 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 12317 "} // namespace bar"); 12318 verifyFormat("namespace bar {\n" 12319 "// broken:\n" 12320 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 12321 "} // namespace bar"); 12322 verifyFormat("namespace bar {\n" 12323 "// broken:\n" 12324 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 12325 "} // namespace bar"); 12326 verifyFormat("namespace bar {\n" 12327 "// broken:\n" 12328 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 12329 "} // namespace bar"); 12330 verifyFormat("namespace bar {\n" 12331 "// broken:\n" 12332 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 12333 "} // namespace bar"); 12334 verifyFormat("namespace bar {\n" 12335 "// broken:\n" 12336 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 12337 "} // namespace bar"); 12338 verifyFormat("namespace bar {\n" 12339 "// broken:\n" 12340 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 12341 "} // namespace bar"); 12342 verifyFormat("[]() -> a<1> {};"); 12343 verifyFormat("[]() -> a<1> { ; };"); 12344 verifyFormat("[]() -> a<1> { ; }();"); 12345 verifyFormat("[a, a]() -> a<true> {};"); 12346 verifyFormat("[]() -> a<true> {};"); 12347 verifyFormat("[]() -> a<true> { ; };"); 12348 verifyFormat("[]() -> a<true> { ; }();"); 12349 verifyFormat("[a, a]() -> a<false> {};"); 12350 verifyFormat("[]() -> a<false> {};"); 12351 verifyFormat("[]() -> a<false> { ; };"); 12352 verifyFormat("[]() -> a<false> { ; }();"); 12353 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 12354 verifyFormat("namespace bar {\n" 12355 "auto foo{[]() -> foo<false> { ; }};\n" 12356 "} // namespace bar"); 12357 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 12358 " int j) -> int {\n" 12359 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 12360 "};"); 12361 verifyFormat( 12362 "aaaaaaaaaaaaaaaaaaaaaa(\n" 12363 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 12364 " return aaaaaaaaaaaaaaaaa;\n" 12365 " });", 12366 getLLVMStyleWithColumns(70)); 12367 verifyFormat("[]() //\n" 12368 " -> int {\n" 12369 " return 1; //\n" 12370 "};"); 12371 12372 // Multiple lambdas in the same parentheses change indentation rules. These 12373 // lambdas are forced to start on new lines. 12374 verifyFormat("SomeFunction(\n" 12375 " []() {\n" 12376 " //\n" 12377 " },\n" 12378 " []() {\n" 12379 " //\n" 12380 " });"); 12381 12382 // A lambda passed as arg0 is always pushed to the next line. 12383 verifyFormat("SomeFunction(\n" 12384 " [this] {\n" 12385 " //\n" 12386 " },\n" 12387 " 1);\n"); 12388 12389 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like the arg0 12390 // case above. 12391 auto Style = getGoogleStyle(); 12392 Style.BinPackArguments = false; 12393 verifyFormat("SomeFunction(\n" 12394 " a,\n" 12395 " [this] {\n" 12396 " //\n" 12397 " },\n" 12398 " b);\n", 12399 Style); 12400 verifyFormat("SomeFunction(\n" 12401 " a,\n" 12402 " [this] {\n" 12403 " //\n" 12404 " },\n" 12405 " b);\n"); 12406 12407 // A lambda with a very long line forces arg0 to be pushed out irrespective of 12408 // the BinPackArguments value (as long as the code is wide enough). 12409 verifyFormat("something->SomeFunction(\n" 12410 " a,\n" 12411 " [this] {\n" 12412 " D0000000000000000000000000000000000000000000000000000000000001();\n" 12413 " },\n" 12414 " b);\n"); 12415 12416 // A multi-line lambda is pulled up as long as the introducer fits on the previous 12417 // line and there are no further args. 12418 verifyFormat("function(1, [this, that] {\n" 12419 " //\n" 12420 "});\n"); 12421 verifyFormat("function([this, that] {\n" 12422 " //\n" 12423 "});\n"); 12424 // FIXME: this format is not ideal and we should consider forcing the first arg 12425 // onto its own line. 12426 verifyFormat("function(a, b, c, //\n" 12427 " d, [this, that] {\n" 12428 " //\n" 12429 " });\n"); 12430 12431 // Multiple lambdas are treated correctly even when there is a short arg0. 12432 verifyFormat("SomeFunction(\n" 12433 " 1,\n" 12434 " [this] {\n" 12435 " //\n" 12436 " },\n" 12437 " [this] {\n" 12438 " //\n" 12439 " },\n" 12440 " 1);\n"); 12441 12442 // More complex introducers. 12443 verifyFormat("return [i, args...] {};"); 12444 12445 // Not lambdas. 12446 verifyFormat("constexpr char hello[]{\"hello\"};"); 12447 verifyFormat("double &operator[](int i) { return 0; }\n" 12448 "int i;"); 12449 verifyFormat("std::unique_ptr<int[]> foo() {}"); 12450 verifyFormat("int i = a[a][a]->f();"); 12451 verifyFormat("int i = (*b)[a]->f();"); 12452 12453 // Other corner cases. 12454 verifyFormat("void f() {\n" 12455 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 12456 " );\n" 12457 "}"); 12458 12459 // Lambdas created through weird macros. 12460 verifyFormat("void f() {\n" 12461 " MACRO((const AA &a) { return 1; });\n" 12462 " MACRO((AA &a) { return 1; });\n" 12463 "}"); 12464 12465 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 12466 " doo_dah();\n" 12467 " doo_dah();\n" 12468 " })) {\n" 12469 "}"); 12470 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 12471 " doo_dah();\n" 12472 " doo_dah();\n" 12473 " })) {\n" 12474 "}"); 12475 verifyFormat("auto lambda = []() {\n" 12476 " int a = 2\n" 12477 "#if A\n" 12478 " + 2\n" 12479 "#endif\n" 12480 " ;\n" 12481 "};"); 12482 12483 // Lambdas with complex multiline introducers. 12484 verifyFormat( 12485 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12486 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 12487 " -> ::std::unordered_set<\n" 12488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 12489 " //\n" 12490 " });"); 12491 12492 FormatStyle DoNotMerge = getLLVMStyle(); 12493 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 12494 verifyFormat("auto c = []() {\n" 12495 " return b;\n" 12496 "};", 12497 "auto c = []() { return b; };", DoNotMerge); 12498 verifyFormat("auto c = []() {\n" 12499 "};", 12500 " auto c = []() {};", DoNotMerge); 12501 12502 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12503 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 12504 verifyFormat("auto c = []() {\n" 12505 " return b;\n" 12506 "};", 12507 "auto c = []() {\n" 12508 " return b;\n" 12509 " };", 12510 MergeEmptyOnly); 12511 verifyFormat("auto c = []() {};", 12512 "auto c = []() {\n" 12513 "};", 12514 MergeEmptyOnly); 12515 12516 FormatStyle MergeInline = getLLVMStyle(); 12517 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 12518 verifyFormat("auto c = []() {\n" 12519 " return b;\n" 12520 "};", 12521 "auto c = []() { return b; };", MergeInline); 12522 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 12523 MergeInline); 12524 verifyFormat("function([]() { return b; }, a)", 12525 "function([]() { return b; }, a)", MergeInline); 12526 verifyFormat("function(a, []() { return b; })", 12527 "function(a, []() { return b; })", MergeInline); 12528 } 12529 12530 TEST_F(FormatTest, EmptyLinesInLambdas) { 12531 verifyFormat("auto lambda = []() {\n" 12532 " x(); //\n" 12533 "};", 12534 "auto lambda = []() {\n" 12535 "\n" 12536 " x(); //\n" 12537 "\n" 12538 "};"); 12539 } 12540 12541 TEST_F(FormatTest, FormatsBlocks) { 12542 FormatStyle ShortBlocks = getLLVMStyle(); 12543 ShortBlocks.AllowShortBlocksOnASingleLine = true; 12544 verifyFormat("int (^Block)(int, int);", ShortBlocks); 12545 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 12546 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 12547 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 12548 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 12549 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 12550 12551 verifyFormat("foo(^{ bar(); });", ShortBlocks); 12552 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 12553 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 12554 12555 verifyFormat("[operation setCompletionBlock:^{\n" 12556 " [self onOperationDone];\n" 12557 "}];"); 12558 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 12559 " [self onOperationDone];\n" 12560 "}]};"); 12561 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 12562 " f();\n" 12563 "}];"); 12564 verifyFormat("int a = [operation block:^int(int *i) {\n" 12565 " return 1;\n" 12566 "}];"); 12567 verifyFormat("[myObject doSomethingWith:arg1\n" 12568 " aaa:^int(int *a) {\n" 12569 " return 1;\n" 12570 " }\n" 12571 " bbb:f(a * bbbbbbbb)];"); 12572 12573 verifyFormat("[operation setCompletionBlock:^{\n" 12574 " [self.delegate newDataAvailable];\n" 12575 "}];", 12576 getLLVMStyleWithColumns(60)); 12577 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 12578 " NSString *path = [self sessionFilePath];\n" 12579 " if (path) {\n" 12580 " // ...\n" 12581 " }\n" 12582 "});"); 12583 verifyFormat("[[SessionService sharedService]\n" 12584 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12585 " if (window) {\n" 12586 " [self windowDidLoad:window];\n" 12587 " } else {\n" 12588 " [self errorLoadingWindow];\n" 12589 " }\n" 12590 " }];"); 12591 verifyFormat("void (^largeBlock)(void) = ^{\n" 12592 " // ...\n" 12593 "};\n", 12594 getLLVMStyleWithColumns(40)); 12595 verifyFormat("[[SessionService sharedService]\n" 12596 " loadWindowWithCompletionBlock: //\n" 12597 " ^(SessionWindow *window) {\n" 12598 " if (window) {\n" 12599 " [self windowDidLoad:window];\n" 12600 " } else {\n" 12601 " [self errorLoadingWindow];\n" 12602 " }\n" 12603 " }];", 12604 getLLVMStyleWithColumns(60)); 12605 verifyFormat("[myObject doSomethingWith:arg1\n" 12606 " firstBlock:^(Foo *a) {\n" 12607 " // ...\n" 12608 " int i;\n" 12609 " }\n" 12610 " secondBlock:^(Bar *b) {\n" 12611 " // ...\n" 12612 " int i;\n" 12613 " }\n" 12614 " thirdBlock:^Foo(Bar *b) {\n" 12615 " // ...\n" 12616 " int i;\n" 12617 " }];"); 12618 verifyFormat("[myObject doSomethingWith:arg1\n" 12619 " firstBlock:-1\n" 12620 " secondBlock:^(Bar *b) {\n" 12621 " // ...\n" 12622 " int i;\n" 12623 " }];"); 12624 12625 verifyFormat("f(^{\n" 12626 " @autoreleasepool {\n" 12627 " if (a) {\n" 12628 " g();\n" 12629 " }\n" 12630 " }\n" 12631 "});"); 12632 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 12633 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 12634 "};"); 12635 12636 FormatStyle FourIndent = getLLVMStyle(); 12637 FourIndent.ObjCBlockIndentWidth = 4; 12638 verifyFormat("[operation setCompletionBlock:^{\n" 12639 " [self onOperationDone];\n" 12640 "}];", 12641 FourIndent); 12642 } 12643 12644 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 12645 FormatStyle ZeroColumn = getLLVMStyle(); 12646 ZeroColumn.ColumnLimit = 0; 12647 12648 verifyFormat("[[SessionService sharedService] " 12649 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12650 " if (window) {\n" 12651 " [self windowDidLoad:window];\n" 12652 " } else {\n" 12653 " [self errorLoadingWindow];\n" 12654 " }\n" 12655 "}];", 12656 ZeroColumn); 12657 EXPECT_EQ("[[SessionService sharedService]\n" 12658 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12659 " if (window) {\n" 12660 " [self windowDidLoad:window];\n" 12661 " } else {\n" 12662 " [self errorLoadingWindow];\n" 12663 " }\n" 12664 " }];", 12665 format("[[SessionService sharedService]\n" 12666 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12667 " if (window) {\n" 12668 " [self windowDidLoad:window];\n" 12669 " } else {\n" 12670 " [self errorLoadingWindow];\n" 12671 " }\n" 12672 "}];", 12673 ZeroColumn)); 12674 verifyFormat("[myObject doSomethingWith:arg1\n" 12675 " firstBlock:^(Foo *a) {\n" 12676 " // ...\n" 12677 " int i;\n" 12678 " }\n" 12679 " secondBlock:^(Bar *b) {\n" 12680 " // ...\n" 12681 " int i;\n" 12682 " }\n" 12683 " thirdBlock:^Foo(Bar *b) {\n" 12684 " // ...\n" 12685 " int i;\n" 12686 " }];", 12687 ZeroColumn); 12688 verifyFormat("f(^{\n" 12689 " @autoreleasepool {\n" 12690 " if (a) {\n" 12691 " g();\n" 12692 " }\n" 12693 " }\n" 12694 "});", 12695 ZeroColumn); 12696 verifyFormat("void (^largeBlock)(void) = ^{\n" 12697 " // ...\n" 12698 "};", 12699 ZeroColumn); 12700 12701 ZeroColumn.AllowShortBlocksOnASingleLine = true; 12702 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 12703 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 12704 ZeroColumn.AllowShortBlocksOnASingleLine = false; 12705 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 12706 " int i;\n" 12707 "};", 12708 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 12709 } 12710 12711 TEST_F(FormatTest, SupportsCRLF) { 12712 EXPECT_EQ("int a;\r\n" 12713 "int b;\r\n" 12714 "int c;\r\n", 12715 format("int a;\r\n" 12716 " int b;\r\n" 12717 " int c;\r\n", 12718 getLLVMStyle())); 12719 EXPECT_EQ("int a;\r\n" 12720 "int b;\r\n" 12721 "int c;\r\n", 12722 format("int a;\r\n" 12723 " int b;\n" 12724 " int c;\r\n", 12725 getLLVMStyle())); 12726 EXPECT_EQ("int a;\n" 12727 "int b;\n" 12728 "int c;\n", 12729 format("int a;\r\n" 12730 " int b;\n" 12731 " int c;\n", 12732 getLLVMStyle())); 12733 EXPECT_EQ("\"aaaaaaa \"\r\n" 12734 "\"bbbbbbb\";\r\n", 12735 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 12736 EXPECT_EQ("#define A \\\r\n" 12737 " b; \\\r\n" 12738 " c; \\\r\n" 12739 " d;\r\n", 12740 format("#define A \\\r\n" 12741 " b; \\\r\n" 12742 " c; d; \r\n", 12743 getGoogleStyle())); 12744 12745 EXPECT_EQ("/*\r\n" 12746 "multi line block comments\r\n" 12747 "should not introduce\r\n" 12748 "an extra carriage return\r\n" 12749 "*/\r\n", 12750 format("/*\r\n" 12751 "multi line block comments\r\n" 12752 "should not introduce\r\n" 12753 "an extra carriage return\r\n" 12754 "*/\r\n")); 12755 } 12756 12757 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 12758 verifyFormat("MY_CLASS(C) {\n" 12759 " int i;\n" 12760 " int j;\n" 12761 "};"); 12762 } 12763 12764 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 12765 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 12766 TwoIndent.ContinuationIndentWidth = 2; 12767 12768 EXPECT_EQ("int i =\n" 12769 " longFunction(\n" 12770 " arg);", 12771 format("int i = longFunction(arg);", TwoIndent)); 12772 12773 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 12774 SixIndent.ContinuationIndentWidth = 6; 12775 12776 EXPECT_EQ("int i =\n" 12777 " longFunction(\n" 12778 " arg);", 12779 format("int i = longFunction(arg);", SixIndent)); 12780 } 12781 12782 TEST_F(FormatTest, SpacesInAngles) { 12783 FormatStyle Spaces = getLLVMStyle(); 12784 Spaces.SpacesInAngles = true; 12785 12786 verifyFormat("static_cast< int >(arg);", Spaces); 12787 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 12788 verifyFormat("f< int, float >();", Spaces); 12789 verifyFormat("template <> g() {}", Spaces); 12790 verifyFormat("template < std::vector< int > > f() {}", Spaces); 12791 verifyFormat("std::function< void(int, int) > fct;", Spaces); 12792 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 12793 Spaces); 12794 12795 Spaces.Standard = FormatStyle::LS_Cpp03; 12796 Spaces.SpacesInAngles = true; 12797 verifyFormat("A< A< int > >();", Spaces); 12798 12799 Spaces.SpacesInAngles = false; 12800 verifyFormat("A<A<int> >();", Spaces); 12801 12802 Spaces.Standard = FormatStyle::LS_Cpp11; 12803 Spaces.SpacesInAngles = true; 12804 verifyFormat("A< A< int > >();", Spaces); 12805 12806 Spaces.SpacesInAngles = false; 12807 verifyFormat("A<A<int>>();", Spaces); 12808 } 12809 12810 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 12811 FormatStyle Style = getLLVMStyle(); 12812 Style.SpaceAfterTemplateKeyword = false; 12813 verifyFormat("template<int> void foo();", Style); 12814 } 12815 12816 TEST_F(FormatTest, TripleAngleBrackets) { 12817 verifyFormat("f<<<1, 1>>>();"); 12818 verifyFormat("f<<<1, 1, 1, s>>>();"); 12819 verifyFormat("f<<<a, b, c, d>>>();"); 12820 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 12821 verifyFormat("f<param><<<1, 1>>>();"); 12822 verifyFormat("f<1><<<1, 1>>>();"); 12823 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 12824 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12825 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 12826 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 12827 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 12828 } 12829 12830 TEST_F(FormatTest, MergeLessLessAtEnd) { 12831 verifyFormat("<<"); 12832 EXPECT_EQ("< < <", format("\\\n<<<")); 12833 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12834 "aaallvm::outs() <<"); 12835 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12836 "aaaallvm::outs()\n <<"); 12837 } 12838 12839 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 12840 std::string code = "#if A\n" 12841 "#if B\n" 12842 "a.\n" 12843 "#endif\n" 12844 " a = 1;\n" 12845 "#else\n" 12846 "#endif\n" 12847 "#if C\n" 12848 "#else\n" 12849 "#endif\n"; 12850 EXPECT_EQ(code, format(code)); 12851 } 12852 12853 TEST_F(FormatTest, HandleConflictMarkers) { 12854 // Git/SVN conflict markers. 12855 EXPECT_EQ("int a;\n" 12856 "void f() {\n" 12857 " callme(some(parameter1,\n" 12858 "<<<<<<< text by the vcs\n" 12859 " parameter2),\n" 12860 "||||||| text by the vcs\n" 12861 " parameter2),\n" 12862 " parameter3,\n" 12863 "======= text by the vcs\n" 12864 " parameter2, parameter3),\n" 12865 ">>>>>>> text by the vcs\n" 12866 " otherparameter);\n", 12867 format("int a;\n" 12868 "void f() {\n" 12869 " callme(some(parameter1,\n" 12870 "<<<<<<< text by the vcs\n" 12871 " parameter2),\n" 12872 "||||||| text by the vcs\n" 12873 " parameter2),\n" 12874 " parameter3,\n" 12875 "======= text by the vcs\n" 12876 " parameter2,\n" 12877 " parameter3),\n" 12878 ">>>>>>> text by the vcs\n" 12879 " otherparameter);\n")); 12880 12881 // Perforce markers. 12882 EXPECT_EQ("void f() {\n" 12883 " function(\n" 12884 ">>>> text by the vcs\n" 12885 " parameter,\n" 12886 "==== text by the vcs\n" 12887 " parameter,\n" 12888 "==== text by the vcs\n" 12889 " parameter,\n" 12890 "<<<< text by the vcs\n" 12891 " parameter);\n", 12892 format("void f() {\n" 12893 " function(\n" 12894 ">>>> text by the vcs\n" 12895 " parameter,\n" 12896 "==== text by the vcs\n" 12897 " parameter,\n" 12898 "==== text by the vcs\n" 12899 " parameter,\n" 12900 "<<<< text by the vcs\n" 12901 " parameter);\n")); 12902 12903 EXPECT_EQ("<<<<<<<\n" 12904 "|||||||\n" 12905 "=======\n" 12906 ">>>>>>>", 12907 format("<<<<<<<\n" 12908 "|||||||\n" 12909 "=======\n" 12910 ">>>>>>>")); 12911 12912 EXPECT_EQ("<<<<<<<\n" 12913 "|||||||\n" 12914 "int i;\n" 12915 "=======\n" 12916 ">>>>>>>", 12917 format("<<<<<<<\n" 12918 "|||||||\n" 12919 "int i;\n" 12920 "=======\n" 12921 ">>>>>>>")); 12922 12923 // FIXME: Handle parsing of macros around conflict markers correctly: 12924 EXPECT_EQ("#define Macro \\\n" 12925 "<<<<<<<\n" 12926 "Something \\\n" 12927 "|||||||\n" 12928 "Else \\\n" 12929 "=======\n" 12930 "Other \\\n" 12931 ">>>>>>>\n" 12932 " End int i;\n", 12933 format("#define Macro \\\n" 12934 "<<<<<<<\n" 12935 " Something \\\n" 12936 "|||||||\n" 12937 " Else \\\n" 12938 "=======\n" 12939 " Other \\\n" 12940 ">>>>>>>\n" 12941 " End\n" 12942 "int i;\n")); 12943 } 12944 12945 TEST_F(FormatTest, DisableRegions) { 12946 EXPECT_EQ("int i;\n" 12947 "// clang-format off\n" 12948 " int j;\n" 12949 "// clang-format on\n" 12950 "int k;", 12951 format(" int i;\n" 12952 " // clang-format off\n" 12953 " int j;\n" 12954 " // clang-format on\n" 12955 " int k;")); 12956 EXPECT_EQ("int i;\n" 12957 "/* clang-format off */\n" 12958 " int j;\n" 12959 "/* clang-format on */\n" 12960 "int k;", 12961 format(" int i;\n" 12962 " /* clang-format off */\n" 12963 " int j;\n" 12964 " /* clang-format on */\n" 12965 " int k;")); 12966 12967 // Don't reflow comments within disabled regions. 12968 EXPECT_EQ( 12969 "// clang-format off\n" 12970 "// long long long long long long line\n" 12971 "/* clang-format on */\n" 12972 "/* long long long\n" 12973 " * long long long\n" 12974 " * line */\n" 12975 "int i;\n" 12976 "/* clang-format off */\n" 12977 "/* long long long long long long line */\n", 12978 format("// clang-format off\n" 12979 "// long long long long long long line\n" 12980 "/* clang-format on */\n" 12981 "/* long long long long long long line */\n" 12982 "int i;\n" 12983 "/* clang-format off */\n" 12984 "/* long long long long long long line */\n", 12985 getLLVMStyleWithColumns(20))); 12986 } 12987 12988 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 12989 format("? ) ="); 12990 verifyNoCrash("#define a\\\n /**/}"); 12991 } 12992 12993 TEST_F(FormatTest, FormatsTableGenCode) { 12994 FormatStyle Style = getLLVMStyle(); 12995 Style.Language = FormatStyle::LK_TableGen; 12996 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 12997 } 12998 12999 TEST_F(FormatTest, ArrayOfTemplates) { 13000 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 13001 format("auto a = new unique_ptr<int > [ 10];")); 13002 13003 FormatStyle Spaces = getLLVMStyle(); 13004 Spaces.SpacesInSquareBrackets = true; 13005 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 13006 format("auto a = new unique_ptr<int > [10];", Spaces)); 13007 } 13008 13009 TEST_F(FormatTest, ArrayAsTemplateType) { 13010 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 13011 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 13012 13013 FormatStyle Spaces = getLLVMStyle(); 13014 Spaces.SpacesInSquareBrackets = true; 13015 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 13016 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 13017 } 13018 13019 TEST_F(FormatTest, NoSpaceAfterSuper) { 13020 verifyFormat("__super::FooBar();"); 13021 } 13022 13023 TEST(FormatStyle, GetStyleWithEmptyFileName) { 13024 llvm::vfs::InMemoryFileSystem FS; 13025 auto Style1 = getStyle("file", "", "Google", "", &FS); 13026 ASSERT_TRUE((bool)Style1); 13027 ASSERT_EQ(*Style1, getGoogleStyle()); 13028 } 13029 13030 TEST(FormatStyle, GetStyleOfFile) { 13031 llvm::vfs::InMemoryFileSystem FS; 13032 // Test 1: format file in the same directory. 13033 ASSERT_TRUE( 13034 FS.addFile("/a/.clang-format", 0, 13035 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 13036 ASSERT_TRUE( 13037 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 13038 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 13039 ASSERT_TRUE((bool)Style1); 13040 ASSERT_EQ(*Style1, getLLVMStyle()); 13041 13042 // Test 2.1: fallback to default. 13043 ASSERT_TRUE( 13044 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 13045 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 13046 ASSERT_TRUE((bool)Style2); 13047 ASSERT_EQ(*Style2, getMozillaStyle()); 13048 13049 // Test 2.2: no format on 'none' fallback style. 13050 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 13051 ASSERT_TRUE((bool)Style2); 13052 ASSERT_EQ(*Style2, getNoStyle()); 13053 13054 // Test 2.3: format if config is found with no based style while fallback is 13055 // 'none'. 13056 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 13057 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 13058 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 13059 ASSERT_TRUE((bool)Style2); 13060 ASSERT_EQ(*Style2, getLLVMStyle()); 13061 13062 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 13063 Style2 = getStyle("{}", "a.h", "none", "", &FS); 13064 ASSERT_TRUE((bool)Style2); 13065 ASSERT_EQ(*Style2, getLLVMStyle()); 13066 13067 // Test 3: format file in parent directory. 13068 ASSERT_TRUE( 13069 FS.addFile("/c/.clang-format", 0, 13070 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 13071 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 13072 llvm::MemoryBuffer::getMemBuffer("int i;"))); 13073 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 13074 ASSERT_TRUE((bool)Style3); 13075 ASSERT_EQ(*Style3, getGoogleStyle()); 13076 13077 // Test 4: error on invalid fallback style 13078 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 13079 ASSERT_FALSE((bool)Style4); 13080 llvm::consumeError(Style4.takeError()); 13081 13082 // Test 5: error on invalid yaml on command line 13083 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 13084 ASSERT_FALSE((bool)Style5); 13085 llvm::consumeError(Style5.takeError()); 13086 13087 // Test 6: error on invalid style 13088 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 13089 ASSERT_FALSE((bool)Style6); 13090 llvm::consumeError(Style6.takeError()); 13091 13092 // Test 7: found config file, error on parsing it 13093 ASSERT_TRUE( 13094 FS.addFile("/d/.clang-format", 0, 13095 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 13096 "InvalidKey: InvalidValue"))); 13097 ASSERT_TRUE( 13098 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 13099 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 13100 ASSERT_FALSE((bool)Style7); 13101 llvm::consumeError(Style7.takeError()); 13102 13103 // Test 8: inferred per-language defaults apply. 13104 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 13105 ASSERT_TRUE((bool)StyleTd); 13106 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 13107 } 13108 13109 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 13110 // Column limit is 20. 13111 std::string Code = "Type *a =\n" 13112 " new Type();\n" 13113 "g(iiiii, 0, jjjjj,\n" 13114 " 0, kkkkk, 0, mm);\n" 13115 "int bad = format ;"; 13116 std::string Expected = "auto a = new Type();\n" 13117 "g(iiiii, nullptr,\n" 13118 " jjjjj, nullptr,\n" 13119 " kkkkk, nullptr,\n" 13120 " mm);\n" 13121 "int bad = format ;"; 13122 FileID ID = Context.createInMemoryFile("format.cpp", Code); 13123 tooling::Replacements Replaces = toReplacements( 13124 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 13125 "auto "), 13126 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 13127 "nullptr"), 13128 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 13129 "nullptr"), 13130 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 13131 "nullptr")}); 13132 13133 format::FormatStyle Style = format::getLLVMStyle(); 13134 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 13135 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 13136 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 13137 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 13138 auto Result = applyAllReplacements(Code, *FormattedReplaces); 13139 EXPECT_TRUE(static_cast<bool>(Result)); 13140 EXPECT_EQ(Expected, *Result); 13141 } 13142 13143 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 13144 std::string Code = "#include \"a.h\"\n" 13145 "#include \"c.h\"\n" 13146 "\n" 13147 "int main() {\n" 13148 " return 0;\n" 13149 "}"; 13150 std::string Expected = "#include \"a.h\"\n" 13151 "#include \"b.h\"\n" 13152 "#include \"c.h\"\n" 13153 "\n" 13154 "int main() {\n" 13155 " return 0;\n" 13156 "}"; 13157 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 13158 tooling::Replacements Replaces = toReplacements( 13159 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 13160 "#include \"b.h\"\n")}); 13161 13162 format::FormatStyle Style = format::getLLVMStyle(); 13163 Style.SortIncludes = true; 13164 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 13165 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 13166 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 13167 auto Result = applyAllReplacements(Code, *FormattedReplaces); 13168 EXPECT_TRUE(static_cast<bool>(Result)); 13169 EXPECT_EQ(Expected, *Result); 13170 } 13171 13172 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 13173 EXPECT_EQ("using std::cin;\n" 13174 "using std::cout;", 13175 format("using std::cout;\n" 13176 "using std::cin;", getGoogleStyle())); 13177 } 13178 13179 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 13180 format::FormatStyle Style = format::getLLVMStyle(); 13181 Style.Standard = FormatStyle::LS_Cpp03; 13182 // cpp03 recognize this string as identifier u8 and literal character 'a' 13183 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 13184 } 13185 13186 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 13187 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 13188 // all modes, including C++11, C++14 and C++17 13189 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 13190 } 13191 13192 TEST_F(FormatTest, DoNotFormatLikelyXml) { 13193 EXPECT_EQ("<!-- ;> -->", 13194 format("<!-- ;> -->", getGoogleStyle())); 13195 EXPECT_EQ(" <!-- >; -->", 13196 format(" <!-- >; -->", getGoogleStyle())); 13197 } 13198 13199 TEST_F(FormatTest, StructuredBindings) { 13200 // Structured bindings is a C++17 feature. 13201 // all modes, including C++11, C++14 and C++17 13202 verifyFormat("auto [a, b] = f();"); 13203 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 13204 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 13205 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 13206 EXPECT_EQ("auto const volatile [a, b] = f();", 13207 format("auto const volatile[a, b] = f();")); 13208 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 13209 EXPECT_EQ("auto &[a, b, c] = f();", 13210 format("auto &[ a , b,c ] = f();")); 13211 EXPECT_EQ("auto &&[a, b, c] = f();", 13212 format("auto &&[ a , b,c ] = f();")); 13213 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 13214 EXPECT_EQ("auto const volatile &&[a, b] = f();", 13215 format("auto const volatile &&[a, b] = f();")); 13216 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 13217 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 13218 EXPECT_EQ("const auto volatile &&[a, b] = f();", 13219 format("const auto volatile &&[a, b] = f();")); 13220 EXPECT_EQ("volatile const auto &&[a, b] = f();", 13221 format("volatile const auto &&[a, b] = f();")); 13222 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 13223 13224 // Make sure we don't mistake structured bindings for lambdas. 13225 FormatStyle PointerMiddle = getLLVMStyle(); 13226 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 13227 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 13228 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 13229 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 13230 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 13231 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 13232 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 13233 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 13234 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 13235 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 13236 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 13237 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 13238 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 13239 13240 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 13241 format("for (const auto && [a, b] : some_range) {\n}")); 13242 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 13243 format("for (const auto & [a, b] : some_range) {\n}")); 13244 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 13245 format("for (const auto[a, b] : some_range) {\n}")); 13246 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 13247 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 13248 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 13249 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 13250 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 13251 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 13252 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 13253 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 13254 13255 format::FormatStyle Spaces = format::getLLVMStyle(); 13256 Spaces.SpacesInSquareBrackets = true; 13257 verifyFormat("auto [ a, b ] = f();", Spaces); 13258 verifyFormat("auto &&[ a, b ] = f();", Spaces); 13259 verifyFormat("auto &[ a, b ] = f();", Spaces); 13260 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 13261 verifyFormat("auto const &[ a, b ] = f();", Spaces); 13262 } 13263 13264 TEST_F(FormatTest, FileAndCode) { 13265 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 13266 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 13267 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 13268 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 13269 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n")); 13270 EXPECT_EQ( 13271 FormatStyle::LK_ObjC, 13272 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 13273 EXPECT_EQ(FormatStyle::LK_ObjC, 13274 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 13275 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 13276 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 13277 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n")); 13278 EXPECT_EQ(FormatStyle::LK_ObjC, 13279 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 13280 EXPECT_EQ( 13281 FormatStyle::LK_ObjC, 13282 guessLanguage("foo.h", 13283 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 13284 EXPECT_EQ( 13285 FormatStyle::LK_Cpp, 13286 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 13287 } 13288 13289 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 13290 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 13291 EXPECT_EQ(FormatStyle::LK_ObjC, 13292 guessLanguage("foo.h", "array[[calculator getIndex]];")); 13293 EXPECT_EQ(FormatStyle::LK_Cpp, 13294 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 13295 EXPECT_EQ( 13296 FormatStyle::LK_Cpp, 13297 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 13298 EXPECT_EQ(FormatStyle::LK_ObjC, 13299 guessLanguage("foo.h", "[[noreturn foo] bar];")); 13300 EXPECT_EQ(FormatStyle::LK_Cpp, 13301 guessLanguage("foo.h", "[[clang::fallthrough]];")); 13302 EXPECT_EQ(FormatStyle::LK_ObjC, 13303 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 13304 EXPECT_EQ(FormatStyle::LK_Cpp, 13305 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 13306 EXPECT_EQ(FormatStyle::LK_Cpp, 13307 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 13308 EXPECT_EQ(FormatStyle::LK_ObjC, 13309 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 13310 EXPECT_EQ(FormatStyle::LK_Cpp, 13311 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 13312 EXPECT_EQ( 13313 FormatStyle::LK_Cpp, 13314 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 13315 EXPECT_EQ( 13316 FormatStyle::LK_Cpp, 13317 guessLanguage("foo.h", 13318 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 13319 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 13320 } 13321 13322 TEST_F(FormatTest, GuessLanguageWithCaret) { 13323 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 13324 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 13325 EXPECT_EQ(FormatStyle::LK_ObjC, 13326 guessLanguage("foo.h", "int(^)(char, float);")); 13327 EXPECT_EQ(FormatStyle::LK_ObjC, 13328 guessLanguage("foo.h", "int(^foo)(char, float);")); 13329 EXPECT_EQ(FormatStyle::LK_ObjC, 13330 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 13331 EXPECT_EQ(FormatStyle::LK_ObjC, 13332 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 13333 EXPECT_EQ( 13334 FormatStyle::LK_ObjC, 13335 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 13336 } 13337 13338 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 13339 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13340 "void f() {\n" 13341 " asm (\"mov %[e], %[d]\"\n" 13342 " : [d] \"=rm\" (d)\n" 13343 " [e] \"rm\" (*e));\n" 13344 "}")); 13345 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13346 "void f() {\n" 13347 " _asm (\"mov %[e], %[d]\"\n" 13348 " : [d] \"=rm\" (d)\n" 13349 " [e] \"rm\" (*e));\n" 13350 "}")); 13351 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13352 "void f() {\n" 13353 " __asm (\"mov %[e], %[d]\"\n" 13354 " : [d] \"=rm\" (d)\n" 13355 " [e] \"rm\" (*e));\n" 13356 "}")); 13357 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13358 "void f() {\n" 13359 " __asm__ (\"mov %[e], %[d]\"\n" 13360 " : [d] \"=rm\" (d)\n" 13361 " [e] \"rm\" (*e));\n" 13362 "}")); 13363 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13364 "void f() {\n" 13365 " asm (\"mov %[e], %[d]\"\n" 13366 " : [d] \"=rm\" (d),\n" 13367 " [e] \"rm\" (*e));\n" 13368 "}")); 13369 EXPECT_EQ(FormatStyle::LK_Cpp, 13370 guessLanguage("foo.h", "void f() {\n" 13371 " asm volatile (\"mov %[e], %[d]\"\n" 13372 " : [d] \"=rm\" (d)\n" 13373 " [e] \"rm\" (*e));\n" 13374 "}")); 13375 } 13376 13377 TEST_F(FormatTest, GuessLanguageWithChildLines) { 13378 EXPECT_EQ(FormatStyle::LK_Cpp, 13379 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 13380 EXPECT_EQ(FormatStyle::LK_ObjC, 13381 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 13382 EXPECT_EQ( 13383 FormatStyle::LK_Cpp, 13384 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 13385 EXPECT_EQ( 13386 FormatStyle::LK_ObjC, 13387 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 13388 } 13389 13390 } // end namespace 13391 } // end namespace format 13392 } // end namespace clang 13393