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 "llvm/Support/Debug.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "gtest/gtest.h" 17 18 #define DEBUG_TYPE "format-test" 19 20 using clang::tooling::ReplacementTest; 21 using clang::tooling::toReplacements; 22 using testing::ScopedTrace; 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 { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 33 34 std::string format(llvm::StringRef Code, 35 const FormatStyle &Style = getLLVMStyle(), 36 StatusCheck CheckComplete = SC_ExpectComplete) { 37 LLVM_DEBUG(llvm::errs() << "---\n"); 38 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 39 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 40 FormattingAttemptStatus Status; 41 tooling::Replacements Replaces = 42 reformat(Style, Code, Ranges, "<stdin>", &Status); 43 if (CheckComplete != SC_DoNotCheck) { 44 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 45 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 46 << Code << "\n\n"; 47 } 48 ReplacementCount = Replaces.size(); 49 auto Result = applyAllReplacements(Code, Replaces); 50 EXPECT_TRUE(static_cast<bool>(Result)); 51 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 52 return *Result; 53 } 54 55 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 56 Style.ColumnLimit = ColumnLimit; 57 return Style; 58 } 59 60 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 61 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 62 } 63 64 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 66 } 67 68 void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, 69 llvm::StringRef Code, 70 const FormatStyle &Style = getLLVMStyle()) { 71 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 72 EXPECT_EQ(Expected.str(), format(Expected, Style)) 73 << "Expected code is not stable"; 74 EXPECT_EQ(Expected.str(), format(Code, Style)); 75 if (Style.Language == FormatStyle::LK_Cpp) { 76 // Objective-C++ is a superset of C++, so everything checked for C++ 77 // needs to be checked for Objective-C++ as well. 78 FormatStyle ObjCStyle = Style; 79 ObjCStyle.Language = FormatStyle::LK_ObjC; 80 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 81 } 82 } 83 84 void _verifyFormat(const char *File, int Line, llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 _verifyFormat(File, Line, Code, test::messUp(Code), Style); 87 } 88 89 void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void _verifyIndependentOfContext(const char *File, int Line, 97 llvm::StringRef Text, 98 const FormatStyle &Style = getLLVMStyle()) { 99 _verifyFormat(File, Line, Text, Style); 100 _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(), 101 Style); 102 } 103 104 /// \brief Verify that clang-format does not crash on the given input. 105 void verifyNoCrash(llvm::StringRef Code, 106 const FormatStyle &Style = getLLVMStyle()) { 107 format(Code, Style, SC_DoNotCheck); 108 } 109 110 int ReplacementCount; 111 }; 112 113 #define verifyIndependentOfContext(...) \ 114 _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__) 115 #define verifyIncompleteFormat(...) \ 116 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__) 117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) 118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle()) 119 120 TEST_F(FormatTest, MessUp) { 121 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 122 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 123 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 124 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 125 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 126 } 127 128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 129 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 130 } 131 132 TEST_F(FormatTest, LLVMStyleOverride) { 133 EXPECT_EQ(FormatStyle::LK_Proto, 134 getLLVMStyle(FormatStyle::LK_Proto).Language); 135 } 136 137 //===----------------------------------------------------------------------===// 138 // Basic function tests. 139 //===----------------------------------------------------------------------===// 140 141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 142 EXPECT_EQ(";", format(";")); 143 } 144 145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 146 EXPECT_EQ("int i;", format(" int i;")); 147 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 148 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 149 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 150 } 151 152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 153 EXPECT_EQ("int i;", format("int\ni;")); 154 } 155 156 TEST_F(FormatTest, FormatsNestedBlockStatements) { 157 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 158 } 159 160 TEST_F(FormatTest, FormatsNestedCall) { 161 verifyFormat("Method(f1, f2(f3));"); 162 verifyFormat("Method(f1(f2, f3()));"); 163 verifyFormat("Method(f1(f2, (f3())));"); 164 } 165 166 TEST_F(FormatTest, NestedNameSpecifiers) { 167 verifyFormat("vector<::Type> v;"); 168 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 169 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 170 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 171 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 172 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 173 verifyFormat("bool a = 2 < ::SomeFunction();"); 174 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 175 verifyFormat("some::string getName();"); 176 } 177 178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 179 EXPECT_EQ("if (a) {\n" 180 " f();\n" 181 "}", 182 format("if(a){f();}")); 183 EXPECT_EQ(4, ReplacementCount); 184 EXPECT_EQ("if (a) {\n" 185 " f();\n" 186 "}", 187 format("if (a) {\n" 188 " f();\n" 189 "}")); 190 EXPECT_EQ(0, ReplacementCount); 191 EXPECT_EQ("/*\r\n" 192 "\r\n" 193 "*/\r\n", 194 format("/*\r\n" 195 "\r\n" 196 "*/\r\n")); 197 EXPECT_EQ(0, ReplacementCount); 198 } 199 200 TEST_F(FormatTest, RemovesEmptyLines) { 201 EXPECT_EQ("class C {\n" 202 " int i;\n" 203 "};", 204 format("class C {\n" 205 " int i;\n" 206 "\n" 207 "};")); 208 209 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 210 EXPECT_EQ("namespace N {\n" 211 "\n" 212 "int i;\n" 213 "}", 214 format("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 getGoogleStyle())); 219 EXPECT_EQ("/* something */ namespace N {\n" 220 "\n" 221 "int i;\n" 222 "}", 223 format("/* something */ namespace N {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 getGoogleStyle())); 228 EXPECT_EQ("inline namespace N {\n" 229 "\n" 230 "int i;\n" 231 "}", 232 format("inline namespace N {\n" 233 "\n" 234 "int i;\n" 235 "}", 236 getGoogleStyle())); 237 EXPECT_EQ("/* something */ inline namespace N {\n" 238 "\n" 239 "int i;\n" 240 "}", 241 format("/* something */ inline namespace N {\n" 242 "\n" 243 "int i;\n" 244 "}", 245 getGoogleStyle())); 246 EXPECT_EQ("export namespace N {\n" 247 "\n" 248 "int i;\n" 249 "}", 250 format("export namespace N {\n" 251 "\n" 252 "int i;\n" 253 "}", 254 getGoogleStyle())); 255 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 256 "\n" 257 "int i;\n" 258 "}", 259 format("extern /**/ \"C\" /**/ {\n" 260 "\n" 261 "int i;\n" 262 "}", 263 getGoogleStyle())); 264 265 auto CustomStyle = getLLVMStyle(); 266 CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom; 267 CustomStyle.BraceWrapping.AfterNamespace = true; 268 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 269 EXPECT_EQ("namespace N\n" 270 "{\n" 271 "\n" 272 "int i;\n" 273 "}", 274 format("namespace N\n" 275 "{\n" 276 "\n" 277 "\n" 278 "int i;\n" 279 "}", 280 CustomStyle)); 281 EXPECT_EQ("/* something */ namespace N\n" 282 "{\n" 283 "\n" 284 "int i;\n" 285 "}", 286 format("/* something */ namespace N {\n" 287 "\n" 288 "\n" 289 "int i;\n" 290 "}", 291 CustomStyle)); 292 EXPECT_EQ("inline namespace N\n" 293 "{\n" 294 "\n" 295 "int i;\n" 296 "}", 297 format("inline namespace N\n" 298 "{\n" 299 "\n" 300 "\n" 301 "int i;\n" 302 "}", 303 CustomStyle)); 304 EXPECT_EQ("/* something */ inline namespace N\n" 305 "{\n" 306 "\n" 307 "int i;\n" 308 "}", 309 format("/* something */ inline namespace N\n" 310 "{\n" 311 "\n" 312 "int i;\n" 313 "}", 314 CustomStyle)); 315 EXPECT_EQ("export namespace N\n" 316 "{\n" 317 "\n" 318 "int i;\n" 319 "}", 320 format("export namespace N\n" 321 "{\n" 322 "\n" 323 "int i;\n" 324 "}", 325 CustomStyle)); 326 EXPECT_EQ("namespace a\n" 327 "{\n" 328 "namespace b\n" 329 "{\n" 330 "\n" 331 "class AA {};\n" 332 "\n" 333 "} // namespace b\n" 334 "} // namespace a\n", 335 format("namespace a\n" 336 "{\n" 337 "namespace b\n" 338 "{\n" 339 "\n" 340 "\n" 341 "class AA {};\n" 342 "\n" 343 "\n" 344 "}\n" 345 "}\n", 346 CustomStyle)); 347 EXPECT_EQ("namespace A /* comment */\n" 348 "{\n" 349 "class B {}\n" 350 "} // namespace A", 351 format("namespace A /* comment */ { class B {} }", CustomStyle)); 352 EXPECT_EQ("namespace A\n" 353 "{ /* comment */\n" 354 "class B {}\n" 355 "} // namespace A", 356 format("namespace A {/* comment */ class B {} }", CustomStyle)); 357 EXPECT_EQ("namespace A\n" 358 "{ /* comment */\n" 359 "\n" 360 "class B {}\n" 361 "\n" 362 "" 363 "} // namespace A", 364 format("namespace A { /* comment */\n" 365 "\n" 366 "\n" 367 "class B {}\n" 368 "\n" 369 "\n" 370 "}", 371 CustomStyle)); 372 EXPECT_EQ("namespace A /* comment */\n" 373 "{\n" 374 "\n" 375 "class B {}\n" 376 "\n" 377 "} // namespace A", 378 format("namespace A/* comment */ {\n" 379 "\n" 380 "\n" 381 "class B {}\n" 382 "\n" 383 "\n" 384 "}", 385 CustomStyle)); 386 387 // ...but do keep inlining and removing empty lines for non-block extern "C" 388 // functions. 389 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 390 EXPECT_EQ("extern \"C\" int f() {\n" 391 " int i = 42;\n" 392 " return i;\n" 393 "}", 394 format("extern \"C\" int f() {\n" 395 "\n" 396 " int i = 42;\n" 397 " return i;\n" 398 "}", 399 getGoogleStyle())); 400 401 // Remove empty lines at the beginning and end of blocks. 402 EXPECT_EQ("void f() {\n" 403 "\n" 404 " if (a) {\n" 405 "\n" 406 " f();\n" 407 " }\n" 408 "}", 409 format("void f() {\n" 410 "\n" 411 " if (a) {\n" 412 "\n" 413 " f();\n" 414 "\n" 415 " }\n" 416 "\n" 417 "}", 418 getLLVMStyle())); 419 EXPECT_EQ("void f() {\n" 420 " if (a) {\n" 421 " f();\n" 422 " }\n" 423 "}", 424 format("void f() {\n" 425 "\n" 426 " if (a) {\n" 427 "\n" 428 " f();\n" 429 "\n" 430 " }\n" 431 "\n" 432 "}", 433 getGoogleStyle())); 434 435 // Don't remove empty lines in more complex control statements. 436 EXPECT_EQ("void f() {\n" 437 " if (a) {\n" 438 " f();\n" 439 "\n" 440 " } else if (b) {\n" 441 " f();\n" 442 " }\n" 443 "}", 444 format("void f() {\n" 445 " if (a) {\n" 446 " f();\n" 447 "\n" 448 " } else if (b) {\n" 449 " f();\n" 450 "\n" 451 " }\n" 452 "\n" 453 "}")); 454 455 // Don't remove empty lines before namespace endings. 456 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 457 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 458 EXPECT_EQ("namespace {\n" 459 "int i;\n" 460 "\n" 461 "}", 462 format("namespace {\n" 463 "int i;\n" 464 "\n" 465 "}", 466 LLVMWithNoNamespaceFix)); 467 EXPECT_EQ("namespace {\n" 468 "int i;\n" 469 "}", 470 format("namespace {\n" 471 "int i;\n" 472 "}", 473 LLVMWithNoNamespaceFix)); 474 EXPECT_EQ("namespace {\n" 475 "int i;\n" 476 "\n" 477 "};", 478 format("namespace {\n" 479 "int i;\n" 480 "\n" 481 "};", 482 LLVMWithNoNamespaceFix)); 483 EXPECT_EQ("namespace {\n" 484 "int i;\n" 485 "};", 486 format("namespace {\n" 487 "int i;\n" 488 "};", 489 LLVMWithNoNamespaceFix)); 490 EXPECT_EQ("namespace {\n" 491 "int i;\n" 492 "\n" 493 "}", 494 format("namespace {\n" 495 "int i;\n" 496 "\n" 497 "}")); 498 EXPECT_EQ("namespace {\n" 499 "int i;\n" 500 "\n" 501 "} // namespace", 502 format("namespace {\n" 503 "int i;\n" 504 "\n" 505 "} // namespace")); 506 507 FormatStyle Style = getLLVMStyle(); 508 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 509 Style.MaxEmptyLinesToKeep = 2; 510 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 511 Style.BraceWrapping.AfterClass = true; 512 Style.BraceWrapping.AfterFunction = true; 513 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 514 515 EXPECT_EQ("class Foo\n" 516 "{\n" 517 " Foo() {}\n" 518 "\n" 519 " void funk() {}\n" 520 "};", 521 format("class Foo\n" 522 "{\n" 523 " Foo()\n" 524 " {\n" 525 " }\n" 526 "\n" 527 " void funk() {}\n" 528 "};", 529 Style)); 530 } 531 532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 533 verifyFormat("x = (a) and (b);"); 534 verifyFormat("x = (a) or (b);"); 535 verifyFormat("x = (a) bitand (b);"); 536 verifyFormat("x = (a) bitor (b);"); 537 verifyFormat("x = (a) not_eq (b);"); 538 verifyFormat("x = (a) and_eq (b);"); 539 verifyFormat("x = (a) or_eq (b);"); 540 verifyFormat("x = (a) xor (b);"); 541 } 542 543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 544 verifyFormat("x = compl(a);"); 545 verifyFormat("x = not(a);"); 546 verifyFormat("x = bitand(a);"); 547 // Unary operator must not be merged with the next identifier 548 verifyFormat("x = compl a;"); 549 verifyFormat("x = not a;"); 550 verifyFormat("x = bitand a;"); 551 } 552 553 //===----------------------------------------------------------------------===// 554 // Tests for control statements. 555 //===----------------------------------------------------------------------===// 556 557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 558 verifyFormat("if (true)\n f();\ng();"); 559 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 560 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 561 verifyFormat("if constexpr (true)\n" 562 " f();\ng();"); 563 verifyFormat("if CONSTEXPR (true)\n" 564 " f();\ng();"); 565 verifyFormat("if constexpr (a)\n" 566 " if constexpr (b)\n" 567 " if constexpr (c)\n" 568 " g();\n" 569 "h();"); 570 verifyFormat("if CONSTEXPR (a)\n" 571 " if CONSTEXPR (b)\n" 572 " if CONSTEXPR (c)\n" 573 " g();\n" 574 "h();"); 575 verifyFormat("if constexpr (a)\n" 576 " if constexpr (b) {\n" 577 " f();\n" 578 " }\n" 579 "g();"); 580 verifyFormat("if CONSTEXPR (a)\n" 581 " if CONSTEXPR (b) {\n" 582 " f();\n" 583 " }\n" 584 "g();"); 585 586 verifyFormat("if (a)\n" 587 " g();"); 588 verifyFormat("if (a) {\n" 589 " g()\n" 590 "};"); 591 verifyFormat("if (a)\n" 592 " g();\n" 593 "else\n" 594 " g();"); 595 verifyFormat("if (a) {\n" 596 " g();\n" 597 "} else\n" 598 " g();"); 599 verifyFormat("if (a)\n" 600 " g();\n" 601 "else {\n" 602 " g();\n" 603 "}"); 604 verifyFormat("if (a) {\n" 605 " g();\n" 606 "} else {\n" 607 " g();\n" 608 "}"); 609 verifyFormat("if (a)\n" 610 " g();\n" 611 "else if (b)\n" 612 " g();\n" 613 "else\n" 614 " g();"); 615 verifyFormat("if (a) {\n" 616 " g();\n" 617 "} else if (b)\n" 618 " g();\n" 619 "else\n" 620 " g();"); 621 verifyFormat("if (a)\n" 622 " g();\n" 623 "else if (b) {\n" 624 " g();\n" 625 "} else\n" 626 " g();"); 627 verifyFormat("if (a)\n" 628 " g();\n" 629 "else if (b)\n" 630 " g();\n" 631 "else {\n" 632 " g();\n" 633 "}"); 634 verifyFormat("if (a)\n" 635 " g();\n" 636 "else if (b) {\n" 637 " g();\n" 638 "} else {\n" 639 " g();\n" 640 "}"); 641 verifyFormat("if (a) {\n" 642 " g();\n" 643 "} else if (b) {\n" 644 " g();\n" 645 "} else {\n" 646 " g();\n" 647 "}"); 648 649 FormatStyle AllowsMergedIf = getLLVMStyle(); 650 AllowsMergedIf.IfMacros.push_back("MYIF"); 651 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 652 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 653 FormatStyle::SIS_WithoutElse; 654 verifyFormat("if (a)\n" 655 " // comment\n" 656 " f();", 657 AllowsMergedIf); 658 verifyFormat("{\n" 659 " if (a)\n" 660 " label:\n" 661 " f();\n" 662 "}", 663 AllowsMergedIf); 664 verifyFormat("#define A \\\n" 665 " if (a) \\\n" 666 " label: \\\n" 667 " f()", 668 AllowsMergedIf); 669 verifyFormat("if (a)\n" 670 " ;", 671 AllowsMergedIf); 672 verifyFormat("if (a)\n" 673 " if (b) return;", 674 AllowsMergedIf); 675 676 verifyFormat("if (a) // Can't merge this\n" 677 " f();\n", 678 AllowsMergedIf); 679 verifyFormat("if (a) /* still don't merge */\n" 680 " f();", 681 AllowsMergedIf); 682 verifyFormat("if (a) { // Never merge this\n" 683 " f();\n" 684 "}", 685 AllowsMergedIf); 686 verifyFormat("if (a) { /* Never merge this */\n" 687 " f();\n" 688 "}", 689 AllowsMergedIf); 690 verifyFormat("MYIF (a)\n" 691 " // comment\n" 692 " f();", 693 AllowsMergedIf); 694 verifyFormat("{\n" 695 " MYIF (a)\n" 696 " label:\n" 697 " f();\n" 698 "}", 699 AllowsMergedIf); 700 verifyFormat("#define A \\\n" 701 " MYIF (a) \\\n" 702 " label: \\\n" 703 " f()", 704 AllowsMergedIf); 705 verifyFormat("MYIF (a)\n" 706 " ;", 707 AllowsMergedIf); 708 verifyFormat("MYIF (a)\n" 709 " MYIF (b) return;", 710 AllowsMergedIf); 711 712 verifyFormat("MYIF (a) // Can't merge this\n" 713 " f();\n", 714 AllowsMergedIf); 715 verifyFormat("MYIF (a) /* still don't merge */\n" 716 " f();", 717 AllowsMergedIf); 718 verifyFormat("MYIF (a) { // Never merge this\n" 719 " f();\n" 720 "}", 721 AllowsMergedIf); 722 verifyFormat("MYIF (a) { /* Never merge this */\n" 723 " f();\n" 724 "}", 725 AllowsMergedIf); 726 727 AllowsMergedIf.ColumnLimit = 14; 728 // Where line-lengths matter, a 2-letter synonym that maintains line length. 729 // Not IF to avoid any confusion that IF is somehow special. 730 AllowsMergedIf.IfMacros.push_back("FI"); 731 verifyFormat("if (a) return;", AllowsMergedIf); 732 verifyFormat("if (aaaaaaaaa)\n" 733 " return;", 734 AllowsMergedIf); 735 verifyFormat("FI (a) return;", AllowsMergedIf); 736 verifyFormat("FI (aaaaaaaaa)\n" 737 " return;", 738 AllowsMergedIf); 739 740 AllowsMergedIf.ColumnLimit = 13; 741 verifyFormat("if (a)\n return;", AllowsMergedIf); 742 verifyFormat("FI (a)\n return;", AllowsMergedIf); 743 744 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 745 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 746 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 747 FormatStyle::SIS_AllIfsAndElse; 748 verifyFormat("if (a)\n" 749 " // comment\n" 750 " f();\n" 751 "else\n" 752 " // comment\n" 753 " f();", 754 AllowsMergedIfElse); 755 verifyFormat("{\n" 756 " if (a)\n" 757 " label:\n" 758 " f();\n" 759 " else\n" 760 " label:\n" 761 " f();\n" 762 "}", 763 AllowsMergedIfElse); 764 verifyFormat("if (a)\n" 765 " ;\n" 766 "else\n" 767 " ;", 768 AllowsMergedIfElse); 769 verifyFormat("if (a) {\n" 770 "} else {\n" 771 "}", 772 AllowsMergedIfElse); 773 verifyFormat("if (a) return;\n" 774 "else if (b) return;\n" 775 "else return;", 776 AllowsMergedIfElse); 777 verifyFormat("if (a) {\n" 778 "} else return;", 779 AllowsMergedIfElse); 780 verifyFormat("if (a) {\n" 781 "} else if (b) return;\n" 782 "else return;", 783 AllowsMergedIfElse); 784 verifyFormat("if (a) return;\n" 785 "else if (b) {\n" 786 "} else return;", 787 AllowsMergedIfElse); 788 verifyFormat("if (a)\n" 789 " if (b) return;\n" 790 " else return;", 791 AllowsMergedIfElse); 792 verifyFormat("if constexpr (a)\n" 793 " if constexpr (b) return;\n" 794 " else if constexpr (c) return;\n" 795 " else return;", 796 AllowsMergedIfElse); 797 verifyFormat("MYIF (a)\n" 798 " // comment\n" 799 " f();\n" 800 "else\n" 801 " // comment\n" 802 " f();", 803 AllowsMergedIfElse); 804 verifyFormat("{\n" 805 " MYIF (a)\n" 806 " label:\n" 807 " f();\n" 808 " else\n" 809 " label:\n" 810 " f();\n" 811 "}", 812 AllowsMergedIfElse); 813 verifyFormat("MYIF (a)\n" 814 " ;\n" 815 "else\n" 816 " ;", 817 AllowsMergedIfElse); 818 verifyFormat("MYIF (a) {\n" 819 "} else {\n" 820 "}", 821 AllowsMergedIfElse); 822 verifyFormat("MYIF (a) return;\n" 823 "else MYIF (b) return;\n" 824 "else return;", 825 AllowsMergedIfElse); 826 verifyFormat("MYIF (a) {\n" 827 "} else return;", 828 AllowsMergedIfElse); 829 verifyFormat("MYIF (a) {\n" 830 "} else MYIF (b) return;\n" 831 "else return;", 832 AllowsMergedIfElse); 833 verifyFormat("MYIF (a) return;\n" 834 "else MYIF (b) {\n" 835 "} else return;", 836 AllowsMergedIfElse); 837 verifyFormat("MYIF (a)\n" 838 " MYIF (b) return;\n" 839 " else return;", 840 AllowsMergedIfElse); 841 verifyFormat("MYIF constexpr (a)\n" 842 " MYIF constexpr (b) return;\n" 843 " else MYIF constexpr (c) return;\n" 844 " else return;", 845 AllowsMergedIfElse); 846 } 847 848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 849 FormatStyle AllowsMergedIf = getLLVMStyle(); 850 AllowsMergedIf.IfMacros.push_back("MYIF"); 851 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 852 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 853 FormatStyle::SIS_WithoutElse; 854 verifyFormat("if (a)\n" 855 " f();\n" 856 "else {\n" 857 " g();\n" 858 "}", 859 AllowsMergedIf); 860 verifyFormat("if (a)\n" 861 " f();\n" 862 "else\n" 863 " g();\n", 864 AllowsMergedIf); 865 866 verifyFormat("if (a) g();", AllowsMergedIf); 867 verifyFormat("if (a) {\n" 868 " g()\n" 869 "};", 870 AllowsMergedIf); 871 verifyFormat("if (a)\n" 872 " g();\n" 873 "else\n" 874 " g();", 875 AllowsMergedIf); 876 verifyFormat("if (a) {\n" 877 " g();\n" 878 "} else\n" 879 " g();", 880 AllowsMergedIf); 881 verifyFormat("if (a)\n" 882 " g();\n" 883 "else {\n" 884 " g();\n" 885 "}", 886 AllowsMergedIf); 887 verifyFormat("if (a) {\n" 888 " g();\n" 889 "} else {\n" 890 " g();\n" 891 "}", 892 AllowsMergedIf); 893 verifyFormat("if (a)\n" 894 " g();\n" 895 "else if (b)\n" 896 " g();\n" 897 "else\n" 898 " g();", 899 AllowsMergedIf); 900 verifyFormat("if (a) {\n" 901 " g();\n" 902 "} else if (b)\n" 903 " g();\n" 904 "else\n" 905 " g();", 906 AllowsMergedIf); 907 verifyFormat("if (a)\n" 908 " g();\n" 909 "else if (b) {\n" 910 " g();\n" 911 "} else\n" 912 " g();", 913 AllowsMergedIf); 914 verifyFormat("if (a)\n" 915 " g();\n" 916 "else if (b)\n" 917 " g();\n" 918 "else {\n" 919 " g();\n" 920 "}", 921 AllowsMergedIf); 922 verifyFormat("if (a)\n" 923 " g();\n" 924 "else if (b) {\n" 925 " g();\n" 926 "} else {\n" 927 " g();\n" 928 "}", 929 AllowsMergedIf); 930 verifyFormat("if (a) {\n" 931 " g();\n" 932 "} else if (b) {\n" 933 " g();\n" 934 "} else {\n" 935 " g();\n" 936 "}", 937 AllowsMergedIf); 938 verifyFormat("MYIF (a)\n" 939 " f();\n" 940 "else {\n" 941 " g();\n" 942 "}", 943 AllowsMergedIf); 944 verifyFormat("MYIF (a)\n" 945 " f();\n" 946 "else\n" 947 " g();\n", 948 AllowsMergedIf); 949 950 verifyFormat("MYIF (a) g();", AllowsMergedIf); 951 verifyFormat("MYIF (a) {\n" 952 " g()\n" 953 "};", 954 AllowsMergedIf); 955 verifyFormat("MYIF (a)\n" 956 " g();\n" 957 "else\n" 958 " g();", 959 AllowsMergedIf); 960 verifyFormat("MYIF (a) {\n" 961 " g();\n" 962 "} else\n" 963 " g();", 964 AllowsMergedIf); 965 verifyFormat("MYIF (a)\n" 966 " g();\n" 967 "else {\n" 968 " g();\n" 969 "}", 970 AllowsMergedIf); 971 verifyFormat("MYIF (a) {\n" 972 " g();\n" 973 "} else {\n" 974 " g();\n" 975 "}", 976 AllowsMergedIf); 977 verifyFormat("MYIF (a)\n" 978 " g();\n" 979 "else MYIF (b)\n" 980 " g();\n" 981 "else\n" 982 " g();", 983 AllowsMergedIf); 984 verifyFormat("MYIF (a)\n" 985 " g();\n" 986 "else if (b)\n" 987 " g();\n" 988 "else\n" 989 " g();", 990 AllowsMergedIf); 991 verifyFormat("MYIF (a) {\n" 992 " g();\n" 993 "} else MYIF (b)\n" 994 " g();\n" 995 "else\n" 996 " g();", 997 AllowsMergedIf); 998 verifyFormat("MYIF (a) {\n" 999 " g();\n" 1000 "} else if (b)\n" 1001 " g();\n" 1002 "else\n" 1003 " g();", 1004 AllowsMergedIf); 1005 verifyFormat("MYIF (a)\n" 1006 " g();\n" 1007 "else MYIF (b) {\n" 1008 " g();\n" 1009 "} else\n" 1010 " g();", 1011 AllowsMergedIf); 1012 verifyFormat("MYIF (a)\n" 1013 " g();\n" 1014 "else if (b) {\n" 1015 " g();\n" 1016 "} else\n" 1017 " g();", 1018 AllowsMergedIf); 1019 verifyFormat("MYIF (a)\n" 1020 " g();\n" 1021 "else MYIF (b)\n" 1022 " g();\n" 1023 "else {\n" 1024 " g();\n" 1025 "}", 1026 AllowsMergedIf); 1027 verifyFormat("MYIF (a)\n" 1028 " g();\n" 1029 "else if (b)\n" 1030 " g();\n" 1031 "else {\n" 1032 " g();\n" 1033 "}", 1034 AllowsMergedIf); 1035 verifyFormat("MYIF (a)\n" 1036 " g();\n" 1037 "else MYIF (b) {\n" 1038 " g();\n" 1039 "} else {\n" 1040 " g();\n" 1041 "}", 1042 AllowsMergedIf); 1043 verifyFormat("MYIF (a)\n" 1044 " g();\n" 1045 "else if (b) {\n" 1046 " g();\n" 1047 "} else {\n" 1048 " g();\n" 1049 "}", 1050 AllowsMergedIf); 1051 verifyFormat("MYIF (a) {\n" 1052 " g();\n" 1053 "} else MYIF (b) {\n" 1054 " g();\n" 1055 "} else {\n" 1056 " g();\n" 1057 "}", 1058 AllowsMergedIf); 1059 verifyFormat("MYIF (a) {\n" 1060 " g();\n" 1061 "} else if (b) {\n" 1062 " g();\n" 1063 "} else {\n" 1064 " g();\n" 1065 "}", 1066 AllowsMergedIf); 1067 1068 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1069 FormatStyle::SIS_OnlyFirstIf; 1070 1071 verifyFormat("if (a) f();\n" 1072 "else {\n" 1073 " g();\n" 1074 "}", 1075 AllowsMergedIf); 1076 verifyFormat("if (a) f();\n" 1077 "else {\n" 1078 " if (a) f();\n" 1079 " else {\n" 1080 " g();\n" 1081 " }\n" 1082 " g();\n" 1083 "}", 1084 AllowsMergedIf); 1085 1086 verifyFormat("if (a) g();", AllowsMergedIf); 1087 verifyFormat("if (a) {\n" 1088 " g()\n" 1089 "};", 1090 AllowsMergedIf); 1091 verifyFormat("if (a) g();\n" 1092 "else\n" 1093 " g();", 1094 AllowsMergedIf); 1095 verifyFormat("if (a) {\n" 1096 " g();\n" 1097 "} else\n" 1098 " g();", 1099 AllowsMergedIf); 1100 verifyFormat("if (a) g();\n" 1101 "else {\n" 1102 " g();\n" 1103 "}", 1104 AllowsMergedIf); 1105 verifyFormat("if (a) {\n" 1106 " g();\n" 1107 "} else {\n" 1108 " g();\n" 1109 "}", 1110 AllowsMergedIf); 1111 verifyFormat("if (a) g();\n" 1112 "else if (b)\n" 1113 " g();\n" 1114 "else\n" 1115 " g();", 1116 AllowsMergedIf); 1117 verifyFormat("if (a) {\n" 1118 " g();\n" 1119 "} else if (b)\n" 1120 " g();\n" 1121 "else\n" 1122 " g();", 1123 AllowsMergedIf); 1124 verifyFormat("if (a) g();\n" 1125 "else if (b) {\n" 1126 " g();\n" 1127 "} else\n" 1128 " g();", 1129 AllowsMergedIf); 1130 verifyFormat("if (a) g();\n" 1131 "else if (b)\n" 1132 " g();\n" 1133 "else {\n" 1134 " g();\n" 1135 "}", 1136 AllowsMergedIf); 1137 verifyFormat("if (a) g();\n" 1138 "else if (b) {\n" 1139 " g();\n" 1140 "} else {\n" 1141 " g();\n" 1142 "}", 1143 AllowsMergedIf); 1144 verifyFormat("if (a) {\n" 1145 " g();\n" 1146 "} else if (b) {\n" 1147 " g();\n" 1148 "} else {\n" 1149 " g();\n" 1150 "}", 1151 AllowsMergedIf); 1152 verifyFormat("MYIF (a) f();\n" 1153 "else {\n" 1154 " g();\n" 1155 "}", 1156 AllowsMergedIf); 1157 verifyFormat("MYIF (a) f();\n" 1158 "else {\n" 1159 " if (a) f();\n" 1160 " else {\n" 1161 " g();\n" 1162 " }\n" 1163 " g();\n" 1164 "}", 1165 AllowsMergedIf); 1166 1167 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1168 verifyFormat("MYIF (a) {\n" 1169 " g()\n" 1170 "};", 1171 AllowsMergedIf); 1172 verifyFormat("MYIF (a) g();\n" 1173 "else\n" 1174 " g();", 1175 AllowsMergedIf); 1176 verifyFormat("MYIF (a) {\n" 1177 " g();\n" 1178 "} else\n" 1179 " g();", 1180 AllowsMergedIf); 1181 verifyFormat("MYIF (a) g();\n" 1182 "else {\n" 1183 " g();\n" 1184 "}", 1185 AllowsMergedIf); 1186 verifyFormat("MYIF (a) {\n" 1187 " g();\n" 1188 "} else {\n" 1189 " g();\n" 1190 "}", 1191 AllowsMergedIf); 1192 verifyFormat("MYIF (a) g();\n" 1193 "else MYIF (b)\n" 1194 " g();\n" 1195 "else\n" 1196 " g();", 1197 AllowsMergedIf); 1198 verifyFormat("MYIF (a) g();\n" 1199 "else if (b)\n" 1200 " g();\n" 1201 "else\n" 1202 " g();", 1203 AllowsMergedIf); 1204 verifyFormat("MYIF (a) {\n" 1205 " g();\n" 1206 "} else MYIF (b)\n" 1207 " g();\n" 1208 "else\n" 1209 " g();", 1210 AllowsMergedIf); 1211 verifyFormat("MYIF (a) {\n" 1212 " g();\n" 1213 "} else if (b)\n" 1214 " g();\n" 1215 "else\n" 1216 " g();", 1217 AllowsMergedIf); 1218 verifyFormat("MYIF (a) g();\n" 1219 "else MYIF (b) {\n" 1220 " g();\n" 1221 "} else\n" 1222 " g();", 1223 AllowsMergedIf); 1224 verifyFormat("MYIF (a) g();\n" 1225 "else if (b) {\n" 1226 " g();\n" 1227 "} else\n" 1228 " g();", 1229 AllowsMergedIf); 1230 verifyFormat("MYIF (a) g();\n" 1231 "else MYIF (b)\n" 1232 " g();\n" 1233 "else {\n" 1234 " g();\n" 1235 "}", 1236 AllowsMergedIf); 1237 verifyFormat("MYIF (a) g();\n" 1238 "else if (b)\n" 1239 " g();\n" 1240 "else {\n" 1241 " g();\n" 1242 "}", 1243 AllowsMergedIf); 1244 verifyFormat("MYIF (a) g();\n" 1245 "else MYIF (b) {\n" 1246 " g();\n" 1247 "} else {\n" 1248 " g();\n" 1249 "}", 1250 AllowsMergedIf); 1251 verifyFormat("MYIF (a) g();\n" 1252 "else if (b) {\n" 1253 " g();\n" 1254 "} else {\n" 1255 " g();\n" 1256 "}", 1257 AllowsMergedIf); 1258 verifyFormat("MYIF (a) {\n" 1259 " g();\n" 1260 "} else MYIF (b) {\n" 1261 " g();\n" 1262 "} else {\n" 1263 " g();\n" 1264 "}", 1265 AllowsMergedIf); 1266 verifyFormat("MYIF (a) {\n" 1267 " g();\n" 1268 "} else if (b) {\n" 1269 " g();\n" 1270 "} else {\n" 1271 " g();\n" 1272 "}", 1273 AllowsMergedIf); 1274 1275 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1276 FormatStyle::SIS_AllIfsAndElse; 1277 1278 verifyFormat("if (a) f();\n" 1279 "else {\n" 1280 " g();\n" 1281 "}", 1282 AllowsMergedIf); 1283 verifyFormat("if (a) f();\n" 1284 "else {\n" 1285 " if (a) f();\n" 1286 " else {\n" 1287 " g();\n" 1288 " }\n" 1289 " g();\n" 1290 "}", 1291 AllowsMergedIf); 1292 1293 verifyFormat("if (a) g();", AllowsMergedIf); 1294 verifyFormat("if (a) {\n" 1295 " g()\n" 1296 "};", 1297 AllowsMergedIf); 1298 verifyFormat("if (a) g();\n" 1299 "else g();", 1300 AllowsMergedIf); 1301 verifyFormat("if (a) {\n" 1302 " g();\n" 1303 "} else g();", 1304 AllowsMergedIf); 1305 verifyFormat("if (a) g();\n" 1306 "else {\n" 1307 " g();\n" 1308 "}", 1309 AllowsMergedIf); 1310 verifyFormat("if (a) {\n" 1311 " g();\n" 1312 "} else {\n" 1313 " g();\n" 1314 "}", 1315 AllowsMergedIf); 1316 verifyFormat("if (a) g();\n" 1317 "else if (b) g();\n" 1318 "else g();", 1319 AllowsMergedIf); 1320 verifyFormat("if (a) {\n" 1321 " g();\n" 1322 "} else if (b) g();\n" 1323 "else g();", 1324 AllowsMergedIf); 1325 verifyFormat("if (a) g();\n" 1326 "else if (b) {\n" 1327 " g();\n" 1328 "} else g();", 1329 AllowsMergedIf); 1330 verifyFormat("if (a) g();\n" 1331 "else if (b) g();\n" 1332 "else {\n" 1333 " g();\n" 1334 "}", 1335 AllowsMergedIf); 1336 verifyFormat("if (a) g();\n" 1337 "else if (b) {\n" 1338 " g();\n" 1339 "} else {\n" 1340 " g();\n" 1341 "}", 1342 AllowsMergedIf); 1343 verifyFormat("if (a) {\n" 1344 " g();\n" 1345 "} else if (b) {\n" 1346 " g();\n" 1347 "} else {\n" 1348 " g();\n" 1349 "}", 1350 AllowsMergedIf); 1351 verifyFormat("MYIF (a) f();\n" 1352 "else {\n" 1353 " g();\n" 1354 "}", 1355 AllowsMergedIf); 1356 verifyFormat("MYIF (a) f();\n" 1357 "else {\n" 1358 " if (a) f();\n" 1359 " else {\n" 1360 " g();\n" 1361 " }\n" 1362 " g();\n" 1363 "}", 1364 AllowsMergedIf); 1365 1366 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1367 verifyFormat("MYIF (a) {\n" 1368 " g()\n" 1369 "};", 1370 AllowsMergedIf); 1371 verifyFormat("MYIF (a) g();\n" 1372 "else g();", 1373 AllowsMergedIf); 1374 verifyFormat("MYIF (a) {\n" 1375 " g();\n" 1376 "} else g();", 1377 AllowsMergedIf); 1378 verifyFormat("MYIF (a) g();\n" 1379 "else {\n" 1380 " g();\n" 1381 "}", 1382 AllowsMergedIf); 1383 verifyFormat("MYIF (a) {\n" 1384 " g();\n" 1385 "} else {\n" 1386 " g();\n" 1387 "}", 1388 AllowsMergedIf); 1389 verifyFormat("MYIF (a) g();\n" 1390 "else MYIF (b) g();\n" 1391 "else g();", 1392 AllowsMergedIf); 1393 verifyFormat("MYIF (a) g();\n" 1394 "else if (b) g();\n" 1395 "else g();", 1396 AllowsMergedIf); 1397 verifyFormat("MYIF (a) {\n" 1398 " g();\n" 1399 "} else MYIF (b) g();\n" 1400 "else g();", 1401 AllowsMergedIf); 1402 verifyFormat("MYIF (a) {\n" 1403 " g();\n" 1404 "} else if (b) g();\n" 1405 "else g();", 1406 AllowsMergedIf); 1407 verifyFormat("MYIF (a) g();\n" 1408 "else MYIF (b) {\n" 1409 " g();\n" 1410 "} else g();", 1411 AllowsMergedIf); 1412 verifyFormat("MYIF (a) g();\n" 1413 "else if (b) {\n" 1414 " g();\n" 1415 "} else g();", 1416 AllowsMergedIf); 1417 verifyFormat("MYIF (a) g();\n" 1418 "else MYIF (b) g();\n" 1419 "else {\n" 1420 " g();\n" 1421 "}", 1422 AllowsMergedIf); 1423 verifyFormat("MYIF (a) g();\n" 1424 "else if (b) g();\n" 1425 "else {\n" 1426 " g();\n" 1427 "}", 1428 AllowsMergedIf); 1429 verifyFormat("MYIF (a) g();\n" 1430 "else MYIF (b) {\n" 1431 " g();\n" 1432 "} else {\n" 1433 " g();\n" 1434 "}", 1435 AllowsMergedIf); 1436 verifyFormat("MYIF (a) g();\n" 1437 "else if (b) {\n" 1438 " g();\n" 1439 "} else {\n" 1440 " g();\n" 1441 "}", 1442 AllowsMergedIf); 1443 verifyFormat("MYIF (a) {\n" 1444 " g();\n" 1445 "} else MYIF (b) {\n" 1446 " g();\n" 1447 "} else {\n" 1448 " g();\n" 1449 "}", 1450 AllowsMergedIf); 1451 verifyFormat("MYIF (a) {\n" 1452 " g();\n" 1453 "} else if (b) {\n" 1454 " g();\n" 1455 "} else {\n" 1456 " g();\n" 1457 "}", 1458 AllowsMergedIf); 1459 } 1460 1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1462 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1463 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1464 verifyFormat("while (true) continue;", AllowsMergedLoops); 1465 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1466 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1467 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops); 1468 verifyFormat("while (true)\n" 1469 " ;", 1470 AllowsMergedLoops); 1471 verifyFormat("for (;;)\n" 1472 " ;", 1473 AllowsMergedLoops); 1474 verifyFormat("for (;;)\n" 1475 " for (;;) continue;", 1476 AllowsMergedLoops); 1477 verifyFormat("for (;;)\n" 1478 " while (true) continue;", 1479 AllowsMergedLoops); 1480 verifyFormat("while (true)\n" 1481 " for (;;) continue;", 1482 AllowsMergedLoops); 1483 verifyFormat("BOOST_FOREACH (int &v, vec)\n" 1484 " for (;;) continue;", 1485 AllowsMergedLoops); 1486 verifyFormat("for (;;)\n" 1487 " BOOST_FOREACH (int &v, vec) continue;", 1488 AllowsMergedLoops); 1489 verifyFormat("for (;;) // Can't merge this\n" 1490 " continue;", 1491 AllowsMergedLoops); 1492 verifyFormat("for (;;) /* still don't merge */\n" 1493 " continue;", 1494 AllowsMergedLoops); 1495 verifyFormat("do a++;\n" 1496 "while (true);", 1497 AllowsMergedLoops); 1498 verifyFormat("do /* Don't merge */\n" 1499 " a++;\n" 1500 "while (true);", 1501 AllowsMergedLoops); 1502 verifyFormat("do // Don't merge\n" 1503 " a++;\n" 1504 "while (true);", 1505 AllowsMergedLoops); 1506 verifyFormat("do\n" 1507 " // Don't merge\n" 1508 " a++;\n" 1509 "while (true);", 1510 AllowsMergedLoops); 1511 // Without braces labels are interpreted differently. 1512 verifyFormat("{\n" 1513 " do\n" 1514 " label:\n" 1515 " a++;\n" 1516 " while (true);\n" 1517 "}", 1518 AllowsMergedLoops); 1519 } 1520 1521 TEST_F(FormatTest, FormatShortBracedStatements) { 1522 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1523 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1524 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1525 // Not IF to avoid any confusion that IF is somehow special. 1526 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1527 AllowSimpleBracedStatements.ColumnLimit = 40; 1528 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1529 FormatStyle::SBS_Always; 1530 1531 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1532 FormatStyle::SIS_WithoutElse; 1533 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1534 1535 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1536 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1537 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1538 1539 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1540 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1541 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1542 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1543 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1544 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1545 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1546 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1547 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1548 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1549 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1550 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1551 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1552 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1553 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1554 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1555 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1556 AllowSimpleBracedStatements); 1557 verifyFormat("if (true) {\n" 1558 " ffffffffffffffffffffffff();\n" 1559 "}", 1560 AllowSimpleBracedStatements); 1561 verifyFormat("if (true) {\n" 1562 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1563 "}", 1564 AllowSimpleBracedStatements); 1565 verifyFormat("if (true) { //\n" 1566 " f();\n" 1567 "}", 1568 AllowSimpleBracedStatements); 1569 verifyFormat("if (true) {\n" 1570 " f();\n" 1571 " f();\n" 1572 "}", 1573 AllowSimpleBracedStatements); 1574 verifyFormat("if (true) {\n" 1575 " f();\n" 1576 "} else {\n" 1577 " f();\n" 1578 "}", 1579 AllowSimpleBracedStatements); 1580 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1581 AllowSimpleBracedStatements); 1582 verifyFormat("MYIF (true) {\n" 1583 " ffffffffffffffffffffffff();\n" 1584 "}", 1585 AllowSimpleBracedStatements); 1586 verifyFormat("MYIF (true) {\n" 1587 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1588 "}", 1589 AllowSimpleBracedStatements); 1590 verifyFormat("MYIF (true) { //\n" 1591 " f();\n" 1592 "}", 1593 AllowSimpleBracedStatements); 1594 verifyFormat("MYIF (true) {\n" 1595 " f();\n" 1596 " f();\n" 1597 "}", 1598 AllowSimpleBracedStatements); 1599 verifyFormat("MYIF (true) {\n" 1600 " f();\n" 1601 "} else {\n" 1602 " f();\n" 1603 "}", 1604 AllowSimpleBracedStatements); 1605 1606 verifyFormat("struct A2 {\n" 1607 " int X;\n" 1608 "};", 1609 AllowSimpleBracedStatements); 1610 verifyFormat("typedef struct A2 {\n" 1611 " int X;\n" 1612 "} A2_t;", 1613 AllowSimpleBracedStatements); 1614 verifyFormat("template <int> struct A2 {\n" 1615 " struct B {};\n" 1616 "};", 1617 AllowSimpleBracedStatements); 1618 1619 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1620 FormatStyle::SIS_Never; 1621 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1622 verifyFormat("if (true) {\n" 1623 " f();\n" 1624 "}", 1625 AllowSimpleBracedStatements); 1626 verifyFormat("if (true) {\n" 1627 " f();\n" 1628 "} else {\n" 1629 " f();\n" 1630 "}", 1631 AllowSimpleBracedStatements); 1632 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1633 verifyFormat("MYIF (true) {\n" 1634 " f();\n" 1635 "}", 1636 AllowSimpleBracedStatements); 1637 verifyFormat("MYIF (true) {\n" 1638 " f();\n" 1639 "} else {\n" 1640 " f();\n" 1641 "}", 1642 AllowSimpleBracedStatements); 1643 1644 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1645 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1646 verifyFormat("while (true) {\n" 1647 " f();\n" 1648 "}", 1649 AllowSimpleBracedStatements); 1650 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1651 verifyFormat("for (;;) {\n" 1652 " f();\n" 1653 "}", 1654 AllowSimpleBracedStatements); 1655 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1656 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1657 " f();\n" 1658 "}", 1659 AllowSimpleBracedStatements); 1660 1661 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1662 FormatStyle::SIS_WithoutElse; 1663 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1664 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1665 FormatStyle::BWACS_Always; 1666 1667 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1668 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1669 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1670 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1671 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1672 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1673 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1674 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1675 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1676 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1677 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1678 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1679 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1680 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1681 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1682 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1683 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1684 AllowSimpleBracedStatements); 1685 verifyFormat("if (true)\n" 1686 "{\n" 1687 " ffffffffffffffffffffffff();\n" 1688 "}", 1689 AllowSimpleBracedStatements); 1690 verifyFormat("if (true)\n" 1691 "{\n" 1692 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1693 "}", 1694 AllowSimpleBracedStatements); 1695 verifyFormat("if (true)\n" 1696 "{ //\n" 1697 " f();\n" 1698 "}", 1699 AllowSimpleBracedStatements); 1700 verifyFormat("if (true)\n" 1701 "{\n" 1702 " f();\n" 1703 " f();\n" 1704 "}", 1705 AllowSimpleBracedStatements); 1706 verifyFormat("if (true)\n" 1707 "{\n" 1708 " f();\n" 1709 "} else\n" 1710 "{\n" 1711 " f();\n" 1712 "}", 1713 AllowSimpleBracedStatements); 1714 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1715 AllowSimpleBracedStatements); 1716 verifyFormat("MYIF (true)\n" 1717 "{\n" 1718 " ffffffffffffffffffffffff();\n" 1719 "}", 1720 AllowSimpleBracedStatements); 1721 verifyFormat("MYIF (true)\n" 1722 "{\n" 1723 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1724 "}", 1725 AllowSimpleBracedStatements); 1726 verifyFormat("MYIF (true)\n" 1727 "{ //\n" 1728 " f();\n" 1729 "}", 1730 AllowSimpleBracedStatements); 1731 verifyFormat("MYIF (true)\n" 1732 "{\n" 1733 " f();\n" 1734 " f();\n" 1735 "}", 1736 AllowSimpleBracedStatements); 1737 verifyFormat("MYIF (true)\n" 1738 "{\n" 1739 " f();\n" 1740 "} else\n" 1741 "{\n" 1742 " f();\n" 1743 "}", 1744 AllowSimpleBracedStatements); 1745 1746 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1747 FormatStyle::SIS_Never; 1748 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1749 verifyFormat("if (true)\n" 1750 "{\n" 1751 " f();\n" 1752 "}", 1753 AllowSimpleBracedStatements); 1754 verifyFormat("if (true)\n" 1755 "{\n" 1756 " f();\n" 1757 "} else\n" 1758 "{\n" 1759 " f();\n" 1760 "}", 1761 AllowSimpleBracedStatements); 1762 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1763 verifyFormat("MYIF (true)\n" 1764 "{\n" 1765 " f();\n" 1766 "}", 1767 AllowSimpleBracedStatements); 1768 verifyFormat("MYIF (true)\n" 1769 "{\n" 1770 " f();\n" 1771 "} else\n" 1772 "{\n" 1773 " f();\n" 1774 "}", 1775 AllowSimpleBracedStatements); 1776 1777 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1778 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1779 verifyFormat("while (true)\n" 1780 "{\n" 1781 " f();\n" 1782 "}", 1783 AllowSimpleBracedStatements); 1784 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1785 verifyFormat("for (;;)\n" 1786 "{\n" 1787 " f();\n" 1788 "}", 1789 AllowSimpleBracedStatements); 1790 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1791 verifyFormat("BOOST_FOREACH (int v, vec)\n" 1792 "{\n" 1793 " f();\n" 1794 "}", 1795 AllowSimpleBracedStatements); 1796 } 1797 1798 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1799 FormatStyle Style = getLLVMStyleWithColumns(60); 1800 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1801 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1802 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1803 EXPECT_EQ("#define A \\\n" 1804 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1805 " { \\\n" 1806 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1807 " }\n" 1808 "X;", 1809 format("#define A \\\n" 1810 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1811 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1812 " }\n" 1813 "X;", 1814 Style)); 1815 } 1816 1817 TEST_F(FormatTest, ParseIfElse) { 1818 verifyFormat("if (true)\n" 1819 " if (true)\n" 1820 " if (true)\n" 1821 " f();\n" 1822 " else\n" 1823 " g();\n" 1824 " else\n" 1825 " h();\n" 1826 "else\n" 1827 " i();"); 1828 verifyFormat("if (true)\n" 1829 " if (true)\n" 1830 " if (true) {\n" 1831 " if (true)\n" 1832 " f();\n" 1833 " } else {\n" 1834 " g();\n" 1835 " }\n" 1836 " else\n" 1837 " h();\n" 1838 "else {\n" 1839 " i();\n" 1840 "}"); 1841 verifyFormat("if (true)\n" 1842 " if constexpr (true)\n" 1843 " if (true) {\n" 1844 " if constexpr (true)\n" 1845 " f();\n" 1846 " } else {\n" 1847 " g();\n" 1848 " }\n" 1849 " else\n" 1850 " h();\n" 1851 "else {\n" 1852 " i();\n" 1853 "}"); 1854 verifyFormat("if (true)\n" 1855 " if CONSTEXPR (true)\n" 1856 " if (true) {\n" 1857 " if CONSTEXPR (true)\n" 1858 " f();\n" 1859 " } else {\n" 1860 " g();\n" 1861 " }\n" 1862 " else\n" 1863 " h();\n" 1864 "else {\n" 1865 " i();\n" 1866 "}"); 1867 verifyFormat("void f() {\n" 1868 " if (a) {\n" 1869 " } else {\n" 1870 " }\n" 1871 "}"); 1872 } 1873 1874 TEST_F(FormatTest, ElseIf) { 1875 verifyFormat("if (a) {\n} else if (b) {\n}"); 1876 verifyFormat("if (a)\n" 1877 " f();\n" 1878 "else if (b)\n" 1879 " g();\n" 1880 "else\n" 1881 " h();"); 1882 verifyFormat("if (a)\n" 1883 " f();\n" 1884 "else // comment\n" 1885 " if (b) {\n" 1886 " g();\n" 1887 " h();\n" 1888 " }"); 1889 verifyFormat("if constexpr (a)\n" 1890 " f();\n" 1891 "else if constexpr (b)\n" 1892 " g();\n" 1893 "else\n" 1894 " h();"); 1895 verifyFormat("if CONSTEXPR (a)\n" 1896 " f();\n" 1897 "else if CONSTEXPR (b)\n" 1898 " g();\n" 1899 "else\n" 1900 " h();"); 1901 verifyFormat("if (a) {\n" 1902 " f();\n" 1903 "}\n" 1904 "// or else ..\n" 1905 "else {\n" 1906 " g()\n" 1907 "}"); 1908 1909 verifyFormat("if (a) {\n" 1910 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1911 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1912 "}"); 1913 verifyFormat("if (a) {\n" 1914 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1916 "}"); 1917 verifyFormat("if (a) {\n" 1918 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1919 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1920 "}"); 1921 verifyFormat("if (a) {\n" 1922 "} else if (\n" 1923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1924 "}", 1925 getLLVMStyleWithColumns(62)); 1926 verifyFormat("if (a) {\n" 1927 "} else if constexpr (\n" 1928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1929 "}", 1930 getLLVMStyleWithColumns(62)); 1931 verifyFormat("if (a) {\n" 1932 "} else if CONSTEXPR (\n" 1933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1934 "}", 1935 getLLVMStyleWithColumns(62)); 1936 } 1937 1938 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 1939 FormatStyle Style = getLLVMStyle(); 1940 // Check first the default LLVM style 1941 // Style.PointerAlignment = FormatStyle::PAS_Right; 1942 // Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1943 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 1944 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 1945 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 1946 verifyFormat("int *f1(int &a) const &;", Style); 1947 verifyFormat("int *f1(int &a) const & = 0;", Style); 1948 verifyFormat("int *a = f1();", Style); 1949 verifyFormat("int &b = f2();", Style); 1950 verifyFormat("int &&c = f3();", Style); 1951 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 1952 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); 1953 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 1954 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 1955 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 1956 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); 1957 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 1958 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); 1959 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); 1960 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); 1961 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); 1962 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); 1963 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); 1964 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); 1965 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); 1966 verifyFormat("for (f(); int &c : {1, 2, 3})", Style); 1967 1968 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1969 verifyFormat("Const unsigned int *c;\n" 1970 "const unsigned int *d;\n" 1971 "Const unsigned int &e;\n" 1972 "const unsigned int &f;\n" 1973 "const unsigned &&g;\n" 1974 "Const unsigned h;", 1975 Style); 1976 1977 Style.PointerAlignment = FormatStyle::PAS_Left; 1978 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1979 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 1980 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 1981 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 1982 verifyFormat("int* f1(int& a) const& = 0;", Style); 1983 verifyFormat("int* a = f1();", Style); 1984 verifyFormat("int& b = f2();", Style); 1985 verifyFormat("int&& c = f3();", Style); 1986 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 1987 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); 1988 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 1989 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 1990 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 1991 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); 1992 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 1993 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 1994 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style); 1995 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style); 1996 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style); 1997 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 1998 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); 1999 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); 2000 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); 2001 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); 2002 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); 2003 verifyFormat("for (f(); int& c : {1, 2, 3})", Style); 2004 2005 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2006 verifyFormat("Const unsigned int* c;\n" 2007 "const unsigned int* d;\n" 2008 "Const unsigned int& e;\n" 2009 "const unsigned int& f;\n" 2010 "const unsigned&& g;\n" 2011 "Const unsigned h;", 2012 Style); 2013 2014 Style.PointerAlignment = FormatStyle::PAS_Right; 2015 Style.ReferenceAlignment = FormatStyle::RAS_Left; 2016 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 2017 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 2018 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 2019 verifyFormat("int *a = f1();", Style); 2020 verifyFormat("int& b = f2();", Style); 2021 verifyFormat("int&& c = f3();", Style); 2022 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2023 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2024 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); 2025 2026 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2027 verifyFormat("Const unsigned int *c;\n" 2028 "const unsigned int *d;\n" 2029 "Const unsigned int& e;\n" 2030 "const unsigned int& f;\n" 2031 "const unsigned g;\n" 2032 "Const unsigned h;", 2033 Style); 2034 2035 Style.PointerAlignment = FormatStyle::PAS_Left; 2036 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 2037 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 2038 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 2039 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 2040 verifyFormat("int* a = f1();", Style); 2041 verifyFormat("int & b = f2();", Style); 2042 verifyFormat("int && c = f3();", Style); 2043 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); 2044 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); 2045 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); 2046 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2047 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); 2048 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); 2049 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); 2050 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2051 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); 2052 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); 2053 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); 2054 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); 2055 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); 2056 verifyFormat("for (f(); int & c : {1, 2, 3})", Style); 2057 2058 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2059 verifyFormat("Const unsigned int* c;\n" 2060 "const unsigned int* d;\n" 2061 "Const unsigned int & e;\n" 2062 "const unsigned int & f;\n" 2063 "const unsigned && g;\n" 2064 "Const unsigned h;", 2065 Style); 2066 2067 Style.PointerAlignment = FormatStyle::PAS_Middle; 2068 Style.ReferenceAlignment = FormatStyle::RAS_Right; 2069 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 2070 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 2071 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 2072 verifyFormat("int * a = f1();", Style); 2073 verifyFormat("int &b = f2();", Style); 2074 verifyFormat("int &&c = f3();", Style); 2075 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2076 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2077 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); 2078 2079 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2080 // specifically handled 2081 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2082 } 2083 2084 TEST_F(FormatTest, FormatsForLoop) { 2085 verifyFormat( 2086 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2087 " ++VeryVeryLongLoopVariable)\n" 2088 " ;"); 2089 verifyFormat("for (;;)\n" 2090 " f();"); 2091 verifyFormat("for (;;) {\n}"); 2092 verifyFormat("for (;;) {\n" 2093 " f();\n" 2094 "}"); 2095 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2096 2097 verifyFormat( 2098 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2099 " E = UnwrappedLines.end();\n" 2100 " I != E; ++I) {\n}"); 2101 2102 verifyFormat( 2103 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2104 " ++IIIII) {\n}"); 2105 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2106 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2107 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2108 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2109 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2110 " E = FD->getDeclsInPrototypeScope().end();\n" 2111 " I != E; ++I) {\n}"); 2112 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2113 " I = Container.begin(),\n" 2114 " E = Container.end();\n" 2115 " I != E; ++I) {\n}", 2116 getLLVMStyleWithColumns(76)); 2117 2118 verifyFormat( 2119 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2123 " ++aaaaaaaaaaa) {\n}"); 2124 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2125 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2126 " ++i) {\n}"); 2127 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2128 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2129 "}"); 2130 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2131 " aaaaaaaaaa);\n" 2132 " iter; ++iter) {\n" 2133 "}"); 2134 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2135 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2136 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2137 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2138 2139 // These should not be formatted as Objective-C for-in loops. 2140 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2141 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2142 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2143 verifyFormat( 2144 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2145 2146 FormatStyle NoBinPacking = getLLVMStyle(); 2147 NoBinPacking.BinPackParameters = false; 2148 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2149 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2150 " aaaaaaaaaaaaaaaa,\n" 2151 " aaaaaaaaaaaaaaaa,\n" 2152 " aaaaaaaaaaaaaaaa);\n" 2153 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2154 "}", 2155 NoBinPacking); 2156 verifyFormat( 2157 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2158 " E = UnwrappedLines.end();\n" 2159 " I != E;\n" 2160 " ++I) {\n}", 2161 NoBinPacking); 2162 2163 FormatStyle AlignLeft = getLLVMStyle(); 2164 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2165 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2166 } 2167 2168 TEST_F(FormatTest, RangeBasedForLoops) { 2169 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2171 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2172 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2173 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2175 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2176 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2177 } 2178 2179 TEST_F(FormatTest, ForEachLoops) { 2180 FormatStyle Style = getLLVMStyle(); 2181 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2182 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false); 2183 verifyFormat("void f() {\n" 2184 " for (;;) {\n" 2185 " }\n" 2186 " foreach (Item *item, itemlist) {\n" 2187 " }\n" 2188 " Q_FOREACH (Item *item, itemlist) {\n" 2189 " }\n" 2190 " BOOST_FOREACH (Item *item, itemlist) {\n" 2191 " }\n" 2192 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2193 "}", 2194 Style); 2195 verifyFormat("void f() {\n" 2196 " for (;;)\n" 2197 " int j = 1;\n" 2198 " Q_FOREACH (int v, vec)\n" 2199 " v *= 2;\n" 2200 " for (;;) {\n" 2201 " int j = 1;\n" 2202 " }\n" 2203 " Q_FOREACH (int v, vec) {\n" 2204 " v *= 2;\n" 2205 " }\n" 2206 "}", 2207 Style); 2208 2209 FormatStyle ShortBlocks = getLLVMStyle(); 2210 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2211 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false); 2212 verifyFormat("void f() {\n" 2213 " for (;;)\n" 2214 " int j = 1;\n" 2215 " Q_FOREACH (int &v, vec)\n" 2216 " v *= 2;\n" 2217 " for (;;) {\n" 2218 " int j = 1;\n" 2219 " }\n" 2220 " Q_FOREACH (int &v, vec) {\n" 2221 " int j = 1;\n" 2222 " }\n" 2223 "}", 2224 ShortBlocks); 2225 2226 FormatStyle ShortLoops = getLLVMStyle(); 2227 ShortLoops.AllowShortLoopsOnASingleLine = true; 2228 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2229 verifyFormat("void f() {\n" 2230 " for (;;) int j = 1;\n" 2231 " Q_FOREACH (int &v, vec) int j = 1;\n" 2232 " for (;;) {\n" 2233 " int j = 1;\n" 2234 " }\n" 2235 " Q_FOREACH (int &v, vec) {\n" 2236 " int j = 1;\n" 2237 " }\n" 2238 "}", 2239 ShortLoops); 2240 2241 FormatStyle ShortBlocksAndLoops = getLLVMStyle(); 2242 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2243 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true; 2244 verifyFormat("void f() {\n" 2245 " for (;;) int j = 1;\n" 2246 " Q_FOREACH (int &v, vec) int j = 1;\n" 2247 " for (;;) { int j = 1; }\n" 2248 " Q_FOREACH (int &v, vec) { int j = 1; }\n" 2249 "}", 2250 ShortBlocksAndLoops); 2251 2252 Style.SpaceBeforeParens = 2253 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2254 verifyFormat("void f() {\n" 2255 " for (;;) {\n" 2256 " }\n" 2257 " foreach(Item *item, itemlist) {\n" 2258 " }\n" 2259 " Q_FOREACH(Item *item, itemlist) {\n" 2260 " }\n" 2261 " BOOST_FOREACH(Item *item, itemlist) {\n" 2262 " }\n" 2263 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2264 "}", 2265 Style); 2266 2267 // As function-like macros. 2268 verifyFormat("#define foreach(x, y)\n" 2269 "#define Q_FOREACH(x, y)\n" 2270 "#define BOOST_FOREACH(x, y)\n" 2271 "#define UNKNOWN_FOREACH(x, y)\n"); 2272 2273 // Not as function-like macros. 2274 verifyFormat("#define foreach (x, y)\n" 2275 "#define Q_FOREACH (x, y)\n" 2276 "#define BOOST_FOREACH (x, y)\n" 2277 "#define UNKNOWN_FOREACH (x, y)\n"); 2278 2279 // handle microsoft non standard extension 2280 verifyFormat("for each (char c in x->MyStringProperty)"); 2281 } 2282 2283 TEST_F(FormatTest, FormatsWhileLoop) { 2284 verifyFormat("while (true) {\n}"); 2285 verifyFormat("while (true)\n" 2286 " f();"); 2287 verifyFormat("while () {\n}"); 2288 verifyFormat("while () {\n" 2289 " f();\n" 2290 "}"); 2291 } 2292 2293 TEST_F(FormatTest, FormatsDoWhile) { 2294 verifyFormat("do {\n" 2295 " do_something();\n" 2296 "} while (something());"); 2297 verifyFormat("do\n" 2298 " do_something();\n" 2299 "while (something());"); 2300 } 2301 2302 TEST_F(FormatTest, FormatsSwitchStatement) { 2303 verifyFormat("switch (x) {\n" 2304 "case 1:\n" 2305 " f();\n" 2306 " break;\n" 2307 "case kFoo:\n" 2308 "case ns::kBar:\n" 2309 "case kBaz:\n" 2310 " break;\n" 2311 "default:\n" 2312 " g();\n" 2313 " break;\n" 2314 "}"); 2315 verifyFormat("switch (x) {\n" 2316 "case 1: {\n" 2317 " f();\n" 2318 " break;\n" 2319 "}\n" 2320 "case 2: {\n" 2321 " break;\n" 2322 "}\n" 2323 "}"); 2324 verifyFormat("switch (x) {\n" 2325 "case 1: {\n" 2326 " f();\n" 2327 " {\n" 2328 " g();\n" 2329 " h();\n" 2330 " }\n" 2331 " break;\n" 2332 "}\n" 2333 "}"); 2334 verifyFormat("switch (x) {\n" 2335 "case 1: {\n" 2336 " f();\n" 2337 " if (foo) {\n" 2338 " g();\n" 2339 " h();\n" 2340 " }\n" 2341 " break;\n" 2342 "}\n" 2343 "}"); 2344 verifyFormat("switch (x) {\n" 2345 "case 1: {\n" 2346 " f();\n" 2347 " g();\n" 2348 "} break;\n" 2349 "}"); 2350 verifyFormat("switch (test)\n" 2351 " ;"); 2352 verifyFormat("switch (x) {\n" 2353 "default: {\n" 2354 " // Do nothing.\n" 2355 "}\n" 2356 "}"); 2357 verifyFormat("switch (x) {\n" 2358 "// comment\n" 2359 "// if 1, do f()\n" 2360 "case 1:\n" 2361 " f();\n" 2362 "}"); 2363 verifyFormat("switch (x) {\n" 2364 "case 1:\n" 2365 " // Do amazing stuff\n" 2366 " {\n" 2367 " f();\n" 2368 " g();\n" 2369 " }\n" 2370 " break;\n" 2371 "}"); 2372 verifyFormat("#define A \\\n" 2373 " switch (x) { \\\n" 2374 " case a: \\\n" 2375 " foo = b; \\\n" 2376 " }", 2377 getLLVMStyleWithColumns(20)); 2378 verifyFormat("#define OPERATION_CASE(name) \\\n" 2379 " case OP_name: \\\n" 2380 " return operations::Operation##name\n", 2381 getLLVMStyleWithColumns(40)); 2382 verifyFormat("switch (x) {\n" 2383 "case 1:;\n" 2384 "default:;\n" 2385 " int i;\n" 2386 "}"); 2387 2388 verifyGoogleFormat("switch (x) {\n" 2389 " case 1:\n" 2390 " f();\n" 2391 " break;\n" 2392 " case kFoo:\n" 2393 " case ns::kBar:\n" 2394 " case kBaz:\n" 2395 " break;\n" 2396 " default:\n" 2397 " g();\n" 2398 " break;\n" 2399 "}"); 2400 verifyGoogleFormat("switch (x) {\n" 2401 " case 1: {\n" 2402 " f();\n" 2403 " break;\n" 2404 " }\n" 2405 "}"); 2406 verifyGoogleFormat("switch (test)\n" 2407 " ;"); 2408 2409 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2410 " case OP_name: \\\n" 2411 " return operations::Operation##name\n"); 2412 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2413 " // Get the correction operation class.\n" 2414 " switch (OpCode) {\n" 2415 " CASE(Add);\n" 2416 " CASE(Subtract);\n" 2417 " default:\n" 2418 " return operations::Unknown;\n" 2419 " }\n" 2420 "#undef OPERATION_CASE\n" 2421 "}"); 2422 verifyFormat("DEBUG({\n" 2423 " switch (x) {\n" 2424 " case A:\n" 2425 " f();\n" 2426 " break;\n" 2427 " // fallthrough\n" 2428 " case B:\n" 2429 " g();\n" 2430 " break;\n" 2431 " }\n" 2432 "});"); 2433 EXPECT_EQ("DEBUG({\n" 2434 " switch (x) {\n" 2435 " case A:\n" 2436 " f();\n" 2437 " break;\n" 2438 " // On B:\n" 2439 " case B:\n" 2440 " g();\n" 2441 " break;\n" 2442 " }\n" 2443 "});", 2444 format("DEBUG({\n" 2445 " switch (x) {\n" 2446 " case A:\n" 2447 " f();\n" 2448 " break;\n" 2449 " // On B:\n" 2450 " case B:\n" 2451 " g();\n" 2452 " break;\n" 2453 " }\n" 2454 "});", 2455 getLLVMStyle())); 2456 EXPECT_EQ("switch (n) {\n" 2457 "case 0: {\n" 2458 " return false;\n" 2459 "}\n" 2460 "default: {\n" 2461 " return true;\n" 2462 "}\n" 2463 "}", 2464 format("switch (n)\n" 2465 "{\n" 2466 "case 0: {\n" 2467 " return false;\n" 2468 "}\n" 2469 "default: {\n" 2470 " return true;\n" 2471 "}\n" 2472 "}", 2473 getLLVMStyle())); 2474 verifyFormat("switch (a) {\n" 2475 "case (b):\n" 2476 " return;\n" 2477 "}"); 2478 2479 verifyFormat("switch (a) {\n" 2480 "case some_namespace::\n" 2481 " some_constant:\n" 2482 " return;\n" 2483 "}", 2484 getLLVMStyleWithColumns(34)); 2485 2486 FormatStyle Style = getLLVMStyle(); 2487 Style.IndentCaseLabels = true; 2488 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2489 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2490 Style.BraceWrapping.AfterCaseLabel = true; 2491 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2492 EXPECT_EQ("switch (n)\n" 2493 "{\n" 2494 " case 0:\n" 2495 " {\n" 2496 " return false;\n" 2497 " }\n" 2498 " default:\n" 2499 " {\n" 2500 " return true;\n" 2501 " }\n" 2502 "}", 2503 format("switch (n) {\n" 2504 " case 0: {\n" 2505 " return false;\n" 2506 " }\n" 2507 " default: {\n" 2508 " return true;\n" 2509 " }\n" 2510 "}", 2511 Style)); 2512 Style.BraceWrapping.AfterCaseLabel = false; 2513 EXPECT_EQ("switch (n)\n" 2514 "{\n" 2515 " case 0: {\n" 2516 " return false;\n" 2517 " }\n" 2518 " default: {\n" 2519 " return true;\n" 2520 " }\n" 2521 "}", 2522 format("switch (n) {\n" 2523 " case 0:\n" 2524 " {\n" 2525 " return false;\n" 2526 " }\n" 2527 " default:\n" 2528 " {\n" 2529 " return true;\n" 2530 " }\n" 2531 "}", 2532 Style)); 2533 Style.IndentCaseLabels = false; 2534 Style.IndentCaseBlocks = true; 2535 EXPECT_EQ("switch (n)\n" 2536 "{\n" 2537 "case 0:\n" 2538 " {\n" 2539 " return false;\n" 2540 " }\n" 2541 "case 1:\n" 2542 " break;\n" 2543 "default:\n" 2544 " {\n" 2545 " return true;\n" 2546 " }\n" 2547 "}", 2548 format("switch (n) {\n" 2549 "case 0: {\n" 2550 " return false;\n" 2551 "}\n" 2552 "case 1:\n" 2553 " break;\n" 2554 "default: {\n" 2555 " return true;\n" 2556 "}\n" 2557 "}", 2558 Style)); 2559 Style.IndentCaseLabels = true; 2560 Style.IndentCaseBlocks = true; 2561 EXPECT_EQ("switch (n)\n" 2562 "{\n" 2563 " case 0:\n" 2564 " {\n" 2565 " return false;\n" 2566 " }\n" 2567 " case 1:\n" 2568 " break;\n" 2569 " default:\n" 2570 " {\n" 2571 " return true;\n" 2572 " }\n" 2573 "}", 2574 format("switch (n) {\n" 2575 "case 0: {\n" 2576 " return false;\n" 2577 "}\n" 2578 "case 1:\n" 2579 " break;\n" 2580 "default: {\n" 2581 " return true;\n" 2582 "}\n" 2583 "}", 2584 Style)); 2585 } 2586 2587 TEST_F(FormatTest, CaseRanges) { 2588 verifyFormat("switch (x) {\n" 2589 "case 'A' ... 'Z':\n" 2590 "case 1 ... 5:\n" 2591 "case a ... b:\n" 2592 " break;\n" 2593 "}"); 2594 } 2595 2596 TEST_F(FormatTest, ShortEnums) { 2597 FormatStyle Style = getLLVMStyle(); 2598 Style.AllowShortEnumsOnASingleLine = true; 2599 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2600 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2601 Style.AllowShortEnumsOnASingleLine = false; 2602 verifyFormat("enum {\n" 2603 " A,\n" 2604 " B,\n" 2605 " C\n" 2606 "} ShortEnum1, ShortEnum2;", 2607 Style); 2608 verifyFormat("typedef enum {\n" 2609 " A,\n" 2610 " B,\n" 2611 " C\n" 2612 "} ShortEnum1, ShortEnum2;", 2613 Style); 2614 verifyFormat("enum {\n" 2615 " A,\n" 2616 "} ShortEnum1, ShortEnum2;", 2617 Style); 2618 verifyFormat("typedef enum {\n" 2619 " A,\n" 2620 "} ShortEnum1, ShortEnum2;", 2621 Style); 2622 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2623 Style.BraceWrapping.AfterEnum = true; 2624 verifyFormat("enum\n" 2625 "{\n" 2626 " A,\n" 2627 " B,\n" 2628 " C\n" 2629 "} ShortEnum1, ShortEnum2;", 2630 Style); 2631 verifyFormat("typedef enum\n" 2632 "{\n" 2633 " A,\n" 2634 " B,\n" 2635 " C\n" 2636 "} ShortEnum1, ShortEnum2;", 2637 Style); 2638 } 2639 2640 TEST_F(FormatTest, ShortCaseLabels) { 2641 FormatStyle Style = getLLVMStyle(); 2642 Style.AllowShortCaseLabelsOnASingleLine = true; 2643 verifyFormat("switch (a) {\n" 2644 "case 1: x = 1; break;\n" 2645 "case 2: return;\n" 2646 "case 3:\n" 2647 "case 4:\n" 2648 "case 5: return;\n" 2649 "case 6: // comment\n" 2650 " return;\n" 2651 "case 7:\n" 2652 " // comment\n" 2653 " return;\n" 2654 "case 8:\n" 2655 " x = 8; // comment\n" 2656 " break;\n" 2657 "default: y = 1; break;\n" 2658 "}", 2659 Style); 2660 verifyFormat("switch (a) {\n" 2661 "case 0: return; // comment\n" 2662 "case 1: break; // comment\n" 2663 "case 2: return;\n" 2664 "// comment\n" 2665 "case 3: return;\n" 2666 "// comment 1\n" 2667 "// comment 2\n" 2668 "// comment 3\n" 2669 "case 4: break; /* comment */\n" 2670 "case 5:\n" 2671 " // comment\n" 2672 " break;\n" 2673 "case 6: /* comment */ x = 1; break;\n" 2674 "case 7: x = /* comment */ 1; break;\n" 2675 "case 8:\n" 2676 " x = 1; /* comment */\n" 2677 " break;\n" 2678 "case 9:\n" 2679 " break; // comment line 1\n" 2680 " // comment line 2\n" 2681 "}", 2682 Style); 2683 EXPECT_EQ("switch (a) {\n" 2684 "case 1:\n" 2685 " x = 8;\n" 2686 " // fall through\n" 2687 "case 2: x = 8;\n" 2688 "// comment\n" 2689 "case 3:\n" 2690 " return; /* comment line 1\n" 2691 " * comment line 2 */\n" 2692 "case 4: i = 8;\n" 2693 "// something else\n" 2694 "#if FOO\n" 2695 "case 5: break;\n" 2696 "#endif\n" 2697 "}", 2698 format("switch (a) {\n" 2699 "case 1: x = 8;\n" 2700 " // fall through\n" 2701 "case 2:\n" 2702 " x = 8;\n" 2703 "// comment\n" 2704 "case 3:\n" 2705 " return; /* comment line 1\n" 2706 " * comment line 2 */\n" 2707 "case 4:\n" 2708 " i = 8;\n" 2709 "// something else\n" 2710 "#if FOO\n" 2711 "case 5: break;\n" 2712 "#endif\n" 2713 "}", 2714 Style)); 2715 EXPECT_EQ("switch (a) {\n" 2716 "case 0:\n" 2717 " return; // long long long long long long long long long long " 2718 "long long comment\n" 2719 " // line\n" 2720 "}", 2721 format("switch (a) {\n" 2722 "case 0: return; // long long long long long long long long " 2723 "long long long long comment line\n" 2724 "}", 2725 Style)); 2726 EXPECT_EQ("switch (a) {\n" 2727 "case 0:\n" 2728 " return; /* long long long long long long long long long long " 2729 "long long comment\n" 2730 " line */\n" 2731 "}", 2732 format("switch (a) {\n" 2733 "case 0: return; /* long long long long long long long long " 2734 "long long long long comment line */\n" 2735 "}", 2736 Style)); 2737 verifyFormat("switch (a) {\n" 2738 "#if FOO\n" 2739 "case 0: return 0;\n" 2740 "#endif\n" 2741 "}", 2742 Style); 2743 verifyFormat("switch (a) {\n" 2744 "case 1: {\n" 2745 "}\n" 2746 "case 2: {\n" 2747 " return;\n" 2748 "}\n" 2749 "case 3: {\n" 2750 " x = 1;\n" 2751 " return;\n" 2752 "}\n" 2753 "case 4:\n" 2754 " if (x)\n" 2755 " return;\n" 2756 "}", 2757 Style); 2758 Style.ColumnLimit = 21; 2759 verifyFormat("switch (a) {\n" 2760 "case 1: x = 1; break;\n" 2761 "case 2: return;\n" 2762 "case 3:\n" 2763 "case 4:\n" 2764 "case 5: return;\n" 2765 "default:\n" 2766 " y = 1;\n" 2767 " break;\n" 2768 "}", 2769 Style); 2770 Style.ColumnLimit = 80; 2771 Style.AllowShortCaseLabelsOnASingleLine = false; 2772 Style.IndentCaseLabels = true; 2773 EXPECT_EQ("switch (n) {\n" 2774 " default /*comments*/:\n" 2775 " return true;\n" 2776 " case 0:\n" 2777 " return false;\n" 2778 "}", 2779 format("switch (n) {\n" 2780 "default/*comments*/:\n" 2781 " return true;\n" 2782 "case 0:\n" 2783 " return false;\n" 2784 "}", 2785 Style)); 2786 Style.AllowShortCaseLabelsOnASingleLine = true; 2787 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2788 Style.BraceWrapping.AfterCaseLabel = true; 2789 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2790 EXPECT_EQ("switch (n)\n" 2791 "{\n" 2792 " case 0:\n" 2793 " {\n" 2794 " return false;\n" 2795 " }\n" 2796 " default:\n" 2797 " {\n" 2798 " return true;\n" 2799 " }\n" 2800 "}", 2801 format("switch (n) {\n" 2802 " case 0: {\n" 2803 " return false;\n" 2804 " }\n" 2805 " default:\n" 2806 " {\n" 2807 " return true;\n" 2808 " }\n" 2809 "}", 2810 Style)); 2811 } 2812 2813 TEST_F(FormatTest, FormatsLabels) { 2814 verifyFormat("void f() {\n" 2815 " some_code();\n" 2816 "test_label:\n" 2817 " some_other_code();\n" 2818 " {\n" 2819 " some_more_code();\n" 2820 " another_label:\n" 2821 " some_more_code();\n" 2822 " }\n" 2823 "}"); 2824 verifyFormat("{\n" 2825 " some_code();\n" 2826 "test_label:\n" 2827 " some_other_code();\n" 2828 "}"); 2829 verifyFormat("{\n" 2830 " some_code();\n" 2831 "test_label:;\n" 2832 " int i = 0;\n" 2833 "}"); 2834 FormatStyle Style = getLLVMStyle(); 2835 Style.IndentGotoLabels = false; 2836 verifyFormat("void f() {\n" 2837 " some_code();\n" 2838 "test_label:\n" 2839 " some_other_code();\n" 2840 " {\n" 2841 " some_more_code();\n" 2842 "another_label:\n" 2843 " some_more_code();\n" 2844 " }\n" 2845 "}", 2846 Style); 2847 verifyFormat("{\n" 2848 " some_code();\n" 2849 "test_label:\n" 2850 " some_other_code();\n" 2851 "}", 2852 Style); 2853 verifyFormat("{\n" 2854 " some_code();\n" 2855 "test_label:;\n" 2856 " int i = 0;\n" 2857 "}"); 2858 } 2859 2860 TEST_F(FormatTest, MultiLineControlStatements) { 2861 FormatStyle Style = getLLVMStyleWithColumns(20); 2862 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2863 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2864 // Short lines should keep opening brace on same line. 2865 EXPECT_EQ("if (foo) {\n" 2866 " bar();\n" 2867 "}", 2868 format("if(foo){bar();}", Style)); 2869 EXPECT_EQ("if (foo) {\n" 2870 " bar();\n" 2871 "} else {\n" 2872 " baz();\n" 2873 "}", 2874 format("if(foo){bar();}else{baz();}", Style)); 2875 EXPECT_EQ("if (foo && bar) {\n" 2876 " baz();\n" 2877 "}", 2878 format("if(foo&&bar){baz();}", Style)); 2879 EXPECT_EQ("if (foo) {\n" 2880 " bar();\n" 2881 "} else if (baz) {\n" 2882 " quux();\n" 2883 "}", 2884 format("if(foo){bar();}else if(baz){quux();}", Style)); 2885 EXPECT_EQ( 2886 "if (foo) {\n" 2887 " bar();\n" 2888 "} else if (baz) {\n" 2889 " quux();\n" 2890 "} else {\n" 2891 " foobar();\n" 2892 "}", 2893 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2894 EXPECT_EQ("for (;;) {\n" 2895 " foo();\n" 2896 "}", 2897 format("for(;;){foo();}")); 2898 EXPECT_EQ("while (1) {\n" 2899 " foo();\n" 2900 "}", 2901 format("while(1){foo();}", Style)); 2902 EXPECT_EQ("switch (foo) {\n" 2903 "case bar:\n" 2904 " return;\n" 2905 "}", 2906 format("switch(foo){case bar:return;}", Style)); 2907 EXPECT_EQ("try {\n" 2908 " foo();\n" 2909 "} catch (...) {\n" 2910 " bar();\n" 2911 "}", 2912 format("try{foo();}catch(...){bar();}", Style)); 2913 EXPECT_EQ("do {\n" 2914 " foo();\n" 2915 "} while (bar &&\n" 2916 " baz);", 2917 format("do{foo();}while(bar&&baz);", Style)); 2918 // Long lines should put opening brace on new line. 2919 EXPECT_EQ("if (foo && bar &&\n" 2920 " baz)\n" 2921 "{\n" 2922 " quux();\n" 2923 "}", 2924 format("if(foo&&bar&&baz){quux();}", Style)); 2925 EXPECT_EQ("if (foo && bar &&\n" 2926 " baz)\n" 2927 "{\n" 2928 " quux();\n" 2929 "}", 2930 format("if (foo && bar &&\n" 2931 " baz) {\n" 2932 " quux();\n" 2933 "}", 2934 Style)); 2935 EXPECT_EQ("if (foo) {\n" 2936 " bar();\n" 2937 "} else if (baz ||\n" 2938 " quux)\n" 2939 "{\n" 2940 " foobar();\n" 2941 "}", 2942 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2943 EXPECT_EQ( 2944 "if (foo) {\n" 2945 " bar();\n" 2946 "} else if (baz ||\n" 2947 " quux)\n" 2948 "{\n" 2949 " foobar();\n" 2950 "} else {\n" 2951 " barbaz();\n" 2952 "}", 2953 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2954 Style)); 2955 EXPECT_EQ("for (int i = 0;\n" 2956 " i < 10; ++i)\n" 2957 "{\n" 2958 " foo();\n" 2959 "}", 2960 format("for(int i=0;i<10;++i){foo();}", Style)); 2961 EXPECT_EQ("foreach (int i,\n" 2962 " list)\n" 2963 "{\n" 2964 " foo();\n" 2965 "}", 2966 format("foreach(int i, list){foo();}", Style)); 2967 Style.ColumnLimit = 2968 40; // to concentrate at brace wrapping, not line wrap due to column limit 2969 EXPECT_EQ("foreach (int i, list) {\n" 2970 " foo();\n" 2971 "}", 2972 format("foreach(int i, list){foo();}", Style)); 2973 Style.ColumnLimit = 2974 20; // to concentrate at brace wrapping, not line wrap due to column limit 2975 EXPECT_EQ("while (foo || bar ||\n" 2976 " baz)\n" 2977 "{\n" 2978 " quux();\n" 2979 "}", 2980 format("while(foo||bar||baz){quux();}", Style)); 2981 EXPECT_EQ("switch (\n" 2982 " foo = barbaz)\n" 2983 "{\n" 2984 "case quux:\n" 2985 " return;\n" 2986 "}", 2987 format("switch(foo=barbaz){case quux:return;}", Style)); 2988 EXPECT_EQ("try {\n" 2989 " foo();\n" 2990 "} catch (\n" 2991 " Exception &bar)\n" 2992 "{\n" 2993 " baz();\n" 2994 "}", 2995 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2996 Style.ColumnLimit = 2997 40; // to concentrate at brace wrapping, not line wrap due to column limit 2998 EXPECT_EQ("try {\n" 2999 " foo();\n" 3000 "} catch (Exception &bar) {\n" 3001 " baz();\n" 3002 "}", 3003 format("try{foo();}catch(Exception&bar){baz();}", Style)); 3004 Style.ColumnLimit = 3005 20; // to concentrate at brace wrapping, not line wrap due to column limit 3006 3007 Style.BraceWrapping.BeforeElse = true; 3008 EXPECT_EQ( 3009 "if (foo) {\n" 3010 " bar();\n" 3011 "}\n" 3012 "else if (baz ||\n" 3013 " quux)\n" 3014 "{\n" 3015 " foobar();\n" 3016 "}\n" 3017 "else {\n" 3018 " barbaz();\n" 3019 "}", 3020 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3021 Style)); 3022 3023 Style.BraceWrapping.BeforeCatch = true; 3024 EXPECT_EQ("try {\n" 3025 " foo();\n" 3026 "}\n" 3027 "catch (...) {\n" 3028 " baz();\n" 3029 "}", 3030 format("try{foo();}catch(...){baz();}", Style)); 3031 3032 Style.BraceWrapping.AfterFunction = true; 3033 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3034 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3035 Style.ColumnLimit = 80; 3036 verifyFormat("void shortfunction() { bar(); }", Style); 3037 3038 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3039 verifyFormat("void shortfunction()\n" 3040 "{\n" 3041 " bar();\n" 3042 "}", 3043 Style); 3044 } 3045 3046 TEST_F(FormatTest, BeforeWhile) { 3047 FormatStyle Style = getLLVMStyle(); 3048 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3049 3050 verifyFormat("do {\n" 3051 " foo();\n" 3052 "} while (1);", 3053 Style); 3054 Style.BraceWrapping.BeforeWhile = true; 3055 verifyFormat("do {\n" 3056 " foo();\n" 3057 "}\n" 3058 "while (1);", 3059 Style); 3060 } 3061 3062 //===----------------------------------------------------------------------===// 3063 // Tests for classes, namespaces, etc. 3064 //===----------------------------------------------------------------------===// 3065 3066 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 3067 verifyFormat("class A {};"); 3068 } 3069 3070 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 3071 verifyFormat("class A {\n" 3072 "public:\n" 3073 "public: // comment\n" 3074 "protected:\n" 3075 "private:\n" 3076 " void f() {}\n" 3077 "};"); 3078 verifyFormat("export class A {\n" 3079 "public:\n" 3080 "public: // comment\n" 3081 "protected:\n" 3082 "private:\n" 3083 " void f() {}\n" 3084 "};"); 3085 verifyGoogleFormat("class A {\n" 3086 " public:\n" 3087 " protected:\n" 3088 " private:\n" 3089 " void f() {}\n" 3090 "};"); 3091 verifyGoogleFormat("export class A {\n" 3092 " public:\n" 3093 " protected:\n" 3094 " private:\n" 3095 " void f() {}\n" 3096 "};"); 3097 verifyFormat("class A {\n" 3098 "public slots:\n" 3099 " void f1() {}\n" 3100 "public Q_SLOTS:\n" 3101 " void f2() {}\n" 3102 "protected slots:\n" 3103 " void f3() {}\n" 3104 "protected Q_SLOTS:\n" 3105 " void f4() {}\n" 3106 "private slots:\n" 3107 " void f5() {}\n" 3108 "private Q_SLOTS:\n" 3109 " void f6() {}\n" 3110 "signals:\n" 3111 " void g1();\n" 3112 "Q_SIGNALS:\n" 3113 " void g2();\n" 3114 "};"); 3115 3116 // Don't interpret 'signals' the wrong way. 3117 verifyFormat("signals.set();"); 3118 verifyFormat("for (Signals signals : f()) {\n}"); 3119 verifyFormat("{\n" 3120 " signals.set(); // This needs indentation.\n" 3121 "}"); 3122 verifyFormat("void f() {\n" 3123 "label:\n" 3124 " signals.baz();\n" 3125 "}"); 3126 verifyFormat("private[1];"); 3127 verifyFormat("testArray[public] = 1;"); 3128 verifyFormat("public();"); 3129 verifyFormat("myFunc(public);"); 3130 verifyFormat("std::vector<int> testVec = {private};"); 3131 verifyFormat("private.p = 1;"); 3132 verifyFormat("void function(private...){};"); 3133 verifyFormat("if (private && public)\n"); 3134 verifyFormat("private &= true;"); 3135 verifyFormat("int x = private * public;"); 3136 verifyFormat("public *= private;"); 3137 verifyFormat("int x = public + private;"); 3138 verifyFormat("private++;"); 3139 verifyFormat("++private;"); 3140 verifyFormat("public += private;"); 3141 verifyFormat("public = public - private;"); 3142 verifyFormat("public->foo();"); 3143 verifyFormat("private--;"); 3144 verifyFormat("--private;"); 3145 verifyFormat("public -= 1;"); 3146 verifyFormat("if (!private && !public)\n"); 3147 verifyFormat("public != private;"); 3148 verifyFormat("int x = public / private;"); 3149 verifyFormat("public /= 2;"); 3150 verifyFormat("public = public % 2;"); 3151 verifyFormat("public %= 2;"); 3152 verifyFormat("if (public < private)\n"); 3153 verifyFormat("public << private;"); 3154 verifyFormat("public <<= private;"); 3155 verifyFormat("if (public > private)\n"); 3156 verifyFormat("public >> private;"); 3157 verifyFormat("public >>= private;"); 3158 verifyFormat("public ^ private;"); 3159 verifyFormat("public ^= private;"); 3160 verifyFormat("public | private;"); 3161 verifyFormat("public |= private;"); 3162 verifyFormat("auto x = private ? 1 : 2;"); 3163 verifyFormat("if (public == private)\n"); 3164 verifyFormat("void foo(public, private)"); 3165 verifyFormat("public::foo();"); 3166 } 3167 3168 TEST_F(FormatTest, SeparatesLogicalBlocks) { 3169 EXPECT_EQ("class A {\n" 3170 "public:\n" 3171 " void f();\n" 3172 "\n" 3173 "private:\n" 3174 " void g() {}\n" 3175 " // test\n" 3176 "protected:\n" 3177 " int h;\n" 3178 "};", 3179 format("class A {\n" 3180 "public:\n" 3181 "void f();\n" 3182 "private:\n" 3183 "void g() {}\n" 3184 "// test\n" 3185 "protected:\n" 3186 "int h;\n" 3187 "};")); 3188 EXPECT_EQ("class A {\n" 3189 "protected:\n" 3190 "public:\n" 3191 " void f();\n" 3192 "};", 3193 format("class A {\n" 3194 "protected:\n" 3195 "\n" 3196 "public:\n" 3197 "\n" 3198 " void f();\n" 3199 "};")); 3200 3201 // Even ensure proper spacing inside macros. 3202 EXPECT_EQ("#define B \\\n" 3203 " class A { \\\n" 3204 " protected: \\\n" 3205 " public: \\\n" 3206 " void f(); \\\n" 3207 " };", 3208 format("#define B \\\n" 3209 " class A { \\\n" 3210 " protected: \\\n" 3211 " \\\n" 3212 " public: \\\n" 3213 " \\\n" 3214 " void f(); \\\n" 3215 " };", 3216 getGoogleStyle())); 3217 // But don't remove empty lines after macros ending in access specifiers. 3218 EXPECT_EQ("#define A private:\n" 3219 "\n" 3220 "int i;", 3221 format("#define A private:\n" 3222 "\n" 3223 "int i;")); 3224 } 3225 3226 TEST_F(FormatTest, FormatsClasses) { 3227 verifyFormat("class A : public B {};"); 3228 verifyFormat("class A : public ::B {};"); 3229 3230 verifyFormat( 3231 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3232 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3233 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3234 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3235 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3236 verifyFormat( 3237 "class A : public B, public C, public D, public E, public F {};"); 3238 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3239 " public C,\n" 3240 " public D,\n" 3241 " public E,\n" 3242 " public F,\n" 3243 " public G {};"); 3244 3245 verifyFormat("class\n" 3246 " ReallyReallyLongClassName {\n" 3247 " int i;\n" 3248 "};", 3249 getLLVMStyleWithColumns(32)); 3250 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3251 " aaaaaaaaaaaaaaaa> {};"); 3252 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3253 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3254 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3255 verifyFormat("template <class R, class C>\n" 3256 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3257 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3258 verifyFormat("class ::A::B {};"); 3259 } 3260 3261 TEST_F(FormatTest, BreakInheritanceStyle) { 3262 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3263 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3264 FormatStyle::BILS_BeforeComma; 3265 verifyFormat("class MyClass : public X {};", 3266 StyleWithInheritanceBreakBeforeComma); 3267 verifyFormat("class MyClass\n" 3268 " : public X\n" 3269 " , public Y {};", 3270 StyleWithInheritanceBreakBeforeComma); 3271 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3272 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3273 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3274 StyleWithInheritanceBreakBeforeComma); 3275 verifyFormat("struct aaaaaaaaaaaaa\n" 3276 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3277 " aaaaaaaaaaaaaaaa> {};", 3278 StyleWithInheritanceBreakBeforeComma); 3279 3280 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3281 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3282 FormatStyle::BILS_AfterColon; 3283 verifyFormat("class MyClass : public X {};", 3284 StyleWithInheritanceBreakAfterColon); 3285 verifyFormat("class MyClass : public X, public Y {};", 3286 StyleWithInheritanceBreakAfterColon); 3287 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3288 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3289 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3290 StyleWithInheritanceBreakAfterColon); 3291 verifyFormat("struct aaaaaaaaaaaaa :\n" 3292 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3293 " aaaaaaaaaaaaaaaa> {};", 3294 StyleWithInheritanceBreakAfterColon); 3295 3296 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3297 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3298 FormatStyle::BILS_AfterComma; 3299 verifyFormat("class MyClass : public X {};", 3300 StyleWithInheritanceBreakAfterComma); 3301 verifyFormat("class MyClass : public X,\n" 3302 " public Y {};", 3303 StyleWithInheritanceBreakAfterComma); 3304 verifyFormat( 3305 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3306 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3307 "{};", 3308 StyleWithInheritanceBreakAfterComma); 3309 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3310 " aaaaaaaaaaaaaaaa> {};", 3311 StyleWithInheritanceBreakAfterComma); 3312 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3313 " : public OnceBreak,\n" 3314 " public AlwaysBreak,\n" 3315 " EvenBasesFitInOneLine {};", 3316 StyleWithInheritanceBreakAfterComma); 3317 } 3318 3319 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 3320 verifyFormat("class A {\n} a, b;"); 3321 verifyFormat("struct A {\n} a, b;"); 3322 verifyFormat("union A {\n} a;"); 3323 } 3324 3325 TEST_F(FormatTest, FormatsEnum) { 3326 verifyFormat("enum {\n" 3327 " Zero,\n" 3328 " One = 1,\n" 3329 " Two = One + 1,\n" 3330 " Three = (One + Two),\n" 3331 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3332 " Five = (One, Two, Three, Four, 5)\n" 3333 "};"); 3334 verifyGoogleFormat("enum {\n" 3335 " Zero,\n" 3336 " One = 1,\n" 3337 " Two = One + 1,\n" 3338 " Three = (One + Two),\n" 3339 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3340 " Five = (One, Two, Three, Four, 5)\n" 3341 "};"); 3342 verifyFormat("enum Enum {};"); 3343 verifyFormat("enum {};"); 3344 verifyFormat("enum X E {} d;"); 3345 verifyFormat("enum __attribute__((...)) E {} d;"); 3346 verifyFormat("enum __declspec__((...)) E {} d;"); 3347 verifyFormat("enum {\n" 3348 " Bar = Foo<int, int>::value\n" 3349 "};", 3350 getLLVMStyleWithColumns(30)); 3351 3352 verifyFormat("enum ShortEnum { A, B, C };"); 3353 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3354 3355 EXPECT_EQ("enum KeepEmptyLines {\n" 3356 " ONE,\n" 3357 "\n" 3358 " TWO,\n" 3359 "\n" 3360 " THREE\n" 3361 "}", 3362 format("enum KeepEmptyLines {\n" 3363 " ONE,\n" 3364 "\n" 3365 " TWO,\n" 3366 "\n" 3367 "\n" 3368 " THREE\n" 3369 "}")); 3370 verifyFormat("enum E { // comment\n" 3371 " ONE,\n" 3372 " TWO\n" 3373 "};\n" 3374 "int i;"); 3375 3376 FormatStyle EightIndent = getLLVMStyle(); 3377 EightIndent.IndentWidth = 8; 3378 verifyFormat("enum {\n" 3379 " VOID,\n" 3380 " CHAR,\n" 3381 " SHORT,\n" 3382 " INT,\n" 3383 " LONG,\n" 3384 " SIGNED,\n" 3385 " UNSIGNED,\n" 3386 " BOOL,\n" 3387 " FLOAT,\n" 3388 " DOUBLE,\n" 3389 " COMPLEX\n" 3390 "};", 3391 EightIndent); 3392 3393 // Not enums. 3394 verifyFormat("enum X f() {\n" 3395 " a();\n" 3396 " return 42;\n" 3397 "}"); 3398 verifyFormat("enum X Type::f() {\n" 3399 " a();\n" 3400 " return 42;\n" 3401 "}"); 3402 verifyFormat("enum ::X f() {\n" 3403 " a();\n" 3404 " return 42;\n" 3405 "}"); 3406 verifyFormat("enum ns::X f() {\n" 3407 " a();\n" 3408 " return 42;\n" 3409 "}"); 3410 } 3411 3412 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3413 verifyFormat("enum Type {\n" 3414 " One = 0; // These semicolons should be commas.\n" 3415 " Two = 1;\n" 3416 "};"); 3417 verifyFormat("namespace n {\n" 3418 "enum Type {\n" 3419 " One,\n" 3420 " Two, // missing };\n" 3421 " int i;\n" 3422 "}\n" 3423 "void g() {}"); 3424 } 3425 3426 TEST_F(FormatTest, FormatsEnumStruct) { 3427 verifyFormat("enum struct {\n" 3428 " Zero,\n" 3429 " One = 1,\n" 3430 " Two = One + 1,\n" 3431 " Three = (One + Two),\n" 3432 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3433 " Five = (One, Two, Three, Four, 5)\n" 3434 "};"); 3435 verifyFormat("enum struct Enum {};"); 3436 verifyFormat("enum struct {};"); 3437 verifyFormat("enum struct X E {} d;"); 3438 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3439 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3440 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3441 } 3442 3443 TEST_F(FormatTest, FormatsEnumClass) { 3444 verifyFormat("enum class {\n" 3445 " Zero,\n" 3446 " One = 1,\n" 3447 " Two = One + 1,\n" 3448 " Three = (One + Two),\n" 3449 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3450 " Five = (One, Two, Three, Four, 5)\n" 3451 "};"); 3452 verifyFormat("enum class Enum {};"); 3453 verifyFormat("enum class {};"); 3454 verifyFormat("enum class X E {} d;"); 3455 verifyFormat("enum class __attribute__((...)) E {} d;"); 3456 verifyFormat("enum class __declspec__((...)) E {} d;"); 3457 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3458 } 3459 3460 TEST_F(FormatTest, FormatsEnumTypes) { 3461 verifyFormat("enum X : int {\n" 3462 " A, // Force multiple lines.\n" 3463 " B\n" 3464 "};"); 3465 verifyFormat("enum X : int { A, B };"); 3466 verifyFormat("enum X : std::uint32_t { A, B };"); 3467 } 3468 3469 TEST_F(FormatTest, FormatsTypedefEnum) { 3470 FormatStyle Style = getLLVMStyleWithColumns(40); 3471 verifyFormat("typedef enum {} EmptyEnum;"); 3472 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3473 verifyFormat("typedef enum {\n" 3474 " ZERO = 0,\n" 3475 " ONE = 1,\n" 3476 " TWO = 2,\n" 3477 " THREE = 3\n" 3478 "} LongEnum;", 3479 Style); 3480 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3481 Style.BraceWrapping.AfterEnum = true; 3482 verifyFormat("typedef enum {} EmptyEnum;"); 3483 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3484 verifyFormat("typedef enum\n" 3485 "{\n" 3486 " ZERO = 0,\n" 3487 " ONE = 1,\n" 3488 " TWO = 2,\n" 3489 " THREE = 3\n" 3490 "} LongEnum;", 3491 Style); 3492 } 3493 3494 TEST_F(FormatTest, FormatsNSEnums) { 3495 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3496 verifyGoogleFormat( 3497 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3498 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3499 " // Information about someDecentlyLongValue.\n" 3500 " someDecentlyLongValue,\n" 3501 " // Information about anotherDecentlyLongValue.\n" 3502 " anotherDecentlyLongValue,\n" 3503 " // Information about aThirdDecentlyLongValue.\n" 3504 " aThirdDecentlyLongValue\n" 3505 "};"); 3506 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3507 " // Information about someDecentlyLongValue.\n" 3508 " someDecentlyLongValue,\n" 3509 " // Information about anotherDecentlyLongValue.\n" 3510 " anotherDecentlyLongValue,\n" 3511 " // Information about aThirdDecentlyLongValue.\n" 3512 " aThirdDecentlyLongValue\n" 3513 "};"); 3514 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3515 " a = 1,\n" 3516 " b = 2,\n" 3517 " c = 3,\n" 3518 "};"); 3519 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3520 " a = 1,\n" 3521 " b = 2,\n" 3522 " c = 3,\n" 3523 "};"); 3524 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3525 " a = 1,\n" 3526 " b = 2,\n" 3527 " c = 3,\n" 3528 "};"); 3529 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3530 " a = 1,\n" 3531 " b = 2,\n" 3532 " c = 3,\n" 3533 "};"); 3534 } 3535 3536 TEST_F(FormatTest, FormatsBitfields) { 3537 verifyFormat("struct Bitfields {\n" 3538 " unsigned sClass : 8;\n" 3539 " unsigned ValueKind : 2;\n" 3540 "};"); 3541 verifyFormat("struct A {\n" 3542 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3543 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3544 "};"); 3545 verifyFormat("struct MyStruct {\n" 3546 " uchar data;\n" 3547 " uchar : 8;\n" 3548 " uchar : 8;\n" 3549 " uchar other;\n" 3550 "};"); 3551 FormatStyle Style = getLLVMStyle(); 3552 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3553 verifyFormat("struct Bitfields {\n" 3554 " unsigned sClass:8;\n" 3555 " unsigned ValueKind:2;\n" 3556 " uchar other;\n" 3557 "};", 3558 Style); 3559 verifyFormat("struct A {\n" 3560 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3561 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3562 "};", 3563 Style); 3564 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3565 verifyFormat("struct Bitfields {\n" 3566 " unsigned sClass :8;\n" 3567 " unsigned ValueKind :2;\n" 3568 " uchar other;\n" 3569 "};", 3570 Style); 3571 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3572 verifyFormat("struct Bitfields {\n" 3573 " unsigned sClass: 8;\n" 3574 " unsigned ValueKind: 2;\n" 3575 " uchar other;\n" 3576 "};", 3577 Style); 3578 } 3579 3580 TEST_F(FormatTest, FormatsNamespaces) { 3581 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3582 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3583 3584 verifyFormat("namespace some_namespace {\n" 3585 "class A {};\n" 3586 "void f() { f(); }\n" 3587 "}", 3588 LLVMWithNoNamespaceFix); 3589 verifyFormat("namespace N::inline D {\n" 3590 "class A {};\n" 3591 "void f() { f(); }\n" 3592 "}", 3593 LLVMWithNoNamespaceFix); 3594 verifyFormat("namespace N::inline D::E {\n" 3595 "class A {};\n" 3596 "void f() { f(); }\n" 3597 "}", 3598 LLVMWithNoNamespaceFix); 3599 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3600 "class A {};\n" 3601 "void f() { f(); }\n" 3602 "}", 3603 LLVMWithNoNamespaceFix); 3604 verifyFormat("/* something */ namespace some_namespace {\n" 3605 "class A {};\n" 3606 "void f() { f(); }\n" 3607 "}", 3608 LLVMWithNoNamespaceFix); 3609 verifyFormat("namespace {\n" 3610 "class A {};\n" 3611 "void f() { f(); }\n" 3612 "}", 3613 LLVMWithNoNamespaceFix); 3614 verifyFormat("/* something */ namespace {\n" 3615 "class A {};\n" 3616 "void f() { f(); }\n" 3617 "}", 3618 LLVMWithNoNamespaceFix); 3619 verifyFormat("inline namespace X {\n" 3620 "class A {};\n" 3621 "void f() { f(); }\n" 3622 "}", 3623 LLVMWithNoNamespaceFix); 3624 verifyFormat("/* something */ inline namespace X {\n" 3625 "class A {};\n" 3626 "void f() { f(); }\n" 3627 "}", 3628 LLVMWithNoNamespaceFix); 3629 verifyFormat("export namespace X {\n" 3630 "class A {};\n" 3631 "void f() { f(); }\n" 3632 "}", 3633 LLVMWithNoNamespaceFix); 3634 verifyFormat("using namespace some_namespace;\n" 3635 "class A {};\n" 3636 "void f() { f(); }", 3637 LLVMWithNoNamespaceFix); 3638 3639 // This code is more common than we thought; if we 3640 // layout this correctly the semicolon will go into 3641 // its own line, which is undesirable. 3642 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3643 verifyFormat("namespace {\n" 3644 "class A {};\n" 3645 "};", 3646 LLVMWithNoNamespaceFix); 3647 3648 verifyFormat("namespace {\n" 3649 "int SomeVariable = 0; // comment\n" 3650 "} // namespace", 3651 LLVMWithNoNamespaceFix); 3652 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3653 "#define HEADER_GUARD\n" 3654 "namespace my_namespace {\n" 3655 "int i;\n" 3656 "} // my_namespace\n" 3657 "#endif // HEADER_GUARD", 3658 format("#ifndef HEADER_GUARD\n" 3659 " #define HEADER_GUARD\n" 3660 " namespace my_namespace {\n" 3661 "int i;\n" 3662 "} // my_namespace\n" 3663 "#endif // HEADER_GUARD", 3664 LLVMWithNoNamespaceFix)); 3665 3666 EXPECT_EQ("namespace A::B {\n" 3667 "class C {};\n" 3668 "}", 3669 format("namespace A::B {\n" 3670 "class C {};\n" 3671 "}", 3672 LLVMWithNoNamespaceFix)); 3673 3674 FormatStyle Style = getLLVMStyle(); 3675 Style.NamespaceIndentation = FormatStyle::NI_All; 3676 EXPECT_EQ("namespace out {\n" 3677 " int i;\n" 3678 " namespace in {\n" 3679 " int i;\n" 3680 " } // namespace in\n" 3681 "} // namespace out", 3682 format("namespace out {\n" 3683 "int i;\n" 3684 "namespace in {\n" 3685 "int i;\n" 3686 "} // namespace in\n" 3687 "} // namespace out", 3688 Style)); 3689 3690 FormatStyle ShortInlineFunctions = getLLVMStyle(); 3691 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All; 3692 ShortInlineFunctions.AllowShortFunctionsOnASingleLine = 3693 FormatStyle::SFS_Inline; 3694 verifyFormat("namespace {\n" 3695 " void f() {\n" 3696 " return;\n" 3697 " }\n" 3698 "} // namespace\n", 3699 ShortInlineFunctions); 3700 verifyFormat("namespace {\n" 3701 " int some_int;\n" 3702 " void f() {\n" 3703 " return;\n" 3704 " }\n" 3705 "} // namespace\n", 3706 ShortInlineFunctions); 3707 verifyFormat("namespace interface {\n" 3708 " void f() {\n" 3709 " return;\n" 3710 " }\n" 3711 "} // namespace interface\n", 3712 ShortInlineFunctions); 3713 verifyFormat("namespace {\n" 3714 " class X {\n" 3715 " void f() { return; }\n" 3716 " };\n" 3717 "} // namespace\n", 3718 ShortInlineFunctions); 3719 verifyFormat("namespace {\n" 3720 " struct X {\n" 3721 " void f() { return; }\n" 3722 " };\n" 3723 "} // namespace\n", 3724 ShortInlineFunctions); 3725 verifyFormat("namespace {\n" 3726 " union X {\n" 3727 " void f() { return; }\n" 3728 " };\n" 3729 "} // namespace\n", 3730 ShortInlineFunctions); 3731 verifyFormat("extern \"C\" {\n" 3732 "void f() {\n" 3733 " return;\n" 3734 "}\n" 3735 "} // namespace\n", 3736 ShortInlineFunctions); 3737 verifyFormat("namespace {\n" 3738 " class X {\n" 3739 " void f() { return; }\n" 3740 " } x;\n" 3741 "} // namespace\n", 3742 ShortInlineFunctions); 3743 verifyFormat("namespace {\n" 3744 " [[nodiscard]] class X {\n" 3745 " void f() { return; }\n" 3746 " };\n" 3747 "} // namespace\n", 3748 ShortInlineFunctions); 3749 verifyFormat("namespace {\n" 3750 " static class X {\n" 3751 " void f() { return; }\n" 3752 " } x;\n" 3753 "} // namespace\n", 3754 ShortInlineFunctions); 3755 verifyFormat("namespace {\n" 3756 " constexpr class X {\n" 3757 " void f() { return; }\n" 3758 " } x;\n" 3759 "} // namespace\n", 3760 ShortInlineFunctions); 3761 3762 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent; 3763 verifyFormat("extern \"C\" {\n" 3764 " void f() {\n" 3765 " return;\n" 3766 " }\n" 3767 "} // namespace\n", 3768 ShortInlineFunctions); 3769 3770 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3771 EXPECT_EQ("namespace out {\n" 3772 "int i;\n" 3773 "namespace in {\n" 3774 " int i;\n" 3775 "} // namespace in\n" 3776 "} // namespace out", 3777 format("namespace out {\n" 3778 "int i;\n" 3779 "namespace in {\n" 3780 "int i;\n" 3781 "} // namespace in\n" 3782 "} // namespace out", 3783 Style)); 3784 3785 Style.NamespaceIndentation = FormatStyle::NI_None; 3786 verifyFormat("template <class T>\n" 3787 "concept a_concept = X<>;\n" 3788 "namespace B {\n" 3789 "struct b_struct {};\n" 3790 "} // namespace B\n", 3791 Style); 3792 verifyFormat("template <int I> constexpr void foo requires(I == 42) {}\n" 3793 "namespace ns {\n" 3794 "void foo() {}\n" 3795 "} // namespace ns\n", 3796 Style); 3797 } 3798 3799 TEST_F(FormatTest, NamespaceMacros) { 3800 FormatStyle Style = getLLVMStyle(); 3801 Style.NamespaceMacros.push_back("TESTSUITE"); 3802 3803 verifyFormat("TESTSUITE(A) {\n" 3804 "int foo();\n" 3805 "} // TESTSUITE(A)", 3806 Style); 3807 3808 verifyFormat("TESTSUITE(A, B) {\n" 3809 "int foo();\n" 3810 "} // TESTSUITE(A)", 3811 Style); 3812 3813 // Properly indent according to NamespaceIndentation style 3814 Style.NamespaceIndentation = FormatStyle::NI_All; 3815 verifyFormat("TESTSUITE(A) {\n" 3816 " int foo();\n" 3817 "} // TESTSUITE(A)", 3818 Style); 3819 verifyFormat("TESTSUITE(A) {\n" 3820 " namespace B {\n" 3821 " int foo();\n" 3822 " } // namespace B\n" 3823 "} // TESTSUITE(A)", 3824 Style); 3825 verifyFormat("namespace A {\n" 3826 " TESTSUITE(B) {\n" 3827 " int foo();\n" 3828 " } // TESTSUITE(B)\n" 3829 "} // namespace A", 3830 Style); 3831 3832 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3833 verifyFormat("TESTSUITE(A) {\n" 3834 "TESTSUITE(B) {\n" 3835 " int foo();\n" 3836 "} // TESTSUITE(B)\n" 3837 "} // TESTSUITE(A)", 3838 Style); 3839 verifyFormat("TESTSUITE(A) {\n" 3840 "namespace B {\n" 3841 " int foo();\n" 3842 "} // namespace B\n" 3843 "} // TESTSUITE(A)", 3844 Style); 3845 verifyFormat("namespace A {\n" 3846 "TESTSUITE(B) {\n" 3847 " int foo();\n" 3848 "} // TESTSUITE(B)\n" 3849 "} // namespace A", 3850 Style); 3851 3852 // Properly merge namespace-macros blocks in CompactNamespaces mode 3853 Style.NamespaceIndentation = FormatStyle::NI_None; 3854 Style.CompactNamespaces = true; 3855 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3856 "}} // TESTSUITE(A::B)", 3857 Style); 3858 3859 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3860 "}} // TESTSUITE(out::in)", 3861 format("TESTSUITE(out) {\n" 3862 "TESTSUITE(in) {\n" 3863 "} // TESTSUITE(in)\n" 3864 "} // TESTSUITE(out)", 3865 Style)); 3866 3867 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3868 "}} // TESTSUITE(out::in)", 3869 format("TESTSUITE(out) {\n" 3870 "TESTSUITE(in) {\n" 3871 "} // TESTSUITE(in)\n" 3872 "} // TESTSUITE(out)", 3873 Style)); 3874 3875 // Do not merge different namespaces/macros 3876 EXPECT_EQ("namespace out {\n" 3877 "TESTSUITE(in) {\n" 3878 "} // TESTSUITE(in)\n" 3879 "} // namespace out", 3880 format("namespace out {\n" 3881 "TESTSUITE(in) {\n" 3882 "} // TESTSUITE(in)\n" 3883 "} // namespace out", 3884 Style)); 3885 EXPECT_EQ("TESTSUITE(out) {\n" 3886 "namespace in {\n" 3887 "} // namespace in\n" 3888 "} // TESTSUITE(out)", 3889 format("TESTSUITE(out) {\n" 3890 "namespace in {\n" 3891 "} // namespace in\n" 3892 "} // TESTSUITE(out)", 3893 Style)); 3894 Style.NamespaceMacros.push_back("FOOBAR"); 3895 EXPECT_EQ("TESTSUITE(out) {\n" 3896 "FOOBAR(in) {\n" 3897 "} // FOOBAR(in)\n" 3898 "} // TESTSUITE(out)", 3899 format("TESTSUITE(out) {\n" 3900 "FOOBAR(in) {\n" 3901 "} // FOOBAR(in)\n" 3902 "} // TESTSUITE(out)", 3903 Style)); 3904 } 3905 3906 TEST_F(FormatTest, FormatsCompactNamespaces) { 3907 FormatStyle Style = getLLVMStyle(); 3908 Style.CompactNamespaces = true; 3909 Style.NamespaceMacros.push_back("TESTSUITE"); 3910 3911 verifyFormat("namespace A { namespace B {\n" 3912 "}} // namespace A::B", 3913 Style); 3914 3915 EXPECT_EQ("namespace out { namespace in {\n" 3916 "}} // namespace out::in", 3917 format("namespace out {\n" 3918 "namespace in {\n" 3919 "} // namespace in\n" 3920 "} // namespace out", 3921 Style)); 3922 3923 // Only namespaces which have both consecutive opening and end get compacted 3924 EXPECT_EQ("namespace out {\n" 3925 "namespace in1 {\n" 3926 "} // namespace in1\n" 3927 "namespace in2 {\n" 3928 "} // namespace in2\n" 3929 "} // namespace out", 3930 format("namespace out {\n" 3931 "namespace in1 {\n" 3932 "} // namespace in1\n" 3933 "namespace in2 {\n" 3934 "} // namespace in2\n" 3935 "} // namespace out", 3936 Style)); 3937 3938 EXPECT_EQ("namespace out {\n" 3939 "int i;\n" 3940 "namespace in {\n" 3941 "int j;\n" 3942 "} // namespace in\n" 3943 "int k;\n" 3944 "} // namespace out", 3945 format("namespace out { int i;\n" 3946 "namespace in { int j; } // namespace in\n" 3947 "int k; } // namespace out", 3948 Style)); 3949 3950 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 3951 "}}} // namespace A::B::C\n", 3952 format("namespace A { namespace B {\n" 3953 "namespace C {\n" 3954 "}} // namespace B::C\n" 3955 "} // namespace A\n", 3956 Style)); 3957 3958 Style.ColumnLimit = 40; 3959 EXPECT_EQ("namespace aaaaaaaaaa {\n" 3960 "namespace bbbbbbbbbb {\n" 3961 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 3962 format("namespace aaaaaaaaaa {\n" 3963 "namespace bbbbbbbbbb {\n" 3964 "} // namespace bbbbbbbbbb\n" 3965 "} // namespace aaaaaaaaaa", 3966 Style)); 3967 3968 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 3969 "namespace cccccc {\n" 3970 "}}} // namespace aaaaaa::bbbbbb::cccccc", 3971 format("namespace aaaaaa {\n" 3972 "namespace bbbbbb {\n" 3973 "namespace cccccc {\n" 3974 "} // namespace cccccc\n" 3975 "} // namespace bbbbbb\n" 3976 "} // namespace aaaaaa", 3977 Style)); 3978 Style.ColumnLimit = 80; 3979 3980 // Extra semicolon after 'inner' closing brace prevents merging 3981 EXPECT_EQ("namespace out { namespace in {\n" 3982 "}; } // namespace out::in", 3983 format("namespace out {\n" 3984 "namespace in {\n" 3985 "}; // namespace in\n" 3986 "} // namespace out", 3987 Style)); 3988 3989 // Extra semicolon after 'outer' closing brace is conserved 3990 EXPECT_EQ("namespace out { namespace in {\n" 3991 "}}; // namespace out::in", 3992 format("namespace out {\n" 3993 "namespace in {\n" 3994 "} // namespace in\n" 3995 "}; // namespace out", 3996 Style)); 3997 3998 Style.NamespaceIndentation = FormatStyle::NI_All; 3999 EXPECT_EQ("namespace out { namespace in {\n" 4000 " int i;\n" 4001 "}} // namespace out::in", 4002 format("namespace out {\n" 4003 "namespace in {\n" 4004 "int i;\n" 4005 "} // namespace in\n" 4006 "} // namespace out", 4007 Style)); 4008 EXPECT_EQ("namespace out { namespace mid {\n" 4009 " namespace in {\n" 4010 " int j;\n" 4011 " } // namespace in\n" 4012 " int k;\n" 4013 "}} // namespace out::mid", 4014 format("namespace out { namespace mid {\n" 4015 "namespace in { int j; } // namespace in\n" 4016 "int k; }} // namespace out::mid", 4017 Style)); 4018 4019 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4020 EXPECT_EQ("namespace out { namespace in {\n" 4021 " int i;\n" 4022 "}} // namespace out::in", 4023 format("namespace out {\n" 4024 "namespace in {\n" 4025 "int i;\n" 4026 "} // namespace in\n" 4027 "} // namespace out", 4028 Style)); 4029 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 4030 " int i;\n" 4031 "}}} // namespace out::mid::in", 4032 format("namespace out {\n" 4033 "namespace mid {\n" 4034 "namespace in {\n" 4035 "int i;\n" 4036 "} // namespace in\n" 4037 "} // namespace mid\n" 4038 "} // namespace out", 4039 Style)); 4040 4041 Style.CompactNamespaces = true; 4042 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 4043 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4044 Style.BraceWrapping.BeforeLambdaBody = true; 4045 verifyFormat("namespace out { namespace in {\n" 4046 "}} // namespace out::in", 4047 Style); 4048 EXPECT_EQ("namespace out { namespace in {\n" 4049 "}} // namespace out::in", 4050 format("namespace out {\n" 4051 "namespace in {\n" 4052 "} // namespace in\n" 4053 "} // namespace out", 4054 Style)); 4055 } 4056 4057 TEST_F(FormatTest, FormatsExternC) { 4058 verifyFormat("extern \"C\" {\nint a;"); 4059 verifyFormat("extern \"C\" {}"); 4060 verifyFormat("extern \"C\" {\n" 4061 "int foo();\n" 4062 "}"); 4063 verifyFormat("extern \"C\" int foo() {}"); 4064 verifyFormat("extern \"C\" int foo();"); 4065 verifyFormat("extern \"C\" int foo() {\n" 4066 " int i = 42;\n" 4067 " return i;\n" 4068 "}"); 4069 4070 FormatStyle Style = getLLVMStyle(); 4071 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4072 Style.BraceWrapping.AfterFunction = true; 4073 verifyFormat("extern \"C\" int foo() {}", Style); 4074 verifyFormat("extern \"C\" int foo();", Style); 4075 verifyFormat("extern \"C\" int foo()\n" 4076 "{\n" 4077 " int i = 42;\n" 4078 " return i;\n" 4079 "}", 4080 Style); 4081 4082 Style.BraceWrapping.AfterExternBlock = true; 4083 Style.BraceWrapping.SplitEmptyRecord = false; 4084 verifyFormat("extern \"C\"\n" 4085 "{}", 4086 Style); 4087 verifyFormat("extern \"C\"\n" 4088 "{\n" 4089 " int foo();\n" 4090 "}", 4091 Style); 4092 } 4093 4094 TEST_F(FormatTest, IndentExternBlockStyle) { 4095 FormatStyle Style = getLLVMStyle(); 4096 Style.IndentWidth = 2; 4097 4098 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4099 verifyFormat("extern \"C\" { /*9*/\n" 4100 "}", 4101 Style); 4102 verifyFormat("extern \"C\" {\n" 4103 " int foo10();\n" 4104 "}", 4105 Style); 4106 4107 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4108 verifyFormat("extern \"C\" { /*11*/\n" 4109 "}", 4110 Style); 4111 verifyFormat("extern \"C\" {\n" 4112 "int foo12();\n" 4113 "}", 4114 Style); 4115 4116 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4117 Style.BraceWrapping.AfterExternBlock = true; 4118 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4119 verifyFormat("extern \"C\"\n" 4120 "{ /*13*/\n" 4121 "}", 4122 Style); 4123 verifyFormat("extern \"C\"\n{\n" 4124 " int foo14();\n" 4125 "}", 4126 Style); 4127 4128 Style.BraceWrapping.AfterExternBlock = false; 4129 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4130 verifyFormat("extern \"C\" { /*15*/\n" 4131 "}", 4132 Style); 4133 verifyFormat("extern \"C\" {\n" 4134 "int foo16();\n" 4135 "}", 4136 Style); 4137 4138 Style.BraceWrapping.AfterExternBlock = true; 4139 verifyFormat("extern \"C\"\n" 4140 "{ /*13*/\n" 4141 "}", 4142 Style); 4143 verifyFormat("extern \"C\"\n" 4144 "{\n" 4145 "int foo14();\n" 4146 "}", 4147 Style); 4148 4149 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4150 verifyFormat("extern \"C\"\n" 4151 "{ /*13*/\n" 4152 "}", 4153 Style); 4154 verifyFormat("extern \"C\"\n" 4155 "{\n" 4156 " int foo14();\n" 4157 "}", 4158 Style); 4159 } 4160 4161 TEST_F(FormatTest, FormatsInlineASM) { 4162 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 4163 verifyFormat("asm(\"nop\" ::: \"memory\");"); 4164 verifyFormat( 4165 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 4166 " \"cpuid\\n\\t\"\n" 4167 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 4168 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 4169 " : \"a\"(value));"); 4170 EXPECT_EQ( 4171 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4172 " __asm {\n" 4173 " mov edx,[that] // vtable in edx\n" 4174 " mov eax,methodIndex\n" 4175 " call [edx][eax*4] // stdcall\n" 4176 " }\n" 4177 "}", 4178 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4179 " __asm {\n" 4180 " mov edx,[that] // vtable in edx\n" 4181 " mov eax,methodIndex\n" 4182 " call [edx][eax*4] // stdcall\n" 4183 " }\n" 4184 "}")); 4185 EXPECT_EQ("_asm {\n" 4186 " xor eax, eax;\n" 4187 " cpuid;\n" 4188 "}", 4189 format("_asm {\n" 4190 " xor eax, eax;\n" 4191 " cpuid;\n" 4192 "}")); 4193 verifyFormat("void function() {\n" 4194 " // comment\n" 4195 " asm(\"\");\n" 4196 "}"); 4197 EXPECT_EQ("__asm {\n" 4198 "}\n" 4199 "int i;", 4200 format("__asm {\n" 4201 "}\n" 4202 "int i;")); 4203 } 4204 4205 TEST_F(FormatTest, FormatTryCatch) { 4206 verifyFormat("try {\n" 4207 " throw a * b;\n" 4208 "} catch (int a) {\n" 4209 " // Do nothing.\n" 4210 "} catch (...) {\n" 4211 " exit(42);\n" 4212 "}"); 4213 4214 // Function-level try statements. 4215 verifyFormat("int f() try { return 4; } catch (...) {\n" 4216 " return 5;\n" 4217 "}"); 4218 verifyFormat("class A {\n" 4219 " int a;\n" 4220 " A() try : a(0) {\n" 4221 " } catch (...) {\n" 4222 " throw;\n" 4223 " }\n" 4224 "};\n"); 4225 verifyFormat("class A {\n" 4226 " int a;\n" 4227 " A() try : a(0), b{1} {\n" 4228 " } catch (...) {\n" 4229 " throw;\n" 4230 " }\n" 4231 "};\n"); 4232 verifyFormat("class A {\n" 4233 " int a;\n" 4234 " A() try : a(0), b{1}, c{2} {\n" 4235 " } catch (...) {\n" 4236 " throw;\n" 4237 " }\n" 4238 "};\n"); 4239 verifyFormat("class A {\n" 4240 " int a;\n" 4241 " A() try : a(0), b{1}, c{2} {\n" 4242 " { // New scope.\n" 4243 " }\n" 4244 " } catch (...) {\n" 4245 " throw;\n" 4246 " }\n" 4247 "};\n"); 4248 4249 // Incomplete try-catch blocks. 4250 verifyIncompleteFormat("try {} catch ("); 4251 } 4252 4253 TEST_F(FormatTest, FormatTryAsAVariable) { 4254 verifyFormat("int try;"); 4255 verifyFormat("int try, size;"); 4256 verifyFormat("try = foo();"); 4257 verifyFormat("if (try < size) {\n return true;\n}"); 4258 4259 verifyFormat("int catch;"); 4260 verifyFormat("int catch, size;"); 4261 verifyFormat("catch = foo();"); 4262 verifyFormat("if (catch < size) {\n return true;\n}"); 4263 4264 FormatStyle Style = getLLVMStyle(); 4265 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4266 Style.BraceWrapping.AfterFunction = true; 4267 Style.BraceWrapping.BeforeCatch = true; 4268 verifyFormat("try {\n" 4269 " int bar = 1;\n" 4270 "}\n" 4271 "catch (...) {\n" 4272 " int bar = 1;\n" 4273 "}", 4274 Style); 4275 verifyFormat("#if NO_EX\n" 4276 "try\n" 4277 "#endif\n" 4278 "{\n" 4279 "}\n" 4280 "#if NO_EX\n" 4281 "catch (...) {\n" 4282 "}", 4283 Style); 4284 verifyFormat("try /* abc */ {\n" 4285 " int bar = 1;\n" 4286 "}\n" 4287 "catch (...) {\n" 4288 " int bar = 1;\n" 4289 "}", 4290 Style); 4291 verifyFormat("try\n" 4292 "// abc\n" 4293 "{\n" 4294 " int bar = 1;\n" 4295 "}\n" 4296 "catch (...) {\n" 4297 " int bar = 1;\n" 4298 "}", 4299 Style); 4300 } 4301 4302 TEST_F(FormatTest, FormatSEHTryCatch) { 4303 verifyFormat("__try {\n" 4304 " int a = b * c;\n" 4305 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 4306 " // Do nothing.\n" 4307 "}"); 4308 4309 verifyFormat("__try {\n" 4310 " int a = b * c;\n" 4311 "} __finally {\n" 4312 " // Do nothing.\n" 4313 "}"); 4314 4315 verifyFormat("DEBUG({\n" 4316 " __try {\n" 4317 " } __finally {\n" 4318 " }\n" 4319 "});\n"); 4320 } 4321 4322 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 4323 verifyFormat("try {\n" 4324 " f();\n" 4325 "} catch {\n" 4326 " g();\n" 4327 "}"); 4328 verifyFormat("try {\n" 4329 " f();\n" 4330 "} catch (A a) MACRO(x) {\n" 4331 " g();\n" 4332 "} catch (B b) MACRO(x) {\n" 4333 " g();\n" 4334 "}"); 4335 } 4336 4337 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 4338 FormatStyle Style = getLLVMStyle(); 4339 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 4340 FormatStyle::BS_WebKit}) { 4341 Style.BreakBeforeBraces = BraceStyle; 4342 verifyFormat("try {\n" 4343 " // something\n" 4344 "} catch (...) {\n" 4345 " // something\n" 4346 "}", 4347 Style); 4348 } 4349 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4350 verifyFormat("try {\n" 4351 " // something\n" 4352 "}\n" 4353 "catch (...) {\n" 4354 " // something\n" 4355 "}", 4356 Style); 4357 verifyFormat("__try {\n" 4358 " // something\n" 4359 "}\n" 4360 "__finally {\n" 4361 " // something\n" 4362 "}", 4363 Style); 4364 verifyFormat("@try {\n" 4365 " // something\n" 4366 "}\n" 4367 "@finally {\n" 4368 " // something\n" 4369 "}", 4370 Style); 4371 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4372 verifyFormat("try\n" 4373 "{\n" 4374 " // something\n" 4375 "}\n" 4376 "catch (...)\n" 4377 "{\n" 4378 " // something\n" 4379 "}", 4380 Style); 4381 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4382 verifyFormat("try\n" 4383 " {\n" 4384 " // something white\n" 4385 " }\n" 4386 "catch (...)\n" 4387 " {\n" 4388 " // something white\n" 4389 " }", 4390 Style); 4391 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4392 verifyFormat("try\n" 4393 " {\n" 4394 " // something\n" 4395 " }\n" 4396 "catch (...)\n" 4397 " {\n" 4398 " // something\n" 4399 " }", 4400 Style); 4401 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4402 Style.BraceWrapping.BeforeCatch = true; 4403 verifyFormat("try {\n" 4404 " // something\n" 4405 "}\n" 4406 "catch (...) {\n" 4407 " // something\n" 4408 "}", 4409 Style); 4410 } 4411 4412 TEST_F(FormatTest, StaticInitializers) { 4413 verifyFormat("static SomeClass SC = {1, 'a'};"); 4414 4415 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4416 " 100000000, " 4417 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4418 4419 // Here, everything other than the "}" would fit on a line. 4420 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4421 " 10000000000000000000000000};"); 4422 EXPECT_EQ("S s = {a,\n" 4423 "\n" 4424 " b};", 4425 format("S s = {\n" 4426 " a,\n" 4427 "\n" 4428 " b\n" 4429 "};")); 4430 4431 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4432 // line. However, the formatting looks a bit off and this probably doesn't 4433 // happen often in practice. 4434 verifyFormat("static int Variable[1] = {\n" 4435 " {1000000000000000000000000000000000000}};", 4436 getLLVMStyleWithColumns(40)); 4437 } 4438 4439 TEST_F(FormatTest, DesignatedInitializers) { 4440 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4441 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4442 " .bbbbbbbbbb = 2,\n" 4443 " .cccccccccc = 3,\n" 4444 " .dddddddddd = 4,\n" 4445 " .eeeeeeeeee = 5};"); 4446 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4447 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4448 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4449 " .ccccccccccccccccccccccccccc = 3,\n" 4450 " .ddddddddddddddddddddddddddd = 4,\n" 4451 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4452 4453 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4454 4455 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4456 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4457 " [2] = bbbbbbbbbb,\n" 4458 " [3] = cccccccccc,\n" 4459 " [4] = dddddddddd,\n" 4460 " [5] = eeeeeeeeee};"); 4461 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4462 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4463 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4464 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4465 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4466 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4467 } 4468 4469 TEST_F(FormatTest, NestedStaticInitializers) { 4470 verifyFormat("static A x = {{{}}};\n"); 4471 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4472 " {init1, init2, init3, init4}}};", 4473 getLLVMStyleWithColumns(50)); 4474 4475 verifyFormat("somes Status::global_reps[3] = {\n" 4476 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4477 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4478 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4479 getLLVMStyleWithColumns(60)); 4480 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4481 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4482 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4483 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4484 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4485 " {rect.fRight - rect.fLeft, rect.fBottom - " 4486 "rect.fTop}};"); 4487 4488 verifyFormat( 4489 "SomeArrayOfSomeType a = {\n" 4490 " {{1, 2, 3},\n" 4491 " {1, 2, 3},\n" 4492 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4493 " 333333333333333333333333333333},\n" 4494 " {1, 2, 3},\n" 4495 " {1, 2, 3}}};"); 4496 verifyFormat( 4497 "SomeArrayOfSomeType a = {\n" 4498 " {{1, 2, 3}},\n" 4499 " {{1, 2, 3}},\n" 4500 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4501 " 333333333333333333333333333333}},\n" 4502 " {{1, 2, 3}},\n" 4503 " {{1, 2, 3}}};"); 4504 4505 verifyFormat("struct {\n" 4506 " unsigned bit;\n" 4507 " const char *const name;\n" 4508 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4509 " {kOsWin, \"Windows\"},\n" 4510 " {kOsLinux, \"Linux\"},\n" 4511 " {kOsCrOS, \"Chrome OS\"}};"); 4512 verifyFormat("struct {\n" 4513 " unsigned bit;\n" 4514 " const char *const name;\n" 4515 "} kBitsToOs[] = {\n" 4516 " {kOsMac, \"Mac\"},\n" 4517 " {kOsWin, \"Windows\"},\n" 4518 " {kOsLinux, \"Linux\"},\n" 4519 " {kOsCrOS, \"Chrome OS\"},\n" 4520 "};"); 4521 } 4522 4523 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4524 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4525 " \\\n" 4526 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4527 } 4528 4529 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4530 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4531 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4532 4533 // Do break defaulted and deleted functions. 4534 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4535 " default;", 4536 getLLVMStyleWithColumns(40)); 4537 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4538 " delete;", 4539 getLLVMStyleWithColumns(40)); 4540 } 4541 4542 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4543 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4544 getLLVMStyleWithColumns(40)); 4545 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4546 getLLVMStyleWithColumns(40)); 4547 EXPECT_EQ("#define Q \\\n" 4548 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4549 " \"aaaaaaaa.cpp\"", 4550 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4551 getLLVMStyleWithColumns(40))); 4552 } 4553 4554 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4555 EXPECT_EQ("# 123 \"A string literal\"", 4556 format(" # 123 \"A string literal\"")); 4557 } 4558 4559 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4560 EXPECT_EQ("#;", format("#;")); 4561 verifyFormat("#\n;\n;\n;"); 4562 } 4563 4564 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4565 EXPECT_EQ("#line 42 \"test\"\n", 4566 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4567 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4568 getLLVMStyleWithColumns(12))); 4569 } 4570 4571 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4572 EXPECT_EQ("#line 42 \"test\"", 4573 format("# \\\n line \\\n 42 \\\n \"test\"")); 4574 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4575 } 4576 4577 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4578 verifyFormat("#define A \\x20"); 4579 verifyFormat("#define A \\ x20"); 4580 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4581 verifyFormat("#define A ''"); 4582 verifyFormat("#define A ''qqq"); 4583 verifyFormat("#define A `qqq"); 4584 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4585 EXPECT_EQ("const char *c = STRINGIFY(\n" 4586 "\\na : b);", 4587 format("const char * c = STRINGIFY(\n" 4588 "\\na : b);")); 4589 4590 verifyFormat("a\r\\"); 4591 verifyFormat("a\v\\"); 4592 verifyFormat("a\f\\"); 4593 } 4594 4595 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4596 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4597 style.IndentWidth = 4; 4598 style.PPIndentWidth = 1; 4599 4600 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4601 verifyFormat("#ifdef __linux__\n" 4602 "void foo() {\n" 4603 " int x = 0;\n" 4604 "}\n" 4605 "#define FOO\n" 4606 "#endif\n" 4607 "void bar() {\n" 4608 " int y = 0;\n" 4609 "}\n", 4610 style); 4611 4612 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4613 verifyFormat("#ifdef __linux__\n" 4614 "void foo() {\n" 4615 " int x = 0;\n" 4616 "}\n" 4617 "# define FOO foo\n" 4618 "#endif\n" 4619 "void bar() {\n" 4620 " int y = 0;\n" 4621 "}\n", 4622 style); 4623 4624 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4625 verifyFormat("#ifdef __linux__\n" 4626 "void foo() {\n" 4627 " int x = 0;\n" 4628 "}\n" 4629 " #define FOO foo\n" 4630 "#endif\n" 4631 "void bar() {\n" 4632 " int y = 0;\n" 4633 "}\n", 4634 style); 4635 } 4636 4637 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4638 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4639 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4640 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4641 // FIXME: We never break before the macro name. 4642 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4643 4644 verifyFormat("#define A A\n#define A A"); 4645 verifyFormat("#define A(X) A\n#define A A"); 4646 4647 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4648 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4649 } 4650 4651 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4652 EXPECT_EQ("// somecomment\n" 4653 "#include \"a.h\"\n" 4654 "#define A( \\\n" 4655 " A, B)\n" 4656 "#include \"b.h\"\n" 4657 "// somecomment\n", 4658 format(" // somecomment\n" 4659 " #include \"a.h\"\n" 4660 "#define A(A,\\\n" 4661 " B)\n" 4662 " #include \"b.h\"\n" 4663 " // somecomment\n", 4664 getLLVMStyleWithColumns(13))); 4665 } 4666 4667 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4668 4669 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4670 EXPECT_EQ("#define A \\\n" 4671 " c; \\\n" 4672 " e;\n" 4673 "f;", 4674 format("#define A c; e;\n" 4675 "f;", 4676 getLLVMStyleWithColumns(14))); 4677 } 4678 4679 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4680 4681 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4682 EXPECT_EQ("int x,\n" 4683 "#define A\n" 4684 " y;", 4685 format("int x,\n#define A\ny;")); 4686 } 4687 4688 TEST_F(FormatTest, HashInMacroDefinition) { 4689 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4690 EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle())); 4691 EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle())); 4692 EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle())); 4693 EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle())); 4694 EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle())); 4695 EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle())); 4696 EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle())); 4697 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4698 verifyFormat("#define A \\\n" 4699 " { \\\n" 4700 " f(#c); \\\n" 4701 " }", 4702 getLLVMStyleWithColumns(11)); 4703 4704 verifyFormat("#define A(X) \\\n" 4705 " void function##X()", 4706 getLLVMStyleWithColumns(22)); 4707 4708 verifyFormat("#define A(a, b, c) \\\n" 4709 " void a##b##c()", 4710 getLLVMStyleWithColumns(22)); 4711 4712 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4713 } 4714 4715 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4716 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4717 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4718 4719 FormatStyle Style = getLLVMStyle(); 4720 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4721 verifyFormat("#define true ((foo)1)", Style); 4722 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4723 verifyFormat("#define false((foo)0)", Style); 4724 } 4725 4726 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4727 EXPECT_EQ("#define A b;", format("#define A \\\n" 4728 " \\\n" 4729 " b;", 4730 getLLVMStyleWithColumns(25))); 4731 EXPECT_EQ("#define A \\\n" 4732 " \\\n" 4733 " a; \\\n" 4734 " b;", 4735 format("#define A \\\n" 4736 " \\\n" 4737 " a; \\\n" 4738 " b;", 4739 getLLVMStyleWithColumns(11))); 4740 EXPECT_EQ("#define A \\\n" 4741 " a; \\\n" 4742 " \\\n" 4743 " b;", 4744 format("#define A \\\n" 4745 " a; \\\n" 4746 " \\\n" 4747 " b;", 4748 getLLVMStyleWithColumns(11))); 4749 } 4750 4751 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4752 verifyIncompleteFormat("#define A :"); 4753 verifyFormat("#define SOMECASES \\\n" 4754 " case 1: \\\n" 4755 " case 2\n", 4756 getLLVMStyleWithColumns(20)); 4757 verifyFormat("#define MACRO(a) \\\n" 4758 " if (a) \\\n" 4759 " f(); \\\n" 4760 " else \\\n" 4761 " g()", 4762 getLLVMStyleWithColumns(18)); 4763 verifyFormat("#define A template <typename T>"); 4764 verifyIncompleteFormat("#define STR(x) #x\n" 4765 "f(STR(this_is_a_string_literal{));"); 4766 verifyFormat("#pragma omp threadprivate( \\\n" 4767 " y)), // expected-warning", 4768 getLLVMStyleWithColumns(28)); 4769 verifyFormat("#d, = };"); 4770 verifyFormat("#if \"a"); 4771 verifyIncompleteFormat("({\n" 4772 "#define b \\\n" 4773 " } \\\n" 4774 " a\n" 4775 "a", 4776 getLLVMStyleWithColumns(15)); 4777 verifyFormat("#define A \\\n" 4778 " { \\\n" 4779 " {\n" 4780 "#define B \\\n" 4781 " } \\\n" 4782 " }", 4783 getLLVMStyleWithColumns(15)); 4784 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4785 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4786 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4787 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4788 } 4789 4790 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4791 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4792 EXPECT_EQ("class A : public QObject {\n" 4793 " Q_OBJECT\n" 4794 "\n" 4795 " A() {}\n" 4796 "};", 4797 format("class A : public QObject {\n" 4798 " Q_OBJECT\n" 4799 "\n" 4800 " A() {\n}\n" 4801 "} ;")); 4802 EXPECT_EQ("MACRO\n" 4803 "/*static*/ int i;", 4804 format("MACRO\n" 4805 " /*static*/ int i;")); 4806 EXPECT_EQ("SOME_MACRO\n" 4807 "namespace {\n" 4808 "void f();\n" 4809 "} // namespace", 4810 format("SOME_MACRO\n" 4811 " namespace {\n" 4812 "void f( );\n" 4813 "} // namespace")); 4814 // Only if the identifier contains at least 5 characters. 4815 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4816 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4817 // Only if everything is upper case. 4818 EXPECT_EQ("class A : public QObject {\n" 4819 " Q_Object A() {}\n" 4820 "};", 4821 format("class A : public QObject {\n" 4822 " Q_Object\n" 4823 " A() {\n}\n" 4824 "} ;")); 4825 4826 // Only if the next line can actually start an unwrapped line. 4827 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4828 format("SOME_WEIRD_LOG_MACRO\n" 4829 "<< SomeThing;")); 4830 4831 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4832 "(n, buffers))\n", 4833 getChromiumStyle(FormatStyle::LK_Cpp)); 4834 4835 // See PR41483 4836 EXPECT_EQ("/**/ FOO(a)\n" 4837 "FOO(b)", 4838 format("/**/ FOO(a)\n" 4839 "FOO(b)")); 4840 } 4841 4842 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4843 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4844 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4845 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4846 "class X {};\n" 4847 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4848 "int *createScopDetectionPass() { return 0; }", 4849 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4850 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4851 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4852 " class X {};\n" 4853 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4854 " int *createScopDetectionPass() { return 0; }")); 4855 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4856 // braces, so that inner block is indented one level more. 4857 EXPECT_EQ("int q() {\n" 4858 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4859 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4860 " IPC_END_MESSAGE_MAP()\n" 4861 "}", 4862 format("int q() {\n" 4863 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4864 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4865 " IPC_END_MESSAGE_MAP()\n" 4866 "}")); 4867 4868 // Same inside macros. 4869 EXPECT_EQ("#define LIST(L) \\\n" 4870 " L(A) \\\n" 4871 " L(B) \\\n" 4872 " L(C)", 4873 format("#define LIST(L) \\\n" 4874 " L(A) \\\n" 4875 " L(B) \\\n" 4876 " L(C)", 4877 getGoogleStyle())); 4878 4879 // These must not be recognized as macros. 4880 EXPECT_EQ("int q() {\n" 4881 " f(x);\n" 4882 " f(x) {}\n" 4883 " f(x)->g();\n" 4884 " f(x)->*g();\n" 4885 " f(x).g();\n" 4886 " f(x) = x;\n" 4887 " f(x) += x;\n" 4888 " f(x) -= x;\n" 4889 " f(x) *= x;\n" 4890 " f(x) /= x;\n" 4891 " f(x) %= x;\n" 4892 " f(x) &= x;\n" 4893 " f(x) |= x;\n" 4894 " f(x) ^= x;\n" 4895 " f(x) >>= x;\n" 4896 " f(x) <<= x;\n" 4897 " f(x)[y].z();\n" 4898 " LOG(INFO) << x;\n" 4899 " ifstream(x) >> x;\n" 4900 "}\n", 4901 format("int q() {\n" 4902 " f(x)\n;\n" 4903 " f(x)\n {}\n" 4904 " f(x)\n->g();\n" 4905 " f(x)\n->*g();\n" 4906 " f(x)\n.g();\n" 4907 " f(x)\n = x;\n" 4908 " f(x)\n += x;\n" 4909 " f(x)\n -= x;\n" 4910 " f(x)\n *= x;\n" 4911 " f(x)\n /= x;\n" 4912 " f(x)\n %= x;\n" 4913 " f(x)\n &= x;\n" 4914 " f(x)\n |= x;\n" 4915 " f(x)\n ^= x;\n" 4916 " f(x)\n >>= x;\n" 4917 " f(x)\n <<= x;\n" 4918 " f(x)\n[y].z();\n" 4919 " LOG(INFO)\n << x;\n" 4920 " ifstream(x)\n >> x;\n" 4921 "}\n")); 4922 EXPECT_EQ("int q() {\n" 4923 " F(x)\n" 4924 " if (1) {\n" 4925 " }\n" 4926 " F(x)\n" 4927 " while (1) {\n" 4928 " }\n" 4929 " F(x)\n" 4930 " G(x);\n" 4931 " F(x)\n" 4932 " try {\n" 4933 " Q();\n" 4934 " } catch (...) {\n" 4935 " }\n" 4936 "}\n", 4937 format("int q() {\n" 4938 "F(x)\n" 4939 "if (1) {}\n" 4940 "F(x)\n" 4941 "while (1) {}\n" 4942 "F(x)\n" 4943 "G(x);\n" 4944 "F(x)\n" 4945 "try { Q(); } catch (...) {}\n" 4946 "}\n")); 4947 EXPECT_EQ("class A {\n" 4948 " A() : t(0) {}\n" 4949 " A(int i) noexcept() : {}\n" 4950 " A(X x)\n" // FIXME: function-level try blocks are broken. 4951 " try : t(0) {\n" 4952 " } catch (...) {\n" 4953 " }\n" 4954 "};", 4955 format("class A {\n" 4956 " A()\n : t(0) {}\n" 4957 " A(int i)\n noexcept() : {}\n" 4958 " A(X x)\n" 4959 " try : t(0) {} catch (...) {}\n" 4960 "};")); 4961 FormatStyle Style = getLLVMStyle(); 4962 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4963 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 4964 Style.BraceWrapping.AfterFunction = true; 4965 EXPECT_EQ("void f()\n" 4966 "try\n" 4967 "{\n" 4968 "}", 4969 format("void f() try {\n" 4970 "}", 4971 Style)); 4972 EXPECT_EQ("class SomeClass {\n" 4973 "public:\n" 4974 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4975 "};", 4976 format("class SomeClass {\n" 4977 "public:\n" 4978 " SomeClass()\n" 4979 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4980 "};")); 4981 EXPECT_EQ("class SomeClass {\n" 4982 "public:\n" 4983 " SomeClass()\n" 4984 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4985 "};", 4986 format("class SomeClass {\n" 4987 "public:\n" 4988 " SomeClass()\n" 4989 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4990 "};", 4991 getLLVMStyleWithColumns(40))); 4992 4993 verifyFormat("MACRO(>)"); 4994 4995 // Some macros contain an implicit semicolon. 4996 Style = getLLVMStyle(); 4997 Style.StatementMacros.push_back("FOO"); 4998 verifyFormat("FOO(a) int b = 0;"); 4999 verifyFormat("FOO(a)\n" 5000 "int b = 0;", 5001 Style); 5002 verifyFormat("FOO(a);\n" 5003 "int b = 0;", 5004 Style); 5005 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 5006 "int b = 0;", 5007 Style); 5008 verifyFormat("FOO()\n" 5009 "int b = 0;", 5010 Style); 5011 verifyFormat("FOO\n" 5012 "int b = 0;", 5013 Style); 5014 verifyFormat("void f() {\n" 5015 " FOO(a)\n" 5016 " return a;\n" 5017 "}", 5018 Style); 5019 verifyFormat("FOO(a)\n" 5020 "FOO(b)", 5021 Style); 5022 verifyFormat("int a = 0;\n" 5023 "FOO(b)\n" 5024 "int c = 0;", 5025 Style); 5026 verifyFormat("int a = 0;\n" 5027 "int x = FOO(a)\n" 5028 "int b = 0;", 5029 Style); 5030 verifyFormat("void foo(int a) { FOO(a) }\n" 5031 "uint32_t bar() {}", 5032 Style); 5033 } 5034 5035 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) { 5036 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 5037 5038 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()", 5039 ZeroColumn); 5040 } 5041 5042 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 5043 verifyFormat("#define A \\\n" 5044 " f({ \\\n" 5045 " g(); \\\n" 5046 " });", 5047 getLLVMStyleWithColumns(11)); 5048 } 5049 5050 TEST_F(FormatTest, IndentPreprocessorDirectives) { 5051 FormatStyle Style = getLLVMStyleWithColumns(40); 5052 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 5053 verifyFormat("#ifdef _WIN32\n" 5054 "#define A 0\n" 5055 "#ifdef VAR2\n" 5056 "#define B 1\n" 5057 "#include <someheader.h>\n" 5058 "#define MACRO \\\n" 5059 " some_very_long_func_aaaaaaaaaa();\n" 5060 "#endif\n" 5061 "#else\n" 5062 "#define A 1\n" 5063 "#endif", 5064 Style); 5065 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 5066 verifyFormat("#ifdef _WIN32\n" 5067 "# define A 0\n" 5068 "# ifdef VAR2\n" 5069 "# define B 1\n" 5070 "# include <someheader.h>\n" 5071 "# define MACRO \\\n" 5072 " some_very_long_func_aaaaaaaaaa();\n" 5073 "# endif\n" 5074 "#else\n" 5075 "# define A 1\n" 5076 "#endif", 5077 Style); 5078 verifyFormat("#if A\n" 5079 "# define MACRO \\\n" 5080 " void a(int x) { \\\n" 5081 " b(); \\\n" 5082 " c(); \\\n" 5083 " d(); \\\n" 5084 " e(); \\\n" 5085 " f(); \\\n" 5086 " }\n" 5087 "#endif", 5088 Style); 5089 // Comments before include guard. 5090 verifyFormat("// file comment\n" 5091 "// file comment\n" 5092 "#ifndef HEADER_H\n" 5093 "#define HEADER_H\n" 5094 "code();\n" 5095 "#endif", 5096 Style); 5097 // Test with include guards. 5098 verifyFormat("#ifndef HEADER_H\n" 5099 "#define HEADER_H\n" 5100 "code();\n" 5101 "#endif", 5102 Style); 5103 // Include guards must have a #define with the same variable immediately 5104 // after #ifndef. 5105 verifyFormat("#ifndef NOT_GUARD\n" 5106 "# define FOO\n" 5107 "code();\n" 5108 "#endif", 5109 Style); 5110 5111 // Include guards must cover the entire file. 5112 verifyFormat("code();\n" 5113 "code();\n" 5114 "#ifndef NOT_GUARD\n" 5115 "# define NOT_GUARD\n" 5116 "code();\n" 5117 "#endif", 5118 Style); 5119 verifyFormat("#ifndef NOT_GUARD\n" 5120 "# define NOT_GUARD\n" 5121 "code();\n" 5122 "#endif\n" 5123 "code();", 5124 Style); 5125 // Test with trailing blank lines. 5126 verifyFormat("#ifndef HEADER_H\n" 5127 "#define HEADER_H\n" 5128 "code();\n" 5129 "#endif\n", 5130 Style); 5131 // Include guards don't have #else. 5132 verifyFormat("#ifndef NOT_GUARD\n" 5133 "# define NOT_GUARD\n" 5134 "code();\n" 5135 "#else\n" 5136 "#endif", 5137 Style); 5138 verifyFormat("#ifndef NOT_GUARD\n" 5139 "# define NOT_GUARD\n" 5140 "code();\n" 5141 "#elif FOO\n" 5142 "#endif", 5143 Style); 5144 // Non-identifier #define after potential include guard. 5145 verifyFormat("#ifndef FOO\n" 5146 "# define 1\n" 5147 "#endif\n", 5148 Style); 5149 // #if closes past last non-preprocessor line. 5150 verifyFormat("#ifndef FOO\n" 5151 "#define FOO\n" 5152 "#if 1\n" 5153 "int i;\n" 5154 "# define A 0\n" 5155 "#endif\n" 5156 "#endif\n", 5157 Style); 5158 // Don't crash if there is an #elif directive without a condition. 5159 verifyFormat("#if 1\n" 5160 "int x;\n" 5161 "#elif\n" 5162 "int y;\n" 5163 "#else\n" 5164 "int z;\n" 5165 "#endif", 5166 Style); 5167 // FIXME: This doesn't handle the case where there's code between the 5168 // #ifndef and #define but all other conditions hold. This is because when 5169 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 5170 // previous code line yet, so we can't detect it. 5171 EXPECT_EQ("#ifndef NOT_GUARD\n" 5172 "code();\n" 5173 "#define NOT_GUARD\n" 5174 "code();\n" 5175 "#endif", 5176 format("#ifndef NOT_GUARD\n" 5177 "code();\n" 5178 "# define NOT_GUARD\n" 5179 "code();\n" 5180 "#endif", 5181 Style)); 5182 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 5183 // be outside an include guard. Examples are #pragma once and 5184 // #pragma GCC diagnostic, or anything else that does not change the meaning 5185 // of the file if it's included multiple times. 5186 EXPECT_EQ("#ifdef WIN32\n" 5187 "# pragma once\n" 5188 "#endif\n" 5189 "#ifndef HEADER_H\n" 5190 "# define HEADER_H\n" 5191 "code();\n" 5192 "#endif", 5193 format("#ifdef WIN32\n" 5194 "# pragma once\n" 5195 "#endif\n" 5196 "#ifndef HEADER_H\n" 5197 "#define HEADER_H\n" 5198 "code();\n" 5199 "#endif", 5200 Style)); 5201 // FIXME: This does not detect when there is a single non-preprocessor line 5202 // in front of an include-guard-like structure where other conditions hold 5203 // because ScopedLineState hides the line. 5204 EXPECT_EQ("code();\n" 5205 "#ifndef HEADER_H\n" 5206 "#define HEADER_H\n" 5207 "code();\n" 5208 "#endif", 5209 format("code();\n" 5210 "#ifndef HEADER_H\n" 5211 "# define HEADER_H\n" 5212 "code();\n" 5213 "#endif", 5214 Style)); 5215 // Keep comments aligned with #, otherwise indent comments normally. These 5216 // tests cannot use verifyFormat because messUp manipulates leading 5217 // whitespace. 5218 { 5219 const char *Expected = "" 5220 "void f() {\n" 5221 "#if 1\n" 5222 "// Preprocessor aligned.\n" 5223 "# define A 0\n" 5224 " // Code. Separated by blank line.\n" 5225 "\n" 5226 "# define B 0\n" 5227 " // Code. Not aligned with #\n" 5228 "# define C 0\n" 5229 "#endif"; 5230 const char *ToFormat = "" 5231 "void f() {\n" 5232 "#if 1\n" 5233 "// Preprocessor aligned.\n" 5234 "# define A 0\n" 5235 "// Code. Separated by blank line.\n" 5236 "\n" 5237 "# define B 0\n" 5238 " // Code. Not aligned with #\n" 5239 "# define C 0\n" 5240 "#endif"; 5241 EXPECT_EQ(Expected, format(ToFormat, Style)); 5242 EXPECT_EQ(Expected, format(Expected, Style)); 5243 } 5244 // Keep block quotes aligned. 5245 { 5246 const char *Expected = "" 5247 "void f() {\n" 5248 "#if 1\n" 5249 "/* Preprocessor aligned. */\n" 5250 "# define A 0\n" 5251 " /* Code. Separated by blank line. */\n" 5252 "\n" 5253 "# define B 0\n" 5254 " /* Code. Not aligned with # */\n" 5255 "# define C 0\n" 5256 "#endif"; 5257 const char *ToFormat = "" 5258 "void f() {\n" 5259 "#if 1\n" 5260 "/* Preprocessor aligned. */\n" 5261 "# define A 0\n" 5262 "/* Code. Separated by blank line. */\n" 5263 "\n" 5264 "# define B 0\n" 5265 " /* Code. Not aligned with # */\n" 5266 "# define C 0\n" 5267 "#endif"; 5268 EXPECT_EQ(Expected, format(ToFormat, Style)); 5269 EXPECT_EQ(Expected, format(Expected, Style)); 5270 } 5271 // Keep comments aligned with un-indented directives. 5272 { 5273 const char *Expected = "" 5274 "void f() {\n" 5275 "// Preprocessor aligned.\n" 5276 "#define A 0\n" 5277 " // Code. Separated by blank line.\n" 5278 "\n" 5279 "#define B 0\n" 5280 " // Code. Not aligned with #\n" 5281 "#define C 0\n"; 5282 const char *ToFormat = "" 5283 "void f() {\n" 5284 "// Preprocessor aligned.\n" 5285 "#define A 0\n" 5286 "// Code. Separated by blank line.\n" 5287 "\n" 5288 "#define B 0\n" 5289 " // Code. Not aligned with #\n" 5290 "#define C 0\n"; 5291 EXPECT_EQ(Expected, format(ToFormat, Style)); 5292 EXPECT_EQ(Expected, format(Expected, Style)); 5293 } 5294 // Test AfterHash with tabs. 5295 { 5296 FormatStyle Tabbed = Style; 5297 Tabbed.UseTab = FormatStyle::UT_Always; 5298 Tabbed.IndentWidth = 8; 5299 Tabbed.TabWidth = 8; 5300 verifyFormat("#ifdef _WIN32\n" 5301 "#\tdefine A 0\n" 5302 "#\tifdef VAR2\n" 5303 "#\t\tdefine B 1\n" 5304 "#\t\tinclude <someheader.h>\n" 5305 "#\t\tdefine MACRO \\\n" 5306 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 5307 "#\tendif\n" 5308 "#else\n" 5309 "#\tdefine A 1\n" 5310 "#endif", 5311 Tabbed); 5312 } 5313 5314 // Regression test: Multiline-macro inside include guards. 5315 verifyFormat("#ifndef HEADER_H\n" 5316 "#define HEADER_H\n" 5317 "#define A() \\\n" 5318 " int i; \\\n" 5319 " int j;\n" 5320 "#endif // HEADER_H", 5321 getLLVMStyleWithColumns(20)); 5322 5323 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5324 // Basic before hash indent tests 5325 verifyFormat("#ifdef _WIN32\n" 5326 " #define A 0\n" 5327 " #ifdef VAR2\n" 5328 " #define B 1\n" 5329 " #include <someheader.h>\n" 5330 " #define MACRO \\\n" 5331 " some_very_long_func_aaaaaaaaaa();\n" 5332 " #endif\n" 5333 "#else\n" 5334 " #define A 1\n" 5335 "#endif", 5336 Style); 5337 verifyFormat("#if A\n" 5338 " #define MACRO \\\n" 5339 " void a(int x) { \\\n" 5340 " b(); \\\n" 5341 " c(); \\\n" 5342 " d(); \\\n" 5343 " e(); \\\n" 5344 " f(); \\\n" 5345 " }\n" 5346 "#endif", 5347 Style); 5348 // Keep comments aligned with indented directives. These 5349 // tests cannot use verifyFormat because messUp manipulates leading 5350 // whitespace. 5351 { 5352 const char *Expected = "void f() {\n" 5353 "// Aligned to preprocessor.\n" 5354 "#if 1\n" 5355 " // Aligned to code.\n" 5356 " int a;\n" 5357 " #if 1\n" 5358 " // Aligned to preprocessor.\n" 5359 " #define A 0\n" 5360 " // Aligned to code.\n" 5361 " int b;\n" 5362 " #endif\n" 5363 "#endif\n" 5364 "}"; 5365 const char *ToFormat = "void f() {\n" 5366 "// Aligned to preprocessor.\n" 5367 "#if 1\n" 5368 "// Aligned to code.\n" 5369 "int a;\n" 5370 "#if 1\n" 5371 "// Aligned to preprocessor.\n" 5372 "#define A 0\n" 5373 "// Aligned to code.\n" 5374 "int b;\n" 5375 "#endif\n" 5376 "#endif\n" 5377 "}"; 5378 EXPECT_EQ(Expected, format(ToFormat, Style)); 5379 EXPECT_EQ(Expected, format(Expected, Style)); 5380 } 5381 { 5382 const char *Expected = "void f() {\n" 5383 "/* Aligned to preprocessor. */\n" 5384 "#if 1\n" 5385 " /* Aligned to code. */\n" 5386 " int a;\n" 5387 " #if 1\n" 5388 " /* Aligned to preprocessor. */\n" 5389 " #define A 0\n" 5390 " /* Aligned to code. */\n" 5391 " int b;\n" 5392 " #endif\n" 5393 "#endif\n" 5394 "}"; 5395 const char *ToFormat = "void f() {\n" 5396 "/* Aligned to preprocessor. */\n" 5397 "#if 1\n" 5398 "/* Aligned to code. */\n" 5399 "int a;\n" 5400 "#if 1\n" 5401 "/* Aligned to preprocessor. */\n" 5402 "#define A 0\n" 5403 "/* Aligned to code. */\n" 5404 "int b;\n" 5405 "#endif\n" 5406 "#endif\n" 5407 "}"; 5408 EXPECT_EQ(Expected, format(ToFormat, Style)); 5409 EXPECT_EQ(Expected, format(Expected, Style)); 5410 } 5411 5412 // Test single comment before preprocessor 5413 verifyFormat("// Comment\n" 5414 "\n" 5415 "#if 1\n" 5416 "#endif", 5417 Style); 5418 } 5419 5420 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5421 verifyFormat("{\n { a #c; }\n}"); 5422 } 5423 5424 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5425 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5426 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5427 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5428 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5429 } 5430 5431 TEST_F(FormatTest, EscapedNewlines) { 5432 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5433 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5434 format("#define A \\\nint i;\\\n int j;", Narrow)); 5435 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5436 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5437 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5438 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5439 5440 FormatStyle AlignLeft = getLLVMStyle(); 5441 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5442 EXPECT_EQ("#define MACRO(x) \\\n" 5443 "private: \\\n" 5444 " int x(int a);\n", 5445 format("#define MACRO(x) \\\n" 5446 "private: \\\n" 5447 " int x(int a);\n", 5448 AlignLeft)); 5449 5450 // CRLF line endings 5451 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5452 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5453 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5454 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5455 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5456 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5457 EXPECT_EQ("#define MACRO(x) \\\r\n" 5458 "private: \\\r\n" 5459 " int x(int a);\r\n", 5460 format("#define MACRO(x) \\\r\n" 5461 "private: \\\r\n" 5462 " int x(int a);\r\n", 5463 AlignLeft)); 5464 5465 FormatStyle DontAlign = getLLVMStyle(); 5466 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5467 DontAlign.MaxEmptyLinesToKeep = 3; 5468 // FIXME: can't use verifyFormat here because the newline before 5469 // "public:" is not inserted the first time it's reformatted 5470 EXPECT_EQ("#define A \\\n" 5471 " class Foo { \\\n" 5472 " void bar(); \\\n" 5473 "\\\n" 5474 "\\\n" 5475 "\\\n" 5476 " public: \\\n" 5477 " void baz(); \\\n" 5478 " };", 5479 format("#define A \\\n" 5480 " class Foo { \\\n" 5481 " void bar(); \\\n" 5482 "\\\n" 5483 "\\\n" 5484 "\\\n" 5485 " public: \\\n" 5486 " void baz(); \\\n" 5487 " };", 5488 DontAlign)); 5489 } 5490 5491 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5492 verifyFormat("#define A \\\n" 5493 " int v( \\\n" 5494 " a); \\\n" 5495 " int i;", 5496 getLLVMStyleWithColumns(11)); 5497 } 5498 5499 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5500 EXPECT_EQ( 5501 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5502 " \\\n" 5503 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5504 "\n" 5505 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5506 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5507 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5508 "\\\n" 5509 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5510 " \n" 5511 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5512 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5513 } 5514 5515 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5516 EXPECT_EQ("int\n" 5517 "#define A\n" 5518 " a;", 5519 format("int\n#define A\na;")); 5520 verifyFormat("functionCallTo(\n" 5521 " someOtherFunction(\n" 5522 " withSomeParameters, whichInSequence,\n" 5523 " areLongerThanALine(andAnotherCall,\n" 5524 "#define A B\n" 5525 " withMoreParamters,\n" 5526 " whichStronglyInfluenceTheLayout),\n" 5527 " andMoreParameters),\n" 5528 " trailing);", 5529 getLLVMStyleWithColumns(69)); 5530 verifyFormat("Foo::Foo()\n" 5531 "#ifdef BAR\n" 5532 " : baz(0)\n" 5533 "#endif\n" 5534 "{\n" 5535 "}"); 5536 verifyFormat("void f() {\n" 5537 " if (true)\n" 5538 "#ifdef A\n" 5539 " f(42);\n" 5540 " x();\n" 5541 "#else\n" 5542 " g();\n" 5543 " x();\n" 5544 "#endif\n" 5545 "}"); 5546 verifyFormat("void f(param1, param2,\n" 5547 " param3,\n" 5548 "#ifdef A\n" 5549 " param4(param5,\n" 5550 "#ifdef A1\n" 5551 " param6,\n" 5552 "#ifdef A2\n" 5553 " param7),\n" 5554 "#else\n" 5555 " param8),\n" 5556 " param9,\n" 5557 "#endif\n" 5558 " param10,\n" 5559 "#endif\n" 5560 " param11)\n" 5561 "#else\n" 5562 " param12)\n" 5563 "#endif\n" 5564 "{\n" 5565 " x();\n" 5566 "}", 5567 getLLVMStyleWithColumns(28)); 5568 verifyFormat("#if 1\n" 5569 "int i;"); 5570 verifyFormat("#if 1\n" 5571 "#endif\n" 5572 "#if 1\n" 5573 "#else\n" 5574 "#endif\n"); 5575 verifyFormat("DEBUG({\n" 5576 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5578 "});\n" 5579 "#if a\n" 5580 "#else\n" 5581 "#endif"); 5582 5583 verifyIncompleteFormat("void f(\n" 5584 "#if A\n" 5585 ");\n" 5586 "#else\n" 5587 "#endif"); 5588 } 5589 5590 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5591 verifyFormat("#endif\n" 5592 "#if B"); 5593 } 5594 5595 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5596 FormatStyle SingleLine = getLLVMStyle(); 5597 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5598 verifyFormat("#if 0\n" 5599 "#elif 1\n" 5600 "#endif\n" 5601 "void foo() {\n" 5602 " if (test) foo2();\n" 5603 "}", 5604 SingleLine); 5605 } 5606 5607 TEST_F(FormatTest, LayoutBlockInsideParens) { 5608 verifyFormat("functionCall({ int i; });"); 5609 verifyFormat("functionCall({\n" 5610 " int i;\n" 5611 " int j;\n" 5612 "});"); 5613 verifyFormat("functionCall(\n" 5614 " {\n" 5615 " int i;\n" 5616 " int j;\n" 5617 " },\n" 5618 " aaaa, bbbb, cccc);"); 5619 verifyFormat("functionA(functionB({\n" 5620 " int i;\n" 5621 " int j;\n" 5622 " }),\n" 5623 " aaaa, bbbb, cccc);"); 5624 verifyFormat("functionCall(\n" 5625 " {\n" 5626 " int i;\n" 5627 " int j;\n" 5628 " },\n" 5629 " aaaa, bbbb, // comment\n" 5630 " cccc);"); 5631 verifyFormat("functionA(functionB({\n" 5632 " int i;\n" 5633 " int j;\n" 5634 " }),\n" 5635 " aaaa, bbbb, // comment\n" 5636 " cccc);"); 5637 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5638 verifyFormat("functionCall(aaaa, bbbb, {\n" 5639 " int i;\n" 5640 " int j;\n" 5641 "});"); 5642 verifyFormat( 5643 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5644 " {\n" 5645 " int i; // break\n" 5646 " },\n" 5647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5648 " ccccccccccccccccc));"); 5649 verifyFormat("DEBUG({\n" 5650 " if (a)\n" 5651 " f();\n" 5652 "});"); 5653 } 5654 5655 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5656 EXPECT_EQ("SOME_MACRO { int i; }\n" 5657 "int i;", 5658 format(" SOME_MACRO {int i;} int i;")); 5659 } 5660 5661 TEST_F(FormatTest, LayoutNestedBlocks) { 5662 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5663 " struct s {\n" 5664 " int i;\n" 5665 " };\n" 5666 " s kBitsToOs[] = {{10}};\n" 5667 " for (int i = 0; i < 10; ++i)\n" 5668 " return;\n" 5669 "}"); 5670 verifyFormat("call(parameter, {\n" 5671 " something();\n" 5672 " // Comment using all columns.\n" 5673 " somethingelse();\n" 5674 "});", 5675 getLLVMStyleWithColumns(40)); 5676 verifyFormat("DEBUG( //\n" 5677 " { f(); }, a);"); 5678 verifyFormat("DEBUG( //\n" 5679 " {\n" 5680 " f(); //\n" 5681 " },\n" 5682 " a);"); 5683 5684 EXPECT_EQ("call(parameter, {\n" 5685 " something();\n" 5686 " // Comment too\n" 5687 " // looooooooooong.\n" 5688 " somethingElse();\n" 5689 "});", 5690 format("call(parameter, {\n" 5691 " something();\n" 5692 " // Comment too looooooooooong.\n" 5693 " somethingElse();\n" 5694 "});", 5695 getLLVMStyleWithColumns(29))); 5696 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5697 EXPECT_EQ("DEBUG({ // comment\n" 5698 " int i;\n" 5699 "});", 5700 format("DEBUG({ // comment\n" 5701 "int i;\n" 5702 "});")); 5703 EXPECT_EQ("DEBUG({\n" 5704 " int i;\n" 5705 "\n" 5706 " // comment\n" 5707 " int j;\n" 5708 "});", 5709 format("DEBUG({\n" 5710 " int i;\n" 5711 "\n" 5712 " // comment\n" 5713 " int j;\n" 5714 "});")); 5715 5716 verifyFormat("DEBUG({\n" 5717 " if (a)\n" 5718 " return;\n" 5719 "});"); 5720 verifyGoogleFormat("DEBUG({\n" 5721 " if (a) return;\n" 5722 "});"); 5723 FormatStyle Style = getGoogleStyle(); 5724 Style.ColumnLimit = 45; 5725 verifyFormat("Debug(\n" 5726 " aaaaa,\n" 5727 " {\n" 5728 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5729 " },\n" 5730 " a);", 5731 Style); 5732 5733 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5734 5735 verifyNoCrash("^{v^{a}}"); 5736 } 5737 5738 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5739 EXPECT_EQ("#define MACRO() \\\n" 5740 " Debug(aaa, /* force line break */ \\\n" 5741 " { \\\n" 5742 " int i; \\\n" 5743 " int j; \\\n" 5744 " })", 5745 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5746 " { int i; int j; })", 5747 getGoogleStyle())); 5748 5749 EXPECT_EQ("#define A \\\n" 5750 " [] { \\\n" 5751 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5752 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5753 " }", 5754 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5755 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5756 getGoogleStyle())); 5757 } 5758 5759 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5760 EXPECT_EQ("{}", format("{}")); 5761 verifyFormat("enum E {};"); 5762 verifyFormat("enum E {}"); 5763 FormatStyle Style = getLLVMStyle(); 5764 Style.SpaceInEmptyBlock = true; 5765 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5766 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5767 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5768 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5769 Style.BraceWrapping.BeforeElse = false; 5770 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5771 verifyFormat("if (a)\n" 5772 "{\n" 5773 "} else if (b)\n" 5774 "{\n" 5775 "} else\n" 5776 "{ }", 5777 Style); 5778 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 5779 verifyFormat("if (a) {\n" 5780 "} else if (b) {\n" 5781 "} else {\n" 5782 "}", 5783 Style); 5784 Style.BraceWrapping.BeforeElse = true; 5785 verifyFormat("if (a) { }\n" 5786 "else if (b) { }\n" 5787 "else { }", 5788 Style); 5789 } 5790 5791 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5792 FormatStyle Style = getLLVMStyle(); 5793 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5794 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5795 verifyFormat("FOO_BEGIN\n" 5796 " FOO_ENTRY\n" 5797 "FOO_END", 5798 Style); 5799 verifyFormat("FOO_BEGIN\n" 5800 " NESTED_FOO_BEGIN\n" 5801 " NESTED_FOO_ENTRY\n" 5802 " NESTED_FOO_END\n" 5803 "FOO_END", 5804 Style); 5805 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5806 " int x;\n" 5807 " x = 1;\n" 5808 "FOO_END(Baz)", 5809 Style); 5810 } 5811 5812 //===----------------------------------------------------------------------===// 5813 // Line break tests. 5814 //===----------------------------------------------------------------------===// 5815 5816 TEST_F(FormatTest, PreventConfusingIndents) { 5817 verifyFormat( 5818 "void f() {\n" 5819 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5820 " parameter, parameter, parameter)),\n" 5821 " SecondLongCall(parameter));\n" 5822 "}"); 5823 verifyFormat( 5824 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5825 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5826 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5827 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5828 verifyFormat( 5829 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5830 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5831 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5832 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5833 verifyFormat( 5834 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5835 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5836 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5837 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5838 verifyFormat("int a = bbbb && ccc &&\n" 5839 " fffff(\n" 5840 "#define A Just forcing a new line\n" 5841 " ddd);"); 5842 } 5843 5844 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5845 verifyFormat( 5846 "bool aaaaaaa =\n" 5847 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5848 " bbbbbbbb();"); 5849 verifyFormat( 5850 "bool aaaaaaa =\n" 5851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5852 " bbbbbbbb();"); 5853 5854 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5856 " ccccccccc == ddddddddddd;"); 5857 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5859 " ccccccccc == ddddddddddd;"); 5860 verifyFormat( 5861 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5863 " ccccccccc == ddddddddddd;"); 5864 5865 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5866 " aaaaaa) &&\n" 5867 " bbbbbb && cccccc;"); 5868 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5869 " aaaaaa) >>\n" 5870 " bbbbbb;"); 5871 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5872 " SourceMgr.getSpellingColumnNumber(\n" 5873 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5874 " 1);"); 5875 5876 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5877 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5878 " cccccc) {\n}"); 5879 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5880 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5881 " cccccc) {\n}"); 5882 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5883 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5884 " cccccc) {\n}"); 5885 verifyFormat("b = a &&\n" 5886 " // Comment\n" 5887 " b.c && d;"); 5888 5889 // If the LHS of a comparison is not a binary expression itself, the 5890 // additional linebreak confuses many people. 5891 verifyFormat( 5892 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5894 "}"); 5895 verifyFormat( 5896 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5898 "}"); 5899 verifyFormat( 5900 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5901 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5902 "}"); 5903 verifyFormat( 5904 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5906 "}"); 5907 // Even explicit parentheses stress the precedence enough to make the 5908 // additional break unnecessary. 5909 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5911 "}"); 5912 // This cases is borderline, but with the indentation it is still readable. 5913 verifyFormat( 5914 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5915 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5917 "}", 5918 getLLVMStyleWithColumns(75)); 5919 5920 // If the LHS is a binary expression, we should still use the additional break 5921 // as otherwise the formatting hides the operator precedence. 5922 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5924 " 5) {\n" 5925 "}"); 5926 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5927 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5928 " 5) {\n" 5929 "}"); 5930 5931 FormatStyle OnePerLine = getLLVMStyle(); 5932 OnePerLine.BinPackParameters = false; 5933 verifyFormat( 5934 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5936 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5937 OnePerLine); 5938 5939 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5940 " .aaa(aaaaaaaaaaaaa) *\n" 5941 " aaaaaaa +\n" 5942 " aaaaaaa;", 5943 getLLVMStyleWithColumns(40)); 5944 } 5945 5946 TEST_F(FormatTest, ExpressionIndentation) { 5947 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5948 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5949 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5950 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5951 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5952 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5954 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5955 " ccccccccccccccccccccccccccccccccccccccccc;"); 5956 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5957 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5958 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5959 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5960 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5961 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5963 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5964 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5966 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5967 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5968 verifyFormat("if () {\n" 5969 "} else if (aaaaa && bbbbb > // break\n" 5970 " ccccc) {\n" 5971 "}"); 5972 verifyFormat("if () {\n" 5973 "} else if constexpr (aaaaa && bbbbb > // break\n" 5974 " ccccc) {\n" 5975 "}"); 5976 verifyFormat("if () {\n" 5977 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 5978 " ccccc) {\n" 5979 "}"); 5980 verifyFormat("if () {\n" 5981 "} else if (aaaaa &&\n" 5982 " bbbbb > // break\n" 5983 " ccccc &&\n" 5984 " ddddd) {\n" 5985 "}"); 5986 5987 // Presence of a trailing comment used to change indentation of b. 5988 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 5989 " b;\n" 5990 "return aaaaaaaaaaaaaaaaaaa +\n" 5991 " b; //", 5992 getLLVMStyleWithColumns(30)); 5993 } 5994 5995 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 5996 // Not sure what the best system is here. Like this, the LHS can be found 5997 // immediately above an operator (everything with the same or a higher 5998 // indent). The RHS is aligned right of the operator and so compasses 5999 // everything until something with the same indent as the operator is found. 6000 // FIXME: Is this a good system? 6001 FormatStyle Style = getLLVMStyle(); 6002 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6003 verifyFormat( 6004 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6005 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6006 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6007 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6008 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6009 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6010 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6011 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6012 " > ccccccccccccccccccccccccccccccccccccccccc;", 6013 Style); 6014 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6015 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6016 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6017 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6018 Style); 6019 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6020 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6021 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6022 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6023 Style); 6024 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6025 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6026 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6027 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6028 Style); 6029 verifyFormat("if () {\n" 6030 "} else if (aaaaa\n" 6031 " && bbbbb // break\n" 6032 " > ccccc) {\n" 6033 "}", 6034 Style); 6035 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6036 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6037 Style); 6038 verifyFormat("return (a)\n" 6039 " // comment\n" 6040 " + b;", 6041 Style); 6042 verifyFormat( 6043 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6044 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6045 " + cc;", 6046 Style); 6047 6048 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6049 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6050 Style); 6051 6052 // Forced by comments. 6053 verifyFormat( 6054 "unsigned ContentSize =\n" 6055 " sizeof(int16_t) // DWARF ARange version number\n" 6056 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6057 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6058 " + sizeof(int8_t); // Segment Size (in bytes)"); 6059 6060 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6061 " == boost::fusion::at_c<1>(iiii).second;", 6062 Style); 6063 6064 Style.ColumnLimit = 60; 6065 verifyFormat("zzzzzzzzzz\n" 6066 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6067 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6068 Style); 6069 6070 Style.ColumnLimit = 80; 6071 Style.IndentWidth = 4; 6072 Style.TabWidth = 4; 6073 Style.UseTab = FormatStyle::UT_Always; 6074 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6075 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6076 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 6077 "\t&& (someOtherLongishConditionPart1\n" 6078 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 6079 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 6080 "(someOtherLongishConditionPart1 || " 6081 "someOtherEvenLongerNestedConditionPart2);", 6082 Style)); 6083 } 6084 6085 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 6086 FormatStyle Style = getLLVMStyle(); 6087 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6088 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 6089 6090 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6091 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6092 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6093 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6094 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6095 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6096 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6097 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6098 " > ccccccccccccccccccccccccccccccccccccccccc;", 6099 Style); 6100 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6101 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6102 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6103 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6104 Style); 6105 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6106 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6107 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6108 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6109 Style); 6110 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6111 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6112 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6113 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6114 Style); 6115 verifyFormat("if () {\n" 6116 "} else if (aaaaa\n" 6117 " && bbbbb // break\n" 6118 " > ccccc) {\n" 6119 "}", 6120 Style); 6121 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6122 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6123 Style); 6124 verifyFormat("return (a)\n" 6125 " // comment\n" 6126 " + b;", 6127 Style); 6128 verifyFormat( 6129 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6130 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6131 " + cc;", 6132 Style); 6133 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6134 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6135 " : 3333333333333333;", 6136 Style); 6137 verifyFormat( 6138 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6139 " : ccccccccccccccc ? dddddddddddddddddd\n" 6140 " : eeeeeeeeeeeeeeeeee)\n" 6141 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6142 " : 3333333333333333;", 6143 Style); 6144 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6145 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6146 Style); 6147 6148 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6149 " == boost::fusion::at_c<1>(iiii).second;", 6150 Style); 6151 6152 Style.ColumnLimit = 60; 6153 verifyFormat("zzzzzzzzzzzzz\n" 6154 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6155 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6156 Style); 6157 6158 // Forced by comments. 6159 Style.ColumnLimit = 80; 6160 verifyFormat( 6161 "unsigned ContentSize\n" 6162 " = sizeof(int16_t) // DWARF ARange version number\n" 6163 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6164 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6165 " + sizeof(int8_t); // Segment Size (in bytes)", 6166 Style); 6167 6168 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6169 verifyFormat( 6170 "unsigned ContentSize =\n" 6171 " sizeof(int16_t) // DWARF ARange version number\n" 6172 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6173 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6174 " + sizeof(int8_t); // Segment Size (in bytes)", 6175 Style); 6176 6177 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6178 verifyFormat( 6179 "unsigned ContentSize =\n" 6180 " sizeof(int16_t) // DWARF ARange version number\n" 6181 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6182 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6183 " + sizeof(int8_t); // Segment Size (in bytes)", 6184 Style); 6185 } 6186 6187 TEST_F(FormatTest, EnforcedOperatorWraps) { 6188 // Here we'd like to wrap after the || operators, but a comment is forcing an 6189 // earlier wrap. 6190 verifyFormat("bool x = aaaaa //\n" 6191 " || bbbbb\n" 6192 " //\n" 6193 " || cccc;"); 6194 } 6195 6196 TEST_F(FormatTest, NoOperandAlignment) { 6197 FormatStyle Style = getLLVMStyle(); 6198 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6199 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 6200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6202 Style); 6203 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6204 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6205 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6206 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6207 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6208 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6209 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6210 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6211 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6212 " > ccccccccccccccccccccccccccccccccccccccccc;", 6213 Style); 6214 6215 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6216 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6217 " + cc;", 6218 Style); 6219 verifyFormat("int a = aa\n" 6220 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6221 " * cccccccccccccccccccccccccccccccccccc;\n", 6222 Style); 6223 6224 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6225 verifyFormat("return (a > b\n" 6226 " // comment1\n" 6227 " // comment2\n" 6228 " || c);", 6229 Style); 6230 } 6231 6232 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 6233 FormatStyle Style = getLLVMStyle(); 6234 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6235 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6236 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6237 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6238 Style); 6239 } 6240 6241 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 6242 FormatStyle Style = getLLVMStyleWithColumns(40); 6243 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6244 Style.BinPackArguments = false; 6245 verifyFormat("void test() {\n" 6246 " someFunction(\n" 6247 " this + argument + is + quite\n" 6248 " + long + so + it + gets + wrapped\n" 6249 " + but + remains + bin - packed);\n" 6250 "}", 6251 Style); 6252 verifyFormat("void test() {\n" 6253 " someFunction(arg1,\n" 6254 " this + argument + is\n" 6255 " + quite + long + so\n" 6256 " + it + gets + wrapped\n" 6257 " + but + remains + bin\n" 6258 " - packed,\n" 6259 " arg3);\n" 6260 "}", 6261 Style); 6262 verifyFormat("void test() {\n" 6263 " someFunction(\n" 6264 " arg1,\n" 6265 " this + argument + has\n" 6266 " + anotherFunc(nested,\n" 6267 " calls + whose\n" 6268 " + arguments\n" 6269 " + are + also\n" 6270 " + wrapped,\n" 6271 " in + addition)\n" 6272 " + to + being + bin - packed,\n" 6273 " arg3);\n" 6274 "}", 6275 Style); 6276 6277 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6278 verifyFormat("void test() {\n" 6279 " someFunction(\n" 6280 " arg1,\n" 6281 " this + argument + has +\n" 6282 " anotherFunc(nested,\n" 6283 " calls + whose +\n" 6284 " arguments +\n" 6285 " are + also +\n" 6286 " wrapped,\n" 6287 " in + addition) +\n" 6288 " to + being + bin - packed,\n" 6289 " arg3);\n" 6290 "}", 6291 Style); 6292 } 6293 6294 TEST_F(FormatTest, ConstructorInitializers) { 6295 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6296 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 6297 getLLVMStyleWithColumns(45)); 6298 verifyFormat("Constructor()\n" 6299 " : Inttializer(FitsOnTheLine) {}", 6300 getLLVMStyleWithColumns(44)); 6301 verifyFormat("Constructor()\n" 6302 " : Inttializer(FitsOnTheLine) {}", 6303 getLLVMStyleWithColumns(43)); 6304 6305 verifyFormat("template <typename T>\n" 6306 "Constructor() : Initializer(FitsOnTheLine) {}", 6307 getLLVMStyleWithColumns(45)); 6308 6309 verifyFormat( 6310 "SomeClass::Constructor()\n" 6311 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6312 6313 verifyFormat( 6314 "SomeClass::Constructor()\n" 6315 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6316 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 6317 verifyFormat( 6318 "SomeClass::Constructor()\n" 6319 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6320 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6321 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6322 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6323 " : aaaaaaaaaa(aaaaaa) {}"); 6324 6325 verifyFormat("Constructor()\n" 6326 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6327 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6328 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6329 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 6330 6331 verifyFormat("Constructor()\n" 6332 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6334 6335 verifyFormat("Constructor(int Parameter = 0)\n" 6336 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6337 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 6338 verifyFormat("Constructor()\n" 6339 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6340 "}", 6341 getLLVMStyleWithColumns(60)); 6342 verifyFormat("Constructor()\n" 6343 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6344 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 6345 6346 // Here a line could be saved by splitting the second initializer onto two 6347 // lines, but that is not desirable. 6348 verifyFormat("Constructor()\n" 6349 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6350 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6351 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6352 6353 FormatStyle OnePerLine = getLLVMStyle(); 6354 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 6355 verifyFormat("MyClass::MyClass()\n" 6356 " : a(a),\n" 6357 " b(b),\n" 6358 " c(c) {}", 6359 OnePerLine); 6360 verifyFormat("MyClass::MyClass()\n" 6361 " : a(a), // comment\n" 6362 " b(b),\n" 6363 " c(c) {}", 6364 OnePerLine); 6365 verifyFormat("MyClass::MyClass(int a)\n" 6366 " : b(a), // comment\n" 6367 " c(a + 1) { // lined up\n" 6368 "}", 6369 OnePerLine); 6370 verifyFormat("Constructor()\n" 6371 " : a(b, b, b) {}", 6372 OnePerLine); 6373 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6374 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 6375 verifyFormat("SomeClass::Constructor()\n" 6376 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6377 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6378 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6379 OnePerLine); 6380 verifyFormat("SomeClass::Constructor()\n" 6381 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6382 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6383 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6384 OnePerLine); 6385 verifyFormat("MyClass::MyClass(int var)\n" 6386 " : some_var_(var), // 4 space indent\n" 6387 " some_other_var_(var + 1) { // lined up\n" 6388 "}", 6389 OnePerLine); 6390 verifyFormat("Constructor()\n" 6391 " : aaaaa(aaaaaa),\n" 6392 " aaaaa(aaaaaa),\n" 6393 " aaaaa(aaaaaa),\n" 6394 " aaaaa(aaaaaa),\n" 6395 " aaaaa(aaaaaa) {}", 6396 OnePerLine); 6397 verifyFormat("Constructor()\n" 6398 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6399 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6400 OnePerLine); 6401 OnePerLine.BinPackParameters = false; 6402 verifyFormat( 6403 "Constructor()\n" 6404 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6405 " aaaaaaaaaaa().aaa(),\n" 6406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6407 OnePerLine); 6408 OnePerLine.ColumnLimit = 60; 6409 verifyFormat("Constructor()\n" 6410 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6411 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6412 OnePerLine); 6413 6414 EXPECT_EQ("Constructor()\n" 6415 " : // Comment forcing unwanted break.\n" 6416 " aaaa(aaaa) {}", 6417 format("Constructor() :\n" 6418 " // Comment forcing unwanted break.\n" 6419 " aaaa(aaaa) {}")); 6420 } 6421 6422 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6423 FormatStyle Style = getLLVMStyleWithColumns(60); 6424 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6425 Style.BinPackParameters = false; 6426 6427 for (int i = 0; i < 4; ++i) { 6428 // Test all combinations of parameters that should not have an effect. 6429 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6430 Style.AllowAllArgumentsOnNextLine = i & 2; 6431 6432 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6433 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6434 verifyFormat("Constructor()\n" 6435 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6436 Style); 6437 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6438 6439 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6440 verifyFormat("Constructor()\n" 6441 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6442 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6443 Style); 6444 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6445 6446 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6447 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6448 verifyFormat("Constructor()\n" 6449 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6450 Style); 6451 6452 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6453 verifyFormat("Constructor()\n" 6454 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6455 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6456 Style); 6457 6458 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6459 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6460 verifyFormat("Constructor() :\n" 6461 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6462 Style); 6463 6464 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6465 verifyFormat("Constructor() :\n" 6466 " aaaaaaaaaaaaaaaaaa(a),\n" 6467 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6468 Style); 6469 } 6470 6471 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6472 // AllowAllConstructorInitializersOnNextLine in all 6473 // BreakConstructorInitializers modes 6474 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6475 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6476 verifyFormat("SomeClassWithALongName::Constructor(\n" 6477 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6478 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6479 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6480 Style); 6481 6482 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6483 verifyFormat("SomeClassWithALongName::Constructor(\n" 6484 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6485 " int bbbbbbbbbbbbb,\n" 6486 " int cccccccccccccccc)\n" 6487 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6488 Style); 6489 6490 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6491 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6492 verifyFormat("SomeClassWithALongName::Constructor(\n" 6493 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6494 " int bbbbbbbbbbbbb)\n" 6495 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6496 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6497 Style); 6498 6499 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6500 6501 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6502 verifyFormat("SomeClassWithALongName::Constructor(\n" 6503 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6504 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6505 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6506 Style); 6507 6508 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6509 verifyFormat("SomeClassWithALongName::Constructor(\n" 6510 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6511 " int bbbbbbbbbbbbb,\n" 6512 " int cccccccccccccccc)\n" 6513 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6514 Style); 6515 6516 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6517 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6518 verifyFormat("SomeClassWithALongName::Constructor(\n" 6519 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6520 " int bbbbbbbbbbbbb)\n" 6521 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6522 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6523 Style); 6524 6525 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6526 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6527 verifyFormat("SomeClassWithALongName::Constructor(\n" 6528 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6529 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6530 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6531 Style); 6532 6533 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6534 verifyFormat("SomeClassWithALongName::Constructor(\n" 6535 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6536 " int bbbbbbbbbbbbb,\n" 6537 " int cccccccccccccccc) :\n" 6538 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6539 Style); 6540 6541 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6542 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6543 verifyFormat("SomeClassWithALongName::Constructor(\n" 6544 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6545 " int bbbbbbbbbbbbb) :\n" 6546 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6547 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6548 Style); 6549 } 6550 6551 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6552 FormatStyle Style = getLLVMStyleWithColumns(60); 6553 Style.BinPackArguments = false; 6554 for (int i = 0; i < 4; ++i) { 6555 // Test all combinations of parameters that should not have an effect. 6556 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6557 Style.PackConstructorInitializers = 6558 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6559 6560 Style.AllowAllArgumentsOnNextLine = true; 6561 verifyFormat("void foo() {\n" 6562 " FunctionCallWithReallyLongName(\n" 6563 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6564 "}", 6565 Style); 6566 Style.AllowAllArgumentsOnNextLine = false; 6567 verifyFormat("void foo() {\n" 6568 " FunctionCallWithReallyLongName(\n" 6569 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6570 " bbbbbbbbbbbb);\n" 6571 "}", 6572 Style); 6573 6574 Style.AllowAllArgumentsOnNextLine = true; 6575 verifyFormat("void foo() {\n" 6576 " auto VariableWithReallyLongName = {\n" 6577 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6578 "}", 6579 Style); 6580 Style.AllowAllArgumentsOnNextLine = false; 6581 verifyFormat("void foo() {\n" 6582 " auto VariableWithReallyLongName = {\n" 6583 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6584 " bbbbbbbbbbbb};\n" 6585 "}", 6586 Style); 6587 } 6588 6589 // This parameter should not affect declarations. 6590 Style.BinPackParameters = false; 6591 Style.AllowAllArgumentsOnNextLine = false; 6592 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6593 verifyFormat("void FunctionCallWithReallyLongName(\n" 6594 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6595 Style); 6596 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6597 verifyFormat("void FunctionCallWithReallyLongName(\n" 6598 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6599 " int bbbbbbbbbbbb);", 6600 Style); 6601 } 6602 6603 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6604 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6605 // and BAS_Align. 6606 FormatStyle Style = getLLVMStyleWithColumns(35); 6607 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6608 "void functionDecl(int A, int B, int C);"; 6609 Style.AllowAllArgumentsOnNextLine = false; 6610 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6611 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6612 " paramC);\n" 6613 "void functionDecl(int A, int B,\n" 6614 " int C);"), 6615 format(Input, Style)); 6616 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6617 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6618 " paramC);\n" 6619 "void functionDecl(int A, int B,\n" 6620 " int C);"), 6621 format(Input, Style)); 6622 // However, BAS_AlwaysBreak should take precedence over 6623 // AllowAllArgumentsOnNextLine. 6624 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6625 EXPECT_EQ(StringRef("functionCall(\n" 6626 " paramA, paramB, paramC);\n" 6627 "void functionDecl(\n" 6628 " int A, int B, int C);"), 6629 format(Input, Style)); 6630 6631 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6632 // first argument. 6633 Style.AllowAllArgumentsOnNextLine = true; 6634 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6635 EXPECT_EQ(StringRef("functionCall(\n" 6636 " paramA, paramB, paramC);\n" 6637 "void functionDecl(\n" 6638 " int A, int B, int C);"), 6639 format(Input, Style)); 6640 // It wouldn't fit on one line with aligned parameters so this setting 6641 // doesn't change anything for BAS_Align. 6642 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6643 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6644 " paramC);\n" 6645 "void functionDecl(int A, int B,\n" 6646 " int C);"), 6647 format(Input, Style)); 6648 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6649 EXPECT_EQ(StringRef("functionCall(\n" 6650 " paramA, paramB, paramC);\n" 6651 "void functionDecl(\n" 6652 " int A, int B, int C);"), 6653 format(Input, Style)); 6654 } 6655 6656 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6657 FormatStyle Style = getLLVMStyle(); 6658 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6659 6660 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6661 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6662 getStyleWithColumns(Style, 45)); 6663 verifyFormat("Constructor() :\n" 6664 " Initializer(FitsOnTheLine) {}", 6665 getStyleWithColumns(Style, 44)); 6666 verifyFormat("Constructor() :\n" 6667 " Initializer(FitsOnTheLine) {}", 6668 getStyleWithColumns(Style, 43)); 6669 6670 verifyFormat("template <typename T>\n" 6671 "Constructor() : Initializer(FitsOnTheLine) {}", 6672 getStyleWithColumns(Style, 50)); 6673 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6674 verifyFormat( 6675 "SomeClass::Constructor() :\n" 6676 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6677 Style); 6678 6679 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 6680 verifyFormat( 6681 "SomeClass::Constructor() :\n" 6682 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6683 Style); 6684 6685 verifyFormat( 6686 "SomeClass::Constructor() :\n" 6687 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6688 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6689 Style); 6690 verifyFormat( 6691 "SomeClass::Constructor() :\n" 6692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6693 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6694 Style); 6695 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6696 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6697 " aaaaaaaaaa(aaaaaa) {}", 6698 Style); 6699 6700 verifyFormat("Constructor() :\n" 6701 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6702 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6703 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6704 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6705 Style); 6706 6707 verifyFormat("Constructor() :\n" 6708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6710 Style); 6711 6712 verifyFormat("Constructor(int Parameter = 0) :\n" 6713 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6714 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6715 Style); 6716 verifyFormat("Constructor() :\n" 6717 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6718 "}", 6719 getStyleWithColumns(Style, 60)); 6720 verifyFormat("Constructor() :\n" 6721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6722 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6723 Style); 6724 6725 // Here a line could be saved by splitting the second initializer onto two 6726 // lines, but that is not desirable. 6727 verifyFormat("Constructor() :\n" 6728 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6729 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6730 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6731 Style); 6732 6733 FormatStyle OnePerLine = Style; 6734 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6735 verifyFormat("SomeClass::Constructor() :\n" 6736 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6737 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6738 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6739 OnePerLine); 6740 verifyFormat("SomeClass::Constructor() :\n" 6741 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6742 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6743 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6744 OnePerLine); 6745 verifyFormat("MyClass::MyClass(int var) :\n" 6746 " some_var_(var), // 4 space indent\n" 6747 " some_other_var_(var + 1) { // lined up\n" 6748 "}", 6749 OnePerLine); 6750 verifyFormat("Constructor() :\n" 6751 " aaaaa(aaaaaa),\n" 6752 " aaaaa(aaaaaa),\n" 6753 " aaaaa(aaaaaa),\n" 6754 " aaaaa(aaaaaa),\n" 6755 " aaaaa(aaaaaa) {}", 6756 OnePerLine); 6757 verifyFormat("Constructor() :\n" 6758 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6759 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6760 OnePerLine); 6761 OnePerLine.BinPackParameters = false; 6762 verifyFormat("Constructor() :\n" 6763 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6764 " aaaaaaaaaaa().aaa(),\n" 6765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6766 OnePerLine); 6767 OnePerLine.ColumnLimit = 60; 6768 verifyFormat("Constructor() :\n" 6769 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6770 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6771 OnePerLine); 6772 6773 EXPECT_EQ("Constructor() :\n" 6774 " // Comment forcing unwanted break.\n" 6775 " aaaa(aaaa) {}", 6776 format("Constructor() :\n" 6777 " // Comment forcing unwanted break.\n" 6778 " aaaa(aaaa) {}", 6779 Style)); 6780 6781 Style.ColumnLimit = 0; 6782 verifyFormat("SomeClass::Constructor() :\n" 6783 " a(a) {}", 6784 Style); 6785 verifyFormat("SomeClass::Constructor() noexcept :\n" 6786 " a(a) {}", 6787 Style); 6788 verifyFormat("SomeClass::Constructor() :\n" 6789 " a(a), b(b), c(c) {}", 6790 Style); 6791 verifyFormat("SomeClass::Constructor() :\n" 6792 " a(a) {\n" 6793 " foo();\n" 6794 " bar();\n" 6795 "}", 6796 Style); 6797 6798 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6799 verifyFormat("SomeClass::Constructor() :\n" 6800 " a(a), b(b), c(c) {\n" 6801 "}", 6802 Style); 6803 verifyFormat("SomeClass::Constructor() :\n" 6804 " a(a) {\n" 6805 "}", 6806 Style); 6807 6808 Style.ColumnLimit = 80; 6809 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6810 Style.ConstructorInitializerIndentWidth = 2; 6811 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6812 verifyFormat("SomeClass::Constructor() :\n" 6813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6814 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6815 Style); 6816 6817 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6818 // well 6819 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6820 verifyFormat( 6821 "class SomeClass\n" 6822 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6823 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6824 Style); 6825 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6826 verifyFormat( 6827 "class SomeClass\n" 6828 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6829 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6830 Style); 6831 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6832 verifyFormat( 6833 "class SomeClass :\n" 6834 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6835 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6836 Style); 6837 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6838 verifyFormat( 6839 "class SomeClass\n" 6840 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6841 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6842 Style); 6843 } 6844 6845 #ifndef EXPENSIVE_CHECKS 6846 // Expensive checks enables libstdc++ checking which includes validating the 6847 // state of ranges used in std::priority_queue - this blows out the 6848 // runtime/scalability of the function and makes this test unacceptably slow. 6849 TEST_F(FormatTest, MemoizationTests) { 6850 // This breaks if the memoization lookup does not take \c Indent and 6851 // \c LastSpace into account. 6852 verifyFormat( 6853 "extern CFRunLoopTimerRef\n" 6854 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6855 " CFTimeInterval interval, CFOptionFlags flags,\n" 6856 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6857 " CFRunLoopTimerContext *context) {}"); 6858 6859 // Deep nesting somewhat works around our memoization. 6860 verifyFormat( 6861 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6862 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6863 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6864 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6865 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6866 getLLVMStyleWithColumns(65)); 6867 verifyFormat( 6868 "aaaaa(\n" 6869 " aaaaa,\n" 6870 " aaaaa(\n" 6871 " aaaaa,\n" 6872 " aaaaa(\n" 6873 " aaaaa,\n" 6874 " aaaaa(\n" 6875 " aaaaa,\n" 6876 " aaaaa(\n" 6877 " aaaaa,\n" 6878 " aaaaa(\n" 6879 " aaaaa,\n" 6880 " aaaaa(\n" 6881 " aaaaa,\n" 6882 " aaaaa(\n" 6883 " aaaaa,\n" 6884 " aaaaa(\n" 6885 " aaaaa,\n" 6886 " aaaaa(\n" 6887 " aaaaa,\n" 6888 " aaaaa(\n" 6889 " aaaaa,\n" 6890 " aaaaa(\n" 6891 " aaaaa,\n" 6892 " aaaaa))))))))))));", 6893 getLLVMStyleWithColumns(65)); 6894 verifyFormat( 6895 "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" 6896 " a),\n" 6897 " a),\n" 6898 " a),\n" 6899 " a),\n" 6900 " a),\n" 6901 " a),\n" 6902 " a),\n" 6903 " a),\n" 6904 " a),\n" 6905 " a),\n" 6906 " a),\n" 6907 " a),\n" 6908 " a),\n" 6909 " a),\n" 6910 " a),\n" 6911 " a),\n" 6912 " a)", 6913 getLLVMStyleWithColumns(65)); 6914 6915 // This test takes VERY long when memoization is broken. 6916 FormatStyle OnePerLine = getLLVMStyle(); 6917 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6918 OnePerLine.BinPackParameters = false; 6919 std::string input = "Constructor()\n" 6920 " : aaaa(a,\n"; 6921 for (unsigned i = 0, e = 80; i != e; ++i) { 6922 input += " a,\n"; 6923 } 6924 input += " a) {}"; 6925 verifyFormat(input, OnePerLine); 6926 } 6927 #endif 6928 6929 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6930 verifyFormat( 6931 "void f() {\n" 6932 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6933 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6934 " f();\n" 6935 "}"); 6936 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6937 " Intervals[i - 1].getRange().getLast()) {\n}"); 6938 } 6939 6940 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6941 // Principially, we break function declarations in a certain order: 6942 // 1) break amongst arguments. 6943 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6944 " Cccccccccccccc cccccccccccccc);"); 6945 verifyFormat("template <class TemplateIt>\n" 6946 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6947 " TemplateIt *stop) {}"); 6948 6949 // 2) break after return type. 6950 verifyFormat( 6951 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6952 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6953 getGoogleStyle()); 6954 6955 // 3) break after (. 6956 verifyFormat( 6957 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6958 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6959 getGoogleStyle()); 6960 6961 // 4) break before after nested name specifiers. 6962 verifyFormat( 6963 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6964 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6965 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6966 getGoogleStyle()); 6967 6968 // However, there are exceptions, if a sufficient amount of lines can be 6969 // saved. 6970 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6971 // more adjusting. 6972 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6973 " Cccccccccccccc cccccccccc,\n" 6974 " Cccccccccccccc cccccccccc,\n" 6975 " Cccccccccccccc cccccccccc,\n" 6976 " Cccccccccccccc cccccccccc);"); 6977 verifyFormat( 6978 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6979 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6980 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6981 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 6982 getGoogleStyle()); 6983 verifyFormat( 6984 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6985 " Cccccccccccccc cccccccccc,\n" 6986 " Cccccccccccccc cccccccccc,\n" 6987 " Cccccccccccccc cccccccccc,\n" 6988 " Cccccccccccccc cccccccccc,\n" 6989 " Cccccccccccccc cccccccccc,\n" 6990 " Cccccccccccccc cccccccccc);"); 6991 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6992 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6993 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6994 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6995 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 6996 6997 // Break after multi-line parameters. 6998 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7001 " bbbb bbbb);"); 7002 verifyFormat("void SomeLoooooooooooongFunction(\n" 7003 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7004 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7005 " int bbbbbbbbbbbbb);"); 7006 7007 // Treat overloaded operators like other functions. 7008 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7009 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 7010 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7011 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 7012 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7013 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 7014 verifyGoogleFormat( 7015 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 7016 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 7017 verifyGoogleFormat( 7018 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 7019 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 7020 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7021 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 7022 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 7023 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 7024 verifyGoogleFormat( 7025 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 7026 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7027 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 7028 verifyGoogleFormat("template <typename T>\n" 7029 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7030 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 7031 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 7032 7033 FormatStyle Style = getLLVMStyle(); 7034 Style.PointerAlignment = FormatStyle::PAS_Left; 7035 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7036 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 7037 Style); 7038 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 7039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7040 Style); 7041 } 7042 7043 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 7044 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 7045 // Prefer keeping `::` followed by `operator` together. 7046 EXPECT_EQ("const aaaa::bbbbbbb &\n" 7047 "ccccccccc::operator++() {\n" 7048 " stuff();\n" 7049 "}", 7050 format("const aaaa::bbbbbbb\n" 7051 "&ccccccccc::operator++() { stuff(); }", 7052 getLLVMStyleWithColumns(40))); 7053 } 7054 7055 TEST_F(FormatTest, TrailingReturnType) { 7056 verifyFormat("auto foo() -> int;\n"); 7057 // correct trailing return type spacing 7058 verifyFormat("auto operator->() -> int;\n"); 7059 verifyFormat("auto operator++(int) -> int;\n"); 7060 7061 verifyFormat("struct S {\n" 7062 " auto bar() const -> int;\n" 7063 "};"); 7064 verifyFormat("template <size_t Order, typename T>\n" 7065 "auto load_img(const std::string &filename)\n" 7066 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 7067 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 7068 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 7069 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 7070 verifyFormat("template <typename T>\n" 7071 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 7072 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 7073 7074 // Not trailing return types. 7075 verifyFormat("void f() { auto a = b->c(); }"); 7076 verifyFormat("auto a = p->foo();"); 7077 verifyFormat("int a = p->foo();"); 7078 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); 7079 } 7080 7081 TEST_F(FormatTest, DeductionGuides) { 7082 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 7083 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 7084 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 7085 verifyFormat( 7086 "template <class... T>\n" 7087 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 7088 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 7089 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 7090 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 7091 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 7092 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 7093 verifyFormat("template <class T> x() -> x<1>;"); 7094 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 7095 7096 // Ensure not deduction guides. 7097 verifyFormat("c()->f<int>();"); 7098 verifyFormat("x()->foo<1>;"); 7099 verifyFormat("x = p->foo<3>();"); 7100 verifyFormat("x()->x<1>();"); 7101 verifyFormat("x()->x<1>;"); 7102 } 7103 7104 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 7105 // Avoid breaking before trailing 'const' or other trailing annotations, if 7106 // they are not function-like. 7107 FormatStyle Style = getGoogleStyleWithColumns(47); 7108 verifyFormat("void someLongFunction(\n" 7109 " int someLoooooooooooooongParameter) const {\n}", 7110 getLLVMStyleWithColumns(47)); 7111 verifyFormat("LoooooongReturnType\n" 7112 "someLoooooooongFunction() const {}", 7113 getLLVMStyleWithColumns(47)); 7114 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 7115 " const {}", 7116 Style); 7117 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7118 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 7119 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7120 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 7121 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7122 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 7123 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 7124 " aaaaaaaaaaa aaaaa) const override;"); 7125 verifyGoogleFormat( 7126 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7127 " const override;"); 7128 7129 // Even if the first parameter has to be wrapped. 7130 verifyFormat("void someLongFunction(\n" 7131 " int someLongParameter) const {}", 7132 getLLVMStyleWithColumns(46)); 7133 verifyFormat("void someLongFunction(\n" 7134 " int someLongParameter) const {}", 7135 Style); 7136 verifyFormat("void someLongFunction(\n" 7137 " int someLongParameter) override {}", 7138 Style); 7139 verifyFormat("void someLongFunction(\n" 7140 " int someLongParameter) OVERRIDE {}", 7141 Style); 7142 verifyFormat("void someLongFunction(\n" 7143 " int someLongParameter) final {}", 7144 Style); 7145 verifyFormat("void someLongFunction(\n" 7146 " int someLongParameter) FINAL {}", 7147 Style); 7148 verifyFormat("void someLongFunction(\n" 7149 " int parameter) const override {}", 7150 Style); 7151 7152 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 7153 verifyFormat("void someLongFunction(\n" 7154 " int someLongParameter) const\n" 7155 "{\n" 7156 "}", 7157 Style); 7158 7159 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 7160 verifyFormat("void someLongFunction(\n" 7161 " int someLongParameter) const\n" 7162 " {\n" 7163 " }", 7164 Style); 7165 7166 // Unless these are unknown annotations. 7167 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 7168 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7169 " LONG_AND_UGLY_ANNOTATION;"); 7170 7171 // Breaking before function-like trailing annotations is fine to keep them 7172 // close to their arguments. 7173 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7174 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7175 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7176 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7177 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7178 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 7179 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 7180 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 7181 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 7182 7183 verifyFormat( 7184 "void aaaaaaaaaaaaaaaaaa()\n" 7185 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 7186 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 7187 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7188 " __attribute__((unused));"); 7189 verifyGoogleFormat( 7190 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7191 " GUARDED_BY(aaaaaaaaaaaa);"); 7192 verifyGoogleFormat( 7193 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7194 " GUARDED_BY(aaaaaaaaaaaa);"); 7195 verifyGoogleFormat( 7196 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7197 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7198 verifyGoogleFormat( 7199 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7200 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7201 } 7202 7203 TEST_F(FormatTest, FunctionAnnotations) { 7204 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7205 "int OldFunction(const string ¶meter) {}"); 7206 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7207 "string OldFunction(const string ¶meter) {}"); 7208 verifyFormat("template <typename T>\n" 7209 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7210 "string OldFunction(const string ¶meter) {}"); 7211 7212 // Not function annotations. 7213 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7214 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 7215 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 7216 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 7217 verifyFormat("MACRO(abc).function() // wrap\n" 7218 " << abc;"); 7219 verifyFormat("MACRO(abc)->function() // wrap\n" 7220 " << abc;"); 7221 verifyFormat("MACRO(abc)::function() // wrap\n" 7222 " << abc;"); 7223 } 7224 7225 TEST_F(FormatTest, BreaksDesireably) { 7226 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7227 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7228 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 7229 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 7231 "}"); 7232 7233 verifyFormat( 7234 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7235 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7236 7237 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7238 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7239 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7240 7241 verifyFormat( 7242 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7243 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7244 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7245 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 7247 7248 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7249 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7250 7251 verifyFormat( 7252 "void f() {\n" 7253 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7254 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7255 "}"); 7256 verifyFormat( 7257 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7258 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7259 verifyFormat( 7260 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7262 verifyFormat( 7263 "aaaaaa(aaa,\n" 7264 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7265 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7266 " aaaa);"); 7267 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7270 7271 // Indent consistently independent of call expression and unary operator. 7272 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7273 " dddddddddddddddddddddddddddddd));"); 7274 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7275 " dddddddddddddddddddddddddddddd));"); 7276 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 7277 " dddddddddddddddddddddddddddddd));"); 7278 7279 // This test case breaks on an incorrect memoization, i.e. an optimization not 7280 // taking into account the StopAt value. 7281 verifyFormat( 7282 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7283 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7284 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7285 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7286 7287 verifyFormat("{\n {\n {\n" 7288 " Annotation.SpaceRequiredBefore =\n" 7289 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 7290 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 7291 " }\n }\n}"); 7292 7293 // Break on an outer level if there was a break on an inner level. 7294 EXPECT_EQ("f(g(h(a, // comment\n" 7295 " b, c),\n" 7296 " d, e),\n" 7297 " x, y);", 7298 format("f(g(h(a, // comment\n" 7299 " b, c), d, e), x, y);")); 7300 7301 // Prefer breaking similar line breaks. 7302 verifyFormat( 7303 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 7304 " NSTrackingMouseEnteredAndExited |\n" 7305 " NSTrackingActiveAlways;"); 7306 } 7307 7308 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 7309 FormatStyle NoBinPacking = getGoogleStyle(); 7310 NoBinPacking.BinPackParameters = false; 7311 NoBinPacking.BinPackArguments = true; 7312 verifyFormat("void f() {\n" 7313 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 7314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7315 "}", 7316 NoBinPacking); 7317 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 7318 " int aaaaaaaaaaaaaaaaaaaa,\n" 7319 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7320 NoBinPacking); 7321 7322 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7323 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7324 " vector<int> bbbbbbbbbbbbbbb);", 7325 NoBinPacking); 7326 // FIXME: This behavior difference is probably not wanted. However, currently 7327 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 7328 // template arguments from BreakBeforeParameter being set because of the 7329 // one-per-line formatting. 7330 verifyFormat( 7331 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7332 " aaaaaaaaaa> aaaaaaaaaa);", 7333 NoBinPacking); 7334 verifyFormat( 7335 "void fffffffffff(\n" 7336 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 7337 " aaaaaaaaaa);"); 7338 } 7339 7340 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 7341 FormatStyle NoBinPacking = getGoogleStyle(); 7342 NoBinPacking.BinPackParameters = false; 7343 NoBinPacking.BinPackArguments = false; 7344 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 7345 " aaaaaaaaaaaaaaaaaaaa,\n" 7346 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 7347 NoBinPacking); 7348 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 7349 " aaaaaaaaaaaaa,\n" 7350 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 7351 NoBinPacking); 7352 verifyFormat( 7353 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7354 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7356 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 7358 NoBinPacking); 7359 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7360 " .aaaaaaaaaaaaaaaaaa();", 7361 NoBinPacking); 7362 verifyFormat("void f() {\n" 7363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7364 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 7365 "}", 7366 NoBinPacking); 7367 7368 verifyFormat( 7369 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7370 " aaaaaaaaaaaa,\n" 7371 " aaaaaaaaaaaa);", 7372 NoBinPacking); 7373 verifyFormat( 7374 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 7375 " ddddddddddddddddddddddddddddd),\n" 7376 " test);", 7377 NoBinPacking); 7378 7379 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7380 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 7381 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 7382 " aaaaaaaaaaaaaaaaaa;", 7383 NoBinPacking); 7384 verifyFormat("a(\"a\"\n" 7385 " \"a\",\n" 7386 " a);"); 7387 7388 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7389 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7390 " aaaaaaaaa,\n" 7391 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7392 NoBinPacking); 7393 verifyFormat( 7394 "void f() {\n" 7395 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7396 " .aaaaaaa();\n" 7397 "}", 7398 NoBinPacking); 7399 verifyFormat( 7400 "template <class SomeType, class SomeOtherType>\n" 7401 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7402 NoBinPacking); 7403 } 7404 7405 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7406 FormatStyle Style = getLLVMStyleWithColumns(15); 7407 Style.ExperimentalAutoDetectBinPacking = true; 7408 EXPECT_EQ("aaa(aaaa,\n" 7409 " aaaa,\n" 7410 " aaaa);\n" 7411 "aaa(aaaa,\n" 7412 " aaaa,\n" 7413 " aaaa);", 7414 format("aaa(aaaa,\n" // one-per-line 7415 " aaaa,\n" 7416 " aaaa );\n" 7417 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7418 Style)); 7419 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7420 " aaaa);\n" 7421 "aaa(aaaa, aaaa,\n" 7422 " aaaa);", 7423 format("aaa(aaaa, aaaa,\n" // bin-packed 7424 " aaaa );\n" 7425 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7426 Style)); 7427 } 7428 7429 TEST_F(FormatTest, FormatsBuilderPattern) { 7430 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7431 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7432 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7433 " .StartsWith(\".init\", ORDER_INIT)\n" 7434 " .StartsWith(\".fini\", ORDER_FINI)\n" 7435 " .StartsWith(\".hash\", ORDER_HASH)\n" 7436 " .Default(ORDER_TEXT);\n"); 7437 7438 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7439 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7440 verifyFormat("aaaaaaa->aaaaaaa\n" 7441 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7443 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7444 verifyFormat( 7445 "aaaaaaa->aaaaaaa\n" 7446 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7447 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7448 verifyFormat( 7449 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7450 " aaaaaaaaaaaaaa);"); 7451 verifyFormat( 7452 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7453 " aaaaaa->aaaaaaaaaaaa()\n" 7454 " ->aaaaaaaaaaaaaaaa(\n" 7455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7456 " ->aaaaaaaaaaaaaaaaa();"); 7457 verifyGoogleFormat( 7458 "void f() {\n" 7459 " someo->Add((new util::filetools::Handler(dir))\n" 7460 " ->OnEvent1(NewPermanentCallback(\n" 7461 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7462 " ->OnEvent2(NewPermanentCallback(\n" 7463 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7464 " ->OnEvent3(NewPermanentCallback(\n" 7465 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7466 " ->OnEvent5(NewPermanentCallback(\n" 7467 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7468 " ->OnEvent6(NewPermanentCallback(\n" 7469 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7470 "}"); 7471 7472 verifyFormat( 7473 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7474 verifyFormat("aaaaaaaaaaaaaaa()\n" 7475 " .aaaaaaaaaaaaaaa()\n" 7476 " .aaaaaaaaaaaaaaa()\n" 7477 " .aaaaaaaaaaaaaaa()\n" 7478 " .aaaaaaaaaaaaaaa();"); 7479 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7480 " .aaaaaaaaaaaaaaa()\n" 7481 " .aaaaaaaaaaaaaaa()\n" 7482 " .aaaaaaaaaaaaaaa();"); 7483 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7484 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7485 " .aaaaaaaaaaaaaaa();"); 7486 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7487 " ->aaaaaaaaaaaaaae(0)\n" 7488 " ->aaaaaaaaaaaaaaa();"); 7489 7490 // Don't linewrap after very short segments. 7491 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7492 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7493 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7494 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7495 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7496 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7497 verifyFormat("aaa()\n" 7498 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7499 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7500 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7501 7502 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7503 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7504 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7505 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7506 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7508 7509 // Prefer not to break after empty parentheses. 7510 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7511 " First->LastNewlineOffset);"); 7512 7513 // Prefer not to create "hanging" indents. 7514 verifyFormat( 7515 "return !soooooooooooooome_map\n" 7516 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7517 " .second;"); 7518 verifyFormat( 7519 "return aaaaaaaaaaaaaaaa\n" 7520 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7521 " .aaaa(aaaaaaaaaaaaaa);"); 7522 // No hanging indent here. 7523 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7525 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7527 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7528 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7529 getLLVMStyleWithColumns(60)); 7530 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7531 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7532 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7533 getLLVMStyleWithColumns(59)); 7534 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7536 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7537 7538 // Dont break if only closing statements before member call 7539 verifyFormat("test() {\n" 7540 " ([]() -> {\n" 7541 " int b = 32;\n" 7542 " return 3;\n" 7543 " }).foo();\n" 7544 "}"); 7545 verifyFormat("test() {\n" 7546 " (\n" 7547 " []() -> {\n" 7548 " int b = 32;\n" 7549 " return 3;\n" 7550 " },\n" 7551 " foo, bar)\n" 7552 " .foo();\n" 7553 "}"); 7554 verifyFormat("test() {\n" 7555 " ([]() -> {\n" 7556 " int b = 32;\n" 7557 " return 3;\n" 7558 " })\n" 7559 " .foo()\n" 7560 " .bar();\n" 7561 "}"); 7562 verifyFormat("test() {\n" 7563 " ([]() -> {\n" 7564 " int b = 32;\n" 7565 " return 3;\n" 7566 " })\n" 7567 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7568 " \"bbbb\");\n" 7569 "}", 7570 getLLVMStyleWithColumns(30)); 7571 } 7572 7573 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7574 verifyFormat( 7575 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7576 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7577 verifyFormat( 7578 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7579 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7580 7581 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7582 " ccccccccccccccccccccccccc) {\n}"); 7583 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7584 " ccccccccccccccccccccccccc) {\n}"); 7585 7586 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7587 " ccccccccccccccccccccccccc) {\n}"); 7588 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7589 " ccccccccccccccccccccccccc) {\n}"); 7590 7591 verifyFormat( 7592 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7593 " ccccccccccccccccccccccccc) {\n}"); 7594 verifyFormat( 7595 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7596 " ccccccccccccccccccccccccc) {\n}"); 7597 7598 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7599 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7600 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7601 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7602 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7603 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7604 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7605 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7606 7607 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7608 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7609 " aaaaaaaaaaaaaaa != aa) {\n}"); 7610 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7611 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7612 " aaaaaaaaaaaaaaa != aa) {\n}"); 7613 } 7614 7615 TEST_F(FormatTest, BreaksAfterAssignments) { 7616 verifyFormat( 7617 "unsigned Cost =\n" 7618 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7619 " SI->getPointerAddressSpaceee());\n"); 7620 verifyFormat( 7621 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7622 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7623 7624 verifyFormat( 7625 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7626 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7627 verifyFormat("unsigned OriginalStartColumn =\n" 7628 " SourceMgr.getSpellingColumnNumber(\n" 7629 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7630 " 1;"); 7631 } 7632 7633 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7634 FormatStyle Style = getLLVMStyle(); 7635 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7636 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7637 Style); 7638 7639 Style.PenaltyBreakAssignment = 20; 7640 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7641 " cccccccccccccccccccccccccc;", 7642 Style); 7643 } 7644 7645 TEST_F(FormatTest, AlignsAfterAssignments) { 7646 verifyFormat( 7647 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7648 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7649 verifyFormat( 7650 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7651 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7652 verifyFormat( 7653 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7654 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7655 verifyFormat( 7656 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7657 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7658 verifyFormat( 7659 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7660 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7661 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7662 } 7663 7664 TEST_F(FormatTest, AlignsAfterReturn) { 7665 verifyFormat( 7666 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7667 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7668 verifyFormat( 7669 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7670 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7671 verifyFormat( 7672 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7673 " aaaaaaaaaaaaaaaaaaaaaa();"); 7674 verifyFormat( 7675 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7676 " aaaaaaaaaaaaaaaaaaaaaa());"); 7677 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7679 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7682 verifyFormat("return\n" 7683 " // true if code is one of a or b.\n" 7684 " code == a || code == b;"); 7685 } 7686 7687 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7688 verifyFormat( 7689 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7690 " aaaaaaaaa aaaaaaa) {}"); 7691 verifyFormat( 7692 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7693 " aaaaaaaaaaa aaaaaaaaa);"); 7694 verifyFormat( 7695 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7696 " aaaaaaaaaaaaaaaaaaaaa));"); 7697 FormatStyle Style = getLLVMStyle(); 7698 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7699 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7700 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7701 Style); 7702 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7703 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7704 Style); 7705 verifyFormat("SomeLongVariableName->someFunction(\n" 7706 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7707 Style); 7708 verifyFormat( 7709 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7710 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7711 Style); 7712 verifyFormat( 7713 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7714 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7715 Style); 7716 verifyFormat( 7717 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7718 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7719 Style); 7720 7721 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7722 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7723 " b));", 7724 Style); 7725 7726 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7727 Style.BinPackArguments = false; 7728 Style.BinPackParameters = false; 7729 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7730 " aaaaaaaaaaa aaaaaaaa,\n" 7731 " aaaaaaaaa aaaaaaa,\n" 7732 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7733 Style); 7734 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7735 " aaaaaaaaaaa aaaaaaaaa,\n" 7736 " aaaaaaaaaaa aaaaaaaaa,\n" 7737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7738 Style); 7739 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7740 " aaaaaaaaaaaaaaa,\n" 7741 " aaaaaaaaaaaaaaaaaaaaa,\n" 7742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7743 Style); 7744 verifyFormat( 7745 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7746 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7747 Style); 7748 verifyFormat( 7749 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7750 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7751 Style); 7752 verifyFormat( 7753 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7754 " aaaaaaaaaaaaaaaaaaaaa(\n" 7755 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7756 " aaaaaaaaaaaaaaaa);", 7757 Style); 7758 verifyFormat( 7759 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7760 " aaaaaaaaaaaaaaaaaaaaa(\n" 7761 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7762 " aaaaaaaaaaaaaaaa);", 7763 Style); 7764 } 7765 7766 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7767 FormatStyle Style = getLLVMStyleWithColumns(40); 7768 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7769 " bbbbbbbbbbbbbbbbbbbbbb);", 7770 Style); 7771 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7772 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7773 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7774 " bbbbbbbbbbbbbbbbbbbbbb);", 7775 Style); 7776 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7777 Style.AlignOperands = FormatStyle::OAS_Align; 7778 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7779 " bbbbbbbbbbbbbbbbbbbbbb);", 7780 Style); 7781 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7782 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7783 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7784 " bbbbbbbbbbbbbbbbbbbbbb);", 7785 Style); 7786 } 7787 7788 TEST_F(FormatTest, BreaksConditionalExpressions) { 7789 verifyFormat( 7790 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7791 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7792 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7793 verifyFormat( 7794 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7795 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7796 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7797 verifyFormat( 7798 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7799 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7800 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7801 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7802 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7803 verifyFormat( 7804 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7805 " : aaaaaaaaaaaaa);"); 7806 verifyFormat( 7807 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7808 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7809 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7810 " aaaaaaaaaaaaa);"); 7811 verifyFormat( 7812 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7813 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7814 " aaaaaaaaaaaaa);"); 7815 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7816 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7817 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7818 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7820 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7821 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7822 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7823 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7824 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7826 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7827 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7828 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7829 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7830 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7831 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7832 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7833 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7834 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7835 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7836 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7837 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7838 " : aaaaaaaaaaaaaaaa;"); 7839 verifyFormat( 7840 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7841 " ? aaaaaaaaaaaaaaa\n" 7842 " : aaaaaaaaaaaaaaa;"); 7843 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7844 " aaaaaaaaa\n" 7845 " ? b\n" 7846 " : c);"); 7847 verifyFormat("return aaaa == bbbb\n" 7848 " // comment\n" 7849 " ? aaaa\n" 7850 " : bbbb;"); 7851 verifyFormat("unsigned Indent =\n" 7852 " format(TheLine.First,\n" 7853 " IndentForLevel[TheLine.Level] >= 0\n" 7854 " ? IndentForLevel[TheLine.Level]\n" 7855 " : TheLine * 2,\n" 7856 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7857 getLLVMStyleWithColumns(60)); 7858 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7859 " ? aaaaaaaaaaaaaaa\n" 7860 " : bbbbbbbbbbbbbbb //\n" 7861 " ? ccccccccccccccc\n" 7862 " : ddddddddddddddd;"); 7863 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7864 " ? aaaaaaaaaaaaaaa\n" 7865 " : (bbbbbbbbbbbbbbb //\n" 7866 " ? ccccccccccccccc\n" 7867 " : ddddddddddddddd);"); 7868 verifyFormat( 7869 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7870 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7871 " aaaaaaaaaaaaaaaaaaaaa +\n" 7872 " aaaaaaaaaaaaaaaaaaaaa\n" 7873 " : aaaaaaaaaa;"); 7874 verifyFormat( 7875 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7876 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7877 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7878 7879 FormatStyle NoBinPacking = getLLVMStyle(); 7880 NoBinPacking.BinPackArguments = false; 7881 verifyFormat( 7882 "void f() {\n" 7883 " g(aaa,\n" 7884 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7886 " ? aaaaaaaaaaaaaaa\n" 7887 " : aaaaaaaaaaaaaaa);\n" 7888 "}", 7889 NoBinPacking); 7890 verifyFormat( 7891 "void f() {\n" 7892 " g(aaa,\n" 7893 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7894 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7895 " ?: aaaaaaaaaaaaaaa);\n" 7896 "}", 7897 NoBinPacking); 7898 7899 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7900 " // comment.\n" 7901 " ccccccccccccccccccccccccccccccccccccccc\n" 7902 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7903 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7904 7905 // Assignments in conditional expressions. Apparently not uncommon :-(. 7906 verifyFormat("return a != b\n" 7907 " // comment\n" 7908 " ? a = b\n" 7909 " : a = b;"); 7910 verifyFormat("return a != b\n" 7911 " // comment\n" 7912 " ? a = a != b\n" 7913 " // comment\n" 7914 " ? a = b\n" 7915 " : a\n" 7916 " : a;\n"); 7917 verifyFormat("return a != b\n" 7918 " // comment\n" 7919 " ? a\n" 7920 " : a = a != b\n" 7921 " // comment\n" 7922 " ? a = b\n" 7923 " : a;"); 7924 7925 // Chained conditionals 7926 FormatStyle Style = getLLVMStyleWithColumns(70); 7927 Style.AlignOperands = FormatStyle::OAS_Align; 7928 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7929 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7930 " : 3333333333333333;", 7931 Style); 7932 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7933 " : bbbbbbbbbb ? 2222222222222222\n" 7934 " : 3333333333333333;", 7935 Style); 7936 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7937 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7938 " : 3333333333333333;", 7939 Style); 7940 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7941 " : bbbbbbbbbbbbbb ? 222222\n" 7942 " : 333333;", 7943 Style); 7944 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7945 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7946 " : cccccccccccccc ? 3333333333333333\n" 7947 " : 4444444444444444;", 7948 Style); 7949 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7950 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7951 " : 3333333333333333;", 7952 Style); 7953 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7954 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7955 " : (aaa ? bbb : ccc);", 7956 Style); 7957 verifyFormat( 7958 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7959 " : cccccccccccccccccc)\n" 7960 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7961 " : 3333333333333333;", 7962 Style); 7963 verifyFormat( 7964 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7965 " : cccccccccccccccccc)\n" 7966 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7967 " : 3333333333333333;", 7968 Style); 7969 verifyFormat( 7970 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7971 " : dddddddddddddddddd)\n" 7972 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7973 " : 3333333333333333;", 7974 Style); 7975 verifyFormat( 7976 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7977 " : dddddddddddddddddd)\n" 7978 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7979 " : 3333333333333333;", 7980 Style); 7981 verifyFormat( 7982 "return aaaaaaaaa ? 1111111111111111\n" 7983 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7984 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7985 " : dddddddddddddddddd)\n", 7986 Style); 7987 verifyFormat( 7988 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7989 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7990 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7991 " : cccccccccccccccccc);", 7992 Style); 7993 verifyFormat( 7994 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7995 " : ccccccccccccccc ? dddddddddddddddddd\n" 7996 " : eeeeeeeeeeeeeeeeee)\n" 7997 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7998 " : 3333333333333333;", 7999 Style); 8000 verifyFormat( 8001 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8002 " : ccccccccccccccc ? dddddddddddddddddd\n" 8003 " : eeeeeeeeeeeeeeeeee)\n" 8004 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8005 " : 3333333333333333;", 8006 Style); 8007 verifyFormat( 8008 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8009 " : cccccccccccc ? dddddddddddddddddd\n" 8010 " : eeeeeeeeeeeeeeeeee)\n" 8011 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8012 " : 3333333333333333;", 8013 Style); 8014 verifyFormat( 8015 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8016 " : cccccccccccccccccc\n" 8017 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8018 " : 3333333333333333;", 8019 Style); 8020 verifyFormat( 8021 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8022 " : cccccccccccccccc ? dddddddddddddddddd\n" 8023 " : eeeeeeeeeeeeeeeeee\n" 8024 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8025 " : 3333333333333333;", 8026 Style); 8027 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 8028 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8029 " : cccccccccccccccccc ? dddddddddddddddddd\n" 8030 " : eeeeeeeeeeeeeeeeee)\n" 8031 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 8032 " : 3333333333333333;", 8033 Style); 8034 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 8035 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8036 " : cccccccccccccccc ? dddddddddddddddddd\n" 8037 " : eeeeeeeeeeeeeeeeee\n" 8038 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 8039 " : 3333333333333333;", 8040 Style); 8041 8042 Style.AlignOperands = FormatStyle::OAS_DontAlign; 8043 Style.BreakBeforeTernaryOperators = false; 8044 // FIXME: Aligning the question marks is weird given DontAlign. 8045 // Consider disabling this alignment in this case. Also check whether this 8046 // will render the adjustment from https://reviews.llvm.org/D82199 8047 // unnecessary. 8048 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 8049 " bbbb ? cccccccccccccccccc :\n" 8050 " ddddd;\n", 8051 Style); 8052 8053 EXPECT_EQ( 8054 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8055 " /*\n" 8056 " */\n" 8057 " function() {\n" 8058 " try {\n" 8059 " return JJJJJJJJJJJJJJ(\n" 8060 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8061 " }\n" 8062 " } :\n" 8063 " function() {};", 8064 format( 8065 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8066 " /*\n" 8067 " */\n" 8068 " function() {\n" 8069 " try {\n" 8070 " return JJJJJJJJJJJJJJ(\n" 8071 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8072 " }\n" 8073 " } :\n" 8074 " function() {};", 8075 getGoogleStyle(FormatStyle::LK_JavaScript))); 8076 } 8077 8078 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 8079 FormatStyle Style = getLLVMStyleWithColumns(70); 8080 Style.BreakBeforeTernaryOperators = false; 8081 verifyFormat( 8082 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8083 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8084 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8085 Style); 8086 verifyFormat( 8087 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 8088 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8090 Style); 8091 verifyFormat( 8092 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8094 Style); 8095 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 8096 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8098 Style); 8099 verifyFormat( 8100 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 8101 " aaaaaaaaaaaaa);", 8102 Style); 8103 verifyFormat( 8104 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8105 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8107 " aaaaaaaaaaaaa);", 8108 Style); 8109 verifyFormat( 8110 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8111 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8112 " aaaaaaaaaaaaa);", 8113 Style); 8114 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8115 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8117 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8119 Style); 8120 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8124 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8126 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8127 Style); 8128 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 8130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8132 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8133 Style); 8134 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8135 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8136 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8137 Style); 8138 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 8139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8140 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8142 Style); 8143 verifyFormat( 8144 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8145 " aaaaaaaaaaaaaaa :\n" 8146 " aaaaaaaaaaaaaaa;", 8147 Style); 8148 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 8149 " aaaaaaaaa ?\n" 8150 " b :\n" 8151 " c);", 8152 Style); 8153 verifyFormat("unsigned Indent =\n" 8154 " format(TheLine.First,\n" 8155 " IndentForLevel[TheLine.Level] >= 0 ?\n" 8156 " IndentForLevel[TheLine.Level] :\n" 8157 " TheLine * 2,\n" 8158 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 8159 Style); 8160 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8161 " aaaaaaaaaaaaaaa :\n" 8162 " bbbbbbbbbbbbbbb ? //\n" 8163 " ccccccccccccccc :\n" 8164 " ddddddddddddddd;", 8165 Style); 8166 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8167 " aaaaaaaaaaaaaaa :\n" 8168 " (bbbbbbbbbbbbbbb ? //\n" 8169 " ccccccccccccccc :\n" 8170 " ddddddddddddddd);", 8171 Style); 8172 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8173 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 8174 " ccccccccccccccccccccccccccc;", 8175 Style); 8176 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8177 " aaaaa :\n" 8178 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 8179 Style); 8180 8181 // Chained conditionals 8182 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8183 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8184 " 3333333333333333;", 8185 Style); 8186 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8187 " bbbbbbbbbb ? 2222222222222222 :\n" 8188 " 3333333333333333;", 8189 Style); 8190 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 8191 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8192 " 3333333333333333;", 8193 Style); 8194 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8195 " bbbbbbbbbbbbbbbb ? 222222 :\n" 8196 " 333333;", 8197 Style); 8198 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8199 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8200 " cccccccccccccccc ? 3333333333333333 :\n" 8201 " 4444444444444444;", 8202 Style); 8203 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 8204 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8205 " 3333333333333333;", 8206 Style); 8207 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8208 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8209 " (aaa ? bbb : ccc);", 8210 Style); 8211 verifyFormat( 8212 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8213 " cccccccccccccccccc) :\n" 8214 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8215 " 3333333333333333;", 8216 Style); 8217 verifyFormat( 8218 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8219 " cccccccccccccccccc) :\n" 8220 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8221 " 3333333333333333;", 8222 Style); 8223 verifyFormat( 8224 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8225 " dddddddddddddddddd) :\n" 8226 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8227 " 3333333333333333;", 8228 Style); 8229 verifyFormat( 8230 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8231 " dddddddddddddddddd) :\n" 8232 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8233 " 3333333333333333;", 8234 Style); 8235 verifyFormat( 8236 "return aaaaaaaaa ? 1111111111111111 :\n" 8237 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8238 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8239 " dddddddddddddddddd)\n", 8240 Style); 8241 verifyFormat( 8242 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8243 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8244 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8245 " cccccccccccccccccc);", 8246 Style); 8247 verifyFormat( 8248 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8249 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8250 " eeeeeeeeeeeeeeeeee) :\n" 8251 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8252 " 3333333333333333;", 8253 Style); 8254 verifyFormat( 8255 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8256 " ccccccccccccc ? dddddddddddddddddd :\n" 8257 " eeeeeeeeeeeeeeeeee) :\n" 8258 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8259 " 3333333333333333;", 8260 Style); 8261 verifyFormat( 8262 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8263 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8264 " eeeeeeeeeeeeeeeeee) :\n" 8265 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8266 " 3333333333333333;", 8267 Style); 8268 verifyFormat( 8269 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8270 " cccccccccccccccccc :\n" 8271 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8272 " 3333333333333333;", 8273 Style); 8274 verifyFormat( 8275 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8276 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8277 " eeeeeeeeeeeeeeeeee :\n" 8278 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8279 " 3333333333333333;", 8280 Style); 8281 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8282 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8283 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8284 " eeeeeeeeeeeeeeeeee) :\n" 8285 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8286 " 3333333333333333;", 8287 Style); 8288 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8289 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8290 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 8291 " eeeeeeeeeeeeeeeeee :\n" 8292 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8293 " 3333333333333333;", 8294 Style); 8295 } 8296 8297 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 8298 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 8299 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 8300 verifyFormat("bool a = true, b = false;"); 8301 8302 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 8304 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 8305 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 8306 verifyFormat( 8307 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 8308 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 8309 " d = e && f;"); 8310 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 8311 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 8312 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8313 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 8314 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 8315 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 8316 8317 FormatStyle Style = getGoogleStyle(); 8318 Style.PointerAlignment = FormatStyle::PAS_Left; 8319 Style.DerivePointerAlignment = false; 8320 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8321 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 8322 " *b = bbbbbbbbbbbbbbbbbbb;", 8323 Style); 8324 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8325 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 8326 Style); 8327 verifyFormat("vector<int*> a, b;", Style); 8328 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 8329 } 8330 8331 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 8332 verifyFormat("arr[foo ? bar : baz];"); 8333 verifyFormat("f()[foo ? bar : baz];"); 8334 verifyFormat("(a + b)[foo ? bar : baz];"); 8335 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 8336 } 8337 8338 TEST_F(FormatTest, AlignsStringLiterals) { 8339 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 8340 " \"short literal\");"); 8341 verifyFormat( 8342 "looooooooooooooooooooooooongFunction(\n" 8343 " \"short literal\"\n" 8344 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 8345 verifyFormat("someFunction(\"Always break between multi-line\"\n" 8346 " \" string literals\",\n" 8347 " and, other, parameters);"); 8348 EXPECT_EQ("fun + \"1243\" /* comment */\n" 8349 " \"5678\";", 8350 format("fun + \"1243\" /* comment */\n" 8351 " \"5678\";", 8352 getLLVMStyleWithColumns(28))); 8353 EXPECT_EQ( 8354 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8355 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 8356 " \"aaaaaaaaaaaaaaaa\";", 8357 format("aaaaaa =" 8358 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8359 "aaaaaaaaaaaaaaaaaaaaa\" " 8360 "\"aaaaaaaaaaaaaaaa\";")); 8361 verifyFormat("a = a + \"a\"\n" 8362 " \"a\"\n" 8363 " \"a\";"); 8364 verifyFormat("f(\"a\", \"b\"\n" 8365 " \"c\");"); 8366 8367 verifyFormat( 8368 "#define LL_FORMAT \"ll\"\n" 8369 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 8370 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 8371 8372 verifyFormat("#define A(X) \\\n" 8373 " \"aaaaa\" #X \"bbbbbb\" \\\n" 8374 " \"ccccc\"", 8375 getLLVMStyleWithColumns(23)); 8376 verifyFormat("#define A \"def\"\n" 8377 "f(\"abc\" A \"ghi\"\n" 8378 " \"jkl\");"); 8379 8380 verifyFormat("f(L\"a\"\n" 8381 " L\"b\");"); 8382 verifyFormat("#define A(X) \\\n" 8383 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 8384 " L\"ccccc\"", 8385 getLLVMStyleWithColumns(25)); 8386 8387 verifyFormat("f(@\"a\"\n" 8388 " @\"b\");"); 8389 verifyFormat("NSString s = @\"a\"\n" 8390 " @\"b\"\n" 8391 " @\"c\";"); 8392 verifyFormat("NSString s = @\"a\"\n" 8393 " \"b\"\n" 8394 " \"c\";"); 8395 } 8396 8397 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8398 FormatStyle Style = getLLVMStyle(); 8399 // No declarations or definitions should be moved to own line. 8400 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8401 verifyFormat("class A {\n" 8402 " int f() { return 1; }\n" 8403 " int g();\n" 8404 "};\n" 8405 "int f() { return 1; }\n" 8406 "int g();\n", 8407 Style); 8408 8409 // All declarations and definitions should have the return type moved to its 8410 // own line. 8411 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8412 Style.TypenameMacros = {"LIST"}; 8413 verifyFormat("SomeType\n" 8414 "funcdecl(LIST(uint64_t));", 8415 Style); 8416 verifyFormat("class E {\n" 8417 " int\n" 8418 " f() {\n" 8419 " return 1;\n" 8420 " }\n" 8421 " int\n" 8422 " g();\n" 8423 "};\n" 8424 "int\n" 8425 "f() {\n" 8426 " return 1;\n" 8427 "}\n" 8428 "int\n" 8429 "g();\n", 8430 Style); 8431 8432 // Top-level definitions, and no kinds of declarations should have the 8433 // return type moved to its own line. 8434 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8435 verifyFormat("class B {\n" 8436 " int f() { return 1; }\n" 8437 " int g();\n" 8438 "};\n" 8439 "int\n" 8440 "f() {\n" 8441 " return 1;\n" 8442 "}\n" 8443 "int g();\n", 8444 Style); 8445 8446 // Top-level definitions and declarations should have the return type moved 8447 // to its own line. 8448 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8449 verifyFormat("class C {\n" 8450 " int f() { return 1; }\n" 8451 " int g();\n" 8452 "};\n" 8453 "int\n" 8454 "f() {\n" 8455 " return 1;\n" 8456 "}\n" 8457 "int\n" 8458 "g();\n", 8459 Style); 8460 8461 // All definitions should have the return type moved to its own line, but no 8462 // kinds of declarations. 8463 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8464 verifyFormat("class D {\n" 8465 " int\n" 8466 " f() {\n" 8467 " return 1;\n" 8468 " }\n" 8469 " int g();\n" 8470 "};\n" 8471 "int\n" 8472 "f() {\n" 8473 " return 1;\n" 8474 "}\n" 8475 "int g();\n", 8476 Style); 8477 verifyFormat("const char *\n" 8478 "f(void) {\n" // Break here. 8479 " return \"\";\n" 8480 "}\n" 8481 "const char *bar(void);\n", // No break here. 8482 Style); 8483 verifyFormat("template <class T>\n" 8484 "T *\n" 8485 "f(T &c) {\n" // Break here. 8486 " return NULL;\n" 8487 "}\n" 8488 "template <class T> T *f(T &c);\n", // No break here. 8489 Style); 8490 verifyFormat("class C {\n" 8491 " int\n" 8492 " operator+() {\n" 8493 " return 1;\n" 8494 " }\n" 8495 " int\n" 8496 " operator()() {\n" 8497 " return 1;\n" 8498 " }\n" 8499 "};\n", 8500 Style); 8501 verifyFormat("void\n" 8502 "A::operator()() {}\n" 8503 "void\n" 8504 "A::operator>>() {}\n" 8505 "void\n" 8506 "A::operator+() {}\n" 8507 "void\n" 8508 "A::operator*() {}\n" 8509 "void\n" 8510 "A::operator->() {}\n" 8511 "void\n" 8512 "A::operator void *() {}\n" 8513 "void\n" 8514 "A::operator void &() {}\n" 8515 "void\n" 8516 "A::operator void &&() {}\n" 8517 "void\n" 8518 "A::operator char *() {}\n" 8519 "void\n" 8520 "A::operator[]() {}\n" 8521 "void\n" 8522 "A::operator!() {}\n" 8523 "void\n" 8524 "A::operator**() {}\n" 8525 "void\n" 8526 "A::operator<Foo> *() {}\n" 8527 "void\n" 8528 "A::operator<Foo> **() {}\n" 8529 "void\n" 8530 "A::operator<Foo> &() {}\n" 8531 "void\n" 8532 "A::operator void **() {}\n", 8533 Style); 8534 verifyFormat("constexpr auto\n" 8535 "operator()() const -> reference {}\n" 8536 "constexpr auto\n" 8537 "operator>>() const -> reference {}\n" 8538 "constexpr auto\n" 8539 "operator+() const -> reference {}\n" 8540 "constexpr auto\n" 8541 "operator*() const -> reference {}\n" 8542 "constexpr auto\n" 8543 "operator->() const -> reference {}\n" 8544 "constexpr auto\n" 8545 "operator++() const -> reference {}\n" 8546 "constexpr auto\n" 8547 "operator void *() const -> reference {}\n" 8548 "constexpr auto\n" 8549 "operator void **() const -> reference {}\n" 8550 "constexpr auto\n" 8551 "operator void *() const -> reference {}\n" 8552 "constexpr auto\n" 8553 "operator void &() const -> reference {}\n" 8554 "constexpr auto\n" 8555 "operator void &&() const -> reference {}\n" 8556 "constexpr auto\n" 8557 "operator char *() const -> reference {}\n" 8558 "constexpr auto\n" 8559 "operator!() const -> reference {}\n" 8560 "constexpr auto\n" 8561 "operator[]() const -> reference {}\n", 8562 Style); 8563 verifyFormat("void *operator new(std::size_t s);", // No break here. 8564 Style); 8565 verifyFormat("void *\n" 8566 "operator new(std::size_t s) {}", 8567 Style); 8568 verifyFormat("void *\n" 8569 "operator delete[](void *ptr) {}", 8570 Style); 8571 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8572 verifyFormat("const char *\n" 8573 "f(void)\n" // Break here. 8574 "{\n" 8575 " return \"\";\n" 8576 "}\n" 8577 "const char *bar(void);\n", // No break here. 8578 Style); 8579 verifyFormat("template <class T>\n" 8580 "T *\n" // Problem here: no line break 8581 "f(T &c)\n" // Break here. 8582 "{\n" 8583 " return NULL;\n" 8584 "}\n" 8585 "template <class T> T *f(T &c);\n", // No break here. 8586 Style); 8587 verifyFormat("int\n" 8588 "foo(A<bool> a)\n" 8589 "{\n" 8590 " return a;\n" 8591 "}\n", 8592 Style); 8593 verifyFormat("int\n" 8594 "foo(A<8> a)\n" 8595 "{\n" 8596 " return a;\n" 8597 "}\n", 8598 Style); 8599 verifyFormat("int\n" 8600 "foo(A<B<bool>, 8> a)\n" 8601 "{\n" 8602 " return a;\n" 8603 "}\n", 8604 Style); 8605 verifyFormat("int\n" 8606 "foo(A<B<8>, bool> a)\n" 8607 "{\n" 8608 " return a;\n" 8609 "}\n", 8610 Style); 8611 verifyFormat("int\n" 8612 "foo(A<B<bool>, bool> a)\n" 8613 "{\n" 8614 " return a;\n" 8615 "}\n", 8616 Style); 8617 verifyFormat("int\n" 8618 "foo(A<B<8>, 8> a)\n" 8619 "{\n" 8620 " return a;\n" 8621 "}\n", 8622 Style); 8623 8624 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8625 Style.BraceWrapping.AfterFunction = true; 8626 verifyFormat("int f(i);\n" // No break here. 8627 "int\n" // Break here. 8628 "f(i)\n" 8629 "{\n" 8630 " return i + 1;\n" 8631 "}\n" 8632 "int\n" // Break here. 8633 "f(i)\n" 8634 "{\n" 8635 " return i + 1;\n" 8636 "};", 8637 Style); 8638 verifyFormat("int f(a, b, c);\n" // No break here. 8639 "int\n" // Break here. 8640 "f(a, b, c)\n" // Break here. 8641 "short a, b;\n" 8642 "float c;\n" 8643 "{\n" 8644 " return a + b < c;\n" 8645 "}\n" 8646 "int\n" // Break here. 8647 "f(a, b, c)\n" // Break here. 8648 "short a, b;\n" 8649 "float c;\n" 8650 "{\n" 8651 " return a + b < c;\n" 8652 "};", 8653 Style); 8654 verifyFormat("byte *\n" // Break here. 8655 "f(a)\n" // Break here. 8656 "byte a[];\n" 8657 "{\n" 8658 " return a;\n" 8659 "}", 8660 Style); 8661 verifyFormat("bool f(int a, int) override;\n" 8662 "Bar g(int a, Bar) final;\n" 8663 "Bar h(a, Bar) final;", 8664 Style); 8665 verifyFormat("int\n" 8666 "f(a)", 8667 Style); 8668 verifyFormat("bool\n" 8669 "f(size_t = 0, bool b = false)\n" 8670 "{\n" 8671 " return !b;\n" 8672 "}", 8673 Style); 8674 8675 // The return breaking style doesn't affect: 8676 // * function and object definitions with attribute-like macros 8677 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8678 " ABSL_GUARDED_BY(mutex) = {};", 8679 getGoogleStyleWithColumns(40)); 8680 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8681 " ABSL_GUARDED_BY(mutex); // comment", 8682 getGoogleStyleWithColumns(40)); 8683 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8684 " ABSL_GUARDED_BY(mutex1)\n" 8685 " ABSL_GUARDED_BY(mutex2);", 8686 getGoogleStyleWithColumns(40)); 8687 verifyFormat("Tttttt f(int a, int b)\n" 8688 " ABSL_GUARDED_BY(mutex1)\n" 8689 " ABSL_GUARDED_BY(mutex2);", 8690 getGoogleStyleWithColumns(40)); 8691 // * typedefs 8692 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8693 8694 Style = getGNUStyle(); 8695 8696 // Test for comments at the end of function declarations. 8697 verifyFormat("void\n" 8698 "foo (int a, /*abc*/ int b) // def\n" 8699 "{\n" 8700 "}\n", 8701 Style); 8702 8703 verifyFormat("void\n" 8704 "foo (int a, /* abc */ int b) /* def */\n" 8705 "{\n" 8706 "}\n", 8707 Style); 8708 8709 // Definitions that should not break after return type 8710 verifyFormat("void foo (int a, int b); // def\n", Style); 8711 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8712 verifyFormat("void foo (int a, int b);\n", Style); 8713 } 8714 8715 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8716 FormatStyle NoBreak = getLLVMStyle(); 8717 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8718 FormatStyle Break = getLLVMStyle(); 8719 Break.AlwaysBreakBeforeMultilineStrings = true; 8720 verifyFormat("aaaa = \"bbbb\"\n" 8721 " \"cccc\";", 8722 NoBreak); 8723 verifyFormat("aaaa =\n" 8724 " \"bbbb\"\n" 8725 " \"cccc\";", 8726 Break); 8727 verifyFormat("aaaa(\"bbbb\"\n" 8728 " \"cccc\");", 8729 NoBreak); 8730 verifyFormat("aaaa(\n" 8731 " \"bbbb\"\n" 8732 " \"cccc\");", 8733 Break); 8734 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8735 " \"cccc\");", 8736 NoBreak); 8737 verifyFormat("aaaa(qqq,\n" 8738 " \"bbbb\"\n" 8739 " \"cccc\");", 8740 Break); 8741 verifyFormat("aaaa(qqq,\n" 8742 " L\"bbbb\"\n" 8743 " L\"cccc\");", 8744 Break); 8745 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8746 " \"bbbb\"));", 8747 Break); 8748 verifyFormat("string s = someFunction(\n" 8749 " \"abc\"\n" 8750 " \"abc\");", 8751 Break); 8752 8753 // As we break before unary operators, breaking right after them is bad. 8754 verifyFormat("string foo = abc ? \"x\"\n" 8755 " \"blah blah blah blah blah blah\"\n" 8756 " : \"y\";", 8757 Break); 8758 8759 // Don't break if there is no column gain. 8760 verifyFormat("f(\"aaaa\"\n" 8761 " \"bbbb\");", 8762 Break); 8763 8764 // Treat literals with escaped newlines like multi-line string literals. 8765 EXPECT_EQ("x = \"a\\\n" 8766 "b\\\n" 8767 "c\";", 8768 format("x = \"a\\\n" 8769 "b\\\n" 8770 "c\";", 8771 NoBreak)); 8772 EXPECT_EQ("xxxx =\n" 8773 " \"a\\\n" 8774 "b\\\n" 8775 "c\";", 8776 format("xxxx = \"a\\\n" 8777 "b\\\n" 8778 "c\";", 8779 Break)); 8780 8781 EXPECT_EQ("NSString *const kString =\n" 8782 " @\"aaaa\"\n" 8783 " @\"bbbb\";", 8784 format("NSString *const kString = @\"aaaa\"\n" 8785 "@\"bbbb\";", 8786 Break)); 8787 8788 Break.ColumnLimit = 0; 8789 verifyFormat("const char *hello = \"hello llvm\";", Break); 8790 } 8791 8792 TEST_F(FormatTest, AlignsPipes) { 8793 verifyFormat( 8794 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8795 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8796 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8797 verifyFormat( 8798 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8799 " << aaaaaaaaaaaaaaaaaaaa;"); 8800 verifyFormat( 8801 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8802 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8803 verifyFormat( 8804 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8805 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8806 verifyFormat( 8807 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8808 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8809 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8810 verifyFormat( 8811 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8812 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8813 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8814 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8816 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8817 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8818 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8819 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8820 verifyFormat( 8821 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8822 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8823 verifyFormat( 8824 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8825 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8826 8827 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8828 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8829 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8830 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8831 " aaaaaaaaaaaaaaaaaaaaa)\n" 8832 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8833 verifyFormat("LOG_IF(aaa == //\n" 8834 " bbb)\n" 8835 " << a << b;"); 8836 8837 // But sometimes, breaking before the first "<<" is desirable. 8838 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8839 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8840 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8841 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8842 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8843 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8844 " << BEF << IsTemplate << Description << E->getType();"); 8845 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8846 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8847 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8848 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8849 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8851 " << aaa;"); 8852 8853 verifyFormat( 8854 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8855 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8856 8857 // Incomplete string literal. 8858 EXPECT_EQ("llvm::errs() << \"\n" 8859 " << a;", 8860 format("llvm::errs() << \"\n<<a;")); 8861 8862 verifyFormat("void f() {\n" 8863 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8864 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8865 "}"); 8866 8867 // Handle 'endl'. 8868 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8869 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8870 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8871 8872 // Handle '\n'. 8873 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8874 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8875 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8876 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8877 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8878 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8879 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8880 } 8881 8882 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8883 verifyFormat("return out << \"somepacket = {\\n\"\n" 8884 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8885 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8886 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8887 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8888 " << \"}\";"); 8889 8890 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8891 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8892 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8893 verifyFormat( 8894 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8895 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8896 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8897 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8898 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8899 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8900 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8901 verifyFormat( 8902 "void f() {\n" 8903 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8904 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8905 "}"); 8906 8907 // Breaking before the first "<<" is generally not desirable. 8908 verifyFormat( 8909 "llvm::errs()\n" 8910 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8911 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8912 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8913 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8914 getLLVMStyleWithColumns(70)); 8915 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8916 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8917 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8918 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8919 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8920 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8921 getLLVMStyleWithColumns(70)); 8922 8923 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8924 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8925 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8926 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8927 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8928 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8929 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8930 " (aaaa + aaaa);", 8931 getLLVMStyleWithColumns(40)); 8932 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8933 " (aaaaaaa + aaaaa));", 8934 getLLVMStyleWithColumns(40)); 8935 verifyFormat( 8936 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8937 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8938 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8939 } 8940 8941 TEST_F(FormatTest, UnderstandsEquals) { 8942 verifyFormat( 8943 "aaaaaaaaaaaaaaaaa =\n" 8944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8945 verifyFormat( 8946 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8947 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8948 verifyFormat( 8949 "if (a) {\n" 8950 " f();\n" 8951 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8952 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8953 "}"); 8954 8955 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8956 " 100000000 + 10000000) {\n}"); 8957 } 8958 8959 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8960 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8961 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8962 8963 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8964 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8965 8966 verifyFormat( 8967 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8968 " Parameter2);"); 8969 8970 verifyFormat( 8971 "ShortObject->shortFunction(\n" 8972 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8973 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8974 8975 verifyFormat("loooooooooooooongFunction(\n" 8976 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8977 8978 verifyFormat( 8979 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8980 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8981 8982 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8983 " .WillRepeatedly(Return(SomeValue));"); 8984 verifyFormat("void f() {\n" 8985 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8986 " .Times(2)\n" 8987 " .WillRepeatedly(Return(SomeValue));\n" 8988 "}"); 8989 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8990 " ccccccccccccccccccccccc);"); 8991 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8992 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8993 " .aaaaa(aaaaa),\n" 8994 " aaaaaaaaaaaaaaaaaaaaa);"); 8995 verifyFormat("void f() {\n" 8996 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8997 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8998 "}"); 8999 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9001 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9002 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9003 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9004 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9005 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9006 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9007 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 9008 "}"); 9009 9010 // Here, it is not necessary to wrap at "." or "->". 9011 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 9012 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 9013 verifyFormat( 9014 "aaaaaaaaaaa->aaaaaaaaa(\n" 9015 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9016 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 9017 9018 verifyFormat( 9019 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9020 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 9021 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 9022 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 9023 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 9024 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 9025 9026 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9027 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9028 " .a();"); 9029 9030 FormatStyle NoBinPacking = getLLVMStyle(); 9031 NoBinPacking.BinPackParameters = false; 9032 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 9033 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 9034 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 9035 " aaaaaaaaaaaaaaaaaaa,\n" 9036 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9037 NoBinPacking); 9038 9039 // If there is a subsequent call, change to hanging indentation. 9040 verifyFormat( 9041 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9042 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 9043 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9044 verifyFormat( 9045 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9046 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 9047 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9049 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9050 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9052 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9053 } 9054 9055 TEST_F(FormatTest, WrapsTemplateDeclarations) { 9056 verifyFormat("template <typename T>\n" 9057 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9058 verifyFormat("template <typename T>\n" 9059 "// T should be one of {A, B}.\n" 9060 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9061 verifyFormat( 9062 "template <typename T>\n" 9063 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 9064 verifyFormat("template <typename T>\n" 9065 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 9066 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 9067 verifyFormat( 9068 "template <typename T>\n" 9069 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 9070 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 9071 verifyFormat( 9072 "template <typename T>\n" 9073 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 9074 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 9075 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9076 verifyFormat("template <typename T>\n" 9077 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9078 " int aaaaaaaaaaaaaaaaaaaaaa);"); 9079 verifyFormat( 9080 "template <typename T1, typename T2 = char, typename T3 = char,\n" 9081 " typename T4 = char>\n" 9082 "void f();"); 9083 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 9084 " template <typename> class cccccccccccccccccccccc,\n" 9085 " typename ddddddddddddd>\n" 9086 "class C {};"); 9087 verifyFormat( 9088 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 9089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9090 9091 verifyFormat("void f() {\n" 9092 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 9093 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 9094 "}"); 9095 9096 verifyFormat("template <typename T> class C {};"); 9097 verifyFormat("template <typename T> void f();"); 9098 verifyFormat("template <typename T> void f() {}"); 9099 verifyFormat( 9100 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9102 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 9103 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 9106 " bbbbbbbbbbbbbbbbbbbbbbbb);", 9107 getLLVMStyleWithColumns(72)); 9108 EXPECT_EQ("static_cast<A< //\n" 9109 " B> *>(\n" 9110 "\n" 9111 ");", 9112 format("static_cast<A<//\n" 9113 " B>*>(\n" 9114 "\n" 9115 " );")); 9116 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9117 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 9118 9119 FormatStyle AlwaysBreak = getLLVMStyle(); 9120 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9121 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 9122 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 9123 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 9124 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9125 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9126 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 9127 verifyFormat("template <template <typename> class Fooooooo,\n" 9128 " template <typename> class Baaaaaaar>\n" 9129 "struct C {};", 9130 AlwaysBreak); 9131 verifyFormat("template <typename T> // T can be A, B or C.\n" 9132 "struct C {};", 9133 AlwaysBreak); 9134 verifyFormat("template <enum E> class A {\n" 9135 "public:\n" 9136 " E *f();\n" 9137 "};"); 9138 9139 FormatStyle NeverBreak = getLLVMStyle(); 9140 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 9141 verifyFormat("template <typename T> class C {};", NeverBreak); 9142 verifyFormat("template <typename T> void f();", NeverBreak); 9143 verifyFormat("template <typename T> void f() {}", NeverBreak); 9144 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9145 "bbbbbbbbbbbbbbbbbbbb) {}", 9146 NeverBreak); 9147 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9148 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9149 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 9150 NeverBreak); 9151 verifyFormat("template <template <typename> class Fooooooo,\n" 9152 " template <typename> class Baaaaaaar>\n" 9153 "struct C {};", 9154 NeverBreak); 9155 verifyFormat("template <typename T> // T can be A, B or C.\n" 9156 "struct C {};", 9157 NeverBreak); 9158 verifyFormat("template <enum E> class A {\n" 9159 "public:\n" 9160 " E *f();\n" 9161 "};", 9162 NeverBreak); 9163 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 9164 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9165 "bbbbbbbbbbbbbbbbbbbb) {}", 9166 NeverBreak); 9167 } 9168 9169 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 9170 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 9171 Style.ColumnLimit = 60; 9172 EXPECT_EQ("// Baseline - no comments.\n" 9173 "template <\n" 9174 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9175 "void f() {}", 9176 format("// Baseline - no comments.\n" 9177 "template <\n" 9178 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9179 "void f() {}", 9180 Style)); 9181 9182 EXPECT_EQ("template <\n" 9183 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9184 "void f() {}", 9185 format("template <\n" 9186 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9187 "void f() {}", 9188 Style)); 9189 9190 EXPECT_EQ( 9191 "template <\n" 9192 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9193 "void f() {}", 9194 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9195 "void f() {}", 9196 Style)); 9197 9198 EXPECT_EQ( 9199 "template <\n" 9200 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9201 " // multiline\n" 9202 "void f() {}", 9203 format("template <\n" 9204 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9205 " // multiline\n" 9206 "void f() {}", 9207 Style)); 9208 9209 EXPECT_EQ( 9210 "template <typename aaaaaaaaaa<\n" 9211 " bbbbbbbbbbbb>::value> // trailing loooong\n" 9212 "void f() {}", 9213 format( 9214 "template <\n" 9215 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 9216 "void f() {}", 9217 Style)); 9218 } 9219 9220 TEST_F(FormatTest, WrapsTemplateParameters) { 9221 FormatStyle Style = getLLVMStyle(); 9222 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9223 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9224 verifyFormat( 9225 "template <typename... a> struct q {};\n" 9226 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9227 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9228 " y;", 9229 Style); 9230 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9231 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9232 verifyFormat( 9233 "template <typename... a> struct r {};\n" 9234 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9235 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9236 " y;", 9237 Style); 9238 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9239 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9240 verifyFormat("template <typename... a> struct s {};\n" 9241 "extern s<\n" 9242 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9243 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9244 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9245 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9246 " y;", 9247 Style); 9248 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9249 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9250 verifyFormat("template <typename... a> struct t {};\n" 9251 "extern t<\n" 9252 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9253 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9254 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9255 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9256 " y;", 9257 Style); 9258 } 9259 9260 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 9261 verifyFormat( 9262 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9263 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9264 verifyFormat( 9265 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9267 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9268 9269 // FIXME: Should we have the extra indent after the second break? 9270 verifyFormat( 9271 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9274 9275 verifyFormat( 9276 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 9277 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 9278 9279 // Breaking at nested name specifiers is generally not desirable. 9280 verifyFormat( 9281 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9282 " aaaaaaaaaaaaaaaaaaaaaaa);"); 9283 9284 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 9285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9286 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9287 " aaaaaaaaaaaaaaaaaaaaa);", 9288 getLLVMStyleWithColumns(74)); 9289 9290 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9292 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9293 } 9294 9295 TEST_F(FormatTest, UnderstandsTemplateParameters) { 9296 verifyFormat("A<int> a;"); 9297 verifyFormat("A<A<A<int>>> a;"); 9298 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 9299 verifyFormat("bool x = a < 1 || 2 > a;"); 9300 verifyFormat("bool x = 5 < f<int>();"); 9301 verifyFormat("bool x = f<int>() > 5;"); 9302 verifyFormat("bool x = 5 < a<int>::x;"); 9303 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 9304 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 9305 9306 verifyGoogleFormat("A<A<int>> a;"); 9307 verifyGoogleFormat("A<A<A<int>>> a;"); 9308 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 9309 verifyGoogleFormat("A<A<int> > a;"); 9310 verifyGoogleFormat("A<A<A<int> > > a;"); 9311 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 9312 verifyGoogleFormat("A<::A<int>> a;"); 9313 verifyGoogleFormat("A<::A> a;"); 9314 verifyGoogleFormat("A< ::A> a;"); 9315 verifyGoogleFormat("A< ::A<int> > a;"); 9316 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 9317 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 9318 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 9319 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 9320 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 9321 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 9322 9323 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 9324 9325 // template closer followed by a token that starts with > or = 9326 verifyFormat("bool b = a<1> > 1;"); 9327 verifyFormat("bool b = a<1> >= 1;"); 9328 verifyFormat("int i = a<1> >> 1;"); 9329 FormatStyle Style = getLLVMStyle(); 9330 Style.SpaceBeforeAssignmentOperators = false; 9331 verifyFormat("bool b= a<1> == 1;", Style); 9332 verifyFormat("a<int> = 1;", Style); 9333 verifyFormat("a<int> >>= 1;", Style); 9334 9335 verifyFormat("test < a | b >> c;"); 9336 verifyFormat("test<test<a | b>> c;"); 9337 verifyFormat("test >> a >> b;"); 9338 verifyFormat("test << a >> b;"); 9339 9340 verifyFormat("f<int>();"); 9341 verifyFormat("template <typename T> void f() {}"); 9342 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 9343 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 9344 "sizeof(char)>::type>;"); 9345 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 9346 verifyFormat("f(a.operator()<A>());"); 9347 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9348 " .template operator()<A>());", 9349 getLLVMStyleWithColumns(35)); 9350 9351 // Not template parameters. 9352 verifyFormat("return a < b && c > d;"); 9353 verifyFormat("void f() {\n" 9354 " while (a < b && c > d) {\n" 9355 " }\n" 9356 "}"); 9357 verifyFormat("template <typename... Types>\n" 9358 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 9359 9360 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 9362 getLLVMStyleWithColumns(60)); 9363 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 9364 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 9365 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 9366 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 9367 } 9368 9369 TEST_F(FormatTest, UnderstandsShiftOperators) { 9370 verifyFormat("if (i < x >> 1)"); 9371 verifyFormat("while (i < x >> 1)"); 9372 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 9373 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 9374 verifyFormat( 9375 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 9376 verifyFormat("Foo.call<Bar<Function>>()"); 9377 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 9378 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 9379 "++i, v = v >> 1)"); 9380 verifyFormat("if (w<u<v<x>>, 1>::t)"); 9381 } 9382 9383 TEST_F(FormatTest, BitshiftOperatorWidth) { 9384 EXPECT_EQ("int a = 1 << 2; /* foo\n" 9385 " bar */", 9386 format("int a=1<<2; /* foo\n" 9387 " bar */")); 9388 9389 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 9390 " bar */", 9391 format("int b =256>>1 ; /* foo\n" 9392 " bar */")); 9393 } 9394 9395 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9396 verifyFormat("COMPARE(a, ==, b);"); 9397 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9398 } 9399 9400 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9401 verifyFormat("int A::*x;"); 9402 verifyFormat("int (S::*func)(void *);"); 9403 verifyFormat("void f() { int (S::*func)(void *); }"); 9404 verifyFormat("typedef bool *(Class::*Member)() const;"); 9405 verifyFormat("void f() {\n" 9406 " (a->*f)();\n" 9407 " a->*x;\n" 9408 " (a.*f)();\n" 9409 " ((*a).*f)();\n" 9410 " a.*x;\n" 9411 "}"); 9412 verifyFormat("void f() {\n" 9413 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9414 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9415 "}"); 9416 verifyFormat( 9417 "(aaaaaaaaaa->*bbbbbbb)(\n" 9418 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9419 FormatStyle Style = getLLVMStyle(); 9420 Style.PointerAlignment = FormatStyle::PAS_Left; 9421 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9422 } 9423 9424 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9425 verifyFormat("int a = -2;"); 9426 verifyFormat("f(-1, -2, -3);"); 9427 verifyFormat("a[-1] = 5;"); 9428 verifyFormat("int a = 5 + -2;"); 9429 verifyFormat("if (i == -1) {\n}"); 9430 verifyFormat("if (i != -1) {\n}"); 9431 verifyFormat("if (i > -1) {\n}"); 9432 verifyFormat("if (i < -1) {\n}"); 9433 verifyFormat("++(a->f());"); 9434 verifyFormat("--(a->f());"); 9435 verifyFormat("(a->f())++;"); 9436 verifyFormat("a[42]++;"); 9437 verifyFormat("if (!(a->f())) {\n}"); 9438 verifyFormat("if (!+i) {\n}"); 9439 verifyFormat("~&a;"); 9440 9441 verifyFormat("a-- > b;"); 9442 verifyFormat("b ? -a : c;"); 9443 verifyFormat("n * sizeof char16;"); 9444 verifyFormat("n * alignof char16;", getGoogleStyle()); 9445 verifyFormat("sizeof(char);"); 9446 verifyFormat("alignof(char);", getGoogleStyle()); 9447 9448 verifyFormat("return -1;"); 9449 verifyFormat("throw -1;"); 9450 verifyFormat("switch (a) {\n" 9451 "case -1:\n" 9452 " break;\n" 9453 "}"); 9454 verifyFormat("#define X -1"); 9455 verifyFormat("#define X -kConstant"); 9456 9457 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9458 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9459 9460 verifyFormat("int a = /* confusing comment */ -1;"); 9461 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9462 verifyFormat("int a = i /* confusing comment */++;"); 9463 9464 verifyFormat("co_yield -1;"); 9465 verifyFormat("co_return -1;"); 9466 9467 // Check that * is not treated as a binary operator when we set 9468 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9469 FormatStyle PASLeftStyle = getLLVMStyle(); 9470 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9471 verifyFormat("co_return *a;", PASLeftStyle); 9472 verifyFormat("co_await *a;", PASLeftStyle); 9473 verifyFormat("co_yield *a", PASLeftStyle); 9474 verifyFormat("return *a;", PASLeftStyle); 9475 } 9476 9477 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9478 verifyFormat("if (!aaaaaaaaaa( // break\n" 9479 " aaaaa)) {\n" 9480 "}"); 9481 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9482 " aaaaa));"); 9483 verifyFormat("*aaa = aaaaaaa( // break\n" 9484 " bbbbbb);"); 9485 } 9486 9487 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9488 verifyFormat("bool operator<();"); 9489 verifyFormat("bool operator>();"); 9490 verifyFormat("bool operator=();"); 9491 verifyFormat("bool operator==();"); 9492 verifyFormat("bool operator!=();"); 9493 verifyFormat("int operator+();"); 9494 verifyFormat("int operator++();"); 9495 verifyFormat("int operator++(int) volatile noexcept;"); 9496 verifyFormat("bool operator,();"); 9497 verifyFormat("bool operator();"); 9498 verifyFormat("bool operator()();"); 9499 verifyFormat("bool operator[]();"); 9500 verifyFormat("operator bool();"); 9501 verifyFormat("operator int();"); 9502 verifyFormat("operator void *();"); 9503 verifyFormat("operator SomeType<int>();"); 9504 verifyFormat("operator SomeType<int, int>();"); 9505 verifyFormat("operator SomeType<SomeType<int>>();"); 9506 verifyFormat("operator< <>();"); 9507 verifyFormat("operator<< <>();"); 9508 verifyFormat("< <>"); 9509 9510 verifyFormat("void *operator new(std::size_t size);"); 9511 verifyFormat("void *operator new[](std::size_t size);"); 9512 verifyFormat("void operator delete(void *ptr);"); 9513 verifyFormat("void operator delete[](void *ptr);"); 9514 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9515 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9516 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9517 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9518 9519 verifyFormat( 9520 "ostream &operator<<(ostream &OutputStream,\n" 9521 " SomeReallyLongType WithSomeReallyLongValue);"); 9522 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9523 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9524 " return left.group < right.group;\n" 9525 "}"); 9526 verifyFormat("SomeType &operator=(const SomeType &S);"); 9527 verifyFormat("f.template operator()<int>();"); 9528 9529 verifyGoogleFormat("operator void*();"); 9530 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9531 verifyGoogleFormat("operator ::A();"); 9532 9533 verifyFormat("using A::operator+;"); 9534 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9535 "int i;"); 9536 9537 // Calling an operator as a member function. 9538 verifyFormat("void f() { a.operator*(); }"); 9539 verifyFormat("void f() { a.operator*(b & b); }"); 9540 verifyFormat("void f() { a->operator&(a * b); }"); 9541 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9542 // TODO: Calling an operator as a non-member function is hard to distinguish. 9543 // https://llvm.org/PR50629 9544 // verifyFormat("void f() { operator*(a & a); }"); 9545 // verifyFormat("void f() { operator&(a, b * b); }"); 9546 9547 verifyFormat("::operator delete(foo);"); 9548 verifyFormat("::operator new(n * sizeof(foo));"); 9549 verifyFormat("foo() { ::operator delete(foo); }"); 9550 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 9551 } 9552 9553 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9554 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9555 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9556 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9557 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9558 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9559 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9560 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9561 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9562 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9563 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9564 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9565 verifyFormat("void Fn(T const &) const &;"); 9566 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9567 verifyFormat("template <typename T>\n" 9568 "void F(T) && = delete;", 9569 getGoogleStyle()); 9570 9571 FormatStyle AlignLeft = getLLVMStyle(); 9572 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9573 verifyFormat("void A::b() && {}", AlignLeft); 9574 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9575 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9576 AlignLeft); 9577 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9578 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9579 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9580 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9581 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9582 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9583 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9584 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9585 9586 FormatStyle Spaces = getLLVMStyle(); 9587 Spaces.SpacesInCStyleCastParentheses = true; 9588 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9589 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9590 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9591 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9592 9593 Spaces.SpacesInCStyleCastParentheses = false; 9594 Spaces.SpacesInParentheses = true; 9595 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9596 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9597 Spaces); 9598 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9599 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9600 9601 FormatStyle BreakTemplate = getLLVMStyle(); 9602 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9603 9604 verifyFormat("struct f {\n" 9605 " template <class T>\n" 9606 " int &foo(const std::string &str) &noexcept {}\n" 9607 "};", 9608 BreakTemplate); 9609 9610 verifyFormat("struct f {\n" 9611 " template <class T>\n" 9612 " int &foo(const std::string &str) &&noexcept {}\n" 9613 "};", 9614 BreakTemplate); 9615 9616 verifyFormat("struct f {\n" 9617 " template <class T>\n" 9618 " int &foo(const std::string &str) const &noexcept {}\n" 9619 "};", 9620 BreakTemplate); 9621 9622 verifyFormat("struct f {\n" 9623 " template <class T>\n" 9624 " int &foo(const std::string &str) const &noexcept {}\n" 9625 "};", 9626 BreakTemplate); 9627 9628 verifyFormat("struct f {\n" 9629 " template <class T>\n" 9630 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9631 "};", 9632 BreakTemplate); 9633 9634 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9635 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9636 FormatStyle::BTDS_Yes; 9637 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9638 9639 verifyFormat("struct f {\n" 9640 " template <class T>\n" 9641 " int& foo(const std::string& str) & noexcept {}\n" 9642 "};", 9643 AlignLeftBreakTemplate); 9644 9645 verifyFormat("struct f {\n" 9646 " template <class T>\n" 9647 " int& foo(const std::string& str) && noexcept {}\n" 9648 "};", 9649 AlignLeftBreakTemplate); 9650 9651 verifyFormat("struct f {\n" 9652 " template <class T>\n" 9653 " int& foo(const std::string& str) const& noexcept {}\n" 9654 "};", 9655 AlignLeftBreakTemplate); 9656 9657 verifyFormat("struct f {\n" 9658 " template <class T>\n" 9659 " int& foo(const std::string& str) const&& noexcept {}\n" 9660 "};", 9661 AlignLeftBreakTemplate); 9662 9663 verifyFormat("struct f {\n" 9664 " template <class T>\n" 9665 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9666 "};", 9667 AlignLeftBreakTemplate); 9668 9669 // The `&` in `Type&` should not be confused with a trailing `&` of 9670 // DEPRECATED(reason) member function. 9671 verifyFormat("struct f {\n" 9672 " template <class T>\n" 9673 " DEPRECATED(reason)\n" 9674 " Type &foo(arguments) {}\n" 9675 "};", 9676 BreakTemplate); 9677 9678 verifyFormat("struct f {\n" 9679 " template <class T>\n" 9680 " DEPRECATED(reason)\n" 9681 " Type& foo(arguments) {}\n" 9682 "};", 9683 AlignLeftBreakTemplate); 9684 9685 verifyFormat("void (*foopt)(int) = &func;"); 9686 } 9687 9688 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9689 verifyFormat("void f() {\n" 9690 " A *a = new A;\n" 9691 " A *a = new (placement) A;\n" 9692 " delete a;\n" 9693 " delete (A *)a;\n" 9694 "}"); 9695 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9696 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9697 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9698 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9699 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9700 verifyFormat("delete[] h->p;"); 9701 9702 verifyFormat("void operator delete(void *foo) ATTRIB;"); 9703 verifyFormat("void operator new(void *foo) ATTRIB;"); 9704 verifyFormat("void operator delete[](void *foo) ATTRIB;"); 9705 verifyFormat("void operator delete(void *ptr) noexcept;"); 9706 } 9707 9708 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9709 verifyFormat("int *f(int *a) {}"); 9710 verifyFormat("int main(int argc, char **argv) {}"); 9711 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9712 verifyIndependentOfContext("f(a, *a);"); 9713 verifyFormat("void g() { f(*a); }"); 9714 verifyIndependentOfContext("int a = b * 10;"); 9715 verifyIndependentOfContext("int a = 10 * b;"); 9716 verifyIndependentOfContext("int a = b * c;"); 9717 verifyIndependentOfContext("int a += b * c;"); 9718 verifyIndependentOfContext("int a -= b * c;"); 9719 verifyIndependentOfContext("int a *= b * c;"); 9720 verifyIndependentOfContext("int a /= b * c;"); 9721 verifyIndependentOfContext("int a = *b;"); 9722 verifyIndependentOfContext("int a = *b * c;"); 9723 verifyIndependentOfContext("int a = b * *c;"); 9724 verifyIndependentOfContext("int a = b * (10);"); 9725 verifyIndependentOfContext("S << b * (10);"); 9726 verifyIndependentOfContext("return 10 * b;"); 9727 verifyIndependentOfContext("return *b * *c;"); 9728 verifyIndependentOfContext("return a & ~b;"); 9729 verifyIndependentOfContext("f(b ? *c : *d);"); 9730 verifyIndependentOfContext("int a = b ? *c : *d;"); 9731 verifyIndependentOfContext("*b = a;"); 9732 verifyIndependentOfContext("a * ~b;"); 9733 verifyIndependentOfContext("a * !b;"); 9734 verifyIndependentOfContext("a * +b;"); 9735 verifyIndependentOfContext("a * -b;"); 9736 verifyIndependentOfContext("a * ++b;"); 9737 verifyIndependentOfContext("a * --b;"); 9738 verifyIndependentOfContext("a[4] * b;"); 9739 verifyIndependentOfContext("a[a * a] = 1;"); 9740 verifyIndependentOfContext("f() * b;"); 9741 verifyIndependentOfContext("a * [self dostuff];"); 9742 verifyIndependentOfContext("int x = a * (a + b);"); 9743 verifyIndependentOfContext("(a *)(a + b);"); 9744 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9745 verifyIndependentOfContext("int *pa = (int *)&a;"); 9746 verifyIndependentOfContext("return sizeof(int **);"); 9747 verifyIndependentOfContext("return sizeof(int ******);"); 9748 verifyIndependentOfContext("return (int **&)a;"); 9749 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9750 verifyFormat("void f(Type (*parameter)[10]) {}"); 9751 verifyFormat("void f(Type (¶meter)[10]) {}"); 9752 verifyGoogleFormat("return sizeof(int**);"); 9753 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9754 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9755 verifyFormat("auto a = [](int **&, int ***) {};"); 9756 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9757 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9758 verifyFormat("[](const decltype(*a) &value) {}"); 9759 verifyFormat("[](const typeof(*a) &value) {}"); 9760 verifyFormat("[](const _Atomic(a *) &value) {}"); 9761 verifyFormat("[](const __underlying_type(a) &value) {}"); 9762 verifyFormat("decltype(a * b) F();"); 9763 verifyFormat("typeof(a * b) F();"); 9764 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9765 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9766 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9767 verifyIndependentOfContext("int i{a * b};"); 9768 verifyIndependentOfContext("aaa && aaa->f();"); 9769 verifyIndependentOfContext("int x = ~*p;"); 9770 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9771 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9772 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9773 verifyFormat("void f() { f(a, c * d); }"); 9774 verifyFormat("void f() { f(new a(), c * d); }"); 9775 verifyFormat("void f(const MyOverride &override);"); 9776 verifyFormat("void f(const MyFinal &final);"); 9777 verifyIndependentOfContext("bool a = f() && override.f();"); 9778 verifyIndependentOfContext("bool a = f() && final.f();"); 9779 9780 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9781 9782 verifyIndependentOfContext("A<int *> a;"); 9783 verifyIndependentOfContext("A<int **> a;"); 9784 verifyIndependentOfContext("A<int *, int *> a;"); 9785 verifyIndependentOfContext("A<int *[]> a;"); 9786 verifyIndependentOfContext( 9787 "const char *const p = reinterpret_cast<const char *const>(q);"); 9788 verifyIndependentOfContext("A<int **, int **> a;"); 9789 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9790 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9791 verifyFormat("for (; a && b;) {\n}"); 9792 verifyFormat("bool foo = true && [] { return false; }();"); 9793 9794 verifyFormat( 9795 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9797 9798 verifyGoogleFormat("int const* a = &b;"); 9799 verifyGoogleFormat("**outparam = 1;"); 9800 verifyGoogleFormat("*outparam = a * b;"); 9801 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9802 verifyGoogleFormat("A<int*> a;"); 9803 verifyGoogleFormat("A<int**> a;"); 9804 verifyGoogleFormat("A<int*, int*> a;"); 9805 verifyGoogleFormat("A<int**, int**> a;"); 9806 verifyGoogleFormat("f(b ? *c : *d);"); 9807 verifyGoogleFormat("int a = b ? *c : *d;"); 9808 verifyGoogleFormat("Type* t = **x;"); 9809 verifyGoogleFormat("Type* t = *++*x;"); 9810 verifyGoogleFormat("*++*x;"); 9811 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9812 verifyGoogleFormat("Type* t = x++ * y;"); 9813 verifyGoogleFormat( 9814 "const char* const p = reinterpret_cast<const char* const>(q);"); 9815 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9816 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9817 verifyGoogleFormat("template <typename T>\n" 9818 "void f(int i = 0, SomeType** temps = NULL);"); 9819 9820 FormatStyle Left = getLLVMStyle(); 9821 Left.PointerAlignment = FormatStyle::PAS_Left; 9822 verifyFormat("x = *a(x) = *a(y);", Left); 9823 verifyFormat("for (;; *a = b) {\n}", Left); 9824 verifyFormat("return *this += 1;", Left); 9825 verifyFormat("throw *x;", Left); 9826 verifyFormat("delete *x;", Left); 9827 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9828 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9829 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9830 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9831 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9832 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9833 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9834 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9835 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9836 9837 verifyIndependentOfContext("a = *(x + y);"); 9838 verifyIndependentOfContext("a = &(x + y);"); 9839 verifyIndependentOfContext("*(x + y).call();"); 9840 verifyIndependentOfContext("&(x + y)->call();"); 9841 verifyFormat("void f() { &(*I).first; }"); 9842 9843 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9844 verifyFormat("f(* /* confusing comment */ foo);"); 9845 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 9846 verifyFormat("void foo(int * // this is the first paramters\n" 9847 " ,\n" 9848 " int second);"); 9849 verifyFormat("double term = a * // first\n" 9850 " b;"); 9851 verifyFormat( 9852 "int *MyValues = {\n" 9853 " *A, // Operator detection might be confused by the '{'\n" 9854 " *BB // Operator detection might be confused by previous comment\n" 9855 "};"); 9856 9857 verifyIndependentOfContext("if (int *a = &b)"); 9858 verifyIndependentOfContext("if (int &a = *b)"); 9859 verifyIndependentOfContext("if (a & b[i])"); 9860 verifyIndependentOfContext("if constexpr (a & b[i])"); 9861 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9862 verifyIndependentOfContext("if (a * (b * c))"); 9863 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9864 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9865 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9866 verifyIndependentOfContext("if (*b[i])"); 9867 verifyIndependentOfContext("if (int *a = (&b))"); 9868 verifyIndependentOfContext("while (int *a = &b)"); 9869 verifyIndependentOfContext("while (a * (b * c))"); 9870 verifyIndependentOfContext("size = sizeof *a;"); 9871 verifyIndependentOfContext("if (a && (b = c))"); 9872 verifyFormat("void f() {\n" 9873 " for (const int &v : Values) {\n" 9874 " }\n" 9875 "}"); 9876 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9877 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9878 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9879 9880 verifyFormat("#define A (!a * b)"); 9881 verifyFormat("#define MACRO \\\n" 9882 " int *i = a * b; \\\n" 9883 " void f(a *b);", 9884 getLLVMStyleWithColumns(19)); 9885 9886 verifyIndependentOfContext("A = new SomeType *[Length];"); 9887 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9888 verifyIndependentOfContext("T **t = new T *;"); 9889 verifyIndependentOfContext("T **t = new T *();"); 9890 verifyGoogleFormat("A = new SomeType*[Length]();"); 9891 verifyGoogleFormat("A = new SomeType*[Length];"); 9892 verifyGoogleFormat("T** t = new T*;"); 9893 verifyGoogleFormat("T** t = new T*();"); 9894 9895 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9896 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9897 verifyFormat("template <bool a, bool b> " 9898 "typename t::if<x && y>::type f() {}"); 9899 verifyFormat("template <int *y> f() {}"); 9900 verifyFormat("vector<int *> v;"); 9901 verifyFormat("vector<int *const> v;"); 9902 verifyFormat("vector<int *const **const *> v;"); 9903 verifyFormat("vector<int *volatile> v;"); 9904 verifyFormat("vector<a *_Nonnull> v;"); 9905 verifyFormat("vector<a *_Nullable> v;"); 9906 verifyFormat("vector<a *_Null_unspecified> v;"); 9907 verifyFormat("vector<a *__ptr32> v;"); 9908 verifyFormat("vector<a *__ptr64> v;"); 9909 verifyFormat("vector<a *__capability> v;"); 9910 FormatStyle TypeMacros = getLLVMStyle(); 9911 TypeMacros.TypenameMacros = {"LIST"}; 9912 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9913 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9914 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9915 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9916 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9917 9918 FormatStyle CustomQualifier = getLLVMStyle(); 9919 // Add identifiers that should not be parsed as a qualifier by default. 9920 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9921 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9922 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9923 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9924 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9925 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9926 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9927 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9928 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9929 verifyFormat("vector<a * _NotAQualifier> v;"); 9930 verifyFormat("vector<a * __not_a_qualifier> v;"); 9931 verifyFormat("vector<a * b> v;"); 9932 verifyFormat("foo<b && false>();"); 9933 verifyFormat("foo<b & 1>();"); 9934 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9935 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9936 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9937 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9938 verifyFormat( 9939 "template <class T, class = typename std::enable_if<\n" 9940 " std::is_integral<T>::value &&\n" 9941 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9942 "void F();", 9943 getLLVMStyleWithColumns(70)); 9944 verifyFormat("template <class T,\n" 9945 " class = typename std::enable_if<\n" 9946 " std::is_integral<T>::value &&\n" 9947 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9948 " class U>\n" 9949 "void F();", 9950 getLLVMStyleWithColumns(70)); 9951 verifyFormat( 9952 "template <class T,\n" 9953 " class = typename ::std::enable_if<\n" 9954 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9955 "void F();", 9956 getGoogleStyleWithColumns(68)); 9957 9958 verifyIndependentOfContext("MACRO(int *i);"); 9959 verifyIndependentOfContext("MACRO(auto *a);"); 9960 verifyIndependentOfContext("MACRO(const A *a);"); 9961 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9962 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9963 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9964 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9965 verifyIndependentOfContext("MACRO(A *const a);"); 9966 verifyIndependentOfContext("MACRO(A *restrict a);"); 9967 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9968 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9969 verifyIndependentOfContext("MACRO(A *volatile a);"); 9970 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9971 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9972 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9973 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9974 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9975 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9976 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9977 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9978 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9979 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9980 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9981 verifyIndependentOfContext("MACRO(A *__capability);"); 9982 verifyIndependentOfContext("MACRO(A &__capability);"); 9983 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9984 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9985 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9986 // a type declaration: 9987 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9988 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9989 // Also check that TypenameMacros prevents parsing it as multiplication: 9990 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9991 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9992 9993 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9994 verifyFormat("void f() { f(float{1}, a * a); }"); 9995 verifyFormat("void f() { f(float(1), a * a); }"); 9996 9997 verifyFormat("f((void (*)(int))g);"); 9998 verifyFormat("f((void (&)(int))g);"); 9999 verifyFormat("f((void (^)(int))g);"); 10000 10001 // FIXME: Is there a way to make this work? 10002 // verifyIndependentOfContext("MACRO(A *a);"); 10003 verifyFormat("MACRO(A &B);"); 10004 verifyFormat("MACRO(A *B);"); 10005 verifyFormat("void f() { MACRO(A * B); }"); 10006 verifyFormat("void f() { MACRO(A & B); }"); 10007 10008 // This lambda was mis-formatted after D88956 (treating it as a binop): 10009 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 10010 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 10011 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 10012 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 10013 10014 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 10015 verifyFormat("return options != nullptr && operator==(*options);"); 10016 10017 EXPECT_EQ("#define OP(x) \\\n" 10018 " ostream &operator<<(ostream &s, const A &a) { \\\n" 10019 " return s << a.DebugString(); \\\n" 10020 " }", 10021 format("#define OP(x) \\\n" 10022 " ostream &operator<<(ostream &s, const A &a) { \\\n" 10023 " return s << a.DebugString(); \\\n" 10024 " }", 10025 getLLVMStyleWithColumns(50))); 10026 10027 // FIXME: We cannot handle this case yet; we might be able to figure out that 10028 // foo<x> d > v; doesn't make sense. 10029 verifyFormat("foo<a<b && c> d> v;"); 10030 10031 FormatStyle PointerMiddle = getLLVMStyle(); 10032 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 10033 verifyFormat("delete *x;", PointerMiddle); 10034 verifyFormat("int * x;", PointerMiddle); 10035 verifyFormat("int *[] x;", PointerMiddle); 10036 verifyFormat("template <int * y> f() {}", PointerMiddle); 10037 verifyFormat("int * f(int * a) {}", PointerMiddle); 10038 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 10039 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 10040 verifyFormat("A<int *> a;", PointerMiddle); 10041 verifyFormat("A<int **> a;", PointerMiddle); 10042 verifyFormat("A<int *, int *> a;", PointerMiddle); 10043 verifyFormat("A<int *[]> a;", PointerMiddle); 10044 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 10045 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 10046 verifyFormat("T ** t = new T *;", PointerMiddle); 10047 10048 // Member function reference qualifiers aren't binary operators. 10049 verifyFormat("string // break\n" 10050 "operator()() & {}"); 10051 verifyFormat("string // break\n" 10052 "operator()() && {}"); 10053 verifyGoogleFormat("template <typename T>\n" 10054 "auto x() & -> int {}"); 10055 10056 // Should be binary operators when used as an argument expression (overloaded 10057 // operator invoked as a member function). 10058 verifyFormat("void f() { a.operator()(a * a); }"); 10059 verifyFormat("void f() { a->operator()(a & a); }"); 10060 verifyFormat("void f() { a.operator()(*a & *a); }"); 10061 verifyFormat("void f() { a->operator()(*a * *a); }"); 10062 10063 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 10064 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 10065 } 10066 10067 TEST_F(FormatTest, UnderstandsAttributes) { 10068 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 10069 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 10070 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10071 verifyFormat("__attribute__((nodebug)) ::qualified_type f();"); 10072 FormatStyle AfterType = getLLVMStyle(); 10073 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10074 verifyFormat("__attribute__((nodebug)) void\n" 10075 "foo() {}\n", 10076 AfterType); 10077 verifyFormat("__unused void\n" 10078 "foo() {}", 10079 AfterType); 10080 10081 FormatStyle CustomAttrs = getLLVMStyle(); 10082 CustomAttrs.AttributeMacros.push_back("__unused"); 10083 CustomAttrs.AttributeMacros.push_back("__attr1"); 10084 CustomAttrs.AttributeMacros.push_back("__attr2"); 10085 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 10086 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 10087 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 10088 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 10089 // Check that it is parsed as a multiplication without AttributeMacros and 10090 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 10091 verifyFormat("vector<SomeType * __attr1> v;"); 10092 verifyFormat("vector<SomeType __attr1 *> v;"); 10093 verifyFormat("vector<SomeType __attr1 *const> v;"); 10094 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 10095 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 10096 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 10097 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 10098 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 10099 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 10100 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 10101 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 10102 10103 // Check that these are not parsed as function declarations: 10104 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10105 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 10106 verifyFormat("SomeType s(InitValue);", CustomAttrs); 10107 verifyFormat("SomeType s{InitValue};", CustomAttrs); 10108 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 10109 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 10110 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 10111 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 10112 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 10113 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 10114 } 10115 10116 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 10117 // Check that qualifiers on pointers don't break parsing of casts. 10118 verifyFormat("x = (foo *const)*v;"); 10119 verifyFormat("x = (foo *volatile)*v;"); 10120 verifyFormat("x = (foo *restrict)*v;"); 10121 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 10122 verifyFormat("x = (foo *_Nonnull)*v;"); 10123 verifyFormat("x = (foo *_Nullable)*v;"); 10124 verifyFormat("x = (foo *_Null_unspecified)*v;"); 10125 verifyFormat("x = (foo *_Nonnull)*v;"); 10126 verifyFormat("x = (foo *[[clang::attr]])*v;"); 10127 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 10128 verifyFormat("x = (foo *__ptr32)*v;"); 10129 verifyFormat("x = (foo *__ptr64)*v;"); 10130 verifyFormat("x = (foo *__capability)*v;"); 10131 10132 // Check that we handle multiple trailing qualifiers and skip them all to 10133 // determine that the expression is a cast to a pointer type. 10134 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 10135 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 10136 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 10137 StringRef AllQualifiers = 10138 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 10139 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 10140 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 10141 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 10142 10143 // Also check that address-of is not parsed as a binary bitwise-and: 10144 verifyFormat("x = (foo *const)&v;"); 10145 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 10146 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 10147 10148 // Check custom qualifiers: 10149 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 10150 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 10151 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 10152 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 10153 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 10154 CustomQualifier); 10155 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 10156 CustomQualifier); 10157 10158 // Check that unknown identifiers result in binary operator parsing: 10159 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 10160 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 10161 } 10162 10163 TEST_F(FormatTest, UnderstandsSquareAttributes) { 10164 verifyFormat("SomeType s [[unused]] (InitValue);"); 10165 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 10166 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 10167 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 10168 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 10169 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10170 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10171 verifyFormat("[[nodiscard]] bool f() { return false; }"); 10172 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 10173 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 10174 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 10175 verifyFormat("[[nodiscard]] ::qualified_type f();"); 10176 10177 // Make sure we do not mistake attributes for array subscripts. 10178 verifyFormat("int a() {}\n" 10179 "[[unused]] int b() {}\n"); 10180 verifyFormat("NSArray *arr;\n" 10181 "arr[[Foo() bar]];"); 10182 10183 // On the other hand, we still need to correctly find array subscripts. 10184 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 10185 10186 // Make sure that we do not mistake Objective-C method inside array literals 10187 // as attributes, even if those method names are also keywords. 10188 verifyFormat("@[ [foo bar] ];"); 10189 verifyFormat("@[ [NSArray class] ];"); 10190 verifyFormat("@[ [foo enum] ];"); 10191 10192 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 10193 10194 // Make sure we do not parse attributes as lambda introducers. 10195 FormatStyle MultiLineFunctions = getLLVMStyle(); 10196 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10197 verifyFormat("[[unused]] int b() {\n" 10198 " return 42;\n" 10199 "}\n", 10200 MultiLineFunctions); 10201 } 10202 10203 TEST_F(FormatTest, AttributeClass) { 10204 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 10205 verifyFormat("class S {\n" 10206 " S(S&&) = default;\n" 10207 "};", 10208 Style); 10209 verifyFormat("class [[nodiscard]] S {\n" 10210 " S(S&&) = default;\n" 10211 "};", 10212 Style); 10213 verifyFormat("class __attribute((maybeunused)) S {\n" 10214 " S(S&&) = default;\n" 10215 "};", 10216 Style); 10217 verifyFormat("struct S {\n" 10218 " S(S&&) = default;\n" 10219 "};", 10220 Style); 10221 verifyFormat("struct [[nodiscard]] S {\n" 10222 " S(S&&) = default;\n" 10223 "};", 10224 Style); 10225 } 10226 10227 TEST_F(FormatTest, AttributesAfterMacro) { 10228 FormatStyle Style = getLLVMStyle(); 10229 verifyFormat("MACRO;\n" 10230 "__attribute__((maybe_unused)) int foo() {\n" 10231 " //...\n" 10232 "}"); 10233 10234 verifyFormat("MACRO;\n" 10235 "[[nodiscard]] int foo() {\n" 10236 " //...\n" 10237 "}"); 10238 10239 EXPECT_EQ("MACRO\n\n" 10240 "__attribute__((maybe_unused)) int foo() {\n" 10241 " //...\n" 10242 "}", 10243 format("MACRO\n\n" 10244 "__attribute__((maybe_unused)) int foo() {\n" 10245 " //...\n" 10246 "}")); 10247 10248 EXPECT_EQ("MACRO\n\n" 10249 "[[nodiscard]] int foo() {\n" 10250 " //...\n" 10251 "}", 10252 format("MACRO\n\n" 10253 "[[nodiscard]] int foo() {\n" 10254 " //...\n" 10255 "}")); 10256 } 10257 10258 TEST_F(FormatTest, AttributePenaltyBreaking) { 10259 FormatStyle Style = getLLVMStyle(); 10260 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 10261 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10262 Style); 10263 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 10264 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10265 Style); 10266 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 10267 "shared_ptr<ALongTypeName> &C d) {\n}", 10268 Style); 10269 } 10270 10271 TEST_F(FormatTest, UnderstandsEllipsis) { 10272 FormatStyle Style = getLLVMStyle(); 10273 verifyFormat("int printf(const char *fmt, ...);"); 10274 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 10275 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 10276 10277 verifyFormat("template <int *...PP> a;", Style); 10278 10279 Style.PointerAlignment = FormatStyle::PAS_Left; 10280 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 10281 10282 verifyFormat("template <int*... PP> a;", Style); 10283 10284 Style.PointerAlignment = FormatStyle::PAS_Middle; 10285 verifyFormat("template <int *... PP> a;", Style); 10286 } 10287 10288 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 10289 EXPECT_EQ("int *a;\n" 10290 "int *a;\n" 10291 "int *a;", 10292 format("int *a;\n" 10293 "int* a;\n" 10294 "int *a;", 10295 getGoogleStyle())); 10296 EXPECT_EQ("int* a;\n" 10297 "int* a;\n" 10298 "int* a;", 10299 format("int* a;\n" 10300 "int* a;\n" 10301 "int *a;", 10302 getGoogleStyle())); 10303 EXPECT_EQ("int *a;\n" 10304 "int *a;\n" 10305 "int *a;", 10306 format("int *a;\n" 10307 "int * a;\n" 10308 "int * a;", 10309 getGoogleStyle())); 10310 EXPECT_EQ("auto x = [] {\n" 10311 " int *a;\n" 10312 " int *a;\n" 10313 " int *a;\n" 10314 "};", 10315 format("auto x=[]{int *a;\n" 10316 "int * a;\n" 10317 "int * a;};", 10318 getGoogleStyle())); 10319 } 10320 10321 TEST_F(FormatTest, UnderstandsRvalueReferences) { 10322 verifyFormat("int f(int &&a) {}"); 10323 verifyFormat("int f(int a, char &&b) {}"); 10324 verifyFormat("void f() { int &&a = b; }"); 10325 verifyGoogleFormat("int f(int a, char&& b) {}"); 10326 verifyGoogleFormat("void f() { int&& a = b; }"); 10327 10328 verifyIndependentOfContext("A<int &&> a;"); 10329 verifyIndependentOfContext("A<int &&, int &&> a;"); 10330 verifyGoogleFormat("A<int&&> a;"); 10331 verifyGoogleFormat("A<int&&, int&&> a;"); 10332 10333 // Not rvalue references: 10334 verifyFormat("template <bool B, bool C> class A {\n" 10335 " static_assert(B && C, \"Something is wrong\");\n" 10336 "};"); 10337 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 10338 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 10339 verifyFormat("#define A(a, b) (a && b)"); 10340 } 10341 10342 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 10343 verifyFormat("void f() {\n" 10344 " x[aaaaaaaaa -\n" 10345 " b] = 23;\n" 10346 "}", 10347 getLLVMStyleWithColumns(15)); 10348 } 10349 10350 TEST_F(FormatTest, FormatsCasts) { 10351 verifyFormat("Type *A = static_cast<Type *>(P);"); 10352 verifyFormat("Type *A = (Type *)P;"); 10353 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 10354 verifyFormat("int a = (int)(2.0f);"); 10355 verifyFormat("int a = (int)2.0f;"); 10356 verifyFormat("x[(int32)y];"); 10357 verifyFormat("x = (int32)y;"); 10358 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 10359 verifyFormat("int a = (int)*b;"); 10360 verifyFormat("int a = (int)2.0f;"); 10361 verifyFormat("int a = (int)~0;"); 10362 verifyFormat("int a = (int)++a;"); 10363 verifyFormat("int a = (int)sizeof(int);"); 10364 verifyFormat("int a = (int)+2;"); 10365 verifyFormat("my_int a = (my_int)2.0f;"); 10366 verifyFormat("my_int a = (my_int)sizeof(int);"); 10367 verifyFormat("return (my_int)aaa;"); 10368 verifyFormat("#define x ((int)-1)"); 10369 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 10370 verifyFormat("#define p(q) ((int *)&q)"); 10371 verifyFormat("fn(a)(b) + 1;"); 10372 10373 verifyFormat("void f() { my_int a = (my_int)*b; }"); 10374 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 10375 verifyFormat("my_int a = (my_int)~0;"); 10376 verifyFormat("my_int a = (my_int)++a;"); 10377 verifyFormat("my_int a = (my_int)-2;"); 10378 verifyFormat("my_int a = (my_int)1;"); 10379 verifyFormat("my_int a = (my_int *)1;"); 10380 verifyFormat("my_int a = (const my_int)-1;"); 10381 verifyFormat("my_int a = (const my_int *)-1;"); 10382 verifyFormat("my_int a = (my_int)(my_int)-1;"); 10383 verifyFormat("my_int a = (ns::my_int)-2;"); 10384 verifyFormat("case (my_int)ONE:"); 10385 verifyFormat("auto x = (X)this;"); 10386 // Casts in Obj-C style calls used to not be recognized as such. 10387 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 10388 10389 // FIXME: single value wrapped with paren will be treated as cast. 10390 verifyFormat("void f(int i = (kValue)*kMask) {}"); 10391 10392 verifyFormat("{ (void)F; }"); 10393 10394 // Don't break after a cast's 10395 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10396 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 10397 " bbbbbbbbbbbbbbbbbbbbbb);"); 10398 10399 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)"); 10400 verifyFormat("#define CONF_BOOL(x) (bool *)(x)"); 10401 verifyFormat("#define CONF_BOOL(x) (bool)(x)"); 10402 verifyFormat("bool *y = (bool *)(void *)(x);"); 10403 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)"); 10404 verifyFormat("bool *y = (bool *)(void *)(int)(x);"); 10405 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)"); 10406 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);"); 10407 10408 // These are not casts. 10409 verifyFormat("void f(int *) {}"); 10410 verifyFormat("f(foo)->b;"); 10411 verifyFormat("f(foo).b;"); 10412 verifyFormat("f(foo)(b);"); 10413 verifyFormat("f(foo)[b];"); 10414 verifyFormat("[](foo) { return 4; }(bar);"); 10415 verifyFormat("(*funptr)(foo)[4];"); 10416 verifyFormat("funptrs[4](foo)[4];"); 10417 verifyFormat("void f(int *);"); 10418 verifyFormat("void f(int *) = 0;"); 10419 verifyFormat("void f(SmallVector<int>) {}"); 10420 verifyFormat("void f(SmallVector<int>);"); 10421 verifyFormat("void f(SmallVector<int>) = 0;"); 10422 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 10423 verifyFormat("int a = sizeof(int) * b;"); 10424 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 10425 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 10426 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 10427 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 10428 10429 // These are not casts, but at some point were confused with casts. 10430 verifyFormat("virtual void foo(int *) override;"); 10431 verifyFormat("virtual void foo(char &) const;"); 10432 verifyFormat("virtual void foo(int *a, char *) const;"); 10433 verifyFormat("int a = sizeof(int *) + b;"); 10434 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10435 verifyFormat("bool b = f(g<int>) && c;"); 10436 verifyFormat("typedef void (*f)(int i) func;"); 10437 verifyFormat("void operator++(int) noexcept;"); 10438 verifyFormat("void operator++(int &) noexcept;"); 10439 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10440 "&) noexcept;"); 10441 verifyFormat( 10442 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10443 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10444 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10445 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10446 verifyFormat("void operator delete(foo &) noexcept;"); 10447 verifyFormat("void operator delete(foo) noexcept;"); 10448 verifyFormat("void operator delete(int) noexcept;"); 10449 verifyFormat("void operator delete(int &) noexcept;"); 10450 verifyFormat("void operator delete(int &) volatile noexcept;"); 10451 verifyFormat("void operator delete(int &) const"); 10452 verifyFormat("void operator delete(int &) = default"); 10453 verifyFormat("void operator delete(int &) = delete"); 10454 verifyFormat("void operator delete(int &) [[noreturn]]"); 10455 verifyFormat("void operator delete(int &) throw();"); 10456 verifyFormat("void operator delete(int &) throw(int);"); 10457 verifyFormat("auto operator delete(int &) -> int;"); 10458 verifyFormat("auto operator delete(int &) override"); 10459 verifyFormat("auto operator delete(int &) final"); 10460 10461 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10462 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10463 // FIXME: The indentation here is not ideal. 10464 verifyFormat( 10465 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10466 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10467 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10468 } 10469 10470 TEST_F(FormatTest, FormatsFunctionTypes) { 10471 verifyFormat("A<bool()> a;"); 10472 verifyFormat("A<SomeType()> a;"); 10473 verifyFormat("A<void (*)(int, std::string)> a;"); 10474 verifyFormat("A<void *(int)>;"); 10475 verifyFormat("void *(*a)(int *, SomeType *);"); 10476 verifyFormat("int (*func)(void *);"); 10477 verifyFormat("void f() { int (*func)(void *); }"); 10478 verifyFormat("template <class CallbackClass>\n" 10479 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10480 10481 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10482 verifyGoogleFormat("void* (*a)(int);"); 10483 verifyGoogleFormat( 10484 "template <class CallbackClass>\n" 10485 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10486 10487 // Other constructs can look somewhat like function types: 10488 verifyFormat("A<sizeof(*x)> a;"); 10489 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10490 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10491 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10492 verifyFormat("int x = f(&h)();"); 10493 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10494 verifyFormat("std::function<\n" 10495 " LooooooooooongTemplatedType<\n" 10496 " SomeType>*(\n" 10497 " LooooooooooooooooongType type)>\n" 10498 " function;", 10499 getGoogleStyleWithColumns(40)); 10500 } 10501 10502 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10503 verifyFormat("A (*foo_)[6];"); 10504 verifyFormat("vector<int> (*foo_)[6];"); 10505 } 10506 10507 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10508 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10509 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10510 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10511 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10512 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10513 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10514 10515 // Different ways of ()-initializiation. 10516 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10517 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10518 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10519 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10520 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10521 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10522 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10523 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10524 10525 // Lambdas should not confuse the variable declaration heuristic. 10526 verifyFormat("LooooooooooooooooongType\n" 10527 " variable(nullptr, [](A *a) {});", 10528 getLLVMStyleWithColumns(40)); 10529 } 10530 10531 TEST_F(FormatTest, BreaksLongDeclarations) { 10532 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10533 " AnotherNameForTheLongType;"); 10534 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10536 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10537 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10538 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10539 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10540 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10541 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10542 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10543 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10544 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10545 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10546 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10547 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10548 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10549 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10550 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10551 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10552 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10553 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10554 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10555 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10556 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10557 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10558 FormatStyle Indented = getLLVMStyle(); 10559 Indented.IndentWrappedFunctionNames = true; 10560 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10561 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10562 Indented); 10563 verifyFormat( 10564 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10565 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10566 Indented); 10567 verifyFormat( 10568 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10569 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10570 Indented); 10571 verifyFormat( 10572 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10573 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10574 Indented); 10575 10576 // FIXME: Without the comment, this breaks after "(". 10577 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10578 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10579 getGoogleStyle()); 10580 10581 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10582 " int LoooooooooooooooooooongParam2) {}"); 10583 verifyFormat( 10584 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10585 " SourceLocation L, IdentifierIn *II,\n" 10586 " Type *T) {}"); 10587 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10588 "ReallyReaaallyLongFunctionName(\n" 10589 " const std::string &SomeParameter,\n" 10590 " const SomeType<string, SomeOtherTemplateParameter>\n" 10591 " &ReallyReallyLongParameterName,\n" 10592 " const SomeType<string, SomeOtherTemplateParameter>\n" 10593 " &AnotherLongParameterName) {}"); 10594 verifyFormat("template <typename A>\n" 10595 "SomeLoooooooooooooooooooooongType<\n" 10596 " typename some_namespace::SomeOtherType<A>::Type>\n" 10597 "Function() {}"); 10598 10599 verifyGoogleFormat( 10600 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10601 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10602 verifyGoogleFormat( 10603 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10604 " SourceLocation L) {}"); 10605 verifyGoogleFormat( 10606 "some_namespace::LongReturnType\n" 10607 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10608 " int first_long_parameter, int second_parameter) {}"); 10609 10610 verifyGoogleFormat("template <typename T>\n" 10611 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10612 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10613 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10614 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10615 10616 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10617 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10618 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10619 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10620 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10621 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10622 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10623 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10626 10627 verifyFormat("template <typename T> // Templates on own line.\n" 10628 "static int // Some comment.\n" 10629 "MyFunction(int a);", 10630 getLLVMStyle()); 10631 } 10632 10633 TEST_F(FormatTest, FormatsAccessModifiers) { 10634 FormatStyle Style = getLLVMStyle(); 10635 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10636 FormatStyle::ELBAMS_LogicalBlock); 10637 verifyFormat("struct foo {\n" 10638 "private:\n" 10639 " void f() {}\n" 10640 "\n" 10641 "private:\n" 10642 " int i;\n" 10643 "\n" 10644 "protected:\n" 10645 " int j;\n" 10646 "};\n", 10647 Style); 10648 verifyFormat("struct foo {\n" 10649 "private:\n" 10650 " void f() {}\n" 10651 "\n" 10652 "private:\n" 10653 " int i;\n" 10654 "\n" 10655 "protected:\n" 10656 " int j;\n" 10657 "};\n", 10658 "struct foo {\n" 10659 "private:\n" 10660 " void f() {}\n" 10661 "private:\n" 10662 " int i;\n" 10663 "protected:\n" 10664 " int j;\n" 10665 "};\n", 10666 Style); 10667 verifyFormat("struct foo { /* comment */\n" 10668 "private:\n" 10669 " int i;\n" 10670 " // comment\n" 10671 "private:\n" 10672 " int j;\n" 10673 "};\n", 10674 Style); 10675 verifyFormat("struct foo {\n" 10676 "#ifdef FOO\n" 10677 "#endif\n" 10678 "private:\n" 10679 " int i;\n" 10680 "#ifdef FOO\n" 10681 "private:\n" 10682 "#endif\n" 10683 " int j;\n" 10684 "};\n", 10685 Style); 10686 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10687 verifyFormat("struct foo {\n" 10688 "private:\n" 10689 " void f() {}\n" 10690 "private:\n" 10691 " int i;\n" 10692 "protected:\n" 10693 " int j;\n" 10694 "};\n", 10695 Style); 10696 verifyFormat("struct foo {\n" 10697 "private:\n" 10698 " void f() {}\n" 10699 "private:\n" 10700 " int i;\n" 10701 "protected:\n" 10702 " int j;\n" 10703 "};\n", 10704 "struct foo {\n" 10705 "\n" 10706 "private:\n" 10707 " void f() {}\n" 10708 "\n" 10709 "private:\n" 10710 " int i;\n" 10711 "\n" 10712 "protected:\n" 10713 " int j;\n" 10714 "};\n", 10715 Style); 10716 verifyFormat("struct foo { /* comment */\n" 10717 "private:\n" 10718 " int i;\n" 10719 " // comment\n" 10720 "private:\n" 10721 " int j;\n" 10722 "};\n", 10723 "struct foo { /* comment */\n" 10724 "\n" 10725 "private:\n" 10726 " int i;\n" 10727 " // comment\n" 10728 "\n" 10729 "private:\n" 10730 " int j;\n" 10731 "};\n", 10732 Style); 10733 verifyFormat("struct foo {\n" 10734 "#ifdef FOO\n" 10735 "#endif\n" 10736 "private:\n" 10737 " int i;\n" 10738 "#ifdef FOO\n" 10739 "private:\n" 10740 "#endif\n" 10741 " int j;\n" 10742 "};\n", 10743 "struct foo {\n" 10744 "#ifdef FOO\n" 10745 "#endif\n" 10746 "\n" 10747 "private:\n" 10748 " int i;\n" 10749 "#ifdef FOO\n" 10750 "\n" 10751 "private:\n" 10752 "#endif\n" 10753 " int j;\n" 10754 "};\n", 10755 Style); 10756 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10757 verifyFormat("struct foo {\n" 10758 "private:\n" 10759 " void f() {}\n" 10760 "\n" 10761 "private:\n" 10762 " int i;\n" 10763 "\n" 10764 "protected:\n" 10765 " int j;\n" 10766 "};\n", 10767 Style); 10768 verifyFormat("struct foo {\n" 10769 "private:\n" 10770 " void f() {}\n" 10771 "\n" 10772 "private:\n" 10773 " int i;\n" 10774 "\n" 10775 "protected:\n" 10776 " int j;\n" 10777 "};\n", 10778 "struct foo {\n" 10779 "private:\n" 10780 " void f() {}\n" 10781 "private:\n" 10782 " int i;\n" 10783 "protected:\n" 10784 " int j;\n" 10785 "};\n", 10786 Style); 10787 verifyFormat("struct foo { /* comment */\n" 10788 "private:\n" 10789 " int i;\n" 10790 " // comment\n" 10791 "\n" 10792 "private:\n" 10793 " int j;\n" 10794 "};\n", 10795 "struct foo { /* comment */\n" 10796 "private:\n" 10797 " int i;\n" 10798 " // comment\n" 10799 "\n" 10800 "private:\n" 10801 " int j;\n" 10802 "};\n", 10803 Style); 10804 verifyFormat("struct foo {\n" 10805 "#ifdef FOO\n" 10806 "#endif\n" 10807 "\n" 10808 "private:\n" 10809 " int i;\n" 10810 "#ifdef FOO\n" 10811 "\n" 10812 "private:\n" 10813 "#endif\n" 10814 " int j;\n" 10815 "};\n", 10816 "struct foo {\n" 10817 "#ifdef FOO\n" 10818 "#endif\n" 10819 "private:\n" 10820 " int i;\n" 10821 "#ifdef FOO\n" 10822 "private:\n" 10823 "#endif\n" 10824 " int j;\n" 10825 "};\n", 10826 Style); 10827 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10828 EXPECT_EQ("struct foo {\n" 10829 "\n" 10830 "private:\n" 10831 " void f() {}\n" 10832 "\n" 10833 "private:\n" 10834 " int i;\n" 10835 "\n" 10836 "protected:\n" 10837 " int j;\n" 10838 "};\n", 10839 format("struct foo {\n" 10840 "\n" 10841 "private:\n" 10842 " void f() {}\n" 10843 "\n" 10844 "private:\n" 10845 " int i;\n" 10846 "\n" 10847 "protected:\n" 10848 " int j;\n" 10849 "};\n", 10850 Style)); 10851 verifyFormat("struct foo {\n" 10852 "private:\n" 10853 " void f() {}\n" 10854 "private:\n" 10855 " int i;\n" 10856 "protected:\n" 10857 " int j;\n" 10858 "};\n", 10859 Style); 10860 EXPECT_EQ("struct foo { /* comment */\n" 10861 "\n" 10862 "private:\n" 10863 " int i;\n" 10864 " // comment\n" 10865 "\n" 10866 "private:\n" 10867 " int j;\n" 10868 "};\n", 10869 format("struct foo { /* comment */\n" 10870 "\n" 10871 "private:\n" 10872 " int i;\n" 10873 " // comment\n" 10874 "\n" 10875 "private:\n" 10876 " int j;\n" 10877 "};\n", 10878 Style)); 10879 verifyFormat("struct foo { /* comment */\n" 10880 "private:\n" 10881 " int i;\n" 10882 " // comment\n" 10883 "private:\n" 10884 " int j;\n" 10885 "};\n", 10886 Style); 10887 EXPECT_EQ("struct foo {\n" 10888 "#ifdef FOO\n" 10889 "#endif\n" 10890 "\n" 10891 "private:\n" 10892 " int i;\n" 10893 "#ifdef FOO\n" 10894 "\n" 10895 "private:\n" 10896 "#endif\n" 10897 " int j;\n" 10898 "};\n", 10899 format("struct foo {\n" 10900 "#ifdef FOO\n" 10901 "#endif\n" 10902 "\n" 10903 "private:\n" 10904 " int i;\n" 10905 "#ifdef FOO\n" 10906 "\n" 10907 "private:\n" 10908 "#endif\n" 10909 " int j;\n" 10910 "};\n", 10911 Style)); 10912 verifyFormat("struct foo {\n" 10913 "#ifdef FOO\n" 10914 "#endif\n" 10915 "private:\n" 10916 " int i;\n" 10917 "#ifdef FOO\n" 10918 "private:\n" 10919 "#endif\n" 10920 " int j;\n" 10921 "};\n", 10922 Style); 10923 10924 FormatStyle NoEmptyLines = getLLVMStyle(); 10925 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10926 verifyFormat("struct foo {\n" 10927 "private:\n" 10928 " void f() {}\n" 10929 "\n" 10930 "private:\n" 10931 " int i;\n" 10932 "\n" 10933 "public:\n" 10934 "protected:\n" 10935 " int j;\n" 10936 "};\n", 10937 NoEmptyLines); 10938 10939 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10940 verifyFormat("struct foo {\n" 10941 "private:\n" 10942 " void f() {}\n" 10943 "private:\n" 10944 " int i;\n" 10945 "public:\n" 10946 "protected:\n" 10947 " int j;\n" 10948 "};\n", 10949 NoEmptyLines); 10950 10951 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10952 verifyFormat("struct foo {\n" 10953 "private:\n" 10954 " void f() {}\n" 10955 "\n" 10956 "private:\n" 10957 " int i;\n" 10958 "\n" 10959 "public:\n" 10960 "\n" 10961 "protected:\n" 10962 " int j;\n" 10963 "};\n", 10964 NoEmptyLines); 10965 } 10966 10967 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10968 10969 FormatStyle Style = getLLVMStyle(); 10970 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10971 verifyFormat("struct foo {\n" 10972 "private:\n" 10973 " void f() {}\n" 10974 "\n" 10975 "private:\n" 10976 " int i;\n" 10977 "\n" 10978 "protected:\n" 10979 " int j;\n" 10980 "};\n", 10981 Style); 10982 10983 // Check if lines are removed. 10984 verifyFormat("struct foo {\n" 10985 "private:\n" 10986 " void f() {}\n" 10987 "\n" 10988 "private:\n" 10989 " int i;\n" 10990 "\n" 10991 "protected:\n" 10992 " int j;\n" 10993 "};\n", 10994 "struct foo {\n" 10995 "private:\n" 10996 "\n" 10997 " void f() {}\n" 10998 "\n" 10999 "private:\n" 11000 "\n" 11001 " int i;\n" 11002 "\n" 11003 "protected:\n" 11004 "\n" 11005 " int j;\n" 11006 "};\n", 11007 Style); 11008 11009 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11010 verifyFormat("struct foo {\n" 11011 "private:\n" 11012 "\n" 11013 " void f() {}\n" 11014 "\n" 11015 "private:\n" 11016 "\n" 11017 " int i;\n" 11018 "\n" 11019 "protected:\n" 11020 "\n" 11021 " int j;\n" 11022 "};\n", 11023 Style); 11024 11025 // Check if lines are added. 11026 verifyFormat("struct foo {\n" 11027 "private:\n" 11028 "\n" 11029 " void f() {}\n" 11030 "\n" 11031 "private:\n" 11032 "\n" 11033 " int i;\n" 11034 "\n" 11035 "protected:\n" 11036 "\n" 11037 " int j;\n" 11038 "};\n", 11039 "struct foo {\n" 11040 "private:\n" 11041 " void f() {}\n" 11042 "\n" 11043 "private:\n" 11044 " int i;\n" 11045 "\n" 11046 "protected:\n" 11047 " int j;\n" 11048 "};\n", 11049 Style); 11050 11051 // Leave tests rely on the code layout, test::messUp can not be used. 11052 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11053 Style.MaxEmptyLinesToKeep = 0u; 11054 verifyFormat("struct foo {\n" 11055 "private:\n" 11056 " void f() {}\n" 11057 "\n" 11058 "private:\n" 11059 " int i;\n" 11060 "\n" 11061 "protected:\n" 11062 " int j;\n" 11063 "};\n", 11064 Style); 11065 11066 // Check if MaxEmptyLinesToKeep is respected. 11067 EXPECT_EQ("struct foo {\n" 11068 "private:\n" 11069 " void f() {}\n" 11070 "\n" 11071 "private:\n" 11072 " int i;\n" 11073 "\n" 11074 "protected:\n" 11075 " int j;\n" 11076 "};\n", 11077 format("struct foo {\n" 11078 "private:\n" 11079 "\n\n\n" 11080 " void f() {}\n" 11081 "\n" 11082 "private:\n" 11083 "\n\n\n" 11084 " int i;\n" 11085 "\n" 11086 "protected:\n" 11087 "\n\n\n" 11088 " int j;\n" 11089 "};\n", 11090 Style)); 11091 11092 Style.MaxEmptyLinesToKeep = 1u; 11093 EXPECT_EQ("struct foo {\n" 11094 "private:\n" 11095 "\n" 11096 " void f() {}\n" 11097 "\n" 11098 "private:\n" 11099 "\n" 11100 " int i;\n" 11101 "\n" 11102 "protected:\n" 11103 "\n" 11104 " int j;\n" 11105 "};\n", 11106 format("struct foo {\n" 11107 "private:\n" 11108 "\n" 11109 " void f() {}\n" 11110 "\n" 11111 "private:\n" 11112 "\n" 11113 " int i;\n" 11114 "\n" 11115 "protected:\n" 11116 "\n" 11117 " int j;\n" 11118 "};\n", 11119 Style)); 11120 // Check if no lines are kept. 11121 EXPECT_EQ("struct foo {\n" 11122 "private:\n" 11123 " void f() {}\n" 11124 "\n" 11125 "private:\n" 11126 " int i;\n" 11127 "\n" 11128 "protected:\n" 11129 " int j;\n" 11130 "};\n", 11131 format("struct foo {\n" 11132 "private:\n" 11133 " void f() {}\n" 11134 "\n" 11135 "private:\n" 11136 " int i;\n" 11137 "\n" 11138 "protected:\n" 11139 " int j;\n" 11140 "};\n", 11141 Style)); 11142 // Check if MaxEmptyLinesToKeep is respected. 11143 EXPECT_EQ("struct foo {\n" 11144 "private:\n" 11145 "\n" 11146 " void f() {}\n" 11147 "\n" 11148 "private:\n" 11149 "\n" 11150 " int i;\n" 11151 "\n" 11152 "protected:\n" 11153 "\n" 11154 " int j;\n" 11155 "};\n", 11156 format("struct foo {\n" 11157 "private:\n" 11158 "\n\n\n" 11159 " void f() {}\n" 11160 "\n" 11161 "private:\n" 11162 "\n\n\n" 11163 " int i;\n" 11164 "\n" 11165 "protected:\n" 11166 "\n\n\n" 11167 " int j;\n" 11168 "};\n", 11169 Style)); 11170 11171 Style.MaxEmptyLinesToKeep = 10u; 11172 EXPECT_EQ("struct foo {\n" 11173 "private:\n" 11174 "\n\n\n" 11175 " void f() {}\n" 11176 "\n" 11177 "private:\n" 11178 "\n\n\n" 11179 " int i;\n" 11180 "\n" 11181 "protected:\n" 11182 "\n\n\n" 11183 " int j;\n" 11184 "};\n", 11185 format("struct foo {\n" 11186 "private:\n" 11187 "\n\n\n" 11188 " void f() {}\n" 11189 "\n" 11190 "private:\n" 11191 "\n\n\n" 11192 " int i;\n" 11193 "\n" 11194 "protected:\n" 11195 "\n\n\n" 11196 " int j;\n" 11197 "};\n", 11198 Style)); 11199 11200 // Test with comments. 11201 Style = getLLVMStyle(); 11202 verifyFormat("struct foo {\n" 11203 "private:\n" 11204 " // comment\n" 11205 " void f() {}\n" 11206 "\n" 11207 "private: /* comment */\n" 11208 " int i;\n" 11209 "};\n", 11210 Style); 11211 verifyFormat("struct foo {\n" 11212 "private:\n" 11213 " // comment\n" 11214 " void f() {}\n" 11215 "\n" 11216 "private: /* comment */\n" 11217 " int i;\n" 11218 "};\n", 11219 "struct foo {\n" 11220 "private:\n" 11221 "\n" 11222 " // comment\n" 11223 " void f() {}\n" 11224 "\n" 11225 "private: /* comment */\n" 11226 "\n" 11227 " int i;\n" 11228 "};\n", 11229 Style); 11230 11231 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11232 verifyFormat("struct foo {\n" 11233 "private:\n" 11234 "\n" 11235 " // comment\n" 11236 " void f() {}\n" 11237 "\n" 11238 "private: /* comment */\n" 11239 "\n" 11240 " int i;\n" 11241 "};\n", 11242 "struct foo {\n" 11243 "private:\n" 11244 " // comment\n" 11245 " void f() {}\n" 11246 "\n" 11247 "private: /* comment */\n" 11248 " int i;\n" 11249 "};\n", 11250 Style); 11251 verifyFormat("struct foo {\n" 11252 "private:\n" 11253 "\n" 11254 " // comment\n" 11255 " void f() {}\n" 11256 "\n" 11257 "private: /* comment */\n" 11258 "\n" 11259 " int i;\n" 11260 "};\n", 11261 Style); 11262 11263 // Test with preprocessor defines. 11264 Style = getLLVMStyle(); 11265 verifyFormat("struct foo {\n" 11266 "private:\n" 11267 "#ifdef FOO\n" 11268 "#endif\n" 11269 " void f() {}\n" 11270 "};\n", 11271 Style); 11272 verifyFormat("struct foo {\n" 11273 "private:\n" 11274 "#ifdef FOO\n" 11275 "#endif\n" 11276 " void f() {}\n" 11277 "};\n", 11278 "struct foo {\n" 11279 "private:\n" 11280 "\n" 11281 "#ifdef FOO\n" 11282 "#endif\n" 11283 " void f() {}\n" 11284 "};\n", 11285 Style); 11286 11287 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11288 verifyFormat("struct foo {\n" 11289 "private:\n" 11290 "\n" 11291 "#ifdef FOO\n" 11292 "#endif\n" 11293 " void f() {}\n" 11294 "};\n", 11295 "struct foo {\n" 11296 "private:\n" 11297 "#ifdef FOO\n" 11298 "#endif\n" 11299 " void f() {}\n" 11300 "};\n", 11301 Style); 11302 verifyFormat("struct foo {\n" 11303 "private:\n" 11304 "\n" 11305 "#ifdef FOO\n" 11306 "#endif\n" 11307 " void f() {}\n" 11308 "};\n", 11309 Style); 11310 } 11311 11312 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 11313 // Combined tests of EmptyLineAfterAccessModifier and 11314 // EmptyLineBeforeAccessModifier. 11315 FormatStyle Style = getLLVMStyle(); 11316 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11317 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11318 verifyFormat("struct foo {\n" 11319 "private:\n" 11320 "\n" 11321 "protected:\n" 11322 "};\n", 11323 Style); 11324 11325 Style.MaxEmptyLinesToKeep = 10u; 11326 // Both remove all new lines. 11327 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11328 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11329 verifyFormat("struct foo {\n" 11330 "private:\n" 11331 "protected:\n" 11332 "};\n", 11333 "struct foo {\n" 11334 "private:\n" 11335 "\n\n\n" 11336 "protected:\n" 11337 "};\n", 11338 Style); 11339 11340 // Leave tests rely on the code layout, test::messUp can not be used. 11341 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11342 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11343 Style.MaxEmptyLinesToKeep = 10u; 11344 EXPECT_EQ("struct foo {\n" 11345 "private:\n" 11346 "\n\n\n" 11347 "protected:\n" 11348 "};\n", 11349 format("struct foo {\n" 11350 "private:\n" 11351 "\n\n\n" 11352 "protected:\n" 11353 "};\n", 11354 Style)); 11355 Style.MaxEmptyLinesToKeep = 3u; 11356 EXPECT_EQ("struct foo {\n" 11357 "private:\n" 11358 "\n\n\n" 11359 "protected:\n" 11360 "};\n", 11361 format("struct foo {\n" 11362 "private:\n" 11363 "\n\n\n" 11364 "protected:\n" 11365 "};\n", 11366 Style)); 11367 Style.MaxEmptyLinesToKeep = 1u; 11368 EXPECT_EQ("struct foo {\n" 11369 "private:\n" 11370 "\n\n\n" 11371 "protected:\n" 11372 "};\n", 11373 format("struct foo {\n" 11374 "private:\n" 11375 "\n\n\n" 11376 "protected:\n" 11377 "};\n", 11378 Style)); // Based on new lines in original document and not 11379 // on the setting. 11380 11381 Style.MaxEmptyLinesToKeep = 10u; 11382 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11383 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11384 // Newlines are kept if they are greater than zero, 11385 // test::messUp removes all new lines which changes the logic 11386 EXPECT_EQ("struct foo {\n" 11387 "private:\n" 11388 "\n\n\n" 11389 "protected:\n" 11390 "};\n", 11391 format("struct foo {\n" 11392 "private:\n" 11393 "\n\n\n" 11394 "protected:\n" 11395 "};\n", 11396 Style)); 11397 11398 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11399 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11400 // test::messUp removes all new lines which changes the logic 11401 EXPECT_EQ("struct foo {\n" 11402 "private:\n" 11403 "\n\n\n" 11404 "protected:\n" 11405 "};\n", 11406 format("struct foo {\n" 11407 "private:\n" 11408 "\n\n\n" 11409 "protected:\n" 11410 "};\n", 11411 Style)); 11412 11413 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11414 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11415 EXPECT_EQ("struct foo {\n" 11416 "private:\n" 11417 "\n\n\n" 11418 "protected:\n" 11419 "};\n", 11420 format("struct foo {\n" 11421 "private:\n" 11422 "\n\n\n" 11423 "protected:\n" 11424 "};\n", 11425 Style)); // test::messUp removes all new lines which changes 11426 // the logic. 11427 11428 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11429 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11430 verifyFormat("struct foo {\n" 11431 "private:\n" 11432 "protected:\n" 11433 "};\n", 11434 "struct foo {\n" 11435 "private:\n" 11436 "\n\n\n" 11437 "protected:\n" 11438 "};\n", 11439 Style); 11440 11441 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11442 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11443 EXPECT_EQ("struct foo {\n" 11444 "private:\n" 11445 "\n\n\n" 11446 "protected:\n" 11447 "};\n", 11448 format("struct foo {\n" 11449 "private:\n" 11450 "\n\n\n" 11451 "protected:\n" 11452 "};\n", 11453 Style)); // test::messUp removes all new lines which changes 11454 // the logic. 11455 11456 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11457 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11458 verifyFormat("struct foo {\n" 11459 "private:\n" 11460 "protected:\n" 11461 "};\n", 11462 "struct foo {\n" 11463 "private:\n" 11464 "\n\n\n" 11465 "protected:\n" 11466 "};\n", 11467 Style); 11468 11469 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11470 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11471 verifyFormat("struct foo {\n" 11472 "private:\n" 11473 "protected:\n" 11474 "};\n", 11475 "struct foo {\n" 11476 "private:\n" 11477 "\n\n\n" 11478 "protected:\n" 11479 "};\n", 11480 Style); 11481 11482 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11483 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11484 verifyFormat("struct foo {\n" 11485 "private:\n" 11486 "protected:\n" 11487 "};\n", 11488 "struct foo {\n" 11489 "private:\n" 11490 "\n\n\n" 11491 "protected:\n" 11492 "};\n", 11493 Style); 11494 11495 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11496 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11497 verifyFormat("struct foo {\n" 11498 "private:\n" 11499 "protected:\n" 11500 "};\n", 11501 "struct foo {\n" 11502 "private:\n" 11503 "\n\n\n" 11504 "protected:\n" 11505 "};\n", 11506 Style); 11507 } 11508 11509 TEST_F(FormatTest, FormatsArrays) { 11510 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11511 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11512 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11513 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11514 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11515 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11516 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11517 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11518 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11519 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11520 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11521 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11522 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11523 verifyFormat( 11524 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11525 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11526 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11527 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11528 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11529 11530 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11532 verifyFormat( 11533 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11534 " .aaaaaaa[0]\n" 11535 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11536 verifyFormat("a[::b::c];"); 11537 11538 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11539 11540 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11541 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11542 } 11543 11544 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11545 verifyFormat("(a)->b();"); 11546 verifyFormat("--a;"); 11547 } 11548 11549 TEST_F(FormatTest, HandlesIncludeDirectives) { 11550 verifyFormat("#include <string>\n" 11551 "#include <a/b/c.h>\n" 11552 "#include \"a/b/string\"\n" 11553 "#include \"string.h\"\n" 11554 "#include \"string.h\"\n" 11555 "#include <a-a>\n" 11556 "#include < path with space >\n" 11557 "#include_next <test.h>" 11558 "#include \"abc.h\" // this is included for ABC\n" 11559 "#include \"some long include\" // with a comment\n" 11560 "#include \"some very long include path\"\n" 11561 "#include <some/very/long/include/path>\n", 11562 getLLVMStyleWithColumns(35)); 11563 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11564 EXPECT_EQ("#include <a>", format("#include<a>")); 11565 11566 verifyFormat("#import <string>"); 11567 verifyFormat("#import <a/b/c.h>"); 11568 verifyFormat("#import \"a/b/string\""); 11569 verifyFormat("#import \"string.h\""); 11570 verifyFormat("#import \"string.h\""); 11571 verifyFormat("#if __has_include(<strstream>)\n" 11572 "#include <strstream>\n" 11573 "#endif"); 11574 11575 verifyFormat("#define MY_IMPORT <a/b>"); 11576 11577 verifyFormat("#if __has_include(<a/b>)"); 11578 verifyFormat("#if __has_include_next(<a/b>)"); 11579 verifyFormat("#define F __has_include(<a/b>)"); 11580 verifyFormat("#define F __has_include_next(<a/b>)"); 11581 11582 // Protocol buffer definition or missing "#". 11583 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11584 getLLVMStyleWithColumns(30)); 11585 11586 FormatStyle Style = getLLVMStyle(); 11587 Style.AlwaysBreakBeforeMultilineStrings = true; 11588 Style.ColumnLimit = 0; 11589 verifyFormat("#import \"abc.h\"", Style); 11590 11591 // But 'import' might also be a regular C++ namespace. 11592 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11594 } 11595 11596 //===----------------------------------------------------------------------===// 11597 // Error recovery tests. 11598 //===----------------------------------------------------------------------===// 11599 11600 TEST_F(FormatTest, IncompleteParameterLists) { 11601 FormatStyle NoBinPacking = getLLVMStyle(); 11602 NoBinPacking.BinPackParameters = false; 11603 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11604 " double *min_x,\n" 11605 " double *max_x,\n" 11606 " double *min_y,\n" 11607 " double *max_y,\n" 11608 " double *min_z,\n" 11609 " double *max_z, ) {}", 11610 NoBinPacking); 11611 } 11612 11613 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11614 verifyFormat("void f() { return; }\n42"); 11615 verifyFormat("void f() {\n" 11616 " if (0)\n" 11617 " return;\n" 11618 "}\n" 11619 "42"); 11620 verifyFormat("void f() { return }\n42"); 11621 verifyFormat("void f() {\n" 11622 " if (0)\n" 11623 " return\n" 11624 "}\n" 11625 "42"); 11626 } 11627 11628 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11629 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11630 EXPECT_EQ("void f() {\n" 11631 " if (a)\n" 11632 " return\n" 11633 "}", 11634 format("void f ( ) { if ( a ) return }")); 11635 EXPECT_EQ("namespace N {\n" 11636 "void f()\n" 11637 "}", 11638 format("namespace N { void f() }")); 11639 EXPECT_EQ("namespace N {\n" 11640 "void f() {}\n" 11641 "void g()\n" 11642 "} // namespace N", 11643 format("namespace N { void f( ) { } void g( ) }")); 11644 } 11645 11646 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11647 verifyFormat("int aaaaaaaa =\n" 11648 " // Overlylongcomment\n" 11649 " b;", 11650 getLLVMStyleWithColumns(20)); 11651 verifyFormat("function(\n" 11652 " ShortArgument,\n" 11653 " LoooooooooooongArgument);\n", 11654 getLLVMStyleWithColumns(20)); 11655 } 11656 11657 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11658 verifyFormat("public:"); 11659 verifyFormat("class A {\n" 11660 "public\n" 11661 " void f() {}\n" 11662 "};"); 11663 verifyFormat("public\n" 11664 "int qwerty;"); 11665 verifyFormat("public\n" 11666 "B {}"); 11667 verifyFormat("public\n" 11668 "{}"); 11669 verifyFormat("public\n" 11670 "B { int x; }"); 11671 } 11672 11673 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11674 verifyFormat("{"); 11675 verifyFormat("#})"); 11676 verifyNoCrash("(/**/[:!] ?[)."); 11677 } 11678 11679 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11680 // Found by oss-fuzz: 11681 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11682 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11683 Style.ColumnLimit = 60; 11684 verifyNoCrash( 11685 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11686 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11687 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11688 Style); 11689 } 11690 11691 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11692 verifyFormat("do {\n}"); 11693 verifyFormat("do {\n}\n" 11694 "f();"); 11695 verifyFormat("do {\n}\n" 11696 "wheeee(fun);"); 11697 verifyFormat("do {\n" 11698 " f();\n" 11699 "}"); 11700 } 11701 11702 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11703 verifyFormat("if {\n foo;\n foo();\n}"); 11704 verifyFormat("switch {\n foo;\n foo();\n}"); 11705 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11706 verifyFormat("while {\n foo;\n foo();\n}"); 11707 verifyFormat("do {\n foo;\n foo();\n} while;"); 11708 } 11709 11710 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11711 verifyIncompleteFormat("namespace {\n" 11712 "class Foo { Foo (\n" 11713 "};\n" 11714 "} // namespace"); 11715 } 11716 11717 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11718 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11719 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11720 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11721 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11722 11723 EXPECT_EQ("{\n" 11724 " {\n" 11725 " breakme(\n" 11726 " qwe);\n" 11727 " }\n", 11728 format("{\n" 11729 " {\n" 11730 " breakme(qwe);\n" 11731 "}\n", 11732 getLLVMStyleWithColumns(10))); 11733 } 11734 11735 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11736 verifyFormat("int x = {\n" 11737 " avariable,\n" 11738 " b(alongervariable)};", 11739 getLLVMStyleWithColumns(25)); 11740 } 11741 11742 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11743 verifyFormat("return (a)(b){1, 2, 3};"); 11744 } 11745 11746 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11747 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11748 verifyFormat("vector<int> x{\n" 11749 " 1,\n" 11750 " 2,\n" 11751 " 3,\n" 11752 " 4,\n" 11753 "};"); 11754 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11755 verifyFormat("f({1, 2});"); 11756 verifyFormat("auto v = Foo{-1};"); 11757 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11758 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11759 verifyFormat("new vector<int>{1, 2, 3};"); 11760 verifyFormat("new int[3]{1, 2, 3};"); 11761 verifyFormat("new int{1};"); 11762 verifyFormat("return {arg1, arg2};"); 11763 verifyFormat("return {arg1, SomeType{parameter}};"); 11764 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11765 verifyFormat("new T{arg1, arg2};"); 11766 verifyFormat("f(MyMap[{composite, key}]);"); 11767 verifyFormat("class Class {\n" 11768 " T member = {arg1, arg2};\n" 11769 "};"); 11770 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11771 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11772 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11773 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11774 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11775 11776 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11777 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11778 verifyFormat("auto i = decltype(x){};"); 11779 verifyFormat("auto i = typeof(x){};"); 11780 verifyFormat("auto i = _Atomic(x){};"); 11781 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11782 verifyFormat("Node n{1, Node{1000}, //\n" 11783 " 2};"); 11784 verifyFormat("Aaaa aaaaaaa{\n" 11785 " {\n" 11786 " aaaa,\n" 11787 " },\n" 11788 "};"); 11789 verifyFormat("class C : public D {\n" 11790 " SomeClass SC{2};\n" 11791 "};"); 11792 verifyFormat("class C : public A {\n" 11793 " class D : public B {\n" 11794 " void f() { int i{2}; }\n" 11795 " };\n" 11796 "};"); 11797 verifyFormat("#define A {a, a},"); 11798 // Don't confuse braced list initializers with compound statements. 11799 verifyFormat( 11800 "class A {\n" 11801 " A() : a{} {}\n" 11802 " A(int b) : b(b) {}\n" 11803 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" 11804 " int a, b;\n" 11805 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" 11806 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " 11807 "{}\n" 11808 "};"); 11809 11810 // Avoid breaking between equal sign and opening brace 11811 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11812 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11813 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11814 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11815 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11816 " {\"ccccccccccccccccccccc\", 2}};", 11817 AvoidBreakingFirstArgument); 11818 11819 // Binpacking only if there is no trailing comma 11820 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11821 " cccccccccc, dddddddddd};", 11822 getLLVMStyleWithColumns(50)); 11823 verifyFormat("const Aaaaaa aaaaa = {\n" 11824 " aaaaaaaaaaa,\n" 11825 " bbbbbbbbbbb,\n" 11826 " ccccccccccc,\n" 11827 " ddddddddddd,\n" 11828 "};", 11829 getLLVMStyleWithColumns(50)); 11830 11831 // Cases where distinguising braced lists and blocks is hard. 11832 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11833 verifyFormat("void f() {\n" 11834 " return; // comment\n" 11835 "}\n" 11836 "SomeType t;"); 11837 verifyFormat("void f() {\n" 11838 " if (a) {\n" 11839 " f();\n" 11840 " }\n" 11841 "}\n" 11842 "SomeType t;"); 11843 11844 // In combination with BinPackArguments = false. 11845 FormatStyle NoBinPacking = getLLVMStyle(); 11846 NoBinPacking.BinPackArguments = false; 11847 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11848 " bbbbb,\n" 11849 " ccccc,\n" 11850 " ddddd,\n" 11851 " eeeee,\n" 11852 " ffffff,\n" 11853 " ggggg,\n" 11854 " hhhhhh,\n" 11855 " iiiiii,\n" 11856 " jjjjjj,\n" 11857 " kkkkkk};", 11858 NoBinPacking); 11859 verifyFormat("const Aaaaaa aaaaa = {\n" 11860 " aaaaa,\n" 11861 " bbbbb,\n" 11862 " ccccc,\n" 11863 " ddddd,\n" 11864 " eeeee,\n" 11865 " ffffff,\n" 11866 " ggggg,\n" 11867 " hhhhhh,\n" 11868 " iiiiii,\n" 11869 " jjjjjj,\n" 11870 " kkkkkk,\n" 11871 "};", 11872 NoBinPacking); 11873 verifyFormat( 11874 "const Aaaaaa aaaaa = {\n" 11875 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11876 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11877 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11878 "};", 11879 NoBinPacking); 11880 11881 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11882 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11883 " CDDDP83848_BMCR_REGISTER,\n" 11884 " CDDDP83848_BMSR_REGISTER,\n" 11885 " CDDDP83848_RBR_REGISTER};", 11886 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11887 " CDDDP83848_BMSR_REGISTER,\n" 11888 " CDDDP83848_RBR_REGISTER};", 11889 NoBinPacking)); 11890 11891 // FIXME: The alignment of these trailing comments might be bad. Then again, 11892 // this might be utterly useless in real code. 11893 verifyFormat("Constructor::Constructor()\n" 11894 " : some_value{ //\n" 11895 " aaaaaaa, //\n" 11896 " bbbbbbb} {}"); 11897 11898 // In braced lists, the first comment is always assumed to belong to the 11899 // first element. Thus, it can be moved to the next or previous line as 11900 // appropriate. 11901 EXPECT_EQ("function({// First element:\n" 11902 " 1,\n" 11903 " // Second element:\n" 11904 " 2});", 11905 format("function({\n" 11906 " // First element:\n" 11907 " 1,\n" 11908 " // Second element:\n" 11909 " 2});")); 11910 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11911 " // First element:\n" 11912 " 1,\n" 11913 " // Second element:\n" 11914 " 2};", 11915 format("std::vector<int> MyNumbers{// First element:\n" 11916 " 1,\n" 11917 " // Second element:\n" 11918 " 2};", 11919 getLLVMStyleWithColumns(30))); 11920 // A trailing comma should still lead to an enforced line break and no 11921 // binpacking. 11922 EXPECT_EQ("vector<int> SomeVector = {\n" 11923 " // aaa\n" 11924 " 1,\n" 11925 " 2,\n" 11926 "};", 11927 format("vector<int> SomeVector = { // aaa\n" 11928 " 1, 2, };")); 11929 11930 // C++11 brace initializer list l-braces should not be treated any differently 11931 // when breaking before lambda bodies is enabled 11932 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11933 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11934 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11935 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11936 verifyFormat( 11937 "std::runtime_error{\n" 11938 " \"Long string which will force a break onto the next line...\"};", 11939 BreakBeforeLambdaBody); 11940 11941 FormatStyle ExtraSpaces = getLLVMStyle(); 11942 ExtraSpaces.Cpp11BracedListStyle = false; 11943 ExtraSpaces.ColumnLimit = 75; 11944 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11945 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11946 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11947 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11948 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11949 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11950 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11951 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11952 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11953 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11954 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11955 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11956 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11957 verifyFormat("class Class {\n" 11958 " T member = { arg1, arg2 };\n" 11959 "};", 11960 ExtraSpaces); 11961 verifyFormat( 11962 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11963 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11964 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11965 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11966 ExtraSpaces); 11967 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11968 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11969 ExtraSpaces); 11970 verifyFormat( 11971 "someFunction(OtherParam,\n" 11972 " BracedList{ // comment 1 (Forcing interesting break)\n" 11973 " param1, param2,\n" 11974 " // comment 2\n" 11975 " param3, param4 });", 11976 ExtraSpaces); 11977 verifyFormat( 11978 "std::this_thread::sleep_for(\n" 11979 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11980 ExtraSpaces); 11981 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11982 " aaaaaaa,\n" 11983 " aaaaaaaaaa,\n" 11984 " aaaaa,\n" 11985 " aaaaaaaaaaaaaaa,\n" 11986 " aaa,\n" 11987 " aaaaaaaaaa,\n" 11988 " a,\n" 11989 " aaaaaaaaaaaaaaaaaaaaa,\n" 11990 " aaaaaaaaaaaa,\n" 11991 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11992 " aaaaaaa,\n" 11993 " a};"); 11994 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11995 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11996 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11997 11998 // Avoid breaking between initializer/equal sign and opening brace 11999 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 12000 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 12001 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 12002 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 12003 " { \"ccccccccccccccccccccc\", 2 }\n" 12004 "};", 12005 ExtraSpaces); 12006 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 12007 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 12008 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 12009 " { \"ccccccccccccccccccccc\", 2 }\n" 12010 "};", 12011 ExtraSpaces); 12012 12013 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 12014 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 12015 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 12016 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 12017 12018 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 12019 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 12020 SpaceBetweenBraces.SpacesInParentheses = true; 12021 SpaceBetweenBraces.SpacesInSquareBrackets = true; 12022 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 12023 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 12024 verifyFormat("vector< int > x{ // comment 1\n" 12025 " 1, 2, 3, 4 };", 12026 SpaceBetweenBraces); 12027 SpaceBetweenBraces.ColumnLimit = 20; 12028 EXPECT_EQ("vector< int > x{\n" 12029 " 1, 2, 3, 4 };", 12030 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 12031 SpaceBetweenBraces.ColumnLimit = 24; 12032 EXPECT_EQ("vector< int > x{ 1, 2,\n" 12033 " 3, 4 };", 12034 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 12035 EXPECT_EQ("vector< int > x{\n" 12036 " 1,\n" 12037 " 2,\n" 12038 " 3,\n" 12039 " 4,\n" 12040 "};", 12041 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 12042 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 12043 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 12044 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 12045 } 12046 12047 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 12048 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12049 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12050 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12051 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12052 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12053 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12054 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 12055 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12056 " 1, 22, 333, 4444, 55555, //\n" 12057 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12058 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12059 verifyFormat( 12060 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12061 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12062 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 12063 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12064 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12065 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12066 " 7777777};"); 12067 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12068 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12069 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12070 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12071 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12072 " // Separating comment.\n" 12073 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12074 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12075 " // Leading comment\n" 12076 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12077 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12078 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12079 " 1, 1, 1, 1};", 12080 getLLVMStyleWithColumns(39)); 12081 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12082 " 1, 1, 1, 1};", 12083 getLLVMStyleWithColumns(38)); 12084 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 12085 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 12086 getLLVMStyleWithColumns(43)); 12087 verifyFormat( 12088 "static unsigned SomeValues[10][3] = {\n" 12089 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 12090 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 12091 verifyFormat("static auto fields = new vector<string>{\n" 12092 " \"aaaaaaaaaaaaa\",\n" 12093 " \"aaaaaaaaaaaaa\",\n" 12094 " \"aaaaaaaaaaaa\",\n" 12095 " \"aaaaaaaaaaaaaa\",\n" 12096 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12097 " \"aaaaaaaaaaaa\",\n" 12098 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12099 "};"); 12100 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 12101 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 12102 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 12103 " 3, cccccccccccccccccccccc};", 12104 getLLVMStyleWithColumns(60)); 12105 12106 // Trailing commas. 12107 verifyFormat("vector<int> x = {\n" 12108 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 12109 "};", 12110 getLLVMStyleWithColumns(39)); 12111 verifyFormat("vector<int> x = {\n" 12112 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 12113 "};", 12114 getLLVMStyleWithColumns(39)); 12115 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12116 " 1, 1, 1, 1,\n" 12117 " /**/ /**/};", 12118 getLLVMStyleWithColumns(39)); 12119 12120 // Trailing comment in the first line. 12121 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 12122 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 12123 " 111111111, 222222222, 3333333333, 444444444, //\n" 12124 " 11111111, 22222222, 333333333, 44444444};"); 12125 // Trailing comment in the last line. 12126 verifyFormat("int aaaaa[] = {\n" 12127 " 1, 2, 3, // comment\n" 12128 " 4, 5, 6 // comment\n" 12129 "};"); 12130 12131 // With nested lists, we should either format one item per line or all nested 12132 // lists one on line. 12133 // FIXME: For some nested lists, we can do better. 12134 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 12135 " {aaaaaaaaaaaaaaaaaaa},\n" 12136 " {aaaaaaaaaaaaaaaaaaaaa},\n" 12137 " {aaaaaaaaaaaaaaaaa}};", 12138 getLLVMStyleWithColumns(60)); 12139 verifyFormat( 12140 "SomeStruct my_struct_array = {\n" 12141 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 12142 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 12143 " {aaa, aaa},\n" 12144 " {aaa, aaa},\n" 12145 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 12146 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 12147 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 12148 12149 // No column layout should be used here. 12150 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 12151 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 12152 12153 verifyNoCrash("a<,"); 12154 12155 // No braced initializer here. 12156 verifyFormat("void f() {\n" 12157 " struct Dummy {};\n" 12158 " f(v);\n" 12159 "}"); 12160 12161 // Long lists should be formatted in columns even if they are nested. 12162 verifyFormat( 12163 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12164 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12165 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12166 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12167 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12168 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 12169 12170 // Allow "single-column" layout even if that violates the column limit. There 12171 // isn't going to be a better way. 12172 verifyFormat("std::vector<int> a = {\n" 12173 " aaaaaaaa,\n" 12174 " aaaaaaaa,\n" 12175 " aaaaaaaa,\n" 12176 " aaaaaaaa,\n" 12177 " aaaaaaaaaa,\n" 12178 " aaaaaaaa,\n" 12179 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 12180 getLLVMStyleWithColumns(30)); 12181 verifyFormat("vector<int> aaaa = {\n" 12182 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12183 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12184 " aaaaaa.aaaaaaa,\n" 12185 " aaaaaa.aaaaaaa,\n" 12186 " aaaaaa.aaaaaaa,\n" 12187 " aaaaaa.aaaaaaa,\n" 12188 "};"); 12189 12190 // Don't create hanging lists. 12191 verifyFormat("someFunction(Param, {List1, List2,\n" 12192 " List3});", 12193 getLLVMStyleWithColumns(35)); 12194 verifyFormat("someFunction(Param, Param,\n" 12195 " {List1, List2,\n" 12196 " List3});", 12197 getLLVMStyleWithColumns(35)); 12198 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 12199 " aaaaaaaaaaaaaaaaaaaaaaa);"); 12200 } 12201 12202 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 12203 FormatStyle DoNotMerge = getLLVMStyle(); 12204 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12205 12206 verifyFormat("void f() { return 42; }"); 12207 verifyFormat("void f() {\n" 12208 " return 42;\n" 12209 "}", 12210 DoNotMerge); 12211 verifyFormat("void f() {\n" 12212 " // Comment\n" 12213 "}"); 12214 verifyFormat("{\n" 12215 "#error {\n" 12216 " int a;\n" 12217 "}"); 12218 verifyFormat("{\n" 12219 " int a;\n" 12220 "#error {\n" 12221 "}"); 12222 verifyFormat("void f() {} // comment"); 12223 verifyFormat("void f() { int a; } // comment"); 12224 verifyFormat("void f() {\n" 12225 "} // comment", 12226 DoNotMerge); 12227 verifyFormat("void f() {\n" 12228 " int a;\n" 12229 "} // comment", 12230 DoNotMerge); 12231 verifyFormat("void f() {\n" 12232 "} // comment", 12233 getLLVMStyleWithColumns(15)); 12234 12235 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 12236 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 12237 12238 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 12239 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 12240 verifyFormat("class C {\n" 12241 " C()\n" 12242 " : iiiiiiii(nullptr),\n" 12243 " kkkkkkk(nullptr),\n" 12244 " mmmmmmm(nullptr),\n" 12245 " nnnnnnn(nullptr) {}\n" 12246 "};", 12247 getGoogleStyle()); 12248 12249 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 12250 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 12251 EXPECT_EQ("class C {\n" 12252 " A() : b(0) {}\n" 12253 "};", 12254 format("class C{A():b(0){}};", NoColumnLimit)); 12255 EXPECT_EQ("A()\n" 12256 " : b(0) {\n" 12257 "}", 12258 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 12259 12260 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 12261 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 12262 FormatStyle::SFS_None; 12263 EXPECT_EQ("A()\n" 12264 " : b(0) {\n" 12265 "}", 12266 format("A():b(0){}", DoNotMergeNoColumnLimit)); 12267 EXPECT_EQ("A()\n" 12268 " : b(0) {\n" 12269 "}", 12270 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 12271 12272 verifyFormat("#define A \\\n" 12273 " void f() { \\\n" 12274 " int i; \\\n" 12275 " }", 12276 getLLVMStyleWithColumns(20)); 12277 verifyFormat("#define A \\\n" 12278 " void f() { int i; }", 12279 getLLVMStyleWithColumns(21)); 12280 verifyFormat("#define A \\\n" 12281 " void f() { \\\n" 12282 " int i; \\\n" 12283 " } \\\n" 12284 " int j;", 12285 getLLVMStyleWithColumns(22)); 12286 verifyFormat("#define A \\\n" 12287 " void f() { int i; } \\\n" 12288 " int j;", 12289 getLLVMStyleWithColumns(23)); 12290 } 12291 12292 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 12293 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12294 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12295 verifyFormat("class C {\n" 12296 " int f() {}\n" 12297 "};", 12298 MergeEmptyOnly); 12299 verifyFormat("class C {\n" 12300 " int f() {\n" 12301 " return 42;\n" 12302 " }\n" 12303 "};", 12304 MergeEmptyOnly); 12305 verifyFormat("int f() {}", MergeEmptyOnly); 12306 verifyFormat("int f() {\n" 12307 " return 42;\n" 12308 "}", 12309 MergeEmptyOnly); 12310 12311 // Also verify behavior when BraceWrapping.AfterFunction = true 12312 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12313 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 12314 verifyFormat("int f() {}", MergeEmptyOnly); 12315 verifyFormat("class C {\n" 12316 " int f() {}\n" 12317 "};", 12318 MergeEmptyOnly); 12319 } 12320 12321 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 12322 FormatStyle MergeInlineOnly = getLLVMStyle(); 12323 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12324 verifyFormat("class C {\n" 12325 " int f() { return 42; }\n" 12326 "};", 12327 MergeInlineOnly); 12328 verifyFormat("int f() {\n" 12329 " return 42;\n" 12330 "}", 12331 MergeInlineOnly); 12332 12333 // SFS_Inline implies SFS_Empty 12334 verifyFormat("class C {\n" 12335 " int f() {}\n" 12336 "};", 12337 MergeInlineOnly); 12338 verifyFormat("int f() {}", MergeInlineOnly); 12339 12340 // Also verify behavior when BraceWrapping.AfterFunction = true 12341 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12342 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12343 verifyFormat("class C {\n" 12344 " int f() { return 42; }\n" 12345 "};", 12346 MergeInlineOnly); 12347 verifyFormat("int f()\n" 12348 "{\n" 12349 " return 42;\n" 12350 "}", 12351 MergeInlineOnly); 12352 12353 // SFS_Inline implies SFS_Empty 12354 verifyFormat("int f() {}", MergeInlineOnly); 12355 verifyFormat("class C {\n" 12356 " int f() {}\n" 12357 "};", 12358 MergeInlineOnly); 12359 12360 MergeInlineOnly.BraceWrapping.AfterClass = true; 12361 MergeInlineOnly.BraceWrapping.AfterStruct = true; 12362 verifyFormat("class C\n" 12363 "{\n" 12364 " int f() { return 42; }\n" 12365 "};", 12366 MergeInlineOnly); 12367 verifyFormat("struct C\n" 12368 "{\n" 12369 " int f() { return 42; }\n" 12370 "};", 12371 MergeInlineOnly); 12372 verifyFormat("int f()\n" 12373 "{\n" 12374 " return 42;\n" 12375 "}", 12376 MergeInlineOnly); 12377 verifyFormat("int f() {}", MergeInlineOnly); 12378 verifyFormat("class C\n" 12379 "{\n" 12380 " int f() { return 42; }\n" 12381 "};", 12382 MergeInlineOnly); 12383 verifyFormat("struct C\n" 12384 "{\n" 12385 " int f() { return 42; }\n" 12386 "};", 12387 MergeInlineOnly); 12388 verifyFormat("struct C\n" 12389 "// comment\n" 12390 "/* comment */\n" 12391 "// comment\n" 12392 "{\n" 12393 " int f() { return 42; }\n" 12394 "};", 12395 MergeInlineOnly); 12396 verifyFormat("/* comment */ struct C\n" 12397 "{\n" 12398 " int f() { return 42; }\n" 12399 "};", 12400 MergeInlineOnly); 12401 } 12402 12403 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 12404 FormatStyle MergeInlineOnly = getLLVMStyle(); 12405 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 12406 FormatStyle::SFS_InlineOnly; 12407 verifyFormat("class C {\n" 12408 " int f() { return 42; }\n" 12409 "};", 12410 MergeInlineOnly); 12411 verifyFormat("int f() {\n" 12412 " return 42;\n" 12413 "}", 12414 MergeInlineOnly); 12415 12416 // SFS_InlineOnly does not imply SFS_Empty 12417 verifyFormat("class C {\n" 12418 " int f() {}\n" 12419 "};", 12420 MergeInlineOnly); 12421 verifyFormat("int f() {\n" 12422 "}", 12423 MergeInlineOnly); 12424 12425 // Also verify behavior when BraceWrapping.AfterFunction = true 12426 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12427 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12428 verifyFormat("class C {\n" 12429 " int f() { return 42; }\n" 12430 "};", 12431 MergeInlineOnly); 12432 verifyFormat("int f()\n" 12433 "{\n" 12434 " return 42;\n" 12435 "}", 12436 MergeInlineOnly); 12437 12438 // SFS_InlineOnly does not imply SFS_Empty 12439 verifyFormat("int f()\n" 12440 "{\n" 12441 "}", 12442 MergeInlineOnly); 12443 verifyFormat("class C {\n" 12444 " int f() {}\n" 12445 "};", 12446 MergeInlineOnly); 12447 } 12448 12449 TEST_F(FormatTest, SplitEmptyFunction) { 12450 FormatStyle Style = getLLVMStyleWithColumns(40); 12451 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12452 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12453 Style.BraceWrapping.AfterFunction = true; 12454 Style.BraceWrapping.SplitEmptyFunction = false; 12455 12456 verifyFormat("int f()\n" 12457 "{}", 12458 Style); 12459 verifyFormat("int f()\n" 12460 "{\n" 12461 " return 42;\n" 12462 "}", 12463 Style); 12464 verifyFormat("int f()\n" 12465 "{\n" 12466 " // some comment\n" 12467 "}", 12468 Style); 12469 12470 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12471 verifyFormat("int f() {}", Style); 12472 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12473 "{}", 12474 Style); 12475 verifyFormat("int f()\n" 12476 "{\n" 12477 " return 0;\n" 12478 "}", 12479 Style); 12480 12481 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12482 verifyFormat("class Foo {\n" 12483 " int f() {}\n" 12484 "};\n", 12485 Style); 12486 verifyFormat("class Foo {\n" 12487 " int f() { return 0; }\n" 12488 "};\n", 12489 Style); 12490 verifyFormat("class Foo {\n" 12491 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12492 " {}\n" 12493 "};\n", 12494 Style); 12495 verifyFormat("class Foo {\n" 12496 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12497 " {\n" 12498 " return 0;\n" 12499 " }\n" 12500 "};\n", 12501 Style); 12502 12503 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12504 verifyFormat("int f() {}", Style); 12505 verifyFormat("int f() { return 0; }", Style); 12506 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12507 "{}", 12508 Style); 12509 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12510 "{\n" 12511 " return 0;\n" 12512 "}", 12513 Style); 12514 } 12515 12516 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { 12517 FormatStyle Style = getLLVMStyleWithColumns(40); 12518 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12519 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12520 Style.BraceWrapping.AfterFunction = true; 12521 Style.BraceWrapping.SplitEmptyFunction = true; 12522 Style.BraceWrapping.SplitEmptyRecord = false; 12523 12524 verifyFormat("class C {};", Style); 12525 verifyFormat("struct C {};", Style); 12526 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12527 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12528 "{\n" 12529 "}", 12530 Style); 12531 verifyFormat("class C {\n" 12532 " C()\n" 12533 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" 12534 " bbbbbbbbbbbbbbbbbbb()\n" 12535 " {\n" 12536 " }\n" 12537 " void\n" 12538 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12539 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12540 " {\n" 12541 " }\n" 12542 "};", 12543 Style); 12544 } 12545 12546 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12547 FormatStyle Style = getLLVMStyle(); 12548 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12549 verifyFormat("#ifdef A\n" 12550 "int f() {}\n" 12551 "#else\n" 12552 "int g() {}\n" 12553 "#endif", 12554 Style); 12555 } 12556 12557 TEST_F(FormatTest, SplitEmptyClass) { 12558 FormatStyle Style = getLLVMStyle(); 12559 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12560 Style.BraceWrapping.AfterClass = true; 12561 Style.BraceWrapping.SplitEmptyRecord = false; 12562 12563 verifyFormat("class Foo\n" 12564 "{};", 12565 Style); 12566 verifyFormat("/* something */ class Foo\n" 12567 "{};", 12568 Style); 12569 verifyFormat("template <typename X> class Foo\n" 12570 "{};", 12571 Style); 12572 verifyFormat("class Foo\n" 12573 "{\n" 12574 " Foo();\n" 12575 "};", 12576 Style); 12577 verifyFormat("typedef class Foo\n" 12578 "{\n" 12579 "} Foo_t;", 12580 Style); 12581 12582 Style.BraceWrapping.SplitEmptyRecord = true; 12583 Style.BraceWrapping.AfterStruct = true; 12584 verifyFormat("class rep\n" 12585 "{\n" 12586 "};", 12587 Style); 12588 verifyFormat("struct rep\n" 12589 "{\n" 12590 "};", 12591 Style); 12592 verifyFormat("template <typename T> class rep\n" 12593 "{\n" 12594 "};", 12595 Style); 12596 verifyFormat("template <typename T> struct rep\n" 12597 "{\n" 12598 "};", 12599 Style); 12600 verifyFormat("class rep\n" 12601 "{\n" 12602 " int x;\n" 12603 "};", 12604 Style); 12605 verifyFormat("struct rep\n" 12606 "{\n" 12607 " int x;\n" 12608 "};", 12609 Style); 12610 verifyFormat("template <typename T> class rep\n" 12611 "{\n" 12612 " int x;\n" 12613 "};", 12614 Style); 12615 verifyFormat("template <typename T> struct rep\n" 12616 "{\n" 12617 " int x;\n" 12618 "};", 12619 Style); 12620 verifyFormat("template <typename T> class rep // Foo\n" 12621 "{\n" 12622 " int x;\n" 12623 "};", 12624 Style); 12625 verifyFormat("template <typename T> struct rep // Bar\n" 12626 "{\n" 12627 " int x;\n" 12628 "};", 12629 Style); 12630 12631 verifyFormat("template <typename T> class rep<T>\n" 12632 "{\n" 12633 " int x;\n" 12634 "};", 12635 Style); 12636 12637 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12638 "{\n" 12639 " int x;\n" 12640 "};", 12641 Style); 12642 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12643 "{\n" 12644 "};", 12645 Style); 12646 12647 verifyFormat("#include \"stdint.h\"\n" 12648 "namespace rep {}", 12649 Style); 12650 verifyFormat("#include <stdint.h>\n" 12651 "namespace rep {}", 12652 Style); 12653 verifyFormat("#include <stdint.h>\n" 12654 "namespace rep {}", 12655 "#include <stdint.h>\n" 12656 "namespace rep {\n" 12657 "\n" 12658 "\n" 12659 "}", 12660 Style); 12661 } 12662 12663 TEST_F(FormatTest, SplitEmptyStruct) { 12664 FormatStyle Style = getLLVMStyle(); 12665 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12666 Style.BraceWrapping.AfterStruct = true; 12667 Style.BraceWrapping.SplitEmptyRecord = false; 12668 12669 verifyFormat("struct Foo\n" 12670 "{};", 12671 Style); 12672 verifyFormat("/* something */ struct Foo\n" 12673 "{};", 12674 Style); 12675 verifyFormat("template <typename X> struct Foo\n" 12676 "{};", 12677 Style); 12678 verifyFormat("struct Foo\n" 12679 "{\n" 12680 " Foo();\n" 12681 "};", 12682 Style); 12683 verifyFormat("typedef struct Foo\n" 12684 "{\n" 12685 "} Foo_t;", 12686 Style); 12687 // typedef struct Bar {} Bar_t; 12688 } 12689 12690 TEST_F(FormatTest, SplitEmptyUnion) { 12691 FormatStyle Style = getLLVMStyle(); 12692 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12693 Style.BraceWrapping.AfterUnion = true; 12694 Style.BraceWrapping.SplitEmptyRecord = false; 12695 12696 verifyFormat("union Foo\n" 12697 "{};", 12698 Style); 12699 verifyFormat("/* something */ union Foo\n" 12700 "{};", 12701 Style); 12702 verifyFormat("union Foo\n" 12703 "{\n" 12704 " A,\n" 12705 "};", 12706 Style); 12707 verifyFormat("typedef union Foo\n" 12708 "{\n" 12709 "} Foo_t;", 12710 Style); 12711 } 12712 12713 TEST_F(FormatTest, SplitEmptyNamespace) { 12714 FormatStyle Style = getLLVMStyle(); 12715 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12716 Style.BraceWrapping.AfterNamespace = true; 12717 Style.BraceWrapping.SplitEmptyNamespace = false; 12718 12719 verifyFormat("namespace Foo\n" 12720 "{};", 12721 Style); 12722 verifyFormat("/* something */ namespace Foo\n" 12723 "{};", 12724 Style); 12725 verifyFormat("inline namespace Foo\n" 12726 "{};", 12727 Style); 12728 verifyFormat("/* something */ inline namespace Foo\n" 12729 "{};", 12730 Style); 12731 verifyFormat("export namespace Foo\n" 12732 "{};", 12733 Style); 12734 verifyFormat("namespace Foo\n" 12735 "{\n" 12736 "void Bar();\n" 12737 "};", 12738 Style); 12739 } 12740 12741 TEST_F(FormatTest, NeverMergeShortRecords) { 12742 FormatStyle Style = getLLVMStyle(); 12743 12744 verifyFormat("class Foo {\n" 12745 " Foo();\n" 12746 "};", 12747 Style); 12748 verifyFormat("typedef class Foo {\n" 12749 " Foo();\n" 12750 "} Foo_t;", 12751 Style); 12752 verifyFormat("struct Foo {\n" 12753 " Foo();\n" 12754 "};", 12755 Style); 12756 verifyFormat("typedef struct Foo {\n" 12757 " Foo();\n" 12758 "} Foo_t;", 12759 Style); 12760 verifyFormat("union Foo {\n" 12761 " A,\n" 12762 "};", 12763 Style); 12764 verifyFormat("typedef union Foo {\n" 12765 " A,\n" 12766 "} Foo_t;", 12767 Style); 12768 verifyFormat("namespace Foo {\n" 12769 "void Bar();\n" 12770 "};", 12771 Style); 12772 12773 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12774 Style.BraceWrapping.AfterClass = true; 12775 Style.BraceWrapping.AfterStruct = true; 12776 Style.BraceWrapping.AfterUnion = true; 12777 Style.BraceWrapping.AfterNamespace = true; 12778 verifyFormat("class Foo\n" 12779 "{\n" 12780 " Foo();\n" 12781 "};", 12782 Style); 12783 verifyFormat("typedef class Foo\n" 12784 "{\n" 12785 " Foo();\n" 12786 "} Foo_t;", 12787 Style); 12788 verifyFormat("struct Foo\n" 12789 "{\n" 12790 " Foo();\n" 12791 "};", 12792 Style); 12793 verifyFormat("typedef struct Foo\n" 12794 "{\n" 12795 " Foo();\n" 12796 "} Foo_t;", 12797 Style); 12798 verifyFormat("union Foo\n" 12799 "{\n" 12800 " A,\n" 12801 "};", 12802 Style); 12803 verifyFormat("typedef union Foo\n" 12804 "{\n" 12805 " A,\n" 12806 "} Foo_t;", 12807 Style); 12808 verifyFormat("namespace Foo\n" 12809 "{\n" 12810 "void Bar();\n" 12811 "};", 12812 Style); 12813 } 12814 12815 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12816 // Elaborate type variable declarations. 12817 verifyFormat("struct foo a = {bar};\nint n;"); 12818 verifyFormat("class foo a = {bar};\nint n;"); 12819 verifyFormat("union foo a = {bar};\nint n;"); 12820 12821 // Elaborate types inside function definitions. 12822 verifyFormat("struct foo f() {}\nint n;"); 12823 verifyFormat("class foo f() {}\nint n;"); 12824 verifyFormat("union foo f() {}\nint n;"); 12825 12826 // Templates. 12827 verifyFormat("template <class X> void f() {}\nint n;"); 12828 verifyFormat("template <struct X> void f() {}\nint n;"); 12829 verifyFormat("template <union X> void f() {}\nint n;"); 12830 12831 // Actual definitions... 12832 verifyFormat("struct {\n} n;"); 12833 verifyFormat( 12834 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12835 verifyFormat("union Z {\n int n;\n} x;"); 12836 verifyFormat("class MACRO Z {\n} n;"); 12837 verifyFormat("class MACRO(X) Z {\n} n;"); 12838 verifyFormat("class __attribute__(X) Z {\n} n;"); 12839 verifyFormat("class __declspec(X) Z {\n} n;"); 12840 verifyFormat("class A##B##C {\n} n;"); 12841 verifyFormat("class alignas(16) Z {\n} n;"); 12842 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12843 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12844 12845 // Redefinition from nested context: 12846 verifyFormat("class A::B::C {\n} n;"); 12847 12848 // Template definitions. 12849 verifyFormat( 12850 "template <typename F>\n" 12851 "Matcher(const Matcher<F> &Other,\n" 12852 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12853 " !is_same<F, T>::value>::type * = 0)\n" 12854 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12855 12856 // FIXME: This is still incorrectly handled at the formatter side. 12857 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12858 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12859 12860 // FIXME: 12861 // This now gets parsed incorrectly as class definition. 12862 // verifyFormat("class A<int> f() {\n}\nint n;"); 12863 12864 // Elaborate types where incorrectly parsing the structural element would 12865 // break the indent. 12866 verifyFormat("if (true)\n" 12867 " class X x;\n" 12868 "else\n" 12869 " f();\n"); 12870 12871 // This is simply incomplete. Formatting is not important, but must not crash. 12872 verifyFormat("class A:"); 12873 } 12874 12875 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12876 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12877 format("#error Leave all white!!!!! space* alone!\n")); 12878 EXPECT_EQ( 12879 "#warning Leave all white!!!!! space* alone!\n", 12880 format("#warning Leave all white!!!!! space* alone!\n")); 12881 EXPECT_EQ("#error 1", format(" # error 1")); 12882 EXPECT_EQ("#warning 1", format(" # warning 1")); 12883 } 12884 12885 TEST_F(FormatTest, FormatHashIfExpressions) { 12886 verifyFormat("#if AAAA && BBBB"); 12887 verifyFormat("#if (AAAA && BBBB)"); 12888 verifyFormat("#elif (AAAA && BBBB)"); 12889 // FIXME: Come up with a better indentation for #elif. 12890 verifyFormat( 12891 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12892 " defined(BBBBBBBB)\n" 12893 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12894 " defined(BBBBBBBB)\n" 12895 "#endif", 12896 getLLVMStyleWithColumns(65)); 12897 } 12898 12899 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12900 FormatStyle AllowsMergedIf = getGoogleStyle(); 12901 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12902 FormatStyle::SIS_WithoutElse; 12903 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12904 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12905 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12906 EXPECT_EQ("if (true) return 42;", 12907 format("if (true)\nreturn 42;", AllowsMergedIf)); 12908 FormatStyle ShortMergedIf = AllowsMergedIf; 12909 ShortMergedIf.ColumnLimit = 25; 12910 verifyFormat("#define A \\\n" 12911 " if (true) return 42;", 12912 ShortMergedIf); 12913 verifyFormat("#define A \\\n" 12914 " f(); \\\n" 12915 " if (true)\n" 12916 "#define B", 12917 ShortMergedIf); 12918 verifyFormat("#define A \\\n" 12919 " f(); \\\n" 12920 " if (true)\n" 12921 "g();", 12922 ShortMergedIf); 12923 verifyFormat("{\n" 12924 "#ifdef A\n" 12925 " // Comment\n" 12926 " if (true) continue;\n" 12927 "#endif\n" 12928 " // Comment\n" 12929 " if (true) continue;\n" 12930 "}", 12931 ShortMergedIf); 12932 ShortMergedIf.ColumnLimit = 33; 12933 verifyFormat("#define A \\\n" 12934 " if constexpr (true) return 42;", 12935 ShortMergedIf); 12936 verifyFormat("#define A \\\n" 12937 " if CONSTEXPR (true) return 42;", 12938 ShortMergedIf); 12939 ShortMergedIf.ColumnLimit = 29; 12940 verifyFormat("#define A \\\n" 12941 " if (aaaaaaaaaa) return 1; \\\n" 12942 " return 2;", 12943 ShortMergedIf); 12944 ShortMergedIf.ColumnLimit = 28; 12945 verifyFormat("#define A \\\n" 12946 " if (aaaaaaaaaa) \\\n" 12947 " return 1; \\\n" 12948 " return 2;", 12949 ShortMergedIf); 12950 verifyFormat("#define A \\\n" 12951 " if constexpr (aaaaaaa) \\\n" 12952 " return 1; \\\n" 12953 " return 2;", 12954 ShortMergedIf); 12955 verifyFormat("#define A \\\n" 12956 " if CONSTEXPR (aaaaaaa) \\\n" 12957 " return 1; \\\n" 12958 " return 2;", 12959 ShortMergedIf); 12960 } 12961 12962 TEST_F(FormatTest, FormatStarDependingOnContext) { 12963 verifyFormat("void f(int *a);"); 12964 verifyFormat("void f() { f(fint * b); }"); 12965 verifyFormat("class A {\n void f(int *a);\n};"); 12966 verifyFormat("class A {\n int *a;\n};"); 12967 verifyFormat("namespace a {\n" 12968 "namespace b {\n" 12969 "class A {\n" 12970 " void f() {}\n" 12971 " int *a;\n" 12972 "};\n" 12973 "} // namespace b\n" 12974 "} // namespace a"); 12975 } 12976 12977 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12978 verifyFormat("while"); 12979 verifyFormat("operator"); 12980 } 12981 12982 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12983 // This code would be painfully slow to format if we didn't skip it. 12984 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 12985 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12986 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12987 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12988 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12989 "A(1, 1)\n" 12990 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12991 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12992 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12993 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12994 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12995 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12996 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12997 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12998 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12999 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 13000 // Deeply nested part is untouched, rest is formatted. 13001 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 13002 format(std::string("int i;\n") + Code + "int j;\n", 13003 getLLVMStyle(), SC_ExpectIncomplete)); 13004 } 13005 13006 //===----------------------------------------------------------------------===// 13007 // Objective-C tests. 13008 //===----------------------------------------------------------------------===// 13009 13010 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 13011 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 13012 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 13013 format("-(NSUInteger)indexOfObject:(id)anObject;")); 13014 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 13015 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 13016 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 13017 format("-(NSInteger)Method3:(id)anObject;")); 13018 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 13019 format("-(NSInteger)Method4:(id)anObject;")); 13020 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 13021 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 13022 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 13023 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 13024 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 13025 "forAllCells:(BOOL)flag;", 13026 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 13027 "forAllCells:(BOOL)flag;")); 13028 13029 // Very long objectiveC method declaration. 13030 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 13031 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 13032 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 13033 " inRange:(NSRange)range\n" 13034 " outRange:(NSRange)out_range\n" 13035 " outRange1:(NSRange)out_range1\n" 13036 " outRange2:(NSRange)out_range2\n" 13037 " outRange3:(NSRange)out_range3\n" 13038 " outRange4:(NSRange)out_range4\n" 13039 " outRange5:(NSRange)out_range5\n" 13040 " outRange6:(NSRange)out_range6\n" 13041 " outRange7:(NSRange)out_range7\n" 13042 " outRange8:(NSRange)out_range8\n" 13043 " outRange9:(NSRange)out_range9;"); 13044 13045 // When the function name has to be wrapped. 13046 FormatStyle Style = getLLVMStyle(); 13047 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 13048 // and always indents instead. 13049 Style.IndentWrappedFunctionNames = false; 13050 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13051 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 13052 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 13053 "}", 13054 Style); 13055 Style.IndentWrappedFunctionNames = true; 13056 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13057 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 13058 " anotherName:(NSString)dddddddddddddd {\n" 13059 "}", 13060 Style); 13061 13062 verifyFormat("- (int)sum:(vector<int>)numbers;"); 13063 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 13064 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 13065 // protocol lists (but not for template classes): 13066 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 13067 13068 verifyFormat("- (int (*)())foo:(int (*)())f;"); 13069 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 13070 13071 // If there's no return type (very rare in practice!), LLVM and Google style 13072 // agree. 13073 verifyFormat("- foo;"); 13074 verifyFormat("- foo:(int)f;"); 13075 verifyGoogleFormat("- foo:(int)foo;"); 13076 } 13077 13078 TEST_F(FormatTest, BreaksStringLiterals) { 13079 EXPECT_EQ("\"some text \"\n" 13080 "\"other\";", 13081 format("\"some text other\";", getLLVMStyleWithColumns(12))); 13082 EXPECT_EQ("\"some text \"\n" 13083 "\"other\";", 13084 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 13085 EXPECT_EQ( 13086 "#define A \\\n" 13087 " \"some \" \\\n" 13088 " \"text \" \\\n" 13089 " \"other\";", 13090 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 13091 EXPECT_EQ( 13092 "#define A \\\n" 13093 " \"so \" \\\n" 13094 " \"text \" \\\n" 13095 " \"other\";", 13096 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 13097 13098 EXPECT_EQ("\"some text\"", 13099 format("\"some text\"", getLLVMStyleWithColumns(1))); 13100 EXPECT_EQ("\"some text\"", 13101 format("\"some text\"", getLLVMStyleWithColumns(11))); 13102 EXPECT_EQ("\"some \"\n" 13103 "\"text\"", 13104 format("\"some text\"", getLLVMStyleWithColumns(10))); 13105 EXPECT_EQ("\"some \"\n" 13106 "\"text\"", 13107 format("\"some text\"", getLLVMStyleWithColumns(7))); 13108 EXPECT_EQ("\"some\"\n" 13109 "\" tex\"\n" 13110 "\"t\"", 13111 format("\"some text\"", getLLVMStyleWithColumns(6))); 13112 EXPECT_EQ("\"some\"\n" 13113 "\" tex\"\n" 13114 "\" and\"", 13115 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 13116 EXPECT_EQ("\"some\"\n" 13117 "\"/tex\"\n" 13118 "\"/and\"", 13119 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 13120 13121 EXPECT_EQ("variable =\n" 13122 " \"long string \"\n" 13123 " \"literal\";", 13124 format("variable = \"long string literal\";", 13125 getLLVMStyleWithColumns(20))); 13126 13127 EXPECT_EQ("variable = f(\n" 13128 " \"long string \"\n" 13129 " \"literal\",\n" 13130 " short,\n" 13131 " loooooooooooooooooooong);", 13132 format("variable = f(\"long string literal\", short, " 13133 "loooooooooooooooooooong);", 13134 getLLVMStyleWithColumns(20))); 13135 13136 EXPECT_EQ( 13137 "f(g(\"long string \"\n" 13138 " \"literal\"),\n" 13139 " b);", 13140 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 13141 EXPECT_EQ("f(g(\"long string \"\n" 13142 " \"literal\",\n" 13143 " a),\n" 13144 " b);", 13145 format("f(g(\"long string literal\", a), b);", 13146 getLLVMStyleWithColumns(20))); 13147 EXPECT_EQ( 13148 "f(\"one two\".split(\n" 13149 " variable));", 13150 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 13151 EXPECT_EQ("f(\"one two three four five six \"\n" 13152 " \"seven\".split(\n" 13153 " really_looooong_variable));", 13154 format("f(\"one two three four five six seven\"." 13155 "split(really_looooong_variable));", 13156 getLLVMStyleWithColumns(33))); 13157 13158 EXPECT_EQ("f(\"some \"\n" 13159 " \"text\",\n" 13160 " other);", 13161 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 13162 13163 // Only break as a last resort. 13164 verifyFormat( 13165 "aaaaaaaaaaaaaaaaaaaa(\n" 13166 " aaaaaaaaaaaaaaaaaaaa,\n" 13167 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 13168 13169 EXPECT_EQ("\"splitmea\"\n" 13170 "\"trandomp\"\n" 13171 "\"oint\"", 13172 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 13173 13174 EXPECT_EQ("\"split/\"\n" 13175 "\"pathat/\"\n" 13176 "\"slashes\"", 13177 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13178 13179 EXPECT_EQ("\"split/\"\n" 13180 "\"pathat/\"\n" 13181 "\"slashes\"", 13182 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13183 EXPECT_EQ("\"split at \"\n" 13184 "\"spaces/at/\"\n" 13185 "\"slashes.at.any$\"\n" 13186 "\"non-alphanumeric%\"\n" 13187 "\"1111111111characte\"\n" 13188 "\"rs\"", 13189 format("\"split at " 13190 "spaces/at/" 13191 "slashes.at." 13192 "any$non-" 13193 "alphanumeric%" 13194 "1111111111characte" 13195 "rs\"", 13196 getLLVMStyleWithColumns(20))); 13197 13198 // Verify that splitting the strings understands 13199 // Style::AlwaysBreakBeforeMultilineStrings. 13200 EXPECT_EQ("aaaaaaaaaaaa(\n" 13201 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 13202 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 13203 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 13204 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13205 "aaaaaaaaaaaaaaaaaaaaaa\");", 13206 getGoogleStyle())); 13207 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13208 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 13209 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 13210 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13211 "aaaaaaaaaaaaaaaaaaaaaa\";", 13212 getGoogleStyle())); 13213 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13214 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13215 format("llvm::outs() << " 13216 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 13217 "aaaaaaaaaaaaaaaaaaa\";")); 13218 EXPECT_EQ("ffff(\n" 13219 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13220 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13221 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 13222 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13223 getGoogleStyle())); 13224 13225 FormatStyle Style = getLLVMStyleWithColumns(12); 13226 Style.BreakStringLiterals = false; 13227 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 13228 13229 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 13230 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13231 EXPECT_EQ("#define A \\\n" 13232 " \"some \" \\\n" 13233 " \"text \" \\\n" 13234 " \"other\";", 13235 format("#define A \"some text other\";", AlignLeft)); 13236 } 13237 13238 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 13239 EXPECT_EQ("C a = \"some more \"\n" 13240 " \"text\";", 13241 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 13242 } 13243 13244 TEST_F(FormatTest, FullyRemoveEmptyLines) { 13245 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 13246 NoEmptyLines.MaxEmptyLinesToKeep = 0; 13247 EXPECT_EQ("int i = a(b());", 13248 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 13249 } 13250 13251 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 13252 EXPECT_EQ( 13253 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13254 "(\n" 13255 " \"x\t\");", 13256 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13257 "aaaaaaa(" 13258 "\"x\t\");")); 13259 } 13260 13261 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 13262 EXPECT_EQ( 13263 "u8\"utf8 string \"\n" 13264 "u8\"literal\";", 13265 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 13266 EXPECT_EQ( 13267 "u\"utf16 string \"\n" 13268 "u\"literal\";", 13269 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 13270 EXPECT_EQ( 13271 "U\"utf32 string \"\n" 13272 "U\"literal\";", 13273 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 13274 EXPECT_EQ("L\"wide string \"\n" 13275 "L\"literal\";", 13276 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 13277 EXPECT_EQ("@\"NSString \"\n" 13278 "@\"literal\";", 13279 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 13280 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 13281 13282 // This input makes clang-format try to split the incomplete unicode escape 13283 // sequence, which used to lead to a crasher. 13284 verifyNoCrash( 13285 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 13286 getLLVMStyleWithColumns(60)); 13287 } 13288 13289 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 13290 FormatStyle Style = getGoogleStyleWithColumns(15); 13291 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 13292 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 13293 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 13294 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 13295 EXPECT_EQ("u8R\"x(raw literal)x\";", 13296 format("u8R\"x(raw literal)x\";", Style)); 13297 } 13298 13299 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 13300 FormatStyle Style = getLLVMStyleWithColumns(20); 13301 EXPECT_EQ( 13302 "_T(\"aaaaaaaaaaaaaa\")\n" 13303 "_T(\"aaaaaaaaaaaaaa\")\n" 13304 "_T(\"aaaaaaaaaaaa\")", 13305 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 13306 EXPECT_EQ("f(x,\n" 13307 " _T(\"aaaaaaaaaaaa\")\n" 13308 " _T(\"aaa\"),\n" 13309 " z);", 13310 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 13311 13312 // FIXME: Handle embedded spaces in one iteration. 13313 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 13314 // "_T(\"aaaaaaaaaaaaa\")\n" 13315 // "_T(\"aaaaaaaaaaaaa\")\n" 13316 // "_T(\"a\")", 13317 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13318 // getLLVMStyleWithColumns(20))); 13319 EXPECT_EQ( 13320 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13321 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 13322 EXPECT_EQ("f(\n" 13323 "#if !TEST\n" 13324 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13325 "#endif\n" 13326 ");", 13327 format("f(\n" 13328 "#if !TEST\n" 13329 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13330 "#endif\n" 13331 ");")); 13332 EXPECT_EQ("f(\n" 13333 "\n" 13334 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 13335 format("f(\n" 13336 "\n" 13337 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 13338 // Regression test for accessing tokens past the end of a vector in the 13339 // TokenLexer. 13340 verifyNoCrash(R"(_T( 13341 " 13342 ) 13343 )"); 13344 } 13345 13346 TEST_F(FormatTest, BreaksStringLiteralOperands) { 13347 // In a function call with two operands, the second can be broken with no line 13348 // break before it. 13349 EXPECT_EQ( 13350 "func(a, \"long long \"\n" 13351 " \"long long\");", 13352 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 13353 // In a function call with three operands, the second must be broken with a 13354 // line break before it. 13355 EXPECT_EQ("func(a,\n" 13356 " \"long long long \"\n" 13357 " \"long\",\n" 13358 " c);", 13359 format("func(a, \"long long long long\", c);", 13360 getLLVMStyleWithColumns(24))); 13361 // In a function call with three operands, the third must be broken with a 13362 // line break before it. 13363 EXPECT_EQ("func(a, b,\n" 13364 " \"long long long \"\n" 13365 " \"long\");", 13366 format("func(a, b, \"long long long long\");", 13367 getLLVMStyleWithColumns(24))); 13368 // In a function call with three operands, both the second and the third must 13369 // be broken with a line break before them. 13370 EXPECT_EQ("func(a,\n" 13371 " \"long long long \"\n" 13372 " \"long\",\n" 13373 " \"long long long \"\n" 13374 " \"long\");", 13375 format("func(a, \"long long long long\", \"long long long long\");", 13376 getLLVMStyleWithColumns(24))); 13377 // In a chain of << with two operands, the second can be broken with no line 13378 // break before it. 13379 EXPECT_EQ("a << \"line line \"\n" 13380 " \"line\";", 13381 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 13382 // In a chain of << with three operands, the second can be broken with no line 13383 // break before it. 13384 EXPECT_EQ( 13385 "abcde << \"line \"\n" 13386 " \"line line\"\n" 13387 " << c;", 13388 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 13389 // In a chain of << with three operands, the third must be broken with a line 13390 // break before it. 13391 EXPECT_EQ( 13392 "a << b\n" 13393 " << \"line line \"\n" 13394 " \"line\";", 13395 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 13396 // In a chain of << with three operands, the second can be broken with no line 13397 // break before it and the third must be broken with a line break before it. 13398 EXPECT_EQ("abcd << \"line line \"\n" 13399 " \"line\"\n" 13400 " << \"line line \"\n" 13401 " \"line\";", 13402 format("abcd << \"line line line\" << \"line line line\";", 13403 getLLVMStyleWithColumns(20))); 13404 // In a chain of binary operators with two operands, the second can be broken 13405 // with no line break before it. 13406 EXPECT_EQ( 13407 "abcd + \"line line \"\n" 13408 " \"line line\";", 13409 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 13410 // In a chain of binary operators with three operands, the second must be 13411 // broken with a line break before it. 13412 EXPECT_EQ("abcd +\n" 13413 " \"line line \"\n" 13414 " \"line line\" +\n" 13415 " e;", 13416 format("abcd + \"line line line line\" + e;", 13417 getLLVMStyleWithColumns(20))); 13418 // In a function call with two operands, with AlignAfterOpenBracket enabled, 13419 // the first must be broken with a line break before it. 13420 FormatStyle Style = getLLVMStyleWithColumns(25); 13421 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13422 EXPECT_EQ("someFunction(\n" 13423 " \"long long long \"\n" 13424 " \"long\",\n" 13425 " a);", 13426 format("someFunction(\"long long long long\", a);", Style)); 13427 } 13428 13429 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 13430 EXPECT_EQ( 13431 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13434 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 13437 } 13438 13439 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 13440 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 13441 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 13442 EXPECT_EQ("fffffffffff(g(R\"x(\n" 13443 "multiline raw string literal xxxxxxxxxxxxxx\n" 13444 ")x\",\n" 13445 " a),\n" 13446 " b);", 13447 format("fffffffffff(g(R\"x(\n" 13448 "multiline raw string literal xxxxxxxxxxxxxx\n" 13449 ")x\", a), b);", 13450 getGoogleStyleWithColumns(20))); 13451 EXPECT_EQ("fffffffffff(\n" 13452 " g(R\"x(qqq\n" 13453 "multiline raw string literal xxxxxxxxxxxxxx\n" 13454 ")x\",\n" 13455 " a),\n" 13456 " b);", 13457 format("fffffffffff(g(R\"x(qqq\n" 13458 "multiline raw string literal xxxxxxxxxxxxxx\n" 13459 ")x\", a), b);", 13460 getGoogleStyleWithColumns(20))); 13461 13462 EXPECT_EQ("fffffffffff(R\"x(\n" 13463 "multiline raw string literal xxxxxxxxxxxxxx\n" 13464 ")x\");", 13465 format("fffffffffff(R\"x(\n" 13466 "multiline raw string literal xxxxxxxxxxxxxx\n" 13467 ")x\");", 13468 getGoogleStyleWithColumns(20))); 13469 EXPECT_EQ("fffffffffff(R\"x(\n" 13470 "multiline raw string literal xxxxxxxxxxxxxx\n" 13471 ")x\" + bbbbbb);", 13472 format("fffffffffff(R\"x(\n" 13473 "multiline raw string literal xxxxxxxxxxxxxx\n" 13474 ")x\" + bbbbbb);", 13475 getGoogleStyleWithColumns(20))); 13476 EXPECT_EQ("fffffffffff(\n" 13477 " R\"x(\n" 13478 "multiline raw string literal xxxxxxxxxxxxxx\n" 13479 ")x\" +\n" 13480 " bbbbbb);", 13481 format("fffffffffff(\n" 13482 " R\"x(\n" 13483 "multiline raw string literal xxxxxxxxxxxxxx\n" 13484 ")x\" + bbbbbb);", 13485 getGoogleStyleWithColumns(20))); 13486 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 13487 format("fffffffffff(\n" 13488 " R\"(single line raw string)\" + bbbbbb);")); 13489 } 13490 13491 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13492 verifyFormat("string a = \"unterminated;"); 13493 EXPECT_EQ("function(\"unterminated,\n" 13494 " OtherParameter);", 13495 format("function( \"unterminated,\n" 13496 " OtherParameter);")); 13497 } 13498 13499 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13500 FormatStyle Style = getLLVMStyle(); 13501 Style.Standard = FormatStyle::LS_Cpp03; 13502 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13503 format("#define x(_a) printf(\"foo\"_a);", Style)); 13504 } 13505 13506 TEST_F(FormatTest, CppLexVersion) { 13507 FormatStyle Style = getLLVMStyle(); 13508 // Formatting of x * y differs if x is a type. 13509 verifyFormat("void foo() { MACRO(a * b); }", Style); 13510 verifyFormat("void foo() { MACRO(int *b); }", Style); 13511 13512 // LLVM style uses latest lexer. 13513 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13514 Style.Standard = FormatStyle::LS_Cpp17; 13515 // But in c++17, char8_t isn't a keyword. 13516 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13517 } 13518 13519 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13520 13521 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13522 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13523 " \"ddeeefff\");", 13524 format("someFunction(\"aaabbbcccdddeeefff\");", 13525 getLLVMStyleWithColumns(25))); 13526 EXPECT_EQ("someFunction1234567890(\n" 13527 " \"aaabbbcccdddeeefff\");", 13528 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13529 getLLVMStyleWithColumns(26))); 13530 EXPECT_EQ("someFunction1234567890(\n" 13531 " \"aaabbbcccdddeeeff\"\n" 13532 " \"f\");", 13533 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13534 getLLVMStyleWithColumns(25))); 13535 EXPECT_EQ("someFunction1234567890(\n" 13536 " \"aaabbbcccdddeeeff\"\n" 13537 " \"f\");", 13538 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13539 getLLVMStyleWithColumns(24))); 13540 EXPECT_EQ("someFunction(\n" 13541 " \"aaabbbcc ddde \"\n" 13542 " \"efff\");", 13543 format("someFunction(\"aaabbbcc ddde efff\");", 13544 getLLVMStyleWithColumns(25))); 13545 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13546 " \"ddeeefff\");", 13547 format("someFunction(\"aaabbbccc ddeeefff\");", 13548 getLLVMStyleWithColumns(25))); 13549 EXPECT_EQ("someFunction1234567890(\n" 13550 " \"aaabb \"\n" 13551 " \"cccdddeeefff\");", 13552 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13553 getLLVMStyleWithColumns(25))); 13554 EXPECT_EQ("#define A \\\n" 13555 " string s = \\\n" 13556 " \"123456789\" \\\n" 13557 " \"0\"; \\\n" 13558 " int i;", 13559 format("#define A string s = \"1234567890\"; int i;", 13560 getLLVMStyleWithColumns(20))); 13561 EXPECT_EQ("someFunction(\n" 13562 " \"aaabbbcc \"\n" 13563 " \"dddeeefff\");", 13564 format("someFunction(\"aaabbbcc dddeeefff\");", 13565 getLLVMStyleWithColumns(25))); 13566 } 13567 13568 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13569 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13570 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13571 EXPECT_EQ("\"test\"\n" 13572 "\"\\n\"", 13573 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13574 EXPECT_EQ("\"tes\\\\\"\n" 13575 "\"n\"", 13576 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13577 EXPECT_EQ("\"\\\\\\\\\"\n" 13578 "\"\\n\"", 13579 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13580 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13581 EXPECT_EQ("\"\\uff01\"\n" 13582 "\"test\"", 13583 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13584 EXPECT_EQ("\"\\Uff01ff02\"", 13585 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13586 EXPECT_EQ("\"\\x000000000001\"\n" 13587 "\"next\"", 13588 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13589 EXPECT_EQ("\"\\x000000000001next\"", 13590 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13591 EXPECT_EQ("\"\\x000000000001\"", 13592 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13593 EXPECT_EQ("\"test\"\n" 13594 "\"\\000000\"\n" 13595 "\"000001\"", 13596 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13597 EXPECT_EQ("\"test\\000\"\n" 13598 "\"00000000\"\n" 13599 "\"1\"", 13600 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13601 } 13602 13603 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13604 verifyFormat("void f() {\n" 13605 " return g() {}\n" 13606 " void h() {}"); 13607 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13608 "g();\n" 13609 "}"); 13610 } 13611 13612 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13613 verifyFormat( 13614 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13615 } 13616 13617 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13618 verifyFormat("class X {\n" 13619 " void f() {\n" 13620 " }\n" 13621 "};", 13622 getLLVMStyleWithColumns(12)); 13623 } 13624 13625 TEST_F(FormatTest, ConfigurableIndentWidth) { 13626 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13627 EightIndent.IndentWidth = 8; 13628 EightIndent.ContinuationIndentWidth = 8; 13629 verifyFormat("void f() {\n" 13630 " someFunction();\n" 13631 " if (true) {\n" 13632 " f();\n" 13633 " }\n" 13634 "}", 13635 EightIndent); 13636 verifyFormat("class X {\n" 13637 " void f() {\n" 13638 " }\n" 13639 "};", 13640 EightIndent); 13641 verifyFormat("int x[] = {\n" 13642 " call(),\n" 13643 " call()};", 13644 EightIndent); 13645 } 13646 13647 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13648 verifyFormat("double\n" 13649 "f();", 13650 getLLVMStyleWithColumns(8)); 13651 } 13652 13653 TEST_F(FormatTest, ConfigurableUseOfTab) { 13654 FormatStyle Tab = getLLVMStyleWithColumns(42); 13655 Tab.IndentWidth = 8; 13656 Tab.UseTab = FormatStyle::UT_Always; 13657 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13658 13659 EXPECT_EQ("if (aaaaaaaa && // q\n" 13660 " bb)\t\t// w\n" 13661 "\t;", 13662 format("if (aaaaaaaa &&// q\n" 13663 "bb)// w\n" 13664 ";", 13665 Tab)); 13666 EXPECT_EQ("if (aaa && bbb) // w\n" 13667 "\t;", 13668 format("if(aaa&&bbb)// w\n" 13669 ";", 13670 Tab)); 13671 13672 verifyFormat("class X {\n" 13673 "\tvoid f() {\n" 13674 "\t\tsomeFunction(parameter1,\n" 13675 "\t\t\t parameter2);\n" 13676 "\t}\n" 13677 "};", 13678 Tab); 13679 verifyFormat("#define A \\\n" 13680 "\tvoid f() { \\\n" 13681 "\t\tsomeFunction( \\\n" 13682 "\t\t parameter1, \\\n" 13683 "\t\t parameter2); \\\n" 13684 "\t}", 13685 Tab); 13686 verifyFormat("int a;\t // x\n" 13687 "int bbbbbbbb; // x\n", 13688 Tab); 13689 13690 Tab.TabWidth = 4; 13691 Tab.IndentWidth = 8; 13692 verifyFormat("class TabWidth4Indent8 {\n" 13693 "\t\tvoid f() {\n" 13694 "\t\t\t\tsomeFunction(parameter1,\n" 13695 "\t\t\t\t\t\t\t parameter2);\n" 13696 "\t\t}\n" 13697 "};", 13698 Tab); 13699 13700 Tab.TabWidth = 4; 13701 Tab.IndentWidth = 4; 13702 verifyFormat("class TabWidth4Indent4 {\n" 13703 "\tvoid f() {\n" 13704 "\t\tsomeFunction(parameter1,\n" 13705 "\t\t\t\t\t parameter2);\n" 13706 "\t}\n" 13707 "};", 13708 Tab); 13709 13710 Tab.TabWidth = 8; 13711 Tab.IndentWidth = 4; 13712 verifyFormat("class TabWidth8Indent4 {\n" 13713 " void f() {\n" 13714 "\tsomeFunction(parameter1,\n" 13715 "\t\t parameter2);\n" 13716 " }\n" 13717 "};", 13718 Tab); 13719 13720 Tab.TabWidth = 8; 13721 Tab.IndentWidth = 8; 13722 EXPECT_EQ("/*\n" 13723 "\t a\t\tcomment\n" 13724 "\t in multiple lines\n" 13725 " */", 13726 format(" /*\t \t \n" 13727 " \t \t a\t\tcomment\t \t\n" 13728 " \t \t in multiple lines\t\n" 13729 " \t */", 13730 Tab)); 13731 13732 Tab.UseTab = FormatStyle::UT_ForIndentation; 13733 verifyFormat("{\n" 13734 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13735 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13736 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13737 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13738 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13739 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13740 "};", 13741 Tab); 13742 verifyFormat("enum AA {\n" 13743 "\ta1, // Force multiple lines\n" 13744 "\ta2,\n" 13745 "\ta3\n" 13746 "};", 13747 Tab); 13748 EXPECT_EQ("if (aaaaaaaa && // q\n" 13749 " bb) // w\n" 13750 "\t;", 13751 format("if (aaaaaaaa &&// q\n" 13752 "bb)// w\n" 13753 ";", 13754 Tab)); 13755 verifyFormat("class X {\n" 13756 "\tvoid f() {\n" 13757 "\t\tsomeFunction(parameter1,\n" 13758 "\t\t parameter2);\n" 13759 "\t}\n" 13760 "};", 13761 Tab); 13762 verifyFormat("{\n" 13763 "\tQ(\n" 13764 "\t {\n" 13765 "\t\t int a;\n" 13766 "\t\t someFunction(aaaaaaaa,\n" 13767 "\t\t bbbbbbb);\n" 13768 "\t },\n" 13769 "\t p);\n" 13770 "}", 13771 Tab); 13772 EXPECT_EQ("{\n" 13773 "\t/* aaaa\n" 13774 "\t bbbb */\n" 13775 "}", 13776 format("{\n" 13777 "/* aaaa\n" 13778 " bbbb */\n" 13779 "}", 13780 Tab)); 13781 EXPECT_EQ("{\n" 13782 "\t/*\n" 13783 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13784 "\t bbbbbbbbbbbbb\n" 13785 "\t*/\n" 13786 "}", 13787 format("{\n" 13788 "/*\n" 13789 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13790 "*/\n" 13791 "}", 13792 Tab)); 13793 EXPECT_EQ("{\n" 13794 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13795 "\t// bbbbbbbbbbbbb\n" 13796 "}", 13797 format("{\n" 13798 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13799 "}", 13800 Tab)); 13801 EXPECT_EQ("{\n" 13802 "\t/*\n" 13803 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13804 "\t bbbbbbbbbbbbb\n" 13805 "\t*/\n" 13806 "}", 13807 format("{\n" 13808 "\t/*\n" 13809 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13810 "\t*/\n" 13811 "}", 13812 Tab)); 13813 EXPECT_EQ("{\n" 13814 "\t/*\n" 13815 "\n" 13816 "\t*/\n" 13817 "}", 13818 format("{\n" 13819 "\t/*\n" 13820 "\n" 13821 "\t*/\n" 13822 "}", 13823 Tab)); 13824 EXPECT_EQ("{\n" 13825 "\t/*\n" 13826 " asdf\n" 13827 "\t*/\n" 13828 "}", 13829 format("{\n" 13830 "\t/*\n" 13831 " asdf\n" 13832 "\t*/\n" 13833 "}", 13834 Tab)); 13835 13836 verifyFormat("void f() {\n" 13837 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 13838 "\t : bbbbbbbbbbbbbbbbbb\n" 13839 "}", 13840 Tab); 13841 FormatStyle TabNoBreak = Tab; 13842 TabNoBreak.BreakBeforeTernaryOperators = false; 13843 verifyFormat("void f() {\n" 13844 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 13845 "\t bbbbbbbbbbbbbbbbbb\n" 13846 "}", 13847 TabNoBreak); 13848 verifyFormat("void f() {\n" 13849 "\treturn true ?\n" 13850 "\t aaaaaaaaaaaaaaaaaaaa :\n" 13851 "\t bbbbbbbbbbbbbbbbbbbb\n" 13852 "}", 13853 TabNoBreak); 13854 13855 Tab.UseTab = FormatStyle::UT_Never; 13856 EXPECT_EQ("/*\n" 13857 " a\t\tcomment\n" 13858 " in multiple lines\n" 13859 " */", 13860 format(" /*\t \t \n" 13861 " \t \t a\t\tcomment\t \t\n" 13862 " \t \t in multiple lines\t\n" 13863 " \t */", 13864 Tab)); 13865 EXPECT_EQ("/* some\n" 13866 " comment */", 13867 format(" \t \t /* some\n" 13868 " \t \t comment */", 13869 Tab)); 13870 EXPECT_EQ("int a; /* some\n" 13871 " comment */", 13872 format(" \t \t int a; /* some\n" 13873 " \t \t comment */", 13874 Tab)); 13875 13876 EXPECT_EQ("int a; /* some\n" 13877 "comment */", 13878 format(" \t \t int\ta; /* some\n" 13879 " \t \t comment */", 13880 Tab)); 13881 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13882 " comment */", 13883 format(" \t \t f(\"\t\t\"); /* some\n" 13884 " \t \t comment */", 13885 Tab)); 13886 EXPECT_EQ("{\n" 13887 " /*\n" 13888 " * Comment\n" 13889 " */\n" 13890 " int i;\n" 13891 "}", 13892 format("{\n" 13893 "\t/*\n" 13894 "\t * Comment\n" 13895 "\t */\n" 13896 "\t int i;\n" 13897 "}", 13898 Tab)); 13899 13900 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13901 Tab.TabWidth = 8; 13902 Tab.IndentWidth = 8; 13903 EXPECT_EQ("if (aaaaaaaa && // q\n" 13904 " bb) // w\n" 13905 "\t;", 13906 format("if (aaaaaaaa &&// q\n" 13907 "bb)// w\n" 13908 ";", 13909 Tab)); 13910 EXPECT_EQ("if (aaa && bbb) // w\n" 13911 "\t;", 13912 format("if(aaa&&bbb)// w\n" 13913 ";", 13914 Tab)); 13915 verifyFormat("class X {\n" 13916 "\tvoid f() {\n" 13917 "\t\tsomeFunction(parameter1,\n" 13918 "\t\t\t parameter2);\n" 13919 "\t}\n" 13920 "};", 13921 Tab); 13922 verifyFormat("#define A \\\n" 13923 "\tvoid f() { \\\n" 13924 "\t\tsomeFunction( \\\n" 13925 "\t\t parameter1, \\\n" 13926 "\t\t parameter2); \\\n" 13927 "\t}", 13928 Tab); 13929 Tab.TabWidth = 4; 13930 Tab.IndentWidth = 8; 13931 verifyFormat("class TabWidth4Indent8 {\n" 13932 "\t\tvoid f() {\n" 13933 "\t\t\t\tsomeFunction(parameter1,\n" 13934 "\t\t\t\t\t\t\t parameter2);\n" 13935 "\t\t}\n" 13936 "};", 13937 Tab); 13938 Tab.TabWidth = 4; 13939 Tab.IndentWidth = 4; 13940 verifyFormat("class TabWidth4Indent4 {\n" 13941 "\tvoid f() {\n" 13942 "\t\tsomeFunction(parameter1,\n" 13943 "\t\t\t\t\t parameter2);\n" 13944 "\t}\n" 13945 "};", 13946 Tab); 13947 Tab.TabWidth = 8; 13948 Tab.IndentWidth = 4; 13949 verifyFormat("class TabWidth8Indent4 {\n" 13950 " void f() {\n" 13951 "\tsomeFunction(parameter1,\n" 13952 "\t\t parameter2);\n" 13953 " }\n" 13954 "};", 13955 Tab); 13956 Tab.TabWidth = 8; 13957 Tab.IndentWidth = 8; 13958 EXPECT_EQ("/*\n" 13959 "\t a\t\tcomment\n" 13960 "\t in multiple lines\n" 13961 " */", 13962 format(" /*\t \t \n" 13963 " \t \t a\t\tcomment\t \t\n" 13964 " \t \t in multiple lines\t\n" 13965 " \t */", 13966 Tab)); 13967 verifyFormat("{\n" 13968 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13969 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13970 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13971 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13972 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13973 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13974 "};", 13975 Tab); 13976 verifyFormat("enum AA {\n" 13977 "\ta1, // Force multiple lines\n" 13978 "\ta2,\n" 13979 "\ta3\n" 13980 "};", 13981 Tab); 13982 EXPECT_EQ("if (aaaaaaaa && // q\n" 13983 " bb) // w\n" 13984 "\t;", 13985 format("if (aaaaaaaa &&// q\n" 13986 "bb)// w\n" 13987 ";", 13988 Tab)); 13989 verifyFormat("class X {\n" 13990 "\tvoid f() {\n" 13991 "\t\tsomeFunction(parameter1,\n" 13992 "\t\t\t parameter2);\n" 13993 "\t}\n" 13994 "};", 13995 Tab); 13996 verifyFormat("{\n" 13997 "\tQ(\n" 13998 "\t {\n" 13999 "\t\t int a;\n" 14000 "\t\t someFunction(aaaaaaaa,\n" 14001 "\t\t\t\t bbbbbbb);\n" 14002 "\t },\n" 14003 "\t p);\n" 14004 "}", 14005 Tab); 14006 EXPECT_EQ("{\n" 14007 "\t/* aaaa\n" 14008 "\t bbbb */\n" 14009 "}", 14010 format("{\n" 14011 "/* aaaa\n" 14012 " bbbb */\n" 14013 "}", 14014 Tab)); 14015 EXPECT_EQ("{\n" 14016 "\t/*\n" 14017 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14018 "\t bbbbbbbbbbbbb\n" 14019 "\t*/\n" 14020 "}", 14021 format("{\n" 14022 "/*\n" 14023 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14024 "*/\n" 14025 "}", 14026 Tab)); 14027 EXPECT_EQ("{\n" 14028 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14029 "\t// bbbbbbbbbbbbb\n" 14030 "}", 14031 format("{\n" 14032 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14033 "}", 14034 Tab)); 14035 EXPECT_EQ("{\n" 14036 "\t/*\n" 14037 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14038 "\t bbbbbbbbbbbbb\n" 14039 "\t*/\n" 14040 "}", 14041 format("{\n" 14042 "\t/*\n" 14043 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14044 "\t*/\n" 14045 "}", 14046 Tab)); 14047 EXPECT_EQ("{\n" 14048 "\t/*\n" 14049 "\n" 14050 "\t*/\n" 14051 "}", 14052 format("{\n" 14053 "\t/*\n" 14054 "\n" 14055 "\t*/\n" 14056 "}", 14057 Tab)); 14058 EXPECT_EQ("{\n" 14059 "\t/*\n" 14060 " asdf\n" 14061 "\t*/\n" 14062 "}", 14063 format("{\n" 14064 "\t/*\n" 14065 " asdf\n" 14066 "\t*/\n" 14067 "}", 14068 Tab)); 14069 EXPECT_EQ("/* some\n" 14070 " comment */", 14071 format(" \t \t /* some\n" 14072 " \t \t comment */", 14073 Tab)); 14074 EXPECT_EQ("int a; /* some\n" 14075 " comment */", 14076 format(" \t \t int a; /* some\n" 14077 " \t \t comment */", 14078 Tab)); 14079 EXPECT_EQ("int a; /* some\n" 14080 "comment */", 14081 format(" \t \t int\ta; /* some\n" 14082 " \t \t comment */", 14083 Tab)); 14084 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14085 " comment */", 14086 format(" \t \t f(\"\t\t\"); /* some\n" 14087 " \t \t comment */", 14088 Tab)); 14089 EXPECT_EQ("{\n" 14090 "\t/*\n" 14091 "\t * Comment\n" 14092 "\t */\n" 14093 "\tint i;\n" 14094 "}", 14095 format("{\n" 14096 "\t/*\n" 14097 "\t * Comment\n" 14098 "\t */\n" 14099 "\t int i;\n" 14100 "}", 14101 Tab)); 14102 Tab.TabWidth = 2; 14103 Tab.IndentWidth = 2; 14104 EXPECT_EQ("{\n" 14105 "\t/* aaaa\n" 14106 "\t\t bbbb */\n" 14107 "}", 14108 format("{\n" 14109 "/* aaaa\n" 14110 "\t bbbb */\n" 14111 "}", 14112 Tab)); 14113 EXPECT_EQ("{\n" 14114 "\t/*\n" 14115 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14116 "\t\tbbbbbbbbbbbbb\n" 14117 "\t*/\n" 14118 "}", 14119 format("{\n" 14120 "/*\n" 14121 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14122 "*/\n" 14123 "}", 14124 Tab)); 14125 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14126 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14127 Tab.TabWidth = 4; 14128 Tab.IndentWidth = 4; 14129 verifyFormat("class Assign {\n" 14130 "\tvoid f() {\n" 14131 "\t\tint x = 123;\n" 14132 "\t\tint random = 4;\n" 14133 "\t\tstd::string alphabet =\n" 14134 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14135 "\t}\n" 14136 "};", 14137 Tab); 14138 14139 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14140 Tab.TabWidth = 8; 14141 Tab.IndentWidth = 8; 14142 EXPECT_EQ("if (aaaaaaaa && // q\n" 14143 " bb) // w\n" 14144 "\t;", 14145 format("if (aaaaaaaa &&// q\n" 14146 "bb)// w\n" 14147 ";", 14148 Tab)); 14149 EXPECT_EQ("if (aaa && bbb) // w\n" 14150 "\t;", 14151 format("if(aaa&&bbb)// w\n" 14152 ";", 14153 Tab)); 14154 verifyFormat("class X {\n" 14155 "\tvoid f() {\n" 14156 "\t\tsomeFunction(parameter1,\n" 14157 "\t\t parameter2);\n" 14158 "\t}\n" 14159 "};", 14160 Tab); 14161 verifyFormat("#define A \\\n" 14162 "\tvoid f() { \\\n" 14163 "\t\tsomeFunction( \\\n" 14164 "\t\t parameter1, \\\n" 14165 "\t\t parameter2); \\\n" 14166 "\t}", 14167 Tab); 14168 Tab.TabWidth = 4; 14169 Tab.IndentWidth = 8; 14170 verifyFormat("class TabWidth4Indent8 {\n" 14171 "\t\tvoid f() {\n" 14172 "\t\t\t\tsomeFunction(parameter1,\n" 14173 "\t\t\t\t parameter2);\n" 14174 "\t\t}\n" 14175 "};", 14176 Tab); 14177 Tab.TabWidth = 4; 14178 Tab.IndentWidth = 4; 14179 verifyFormat("class TabWidth4Indent4 {\n" 14180 "\tvoid f() {\n" 14181 "\t\tsomeFunction(parameter1,\n" 14182 "\t\t parameter2);\n" 14183 "\t}\n" 14184 "};", 14185 Tab); 14186 Tab.TabWidth = 8; 14187 Tab.IndentWidth = 4; 14188 verifyFormat("class TabWidth8Indent4 {\n" 14189 " void f() {\n" 14190 "\tsomeFunction(parameter1,\n" 14191 "\t parameter2);\n" 14192 " }\n" 14193 "};", 14194 Tab); 14195 Tab.TabWidth = 8; 14196 Tab.IndentWidth = 8; 14197 EXPECT_EQ("/*\n" 14198 " a\t\tcomment\n" 14199 " in multiple lines\n" 14200 " */", 14201 format(" /*\t \t \n" 14202 " \t \t a\t\tcomment\t \t\n" 14203 " \t \t in multiple lines\t\n" 14204 " \t */", 14205 Tab)); 14206 verifyFormat("{\n" 14207 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14208 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14209 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14210 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14211 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14212 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14213 "};", 14214 Tab); 14215 verifyFormat("enum AA {\n" 14216 "\ta1, // Force multiple lines\n" 14217 "\ta2,\n" 14218 "\ta3\n" 14219 "};", 14220 Tab); 14221 EXPECT_EQ("if (aaaaaaaa && // q\n" 14222 " bb) // w\n" 14223 "\t;", 14224 format("if (aaaaaaaa &&// q\n" 14225 "bb)// w\n" 14226 ";", 14227 Tab)); 14228 verifyFormat("class X {\n" 14229 "\tvoid f() {\n" 14230 "\t\tsomeFunction(parameter1,\n" 14231 "\t\t parameter2);\n" 14232 "\t}\n" 14233 "};", 14234 Tab); 14235 verifyFormat("{\n" 14236 "\tQ(\n" 14237 "\t {\n" 14238 "\t\t int a;\n" 14239 "\t\t someFunction(aaaaaaaa,\n" 14240 "\t\t bbbbbbb);\n" 14241 "\t },\n" 14242 "\t p);\n" 14243 "}", 14244 Tab); 14245 EXPECT_EQ("{\n" 14246 "\t/* aaaa\n" 14247 "\t bbbb */\n" 14248 "}", 14249 format("{\n" 14250 "/* aaaa\n" 14251 " bbbb */\n" 14252 "}", 14253 Tab)); 14254 EXPECT_EQ("{\n" 14255 "\t/*\n" 14256 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14257 "\t bbbbbbbbbbbbb\n" 14258 "\t*/\n" 14259 "}", 14260 format("{\n" 14261 "/*\n" 14262 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14263 "*/\n" 14264 "}", 14265 Tab)); 14266 EXPECT_EQ("{\n" 14267 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14268 "\t// bbbbbbbbbbbbb\n" 14269 "}", 14270 format("{\n" 14271 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14272 "}", 14273 Tab)); 14274 EXPECT_EQ("{\n" 14275 "\t/*\n" 14276 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14277 "\t bbbbbbbbbbbbb\n" 14278 "\t*/\n" 14279 "}", 14280 format("{\n" 14281 "\t/*\n" 14282 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14283 "\t*/\n" 14284 "}", 14285 Tab)); 14286 EXPECT_EQ("{\n" 14287 "\t/*\n" 14288 "\n" 14289 "\t*/\n" 14290 "}", 14291 format("{\n" 14292 "\t/*\n" 14293 "\n" 14294 "\t*/\n" 14295 "}", 14296 Tab)); 14297 EXPECT_EQ("{\n" 14298 "\t/*\n" 14299 " asdf\n" 14300 "\t*/\n" 14301 "}", 14302 format("{\n" 14303 "\t/*\n" 14304 " asdf\n" 14305 "\t*/\n" 14306 "}", 14307 Tab)); 14308 EXPECT_EQ("/* some\n" 14309 " comment */", 14310 format(" \t \t /* some\n" 14311 " \t \t comment */", 14312 Tab)); 14313 EXPECT_EQ("int a; /* some\n" 14314 " comment */", 14315 format(" \t \t int a; /* some\n" 14316 " \t \t comment */", 14317 Tab)); 14318 EXPECT_EQ("int a; /* some\n" 14319 "comment */", 14320 format(" \t \t int\ta; /* some\n" 14321 " \t \t comment */", 14322 Tab)); 14323 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14324 " comment */", 14325 format(" \t \t f(\"\t\t\"); /* some\n" 14326 " \t \t comment */", 14327 Tab)); 14328 EXPECT_EQ("{\n" 14329 "\t/*\n" 14330 "\t * Comment\n" 14331 "\t */\n" 14332 "\tint i;\n" 14333 "}", 14334 format("{\n" 14335 "\t/*\n" 14336 "\t * Comment\n" 14337 "\t */\n" 14338 "\t int i;\n" 14339 "}", 14340 Tab)); 14341 Tab.TabWidth = 2; 14342 Tab.IndentWidth = 2; 14343 EXPECT_EQ("{\n" 14344 "\t/* aaaa\n" 14345 "\t bbbb */\n" 14346 "}", 14347 format("{\n" 14348 "/* aaaa\n" 14349 " bbbb */\n" 14350 "}", 14351 Tab)); 14352 EXPECT_EQ("{\n" 14353 "\t/*\n" 14354 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14355 "\t bbbbbbbbbbbbb\n" 14356 "\t*/\n" 14357 "}", 14358 format("{\n" 14359 "/*\n" 14360 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14361 "*/\n" 14362 "}", 14363 Tab)); 14364 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14365 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14366 Tab.TabWidth = 4; 14367 Tab.IndentWidth = 4; 14368 verifyFormat("class Assign {\n" 14369 "\tvoid f() {\n" 14370 "\t\tint x = 123;\n" 14371 "\t\tint random = 4;\n" 14372 "\t\tstd::string alphabet =\n" 14373 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14374 "\t}\n" 14375 "};", 14376 Tab); 14377 Tab.AlignOperands = FormatStyle::OAS_Align; 14378 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 14379 " cccccccccccccccccccc;", 14380 Tab); 14381 // no alignment 14382 verifyFormat("int aaaaaaaaaa =\n" 14383 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 14384 Tab); 14385 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 14386 " : bbbbbbbbbbbbbb ? 222222222222222\n" 14387 " : 333333333333333;", 14388 Tab); 14389 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14390 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 14391 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 14392 " + cccccccccccccccccccc;", 14393 Tab); 14394 } 14395 14396 TEST_F(FormatTest, ZeroTabWidth) { 14397 FormatStyle Tab = getLLVMStyleWithColumns(42); 14398 Tab.IndentWidth = 8; 14399 Tab.UseTab = FormatStyle::UT_Never; 14400 Tab.TabWidth = 0; 14401 EXPECT_EQ("void a(){\n" 14402 " // line starts with '\t'\n" 14403 "};", 14404 format("void a(){\n" 14405 "\t// line starts with '\t'\n" 14406 "};", 14407 Tab)); 14408 14409 EXPECT_EQ("void a(){\n" 14410 " // line starts with '\t'\n" 14411 "};", 14412 format("void a(){\n" 14413 "\t\t// line starts with '\t'\n" 14414 "};", 14415 Tab)); 14416 14417 Tab.UseTab = FormatStyle::UT_ForIndentation; 14418 EXPECT_EQ("void a(){\n" 14419 " // line starts with '\t'\n" 14420 "};", 14421 format("void a(){\n" 14422 "\t// line starts with '\t'\n" 14423 "};", 14424 Tab)); 14425 14426 EXPECT_EQ("void a(){\n" 14427 " // line starts with '\t'\n" 14428 "};", 14429 format("void a(){\n" 14430 "\t\t// line starts with '\t'\n" 14431 "};", 14432 Tab)); 14433 14434 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14435 EXPECT_EQ("void a(){\n" 14436 " // line starts with '\t'\n" 14437 "};", 14438 format("void a(){\n" 14439 "\t// line starts with '\t'\n" 14440 "};", 14441 Tab)); 14442 14443 EXPECT_EQ("void a(){\n" 14444 " // line starts with '\t'\n" 14445 "};", 14446 format("void a(){\n" 14447 "\t\t// line starts with '\t'\n" 14448 "};", 14449 Tab)); 14450 14451 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14452 EXPECT_EQ("void a(){\n" 14453 " // line starts with '\t'\n" 14454 "};", 14455 format("void a(){\n" 14456 "\t// line starts with '\t'\n" 14457 "};", 14458 Tab)); 14459 14460 EXPECT_EQ("void a(){\n" 14461 " // line starts with '\t'\n" 14462 "};", 14463 format("void a(){\n" 14464 "\t\t// line starts with '\t'\n" 14465 "};", 14466 Tab)); 14467 14468 Tab.UseTab = FormatStyle::UT_Always; 14469 EXPECT_EQ("void a(){\n" 14470 "// line starts with '\t'\n" 14471 "};", 14472 format("void a(){\n" 14473 "\t// line starts with '\t'\n" 14474 "};", 14475 Tab)); 14476 14477 EXPECT_EQ("void a(){\n" 14478 "// line starts with '\t'\n" 14479 "};", 14480 format("void a(){\n" 14481 "\t\t// line starts with '\t'\n" 14482 "};", 14483 Tab)); 14484 } 14485 14486 TEST_F(FormatTest, CalculatesOriginalColumn) { 14487 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14488 "q\"; /* some\n" 14489 " comment */", 14490 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14491 "q\"; /* some\n" 14492 " comment */", 14493 getLLVMStyle())); 14494 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14495 "/* some\n" 14496 " comment */", 14497 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14498 " /* some\n" 14499 " comment */", 14500 getLLVMStyle())); 14501 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14502 "qqq\n" 14503 "/* some\n" 14504 " comment */", 14505 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14506 "qqq\n" 14507 " /* some\n" 14508 " comment */", 14509 getLLVMStyle())); 14510 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14511 "wwww; /* some\n" 14512 " comment */", 14513 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14514 "wwww; /* some\n" 14515 " comment */", 14516 getLLVMStyle())); 14517 } 14518 14519 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 14520 FormatStyle NoSpace = getLLVMStyle(); 14521 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 14522 14523 verifyFormat("while(true)\n" 14524 " continue;", 14525 NoSpace); 14526 verifyFormat("for(;;)\n" 14527 " continue;", 14528 NoSpace); 14529 verifyFormat("if(true)\n" 14530 " f();\n" 14531 "else if(true)\n" 14532 " f();", 14533 NoSpace); 14534 verifyFormat("do {\n" 14535 " do_something();\n" 14536 "} while(something());", 14537 NoSpace); 14538 verifyFormat("switch(x) {\n" 14539 "default:\n" 14540 " break;\n" 14541 "}", 14542 NoSpace); 14543 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14544 verifyFormat("size_t x = sizeof(x);", NoSpace); 14545 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14546 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14547 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14548 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14549 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14550 verifyFormat("alignas(128) char a[128];", NoSpace); 14551 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14552 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14553 verifyFormat("int f() throw(Deprecated);", NoSpace); 14554 verifyFormat("typedef void (*cb)(int);", NoSpace); 14555 verifyFormat("T A::operator()();", NoSpace); 14556 verifyFormat("X A::operator++(T);", NoSpace); 14557 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14558 14559 FormatStyle Space = getLLVMStyle(); 14560 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14561 14562 verifyFormat("int f ();", Space); 14563 verifyFormat("void f (int a, T b) {\n" 14564 " while (true)\n" 14565 " continue;\n" 14566 "}", 14567 Space); 14568 verifyFormat("if (true)\n" 14569 " f ();\n" 14570 "else if (true)\n" 14571 " f ();", 14572 Space); 14573 verifyFormat("do {\n" 14574 " do_something ();\n" 14575 "} while (something ());", 14576 Space); 14577 verifyFormat("switch (x) {\n" 14578 "default:\n" 14579 " break;\n" 14580 "}", 14581 Space); 14582 verifyFormat("A::A () : a (1) {}", Space); 14583 verifyFormat("void f () __attribute__ ((asdf));", Space); 14584 verifyFormat("*(&a + 1);\n" 14585 "&((&a)[1]);\n" 14586 "a[(b + c) * d];\n" 14587 "(((a + 1) * 2) + 3) * 4;", 14588 Space); 14589 verifyFormat("#define A(x) x", Space); 14590 verifyFormat("#define A (x) x", Space); 14591 verifyFormat("#if defined(x)\n" 14592 "#endif", 14593 Space); 14594 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14595 verifyFormat("size_t x = sizeof (x);", Space); 14596 verifyFormat("auto f (int x) -> decltype (x);", Space); 14597 verifyFormat("auto f (int x) -> typeof (x);", Space); 14598 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14599 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14600 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14601 verifyFormat("alignas (128) char a[128];", Space); 14602 verifyFormat("size_t x = alignof (MyType);", Space); 14603 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14604 verifyFormat("int f () throw (Deprecated);", Space); 14605 verifyFormat("typedef void (*cb) (int);", Space); 14606 // FIXME these tests regressed behaviour. 14607 // verifyFormat("T A::operator() ();", Space); 14608 // verifyFormat("X A::operator++ (T);", Space); 14609 verifyFormat("auto lambda = [] () { return 0; };", Space); 14610 verifyFormat("int x = int (y);", Space); 14611 14612 FormatStyle SomeSpace = getLLVMStyle(); 14613 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14614 14615 verifyFormat("[]() -> float {}", SomeSpace); 14616 verifyFormat("[] (auto foo) {}", SomeSpace); 14617 verifyFormat("[foo]() -> int {}", SomeSpace); 14618 verifyFormat("int f();", SomeSpace); 14619 verifyFormat("void f (int a, T b) {\n" 14620 " while (true)\n" 14621 " continue;\n" 14622 "}", 14623 SomeSpace); 14624 verifyFormat("if (true)\n" 14625 " f();\n" 14626 "else if (true)\n" 14627 " f();", 14628 SomeSpace); 14629 verifyFormat("do {\n" 14630 " do_something();\n" 14631 "} while (something());", 14632 SomeSpace); 14633 verifyFormat("switch (x) {\n" 14634 "default:\n" 14635 " break;\n" 14636 "}", 14637 SomeSpace); 14638 verifyFormat("A::A() : a (1) {}", SomeSpace); 14639 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14640 verifyFormat("*(&a + 1);\n" 14641 "&((&a)[1]);\n" 14642 "a[(b + c) * d];\n" 14643 "(((a + 1) * 2) + 3) * 4;", 14644 SomeSpace); 14645 verifyFormat("#define A(x) x", SomeSpace); 14646 verifyFormat("#define A (x) x", SomeSpace); 14647 verifyFormat("#if defined(x)\n" 14648 "#endif", 14649 SomeSpace); 14650 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14651 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14652 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14653 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14654 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14655 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14656 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14657 verifyFormat("alignas (128) char a[128];", SomeSpace); 14658 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14659 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14660 SomeSpace); 14661 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14662 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14663 verifyFormat("T A::operator()();", SomeSpace); 14664 // FIXME these tests regressed behaviour. 14665 // verifyFormat("X A::operator++ (T);", SomeSpace); 14666 verifyFormat("int x = int (y);", SomeSpace); 14667 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14668 14669 FormatStyle SpaceControlStatements = getLLVMStyle(); 14670 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14671 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 14672 14673 verifyFormat("while (true)\n" 14674 " continue;", 14675 SpaceControlStatements); 14676 verifyFormat("if (true)\n" 14677 " f();\n" 14678 "else if (true)\n" 14679 " f();", 14680 SpaceControlStatements); 14681 verifyFormat("for (;;) {\n" 14682 " do_something();\n" 14683 "}", 14684 SpaceControlStatements); 14685 verifyFormat("do {\n" 14686 " do_something();\n" 14687 "} while (something());", 14688 SpaceControlStatements); 14689 verifyFormat("switch (x) {\n" 14690 "default:\n" 14691 " break;\n" 14692 "}", 14693 SpaceControlStatements); 14694 14695 FormatStyle SpaceFuncDecl = getLLVMStyle(); 14696 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14697 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 14698 14699 verifyFormat("int f ();", SpaceFuncDecl); 14700 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 14701 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 14702 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 14703 verifyFormat("#define A(x) x", SpaceFuncDecl); 14704 verifyFormat("#define A (x) x", SpaceFuncDecl); 14705 verifyFormat("#if defined(x)\n" 14706 "#endif", 14707 SpaceFuncDecl); 14708 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 14709 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 14710 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 14711 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 14712 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 14713 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 14714 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 14715 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 14716 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 14717 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14718 SpaceFuncDecl); 14719 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 14720 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 14721 // FIXME these tests regressed behaviour. 14722 // verifyFormat("T A::operator() ();", SpaceFuncDecl); 14723 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 14724 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 14725 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 14726 verifyFormat("int x = int(y);", SpaceFuncDecl); 14727 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14728 SpaceFuncDecl); 14729 14730 FormatStyle SpaceFuncDef = getLLVMStyle(); 14731 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14732 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 14733 14734 verifyFormat("int f();", SpaceFuncDef); 14735 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 14736 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 14737 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 14738 verifyFormat("#define A(x) x", SpaceFuncDef); 14739 verifyFormat("#define A (x) x", SpaceFuncDef); 14740 verifyFormat("#if defined(x)\n" 14741 "#endif", 14742 SpaceFuncDef); 14743 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 14744 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 14745 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 14746 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 14747 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 14748 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 14749 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 14750 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 14751 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 14752 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14753 SpaceFuncDef); 14754 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 14755 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 14756 verifyFormat("T A::operator()();", SpaceFuncDef); 14757 verifyFormat("X A::operator++(T);", SpaceFuncDef); 14758 // verifyFormat("T A::operator() () {}", SpaceFuncDef); 14759 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 14760 verifyFormat("int x = int(y);", SpaceFuncDef); 14761 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14762 SpaceFuncDef); 14763 14764 FormatStyle SpaceIfMacros = getLLVMStyle(); 14765 SpaceIfMacros.IfMacros.clear(); 14766 SpaceIfMacros.IfMacros.push_back("MYIF"); 14767 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14768 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 14769 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 14770 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 14771 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 14772 14773 FormatStyle SpaceForeachMacros = getLLVMStyle(); 14774 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine, 14775 FormatStyle::SBS_Never); 14776 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false); 14777 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14778 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 14779 verifyFormat("for (;;) {\n" 14780 "}", 14781 SpaceForeachMacros); 14782 verifyFormat("foreach (Item *item, itemlist) {\n" 14783 "}", 14784 SpaceForeachMacros); 14785 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n" 14786 "}", 14787 SpaceForeachMacros); 14788 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n" 14789 "}", 14790 SpaceForeachMacros); 14791 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 14792 14793 FormatStyle SomeSpace2 = getLLVMStyle(); 14794 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14795 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 14796 verifyFormat("[]() -> float {}", SomeSpace2); 14797 verifyFormat("[] (auto foo) {}", SomeSpace2); 14798 verifyFormat("[foo]() -> int {}", SomeSpace2); 14799 verifyFormat("int f();", SomeSpace2); 14800 verifyFormat("void f (int a, T b) {\n" 14801 " while (true)\n" 14802 " continue;\n" 14803 "}", 14804 SomeSpace2); 14805 verifyFormat("if (true)\n" 14806 " f();\n" 14807 "else if (true)\n" 14808 " f();", 14809 SomeSpace2); 14810 verifyFormat("do {\n" 14811 " do_something();\n" 14812 "} while (something());", 14813 SomeSpace2); 14814 verifyFormat("switch (x) {\n" 14815 "default:\n" 14816 " break;\n" 14817 "}", 14818 SomeSpace2); 14819 verifyFormat("A::A() : a (1) {}", SomeSpace2); 14820 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 14821 verifyFormat("*(&a + 1);\n" 14822 "&((&a)[1]);\n" 14823 "a[(b + c) * d];\n" 14824 "(((a + 1) * 2) + 3) * 4;", 14825 SomeSpace2); 14826 verifyFormat("#define A(x) x", SomeSpace2); 14827 verifyFormat("#define A (x) x", SomeSpace2); 14828 verifyFormat("#if defined(x)\n" 14829 "#endif", 14830 SomeSpace2); 14831 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 14832 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 14833 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 14834 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 14835 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 14836 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 14837 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 14838 verifyFormat("alignas (128) char a[128];", SomeSpace2); 14839 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 14840 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14841 SomeSpace2); 14842 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 14843 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 14844 verifyFormat("T A::operator()();", SomeSpace2); 14845 // verifyFormat("X A::operator++ (T);", SomeSpace2); 14846 verifyFormat("int x = int (y);", SomeSpace2); 14847 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 14848 14849 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle(); 14850 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14851 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 14852 .AfterOverloadedOperator = true; 14853 14854 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator); 14855 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator); 14856 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator); 14857 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 14858 14859 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 14860 .AfterOverloadedOperator = false; 14861 14862 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator); 14863 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator); 14864 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator); 14865 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 14866 } 14867 14868 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14869 FormatStyle Spaces = getLLVMStyle(); 14870 Spaces.SpaceAfterLogicalNot = true; 14871 14872 verifyFormat("bool x = ! y", Spaces); 14873 verifyFormat("if (! isFailure())", Spaces); 14874 verifyFormat("if (! (a && b))", Spaces); 14875 verifyFormat("\"Error!\"", Spaces); 14876 verifyFormat("! ! x", Spaces); 14877 } 14878 14879 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14880 FormatStyle Spaces = getLLVMStyle(); 14881 14882 Spaces.SpacesInParentheses = true; 14883 verifyFormat("do_something( ::globalVar );", Spaces); 14884 verifyFormat("call( x, y, z );", Spaces); 14885 verifyFormat("call();", Spaces); 14886 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14887 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14888 Spaces); 14889 verifyFormat("while ( (bool)1 )\n" 14890 " continue;", 14891 Spaces); 14892 verifyFormat("for ( ;; )\n" 14893 " continue;", 14894 Spaces); 14895 verifyFormat("if ( true )\n" 14896 " f();\n" 14897 "else if ( true )\n" 14898 " f();", 14899 Spaces); 14900 verifyFormat("do {\n" 14901 " do_something( (int)i );\n" 14902 "} while ( something() );", 14903 Spaces); 14904 verifyFormat("switch ( x ) {\n" 14905 "default:\n" 14906 " break;\n" 14907 "}", 14908 Spaces); 14909 14910 Spaces.SpacesInParentheses = false; 14911 Spaces.SpacesInCStyleCastParentheses = true; 14912 verifyFormat("Type *A = ( Type * )P;", Spaces); 14913 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14914 verifyFormat("x = ( int32 )y;", Spaces); 14915 verifyFormat("int a = ( int )(2.0f);", Spaces); 14916 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14917 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14918 verifyFormat("#define x (( int )-1)", Spaces); 14919 14920 // Run the first set of tests again with: 14921 Spaces.SpacesInParentheses = false; 14922 Spaces.SpaceInEmptyParentheses = true; 14923 Spaces.SpacesInCStyleCastParentheses = true; 14924 verifyFormat("call(x, y, z);", Spaces); 14925 verifyFormat("call( );", Spaces); 14926 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14927 verifyFormat("while (( bool )1)\n" 14928 " continue;", 14929 Spaces); 14930 verifyFormat("for (;;)\n" 14931 " continue;", 14932 Spaces); 14933 verifyFormat("if (true)\n" 14934 " f( );\n" 14935 "else if (true)\n" 14936 " f( );", 14937 Spaces); 14938 verifyFormat("do {\n" 14939 " do_something(( int )i);\n" 14940 "} while (something( ));", 14941 Spaces); 14942 verifyFormat("switch (x) {\n" 14943 "default:\n" 14944 " break;\n" 14945 "}", 14946 Spaces); 14947 14948 // Run the first set of tests again with: 14949 Spaces.SpaceAfterCStyleCast = true; 14950 verifyFormat("call(x, y, z);", Spaces); 14951 verifyFormat("call( );", Spaces); 14952 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14953 verifyFormat("while (( bool ) 1)\n" 14954 " continue;", 14955 Spaces); 14956 verifyFormat("for (;;)\n" 14957 " continue;", 14958 Spaces); 14959 verifyFormat("if (true)\n" 14960 " f( );\n" 14961 "else if (true)\n" 14962 " f( );", 14963 Spaces); 14964 verifyFormat("do {\n" 14965 " do_something(( int ) i);\n" 14966 "} while (something( ));", 14967 Spaces); 14968 verifyFormat("switch (x) {\n" 14969 "default:\n" 14970 " break;\n" 14971 "}", 14972 Spaces); 14973 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces); 14974 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces); 14975 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces); 14976 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); 14977 verifyFormat("bool *y = ( bool * ) (x);", Spaces); 14978 14979 // Run subset of tests again with: 14980 Spaces.SpacesInCStyleCastParentheses = false; 14981 Spaces.SpaceAfterCStyleCast = true; 14982 verifyFormat("while ((bool) 1)\n" 14983 " continue;", 14984 Spaces); 14985 verifyFormat("do {\n" 14986 " do_something((int) i);\n" 14987 "} while (something( ));", 14988 Spaces); 14989 14990 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14991 verifyFormat("size_t idx = (size_t) a;", Spaces); 14992 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14993 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14994 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14995 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14996 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14997 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces); 14998 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces); 14999 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces); 15000 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); 15001 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); 15002 Spaces.ColumnLimit = 80; 15003 Spaces.IndentWidth = 4; 15004 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 15005 verifyFormat("void foo( ) {\n" 15006 " size_t foo = (*(function))(\n" 15007 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 15008 "BarrrrrrrrrrrrLong,\n" 15009 " FoooooooooLooooong);\n" 15010 "}", 15011 Spaces); 15012 Spaces.SpaceAfterCStyleCast = false; 15013 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 15014 verifyFormat("size_t idx = (size_t)a;", Spaces); 15015 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 15016 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 15017 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 15018 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 15019 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 15020 15021 verifyFormat("void foo( ) {\n" 15022 " size_t foo = (*(function))(\n" 15023 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 15024 "BarrrrrrrrrrrrLong,\n" 15025 " FoooooooooLooooong);\n" 15026 "}", 15027 Spaces); 15028 } 15029 15030 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 15031 verifyFormat("int a[5];"); 15032 verifyFormat("a[3] += 42;"); 15033 15034 FormatStyle Spaces = getLLVMStyle(); 15035 Spaces.SpacesInSquareBrackets = true; 15036 // Not lambdas. 15037 verifyFormat("int a[ 5 ];", Spaces); 15038 verifyFormat("a[ 3 ] += 42;", Spaces); 15039 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 15040 verifyFormat("double &operator[](int i) { return 0; }\n" 15041 "int i;", 15042 Spaces); 15043 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 15044 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 15045 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 15046 // Lambdas. 15047 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 15048 verifyFormat("return [ i, args... ] {};", Spaces); 15049 verifyFormat("int foo = [ &bar ]() {};", Spaces); 15050 verifyFormat("int foo = [ = ]() {};", Spaces); 15051 verifyFormat("int foo = [ & ]() {};", Spaces); 15052 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 15053 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 15054 } 15055 15056 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 15057 FormatStyle NoSpaceStyle = getLLVMStyle(); 15058 verifyFormat("int a[5];", NoSpaceStyle); 15059 verifyFormat("a[3] += 42;", NoSpaceStyle); 15060 15061 verifyFormat("int a[1];", NoSpaceStyle); 15062 verifyFormat("int 1 [a];", NoSpaceStyle); 15063 verifyFormat("int a[1][2];", NoSpaceStyle); 15064 verifyFormat("a[7] = 5;", NoSpaceStyle); 15065 verifyFormat("int a = (f())[23];", NoSpaceStyle); 15066 verifyFormat("f([] {})", NoSpaceStyle); 15067 15068 FormatStyle Space = getLLVMStyle(); 15069 Space.SpaceBeforeSquareBrackets = true; 15070 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 15071 verifyFormat("return [i, args...] {};", Space); 15072 15073 verifyFormat("int a [5];", Space); 15074 verifyFormat("a [3] += 42;", Space); 15075 verifyFormat("constexpr char hello []{\"hello\"};", Space); 15076 verifyFormat("double &operator[](int i) { return 0; }\n" 15077 "int i;", 15078 Space); 15079 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 15080 verifyFormat("int i = a [a][a]->f();", Space); 15081 verifyFormat("int i = (*b) [a]->f();", Space); 15082 15083 verifyFormat("int a [1];", Space); 15084 verifyFormat("int 1 [a];", Space); 15085 verifyFormat("int a [1][2];", Space); 15086 verifyFormat("a [7] = 5;", Space); 15087 verifyFormat("int a = (f()) [23];", Space); 15088 verifyFormat("f([] {})", Space); 15089 } 15090 15091 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 15092 verifyFormat("int a = 5;"); 15093 verifyFormat("a += 42;"); 15094 verifyFormat("a or_eq 8;"); 15095 15096 FormatStyle Spaces = getLLVMStyle(); 15097 Spaces.SpaceBeforeAssignmentOperators = false; 15098 verifyFormat("int a= 5;", Spaces); 15099 verifyFormat("a+= 42;", Spaces); 15100 verifyFormat("a or_eq 8;", Spaces); 15101 } 15102 15103 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 15104 verifyFormat("class Foo : public Bar {};"); 15105 verifyFormat("Foo::Foo() : foo(1) {}"); 15106 verifyFormat("for (auto a : b) {\n}"); 15107 verifyFormat("int x = a ? b : c;"); 15108 verifyFormat("{\n" 15109 "label0:\n" 15110 " int x = 0;\n" 15111 "}"); 15112 verifyFormat("switch (x) {\n" 15113 "case 1:\n" 15114 "default:\n" 15115 "}"); 15116 verifyFormat("switch (allBraces) {\n" 15117 "case 1: {\n" 15118 " break;\n" 15119 "}\n" 15120 "case 2: {\n" 15121 " [[fallthrough]];\n" 15122 "}\n" 15123 "default: {\n" 15124 " break;\n" 15125 "}\n" 15126 "}"); 15127 15128 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 15129 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 15130 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 15131 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 15132 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 15133 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 15134 verifyFormat("{\n" 15135 "label1:\n" 15136 " int x = 0;\n" 15137 "}", 15138 CtorInitializerStyle); 15139 verifyFormat("switch (x) {\n" 15140 "case 1:\n" 15141 "default:\n" 15142 "}", 15143 CtorInitializerStyle); 15144 verifyFormat("switch (allBraces) {\n" 15145 "case 1: {\n" 15146 " break;\n" 15147 "}\n" 15148 "case 2: {\n" 15149 " [[fallthrough]];\n" 15150 "}\n" 15151 "default: {\n" 15152 " break;\n" 15153 "}\n" 15154 "}", 15155 CtorInitializerStyle); 15156 CtorInitializerStyle.BreakConstructorInitializers = 15157 FormatStyle::BCIS_AfterColon; 15158 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 15159 " aaaaaaaaaaaaaaaa(1),\n" 15160 " bbbbbbbbbbbbbbbb(2) {}", 15161 CtorInitializerStyle); 15162 CtorInitializerStyle.BreakConstructorInitializers = 15163 FormatStyle::BCIS_BeforeComma; 15164 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15165 " : aaaaaaaaaaaaaaaa(1)\n" 15166 " , bbbbbbbbbbbbbbbb(2) {}", 15167 CtorInitializerStyle); 15168 CtorInitializerStyle.BreakConstructorInitializers = 15169 FormatStyle::BCIS_BeforeColon; 15170 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15171 " : aaaaaaaaaaaaaaaa(1),\n" 15172 " bbbbbbbbbbbbbbbb(2) {}", 15173 CtorInitializerStyle); 15174 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 15175 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15176 ": aaaaaaaaaaaaaaaa(1),\n" 15177 " bbbbbbbbbbbbbbbb(2) {}", 15178 CtorInitializerStyle); 15179 15180 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 15181 InheritanceStyle.SpaceBeforeInheritanceColon = false; 15182 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 15183 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 15184 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 15185 verifyFormat("int x = a ? b : c;", InheritanceStyle); 15186 verifyFormat("{\n" 15187 "label2:\n" 15188 " int x = 0;\n" 15189 "}", 15190 InheritanceStyle); 15191 verifyFormat("switch (x) {\n" 15192 "case 1:\n" 15193 "default:\n" 15194 "}", 15195 InheritanceStyle); 15196 verifyFormat("switch (allBraces) {\n" 15197 "case 1: {\n" 15198 " break;\n" 15199 "}\n" 15200 "case 2: {\n" 15201 " [[fallthrough]];\n" 15202 "}\n" 15203 "default: {\n" 15204 " break;\n" 15205 "}\n" 15206 "}", 15207 InheritanceStyle); 15208 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 15209 verifyFormat("class Foooooooooooooooooooooo\n" 15210 " : public aaaaaaaaaaaaaaaaaa,\n" 15211 " public bbbbbbbbbbbbbbbbbb {\n" 15212 "}", 15213 InheritanceStyle); 15214 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 15215 verifyFormat("class Foooooooooooooooooooooo:\n" 15216 " public aaaaaaaaaaaaaaaaaa,\n" 15217 " public bbbbbbbbbbbbbbbbbb {\n" 15218 "}", 15219 InheritanceStyle); 15220 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 15221 verifyFormat("class Foooooooooooooooooooooo\n" 15222 " : public aaaaaaaaaaaaaaaaaa\n" 15223 " , public bbbbbbbbbbbbbbbbbb {\n" 15224 "}", 15225 InheritanceStyle); 15226 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 15227 verifyFormat("class Foooooooooooooooooooooo\n" 15228 " : public aaaaaaaaaaaaaaaaaa,\n" 15229 " public bbbbbbbbbbbbbbbbbb {\n" 15230 "}", 15231 InheritanceStyle); 15232 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 15233 verifyFormat("class Foooooooooooooooooooooo\n" 15234 ": public aaaaaaaaaaaaaaaaaa,\n" 15235 " public bbbbbbbbbbbbbbbbbb {}", 15236 InheritanceStyle); 15237 15238 FormatStyle ForLoopStyle = getLLVMStyle(); 15239 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 15240 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 15241 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 15242 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 15243 verifyFormat("int x = a ? b : c;", ForLoopStyle); 15244 verifyFormat("{\n" 15245 "label2:\n" 15246 " int x = 0;\n" 15247 "}", 15248 ForLoopStyle); 15249 verifyFormat("switch (x) {\n" 15250 "case 1:\n" 15251 "default:\n" 15252 "}", 15253 ForLoopStyle); 15254 verifyFormat("switch (allBraces) {\n" 15255 "case 1: {\n" 15256 " break;\n" 15257 "}\n" 15258 "case 2: {\n" 15259 " [[fallthrough]];\n" 15260 "}\n" 15261 "default: {\n" 15262 " break;\n" 15263 "}\n" 15264 "}", 15265 ForLoopStyle); 15266 15267 FormatStyle CaseStyle = getLLVMStyle(); 15268 CaseStyle.SpaceBeforeCaseColon = true; 15269 verifyFormat("class Foo : public Bar {};", CaseStyle); 15270 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 15271 verifyFormat("for (auto a : b) {\n}", CaseStyle); 15272 verifyFormat("int x = a ? b : c;", CaseStyle); 15273 verifyFormat("switch (x) {\n" 15274 "case 1 :\n" 15275 "default :\n" 15276 "}", 15277 CaseStyle); 15278 verifyFormat("switch (allBraces) {\n" 15279 "case 1 : {\n" 15280 " break;\n" 15281 "}\n" 15282 "case 2 : {\n" 15283 " [[fallthrough]];\n" 15284 "}\n" 15285 "default : {\n" 15286 " break;\n" 15287 "}\n" 15288 "}", 15289 CaseStyle); 15290 15291 FormatStyle NoSpaceStyle = getLLVMStyle(); 15292 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 15293 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15294 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 15295 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15296 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 15297 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 15298 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 15299 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 15300 verifyFormat("{\n" 15301 "label3:\n" 15302 " int x = 0;\n" 15303 "}", 15304 NoSpaceStyle); 15305 verifyFormat("switch (x) {\n" 15306 "case 1:\n" 15307 "default:\n" 15308 "}", 15309 NoSpaceStyle); 15310 verifyFormat("switch (allBraces) {\n" 15311 "case 1: {\n" 15312 " break;\n" 15313 "}\n" 15314 "case 2: {\n" 15315 " [[fallthrough]];\n" 15316 "}\n" 15317 "default: {\n" 15318 " break;\n" 15319 "}\n" 15320 "}", 15321 NoSpaceStyle); 15322 15323 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 15324 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 15325 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15326 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 15327 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15328 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 15329 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 15330 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 15331 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 15332 verifyFormat("{\n" 15333 "label3:\n" 15334 " int x = 0;\n" 15335 "}", 15336 InvertedSpaceStyle); 15337 verifyFormat("switch (x) {\n" 15338 "case 1 :\n" 15339 "case 2 : {\n" 15340 " break;\n" 15341 "}\n" 15342 "default :\n" 15343 " break;\n" 15344 "}", 15345 InvertedSpaceStyle); 15346 verifyFormat("switch (allBraces) {\n" 15347 "case 1 : {\n" 15348 " break;\n" 15349 "}\n" 15350 "case 2 : {\n" 15351 " [[fallthrough]];\n" 15352 "}\n" 15353 "default : {\n" 15354 " break;\n" 15355 "}\n" 15356 "}", 15357 InvertedSpaceStyle); 15358 } 15359 15360 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 15361 FormatStyle Style = getLLVMStyle(); 15362 15363 Style.PointerAlignment = FormatStyle::PAS_Left; 15364 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15365 verifyFormat("void* const* x = NULL;", Style); 15366 15367 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 15368 do { \ 15369 Style.PointerAlignment = FormatStyle::Pointers; \ 15370 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 15371 verifyFormat(Code, Style); \ 15372 } while (false) 15373 15374 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 15375 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 15376 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 15377 15378 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 15379 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 15380 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 15381 15382 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 15383 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 15384 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 15385 15386 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 15387 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 15388 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 15389 15390 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 15391 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15392 SAPQ_Default); 15393 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15394 SAPQ_Default); 15395 15396 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 15397 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15398 SAPQ_Before); 15399 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15400 SAPQ_Before); 15401 15402 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 15403 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 15404 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15405 SAPQ_After); 15406 15407 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 15408 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 15409 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 15410 15411 #undef verifyQualifierSpaces 15412 15413 FormatStyle Spaces = getLLVMStyle(); 15414 Spaces.AttributeMacros.push_back("qualified"); 15415 Spaces.PointerAlignment = FormatStyle::PAS_Right; 15416 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15417 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 15418 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 15419 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 15420 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 15421 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15422 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15423 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 15424 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 15425 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15426 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15427 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15428 15429 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 15430 Spaces.PointerAlignment = FormatStyle::PAS_Left; 15431 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15432 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 15433 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 15434 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 15435 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 15436 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15437 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 15438 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15439 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 15440 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 15441 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 15442 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 15443 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15444 15445 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 15446 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 15447 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15448 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 15449 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 15450 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15451 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15452 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15453 } 15454 15455 TEST_F(FormatTest, AlignConsecutiveMacros) { 15456 FormatStyle Style = getLLVMStyle(); 15457 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15458 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15459 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15460 15461 verifyFormat("#define a 3\n" 15462 "#define bbbb 4\n" 15463 "#define ccc (5)", 15464 Style); 15465 15466 verifyFormat("#define f(x) (x * x)\n" 15467 "#define fff(x, y, z) (x * y + z)\n" 15468 "#define ffff(x, y) (x - y)", 15469 Style); 15470 15471 verifyFormat("#define foo(x, y) (x + y)\n" 15472 "#define bar (5, 6)(2 + 2)", 15473 Style); 15474 15475 verifyFormat("#define a 3\n" 15476 "#define bbbb 4\n" 15477 "#define ccc (5)\n" 15478 "#define f(x) (x * x)\n" 15479 "#define fff(x, y, z) (x * y + z)\n" 15480 "#define ffff(x, y) (x - y)", 15481 Style); 15482 15483 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15484 verifyFormat("#define a 3\n" 15485 "#define bbbb 4\n" 15486 "#define ccc (5)", 15487 Style); 15488 15489 verifyFormat("#define f(x) (x * x)\n" 15490 "#define fff(x, y, z) (x * y + z)\n" 15491 "#define ffff(x, y) (x - y)", 15492 Style); 15493 15494 verifyFormat("#define foo(x, y) (x + y)\n" 15495 "#define bar (5, 6)(2 + 2)", 15496 Style); 15497 15498 verifyFormat("#define a 3\n" 15499 "#define bbbb 4\n" 15500 "#define ccc (5)\n" 15501 "#define f(x) (x * x)\n" 15502 "#define fff(x, y, z) (x * y + z)\n" 15503 "#define ffff(x, y) (x - y)", 15504 Style); 15505 15506 verifyFormat("#define a 5\n" 15507 "#define foo(x, y) (x + y)\n" 15508 "#define CCC (6)\n" 15509 "auto lambda = []() {\n" 15510 " auto ii = 0;\n" 15511 " float j = 0;\n" 15512 " return 0;\n" 15513 "};\n" 15514 "int i = 0;\n" 15515 "float i2 = 0;\n" 15516 "auto v = type{\n" 15517 " i = 1, //\n" 15518 " (i = 2), //\n" 15519 " i = 3 //\n" 15520 "};", 15521 Style); 15522 15523 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15524 Style.ColumnLimit = 20; 15525 15526 verifyFormat("#define a \\\n" 15527 " \"aabbbbbbbbbbbb\"\n" 15528 "#define D \\\n" 15529 " \"aabbbbbbbbbbbb\" \\\n" 15530 " \"ccddeeeeeeeee\"\n" 15531 "#define B \\\n" 15532 " \"QQQQQQQQQQQQQ\" \\\n" 15533 " \"FFFFFFFFFFFFF\" \\\n" 15534 " \"LLLLLLLL\"\n", 15535 Style); 15536 15537 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15538 verifyFormat("#define a \\\n" 15539 " \"aabbbbbbbbbbbb\"\n" 15540 "#define D \\\n" 15541 " \"aabbbbbbbbbbbb\" \\\n" 15542 " \"ccddeeeeeeeee\"\n" 15543 "#define B \\\n" 15544 " \"QQQQQQQQQQQQQ\" \\\n" 15545 " \"FFFFFFFFFFFFF\" \\\n" 15546 " \"LLLLLLLL\"\n", 15547 Style); 15548 15549 // Test across comments 15550 Style.MaxEmptyLinesToKeep = 10; 15551 Style.ReflowComments = false; 15552 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 15553 EXPECT_EQ("#define a 3\n" 15554 "// line comment\n" 15555 "#define bbbb 4\n" 15556 "#define ccc (5)", 15557 format("#define a 3\n" 15558 "// line comment\n" 15559 "#define bbbb 4\n" 15560 "#define ccc (5)", 15561 Style)); 15562 15563 EXPECT_EQ("#define a 3\n" 15564 "/* block comment */\n" 15565 "#define bbbb 4\n" 15566 "#define ccc (5)", 15567 format("#define a 3\n" 15568 "/* block comment */\n" 15569 "#define bbbb 4\n" 15570 "#define ccc (5)", 15571 Style)); 15572 15573 EXPECT_EQ("#define a 3\n" 15574 "/* multi-line *\n" 15575 " * block comment */\n" 15576 "#define bbbb 4\n" 15577 "#define ccc (5)", 15578 format("#define a 3\n" 15579 "/* multi-line *\n" 15580 " * block comment */\n" 15581 "#define bbbb 4\n" 15582 "#define ccc (5)", 15583 Style)); 15584 15585 EXPECT_EQ("#define a 3\n" 15586 "// multi-line line comment\n" 15587 "//\n" 15588 "#define bbbb 4\n" 15589 "#define ccc (5)", 15590 format("#define a 3\n" 15591 "// multi-line line comment\n" 15592 "//\n" 15593 "#define bbbb 4\n" 15594 "#define ccc (5)", 15595 Style)); 15596 15597 EXPECT_EQ("#define a 3\n" 15598 "// empty lines still break.\n" 15599 "\n" 15600 "#define bbbb 4\n" 15601 "#define ccc (5)", 15602 format("#define a 3\n" 15603 "// empty lines still break.\n" 15604 "\n" 15605 "#define bbbb 4\n" 15606 "#define ccc (5)", 15607 Style)); 15608 15609 // Test across empty lines 15610 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 15611 EXPECT_EQ("#define a 3\n" 15612 "\n" 15613 "#define bbbb 4\n" 15614 "#define ccc (5)", 15615 format("#define a 3\n" 15616 "\n" 15617 "#define bbbb 4\n" 15618 "#define ccc (5)", 15619 Style)); 15620 15621 EXPECT_EQ("#define a 3\n" 15622 "\n" 15623 "\n" 15624 "\n" 15625 "#define bbbb 4\n" 15626 "#define ccc (5)", 15627 format("#define a 3\n" 15628 "\n" 15629 "\n" 15630 "\n" 15631 "#define bbbb 4\n" 15632 "#define ccc (5)", 15633 Style)); 15634 15635 EXPECT_EQ("#define a 3\n" 15636 "// comments should break alignment\n" 15637 "//\n" 15638 "#define bbbb 4\n" 15639 "#define ccc (5)", 15640 format("#define a 3\n" 15641 "// comments should break alignment\n" 15642 "//\n" 15643 "#define bbbb 4\n" 15644 "#define ccc (5)", 15645 Style)); 15646 15647 // Test across empty lines and comments 15648 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 15649 verifyFormat("#define a 3\n" 15650 "\n" 15651 "// line comment\n" 15652 "#define bbbb 4\n" 15653 "#define ccc (5)", 15654 Style); 15655 15656 EXPECT_EQ("#define a 3\n" 15657 "\n" 15658 "\n" 15659 "/* multi-line *\n" 15660 " * block comment */\n" 15661 "\n" 15662 "\n" 15663 "#define bbbb 4\n" 15664 "#define ccc (5)", 15665 format("#define a 3\n" 15666 "\n" 15667 "\n" 15668 "/* multi-line *\n" 15669 " * block comment */\n" 15670 "\n" 15671 "\n" 15672 "#define bbbb 4\n" 15673 "#define ccc (5)", 15674 Style)); 15675 15676 EXPECT_EQ("#define a 3\n" 15677 "\n" 15678 "\n" 15679 "/* multi-line *\n" 15680 " * block comment */\n" 15681 "\n" 15682 "\n" 15683 "#define bbbb 4\n" 15684 "#define ccc (5)", 15685 format("#define a 3\n" 15686 "\n" 15687 "\n" 15688 "/* multi-line *\n" 15689 " * block comment */\n" 15690 "\n" 15691 "\n" 15692 "#define bbbb 4\n" 15693 "#define ccc (5)", 15694 Style)); 15695 } 15696 15697 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 15698 FormatStyle Alignment = getLLVMStyle(); 15699 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15700 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 15701 15702 Alignment.MaxEmptyLinesToKeep = 10; 15703 /* Test alignment across empty lines */ 15704 EXPECT_EQ("int a = 5;\n" 15705 "\n" 15706 "int oneTwoThree = 123;", 15707 format("int a = 5;\n" 15708 "\n" 15709 "int oneTwoThree= 123;", 15710 Alignment)); 15711 EXPECT_EQ("int a = 5;\n" 15712 "int one = 1;\n" 15713 "\n" 15714 "int oneTwoThree = 123;", 15715 format("int a = 5;\n" 15716 "int one = 1;\n" 15717 "\n" 15718 "int oneTwoThree = 123;", 15719 Alignment)); 15720 EXPECT_EQ("int a = 5;\n" 15721 "int one = 1;\n" 15722 "\n" 15723 "int oneTwoThree = 123;\n" 15724 "int oneTwo = 12;", 15725 format("int a = 5;\n" 15726 "int one = 1;\n" 15727 "\n" 15728 "int oneTwoThree = 123;\n" 15729 "int oneTwo = 12;", 15730 Alignment)); 15731 15732 /* Test across comments */ 15733 EXPECT_EQ("int a = 5;\n" 15734 "/* block comment */\n" 15735 "int oneTwoThree = 123;", 15736 format("int a = 5;\n" 15737 "/* block comment */\n" 15738 "int oneTwoThree=123;", 15739 Alignment)); 15740 15741 EXPECT_EQ("int a = 5;\n" 15742 "// line comment\n" 15743 "int oneTwoThree = 123;", 15744 format("int a = 5;\n" 15745 "// line comment\n" 15746 "int oneTwoThree=123;", 15747 Alignment)); 15748 15749 /* Test across comments and newlines */ 15750 EXPECT_EQ("int a = 5;\n" 15751 "\n" 15752 "/* block comment */\n" 15753 "int oneTwoThree = 123;", 15754 format("int a = 5;\n" 15755 "\n" 15756 "/* block comment */\n" 15757 "int oneTwoThree=123;", 15758 Alignment)); 15759 15760 EXPECT_EQ("int a = 5;\n" 15761 "\n" 15762 "// line comment\n" 15763 "int oneTwoThree = 123;", 15764 format("int a = 5;\n" 15765 "\n" 15766 "// line comment\n" 15767 "int oneTwoThree=123;", 15768 Alignment)); 15769 } 15770 15771 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15772 FormatStyle Alignment = getLLVMStyle(); 15773 Alignment.AlignConsecutiveDeclarations = 15774 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15775 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15776 15777 Alignment.MaxEmptyLinesToKeep = 10; 15778 /* Test alignment across empty lines */ 15779 EXPECT_EQ("int a = 5;\n" 15780 "\n" 15781 "float const oneTwoThree = 123;", 15782 format("int a = 5;\n" 15783 "\n" 15784 "float const oneTwoThree = 123;", 15785 Alignment)); 15786 EXPECT_EQ("int a = 5;\n" 15787 "float const one = 1;\n" 15788 "\n" 15789 "int oneTwoThree = 123;", 15790 format("int a = 5;\n" 15791 "float const one = 1;\n" 15792 "\n" 15793 "int oneTwoThree = 123;", 15794 Alignment)); 15795 15796 /* Test across comments */ 15797 EXPECT_EQ("float const a = 5;\n" 15798 "/* block comment */\n" 15799 "int oneTwoThree = 123;", 15800 format("float const a = 5;\n" 15801 "/* block comment */\n" 15802 "int oneTwoThree=123;", 15803 Alignment)); 15804 15805 EXPECT_EQ("float const a = 5;\n" 15806 "// line comment\n" 15807 "int oneTwoThree = 123;", 15808 format("float const a = 5;\n" 15809 "// line comment\n" 15810 "int oneTwoThree=123;", 15811 Alignment)); 15812 15813 /* Test across comments and newlines */ 15814 EXPECT_EQ("float const a = 5;\n" 15815 "\n" 15816 "/* block comment */\n" 15817 "int oneTwoThree = 123;", 15818 format("float const a = 5;\n" 15819 "\n" 15820 "/* block comment */\n" 15821 "int oneTwoThree=123;", 15822 Alignment)); 15823 15824 EXPECT_EQ("float const a = 5;\n" 15825 "\n" 15826 "// line comment\n" 15827 "int oneTwoThree = 123;", 15828 format("float const a = 5;\n" 15829 "\n" 15830 "// line comment\n" 15831 "int oneTwoThree=123;", 15832 Alignment)); 15833 } 15834 15835 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15836 FormatStyle Alignment = getLLVMStyle(); 15837 Alignment.AlignConsecutiveBitFields = 15838 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15839 15840 Alignment.MaxEmptyLinesToKeep = 10; 15841 /* Test alignment across empty lines */ 15842 EXPECT_EQ("int a : 5;\n" 15843 "\n" 15844 "int longbitfield : 6;", 15845 format("int a : 5;\n" 15846 "\n" 15847 "int longbitfield : 6;", 15848 Alignment)); 15849 EXPECT_EQ("int a : 5;\n" 15850 "int one : 1;\n" 15851 "\n" 15852 "int longbitfield : 6;", 15853 format("int a : 5;\n" 15854 "int one : 1;\n" 15855 "\n" 15856 "int longbitfield : 6;", 15857 Alignment)); 15858 15859 /* Test across comments */ 15860 EXPECT_EQ("int a : 5;\n" 15861 "/* block comment */\n" 15862 "int longbitfield : 6;", 15863 format("int a : 5;\n" 15864 "/* block comment */\n" 15865 "int longbitfield : 6;", 15866 Alignment)); 15867 EXPECT_EQ("int a : 5;\n" 15868 "int one : 1;\n" 15869 "// line comment\n" 15870 "int longbitfield : 6;", 15871 format("int a : 5;\n" 15872 "int one : 1;\n" 15873 "// line comment\n" 15874 "int longbitfield : 6;", 15875 Alignment)); 15876 15877 /* Test across comments and newlines */ 15878 EXPECT_EQ("int a : 5;\n" 15879 "/* block comment */\n" 15880 "\n" 15881 "int longbitfield : 6;", 15882 format("int a : 5;\n" 15883 "/* block comment */\n" 15884 "\n" 15885 "int longbitfield : 6;", 15886 Alignment)); 15887 EXPECT_EQ("int a : 5;\n" 15888 "int one : 1;\n" 15889 "\n" 15890 "// line comment\n" 15891 "\n" 15892 "int longbitfield : 6;", 15893 format("int a : 5;\n" 15894 "int one : 1;\n" 15895 "\n" 15896 "// line comment \n" 15897 "\n" 15898 "int longbitfield : 6;", 15899 Alignment)); 15900 } 15901 15902 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15903 FormatStyle Alignment = getLLVMStyle(); 15904 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15905 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15906 15907 Alignment.MaxEmptyLinesToKeep = 10; 15908 /* Test alignment across empty lines */ 15909 EXPECT_EQ("int a = 5;\n" 15910 "\n" 15911 "int oneTwoThree = 123;", 15912 format("int a = 5;\n" 15913 "\n" 15914 "int oneTwoThree= 123;", 15915 Alignment)); 15916 EXPECT_EQ("int a = 5;\n" 15917 "int one = 1;\n" 15918 "\n" 15919 "int oneTwoThree = 123;", 15920 format("int a = 5;\n" 15921 "int one = 1;\n" 15922 "\n" 15923 "int oneTwoThree = 123;", 15924 Alignment)); 15925 15926 /* Test across comments */ 15927 EXPECT_EQ("int a = 5;\n" 15928 "/* block comment */\n" 15929 "int oneTwoThree = 123;", 15930 format("int a = 5;\n" 15931 "/* block comment */\n" 15932 "int oneTwoThree=123;", 15933 Alignment)); 15934 15935 EXPECT_EQ("int a = 5;\n" 15936 "// line comment\n" 15937 "int oneTwoThree = 123;", 15938 format("int a = 5;\n" 15939 "// line comment\n" 15940 "int oneTwoThree=123;", 15941 Alignment)); 15942 15943 EXPECT_EQ("int a = 5;\n" 15944 "/*\n" 15945 " * multi-line block comment\n" 15946 " */\n" 15947 "int oneTwoThree = 123;", 15948 format("int a = 5;\n" 15949 "/*\n" 15950 " * multi-line block comment\n" 15951 " */\n" 15952 "int oneTwoThree=123;", 15953 Alignment)); 15954 15955 EXPECT_EQ("int a = 5;\n" 15956 "//\n" 15957 "// multi-line line comment\n" 15958 "//\n" 15959 "int oneTwoThree = 123;", 15960 format("int a = 5;\n" 15961 "//\n" 15962 "// multi-line line comment\n" 15963 "//\n" 15964 "int oneTwoThree=123;", 15965 Alignment)); 15966 15967 /* Test across comments and newlines */ 15968 EXPECT_EQ("int a = 5;\n" 15969 "\n" 15970 "/* block comment */\n" 15971 "int oneTwoThree = 123;", 15972 format("int a = 5;\n" 15973 "\n" 15974 "/* block comment */\n" 15975 "int oneTwoThree=123;", 15976 Alignment)); 15977 15978 EXPECT_EQ("int a = 5;\n" 15979 "\n" 15980 "// line comment\n" 15981 "int oneTwoThree = 123;", 15982 format("int a = 5;\n" 15983 "\n" 15984 "// line comment\n" 15985 "int oneTwoThree=123;", 15986 Alignment)); 15987 } 15988 15989 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15990 FormatStyle Alignment = getLLVMStyle(); 15991 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15992 Alignment.AlignConsecutiveAssignments = 15993 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15994 verifyFormat("int a = 5;\n" 15995 "int oneTwoThree = 123;", 15996 Alignment); 15997 verifyFormat("int a = method();\n" 15998 "int oneTwoThree = 133;", 15999 Alignment); 16000 verifyFormat("a &= 5;\n" 16001 "bcd *= 5;\n" 16002 "ghtyf += 5;\n" 16003 "dvfvdb -= 5;\n" 16004 "a /= 5;\n" 16005 "vdsvsv %= 5;\n" 16006 "sfdbddfbdfbb ^= 5;\n" 16007 "dvsdsv |= 5;\n" 16008 "int dsvvdvsdvvv = 123;", 16009 Alignment); 16010 verifyFormat("int i = 1, j = 10;\n" 16011 "something = 2000;", 16012 Alignment); 16013 verifyFormat("something = 2000;\n" 16014 "int i = 1, j = 10;\n", 16015 Alignment); 16016 verifyFormat("something = 2000;\n" 16017 "another = 911;\n" 16018 "int i = 1, j = 10;\n" 16019 "oneMore = 1;\n" 16020 "i = 2;", 16021 Alignment); 16022 verifyFormat("int a = 5;\n" 16023 "int one = 1;\n" 16024 "method();\n" 16025 "int oneTwoThree = 123;\n" 16026 "int oneTwo = 12;", 16027 Alignment); 16028 verifyFormat("int oneTwoThree = 123;\n" 16029 "int oneTwo = 12;\n" 16030 "method();\n", 16031 Alignment); 16032 verifyFormat("int oneTwoThree = 123; // comment\n" 16033 "int oneTwo = 12; // comment", 16034 Alignment); 16035 16036 // Bug 25167 16037 /* Uncomment when fixed 16038 verifyFormat("#if A\n" 16039 "#else\n" 16040 "int aaaaaaaa = 12;\n" 16041 "#endif\n" 16042 "#if B\n" 16043 "#else\n" 16044 "int a = 12;\n" 16045 "#endif\n", 16046 Alignment); 16047 verifyFormat("enum foo {\n" 16048 "#if A\n" 16049 "#else\n" 16050 " aaaaaaaa = 12;\n" 16051 "#endif\n" 16052 "#if B\n" 16053 "#else\n" 16054 " a = 12;\n" 16055 "#endif\n" 16056 "};\n", 16057 Alignment); 16058 */ 16059 16060 Alignment.MaxEmptyLinesToKeep = 10; 16061 /* Test alignment across empty lines */ 16062 EXPECT_EQ("int a = 5;\n" 16063 "\n" 16064 "int oneTwoThree = 123;", 16065 format("int a = 5;\n" 16066 "\n" 16067 "int oneTwoThree= 123;", 16068 Alignment)); 16069 EXPECT_EQ("int a = 5;\n" 16070 "int one = 1;\n" 16071 "\n" 16072 "int oneTwoThree = 123;", 16073 format("int a = 5;\n" 16074 "int one = 1;\n" 16075 "\n" 16076 "int oneTwoThree = 123;", 16077 Alignment)); 16078 EXPECT_EQ("int a = 5;\n" 16079 "int one = 1;\n" 16080 "\n" 16081 "int oneTwoThree = 123;\n" 16082 "int oneTwo = 12;", 16083 format("int a = 5;\n" 16084 "int one = 1;\n" 16085 "\n" 16086 "int oneTwoThree = 123;\n" 16087 "int oneTwo = 12;", 16088 Alignment)); 16089 16090 /* Test across comments */ 16091 EXPECT_EQ("int a = 5;\n" 16092 "/* block comment */\n" 16093 "int oneTwoThree = 123;", 16094 format("int a = 5;\n" 16095 "/* block comment */\n" 16096 "int oneTwoThree=123;", 16097 Alignment)); 16098 16099 EXPECT_EQ("int a = 5;\n" 16100 "// line comment\n" 16101 "int oneTwoThree = 123;", 16102 format("int a = 5;\n" 16103 "// line comment\n" 16104 "int oneTwoThree=123;", 16105 Alignment)); 16106 16107 /* Test across comments and newlines */ 16108 EXPECT_EQ("int a = 5;\n" 16109 "\n" 16110 "/* block comment */\n" 16111 "int oneTwoThree = 123;", 16112 format("int a = 5;\n" 16113 "\n" 16114 "/* block comment */\n" 16115 "int oneTwoThree=123;", 16116 Alignment)); 16117 16118 EXPECT_EQ("int a = 5;\n" 16119 "\n" 16120 "// line comment\n" 16121 "int oneTwoThree = 123;", 16122 format("int a = 5;\n" 16123 "\n" 16124 "// line comment\n" 16125 "int oneTwoThree=123;", 16126 Alignment)); 16127 16128 EXPECT_EQ("int a = 5;\n" 16129 "//\n" 16130 "// multi-line line comment\n" 16131 "//\n" 16132 "int oneTwoThree = 123;", 16133 format("int a = 5;\n" 16134 "//\n" 16135 "// multi-line line comment\n" 16136 "//\n" 16137 "int oneTwoThree=123;", 16138 Alignment)); 16139 16140 EXPECT_EQ("int a = 5;\n" 16141 "/*\n" 16142 " * multi-line block comment\n" 16143 " */\n" 16144 "int oneTwoThree = 123;", 16145 format("int a = 5;\n" 16146 "/*\n" 16147 " * multi-line block comment\n" 16148 " */\n" 16149 "int oneTwoThree=123;", 16150 Alignment)); 16151 16152 EXPECT_EQ("int a = 5;\n" 16153 "\n" 16154 "/* block comment */\n" 16155 "\n" 16156 "\n" 16157 "\n" 16158 "int oneTwoThree = 123;", 16159 format("int a = 5;\n" 16160 "\n" 16161 "/* block comment */\n" 16162 "\n" 16163 "\n" 16164 "\n" 16165 "int oneTwoThree=123;", 16166 Alignment)); 16167 16168 EXPECT_EQ("int a = 5;\n" 16169 "\n" 16170 "// line comment\n" 16171 "\n" 16172 "\n" 16173 "\n" 16174 "int oneTwoThree = 123;", 16175 format("int a = 5;\n" 16176 "\n" 16177 "// line comment\n" 16178 "\n" 16179 "\n" 16180 "\n" 16181 "int oneTwoThree=123;", 16182 Alignment)); 16183 16184 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16185 verifyFormat("#define A \\\n" 16186 " int aaaa = 12; \\\n" 16187 " int b = 23; \\\n" 16188 " int ccc = 234; \\\n" 16189 " int dddddddddd = 2345;", 16190 Alignment); 16191 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16192 verifyFormat("#define A \\\n" 16193 " int aaaa = 12; \\\n" 16194 " int b = 23; \\\n" 16195 " int ccc = 234; \\\n" 16196 " int dddddddddd = 2345;", 16197 Alignment); 16198 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16199 verifyFormat("#define A " 16200 " \\\n" 16201 " int aaaa = 12; " 16202 " \\\n" 16203 " int b = 23; " 16204 " \\\n" 16205 " int ccc = 234; " 16206 " \\\n" 16207 " int dddddddddd = 2345;", 16208 Alignment); 16209 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16210 "k = 4, int l = 5,\n" 16211 " int m = 6) {\n" 16212 " int j = 10;\n" 16213 " otherThing = 1;\n" 16214 "}", 16215 Alignment); 16216 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16217 " int i = 1;\n" 16218 " int j = 2;\n" 16219 " int big = 10000;\n" 16220 "}", 16221 Alignment); 16222 verifyFormat("class C {\n" 16223 "public:\n" 16224 " int i = 1;\n" 16225 " virtual void f() = 0;\n" 16226 "};", 16227 Alignment); 16228 verifyFormat("int i = 1;\n" 16229 "if (SomeType t = getSomething()) {\n" 16230 "}\n" 16231 "int j = 2;\n" 16232 "int big = 10000;", 16233 Alignment); 16234 verifyFormat("int j = 7;\n" 16235 "for (int k = 0; k < N; ++k) {\n" 16236 "}\n" 16237 "int j = 2;\n" 16238 "int big = 10000;\n" 16239 "}", 16240 Alignment); 16241 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16242 verifyFormat("int i = 1;\n" 16243 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16244 " = someLooooooooooooooooongFunction();\n" 16245 "int j = 2;", 16246 Alignment); 16247 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16248 verifyFormat("int i = 1;\n" 16249 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16250 " someLooooooooooooooooongFunction();\n" 16251 "int j = 2;", 16252 Alignment); 16253 16254 verifyFormat("auto lambda = []() {\n" 16255 " auto i = 0;\n" 16256 " return 0;\n" 16257 "};\n" 16258 "int i = 0;\n" 16259 "auto v = type{\n" 16260 " i = 1, //\n" 16261 " (i = 2), //\n" 16262 " i = 3 //\n" 16263 "};", 16264 Alignment); 16265 16266 verifyFormat( 16267 "int i = 1;\n" 16268 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16269 " loooooooooooooooooooooongParameterB);\n" 16270 "int j = 2;", 16271 Alignment); 16272 16273 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16274 " typename B = very_long_type_name_1,\n" 16275 " typename T_2 = very_long_type_name_2>\n" 16276 "auto foo() {}\n", 16277 Alignment); 16278 verifyFormat("int a, b = 1;\n" 16279 "int c = 2;\n" 16280 "int dd = 3;\n", 16281 Alignment); 16282 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16283 "float b[1][] = {{3.f}};\n", 16284 Alignment); 16285 verifyFormat("for (int i = 0; i < 1; i++)\n" 16286 " int x = 1;\n", 16287 Alignment); 16288 verifyFormat("for (i = 0; i < 1; i++)\n" 16289 " x = 1;\n" 16290 "y = 1;\n", 16291 Alignment); 16292 16293 Alignment.ReflowComments = true; 16294 Alignment.ColumnLimit = 50; 16295 EXPECT_EQ("int x = 0;\n" 16296 "int yy = 1; /// specificlennospace\n" 16297 "int zzz = 2;\n", 16298 format("int x = 0;\n" 16299 "int yy = 1; ///specificlennospace\n" 16300 "int zzz = 2;\n", 16301 Alignment)); 16302 } 16303 16304 TEST_F(FormatTest, AlignConsecutiveAssignments) { 16305 FormatStyle Alignment = getLLVMStyle(); 16306 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16307 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16308 verifyFormat("int a = 5;\n" 16309 "int oneTwoThree = 123;", 16310 Alignment); 16311 verifyFormat("int a = 5;\n" 16312 "int oneTwoThree = 123;", 16313 Alignment); 16314 16315 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16316 verifyFormat("int a = 5;\n" 16317 "int oneTwoThree = 123;", 16318 Alignment); 16319 verifyFormat("int a = method();\n" 16320 "int oneTwoThree = 133;", 16321 Alignment); 16322 verifyFormat("a &= 5;\n" 16323 "bcd *= 5;\n" 16324 "ghtyf += 5;\n" 16325 "dvfvdb -= 5;\n" 16326 "a /= 5;\n" 16327 "vdsvsv %= 5;\n" 16328 "sfdbddfbdfbb ^= 5;\n" 16329 "dvsdsv |= 5;\n" 16330 "int dsvvdvsdvvv = 123;", 16331 Alignment); 16332 verifyFormat("int i = 1, j = 10;\n" 16333 "something = 2000;", 16334 Alignment); 16335 verifyFormat("something = 2000;\n" 16336 "int i = 1, j = 10;\n", 16337 Alignment); 16338 verifyFormat("something = 2000;\n" 16339 "another = 911;\n" 16340 "int i = 1, j = 10;\n" 16341 "oneMore = 1;\n" 16342 "i = 2;", 16343 Alignment); 16344 verifyFormat("int a = 5;\n" 16345 "int one = 1;\n" 16346 "method();\n" 16347 "int oneTwoThree = 123;\n" 16348 "int oneTwo = 12;", 16349 Alignment); 16350 verifyFormat("int oneTwoThree = 123;\n" 16351 "int oneTwo = 12;\n" 16352 "method();\n", 16353 Alignment); 16354 verifyFormat("int oneTwoThree = 123; // comment\n" 16355 "int oneTwo = 12; // comment", 16356 Alignment); 16357 verifyFormat("int f() = default;\n" 16358 "int &operator() = default;\n" 16359 "int &operator=() {", 16360 Alignment); 16361 verifyFormat("int f() = delete;\n" 16362 "int &operator() = delete;\n" 16363 "int &operator=() {", 16364 Alignment); 16365 verifyFormat("int f() = default; // comment\n" 16366 "int &operator() = default; // comment\n" 16367 "int &operator=() {", 16368 Alignment); 16369 verifyFormat("int f() = default;\n" 16370 "int &operator() = default;\n" 16371 "int &operator==() {", 16372 Alignment); 16373 verifyFormat("int f() = default;\n" 16374 "int &operator() = default;\n" 16375 "int &operator<=() {", 16376 Alignment); 16377 verifyFormat("int f() = default;\n" 16378 "int &operator() = default;\n" 16379 "int &operator!=() {", 16380 Alignment); 16381 verifyFormat("int f() = default;\n" 16382 "int &operator() = default;\n" 16383 "int &operator=();", 16384 Alignment); 16385 verifyFormat("int f() = delete;\n" 16386 "int &operator() = delete;\n" 16387 "int &operator=();", 16388 Alignment); 16389 verifyFormat("/* long long padding */ int f() = default;\n" 16390 "int &operator() = default;\n" 16391 "int &operator/**/ =();", 16392 Alignment); 16393 // https://llvm.org/PR33697 16394 FormatStyle AlignmentWithPenalty = getLLVMStyle(); 16395 AlignmentWithPenalty.AlignConsecutiveAssignments = 16396 FormatStyle::ACS_Consecutive; 16397 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000; 16398 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n" 16399 " void f() = delete;\n" 16400 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n" 16401 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n" 16402 "};\n", 16403 AlignmentWithPenalty); 16404 16405 // Bug 25167 16406 /* Uncomment when fixed 16407 verifyFormat("#if A\n" 16408 "#else\n" 16409 "int aaaaaaaa = 12;\n" 16410 "#endif\n" 16411 "#if B\n" 16412 "#else\n" 16413 "int a = 12;\n" 16414 "#endif\n", 16415 Alignment); 16416 verifyFormat("enum foo {\n" 16417 "#if A\n" 16418 "#else\n" 16419 " aaaaaaaa = 12;\n" 16420 "#endif\n" 16421 "#if B\n" 16422 "#else\n" 16423 " a = 12;\n" 16424 "#endif\n" 16425 "};\n", 16426 Alignment); 16427 */ 16428 16429 EXPECT_EQ("int a = 5;\n" 16430 "\n" 16431 "int oneTwoThree = 123;", 16432 format("int a = 5;\n" 16433 "\n" 16434 "int oneTwoThree= 123;", 16435 Alignment)); 16436 EXPECT_EQ("int a = 5;\n" 16437 "int one = 1;\n" 16438 "\n" 16439 "int oneTwoThree = 123;", 16440 format("int a = 5;\n" 16441 "int one = 1;\n" 16442 "\n" 16443 "int oneTwoThree = 123;", 16444 Alignment)); 16445 EXPECT_EQ("int a = 5;\n" 16446 "int one = 1;\n" 16447 "\n" 16448 "int oneTwoThree = 123;\n" 16449 "int oneTwo = 12;", 16450 format("int a = 5;\n" 16451 "int one = 1;\n" 16452 "\n" 16453 "int oneTwoThree = 123;\n" 16454 "int oneTwo = 12;", 16455 Alignment)); 16456 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16457 verifyFormat("#define A \\\n" 16458 " int aaaa = 12; \\\n" 16459 " int b = 23; \\\n" 16460 " int ccc = 234; \\\n" 16461 " int dddddddddd = 2345;", 16462 Alignment); 16463 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16464 verifyFormat("#define A \\\n" 16465 " int aaaa = 12; \\\n" 16466 " int b = 23; \\\n" 16467 " int ccc = 234; \\\n" 16468 " int dddddddddd = 2345;", 16469 Alignment); 16470 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16471 verifyFormat("#define A " 16472 " \\\n" 16473 " int aaaa = 12; " 16474 " \\\n" 16475 " int b = 23; " 16476 " \\\n" 16477 " int ccc = 234; " 16478 " \\\n" 16479 " int dddddddddd = 2345;", 16480 Alignment); 16481 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16482 "k = 4, int l = 5,\n" 16483 " int m = 6) {\n" 16484 " int j = 10;\n" 16485 " otherThing = 1;\n" 16486 "}", 16487 Alignment); 16488 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16489 " int i = 1;\n" 16490 " int j = 2;\n" 16491 " int big = 10000;\n" 16492 "}", 16493 Alignment); 16494 verifyFormat("class C {\n" 16495 "public:\n" 16496 " int i = 1;\n" 16497 " virtual void f() = 0;\n" 16498 "};", 16499 Alignment); 16500 verifyFormat("int i = 1;\n" 16501 "if (SomeType t = getSomething()) {\n" 16502 "}\n" 16503 "int j = 2;\n" 16504 "int big = 10000;", 16505 Alignment); 16506 verifyFormat("int j = 7;\n" 16507 "for (int k = 0; k < N; ++k) {\n" 16508 "}\n" 16509 "int j = 2;\n" 16510 "int big = 10000;\n" 16511 "}", 16512 Alignment); 16513 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16514 verifyFormat("int i = 1;\n" 16515 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16516 " = someLooooooooooooooooongFunction();\n" 16517 "int j = 2;", 16518 Alignment); 16519 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16520 verifyFormat("int i = 1;\n" 16521 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16522 " someLooooooooooooooooongFunction();\n" 16523 "int j = 2;", 16524 Alignment); 16525 16526 verifyFormat("auto lambda = []() {\n" 16527 " auto i = 0;\n" 16528 " return 0;\n" 16529 "};\n" 16530 "int i = 0;\n" 16531 "auto v = type{\n" 16532 " i = 1, //\n" 16533 " (i = 2), //\n" 16534 " i = 3 //\n" 16535 "};", 16536 Alignment); 16537 16538 verifyFormat( 16539 "int i = 1;\n" 16540 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16541 " loooooooooooooooooooooongParameterB);\n" 16542 "int j = 2;", 16543 Alignment); 16544 16545 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16546 " typename B = very_long_type_name_1,\n" 16547 " typename T_2 = very_long_type_name_2>\n" 16548 "auto foo() {}\n", 16549 Alignment); 16550 verifyFormat("int a, b = 1;\n" 16551 "int c = 2;\n" 16552 "int dd = 3;\n", 16553 Alignment); 16554 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16555 "float b[1][] = {{3.f}};\n", 16556 Alignment); 16557 verifyFormat("for (int i = 0; i < 1; i++)\n" 16558 " int x = 1;\n", 16559 Alignment); 16560 verifyFormat("for (i = 0; i < 1; i++)\n" 16561 " x = 1;\n" 16562 "y = 1;\n", 16563 Alignment); 16564 16565 EXPECT_EQ(Alignment.ReflowComments, true); 16566 Alignment.ColumnLimit = 50; 16567 EXPECT_EQ("int x = 0;\n" 16568 "int yy = 1; /// specificlennospace\n" 16569 "int zzz = 2;\n", 16570 format("int x = 0;\n" 16571 "int yy = 1; ///specificlennospace\n" 16572 "int zzz = 2;\n", 16573 Alignment)); 16574 16575 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16576 "auto b = [] {\n" 16577 " f();\n" 16578 " return;\n" 16579 "};", 16580 Alignment); 16581 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16582 "auto b = g([] {\n" 16583 " f();\n" 16584 " return;\n" 16585 "});", 16586 Alignment); 16587 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16588 "auto b = g(param, [] {\n" 16589 " f();\n" 16590 " return;\n" 16591 "});", 16592 Alignment); 16593 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16594 "auto b = [] {\n" 16595 " if (condition) {\n" 16596 " return;\n" 16597 " }\n" 16598 "};", 16599 Alignment); 16600 16601 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 16602 " ccc ? aaaaa : bbbbb,\n" 16603 " dddddddddddddddddddddddddd);", 16604 Alignment); 16605 // FIXME: https://llvm.org/PR53497 16606 // verifyFormat("auto aaaaaaaaaaaa = f();\n" 16607 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 16608 // " ccc ? aaaaa : bbbbb,\n" 16609 // " dddddddddddddddddddddddddd);", 16610 // Alignment); 16611 } 16612 16613 TEST_F(FormatTest, AlignConsecutiveBitFields) { 16614 FormatStyle Alignment = getLLVMStyle(); 16615 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 16616 verifyFormat("int const a : 5;\n" 16617 "int oneTwoThree : 23;", 16618 Alignment); 16619 16620 // Initializers are allowed starting with c++2a 16621 verifyFormat("int const a : 5 = 1;\n" 16622 "int oneTwoThree : 23 = 0;", 16623 Alignment); 16624 16625 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16626 verifyFormat("int const a : 5;\n" 16627 "int oneTwoThree : 23;", 16628 Alignment); 16629 16630 verifyFormat("int const a : 5; // comment\n" 16631 "int oneTwoThree : 23; // comment", 16632 Alignment); 16633 16634 verifyFormat("int const a : 5 = 1;\n" 16635 "int oneTwoThree : 23 = 0;", 16636 Alignment); 16637 16638 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16639 verifyFormat("int const a : 5 = 1;\n" 16640 "int oneTwoThree : 23 = 0;", 16641 Alignment); 16642 verifyFormat("int const a : 5 = {1};\n" 16643 "int oneTwoThree : 23 = 0;", 16644 Alignment); 16645 16646 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 16647 verifyFormat("int const a :5;\n" 16648 "int oneTwoThree:23;", 16649 Alignment); 16650 16651 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 16652 verifyFormat("int const a :5;\n" 16653 "int oneTwoThree :23;", 16654 Alignment); 16655 16656 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 16657 verifyFormat("int const a : 5;\n" 16658 "int oneTwoThree: 23;", 16659 Alignment); 16660 16661 // Known limitations: ':' is only recognized as a bitfield colon when 16662 // followed by a number. 16663 /* 16664 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 16665 "int a : 5;", 16666 Alignment); 16667 */ 16668 } 16669 16670 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 16671 FormatStyle Alignment = getLLVMStyle(); 16672 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16673 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16674 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16675 verifyFormat("float const a = 5;\n" 16676 "int oneTwoThree = 123;", 16677 Alignment); 16678 verifyFormat("int a = 5;\n" 16679 "float const oneTwoThree = 123;", 16680 Alignment); 16681 16682 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16683 verifyFormat("float const a = 5;\n" 16684 "int oneTwoThree = 123;", 16685 Alignment); 16686 verifyFormat("int a = method();\n" 16687 "float const oneTwoThree = 133;", 16688 Alignment); 16689 verifyFormat("int i = 1, j = 10;\n" 16690 "something = 2000;", 16691 Alignment); 16692 verifyFormat("something = 2000;\n" 16693 "int i = 1, j = 10;\n", 16694 Alignment); 16695 verifyFormat("float something = 2000;\n" 16696 "double another = 911;\n" 16697 "int i = 1, j = 10;\n" 16698 "const int *oneMore = 1;\n" 16699 "unsigned i = 2;", 16700 Alignment); 16701 verifyFormat("float a = 5;\n" 16702 "int one = 1;\n" 16703 "method();\n" 16704 "const double oneTwoThree = 123;\n" 16705 "const unsigned int oneTwo = 12;", 16706 Alignment); 16707 verifyFormat("int oneTwoThree{0}; // comment\n" 16708 "unsigned oneTwo; // comment", 16709 Alignment); 16710 verifyFormat("unsigned int *a;\n" 16711 "int *b;\n" 16712 "unsigned int Const *c;\n" 16713 "unsigned int const *d;\n" 16714 "unsigned int Const &e;\n" 16715 "unsigned int const &f;", 16716 Alignment); 16717 verifyFormat("Const unsigned int *c;\n" 16718 "const unsigned int *d;\n" 16719 "Const unsigned int &e;\n" 16720 "const unsigned int &f;\n" 16721 "const unsigned g;\n" 16722 "Const unsigned h;", 16723 Alignment); 16724 EXPECT_EQ("float const a = 5;\n" 16725 "\n" 16726 "int oneTwoThree = 123;", 16727 format("float const a = 5;\n" 16728 "\n" 16729 "int oneTwoThree= 123;", 16730 Alignment)); 16731 EXPECT_EQ("float a = 5;\n" 16732 "int one = 1;\n" 16733 "\n" 16734 "unsigned oneTwoThree = 123;", 16735 format("float a = 5;\n" 16736 "int one = 1;\n" 16737 "\n" 16738 "unsigned oneTwoThree = 123;", 16739 Alignment)); 16740 EXPECT_EQ("float a = 5;\n" 16741 "int one = 1;\n" 16742 "\n" 16743 "unsigned oneTwoThree = 123;\n" 16744 "int oneTwo = 12;", 16745 format("float a = 5;\n" 16746 "int one = 1;\n" 16747 "\n" 16748 "unsigned oneTwoThree = 123;\n" 16749 "int oneTwo = 12;", 16750 Alignment)); 16751 // Function prototype alignment 16752 verifyFormat("int a();\n" 16753 "double b();", 16754 Alignment); 16755 verifyFormat("int a(int x);\n" 16756 "double b();", 16757 Alignment); 16758 unsigned OldColumnLimit = Alignment.ColumnLimit; 16759 // We need to set ColumnLimit to zero, in order to stress nested alignments, 16760 // otherwise the function parameters will be re-flowed onto a single line. 16761 Alignment.ColumnLimit = 0; 16762 EXPECT_EQ("int a(int x,\n" 16763 " float y);\n" 16764 "double b(int x,\n" 16765 " double y);", 16766 format("int a(int x,\n" 16767 " float y);\n" 16768 "double b(int x,\n" 16769 " double y);", 16770 Alignment)); 16771 // This ensures that function parameters of function declarations are 16772 // correctly indented when their owning functions are indented. 16773 // The failure case here is for 'double y' to not be indented enough. 16774 EXPECT_EQ("double a(int x);\n" 16775 "int b(int y,\n" 16776 " double z);", 16777 format("double a(int x);\n" 16778 "int b(int y,\n" 16779 " double z);", 16780 Alignment)); 16781 // Set ColumnLimit low so that we induce wrapping immediately after 16782 // the function name and opening paren. 16783 Alignment.ColumnLimit = 13; 16784 verifyFormat("int function(\n" 16785 " int x,\n" 16786 " bool y);", 16787 Alignment); 16788 Alignment.ColumnLimit = OldColumnLimit; 16789 // Ensure function pointers don't screw up recursive alignment 16790 verifyFormat("int a(int x, void (*fp)(int y));\n" 16791 "double b();", 16792 Alignment); 16793 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16794 // Ensure recursive alignment is broken by function braces, so that the 16795 // "a = 1" does not align with subsequent assignments inside the function 16796 // body. 16797 verifyFormat("int func(int a = 1) {\n" 16798 " int b = 2;\n" 16799 " int cc = 3;\n" 16800 "}", 16801 Alignment); 16802 verifyFormat("float something = 2000;\n" 16803 "double another = 911;\n" 16804 "int i = 1, j = 10;\n" 16805 "const int *oneMore = 1;\n" 16806 "unsigned i = 2;", 16807 Alignment); 16808 verifyFormat("int oneTwoThree = {0}; // comment\n" 16809 "unsigned oneTwo = 0; // comment", 16810 Alignment); 16811 // Make sure that scope is correctly tracked, in the absence of braces 16812 verifyFormat("for (int i = 0; i < n; i++)\n" 16813 " j = i;\n" 16814 "double x = 1;\n", 16815 Alignment); 16816 verifyFormat("if (int i = 0)\n" 16817 " j = i;\n" 16818 "double x = 1;\n", 16819 Alignment); 16820 // Ensure operator[] and operator() are comprehended 16821 verifyFormat("struct test {\n" 16822 " long long int foo();\n" 16823 " int operator[](int a);\n" 16824 " double bar();\n" 16825 "};\n", 16826 Alignment); 16827 verifyFormat("struct test {\n" 16828 " long long int foo();\n" 16829 " int operator()(int a);\n" 16830 " double bar();\n" 16831 "};\n", 16832 Alignment); 16833 // http://llvm.org/PR52914 16834 verifyFormat("char *a[] = {\"a\", // comment\n" 16835 " \"bb\"};\n" 16836 "int bbbbbbb = 0;", 16837 Alignment); 16838 16839 // PAS_Right 16840 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16841 " int const i = 1;\n" 16842 " int *j = 2;\n" 16843 " int big = 10000;\n" 16844 "\n" 16845 " unsigned oneTwoThree = 123;\n" 16846 " int oneTwo = 12;\n" 16847 " method();\n" 16848 " float k = 2;\n" 16849 " int ll = 10000;\n" 16850 "}", 16851 format("void SomeFunction(int parameter= 0) {\n" 16852 " int const i= 1;\n" 16853 " int *j=2;\n" 16854 " int big = 10000;\n" 16855 "\n" 16856 "unsigned oneTwoThree =123;\n" 16857 "int oneTwo = 12;\n" 16858 " method();\n" 16859 "float k= 2;\n" 16860 "int ll=10000;\n" 16861 "}", 16862 Alignment)); 16863 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16864 " int const i = 1;\n" 16865 " int **j = 2, ***k;\n" 16866 " int &k = i;\n" 16867 " int &&l = i + j;\n" 16868 " int big = 10000;\n" 16869 "\n" 16870 " unsigned oneTwoThree = 123;\n" 16871 " int oneTwo = 12;\n" 16872 " method();\n" 16873 " float k = 2;\n" 16874 " int ll = 10000;\n" 16875 "}", 16876 format("void SomeFunction(int parameter= 0) {\n" 16877 " int const i= 1;\n" 16878 " int **j=2,***k;\n" 16879 "int &k=i;\n" 16880 "int &&l=i+j;\n" 16881 " int big = 10000;\n" 16882 "\n" 16883 "unsigned oneTwoThree =123;\n" 16884 "int oneTwo = 12;\n" 16885 " method();\n" 16886 "float k= 2;\n" 16887 "int ll=10000;\n" 16888 "}", 16889 Alignment)); 16890 // variables are aligned at their name, pointers are at the right most 16891 // position 16892 verifyFormat("int *a;\n" 16893 "int **b;\n" 16894 "int ***c;\n" 16895 "int foobar;\n", 16896 Alignment); 16897 16898 // PAS_Left 16899 FormatStyle AlignmentLeft = Alignment; 16900 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16901 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16902 " int const i = 1;\n" 16903 " int* j = 2;\n" 16904 " int big = 10000;\n" 16905 "\n" 16906 " unsigned oneTwoThree = 123;\n" 16907 " int oneTwo = 12;\n" 16908 " method();\n" 16909 " float k = 2;\n" 16910 " int ll = 10000;\n" 16911 "}", 16912 format("void SomeFunction(int parameter= 0) {\n" 16913 " int const i= 1;\n" 16914 " int *j=2;\n" 16915 " int big = 10000;\n" 16916 "\n" 16917 "unsigned oneTwoThree =123;\n" 16918 "int oneTwo = 12;\n" 16919 " method();\n" 16920 "float k= 2;\n" 16921 "int ll=10000;\n" 16922 "}", 16923 AlignmentLeft)); 16924 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16925 " int const i = 1;\n" 16926 " int** j = 2;\n" 16927 " int& k = i;\n" 16928 " int&& l = i + j;\n" 16929 " int big = 10000;\n" 16930 "\n" 16931 " unsigned oneTwoThree = 123;\n" 16932 " int oneTwo = 12;\n" 16933 " method();\n" 16934 " float k = 2;\n" 16935 " int ll = 10000;\n" 16936 "}", 16937 format("void SomeFunction(int parameter= 0) {\n" 16938 " int const i= 1;\n" 16939 " int **j=2;\n" 16940 "int &k=i;\n" 16941 "int &&l=i+j;\n" 16942 " int big = 10000;\n" 16943 "\n" 16944 "unsigned oneTwoThree =123;\n" 16945 "int oneTwo = 12;\n" 16946 " method();\n" 16947 "float k= 2;\n" 16948 "int ll=10000;\n" 16949 "}", 16950 AlignmentLeft)); 16951 // variables are aligned at their name, pointers are at the left most position 16952 verifyFormat("int* a;\n" 16953 "int** b;\n" 16954 "int*** c;\n" 16955 "int foobar;\n", 16956 AlignmentLeft); 16957 16958 // PAS_Middle 16959 FormatStyle AlignmentMiddle = Alignment; 16960 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16961 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16962 " int const i = 1;\n" 16963 " int * j = 2;\n" 16964 " int big = 10000;\n" 16965 "\n" 16966 " unsigned oneTwoThree = 123;\n" 16967 " int oneTwo = 12;\n" 16968 " method();\n" 16969 " float k = 2;\n" 16970 " int ll = 10000;\n" 16971 "}", 16972 format("void SomeFunction(int parameter= 0) {\n" 16973 " int const i= 1;\n" 16974 " int *j=2;\n" 16975 " int big = 10000;\n" 16976 "\n" 16977 "unsigned oneTwoThree =123;\n" 16978 "int oneTwo = 12;\n" 16979 " method();\n" 16980 "float k= 2;\n" 16981 "int ll=10000;\n" 16982 "}", 16983 AlignmentMiddle)); 16984 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16985 " int const i = 1;\n" 16986 " int ** j = 2, ***k;\n" 16987 " int & k = i;\n" 16988 " int && l = i + j;\n" 16989 " int big = 10000;\n" 16990 "\n" 16991 " unsigned oneTwoThree = 123;\n" 16992 " int oneTwo = 12;\n" 16993 " method();\n" 16994 " float k = 2;\n" 16995 " int ll = 10000;\n" 16996 "}", 16997 format("void SomeFunction(int parameter= 0) {\n" 16998 " int const i= 1;\n" 16999 " int **j=2,***k;\n" 17000 "int &k=i;\n" 17001 "int &&l=i+j;\n" 17002 " int big = 10000;\n" 17003 "\n" 17004 "unsigned oneTwoThree =123;\n" 17005 "int oneTwo = 12;\n" 17006 " method();\n" 17007 "float k= 2;\n" 17008 "int ll=10000;\n" 17009 "}", 17010 AlignmentMiddle)); 17011 // variables are aligned at their name, pointers are in the middle 17012 verifyFormat("int * a;\n" 17013 "int * b;\n" 17014 "int *** c;\n" 17015 "int foobar;\n", 17016 AlignmentMiddle); 17017 17018 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17019 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 17020 verifyFormat("#define A \\\n" 17021 " int aaaa = 12; \\\n" 17022 " float b = 23; \\\n" 17023 " const int ccc = 234; \\\n" 17024 " unsigned dddddddddd = 2345;", 17025 Alignment); 17026 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 17027 verifyFormat("#define A \\\n" 17028 " int aaaa = 12; \\\n" 17029 " float b = 23; \\\n" 17030 " const int ccc = 234; \\\n" 17031 " unsigned dddddddddd = 2345;", 17032 Alignment); 17033 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 17034 Alignment.ColumnLimit = 30; 17035 verifyFormat("#define A \\\n" 17036 " int aaaa = 12; \\\n" 17037 " float b = 23; \\\n" 17038 " const int ccc = 234; \\\n" 17039 " int dddddddddd = 2345;", 17040 Alignment); 17041 Alignment.ColumnLimit = 80; 17042 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 17043 "k = 4, int l = 5,\n" 17044 " int m = 6) {\n" 17045 " const int j = 10;\n" 17046 " otherThing = 1;\n" 17047 "}", 17048 Alignment); 17049 verifyFormat("void SomeFunction(int parameter = 0) {\n" 17050 " int const i = 1;\n" 17051 " int *j = 2;\n" 17052 " int big = 10000;\n" 17053 "}", 17054 Alignment); 17055 verifyFormat("class C {\n" 17056 "public:\n" 17057 " int i = 1;\n" 17058 " virtual void f() = 0;\n" 17059 "};", 17060 Alignment); 17061 verifyFormat("float i = 1;\n" 17062 "if (SomeType t = getSomething()) {\n" 17063 "}\n" 17064 "const unsigned j = 2;\n" 17065 "int big = 10000;", 17066 Alignment); 17067 verifyFormat("float j = 7;\n" 17068 "for (int k = 0; k < N; ++k) {\n" 17069 "}\n" 17070 "unsigned j = 2;\n" 17071 "int big = 10000;\n" 17072 "}", 17073 Alignment); 17074 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 17075 verifyFormat("float i = 1;\n" 17076 "LooooooooooongType loooooooooooooooooooooongVariable\n" 17077 " = someLooooooooooooooooongFunction();\n" 17078 "int j = 2;", 17079 Alignment); 17080 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 17081 verifyFormat("int i = 1;\n" 17082 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 17083 " someLooooooooooooooooongFunction();\n" 17084 "int j = 2;", 17085 Alignment); 17086 17087 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17088 verifyFormat("auto lambda = []() {\n" 17089 " auto ii = 0;\n" 17090 " float j = 0;\n" 17091 " return 0;\n" 17092 "};\n" 17093 "int i = 0;\n" 17094 "float i2 = 0;\n" 17095 "auto v = type{\n" 17096 " i = 1, //\n" 17097 " (i = 2), //\n" 17098 " i = 3 //\n" 17099 "};", 17100 Alignment); 17101 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17102 17103 verifyFormat( 17104 "int i = 1;\n" 17105 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 17106 " loooooooooooooooooooooongParameterB);\n" 17107 "int j = 2;", 17108 Alignment); 17109 17110 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 17111 // We expect declarations and assignments to align, as long as it doesn't 17112 // exceed the column limit, starting a new alignment sequence whenever it 17113 // happens. 17114 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17115 Alignment.ColumnLimit = 30; 17116 verifyFormat("float ii = 1;\n" 17117 "unsigned j = 2;\n" 17118 "int someVerylongVariable = 1;\n" 17119 "AnotherLongType ll = 123456;\n" 17120 "VeryVeryLongType k = 2;\n" 17121 "int myvar = 1;", 17122 Alignment); 17123 Alignment.ColumnLimit = 80; 17124 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17125 17126 verifyFormat( 17127 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 17128 " typename LongType, typename B>\n" 17129 "auto foo() {}\n", 17130 Alignment); 17131 verifyFormat("float a, b = 1;\n" 17132 "int c = 2;\n" 17133 "int dd = 3;\n", 17134 Alignment); 17135 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17136 "float b[1][] = {{3.f}};\n", 17137 Alignment); 17138 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17139 verifyFormat("float a, b = 1;\n" 17140 "int c = 2;\n" 17141 "int dd = 3;\n", 17142 Alignment); 17143 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17144 "float b[1][] = {{3.f}};\n", 17145 Alignment); 17146 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17147 17148 Alignment.ColumnLimit = 30; 17149 Alignment.BinPackParameters = false; 17150 verifyFormat("void foo(float a,\n" 17151 " float b,\n" 17152 " int c,\n" 17153 " uint32_t *d) {\n" 17154 " int *e = 0;\n" 17155 " float f = 0;\n" 17156 " double g = 0;\n" 17157 "}\n" 17158 "void bar(ino_t a,\n" 17159 " int b,\n" 17160 " uint32_t *c,\n" 17161 " bool d) {}\n", 17162 Alignment); 17163 Alignment.BinPackParameters = true; 17164 Alignment.ColumnLimit = 80; 17165 17166 // Bug 33507 17167 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17168 verifyFormat( 17169 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 17170 " static const Version verVs2017;\n" 17171 " return true;\n" 17172 "});\n", 17173 Alignment); 17174 Alignment.PointerAlignment = FormatStyle::PAS_Right; 17175 17176 // See llvm.org/PR35641 17177 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17178 verifyFormat("int func() { //\n" 17179 " int b;\n" 17180 " unsigned c;\n" 17181 "}", 17182 Alignment); 17183 17184 // See PR37175 17185 FormatStyle Style = getMozillaStyle(); 17186 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17187 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 17188 "foo(int a);", 17189 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 17190 17191 Alignment.PointerAlignment = FormatStyle::PAS_Left; 17192 verifyFormat("unsigned int* a;\n" 17193 "int* b;\n" 17194 "unsigned int Const* c;\n" 17195 "unsigned int const* d;\n" 17196 "unsigned int Const& e;\n" 17197 "unsigned int const& f;", 17198 Alignment); 17199 verifyFormat("Const unsigned int* c;\n" 17200 "const unsigned int* d;\n" 17201 "Const unsigned int& e;\n" 17202 "const unsigned int& f;\n" 17203 "const unsigned g;\n" 17204 "Const unsigned h;", 17205 Alignment); 17206 17207 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17208 verifyFormat("unsigned int * a;\n" 17209 "int * b;\n" 17210 "unsigned int Const * c;\n" 17211 "unsigned int const * d;\n" 17212 "unsigned int Const & e;\n" 17213 "unsigned int const & f;", 17214 Alignment); 17215 verifyFormat("Const unsigned int * c;\n" 17216 "const unsigned int * d;\n" 17217 "Const unsigned int & e;\n" 17218 "const unsigned int & f;\n" 17219 "const unsigned g;\n" 17220 "Const unsigned h;", 17221 Alignment); 17222 } 17223 17224 TEST_F(FormatTest, AlignWithLineBreaks) { 17225 auto Style = getLLVMStyleWithColumns(120); 17226 17227 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 17228 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 17229 verifyFormat("void foo() {\n" 17230 " int myVar = 5;\n" 17231 " double x = 3.14;\n" 17232 " auto str = \"Hello \"\n" 17233 " \"World\";\n" 17234 " auto s = \"Hello \"\n" 17235 " \"Again\";\n" 17236 "}", 17237 Style); 17238 17239 // clang-format off 17240 verifyFormat("void foo() {\n" 17241 " const int capacityBefore = Entries.capacity();\n" 17242 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17243 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17244 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17245 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17246 "}", 17247 Style); 17248 // clang-format on 17249 17250 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17251 verifyFormat("void foo() {\n" 17252 " int myVar = 5;\n" 17253 " double x = 3.14;\n" 17254 " auto str = \"Hello \"\n" 17255 " \"World\";\n" 17256 " auto s = \"Hello \"\n" 17257 " \"Again\";\n" 17258 "}", 17259 Style); 17260 17261 // clang-format off 17262 verifyFormat("void foo() {\n" 17263 " const int capacityBefore = Entries.capacity();\n" 17264 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17265 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17266 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17267 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17268 "}", 17269 Style); 17270 // clang-format on 17271 17272 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17273 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17274 verifyFormat("void foo() {\n" 17275 " int myVar = 5;\n" 17276 " double x = 3.14;\n" 17277 " auto str = \"Hello \"\n" 17278 " \"World\";\n" 17279 " auto s = \"Hello \"\n" 17280 " \"Again\";\n" 17281 "}", 17282 Style); 17283 17284 // clang-format off 17285 verifyFormat("void foo() {\n" 17286 " const int capacityBefore = Entries.capacity();\n" 17287 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17288 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17289 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17290 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17291 "}", 17292 Style); 17293 // clang-format on 17294 17295 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17296 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17297 17298 verifyFormat("void foo() {\n" 17299 " int myVar = 5;\n" 17300 " double x = 3.14;\n" 17301 " auto str = \"Hello \"\n" 17302 " \"World\";\n" 17303 " auto s = \"Hello \"\n" 17304 " \"Again\";\n" 17305 "}", 17306 Style); 17307 17308 // clang-format off 17309 verifyFormat("void foo() {\n" 17310 " const int capacityBefore = Entries.capacity();\n" 17311 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17312 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17313 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17314 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17315 "}", 17316 Style); 17317 // clang-format on 17318 17319 Style = getLLVMStyleWithColumns(120); 17320 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17321 Style.ContinuationIndentWidth = 4; 17322 Style.IndentWidth = 4; 17323 17324 // clang-format off 17325 verifyFormat("void SomeFunc() {\n" 17326 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17327 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17328 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17329 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17330 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17331 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17332 "}", 17333 Style); 17334 // clang-format on 17335 17336 Style.BinPackArguments = false; 17337 17338 // clang-format off 17339 verifyFormat("void SomeFunc() {\n" 17340 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 17341 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17342 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 17343 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17344 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 17345 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17346 "}", 17347 Style); 17348 // clang-format on 17349 } 17350 17351 TEST_F(FormatTest, AlignWithInitializerPeriods) { 17352 auto Style = getLLVMStyleWithColumns(60); 17353 17354 verifyFormat("void foo1(void) {\n" 17355 " BYTE p[1] = 1;\n" 17356 " A B = {.one_foooooooooooooooo = 2,\n" 17357 " .two_fooooooooooooo = 3,\n" 17358 " .three_fooooooooooooo = 4};\n" 17359 " BYTE payload = 2;\n" 17360 "}", 17361 Style); 17362 17363 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17364 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 17365 verifyFormat("void foo2(void) {\n" 17366 " BYTE p[1] = 1;\n" 17367 " A B = {.one_foooooooooooooooo = 2,\n" 17368 " .two_fooooooooooooo = 3,\n" 17369 " .three_fooooooooooooo = 4};\n" 17370 " BYTE payload = 2;\n" 17371 "}", 17372 Style); 17373 17374 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17375 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17376 verifyFormat("void foo3(void) {\n" 17377 " BYTE p[1] = 1;\n" 17378 " A B = {.one_foooooooooooooooo = 2,\n" 17379 " .two_fooooooooooooo = 3,\n" 17380 " .three_fooooooooooooo = 4};\n" 17381 " BYTE payload = 2;\n" 17382 "}", 17383 Style); 17384 17385 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17386 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17387 verifyFormat("void foo4(void) {\n" 17388 " BYTE p[1] = 1;\n" 17389 " A B = {.one_foooooooooooooooo = 2,\n" 17390 " .two_fooooooooooooo = 3,\n" 17391 " .three_fooooooooooooo = 4};\n" 17392 " BYTE payload = 2;\n" 17393 "}", 17394 Style); 17395 } 17396 17397 TEST_F(FormatTest, LinuxBraceBreaking) { 17398 FormatStyle LinuxBraceStyle = getLLVMStyle(); 17399 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 17400 verifyFormat("namespace a\n" 17401 "{\n" 17402 "class A\n" 17403 "{\n" 17404 " void f()\n" 17405 " {\n" 17406 " if (true) {\n" 17407 " a();\n" 17408 " b();\n" 17409 " } else {\n" 17410 " a();\n" 17411 " }\n" 17412 " }\n" 17413 " void g() { return; }\n" 17414 "};\n" 17415 "struct B {\n" 17416 " int x;\n" 17417 "};\n" 17418 "} // namespace a\n", 17419 LinuxBraceStyle); 17420 verifyFormat("enum X {\n" 17421 " Y = 0,\n" 17422 "}\n", 17423 LinuxBraceStyle); 17424 verifyFormat("struct S {\n" 17425 " int Type;\n" 17426 " union {\n" 17427 " int x;\n" 17428 " double y;\n" 17429 " } Value;\n" 17430 " class C\n" 17431 " {\n" 17432 " MyFavoriteType Value;\n" 17433 " } Class;\n" 17434 "}\n", 17435 LinuxBraceStyle); 17436 } 17437 17438 TEST_F(FormatTest, MozillaBraceBreaking) { 17439 FormatStyle MozillaBraceStyle = getLLVMStyle(); 17440 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 17441 MozillaBraceStyle.FixNamespaceComments = false; 17442 verifyFormat("namespace a {\n" 17443 "class A\n" 17444 "{\n" 17445 " void f()\n" 17446 " {\n" 17447 " if (true) {\n" 17448 " a();\n" 17449 " b();\n" 17450 " }\n" 17451 " }\n" 17452 " void g() { return; }\n" 17453 "};\n" 17454 "enum E\n" 17455 "{\n" 17456 " A,\n" 17457 " // foo\n" 17458 " B,\n" 17459 " C\n" 17460 "};\n" 17461 "struct B\n" 17462 "{\n" 17463 " int x;\n" 17464 "};\n" 17465 "}\n", 17466 MozillaBraceStyle); 17467 verifyFormat("struct S\n" 17468 "{\n" 17469 " int Type;\n" 17470 " union\n" 17471 " {\n" 17472 " int x;\n" 17473 " double y;\n" 17474 " } Value;\n" 17475 " class C\n" 17476 " {\n" 17477 " MyFavoriteType Value;\n" 17478 " } Class;\n" 17479 "}\n", 17480 MozillaBraceStyle); 17481 } 17482 17483 TEST_F(FormatTest, StroustrupBraceBreaking) { 17484 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 17485 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 17486 verifyFormat("namespace a {\n" 17487 "class A {\n" 17488 " void f()\n" 17489 " {\n" 17490 " if (true) {\n" 17491 " a();\n" 17492 " b();\n" 17493 " }\n" 17494 " }\n" 17495 " void g() { return; }\n" 17496 "};\n" 17497 "struct B {\n" 17498 " int x;\n" 17499 "};\n" 17500 "} // namespace a\n", 17501 StroustrupBraceStyle); 17502 17503 verifyFormat("void foo()\n" 17504 "{\n" 17505 " if (a) {\n" 17506 " a();\n" 17507 " }\n" 17508 " else {\n" 17509 " b();\n" 17510 " }\n" 17511 "}\n", 17512 StroustrupBraceStyle); 17513 17514 verifyFormat("#ifdef _DEBUG\n" 17515 "int foo(int i = 0)\n" 17516 "#else\n" 17517 "int foo(int i = 5)\n" 17518 "#endif\n" 17519 "{\n" 17520 " return i;\n" 17521 "}", 17522 StroustrupBraceStyle); 17523 17524 verifyFormat("void foo() {}\n" 17525 "void bar()\n" 17526 "#ifdef _DEBUG\n" 17527 "{\n" 17528 " foo();\n" 17529 "}\n" 17530 "#else\n" 17531 "{\n" 17532 "}\n" 17533 "#endif", 17534 StroustrupBraceStyle); 17535 17536 verifyFormat("void foobar() { int i = 5; }\n" 17537 "#ifdef _DEBUG\n" 17538 "void bar() {}\n" 17539 "#else\n" 17540 "void bar() { foobar(); }\n" 17541 "#endif", 17542 StroustrupBraceStyle); 17543 } 17544 17545 TEST_F(FormatTest, AllmanBraceBreaking) { 17546 FormatStyle AllmanBraceStyle = getLLVMStyle(); 17547 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 17548 17549 EXPECT_EQ("namespace a\n" 17550 "{\n" 17551 "void f();\n" 17552 "void g();\n" 17553 "} // namespace a\n", 17554 format("namespace a\n" 17555 "{\n" 17556 "void f();\n" 17557 "void g();\n" 17558 "}\n", 17559 AllmanBraceStyle)); 17560 17561 verifyFormat("namespace a\n" 17562 "{\n" 17563 "class A\n" 17564 "{\n" 17565 " void f()\n" 17566 " {\n" 17567 " if (true)\n" 17568 " {\n" 17569 " a();\n" 17570 " b();\n" 17571 " }\n" 17572 " }\n" 17573 " void g() { return; }\n" 17574 "};\n" 17575 "struct B\n" 17576 "{\n" 17577 " int x;\n" 17578 "};\n" 17579 "union C\n" 17580 "{\n" 17581 "};\n" 17582 "} // namespace a", 17583 AllmanBraceStyle); 17584 17585 verifyFormat("void f()\n" 17586 "{\n" 17587 " if (true)\n" 17588 " {\n" 17589 " a();\n" 17590 " }\n" 17591 " else if (false)\n" 17592 " {\n" 17593 " b();\n" 17594 " }\n" 17595 " else\n" 17596 " {\n" 17597 " c();\n" 17598 " }\n" 17599 "}\n", 17600 AllmanBraceStyle); 17601 17602 verifyFormat("void f()\n" 17603 "{\n" 17604 " for (int i = 0; i < 10; ++i)\n" 17605 " {\n" 17606 " a();\n" 17607 " }\n" 17608 " while (false)\n" 17609 " {\n" 17610 " b();\n" 17611 " }\n" 17612 " do\n" 17613 " {\n" 17614 " c();\n" 17615 " } while (false)\n" 17616 "}\n", 17617 AllmanBraceStyle); 17618 17619 verifyFormat("void f(int a)\n" 17620 "{\n" 17621 " switch (a)\n" 17622 " {\n" 17623 " case 0:\n" 17624 " break;\n" 17625 " case 1:\n" 17626 " {\n" 17627 " break;\n" 17628 " }\n" 17629 " case 2:\n" 17630 " {\n" 17631 " }\n" 17632 " break;\n" 17633 " default:\n" 17634 " break;\n" 17635 " }\n" 17636 "}\n", 17637 AllmanBraceStyle); 17638 17639 verifyFormat("enum X\n" 17640 "{\n" 17641 " Y = 0,\n" 17642 "}\n", 17643 AllmanBraceStyle); 17644 verifyFormat("enum X\n" 17645 "{\n" 17646 " Y = 0\n" 17647 "}\n", 17648 AllmanBraceStyle); 17649 17650 verifyFormat("@interface BSApplicationController ()\n" 17651 "{\n" 17652 "@private\n" 17653 " id _extraIvar;\n" 17654 "}\n" 17655 "@end\n", 17656 AllmanBraceStyle); 17657 17658 verifyFormat("#ifdef _DEBUG\n" 17659 "int foo(int i = 0)\n" 17660 "#else\n" 17661 "int foo(int i = 5)\n" 17662 "#endif\n" 17663 "{\n" 17664 " return i;\n" 17665 "}", 17666 AllmanBraceStyle); 17667 17668 verifyFormat("void foo() {}\n" 17669 "void bar()\n" 17670 "#ifdef _DEBUG\n" 17671 "{\n" 17672 " foo();\n" 17673 "}\n" 17674 "#else\n" 17675 "{\n" 17676 "}\n" 17677 "#endif", 17678 AllmanBraceStyle); 17679 17680 verifyFormat("void foobar() { int i = 5; }\n" 17681 "#ifdef _DEBUG\n" 17682 "void bar() {}\n" 17683 "#else\n" 17684 "void bar() { foobar(); }\n" 17685 "#endif", 17686 AllmanBraceStyle); 17687 17688 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 17689 FormatStyle::SLS_All); 17690 17691 verifyFormat("[](int i) { return i + 2; };\n" 17692 "[](int i, int j)\n" 17693 "{\n" 17694 " auto x = i + j;\n" 17695 " auto y = i * j;\n" 17696 " return x ^ y;\n" 17697 "};\n" 17698 "void foo()\n" 17699 "{\n" 17700 " auto shortLambda = [](int i) { return i + 2; };\n" 17701 " auto longLambda = [](int i, int j)\n" 17702 " {\n" 17703 " auto x = i + j;\n" 17704 " auto y = i * j;\n" 17705 " return x ^ y;\n" 17706 " };\n" 17707 "}", 17708 AllmanBraceStyle); 17709 17710 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17711 17712 verifyFormat("[](int i)\n" 17713 "{\n" 17714 " return i + 2;\n" 17715 "};\n" 17716 "[](int i, int j)\n" 17717 "{\n" 17718 " auto x = i + j;\n" 17719 " auto y = i * j;\n" 17720 " return x ^ y;\n" 17721 "};\n" 17722 "void foo()\n" 17723 "{\n" 17724 " auto shortLambda = [](int i)\n" 17725 " {\n" 17726 " return i + 2;\n" 17727 " };\n" 17728 " auto longLambda = [](int i, int j)\n" 17729 " {\n" 17730 " auto x = i + j;\n" 17731 " auto y = i * j;\n" 17732 " return x ^ y;\n" 17733 " };\n" 17734 "}", 17735 AllmanBraceStyle); 17736 17737 // Reset 17738 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 17739 17740 // This shouldn't affect ObjC blocks.. 17741 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17742 " // ...\n" 17743 " int i;\n" 17744 "}];", 17745 AllmanBraceStyle); 17746 verifyFormat("void (^block)(void) = ^{\n" 17747 " // ...\n" 17748 " int i;\n" 17749 "};", 17750 AllmanBraceStyle); 17751 // .. or dict literals. 17752 verifyFormat("void f()\n" 17753 "{\n" 17754 " // ...\n" 17755 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17756 "}", 17757 AllmanBraceStyle); 17758 verifyFormat("void f()\n" 17759 "{\n" 17760 " // ...\n" 17761 " [object someMethod:@{a : @\"b\"}];\n" 17762 "}", 17763 AllmanBraceStyle); 17764 verifyFormat("int f()\n" 17765 "{ // comment\n" 17766 " return 42;\n" 17767 "}", 17768 AllmanBraceStyle); 17769 17770 AllmanBraceStyle.ColumnLimit = 19; 17771 verifyFormat("void f() { int i; }", AllmanBraceStyle); 17772 AllmanBraceStyle.ColumnLimit = 18; 17773 verifyFormat("void f()\n" 17774 "{\n" 17775 " int i;\n" 17776 "}", 17777 AllmanBraceStyle); 17778 AllmanBraceStyle.ColumnLimit = 80; 17779 17780 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 17781 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17782 FormatStyle::SIS_WithoutElse; 17783 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17784 verifyFormat("void f(bool b)\n" 17785 "{\n" 17786 " if (b)\n" 17787 " {\n" 17788 " return;\n" 17789 " }\n" 17790 "}\n", 17791 BreakBeforeBraceShortIfs); 17792 verifyFormat("void f(bool b)\n" 17793 "{\n" 17794 " if constexpr (b)\n" 17795 " {\n" 17796 " return;\n" 17797 " }\n" 17798 "}\n", 17799 BreakBeforeBraceShortIfs); 17800 verifyFormat("void f(bool b)\n" 17801 "{\n" 17802 " if CONSTEXPR (b)\n" 17803 " {\n" 17804 " return;\n" 17805 " }\n" 17806 "}\n", 17807 BreakBeforeBraceShortIfs); 17808 verifyFormat("void f(bool b)\n" 17809 "{\n" 17810 " if (b) return;\n" 17811 "}\n", 17812 BreakBeforeBraceShortIfs); 17813 verifyFormat("void f(bool b)\n" 17814 "{\n" 17815 " if constexpr (b) return;\n" 17816 "}\n", 17817 BreakBeforeBraceShortIfs); 17818 verifyFormat("void f(bool b)\n" 17819 "{\n" 17820 " if CONSTEXPR (b) return;\n" 17821 "}\n", 17822 BreakBeforeBraceShortIfs); 17823 verifyFormat("void f(bool b)\n" 17824 "{\n" 17825 " while (b)\n" 17826 " {\n" 17827 " return;\n" 17828 " }\n" 17829 "}\n", 17830 BreakBeforeBraceShortIfs); 17831 } 17832 17833 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 17834 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0); 17835 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 17836 17837 // Make a few changes to the style for testing purposes 17838 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 17839 FormatStyle::SFS_Empty; 17840 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17841 17842 // FIXME: this test case can't decide whether there should be a blank line 17843 // after the ~D() line or not. It adds one if one doesn't exist in the test 17844 // and it removes the line if one exists. 17845 /* 17846 verifyFormat("class A;\n" 17847 "namespace B\n" 17848 " {\n" 17849 "class C;\n" 17850 "// Comment\n" 17851 "class D\n" 17852 " {\n" 17853 "public:\n" 17854 " D();\n" 17855 " ~D() {}\n" 17856 "private:\n" 17857 " enum E\n" 17858 " {\n" 17859 " F\n" 17860 " }\n" 17861 " };\n" 17862 " } // namespace B\n", 17863 WhitesmithsBraceStyle); 17864 */ 17865 17866 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17867 verifyFormat("namespace a\n" 17868 " {\n" 17869 "class A\n" 17870 " {\n" 17871 " void f()\n" 17872 " {\n" 17873 " if (true)\n" 17874 " {\n" 17875 " a();\n" 17876 " b();\n" 17877 " }\n" 17878 " }\n" 17879 " void g()\n" 17880 " {\n" 17881 " return;\n" 17882 " }\n" 17883 " };\n" 17884 "struct B\n" 17885 " {\n" 17886 " int x;\n" 17887 " };\n" 17888 " } // namespace a", 17889 WhitesmithsBraceStyle); 17890 17891 verifyFormat("namespace a\n" 17892 " {\n" 17893 "namespace b\n" 17894 " {\n" 17895 "class A\n" 17896 " {\n" 17897 " void f()\n" 17898 " {\n" 17899 " if (true)\n" 17900 " {\n" 17901 " a();\n" 17902 " b();\n" 17903 " }\n" 17904 " }\n" 17905 " void g()\n" 17906 " {\n" 17907 " return;\n" 17908 " }\n" 17909 " };\n" 17910 "struct B\n" 17911 " {\n" 17912 " int x;\n" 17913 " };\n" 17914 " } // namespace b\n" 17915 " } // namespace a", 17916 WhitesmithsBraceStyle); 17917 17918 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17919 verifyFormat("namespace a\n" 17920 " {\n" 17921 "namespace b\n" 17922 " {\n" 17923 " class A\n" 17924 " {\n" 17925 " void f()\n" 17926 " {\n" 17927 " if (true)\n" 17928 " {\n" 17929 " a();\n" 17930 " b();\n" 17931 " }\n" 17932 " }\n" 17933 " void g()\n" 17934 " {\n" 17935 " return;\n" 17936 " }\n" 17937 " };\n" 17938 " struct B\n" 17939 " {\n" 17940 " int x;\n" 17941 " };\n" 17942 " } // namespace b\n" 17943 " } // namespace a", 17944 WhitesmithsBraceStyle); 17945 17946 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17947 verifyFormat("namespace a\n" 17948 " {\n" 17949 " namespace b\n" 17950 " {\n" 17951 " class A\n" 17952 " {\n" 17953 " void f()\n" 17954 " {\n" 17955 " if (true)\n" 17956 " {\n" 17957 " a();\n" 17958 " b();\n" 17959 " }\n" 17960 " }\n" 17961 " void g()\n" 17962 " {\n" 17963 " return;\n" 17964 " }\n" 17965 " };\n" 17966 " struct B\n" 17967 " {\n" 17968 " int x;\n" 17969 " };\n" 17970 " } // namespace b\n" 17971 " } // namespace a", 17972 WhitesmithsBraceStyle); 17973 17974 verifyFormat("void f()\n" 17975 " {\n" 17976 " if (true)\n" 17977 " {\n" 17978 " a();\n" 17979 " }\n" 17980 " else if (false)\n" 17981 " {\n" 17982 " b();\n" 17983 " }\n" 17984 " else\n" 17985 " {\n" 17986 " c();\n" 17987 " }\n" 17988 " }\n", 17989 WhitesmithsBraceStyle); 17990 17991 verifyFormat("void f()\n" 17992 " {\n" 17993 " for (int i = 0; i < 10; ++i)\n" 17994 " {\n" 17995 " a();\n" 17996 " }\n" 17997 " while (false)\n" 17998 " {\n" 17999 " b();\n" 18000 " }\n" 18001 " do\n" 18002 " {\n" 18003 " c();\n" 18004 " } while (false)\n" 18005 " }\n", 18006 WhitesmithsBraceStyle); 18007 18008 WhitesmithsBraceStyle.IndentCaseLabels = true; 18009 verifyFormat("void switchTest1(int a)\n" 18010 " {\n" 18011 " switch (a)\n" 18012 " {\n" 18013 " case 2:\n" 18014 " {\n" 18015 " }\n" 18016 " break;\n" 18017 " }\n" 18018 " }\n", 18019 WhitesmithsBraceStyle); 18020 18021 verifyFormat("void switchTest2(int a)\n" 18022 " {\n" 18023 " switch (a)\n" 18024 " {\n" 18025 " case 0:\n" 18026 " break;\n" 18027 " case 1:\n" 18028 " {\n" 18029 " break;\n" 18030 " }\n" 18031 " case 2:\n" 18032 " {\n" 18033 " }\n" 18034 " break;\n" 18035 " default:\n" 18036 " break;\n" 18037 " }\n" 18038 " }\n", 18039 WhitesmithsBraceStyle); 18040 18041 verifyFormat("void switchTest3(int a)\n" 18042 " {\n" 18043 " switch (a)\n" 18044 " {\n" 18045 " case 0:\n" 18046 " {\n" 18047 " foo(x);\n" 18048 " }\n" 18049 " break;\n" 18050 " default:\n" 18051 " {\n" 18052 " foo(1);\n" 18053 " }\n" 18054 " break;\n" 18055 " }\n" 18056 " }\n", 18057 WhitesmithsBraceStyle); 18058 18059 WhitesmithsBraceStyle.IndentCaseLabels = false; 18060 18061 verifyFormat("void switchTest4(int a)\n" 18062 " {\n" 18063 " switch (a)\n" 18064 " {\n" 18065 " case 2:\n" 18066 " {\n" 18067 " }\n" 18068 " break;\n" 18069 " }\n" 18070 " }\n", 18071 WhitesmithsBraceStyle); 18072 18073 verifyFormat("void switchTest5(int a)\n" 18074 " {\n" 18075 " switch (a)\n" 18076 " {\n" 18077 " case 0:\n" 18078 " break;\n" 18079 " case 1:\n" 18080 " {\n" 18081 " foo();\n" 18082 " break;\n" 18083 " }\n" 18084 " case 2:\n" 18085 " {\n" 18086 " }\n" 18087 " break;\n" 18088 " default:\n" 18089 " break;\n" 18090 " }\n" 18091 " }\n", 18092 WhitesmithsBraceStyle); 18093 18094 verifyFormat("void switchTest6(int a)\n" 18095 " {\n" 18096 " switch (a)\n" 18097 " {\n" 18098 " case 0:\n" 18099 " {\n" 18100 " foo(x);\n" 18101 " }\n" 18102 " break;\n" 18103 " default:\n" 18104 " {\n" 18105 " foo(1);\n" 18106 " }\n" 18107 " break;\n" 18108 " }\n" 18109 " }\n", 18110 WhitesmithsBraceStyle); 18111 18112 verifyFormat("enum X\n" 18113 " {\n" 18114 " Y = 0, // testing\n" 18115 " }\n", 18116 WhitesmithsBraceStyle); 18117 18118 verifyFormat("enum X\n" 18119 " {\n" 18120 " Y = 0\n" 18121 " }\n", 18122 WhitesmithsBraceStyle); 18123 verifyFormat("enum X\n" 18124 " {\n" 18125 " Y = 0,\n" 18126 " Z = 1\n" 18127 " };\n", 18128 WhitesmithsBraceStyle); 18129 18130 verifyFormat("@interface BSApplicationController ()\n" 18131 " {\n" 18132 "@private\n" 18133 " id _extraIvar;\n" 18134 " }\n" 18135 "@end\n", 18136 WhitesmithsBraceStyle); 18137 18138 verifyFormat("#ifdef _DEBUG\n" 18139 "int foo(int i = 0)\n" 18140 "#else\n" 18141 "int foo(int i = 5)\n" 18142 "#endif\n" 18143 " {\n" 18144 " return i;\n" 18145 " }", 18146 WhitesmithsBraceStyle); 18147 18148 verifyFormat("void foo() {}\n" 18149 "void bar()\n" 18150 "#ifdef _DEBUG\n" 18151 " {\n" 18152 " foo();\n" 18153 " }\n" 18154 "#else\n" 18155 " {\n" 18156 " }\n" 18157 "#endif", 18158 WhitesmithsBraceStyle); 18159 18160 verifyFormat("void foobar()\n" 18161 " {\n" 18162 " int i = 5;\n" 18163 " }\n" 18164 "#ifdef _DEBUG\n" 18165 "void bar()\n" 18166 " {\n" 18167 " }\n" 18168 "#else\n" 18169 "void bar()\n" 18170 " {\n" 18171 " foobar();\n" 18172 " }\n" 18173 "#endif", 18174 WhitesmithsBraceStyle); 18175 18176 // This shouldn't affect ObjC blocks.. 18177 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 18178 " // ...\n" 18179 " int i;\n" 18180 "}];", 18181 WhitesmithsBraceStyle); 18182 verifyFormat("void (^block)(void) = ^{\n" 18183 " // ...\n" 18184 " int i;\n" 18185 "};", 18186 WhitesmithsBraceStyle); 18187 // .. or dict literals. 18188 verifyFormat("void f()\n" 18189 " {\n" 18190 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 18191 " }", 18192 WhitesmithsBraceStyle); 18193 18194 verifyFormat("int f()\n" 18195 " { // comment\n" 18196 " return 42;\n" 18197 " }", 18198 WhitesmithsBraceStyle); 18199 18200 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 18201 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 18202 FormatStyle::SIS_OnlyFirstIf; 18203 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 18204 verifyFormat("void f(bool b)\n" 18205 " {\n" 18206 " if (b)\n" 18207 " {\n" 18208 " return;\n" 18209 " }\n" 18210 " }\n", 18211 BreakBeforeBraceShortIfs); 18212 verifyFormat("void f(bool b)\n" 18213 " {\n" 18214 " if (b) return;\n" 18215 " }\n", 18216 BreakBeforeBraceShortIfs); 18217 verifyFormat("void f(bool b)\n" 18218 " {\n" 18219 " while (b)\n" 18220 " {\n" 18221 " return;\n" 18222 " }\n" 18223 " }\n", 18224 BreakBeforeBraceShortIfs); 18225 } 18226 18227 TEST_F(FormatTest, GNUBraceBreaking) { 18228 FormatStyle GNUBraceStyle = getLLVMStyle(); 18229 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 18230 verifyFormat("namespace a\n" 18231 "{\n" 18232 "class A\n" 18233 "{\n" 18234 " void f()\n" 18235 " {\n" 18236 " int a;\n" 18237 " {\n" 18238 " int b;\n" 18239 " }\n" 18240 " if (true)\n" 18241 " {\n" 18242 " a();\n" 18243 " b();\n" 18244 " }\n" 18245 " }\n" 18246 " void g() { return; }\n" 18247 "}\n" 18248 "} // namespace a", 18249 GNUBraceStyle); 18250 18251 verifyFormat("void f()\n" 18252 "{\n" 18253 " if (true)\n" 18254 " {\n" 18255 " a();\n" 18256 " }\n" 18257 " else if (false)\n" 18258 " {\n" 18259 " b();\n" 18260 " }\n" 18261 " else\n" 18262 " {\n" 18263 " c();\n" 18264 " }\n" 18265 "}\n", 18266 GNUBraceStyle); 18267 18268 verifyFormat("void f()\n" 18269 "{\n" 18270 " for (int i = 0; i < 10; ++i)\n" 18271 " {\n" 18272 " a();\n" 18273 " }\n" 18274 " while (false)\n" 18275 " {\n" 18276 " b();\n" 18277 " }\n" 18278 " do\n" 18279 " {\n" 18280 " c();\n" 18281 " }\n" 18282 " while (false);\n" 18283 "}\n", 18284 GNUBraceStyle); 18285 18286 verifyFormat("void f(int a)\n" 18287 "{\n" 18288 " switch (a)\n" 18289 " {\n" 18290 " case 0:\n" 18291 " break;\n" 18292 " case 1:\n" 18293 " {\n" 18294 " break;\n" 18295 " }\n" 18296 " case 2:\n" 18297 " {\n" 18298 " }\n" 18299 " break;\n" 18300 " default:\n" 18301 " break;\n" 18302 " }\n" 18303 "}\n", 18304 GNUBraceStyle); 18305 18306 verifyFormat("enum X\n" 18307 "{\n" 18308 " Y = 0,\n" 18309 "}\n", 18310 GNUBraceStyle); 18311 18312 verifyFormat("@interface BSApplicationController ()\n" 18313 "{\n" 18314 "@private\n" 18315 " id _extraIvar;\n" 18316 "}\n" 18317 "@end\n", 18318 GNUBraceStyle); 18319 18320 verifyFormat("#ifdef _DEBUG\n" 18321 "int foo(int i = 0)\n" 18322 "#else\n" 18323 "int foo(int i = 5)\n" 18324 "#endif\n" 18325 "{\n" 18326 " return i;\n" 18327 "}", 18328 GNUBraceStyle); 18329 18330 verifyFormat("void foo() {}\n" 18331 "void bar()\n" 18332 "#ifdef _DEBUG\n" 18333 "{\n" 18334 " foo();\n" 18335 "}\n" 18336 "#else\n" 18337 "{\n" 18338 "}\n" 18339 "#endif", 18340 GNUBraceStyle); 18341 18342 verifyFormat("void foobar() { int i = 5; }\n" 18343 "#ifdef _DEBUG\n" 18344 "void bar() {}\n" 18345 "#else\n" 18346 "void bar() { foobar(); }\n" 18347 "#endif", 18348 GNUBraceStyle); 18349 } 18350 18351 TEST_F(FormatTest, WebKitBraceBreaking) { 18352 FormatStyle WebKitBraceStyle = getLLVMStyle(); 18353 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 18354 WebKitBraceStyle.FixNamespaceComments = false; 18355 verifyFormat("namespace a {\n" 18356 "class A {\n" 18357 " void f()\n" 18358 " {\n" 18359 " if (true) {\n" 18360 " a();\n" 18361 " b();\n" 18362 " }\n" 18363 " }\n" 18364 " void g() { return; }\n" 18365 "};\n" 18366 "enum E {\n" 18367 " A,\n" 18368 " // foo\n" 18369 " B,\n" 18370 " C\n" 18371 "};\n" 18372 "struct B {\n" 18373 " int x;\n" 18374 "};\n" 18375 "}\n", 18376 WebKitBraceStyle); 18377 verifyFormat("struct S {\n" 18378 " int Type;\n" 18379 " union {\n" 18380 " int x;\n" 18381 " double y;\n" 18382 " } Value;\n" 18383 " class C {\n" 18384 " MyFavoriteType Value;\n" 18385 " } Class;\n" 18386 "};\n", 18387 WebKitBraceStyle); 18388 } 18389 18390 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 18391 verifyFormat("void f() {\n" 18392 " try {\n" 18393 " } catch (const Exception &e) {\n" 18394 " }\n" 18395 "}\n", 18396 getLLVMStyle()); 18397 } 18398 18399 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 18400 auto Style = getLLVMStyle(); 18401 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18402 Style.AlignConsecutiveAssignments = 18403 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18404 Style.AlignConsecutiveDeclarations = 18405 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18406 verifyFormat("struct test demo[] = {\n" 18407 " {56, 23, \"hello\"},\n" 18408 " {-1, 93463, \"world\"},\n" 18409 " { 7, 5, \"!!\"}\n" 18410 "};\n", 18411 Style); 18412 18413 verifyFormat("struct test demo[] = {\n" 18414 " {56, 23, \"hello\"}, // first line\n" 18415 " {-1, 93463, \"world\"}, // second line\n" 18416 " { 7, 5, \"!!\"} // third line\n" 18417 "};\n", 18418 Style); 18419 18420 verifyFormat("struct test demo[4] = {\n" 18421 " { 56, 23, 21, \"oh\"}, // first line\n" 18422 " { -1, 93463, 22, \"my\"}, // second line\n" 18423 " { 7, 5, 1, \"goodness\"} // third line\n" 18424 " {234, 5, 1, \"gracious\"} // fourth line\n" 18425 "};\n", 18426 Style); 18427 18428 verifyFormat("struct test demo[3] = {\n" 18429 " {56, 23, \"hello\"},\n" 18430 " {-1, 93463, \"world\"},\n" 18431 " { 7, 5, \"!!\"}\n" 18432 "};\n", 18433 Style); 18434 18435 verifyFormat("struct test demo[3] = {\n" 18436 " {int{56}, 23, \"hello\"},\n" 18437 " {int{-1}, 93463, \"world\"},\n" 18438 " { int{7}, 5, \"!!\"}\n" 18439 "};\n", 18440 Style); 18441 18442 verifyFormat("struct test demo[] = {\n" 18443 " {56, 23, \"hello\"},\n" 18444 " {-1, 93463, \"world\"},\n" 18445 " { 7, 5, \"!!\"},\n" 18446 "};\n", 18447 Style); 18448 18449 verifyFormat("test demo[] = {\n" 18450 " {56, 23, \"hello\"},\n" 18451 " {-1, 93463, \"world\"},\n" 18452 " { 7, 5, \"!!\"},\n" 18453 "};\n", 18454 Style); 18455 18456 verifyFormat("demo = std::array<struct test, 3>{\n" 18457 " test{56, 23, \"hello\"},\n" 18458 " test{-1, 93463, \"world\"},\n" 18459 " test{ 7, 5, \"!!\"},\n" 18460 "};\n", 18461 Style); 18462 18463 verifyFormat("test demo[] = {\n" 18464 " {56, 23, \"hello\"},\n" 18465 "#if X\n" 18466 " {-1, 93463, \"world\"},\n" 18467 "#endif\n" 18468 " { 7, 5, \"!!\"}\n" 18469 "};\n", 18470 Style); 18471 18472 verifyFormat( 18473 "test demo[] = {\n" 18474 " { 7, 23,\n" 18475 " \"hello world i am a very long line that really, in any\"\n" 18476 " \"just world, ought to be split over multiple lines\"},\n" 18477 " {-1, 93463, \"world\"},\n" 18478 " {56, 5, \"!!\"}\n" 18479 "};\n", 18480 Style); 18481 18482 verifyFormat("return GradForUnaryCwise(g, {\n" 18483 " {{\"sign\"}, \"Sign\", " 18484 " {\"x\", \"dy\"}},\n" 18485 " { {\"dx\"}, \"Mul\", {\"dy\"" 18486 ", \"sign\"}},\n" 18487 "});\n", 18488 Style); 18489 18490 Style.ColumnLimit = 0; 18491 EXPECT_EQ( 18492 "test demo[] = {\n" 18493 " {56, 23, \"hello world i am a very long line that really, " 18494 "in any just world, ought to be split over multiple lines\"},\n" 18495 " {-1, 93463, " 18496 " \"world\"},\n" 18497 " { 7, 5, " 18498 " \"!!\"},\n" 18499 "};", 18500 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18501 "that really, in any just world, ought to be split over multiple " 18502 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18503 Style)); 18504 18505 Style.ColumnLimit = 80; 18506 verifyFormat("test demo[] = {\n" 18507 " {56, 23, /* a comment */ \"hello\"},\n" 18508 " {-1, 93463, \"world\"},\n" 18509 " { 7, 5, \"!!\"}\n" 18510 "};\n", 18511 Style); 18512 18513 verifyFormat("test demo[] = {\n" 18514 " {56, 23, \"hello\"},\n" 18515 " {-1, 93463, \"world\" /* comment here */},\n" 18516 " { 7, 5, \"!!\"}\n" 18517 "};\n", 18518 Style); 18519 18520 verifyFormat("test demo[] = {\n" 18521 " {56, /* a comment */ 23, \"hello\"},\n" 18522 " {-1, 93463, \"world\"},\n" 18523 " { 7, 5, \"!!\"}\n" 18524 "};\n", 18525 Style); 18526 18527 Style.ColumnLimit = 20; 18528 EXPECT_EQ( 18529 "demo = std::array<\n" 18530 " struct test, 3>{\n" 18531 " test{\n" 18532 " 56, 23,\n" 18533 " \"hello \"\n" 18534 " \"world i \"\n" 18535 " \"am a very \"\n" 18536 " \"long line \"\n" 18537 " \"that \"\n" 18538 " \"really, \"\n" 18539 " \"in any \"\n" 18540 " \"just \"\n" 18541 " \"world, \"\n" 18542 " \"ought to \"\n" 18543 " \"be split \"\n" 18544 " \"over \"\n" 18545 " \"multiple \"\n" 18546 " \"lines\"},\n" 18547 " test{-1, 93463,\n" 18548 " \"world\"},\n" 18549 " test{ 7, 5,\n" 18550 " \"!!\" },\n" 18551 "};", 18552 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18553 "i am a very long line that really, in any just world, ought " 18554 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18555 "test{7, 5, \"!!\"},};", 18556 Style)); 18557 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18558 Style = getLLVMStyleWithColumns(50); 18559 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18560 verifyFormat("static A x = {\n" 18561 " {{init1, init2, init3, init4},\n" 18562 " {init1, init2, init3, init4}}\n" 18563 "};", 18564 Style); 18565 Style.ColumnLimit = 100; 18566 EXPECT_EQ( 18567 "test demo[] = {\n" 18568 " {56, 23,\n" 18569 " \"hello world i am a very long line that really, in any just world" 18570 ", ought to be split over \"\n" 18571 " \"multiple lines\" },\n" 18572 " {-1, 93463, \"world\"},\n" 18573 " { 7, 5, \"!!\"},\n" 18574 "};", 18575 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18576 "that really, in any just world, ought to be split over multiple " 18577 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18578 Style)); 18579 18580 Style = getLLVMStyleWithColumns(50); 18581 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18582 Style.AlignConsecutiveAssignments = 18583 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18584 Style.AlignConsecutiveDeclarations = 18585 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18586 verifyFormat("struct test demo[] = {\n" 18587 " {56, 23, \"hello\"},\n" 18588 " {-1, 93463, \"world\"},\n" 18589 " { 7, 5, \"!!\"}\n" 18590 "};\n" 18591 "static A x = {\n" 18592 " {{init1, init2, init3, init4},\n" 18593 " {init1, init2, init3, init4}}\n" 18594 "};", 18595 Style); 18596 Style.ColumnLimit = 100; 18597 Style.AlignConsecutiveAssignments = 18598 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18599 Style.AlignConsecutiveDeclarations = 18600 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18601 verifyFormat("struct test demo[] = {\n" 18602 " {56, 23, \"hello\"},\n" 18603 " {-1, 93463, \"world\"},\n" 18604 " { 7, 5, \"!!\"}\n" 18605 "};\n" 18606 "struct test demo[4] = {\n" 18607 " { 56, 23, 21, \"oh\"}, // first line\n" 18608 " { -1, 93463, 22, \"my\"}, // second line\n" 18609 " { 7, 5, 1, \"goodness\"} // third line\n" 18610 " {234, 5, 1, \"gracious\"} // fourth line\n" 18611 "};\n", 18612 Style); 18613 EXPECT_EQ( 18614 "test demo[] = {\n" 18615 " {56,\n" 18616 " \"hello world i am a very long line that really, in any just world" 18617 ", ought to be split over \"\n" 18618 " \"multiple lines\", 23},\n" 18619 " {-1, \"world\", 93463},\n" 18620 " { 7, \"!!\", 5},\n" 18621 "};", 18622 format("test demo[] = {{56, \"hello world i am a very long line " 18623 "that really, in any just world, ought to be split over multiple " 18624 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 18625 Style)); 18626 } 18627 18628 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 18629 auto Style = getLLVMStyle(); 18630 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18631 /* FIXME: This case gets misformatted. 18632 verifyFormat("auto foo = Items{\n" 18633 " Section{0, bar(), },\n" 18634 " Section{1, boo() }\n" 18635 "};\n", 18636 Style); 18637 */ 18638 verifyFormat("auto foo = Items{\n" 18639 " Section{\n" 18640 " 0, bar(),\n" 18641 " }\n" 18642 "};\n", 18643 Style); 18644 verifyFormat("struct test demo[] = {\n" 18645 " {56, 23, \"hello\"},\n" 18646 " {-1, 93463, \"world\"},\n" 18647 " {7, 5, \"!!\" }\n" 18648 "};\n", 18649 Style); 18650 verifyFormat("struct test demo[] = {\n" 18651 " {56, 23, \"hello\"}, // first line\n" 18652 " {-1, 93463, \"world\"}, // second line\n" 18653 " {7, 5, \"!!\" } // third line\n" 18654 "};\n", 18655 Style); 18656 verifyFormat("struct test demo[4] = {\n" 18657 " {56, 23, 21, \"oh\" }, // first line\n" 18658 " {-1, 93463, 22, \"my\" }, // second line\n" 18659 " {7, 5, 1, \"goodness\"} // third line\n" 18660 " {234, 5, 1, \"gracious\"} // fourth line\n" 18661 "};\n", 18662 Style); 18663 verifyFormat("struct test demo[3] = {\n" 18664 " {56, 23, \"hello\"},\n" 18665 " {-1, 93463, \"world\"},\n" 18666 " {7, 5, \"!!\" }\n" 18667 "};\n", 18668 Style); 18669 18670 verifyFormat("struct test demo[3] = {\n" 18671 " {int{56}, 23, \"hello\"},\n" 18672 " {int{-1}, 93463, \"world\"},\n" 18673 " {int{7}, 5, \"!!\" }\n" 18674 "};\n", 18675 Style); 18676 verifyFormat("struct test demo[] = {\n" 18677 " {56, 23, \"hello\"},\n" 18678 " {-1, 93463, \"world\"},\n" 18679 " {7, 5, \"!!\" },\n" 18680 "};\n", 18681 Style); 18682 verifyFormat("test demo[] = {\n" 18683 " {56, 23, \"hello\"},\n" 18684 " {-1, 93463, \"world\"},\n" 18685 " {7, 5, \"!!\" },\n" 18686 "};\n", 18687 Style); 18688 verifyFormat("demo = std::array<struct test, 3>{\n" 18689 " test{56, 23, \"hello\"},\n" 18690 " test{-1, 93463, \"world\"},\n" 18691 " test{7, 5, \"!!\" },\n" 18692 "};\n", 18693 Style); 18694 verifyFormat("test demo[] = {\n" 18695 " {56, 23, \"hello\"},\n" 18696 "#if X\n" 18697 " {-1, 93463, \"world\"},\n" 18698 "#endif\n" 18699 " {7, 5, \"!!\" }\n" 18700 "};\n", 18701 Style); 18702 verifyFormat( 18703 "test demo[] = {\n" 18704 " {7, 23,\n" 18705 " \"hello world i am a very long line that really, in any\"\n" 18706 " \"just world, ought to be split over multiple lines\"},\n" 18707 " {-1, 93463, \"world\" },\n" 18708 " {56, 5, \"!!\" }\n" 18709 "};\n", 18710 Style); 18711 18712 verifyFormat("return GradForUnaryCwise(g, {\n" 18713 " {{\"sign\"}, \"Sign\", {\"x\", " 18714 "\"dy\"} },\n" 18715 " {{\"dx\"}, \"Mul\", " 18716 "{\"dy\", \"sign\"}},\n" 18717 "});\n", 18718 Style); 18719 18720 Style.ColumnLimit = 0; 18721 EXPECT_EQ( 18722 "test demo[] = {\n" 18723 " {56, 23, \"hello world i am a very long line that really, in any " 18724 "just world, ought to be split over multiple lines\"},\n" 18725 " {-1, 93463, \"world\" " 18726 " },\n" 18727 " {7, 5, \"!!\" " 18728 " },\n" 18729 "};", 18730 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18731 "that really, in any just world, ought to be split over multiple " 18732 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18733 Style)); 18734 18735 Style.ColumnLimit = 80; 18736 verifyFormat("test demo[] = {\n" 18737 " {56, 23, /* a comment */ \"hello\"},\n" 18738 " {-1, 93463, \"world\" },\n" 18739 " {7, 5, \"!!\" }\n" 18740 "};\n", 18741 Style); 18742 18743 verifyFormat("test demo[] = {\n" 18744 " {56, 23, \"hello\" },\n" 18745 " {-1, 93463, \"world\" /* comment here */},\n" 18746 " {7, 5, \"!!\" }\n" 18747 "};\n", 18748 Style); 18749 18750 verifyFormat("test demo[] = {\n" 18751 " {56, /* a comment */ 23, \"hello\"},\n" 18752 " {-1, 93463, \"world\"},\n" 18753 " {7, 5, \"!!\" }\n" 18754 "};\n", 18755 Style); 18756 18757 Style.ColumnLimit = 20; 18758 EXPECT_EQ( 18759 "demo = std::array<\n" 18760 " struct test, 3>{\n" 18761 " test{\n" 18762 " 56, 23,\n" 18763 " \"hello \"\n" 18764 " \"world i \"\n" 18765 " \"am a very \"\n" 18766 " \"long line \"\n" 18767 " \"that \"\n" 18768 " \"really, \"\n" 18769 " \"in any \"\n" 18770 " \"just \"\n" 18771 " \"world, \"\n" 18772 " \"ought to \"\n" 18773 " \"be split \"\n" 18774 " \"over \"\n" 18775 " \"multiple \"\n" 18776 " \"lines\"},\n" 18777 " test{-1, 93463,\n" 18778 " \"world\"},\n" 18779 " test{7, 5,\n" 18780 " \"!!\" },\n" 18781 "};", 18782 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18783 "i am a very long line that really, in any just world, ought " 18784 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18785 "test{7, 5, \"!!\"},};", 18786 Style)); 18787 18788 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18789 Style = getLLVMStyleWithColumns(50); 18790 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18791 verifyFormat("static A x = {\n" 18792 " {{init1, init2, init3, init4},\n" 18793 " {init1, init2, init3, init4}}\n" 18794 "};", 18795 Style); 18796 Style.ColumnLimit = 100; 18797 EXPECT_EQ( 18798 "test demo[] = {\n" 18799 " {56, 23,\n" 18800 " \"hello world i am a very long line that really, in any just world" 18801 ", ought to be split over \"\n" 18802 " \"multiple lines\" },\n" 18803 " {-1, 93463, \"world\"},\n" 18804 " {7, 5, \"!!\" },\n" 18805 "};", 18806 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18807 "that really, in any just world, ought to be split over multiple " 18808 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18809 Style)); 18810 } 18811 18812 TEST_F(FormatTest, UnderstandsPragmas) { 18813 verifyFormat("#pragma omp reduction(| : var)"); 18814 verifyFormat("#pragma omp reduction(+ : var)"); 18815 18816 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 18817 "(including parentheses).", 18818 format("#pragma mark Any non-hyphenated or hyphenated string " 18819 "(including parentheses).")); 18820 } 18821 18822 TEST_F(FormatTest, UnderstandPragmaOption) { 18823 verifyFormat("#pragma option -C -A"); 18824 18825 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 18826 } 18827 18828 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 18829 FormatStyle Style = getLLVMStyleWithColumns(20); 18830 18831 // See PR41213 18832 EXPECT_EQ("/*\n" 18833 " *\t9012345\n" 18834 " * /8901\n" 18835 " */", 18836 format("/*\n" 18837 " *\t9012345 /8901\n" 18838 " */", 18839 Style)); 18840 EXPECT_EQ("/*\n" 18841 " *345678\n" 18842 " *\t/8901\n" 18843 " */", 18844 format("/*\n" 18845 " *345678\t/8901\n" 18846 " */", 18847 Style)); 18848 18849 verifyFormat("int a; // the\n" 18850 " // comment", 18851 Style); 18852 EXPECT_EQ("int a; /* first line\n" 18853 " * second\n" 18854 " * line third\n" 18855 " * line\n" 18856 " */", 18857 format("int a; /* first line\n" 18858 " * second\n" 18859 " * line third\n" 18860 " * line\n" 18861 " */", 18862 Style)); 18863 EXPECT_EQ("int a; // first line\n" 18864 " // second\n" 18865 " // line third\n" 18866 " // line", 18867 format("int a; // first line\n" 18868 " // second line\n" 18869 " // third line", 18870 Style)); 18871 18872 Style.PenaltyExcessCharacter = 90; 18873 verifyFormat("int a; // the comment", Style); 18874 EXPECT_EQ("int a; // the comment\n" 18875 " // aaa", 18876 format("int a; // the comment aaa", Style)); 18877 EXPECT_EQ("int a; /* first line\n" 18878 " * second line\n" 18879 " * third line\n" 18880 " */", 18881 format("int a; /* first line\n" 18882 " * second line\n" 18883 " * third line\n" 18884 " */", 18885 Style)); 18886 EXPECT_EQ("int a; // first line\n" 18887 " // second line\n" 18888 " // third line", 18889 format("int a; // first line\n" 18890 " // second line\n" 18891 " // third line", 18892 Style)); 18893 // FIXME: Investigate why this is not getting the same layout as the test 18894 // above. 18895 EXPECT_EQ("int a; /* first line\n" 18896 " * second line\n" 18897 " * third line\n" 18898 " */", 18899 format("int a; /* first line second line third line" 18900 "\n*/", 18901 Style)); 18902 18903 EXPECT_EQ("// foo bar baz bazfoo\n" 18904 "// foo bar foo bar\n", 18905 format("// foo bar baz bazfoo\n" 18906 "// foo bar foo bar\n", 18907 Style)); 18908 EXPECT_EQ("// foo bar baz bazfoo\n" 18909 "// foo bar foo bar\n", 18910 format("// foo bar baz bazfoo\n" 18911 "// foo bar foo bar\n", 18912 Style)); 18913 18914 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18915 // next one. 18916 EXPECT_EQ("// foo bar baz bazfoo\n" 18917 "// bar foo bar\n", 18918 format("// foo bar baz bazfoo bar\n" 18919 "// foo bar\n", 18920 Style)); 18921 18922 EXPECT_EQ("// foo bar baz bazfoo\n" 18923 "// foo bar baz bazfoo\n" 18924 "// bar foo bar\n", 18925 format("// foo bar baz bazfoo\n" 18926 "// foo bar baz bazfoo bar\n" 18927 "// foo bar\n", 18928 Style)); 18929 18930 EXPECT_EQ("// foo bar baz bazfoo\n" 18931 "// foo bar baz bazfoo\n" 18932 "// bar foo bar\n", 18933 format("// foo bar baz bazfoo\n" 18934 "// foo bar baz bazfoo bar\n" 18935 "// foo bar\n", 18936 Style)); 18937 18938 // Make sure we do not keep protruding characters if strict mode reflow is 18939 // cheaper than keeping protruding characters. 18940 Style.ColumnLimit = 21; 18941 EXPECT_EQ( 18942 "// foo foo foo foo\n" 18943 "// foo foo foo foo\n" 18944 "// foo foo foo foo\n", 18945 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18946 18947 EXPECT_EQ("int a = /* long block\n" 18948 " comment */\n" 18949 " 42;", 18950 format("int a = /* long block comment */ 42;", Style)); 18951 } 18952 18953 TEST_F(FormatTest, BreakPenaltyAfterLParen) { 18954 FormatStyle Style = getLLVMStyle(); 18955 Style.ColumnLimit = 8; 18956 Style.PenaltyExcessCharacter = 15; 18957 verifyFormat("int foo(\n" 18958 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 18959 Style); 18960 Style.PenaltyBreakOpenParenthesis = 200; 18961 EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", 18962 format("int foo(\n" 18963 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 18964 Style)); 18965 } 18966 18967 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { 18968 FormatStyle Style = getLLVMStyle(); 18969 Style.ColumnLimit = 5; 18970 Style.PenaltyExcessCharacter = 150; 18971 verifyFormat("foo((\n" 18972 " int)aaaaaaaaaaaaaaaaaaaaaaaa);", 18973 18974 Style); 18975 Style.PenaltyBreakOpenParenthesis = 100000; 18976 EXPECT_EQ("foo((int)\n" 18977 " aaaaaaaaaaaaaaaaaaaaaaaa);", 18978 format("foo((\n" 18979 "int)aaaaaaaaaaaaaaaaaaaaaaaa);", 18980 Style)); 18981 } 18982 18983 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { 18984 FormatStyle Style = getLLVMStyle(); 18985 Style.ColumnLimit = 4; 18986 Style.PenaltyExcessCharacter = 100; 18987 verifyFormat("for (\n" 18988 " int iiiiiiiiiiiiiiiii =\n" 18989 " 0;\n" 18990 " iiiiiiiiiiiiiiiii <\n" 18991 " 2;\n" 18992 " iiiiiiiiiiiiiiiii++) {\n" 18993 "}", 18994 18995 Style); 18996 Style.PenaltyBreakOpenParenthesis = 1250; 18997 EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n" 18998 " 0;\n" 18999 " iiiiiiiiiiiiiiiii <\n" 19000 " 2;\n" 19001 " iiiiiiiiiiiiiiiii++) {\n" 19002 "}", 19003 format("for (\n" 19004 " int iiiiiiiiiiiiiiiii =\n" 19005 " 0;\n" 19006 " iiiiiiiiiiiiiiiii <\n" 19007 " 2;\n" 19008 " iiiiiiiiiiiiiiiii++) {\n" 19009 "}", 19010 Style)); 19011 } 19012 19013 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 19014 for (size_t i = 1; i < Styles.size(); ++i) \ 19015 EXPECT_EQ(Styles[0], Styles[i]) \ 19016 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 19017 19018 TEST_F(FormatTest, GetsPredefinedStyleByName) { 19019 SmallVector<FormatStyle, 3> Styles; 19020 Styles.resize(3); 19021 19022 Styles[0] = getLLVMStyle(); 19023 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 19024 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 19025 EXPECT_ALL_STYLES_EQUAL(Styles); 19026 19027 Styles[0] = getGoogleStyle(); 19028 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 19029 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 19030 EXPECT_ALL_STYLES_EQUAL(Styles); 19031 19032 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 19033 EXPECT_TRUE( 19034 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 19035 EXPECT_TRUE( 19036 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 19037 EXPECT_ALL_STYLES_EQUAL(Styles); 19038 19039 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 19040 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 19041 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 19042 EXPECT_ALL_STYLES_EQUAL(Styles); 19043 19044 Styles[0] = getMozillaStyle(); 19045 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 19046 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 19047 EXPECT_ALL_STYLES_EQUAL(Styles); 19048 19049 Styles[0] = getWebKitStyle(); 19050 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 19051 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 19052 EXPECT_ALL_STYLES_EQUAL(Styles); 19053 19054 Styles[0] = getGNUStyle(); 19055 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 19056 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 19057 EXPECT_ALL_STYLES_EQUAL(Styles); 19058 19059 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 19060 } 19061 19062 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 19063 SmallVector<FormatStyle, 8> Styles; 19064 Styles.resize(2); 19065 19066 Styles[0] = getGoogleStyle(); 19067 Styles[1] = getLLVMStyle(); 19068 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 19069 EXPECT_ALL_STYLES_EQUAL(Styles); 19070 19071 Styles.resize(5); 19072 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 19073 Styles[1] = getLLVMStyle(); 19074 Styles[1].Language = FormatStyle::LK_JavaScript; 19075 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 19076 19077 Styles[2] = getLLVMStyle(); 19078 Styles[2].Language = FormatStyle::LK_JavaScript; 19079 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 19080 "BasedOnStyle: Google", 19081 &Styles[2]) 19082 .value()); 19083 19084 Styles[3] = getLLVMStyle(); 19085 Styles[3].Language = FormatStyle::LK_JavaScript; 19086 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 19087 "Language: JavaScript", 19088 &Styles[3]) 19089 .value()); 19090 19091 Styles[4] = getLLVMStyle(); 19092 Styles[4].Language = FormatStyle::LK_JavaScript; 19093 EXPECT_EQ(0, parseConfiguration("---\n" 19094 "BasedOnStyle: LLVM\n" 19095 "IndentWidth: 123\n" 19096 "---\n" 19097 "BasedOnStyle: Google\n" 19098 "Language: JavaScript", 19099 &Styles[4]) 19100 .value()); 19101 EXPECT_ALL_STYLES_EQUAL(Styles); 19102 } 19103 19104 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 19105 Style.FIELD = false; \ 19106 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 19107 EXPECT_TRUE(Style.FIELD); \ 19108 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 19109 EXPECT_FALSE(Style.FIELD); 19110 19111 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 19112 19113 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 19114 Style.STRUCT.FIELD = false; \ 19115 EXPECT_EQ(0, \ 19116 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 19117 .value()); \ 19118 EXPECT_TRUE(Style.STRUCT.FIELD); \ 19119 EXPECT_EQ(0, \ 19120 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 19121 .value()); \ 19122 EXPECT_FALSE(Style.STRUCT.FIELD); 19123 19124 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 19125 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 19126 19127 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 19128 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 19129 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 19130 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 19131 19132 TEST_F(FormatTest, ParsesConfigurationBools) { 19133 FormatStyle Style = {}; 19134 Style.Language = FormatStyle::LK_Cpp; 19135 CHECK_PARSE_BOOL(AlignTrailingComments); 19136 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 19137 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 19138 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 19139 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 19140 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 19141 CHECK_PARSE_BOOL(BinPackArguments); 19142 CHECK_PARSE_BOOL(BinPackParameters); 19143 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 19144 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 19145 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 19146 CHECK_PARSE_BOOL(BreakStringLiterals); 19147 CHECK_PARSE_BOOL(CompactNamespaces); 19148 CHECK_PARSE_BOOL(DeriveLineEnding); 19149 CHECK_PARSE_BOOL(DerivePointerAlignment); 19150 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 19151 CHECK_PARSE_BOOL(DisableFormat); 19152 CHECK_PARSE_BOOL(IndentAccessModifiers); 19153 CHECK_PARSE_BOOL(IndentCaseLabels); 19154 CHECK_PARSE_BOOL(IndentCaseBlocks); 19155 CHECK_PARSE_BOOL(IndentGotoLabels); 19156 CHECK_PARSE_BOOL(IndentRequires); 19157 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 19158 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 19159 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 19160 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 19161 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 19162 CHECK_PARSE_BOOL(ReflowComments); 19163 CHECK_PARSE_BOOL(RemoveBracesLLVM); 19164 CHECK_PARSE_BOOL(SortUsingDeclarations); 19165 CHECK_PARSE_BOOL(SpacesInParentheses); 19166 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 19167 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 19168 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 19169 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 19170 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 19171 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 19172 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 19173 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 19174 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 19175 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 19176 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 19177 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 19178 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 19179 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 19180 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 19181 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 19182 CHECK_PARSE_BOOL(UseCRLF); 19183 19184 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 19185 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 19186 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 19187 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 19188 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 19189 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 19190 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 19191 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 19192 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 19193 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 19194 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 19195 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 19196 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 19197 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 19198 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 19199 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 19200 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 19201 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements); 19202 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros); 19203 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19204 AfterFunctionDeclarationName); 19205 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19206 AfterFunctionDefinitionName); 19207 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros); 19208 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator); 19209 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses); 19210 } 19211 19212 #undef CHECK_PARSE_BOOL 19213 19214 TEST_F(FormatTest, ParsesConfiguration) { 19215 FormatStyle Style = {}; 19216 Style.Language = FormatStyle::LK_Cpp; 19217 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 19218 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 19219 ConstructorInitializerIndentWidth, 1234u); 19220 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 19221 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 19222 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 19223 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 19224 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 19225 PenaltyBreakBeforeFirstCallParameter, 1234u); 19226 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 19227 PenaltyBreakTemplateDeclaration, 1234u); 19228 CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis, 19229 1234u); 19230 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 19231 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 19232 PenaltyReturnTypeOnItsOwnLine, 1234u); 19233 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 19234 SpacesBeforeTrailingComments, 1234u); 19235 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 19236 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 19237 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 19238 19239 Style.QualifierAlignment = FormatStyle::QAS_Right; 19240 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 19241 FormatStyle::QAS_Leave); 19242 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 19243 FormatStyle::QAS_Right); 19244 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 19245 FormatStyle::QAS_Left); 19246 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 19247 FormatStyle::QAS_Custom); 19248 19249 Style.QualifierOrder.clear(); 19250 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 19251 std::vector<std::string>({"const", "volatile", "type"})); 19252 Style.QualifierOrder.clear(); 19253 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 19254 std::vector<std::string>({"const", "type"})); 19255 Style.QualifierOrder.clear(); 19256 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 19257 std::vector<std::string>({"volatile", "type"})); 19258 19259 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 19260 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 19261 FormatStyle::ACS_None); 19262 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 19263 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 19264 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 19265 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 19266 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 19267 AlignConsecutiveAssignments, 19268 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19269 // For backwards compability, false / true should still parse 19270 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 19271 FormatStyle::ACS_None); 19272 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 19273 FormatStyle::ACS_Consecutive); 19274 19275 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 19276 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 19277 FormatStyle::ACS_None); 19278 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 19279 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 19280 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 19281 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 19282 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 19283 AlignConsecutiveBitFields, 19284 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19285 // For backwards compability, false / true should still parse 19286 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 19287 FormatStyle::ACS_None); 19288 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 19289 FormatStyle::ACS_Consecutive); 19290 19291 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 19292 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 19293 FormatStyle::ACS_None); 19294 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 19295 FormatStyle::ACS_Consecutive); 19296 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 19297 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 19298 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 19299 AlignConsecutiveMacros, 19300 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19301 // For backwards compability, false / true should still parse 19302 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 19303 FormatStyle::ACS_None); 19304 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 19305 FormatStyle::ACS_Consecutive); 19306 19307 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 19308 CHECK_PARSE("AlignConsecutiveDeclarations: None", 19309 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 19310 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 19311 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 19312 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 19313 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 19314 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 19315 AlignConsecutiveDeclarations, 19316 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19317 // For backwards compability, false / true should still parse 19318 CHECK_PARSE("AlignConsecutiveDeclarations: false", 19319 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 19320 CHECK_PARSE("AlignConsecutiveDeclarations: true", 19321 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 19322 19323 Style.PointerAlignment = FormatStyle::PAS_Middle; 19324 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 19325 FormatStyle::PAS_Left); 19326 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 19327 FormatStyle::PAS_Right); 19328 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 19329 FormatStyle::PAS_Middle); 19330 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 19331 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 19332 FormatStyle::RAS_Pointer); 19333 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 19334 FormatStyle::RAS_Left); 19335 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 19336 FormatStyle::RAS_Right); 19337 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 19338 FormatStyle::RAS_Middle); 19339 // For backward compatibility: 19340 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 19341 FormatStyle::PAS_Left); 19342 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 19343 FormatStyle::PAS_Right); 19344 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 19345 FormatStyle::PAS_Middle); 19346 19347 Style.Standard = FormatStyle::LS_Auto; 19348 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 19349 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 19350 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 19351 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 19352 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 19353 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 19354 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 19355 // Legacy aliases: 19356 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 19357 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 19358 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 19359 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 19360 19361 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 19362 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 19363 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 19364 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 19365 FormatStyle::BOS_None); 19366 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 19367 FormatStyle::BOS_All); 19368 // For backward compatibility: 19369 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 19370 FormatStyle::BOS_None); 19371 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 19372 FormatStyle::BOS_All); 19373 19374 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 19375 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 19376 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 19377 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 19378 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 19379 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 19380 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 19381 // For backward compatibility: 19382 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 19383 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 19384 19385 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 19386 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 19387 FormatStyle::BILS_AfterComma); 19388 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 19389 FormatStyle::BILS_BeforeComma); 19390 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 19391 FormatStyle::BILS_AfterColon); 19392 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 19393 FormatStyle::BILS_BeforeColon); 19394 // For backward compatibility: 19395 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 19396 FormatStyle::BILS_BeforeComma); 19397 19398 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19399 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 19400 FormatStyle::PCIS_Never); 19401 CHECK_PARSE("PackConstructorInitializers: BinPack", 19402 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19403 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 19404 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19405 CHECK_PARSE("PackConstructorInitializers: NextLine", 19406 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19407 // For backward compatibility: 19408 CHECK_PARSE("BasedOnStyle: Google\n" 19409 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19410 "AllowAllConstructorInitializersOnNextLine: false", 19411 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19412 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19413 CHECK_PARSE("BasedOnStyle: Google\n" 19414 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 19415 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19416 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19417 "AllowAllConstructorInitializersOnNextLine: true", 19418 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19419 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19420 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19421 "AllowAllConstructorInitializersOnNextLine: false", 19422 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19423 19424 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 19425 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 19426 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 19427 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 19428 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 19429 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 19430 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 19431 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 19432 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 19433 19434 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19435 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 19436 FormatStyle::BAS_Align); 19437 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 19438 FormatStyle::BAS_DontAlign); 19439 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 19440 FormatStyle::BAS_AlwaysBreak); 19441 CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket, 19442 FormatStyle::BAS_BlockIndent); 19443 // For backward compatibility: 19444 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 19445 FormatStyle::BAS_DontAlign); 19446 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 19447 FormatStyle::BAS_Align); 19448 19449 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 19450 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 19451 FormatStyle::ENAS_DontAlign); 19452 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 19453 FormatStyle::ENAS_Left); 19454 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 19455 FormatStyle::ENAS_Right); 19456 // For backward compatibility: 19457 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 19458 FormatStyle::ENAS_Left); 19459 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 19460 FormatStyle::ENAS_Right); 19461 19462 Style.AlignOperands = FormatStyle::OAS_Align; 19463 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 19464 FormatStyle::OAS_DontAlign); 19465 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 19466 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 19467 FormatStyle::OAS_AlignAfterOperator); 19468 // For backward compatibility: 19469 CHECK_PARSE("AlignOperands: false", AlignOperands, 19470 FormatStyle::OAS_DontAlign); 19471 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 19472 19473 Style.UseTab = FormatStyle::UT_ForIndentation; 19474 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 19475 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 19476 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 19477 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 19478 FormatStyle::UT_ForContinuationAndIndentation); 19479 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 19480 FormatStyle::UT_AlignWithSpaces); 19481 // For backward compatibility: 19482 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 19483 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 19484 19485 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 19486 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 19487 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19488 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 19489 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 19490 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 19491 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19492 // For backward compatibility: 19493 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 19494 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19495 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 19496 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19497 19498 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 19499 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 19500 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19501 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 19502 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 19503 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 19504 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 19505 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 19506 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19507 // For backward compatibility: 19508 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 19509 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19510 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 19511 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19512 19513 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 19514 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 19515 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 19516 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 19517 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 19518 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 19519 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 19520 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 19521 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 19522 19523 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 19524 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 19525 FormatStyle::SBPO_Never); 19526 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 19527 FormatStyle::SBPO_Always); 19528 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 19529 FormatStyle::SBPO_ControlStatements); 19530 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 19531 SpaceBeforeParens, 19532 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19533 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 19534 FormatStyle::SBPO_NonEmptyParentheses); 19535 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 19536 FormatStyle::SBPO_Custom); 19537 // For backward compatibility: 19538 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 19539 FormatStyle::SBPO_Never); 19540 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 19541 FormatStyle::SBPO_ControlStatements); 19542 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 19543 SpaceBeforeParens, 19544 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19545 19546 Style.ColumnLimit = 123; 19547 FormatStyle BaseStyle = getLLVMStyle(); 19548 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 19549 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 19550 19551 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 19552 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 19553 FormatStyle::BS_Attach); 19554 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 19555 FormatStyle::BS_Linux); 19556 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 19557 FormatStyle::BS_Mozilla); 19558 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 19559 FormatStyle::BS_Stroustrup); 19560 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 19561 FormatStyle::BS_Allman); 19562 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 19563 FormatStyle::BS_Whitesmiths); 19564 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 19565 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 19566 FormatStyle::BS_WebKit); 19567 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 19568 FormatStyle::BS_Custom); 19569 19570 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 19571 CHECK_PARSE("BraceWrapping:\n" 19572 " AfterControlStatement: MultiLine", 19573 BraceWrapping.AfterControlStatement, 19574 FormatStyle::BWACS_MultiLine); 19575 CHECK_PARSE("BraceWrapping:\n" 19576 " AfterControlStatement: Always", 19577 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19578 CHECK_PARSE("BraceWrapping:\n" 19579 " AfterControlStatement: Never", 19580 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19581 // For backward compatibility: 19582 CHECK_PARSE("BraceWrapping:\n" 19583 " AfterControlStatement: true", 19584 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19585 CHECK_PARSE("BraceWrapping:\n" 19586 " AfterControlStatement: false", 19587 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19588 19589 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 19590 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 19591 FormatStyle::RTBS_None); 19592 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 19593 FormatStyle::RTBS_All); 19594 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 19595 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 19596 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 19597 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 19598 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 19599 AlwaysBreakAfterReturnType, 19600 FormatStyle::RTBS_TopLevelDefinitions); 19601 19602 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 19603 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 19604 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 19605 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 19606 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19607 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 19608 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19609 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 19610 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19611 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 19612 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19613 19614 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 19615 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 19616 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 19617 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 19618 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 19619 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 19620 AlwaysBreakAfterDefinitionReturnType, 19621 FormatStyle::DRTBS_TopLevel); 19622 19623 Style.NamespaceIndentation = FormatStyle::NI_All; 19624 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 19625 FormatStyle::NI_None); 19626 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 19627 FormatStyle::NI_Inner); 19628 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 19629 FormatStyle::NI_All); 19630 19631 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 19632 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 19633 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19634 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 19635 AllowShortIfStatementsOnASingleLine, 19636 FormatStyle::SIS_WithoutElse); 19637 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 19638 AllowShortIfStatementsOnASingleLine, 19639 FormatStyle::SIS_OnlyFirstIf); 19640 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 19641 AllowShortIfStatementsOnASingleLine, 19642 FormatStyle::SIS_AllIfsAndElse); 19643 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 19644 AllowShortIfStatementsOnASingleLine, 19645 FormatStyle::SIS_OnlyFirstIf); 19646 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 19647 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19648 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 19649 AllowShortIfStatementsOnASingleLine, 19650 FormatStyle::SIS_WithoutElse); 19651 19652 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 19653 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 19654 FormatStyle::IEBS_AfterExternBlock); 19655 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 19656 FormatStyle::IEBS_Indent); 19657 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 19658 FormatStyle::IEBS_NoIndent); 19659 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 19660 FormatStyle::IEBS_Indent); 19661 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 19662 FormatStyle::IEBS_NoIndent); 19663 19664 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 19665 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 19666 FormatStyle::BFCS_Both); 19667 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 19668 FormatStyle::BFCS_None); 19669 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 19670 FormatStyle::BFCS_Before); 19671 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 19672 FormatStyle::BFCS_After); 19673 19674 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 19675 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 19676 FormatStyle::SJSIO_After); 19677 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 19678 FormatStyle::SJSIO_Before); 19679 19680 // FIXME: This is required because parsing a configuration simply overwrites 19681 // the first N elements of the list instead of resetting it. 19682 Style.ForEachMacros.clear(); 19683 std::vector<std::string> BoostForeach; 19684 BoostForeach.push_back("BOOST_FOREACH"); 19685 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 19686 std::vector<std::string> BoostAndQForeach; 19687 BoostAndQForeach.push_back("BOOST_FOREACH"); 19688 BoostAndQForeach.push_back("Q_FOREACH"); 19689 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 19690 BoostAndQForeach); 19691 19692 Style.IfMacros.clear(); 19693 std::vector<std::string> CustomIfs; 19694 CustomIfs.push_back("MYIF"); 19695 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 19696 19697 Style.AttributeMacros.clear(); 19698 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 19699 std::vector<std::string>{"__capability"}); 19700 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 19701 std::vector<std::string>({"attr1", "attr2"})); 19702 19703 Style.StatementAttributeLikeMacros.clear(); 19704 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 19705 StatementAttributeLikeMacros, 19706 std::vector<std::string>({"emit", "Q_EMIT"})); 19707 19708 Style.StatementMacros.clear(); 19709 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 19710 std::vector<std::string>{"QUNUSED"}); 19711 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 19712 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 19713 19714 Style.NamespaceMacros.clear(); 19715 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 19716 std::vector<std::string>{"TESTSUITE"}); 19717 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 19718 std::vector<std::string>({"TESTSUITE", "SUITE"})); 19719 19720 Style.WhitespaceSensitiveMacros.clear(); 19721 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 19722 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19723 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 19724 WhitespaceSensitiveMacros, 19725 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19726 Style.WhitespaceSensitiveMacros.clear(); 19727 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 19728 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19729 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 19730 WhitespaceSensitiveMacros, 19731 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19732 19733 Style.IncludeStyle.IncludeCategories.clear(); 19734 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 19735 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 19736 CHECK_PARSE("IncludeCategories:\n" 19737 " - Regex: abc/.*\n" 19738 " Priority: 2\n" 19739 " - Regex: .*\n" 19740 " Priority: 1\n" 19741 " CaseSensitive: true\n", 19742 IncludeStyle.IncludeCategories, ExpectedCategories); 19743 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 19744 "abc$"); 19745 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 19746 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 19747 19748 Style.SortIncludes = FormatStyle::SI_Never; 19749 CHECK_PARSE("SortIncludes: true", SortIncludes, 19750 FormatStyle::SI_CaseSensitive); 19751 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 19752 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 19753 FormatStyle::SI_CaseInsensitive); 19754 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 19755 FormatStyle::SI_CaseSensitive); 19756 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 19757 19758 Style.RawStringFormats.clear(); 19759 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 19760 { 19761 FormatStyle::LK_TextProto, 19762 {"pb", "proto"}, 19763 {"PARSE_TEXT_PROTO"}, 19764 /*CanonicalDelimiter=*/"", 19765 "llvm", 19766 }, 19767 { 19768 FormatStyle::LK_Cpp, 19769 {"cc", "cpp"}, 19770 {"C_CODEBLOCK", "CPPEVAL"}, 19771 /*CanonicalDelimiter=*/"cc", 19772 /*BasedOnStyle=*/"", 19773 }, 19774 }; 19775 19776 CHECK_PARSE("RawStringFormats:\n" 19777 " - Language: TextProto\n" 19778 " Delimiters:\n" 19779 " - 'pb'\n" 19780 " - 'proto'\n" 19781 " EnclosingFunctions:\n" 19782 " - 'PARSE_TEXT_PROTO'\n" 19783 " BasedOnStyle: llvm\n" 19784 " - Language: Cpp\n" 19785 " Delimiters:\n" 19786 " - 'cc'\n" 19787 " - 'cpp'\n" 19788 " EnclosingFunctions:\n" 19789 " - 'C_CODEBLOCK'\n" 19790 " - 'CPPEVAL'\n" 19791 " CanonicalDelimiter: 'cc'", 19792 RawStringFormats, ExpectedRawStringFormats); 19793 19794 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19795 " Minimum: 0\n" 19796 " Maximum: 0", 19797 SpacesInLineCommentPrefix.Minimum, 0u); 19798 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 19799 Style.SpacesInLineCommentPrefix.Minimum = 1; 19800 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19801 " Minimum: 2", 19802 SpacesInLineCommentPrefix.Minimum, 0u); 19803 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19804 " Maximum: -1", 19805 SpacesInLineCommentPrefix.Maximum, -1u); 19806 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19807 " Minimum: 2", 19808 SpacesInLineCommentPrefix.Minimum, 2u); 19809 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19810 " Maximum: 1", 19811 SpacesInLineCommentPrefix.Maximum, 1u); 19812 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 19813 19814 Style.SpacesInAngles = FormatStyle::SIAS_Always; 19815 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 19816 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 19817 FormatStyle::SIAS_Always); 19818 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 19819 // For backward compatibility: 19820 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 19821 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 19822 } 19823 19824 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 19825 FormatStyle Style = {}; 19826 Style.Language = FormatStyle::LK_Cpp; 19827 CHECK_PARSE("Language: Cpp\n" 19828 "IndentWidth: 12", 19829 IndentWidth, 12u); 19830 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 19831 "IndentWidth: 34", 19832 &Style), 19833 ParseError::Unsuitable); 19834 FormatStyle BinPackedTCS = {}; 19835 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 19836 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 19837 "InsertTrailingCommas: Wrapped", 19838 &BinPackedTCS), 19839 ParseError::BinPackTrailingCommaConflict); 19840 EXPECT_EQ(12u, Style.IndentWidth); 19841 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19842 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19843 19844 Style.Language = FormatStyle::LK_JavaScript; 19845 CHECK_PARSE("Language: JavaScript\n" 19846 "IndentWidth: 12", 19847 IndentWidth, 12u); 19848 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 19849 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 19850 "IndentWidth: 34", 19851 &Style), 19852 ParseError::Unsuitable); 19853 EXPECT_EQ(23u, Style.IndentWidth); 19854 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19855 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19856 19857 CHECK_PARSE("BasedOnStyle: LLVM\n" 19858 "IndentWidth: 67", 19859 IndentWidth, 67u); 19860 19861 CHECK_PARSE("---\n" 19862 "Language: JavaScript\n" 19863 "IndentWidth: 12\n" 19864 "---\n" 19865 "Language: Cpp\n" 19866 "IndentWidth: 34\n" 19867 "...\n", 19868 IndentWidth, 12u); 19869 19870 Style.Language = FormatStyle::LK_Cpp; 19871 CHECK_PARSE("---\n" 19872 "Language: JavaScript\n" 19873 "IndentWidth: 12\n" 19874 "---\n" 19875 "Language: Cpp\n" 19876 "IndentWidth: 34\n" 19877 "...\n", 19878 IndentWidth, 34u); 19879 CHECK_PARSE("---\n" 19880 "IndentWidth: 78\n" 19881 "---\n" 19882 "Language: JavaScript\n" 19883 "IndentWidth: 56\n" 19884 "...\n", 19885 IndentWidth, 78u); 19886 19887 Style.ColumnLimit = 123; 19888 Style.IndentWidth = 234; 19889 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 19890 Style.TabWidth = 345; 19891 EXPECT_FALSE(parseConfiguration("---\n" 19892 "IndentWidth: 456\n" 19893 "BreakBeforeBraces: Allman\n" 19894 "---\n" 19895 "Language: JavaScript\n" 19896 "IndentWidth: 111\n" 19897 "TabWidth: 111\n" 19898 "---\n" 19899 "Language: Cpp\n" 19900 "BreakBeforeBraces: Stroustrup\n" 19901 "TabWidth: 789\n" 19902 "...\n", 19903 &Style)); 19904 EXPECT_EQ(123u, Style.ColumnLimit); 19905 EXPECT_EQ(456u, Style.IndentWidth); 19906 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 19907 EXPECT_EQ(789u, Style.TabWidth); 19908 19909 EXPECT_EQ(parseConfiguration("---\n" 19910 "Language: JavaScript\n" 19911 "IndentWidth: 56\n" 19912 "---\n" 19913 "IndentWidth: 78\n" 19914 "...\n", 19915 &Style), 19916 ParseError::Error); 19917 EXPECT_EQ(parseConfiguration("---\n" 19918 "Language: JavaScript\n" 19919 "IndentWidth: 56\n" 19920 "---\n" 19921 "Language: JavaScript\n" 19922 "IndentWidth: 78\n" 19923 "...\n", 19924 &Style), 19925 ParseError::Error); 19926 19927 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19928 } 19929 19930 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 19931 FormatStyle Style = {}; 19932 Style.Language = FormatStyle::LK_JavaScript; 19933 Style.BreakBeforeTernaryOperators = true; 19934 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 19935 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19936 19937 Style.BreakBeforeTernaryOperators = true; 19938 EXPECT_EQ(0, parseConfiguration("---\n" 19939 "BasedOnStyle: Google\n" 19940 "---\n" 19941 "Language: JavaScript\n" 19942 "IndentWidth: 76\n" 19943 "...\n", 19944 &Style) 19945 .value()); 19946 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19947 EXPECT_EQ(76u, Style.IndentWidth); 19948 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19949 } 19950 19951 TEST_F(FormatTest, ConfigurationRoundTripTest) { 19952 FormatStyle Style = getLLVMStyle(); 19953 std::string YAML = configurationAsText(Style); 19954 FormatStyle ParsedStyle = {}; 19955 ParsedStyle.Language = FormatStyle::LK_Cpp; 19956 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 19957 EXPECT_EQ(Style, ParsedStyle); 19958 } 19959 19960 TEST_F(FormatTest, WorksFor8bitEncodings) { 19961 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 19962 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 19963 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 19964 "\"\xef\xee\xf0\xf3...\"", 19965 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 19966 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 19967 "\xef\xee\xf0\xf3...\"", 19968 getLLVMStyleWithColumns(12))); 19969 } 19970 19971 TEST_F(FormatTest, HandlesUTF8BOM) { 19972 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 19973 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 19974 format("\xef\xbb\xbf#include <iostream>")); 19975 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 19976 format("\xef\xbb\xbf\n#include <iostream>")); 19977 } 19978 19979 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 19980 #if !defined(_MSC_VER) 19981 19982 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 19983 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 19984 getLLVMStyleWithColumns(35)); 19985 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 19986 getLLVMStyleWithColumns(31)); 19987 verifyFormat("// Однажды в студёную зимнюю пору...", 19988 getLLVMStyleWithColumns(36)); 19989 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 19990 verifyFormat("/* Однажды в студёную зимнюю пору... */", 19991 getLLVMStyleWithColumns(39)); 19992 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 19993 getLLVMStyleWithColumns(35)); 19994 } 19995 19996 TEST_F(FormatTest, SplitsUTF8Strings) { 19997 // Non-printable characters' width is currently considered to be the length in 19998 // bytes in UTF8. The characters can be displayed in very different manner 19999 // (zero-width, single width with a substitution glyph, expanded to their code 20000 // (e.g. "<8d>"), so there's no single correct way to handle them. 20001 EXPECT_EQ("\"aaaaÄ\"\n" 20002 "\"\xc2\x8d\";", 20003 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 20004 EXPECT_EQ("\"aaaaaaaÄ\"\n" 20005 "\"\xc2\x8d\";", 20006 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 20007 EXPECT_EQ("\"Однажды, в \"\n" 20008 "\"студёную \"\n" 20009 "\"зимнюю \"\n" 20010 "\"пору,\"", 20011 format("\"Однажды, в студёную зимнюю пору,\"", 20012 getLLVMStyleWithColumns(13))); 20013 EXPECT_EQ( 20014 "\"一 二 三 \"\n" 20015 "\"四 五六 \"\n" 20016 "\"七 八 九 \"\n" 20017 "\"十\"", 20018 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 20019 EXPECT_EQ("\"一\t\"\n" 20020 "\"二 \t\"\n" 20021 "\"三 四 \"\n" 20022 "\"五\t\"\n" 20023 "\"六 \t\"\n" 20024 "\"七 \"\n" 20025 "\"八九十\tqq\"", 20026 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 20027 getLLVMStyleWithColumns(11))); 20028 20029 // UTF8 character in an escape sequence. 20030 EXPECT_EQ("\"aaaaaa\"\n" 20031 "\"\\\xC2\x8D\"", 20032 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 20033 } 20034 20035 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 20036 EXPECT_EQ("const char *sssss =\n" 20037 " \"一二三四五六七八\\\n" 20038 " 九 十\";", 20039 format("const char *sssss = \"一二三四五六七八\\\n" 20040 " 九 十\";", 20041 getLLVMStyleWithColumns(30))); 20042 } 20043 20044 TEST_F(FormatTest, SplitsUTF8LineComments) { 20045 EXPECT_EQ("// aaaaÄ\xc2\x8d", 20046 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 20047 EXPECT_EQ("// Я из лесу\n" 20048 "// вышел; был\n" 20049 "// сильный\n" 20050 "// мороз.", 20051 format("// Я из лесу вышел; был сильный мороз.", 20052 getLLVMStyleWithColumns(13))); 20053 EXPECT_EQ("// 一二三\n" 20054 "// 四五六七\n" 20055 "// 八 九\n" 20056 "// 十", 20057 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 20058 } 20059 20060 TEST_F(FormatTest, SplitsUTF8BlockComments) { 20061 EXPECT_EQ("/* Гляжу,\n" 20062 " * поднимается\n" 20063 " * медленно в\n" 20064 " * гору\n" 20065 " * Лошадка,\n" 20066 " * везущая\n" 20067 " * хворосту\n" 20068 " * воз. */", 20069 format("/* Гляжу, поднимается медленно в гору\n" 20070 " * Лошадка, везущая хворосту воз. */", 20071 getLLVMStyleWithColumns(13))); 20072 EXPECT_EQ( 20073 "/* 一二三\n" 20074 " * 四五六七\n" 20075 " * 八 九\n" 20076 " * 十 */", 20077 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 20078 EXPECT_EQ("/* \n" 20079 " * \n" 20080 " * - */", 20081 format("/* - */", getLLVMStyleWithColumns(12))); 20082 } 20083 20084 #endif // _MSC_VER 20085 20086 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 20087 FormatStyle Style = getLLVMStyle(); 20088 20089 Style.ConstructorInitializerIndentWidth = 4; 20090 verifyFormat( 20091 "SomeClass::Constructor()\n" 20092 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20093 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20094 Style); 20095 20096 Style.ConstructorInitializerIndentWidth = 2; 20097 verifyFormat( 20098 "SomeClass::Constructor()\n" 20099 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20100 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20101 Style); 20102 20103 Style.ConstructorInitializerIndentWidth = 0; 20104 verifyFormat( 20105 "SomeClass::Constructor()\n" 20106 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20107 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20108 Style); 20109 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 20110 verifyFormat( 20111 "SomeLongTemplateVariableName<\n" 20112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 20113 Style); 20114 verifyFormat("bool smaller = 1 < " 20115 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 20116 " " 20117 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 20118 Style); 20119 20120 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 20121 verifyFormat("SomeClass::Constructor() :\n" 20122 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 20123 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 20124 Style); 20125 } 20126 20127 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 20128 FormatStyle Style = getLLVMStyle(); 20129 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20130 Style.ConstructorInitializerIndentWidth = 4; 20131 verifyFormat("SomeClass::Constructor()\n" 20132 " : a(a)\n" 20133 " , b(b)\n" 20134 " , c(c) {}", 20135 Style); 20136 verifyFormat("SomeClass::Constructor()\n" 20137 " : a(a) {}", 20138 Style); 20139 20140 Style.ColumnLimit = 0; 20141 verifyFormat("SomeClass::Constructor()\n" 20142 " : a(a) {}", 20143 Style); 20144 verifyFormat("SomeClass::Constructor() noexcept\n" 20145 " : a(a) {}", 20146 Style); 20147 verifyFormat("SomeClass::Constructor()\n" 20148 " : a(a)\n" 20149 " , b(b)\n" 20150 " , c(c) {}", 20151 Style); 20152 verifyFormat("SomeClass::Constructor()\n" 20153 " : a(a) {\n" 20154 " foo();\n" 20155 " bar();\n" 20156 "}", 20157 Style); 20158 20159 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 20160 verifyFormat("SomeClass::Constructor()\n" 20161 " : a(a)\n" 20162 " , b(b)\n" 20163 " , c(c) {\n}", 20164 Style); 20165 verifyFormat("SomeClass::Constructor()\n" 20166 " : a(a) {\n}", 20167 Style); 20168 20169 Style.ColumnLimit = 80; 20170 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 20171 Style.ConstructorInitializerIndentWidth = 2; 20172 verifyFormat("SomeClass::Constructor()\n" 20173 " : a(a)\n" 20174 " , b(b)\n" 20175 " , c(c) {}", 20176 Style); 20177 20178 Style.ConstructorInitializerIndentWidth = 0; 20179 verifyFormat("SomeClass::Constructor()\n" 20180 ": a(a)\n" 20181 ", b(b)\n" 20182 ", c(c) {}", 20183 Style); 20184 20185 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 20186 Style.ConstructorInitializerIndentWidth = 4; 20187 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 20188 verifyFormat( 20189 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 20190 Style); 20191 verifyFormat( 20192 "SomeClass::Constructor()\n" 20193 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 20194 Style); 20195 Style.ConstructorInitializerIndentWidth = 4; 20196 Style.ColumnLimit = 60; 20197 verifyFormat("SomeClass::Constructor()\n" 20198 " : aaaaaaaa(aaaaaaaa)\n" 20199 " , aaaaaaaa(aaaaaaaa)\n" 20200 " , aaaaaaaa(aaaaaaaa) {}", 20201 Style); 20202 } 20203 20204 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 20205 FormatStyle Style = getLLVMStyle(); 20206 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20207 Style.ConstructorInitializerIndentWidth = 4; 20208 verifyFormat("SomeClass::Constructor()\n" 20209 " : a{a}\n" 20210 " , b{b} {}", 20211 Style); 20212 verifyFormat("SomeClass::Constructor()\n" 20213 " : a{a}\n" 20214 "#if CONDITION\n" 20215 " , b{b}\n" 20216 "#endif\n" 20217 "{\n}", 20218 Style); 20219 Style.ConstructorInitializerIndentWidth = 2; 20220 verifyFormat("SomeClass::Constructor()\n" 20221 "#if CONDITION\n" 20222 " : a{a}\n" 20223 "#endif\n" 20224 " , b{b}\n" 20225 " , c{c} {\n}", 20226 Style); 20227 Style.ConstructorInitializerIndentWidth = 0; 20228 verifyFormat("SomeClass::Constructor()\n" 20229 ": a{a}\n" 20230 "#ifdef CONDITION\n" 20231 ", b{b}\n" 20232 "#else\n" 20233 ", c{c}\n" 20234 "#endif\n" 20235 ", d{d} {\n}", 20236 Style); 20237 Style.ConstructorInitializerIndentWidth = 4; 20238 verifyFormat("SomeClass::Constructor()\n" 20239 " : a{a}\n" 20240 "#if WINDOWS\n" 20241 "#if DEBUG\n" 20242 " , b{0}\n" 20243 "#else\n" 20244 " , b{1}\n" 20245 "#endif\n" 20246 "#else\n" 20247 "#if DEBUG\n" 20248 " , b{2}\n" 20249 "#else\n" 20250 " , b{3}\n" 20251 "#endif\n" 20252 "#endif\n" 20253 "{\n}", 20254 Style); 20255 verifyFormat("SomeClass::Constructor()\n" 20256 " : a{a}\n" 20257 "#if WINDOWS\n" 20258 " , b{0}\n" 20259 "#if DEBUG\n" 20260 " , c{0}\n" 20261 "#else\n" 20262 " , c{1}\n" 20263 "#endif\n" 20264 "#else\n" 20265 "#if DEBUG\n" 20266 " , c{2}\n" 20267 "#else\n" 20268 " , c{3}\n" 20269 "#endif\n" 20270 " , b{1}\n" 20271 "#endif\n" 20272 "{\n}", 20273 Style); 20274 } 20275 20276 TEST_F(FormatTest, Destructors) { 20277 verifyFormat("void F(int &i) { i.~int(); }"); 20278 verifyFormat("void F(int &i) { i->~int(); }"); 20279 } 20280 20281 TEST_F(FormatTest, FormatsWithWebKitStyle) { 20282 FormatStyle Style = getWebKitStyle(); 20283 20284 // Don't indent in outer namespaces. 20285 verifyFormat("namespace outer {\n" 20286 "int i;\n" 20287 "namespace inner {\n" 20288 " int i;\n" 20289 "} // namespace inner\n" 20290 "} // namespace outer\n" 20291 "namespace other_outer {\n" 20292 "int i;\n" 20293 "}", 20294 Style); 20295 20296 // Don't indent case labels. 20297 verifyFormat("switch (variable) {\n" 20298 "case 1:\n" 20299 "case 2:\n" 20300 " doSomething();\n" 20301 " break;\n" 20302 "default:\n" 20303 " ++variable;\n" 20304 "}", 20305 Style); 20306 20307 // Wrap before binary operators. 20308 EXPECT_EQ("void f()\n" 20309 "{\n" 20310 " if (aaaaaaaaaaaaaaaa\n" 20311 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 20312 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 20313 " return;\n" 20314 "}", 20315 format("void f() {\n" 20316 "if (aaaaaaaaaaaaaaaa\n" 20317 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 20318 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 20319 "return;\n" 20320 "}", 20321 Style)); 20322 20323 // Allow functions on a single line. 20324 verifyFormat("void f() { return; }", Style); 20325 20326 // Allow empty blocks on a single line and insert a space in empty blocks. 20327 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 20328 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 20329 // However, don't merge non-empty short loops. 20330 EXPECT_EQ("while (true) {\n" 20331 " continue;\n" 20332 "}", 20333 format("while (true) { continue; }", Style)); 20334 20335 // Constructor initializers are formatted one per line with the "," on the 20336 // new line. 20337 verifyFormat("Constructor()\n" 20338 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 20339 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 20340 " aaaaaaaaaaaaaa)\n" 20341 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 20342 "{\n" 20343 "}", 20344 Style); 20345 verifyFormat("SomeClass::Constructor()\n" 20346 " : a(a)\n" 20347 "{\n" 20348 "}", 20349 Style); 20350 EXPECT_EQ("SomeClass::Constructor()\n" 20351 " : a(a)\n" 20352 "{\n" 20353 "}", 20354 format("SomeClass::Constructor():a(a){}", Style)); 20355 verifyFormat("SomeClass::Constructor()\n" 20356 " : a(a)\n" 20357 " , b(b)\n" 20358 " , c(c)\n" 20359 "{\n" 20360 "}", 20361 Style); 20362 verifyFormat("SomeClass::Constructor()\n" 20363 " : a(a)\n" 20364 "{\n" 20365 " foo();\n" 20366 " bar();\n" 20367 "}", 20368 Style); 20369 20370 // Access specifiers should be aligned left. 20371 verifyFormat("class C {\n" 20372 "public:\n" 20373 " int i;\n" 20374 "};", 20375 Style); 20376 20377 // Do not align comments. 20378 verifyFormat("int a; // Do not\n" 20379 "double b; // align comments.", 20380 Style); 20381 20382 // Do not align operands. 20383 EXPECT_EQ("ASSERT(aaaa\n" 20384 " || bbbb);", 20385 format("ASSERT ( aaaa\n||bbbb);", Style)); 20386 20387 // Accept input's line breaks. 20388 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 20389 " || bbbbbbbbbbbbbbb) {\n" 20390 " i++;\n" 20391 "}", 20392 format("if (aaaaaaaaaaaaaaa\n" 20393 "|| bbbbbbbbbbbbbbb) { i++; }", 20394 Style)); 20395 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 20396 " i++;\n" 20397 "}", 20398 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 20399 20400 // Don't automatically break all macro definitions (llvm.org/PR17842). 20401 verifyFormat("#define aNumber 10", Style); 20402 // However, generally keep the line breaks that the user authored. 20403 EXPECT_EQ("#define aNumber \\\n" 20404 " 10", 20405 format("#define aNumber \\\n" 20406 " 10", 20407 Style)); 20408 20409 // Keep empty and one-element array literals on a single line. 20410 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 20411 " copyItems:YES];", 20412 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 20413 "copyItems:YES];", 20414 Style)); 20415 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 20416 " copyItems:YES];", 20417 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 20418 " copyItems:YES];", 20419 Style)); 20420 // FIXME: This does not seem right, there should be more indentation before 20421 // the array literal's entries. Nested blocks have the same problem. 20422 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20423 " @\"a\",\n" 20424 " @\"a\"\n" 20425 "]\n" 20426 " copyItems:YES];", 20427 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20428 " @\"a\",\n" 20429 " @\"a\"\n" 20430 " ]\n" 20431 " copyItems:YES];", 20432 Style)); 20433 EXPECT_EQ( 20434 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20435 " copyItems:YES];", 20436 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20437 " copyItems:YES];", 20438 Style)); 20439 20440 verifyFormat("[self.a b:c c:d];", Style); 20441 EXPECT_EQ("[self.a b:c\n" 20442 " c:d];", 20443 format("[self.a b:c\n" 20444 "c:d];", 20445 Style)); 20446 } 20447 20448 TEST_F(FormatTest, FormatsLambdas) { 20449 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 20450 verifyFormat( 20451 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 20452 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 20453 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 20454 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 20455 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 20456 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 20457 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 20458 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 20459 verifyFormat("int x = f(*+[] {});"); 20460 verifyFormat("void f() {\n" 20461 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 20462 "}\n"); 20463 verifyFormat("void f() {\n" 20464 " other(x.begin(), //\n" 20465 " x.end(), //\n" 20466 " [&](int, int) { return 1; });\n" 20467 "}\n"); 20468 verifyFormat("void f() {\n" 20469 " other.other.other.other.other(\n" 20470 " x.begin(), x.end(),\n" 20471 " [something, rather](int, int, int, int, int, int, int) { " 20472 "return 1; });\n" 20473 "}\n"); 20474 verifyFormat( 20475 "void f() {\n" 20476 " other.other.other.other.other(\n" 20477 " x.begin(), x.end(),\n" 20478 " [something, rather](int, int, int, int, int, int, int) {\n" 20479 " //\n" 20480 " });\n" 20481 "}\n"); 20482 verifyFormat("SomeFunction([]() { // A cool function...\n" 20483 " return 43;\n" 20484 "});"); 20485 EXPECT_EQ("SomeFunction([]() {\n" 20486 "#define A a\n" 20487 " return 43;\n" 20488 "});", 20489 format("SomeFunction([](){\n" 20490 "#define A a\n" 20491 "return 43;\n" 20492 "});")); 20493 verifyFormat("void f() {\n" 20494 " SomeFunction([](decltype(x), A *a) {});\n" 20495 " SomeFunction([](typeof(x), A *a) {});\n" 20496 " SomeFunction([](_Atomic(x), A *a) {});\n" 20497 " SomeFunction([](__underlying_type(x), A *a) {});\n" 20498 "}"); 20499 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20500 " [](const aaaaaaaaaa &a) { return a; });"); 20501 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 20502 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 20503 "});"); 20504 verifyFormat("Constructor()\n" 20505 " : Field([] { // comment\n" 20506 " int i;\n" 20507 " }) {}"); 20508 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 20509 " return some_parameter.size();\n" 20510 "};"); 20511 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 20512 " [](const string &s) { return s; };"); 20513 verifyFormat("int i = aaaaaa ? 1 //\n" 20514 " : [] {\n" 20515 " return 2; //\n" 20516 " }();"); 20517 verifyFormat("llvm::errs() << \"number of twos is \"\n" 20518 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 20519 " return x == 2; // force break\n" 20520 " });"); 20521 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20522 " [=](int iiiiiiiiiiii) {\n" 20523 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 20524 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 20525 " });", 20526 getLLVMStyleWithColumns(60)); 20527 20528 verifyFormat("SomeFunction({[&] {\n" 20529 " // comment\n" 20530 " },\n" 20531 " [&] {\n" 20532 " // comment\n" 20533 " }});"); 20534 verifyFormat("SomeFunction({[&] {\n" 20535 " // comment\n" 20536 "}});"); 20537 verifyFormat( 20538 "virtual aaaaaaaaaaaaaaaa(\n" 20539 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 20540 " aaaaa aaaaaaaaa);"); 20541 20542 // Lambdas with return types. 20543 verifyFormat("int c = []() -> int { return 2; }();\n"); 20544 verifyFormat("int c = []() -> int * { return 2; }();\n"); 20545 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 20546 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 20547 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 20548 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 20549 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 20550 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 20551 verifyFormat("[a, a]() -> a<1> {};"); 20552 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 20553 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 20554 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 20555 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 20556 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 20557 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 20558 verifyFormat("[]() -> foo<!5> { return {}; };"); 20559 verifyFormat("[]() -> foo<~5> { return {}; };"); 20560 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 20561 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 20562 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 20563 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 20564 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 20565 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 20566 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 20567 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 20568 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 20569 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 20570 verifyFormat("namespace bar {\n" 20571 "// broken:\n" 20572 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 20573 "} // namespace bar"); 20574 verifyFormat("namespace bar {\n" 20575 "// broken:\n" 20576 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 20577 "} // namespace bar"); 20578 verifyFormat("namespace bar {\n" 20579 "// broken:\n" 20580 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 20581 "} // namespace bar"); 20582 verifyFormat("namespace bar {\n" 20583 "// broken:\n" 20584 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 20585 "} // namespace bar"); 20586 verifyFormat("namespace bar {\n" 20587 "// broken:\n" 20588 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 20589 "} // namespace bar"); 20590 verifyFormat("namespace bar {\n" 20591 "// broken:\n" 20592 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 20593 "} // namespace bar"); 20594 verifyFormat("namespace bar {\n" 20595 "// broken:\n" 20596 "auto foo{[]() -> foo<!5> { return {}; }};\n" 20597 "} // namespace bar"); 20598 verifyFormat("namespace bar {\n" 20599 "// broken:\n" 20600 "auto foo{[]() -> foo<~5> { return {}; }};\n" 20601 "} // namespace bar"); 20602 verifyFormat("namespace bar {\n" 20603 "// broken:\n" 20604 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 20605 "} // namespace bar"); 20606 verifyFormat("namespace bar {\n" 20607 "// broken:\n" 20608 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 20609 "} // namespace bar"); 20610 verifyFormat("namespace bar {\n" 20611 "// broken:\n" 20612 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 20613 "} // namespace bar"); 20614 verifyFormat("namespace bar {\n" 20615 "// broken:\n" 20616 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 20617 "} // namespace bar"); 20618 verifyFormat("namespace bar {\n" 20619 "// broken:\n" 20620 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 20621 "} // namespace bar"); 20622 verifyFormat("namespace bar {\n" 20623 "// broken:\n" 20624 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 20625 "} // namespace bar"); 20626 verifyFormat("namespace bar {\n" 20627 "// broken:\n" 20628 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 20629 "} // namespace bar"); 20630 verifyFormat("namespace bar {\n" 20631 "// broken:\n" 20632 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 20633 "} // namespace bar"); 20634 verifyFormat("namespace bar {\n" 20635 "// broken:\n" 20636 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 20637 "} // namespace bar"); 20638 verifyFormat("namespace bar {\n" 20639 "// broken:\n" 20640 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 20641 "} // namespace bar"); 20642 verifyFormat("[]() -> a<1> {};"); 20643 verifyFormat("[]() -> a<1> { ; };"); 20644 verifyFormat("[]() -> a<1> { ; }();"); 20645 verifyFormat("[a, a]() -> a<true> {};"); 20646 verifyFormat("[]() -> a<true> {};"); 20647 verifyFormat("[]() -> a<true> { ; };"); 20648 verifyFormat("[]() -> a<true> { ; }();"); 20649 verifyFormat("[a, a]() -> a<false> {};"); 20650 verifyFormat("[]() -> a<false> {};"); 20651 verifyFormat("[]() -> a<false> { ; };"); 20652 verifyFormat("[]() -> a<false> { ; }();"); 20653 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 20654 verifyFormat("namespace bar {\n" 20655 "auto foo{[]() -> foo<false> { ; }};\n" 20656 "} // namespace bar"); 20657 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 20658 " int j) -> int {\n" 20659 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 20660 "};"); 20661 verifyFormat( 20662 "aaaaaaaaaaaaaaaaaaaaaa(\n" 20663 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 20664 " return aaaaaaaaaaaaaaaaa;\n" 20665 " });", 20666 getLLVMStyleWithColumns(70)); 20667 verifyFormat("[]() //\n" 20668 " -> int {\n" 20669 " return 1; //\n" 20670 "};"); 20671 verifyFormat("[]() -> Void<T...> {};"); 20672 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 20673 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});"); 20674 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});"); 20675 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});"); 20676 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});"); 20677 verifyFormat("return int{[x = x]() { return x; }()};"); 20678 20679 // Lambdas with explicit template argument lists. 20680 verifyFormat( 20681 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 20682 verifyFormat("auto L = []<class T>(T) {\n" 20683 " {\n" 20684 " f();\n" 20685 " g();\n" 20686 " }\n" 20687 "};\n"); 20688 verifyFormat("auto L = []<class... T>(T...) {\n" 20689 " {\n" 20690 " f();\n" 20691 " g();\n" 20692 " }\n" 20693 "};\n"); 20694 verifyFormat("auto L = []<typename... T>(T...) {\n" 20695 " {\n" 20696 " f();\n" 20697 " g();\n" 20698 " }\n" 20699 "};\n"); 20700 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n" 20701 " {\n" 20702 " f();\n" 20703 " g();\n" 20704 " }\n" 20705 "};\n"); 20706 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n" 20707 " {\n" 20708 " f();\n" 20709 " g();\n" 20710 " }\n" 20711 "};\n"); 20712 20713 // Multiple lambdas in the same parentheses change indentation rules. These 20714 // lambdas are forced to start on new lines. 20715 verifyFormat("SomeFunction(\n" 20716 " []() {\n" 20717 " //\n" 20718 " },\n" 20719 " []() {\n" 20720 " //\n" 20721 " });"); 20722 20723 // A lambda passed as arg0 is always pushed to the next line. 20724 verifyFormat("SomeFunction(\n" 20725 " [this] {\n" 20726 " //\n" 20727 " },\n" 20728 " 1);\n"); 20729 20730 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 20731 // the arg0 case above. 20732 auto Style = getGoogleStyle(); 20733 Style.BinPackArguments = false; 20734 verifyFormat("SomeFunction(\n" 20735 " a,\n" 20736 " [this] {\n" 20737 " //\n" 20738 " },\n" 20739 " b);\n", 20740 Style); 20741 verifyFormat("SomeFunction(\n" 20742 " a,\n" 20743 " [this] {\n" 20744 " //\n" 20745 " },\n" 20746 " b);\n"); 20747 20748 // A lambda with a very long line forces arg0 to be pushed out irrespective of 20749 // the BinPackArguments value (as long as the code is wide enough). 20750 verifyFormat( 20751 "something->SomeFunction(\n" 20752 " a,\n" 20753 " [this] {\n" 20754 " " 20755 "D0000000000000000000000000000000000000000000000000000000000001();\n" 20756 " },\n" 20757 " b);\n"); 20758 20759 // A multi-line lambda is pulled up as long as the introducer fits on the 20760 // previous line and there are no further args. 20761 verifyFormat("function(1, [this, that] {\n" 20762 " //\n" 20763 "});\n"); 20764 verifyFormat("function([this, that] {\n" 20765 " //\n" 20766 "});\n"); 20767 // FIXME: this format is not ideal and we should consider forcing the first 20768 // arg onto its own line. 20769 verifyFormat("function(a, b, c, //\n" 20770 " d, [this, that] {\n" 20771 " //\n" 20772 " });\n"); 20773 20774 // Multiple lambdas are treated correctly even when there is a short arg0. 20775 verifyFormat("SomeFunction(\n" 20776 " 1,\n" 20777 " [this] {\n" 20778 " //\n" 20779 " },\n" 20780 " [this] {\n" 20781 " //\n" 20782 " },\n" 20783 " 1);\n"); 20784 20785 // More complex introducers. 20786 verifyFormat("return [i, args...] {};"); 20787 20788 // Not lambdas. 20789 verifyFormat("constexpr char hello[]{\"hello\"};"); 20790 verifyFormat("double &operator[](int i) { return 0; }\n" 20791 "int i;"); 20792 verifyFormat("std::unique_ptr<int[]> foo() {}"); 20793 verifyFormat("int i = a[a][a]->f();"); 20794 verifyFormat("int i = (*b)[a]->f();"); 20795 20796 // Other corner cases. 20797 verifyFormat("void f() {\n" 20798 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 20799 " );\n" 20800 "}"); 20801 20802 // Lambdas created through weird macros. 20803 verifyFormat("void f() {\n" 20804 " MACRO((const AA &a) { return 1; });\n" 20805 " MACRO((AA &a) { return 1; });\n" 20806 "}"); 20807 20808 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 20809 " doo_dah();\n" 20810 " doo_dah();\n" 20811 " })) {\n" 20812 "}"); 20813 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 20814 " doo_dah();\n" 20815 " doo_dah();\n" 20816 " })) {\n" 20817 "}"); 20818 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 20819 " doo_dah();\n" 20820 " doo_dah();\n" 20821 " })) {\n" 20822 "}"); 20823 verifyFormat("auto lambda = []() {\n" 20824 " int a = 2\n" 20825 "#if A\n" 20826 " + 2\n" 20827 "#endif\n" 20828 " ;\n" 20829 "};"); 20830 20831 // Lambdas with complex multiline introducers. 20832 verifyFormat( 20833 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20834 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 20835 " -> ::std::unordered_set<\n" 20836 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 20837 " //\n" 20838 " });"); 20839 20840 FormatStyle DoNotMerge = getLLVMStyle(); 20841 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20842 verifyFormat("auto c = []() {\n" 20843 " return b;\n" 20844 "};", 20845 "auto c = []() { return b; };", DoNotMerge); 20846 verifyFormat("auto c = []() {\n" 20847 "};", 20848 " auto c = []() {};", DoNotMerge); 20849 20850 FormatStyle MergeEmptyOnly = getLLVMStyle(); 20851 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 20852 verifyFormat("auto c = []() {\n" 20853 " return b;\n" 20854 "};", 20855 "auto c = []() {\n" 20856 " return b;\n" 20857 " };", 20858 MergeEmptyOnly); 20859 verifyFormat("auto c = []() {};", 20860 "auto c = []() {\n" 20861 "};", 20862 MergeEmptyOnly); 20863 20864 FormatStyle MergeInline = getLLVMStyle(); 20865 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 20866 verifyFormat("auto c = []() {\n" 20867 " return b;\n" 20868 "};", 20869 "auto c = []() { return b; };", MergeInline); 20870 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 20871 MergeInline); 20872 verifyFormat("function([]() { return b; }, a)", 20873 "function([]() { return b; }, a)", MergeInline); 20874 verifyFormat("function(a, []() { return b; })", 20875 "function(a, []() { return b; })", MergeInline); 20876 20877 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 20878 // AllowShortLambdasOnASingleLine 20879 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20880 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20881 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20882 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20883 FormatStyle::ShortLambdaStyle::SLS_None; 20884 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 20885 " []()\n" 20886 " {\n" 20887 " return 17;\n" 20888 " });", 20889 LLVMWithBeforeLambdaBody); 20890 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 20891 " []()\n" 20892 " {\n" 20893 " });", 20894 LLVMWithBeforeLambdaBody); 20895 verifyFormat("auto fct_SLS_None = []()\n" 20896 "{\n" 20897 " return 17;\n" 20898 "};", 20899 LLVMWithBeforeLambdaBody); 20900 verifyFormat("TwoNestedLambdas_SLS_None(\n" 20901 " []()\n" 20902 " {\n" 20903 " return Call(\n" 20904 " []()\n" 20905 " {\n" 20906 " return 17;\n" 20907 " });\n" 20908 " });", 20909 LLVMWithBeforeLambdaBody); 20910 verifyFormat("void Fct() {\n" 20911 " return {[]()\n" 20912 " {\n" 20913 " return 17;\n" 20914 " }};\n" 20915 "}", 20916 LLVMWithBeforeLambdaBody); 20917 20918 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20919 FormatStyle::ShortLambdaStyle::SLS_Empty; 20920 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 20921 " []()\n" 20922 " {\n" 20923 " return 17;\n" 20924 " });", 20925 LLVMWithBeforeLambdaBody); 20926 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 20927 LLVMWithBeforeLambdaBody); 20928 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 20929 "ongFunctionName_SLS_Empty(\n" 20930 " []() {});", 20931 LLVMWithBeforeLambdaBody); 20932 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 20933 " []()\n" 20934 " {\n" 20935 " return 17;\n" 20936 " });", 20937 LLVMWithBeforeLambdaBody); 20938 verifyFormat("auto fct_SLS_Empty = []()\n" 20939 "{\n" 20940 " return 17;\n" 20941 "};", 20942 LLVMWithBeforeLambdaBody); 20943 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 20944 " []()\n" 20945 " {\n" 20946 " return Call([]() {});\n" 20947 " });", 20948 LLVMWithBeforeLambdaBody); 20949 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 20950 " []()\n" 20951 " {\n" 20952 " return Call([]() {});\n" 20953 " });", 20954 LLVMWithBeforeLambdaBody); 20955 verifyFormat( 20956 "FctWithLongLineInLambda_SLS_Empty(\n" 20957 " []()\n" 20958 " {\n" 20959 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20960 " AndShouldNotBeConsiderAsInline,\n" 20961 " LambdaBodyMustBeBreak);\n" 20962 " });", 20963 LLVMWithBeforeLambdaBody); 20964 20965 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20966 FormatStyle::ShortLambdaStyle::SLS_Inline; 20967 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 20968 LLVMWithBeforeLambdaBody); 20969 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 20970 LLVMWithBeforeLambdaBody); 20971 verifyFormat("auto fct_SLS_Inline = []()\n" 20972 "{\n" 20973 " return 17;\n" 20974 "};", 20975 LLVMWithBeforeLambdaBody); 20976 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 20977 "17; }); });", 20978 LLVMWithBeforeLambdaBody); 20979 verifyFormat( 20980 "FctWithLongLineInLambda_SLS_Inline(\n" 20981 " []()\n" 20982 " {\n" 20983 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20984 " AndShouldNotBeConsiderAsInline,\n" 20985 " LambdaBodyMustBeBreak);\n" 20986 " });", 20987 LLVMWithBeforeLambdaBody); 20988 verifyFormat("FctWithMultipleParams_SLS_Inline(" 20989 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20990 " []() { return 17; });", 20991 LLVMWithBeforeLambdaBody); 20992 verifyFormat( 20993 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 20994 LLVMWithBeforeLambdaBody); 20995 20996 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20997 FormatStyle::ShortLambdaStyle::SLS_All; 20998 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 20999 LLVMWithBeforeLambdaBody); 21000 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 21001 LLVMWithBeforeLambdaBody); 21002 verifyFormat("auto fct_SLS_All = []() { return 17; };", 21003 LLVMWithBeforeLambdaBody); 21004 verifyFormat("FctWithOneParam_SLS_All(\n" 21005 " []()\n" 21006 " {\n" 21007 " // A cool function...\n" 21008 " return 43;\n" 21009 " });", 21010 LLVMWithBeforeLambdaBody); 21011 verifyFormat("FctWithMultipleParams_SLS_All(" 21012 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 21013 " []() { return 17; });", 21014 LLVMWithBeforeLambdaBody); 21015 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 21016 LLVMWithBeforeLambdaBody); 21017 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 21018 LLVMWithBeforeLambdaBody); 21019 verifyFormat( 21020 "FctWithLongLineInLambda_SLS_All(\n" 21021 " []()\n" 21022 " {\n" 21023 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21024 " AndShouldNotBeConsiderAsInline,\n" 21025 " LambdaBodyMustBeBreak);\n" 21026 " });", 21027 LLVMWithBeforeLambdaBody); 21028 verifyFormat( 21029 "auto fct_SLS_All = []()\n" 21030 "{\n" 21031 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21032 " AndShouldNotBeConsiderAsInline,\n" 21033 " LambdaBodyMustBeBreak);\n" 21034 "};", 21035 LLVMWithBeforeLambdaBody); 21036 LLVMWithBeforeLambdaBody.BinPackParameters = false; 21037 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 21038 LLVMWithBeforeLambdaBody); 21039 verifyFormat( 21040 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 21041 " FirstParam,\n" 21042 " SecondParam,\n" 21043 " ThirdParam,\n" 21044 " FourthParam);", 21045 LLVMWithBeforeLambdaBody); 21046 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 21047 " []() { return " 21048 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 21049 " FirstParam,\n" 21050 " SecondParam,\n" 21051 " ThirdParam,\n" 21052 " FourthParam);", 21053 LLVMWithBeforeLambdaBody); 21054 verifyFormat( 21055 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 21056 " SecondParam,\n" 21057 " ThirdParam,\n" 21058 " FourthParam,\n" 21059 " []() { return SomeValueNotSoLong; });", 21060 LLVMWithBeforeLambdaBody); 21061 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 21062 " []()\n" 21063 " {\n" 21064 " return " 21065 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 21066 "eConsiderAsInline;\n" 21067 " });", 21068 LLVMWithBeforeLambdaBody); 21069 verifyFormat( 21070 "FctWithLongLineInLambda_SLS_All(\n" 21071 " []()\n" 21072 " {\n" 21073 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21074 " AndShouldNotBeConsiderAsInline,\n" 21075 " LambdaBodyMustBeBreak);\n" 21076 " });", 21077 LLVMWithBeforeLambdaBody); 21078 verifyFormat("FctWithTwoParams_SLS_All(\n" 21079 " []()\n" 21080 " {\n" 21081 " // A cool function...\n" 21082 " return 43;\n" 21083 " },\n" 21084 " 87);", 21085 LLVMWithBeforeLambdaBody); 21086 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 21087 LLVMWithBeforeLambdaBody); 21088 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 21089 LLVMWithBeforeLambdaBody); 21090 verifyFormat( 21091 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 21092 LLVMWithBeforeLambdaBody); 21093 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 21094 "}); }, x);", 21095 LLVMWithBeforeLambdaBody); 21096 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21097 " []()\n" 21098 " {\n" 21099 " // A cool function...\n" 21100 " return Call([]() { return 17; });\n" 21101 " });", 21102 LLVMWithBeforeLambdaBody); 21103 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21104 " []()\n" 21105 " {\n" 21106 " return Call(\n" 21107 " []()\n" 21108 " {\n" 21109 " // A cool function...\n" 21110 " return 17;\n" 21111 " });\n" 21112 " });", 21113 LLVMWithBeforeLambdaBody); 21114 21115 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21116 FormatStyle::ShortLambdaStyle::SLS_None; 21117 21118 verifyFormat("auto select = [this]() -> const Library::Object *\n" 21119 "{\n" 21120 " return MyAssignment::SelectFromList(this);\n" 21121 "};\n", 21122 LLVMWithBeforeLambdaBody); 21123 21124 verifyFormat("auto select = [this]() -> const Library::Object &\n" 21125 "{\n" 21126 " return MyAssignment::SelectFromList(this);\n" 21127 "};\n", 21128 LLVMWithBeforeLambdaBody); 21129 21130 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 21131 "{\n" 21132 " return MyAssignment::SelectFromList(this);\n" 21133 "};\n", 21134 LLVMWithBeforeLambdaBody); 21135 21136 verifyFormat("namespace test {\n" 21137 "class Test {\n" 21138 "public:\n" 21139 " Test() = default;\n" 21140 "};\n" 21141 "} // namespace test", 21142 LLVMWithBeforeLambdaBody); 21143 21144 // Lambdas with different indentation styles. 21145 Style = getLLVMStyleWithColumns(100); 21146 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21147 " return promise.then(\n" 21148 " [this, &someVariable, someObject = " 21149 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21150 " return someObject.startAsyncAction().then(\n" 21151 " [this, &someVariable](AsyncActionResult result) " 21152 "mutable { result.processMore(); });\n" 21153 " });\n" 21154 "}\n", 21155 format("SomeResult doSomething(SomeObject promise) {\n" 21156 " return promise.then([this, &someVariable, someObject = " 21157 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21158 " return someObject.startAsyncAction().then([this, " 21159 "&someVariable](AsyncActionResult result) mutable {\n" 21160 " result.processMore();\n" 21161 " });\n" 21162 " });\n" 21163 "}\n", 21164 Style)); 21165 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21166 verifyFormat("test() {\n" 21167 " ([]() -> {\n" 21168 " int b = 32;\n" 21169 " return 3;\n" 21170 " }).foo();\n" 21171 "}", 21172 Style); 21173 verifyFormat("test() {\n" 21174 " []() -> {\n" 21175 " int b = 32;\n" 21176 " return 3;\n" 21177 " }\n" 21178 "}", 21179 Style); 21180 verifyFormat("std::sort(v.begin(), v.end(),\n" 21181 " [](const auto &someLongArgumentName, const auto " 21182 "&someOtherLongArgumentName) {\n" 21183 " return someLongArgumentName.someMemberVariable < " 21184 "someOtherLongArgumentName.someMemberVariable;\n" 21185 "});", 21186 Style); 21187 verifyFormat("test() {\n" 21188 " (\n" 21189 " []() -> {\n" 21190 " int b = 32;\n" 21191 " return 3;\n" 21192 " },\n" 21193 " foo, bar)\n" 21194 " .foo();\n" 21195 "}", 21196 Style); 21197 verifyFormat("test() {\n" 21198 " ([]() -> {\n" 21199 " int b = 32;\n" 21200 " return 3;\n" 21201 " })\n" 21202 " .foo()\n" 21203 " .bar();\n" 21204 "}", 21205 Style); 21206 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21207 " return promise.then(\n" 21208 " [this, &someVariable, someObject = " 21209 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21210 " return someObject.startAsyncAction().then(\n" 21211 " [this, &someVariable](AsyncActionResult result) mutable { " 21212 "result.processMore(); });\n" 21213 " });\n" 21214 "}\n", 21215 format("SomeResult doSomething(SomeObject promise) {\n" 21216 " return promise.then([this, &someVariable, someObject = " 21217 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21218 " return someObject.startAsyncAction().then([this, " 21219 "&someVariable](AsyncActionResult result) mutable {\n" 21220 " result.processMore();\n" 21221 " });\n" 21222 " });\n" 21223 "}\n", 21224 Style)); 21225 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21226 " return promise.then([this, &someVariable] {\n" 21227 " return someObject.startAsyncAction().then(\n" 21228 " [this, &someVariable](AsyncActionResult result) mutable { " 21229 "result.processMore(); });\n" 21230 " });\n" 21231 "}\n", 21232 format("SomeResult doSomething(SomeObject promise) {\n" 21233 " return promise.then([this, &someVariable] {\n" 21234 " return someObject.startAsyncAction().then([this, " 21235 "&someVariable](AsyncActionResult result) mutable {\n" 21236 " result.processMore();\n" 21237 " });\n" 21238 " });\n" 21239 "}\n", 21240 Style)); 21241 Style = getGoogleStyle(); 21242 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21243 EXPECT_EQ("#define A \\\n" 21244 " [] { \\\n" 21245 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 21246 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 21247 " }", 21248 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 21249 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 21250 Style)); 21251 // TODO: The current formatting has a minor issue that's not worth fixing 21252 // right now whereby the closing brace is indented relative to the signature 21253 // instead of being aligned. This only happens with macros. 21254 } 21255 21256 TEST_F(FormatTest, LambdaWithLineComments) { 21257 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 21258 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 21259 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 21260 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21261 FormatStyle::ShortLambdaStyle::SLS_All; 21262 21263 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 21264 verifyFormat("auto k = []() // comment\n" 21265 "{ return; }", 21266 LLVMWithBeforeLambdaBody); 21267 verifyFormat("auto k = []() /* comment */ { return; }", 21268 LLVMWithBeforeLambdaBody); 21269 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 21270 LLVMWithBeforeLambdaBody); 21271 verifyFormat("auto k = []() // X\n" 21272 "{ return; }", 21273 LLVMWithBeforeLambdaBody); 21274 verifyFormat( 21275 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 21276 "{ return; }", 21277 LLVMWithBeforeLambdaBody); 21278 } 21279 21280 TEST_F(FormatTest, EmptyLinesInLambdas) { 21281 verifyFormat("auto lambda = []() {\n" 21282 " x(); //\n" 21283 "};", 21284 "auto lambda = []() {\n" 21285 "\n" 21286 " x(); //\n" 21287 "\n" 21288 "};"); 21289 } 21290 21291 TEST_F(FormatTest, FormatsBlocks) { 21292 FormatStyle ShortBlocks = getLLVMStyle(); 21293 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21294 verifyFormat("int (^Block)(int, int);", ShortBlocks); 21295 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 21296 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 21297 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 21298 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 21299 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 21300 21301 verifyFormat("foo(^{ bar(); });", ShortBlocks); 21302 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 21303 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 21304 21305 verifyFormat("[operation setCompletionBlock:^{\n" 21306 " [self onOperationDone];\n" 21307 "}];"); 21308 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 21309 " [self onOperationDone];\n" 21310 "}]};"); 21311 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 21312 " f();\n" 21313 "}];"); 21314 verifyFormat("int a = [operation block:^int(int *i) {\n" 21315 " return 1;\n" 21316 "}];"); 21317 verifyFormat("[myObject doSomethingWith:arg1\n" 21318 " aaa:^int(int *a) {\n" 21319 " return 1;\n" 21320 " }\n" 21321 " bbb:f(a * bbbbbbbb)];"); 21322 21323 verifyFormat("[operation setCompletionBlock:^{\n" 21324 " [self.delegate newDataAvailable];\n" 21325 "}];", 21326 getLLVMStyleWithColumns(60)); 21327 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 21328 " NSString *path = [self sessionFilePath];\n" 21329 " if (path) {\n" 21330 " // ...\n" 21331 " }\n" 21332 "});"); 21333 verifyFormat("[[SessionService sharedService]\n" 21334 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21335 " if (window) {\n" 21336 " [self windowDidLoad:window];\n" 21337 " } else {\n" 21338 " [self errorLoadingWindow];\n" 21339 " }\n" 21340 " }];"); 21341 verifyFormat("void (^largeBlock)(void) = ^{\n" 21342 " // ...\n" 21343 "};\n", 21344 getLLVMStyleWithColumns(40)); 21345 verifyFormat("[[SessionService sharedService]\n" 21346 " loadWindowWithCompletionBlock: //\n" 21347 " ^(SessionWindow *window) {\n" 21348 " if (window) {\n" 21349 " [self windowDidLoad:window];\n" 21350 " } else {\n" 21351 " [self errorLoadingWindow];\n" 21352 " }\n" 21353 " }];", 21354 getLLVMStyleWithColumns(60)); 21355 verifyFormat("[myObject doSomethingWith:arg1\n" 21356 " firstBlock:^(Foo *a) {\n" 21357 " // ...\n" 21358 " int i;\n" 21359 " }\n" 21360 " secondBlock:^(Bar *b) {\n" 21361 " // ...\n" 21362 " int i;\n" 21363 " }\n" 21364 " thirdBlock:^Foo(Bar *b) {\n" 21365 " // ...\n" 21366 " int i;\n" 21367 " }];"); 21368 verifyFormat("[myObject doSomethingWith:arg1\n" 21369 " firstBlock:-1\n" 21370 " secondBlock:^(Bar *b) {\n" 21371 " // ...\n" 21372 " int i;\n" 21373 " }];"); 21374 21375 verifyFormat("f(^{\n" 21376 " @autoreleasepool {\n" 21377 " if (a) {\n" 21378 " g();\n" 21379 " }\n" 21380 " }\n" 21381 "});"); 21382 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 21383 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 21384 "};"); 21385 21386 FormatStyle FourIndent = getLLVMStyle(); 21387 FourIndent.ObjCBlockIndentWidth = 4; 21388 verifyFormat("[operation setCompletionBlock:^{\n" 21389 " [self onOperationDone];\n" 21390 "}];", 21391 FourIndent); 21392 } 21393 21394 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 21395 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 21396 21397 verifyFormat("[[SessionService sharedService] " 21398 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21399 " if (window) {\n" 21400 " [self windowDidLoad:window];\n" 21401 " } else {\n" 21402 " [self errorLoadingWindow];\n" 21403 " }\n" 21404 "}];", 21405 ZeroColumn); 21406 EXPECT_EQ("[[SessionService sharedService]\n" 21407 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21408 " if (window) {\n" 21409 " [self windowDidLoad:window];\n" 21410 " } else {\n" 21411 " [self errorLoadingWindow];\n" 21412 " }\n" 21413 " }];", 21414 format("[[SessionService sharedService]\n" 21415 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21416 " if (window) {\n" 21417 " [self windowDidLoad:window];\n" 21418 " } else {\n" 21419 " [self errorLoadingWindow];\n" 21420 " }\n" 21421 "}];", 21422 ZeroColumn)); 21423 verifyFormat("[myObject doSomethingWith:arg1\n" 21424 " firstBlock:^(Foo *a) {\n" 21425 " // ...\n" 21426 " int i;\n" 21427 " }\n" 21428 " secondBlock:^(Bar *b) {\n" 21429 " // ...\n" 21430 " int i;\n" 21431 " }\n" 21432 " thirdBlock:^Foo(Bar *b) {\n" 21433 " // ...\n" 21434 " int i;\n" 21435 " }];", 21436 ZeroColumn); 21437 verifyFormat("f(^{\n" 21438 " @autoreleasepool {\n" 21439 " if (a) {\n" 21440 " g();\n" 21441 " }\n" 21442 " }\n" 21443 "});", 21444 ZeroColumn); 21445 verifyFormat("void (^largeBlock)(void) = ^{\n" 21446 " // ...\n" 21447 "};", 21448 ZeroColumn); 21449 21450 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21451 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 21452 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21453 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 21454 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 21455 " int i;\n" 21456 "};", 21457 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21458 } 21459 21460 TEST_F(FormatTest, SupportsCRLF) { 21461 EXPECT_EQ("int a;\r\n" 21462 "int b;\r\n" 21463 "int c;\r\n", 21464 format("int a;\r\n" 21465 " int b;\r\n" 21466 " int c;\r\n", 21467 getLLVMStyle())); 21468 EXPECT_EQ("int a;\r\n" 21469 "int b;\r\n" 21470 "int c;\r\n", 21471 format("int a;\r\n" 21472 " int b;\n" 21473 " int c;\r\n", 21474 getLLVMStyle())); 21475 EXPECT_EQ("int a;\n" 21476 "int b;\n" 21477 "int c;\n", 21478 format("int a;\r\n" 21479 " int b;\n" 21480 " int c;\n", 21481 getLLVMStyle())); 21482 EXPECT_EQ("\"aaaaaaa \"\r\n" 21483 "\"bbbbbbb\";\r\n", 21484 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 21485 EXPECT_EQ("#define A \\\r\n" 21486 " b; \\\r\n" 21487 " c; \\\r\n" 21488 " d;\r\n", 21489 format("#define A \\\r\n" 21490 " b; \\\r\n" 21491 " c; d; \r\n", 21492 getGoogleStyle())); 21493 21494 EXPECT_EQ("/*\r\n" 21495 "multi line block comments\r\n" 21496 "should not introduce\r\n" 21497 "an extra carriage return\r\n" 21498 "*/\r\n", 21499 format("/*\r\n" 21500 "multi line block comments\r\n" 21501 "should not introduce\r\n" 21502 "an extra carriage return\r\n" 21503 "*/\r\n")); 21504 EXPECT_EQ("/*\r\n" 21505 "\r\n" 21506 "*/", 21507 format("/*\r\n" 21508 " \r\r\r\n" 21509 "*/")); 21510 21511 FormatStyle style = getLLVMStyle(); 21512 21513 style.DeriveLineEnding = true; 21514 style.UseCRLF = false; 21515 EXPECT_EQ("union FooBarBazQux {\n" 21516 " int foo;\n" 21517 " int bar;\n" 21518 " int baz;\n" 21519 "};", 21520 format("union FooBarBazQux {\r\n" 21521 " int foo;\n" 21522 " int bar;\r\n" 21523 " int baz;\n" 21524 "};", 21525 style)); 21526 style.UseCRLF = true; 21527 EXPECT_EQ("union FooBarBazQux {\r\n" 21528 " int foo;\r\n" 21529 " int bar;\r\n" 21530 " int baz;\r\n" 21531 "};", 21532 format("union FooBarBazQux {\r\n" 21533 " int foo;\n" 21534 " int bar;\r\n" 21535 " int baz;\n" 21536 "};", 21537 style)); 21538 21539 style.DeriveLineEnding = false; 21540 style.UseCRLF = false; 21541 EXPECT_EQ("union FooBarBazQux {\n" 21542 " int foo;\n" 21543 " int bar;\n" 21544 " int baz;\n" 21545 " int qux;\n" 21546 "};", 21547 format("union FooBarBazQux {\r\n" 21548 " int foo;\n" 21549 " int bar;\r\n" 21550 " int baz;\n" 21551 " int qux;\r\n" 21552 "};", 21553 style)); 21554 style.UseCRLF = true; 21555 EXPECT_EQ("union FooBarBazQux {\r\n" 21556 " int foo;\r\n" 21557 " int bar;\r\n" 21558 " int baz;\r\n" 21559 " int qux;\r\n" 21560 "};", 21561 format("union FooBarBazQux {\r\n" 21562 " int foo;\n" 21563 " int bar;\r\n" 21564 " int baz;\n" 21565 " int qux;\n" 21566 "};", 21567 style)); 21568 21569 style.DeriveLineEnding = true; 21570 style.UseCRLF = false; 21571 EXPECT_EQ("union FooBarBazQux {\r\n" 21572 " int foo;\r\n" 21573 " int bar;\r\n" 21574 " int baz;\r\n" 21575 " int qux;\r\n" 21576 "};", 21577 format("union FooBarBazQux {\r\n" 21578 " int foo;\n" 21579 " int bar;\r\n" 21580 " int baz;\n" 21581 " int qux;\r\n" 21582 "};", 21583 style)); 21584 style.UseCRLF = true; 21585 EXPECT_EQ("union FooBarBazQux {\n" 21586 " int foo;\n" 21587 " int bar;\n" 21588 " int baz;\n" 21589 " int qux;\n" 21590 "};", 21591 format("union FooBarBazQux {\r\n" 21592 " int foo;\n" 21593 " int bar;\r\n" 21594 " int baz;\n" 21595 " int qux;\n" 21596 "};", 21597 style)); 21598 } 21599 21600 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 21601 verifyFormat("MY_CLASS(C) {\n" 21602 " int i;\n" 21603 " int j;\n" 21604 "};"); 21605 } 21606 21607 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 21608 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 21609 TwoIndent.ContinuationIndentWidth = 2; 21610 21611 EXPECT_EQ("int i =\n" 21612 " longFunction(\n" 21613 " arg);", 21614 format("int i = longFunction(arg);", TwoIndent)); 21615 21616 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 21617 SixIndent.ContinuationIndentWidth = 6; 21618 21619 EXPECT_EQ("int i =\n" 21620 " longFunction(\n" 21621 " arg);", 21622 format("int i = longFunction(arg);", SixIndent)); 21623 } 21624 21625 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 21626 FormatStyle Style = getLLVMStyle(); 21627 verifyFormat("int Foo::getter(\n" 21628 " //\n" 21629 ") const {\n" 21630 " return foo;\n" 21631 "}", 21632 Style); 21633 verifyFormat("void Foo::setter(\n" 21634 " //\n" 21635 ") {\n" 21636 " foo = 1;\n" 21637 "}", 21638 Style); 21639 } 21640 21641 TEST_F(FormatTest, SpacesInAngles) { 21642 FormatStyle Spaces = getLLVMStyle(); 21643 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21644 21645 verifyFormat("vector< ::std::string > x1;", Spaces); 21646 verifyFormat("Foo< int, Bar > x2;", Spaces); 21647 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 21648 21649 verifyFormat("static_cast< int >(arg);", Spaces); 21650 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 21651 verifyFormat("f< int, float >();", Spaces); 21652 verifyFormat("template <> g() {}", Spaces); 21653 verifyFormat("template < std::vector< int > > f() {}", Spaces); 21654 verifyFormat("std::function< void(int, int) > fct;", Spaces); 21655 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 21656 Spaces); 21657 21658 Spaces.Standard = FormatStyle::LS_Cpp03; 21659 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21660 verifyFormat("A< A< int > >();", Spaces); 21661 21662 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21663 verifyFormat("A<A<int> >();", Spaces); 21664 21665 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21666 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 21667 Spaces); 21668 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 21669 Spaces); 21670 21671 verifyFormat("A<A<int> >();", Spaces); 21672 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 21673 verifyFormat("A< A< int > >();", Spaces); 21674 21675 Spaces.Standard = FormatStyle::LS_Cpp11; 21676 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21677 verifyFormat("A< A< int > >();", Spaces); 21678 21679 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21680 verifyFormat("vector<::std::string> x4;", Spaces); 21681 verifyFormat("vector<int> x5;", Spaces); 21682 verifyFormat("Foo<int, Bar> x6;", Spaces); 21683 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21684 21685 verifyFormat("A<A<int>>();", Spaces); 21686 21687 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21688 verifyFormat("vector<::std::string> x4;", Spaces); 21689 verifyFormat("vector< ::std::string > x4;", Spaces); 21690 verifyFormat("vector<int> x5;", Spaces); 21691 verifyFormat("vector< int > x5;", Spaces); 21692 verifyFormat("Foo<int, Bar> x6;", Spaces); 21693 verifyFormat("Foo< int, Bar > x6;", Spaces); 21694 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21695 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 21696 21697 verifyFormat("A<A<int>>();", Spaces); 21698 verifyFormat("A< A< int > >();", Spaces); 21699 verifyFormat("A<A<int > >();", Spaces); 21700 verifyFormat("A< A< int>>();", Spaces); 21701 21702 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21703 verifyFormat("// clang-format off\n" 21704 "foo<<<1, 1>>>();\n" 21705 "// clang-format on\n", 21706 Spaces); 21707 verifyFormat("// clang-format off\n" 21708 "foo< < <1, 1> > >();\n" 21709 "// clang-format on\n", 21710 Spaces); 21711 } 21712 21713 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 21714 FormatStyle Style = getLLVMStyle(); 21715 Style.SpaceAfterTemplateKeyword = false; 21716 verifyFormat("template<int> void foo();", Style); 21717 } 21718 21719 TEST_F(FormatTest, TripleAngleBrackets) { 21720 verifyFormat("f<<<1, 1>>>();"); 21721 verifyFormat("f<<<1, 1, 1, s>>>();"); 21722 verifyFormat("f<<<a, b, c, d>>>();"); 21723 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 21724 verifyFormat("f<param><<<1, 1>>>();"); 21725 verifyFormat("f<1><<<1, 1>>>();"); 21726 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 21727 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21728 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 21729 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 21730 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 21731 } 21732 21733 TEST_F(FormatTest, MergeLessLessAtEnd) { 21734 verifyFormat("<<"); 21735 EXPECT_EQ("< < <", format("\\\n<<<")); 21736 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21737 "aaallvm::outs() <<"); 21738 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21739 "aaaallvm::outs()\n <<"); 21740 } 21741 21742 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 21743 std::string code = "#if A\n" 21744 "#if B\n" 21745 "a.\n" 21746 "#endif\n" 21747 " a = 1;\n" 21748 "#else\n" 21749 "#endif\n" 21750 "#if C\n" 21751 "#else\n" 21752 "#endif\n"; 21753 EXPECT_EQ(code, format(code)); 21754 } 21755 21756 TEST_F(FormatTest, HandleConflictMarkers) { 21757 // Git/SVN conflict markers. 21758 EXPECT_EQ("int a;\n" 21759 "void f() {\n" 21760 " callme(some(parameter1,\n" 21761 "<<<<<<< text by the vcs\n" 21762 " parameter2),\n" 21763 "||||||| text by the vcs\n" 21764 " parameter2),\n" 21765 " parameter3,\n" 21766 "======= text by the vcs\n" 21767 " parameter2, parameter3),\n" 21768 ">>>>>>> text by the vcs\n" 21769 " otherparameter);\n", 21770 format("int a;\n" 21771 "void f() {\n" 21772 " callme(some(parameter1,\n" 21773 "<<<<<<< text by the vcs\n" 21774 " parameter2),\n" 21775 "||||||| text by the vcs\n" 21776 " parameter2),\n" 21777 " parameter3,\n" 21778 "======= text by the vcs\n" 21779 " parameter2,\n" 21780 " parameter3),\n" 21781 ">>>>>>> text by the vcs\n" 21782 " otherparameter);\n")); 21783 21784 // Perforce markers. 21785 EXPECT_EQ("void f() {\n" 21786 " function(\n" 21787 ">>>> text by the vcs\n" 21788 " parameter,\n" 21789 "==== text by the vcs\n" 21790 " parameter,\n" 21791 "==== text by the vcs\n" 21792 " parameter,\n" 21793 "<<<< text by the vcs\n" 21794 " parameter);\n", 21795 format("void f() {\n" 21796 " function(\n" 21797 ">>>> text by the vcs\n" 21798 " parameter,\n" 21799 "==== text by the vcs\n" 21800 " parameter,\n" 21801 "==== text by the vcs\n" 21802 " parameter,\n" 21803 "<<<< text by the vcs\n" 21804 " parameter);\n")); 21805 21806 EXPECT_EQ("<<<<<<<\n" 21807 "|||||||\n" 21808 "=======\n" 21809 ">>>>>>>", 21810 format("<<<<<<<\n" 21811 "|||||||\n" 21812 "=======\n" 21813 ">>>>>>>")); 21814 21815 EXPECT_EQ("<<<<<<<\n" 21816 "|||||||\n" 21817 "int i;\n" 21818 "=======\n" 21819 ">>>>>>>", 21820 format("<<<<<<<\n" 21821 "|||||||\n" 21822 "int i;\n" 21823 "=======\n" 21824 ">>>>>>>")); 21825 21826 // FIXME: Handle parsing of macros around conflict markers correctly: 21827 EXPECT_EQ("#define Macro \\\n" 21828 "<<<<<<<\n" 21829 "Something \\\n" 21830 "|||||||\n" 21831 "Else \\\n" 21832 "=======\n" 21833 "Other \\\n" 21834 ">>>>>>>\n" 21835 " End int i;\n", 21836 format("#define Macro \\\n" 21837 "<<<<<<<\n" 21838 " Something \\\n" 21839 "|||||||\n" 21840 " Else \\\n" 21841 "=======\n" 21842 " Other \\\n" 21843 ">>>>>>>\n" 21844 " End\n" 21845 "int i;\n")); 21846 21847 verifyFormat(R"(==== 21848 #ifdef A 21849 a 21850 #else 21851 b 21852 #endif 21853 )"); 21854 } 21855 21856 TEST_F(FormatTest, DisableRegions) { 21857 EXPECT_EQ("int i;\n" 21858 "// clang-format off\n" 21859 " int j;\n" 21860 "// clang-format on\n" 21861 "int k;", 21862 format(" int i;\n" 21863 " // clang-format off\n" 21864 " int j;\n" 21865 " // clang-format on\n" 21866 " int k;")); 21867 EXPECT_EQ("int i;\n" 21868 "/* clang-format off */\n" 21869 " int j;\n" 21870 "/* clang-format on */\n" 21871 "int k;", 21872 format(" int i;\n" 21873 " /* clang-format off */\n" 21874 " int j;\n" 21875 " /* clang-format on */\n" 21876 " int k;")); 21877 21878 // Don't reflow comments within disabled regions. 21879 EXPECT_EQ("// clang-format off\n" 21880 "// long long long long long long line\n" 21881 "/* clang-format on */\n" 21882 "/* long long long\n" 21883 " * long long long\n" 21884 " * line */\n" 21885 "int i;\n" 21886 "/* clang-format off */\n" 21887 "/* long long long long long long line */\n", 21888 format("// clang-format off\n" 21889 "// long long long long long long line\n" 21890 "/* clang-format on */\n" 21891 "/* long long long long long long line */\n" 21892 "int i;\n" 21893 "/* clang-format off */\n" 21894 "/* long long long long long long line */\n", 21895 getLLVMStyleWithColumns(20))); 21896 } 21897 21898 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 21899 format("? ) ="); 21900 verifyNoCrash("#define a\\\n /**/}"); 21901 } 21902 21903 TEST_F(FormatTest, FormatsTableGenCode) { 21904 FormatStyle Style = getLLVMStyle(); 21905 Style.Language = FormatStyle::LK_TableGen; 21906 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 21907 } 21908 21909 TEST_F(FormatTest, ArrayOfTemplates) { 21910 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 21911 format("auto a = new unique_ptr<int > [ 10];")); 21912 21913 FormatStyle Spaces = getLLVMStyle(); 21914 Spaces.SpacesInSquareBrackets = true; 21915 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 21916 format("auto a = new unique_ptr<int > [10];", Spaces)); 21917 } 21918 21919 TEST_F(FormatTest, ArrayAsTemplateType) { 21920 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 21921 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 21922 21923 FormatStyle Spaces = getLLVMStyle(); 21924 Spaces.SpacesInSquareBrackets = true; 21925 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 21926 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 21927 } 21928 21929 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 21930 21931 TEST(FormatStyle, GetStyleWithEmptyFileName) { 21932 llvm::vfs::InMemoryFileSystem FS; 21933 auto Style1 = getStyle("file", "", "Google", "", &FS); 21934 ASSERT_TRUE((bool)Style1); 21935 ASSERT_EQ(*Style1, getGoogleStyle()); 21936 } 21937 21938 TEST(FormatStyle, GetStyleOfFile) { 21939 llvm::vfs::InMemoryFileSystem FS; 21940 // Test 1: format file in the same directory. 21941 ASSERT_TRUE( 21942 FS.addFile("/a/.clang-format", 0, 21943 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 21944 ASSERT_TRUE( 21945 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21946 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 21947 ASSERT_TRUE((bool)Style1); 21948 ASSERT_EQ(*Style1, getLLVMStyle()); 21949 21950 // Test 2.1: fallback to default. 21951 ASSERT_TRUE( 21952 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21953 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 21954 ASSERT_TRUE((bool)Style2); 21955 ASSERT_EQ(*Style2, getMozillaStyle()); 21956 21957 // Test 2.2: no format on 'none' fallback style. 21958 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21959 ASSERT_TRUE((bool)Style2); 21960 ASSERT_EQ(*Style2, getNoStyle()); 21961 21962 // Test 2.3: format if config is found with no based style while fallback is 21963 // 'none'. 21964 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 21965 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 21966 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21967 ASSERT_TRUE((bool)Style2); 21968 ASSERT_EQ(*Style2, getLLVMStyle()); 21969 21970 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 21971 Style2 = getStyle("{}", "a.h", "none", "", &FS); 21972 ASSERT_TRUE((bool)Style2); 21973 ASSERT_EQ(*Style2, getLLVMStyle()); 21974 21975 // Test 3: format file in parent directory. 21976 ASSERT_TRUE( 21977 FS.addFile("/c/.clang-format", 0, 21978 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 21979 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 21980 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21981 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 21982 ASSERT_TRUE((bool)Style3); 21983 ASSERT_EQ(*Style3, getGoogleStyle()); 21984 21985 // Test 4: error on invalid fallback style 21986 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 21987 ASSERT_FALSE((bool)Style4); 21988 llvm::consumeError(Style4.takeError()); 21989 21990 // Test 5: error on invalid yaml on command line 21991 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 21992 ASSERT_FALSE((bool)Style5); 21993 llvm::consumeError(Style5.takeError()); 21994 21995 // Test 6: error on invalid style 21996 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 21997 ASSERT_FALSE((bool)Style6); 21998 llvm::consumeError(Style6.takeError()); 21999 22000 // Test 7: found config file, error on parsing it 22001 ASSERT_TRUE( 22002 FS.addFile("/d/.clang-format", 0, 22003 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 22004 "InvalidKey: InvalidValue"))); 22005 ASSERT_TRUE( 22006 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22007 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 22008 ASSERT_FALSE((bool)Style7a); 22009 llvm::consumeError(Style7a.takeError()); 22010 22011 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 22012 ASSERT_TRUE((bool)Style7b); 22013 22014 // Test 8: inferred per-language defaults apply. 22015 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 22016 ASSERT_TRUE((bool)StyleTd); 22017 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 22018 22019 // Test 9.1.1: overwriting a file style, when no parent file exists with no 22020 // fallback style. 22021 ASSERT_TRUE(FS.addFile( 22022 "/e/sub/.clang-format", 0, 22023 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 22024 "ColumnLimit: 20"))); 22025 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 22026 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22027 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 22028 ASSERT_TRUE(static_cast<bool>(Style9)); 22029 ASSERT_EQ(*Style9, [] { 22030 auto Style = getNoStyle(); 22031 Style.ColumnLimit = 20; 22032 return Style; 22033 }()); 22034 22035 // Test 9.1.2: propagate more than one level with no parent file. 22036 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 22037 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22038 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 22039 llvm::MemoryBuffer::getMemBuffer( 22040 "BasedOnStyle: InheritParentConfig\n" 22041 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 22042 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 22043 22044 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22045 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22046 ASSERT_TRUE(static_cast<bool>(Style9)); 22047 ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] { 22048 auto Style = getNoStyle(); 22049 Style.ColumnLimit = 20; 22050 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22051 return Style; 22052 }()); 22053 22054 // Test 9.2: with LLVM fallback style 22055 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 22056 ASSERT_TRUE(static_cast<bool>(Style9)); 22057 ASSERT_EQ(*Style9, [] { 22058 auto Style = getLLVMStyle(); 22059 Style.ColumnLimit = 20; 22060 return Style; 22061 }()); 22062 22063 // Test 9.3: with a parent file 22064 ASSERT_TRUE( 22065 FS.addFile("/e/.clang-format", 0, 22066 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 22067 "UseTab: Always"))); 22068 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 22069 ASSERT_TRUE(static_cast<bool>(Style9)); 22070 ASSERT_EQ(*Style9, [] { 22071 auto Style = getGoogleStyle(); 22072 Style.ColumnLimit = 20; 22073 Style.UseTab = FormatStyle::UT_Always; 22074 return Style; 22075 }()); 22076 22077 // Test 9.4: propagate more than one level with a parent file. 22078 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 22079 auto Style = getGoogleStyle(); 22080 Style.ColumnLimit = 20; 22081 Style.UseTab = FormatStyle::UT_Always; 22082 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22083 return Style; 22084 }(); 22085 22086 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22087 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22088 ASSERT_TRUE(static_cast<bool>(Style9)); 22089 ASSERT_EQ(*Style9, SubSubStyle); 22090 22091 // Test 9.5: use InheritParentConfig as style name 22092 Style9 = 22093 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 22094 ASSERT_TRUE(static_cast<bool>(Style9)); 22095 ASSERT_EQ(*Style9, SubSubStyle); 22096 22097 // Test 9.6: use command line style with inheritance 22098 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 22099 "none", "", &FS); 22100 ASSERT_TRUE(static_cast<bool>(Style9)); 22101 ASSERT_EQ(*Style9, SubSubStyle); 22102 22103 // Test 9.7: use command line style with inheritance and own config 22104 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 22105 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 22106 "/e/sub/code.cpp", "none", "", &FS); 22107 ASSERT_TRUE(static_cast<bool>(Style9)); 22108 ASSERT_EQ(*Style9, SubSubStyle); 22109 22110 // Test 9.8: use inheritance from a file without BasedOnStyle 22111 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 22112 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 22113 ASSERT_TRUE( 22114 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 22115 llvm::MemoryBuffer::getMemBuffer( 22116 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 22117 // Make sure we do not use the fallback style 22118 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 22119 ASSERT_TRUE(static_cast<bool>(Style9)); 22120 ASSERT_EQ(*Style9, [] { 22121 auto Style = getLLVMStyle(); 22122 Style.ColumnLimit = 123; 22123 return Style; 22124 }()); 22125 22126 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 22127 ASSERT_TRUE(static_cast<bool>(Style9)); 22128 ASSERT_EQ(*Style9, [] { 22129 auto Style = getLLVMStyle(); 22130 Style.ColumnLimit = 123; 22131 Style.IndentWidth = 7; 22132 return Style; 22133 }()); 22134 22135 // Test 9.9: use inheritance from a specific config file. 22136 Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp", 22137 "none", "", &FS); 22138 ASSERT_TRUE(static_cast<bool>(Style9)); 22139 ASSERT_EQ(*Style9, SubSubStyle); 22140 } 22141 22142 TEST(FormatStyle, GetStyleOfSpecificFile) { 22143 llvm::vfs::InMemoryFileSystem FS; 22144 // Specify absolute path to a format file in a parent directory. 22145 ASSERT_TRUE( 22146 FS.addFile("/e/.clang-format", 0, 22147 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 22148 ASSERT_TRUE( 22149 FS.addFile("/e/explicit.clang-format", 0, 22150 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22151 ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0, 22152 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22153 auto Style = getStyle("file:/e/explicit.clang-format", 22154 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22155 ASSERT_TRUE(static_cast<bool>(Style)); 22156 ASSERT_EQ(*Style, getGoogleStyle()); 22157 22158 // Specify relative path to a format file. 22159 ASSERT_TRUE( 22160 FS.addFile("../../e/explicit.clang-format", 0, 22161 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22162 Style = getStyle("file:../../e/explicit.clang-format", 22163 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22164 ASSERT_TRUE(static_cast<bool>(Style)); 22165 ASSERT_EQ(*Style, getGoogleStyle()); 22166 22167 // Specify path to a format file that does not exist. 22168 Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp", 22169 "LLVM", "", &FS); 22170 ASSERT_FALSE(static_cast<bool>(Style)); 22171 llvm::consumeError(Style.takeError()); 22172 22173 // Specify path to a file on the filesystem. 22174 SmallString<128> FormatFilePath; 22175 std::error_code ECF = llvm::sys::fs::createTemporaryFile( 22176 "FormatFileTest", "tpl", FormatFilePath); 22177 EXPECT_FALSE((bool)ECF); 22178 llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF); 22179 EXPECT_FALSE((bool)ECF); 22180 FormatFileTest << "BasedOnStyle: Google\n"; 22181 FormatFileTest.close(); 22182 22183 SmallString<128> TestFilePath; 22184 std::error_code ECT = 22185 llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath); 22186 EXPECT_FALSE((bool)ECT); 22187 llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT); 22188 CodeFileTest << "int i;\n"; 22189 CodeFileTest.close(); 22190 22191 std::string format_file_arg = std::string("file:") + FormatFilePath.c_str(); 22192 Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr); 22193 22194 llvm::sys::fs::remove(FormatFilePath.c_str()); 22195 llvm::sys::fs::remove(TestFilePath.c_str()); 22196 ASSERT_TRUE(static_cast<bool>(Style)); 22197 ASSERT_EQ(*Style, getGoogleStyle()); 22198 } 22199 22200 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 22201 // Column limit is 20. 22202 std::string Code = "Type *a =\n" 22203 " new Type();\n" 22204 "g(iiiii, 0, jjjjj,\n" 22205 " 0, kkkkk, 0, mm);\n" 22206 "int bad = format ;"; 22207 std::string Expected = "auto a = new Type();\n" 22208 "g(iiiii, nullptr,\n" 22209 " jjjjj, nullptr,\n" 22210 " kkkkk, nullptr,\n" 22211 " mm);\n" 22212 "int bad = format ;"; 22213 FileID ID = Context.createInMemoryFile("format.cpp", Code); 22214 tooling::Replacements Replaces = toReplacements( 22215 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 22216 "auto "), 22217 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 22218 "nullptr"), 22219 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 22220 "nullptr"), 22221 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 22222 "nullptr")}); 22223 22224 FormatStyle Style = getLLVMStyle(); 22225 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 22226 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 22227 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 22228 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 22229 auto Result = applyAllReplacements(Code, *FormattedReplaces); 22230 EXPECT_TRUE(static_cast<bool>(Result)); 22231 EXPECT_EQ(Expected, *Result); 22232 } 22233 22234 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 22235 std::string Code = "#include \"a.h\"\n" 22236 "#include \"c.h\"\n" 22237 "\n" 22238 "int main() {\n" 22239 " return 0;\n" 22240 "}"; 22241 std::string Expected = "#include \"a.h\"\n" 22242 "#include \"b.h\"\n" 22243 "#include \"c.h\"\n" 22244 "\n" 22245 "int main() {\n" 22246 " return 0;\n" 22247 "}"; 22248 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 22249 tooling::Replacements Replaces = toReplacements( 22250 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 22251 "#include \"b.h\"\n")}); 22252 22253 FormatStyle Style = getLLVMStyle(); 22254 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 22255 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 22256 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 22257 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 22258 auto Result = applyAllReplacements(Code, *FormattedReplaces); 22259 EXPECT_TRUE(static_cast<bool>(Result)); 22260 EXPECT_EQ(Expected, *Result); 22261 } 22262 22263 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 22264 EXPECT_EQ("using std::cin;\n" 22265 "using std::cout;", 22266 format("using std::cout;\n" 22267 "using std::cin;", 22268 getGoogleStyle())); 22269 } 22270 22271 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 22272 FormatStyle Style = getLLVMStyle(); 22273 Style.Standard = FormatStyle::LS_Cpp03; 22274 // cpp03 recognize this string as identifier u8 and literal character 'a' 22275 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 22276 } 22277 22278 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 22279 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 22280 // all modes, including C++11, C++14 and C++17 22281 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 22282 } 22283 22284 TEST_F(FormatTest, DoNotFormatLikelyXml) { 22285 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 22286 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 22287 } 22288 22289 TEST_F(FormatTest, StructuredBindings) { 22290 // Structured bindings is a C++17 feature. 22291 // all modes, including C++11, C++14 and C++17 22292 verifyFormat("auto [a, b] = f();"); 22293 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 22294 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 22295 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 22296 EXPECT_EQ("auto const volatile [a, b] = f();", 22297 format("auto const volatile[a, b] = f();")); 22298 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 22299 EXPECT_EQ("auto &[a, b, c] = f();", 22300 format("auto &[ a , b,c ] = f();")); 22301 EXPECT_EQ("auto &&[a, b, c] = f();", 22302 format("auto &&[ a , b,c ] = f();")); 22303 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 22304 EXPECT_EQ("auto const volatile &&[a, b] = f();", 22305 format("auto const volatile &&[a, b] = f();")); 22306 EXPECT_EQ("auto const &&[a, b] = f();", 22307 format("auto const && [a, b] = f();")); 22308 EXPECT_EQ("const auto &[a, b] = f();", 22309 format("const auto & [a, b] = f();")); 22310 EXPECT_EQ("const auto volatile &&[a, b] = f();", 22311 format("const auto volatile &&[a, b] = f();")); 22312 EXPECT_EQ("volatile const auto &&[a, b] = f();", 22313 format("volatile const auto &&[a, b] = f();")); 22314 EXPECT_EQ("const auto &&[a, b] = f();", 22315 format("const auto && [a, b] = f();")); 22316 22317 // Make sure we don't mistake structured bindings for lambdas. 22318 FormatStyle PointerMiddle = getLLVMStyle(); 22319 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 22320 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 22321 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 22322 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 22323 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 22324 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 22325 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 22326 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 22327 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 22328 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 22329 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 22330 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 22331 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 22332 22333 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 22334 format("for (const auto && [a, b] : some_range) {\n}")); 22335 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 22336 format("for (const auto & [a, b] : some_range) {\n}")); 22337 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 22338 format("for (const auto[a, b] : some_range) {\n}")); 22339 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 22340 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 22341 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 22342 EXPECT_EQ("auto const &[x, y](expr);", 22343 format("auto const & [x,y] (expr);")); 22344 EXPECT_EQ("auto const &&[x, y](expr);", 22345 format("auto const && [x,y] (expr);")); 22346 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 22347 EXPECT_EQ("auto const &[x, y]{expr};", 22348 format("auto const & [x,y] {expr};")); 22349 EXPECT_EQ("auto const &&[x, y]{expr};", 22350 format("auto const && [x,y] {expr};")); 22351 22352 FormatStyle Spaces = getLLVMStyle(); 22353 Spaces.SpacesInSquareBrackets = true; 22354 verifyFormat("auto [ a, b ] = f();", Spaces); 22355 verifyFormat("auto &&[ a, b ] = f();", Spaces); 22356 verifyFormat("auto &[ a, b ] = f();", Spaces); 22357 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 22358 verifyFormat("auto const &[ a, b ] = f();", Spaces); 22359 } 22360 22361 TEST_F(FormatTest, FileAndCode) { 22362 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 22363 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 22364 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 22365 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 22366 EXPECT_EQ(FormatStyle::LK_ObjC, 22367 guessLanguage("foo.h", "@interface Foo\n@end\n")); 22368 EXPECT_EQ( 22369 FormatStyle::LK_ObjC, 22370 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 22371 EXPECT_EQ(FormatStyle::LK_ObjC, 22372 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 22373 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 22374 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 22375 EXPECT_EQ(FormatStyle::LK_ObjC, 22376 guessLanguage("foo", "@interface Foo\n@end\n")); 22377 EXPECT_EQ(FormatStyle::LK_ObjC, 22378 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 22379 EXPECT_EQ( 22380 FormatStyle::LK_ObjC, 22381 guessLanguage("foo.h", 22382 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 22383 EXPECT_EQ( 22384 FormatStyle::LK_Cpp, 22385 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 22386 } 22387 22388 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 22389 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 22390 EXPECT_EQ(FormatStyle::LK_ObjC, 22391 guessLanguage("foo.h", "array[[calculator getIndex]];")); 22392 EXPECT_EQ(FormatStyle::LK_Cpp, 22393 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 22394 EXPECT_EQ( 22395 FormatStyle::LK_Cpp, 22396 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 22397 EXPECT_EQ(FormatStyle::LK_ObjC, 22398 guessLanguage("foo.h", "[[noreturn foo] bar];")); 22399 EXPECT_EQ(FormatStyle::LK_Cpp, 22400 guessLanguage("foo.h", "[[clang::fallthrough]];")); 22401 EXPECT_EQ(FormatStyle::LK_ObjC, 22402 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 22403 EXPECT_EQ(FormatStyle::LK_Cpp, 22404 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 22405 EXPECT_EQ(FormatStyle::LK_Cpp, 22406 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 22407 EXPECT_EQ(FormatStyle::LK_ObjC, 22408 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 22409 EXPECT_EQ(FormatStyle::LK_Cpp, 22410 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 22411 EXPECT_EQ( 22412 FormatStyle::LK_Cpp, 22413 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 22414 EXPECT_EQ( 22415 FormatStyle::LK_Cpp, 22416 guessLanguage("foo.h", 22417 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 22418 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 22419 } 22420 22421 TEST_F(FormatTest, GuessLanguageWithCaret) { 22422 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 22423 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 22424 EXPECT_EQ(FormatStyle::LK_ObjC, 22425 guessLanguage("foo.h", "int(^)(char, float);")); 22426 EXPECT_EQ(FormatStyle::LK_ObjC, 22427 guessLanguage("foo.h", "int(^foo)(char, float);")); 22428 EXPECT_EQ(FormatStyle::LK_ObjC, 22429 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 22430 EXPECT_EQ(FormatStyle::LK_ObjC, 22431 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 22432 EXPECT_EQ( 22433 FormatStyle::LK_ObjC, 22434 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 22435 } 22436 22437 TEST_F(FormatTest, GuessLanguageWithPragmas) { 22438 EXPECT_EQ(FormatStyle::LK_Cpp, 22439 guessLanguage("foo.h", "__pragma(warning(disable:))")); 22440 EXPECT_EQ(FormatStyle::LK_Cpp, 22441 guessLanguage("foo.h", "#pragma(warning(disable:))")); 22442 EXPECT_EQ(FormatStyle::LK_Cpp, 22443 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 22444 } 22445 22446 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 22447 // ASM symbolic names are identifiers that must be surrounded by [] without 22448 // space in between: 22449 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 22450 22451 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 22452 verifyFormat(R"(// 22453 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 22454 )"); 22455 22456 // A list of several ASM symbolic names. 22457 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 22458 22459 // ASM symbolic names in inline ASM with inputs and outputs. 22460 verifyFormat(R"(// 22461 asm("cmoveq %1, %2, %[result]" 22462 : [result] "=r"(result) 22463 : "r"(test), "r"(new), "[result]"(old)); 22464 )"); 22465 22466 // ASM symbolic names in inline ASM with no outputs. 22467 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 22468 } 22469 22470 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 22471 EXPECT_EQ(FormatStyle::LK_Cpp, 22472 guessLanguage("foo.h", "void f() {\n" 22473 " asm (\"mov %[e], %[d]\"\n" 22474 " : [d] \"=rm\" (d)\n" 22475 " [e] \"rm\" (*e));\n" 22476 "}")); 22477 EXPECT_EQ(FormatStyle::LK_Cpp, 22478 guessLanguage("foo.h", "void f() {\n" 22479 " _asm (\"mov %[e], %[d]\"\n" 22480 " : [d] \"=rm\" (d)\n" 22481 " [e] \"rm\" (*e));\n" 22482 "}")); 22483 EXPECT_EQ(FormatStyle::LK_Cpp, 22484 guessLanguage("foo.h", "void f() {\n" 22485 " __asm (\"mov %[e], %[d]\"\n" 22486 " : [d] \"=rm\" (d)\n" 22487 " [e] \"rm\" (*e));\n" 22488 "}")); 22489 EXPECT_EQ(FormatStyle::LK_Cpp, 22490 guessLanguage("foo.h", "void f() {\n" 22491 " __asm__ (\"mov %[e], %[d]\"\n" 22492 " : [d] \"=rm\" (d)\n" 22493 " [e] \"rm\" (*e));\n" 22494 "}")); 22495 EXPECT_EQ(FormatStyle::LK_Cpp, 22496 guessLanguage("foo.h", "void f() {\n" 22497 " asm (\"mov %[e], %[d]\"\n" 22498 " : [d] \"=rm\" (d),\n" 22499 " [e] \"rm\" (*e));\n" 22500 "}")); 22501 EXPECT_EQ(FormatStyle::LK_Cpp, 22502 guessLanguage("foo.h", "void f() {\n" 22503 " asm volatile (\"mov %[e], %[d]\"\n" 22504 " : [d] \"=rm\" (d)\n" 22505 " [e] \"rm\" (*e));\n" 22506 "}")); 22507 } 22508 22509 TEST_F(FormatTest, GuessLanguageWithChildLines) { 22510 EXPECT_EQ(FormatStyle::LK_Cpp, 22511 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 22512 EXPECT_EQ(FormatStyle::LK_ObjC, 22513 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 22514 EXPECT_EQ( 22515 FormatStyle::LK_Cpp, 22516 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 22517 EXPECT_EQ( 22518 FormatStyle::LK_ObjC, 22519 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 22520 } 22521 22522 TEST_F(FormatTest, TypenameMacros) { 22523 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 22524 22525 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 22526 FormatStyle Google = getGoogleStyleWithColumns(0); 22527 Google.TypenameMacros = TypenameMacros; 22528 verifyFormat("struct foo {\n" 22529 " int bar;\n" 22530 " TAILQ_ENTRY(a) bleh;\n" 22531 "};", 22532 Google); 22533 22534 FormatStyle Macros = getLLVMStyle(); 22535 Macros.TypenameMacros = TypenameMacros; 22536 22537 verifyFormat("STACK_OF(int) a;", Macros); 22538 verifyFormat("STACK_OF(int) *a;", Macros); 22539 verifyFormat("STACK_OF(int const *) *a;", Macros); 22540 verifyFormat("STACK_OF(int *const) *a;", Macros); 22541 verifyFormat("STACK_OF(int, string) a;", Macros); 22542 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 22543 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 22544 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 22545 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 22546 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 22547 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 22548 22549 Macros.PointerAlignment = FormatStyle::PAS_Left; 22550 verifyFormat("STACK_OF(int)* a;", Macros); 22551 verifyFormat("STACK_OF(int*)* a;", Macros); 22552 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 22553 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 22554 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 22555 } 22556 22557 TEST_F(FormatTest, AtomicQualifier) { 22558 // Check that we treate _Atomic as a type and not a function call 22559 FormatStyle Google = getGoogleStyleWithColumns(0); 22560 verifyFormat("struct foo {\n" 22561 " int a1;\n" 22562 " _Atomic(a) a2;\n" 22563 " _Atomic(_Atomic(int) *const) a3;\n" 22564 "};", 22565 Google); 22566 verifyFormat("_Atomic(uint64_t) a;"); 22567 verifyFormat("_Atomic(uint64_t) *a;"); 22568 verifyFormat("_Atomic(uint64_t const *) *a;"); 22569 verifyFormat("_Atomic(uint64_t *const) *a;"); 22570 verifyFormat("_Atomic(const uint64_t *) *a;"); 22571 verifyFormat("_Atomic(uint64_t) a;"); 22572 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 22573 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 22574 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 22575 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 22576 22577 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 22578 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 22579 FormatStyle Style = getLLVMStyle(); 22580 Style.PointerAlignment = FormatStyle::PAS_Left; 22581 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 22582 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 22583 verifyFormat("_Atomic(int)* a;", Style); 22584 verifyFormat("_Atomic(int*)* a;", Style); 22585 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 22586 22587 Style.SpacesInCStyleCastParentheses = true; 22588 Style.SpacesInParentheses = false; 22589 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 22590 Style.SpacesInCStyleCastParentheses = false; 22591 Style.SpacesInParentheses = true; 22592 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 22593 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 22594 } 22595 22596 TEST_F(FormatTest, AmbersandInLamda) { 22597 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 22598 FormatStyle AlignStyle = getLLVMStyle(); 22599 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 22600 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 22601 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 22602 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 22603 } 22604 22605 TEST_F(FormatTest, SpacesInConditionalStatement) { 22606 FormatStyle Spaces = getLLVMStyle(); 22607 Spaces.IfMacros.clear(); 22608 Spaces.IfMacros.push_back("MYIF"); 22609 Spaces.SpacesInConditionalStatement = true; 22610 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 22611 verifyFormat("if ( !a )\n return;", Spaces); 22612 verifyFormat("if ( a )\n return;", Spaces); 22613 verifyFormat("if constexpr ( a )\n return;", Spaces); 22614 verifyFormat("MYIF ( a )\n return;", Spaces); 22615 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 22616 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 22617 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 22618 verifyFormat("while ( a )\n return;", Spaces); 22619 verifyFormat("while ( (a && b) )\n return;", Spaces); 22620 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 22621 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 22622 // Check that space on the left of "::" is inserted as expected at beginning 22623 // of condition. 22624 verifyFormat("while ( ::func() )\n return;", Spaces); 22625 22626 // Check impact of ControlStatementsExceptControlMacros is honored. 22627 Spaces.SpaceBeforeParens = 22628 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 22629 verifyFormat("MYIF( a )\n return;", Spaces); 22630 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 22631 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 22632 } 22633 22634 TEST_F(FormatTest, AlternativeOperators) { 22635 // Test case for ensuring alternate operators are not 22636 // combined with their right most neighbour. 22637 verifyFormat("int a and b;"); 22638 verifyFormat("int a and_eq b;"); 22639 verifyFormat("int a bitand b;"); 22640 verifyFormat("int a bitor b;"); 22641 verifyFormat("int a compl b;"); 22642 verifyFormat("int a not b;"); 22643 verifyFormat("int a not_eq b;"); 22644 verifyFormat("int a or b;"); 22645 verifyFormat("int a xor b;"); 22646 verifyFormat("int a xor_eq b;"); 22647 verifyFormat("return this not_eq bitand other;"); 22648 verifyFormat("bool operator not_eq(const X bitand other)"); 22649 22650 verifyFormat("int a and 5;"); 22651 verifyFormat("int a and_eq 5;"); 22652 verifyFormat("int a bitand 5;"); 22653 verifyFormat("int a bitor 5;"); 22654 verifyFormat("int a compl 5;"); 22655 verifyFormat("int a not 5;"); 22656 verifyFormat("int a not_eq 5;"); 22657 verifyFormat("int a or 5;"); 22658 verifyFormat("int a xor 5;"); 22659 verifyFormat("int a xor_eq 5;"); 22660 22661 verifyFormat("int a compl(5);"); 22662 verifyFormat("int a not(5);"); 22663 22664 /* FIXME handle alternate tokens 22665 * https://en.cppreference.com/w/cpp/language/operator_alternative 22666 // alternative tokens 22667 verifyFormat("compl foo();"); // ~foo(); 22668 verifyFormat("foo() <%%>;"); // foo(); 22669 verifyFormat("void foo() <%%>;"); // void foo(){} 22670 verifyFormat("int a <:1:>;"); // int a[1];[ 22671 verifyFormat("%:define ABC abc"); // #define ABC abc 22672 verifyFormat("%:%:"); // ## 22673 */ 22674 } 22675 22676 TEST_F(FormatTest, STLWhileNotDefineChed) { 22677 verifyFormat("#if defined(while)\n" 22678 "#define while EMIT WARNING C4005\n" 22679 "#endif // while"); 22680 } 22681 22682 TEST_F(FormatTest, OperatorSpacing) { 22683 FormatStyle Style = getLLVMStyle(); 22684 Style.PointerAlignment = FormatStyle::PAS_Right; 22685 verifyFormat("Foo::operator*();", Style); 22686 verifyFormat("Foo::operator void *();", Style); 22687 verifyFormat("Foo::operator void **();", Style); 22688 verifyFormat("Foo::operator void *&();", Style); 22689 verifyFormat("Foo::operator void *&&();", Style); 22690 verifyFormat("Foo::operator void const *();", Style); 22691 verifyFormat("Foo::operator void const **();", Style); 22692 verifyFormat("Foo::operator void const *&();", Style); 22693 verifyFormat("Foo::operator void const *&&();", Style); 22694 verifyFormat("Foo::operator()(void *);", Style); 22695 verifyFormat("Foo::operator*(void *);", Style); 22696 verifyFormat("Foo::operator*();", Style); 22697 verifyFormat("Foo::operator**();", Style); 22698 verifyFormat("Foo::operator&();", Style); 22699 verifyFormat("Foo::operator<int> *();", Style); 22700 verifyFormat("Foo::operator<Foo> *();", Style); 22701 verifyFormat("Foo::operator<int> **();", Style); 22702 verifyFormat("Foo::operator<Foo> **();", Style); 22703 verifyFormat("Foo::operator<int> &();", Style); 22704 verifyFormat("Foo::operator<Foo> &();", Style); 22705 verifyFormat("Foo::operator<int> &&();", Style); 22706 verifyFormat("Foo::operator<Foo> &&();", Style); 22707 verifyFormat("Foo::operator<int> *&();", Style); 22708 verifyFormat("Foo::operator<Foo> *&();", Style); 22709 verifyFormat("Foo::operator<int> *&&();", Style); 22710 verifyFormat("Foo::operator<Foo> *&&();", Style); 22711 verifyFormat("operator*(int (*)(), class Foo);", Style); 22712 22713 verifyFormat("Foo::operator&();", Style); 22714 verifyFormat("Foo::operator void &();", Style); 22715 verifyFormat("Foo::operator void const &();", Style); 22716 verifyFormat("Foo::operator()(void &);", Style); 22717 verifyFormat("Foo::operator&(void &);", Style); 22718 verifyFormat("Foo::operator&();", Style); 22719 verifyFormat("operator&(int (&)(), class Foo);", Style); 22720 verifyFormat("operator&&(int (&)(), class Foo);", Style); 22721 22722 verifyFormat("Foo::operator&&();", Style); 22723 verifyFormat("Foo::operator**();", Style); 22724 verifyFormat("Foo::operator void &&();", Style); 22725 verifyFormat("Foo::operator void const &&();", Style); 22726 verifyFormat("Foo::operator()(void &&);", Style); 22727 verifyFormat("Foo::operator&&(void &&);", Style); 22728 verifyFormat("Foo::operator&&();", Style); 22729 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22730 verifyFormat("operator const nsTArrayRight<E> &()", Style); 22731 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 22732 Style); 22733 verifyFormat("operator void **()", Style); 22734 verifyFormat("operator const FooRight<Object> &()", Style); 22735 verifyFormat("operator const FooRight<Object> *()", Style); 22736 verifyFormat("operator const FooRight<Object> **()", Style); 22737 verifyFormat("operator const FooRight<Object> *&()", Style); 22738 verifyFormat("operator const FooRight<Object> *&&()", Style); 22739 22740 Style.PointerAlignment = FormatStyle::PAS_Left; 22741 verifyFormat("Foo::operator*();", Style); 22742 verifyFormat("Foo::operator**();", Style); 22743 verifyFormat("Foo::operator void*();", Style); 22744 verifyFormat("Foo::operator void**();", Style); 22745 verifyFormat("Foo::operator void*&();", Style); 22746 verifyFormat("Foo::operator void*&&();", Style); 22747 verifyFormat("Foo::operator void const*();", Style); 22748 verifyFormat("Foo::operator void const**();", Style); 22749 verifyFormat("Foo::operator void const*&();", Style); 22750 verifyFormat("Foo::operator void const*&&();", Style); 22751 verifyFormat("Foo::operator/*comment*/ void*();", Style); 22752 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 22753 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 22754 verifyFormat("Foo::operator()(void*);", Style); 22755 verifyFormat("Foo::operator*(void*);", Style); 22756 verifyFormat("Foo::operator*();", Style); 22757 verifyFormat("Foo::operator<int>*();", Style); 22758 verifyFormat("Foo::operator<Foo>*();", Style); 22759 verifyFormat("Foo::operator<int>**();", Style); 22760 verifyFormat("Foo::operator<Foo>**();", Style); 22761 verifyFormat("Foo::operator<Foo>*&();", Style); 22762 verifyFormat("Foo::operator<int>&();", Style); 22763 verifyFormat("Foo::operator<Foo>&();", Style); 22764 verifyFormat("Foo::operator<int>&&();", Style); 22765 verifyFormat("Foo::operator<Foo>&&();", Style); 22766 verifyFormat("Foo::operator<int>*&();", Style); 22767 verifyFormat("Foo::operator<Foo>*&();", Style); 22768 verifyFormat("operator*(int (*)(), class Foo);", Style); 22769 22770 verifyFormat("Foo::operator&();", Style); 22771 verifyFormat("Foo::operator void&();", Style); 22772 verifyFormat("Foo::operator void const&();", Style); 22773 verifyFormat("Foo::operator/*comment*/ void&();", Style); 22774 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 22775 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 22776 verifyFormat("Foo::operator()(void&);", Style); 22777 verifyFormat("Foo::operator&(void&);", Style); 22778 verifyFormat("Foo::operator&();", Style); 22779 verifyFormat("operator&(int (&)(), class Foo);", Style); 22780 verifyFormat("operator&(int (&&)(), class Foo);", Style); 22781 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22782 22783 verifyFormat("Foo::operator&&();", Style); 22784 verifyFormat("Foo::operator void&&();", Style); 22785 verifyFormat("Foo::operator void const&&();", Style); 22786 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 22787 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 22788 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 22789 verifyFormat("Foo::operator()(void&&);", Style); 22790 verifyFormat("Foo::operator&&(void&&);", Style); 22791 verifyFormat("Foo::operator&&();", Style); 22792 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22793 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 22794 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 22795 Style); 22796 verifyFormat("operator void**()", Style); 22797 verifyFormat("operator const FooLeft<Object>&()", Style); 22798 verifyFormat("operator const FooLeft<Object>*()", Style); 22799 verifyFormat("operator const FooLeft<Object>**()", Style); 22800 verifyFormat("operator const FooLeft<Object>*&()", Style); 22801 verifyFormat("operator const FooLeft<Object>*&&()", Style); 22802 22803 // PR45107 22804 verifyFormat("operator Vector<String>&();", Style); 22805 verifyFormat("operator const Vector<String>&();", Style); 22806 verifyFormat("operator foo::Bar*();", Style); 22807 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 22808 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 22809 Style); 22810 22811 Style.PointerAlignment = FormatStyle::PAS_Middle; 22812 verifyFormat("Foo::operator*();", Style); 22813 verifyFormat("Foo::operator void *();", Style); 22814 verifyFormat("Foo::operator()(void *);", Style); 22815 verifyFormat("Foo::operator*(void *);", Style); 22816 verifyFormat("Foo::operator*();", Style); 22817 verifyFormat("operator*(int (*)(), class Foo);", Style); 22818 22819 verifyFormat("Foo::operator&();", Style); 22820 verifyFormat("Foo::operator void &();", Style); 22821 verifyFormat("Foo::operator void const &();", Style); 22822 verifyFormat("Foo::operator()(void &);", Style); 22823 verifyFormat("Foo::operator&(void &);", Style); 22824 verifyFormat("Foo::operator&();", Style); 22825 verifyFormat("operator&(int (&)(), class Foo);", Style); 22826 22827 verifyFormat("Foo::operator&&();", Style); 22828 verifyFormat("Foo::operator void &&();", Style); 22829 verifyFormat("Foo::operator void const &&();", Style); 22830 verifyFormat("Foo::operator()(void &&);", Style); 22831 verifyFormat("Foo::operator&&(void &&);", Style); 22832 verifyFormat("Foo::operator&&();", Style); 22833 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22834 } 22835 22836 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 22837 FormatStyle Style = getLLVMStyle(); 22838 // PR46157 22839 verifyFormat("foo(operator+, -42);", Style); 22840 verifyFormat("foo(operator++, -42);", Style); 22841 verifyFormat("foo(operator--, -42);", Style); 22842 verifyFormat("foo(-42, operator--);", Style); 22843 verifyFormat("foo(-42, operator, );", Style); 22844 verifyFormat("foo(operator, , -42);", Style); 22845 } 22846 22847 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 22848 FormatStyle Style = getLLVMStyle(); 22849 Style.WhitespaceSensitiveMacros.push_back("FOO"); 22850 22851 // Don't use the helpers here, since 'mess up' will change the whitespace 22852 // and these are all whitespace sensitive by definition 22853 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 22854 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 22855 EXPECT_EQ( 22856 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 22857 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 22858 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 22859 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 22860 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 22861 " Still=Intentional);", 22862 format("FOO(String-ized&Messy+But,: :\n" 22863 " Still=Intentional);", 22864 Style)); 22865 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 22866 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 22867 " Still=Intentional);", 22868 format("FOO(String-ized=&Messy+But,: :\n" 22869 " Still=Intentional);", 22870 Style)); 22871 22872 Style.ColumnLimit = 21; 22873 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 22874 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 22875 } 22876 22877 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 22878 // These tests are not in NamespaceFixer because that doesn't 22879 // test its interaction with line wrapping 22880 FormatStyle Style = getLLVMStyleWithColumns(80); 22881 verifyFormat("namespace {\n" 22882 "int i;\n" 22883 "int j;\n" 22884 "} // namespace", 22885 Style); 22886 22887 verifyFormat("namespace AAA {\n" 22888 "int i;\n" 22889 "int j;\n" 22890 "} // namespace AAA", 22891 Style); 22892 22893 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 22894 "int i;\n" 22895 "int j;\n" 22896 "} // namespace Averyveryveryverylongnamespace", 22897 format("namespace Averyveryveryverylongnamespace {\n" 22898 "int i;\n" 22899 "int j;\n" 22900 "}", 22901 Style)); 22902 22903 EXPECT_EQ( 22904 "namespace " 22905 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22906 " went::mad::now {\n" 22907 "int i;\n" 22908 "int j;\n" 22909 "} // namespace\n" 22910 " // " 22911 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22912 "went::mad::now", 22913 format("namespace " 22914 "would::it::save::you::a::lot::of::time::if_::i::" 22915 "just::gave::up::and_::went::mad::now {\n" 22916 "int i;\n" 22917 "int j;\n" 22918 "}", 22919 Style)); 22920 22921 // This used to duplicate the comment again and again on subsequent runs 22922 EXPECT_EQ( 22923 "namespace " 22924 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22925 " went::mad::now {\n" 22926 "int i;\n" 22927 "int j;\n" 22928 "} // namespace\n" 22929 " // " 22930 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22931 "went::mad::now", 22932 format("namespace " 22933 "would::it::save::you::a::lot::of::time::if_::i::" 22934 "just::gave::up::and_::went::mad::now {\n" 22935 "int i;\n" 22936 "int j;\n" 22937 "} // namespace\n" 22938 " // " 22939 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 22940 "and_::went::mad::now", 22941 Style)); 22942 } 22943 22944 TEST_F(FormatTest, LikelyUnlikely) { 22945 FormatStyle Style = getLLVMStyle(); 22946 22947 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22948 " return 29;\n" 22949 "}", 22950 Style); 22951 22952 verifyFormat("if (argc > 5) [[likely]] {\n" 22953 " return 29;\n" 22954 "}", 22955 Style); 22956 22957 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22958 " return 29;\n" 22959 "} else [[likely]] {\n" 22960 " return 42;\n" 22961 "}\n", 22962 Style); 22963 22964 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22965 " return 29;\n" 22966 "} else if (argc > 10) [[likely]] {\n" 22967 " return 99;\n" 22968 "} else {\n" 22969 " return 42;\n" 22970 "}\n", 22971 Style); 22972 22973 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 22974 " return 29;\n" 22975 "}", 22976 Style); 22977 22978 verifyFormat("if (argc > 5) [[unlikely]]\n" 22979 " return 29;\n", 22980 Style); 22981 verifyFormat("if (argc > 5) [[likely]]\n" 22982 " return 29;\n", 22983 Style); 22984 22985 Style.AttributeMacros.push_back("UNLIKELY"); 22986 Style.AttributeMacros.push_back("LIKELY"); 22987 verifyFormat("if (argc > 5) UNLIKELY\n" 22988 " return 29;\n", 22989 Style); 22990 22991 verifyFormat("if (argc > 5) UNLIKELY {\n" 22992 " return 29;\n" 22993 "}", 22994 Style); 22995 verifyFormat("if (argc > 5) UNLIKELY {\n" 22996 " return 29;\n" 22997 "} else [[likely]] {\n" 22998 " return 42;\n" 22999 "}\n", 23000 Style); 23001 verifyFormat("if (argc > 5) UNLIKELY {\n" 23002 " return 29;\n" 23003 "} else LIKELY {\n" 23004 " return 42;\n" 23005 "}\n", 23006 Style); 23007 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23008 " return 29;\n" 23009 "} else LIKELY {\n" 23010 " return 42;\n" 23011 "}\n", 23012 Style); 23013 } 23014 23015 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 23016 verifyFormat("Constructor()\n" 23017 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23018 " aaaa(aaaaaaaaaaaaaaaaaa, " 23019 "aaaaaaaaaaaaaaaaaat))"); 23020 verifyFormat("Constructor()\n" 23021 " : aaaaaaaaaaaaa(aaaaaa), " 23022 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 23023 23024 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 23025 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 23026 verifyFormat("Constructor()\n" 23027 " : aaaaaa(aaaaaa),\n" 23028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23029 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 23030 StyleWithWhitespacePenalty); 23031 verifyFormat("Constructor()\n" 23032 " : aaaaaaaaaaaaa(aaaaaa), " 23033 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 23034 StyleWithWhitespacePenalty); 23035 } 23036 23037 TEST_F(FormatTest, LLVMDefaultStyle) { 23038 FormatStyle Style = getLLVMStyle(); 23039 verifyFormat("extern \"C\" {\n" 23040 "int foo();\n" 23041 "}", 23042 Style); 23043 } 23044 TEST_F(FormatTest, GNUDefaultStyle) { 23045 FormatStyle Style = getGNUStyle(); 23046 verifyFormat("extern \"C\"\n" 23047 "{\n" 23048 " int foo ();\n" 23049 "}", 23050 Style); 23051 } 23052 TEST_F(FormatTest, MozillaDefaultStyle) { 23053 FormatStyle Style = getMozillaStyle(); 23054 verifyFormat("extern \"C\"\n" 23055 "{\n" 23056 " int foo();\n" 23057 "}", 23058 Style); 23059 } 23060 TEST_F(FormatTest, GoogleDefaultStyle) { 23061 FormatStyle Style = getGoogleStyle(); 23062 verifyFormat("extern \"C\" {\n" 23063 "int foo();\n" 23064 "}", 23065 Style); 23066 } 23067 TEST_F(FormatTest, ChromiumDefaultStyle) { 23068 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 23069 verifyFormat("extern \"C\" {\n" 23070 "int foo();\n" 23071 "}", 23072 Style); 23073 } 23074 TEST_F(FormatTest, MicrosoftDefaultStyle) { 23075 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 23076 verifyFormat("extern \"C\"\n" 23077 "{\n" 23078 " int foo();\n" 23079 "}", 23080 Style); 23081 } 23082 TEST_F(FormatTest, WebKitDefaultStyle) { 23083 FormatStyle Style = getWebKitStyle(); 23084 verifyFormat("extern \"C\" {\n" 23085 "int foo();\n" 23086 "}", 23087 Style); 23088 } 23089 23090 TEST_F(FormatTest, ConceptsAndRequires) { 23091 FormatStyle Style = getLLVMStyle(); 23092 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 23093 23094 verifyFormat("template <typename T>\n" 23095 "concept Hashable = requires(T a) {\n" 23096 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 23097 "};", 23098 Style); 23099 verifyFormat("template <typename T>\n" 23100 "concept EqualityComparable = requires(T a, T b) {\n" 23101 " { a == b } -> bool;\n" 23102 "};", 23103 Style); 23104 verifyFormat("template <typename T>\n" 23105 "concept EqualityComparable = requires(T a, T b) {\n" 23106 " { a == b } -> bool;\n" 23107 " { a != b } -> bool;\n" 23108 "};", 23109 Style); 23110 verifyFormat("template <typename T>\n" 23111 "concept EqualityComparable = requires(T a, T b) {\n" 23112 " { a == b } -> bool;\n" 23113 " { a != b } -> bool;\n" 23114 "};", 23115 Style); 23116 23117 verifyFormat("template <typename It>\n" 23118 "requires Iterator<It>\n" 23119 "void sort(It begin, It end) {\n" 23120 " //....\n" 23121 "}", 23122 Style); 23123 23124 verifyFormat("template <typename T>\n" 23125 "concept Large = sizeof(T) > 10;", 23126 Style); 23127 23128 verifyFormat("template <typename T, typename U>\n" 23129 "concept FooableWith = requires(T t, U u) {\n" 23130 " typename T::foo_type;\n" 23131 " { t.foo(u) } -> typename T::foo_type;\n" 23132 " t++;\n" 23133 "};\n" 23134 "void doFoo(FooableWith<int> auto t) {\n" 23135 " t.foo(3);\n" 23136 "}", 23137 Style); 23138 verifyFormat("template <typename T>\n" 23139 "concept Context = sizeof(T) == 1;", 23140 Style); 23141 verifyFormat("template <typename T>\n" 23142 "concept Context = is_specialization_of_v<context, T>;", 23143 Style); 23144 verifyFormat("template <typename T>\n" 23145 "concept Node = std::is_object_v<T>;", 23146 Style); 23147 verifyFormat("template <typename T>\n" 23148 "concept Tree = true;", 23149 Style); 23150 23151 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 23152 " //...\n" 23153 "}", 23154 Style); 23155 23156 verifyFormat( 23157 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 23158 " //...\n" 23159 "}", 23160 Style); 23161 23162 verifyFormat( 23163 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 23164 " //...\n" 23165 "}", 23166 Style); 23167 23168 verifyFormat("template <typename T>\n" 23169 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 23170 "Concept2<I> {\n" 23171 " //...\n" 23172 "}", 23173 Style); 23174 23175 verifyFormat("template <typename T>\n" 23176 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 23177 "Concept2<I> {\n" 23178 " //...\n" 23179 "}", 23180 Style); 23181 23182 verifyFormat( 23183 "template <typename T>\n" 23184 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 23185 " //...\n" 23186 "}", 23187 Style); 23188 23189 verifyFormat( 23190 "template <typename T>\n" 23191 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 23192 " //...\n" 23193 "}", 23194 Style); 23195 23196 verifyFormat("template <typename It>\n" 23197 "requires Foo<It>() && Bar<It> {\n" 23198 " //....\n" 23199 "}", 23200 Style); 23201 23202 verifyFormat("template <typename It>\n" 23203 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 23204 " //....\n" 23205 "}", 23206 Style); 23207 23208 verifyFormat("template <typename It>\n" 23209 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 23210 " //....\n" 23211 "}", 23212 Style); 23213 23214 verifyFormat( 23215 "template <typename It>\n" 23216 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 23217 " //....\n" 23218 "}", 23219 Style); 23220 23221 Style.IndentRequires = true; 23222 verifyFormat("template <typename It>\n" 23223 " requires Iterator<It>\n" 23224 "void sort(It begin, It end) {\n" 23225 " //....\n" 23226 "}", 23227 Style); 23228 verifyFormat("template <std::size index_>\n" 23229 " requires(index_ < sizeof...(Children_))\n" 23230 "Tree auto &child() {\n" 23231 " // ...\n" 23232 "}", 23233 Style); 23234 23235 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 23236 verifyFormat("template <typename T>\n" 23237 "concept Hashable = requires (T a) {\n" 23238 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 23239 "};", 23240 Style); 23241 23242 verifyFormat("template <class T = void>\n" 23243 " requires EqualityComparable<T> || Same<T, void>\n" 23244 "struct equal_to;", 23245 Style); 23246 23247 verifyFormat("template <class T>\n" 23248 " requires requires {\n" 23249 " T{};\n" 23250 " T (int);\n" 23251 " }\n", 23252 Style); 23253 23254 Style.ColumnLimit = 78; 23255 verifyFormat("template <typename T>\n" 23256 "concept Context = Traits<typename T::traits_type> and\n" 23257 " Interface<typename T::interface_type> and\n" 23258 " Request<typename T::request_type> and\n" 23259 " Response<typename T::response_type> and\n" 23260 " ContextExtension<typename T::extension_type> and\n" 23261 " ::std::is_copy_constructable<T> and " 23262 "::std::is_move_constructable<T> and\n" 23263 " requires (T c) {\n" 23264 " { c.response; } -> Response;\n" 23265 "} and requires (T c) {\n" 23266 " { c.request; } -> Request;\n" 23267 "}\n", 23268 Style); 23269 23270 verifyFormat("template <typename T>\n" 23271 "concept Context = Traits<typename T::traits_type> or\n" 23272 " Interface<typename T::interface_type> or\n" 23273 " Request<typename T::request_type> or\n" 23274 " Response<typename T::response_type> or\n" 23275 " ContextExtension<typename T::extension_type> or\n" 23276 " ::std::is_copy_constructable<T> or " 23277 "::std::is_move_constructable<T> or\n" 23278 " requires (T c) {\n" 23279 " { c.response; } -> Response;\n" 23280 "} or requires (T c) {\n" 23281 " { c.request; } -> Request;\n" 23282 "}\n", 23283 Style); 23284 23285 verifyFormat("template <typename T>\n" 23286 "concept Context = Traits<typename T::traits_type> &&\n" 23287 " Interface<typename T::interface_type> &&\n" 23288 " Request<typename T::request_type> &&\n" 23289 " Response<typename T::response_type> &&\n" 23290 " ContextExtension<typename T::extension_type> &&\n" 23291 " ::std::is_copy_constructable<T> && " 23292 "::std::is_move_constructable<T> &&\n" 23293 " requires (T c) {\n" 23294 " { c.response; } -> Response;\n" 23295 "} && requires (T c) {\n" 23296 " { c.request; } -> Request;\n" 23297 "}\n", 23298 Style); 23299 23300 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 23301 "Constraint2<T>;"); 23302 23303 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 23304 Style.BraceWrapping.AfterFunction = true; 23305 Style.BraceWrapping.AfterClass = true; 23306 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 23307 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 23308 verifyFormat("void Foo () requires (std::copyable<T>)\n" 23309 "{\n" 23310 " return\n" 23311 "}\n", 23312 Style); 23313 23314 verifyFormat("void Foo () requires std::copyable<T>\n" 23315 "{\n" 23316 " return\n" 23317 "}\n", 23318 Style); 23319 23320 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 23321 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 23322 "struct constant;", 23323 Style); 23324 23325 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 23326 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 23327 "struct constant;", 23328 Style); 23329 23330 verifyFormat("template <class T>\n" 23331 "class plane_with_very_very_very_long_name\n" 23332 "{\n" 23333 " constexpr plane_with_very_very_very_long_name () requires " 23334 "std::copyable<T>\n" 23335 " : plane_with_very_very_very_long_name (1)\n" 23336 " {\n" 23337 " }\n" 23338 "}\n", 23339 Style); 23340 23341 verifyFormat("template <class T>\n" 23342 "class plane_with_long_name\n" 23343 "{\n" 23344 " constexpr plane_with_long_name () requires std::copyable<T>\n" 23345 " : plane_with_long_name (1)\n" 23346 " {\n" 23347 " }\n" 23348 "}\n", 23349 Style); 23350 23351 Style.BreakBeforeConceptDeclarations = false; 23352 verifyFormat("template <typename T> concept Tree = true;", Style); 23353 23354 Style.IndentRequires = false; 23355 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 23356 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 23357 "struct constant;", 23358 Style); 23359 } 23360 23361 TEST_F(FormatTest, StatementAttributeLikeMacros) { 23362 FormatStyle Style = getLLVMStyle(); 23363 StringRef Source = "void Foo::slot() {\n" 23364 " unsigned char MyChar = 'x';\n" 23365 " emit signal(MyChar);\n" 23366 " Q_EMIT signal(MyChar);\n" 23367 "}"; 23368 23369 EXPECT_EQ(Source, format(Source, Style)); 23370 23371 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 23372 EXPECT_EQ("void Foo::slot() {\n" 23373 " unsigned char MyChar = 'x';\n" 23374 " emit signal(MyChar);\n" 23375 " Q_EMIT signal(MyChar);\n" 23376 "}", 23377 format(Source, Style)); 23378 23379 Style.StatementAttributeLikeMacros.push_back("emit"); 23380 EXPECT_EQ(Source, format(Source, Style)); 23381 23382 Style.StatementAttributeLikeMacros = {}; 23383 EXPECT_EQ("void Foo::slot() {\n" 23384 " unsigned char MyChar = 'x';\n" 23385 " emit signal(MyChar);\n" 23386 " Q_EMIT signal(MyChar);\n" 23387 "}", 23388 format(Source, Style)); 23389 } 23390 23391 TEST_F(FormatTest, IndentAccessModifiers) { 23392 FormatStyle Style = getLLVMStyle(); 23393 Style.IndentAccessModifiers = true; 23394 // Members are *two* levels below the record; 23395 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 23396 verifyFormat("class C {\n" 23397 " int i;\n" 23398 "};\n", 23399 Style); 23400 verifyFormat("union C {\n" 23401 " int i;\n" 23402 " unsigned u;\n" 23403 "};\n", 23404 Style); 23405 // Access modifiers should be indented one level below the record. 23406 verifyFormat("class C {\n" 23407 " public:\n" 23408 " int i;\n" 23409 "};\n", 23410 Style); 23411 verifyFormat("struct S {\n" 23412 " private:\n" 23413 " class C {\n" 23414 " int j;\n" 23415 "\n" 23416 " public:\n" 23417 " C();\n" 23418 " };\n" 23419 "\n" 23420 " public:\n" 23421 " int i;\n" 23422 "};\n", 23423 Style); 23424 // Enumerations are not records and should be unaffected. 23425 Style.AllowShortEnumsOnASingleLine = false; 23426 verifyFormat("enum class E {\n" 23427 " A,\n" 23428 " B\n" 23429 "};\n", 23430 Style); 23431 // Test with a different indentation width; 23432 // also proves that the result is Style.AccessModifierOffset agnostic. 23433 Style.IndentWidth = 3; 23434 verifyFormat("class C {\n" 23435 " public:\n" 23436 " int i;\n" 23437 "};\n", 23438 Style); 23439 } 23440 23441 TEST_F(FormatTest, LimitlessStringsAndComments) { 23442 auto Style = getLLVMStyleWithColumns(0); 23443 constexpr StringRef Code = 23444 "/**\n" 23445 " * This is a multiline comment with quite some long lines, at least for " 23446 "the LLVM Style.\n" 23447 " * We will redo this with strings and line comments. Just to check if " 23448 "everything is working.\n" 23449 " */\n" 23450 "bool foo() {\n" 23451 " /* Single line multi line comment. */\n" 23452 " const std::string String = \"This is a multiline string with quite " 23453 "some long lines, at least for the LLVM Style.\"\n" 23454 " \"We already did it with multi line " 23455 "comments, and we will do it with line comments. Just to check if " 23456 "everything is working.\";\n" 23457 " // This is a line comment (block) with quite some long lines, at " 23458 "least for the LLVM Style.\n" 23459 " // We already did this with multi line comments and strings. Just to " 23460 "check if everything is working.\n" 23461 " const std::string SmallString = \"Hello World\";\n" 23462 " // Small line comment\n" 23463 " return String.size() > SmallString.size();\n" 23464 "}"; 23465 EXPECT_EQ(Code, format(Code, Style)); 23466 } 23467 23468 TEST_F(FormatTest, FormatDecayCopy) { 23469 // error cases from unit tests 23470 verifyFormat("foo(auto())"); 23471 verifyFormat("foo(auto{})"); 23472 verifyFormat("foo(auto({}))"); 23473 verifyFormat("foo(auto{{}})"); 23474 23475 verifyFormat("foo(auto(1))"); 23476 verifyFormat("foo(auto{1})"); 23477 verifyFormat("foo(new auto(1))"); 23478 verifyFormat("foo(new auto{1})"); 23479 verifyFormat("decltype(auto(1)) x;"); 23480 verifyFormat("decltype(auto{1}) x;"); 23481 verifyFormat("auto(x);"); 23482 verifyFormat("auto{x};"); 23483 verifyFormat("new auto{x};"); 23484 verifyFormat("auto{x} = y;"); 23485 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 23486 // the user's own fault 23487 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 23488 // clearly the user's own fault 23489 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 23490 } 23491 23492 TEST_F(FormatTest, Cpp20ModulesSupport) { 23493 FormatStyle Style = getLLVMStyle(); 23494 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 23495 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 23496 23497 verifyFormat("export import foo;", Style); 23498 verifyFormat("export import foo:bar;", Style); 23499 verifyFormat("export import foo.bar;", Style); 23500 verifyFormat("export import foo.bar:baz;", Style); 23501 verifyFormat("export import :bar;", Style); 23502 verifyFormat("export module foo:bar;", Style); 23503 verifyFormat("export module foo;", Style); 23504 verifyFormat("export module foo.bar;", Style); 23505 verifyFormat("export module foo.bar:baz;", Style); 23506 verifyFormat("export import <string_view>;", Style); 23507 23508 verifyFormat("export type_name var;", Style); 23509 verifyFormat("template <class T> export using A = B<T>;", Style); 23510 verifyFormat("export using A = B;", Style); 23511 verifyFormat("export int func() {\n" 23512 " foo();\n" 23513 "}", 23514 Style); 23515 verifyFormat("export struct {\n" 23516 " int foo;\n" 23517 "};", 23518 Style); 23519 verifyFormat("export {\n" 23520 " int foo;\n" 23521 "};", 23522 Style); 23523 verifyFormat("export export char const *hello() { return \"hello\"; }"); 23524 23525 verifyFormat("import bar;", Style); 23526 verifyFormat("import foo.bar;", Style); 23527 verifyFormat("import foo:bar;", Style); 23528 verifyFormat("import :bar;", Style); 23529 verifyFormat("import <ctime>;", Style); 23530 verifyFormat("import \"header\";", Style); 23531 23532 verifyFormat("module foo;", Style); 23533 verifyFormat("module foo:bar;", Style); 23534 verifyFormat("module foo.bar;", Style); 23535 verifyFormat("module;", Style); 23536 23537 verifyFormat("export namespace hi {\n" 23538 "const char *sayhi();\n" 23539 "}", 23540 Style); 23541 23542 verifyFormat("module :private;", Style); 23543 verifyFormat("import <foo/bar.h>;", Style); 23544 verifyFormat("import foo...bar;", Style); 23545 verifyFormat("import ..........;", Style); 23546 verifyFormat("module foo:private;", Style); 23547 verifyFormat("import a", Style); 23548 verifyFormat("module a", Style); 23549 verifyFormat("export import a", Style); 23550 verifyFormat("export module a", Style); 23551 23552 verifyFormat("import", Style); 23553 verifyFormat("module", Style); 23554 verifyFormat("export", Style); 23555 } 23556 23557 TEST_F(FormatTest, CoroutineForCoawait) { 23558 FormatStyle Style = getLLVMStyle(); 23559 verifyFormat("for co_await (auto x : range())\n ;"); 23560 verifyFormat("for (auto i : arr) {\n" 23561 "}", 23562 Style); 23563 verifyFormat("for co_await (auto i : arr) {\n" 23564 "}", 23565 Style); 23566 verifyFormat("for co_await (auto i : foo(T{})) {\n" 23567 "}", 23568 Style); 23569 } 23570 23571 TEST_F(FormatTest, CoroutineCoAwait) { 23572 verifyFormat("int x = co_await foo();"); 23573 verifyFormat("int x = (co_await foo());"); 23574 verifyFormat("co_await (42);"); 23575 verifyFormat("void operator co_await(int);"); 23576 verifyFormat("void operator co_await(a);"); 23577 verifyFormat("co_await a;"); 23578 verifyFormat("co_await missing_await_resume{};"); 23579 verifyFormat("co_await a; // comment"); 23580 verifyFormat("void test0() { co_await a; }"); 23581 verifyFormat("co_await co_await co_await foo();"); 23582 verifyFormat("co_await foo().bar();"); 23583 verifyFormat("co_await [this]() -> Task { co_return x; }"); 23584 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 23585 "foo(); }(x, y);"); 23586 23587 FormatStyle Style = getLLVMStyleWithColumns(40); 23588 verifyFormat("co_await [this](int a, int b) -> Task {\n" 23589 " co_return co_await foo();\n" 23590 "}(x, y);", 23591 Style); 23592 verifyFormat("co_await;"); 23593 } 23594 23595 TEST_F(FormatTest, CoroutineCoYield) { 23596 verifyFormat("int x = co_yield foo();"); 23597 verifyFormat("int x = (co_yield foo());"); 23598 verifyFormat("co_yield (42);"); 23599 verifyFormat("co_yield {42};"); 23600 verifyFormat("co_yield 42;"); 23601 verifyFormat("co_yield n++;"); 23602 verifyFormat("co_yield ++n;"); 23603 verifyFormat("co_yield;"); 23604 } 23605 23606 TEST_F(FormatTest, CoroutineCoReturn) { 23607 verifyFormat("co_return (42);"); 23608 verifyFormat("co_return;"); 23609 verifyFormat("co_return {};"); 23610 verifyFormat("co_return x;"); 23611 verifyFormat("co_return co_await foo();"); 23612 verifyFormat("co_return co_yield foo();"); 23613 } 23614 23615 TEST_F(FormatTest, EmptyShortBlock) { 23616 auto Style = getLLVMStyle(); 23617 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 23618 23619 verifyFormat("try {\n" 23620 " doA();\n" 23621 "} catch (Exception &e) {\n" 23622 " e.printStackTrace();\n" 23623 "}\n", 23624 Style); 23625 23626 verifyFormat("try {\n" 23627 " doA();\n" 23628 "} catch (Exception &e) {}\n", 23629 Style); 23630 } 23631 23632 TEST_F(FormatTest, ShortTemplatedArgumentLists) { 23633 auto Style = getLLVMStyle(); 23634 23635 verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style); 23636 verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style); 23637 verifyFormat("struct Y : X<[] { return 0; }> {};", Style); 23638 verifyFormat("struct Y<[] { return 0; }> {};", Style); 23639 23640 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style); 23641 } 23642 23643 TEST_F(FormatTest, RemoveBraces) { 23644 FormatStyle Style = getLLVMStyle(); 23645 Style.RemoveBracesLLVM = true; 23646 23647 // The following eight test cases are fully-braced versions of the examples at 23648 // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single- 23649 // statement-bodies-of-if-else-loop-statements". 23650 23651 // 1. Omit the braces, since the body is simple and clearly associated with 23652 // the if. 23653 verifyFormat("if (isa<FunctionDecl>(D))\n" 23654 " handleFunctionDecl(D);\n" 23655 "else if (isa<VarDecl>(D))\n" 23656 " handleVarDecl(D);", 23657 "if (isa<FunctionDecl>(D)) {\n" 23658 " handleFunctionDecl(D);\n" 23659 "} else if (isa<VarDecl>(D)) {\n" 23660 " handleVarDecl(D);\n" 23661 "}", 23662 Style); 23663 23664 // 2. Here we document the condition itself and not the body. 23665 verifyFormat("if (isa<VarDecl>(D)) {\n" 23666 " // It is necessary that we explain the situation with this\n" 23667 " // surprisingly long comment, so it would be unclear\n" 23668 " // without the braces whether the following statement is in\n" 23669 " // the scope of the `if`.\n" 23670 " // Because the condition is documented, we can't really\n" 23671 " // hoist this comment that applies to the body above the\n" 23672 " // if.\n" 23673 " handleOtherDecl(D);\n" 23674 "}", 23675 Style); 23676 23677 // 3. Use braces on the outer `if` to avoid a potential dangling else 23678 // situation. 23679 verifyFormat("if (isa<VarDecl>(D)) {\n" 23680 " for (auto *A : D.attrs())\n" 23681 " if (shouldProcessAttr(A))\n" 23682 " handleAttr(A);\n" 23683 "}", 23684 "if (isa<VarDecl>(D)) {\n" 23685 " for (auto *A : D.attrs()) {\n" 23686 " if (shouldProcessAttr(A)) {\n" 23687 " handleAttr(A);\n" 23688 " }\n" 23689 " }\n" 23690 "}", 23691 Style); 23692 23693 // 4. Use braces for the `if` block to keep it uniform with the else block. 23694 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 23695 " handleFunctionDecl(D);\n" 23696 "} else {\n" 23697 " // In this else case, it is necessary that we explain the\n" 23698 " // situation with this surprisingly long comment, so it\n" 23699 " // would be unclear without the braces whether the\n" 23700 " // following statement is in the scope of the `if`.\n" 23701 " handleOtherDecl(D);\n" 23702 "}", 23703 Style); 23704 23705 // 5. This should also omit braces. The `for` loop contains only a single 23706 // statement, so it shouldn't have braces. The `if` also only contains a 23707 // single simple statement (the for loop), so it also should omit braces. 23708 verifyFormat("if (isa<FunctionDecl>(D))\n" 23709 " for (auto *A : D.attrs())\n" 23710 " handleAttr(A);", 23711 "if (isa<FunctionDecl>(D)) {\n" 23712 " for (auto *A : D.attrs()) {\n" 23713 " handleAttr(A);\n" 23714 " }\n" 23715 "}", 23716 Style); 23717 23718 // 6. Use braces for the outer `if` since the nested `for` is braced. 23719 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 23720 " for (auto *A : D.attrs()) {\n" 23721 " // In this for loop body, it is necessary that we explain\n" 23722 " // the situation with this surprisingly long comment,\n" 23723 " // forcing braces on the `for` block.\n" 23724 " handleAttr(A);\n" 23725 " }\n" 23726 "}", 23727 Style); 23728 23729 // 7. Use braces on the outer block because there are more than two levels of 23730 // nesting. 23731 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 23732 " for (auto *A : D.attrs())\n" 23733 " for (ssize_t i : llvm::seq<ssize_t>(count))\n" 23734 " handleAttrOnDecl(D, A, i);\n" 23735 "}", 23736 "if (isa<FunctionDecl>(D)) {\n" 23737 " for (auto *A : D.attrs()) {\n" 23738 " for (ssize_t i : llvm::seq<ssize_t>(count)) {\n" 23739 " handleAttrOnDecl(D, A, i);\n" 23740 " }\n" 23741 " }\n" 23742 "}", 23743 Style); 23744 23745 // 8. Use braces on the outer block because of a nested `if`, otherwise the 23746 // compiler would warn: `add explicit braces to avoid dangling else` 23747 verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 23748 " if (shouldProcess(D))\n" 23749 " handleVarDecl(D);\n" 23750 " else\n" 23751 " markAsIgnored(D);\n" 23752 "}", 23753 "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 23754 " if (shouldProcess(D)) {\n" 23755 " handleVarDecl(D);\n" 23756 " } else {\n" 23757 " markAsIgnored(D);\n" 23758 " }\n" 23759 "}", 23760 Style); 23761 23762 verifyFormat("if (a)\n" 23763 " b; // comment\n" 23764 "else if (c)\n" 23765 " d; /* comment */\n" 23766 "else\n" 23767 " e;", 23768 "if (a) {\n" 23769 " b; // comment\n" 23770 "} else if (c) {\n" 23771 " d; /* comment */\n" 23772 "} else {\n" 23773 " e;\n" 23774 "}", 23775 Style); 23776 23777 verifyFormat("if (a) {\n" 23778 " b;\n" 23779 " c;\n" 23780 "} else if (d) {\n" 23781 " e;\n" 23782 "}", 23783 Style); 23784 23785 verifyFormat("if (a) {\n" 23786 "#undef NDEBUG\n" 23787 " b;\n" 23788 "} else {\n" 23789 " c;\n" 23790 "}", 23791 Style); 23792 23793 verifyFormat("if (a) {\n" 23794 " // comment\n" 23795 "} else if (b) {\n" 23796 " c;\n" 23797 "}", 23798 Style); 23799 23800 verifyFormat("if (a) {\n" 23801 " b;\n" 23802 "} else {\n" 23803 " { c; }\n" 23804 "}", 23805 Style); 23806 23807 verifyFormat("if (a) {\n" 23808 " if (b) // comment\n" 23809 " c;\n" 23810 "} else if (d) {\n" 23811 " e;\n" 23812 "}", 23813 "if (a) {\n" 23814 " if (b) { // comment\n" 23815 " c;\n" 23816 " }\n" 23817 "} else if (d) {\n" 23818 " e;\n" 23819 "}", 23820 Style); 23821 23822 verifyFormat("if (a) {\n" 23823 " if (b) {\n" 23824 " c;\n" 23825 " // comment\n" 23826 " } else if (d) {\n" 23827 " e;\n" 23828 " }\n" 23829 "}", 23830 Style); 23831 23832 verifyFormat("if (a) {\n" 23833 " if (b)\n" 23834 " c;\n" 23835 "}", 23836 "if (a) {\n" 23837 " if (b) {\n" 23838 " c;\n" 23839 " }\n" 23840 "}", 23841 Style); 23842 23843 verifyFormat("if (a)\n" 23844 " if (b)\n" 23845 " c;\n" 23846 " else\n" 23847 " d;\n" 23848 "else\n" 23849 " e;", 23850 "if (a) {\n" 23851 " if (b) {\n" 23852 " c;\n" 23853 " } else {\n" 23854 " d;\n" 23855 " }\n" 23856 "} else {\n" 23857 " e;\n" 23858 "}", 23859 Style); 23860 23861 verifyFormat("if (a) {\n" 23862 " // comment\n" 23863 " if (b)\n" 23864 " c;\n" 23865 " else if (d)\n" 23866 " e;\n" 23867 "} else {\n" 23868 " g;\n" 23869 "}", 23870 "if (a) {\n" 23871 " // comment\n" 23872 " if (b) {\n" 23873 " c;\n" 23874 " } else if (d) {\n" 23875 " e;\n" 23876 " }\n" 23877 "} else {\n" 23878 " g;\n" 23879 "}", 23880 Style); 23881 23882 verifyFormat("if (a)\n" 23883 " b;\n" 23884 "else if (c)\n" 23885 " d;\n" 23886 "else\n" 23887 " e;", 23888 "if (a) {\n" 23889 " b;\n" 23890 "} else {\n" 23891 " if (c) {\n" 23892 " d;\n" 23893 " } else {\n" 23894 " e;\n" 23895 " }\n" 23896 "}", 23897 Style); 23898 23899 verifyFormat("if (a) {\n" 23900 " if (b)\n" 23901 " c;\n" 23902 " else if (d)\n" 23903 " e;\n" 23904 "} else {\n" 23905 " g;\n" 23906 "}", 23907 "if (a) {\n" 23908 " if (b)\n" 23909 " c;\n" 23910 " else {\n" 23911 " if (d)\n" 23912 " e;\n" 23913 " }\n" 23914 "} else {\n" 23915 " g;\n" 23916 "}", 23917 Style); 23918 23919 verifyFormat("if (a)\n" 23920 " b;\n" 23921 "else if (c)\n" 23922 " while (d)\n" 23923 " e;\n" 23924 "// comment", 23925 "if (a)\n" 23926 "{\n" 23927 " b;\n" 23928 "} else if (c) {\n" 23929 " while (d) {\n" 23930 " e;\n" 23931 " }\n" 23932 "}\n" 23933 "// comment", 23934 Style); 23935 23936 verifyFormat("if (a) {\n" 23937 " b;\n" 23938 "} else if (c) {\n" 23939 " d;\n" 23940 "} else {\n" 23941 " e;\n" 23942 " g;\n" 23943 "}", 23944 Style); 23945 23946 verifyFormat("if (a) {\n" 23947 " b;\n" 23948 "} else if (c) {\n" 23949 " d;\n" 23950 "} else {\n" 23951 " e;\n" 23952 "} // comment", 23953 Style); 23954 23955 verifyFormat("int abs = [](int i) {\n" 23956 " if (i >= 0)\n" 23957 " return i;\n" 23958 " return -i;\n" 23959 "};", 23960 "int abs = [](int i) {\n" 23961 " if (i >= 0) {\n" 23962 " return i;\n" 23963 " }\n" 23964 " return -i;\n" 23965 "};", 23966 Style); 23967 23968 Style.ColumnLimit = 20; 23969 23970 verifyFormat("if (a) {\n" 23971 " b = c + // 1 -\n" 23972 " d;\n" 23973 "}", 23974 Style); 23975 23976 verifyFormat("if (a) {\n" 23977 " b = c >= 0 ? d\n" 23978 " : e;\n" 23979 "}", 23980 "if (a) {\n" 23981 " b = c >= 0 ? d : e;\n" 23982 "}", 23983 Style); 23984 23985 verifyFormat("if (a)\n" 23986 " b = c > 0 ? d : e;", 23987 "if (a) {\n" 23988 " b = c > 0 ? d : e;\n" 23989 "}", 23990 Style); 23991 23992 Style.ColumnLimit = 0; 23993 23994 verifyFormat("if (a)\n" 23995 " b234567890223456789032345678904234567890 = " 23996 "c234567890223456789032345678904234567890;", 23997 "if (a) {\n" 23998 " b234567890223456789032345678904234567890 = " 23999 "c234567890223456789032345678904234567890;\n" 24000 "}", 24001 Style); 24002 } 24003 24004 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { 24005 auto Style = getLLVMStyle(); 24006 24007 StringRef Short = "functionCall(paramA, paramB, paramC);\n" 24008 "void functionDecl(int a, int b, int c);"; 24009 24010 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, " 24011 "paramF, paramG, paramH, paramI);\n" 24012 "void functionDecl(int argumentA, int argumentB, int " 24013 "argumentC, int argumentD, int argumentE);"; 24014 24015 verifyFormat(Short, Style); 24016 24017 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, " 24018 "paramF, paramG, paramH,\n" 24019 " paramI);\n" 24020 "void functionDecl(int argumentA, int argumentB, int " 24021 "argumentC, int argumentD,\n" 24022 " int argumentE);"; 24023 24024 verifyFormat(NoBreak, Medium, Style); 24025 verifyFormat(NoBreak, 24026 "functionCall(\n" 24027 " paramA,\n" 24028 " paramB,\n" 24029 " paramC,\n" 24030 " paramD,\n" 24031 " paramE,\n" 24032 " paramF,\n" 24033 " paramG,\n" 24034 " paramH,\n" 24035 " paramI\n" 24036 ");\n" 24037 "void functionDecl(\n" 24038 " int argumentA,\n" 24039 " int argumentB,\n" 24040 " int argumentC,\n" 24041 " int argumentD,\n" 24042 " int argumentE\n" 24043 ");", 24044 Style); 24045 24046 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n" 24047 " nestedLongFunctionCall(argument1, " 24048 "argument2, argument3,\n" 24049 " argument4, " 24050 "argument5));", 24051 Style); 24052 24053 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24054 24055 verifyFormat(Short, Style); 24056 verifyFormat( 24057 "functionCall(\n" 24058 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 24059 "paramI\n" 24060 ");\n" 24061 "void functionDecl(\n" 24062 " int argumentA, int argumentB, int argumentC, int argumentD, int " 24063 "argumentE\n" 24064 ");", 24065 Medium, Style); 24066 24067 Style.AllowAllArgumentsOnNextLine = false; 24068 Style.AllowAllParametersOfDeclarationOnNextLine = false; 24069 24070 verifyFormat(Short, Style); 24071 verifyFormat( 24072 "functionCall(\n" 24073 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 24074 "paramI\n" 24075 ");\n" 24076 "void functionDecl(\n" 24077 " int argumentA, int argumentB, int argumentC, int argumentD, int " 24078 "argumentE\n" 24079 ");", 24080 Medium, Style); 24081 24082 Style.BinPackArguments = false; 24083 Style.BinPackParameters = false; 24084 24085 verifyFormat(Short, Style); 24086 24087 verifyFormat("functionCall(\n" 24088 " paramA,\n" 24089 " paramB,\n" 24090 " paramC,\n" 24091 " paramD,\n" 24092 " paramE,\n" 24093 " paramF,\n" 24094 " paramG,\n" 24095 " paramH,\n" 24096 " paramI\n" 24097 ");\n" 24098 "void functionDecl(\n" 24099 " int argumentA,\n" 24100 " int argumentB,\n" 24101 " int argumentC,\n" 24102 " int argumentD,\n" 24103 " int argumentE\n" 24104 ");", 24105 Medium, Style); 24106 24107 verifyFormat("outerFunctionCall(\n" 24108 " nestedFunctionCall(argument1),\n" 24109 " nestedLongFunctionCall(\n" 24110 " argument1,\n" 24111 " argument2,\n" 24112 " argument3,\n" 24113 " argument4,\n" 24114 " argument5\n" 24115 " )\n" 24116 ");", 24117 Style); 24118 24119 verifyFormat("int a = (int)b;", Style); 24120 verifyFormat("int a = (int)b;", 24121 "int a = (\n" 24122 " int\n" 24123 ") b;", 24124 Style); 24125 24126 verifyFormat("return (true);", Style); 24127 verifyFormat("return (true);", 24128 "return (\n" 24129 " true\n" 24130 ");", 24131 Style); 24132 24133 verifyFormat("void foo();", Style); 24134 verifyFormat("void foo();", 24135 "void foo(\n" 24136 ");", 24137 Style); 24138 24139 verifyFormat("void foo() {}", Style); 24140 verifyFormat("void foo() {}", 24141 "void foo(\n" 24142 ") {\n" 24143 "}", 24144 Style); 24145 24146 verifyFormat("auto string = std::string();", Style); 24147 verifyFormat("auto string = std::string();", 24148 "auto string = std::string(\n" 24149 ");", 24150 Style); 24151 24152 verifyFormat("void (*functionPointer)() = nullptr;", Style); 24153 verifyFormat("void (*functionPointer)() = nullptr;", 24154 "void (\n" 24155 " *functionPointer\n" 24156 ")\n" 24157 "(\n" 24158 ") = nullptr;", 24159 Style); 24160 } 24161 24162 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) { 24163 auto Style = getLLVMStyle(); 24164 24165 verifyFormat("if (foo()) {\n" 24166 " return;\n" 24167 "}", 24168 Style); 24169 24170 verifyFormat("if (quitelongarg !=\n" 24171 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 24172 "comment\n" 24173 " return;\n" 24174 "}", 24175 Style); 24176 24177 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24178 24179 verifyFormat("if (foo()) {\n" 24180 " return;\n" 24181 "}", 24182 Style); 24183 24184 verifyFormat("if (quitelongarg !=\n" 24185 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 24186 "comment\n" 24187 " return;\n" 24188 "}", 24189 Style); 24190 } 24191 24192 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) { 24193 auto Style = getLLVMStyle(); 24194 24195 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 24196 " doSomething();\n" 24197 "}", 24198 Style); 24199 24200 verifyFormat("for (int myReallyLongCountVariable = 0; " 24201 "myReallyLongCountVariable < count;\n" 24202 " myReallyLongCountVariable++) {\n" 24203 " doSomething();\n" 24204 "}", 24205 Style); 24206 24207 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24208 24209 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 24210 " doSomething();\n" 24211 "}", 24212 Style); 24213 24214 verifyFormat("for (int myReallyLongCountVariable = 0; " 24215 "myReallyLongCountVariable < count;\n" 24216 " myReallyLongCountVariable++) {\n" 24217 " doSomething();\n" 24218 "}", 24219 Style); 24220 } 24221 24222 } // namespace 24223 } // namespace format 24224 } // namespace clang 24225