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 consteval {\n}"); 587 verifyFormat("if !consteval {\n}"); 588 verifyFormat("if not consteval {\n}"); 589 verifyFormat("if consteval {\n} else {\n}"); 590 verifyFormat("if !consteval {\n} else {\n}"); 591 verifyFormat("if consteval {\n" 592 " f();\n" 593 "}"); 594 verifyFormat("if !consteval {\n" 595 " f();\n" 596 "}"); 597 verifyFormat("if consteval {\n" 598 " f();\n" 599 "} else {\n" 600 " g();\n" 601 "}"); 602 verifyFormat("if CONSTEVAL {\n" 603 " f();\n" 604 "}"); 605 verifyFormat("if !CONSTEVAL {\n" 606 " f();\n" 607 "}"); 608 609 verifyFormat("if (a)\n" 610 " g();"); 611 verifyFormat("if (a) {\n" 612 " g()\n" 613 "};"); 614 verifyFormat("if (a)\n" 615 " g();\n" 616 "else\n" 617 " g();"); 618 verifyFormat("if (a) {\n" 619 " g();\n" 620 "} else\n" 621 " g();"); 622 verifyFormat("if (a)\n" 623 " g();\n" 624 "else {\n" 625 " g();\n" 626 "}"); 627 verifyFormat("if (a) {\n" 628 " g();\n" 629 "} else {\n" 630 " g();\n" 631 "}"); 632 verifyFormat("if (a)\n" 633 " g();\n" 634 "else if (b)\n" 635 " g();\n" 636 "else\n" 637 " g();"); 638 verifyFormat("if (a) {\n" 639 " g();\n" 640 "} else if (b)\n" 641 " g();\n" 642 "else\n" 643 " g();"); 644 verifyFormat("if (a)\n" 645 " g();\n" 646 "else if (b) {\n" 647 " g();\n" 648 "} else\n" 649 " g();"); 650 verifyFormat("if (a)\n" 651 " g();\n" 652 "else if (b)\n" 653 " g();\n" 654 "else {\n" 655 " g();\n" 656 "}"); 657 verifyFormat("if (a)\n" 658 " g();\n" 659 "else if (b) {\n" 660 " g();\n" 661 "} else {\n" 662 " g();\n" 663 "}"); 664 verifyFormat("if (a) {\n" 665 " g();\n" 666 "} else if (b) {\n" 667 " g();\n" 668 "} else {\n" 669 " g();\n" 670 "}"); 671 672 FormatStyle AllowsMergedIf = getLLVMStyle(); 673 AllowsMergedIf.IfMacros.push_back("MYIF"); 674 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 675 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 676 FormatStyle::SIS_WithoutElse; 677 verifyFormat("if (a)\n" 678 " // comment\n" 679 " f();", 680 AllowsMergedIf); 681 verifyFormat("{\n" 682 " if (a)\n" 683 " label:\n" 684 " f();\n" 685 "}", 686 AllowsMergedIf); 687 verifyFormat("#define A \\\n" 688 " if (a) \\\n" 689 " label: \\\n" 690 " f()", 691 AllowsMergedIf); 692 verifyFormat("if (a)\n" 693 " ;", 694 AllowsMergedIf); 695 verifyFormat("if (a)\n" 696 " if (b) return;", 697 AllowsMergedIf); 698 699 verifyFormat("if (a) // Can't merge this\n" 700 " f();\n", 701 AllowsMergedIf); 702 verifyFormat("if (a) /* still don't merge */\n" 703 " f();", 704 AllowsMergedIf); 705 verifyFormat("if (a) { // Never merge this\n" 706 " f();\n" 707 "}", 708 AllowsMergedIf); 709 verifyFormat("if (a) { /* Never merge this */\n" 710 " f();\n" 711 "}", 712 AllowsMergedIf); 713 verifyFormat("MYIF (a)\n" 714 " // comment\n" 715 " f();", 716 AllowsMergedIf); 717 verifyFormat("{\n" 718 " MYIF (a)\n" 719 " label:\n" 720 " f();\n" 721 "}", 722 AllowsMergedIf); 723 verifyFormat("#define A \\\n" 724 " MYIF (a) \\\n" 725 " label: \\\n" 726 " f()", 727 AllowsMergedIf); 728 verifyFormat("MYIF (a)\n" 729 " ;", 730 AllowsMergedIf); 731 verifyFormat("MYIF (a)\n" 732 " MYIF (b) return;", 733 AllowsMergedIf); 734 735 verifyFormat("MYIF (a) // Can't merge this\n" 736 " f();\n", 737 AllowsMergedIf); 738 verifyFormat("MYIF (a) /* still don't merge */\n" 739 " f();", 740 AllowsMergedIf); 741 verifyFormat("MYIF (a) { // Never merge this\n" 742 " f();\n" 743 "}", 744 AllowsMergedIf); 745 verifyFormat("MYIF (a) { /* Never merge this */\n" 746 " f();\n" 747 "}", 748 AllowsMergedIf); 749 750 AllowsMergedIf.ColumnLimit = 14; 751 // Where line-lengths matter, a 2-letter synonym that maintains line length. 752 // Not IF to avoid any confusion that IF is somehow special. 753 AllowsMergedIf.IfMacros.push_back("FI"); 754 verifyFormat("if (a) return;", AllowsMergedIf); 755 verifyFormat("if (aaaaaaaaa)\n" 756 " return;", 757 AllowsMergedIf); 758 verifyFormat("FI (a) return;", AllowsMergedIf); 759 verifyFormat("FI (aaaaaaaaa)\n" 760 " return;", 761 AllowsMergedIf); 762 763 AllowsMergedIf.ColumnLimit = 13; 764 verifyFormat("if (a)\n return;", AllowsMergedIf); 765 verifyFormat("FI (a)\n return;", AllowsMergedIf); 766 767 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 768 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 769 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 770 FormatStyle::SIS_AllIfsAndElse; 771 verifyFormat("if (a)\n" 772 " // comment\n" 773 " f();\n" 774 "else\n" 775 " // comment\n" 776 " f();", 777 AllowsMergedIfElse); 778 verifyFormat("{\n" 779 " if (a)\n" 780 " label:\n" 781 " f();\n" 782 " else\n" 783 " label:\n" 784 " f();\n" 785 "}", 786 AllowsMergedIfElse); 787 verifyFormat("if (a)\n" 788 " ;\n" 789 "else\n" 790 " ;", 791 AllowsMergedIfElse); 792 verifyFormat("if (a) {\n" 793 "} else {\n" 794 "}", 795 AllowsMergedIfElse); 796 verifyFormat("if (a) return;\n" 797 "else if (b) return;\n" 798 "else return;", 799 AllowsMergedIfElse); 800 verifyFormat("if (a) {\n" 801 "} else return;", 802 AllowsMergedIfElse); 803 verifyFormat("if (a) {\n" 804 "} else if (b) return;\n" 805 "else return;", 806 AllowsMergedIfElse); 807 verifyFormat("if (a) return;\n" 808 "else if (b) {\n" 809 "} else return;", 810 AllowsMergedIfElse); 811 verifyFormat("if (a)\n" 812 " if (b) return;\n" 813 " else return;", 814 AllowsMergedIfElse); 815 verifyFormat("if constexpr (a)\n" 816 " if constexpr (b) return;\n" 817 " else if constexpr (c) return;\n" 818 " else return;", 819 AllowsMergedIfElse); 820 verifyFormat("MYIF (a)\n" 821 " // comment\n" 822 " f();\n" 823 "else\n" 824 " // comment\n" 825 " f();", 826 AllowsMergedIfElse); 827 verifyFormat("{\n" 828 " MYIF (a)\n" 829 " label:\n" 830 " f();\n" 831 " else\n" 832 " label:\n" 833 " f();\n" 834 "}", 835 AllowsMergedIfElse); 836 verifyFormat("MYIF (a)\n" 837 " ;\n" 838 "else\n" 839 " ;", 840 AllowsMergedIfElse); 841 verifyFormat("MYIF (a) {\n" 842 "} else {\n" 843 "}", 844 AllowsMergedIfElse); 845 verifyFormat("MYIF (a) return;\n" 846 "else MYIF (b) return;\n" 847 "else return;", 848 AllowsMergedIfElse); 849 verifyFormat("MYIF (a) {\n" 850 "} else return;", 851 AllowsMergedIfElse); 852 verifyFormat("MYIF (a) {\n" 853 "} else MYIF (b) return;\n" 854 "else return;", 855 AllowsMergedIfElse); 856 verifyFormat("MYIF (a) return;\n" 857 "else MYIF (b) {\n" 858 "} else return;", 859 AllowsMergedIfElse); 860 verifyFormat("MYIF (a)\n" 861 " MYIF (b) return;\n" 862 " else return;", 863 AllowsMergedIfElse); 864 verifyFormat("MYIF constexpr (a)\n" 865 " MYIF constexpr (b) return;\n" 866 " else MYIF constexpr (c) return;\n" 867 " else return;", 868 AllowsMergedIfElse); 869 } 870 871 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 872 FormatStyle AllowsMergedIf = getLLVMStyle(); 873 AllowsMergedIf.IfMacros.push_back("MYIF"); 874 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 875 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 876 FormatStyle::SIS_WithoutElse; 877 verifyFormat("if (a)\n" 878 " f();\n" 879 "else {\n" 880 " g();\n" 881 "}", 882 AllowsMergedIf); 883 verifyFormat("if (a)\n" 884 " f();\n" 885 "else\n" 886 " g();\n", 887 AllowsMergedIf); 888 889 verifyFormat("if (a) g();", AllowsMergedIf); 890 verifyFormat("if (a) {\n" 891 " g()\n" 892 "};", 893 AllowsMergedIf); 894 verifyFormat("if (a)\n" 895 " g();\n" 896 "else\n" 897 " g();", 898 AllowsMergedIf); 899 verifyFormat("if (a) {\n" 900 " g();\n" 901 "} else\n" 902 " g();", 903 AllowsMergedIf); 904 verifyFormat("if (a)\n" 905 " g();\n" 906 "else {\n" 907 " g();\n" 908 "}", 909 AllowsMergedIf); 910 verifyFormat("if (a) {\n" 911 " g();\n" 912 "} else {\n" 913 " g();\n" 914 "}", 915 AllowsMergedIf); 916 verifyFormat("if (a)\n" 917 " g();\n" 918 "else if (b)\n" 919 " g();\n" 920 "else\n" 921 " g();", 922 AllowsMergedIf); 923 verifyFormat("if (a) {\n" 924 " g();\n" 925 "} else if (b)\n" 926 " g();\n" 927 "else\n" 928 " g();", 929 AllowsMergedIf); 930 verifyFormat("if (a)\n" 931 " g();\n" 932 "else if (b) {\n" 933 " g();\n" 934 "} else\n" 935 " g();", 936 AllowsMergedIf); 937 verifyFormat("if (a)\n" 938 " g();\n" 939 "else if (b)\n" 940 " g();\n" 941 "else {\n" 942 " g();\n" 943 "}", 944 AllowsMergedIf); 945 verifyFormat("if (a)\n" 946 " g();\n" 947 "else if (b) {\n" 948 " g();\n" 949 "} else {\n" 950 " g();\n" 951 "}", 952 AllowsMergedIf); 953 verifyFormat("if (a) {\n" 954 " g();\n" 955 "} else if (b) {\n" 956 " g();\n" 957 "} else {\n" 958 " g();\n" 959 "}", 960 AllowsMergedIf); 961 verifyFormat("MYIF (a)\n" 962 " f();\n" 963 "else {\n" 964 " g();\n" 965 "}", 966 AllowsMergedIf); 967 verifyFormat("MYIF (a)\n" 968 " f();\n" 969 "else\n" 970 " g();\n", 971 AllowsMergedIf); 972 973 verifyFormat("MYIF (a) g();", AllowsMergedIf); 974 verifyFormat("MYIF (a) {\n" 975 " g()\n" 976 "};", 977 AllowsMergedIf); 978 verifyFormat("MYIF (a)\n" 979 " g();\n" 980 "else\n" 981 " g();", 982 AllowsMergedIf); 983 verifyFormat("MYIF (a) {\n" 984 " g();\n" 985 "} else\n" 986 " g();", 987 AllowsMergedIf); 988 verifyFormat("MYIF (a)\n" 989 " g();\n" 990 "else {\n" 991 " g();\n" 992 "}", 993 AllowsMergedIf); 994 verifyFormat("MYIF (a) {\n" 995 " g();\n" 996 "} else {\n" 997 " g();\n" 998 "}", 999 AllowsMergedIf); 1000 verifyFormat("MYIF (a)\n" 1001 " g();\n" 1002 "else MYIF (b)\n" 1003 " g();\n" 1004 "else\n" 1005 " g();", 1006 AllowsMergedIf); 1007 verifyFormat("MYIF (a)\n" 1008 " g();\n" 1009 "else if (b)\n" 1010 " g();\n" 1011 "else\n" 1012 " g();", 1013 AllowsMergedIf); 1014 verifyFormat("MYIF (a) {\n" 1015 " g();\n" 1016 "} else MYIF (b)\n" 1017 " g();\n" 1018 "else\n" 1019 " g();", 1020 AllowsMergedIf); 1021 verifyFormat("MYIF (a) {\n" 1022 " g();\n" 1023 "} else if (b)\n" 1024 " g();\n" 1025 "else\n" 1026 " g();", 1027 AllowsMergedIf); 1028 verifyFormat("MYIF (a)\n" 1029 " g();\n" 1030 "else MYIF (b) {\n" 1031 " g();\n" 1032 "} else\n" 1033 " g();", 1034 AllowsMergedIf); 1035 verifyFormat("MYIF (a)\n" 1036 " g();\n" 1037 "else if (b) {\n" 1038 " g();\n" 1039 "} else\n" 1040 " g();", 1041 AllowsMergedIf); 1042 verifyFormat("MYIF (a)\n" 1043 " g();\n" 1044 "else MYIF (b)\n" 1045 " g();\n" 1046 "else {\n" 1047 " g();\n" 1048 "}", 1049 AllowsMergedIf); 1050 verifyFormat("MYIF (a)\n" 1051 " g();\n" 1052 "else if (b)\n" 1053 " g();\n" 1054 "else {\n" 1055 " g();\n" 1056 "}", 1057 AllowsMergedIf); 1058 verifyFormat("MYIF (a)\n" 1059 " g();\n" 1060 "else MYIF (b) {\n" 1061 " g();\n" 1062 "} else {\n" 1063 " g();\n" 1064 "}", 1065 AllowsMergedIf); 1066 verifyFormat("MYIF (a)\n" 1067 " g();\n" 1068 "else if (b) {\n" 1069 " g();\n" 1070 "} else {\n" 1071 " g();\n" 1072 "}", 1073 AllowsMergedIf); 1074 verifyFormat("MYIF (a) {\n" 1075 " g();\n" 1076 "} else MYIF (b) {\n" 1077 " g();\n" 1078 "} else {\n" 1079 " g();\n" 1080 "}", 1081 AllowsMergedIf); 1082 verifyFormat("MYIF (a) {\n" 1083 " g();\n" 1084 "} else if (b) {\n" 1085 " g();\n" 1086 "} else {\n" 1087 " g();\n" 1088 "}", 1089 AllowsMergedIf); 1090 1091 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1092 FormatStyle::SIS_OnlyFirstIf; 1093 1094 verifyFormat("if (a) f();\n" 1095 "else {\n" 1096 " g();\n" 1097 "}", 1098 AllowsMergedIf); 1099 verifyFormat("if (a) f();\n" 1100 "else {\n" 1101 " if (a) f();\n" 1102 " else {\n" 1103 " g();\n" 1104 " }\n" 1105 " g();\n" 1106 "}", 1107 AllowsMergedIf); 1108 1109 verifyFormat("if (a) g();", AllowsMergedIf); 1110 verifyFormat("if (a) {\n" 1111 " g()\n" 1112 "};", 1113 AllowsMergedIf); 1114 verifyFormat("if (a) g();\n" 1115 "else\n" 1116 " g();", 1117 AllowsMergedIf); 1118 verifyFormat("if (a) {\n" 1119 " g();\n" 1120 "} else\n" 1121 " g();", 1122 AllowsMergedIf); 1123 verifyFormat("if (a) g();\n" 1124 "else {\n" 1125 " g();\n" 1126 "}", 1127 AllowsMergedIf); 1128 verifyFormat("if (a) {\n" 1129 " g();\n" 1130 "} else {\n" 1131 " g();\n" 1132 "}", 1133 AllowsMergedIf); 1134 verifyFormat("if (a) g();\n" 1135 "else if (b)\n" 1136 " g();\n" 1137 "else\n" 1138 " g();", 1139 AllowsMergedIf); 1140 verifyFormat("if (a) {\n" 1141 " g();\n" 1142 "} else if (b)\n" 1143 " g();\n" 1144 "else\n" 1145 " g();", 1146 AllowsMergedIf); 1147 verifyFormat("if (a) g();\n" 1148 "else if (b) {\n" 1149 " g();\n" 1150 "} else\n" 1151 " g();", 1152 AllowsMergedIf); 1153 verifyFormat("if (a) g();\n" 1154 "else if (b)\n" 1155 " g();\n" 1156 "else {\n" 1157 " g();\n" 1158 "}", 1159 AllowsMergedIf); 1160 verifyFormat("if (a) g();\n" 1161 "else if (b) {\n" 1162 " g();\n" 1163 "} else {\n" 1164 " g();\n" 1165 "}", 1166 AllowsMergedIf); 1167 verifyFormat("if (a) {\n" 1168 " g();\n" 1169 "} else if (b) {\n" 1170 " g();\n" 1171 "} else {\n" 1172 " g();\n" 1173 "}", 1174 AllowsMergedIf); 1175 verifyFormat("MYIF (a) f();\n" 1176 "else {\n" 1177 " g();\n" 1178 "}", 1179 AllowsMergedIf); 1180 verifyFormat("MYIF (a) f();\n" 1181 "else {\n" 1182 " if (a) f();\n" 1183 " else {\n" 1184 " g();\n" 1185 " }\n" 1186 " g();\n" 1187 "}", 1188 AllowsMergedIf); 1189 1190 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1191 verifyFormat("MYIF (a) {\n" 1192 " g()\n" 1193 "};", 1194 AllowsMergedIf); 1195 verifyFormat("MYIF (a) g();\n" 1196 "else\n" 1197 " g();", 1198 AllowsMergedIf); 1199 verifyFormat("MYIF (a) {\n" 1200 " g();\n" 1201 "} else\n" 1202 " g();", 1203 AllowsMergedIf); 1204 verifyFormat("MYIF (a) g();\n" 1205 "else {\n" 1206 " g();\n" 1207 "}", 1208 AllowsMergedIf); 1209 verifyFormat("MYIF (a) {\n" 1210 " g();\n" 1211 "} else {\n" 1212 " g();\n" 1213 "}", 1214 AllowsMergedIf); 1215 verifyFormat("MYIF (a) g();\n" 1216 "else MYIF (b)\n" 1217 " g();\n" 1218 "else\n" 1219 " g();", 1220 AllowsMergedIf); 1221 verifyFormat("MYIF (a) g();\n" 1222 "else if (b)\n" 1223 " g();\n" 1224 "else\n" 1225 " g();", 1226 AllowsMergedIf); 1227 verifyFormat("MYIF (a) {\n" 1228 " g();\n" 1229 "} else MYIF (b)\n" 1230 " g();\n" 1231 "else\n" 1232 " g();", 1233 AllowsMergedIf); 1234 verifyFormat("MYIF (a) {\n" 1235 " g();\n" 1236 "} else if (b)\n" 1237 " g();\n" 1238 "else\n" 1239 " g();", 1240 AllowsMergedIf); 1241 verifyFormat("MYIF (a) g();\n" 1242 "else MYIF (b) {\n" 1243 " g();\n" 1244 "} else\n" 1245 " g();", 1246 AllowsMergedIf); 1247 verifyFormat("MYIF (a) g();\n" 1248 "else if (b) {\n" 1249 " g();\n" 1250 "} else\n" 1251 " g();", 1252 AllowsMergedIf); 1253 verifyFormat("MYIF (a) g();\n" 1254 "else MYIF (b)\n" 1255 " g();\n" 1256 "else {\n" 1257 " g();\n" 1258 "}", 1259 AllowsMergedIf); 1260 verifyFormat("MYIF (a) g();\n" 1261 "else if (b)\n" 1262 " g();\n" 1263 "else {\n" 1264 " g();\n" 1265 "}", 1266 AllowsMergedIf); 1267 verifyFormat("MYIF (a) g();\n" 1268 "else MYIF (b) {\n" 1269 " g();\n" 1270 "} else {\n" 1271 " g();\n" 1272 "}", 1273 AllowsMergedIf); 1274 verifyFormat("MYIF (a) g();\n" 1275 "else if (b) {\n" 1276 " g();\n" 1277 "} else {\n" 1278 " g();\n" 1279 "}", 1280 AllowsMergedIf); 1281 verifyFormat("MYIF (a) {\n" 1282 " g();\n" 1283 "} else MYIF (b) {\n" 1284 " g();\n" 1285 "} else {\n" 1286 " g();\n" 1287 "}", 1288 AllowsMergedIf); 1289 verifyFormat("MYIF (a) {\n" 1290 " g();\n" 1291 "} else if (b) {\n" 1292 " g();\n" 1293 "} else {\n" 1294 " g();\n" 1295 "}", 1296 AllowsMergedIf); 1297 1298 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1299 FormatStyle::SIS_AllIfsAndElse; 1300 1301 verifyFormat("if (a) f();\n" 1302 "else {\n" 1303 " g();\n" 1304 "}", 1305 AllowsMergedIf); 1306 verifyFormat("if (a) f();\n" 1307 "else {\n" 1308 " if (a) f();\n" 1309 " else {\n" 1310 " g();\n" 1311 " }\n" 1312 " g();\n" 1313 "}", 1314 AllowsMergedIf); 1315 1316 verifyFormat("if (a) g();", AllowsMergedIf); 1317 verifyFormat("if (a) {\n" 1318 " g()\n" 1319 "};", 1320 AllowsMergedIf); 1321 verifyFormat("if (a) g();\n" 1322 "else g();", 1323 AllowsMergedIf); 1324 verifyFormat("if (a) {\n" 1325 " g();\n" 1326 "} else g();", 1327 AllowsMergedIf); 1328 verifyFormat("if (a) g();\n" 1329 "else {\n" 1330 " g();\n" 1331 "}", 1332 AllowsMergedIf); 1333 verifyFormat("if (a) {\n" 1334 " g();\n" 1335 "} else {\n" 1336 " g();\n" 1337 "}", 1338 AllowsMergedIf); 1339 verifyFormat("if (a) g();\n" 1340 "else if (b) g();\n" 1341 "else g();", 1342 AllowsMergedIf); 1343 verifyFormat("if (a) {\n" 1344 " g();\n" 1345 "} else if (b) g();\n" 1346 "else g();", 1347 AllowsMergedIf); 1348 verifyFormat("if (a) g();\n" 1349 "else if (b) {\n" 1350 " g();\n" 1351 "} else g();", 1352 AllowsMergedIf); 1353 verifyFormat("if (a) g();\n" 1354 "else if (b) g();\n" 1355 "else {\n" 1356 " g();\n" 1357 "}", 1358 AllowsMergedIf); 1359 verifyFormat("if (a) g();\n" 1360 "else if (b) {\n" 1361 " g();\n" 1362 "} else {\n" 1363 " g();\n" 1364 "}", 1365 AllowsMergedIf); 1366 verifyFormat("if (a) {\n" 1367 " g();\n" 1368 "} else if (b) {\n" 1369 " g();\n" 1370 "} else {\n" 1371 " g();\n" 1372 "}", 1373 AllowsMergedIf); 1374 verifyFormat("MYIF (a) f();\n" 1375 "else {\n" 1376 " g();\n" 1377 "}", 1378 AllowsMergedIf); 1379 verifyFormat("MYIF (a) f();\n" 1380 "else {\n" 1381 " if (a) f();\n" 1382 " else {\n" 1383 " g();\n" 1384 " }\n" 1385 " g();\n" 1386 "}", 1387 AllowsMergedIf); 1388 1389 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1390 verifyFormat("MYIF (a) {\n" 1391 " g()\n" 1392 "};", 1393 AllowsMergedIf); 1394 verifyFormat("MYIF (a) g();\n" 1395 "else g();", 1396 AllowsMergedIf); 1397 verifyFormat("MYIF (a) {\n" 1398 " g();\n" 1399 "} else g();", 1400 AllowsMergedIf); 1401 verifyFormat("MYIF (a) g();\n" 1402 "else {\n" 1403 " g();\n" 1404 "}", 1405 AllowsMergedIf); 1406 verifyFormat("MYIF (a) {\n" 1407 " g();\n" 1408 "} else {\n" 1409 " g();\n" 1410 "}", 1411 AllowsMergedIf); 1412 verifyFormat("MYIF (a) g();\n" 1413 "else MYIF (b) g();\n" 1414 "else g();", 1415 AllowsMergedIf); 1416 verifyFormat("MYIF (a) g();\n" 1417 "else if (b) g();\n" 1418 "else g();", 1419 AllowsMergedIf); 1420 verifyFormat("MYIF (a) {\n" 1421 " g();\n" 1422 "} else MYIF (b) g();\n" 1423 "else g();", 1424 AllowsMergedIf); 1425 verifyFormat("MYIF (a) {\n" 1426 " g();\n" 1427 "} else if (b) g();\n" 1428 "else g();", 1429 AllowsMergedIf); 1430 verifyFormat("MYIF (a) g();\n" 1431 "else MYIF (b) {\n" 1432 " g();\n" 1433 "} else g();", 1434 AllowsMergedIf); 1435 verifyFormat("MYIF (a) g();\n" 1436 "else if (b) {\n" 1437 " g();\n" 1438 "} else g();", 1439 AllowsMergedIf); 1440 verifyFormat("MYIF (a) g();\n" 1441 "else MYIF (b) g();\n" 1442 "else {\n" 1443 " g();\n" 1444 "}", 1445 AllowsMergedIf); 1446 verifyFormat("MYIF (a) g();\n" 1447 "else if (b) g();\n" 1448 "else {\n" 1449 " g();\n" 1450 "}", 1451 AllowsMergedIf); 1452 verifyFormat("MYIF (a) g();\n" 1453 "else MYIF (b) {\n" 1454 " g();\n" 1455 "} else {\n" 1456 " g();\n" 1457 "}", 1458 AllowsMergedIf); 1459 verifyFormat("MYIF (a) g();\n" 1460 "else if (b) {\n" 1461 " g();\n" 1462 "} else {\n" 1463 " g();\n" 1464 "}", 1465 AllowsMergedIf); 1466 verifyFormat("MYIF (a) {\n" 1467 " g();\n" 1468 "} else MYIF (b) {\n" 1469 " g();\n" 1470 "} else {\n" 1471 " g();\n" 1472 "}", 1473 AllowsMergedIf); 1474 verifyFormat("MYIF (a) {\n" 1475 " g();\n" 1476 "} else if (b) {\n" 1477 " g();\n" 1478 "} else {\n" 1479 " g();\n" 1480 "}", 1481 AllowsMergedIf); 1482 } 1483 1484 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1485 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1486 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1487 verifyFormat("while (true) continue;", AllowsMergedLoops); 1488 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1489 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1490 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops); 1491 verifyFormat("while (true)\n" 1492 " ;", 1493 AllowsMergedLoops); 1494 verifyFormat("for (;;)\n" 1495 " ;", 1496 AllowsMergedLoops); 1497 verifyFormat("for (;;)\n" 1498 " for (;;) continue;", 1499 AllowsMergedLoops); 1500 verifyFormat("for (;;)\n" 1501 " while (true) continue;", 1502 AllowsMergedLoops); 1503 verifyFormat("while (true)\n" 1504 " for (;;) continue;", 1505 AllowsMergedLoops); 1506 verifyFormat("BOOST_FOREACH (int &v, vec)\n" 1507 " for (;;) continue;", 1508 AllowsMergedLoops); 1509 verifyFormat("for (;;)\n" 1510 " BOOST_FOREACH (int &v, vec) continue;", 1511 AllowsMergedLoops); 1512 verifyFormat("for (;;) // Can't merge this\n" 1513 " continue;", 1514 AllowsMergedLoops); 1515 verifyFormat("for (;;) /* still don't merge */\n" 1516 " continue;", 1517 AllowsMergedLoops); 1518 verifyFormat("do a++;\n" 1519 "while (true);", 1520 AllowsMergedLoops); 1521 verifyFormat("do /* Don't merge */\n" 1522 " a++;\n" 1523 "while (true);", 1524 AllowsMergedLoops); 1525 verifyFormat("do // Don't merge\n" 1526 " a++;\n" 1527 "while (true);", 1528 AllowsMergedLoops); 1529 verifyFormat("do\n" 1530 " // Don't merge\n" 1531 " a++;\n" 1532 "while (true);", 1533 AllowsMergedLoops); 1534 // Without braces labels are interpreted differently. 1535 verifyFormat("{\n" 1536 " do\n" 1537 " label:\n" 1538 " a++;\n" 1539 " while (true);\n" 1540 "}", 1541 AllowsMergedLoops); 1542 } 1543 1544 TEST_F(FormatTest, FormatShortBracedStatements) { 1545 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1546 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false); 1547 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine, 1548 FormatStyle::SIS_Never); 1549 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false); 1550 EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false); 1551 verifyFormat("for (;;) {\n" 1552 " f();\n" 1553 "}"); 1554 verifyFormat("/*comment*/ for (;;) {\n" 1555 " f();\n" 1556 "}"); 1557 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1558 " f();\n" 1559 "}"); 1560 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n" 1561 " f();\n" 1562 "}"); 1563 verifyFormat("while (true) {\n" 1564 " f();\n" 1565 "}"); 1566 verifyFormat("/*comment*/ while (true) {\n" 1567 " f();\n" 1568 "}"); 1569 verifyFormat("if (true) {\n" 1570 " f();\n" 1571 "}"); 1572 verifyFormat("/*comment*/ if (true) {\n" 1573 " f();\n" 1574 "}"); 1575 1576 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1577 FormatStyle::SBS_Empty; 1578 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1579 FormatStyle::SIS_WithoutElse; 1580 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1581 verifyFormat("if (i) break;", AllowSimpleBracedStatements); 1582 verifyFormat("if (i > 0) {\n" 1583 " return i;\n" 1584 "}", 1585 AllowSimpleBracedStatements); 1586 1587 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1588 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1589 // Not IF to avoid any confusion that IF is somehow special. 1590 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1591 AllowSimpleBracedStatements.ColumnLimit = 40; 1592 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1593 FormatStyle::SBS_Always; 1594 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1595 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1596 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1597 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1598 1599 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1600 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1601 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1602 verifyFormat("if consteval {}", AllowSimpleBracedStatements); 1603 verifyFormat("if !consteval {}", AllowSimpleBracedStatements); 1604 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements); 1605 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1606 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1607 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1608 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1609 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1610 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1611 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1612 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1613 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements); 1614 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements); 1615 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1616 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1617 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1618 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements); 1619 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements); 1620 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1621 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1622 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1623 AllowSimpleBracedStatements); 1624 verifyFormat("if (true) {\n" 1625 " ffffffffffffffffffffffff();\n" 1626 "}", 1627 AllowSimpleBracedStatements); 1628 verifyFormat("if (true) {\n" 1629 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1630 "}", 1631 AllowSimpleBracedStatements); 1632 verifyFormat("if (true) { //\n" 1633 " f();\n" 1634 "}", 1635 AllowSimpleBracedStatements); 1636 verifyFormat("if (true) {\n" 1637 " f();\n" 1638 " f();\n" 1639 "}", 1640 AllowSimpleBracedStatements); 1641 verifyFormat("if (true) {\n" 1642 " f();\n" 1643 "} else {\n" 1644 " f();\n" 1645 "}", 1646 AllowSimpleBracedStatements); 1647 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1648 AllowSimpleBracedStatements); 1649 verifyFormat("MYIF (true) {\n" 1650 " ffffffffffffffffffffffff();\n" 1651 "}", 1652 AllowSimpleBracedStatements); 1653 verifyFormat("MYIF (true) {\n" 1654 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1655 "}", 1656 AllowSimpleBracedStatements); 1657 verifyFormat("MYIF (true) { //\n" 1658 " f();\n" 1659 "}", 1660 AllowSimpleBracedStatements); 1661 verifyFormat("MYIF (true) {\n" 1662 " f();\n" 1663 " f();\n" 1664 "}", 1665 AllowSimpleBracedStatements); 1666 verifyFormat("MYIF (true) {\n" 1667 " f();\n" 1668 "} else {\n" 1669 " f();\n" 1670 "}", 1671 AllowSimpleBracedStatements); 1672 1673 verifyFormat("struct A2 {\n" 1674 " int X;\n" 1675 "};", 1676 AllowSimpleBracedStatements); 1677 verifyFormat("typedef struct A2 {\n" 1678 " int X;\n" 1679 "} A2_t;", 1680 AllowSimpleBracedStatements); 1681 verifyFormat("template <int> struct A2 {\n" 1682 " struct B {};\n" 1683 "};", 1684 AllowSimpleBracedStatements); 1685 1686 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1687 FormatStyle::SIS_Never; 1688 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1689 verifyFormat("if (true) {\n" 1690 " f();\n" 1691 "}", 1692 AllowSimpleBracedStatements); 1693 verifyFormat("if (true) {\n" 1694 " f();\n" 1695 "} else {\n" 1696 " f();\n" 1697 "}", 1698 AllowSimpleBracedStatements); 1699 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1700 verifyFormat("MYIF (true) {\n" 1701 " f();\n" 1702 "}", 1703 AllowSimpleBracedStatements); 1704 verifyFormat("MYIF (true) {\n" 1705 " f();\n" 1706 "} else {\n" 1707 " f();\n" 1708 "}", 1709 AllowSimpleBracedStatements); 1710 1711 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1712 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1713 verifyFormat("while (true) {\n" 1714 " f();\n" 1715 "}", 1716 AllowSimpleBracedStatements); 1717 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1718 verifyFormat("for (;;) {\n" 1719 " f();\n" 1720 "}", 1721 AllowSimpleBracedStatements); 1722 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1723 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1724 " f();\n" 1725 "}", 1726 AllowSimpleBracedStatements); 1727 1728 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1729 FormatStyle::SIS_WithoutElse; 1730 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1731 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1732 FormatStyle::BWACS_Always; 1733 1734 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1735 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1736 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1737 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1738 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1739 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1740 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1741 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1742 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1743 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1744 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1745 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1746 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1747 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1748 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1749 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1750 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1751 AllowSimpleBracedStatements); 1752 verifyFormat("if (true)\n" 1753 "{\n" 1754 " ffffffffffffffffffffffff();\n" 1755 "}", 1756 AllowSimpleBracedStatements); 1757 verifyFormat("if (true)\n" 1758 "{\n" 1759 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1760 "}", 1761 AllowSimpleBracedStatements); 1762 verifyFormat("if (true)\n" 1763 "{ //\n" 1764 " f();\n" 1765 "}", 1766 AllowSimpleBracedStatements); 1767 verifyFormat("if (true)\n" 1768 "{\n" 1769 " f();\n" 1770 " f();\n" 1771 "}", 1772 AllowSimpleBracedStatements); 1773 verifyFormat("if (true)\n" 1774 "{\n" 1775 " f();\n" 1776 "} else\n" 1777 "{\n" 1778 " f();\n" 1779 "}", 1780 AllowSimpleBracedStatements); 1781 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1782 AllowSimpleBracedStatements); 1783 verifyFormat("MYIF (true)\n" 1784 "{\n" 1785 " ffffffffffffffffffffffff();\n" 1786 "}", 1787 AllowSimpleBracedStatements); 1788 verifyFormat("MYIF (true)\n" 1789 "{\n" 1790 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1791 "}", 1792 AllowSimpleBracedStatements); 1793 verifyFormat("MYIF (true)\n" 1794 "{ //\n" 1795 " f();\n" 1796 "}", 1797 AllowSimpleBracedStatements); 1798 verifyFormat("MYIF (true)\n" 1799 "{\n" 1800 " f();\n" 1801 " f();\n" 1802 "}", 1803 AllowSimpleBracedStatements); 1804 verifyFormat("MYIF (true)\n" 1805 "{\n" 1806 " f();\n" 1807 "} else\n" 1808 "{\n" 1809 " f();\n" 1810 "}", 1811 AllowSimpleBracedStatements); 1812 1813 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1814 FormatStyle::SIS_Never; 1815 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1816 verifyFormat("if (true)\n" 1817 "{\n" 1818 " f();\n" 1819 "}", 1820 AllowSimpleBracedStatements); 1821 verifyFormat("if (true)\n" 1822 "{\n" 1823 " f();\n" 1824 "} else\n" 1825 "{\n" 1826 " f();\n" 1827 "}", 1828 AllowSimpleBracedStatements); 1829 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1830 verifyFormat("MYIF (true)\n" 1831 "{\n" 1832 " f();\n" 1833 "}", 1834 AllowSimpleBracedStatements); 1835 verifyFormat("MYIF (true)\n" 1836 "{\n" 1837 " f();\n" 1838 "} else\n" 1839 "{\n" 1840 " f();\n" 1841 "}", 1842 AllowSimpleBracedStatements); 1843 1844 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1845 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1846 verifyFormat("while (true)\n" 1847 "{\n" 1848 " f();\n" 1849 "}", 1850 AllowSimpleBracedStatements); 1851 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1852 verifyFormat("for (;;)\n" 1853 "{\n" 1854 " f();\n" 1855 "}", 1856 AllowSimpleBracedStatements); 1857 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1858 verifyFormat("BOOST_FOREACH (int v, vec)\n" 1859 "{\n" 1860 " f();\n" 1861 "}", 1862 AllowSimpleBracedStatements); 1863 } 1864 1865 TEST_F(FormatTest, UnderstandsMacros) { 1866 verifyFormat("#define A (parentheses)"); 1867 verifyFormat("/* comment */ #define A (parentheses)"); 1868 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)"); 1869 // Even the partial code should never be merged. 1870 EXPECT_EQ("/* comment */ #define A (parentheses)\n" 1871 "#", 1872 format("/* comment */ #define A (parentheses)\n" 1873 "#")); 1874 verifyFormat("/* comment */ #define A (parentheses)\n" 1875 "#\n"); 1876 verifyFormat("/* comment */ #define A (parentheses)\n" 1877 "#define B (parentheses)"); 1878 verifyFormat("#define true ((int)1)"); 1879 verifyFormat("#define and(x)"); 1880 verifyFormat("#define if(x) x"); 1881 verifyFormat("#define return(x) (x)"); 1882 verifyFormat("#define while(x) for (; x;)"); 1883 verifyFormat("#define xor(x) (^(x))"); 1884 verifyFormat("#define __except(x)"); 1885 verifyFormat("#define __try(x)"); 1886 1887 // https://llvm.org/PR54348. 1888 verifyFormat( 1889 "#define A" 1890 " " 1891 "\\\n" 1892 " class & {}"); 1893 1894 FormatStyle Style = getLLVMStyle(); 1895 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1896 Style.BraceWrapping.AfterFunction = true; 1897 // Test that a macro definition never gets merged with the following 1898 // definition. 1899 // FIXME: The AAA macro definition probably should not be split into 3 lines. 1900 verifyFormat("#define AAA " 1901 " \\\n" 1902 " N " 1903 " \\\n" 1904 " {\n" 1905 "#define BBB }\n", 1906 Style); 1907 // verifyFormat("#define AAA N { //\n", Style); 1908 1909 verifyFormat("MACRO(return)"); 1910 verifyFormat("MACRO(co_await)"); 1911 verifyFormat("MACRO(co_return)"); 1912 verifyFormat("MACRO(co_yield)"); 1913 verifyFormat("MACRO(return, something)"); 1914 verifyFormat("MACRO(co_return, something)"); 1915 verifyFormat("MACRO(something##something)"); 1916 verifyFormat("MACRO(return##something)"); 1917 verifyFormat("MACRO(co_return##something)"); 1918 } 1919 1920 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1921 FormatStyle Style = getLLVMStyleWithColumns(60); 1922 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1923 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1924 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1925 EXPECT_EQ("#define A \\\n" 1926 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1927 " { \\\n" 1928 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1929 " }\n" 1930 "X;", 1931 format("#define A \\\n" 1932 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1933 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1934 " }\n" 1935 "X;", 1936 Style)); 1937 } 1938 1939 TEST_F(FormatTest, ParseIfElse) { 1940 verifyFormat("if (true)\n" 1941 " if (true)\n" 1942 " if (true)\n" 1943 " f();\n" 1944 " else\n" 1945 " g();\n" 1946 " else\n" 1947 " h();\n" 1948 "else\n" 1949 " i();"); 1950 verifyFormat("if (true)\n" 1951 " if (true)\n" 1952 " if (true) {\n" 1953 " if (true)\n" 1954 " f();\n" 1955 " } else {\n" 1956 " g();\n" 1957 " }\n" 1958 " else\n" 1959 " h();\n" 1960 "else {\n" 1961 " i();\n" 1962 "}"); 1963 verifyFormat("if (true)\n" 1964 " if constexpr (true)\n" 1965 " if (true) {\n" 1966 " if constexpr (true)\n" 1967 " f();\n" 1968 " } else {\n" 1969 " g();\n" 1970 " }\n" 1971 " else\n" 1972 " h();\n" 1973 "else {\n" 1974 " i();\n" 1975 "}"); 1976 verifyFormat("if (true)\n" 1977 " if CONSTEXPR (true)\n" 1978 " if (true) {\n" 1979 " if CONSTEXPR (true)\n" 1980 " f();\n" 1981 " } else {\n" 1982 " g();\n" 1983 " }\n" 1984 " else\n" 1985 " h();\n" 1986 "else {\n" 1987 " i();\n" 1988 "}"); 1989 verifyFormat("void f() {\n" 1990 " if (a) {\n" 1991 " } else {\n" 1992 " }\n" 1993 "}"); 1994 } 1995 1996 TEST_F(FormatTest, ElseIf) { 1997 verifyFormat("if (a) {\n} else if (b) {\n}"); 1998 verifyFormat("if (a)\n" 1999 " f();\n" 2000 "else if (b)\n" 2001 " g();\n" 2002 "else\n" 2003 " h();"); 2004 verifyFormat("if (a)\n" 2005 " f();\n" 2006 "else // comment\n" 2007 " if (b) {\n" 2008 " g();\n" 2009 " h();\n" 2010 " }"); 2011 verifyFormat("if constexpr (a)\n" 2012 " f();\n" 2013 "else if constexpr (b)\n" 2014 " g();\n" 2015 "else\n" 2016 " h();"); 2017 verifyFormat("if CONSTEXPR (a)\n" 2018 " f();\n" 2019 "else if CONSTEXPR (b)\n" 2020 " g();\n" 2021 "else\n" 2022 " h();"); 2023 verifyFormat("if (a) {\n" 2024 " f();\n" 2025 "}\n" 2026 "// or else ..\n" 2027 "else {\n" 2028 " g()\n" 2029 "}"); 2030 2031 verifyFormat("if (a) {\n" 2032 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2034 "}"); 2035 verifyFormat("if (a) {\n" 2036 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2037 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2038 "}"); 2039 verifyFormat("if (a) {\n" 2040 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2042 "}"); 2043 verifyFormat("if (a) {\n" 2044 "} else if (\n" 2045 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2046 "}", 2047 getLLVMStyleWithColumns(62)); 2048 verifyFormat("if (a) {\n" 2049 "} else if constexpr (\n" 2050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2051 "}", 2052 getLLVMStyleWithColumns(62)); 2053 verifyFormat("if (a) {\n" 2054 "} else if CONSTEXPR (\n" 2055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2056 "}", 2057 getLLVMStyleWithColumns(62)); 2058 } 2059 2060 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 2061 FormatStyle Style = getLLVMStyle(); 2062 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); 2063 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer); 2064 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 2065 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 2066 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 2067 verifyFormat("int *f1(int &a) const &;", Style); 2068 verifyFormat("int *f1(int &a) const & = 0;", Style); 2069 verifyFormat("int *a = f1();", Style); 2070 verifyFormat("int &b = f2();", Style); 2071 verifyFormat("int &&c = f3();", Style); 2072 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 2073 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); 2074 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", 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 auto &c : {1, 2, 3})", Style); 2077 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); 2078 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 2079 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); 2080 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); 2081 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); 2082 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); 2083 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); 2084 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); 2085 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); 2086 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); 2087 verifyFormat("for (f(); int &c : {1, 2, 3})", Style); 2088 verifyFormat( 2089 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n" 2090 " res2 = [](int &a) { return 0000000000000; };", 2091 Style); 2092 2093 Style.AlignConsecutiveDeclarations.Enabled = true; 2094 verifyFormat("Const unsigned int *c;\n" 2095 "const unsigned int *d;\n" 2096 "Const unsigned int &e;\n" 2097 "const unsigned int &f;\n" 2098 "const unsigned &&g;\n" 2099 "Const unsigned h;", 2100 Style); 2101 2102 Style.PointerAlignment = FormatStyle::PAS_Left; 2103 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 2104 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 2105 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 2106 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 2107 verifyFormat("int* f1(int& a) const& = 0;", Style); 2108 verifyFormat("int* a = f1();", Style); 2109 verifyFormat("int& b = f2();", Style); 2110 verifyFormat("int&& c = f3();", Style); 2111 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2112 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2113 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2114 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2115 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2116 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2117 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2118 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2119 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style); 2120 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style); 2121 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style); 2122 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2123 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); 2124 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); 2125 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); 2126 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); 2127 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); 2128 verifyFormat("for (f(); int& c : {1, 2, 3})", Style); 2129 verifyFormat( 2130 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n" 2131 " res2 = [](int& a) { return 0000000000000; };", 2132 Style); 2133 2134 Style.AlignConsecutiveDeclarations.Enabled = true; 2135 verifyFormat("Const unsigned int* c;\n" 2136 "const unsigned int* d;\n" 2137 "Const unsigned int& e;\n" 2138 "const unsigned int& f;\n" 2139 "const unsigned&& g;\n" 2140 "Const unsigned h;", 2141 Style); 2142 2143 Style.PointerAlignment = FormatStyle::PAS_Right; 2144 Style.ReferenceAlignment = FormatStyle::RAS_Left; 2145 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 2146 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 2147 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 2148 verifyFormat("int *a = f1();", Style); 2149 verifyFormat("int& b = f2();", Style); 2150 verifyFormat("int&& c = f3();", Style); 2151 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2152 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2153 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); 2154 2155 Style.AlignConsecutiveDeclarations.Enabled = true; 2156 verifyFormat("Const unsigned int *c;\n" 2157 "const unsigned int *d;\n" 2158 "Const unsigned int& e;\n" 2159 "const unsigned int& f;\n" 2160 "const unsigned g;\n" 2161 "Const unsigned h;", 2162 Style); 2163 2164 Style.PointerAlignment = FormatStyle::PAS_Left; 2165 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 2166 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 2167 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 2168 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 2169 verifyFormat("int* a = f1();", Style); 2170 verifyFormat("int & b = f2();", Style); 2171 verifyFormat("int && c = f3();", Style); 2172 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); 2173 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); 2174 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); 2175 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2176 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); 2177 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); 2178 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); 2179 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2180 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); 2181 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); 2182 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); 2183 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); 2184 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); 2185 verifyFormat("for (f(); int & c : {1, 2, 3})", Style); 2186 verifyFormat( 2187 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n" 2188 " res2 = [](int & a) { return 0000000000000; };", 2189 Style); 2190 2191 Style.AlignConsecutiveDeclarations.Enabled = true; 2192 verifyFormat("Const unsigned int* c;\n" 2193 "const unsigned int* d;\n" 2194 "Const unsigned int & e;\n" 2195 "const unsigned int & f;\n" 2196 "const unsigned && g;\n" 2197 "Const unsigned h;", 2198 Style); 2199 2200 Style.PointerAlignment = FormatStyle::PAS_Middle; 2201 Style.ReferenceAlignment = FormatStyle::RAS_Right; 2202 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 2203 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 2204 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 2205 verifyFormat("int * a = f1();", Style); 2206 verifyFormat("int &b = f2();", Style); 2207 verifyFormat("int &&c = f3();", Style); 2208 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2209 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2210 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); 2211 2212 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2213 // specifically handled 2214 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2215 } 2216 2217 TEST_F(FormatTest, FormatsForLoop) { 2218 verifyFormat( 2219 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2220 " ++VeryVeryLongLoopVariable)\n" 2221 " ;"); 2222 verifyFormat("for (;;)\n" 2223 " f();"); 2224 verifyFormat("for (;;) {\n}"); 2225 verifyFormat("for (;;) {\n" 2226 " f();\n" 2227 "}"); 2228 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2229 2230 verifyFormat( 2231 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2232 " E = UnwrappedLines.end();\n" 2233 " I != E; ++I) {\n}"); 2234 2235 verifyFormat( 2236 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2237 " ++IIIII) {\n}"); 2238 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2239 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2240 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2241 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2242 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2243 " E = FD->getDeclsInPrototypeScope().end();\n" 2244 " I != E; ++I) {\n}"); 2245 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2246 " I = Container.begin(),\n" 2247 " E = Container.end();\n" 2248 " I != E; ++I) {\n}", 2249 getLLVMStyleWithColumns(76)); 2250 2251 verifyFormat( 2252 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2253 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2254 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2256 " ++aaaaaaaaaaa) {\n}"); 2257 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2258 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2259 " ++i) {\n}"); 2260 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2261 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2262 "}"); 2263 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2264 " aaaaaaaaaa);\n" 2265 " iter; ++iter) {\n" 2266 "}"); 2267 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2269 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2270 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2271 2272 // These should not be formatted as Objective-C for-in loops. 2273 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2274 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2275 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2276 verifyFormat( 2277 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2278 2279 FormatStyle NoBinPacking = getLLVMStyle(); 2280 NoBinPacking.BinPackParameters = false; 2281 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2282 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2283 " aaaaaaaaaaaaaaaa,\n" 2284 " aaaaaaaaaaaaaaaa,\n" 2285 " aaaaaaaaaaaaaaaa);\n" 2286 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2287 "}", 2288 NoBinPacking); 2289 verifyFormat( 2290 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2291 " E = UnwrappedLines.end();\n" 2292 " I != E;\n" 2293 " ++I) {\n}", 2294 NoBinPacking); 2295 2296 FormatStyle AlignLeft = getLLVMStyle(); 2297 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2298 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2299 } 2300 2301 TEST_F(FormatTest, RangeBasedForLoops) { 2302 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2304 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2305 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2306 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2308 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2309 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2310 } 2311 2312 TEST_F(FormatTest, ForEachLoops) { 2313 FormatStyle Style = getLLVMStyle(); 2314 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2315 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false); 2316 verifyFormat("void f() {\n" 2317 " for (;;) {\n" 2318 " }\n" 2319 " foreach (Item *item, itemlist) {\n" 2320 " }\n" 2321 " Q_FOREACH (Item *item, itemlist) {\n" 2322 " }\n" 2323 " BOOST_FOREACH (Item *item, itemlist) {\n" 2324 " }\n" 2325 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2326 "}", 2327 Style); 2328 verifyFormat("void f() {\n" 2329 " for (;;)\n" 2330 " int j = 1;\n" 2331 " Q_FOREACH (int v, vec)\n" 2332 " v *= 2;\n" 2333 " for (;;) {\n" 2334 " int j = 1;\n" 2335 " }\n" 2336 " Q_FOREACH (int v, vec) {\n" 2337 " v *= 2;\n" 2338 " }\n" 2339 "}", 2340 Style); 2341 2342 FormatStyle ShortBlocks = getLLVMStyle(); 2343 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2344 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false); 2345 verifyFormat("void f() {\n" 2346 " for (;;)\n" 2347 " int j = 1;\n" 2348 " Q_FOREACH (int &v, vec)\n" 2349 " v *= 2;\n" 2350 " for (;;) {\n" 2351 " int j = 1;\n" 2352 " }\n" 2353 " Q_FOREACH (int &v, vec) {\n" 2354 " int j = 1;\n" 2355 " }\n" 2356 "}", 2357 ShortBlocks); 2358 2359 FormatStyle ShortLoops = getLLVMStyle(); 2360 ShortLoops.AllowShortLoopsOnASingleLine = true; 2361 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2362 verifyFormat("void f() {\n" 2363 " for (;;) int j = 1;\n" 2364 " Q_FOREACH (int &v, vec) int j = 1;\n" 2365 " for (;;) {\n" 2366 " int j = 1;\n" 2367 " }\n" 2368 " Q_FOREACH (int &v, vec) {\n" 2369 " int j = 1;\n" 2370 " }\n" 2371 "}", 2372 ShortLoops); 2373 2374 FormatStyle ShortBlocksAndLoops = getLLVMStyle(); 2375 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2376 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true; 2377 verifyFormat("void f() {\n" 2378 " for (;;) int j = 1;\n" 2379 " Q_FOREACH (int &v, vec) int j = 1;\n" 2380 " for (;;) { int j = 1; }\n" 2381 " Q_FOREACH (int &v, vec) { int j = 1; }\n" 2382 "}", 2383 ShortBlocksAndLoops); 2384 2385 Style.SpaceBeforeParens = 2386 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2387 verifyFormat("void f() {\n" 2388 " for (;;) {\n" 2389 " }\n" 2390 " foreach(Item *item, itemlist) {\n" 2391 " }\n" 2392 " Q_FOREACH(Item *item, itemlist) {\n" 2393 " }\n" 2394 " BOOST_FOREACH(Item *item, itemlist) {\n" 2395 " }\n" 2396 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2397 "}", 2398 Style); 2399 2400 // As function-like macros. 2401 verifyFormat("#define foreach(x, y)\n" 2402 "#define Q_FOREACH(x, y)\n" 2403 "#define BOOST_FOREACH(x, y)\n" 2404 "#define UNKNOWN_FOREACH(x, y)\n"); 2405 2406 // Not as function-like macros. 2407 verifyFormat("#define foreach (x, y)\n" 2408 "#define Q_FOREACH (x, y)\n" 2409 "#define BOOST_FOREACH (x, y)\n" 2410 "#define UNKNOWN_FOREACH (x, y)\n"); 2411 2412 // handle microsoft non standard extension 2413 verifyFormat("for each (char c in x->MyStringProperty)"); 2414 } 2415 2416 TEST_F(FormatTest, FormatsWhileLoop) { 2417 verifyFormat("while (true) {\n}"); 2418 verifyFormat("while (true)\n" 2419 " f();"); 2420 verifyFormat("while () {\n}"); 2421 verifyFormat("while () {\n" 2422 " f();\n" 2423 "}"); 2424 } 2425 2426 TEST_F(FormatTest, FormatsDoWhile) { 2427 verifyFormat("do {\n" 2428 " do_something();\n" 2429 "} while (something());"); 2430 verifyFormat("do\n" 2431 " do_something();\n" 2432 "while (something());"); 2433 } 2434 2435 TEST_F(FormatTest, FormatsSwitchStatement) { 2436 verifyFormat("switch (x) {\n" 2437 "case 1:\n" 2438 " f();\n" 2439 " break;\n" 2440 "case kFoo:\n" 2441 "case ns::kBar:\n" 2442 "case kBaz:\n" 2443 " break;\n" 2444 "default:\n" 2445 " g();\n" 2446 " break;\n" 2447 "}"); 2448 verifyFormat("switch (x) {\n" 2449 "case 1: {\n" 2450 " f();\n" 2451 " break;\n" 2452 "}\n" 2453 "case 2: {\n" 2454 " break;\n" 2455 "}\n" 2456 "}"); 2457 verifyFormat("switch (x) {\n" 2458 "case 1: {\n" 2459 " f();\n" 2460 " {\n" 2461 " g();\n" 2462 " h();\n" 2463 " }\n" 2464 " break;\n" 2465 "}\n" 2466 "}"); 2467 verifyFormat("switch (x) {\n" 2468 "case 1: {\n" 2469 " f();\n" 2470 " if (foo) {\n" 2471 " g();\n" 2472 " h();\n" 2473 " }\n" 2474 " break;\n" 2475 "}\n" 2476 "}"); 2477 verifyFormat("switch (x) {\n" 2478 "case 1: {\n" 2479 " f();\n" 2480 " g();\n" 2481 "} break;\n" 2482 "}"); 2483 verifyFormat("switch (test)\n" 2484 " ;"); 2485 verifyFormat("switch (x) {\n" 2486 "default: {\n" 2487 " // Do nothing.\n" 2488 "}\n" 2489 "}"); 2490 verifyFormat("switch (x) {\n" 2491 "// comment\n" 2492 "// if 1, do f()\n" 2493 "case 1:\n" 2494 " f();\n" 2495 "}"); 2496 verifyFormat("switch (x) {\n" 2497 "case 1:\n" 2498 " // Do amazing stuff\n" 2499 " {\n" 2500 " f();\n" 2501 " g();\n" 2502 " }\n" 2503 " break;\n" 2504 "}"); 2505 verifyFormat("#define A \\\n" 2506 " switch (x) { \\\n" 2507 " case a: \\\n" 2508 " foo = b; \\\n" 2509 " }", 2510 getLLVMStyleWithColumns(20)); 2511 verifyFormat("#define OPERATION_CASE(name) \\\n" 2512 " case OP_name: \\\n" 2513 " return operations::Operation##name\n", 2514 getLLVMStyleWithColumns(40)); 2515 verifyFormat("switch (x) {\n" 2516 "case 1:;\n" 2517 "default:;\n" 2518 " int i;\n" 2519 "}"); 2520 2521 verifyGoogleFormat("switch (x) {\n" 2522 " case 1:\n" 2523 " f();\n" 2524 " break;\n" 2525 " case kFoo:\n" 2526 " case ns::kBar:\n" 2527 " case kBaz:\n" 2528 " break;\n" 2529 " default:\n" 2530 " g();\n" 2531 " break;\n" 2532 "}"); 2533 verifyGoogleFormat("switch (x) {\n" 2534 " case 1: {\n" 2535 " f();\n" 2536 " break;\n" 2537 " }\n" 2538 "}"); 2539 verifyGoogleFormat("switch (test)\n" 2540 " ;"); 2541 2542 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2543 " case OP_name: \\\n" 2544 " return operations::Operation##name\n"); 2545 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2546 " // Get the correction operation class.\n" 2547 " switch (OpCode) {\n" 2548 " CASE(Add);\n" 2549 " CASE(Subtract);\n" 2550 " default:\n" 2551 " return operations::Unknown;\n" 2552 " }\n" 2553 "#undef OPERATION_CASE\n" 2554 "}"); 2555 verifyFormat("DEBUG({\n" 2556 " switch (x) {\n" 2557 " case A:\n" 2558 " f();\n" 2559 " break;\n" 2560 " // fallthrough\n" 2561 " case B:\n" 2562 " g();\n" 2563 " break;\n" 2564 " }\n" 2565 "});"); 2566 EXPECT_EQ("DEBUG({\n" 2567 " switch (x) {\n" 2568 " case A:\n" 2569 " f();\n" 2570 " break;\n" 2571 " // On B:\n" 2572 " case B:\n" 2573 " g();\n" 2574 " break;\n" 2575 " }\n" 2576 "});", 2577 format("DEBUG({\n" 2578 " switch (x) {\n" 2579 " case A:\n" 2580 " f();\n" 2581 " break;\n" 2582 " // On B:\n" 2583 " case B:\n" 2584 " g();\n" 2585 " break;\n" 2586 " }\n" 2587 "});", 2588 getLLVMStyle())); 2589 EXPECT_EQ("switch (n) {\n" 2590 "case 0: {\n" 2591 " return false;\n" 2592 "}\n" 2593 "default: {\n" 2594 " return true;\n" 2595 "}\n" 2596 "}", 2597 format("switch (n)\n" 2598 "{\n" 2599 "case 0: {\n" 2600 " return false;\n" 2601 "}\n" 2602 "default: {\n" 2603 " return true;\n" 2604 "}\n" 2605 "}", 2606 getLLVMStyle())); 2607 verifyFormat("switch (a) {\n" 2608 "case (b):\n" 2609 " return;\n" 2610 "}"); 2611 2612 verifyFormat("switch (a) {\n" 2613 "case some_namespace::\n" 2614 " some_constant:\n" 2615 " return;\n" 2616 "}", 2617 getLLVMStyleWithColumns(34)); 2618 2619 verifyFormat("switch (a) {\n" 2620 "[[likely]] case 1:\n" 2621 " return;\n" 2622 "}"); 2623 verifyFormat("switch (a) {\n" 2624 "[[likely]] [[other::likely]] case 1:\n" 2625 " return;\n" 2626 "}"); 2627 verifyFormat("switch (x) {\n" 2628 "case 1:\n" 2629 " return;\n" 2630 "[[likely]] case 2:\n" 2631 " return;\n" 2632 "}"); 2633 verifyFormat("switch (a) {\n" 2634 "case 1:\n" 2635 "[[likely]] case 2:\n" 2636 " return;\n" 2637 "}"); 2638 FormatStyle Attributes = getLLVMStyle(); 2639 Attributes.AttributeMacros.push_back("LIKELY"); 2640 Attributes.AttributeMacros.push_back("OTHER_LIKELY"); 2641 verifyFormat("switch (a) {\n" 2642 "LIKELY case b:\n" 2643 " return;\n" 2644 "}", 2645 Attributes); 2646 verifyFormat("switch (a) {\n" 2647 "LIKELY OTHER_LIKELY() case b:\n" 2648 " return;\n" 2649 "}", 2650 Attributes); 2651 verifyFormat("switch (a) {\n" 2652 "case 1:\n" 2653 " return;\n" 2654 "LIKELY case 2:\n" 2655 " return;\n" 2656 "}", 2657 Attributes); 2658 verifyFormat("switch (a) {\n" 2659 "case 1:\n" 2660 "LIKELY case 2:\n" 2661 " return;\n" 2662 "}", 2663 Attributes); 2664 2665 FormatStyle Style = getLLVMStyle(); 2666 Style.IndentCaseLabels = true; 2667 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2668 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2669 Style.BraceWrapping.AfterCaseLabel = true; 2670 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2671 EXPECT_EQ("switch (n)\n" 2672 "{\n" 2673 " case 0:\n" 2674 " {\n" 2675 " return false;\n" 2676 " }\n" 2677 " default:\n" 2678 " {\n" 2679 " return true;\n" 2680 " }\n" 2681 "}", 2682 format("switch (n) {\n" 2683 " case 0: {\n" 2684 " return false;\n" 2685 " }\n" 2686 " default: {\n" 2687 " return true;\n" 2688 " }\n" 2689 "}", 2690 Style)); 2691 Style.BraceWrapping.AfterCaseLabel = false; 2692 EXPECT_EQ("switch (n)\n" 2693 "{\n" 2694 " case 0: {\n" 2695 " return false;\n" 2696 " }\n" 2697 " default: {\n" 2698 " return true;\n" 2699 " }\n" 2700 "}", 2701 format("switch (n) {\n" 2702 " case 0:\n" 2703 " {\n" 2704 " return false;\n" 2705 " }\n" 2706 " default:\n" 2707 " {\n" 2708 " return true;\n" 2709 " }\n" 2710 "}", 2711 Style)); 2712 Style.IndentCaseLabels = false; 2713 Style.IndentCaseBlocks = true; 2714 EXPECT_EQ("switch (n)\n" 2715 "{\n" 2716 "case 0:\n" 2717 " {\n" 2718 " return false;\n" 2719 " }\n" 2720 "case 1:\n" 2721 " break;\n" 2722 "default:\n" 2723 " {\n" 2724 " return true;\n" 2725 " }\n" 2726 "}", 2727 format("switch (n) {\n" 2728 "case 0: {\n" 2729 " return false;\n" 2730 "}\n" 2731 "case 1:\n" 2732 " break;\n" 2733 "default: {\n" 2734 " return true;\n" 2735 "}\n" 2736 "}", 2737 Style)); 2738 Style.IndentCaseLabels = true; 2739 Style.IndentCaseBlocks = true; 2740 EXPECT_EQ("switch (n)\n" 2741 "{\n" 2742 " case 0:\n" 2743 " {\n" 2744 " return false;\n" 2745 " }\n" 2746 " case 1:\n" 2747 " break;\n" 2748 " default:\n" 2749 " {\n" 2750 " return true;\n" 2751 " }\n" 2752 "}", 2753 format("switch (n) {\n" 2754 "case 0: {\n" 2755 " return false;\n" 2756 "}\n" 2757 "case 1:\n" 2758 " break;\n" 2759 "default: {\n" 2760 " return true;\n" 2761 "}\n" 2762 "}", 2763 Style)); 2764 } 2765 2766 TEST_F(FormatTest, CaseRanges) { 2767 verifyFormat("switch (x) {\n" 2768 "case 'A' ... 'Z':\n" 2769 "case 1 ... 5:\n" 2770 "case a ... b:\n" 2771 " break;\n" 2772 "}"); 2773 } 2774 2775 TEST_F(FormatTest, ShortEnums) { 2776 FormatStyle Style = getLLVMStyle(); 2777 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine); 2778 EXPECT_FALSE(Style.BraceWrapping.AfterEnum); 2779 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2780 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2781 Style.AllowShortEnumsOnASingleLine = false; 2782 verifyFormat("enum {\n" 2783 " A,\n" 2784 " B,\n" 2785 " C\n" 2786 "} ShortEnum1, ShortEnum2;", 2787 Style); 2788 verifyFormat("typedef enum {\n" 2789 " A,\n" 2790 " B,\n" 2791 " C\n" 2792 "} ShortEnum1, ShortEnum2;", 2793 Style); 2794 verifyFormat("enum {\n" 2795 " A,\n" 2796 "} ShortEnum1, ShortEnum2;", 2797 Style); 2798 verifyFormat("typedef enum {\n" 2799 " A,\n" 2800 "} ShortEnum1, ShortEnum2;", 2801 Style); 2802 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2803 Style.BraceWrapping.AfterEnum = true; 2804 verifyFormat("enum\n" 2805 "{\n" 2806 " A,\n" 2807 " B,\n" 2808 " C\n" 2809 "} ShortEnum1, ShortEnum2;", 2810 Style); 2811 verifyFormat("typedef enum\n" 2812 "{\n" 2813 " A,\n" 2814 " B,\n" 2815 " C\n" 2816 "} ShortEnum1, ShortEnum2;", 2817 Style); 2818 } 2819 2820 TEST_F(FormatTest, ShortCaseLabels) { 2821 FormatStyle Style = getLLVMStyle(); 2822 Style.AllowShortCaseLabelsOnASingleLine = true; 2823 verifyFormat("switch (a) {\n" 2824 "case 1: x = 1; break;\n" 2825 "case 2: return;\n" 2826 "case 3:\n" 2827 "case 4:\n" 2828 "case 5: return;\n" 2829 "case 6: // comment\n" 2830 " return;\n" 2831 "case 7:\n" 2832 " // comment\n" 2833 " return;\n" 2834 "case 8:\n" 2835 " x = 8; // comment\n" 2836 " break;\n" 2837 "default: y = 1; break;\n" 2838 "}", 2839 Style); 2840 verifyFormat("switch (a) {\n" 2841 "case 0: return; // comment\n" 2842 "case 1: break; // comment\n" 2843 "case 2: return;\n" 2844 "// comment\n" 2845 "case 3: return;\n" 2846 "// comment 1\n" 2847 "// comment 2\n" 2848 "// comment 3\n" 2849 "case 4: break; /* comment */\n" 2850 "case 5:\n" 2851 " // comment\n" 2852 " break;\n" 2853 "case 6: /* comment */ x = 1; break;\n" 2854 "case 7: x = /* comment */ 1; break;\n" 2855 "case 8:\n" 2856 " x = 1; /* comment */\n" 2857 " break;\n" 2858 "case 9:\n" 2859 " break; // comment line 1\n" 2860 " // comment line 2\n" 2861 "}", 2862 Style); 2863 EXPECT_EQ("switch (a) {\n" 2864 "case 1:\n" 2865 " x = 8;\n" 2866 " // fall through\n" 2867 "case 2: x = 8;\n" 2868 "// comment\n" 2869 "case 3:\n" 2870 " return; /* comment line 1\n" 2871 " * comment line 2 */\n" 2872 "case 4: i = 8;\n" 2873 "// something else\n" 2874 "#if FOO\n" 2875 "case 5: break;\n" 2876 "#endif\n" 2877 "}", 2878 format("switch (a) {\n" 2879 "case 1: x = 8;\n" 2880 " // fall through\n" 2881 "case 2:\n" 2882 " x = 8;\n" 2883 "// comment\n" 2884 "case 3:\n" 2885 " return; /* comment line 1\n" 2886 " * comment line 2 */\n" 2887 "case 4:\n" 2888 " i = 8;\n" 2889 "// something else\n" 2890 "#if FOO\n" 2891 "case 5: break;\n" 2892 "#endif\n" 2893 "}", 2894 Style)); 2895 EXPECT_EQ("switch (a) {\n" 2896 "case 0:\n" 2897 " return; // long long long long long long long long long long " 2898 "long long comment\n" 2899 " // line\n" 2900 "}", 2901 format("switch (a) {\n" 2902 "case 0: return; // long long long long long long long long " 2903 "long long long long comment line\n" 2904 "}", 2905 Style)); 2906 EXPECT_EQ("switch (a) {\n" 2907 "case 0:\n" 2908 " return; /* long long long long long long long long long long " 2909 "long long comment\n" 2910 " line */\n" 2911 "}", 2912 format("switch (a) {\n" 2913 "case 0: return; /* long long long long long long long long " 2914 "long long long long comment line */\n" 2915 "}", 2916 Style)); 2917 verifyFormat("switch (a) {\n" 2918 "#if FOO\n" 2919 "case 0: return 0;\n" 2920 "#endif\n" 2921 "}", 2922 Style); 2923 verifyFormat("switch (a) {\n" 2924 "case 1: {\n" 2925 "}\n" 2926 "case 2: {\n" 2927 " return;\n" 2928 "}\n" 2929 "case 3: {\n" 2930 " x = 1;\n" 2931 " return;\n" 2932 "}\n" 2933 "case 4:\n" 2934 " if (x)\n" 2935 " return;\n" 2936 "}", 2937 Style); 2938 Style.ColumnLimit = 21; 2939 verifyFormat("switch (a) {\n" 2940 "case 1: x = 1; break;\n" 2941 "case 2: return;\n" 2942 "case 3:\n" 2943 "case 4:\n" 2944 "case 5: return;\n" 2945 "default:\n" 2946 " y = 1;\n" 2947 " break;\n" 2948 "}", 2949 Style); 2950 Style.ColumnLimit = 80; 2951 Style.AllowShortCaseLabelsOnASingleLine = false; 2952 Style.IndentCaseLabels = true; 2953 EXPECT_EQ("switch (n) {\n" 2954 " default /*comments*/:\n" 2955 " return true;\n" 2956 " case 0:\n" 2957 " return false;\n" 2958 "}", 2959 format("switch (n) {\n" 2960 "default/*comments*/:\n" 2961 " return true;\n" 2962 "case 0:\n" 2963 " return false;\n" 2964 "}", 2965 Style)); 2966 Style.AllowShortCaseLabelsOnASingleLine = true; 2967 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2968 Style.BraceWrapping.AfterCaseLabel = true; 2969 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2970 EXPECT_EQ("switch (n)\n" 2971 "{\n" 2972 " case 0:\n" 2973 " {\n" 2974 " return false;\n" 2975 " }\n" 2976 " default:\n" 2977 " {\n" 2978 " return true;\n" 2979 " }\n" 2980 "}", 2981 format("switch (n) {\n" 2982 " case 0: {\n" 2983 " return false;\n" 2984 " }\n" 2985 " default:\n" 2986 " {\n" 2987 " return true;\n" 2988 " }\n" 2989 "}", 2990 Style)); 2991 } 2992 2993 TEST_F(FormatTest, FormatsLabels) { 2994 verifyFormat("void f() {\n" 2995 " some_code();\n" 2996 "test_label:\n" 2997 " some_other_code();\n" 2998 " {\n" 2999 " some_more_code();\n" 3000 " another_label:\n" 3001 " some_more_code();\n" 3002 " }\n" 3003 "}"); 3004 verifyFormat("{\n" 3005 " some_code();\n" 3006 "test_label:\n" 3007 " some_other_code();\n" 3008 "}"); 3009 verifyFormat("{\n" 3010 " some_code();\n" 3011 "test_label:;\n" 3012 " int i = 0;\n" 3013 "}"); 3014 FormatStyle Style = getLLVMStyle(); 3015 Style.IndentGotoLabels = false; 3016 verifyFormat("void f() {\n" 3017 " some_code();\n" 3018 "test_label:\n" 3019 " some_other_code();\n" 3020 " {\n" 3021 " some_more_code();\n" 3022 "another_label:\n" 3023 " some_more_code();\n" 3024 " }\n" 3025 "}", 3026 Style); 3027 verifyFormat("{\n" 3028 " some_code();\n" 3029 "test_label:\n" 3030 " some_other_code();\n" 3031 "}", 3032 Style); 3033 verifyFormat("{\n" 3034 " some_code();\n" 3035 "test_label:;\n" 3036 " int i = 0;\n" 3037 "}"); 3038 } 3039 3040 TEST_F(FormatTest, MultiLineControlStatements) { 3041 FormatStyle Style = getLLVMStyleWithColumns(20); 3042 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3043 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3044 // Short lines should keep opening brace on same line. 3045 EXPECT_EQ("if (foo) {\n" 3046 " bar();\n" 3047 "}", 3048 format("if(foo){bar();}", Style)); 3049 EXPECT_EQ("if (foo) {\n" 3050 " bar();\n" 3051 "} else {\n" 3052 " baz();\n" 3053 "}", 3054 format("if(foo){bar();}else{baz();}", Style)); 3055 EXPECT_EQ("if (foo && bar) {\n" 3056 " baz();\n" 3057 "}", 3058 format("if(foo&&bar){baz();}", Style)); 3059 EXPECT_EQ("if (foo) {\n" 3060 " bar();\n" 3061 "} else if (baz) {\n" 3062 " quux();\n" 3063 "}", 3064 format("if(foo){bar();}else if(baz){quux();}", Style)); 3065 EXPECT_EQ( 3066 "if (foo) {\n" 3067 " bar();\n" 3068 "} else if (baz) {\n" 3069 " quux();\n" 3070 "} else {\n" 3071 " foobar();\n" 3072 "}", 3073 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 3074 EXPECT_EQ("for (;;) {\n" 3075 " foo();\n" 3076 "}", 3077 format("for(;;){foo();}")); 3078 EXPECT_EQ("while (1) {\n" 3079 " foo();\n" 3080 "}", 3081 format("while(1){foo();}", Style)); 3082 EXPECT_EQ("switch (foo) {\n" 3083 "case bar:\n" 3084 " return;\n" 3085 "}", 3086 format("switch(foo){case bar:return;}", Style)); 3087 EXPECT_EQ("try {\n" 3088 " foo();\n" 3089 "} catch (...) {\n" 3090 " bar();\n" 3091 "}", 3092 format("try{foo();}catch(...){bar();}", Style)); 3093 EXPECT_EQ("do {\n" 3094 " foo();\n" 3095 "} while (bar &&\n" 3096 " baz);", 3097 format("do{foo();}while(bar&&baz);", Style)); 3098 // Long lines should put opening brace on new line. 3099 verifyFormat("void f() {\n" 3100 " if (a1 && a2 &&\n" 3101 " a3)\n" 3102 " {\n" 3103 " quux();\n" 3104 " }\n" 3105 "}", 3106 "void f(){if(a1&&a2&&a3){quux();}}", Style); 3107 EXPECT_EQ("if (foo && bar &&\n" 3108 " baz)\n" 3109 "{\n" 3110 " quux();\n" 3111 "}", 3112 format("if(foo&&bar&&baz){quux();}", Style)); 3113 EXPECT_EQ("if (foo && bar &&\n" 3114 " baz)\n" 3115 "{\n" 3116 " quux();\n" 3117 "}", 3118 format("if (foo && bar &&\n" 3119 " baz) {\n" 3120 " quux();\n" 3121 "}", 3122 Style)); 3123 EXPECT_EQ("if (foo) {\n" 3124 " bar();\n" 3125 "} else if (baz ||\n" 3126 " quux)\n" 3127 "{\n" 3128 " foobar();\n" 3129 "}", 3130 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 3131 EXPECT_EQ( 3132 "if (foo) {\n" 3133 " bar();\n" 3134 "} else if (baz ||\n" 3135 " quux)\n" 3136 "{\n" 3137 " foobar();\n" 3138 "} else {\n" 3139 " barbaz();\n" 3140 "}", 3141 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3142 Style)); 3143 EXPECT_EQ("for (int i = 0;\n" 3144 " i < 10; ++i)\n" 3145 "{\n" 3146 " foo();\n" 3147 "}", 3148 format("for(int i=0;i<10;++i){foo();}", Style)); 3149 EXPECT_EQ("foreach (int i,\n" 3150 " list)\n" 3151 "{\n" 3152 " foo();\n" 3153 "}", 3154 format("foreach(int i, list){foo();}", Style)); 3155 Style.ColumnLimit = 3156 40; // to concentrate at brace wrapping, not line wrap due to column limit 3157 EXPECT_EQ("foreach (int i, list) {\n" 3158 " foo();\n" 3159 "}", 3160 format("foreach(int i, list){foo();}", Style)); 3161 Style.ColumnLimit = 3162 20; // to concentrate at brace wrapping, not line wrap due to column limit 3163 EXPECT_EQ("while (foo || bar ||\n" 3164 " baz)\n" 3165 "{\n" 3166 " quux();\n" 3167 "}", 3168 format("while(foo||bar||baz){quux();}", Style)); 3169 EXPECT_EQ("switch (\n" 3170 " foo = barbaz)\n" 3171 "{\n" 3172 "case quux:\n" 3173 " return;\n" 3174 "}", 3175 format("switch(foo=barbaz){case quux:return;}", Style)); 3176 EXPECT_EQ("try {\n" 3177 " foo();\n" 3178 "} catch (\n" 3179 " Exception &bar)\n" 3180 "{\n" 3181 " baz();\n" 3182 "}", 3183 format("try{foo();}catch(Exception&bar){baz();}", Style)); 3184 Style.ColumnLimit = 3185 40; // to concentrate at brace wrapping, not line wrap due to column limit 3186 EXPECT_EQ("try {\n" 3187 " foo();\n" 3188 "} catch (Exception &bar) {\n" 3189 " baz();\n" 3190 "}", 3191 format("try{foo();}catch(Exception&bar){baz();}", Style)); 3192 Style.ColumnLimit = 3193 20; // to concentrate at brace wrapping, not line wrap due to column limit 3194 3195 Style.BraceWrapping.BeforeElse = true; 3196 EXPECT_EQ( 3197 "if (foo) {\n" 3198 " bar();\n" 3199 "}\n" 3200 "else if (baz ||\n" 3201 " quux)\n" 3202 "{\n" 3203 " foobar();\n" 3204 "}\n" 3205 "else {\n" 3206 " barbaz();\n" 3207 "}", 3208 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3209 Style)); 3210 3211 Style.BraceWrapping.BeforeCatch = true; 3212 EXPECT_EQ("try {\n" 3213 " foo();\n" 3214 "}\n" 3215 "catch (...) {\n" 3216 " baz();\n" 3217 "}", 3218 format("try{foo();}catch(...){baz();}", Style)); 3219 3220 Style.BraceWrapping.AfterFunction = true; 3221 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3222 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3223 Style.ColumnLimit = 80; 3224 verifyFormat("void shortfunction() { bar(); }", Style); 3225 3226 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3227 verifyFormat("void shortfunction()\n" 3228 "{\n" 3229 " bar();\n" 3230 "}", 3231 Style); 3232 } 3233 3234 TEST_F(FormatTest, BeforeWhile) { 3235 FormatStyle Style = getLLVMStyle(); 3236 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3237 3238 verifyFormat("do {\n" 3239 " foo();\n" 3240 "} while (1);", 3241 Style); 3242 Style.BraceWrapping.BeforeWhile = true; 3243 verifyFormat("do {\n" 3244 " foo();\n" 3245 "}\n" 3246 "while (1);", 3247 Style); 3248 } 3249 3250 //===----------------------------------------------------------------------===// 3251 // Tests for classes, namespaces, etc. 3252 //===----------------------------------------------------------------------===// 3253 3254 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 3255 verifyFormat("class A {};"); 3256 } 3257 3258 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 3259 verifyFormat("class A {\n" 3260 "public:\n" 3261 "public: // comment\n" 3262 "protected:\n" 3263 "private:\n" 3264 " void f() {}\n" 3265 "};"); 3266 verifyFormat("export class A {\n" 3267 "public:\n" 3268 "public: // comment\n" 3269 "protected:\n" 3270 "private:\n" 3271 " void f() {}\n" 3272 "};"); 3273 verifyGoogleFormat("class A {\n" 3274 " public:\n" 3275 " protected:\n" 3276 " private:\n" 3277 " void f() {}\n" 3278 "};"); 3279 verifyGoogleFormat("export class A {\n" 3280 " public:\n" 3281 " protected:\n" 3282 " private:\n" 3283 " void f() {}\n" 3284 "};"); 3285 verifyFormat("class A {\n" 3286 "public slots:\n" 3287 " void f1() {}\n" 3288 "public Q_SLOTS:\n" 3289 " void f2() {}\n" 3290 "protected slots:\n" 3291 " void f3() {}\n" 3292 "protected Q_SLOTS:\n" 3293 " void f4() {}\n" 3294 "private slots:\n" 3295 " void f5() {}\n" 3296 "private Q_SLOTS:\n" 3297 " void f6() {}\n" 3298 "signals:\n" 3299 " void g1();\n" 3300 "Q_SIGNALS:\n" 3301 " void g2();\n" 3302 "};"); 3303 3304 // Don't interpret 'signals' the wrong way. 3305 verifyFormat("signals.set();"); 3306 verifyFormat("for (Signals signals : f()) {\n}"); 3307 verifyFormat("{\n" 3308 " signals.set(); // This needs indentation.\n" 3309 "}"); 3310 verifyFormat("void f() {\n" 3311 "label:\n" 3312 " signals.baz();\n" 3313 "}"); 3314 verifyFormat("private[1];"); 3315 verifyFormat("testArray[public] = 1;"); 3316 verifyFormat("public();"); 3317 verifyFormat("myFunc(public);"); 3318 verifyFormat("std::vector<int> testVec = {private};"); 3319 verifyFormat("private.p = 1;"); 3320 verifyFormat("void function(private...){};"); 3321 verifyFormat("if (private && public)\n"); 3322 verifyFormat("private &= true;"); 3323 verifyFormat("int x = private * public;"); 3324 verifyFormat("public *= private;"); 3325 verifyFormat("int x = public + private;"); 3326 verifyFormat("private++;"); 3327 verifyFormat("++private;"); 3328 verifyFormat("public += private;"); 3329 verifyFormat("public = public - private;"); 3330 verifyFormat("public->foo();"); 3331 verifyFormat("private--;"); 3332 verifyFormat("--private;"); 3333 verifyFormat("public -= 1;"); 3334 verifyFormat("if (!private && !public)\n"); 3335 verifyFormat("public != private;"); 3336 verifyFormat("int x = public / private;"); 3337 verifyFormat("public /= 2;"); 3338 verifyFormat("public = public % 2;"); 3339 verifyFormat("public %= 2;"); 3340 verifyFormat("if (public < private)\n"); 3341 verifyFormat("public << private;"); 3342 verifyFormat("public <<= private;"); 3343 verifyFormat("if (public > private)\n"); 3344 verifyFormat("public >> private;"); 3345 verifyFormat("public >>= private;"); 3346 verifyFormat("public ^ private;"); 3347 verifyFormat("public ^= private;"); 3348 verifyFormat("public | private;"); 3349 verifyFormat("public |= private;"); 3350 verifyFormat("auto x = private ? 1 : 2;"); 3351 verifyFormat("if (public == private)\n"); 3352 verifyFormat("void foo(public, private)"); 3353 verifyFormat("public::foo();"); 3354 3355 verifyFormat("class A {\n" 3356 "public:\n" 3357 " std::unique_ptr<int *[]> b() { return nullptr; }\n" 3358 "\n" 3359 "private:\n" 3360 " int c;\n" 3361 "};"); 3362 } 3363 3364 TEST_F(FormatTest, SeparatesLogicalBlocks) { 3365 EXPECT_EQ("class A {\n" 3366 "public:\n" 3367 " void f();\n" 3368 "\n" 3369 "private:\n" 3370 " void g() {}\n" 3371 " // test\n" 3372 "protected:\n" 3373 " int h;\n" 3374 "};", 3375 format("class A {\n" 3376 "public:\n" 3377 "void f();\n" 3378 "private:\n" 3379 "void g() {}\n" 3380 "// test\n" 3381 "protected:\n" 3382 "int h;\n" 3383 "};")); 3384 EXPECT_EQ("class A {\n" 3385 "protected:\n" 3386 "public:\n" 3387 " void f();\n" 3388 "};", 3389 format("class A {\n" 3390 "protected:\n" 3391 "\n" 3392 "public:\n" 3393 "\n" 3394 " void f();\n" 3395 "};")); 3396 3397 // Even ensure proper spacing inside macros. 3398 EXPECT_EQ("#define B \\\n" 3399 " class A { \\\n" 3400 " protected: \\\n" 3401 " public: \\\n" 3402 " void f(); \\\n" 3403 " };", 3404 format("#define B \\\n" 3405 " class A { \\\n" 3406 " protected: \\\n" 3407 " \\\n" 3408 " public: \\\n" 3409 " \\\n" 3410 " void f(); \\\n" 3411 " };", 3412 getGoogleStyle())); 3413 // But don't remove empty lines after macros ending in access specifiers. 3414 EXPECT_EQ("#define A private:\n" 3415 "\n" 3416 "int i;", 3417 format("#define A private:\n" 3418 "\n" 3419 "int i;")); 3420 } 3421 3422 TEST_F(FormatTest, FormatsClasses) { 3423 verifyFormat("class A : public B {};"); 3424 verifyFormat("class A : public ::B {};"); 3425 3426 verifyFormat( 3427 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3428 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3429 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3430 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3431 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3432 verifyFormat( 3433 "class A : public B, public C, public D, public E, public F {};"); 3434 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3435 " public C,\n" 3436 " public D,\n" 3437 " public E,\n" 3438 " public F,\n" 3439 " public G {};"); 3440 3441 verifyFormat("class\n" 3442 " ReallyReallyLongClassName {\n" 3443 " int i;\n" 3444 "};", 3445 getLLVMStyleWithColumns(32)); 3446 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3447 " aaaaaaaaaaaaaaaa> {};"); 3448 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3449 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3450 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3451 verifyFormat("template <class R, class C>\n" 3452 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3453 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3454 verifyFormat("class ::A::B {};"); 3455 } 3456 3457 TEST_F(FormatTest, BreakInheritanceStyle) { 3458 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3459 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3460 FormatStyle::BILS_BeforeComma; 3461 verifyFormat("class MyClass : public X {};", 3462 StyleWithInheritanceBreakBeforeComma); 3463 verifyFormat("class MyClass\n" 3464 " : public X\n" 3465 " , public Y {};", 3466 StyleWithInheritanceBreakBeforeComma); 3467 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3468 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3469 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3470 StyleWithInheritanceBreakBeforeComma); 3471 verifyFormat("struct aaaaaaaaaaaaa\n" 3472 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3473 " aaaaaaaaaaaaaaaa> {};", 3474 StyleWithInheritanceBreakBeforeComma); 3475 3476 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3477 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3478 FormatStyle::BILS_AfterColon; 3479 verifyFormat("class MyClass : public X {};", 3480 StyleWithInheritanceBreakAfterColon); 3481 verifyFormat("class MyClass : public X, public Y {};", 3482 StyleWithInheritanceBreakAfterColon); 3483 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3484 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3485 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3486 StyleWithInheritanceBreakAfterColon); 3487 verifyFormat("struct aaaaaaaaaaaaa :\n" 3488 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3489 " aaaaaaaaaaaaaaaa> {};", 3490 StyleWithInheritanceBreakAfterColon); 3491 3492 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3493 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3494 FormatStyle::BILS_AfterComma; 3495 verifyFormat("class MyClass : public X {};", 3496 StyleWithInheritanceBreakAfterComma); 3497 verifyFormat("class MyClass : public X,\n" 3498 " public Y {};", 3499 StyleWithInheritanceBreakAfterComma); 3500 verifyFormat( 3501 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3502 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3503 "{};", 3504 StyleWithInheritanceBreakAfterComma); 3505 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3506 " aaaaaaaaaaaaaaaa> {};", 3507 StyleWithInheritanceBreakAfterComma); 3508 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3509 " : public OnceBreak,\n" 3510 " public AlwaysBreak,\n" 3511 " EvenBasesFitInOneLine {};", 3512 StyleWithInheritanceBreakAfterComma); 3513 } 3514 3515 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) { 3516 verifyFormat("class A {\n} a, b;"); 3517 verifyFormat("struct A {\n} a, b;"); 3518 verifyFormat("union A {\n} a, b;"); 3519 3520 verifyFormat("constexpr class A {\n} a, b;"); 3521 verifyFormat("constexpr struct A {\n} a, b;"); 3522 verifyFormat("constexpr union A {\n} a, b;"); 3523 3524 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace"); 3525 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace"); 3526 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace"); 3527 3528 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace"); 3529 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace"); 3530 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace"); 3531 3532 verifyFormat("namespace ns {\n" 3533 "class {\n" 3534 "} a, b;\n" 3535 "} // namespace ns"); 3536 verifyFormat("namespace ns {\n" 3537 "const class {\n" 3538 "} a, b;\n" 3539 "} // namespace ns"); 3540 verifyFormat("namespace ns {\n" 3541 "constexpr class C {\n" 3542 "} a, b;\n" 3543 "} // namespace ns"); 3544 verifyFormat("namespace ns {\n" 3545 "class { /* comment */\n" 3546 "} a, b;\n" 3547 "} // namespace ns"); 3548 verifyFormat("namespace ns {\n" 3549 "const class { /* comment */\n" 3550 "} a, b;\n" 3551 "} // namespace ns"); 3552 } 3553 3554 TEST_F(FormatTest, FormatsEnum) { 3555 verifyFormat("enum {\n" 3556 " Zero,\n" 3557 " One = 1,\n" 3558 " Two = One + 1,\n" 3559 " Three = (One + Two),\n" 3560 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3561 " Five = (One, Two, Three, Four, 5)\n" 3562 "};"); 3563 verifyGoogleFormat("enum {\n" 3564 " Zero,\n" 3565 " One = 1,\n" 3566 " Two = One + 1,\n" 3567 " Three = (One + Two),\n" 3568 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3569 " Five = (One, Two, Three, Four, 5)\n" 3570 "};"); 3571 verifyFormat("enum Enum {};"); 3572 verifyFormat("enum {};"); 3573 verifyFormat("enum X E {} d;"); 3574 verifyFormat("enum __attribute__((...)) E {} d;"); 3575 verifyFormat("enum __declspec__((...)) E {} d;"); 3576 verifyFormat("enum {\n" 3577 " Bar = Foo<int, int>::value\n" 3578 "};", 3579 getLLVMStyleWithColumns(30)); 3580 3581 verifyFormat("enum ShortEnum { A, B, C };"); 3582 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3583 3584 EXPECT_EQ("enum KeepEmptyLines {\n" 3585 " ONE,\n" 3586 "\n" 3587 " TWO,\n" 3588 "\n" 3589 " THREE\n" 3590 "}", 3591 format("enum KeepEmptyLines {\n" 3592 " ONE,\n" 3593 "\n" 3594 " TWO,\n" 3595 "\n" 3596 "\n" 3597 " THREE\n" 3598 "}")); 3599 verifyFormat("enum E { // comment\n" 3600 " ONE,\n" 3601 " TWO\n" 3602 "};\n" 3603 "int i;"); 3604 3605 FormatStyle EightIndent = getLLVMStyle(); 3606 EightIndent.IndentWidth = 8; 3607 verifyFormat("enum {\n" 3608 " VOID,\n" 3609 " CHAR,\n" 3610 " SHORT,\n" 3611 " INT,\n" 3612 " LONG,\n" 3613 " SIGNED,\n" 3614 " UNSIGNED,\n" 3615 " BOOL,\n" 3616 " FLOAT,\n" 3617 " DOUBLE,\n" 3618 " COMPLEX\n" 3619 "};", 3620 EightIndent); 3621 3622 // Not enums. 3623 verifyFormat("enum X f() {\n" 3624 " a();\n" 3625 " return 42;\n" 3626 "}"); 3627 verifyFormat("enum X Type::f() {\n" 3628 " a();\n" 3629 " return 42;\n" 3630 "}"); 3631 verifyFormat("enum ::X f() {\n" 3632 " a();\n" 3633 " return 42;\n" 3634 "}"); 3635 verifyFormat("enum ns::X f() {\n" 3636 " a();\n" 3637 " return 42;\n" 3638 "}"); 3639 } 3640 3641 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3642 verifyFormat("enum Type {\n" 3643 " One = 0; // These semicolons should be commas.\n" 3644 " Two = 1;\n" 3645 "};"); 3646 verifyFormat("namespace n {\n" 3647 "enum Type {\n" 3648 " One,\n" 3649 " Two, // missing };\n" 3650 " int i;\n" 3651 "}\n" 3652 "void g() {}"); 3653 } 3654 3655 TEST_F(FormatTest, FormatsEnumStruct) { 3656 verifyFormat("enum struct {\n" 3657 " Zero,\n" 3658 " One = 1,\n" 3659 " Two = One + 1,\n" 3660 " Three = (One + Two),\n" 3661 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3662 " Five = (One, Two, Three, Four, 5)\n" 3663 "};"); 3664 verifyFormat("enum struct Enum {};"); 3665 verifyFormat("enum struct {};"); 3666 verifyFormat("enum struct X E {} d;"); 3667 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3668 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3669 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3670 } 3671 3672 TEST_F(FormatTest, FormatsEnumClass) { 3673 verifyFormat("enum class {\n" 3674 " Zero,\n" 3675 " One = 1,\n" 3676 " Two = One + 1,\n" 3677 " Three = (One + Two),\n" 3678 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3679 " Five = (One, Two, Three, Four, 5)\n" 3680 "};"); 3681 verifyFormat("enum class Enum {};"); 3682 verifyFormat("enum class {};"); 3683 verifyFormat("enum class X E {} d;"); 3684 verifyFormat("enum class __attribute__((...)) E {} d;"); 3685 verifyFormat("enum class __declspec__((...)) E {} d;"); 3686 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3687 } 3688 3689 TEST_F(FormatTest, FormatsEnumTypes) { 3690 verifyFormat("enum X : int {\n" 3691 " A, // Force multiple lines.\n" 3692 " B\n" 3693 "};"); 3694 verifyFormat("enum X : int { A, B };"); 3695 verifyFormat("enum X : std::uint32_t { A, B };"); 3696 } 3697 3698 TEST_F(FormatTest, FormatsTypedefEnum) { 3699 FormatStyle Style = getLLVMStyleWithColumns(40); 3700 verifyFormat("typedef enum {} EmptyEnum;"); 3701 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3702 verifyFormat("typedef enum {\n" 3703 " ZERO = 0,\n" 3704 " ONE = 1,\n" 3705 " TWO = 2,\n" 3706 " THREE = 3\n" 3707 "} LongEnum;", 3708 Style); 3709 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3710 Style.BraceWrapping.AfterEnum = true; 3711 verifyFormat("typedef enum {} EmptyEnum;"); 3712 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3713 verifyFormat("typedef enum\n" 3714 "{\n" 3715 " ZERO = 0,\n" 3716 " ONE = 1,\n" 3717 " TWO = 2,\n" 3718 " THREE = 3\n" 3719 "} LongEnum;", 3720 Style); 3721 } 3722 3723 TEST_F(FormatTest, FormatsNSEnums) { 3724 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3725 verifyGoogleFormat( 3726 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3727 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3728 " // Information about someDecentlyLongValue.\n" 3729 " someDecentlyLongValue,\n" 3730 " // Information about anotherDecentlyLongValue.\n" 3731 " anotherDecentlyLongValue,\n" 3732 " // Information about aThirdDecentlyLongValue.\n" 3733 " aThirdDecentlyLongValue\n" 3734 "};"); 3735 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3736 " // Information about someDecentlyLongValue.\n" 3737 " someDecentlyLongValue,\n" 3738 " // Information about anotherDecentlyLongValue.\n" 3739 " anotherDecentlyLongValue,\n" 3740 " // Information about aThirdDecentlyLongValue.\n" 3741 " aThirdDecentlyLongValue\n" 3742 "};"); 3743 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3744 " a = 1,\n" 3745 " b = 2,\n" 3746 " c = 3,\n" 3747 "};"); 3748 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3749 " a = 1,\n" 3750 " b = 2,\n" 3751 " c = 3,\n" 3752 "};"); 3753 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3754 " a = 1,\n" 3755 " b = 2,\n" 3756 " c = 3,\n" 3757 "};"); 3758 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3759 " a = 1,\n" 3760 " b = 2,\n" 3761 " c = 3,\n" 3762 "};"); 3763 } 3764 3765 TEST_F(FormatTest, FormatsBitfields) { 3766 verifyFormat("struct Bitfields {\n" 3767 " unsigned sClass : 8;\n" 3768 " unsigned ValueKind : 2;\n" 3769 "};"); 3770 verifyFormat("struct A {\n" 3771 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3772 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3773 "};"); 3774 verifyFormat("struct MyStruct {\n" 3775 " uchar data;\n" 3776 " uchar : 8;\n" 3777 " uchar : 8;\n" 3778 " uchar other;\n" 3779 "};"); 3780 FormatStyle Style = getLLVMStyle(); 3781 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3782 verifyFormat("struct Bitfields {\n" 3783 " unsigned sClass:8;\n" 3784 " unsigned ValueKind:2;\n" 3785 " uchar other;\n" 3786 "};", 3787 Style); 3788 verifyFormat("struct A {\n" 3789 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3790 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3791 "};", 3792 Style); 3793 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3794 verifyFormat("struct Bitfields {\n" 3795 " unsigned sClass :8;\n" 3796 " unsigned ValueKind :2;\n" 3797 " uchar other;\n" 3798 "};", 3799 Style); 3800 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3801 verifyFormat("struct Bitfields {\n" 3802 " unsigned sClass: 8;\n" 3803 " unsigned ValueKind: 2;\n" 3804 " uchar other;\n" 3805 "};", 3806 Style); 3807 } 3808 3809 TEST_F(FormatTest, FormatsNamespaces) { 3810 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3811 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3812 3813 verifyFormat("namespace some_namespace {\n" 3814 "class A {};\n" 3815 "void f() { f(); }\n" 3816 "}", 3817 LLVMWithNoNamespaceFix); 3818 verifyFormat("#define M(x) x##x\n" 3819 "namespace M(x) {\n" 3820 "class A {};\n" 3821 "void f() { f(); }\n" 3822 "}", 3823 LLVMWithNoNamespaceFix); 3824 verifyFormat("#define M(x) x##x\n" 3825 "namespace N::inline M(x) {\n" 3826 "class A {};\n" 3827 "void f() { f(); }\n" 3828 "}", 3829 LLVMWithNoNamespaceFix); 3830 verifyFormat("#define M(x) x##x\n" 3831 "namespace M(x)::inline N {\n" 3832 "class A {};\n" 3833 "void f() { f(); }\n" 3834 "}", 3835 LLVMWithNoNamespaceFix); 3836 verifyFormat("#define M(x) x##x\n" 3837 "namespace N::M(x) {\n" 3838 "class A {};\n" 3839 "void f() { f(); }\n" 3840 "}", 3841 LLVMWithNoNamespaceFix); 3842 verifyFormat("#define M(x) x##x\n" 3843 "namespace M::N(x) {\n" 3844 "class A {};\n" 3845 "void f() { f(); }\n" 3846 "}", 3847 LLVMWithNoNamespaceFix); 3848 verifyFormat("namespace N::inline D {\n" 3849 "class A {};\n" 3850 "void f() { f(); }\n" 3851 "}", 3852 LLVMWithNoNamespaceFix); 3853 verifyFormat("namespace N::inline D::E {\n" 3854 "class A {};\n" 3855 "void f() { f(); }\n" 3856 "}", 3857 LLVMWithNoNamespaceFix); 3858 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3859 "class A {};\n" 3860 "void f() { f(); }\n" 3861 "}", 3862 LLVMWithNoNamespaceFix); 3863 verifyFormat("/* something */ namespace some_namespace {\n" 3864 "class A {};\n" 3865 "void f() { f(); }\n" 3866 "}", 3867 LLVMWithNoNamespaceFix); 3868 verifyFormat("namespace {\n" 3869 "class A {};\n" 3870 "void f() { f(); }\n" 3871 "}", 3872 LLVMWithNoNamespaceFix); 3873 verifyFormat("/* something */ namespace {\n" 3874 "class A {};\n" 3875 "void f() { f(); }\n" 3876 "}", 3877 LLVMWithNoNamespaceFix); 3878 verifyFormat("inline namespace X {\n" 3879 "class A {};\n" 3880 "void f() { f(); }\n" 3881 "}", 3882 LLVMWithNoNamespaceFix); 3883 verifyFormat("/* something */ inline namespace X {\n" 3884 "class A {};\n" 3885 "void f() { f(); }\n" 3886 "}", 3887 LLVMWithNoNamespaceFix); 3888 verifyFormat("export namespace X {\n" 3889 "class A {};\n" 3890 "void f() { f(); }\n" 3891 "}", 3892 LLVMWithNoNamespaceFix); 3893 verifyFormat("using namespace some_namespace;\n" 3894 "class A {};\n" 3895 "void f() { f(); }", 3896 LLVMWithNoNamespaceFix); 3897 3898 // This code is more common than we thought; if we 3899 // layout this correctly the semicolon will go into 3900 // its own line, which is undesirable. 3901 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3902 verifyFormat("namespace {\n" 3903 "class A {};\n" 3904 "};", 3905 LLVMWithNoNamespaceFix); 3906 3907 verifyFormat("namespace {\n" 3908 "int SomeVariable = 0; // comment\n" 3909 "} // namespace", 3910 LLVMWithNoNamespaceFix); 3911 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3912 "#define HEADER_GUARD\n" 3913 "namespace my_namespace {\n" 3914 "int i;\n" 3915 "} // my_namespace\n" 3916 "#endif // HEADER_GUARD", 3917 format("#ifndef HEADER_GUARD\n" 3918 " #define HEADER_GUARD\n" 3919 " namespace my_namespace {\n" 3920 "int i;\n" 3921 "} // my_namespace\n" 3922 "#endif // HEADER_GUARD", 3923 LLVMWithNoNamespaceFix)); 3924 3925 EXPECT_EQ("namespace A::B {\n" 3926 "class C {};\n" 3927 "}", 3928 format("namespace A::B {\n" 3929 "class C {};\n" 3930 "}", 3931 LLVMWithNoNamespaceFix)); 3932 3933 FormatStyle Style = getLLVMStyle(); 3934 Style.NamespaceIndentation = FormatStyle::NI_All; 3935 EXPECT_EQ("namespace out {\n" 3936 " int i;\n" 3937 " namespace in {\n" 3938 " int i;\n" 3939 " } // namespace in\n" 3940 "} // namespace out", 3941 format("namespace out {\n" 3942 "int i;\n" 3943 "namespace in {\n" 3944 "int i;\n" 3945 "} // namespace in\n" 3946 "} // namespace out", 3947 Style)); 3948 3949 FormatStyle ShortInlineFunctions = getLLVMStyle(); 3950 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All; 3951 ShortInlineFunctions.AllowShortFunctionsOnASingleLine = 3952 FormatStyle::SFS_Inline; 3953 verifyFormat("namespace {\n" 3954 " void f() {\n" 3955 " return;\n" 3956 " }\n" 3957 "} // namespace\n", 3958 ShortInlineFunctions); 3959 verifyFormat("namespace { /* comment */\n" 3960 " void f() {\n" 3961 " return;\n" 3962 " }\n" 3963 "} // namespace\n", 3964 ShortInlineFunctions); 3965 verifyFormat("namespace { // comment\n" 3966 " void f() {\n" 3967 " return;\n" 3968 " }\n" 3969 "} // namespace\n", 3970 ShortInlineFunctions); 3971 verifyFormat("namespace {\n" 3972 " int some_int;\n" 3973 " void f() {\n" 3974 " return;\n" 3975 " }\n" 3976 "} // namespace\n", 3977 ShortInlineFunctions); 3978 verifyFormat("namespace interface {\n" 3979 " void f() {\n" 3980 " return;\n" 3981 " }\n" 3982 "} // namespace interface\n", 3983 ShortInlineFunctions); 3984 verifyFormat("namespace {\n" 3985 " class X {\n" 3986 " void f() { return; }\n" 3987 " };\n" 3988 "} // namespace\n", 3989 ShortInlineFunctions); 3990 verifyFormat("namespace {\n" 3991 " class X { /* comment */\n" 3992 " void f() { return; }\n" 3993 " };\n" 3994 "} // namespace\n", 3995 ShortInlineFunctions); 3996 verifyFormat("namespace {\n" 3997 " class X { // comment\n" 3998 " void f() { return; }\n" 3999 " };\n" 4000 "} // namespace\n", 4001 ShortInlineFunctions); 4002 verifyFormat("namespace {\n" 4003 " struct X {\n" 4004 " void f() { return; }\n" 4005 " };\n" 4006 "} // namespace\n", 4007 ShortInlineFunctions); 4008 verifyFormat("namespace {\n" 4009 " union X {\n" 4010 " void f() { return; }\n" 4011 " };\n" 4012 "} // namespace\n", 4013 ShortInlineFunctions); 4014 verifyFormat("extern \"C\" {\n" 4015 "void f() {\n" 4016 " return;\n" 4017 "}\n" 4018 "} // namespace\n", 4019 ShortInlineFunctions); 4020 verifyFormat("namespace {\n" 4021 " class X {\n" 4022 " void f() { return; }\n" 4023 " } x;\n" 4024 "} // namespace\n", 4025 ShortInlineFunctions); 4026 verifyFormat("namespace {\n" 4027 " [[nodiscard]] class X {\n" 4028 " void f() { return; }\n" 4029 " };\n" 4030 "} // namespace\n", 4031 ShortInlineFunctions); 4032 verifyFormat("namespace {\n" 4033 " static class X {\n" 4034 " void f() { return; }\n" 4035 " } x;\n" 4036 "} // namespace\n", 4037 ShortInlineFunctions); 4038 verifyFormat("namespace {\n" 4039 " constexpr class X {\n" 4040 " void f() { return; }\n" 4041 " } x;\n" 4042 "} // namespace\n", 4043 ShortInlineFunctions); 4044 4045 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent; 4046 verifyFormat("extern \"C\" {\n" 4047 " void f() {\n" 4048 " return;\n" 4049 " }\n" 4050 "} // namespace\n", 4051 ShortInlineFunctions); 4052 4053 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4054 EXPECT_EQ("namespace out {\n" 4055 "int i;\n" 4056 "namespace in {\n" 4057 " int i;\n" 4058 "} // namespace in\n" 4059 "} // namespace out", 4060 format("namespace out {\n" 4061 "int i;\n" 4062 "namespace in {\n" 4063 "int i;\n" 4064 "} // namespace in\n" 4065 "} // namespace out", 4066 Style)); 4067 4068 Style.NamespaceIndentation = FormatStyle::NI_None; 4069 verifyFormat("template <class T>\n" 4070 "concept a_concept = X<>;\n" 4071 "namespace B {\n" 4072 "struct b_struct {};\n" 4073 "} // namespace B\n", 4074 Style); 4075 verifyFormat("template <int I>\n" 4076 "constexpr void foo()\n" 4077 " requires(I == 42)\n" 4078 "{}\n" 4079 "namespace ns {\n" 4080 "void foo() {}\n" 4081 "} // namespace ns\n", 4082 Style); 4083 } 4084 4085 TEST_F(FormatTest, NamespaceMacros) { 4086 FormatStyle Style = getLLVMStyle(); 4087 Style.NamespaceMacros.push_back("TESTSUITE"); 4088 4089 verifyFormat("TESTSUITE(A) {\n" 4090 "int foo();\n" 4091 "} // TESTSUITE(A)", 4092 Style); 4093 4094 verifyFormat("TESTSUITE(A, B) {\n" 4095 "int foo();\n" 4096 "} // TESTSUITE(A)", 4097 Style); 4098 4099 // Properly indent according to NamespaceIndentation style 4100 Style.NamespaceIndentation = FormatStyle::NI_All; 4101 verifyFormat("TESTSUITE(A) {\n" 4102 " int foo();\n" 4103 "} // TESTSUITE(A)", 4104 Style); 4105 verifyFormat("TESTSUITE(A) {\n" 4106 " namespace B {\n" 4107 " int foo();\n" 4108 " } // namespace B\n" 4109 "} // TESTSUITE(A)", 4110 Style); 4111 verifyFormat("namespace A {\n" 4112 " TESTSUITE(B) {\n" 4113 " int foo();\n" 4114 " } // TESTSUITE(B)\n" 4115 "} // namespace A", 4116 Style); 4117 4118 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4119 verifyFormat("TESTSUITE(A) {\n" 4120 "TESTSUITE(B) {\n" 4121 " int foo();\n" 4122 "} // TESTSUITE(B)\n" 4123 "} // TESTSUITE(A)", 4124 Style); 4125 verifyFormat("TESTSUITE(A) {\n" 4126 "namespace B {\n" 4127 " int foo();\n" 4128 "} // namespace B\n" 4129 "} // TESTSUITE(A)", 4130 Style); 4131 verifyFormat("namespace A {\n" 4132 "TESTSUITE(B) {\n" 4133 " int foo();\n" 4134 "} // TESTSUITE(B)\n" 4135 "} // namespace A", 4136 Style); 4137 4138 // Properly merge namespace-macros blocks in CompactNamespaces mode 4139 Style.NamespaceIndentation = FormatStyle::NI_None; 4140 Style.CompactNamespaces = true; 4141 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 4142 "}} // TESTSUITE(A::B)", 4143 Style); 4144 4145 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 4146 "}} // TESTSUITE(out::in)", 4147 format("TESTSUITE(out) {\n" 4148 "TESTSUITE(in) {\n" 4149 "} // TESTSUITE(in)\n" 4150 "} // TESTSUITE(out)", 4151 Style)); 4152 4153 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 4154 "}} // TESTSUITE(out::in)", 4155 format("TESTSUITE(out) {\n" 4156 "TESTSUITE(in) {\n" 4157 "} // TESTSUITE(in)\n" 4158 "} // TESTSUITE(out)", 4159 Style)); 4160 4161 // Do not merge different namespaces/macros 4162 EXPECT_EQ("namespace out {\n" 4163 "TESTSUITE(in) {\n" 4164 "} // TESTSUITE(in)\n" 4165 "} // namespace out", 4166 format("namespace out {\n" 4167 "TESTSUITE(in) {\n" 4168 "} // TESTSUITE(in)\n" 4169 "} // namespace out", 4170 Style)); 4171 EXPECT_EQ("TESTSUITE(out) {\n" 4172 "namespace in {\n" 4173 "} // namespace in\n" 4174 "} // TESTSUITE(out)", 4175 format("TESTSUITE(out) {\n" 4176 "namespace in {\n" 4177 "} // namespace in\n" 4178 "} // TESTSUITE(out)", 4179 Style)); 4180 Style.NamespaceMacros.push_back("FOOBAR"); 4181 EXPECT_EQ("TESTSUITE(out) {\n" 4182 "FOOBAR(in) {\n" 4183 "} // FOOBAR(in)\n" 4184 "} // TESTSUITE(out)", 4185 format("TESTSUITE(out) {\n" 4186 "FOOBAR(in) {\n" 4187 "} // FOOBAR(in)\n" 4188 "} // TESTSUITE(out)", 4189 Style)); 4190 } 4191 4192 TEST_F(FormatTest, FormatsCompactNamespaces) { 4193 FormatStyle Style = getLLVMStyle(); 4194 Style.CompactNamespaces = true; 4195 Style.NamespaceMacros.push_back("TESTSUITE"); 4196 4197 verifyFormat("namespace A { namespace B {\n" 4198 "}} // namespace A::B", 4199 Style); 4200 4201 EXPECT_EQ("namespace out { namespace in {\n" 4202 "}} // namespace out::in", 4203 format("namespace out {\n" 4204 "namespace in {\n" 4205 "} // namespace in\n" 4206 "} // namespace out", 4207 Style)); 4208 4209 // Only namespaces which have both consecutive opening and end get compacted 4210 EXPECT_EQ("namespace out {\n" 4211 "namespace in1 {\n" 4212 "} // namespace in1\n" 4213 "namespace in2 {\n" 4214 "} // namespace in2\n" 4215 "} // namespace out", 4216 format("namespace out {\n" 4217 "namespace in1 {\n" 4218 "} // namespace in1\n" 4219 "namespace in2 {\n" 4220 "} // namespace in2\n" 4221 "} // namespace out", 4222 Style)); 4223 4224 EXPECT_EQ("namespace out {\n" 4225 "int i;\n" 4226 "namespace in {\n" 4227 "int j;\n" 4228 "} // namespace in\n" 4229 "int k;\n" 4230 "} // namespace out", 4231 format("namespace out { int i;\n" 4232 "namespace in { int j; } // namespace in\n" 4233 "int k; } // namespace out", 4234 Style)); 4235 4236 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 4237 "}}} // namespace A::B::C\n", 4238 format("namespace A { namespace B {\n" 4239 "namespace C {\n" 4240 "}} // namespace B::C\n" 4241 "} // namespace A\n", 4242 Style)); 4243 4244 Style.ColumnLimit = 40; 4245 EXPECT_EQ("namespace aaaaaaaaaa {\n" 4246 "namespace bbbbbbbbbb {\n" 4247 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 4248 format("namespace aaaaaaaaaa {\n" 4249 "namespace bbbbbbbbbb {\n" 4250 "} // namespace bbbbbbbbbb\n" 4251 "} // namespace aaaaaaaaaa", 4252 Style)); 4253 4254 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 4255 "namespace cccccc {\n" 4256 "}}} // namespace aaaaaa::bbbbbb::cccccc", 4257 format("namespace aaaaaa {\n" 4258 "namespace bbbbbb {\n" 4259 "namespace cccccc {\n" 4260 "} // namespace cccccc\n" 4261 "} // namespace bbbbbb\n" 4262 "} // namespace aaaaaa", 4263 Style)); 4264 Style.ColumnLimit = 80; 4265 4266 // Extra semicolon after 'inner' closing brace prevents merging 4267 EXPECT_EQ("namespace out { namespace in {\n" 4268 "}; } // namespace out::in", 4269 format("namespace out {\n" 4270 "namespace in {\n" 4271 "}; // namespace in\n" 4272 "} // namespace out", 4273 Style)); 4274 4275 // Extra semicolon after 'outer' closing brace is conserved 4276 EXPECT_EQ("namespace out { namespace in {\n" 4277 "}}; // namespace out::in", 4278 format("namespace out {\n" 4279 "namespace in {\n" 4280 "} // namespace in\n" 4281 "}; // namespace out", 4282 Style)); 4283 4284 Style.NamespaceIndentation = FormatStyle::NI_All; 4285 EXPECT_EQ("namespace out { namespace in {\n" 4286 " int i;\n" 4287 "}} // namespace out::in", 4288 format("namespace out {\n" 4289 "namespace in {\n" 4290 "int i;\n" 4291 "} // namespace in\n" 4292 "} // namespace out", 4293 Style)); 4294 EXPECT_EQ("namespace out { namespace mid {\n" 4295 " namespace in {\n" 4296 " int j;\n" 4297 " } // namespace in\n" 4298 " int k;\n" 4299 "}} // namespace out::mid", 4300 format("namespace out { namespace mid {\n" 4301 "namespace in { int j; } // namespace in\n" 4302 "int k; }} // namespace out::mid", 4303 Style)); 4304 4305 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4306 EXPECT_EQ("namespace out { namespace in {\n" 4307 " int i;\n" 4308 "}} // namespace out::in", 4309 format("namespace out {\n" 4310 "namespace in {\n" 4311 "int i;\n" 4312 "} // namespace in\n" 4313 "} // namespace out", 4314 Style)); 4315 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 4316 " int i;\n" 4317 "}}} // namespace out::mid::in", 4318 format("namespace out {\n" 4319 "namespace mid {\n" 4320 "namespace in {\n" 4321 "int i;\n" 4322 "} // namespace in\n" 4323 "} // namespace mid\n" 4324 "} // namespace out", 4325 Style)); 4326 4327 Style.CompactNamespaces = true; 4328 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 4329 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4330 Style.BraceWrapping.BeforeLambdaBody = true; 4331 verifyFormat("namespace out { namespace in {\n" 4332 "}} // namespace out::in", 4333 Style); 4334 EXPECT_EQ("namespace out { namespace in {\n" 4335 "}} // namespace out::in", 4336 format("namespace out {\n" 4337 "namespace in {\n" 4338 "} // namespace in\n" 4339 "} // namespace out", 4340 Style)); 4341 } 4342 4343 TEST_F(FormatTest, FormatsExternC) { 4344 verifyFormat("extern \"C\" {\nint a;"); 4345 verifyFormat("extern \"C\" {}"); 4346 verifyFormat("extern \"C\" {\n" 4347 "int foo();\n" 4348 "}"); 4349 verifyFormat("extern \"C\" int foo() {}"); 4350 verifyFormat("extern \"C\" int foo();"); 4351 verifyFormat("extern \"C\" int foo() {\n" 4352 " int i = 42;\n" 4353 " return i;\n" 4354 "}"); 4355 4356 FormatStyle Style = getLLVMStyle(); 4357 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4358 Style.BraceWrapping.AfterFunction = true; 4359 verifyFormat("extern \"C\" int foo() {}", Style); 4360 verifyFormat("extern \"C\" int foo();", Style); 4361 verifyFormat("extern \"C\" int foo()\n" 4362 "{\n" 4363 " int i = 42;\n" 4364 " return i;\n" 4365 "}", 4366 Style); 4367 4368 Style.BraceWrapping.AfterExternBlock = true; 4369 Style.BraceWrapping.SplitEmptyRecord = false; 4370 verifyFormat("extern \"C\"\n" 4371 "{}", 4372 Style); 4373 verifyFormat("extern \"C\"\n" 4374 "{\n" 4375 " int foo();\n" 4376 "}", 4377 Style); 4378 } 4379 4380 TEST_F(FormatTest, IndentExternBlockStyle) { 4381 FormatStyle Style = getLLVMStyle(); 4382 Style.IndentWidth = 2; 4383 4384 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4385 verifyFormat("extern \"C\" { /*9*/\n" 4386 "}", 4387 Style); 4388 verifyFormat("extern \"C\" {\n" 4389 " int foo10();\n" 4390 "}", 4391 Style); 4392 4393 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4394 verifyFormat("extern \"C\" { /*11*/\n" 4395 "}", 4396 Style); 4397 verifyFormat("extern \"C\" {\n" 4398 "int foo12();\n" 4399 "}", 4400 Style); 4401 4402 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4403 Style.BraceWrapping.AfterExternBlock = true; 4404 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4405 verifyFormat("extern \"C\"\n" 4406 "{ /*13*/\n" 4407 "}", 4408 Style); 4409 verifyFormat("extern \"C\"\n{\n" 4410 " int foo14();\n" 4411 "}", 4412 Style); 4413 4414 Style.BraceWrapping.AfterExternBlock = false; 4415 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4416 verifyFormat("extern \"C\" { /*15*/\n" 4417 "}", 4418 Style); 4419 verifyFormat("extern \"C\" {\n" 4420 "int foo16();\n" 4421 "}", 4422 Style); 4423 4424 Style.BraceWrapping.AfterExternBlock = true; 4425 verifyFormat("extern \"C\"\n" 4426 "{ /*13*/\n" 4427 "}", 4428 Style); 4429 verifyFormat("extern \"C\"\n" 4430 "{\n" 4431 "int foo14();\n" 4432 "}", 4433 Style); 4434 4435 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4436 verifyFormat("extern \"C\"\n" 4437 "{ /*13*/\n" 4438 "}", 4439 Style); 4440 verifyFormat("extern \"C\"\n" 4441 "{\n" 4442 " int foo14();\n" 4443 "}", 4444 Style); 4445 } 4446 4447 TEST_F(FormatTest, FormatsInlineASM) { 4448 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 4449 verifyFormat("asm(\"nop\" ::: \"memory\");"); 4450 verifyFormat( 4451 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 4452 " \"cpuid\\n\\t\"\n" 4453 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 4454 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 4455 " : \"a\"(value));"); 4456 EXPECT_EQ( 4457 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4458 " __asm {\n" 4459 " mov edx,[that] // vtable in edx\n" 4460 " mov eax,methodIndex\n" 4461 " call [edx][eax*4] // stdcall\n" 4462 " }\n" 4463 "}", 4464 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4465 " __asm {\n" 4466 " mov edx,[that] // vtable in edx\n" 4467 " mov eax,methodIndex\n" 4468 " call [edx][eax*4] // stdcall\n" 4469 " }\n" 4470 "}")); 4471 EXPECT_EQ("_asm {\n" 4472 " xor eax, eax;\n" 4473 " cpuid;\n" 4474 "}", 4475 format("_asm {\n" 4476 " xor eax, eax;\n" 4477 " cpuid;\n" 4478 "}")); 4479 verifyFormat("void function() {\n" 4480 " // comment\n" 4481 " asm(\"\");\n" 4482 "}"); 4483 EXPECT_EQ("__asm {\n" 4484 "}\n" 4485 "int i;", 4486 format("__asm {\n" 4487 "}\n" 4488 "int i;")); 4489 } 4490 4491 TEST_F(FormatTest, FormatTryCatch) { 4492 verifyFormat("try {\n" 4493 " throw a * b;\n" 4494 "} catch (int a) {\n" 4495 " // Do nothing.\n" 4496 "} catch (...) {\n" 4497 " exit(42);\n" 4498 "}"); 4499 4500 // Function-level try statements. 4501 verifyFormat("int f() try { return 4; } catch (...) {\n" 4502 " return 5;\n" 4503 "}"); 4504 verifyFormat("class A {\n" 4505 " int a;\n" 4506 " A() try : a(0) {\n" 4507 " } catch (...) {\n" 4508 " throw;\n" 4509 " }\n" 4510 "};\n"); 4511 verifyFormat("class A {\n" 4512 " int a;\n" 4513 " A() try : a(0), b{1} {\n" 4514 " } catch (...) {\n" 4515 " throw;\n" 4516 " }\n" 4517 "};\n"); 4518 verifyFormat("class A {\n" 4519 " int a;\n" 4520 " A() try : a(0), b{1}, c{2} {\n" 4521 " } catch (...) {\n" 4522 " throw;\n" 4523 " }\n" 4524 "};\n"); 4525 verifyFormat("class A {\n" 4526 " int a;\n" 4527 " A() try : a(0), b{1}, c{2} {\n" 4528 " { // New scope.\n" 4529 " }\n" 4530 " } catch (...) {\n" 4531 " throw;\n" 4532 " }\n" 4533 "};\n"); 4534 4535 // Incomplete try-catch blocks. 4536 verifyIncompleteFormat("try {} catch ("); 4537 } 4538 4539 TEST_F(FormatTest, FormatTryAsAVariable) { 4540 verifyFormat("int try;"); 4541 verifyFormat("int try, size;"); 4542 verifyFormat("try = foo();"); 4543 verifyFormat("if (try < size) {\n return true;\n}"); 4544 4545 verifyFormat("int catch;"); 4546 verifyFormat("int catch, size;"); 4547 verifyFormat("catch = foo();"); 4548 verifyFormat("if (catch < size) {\n return true;\n}"); 4549 4550 FormatStyle Style = getLLVMStyle(); 4551 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4552 Style.BraceWrapping.AfterFunction = true; 4553 Style.BraceWrapping.BeforeCatch = true; 4554 verifyFormat("try {\n" 4555 " int bar = 1;\n" 4556 "}\n" 4557 "catch (...) {\n" 4558 " int bar = 1;\n" 4559 "}", 4560 Style); 4561 verifyFormat("#if NO_EX\n" 4562 "try\n" 4563 "#endif\n" 4564 "{\n" 4565 "}\n" 4566 "#if NO_EX\n" 4567 "catch (...) {\n" 4568 "}", 4569 Style); 4570 verifyFormat("try /* abc */ {\n" 4571 " int bar = 1;\n" 4572 "}\n" 4573 "catch (...) {\n" 4574 " int bar = 1;\n" 4575 "}", 4576 Style); 4577 verifyFormat("try\n" 4578 "// abc\n" 4579 "{\n" 4580 " int bar = 1;\n" 4581 "}\n" 4582 "catch (...) {\n" 4583 " int bar = 1;\n" 4584 "}", 4585 Style); 4586 } 4587 4588 TEST_F(FormatTest, FormatSEHTryCatch) { 4589 verifyFormat("__try {\n" 4590 " int a = b * c;\n" 4591 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 4592 " // Do nothing.\n" 4593 "}"); 4594 4595 verifyFormat("__try {\n" 4596 " int a = b * c;\n" 4597 "} __finally {\n" 4598 " // Do nothing.\n" 4599 "}"); 4600 4601 verifyFormat("DEBUG({\n" 4602 " __try {\n" 4603 " } __finally {\n" 4604 " }\n" 4605 "});\n"); 4606 } 4607 4608 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 4609 verifyFormat("try {\n" 4610 " f();\n" 4611 "} catch {\n" 4612 " g();\n" 4613 "}"); 4614 verifyFormat("try {\n" 4615 " f();\n" 4616 "} catch (A a) MACRO(x) {\n" 4617 " g();\n" 4618 "} catch (B b) MACRO(x) {\n" 4619 " g();\n" 4620 "}"); 4621 } 4622 4623 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 4624 FormatStyle Style = getLLVMStyle(); 4625 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 4626 FormatStyle::BS_WebKit}) { 4627 Style.BreakBeforeBraces = BraceStyle; 4628 verifyFormat("try {\n" 4629 " // something\n" 4630 "} catch (...) {\n" 4631 " // something\n" 4632 "}", 4633 Style); 4634 } 4635 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4636 verifyFormat("try {\n" 4637 " // something\n" 4638 "}\n" 4639 "catch (...) {\n" 4640 " // something\n" 4641 "}", 4642 Style); 4643 verifyFormat("__try {\n" 4644 " // something\n" 4645 "}\n" 4646 "__finally {\n" 4647 " // something\n" 4648 "}", 4649 Style); 4650 verifyFormat("@try {\n" 4651 " // something\n" 4652 "}\n" 4653 "@finally {\n" 4654 " // something\n" 4655 "}", 4656 Style); 4657 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4658 verifyFormat("try\n" 4659 "{\n" 4660 " // something\n" 4661 "}\n" 4662 "catch (...)\n" 4663 "{\n" 4664 " // something\n" 4665 "}", 4666 Style); 4667 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4668 verifyFormat("try\n" 4669 " {\n" 4670 " // something white\n" 4671 " }\n" 4672 "catch (...)\n" 4673 " {\n" 4674 " // something white\n" 4675 " }", 4676 Style); 4677 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4678 verifyFormat("try\n" 4679 " {\n" 4680 " // something\n" 4681 " }\n" 4682 "catch (...)\n" 4683 " {\n" 4684 " // something\n" 4685 " }", 4686 Style); 4687 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4688 Style.BraceWrapping.BeforeCatch = true; 4689 verifyFormat("try {\n" 4690 " // something\n" 4691 "}\n" 4692 "catch (...) {\n" 4693 " // something\n" 4694 "}", 4695 Style); 4696 } 4697 4698 TEST_F(FormatTest, StaticInitializers) { 4699 verifyFormat("static SomeClass SC = {1, 'a'};"); 4700 4701 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4702 " 100000000, " 4703 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4704 4705 // Here, everything other than the "}" would fit on a line. 4706 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4707 " 10000000000000000000000000};"); 4708 EXPECT_EQ("S s = {a,\n" 4709 "\n" 4710 " b};", 4711 format("S s = {\n" 4712 " a,\n" 4713 "\n" 4714 " b\n" 4715 "};")); 4716 4717 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4718 // line. However, the formatting looks a bit off and this probably doesn't 4719 // happen often in practice. 4720 verifyFormat("static int Variable[1] = {\n" 4721 " {1000000000000000000000000000000000000}};", 4722 getLLVMStyleWithColumns(40)); 4723 } 4724 4725 TEST_F(FormatTest, DesignatedInitializers) { 4726 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4727 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4728 " .bbbbbbbbbb = 2,\n" 4729 " .cccccccccc = 3,\n" 4730 " .dddddddddd = 4,\n" 4731 " .eeeeeeeeee = 5};"); 4732 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4733 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4734 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4735 " .ccccccccccccccccccccccccccc = 3,\n" 4736 " .ddddddddddddddddddddddddddd = 4,\n" 4737 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4738 4739 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4740 4741 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4742 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4743 " [2] = bbbbbbbbbb,\n" 4744 " [3] = cccccccccc,\n" 4745 " [4] = dddddddddd,\n" 4746 " [5] = eeeeeeeeee};"); 4747 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4748 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4749 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4750 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4751 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4752 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4753 } 4754 4755 TEST_F(FormatTest, NestedStaticInitializers) { 4756 verifyFormat("static A x = {{{}}};\n"); 4757 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4758 " {init1, init2, init3, init4}}};", 4759 getLLVMStyleWithColumns(50)); 4760 4761 verifyFormat("somes Status::global_reps[3] = {\n" 4762 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4763 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4764 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4765 getLLVMStyleWithColumns(60)); 4766 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4767 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4768 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4769 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4770 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4771 " {rect.fRight - rect.fLeft, rect.fBottom - " 4772 "rect.fTop}};"); 4773 4774 verifyFormat( 4775 "SomeArrayOfSomeType a = {\n" 4776 " {{1, 2, 3},\n" 4777 " {1, 2, 3},\n" 4778 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4779 " 333333333333333333333333333333},\n" 4780 " {1, 2, 3},\n" 4781 " {1, 2, 3}}};"); 4782 verifyFormat( 4783 "SomeArrayOfSomeType a = {\n" 4784 " {{1, 2, 3}},\n" 4785 " {{1, 2, 3}},\n" 4786 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4787 " 333333333333333333333333333333}},\n" 4788 " {{1, 2, 3}},\n" 4789 " {{1, 2, 3}}};"); 4790 4791 verifyFormat("struct {\n" 4792 " unsigned bit;\n" 4793 " const char *const name;\n" 4794 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4795 " {kOsWin, \"Windows\"},\n" 4796 " {kOsLinux, \"Linux\"},\n" 4797 " {kOsCrOS, \"Chrome OS\"}};"); 4798 verifyFormat("struct {\n" 4799 " unsigned bit;\n" 4800 " const char *const name;\n" 4801 "} kBitsToOs[] = {\n" 4802 " {kOsMac, \"Mac\"},\n" 4803 " {kOsWin, \"Windows\"},\n" 4804 " {kOsLinux, \"Linux\"},\n" 4805 " {kOsCrOS, \"Chrome OS\"},\n" 4806 "};"); 4807 } 4808 4809 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4810 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4811 " \\\n" 4812 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4813 } 4814 4815 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4816 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4817 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4818 4819 // Do break defaulted and deleted functions. 4820 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4821 " default;", 4822 getLLVMStyleWithColumns(40)); 4823 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4824 " delete;", 4825 getLLVMStyleWithColumns(40)); 4826 } 4827 4828 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4829 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4830 getLLVMStyleWithColumns(40)); 4831 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4832 getLLVMStyleWithColumns(40)); 4833 EXPECT_EQ("#define Q \\\n" 4834 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4835 " \"aaaaaaaa.cpp\"", 4836 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4837 getLLVMStyleWithColumns(40))); 4838 } 4839 4840 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4841 EXPECT_EQ("# 123 \"A string literal\"", 4842 format(" # 123 \"A string literal\"")); 4843 } 4844 4845 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4846 EXPECT_EQ("#;", format("#;")); 4847 verifyFormat("#\n;\n;\n;"); 4848 } 4849 4850 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4851 EXPECT_EQ("#line 42 \"test\"\n", 4852 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4853 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4854 getLLVMStyleWithColumns(12))); 4855 } 4856 4857 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4858 EXPECT_EQ("#line 42 \"test\"", 4859 format("# \\\n line \\\n 42 \\\n \"test\"")); 4860 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4861 } 4862 4863 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4864 verifyFormat("#define A \\x20"); 4865 verifyFormat("#define A \\ x20"); 4866 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4867 verifyFormat("#define A ''"); 4868 verifyFormat("#define A ''qqq"); 4869 verifyFormat("#define A `qqq"); 4870 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4871 EXPECT_EQ("const char *c = STRINGIFY(\n" 4872 "\\na : b);", 4873 format("const char * c = STRINGIFY(\n" 4874 "\\na : b);")); 4875 4876 verifyFormat("a\r\\"); 4877 verifyFormat("a\v\\"); 4878 verifyFormat("a\f\\"); 4879 } 4880 4881 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4882 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4883 style.IndentWidth = 4; 4884 style.PPIndentWidth = 1; 4885 4886 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4887 verifyFormat("#ifdef __linux__\n" 4888 "void foo() {\n" 4889 " int x = 0;\n" 4890 "}\n" 4891 "#define FOO\n" 4892 "#endif\n" 4893 "void bar() {\n" 4894 " int y = 0;\n" 4895 "}\n", 4896 style); 4897 4898 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4899 verifyFormat("#ifdef __linux__\n" 4900 "void foo() {\n" 4901 " int x = 0;\n" 4902 "}\n" 4903 "# define FOO foo\n" 4904 "#endif\n" 4905 "void bar() {\n" 4906 " int y = 0;\n" 4907 "}\n", 4908 style); 4909 4910 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4911 verifyFormat("#ifdef __linux__\n" 4912 "void foo() {\n" 4913 " int x = 0;\n" 4914 "}\n" 4915 " #define FOO foo\n" 4916 "#endif\n" 4917 "void bar() {\n" 4918 " int y = 0;\n" 4919 "}\n", 4920 style); 4921 } 4922 4923 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4924 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4925 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4926 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4927 // FIXME: We never break before the macro name. 4928 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4929 4930 verifyFormat("#define A A\n#define A A"); 4931 verifyFormat("#define A(X) A\n#define A A"); 4932 4933 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4934 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4935 } 4936 4937 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4938 EXPECT_EQ("// somecomment\n" 4939 "#include \"a.h\"\n" 4940 "#define A( \\\n" 4941 " A, B)\n" 4942 "#include \"b.h\"\n" 4943 "// somecomment\n", 4944 format(" // somecomment\n" 4945 " #include \"a.h\"\n" 4946 "#define A(A,\\\n" 4947 " B)\n" 4948 " #include \"b.h\"\n" 4949 " // somecomment\n", 4950 getLLVMStyleWithColumns(13))); 4951 } 4952 4953 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4954 4955 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4956 EXPECT_EQ("#define A \\\n" 4957 " c; \\\n" 4958 " e;\n" 4959 "f;", 4960 format("#define A c; e;\n" 4961 "f;", 4962 getLLVMStyleWithColumns(14))); 4963 } 4964 4965 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4966 4967 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4968 EXPECT_EQ("int x,\n" 4969 "#define A\n" 4970 " y;", 4971 format("int x,\n#define A\ny;")); 4972 } 4973 4974 TEST_F(FormatTest, HashInMacroDefinition) { 4975 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4976 EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle())); 4977 EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle())); 4978 EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle())); 4979 EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle())); 4980 EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle())); 4981 EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle())); 4982 EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle())); 4983 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4984 verifyFormat("#define A \\\n" 4985 " { \\\n" 4986 " f(#c); \\\n" 4987 " }", 4988 getLLVMStyleWithColumns(11)); 4989 4990 verifyFormat("#define A(X) \\\n" 4991 " void function##X()", 4992 getLLVMStyleWithColumns(22)); 4993 4994 verifyFormat("#define A(a, b, c) \\\n" 4995 " void a##b##c()", 4996 getLLVMStyleWithColumns(22)); 4997 4998 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4999 } 5000 5001 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 5002 EXPECT_EQ("#define A (x)", format("#define A (x)")); 5003 EXPECT_EQ("#define A(x)", format("#define A(x)")); 5004 5005 FormatStyle Style = getLLVMStyle(); 5006 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 5007 verifyFormat("#define true ((foo)1)", Style); 5008 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 5009 verifyFormat("#define false((foo)0)", Style); 5010 } 5011 5012 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 5013 EXPECT_EQ("#define A b;", format("#define A \\\n" 5014 " \\\n" 5015 " b;", 5016 getLLVMStyleWithColumns(25))); 5017 EXPECT_EQ("#define A \\\n" 5018 " \\\n" 5019 " a; \\\n" 5020 " b;", 5021 format("#define A \\\n" 5022 " \\\n" 5023 " a; \\\n" 5024 " b;", 5025 getLLVMStyleWithColumns(11))); 5026 EXPECT_EQ("#define A \\\n" 5027 " a; \\\n" 5028 " \\\n" 5029 " b;", 5030 format("#define A \\\n" 5031 " a; \\\n" 5032 " \\\n" 5033 " b;", 5034 getLLVMStyleWithColumns(11))); 5035 } 5036 5037 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 5038 verifyIncompleteFormat("#define A :"); 5039 verifyFormat("#define SOMECASES \\\n" 5040 " case 1: \\\n" 5041 " case 2\n", 5042 getLLVMStyleWithColumns(20)); 5043 verifyFormat("#define MACRO(a) \\\n" 5044 " if (a) \\\n" 5045 " f(); \\\n" 5046 " else \\\n" 5047 " g()", 5048 getLLVMStyleWithColumns(18)); 5049 verifyFormat("#define A template <typename T>"); 5050 verifyIncompleteFormat("#define STR(x) #x\n" 5051 "f(STR(this_is_a_string_literal{));"); 5052 verifyFormat("#pragma omp threadprivate( \\\n" 5053 " y)), // expected-warning", 5054 getLLVMStyleWithColumns(28)); 5055 verifyFormat("#d, = };"); 5056 verifyFormat("#if \"a"); 5057 verifyIncompleteFormat("({\n" 5058 "#define b \\\n" 5059 " } \\\n" 5060 " a\n" 5061 "a", 5062 getLLVMStyleWithColumns(15)); 5063 verifyFormat("#define A \\\n" 5064 " { \\\n" 5065 " {\n" 5066 "#define B \\\n" 5067 " } \\\n" 5068 " }", 5069 getLLVMStyleWithColumns(15)); 5070 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 5071 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 5072 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 5073 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 5074 } 5075 5076 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 5077 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 5078 EXPECT_EQ("class A : public QObject {\n" 5079 " Q_OBJECT\n" 5080 "\n" 5081 " A() {}\n" 5082 "};", 5083 format("class A : public QObject {\n" 5084 " Q_OBJECT\n" 5085 "\n" 5086 " A() {\n}\n" 5087 "} ;")); 5088 EXPECT_EQ("MACRO\n" 5089 "/*static*/ int i;", 5090 format("MACRO\n" 5091 " /*static*/ int i;")); 5092 EXPECT_EQ("SOME_MACRO\n" 5093 "namespace {\n" 5094 "void f();\n" 5095 "} // namespace", 5096 format("SOME_MACRO\n" 5097 " namespace {\n" 5098 "void f( );\n" 5099 "} // namespace")); 5100 // Only if the identifier contains at least 5 characters. 5101 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 5102 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 5103 // Only if everything is upper case. 5104 EXPECT_EQ("class A : public QObject {\n" 5105 " Q_Object A() {}\n" 5106 "};", 5107 format("class A : public QObject {\n" 5108 " Q_Object\n" 5109 " A() {\n}\n" 5110 "} ;")); 5111 5112 // Only if the next line can actually start an unwrapped line. 5113 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 5114 format("SOME_WEIRD_LOG_MACRO\n" 5115 "<< SomeThing;")); 5116 5117 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 5118 "(n, buffers))\n", 5119 getChromiumStyle(FormatStyle::LK_Cpp)); 5120 5121 // See PR41483 5122 EXPECT_EQ("/**/ FOO(a)\n" 5123 "FOO(b)", 5124 format("/**/ FOO(a)\n" 5125 "FOO(b)")); 5126 } 5127 5128 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 5129 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 5130 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 5131 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 5132 "class X {};\n" 5133 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 5134 "int *createScopDetectionPass() { return 0; }", 5135 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 5136 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 5137 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 5138 " class X {};\n" 5139 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 5140 " int *createScopDetectionPass() { return 0; }")); 5141 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 5142 // braces, so that inner block is indented one level more. 5143 EXPECT_EQ("int q() {\n" 5144 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 5145 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 5146 " IPC_END_MESSAGE_MAP()\n" 5147 "}", 5148 format("int q() {\n" 5149 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 5150 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 5151 " IPC_END_MESSAGE_MAP()\n" 5152 "}")); 5153 5154 // Same inside macros. 5155 EXPECT_EQ("#define LIST(L) \\\n" 5156 " L(A) \\\n" 5157 " L(B) \\\n" 5158 " L(C)", 5159 format("#define LIST(L) \\\n" 5160 " L(A) \\\n" 5161 " L(B) \\\n" 5162 " L(C)", 5163 getGoogleStyle())); 5164 5165 // These must not be recognized as macros. 5166 EXPECT_EQ("int q() {\n" 5167 " f(x);\n" 5168 " f(x) {}\n" 5169 " f(x)->g();\n" 5170 " f(x)->*g();\n" 5171 " f(x).g();\n" 5172 " f(x) = x;\n" 5173 " f(x) += x;\n" 5174 " f(x) -= x;\n" 5175 " f(x) *= x;\n" 5176 " f(x) /= x;\n" 5177 " f(x) %= x;\n" 5178 " f(x) &= x;\n" 5179 " f(x) |= x;\n" 5180 " f(x) ^= x;\n" 5181 " f(x) >>= x;\n" 5182 " f(x) <<= x;\n" 5183 " f(x)[y].z();\n" 5184 " LOG(INFO) << x;\n" 5185 " ifstream(x) >> x;\n" 5186 "}\n", 5187 format("int q() {\n" 5188 " f(x)\n;\n" 5189 " f(x)\n {}\n" 5190 " f(x)\n->g();\n" 5191 " f(x)\n->*g();\n" 5192 " f(x)\n.g();\n" 5193 " f(x)\n = x;\n" 5194 " f(x)\n += x;\n" 5195 " f(x)\n -= x;\n" 5196 " f(x)\n *= x;\n" 5197 " f(x)\n /= x;\n" 5198 " f(x)\n %= x;\n" 5199 " f(x)\n &= x;\n" 5200 " f(x)\n |= x;\n" 5201 " f(x)\n ^= x;\n" 5202 " f(x)\n >>= x;\n" 5203 " f(x)\n <<= x;\n" 5204 " f(x)\n[y].z();\n" 5205 " LOG(INFO)\n << x;\n" 5206 " ifstream(x)\n >> x;\n" 5207 "}\n")); 5208 EXPECT_EQ("int q() {\n" 5209 " F(x)\n" 5210 " if (1) {\n" 5211 " }\n" 5212 " F(x)\n" 5213 " while (1) {\n" 5214 " }\n" 5215 " F(x)\n" 5216 " G(x);\n" 5217 " F(x)\n" 5218 " try {\n" 5219 " Q();\n" 5220 " } catch (...) {\n" 5221 " }\n" 5222 "}\n", 5223 format("int q() {\n" 5224 "F(x)\n" 5225 "if (1) {}\n" 5226 "F(x)\n" 5227 "while (1) {}\n" 5228 "F(x)\n" 5229 "G(x);\n" 5230 "F(x)\n" 5231 "try { Q(); } catch (...) {}\n" 5232 "}\n")); 5233 EXPECT_EQ("class A {\n" 5234 " A() : t(0) {}\n" 5235 " A(int i) noexcept() : {}\n" 5236 " A(X x)\n" // FIXME: function-level try blocks are broken. 5237 " try : t(0) {\n" 5238 " } catch (...) {\n" 5239 " }\n" 5240 "};", 5241 format("class A {\n" 5242 " A()\n : t(0) {}\n" 5243 " A(int i)\n noexcept() : {}\n" 5244 " A(X x)\n" 5245 " try : t(0) {} catch (...) {}\n" 5246 "};")); 5247 FormatStyle Style = getLLVMStyle(); 5248 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5249 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5250 Style.BraceWrapping.AfterFunction = true; 5251 EXPECT_EQ("void f()\n" 5252 "try\n" 5253 "{\n" 5254 "}", 5255 format("void f() try {\n" 5256 "}", 5257 Style)); 5258 EXPECT_EQ("class SomeClass {\n" 5259 "public:\n" 5260 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5261 "};", 5262 format("class SomeClass {\n" 5263 "public:\n" 5264 " SomeClass()\n" 5265 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5266 "};")); 5267 EXPECT_EQ("class SomeClass {\n" 5268 "public:\n" 5269 " SomeClass()\n" 5270 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5271 "};", 5272 format("class SomeClass {\n" 5273 "public:\n" 5274 " SomeClass()\n" 5275 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5276 "};", 5277 getLLVMStyleWithColumns(40))); 5278 5279 verifyFormat("MACRO(>)"); 5280 5281 // Some macros contain an implicit semicolon. 5282 Style = getLLVMStyle(); 5283 Style.StatementMacros.push_back("FOO"); 5284 verifyFormat("FOO(a) int b = 0;"); 5285 verifyFormat("FOO(a)\n" 5286 "int b = 0;", 5287 Style); 5288 verifyFormat("FOO(a);\n" 5289 "int b = 0;", 5290 Style); 5291 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 5292 "int b = 0;", 5293 Style); 5294 verifyFormat("FOO()\n" 5295 "int b = 0;", 5296 Style); 5297 verifyFormat("FOO\n" 5298 "int b = 0;", 5299 Style); 5300 verifyFormat("void f() {\n" 5301 " FOO(a)\n" 5302 " return a;\n" 5303 "}", 5304 Style); 5305 verifyFormat("FOO(a)\n" 5306 "FOO(b)", 5307 Style); 5308 verifyFormat("int a = 0;\n" 5309 "FOO(b)\n" 5310 "int c = 0;", 5311 Style); 5312 verifyFormat("int a = 0;\n" 5313 "int x = FOO(a)\n" 5314 "int b = 0;", 5315 Style); 5316 verifyFormat("void foo(int a) { FOO(a) }\n" 5317 "uint32_t bar() {}", 5318 Style); 5319 } 5320 5321 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) { 5322 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 5323 5324 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()", 5325 ZeroColumn); 5326 } 5327 5328 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 5329 verifyFormat("#define A \\\n" 5330 " f({ \\\n" 5331 " g(); \\\n" 5332 " });", 5333 getLLVMStyleWithColumns(11)); 5334 } 5335 5336 TEST_F(FormatTest, IndentPreprocessorDirectives) { 5337 FormatStyle Style = getLLVMStyleWithColumns(40); 5338 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 5339 verifyFormat("#ifdef _WIN32\n" 5340 "#define A 0\n" 5341 "#ifdef VAR2\n" 5342 "#define B 1\n" 5343 "#include <someheader.h>\n" 5344 "#define MACRO \\\n" 5345 " some_very_long_func_aaaaaaaaaa();\n" 5346 "#endif\n" 5347 "#else\n" 5348 "#define A 1\n" 5349 "#endif", 5350 Style); 5351 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 5352 verifyFormat("#ifdef _WIN32\n" 5353 "# define A 0\n" 5354 "# ifdef VAR2\n" 5355 "# define B 1\n" 5356 "# include <someheader.h>\n" 5357 "# define MACRO \\\n" 5358 " some_very_long_func_aaaaaaaaaa();\n" 5359 "# endif\n" 5360 "#else\n" 5361 "# define A 1\n" 5362 "#endif", 5363 Style); 5364 verifyFormat("#if A\n" 5365 "# define MACRO \\\n" 5366 " void a(int x) { \\\n" 5367 " b(); \\\n" 5368 " c(); \\\n" 5369 " d(); \\\n" 5370 " e(); \\\n" 5371 " f(); \\\n" 5372 " }\n" 5373 "#endif", 5374 Style); 5375 // Comments before include guard. 5376 verifyFormat("// file comment\n" 5377 "// file comment\n" 5378 "#ifndef HEADER_H\n" 5379 "#define HEADER_H\n" 5380 "code();\n" 5381 "#endif", 5382 Style); 5383 // Test with include guards. 5384 verifyFormat("#ifndef HEADER_H\n" 5385 "#define HEADER_H\n" 5386 "code();\n" 5387 "#endif", 5388 Style); 5389 // Include guards must have a #define with the same variable immediately 5390 // after #ifndef. 5391 verifyFormat("#ifndef NOT_GUARD\n" 5392 "# define FOO\n" 5393 "code();\n" 5394 "#endif", 5395 Style); 5396 5397 // Include guards must cover the entire file. 5398 verifyFormat("code();\n" 5399 "code();\n" 5400 "#ifndef NOT_GUARD\n" 5401 "# define NOT_GUARD\n" 5402 "code();\n" 5403 "#endif", 5404 Style); 5405 verifyFormat("#ifndef NOT_GUARD\n" 5406 "# define NOT_GUARD\n" 5407 "code();\n" 5408 "#endif\n" 5409 "code();", 5410 Style); 5411 // Test with trailing blank lines. 5412 verifyFormat("#ifndef HEADER_H\n" 5413 "#define HEADER_H\n" 5414 "code();\n" 5415 "#endif\n", 5416 Style); 5417 // Include guards don't have #else. 5418 verifyFormat("#ifndef NOT_GUARD\n" 5419 "# define NOT_GUARD\n" 5420 "code();\n" 5421 "#else\n" 5422 "#endif", 5423 Style); 5424 verifyFormat("#ifndef NOT_GUARD\n" 5425 "# define NOT_GUARD\n" 5426 "code();\n" 5427 "#elif FOO\n" 5428 "#endif", 5429 Style); 5430 // Non-identifier #define after potential include guard. 5431 verifyFormat("#ifndef FOO\n" 5432 "# define 1\n" 5433 "#endif\n", 5434 Style); 5435 // #if closes past last non-preprocessor line. 5436 verifyFormat("#ifndef FOO\n" 5437 "#define FOO\n" 5438 "#if 1\n" 5439 "int i;\n" 5440 "# define A 0\n" 5441 "#endif\n" 5442 "#endif\n", 5443 Style); 5444 // Don't crash if there is an #elif directive without a condition. 5445 verifyFormat("#if 1\n" 5446 "int x;\n" 5447 "#elif\n" 5448 "int y;\n" 5449 "#else\n" 5450 "int z;\n" 5451 "#endif", 5452 Style); 5453 // FIXME: This doesn't handle the case where there's code between the 5454 // #ifndef and #define but all other conditions hold. This is because when 5455 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 5456 // previous code line yet, so we can't detect it. 5457 EXPECT_EQ("#ifndef NOT_GUARD\n" 5458 "code();\n" 5459 "#define NOT_GUARD\n" 5460 "code();\n" 5461 "#endif", 5462 format("#ifndef NOT_GUARD\n" 5463 "code();\n" 5464 "# define NOT_GUARD\n" 5465 "code();\n" 5466 "#endif", 5467 Style)); 5468 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 5469 // be outside an include guard. Examples are #pragma once and 5470 // #pragma GCC diagnostic, or anything else that does not change the meaning 5471 // of the file if it's included multiple times. 5472 EXPECT_EQ("#ifdef WIN32\n" 5473 "# pragma once\n" 5474 "#endif\n" 5475 "#ifndef HEADER_H\n" 5476 "# define HEADER_H\n" 5477 "code();\n" 5478 "#endif", 5479 format("#ifdef WIN32\n" 5480 "# pragma once\n" 5481 "#endif\n" 5482 "#ifndef HEADER_H\n" 5483 "#define HEADER_H\n" 5484 "code();\n" 5485 "#endif", 5486 Style)); 5487 // FIXME: This does not detect when there is a single non-preprocessor line 5488 // in front of an include-guard-like structure where other conditions hold 5489 // because ScopedLineState hides the line. 5490 EXPECT_EQ("code();\n" 5491 "#ifndef HEADER_H\n" 5492 "#define HEADER_H\n" 5493 "code();\n" 5494 "#endif", 5495 format("code();\n" 5496 "#ifndef HEADER_H\n" 5497 "# define HEADER_H\n" 5498 "code();\n" 5499 "#endif", 5500 Style)); 5501 // Keep comments aligned with #, otherwise indent comments normally. These 5502 // tests cannot use verifyFormat because messUp manipulates leading 5503 // whitespace. 5504 { 5505 const char *Expected = "" 5506 "void f() {\n" 5507 "#if 1\n" 5508 "// Preprocessor aligned.\n" 5509 "# define A 0\n" 5510 " // Code. Separated by blank line.\n" 5511 "\n" 5512 "# define B 0\n" 5513 " // Code. Not aligned with #\n" 5514 "# define C 0\n" 5515 "#endif"; 5516 const char *ToFormat = "" 5517 "void f() {\n" 5518 "#if 1\n" 5519 "// Preprocessor aligned.\n" 5520 "# define A 0\n" 5521 "// Code. Separated by blank line.\n" 5522 "\n" 5523 "# define B 0\n" 5524 " // Code. Not aligned with #\n" 5525 "# define C 0\n" 5526 "#endif"; 5527 EXPECT_EQ(Expected, format(ToFormat, Style)); 5528 EXPECT_EQ(Expected, format(Expected, Style)); 5529 } 5530 // Keep block quotes aligned. 5531 { 5532 const char *Expected = "" 5533 "void f() {\n" 5534 "#if 1\n" 5535 "/* Preprocessor aligned. */\n" 5536 "# define A 0\n" 5537 " /* Code. Separated by blank line. */\n" 5538 "\n" 5539 "# define B 0\n" 5540 " /* Code. Not aligned with # */\n" 5541 "# define C 0\n" 5542 "#endif"; 5543 const char *ToFormat = "" 5544 "void f() {\n" 5545 "#if 1\n" 5546 "/* Preprocessor aligned. */\n" 5547 "# define A 0\n" 5548 "/* Code. Separated by blank line. */\n" 5549 "\n" 5550 "# define B 0\n" 5551 " /* Code. Not aligned with # */\n" 5552 "# define C 0\n" 5553 "#endif"; 5554 EXPECT_EQ(Expected, format(ToFormat, Style)); 5555 EXPECT_EQ(Expected, format(Expected, Style)); 5556 } 5557 // Keep comments aligned with un-indented directives. 5558 { 5559 const char *Expected = "" 5560 "void f() {\n" 5561 "// Preprocessor aligned.\n" 5562 "#define A 0\n" 5563 " // Code. Separated by blank line.\n" 5564 "\n" 5565 "#define B 0\n" 5566 " // Code. Not aligned with #\n" 5567 "#define C 0\n"; 5568 const char *ToFormat = "" 5569 "void f() {\n" 5570 "// Preprocessor aligned.\n" 5571 "#define A 0\n" 5572 "// Code. Separated by blank line.\n" 5573 "\n" 5574 "#define B 0\n" 5575 " // Code. Not aligned with #\n" 5576 "#define C 0\n"; 5577 EXPECT_EQ(Expected, format(ToFormat, Style)); 5578 EXPECT_EQ(Expected, format(Expected, Style)); 5579 } 5580 // Test AfterHash with tabs. 5581 { 5582 FormatStyle Tabbed = Style; 5583 Tabbed.UseTab = FormatStyle::UT_Always; 5584 Tabbed.IndentWidth = 8; 5585 Tabbed.TabWidth = 8; 5586 verifyFormat("#ifdef _WIN32\n" 5587 "#\tdefine A 0\n" 5588 "#\tifdef VAR2\n" 5589 "#\t\tdefine B 1\n" 5590 "#\t\tinclude <someheader.h>\n" 5591 "#\t\tdefine MACRO \\\n" 5592 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 5593 "#\tendif\n" 5594 "#else\n" 5595 "#\tdefine A 1\n" 5596 "#endif", 5597 Tabbed); 5598 } 5599 5600 // Regression test: Multiline-macro inside include guards. 5601 verifyFormat("#ifndef HEADER_H\n" 5602 "#define HEADER_H\n" 5603 "#define A() \\\n" 5604 " int i; \\\n" 5605 " int j;\n" 5606 "#endif // HEADER_H", 5607 getLLVMStyleWithColumns(20)); 5608 5609 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5610 // Basic before hash indent tests 5611 verifyFormat("#ifdef _WIN32\n" 5612 " #define A 0\n" 5613 " #ifdef VAR2\n" 5614 " #define B 1\n" 5615 " #include <someheader.h>\n" 5616 " #define MACRO \\\n" 5617 " some_very_long_func_aaaaaaaaaa();\n" 5618 " #endif\n" 5619 "#else\n" 5620 " #define A 1\n" 5621 "#endif", 5622 Style); 5623 verifyFormat("#if A\n" 5624 " #define MACRO \\\n" 5625 " void a(int x) { \\\n" 5626 " b(); \\\n" 5627 " c(); \\\n" 5628 " d(); \\\n" 5629 " e(); \\\n" 5630 " f(); \\\n" 5631 " }\n" 5632 "#endif", 5633 Style); 5634 // Keep comments aligned with indented directives. These 5635 // tests cannot use verifyFormat because messUp manipulates leading 5636 // whitespace. 5637 { 5638 const char *Expected = "void f() {\n" 5639 "// Aligned to preprocessor.\n" 5640 "#if 1\n" 5641 " // Aligned to code.\n" 5642 " int a;\n" 5643 " #if 1\n" 5644 " // Aligned to preprocessor.\n" 5645 " #define A 0\n" 5646 " // Aligned to code.\n" 5647 " int b;\n" 5648 " #endif\n" 5649 "#endif\n" 5650 "}"; 5651 const char *ToFormat = "void f() {\n" 5652 "// Aligned to preprocessor.\n" 5653 "#if 1\n" 5654 "// Aligned to code.\n" 5655 "int a;\n" 5656 "#if 1\n" 5657 "// Aligned to preprocessor.\n" 5658 "#define A 0\n" 5659 "// Aligned to code.\n" 5660 "int b;\n" 5661 "#endif\n" 5662 "#endif\n" 5663 "}"; 5664 EXPECT_EQ(Expected, format(ToFormat, Style)); 5665 EXPECT_EQ(Expected, format(Expected, Style)); 5666 } 5667 { 5668 const char *Expected = "void f() {\n" 5669 "/* Aligned to preprocessor. */\n" 5670 "#if 1\n" 5671 " /* Aligned to code. */\n" 5672 " int a;\n" 5673 " #if 1\n" 5674 " /* Aligned to preprocessor. */\n" 5675 " #define A 0\n" 5676 " /* Aligned to code. */\n" 5677 " int b;\n" 5678 " #endif\n" 5679 "#endif\n" 5680 "}"; 5681 const char *ToFormat = "void f() {\n" 5682 "/* Aligned to preprocessor. */\n" 5683 "#if 1\n" 5684 "/* Aligned to code. */\n" 5685 "int a;\n" 5686 "#if 1\n" 5687 "/* Aligned to preprocessor. */\n" 5688 "#define A 0\n" 5689 "/* Aligned to code. */\n" 5690 "int b;\n" 5691 "#endif\n" 5692 "#endif\n" 5693 "}"; 5694 EXPECT_EQ(Expected, format(ToFormat, Style)); 5695 EXPECT_EQ(Expected, format(Expected, Style)); 5696 } 5697 5698 // Test single comment before preprocessor 5699 verifyFormat("// Comment\n" 5700 "\n" 5701 "#if 1\n" 5702 "#endif", 5703 Style); 5704 } 5705 5706 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5707 verifyFormat("{\n { a #c; }\n}"); 5708 } 5709 5710 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5711 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5712 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5713 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5714 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5715 } 5716 5717 TEST_F(FormatTest, EscapedNewlines) { 5718 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5719 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5720 format("#define A \\\nint i;\\\n int j;", Narrow)); 5721 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5722 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5723 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5724 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5725 5726 FormatStyle AlignLeft = getLLVMStyle(); 5727 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5728 EXPECT_EQ("#define MACRO(x) \\\n" 5729 "private: \\\n" 5730 " int x(int a);\n", 5731 format("#define MACRO(x) \\\n" 5732 "private: \\\n" 5733 " int x(int a);\n", 5734 AlignLeft)); 5735 5736 // CRLF line endings 5737 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5738 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5739 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5740 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5741 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5742 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5743 EXPECT_EQ("#define MACRO(x) \\\r\n" 5744 "private: \\\r\n" 5745 " int x(int a);\r\n", 5746 format("#define MACRO(x) \\\r\n" 5747 "private: \\\r\n" 5748 " int x(int a);\r\n", 5749 AlignLeft)); 5750 5751 FormatStyle DontAlign = getLLVMStyle(); 5752 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5753 DontAlign.MaxEmptyLinesToKeep = 3; 5754 // FIXME: can't use verifyFormat here because the newline before 5755 // "public:" is not inserted the first time it's reformatted 5756 EXPECT_EQ("#define A \\\n" 5757 " class Foo { \\\n" 5758 " void bar(); \\\n" 5759 "\\\n" 5760 "\\\n" 5761 "\\\n" 5762 " public: \\\n" 5763 " void baz(); \\\n" 5764 " };", 5765 format("#define A \\\n" 5766 " class Foo { \\\n" 5767 " void bar(); \\\n" 5768 "\\\n" 5769 "\\\n" 5770 "\\\n" 5771 " public: \\\n" 5772 " void baz(); \\\n" 5773 " };", 5774 DontAlign)); 5775 } 5776 5777 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5778 verifyFormat("#define A \\\n" 5779 " int v( \\\n" 5780 " a); \\\n" 5781 " int i;", 5782 getLLVMStyleWithColumns(11)); 5783 } 5784 5785 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5786 EXPECT_EQ( 5787 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5788 " \\\n" 5789 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5790 "\n" 5791 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5792 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5793 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5794 "\\\n" 5795 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5796 " \n" 5797 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5798 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5799 } 5800 5801 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5802 EXPECT_EQ("int\n" 5803 "#define A\n" 5804 " a;", 5805 format("int\n#define A\na;")); 5806 verifyFormat("functionCallTo(\n" 5807 " someOtherFunction(\n" 5808 " withSomeParameters, whichInSequence,\n" 5809 " areLongerThanALine(andAnotherCall,\n" 5810 "#define A B\n" 5811 " withMoreParamters,\n" 5812 " whichStronglyInfluenceTheLayout),\n" 5813 " andMoreParameters),\n" 5814 " trailing);", 5815 getLLVMStyleWithColumns(69)); 5816 verifyFormat("Foo::Foo()\n" 5817 "#ifdef BAR\n" 5818 " : baz(0)\n" 5819 "#endif\n" 5820 "{\n" 5821 "}"); 5822 verifyFormat("void f() {\n" 5823 " if (true)\n" 5824 "#ifdef A\n" 5825 " f(42);\n" 5826 " x();\n" 5827 "#else\n" 5828 " g();\n" 5829 " x();\n" 5830 "#endif\n" 5831 "}"); 5832 verifyFormat("void f(param1, param2,\n" 5833 " param3,\n" 5834 "#ifdef A\n" 5835 " param4(param5,\n" 5836 "#ifdef A1\n" 5837 " param6,\n" 5838 "#ifdef A2\n" 5839 " param7),\n" 5840 "#else\n" 5841 " param8),\n" 5842 " param9,\n" 5843 "#endif\n" 5844 " param10,\n" 5845 "#endif\n" 5846 " param11)\n" 5847 "#else\n" 5848 " param12)\n" 5849 "#endif\n" 5850 "{\n" 5851 " x();\n" 5852 "}", 5853 getLLVMStyleWithColumns(28)); 5854 verifyFormat("#if 1\n" 5855 "int i;"); 5856 verifyFormat("#if 1\n" 5857 "#endif\n" 5858 "#if 1\n" 5859 "#else\n" 5860 "#endif\n"); 5861 verifyFormat("DEBUG({\n" 5862 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5864 "});\n" 5865 "#if a\n" 5866 "#else\n" 5867 "#endif"); 5868 5869 verifyIncompleteFormat("void f(\n" 5870 "#if A\n" 5871 ");\n" 5872 "#else\n" 5873 "#endif"); 5874 } 5875 5876 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5877 verifyFormat("#endif\n" 5878 "#if B"); 5879 } 5880 5881 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5882 FormatStyle SingleLine = getLLVMStyle(); 5883 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5884 verifyFormat("#if 0\n" 5885 "#elif 1\n" 5886 "#endif\n" 5887 "void foo() {\n" 5888 " if (test) foo2();\n" 5889 "}", 5890 SingleLine); 5891 } 5892 5893 TEST_F(FormatTest, LayoutBlockInsideParens) { 5894 verifyFormat("functionCall({ int i; });"); 5895 verifyFormat("functionCall({\n" 5896 " int i;\n" 5897 " int j;\n" 5898 "});"); 5899 verifyFormat("functionCall(\n" 5900 " {\n" 5901 " int i;\n" 5902 " int j;\n" 5903 " },\n" 5904 " aaaa, bbbb, cccc);"); 5905 verifyFormat("functionA(functionB({\n" 5906 " int i;\n" 5907 " int j;\n" 5908 " }),\n" 5909 " aaaa, bbbb, cccc);"); 5910 verifyFormat("functionCall(\n" 5911 " {\n" 5912 " int i;\n" 5913 " int j;\n" 5914 " },\n" 5915 " aaaa, bbbb, // comment\n" 5916 " cccc);"); 5917 verifyFormat("functionA(functionB({\n" 5918 " int i;\n" 5919 " int j;\n" 5920 " }),\n" 5921 " aaaa, bbbb, // comment\n" 5922 " cccc);"); 5923 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5924 verifyFormat("functionCall(aaaa, bbbb, {\n" 5925 " int i;\n" 5926 " int j;\n" 5927 "});"); 5928 verifyFormat( 5929 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5930 " {\n" 5931 " int i; // break\n" 5932 " },\n" 5933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5934 " ccccccccccccccccc));"); 5935 verifyFormat("DEBUG({\n" 5936 " if (a)\n" 5937 " f();\n" 5938 "});"); 5939 } 5940 5941 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5942 EXPECT_EQ("SOME_MACRO { int i; }\n" 5943 "int i;", 5944 format(" SOME_MACRO {int i;} int i;")); 5945 } 5946 5947 TEST_F(FormatTest, LayoutNestedBlocks) { 5948 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5949 " struct s {\n" 5950 " int i;\n" 5951 " };\n" 5952 " s kBitsToOs[] = {{10}};\n" 5953 " for (int i = 0; i < 10; ++i)\n" 5954 " return;\n" 5955 "}"); 5956 verifyFormat("call(parameter, {\n" 5957 " something();\n" 5958 " // Comment using all columns.\n" 5959 " somethingelse();\n" 5960 "});", 5961 getLLVMStyleWithColumns(40)); 5962 verifyFormat("DEBUG( //\n" 5963 " { f(); }, a);"); 5964 verifyFormat("DEBUG( //\n" 5965 " {\n" 5966 " f(); //\n" 5967 " },\n" 5968 " a);"); 5969 5970 EXPECT_EQ("call(parameter, {\n" 5971 " something();\n" 5972 " // Comment too\n" 5973 " // looooooooooong.\n" 5974 " somethingElse();\n" 5975 "});", 5976 format("call(parameter, {\n" 5977 " something();\n" 5978 " // Comment too looooooooooong.\n" 5979 " somethingElse();\n" 5980 "});", 5981 getLLVMStyleWithColumns(29))); 5982 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5983 EXPECT_EQ("DEBUG({ // comment\n" 5984 " int i;\n" 5985 "});", 5986 format("DEBUG({ // comment\n" 5987 "int i;\n" 5988 "});")); 5989 EXPECT_EQ("DEBUG({\n" 5990 " int i;\n" 5991 "\n" 5992 " // comment\n" 5993 " int j;\n" 5994 "});", 5995 format("DEBUG({\n" 5996 " int i;\n" 5997 "\n" 5998 " // comment\n" 5999 " int j;\n" 6000 "});")); 6001 6002 verifyFormat("DEBUG({\n" 6003 " if (a)\n" 6004 " return;\n" 6005 "});"); 6006 verifyGoogleFormat("DEBUG({\n" 6007 " if (a) return;\n" 6008 "});"); 6009 FormatStyle Style = getGoogleStyle(); 6010 Style.ColumnLimit = 45; 6011 verifyFormat("Debug(\n" 6012 " aaaaa,\n" 6013 " {\n" 6014 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 6015 " },\n" 6016 " a);", 6017 Style); 6018 6019 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 6020 6021 verifyNoCrash("^{v^{a}}"); 6022 } 6023 6024 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 6025 EXPECT_EQ("#define MACRO() \\\n" 6026 " Debug(aaa, /* force line break */ \\\n" 6027 " { \\\n" 6028 " int i; \\\n" 6029 " int j; \\\n" 6030 " })", 6031 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 6032 " { int i; int j; })", 6033 getGoogleStyle())); 6034 6035 EXPECT_EQ("#define A \\\n" 6036 " [] { \\\n" 6037 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 6038 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 6039 " }", 6040 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 6041 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 6042 getGoogleStyle())); 6043 } 6044 6045 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 6046 EXPECT_EQ("{}", format("{}")); 6047 verifyFormat("enum E {};"); 6048 verifyFormat("enum E {}"); 6049 FormatStyle Style = getLLVMStyle(); 6050 Style.SpaceInEmptyBlock = true; 6051 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 6052 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 6053 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 6054 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6055 Style.BraceWrapping.BeforeElse = false; 6056 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 6057 verifyFormat("if (a)\n" 6058 "{\n" 6059 "} else if (b)\n" 6060 "{\n" 6061 "} else\n" 6062 "{ }", 6063 Style); 6064 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 6065 verifyFormat("if (a) {\n" 6066 "} else if (b) {\n" 6067 "} else {\n" 6068 "}", 6069 Style); 6070 Style.BraceWrapping.BeforeElse = true; 6071 verifyFormat("if (a) { }\n" 6072 "else if (b) { }\n" 6073 "else { }", 6074 Style); 6075 } 6076 6077 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 6078 FormatStyle Style = getLLVMStyle(); 6079 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 6080 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 6081 verifyFormat("FOO_BEGIN\n" 6082 " FOO_ENTRY\n" 6083 "FOO_END", 6084 Style); 6085 verifyFormat("FOO_BEGIN\n" 6086 " NESTED_FOO_BEGIN\n" 6087 " NESTED_FOO_ENTRY\n" 6088 " NESTED_FOO_END\n" 6089 "FOO_END", 6090 Style); 6091 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 6092 " int x;\n" 6093 " x = 1;\n" 6094 "FOO_END(Baz)", 6095 Style); 6096 } 6097 6098 //===----------------------------------------------------------------------===// 6099 // Line break tests. 6100 //===----------------------------------------------------------------------===// 6101 6102 TEST_F(FormatTest, PreventConfusingIndents) { 6103 verifyFormat( 6104 "void f() {\n" 6105 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 6106 " parameter, parameter, parameter)),\n" 6107 " SecondLongCall(parameter));\n" 6108 "}"); 6109 verifyFormat( 6110 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6111 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6113 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6114 verifyFormat( 6115 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6116 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 6117 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6118 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 6119 verifyFormat( 6120 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6121 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 6122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 6123 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 6124 verifyFormat("int a = bbbb && ccc &&\n" 6125 " fffff(\n" 6126 "#define A Just forcing a new line\n" 6127 " ddd);"); 6128 } 6129 6130 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 6131 verifyFormat( 6132 "bool aaaaaaa =\n" 6133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 6134 " bbbbbbbb();"); 6135 verifyFormat( 6136 "bool aaaaaaa =\n" 6137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 6138 " bbbbbbbb();"); 6139 6140 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 6141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 6142 " ccccccccc == ddddddddddd;"); 6143 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 6144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 6145 " ccccccccc == ddddddddddd;"); 6146 verifyFormat( 6147 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 6148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 6149 " ccccccccc == ddddddddddd;"); 6150 6151 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 6152 " aaaaaa) &&\n" 6153 " bbbbbb && cccccc;"); 6154 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 6155 " aaaaaa) >>\n" 6156 " bbbbbb;"); 6157 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 6158 " SourceMgr.getSpellingColumnNumber(\n" 6159 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 6160 " 1);"); 6161 6162 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6163 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 6164 " cccccc) {\n}"); 6165 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6166 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 6167 " cccccc) {\n}"); 6168 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6169 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 6170 " cccccc) {\n}"); 6171 verifyFormat("b = a &&\n" 6172 " // Comment\n" 6173 " b.c && d;"); 6174 6175 // If the LHS of a comparison is not a binary expression itself, the 6176 // additional linebreak confuses many people. 6177 verifyFormat( 6178 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6179 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 6180 "}"); 6181 verifyFormat( 6182 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6183 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 6184 "}"); 6185 verifyFormat( 6186 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 6187 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 6188 "}"); 6189 verifyFormat( 6190 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6191 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 6192 "}"); 6193 // Even explicit parentheses stress the precedence enough to make the 6194 // additional break unnecessary. 6195 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 6197 "}"); 6198 // This cases is borderline, but with the indentation it is still readable. 6199 verifyFormat( 6200 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6201 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 6203 "}", 6204 getLLVMStyleWithColumns(75)); 6205 6206 // If the LHS is a binary expression, we should still use the additional break 6207 // as otherwise the formatting hides the operator precedence. 6208 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6210 " 5) {\n" 6211 "}"); 6212 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 6214 " 5) {\n" 6215 "}"); 6216 6217 FormatStyle OnePerLine = getLLVMStyle(); 6218 OnePerLine.BinPackParameters = false; 6219 verifyFormat( 6220 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 6223 OnePerLine); 6224 6225 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 6226 " .aaa(aaaaaaaaaaaaa) *\n" 6227 " aaaaaaa +\n" 6228 " aaaaaaa;", 6229 getLLVMStyleWithColumns(40)); 6230 } 6231 6232 TEST_F(FormatTest, ExpressionIndentation) { 6233 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6234 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6235 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6236 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6237 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 6238 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 6239 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6240 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 6241 " ccccccccccccccccccccccccccccccccccccccccc;"); 6242 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6243 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6244 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6245 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 6246 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6247 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6249 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 6250 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6253 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 6254 verifyFormat("if () {\n" 6255 "} else if (aaaaa && bbbbb > // break\n" 6256 " ccccc) {\n" 6257 "}"); 6258 verifyFormat("if () {\n" 6259 "} else if constexpr (aaaaa && bbbbb > // break\n" 6260 " ccccc) {\n" 6261 "}"); 6262 verifyFormat("if () {\n" 6263 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 6264 " ccccc) {\n" 6265 "}"); 6266 verifyFormat("if () {\n" 6267 "} else if (aaaaa &&\n" 6268 " bbbbb > // break\n" 6269 " ccccc &&\n" 6270 " ddddd) {\n" 6271 "}"); 6272 6273 // Presence of a trailing comment used to change indentation of b. 6274 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 6275 " b;\n" 6276 "return aaaaaaaaaaaaaaaaaaa +\n" 6277 " b; //", 6278 getLLVMStyleWithColumns(30)); 6279 } 6280 6281 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 6282 // Not sure what the best system is here. Like this, the LHS can be found 6283 // immediately above an operator (everything with the same or a higher 6284 // indent). The RHS is aligned right of the operator and so compasses 6285 // everything until something with the same indent as the operator is found. 6286 // FIXME: Is this a good system? 6287 FormatStyle Style = getLLVMStyle(); 6288 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6289 verifyFormat( 6290 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6291 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6292 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6293 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6294 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6295 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6296 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6297 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6298 " > ccccccccccccccccccccccccccccccccccccccccc;", 6299 Style); 6300 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6301 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6302 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6303 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6304 Style); 6305 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6306 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6307 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6308 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6309 Style); 6310 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6311 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6312 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6313 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6314 Style); 6315 verifyFormat("if () {\n" 6316 "} else if (aaaaa\n" 6317 " && bbbbb // break\n" 6318 " > ccccc) {\n" 6319 "}", 6320 Style); 6321 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6322 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6323 Style); 6324 verifyFormat("return (a)\n" 6325 " // comment\n" 6326 " + b;", 6327 Style); 6328 verifyFormat( 6329 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6330 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6331 " + cc;", 6332 Style); 6333 6334 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6335 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6336 Style); 6337 6338 // Forced by comments. 6339 verifyFormat( 6340 "unsigned ContentSize =\n" 6341 " sizeof(int16_t) // DWARF ARange version number\n" 6342 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6343 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6344 " + sizeof(int8_t); // Segment Size (in bytes)"); 6345 6346 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6347 " == boost::fusion::at_c<1>(iiii).second;", 6348 Style); 6349 6350 Style.ColumnLimit = 60; 6351 verifyFormat("zzzzzzzzzz\n" 6352 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6353 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6354 Style); 6355 6356 Style.ColumnLimit = 80; 6357 Style.IndentWidth = 4; 6358 Style.TabWidth = 4; 6359 Style.UseTab = FormatStyle::UT_Always; 6360 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6361 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6362 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 6363 "\t&& (someOtherLongishConditionPart1\n" 6364 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 6365 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 6366 "(someOtherLongishConditionPart1 || " 6367 "someOtherEvenLongerNestedConditionPart2);", 6368 Style)); 6369 } 6370 6371 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 6372 FormatStyle Style = getLLVMStyle(); 6373 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6374 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 6375 6376 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6377 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6378 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6379 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6380 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6381 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6382 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6383 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6384 " > ccccccccccccccccccccccccccccccccccccccccc;", 6385 Style); 6386 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6387 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6388 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6389 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6390 Style); 6391 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6392 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6393 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6394 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6395 Style); 6396 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6397 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6398 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6399 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6400 Style); 6401 verifyFormat("if () {\n" 6402 "} else if (aaaaa\n" 6403 " && bbbbb // break\n" 6404 " > ccccc) {\n" 6405 "}", 6406 Style); 6407 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6408 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6409 Style); 6410 verifyFormat("return (a)\n" 6411 " // comment\n" 6412 " + b;", 6413 Style); 6414 verifyFormat( 6415 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6416 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6417 " + cc;", 6418 Style); 6419 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6420 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6421 " : 3333333333333333;", 6422 Style); 6423 verifyFormat( 6424 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6425 " : ccccccccccccccc ? dddddddddddddddddd\n" 6426 " : eeeeeeeeeeeeeeeeee)\n" 6427 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6428 " : 3333333333333333;", 6429 Style); 6430 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6431 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6432 Style); 6433 6434 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6435 " == boost::fusion::at_c<1>(iiii).second;", 6436 Style); 6437 6438 Style.ColumnLimit = 60; 6439 verifyFormat("zzzzzzzzzzzzz\n" 6440 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6441 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6442 Style); 6443 6444 // Forced by comments. 6445 Style.ColumnLimit = 80; 6446 verifyFormat( 6447 "unsigned ContentSize\n" 6448 " = sizeof(int16_t) // DWARF ARange version number\n" 6449 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6450 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6451 " + sizeof(int8_t); // Segment Size (in bytes)", 6452 Style); 6453 6454 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6455 verifyFormat( 6456 "unsigned ContentSize =\n" 6457 " sizeof(int16_t) // DWARF ARange version number\n" 6458 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6459 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6460 " + sizeof(int8_t); // Segment Size (in bytes)", 6461 Style); 6462 6463 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6464 verifyFormat( 6465 "unsigned ContentSize =\n" 6466 " sizeof(int16_t) // DWARF ARange version number\n" 6467 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6468 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6469 " + sizeof(int8_t); // Segment Size (in bytes)", 6470 Style); 6471 } 6472 6473 TEST_F(FormatTest, EnforcedOperatorWraps) { 6474 // Here we'd like to wrap after the || operators, but a comment is forcing an 6475 // earlier wrap. 6476 verifyFormat("bool x = aaaaa //\n" 6477 " || bbbbb\n" 6478 " //\n" 6479 " || cccc;"); 6480 } 6481 6482 TEST_F(FormatTest, NoOperandAlignment) { 6483 FormatStyle Style = getLLVMStyle(); 6484 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6485 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 6486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6487 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6488 Style); 6489 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6490 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6491 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6492 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6493 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6494 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6495 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6496 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6497 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6498 " > ccccccccccccccccccccccccccccccccccccccccc;", 6499 Style); 6500 6501 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6502 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6503 " + cc;", 6504 Style); 6505 verifyFormat("int a = aa\n" 6506 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6507 " * cccccccccccccccccccccccccccccccccccc;\n", 6508 Style); 6509 6510 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6511 verifyFormat("return (a > b\n" 6512 " // comment1\n" 6513 " // comment2\n" 6514 " || c);", 6515 Style); 6516 } 6517 6518 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 6519 FormatStyle Style = getLLVMStyle(); 6520 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6521 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6523 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6524 Style); 6525 } 6526 6527 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 6528 FormatStyle Style = getLLVMStyleWithColumns(40); 6529 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6530 Style.BinPackArguments = false; 6531 verifyFormat("void test() {\n" 6532 " someFunction(\n" 6533 " this + argument + is + quite\n" 6534 " + long + so + it + gets + wrapped\n" 6535 " + but + remains + bin - packed);\n" 6536 "}", 6537 Style); 6538 verifyFormat("void test() {\n" 6539 " someFunction(arg1,\n" 6540 " this + argument + is\n" 6541 " + quite + long + so\n" 6542 " + it + gets + wrapped\n" 6543 " + but + remains + bin\n" 6544 " - packed,\n" 6545 " arg3);\n" 6546 "}", 6547 Style); 6548 verifyFormat("void test() {\n" 6549 " someFunction(\n" 6550 " arg1,\n" 6551 " this + argument + has\n" 6552 " + anotherFunc(nested,\n" 6553 " calls + whose\n" 6554 " + arguments\n" 6555 " + are + also\n" 6556 " + wrapped,\n" 6557 " in + addition)\n" 6558 " + to + being + bin - packed,\n" 6559 " arg3);\n" 6560 "}", 6561 Style); 6562 6563 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6564 verifyFormat("void test() {\n" 6565 " someFunction(\n" 6566 " arg1,\n" 6567 " this + argument + has +\n" 6568 " anotherFunc(nested,\n" 6569 " calls + whose +\n" 6570 " arguments +\n" 6571 " are + also +\n" 6572 " wrapped,\n" 6573 " in + addition) +\n" 6574 " to + being + bin - packed,\n" 6575 " arg3);\n" 6576 "}", 6577 Style); 6578 } 6579 6580 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) { 6581 auto Style = getLLVMStyleWithColumns(45); 6582 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None); 6583 verifyFormat("bool b =\n" 6584 " is_default_constructible_v<hash<T>> and\n" 6585 " is_copy_constructible_v<hash<T>> and\n" 6586 " is_move_constructible_v<hash<T>> and\n" 6587 " is_copy_assignable_v<hash<T>> and\n" 6588 " is_move_assignable_v<hash<T>> and\n" 6589 " is_destructible_v<hash<T>> and\n" 6590 " is_swappable_v<hash<T>> and\n" 6591 " is_callable_v<hash<T>(T)>;", 6592 Style); 6593 6594 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6595 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n" 6596 " and is_copy_constructible_v<hash<T>>\n" 6597 " and is_move_constructible_v<hash<T>>\n" 6598 " and is_copy_assignable_v<hash<T>>\n" 6599 " and is_move_assignable_v<hash<T>>\n" 6600 " and is_destructible_v<hash<T>>\n" 6601 " and is_swappable_v<hash<T>>\n" 6602 " and is_callable_v<hash<T>(T)>;", 6603 Style); 6604 6605 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6606 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n" 6607 " and is_copy_constructible_v<hash<T>>\n" 6608 " and is_move_constructible_v<hash<T>>\n" 6609 " and is_copy_assignable_v<hash<T>>\n" 6610 " and is_move_assignable_v<hash<T>>\n" 6611 " and is_destructible_v<hash<T>>\n" 6612 " and is_swappable_v<hash<T>>\n" 6613 " and is_callable_v<hash<T>(T)>;", 6614 Style); 6615 } 6616 6617 TEST_F(FormatTest, ConstructorInitializers) { 6618 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6619 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 6620 getLLVMStyleWithColumns(45)); 6621 verifyFormat("Constructor()\n" 6622 " : Inttializer(FitsOnTheLine) {}", 6623 getLLVMStyleWithColumns(44)); 6624 verifyFormat("Constructor()\n" 6625 " : Inttializer(FitsOnTheLine) {}", 6626 getLLVMStyleWithColumns(43)); 6627 6628 verifyFormat("template <typename T>\n" 6629 "Constructor() : Initializer(FitsOnTheLine) {}", 6630 getLLVMStyleWithColumns(45)); 6631 6632 verifyFormat( 6633 "SomeClass::Constructor()\n" 6634 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6635 6636 verifyFormat( 6637 "SomeClass::Constructor()\n" 6638 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6639 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 6640 verifyFormat( 6641 "SomeClass::Constructor()\n" 6642 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6643 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6644 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6645 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6646 " : aaaaaaaaaa(aaaaaa) {}"); 6647 6648 verifyFormat("Constructor()\n" 6649 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6650 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6651 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6652 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 6653 6654 verifyFormat("Constructor()\n" 6655 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6657 6658 verifyFormat("Constructor(int Parameter = 0)\n" 6659 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6660 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 6661 verifyFormat("Constructor()\n" 6662 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6663 "}", 6664 getLLVMStyleWithColumns(60)); 6665 verifyFormat("Constructor()\n" 6666 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6667 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 6668 6669 // Here a line could be saved by splitting the second initializer onto two 6670 // lines, but that is not desirable. 6671 verifyFormat("Constructor()\n" 6672 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6673 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6674 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6675 6676 FormatStyle OnePerLine = getLLVMStyle(); 6677 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 6678 verifyFormat("MyClass::MyClass()\n" 6679 " : a(a),\n" 6680 " b(b),\n" 6681 " c(c) {}", 6682 OnePerLine); 6683 verifyFormat("MyClass::MyClass()\n" 6684 " : a(a), // comment\n" 6685 " b(b),\n" 6686 " c(c) {}", 6687 OnePerLine); 6688 verifyFormat("MyClass::MyClass(int a)\n" 6689 " : b(a), // comment\n" 6690 " c(a + 1) { // lined up\n" 6691 "}", 6692 OnePerLine); 6693 verifyFormat("Constructor()\n" 6694 " : a(b, b, b) {}", 6695 OnePerLine); 6696 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6697 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 6698 verifyFormat("SomeClass::Constructor()\n" 6699 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6700 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6701 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6702 OnePerLine); 6703 verifyFormat("SomeClass::Constructor()\n" 6704 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6705 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6706 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6707 OnePerLine); 6708 verifyFormat("MyClass::MyClass(int var)\n" 6709 " : some_var_(var), // 4 space indent\n" 6710 " some_other_var_(var + 1) { // lined up\n" 6711 "}", 6712 OnePerLine); 6713 verifyFormat("Constructor()\n" 6714 " : aaaaa(aaaaaa),\n" 6715 " aaaaa(aaaaaa),\n" 6716 " aaaaa(aaaaaa),\n" 6717 " aaaaa(aaaaaa),\n" 6718 " aaaaa(aaaaaa) {}", 6719 OnePerLine); 6720 verifyFormat("Constructor()\n" 6721 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6722 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6723 OnePerLine); 6724 OnePerLine.BinPackParameters = false; 6725 verifyFormat( 6726 "Constructor()\n" 6727 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6728 " aaaaaaaaaaa().aaa(),\n" 6729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6730 OnePerLine); 6731 OnePerLine.ColumnLimit = 60; 6732 verifyFormat("Constructor()\n" 6733 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6734 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6735 OnePerLine); 6736 6737 EXPECT_EQ("Constructor()\n" 6738 " : // Comment forcing unwanted break.\n" 6739 " aaaa(aaaa) {}", 6740 format("Constructor() :\n" 6741 " // Comment forcing unwanted break.\n" 6742 " aaaa(aaaa) {}")); 6743 } 6744 6745 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6746 FormatStyle Style = getLLVMStyleWithColumns(60); 6747 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6748 Style.BinPackParameters = false; 6749 6750 for (int i = 0; i < 4; ++i) { 6751 // Test all combinations of parameters that should not have an effect. 6752 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6753 Style.AllowAllArgumentsOnNextLine = i & 2; 6754 6755 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6756 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6757 verifyFormat("Constructor()\n" 6758 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6759 Style); 6760 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6761 6762 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6763 verifyFormat("Constructor()\n" 6764 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6765 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6766 Style); 6767 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6768 6769 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6770 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6771 verifyFormat("Constructor()\n" 6772 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6773 Style); 6774 6775 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6776 verifyFormat("Constructor()\n" 6777 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6778 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6779 Style); 6780 6781 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6782 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6783 verifyFormat("Constructor() :\n" 6784 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6785 Style); 6786 6787 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6788 verifyFormat("Constructor() :\n" 6789 " aaaaaaaaaaaaaaaaaa(a),\n" 6790 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6791 Style); 6792 } 6793 6794 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6795 // AllowAllConstructorInitializersOnNextLine in all 6796 // BreakConstructorInitializers modes 6797 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6798 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6799 verifyFormat("SomeClassWithALongName::Constructor(\n" 6800 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6801 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6802 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6803 Style); 6804 6805 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6806 verifyFormat("SomeClassWithALongName::Constructor(\n" 6807 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6808 " int bbbbbbbbbbbbb,\n" 6809 " int cccccccccccccccc)\n" 6810 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6811 Style); 6812 6813 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6814 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6815 verifyFormat("SomeClassWithALongName::Constructor(\n" 6816 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6817 " int bbbbbbbbbbbbb)\n" 6818 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6819 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6820 Style); 6821 6822 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6823 6824 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6825 verifyFormat("SomeClassWithALongName::Constructor(\n" 6826 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6827 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6828 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6829 Style); 6830 6831 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6832 verifyFormat("SomeClassWithALongName::Constructor(\n" 6833 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6834 " int bbbbbbbbbbbbb,\n" 6835 " int cccccccccccccccc)\n" 6836 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6837 Style); 6838 6839 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6840 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6841 verifyFormat("SomeClassWithALongName::Constructor(\n" 6842 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6843 " int bbbbbbbbbbbbb)\n" 6844 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6845 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6846 Style); 6847 6848 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6849 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6850 verifyFormat("SomeClassWithALongName::Constructor(\n" 6851 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6852 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6853 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6854 Style); 6855 6856 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6857 verifyFormat("SomeClassWithALongName::Constructor(\n" 6858 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6859 " int bbbbbbbbbbbbb,\n" 6860 " int cccccccccccccccc) :\n" 6861 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6862 Style); 6863 6864 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6865 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6866 verifyFormat("SomeClassWithALongName::Constructor(\n" 6867 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6868 " int bbbbbbbbbbbbb) :\n" 6869 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6870 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6871 Style); 6872 } 6873 6874 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6875 FormatStyle Style = getLLVMStyleWithColumns(60); 6876 Style.BinPackArguments = false; 6877 for (int i = 0; i < 4; ++i) { 6878 // Test all combinations of parameters that should not have an effect. 6879 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6880 Style.PackConstructorInitializers = 6881 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6882 6883 Style.AllowAllArgumentsOnNextLine = true; 6884 verifyFormat("void foo() {\n" 6885 " FunctionCallWithReallyLongName(\n" 6886 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6887 "}", 6888 Style); 6889 Style.AllowAllArgumentsOnNextLine = false; 6890 verifyFormat("void foo() {\n" 6891 " FunctionCallWithReallyLongName(\n" 6892 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6893 " bbbbbbbbbbbb);\n" 6894 "}", 6895 Style); 6896 6897 Style.AllowAllArgumentsOnNextLine = true; 6898 verifyFormat("void foo() {\n" 6899 " auto VariableWithReallyLongName = {\n" 6900 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6901 "}", 6902 Style); 6903 Style.AllowAllArgumentsOnNextLine = false; 6904 verifyFormat("void foo() {\n" 6905 " auto VariableWithReallyLongName = {\n" 6906 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6907 " bbbbbbbbbbbb};\n" 6908 "}", 6909 Style); 6910 } 6911 6912 // This parameter should not affect declarations. 6913 Style.BinPackParameters = false; 6914 Style.AllowAllArgumentsOnNextLine = false; 6915 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6916 verifyFormat("void FunctionCallWithReallyLongName(\n" 6917 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6918 Style); 6919 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6920 verifyFormat("void FunctionCallWithReallyLongName(\n" 6921 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6922 " int bbbbbbbbbbbb);", 6923 Style); 6924 } 6925 6926 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6927 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6928 // and BAS_Align. 6929 FormatStyle Style = getLLVMStyleWithColumns(35); 6930 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6931 "void functionDecl(int A, int B, int C);"; 6932 Style.AllowAllArgumentsOnNextLine = false; 6933 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6934 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6935 " paramC);\n" 6936 "void functionDecl(int A, int B,\n" 6937 " int C);"), 6938 format(Input, Style)); 6939 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6940 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6941 " paramC);\n" 6942 "void functionDecl(int A, int B,\n" 6943 " int C);"), 6944 format(Input, Style)); 6945 // However, BAS_AlwaysBreak should take precedence over 6946 // AllowAllArgumentsOnNextLine. 6947 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6948 EXPECT_EQ(StringRef("functionCall(\n" 6949 " paramA, paramB, paramC);\n" 6950 "void functionDecl(\n" 6951 " int A, int B, int C);"), 6952 format(Input, Style)); 6953 6954 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6955 // first argument. 6956 Style.AllowAllArgumentsOnNextLine = true; 6957 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6958 EXPECT_EQ(StringRef("functionCall(\n" 6959 " paramA, paramB, paramC);\n" 6960 "void functionDecl(\n" 6961 " int A, int B, int C);"), 6962 format(Input, Style)); 6963 // It wouldn't fit on one line with aligned parameters so this setting 6964 // doesn't change anything for BAS_Align. 6965 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6966 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6967 " paramC);\n" 6968 "void functionDecl(int A, int B,\n" 6969 " int C);"), 6970 format(Input, Style)); 6971 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6972 EXPECT_EQ(StringRef("functionCall(\n" 6973 " paramA, paramB, paramC);\n" 6974 "void functionDecl(\n" 6975 " int A, int B, int C);"), 6976 format(Input, Style)); 6977 } 6978 6979 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6980 FormatStyle Style = getLLVMStyle(); 6981 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6982 6983 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6984 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6985 getStyleWithColumns(Style, 45)); 6986 verifyFormat("Constructor() :\n" 6987 " Initializer(FitsOnTheLine) {}", 6988 getStyleWithColumns(Style, 44)); 6989 verifyFormat("Constructor() :\n" 6990 " Initializer(FitsOnTheLine) {}", 6991 getStyleWithColumns(Style, 43)); 6992 6993 verifyFormat("template <typename T>\n" 6994 "Constructor() : Initializer(FitsOnTheLine) {}", 6995 getStyleWithColumns(Style, 50)); 6996 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6997 verifyFormat( 6998 "SomeClass::Constructor() :\n" 6999 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 7000 Style); 7001 7002 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 7003 verifyFormat( 7004 "SomeClass::Constructor() :\n" 7005 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 7006 Style); 7007 7008 verifyFormat( 7009 "SomeClass::Constructor() :\n" 7010 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7011 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 7012 Style); 7013 verifyFormat( 7014 "SomeClass::Constructor() :\n" 7015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7016 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 7017 Style); 7018 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7019 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7020 " aaaaaaaaaa(aaaaaa) {}", 7021 Style); 7022 7023 verifyFormat("Constructor() :\n" 7024 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7025 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7026 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7027 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 7028 Style); 7029 7030 verifyFormat("Constructor() :\n" 7031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7033 Style); 7034 7035 verifyFormat("Constructor(int Parameter = 0) :\n" 7036 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 7037 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 7038 Style); 7039 verifyFormat("Constructor() :\n" 7040 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 7041 "}", 7042 getStyleWithColumns(Style, 60)); 7043 verifyFormat("Constructor() :\n" 7044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7045 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 7046 Style); 7047 7048 // Here a line could be saved by splitting the second initializer onto two 7049 // lines, but that is not desirable. 7050 verifyFormat("Constructor() :\n" 7051 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 7052 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 7053 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7054 Style); 7055 7056 FormatStyle OnePerLine = Style; 7057 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7058 verifyFormat("SomeClass::Constructor() :\n" 7059 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7060 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7061 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 7062 OnePerLine); 7063 verifyFormat("SomeClass::Constructor() :\n" 7064 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 7065 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7066 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 7067 OnePerLine); 7068 verifyFormat("MyClass::MyClass(int var) :\n" 7069 " some_var_(var), // 4 space indent\n" 7070 " some_other_var_(var + 1) { // lined up\n" 7071 "}", 7072 OnePerLine); 7073 verifyFormat("Constructor() :\n" 7074 " aaaaa(aaaaaa),\n" 7075 " aaaaa(aaaaaa),\n" 7076 " aaaaa(aaaaaa),\n" 7077 " aaaaa(aaaaaa),\n" 7078 " aaaaa(aaaaaa) {}", 7079 OnePerLine); 7080 verifyFormat("Constructor() :\n" 7081 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 7082 " aaaaaaaaaaaaaaaaaaaaaa) {}", 7083 OnePerLine); 7084 OnePerLine.BinPackParameters = false; 7085 verifyFormat("Constructor() :\n" 7086 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7087 " aaaaaaaaaaa().aaa(),\n" 7088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7089 OnePerLine); 7090 OnePerLine.ColumnLimit = 60; 7091 verifyFormat("Constructor() :\n" 7092 " aaaaaaaaaaaaaaaaaaaa(a),\n" 7093 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 7094 OnePerLine); 7095 7096 EXPECT_EQ("Constructor() :\n" 7097 " // Comment forcing unwanted break.\n" 7098 " aaaa(aaaa) {}", 7099 format("Constructor() :\n" 7100 " // Comment forcing unwanted break.\n" 7101 " aaaa(aaaa) {}", 7102 Style)); 7103 7104 Style.ColumnLimit = 0; 7105 verifyFormat("SomeClass::Constructor() :\n" 7106 " a(a) {}", 7107 Style); 7108 verifyFormat("SomeClass::Constructor() noexcept :\n" 7109 " a(a) {}", 7110 Style); 7111 verifyFormat("SomeClass::Constructor() :\n" 7112 " a(a), b(b), c(c) {}", 7113 Style); 7114 verifyFormat("SomeClass::Constructor() :\n" 7115 " a(a) {\n" 7116 " foo();\n" 7117 " bar();\n" 7118 "}", 7119 Style); 7120 7121 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7122 verifyFormat("SomeClass::Constructor() :\n" 7123 " a(a), b(b), c(c) {\n" 7124 "}", 7125 Style); 7126 verifyFormat("SomeClass::Constructor() :\n" 7127 " a(a) {\n" 7128 "}", 7129 Style); 7130 7131 Style.ColumnLimit = 80; 7132 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7133 Style.ConstructorInitializerIndentWidth = 2; 7134 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 7135 verifyFormat("SomeClass::Constructor() :\n" 7136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7137 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 7138 Style); 7139 7140 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 7141 // well 7142 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 7143 verifyFormat( 7144 "class SomeClass\n" 7145 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7146 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 7147 Style); 7148 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 7149 verifyFormat( 7150 "class SomeClass\n" 7151 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7152 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 7153 Style); 7154 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 7155 verifyFormat( 7156 "class SomeClass :\n" 7157 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7158 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 7159 Style); 7160 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 7161 verifyFormat( 7162 "class SomeClass\n" 7163 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7164 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 7165 Style); 7166 } 7167 7168 #ifndef EXPENSIVE_CHECKS 7169 // Expensive checks enables libstdc++ checking which includes validating the 7170 // state of ranges used in std::priority_queue - this blows out the 7171 // runtime/scalability of the function and makes this test unacceptably slow. 7172 TEST_F(FormatTest, MemoizationTests) { 7173 // This breaks if the memoization lookup does not take \c Indent and 7174 // \c LastSpace into account. 7175 verifyFormat( 7176 "extern CFRunLoopTimerRef\n" 7177 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 7178 " CFTimeInterval interval, CFOptionFlags flags,\n" 7179 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 7180 " CFRunLoopTimerContext *context) {}"); 7181 7182 // Deep nesting somewhat works around our memoization. 7183 verifyFormat( 7184 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 7185 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 7186 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 7187 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 7188 " aaaaa())))))))))))))))))))))))))))))))))))))));", 7189 getLLVMStyleWithColumns(65)); 7190 verifyFormat( 7191 "aaaaa(\n" 7192 " aaaaa,\n" 7193 " aaaaa(\n" 7194 " aaaaa,\n" 7195 " aaaaa(\n" 7196 " aaaaa,\n" 7197 " aaaaa(\n" 7198 " aaaaa,\n" 7199 " aaaaa(\n" 7200 " aaaaa,\n" 7201 " aaaaa(\n" 7202 " aaaaa,\n" 7203 " aaaaa(\n" 7204 " aaaaa,\n" 7205 " aaaaa(\n" 7206 " aaaaa,\n" 7207 " aaaaa(\n" 7208 " aaaaa,\n" 7209 " aaaaa(\n" 7210 " aaaaa,\n" 7211 " aaaaa(\n" 7212 " aaaaa,\n" 7213 " aaaaa(\n" 7214 " aaaaa,\n" 7215 " aaaaa))))))))))));", 7216 getLLVMStyleWithColumns(65)); 7217 verifyFormat( 7218 "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" 7219 " a),\n" 7220 " a),\n" 7221 " a),\n" 7222 " a),\n" 7223 " a),\n" 7224 " a),\n" 7225 " a),\n" 7226 " a),\n" 7227 " a),\n" 7228 " a),\n" 7229 " a),\n" 7230 " a),\n" 7231 " a),\n" 7232 " a),\n" 7233 " a),\n" 7234 " a),\n" 7235 " a)", 7236 getLLVMStyleWithColumns(65)); 7237 7238 // This test takes VERY long when memoization is broken. 7239 FormatStyle OnePerLine = getLLVMStyle(); 7240 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7241 OnePerLine.BinPackParameters = false; 7242 std::string input = "Constructor()\n" 7243 " : aaaa(a,\n"; 7244 for (unsigned i = 0, e = 80; i != e; ++i) 7245 input += " a,\n"; 7246 input += " a) {}"; 7247 verifyFormat(input, OnePerLine); 7248 } 7249 #endif 7250 7251 TEST_F(FormatTest, BreaksAsHighAsPossible) { 7252 verifyFormat( 7253 "void f() {\n" 7254 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 7255 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 7256 " f();\n" 7257 "}"); 7258 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 7259 " Intervals[i - 1].getRange().getLast()) {\n}"); 7260 } 7261 7262 TEST_F(FormatTest, BreaksFunctionDeclarations) { 7263 // Principially, we break function declarations in a certain order: 7264 // 1) break amongst arguments. 7265 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 7266 " Cccccccccccccc cccccccccccccc);"); 7267 verifyFormat("template <class TemplateIt>\n" 7268 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 7269 " TemplateIt *stop) {}"); 7270 7271 // 2) break after return type. 7272 verifyFormat( 7273 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7274 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 7275 getGoogleStyle()); 7276 7277 // 3) break after (. 7278 verifyFormat( 7279 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 7280 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 7281 getGoogleStyle()); 7282 7283 // 4) break before after nested name specifiers. 7284 verifyFormat( 7285 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7286 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 7287 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 7288 getGoogleStyle()); 7289 7290 // However, there are exceptions, if a sufficient amount of lines can be 7291 // saved. 7292 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 7293 // more adjusting. 7294 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 7295 " Cccccccccccccc cccccccccc,\n" 7296 " Cccccccccccccc cccccccccc,\n" 7297 " Cccccccccccccc cccccccccc,\n" 7298 " Cccccccccccccc cccccccccc);"); 7299 verifyFormat( 7300 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7301 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7302 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7303 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 7304 getGoogleStyle()); 7305 verifyFormat( 7306 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 7307 " Cccccccccccccc cccccccccc,\n" 7308 " Cccccccccccccc cccccccccc,\n" 7309 " Cccccccccccccc cccccccccc,\n" 7310 " Cccccccccccccc cccccccccc,\n" 7311 " Cccccccccccccc cccccccccc,\n" 7312 " Cccccccccccccc cccccccccc);"); 7313 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7314 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7315 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7316 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7317 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 7318 7319 // Break after multi-line parameters. 7320 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7323 " bbbb bbbb);"); 7324 verifyFormat("void SomeLoooooooooooongFunction(\n" 7325 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7326 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7327 " int bbbbbbbbbbbbb);"); 7328 7329 // Treat overloaded operators like other functions. 7330 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7331 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 7332 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7333 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 7334 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7335 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 7336 verifyGoogleFormat( 7337 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 7338 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 7339 verifyGoogleFormat( 7340 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 7341 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 7342 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7343 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 7344 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 7345 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 7346 verifyGoogleFormat( 7347 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 7348 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7349 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 7350 verifyGoogleFormat("template <typename T>\n" 7351 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7352 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 7353 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 7354 7355 FormatStyle Style = getLLVMStyle(); 7356 Style.PointerAlignment = FormatStyle::PAS_Left; 7357 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7358 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 7359 Style); 7360 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 7361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7362 Style); 7363 } 7364 7365 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 7366 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 7367 // Prefer keeping `::` followed by `operator` together. 7368 EXPECT_EQ("const aaaa::bbbbbbb &\n" 7369 "ccccccccc::operator++() {\n" 7370 " stuff();\n" 7371 "}", 7372 format("const aaaa::bbbbbbb\n" 7373 "&ccccccccc::operator++() { stuff(); }", 7374 getLLVMStyleWithColumns(40))); 7375 } 7376 7377 TEST_F(FormatTest, TrailingReturnType) { 7378 verifyFormat("auto foo() -> int;\n"); 7379 // correct trailing return type spacing 7380 verifyFormat("auto operator->() -> int;\n"); 7381 verifyFormat("auto operator++(int) -> int;\n"); 7382 7383 verifyFormat("struct S {\n" 7384 " auto bar() const -> int;\n" 7385 "};"); 7386 verifyFormat("template <size_t Order, typename T>\n" 7387 "auto load_img(const std::string &filename)\n" 7388 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 7389 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 7390 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 7391 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 7392 verifyFormat("template <typename T>\n" 7393 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 7394 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 7395 7396 // Not trailing return types. 7397 verifyFormat("void f() { auto a = b->c(); }"); 7398 verifyFormat("auto a = p->foo();"); 7399 verifyFormat("int a = p->foo();"); 7400 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); 7401 } 7402 7403 TEST_F(FormatTest, DeductionGuides) { 7404 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 7405 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 7406 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 7407 verifyFormat( 7408 "template <class... T>\n" 7409 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 7410 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 7411 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 7412 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 7413 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 7414 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 7415 verifyFormat("template <class T> x() -> x<1>;"); 7416 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 7417 7418 // Ensure not deduction guides. 7419 verifyFormat("c()->f<int>();"); 7420 verifyFormat("x()->foo<1>;"); 7421 verifyFormat("x = p->foo<3>();"); 7422 verifyFormat("x()->x<1>();"); 7423 verifyFormat("x()->x<1>;"); 7424 } 7425 7426 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 7427 // Avoid breaking before trailing 'const' or other trailing annotations, if 7428 // they are not function-like. 7429 FormatStyle Style = getGoogleStyleWithColumns(47); 7430 verifyFormat("void someLongFunction(\n" 7431 " int someLoooooooooooooongParameter) const {\n}", 7432 getLLVMStyleWithColumns(47)); 7433 verifyFormat("LoooooongReturnType\n" 7434 "someLoooooooongFunction() const {}", 7435 getLLVMStyleWithColumns(47)); 7436 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 7437 " const {}", 7438 Style); 7439 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7440 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 7441 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7442 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 7443 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7444 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 7445 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 7446 " aaaaaaaaaaa aaaaa) const override;"); 7447 verifyGoogleFormat( 7448 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7449 " const override;"); 7450 7451 // Even if the first parameter has to be wrapped. 7452 verifyFormat("void someLongFunction(\n" 7453 " int someLongParameter) const {}", 7454 getLLVMStyleWithColumns(46)); 7455 verifyFormat("void someLongFunction(\n" 7456 " int someLongParameter) const {}", 7457 Style); 7458 verifyFormat("void someLongFunction(\n" 7459 " int someLongParameter) override {}", 7460 Style); 7461 verifyFormat("void someLongFunction(\n" 7462 " int someLongParameter) OVERRIDE {}", 7463 Style); 7464 verifyFormat("void someLongFunction(\n" 7465 " int someLongParameter) final {}", 7466 Style); 7467 verifyFormat("void someLongFunction(\n" 7468 " int someLongParameter) FINAL {}", 7469 Style); 7470 verifyFormat("void someLongFunction(\n" 7471 " int parameter) const override {}", 7472 Style); 7473 7474 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 7475 verifyFormat("void someLongFunction(\n" 7476 " int someLongParameter) const\n" 7477 "{\n" 7478 "}", 7479 Style); 7480 7481 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 7482 verifyFormat("void someLongFunction(\n" 7483 " int someLongParameter) const\n" 7484 " {\n" 7485 " }", 7486 Style); 7487 7488 // Unless these are unknown annotations. 7489 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 7490 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7491 " LONG_AND_UGLY_ANNOTATION;"); 7492 7493 // Breaking before function-like trailing annotations is fine to keep them 7494 // close to their arguments. 7495 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7496 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7497 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7498 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7499 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7500 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 7501 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 7502 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 7503 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 7504 7505 verifyFormat( 7506 "void aaaaaaaaaaaaaaaaaa()\n" 7507 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 7508 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 7509 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7510 " __attribute__((unused));"); 7511 verifyGoogleFormat( 7512 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7513 " GUARDED_BY(aaaaaaaaaaaa);"); 7514 verifyGoogleFormat( 7515 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7516 " GUARDED_BY(aaaaaaaaaaaa);"); 7517 verifyGoogleFormat( 7518 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7519 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7520 verifyGoogleFormat( 7521 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7522 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7523 } 7524 7525 TEST_F(FormatTest, FunctionAnnotations) { 7526 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7527 "int OldFunction(const string ¶meter) {}"); 7528 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7529 "string OldFunction(const string ¶meter) {}"); 7530 verifyFormat("template <typename T>\n" 7531 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7532 "string OldFunction(const string ¶meter) {}"); 7533 7534 // Not function annotations. 7535 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7536 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 7537 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 7538 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 7539 verifyFormat("MACRO(abc).function() // wrap\n" 7540 " << abc;"); 7541 verifyFormat("MACRO(abc)->function() // wrap\n" 7542 " << abc;"); 7543 verifyFormat("MACRO(abc)::function() // wrap\n" 7544 " << abc;"); 7545 } 7546 7547 TEST_F(FormatTest, BreaksDesireably) { 7548 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7549 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7550 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 7551 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 7553 "}"); 7554 7555 verifyFormat( 7556 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7558 7559 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7560 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7562 7563 verifyFormat( 7564 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7565 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7567 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7568 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 7569 7570 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7571 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7572 7573 verifyFormat( 7574 "void f() {\n" 7575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7576 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7577 "}"); 7578 verifyFormat( 7579 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7581 verifyFormat( 7582 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7584 verifyFormat( 7585 "aaaaaa(aaa,\n" 7586 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7588 " aaaa);"); 7589 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7590 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7592 7593 // Indent consistently independent of call expression and unary operator. 7594 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7595 " dddddddddddddddddddddddddddddd));"); 7596 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7597 " dddddddddddddddddddddddddddddd));"); 7598 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 7599 " dddddddddddddddddddddddddddddd));"); 7600 7601 // This test case breaks on an incorrect memoization, i.e. an optimization not 7602 // taking into account the StopAt value. 7603 verifyFormat( 7604 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7605 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7606 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7607 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7608 7609 verifyFormat("{\n {\n {\n" 7610 " Annotation.SpaceRequiredBefore =\n" 7611 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 7612 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 7613 " }\n }\n}"); 7614 7615 // Break on an outer level if there was a break on an inner level. 7616 EXPECT_EQ("f(g(h(a, // comment\n" 7617 " b, c),\n" 7618 " d, e),\n" 7619 " x, y);", 7620 format("f(g(h(a, // comment\n" 7621 " b, c), d, e), x, y);")); 7622 7623 // Prefer breaking similar line breaks. 7624 verifyFormat( 7625 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 7626 " NSTrackingMouseEnteredAndExited |\n" 7627 " NSTrackingActiveAlways;"); 7628 } 7629 7630 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 7631 FormatStyle NoBinPacking = getGoogleStyle(); 7632 NoBinPacking.BinPackParameters = false; 7633 NoBinPacking.BinPackArguments = true; 7634 verifyFormat("void f() {\n" 7635 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 7636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7637 "}", 7638 NoBinPacking); 7639 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 7640 " int aaaaaaaaaaaaaaaaaaaa,\n" 7641 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7642 NoBinPacking); 7643 7644 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7645 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7646 " vector<int> bbbbbbbbbbbbbbb);", 7647 NoBinPacking); 7648 // FIXME: This behavior difference is probably not wanted. However, currently 7649 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 7650 // template arguments from BreakBeforeParameter being set because of the 7651 // one-per-line formatting. 7652 verifyFormat( 7653 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7654 " aaaaaaaaaa> aaaaaaaaaa);", 7655 NoBinPacking); 7656 verifyFormat( 7657 "void fffffffffff(\n" 7658 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 7659 " aaaaaaaaaa);"); 7660 } 7661 7662 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 7663 FormatStyle NoBinPacking = getGoogleStyle(); 7664 NoBinPacking.BinPackParameters = false; 7665 NoBinPacking.BinPackArguments = false; 7666 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 7667 " aaaaaaaaaaaaaaaaaaaa,\n" 7668 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 7669 NoBinPacking); 7670 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 7671 " aaaaaaaaaaaaa,\n" 7672 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 7673 NoBinPacking); 7674 verifyFormat( 7675 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7676 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7677 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7678 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 7680 NoBinPacking); 7681 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7682 " .aaaaaaaaaaaaaaaaaa();", 7683 NoBinPacking); 7684 verifyFormat("void f() {\n" 7685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7686 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 7687 "}", 7688 NoBinPacking); 7689 7690 verifyFormat( 7691 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7692 " aaaaaaaaaaaa,\n" 7693 " aaaaaaaaaaaa);", 7694 NoBinPacking); 7695 verifyFormat( 7696 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 7697 " ddddddddddddddddddddddddddddd),\n" 7698 " test);", 7699 NoBinPacking); 7700 7701 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7702 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 7703 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 7704 " aaaaaaaaaaaaaaaaaa;", 7705 NoBinPacking); 7706 verifyFormat("a(\"a\"\n" 7707 " \"a\",\n" 7708 " a);"); 7709 7710 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7711 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7712 " aaaaaaaaa,\n" 7713 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7714 NoBinPacking); 7715 verifyFormat( 7716 "void f() {\n" 7717 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7718 " .aaaaaaa();\n" 7719 "}", 7720 NoBinPacking); 7721 verifyFormat( 7722 "template <class SomeType, class SomeOtherType>\n" 7723 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7724 NoBinPacking); 7725 } 7726 7727 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7728 FormatStyle Style = getLLVMStyleWithColumns(15); 7729 Style.ExperimentalAutoDetectBinPacking = true; 7730 EXPECT_EQ("aaa(aaaa,\n" 7731 " aaaa,\n" 7732 " aaaa);\n" 7733 "aaa(aaaa,\n" 7734 " aaaa,\n" 7735 " aaaa);", 7736 format("aaa(aaaa,\n" // one-per-line 7737 " aaaa,\n" 7738 " aaaa );\n" 7739 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7740 Style)); 7741 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7742 " aaaa);\n" 7743 "aaa(aaaa, aaaa,\n" 7744 " aaaa);", 7745 format("aaa(aaaa, aaaa,\n" // bin-packed 7746 " aaaa );\n" 7747 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7748 Style)); 7749 } 7750 7751 TEST_F(FormatTest, FormatsBuilderPattern) { 7752 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7753 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7754 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7755 " .StartsWith(\".init\", ORDER_INIT)\n" 7756 " .StartsWith(\".fini\", ORDER_FINI)\n" 7757 " .StartsWith(\".hash\", ORDER_HASH)\n" 7758 " .Default(ORDER_TEXT);\n"); 7759 7760 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7761 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7762 verifyFormat("aaaaaaa->aaaaaaa\n" 7763 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7765 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7766 verifyFormat( 7767 "aaaaaaa->aaaaaaa\n" 7768 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7769 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7770 verifyFormat( 7771 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7772 " aaaaaaaaaaaaaa);"); 7773 verifyFormat( 7774 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7775 " aaaaaa->aaaaaaaaaaaa()\n" 7776 " ->aaaaaaaaaaaaaaaa(\n" 7777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7778 " ->aaaaaaaaaaaaaaaaa();"); 7779 verifyGoogleFormat( 7780 "void f() {\n" 7781 " someo->Add((new util::filetools::Handler(dir))\n" 7782 " ->OnEvent1(NewPermanentCallback(\n" 7783 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7784 " ->OnEvent2(NewPermanentCallback(\n" 7785 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7786 " ->OnEvent3(NewPermanentCallback(\n" 7787 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7788 " ->OnEvent5(NewPermanentCallback(\n" 7789 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7790 " ->OnEvent6(NewPermanentCallback(\n" 7791 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7792 "}"); 7793 7794 verifyFormat( 7795 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7796 verifyFormat("aaaaaaaaaaaaaaa()\n" 7797 " .aaaaaaaaaaaaaaa()\n" 7798 " .aaaaaaaaaaaaaaa()\n" 7799 " .aaaaaaaaaaaaaaa()\n" 7800 " .aaaaaaaaaaaaaaa();"); 7801 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7802 " .aaaaaaaaaaaaaaa()\n" 7803 " .aaaaaaaaaaaaaaa()\n" 7804 " .aaaaaaaaaaaaaaa();"); 7805 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7806 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7807 " .aaaaaaaaaaaaaaa();"); 7808 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7809 " ->aaaaaaaaaaaaaae(0)\n" 7810 " ->aaaaaaaaaaaaaaa();"); 7811 7812 // Don't linewrap after very short segments. 7813 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7814 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7815 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7816 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7817 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7818 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7819 verifyFormat("aaa()\n" 7820 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7821 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7822 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7823 7824 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7825 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7826 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7827 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7828 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7830 7831 // Prefer not to break after empty parentheses. 7832 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7833 " First->LastNewlineOffset);"); 7834 7835 // Prefer not to create "hanging" indents. 7836 verifyFormat( 7837 "return !soooooooooooooome_map\n" 7838 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7839 " .second;"); 7840 verifyFormat( 7841 "return aaaaaaaaaaaaaaaa\n" 7842 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7843 " .aaaa(aaaaaaaaaaaaaa);"); 7844 // No hanging indent here. 7845 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7846 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7847 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7849 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7850 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7851 getLLVMStyleWithColumns(60)); 7852 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7853 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7854 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7855 getLLVMStyleWithColumns(59)); 7856 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7858 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7859 7860 // Dont break if only closing statements before member call 7861 verifyFormat("test() {\n" 7862 " ([]() -> {\n" 7863 " int b = 32;\n" 7864 " return 3;\n" 7865 " }).foo();\n" 7866 "}"); 7867 verifyFormat("test() {\n" 7868 " (\n" 7869 " []() -> {\n" 7870 " int b = 32;\n" 7871 " return 3;\n" 7872 " },\n" 7873 " foo, bar)\n" 7874 " .foo();\n" 7875 "}"); 7876 verifyFormat("test() {\n" 7877 " ([]() -> {\n" 7878 " int b = 32;\n" 7879 " return 3;\n" 7880 " })\n" 7881 " .foo()\n" 7882 " .bar();\n" 7883 "}"); 7884 verifyFormat("test() {\n" 7885 " ([]() -> {\n" 7886 " int b = 32;\n" 7887 " return 3;\n" 7888 " })\n" 7889 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7890 " \"bbbb\");\n" 7891 "}", 7892 getLLVMStyleWithColumns(30)); 7893 } 7894 7895 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7896 verifyFormat( 7897 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7898 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7899 verifyFormat( 7900 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7901 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7902 7903 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7904 " ccccccccccccccccccccccccc) {\n}"); 7905 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7906 " ccccccccccccccccccccccccc) {\n}"); 7907 7908 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7909 " ccccccccccccccccccccccccc) {\n}"); 7910 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7911 " ccccccccccccccccccccccccc) {\n}"); 7912 7913 verifyFormat( 7914 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7915 " ccccccccccccccccccccccccc) {\n}"); 7916 verifyFormat( 7917 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7918 " ccccccccccccccccccccccccc) {\n}"); 7919 7920 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7921 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7922 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7923 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7924 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7925 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7926 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7927 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7928 7929 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7930 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7931 " aaaaaaaaaaaaaaa != aa) {\n}"); 7932 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7933 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7934 " aaaaaaaaaaaaaaa != aa) {\n}"); 7935 } 7936 7937 TEST_F(FormatTest, BreaksAfterAssignments) { 7938 verifyFormat( 7939 "unsigned Cost =\n" 7940 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7941 " SI->getPointerAddressSpaceee());\n"); 7942 verifyFormat( 7943 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7944 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7945 7946 verifyFormat( 7947 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7948 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7949 verifyFormat("unsigned OriginalStartColumn =\n" 7950 " SourceMgr.getSpellingColumnNumber(\n" 7951 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7952 " 1;"); 7953 } 7954 7955 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7956 FormatStyle Style = getLLVMStyle(); 7957 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7958 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7959 Style); 7960 7961 Style.PenaltyBreakAssignment = 20; 7962 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7963 " cccccccccccccccccccccccccc;", 7964 Style); 7965 } 7966 7967 TEST_F(FormatTest, AlignsAfterAssignments) { 7968 verifyFormat( 7969 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7970 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7971 verifyFormat( 7972 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7973 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7974 verifyFormat( 7975 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7976 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7977 verifyFormat( 7978 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7979 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7980 verifyFormat( 7981 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7982 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7983 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7984 } 7985 7986 TEST_F(FormatTest, AlignsAfterReturn) { 7987 verifyFormat( 7988 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7989 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7990 verifyFormat( 7991 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7992 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7993 verifyFormat( 7994 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7995 " aaaaaaaaaaaaaaaaaaaaaa();"); 7996 verifyFormat( 7997 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7998 " aaaaaaaaaaaaaaaaaaaaaa());"); 7999 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8001 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 8003 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8004 verifyFormat("return\n" 8005 " // true if code is one of a or b.\n" 8006 " code == a || code == b;"); 8007 } 8008 8009 TEST_F(FormatTest, AlignsAfterOpenBracket) { 8010 verifyFormat( 8011 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 8012 " aaaaaaaaa aaaaaaa) {}"); 8013 verifyFormat( 8014 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 8015 " aaaaaaaaaaa aaaaaaaaa);"); 8016 verifyFormat( 8017 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 8018 " aaaaaaaaaaaaaaaaaaaaa));"); 8019 FormatStyle Style = getLLVMStyle(); 8020 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8021 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8022 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 8023 Style); 8024 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 8025 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 8026 Style); 8027 verifyFormat("SomeLongVariableName->someFunction(\n" 8028 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 8029 Style); 8030 verifyFormat( 8031 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 8032 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8033 Style); 8034 verifyFormat( 8035 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 8036 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8037 Style); 8038 verifyFormat( 8039 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 8040 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 8041 Style); 8042 8043 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 8044 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 8045 " b));", 8046 Style); 8047 8048 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8049 Style.BinPackArguments = false; 8050 Style.BinPackParameters = false; 8051 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8052 " aaaaaaaaaaa aaaaaaaa,\n" 8053 " aaaaaaaaa aaaaaaa,\n" 8054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8055 Style); 8056 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 8057 " aaaaaaaaaaa aaaaaaaaa,\n" 8058 " aaaaaaaaaaa aaaaaaaaa,\n" 8059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8060 Style); 8061 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 8062 " aaaaaaaaaaaaaaa,\n" 8063 " aaaaaaaaaaaaaaaaaaaaa,\n" 8064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 8065 Style); 8066 verifyFormat( 8067 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 8068 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 8069 Style); 8070 verifyFormat( 8071 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 8072 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 8073 Style); 8074 verifyFormat( 8075 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 8076 " aaaaaaaaaaaaaaaaaaaaa(\n" 8077 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 8078 " aaaaaaaaaaaaaaaa);", 8079 Style); 8080 verifyFormat( 8081 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 8082 " aaaaaaaaaaaaaaaaaaaaa(\n" 8083 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 8084 " aaaaaaaaaaaaaaaa);", 8085 Style); 8086 } 8087 8088 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 8089 FormatStyle Style = getLLVMStyleWithColumns(40); 8090 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 8091 " bbbbbbbbbbbbbbbbbbbbbb);", 8092 Style); 8093 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 8094 Style.AlignOperands = FormatStyle::OAS_DontAlign; 8095 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 8096 " bbbbbbbbbbbbbbbbbbbbbb);", 8097 Style); 8098 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8099 Style.AlignOperands = FormatStyle::OAS_Align; 8100 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 8101 " bbbbbbbbbbbbbbbbbbbbbb);", 8102 Style); 8103 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8104 Style.AlignOperands = FormatStyle::OAS_DontAlign; 8105 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 8106 " bbbbbbbbbbbbbbbbbbbbbb);", 8107 Style); 8108 } 8109 8110 TEST_F(FormatTest, BreaksConditionalExpressions) { 8111 verifyFormat( 8112 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8113 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8114 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8115 verifyFormat( 8116 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 8117 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8118 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8119 verifyFormat( 8120 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8121 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8122 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 8123 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8124 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8125 verifyFormat( 8126 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 8127 " : aaaaaaaaaaaaa);"); 8128 verifyFormat( 8129 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8130 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8131 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8132 " aaaaaaaaaaaaa);"); 8133 verifyFormat( 8134 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8135 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8136 " aaaaaaaaaaaaa);"); 8137 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8138 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8140 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8142 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8143 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8144 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8146 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8148 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8149 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8151 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8153 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8154 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8155 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8156 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8157 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 8158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8159 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8160 " : aaaaaaaaaaaaaaaa;"); 8161 verifyFormat( 8162 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8163 " ? aaaaaaaaaaaaaaa\n" 8164 " : aaaaaaaaaaaaaaa;"); 8165 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 8166 " aaaaaaaaa\n" 8167 " ? b\n" 8168 " : c);"); 8169 verifyFormat("return aaaa == bbbb\n" 8170 " // comment\n" 8171 " ? aaaa\n" 8172 " : bbbb;"); 8173 verifyFormat("unsigned Indent =\n" 8174 " format(TheLine.First,\n" 8175 " IndentForLevel[TheLine.Level] >= 0\n" 8176 " ? IndentForLevel[TheLine.Level]\n" 8177 " : TheLine * 2,\n" 8178 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 8179 getLLVMStyleWithColumns(60)); 8180 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 8181 " ? aaaaaaaaaaaaaaa\n" 8182 " : bbbbbbbbbbbbbbb //\n" 8183 " ? ccccccccccccccc\n" 8184 " : ddddddddddddddd;"); 8185 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 8186 " ? aaaaaaaaaaaaaaa\n" 8187 " : (bbbbbbbbbbbbbbb //\n" 8188 " ? ccccccccccccccc\n" 8189 " : ddddddddddddddd);"); 8190 verifyFormat( 8191 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8192 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 8193 " aaaaaaaaaaaaaaaaaaaaa +\n" 8194 " aaaaaaaaaaaaaaaaaaaaa\n" 8195 " : aaaaaaaaaa;"); 8196 verifyFormat( 8197 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8198 " : aaaaaaaaaaaaaaaaaaaaaa\n" 8199 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8200 8201 FormatStyle NoBinPacking = getLLVMStyle(); 8202 NoBinPacking.BinPackArguments = false; 8203 verifyFormat( 8204 "void f() {\n" 8205 " g(aaa,\n" 8206 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 8207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8208 " ? aaaaaaaaaaaaaaa\n" 8209 " : aaaaaaaaaaaaaaa);\n" 8210 "}", 8211 NoBinPacking); 8212 verifyFormat( 8213 "void f() {\n" 8214 " g(aaa,\n" 8215 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 8216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8217 " ?: aaaaaaaaaaaaaaa);\n" 8218 "}", 8219 NoBinPacking); 8220 8221 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 8222 " // comment.\n" 8223 " ccccccccccccccccccccccccccccccccccccccc\n" 8224 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8225 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 8226 8227 // Assignments in conditional expressions. Apparently not uncommon :-(. 8228 verifyFormat("return a != b\n" 8229 " // comment\n" 8230 " ? a = b\n" 8231 " : a = b;"); 8232 verifyFormat("return a != b\n" 8233 " // comment\n" 8234 " ? a = a != b\n" 8235 " // comment\n" 8236 " ? a = b\n" 8237 " : a\n" 8238 " : a;\n"); 8239 verifyFormat("return a != b\n" 8240 " // comment\n" 8241 " ? a\n" 8242 " : a = a != b\n" 8243 " // comment\n" 8244 " ? a = b\n" 8245 " : a;"); 8246 8247 // Chained conditionals 8248 FormatStyle Style = getLLVMStyleWithColumns(70); 8249 Style.AlignOperands = FormatStyle::OAS_Align; 8250 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8251 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8252 " : 3333333333333333;", 8253 Style); 8254 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8255 " : bbbbbbbbbb ? 2222222222222222\n" 8256 " : 3333333333333333;", 8257 Style); 8258 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 8259 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 8260 " : 3333333333333333;", 8261 Style); 8262 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8263 " : bbbbbbbbbbbbbb ? 222222\n" 8264 " : 333333;", 8265 Style); 8266 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8267 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8268 " : cccccccccccccc ? 3333333333333333\n" 8269 " : 4444444444444444;", 8270 Style); 8271 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 8272 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8273 " : 3333333333333333;", 8274 Style); 8275 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8276 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8277 " : (aaa ? bbb : ccc);", 8278 Style); 8279 verifyFormat( 8280 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8281 " : cccccccccccccccccc)\n" 8282 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8283 " : 3333333333333333;", 8284 Style); 8285 verifyFormat( 8286 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8287 " : cccccccccccccccccc)\n" 8288 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8289 " : 3333333333333333;", 8290 Style); 8291 verifyFormat( 8292 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8293 " : dddddddddddddddddd)\n" 8294 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8295 " : 3333333333333333;", 8296 Style); 8297 verifyFormat( 8298 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8299 " : dddddddddddddddddd)\n" 8300 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8301 " : 3333333333333333;", 8302 Style); 8303 verifyFormat( 8304 "return aaaaaaaaa ? 1111111111111111\n" 8305 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8306 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8307 " : dddddddddddddddddd)\n", 8308 Style); 8309 verifyFormat( 8310 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8311 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8312 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8313 " : cccccccccccccccccc);", 8314 Style); 8315 verifyFormat( 8316 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8317 " : ccccccccccccccc ? dddddddddddddddddd\n" 8318 " : eeeeeeeeeeeeeeeeee)\n" 8319 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8320 " : 3333333333333333;", 8321 Style); 8322 verifyFormat( 8323 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8324 " : ccccccccccccccc ? dddddddddddddddddd\n" 8325 " : eeeeeeeeeeeeeeeeee)\n" 8326 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8327 " : 3333333333333333;", 8328 Style); 8329 verifyFormat( 8330 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8331 " : cccccccccccc ? dddddddddddddddddd\n" 8332 " : eeeeeeeeeeeeeeeeee)\n" 8333 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8334 " : 3333333333333333;", 8335 Style); 8336 verifyFormat( 8337 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8338 " : cccccccccccccccccc\n" 8339 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8340 " : 3333333333333333;", 8341 Style); 8342 verifyFormat( 8343 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8344 " : cccccccccccccccc ? dddddddddddddddddd\n" 8345 " : eeeeeeeeeeeeeeeeee\n" 8346 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8347 " : 3333333333333333;", 8348 Style); 8349 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 8350 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8351 " : cccccccccccccccccc ? dddddddddddddddddd\n" 8352 " : eeeeeeeeeeeeeeeeee)\n" 8353 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 8354 " : 3333333333333333;", 8355 Style); 8356 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 8357 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8358 " : cccccccccccccccc ? dddddddddddddddddd\n" 8359 " : eeeeeeeeeeeeeeeeee\n" 8360 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 8361 " : 3333333333333333;", 8362 Style); 8363 8364 Style.AlignOperands = FormatStyle::OAS_DontAlign; 8365 Style.BreakBeforeTernaryOperators = false; 8366 // FIXME: Aligning the question marks is weird given DontAlign. 8367 // Consider disabling this alignment in this case. Also check whether this 8368 // will render the adjustment from https://reviews.llvm.org/D82199 8369 // unnecessary. 8370 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 8371 " bbbb ? cccccccccccccccccc :\n" 8372 " ddddd;\n", 8373 Style); 8374 8375 EXPECT_EQ( 8376 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8377 " /*\n" 8378 " */\n" 8379 " function() {\n" 8380 " try {\n" 8381 " return JJJJJJJJJJJJJJ(\n" 8382 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8383 " }\n" 8384 " } :\n" 8385 " function() {};", 8386 format( 8387 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8388 " /*\n" 8389 " */\n" 8390 " function() {\n" 8391 " try {\n" 8392 " return JJJJJJJJJJJJJJ(\n" 8393 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8394 " }\n" 8395 " } :\n" 8396 " function() {};", 8397 getGoogleStyle(FormatStyle::LK_JavaScript))); 8398 } 8399 8400 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 8401 FormatStyle Style = getLLVMStyleWithColumns(70); 8402 Style.BreakBeforeTernaryOperators = false; 8403 verifyFormat( 8404 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8407 Style); 8408 verifyFormat( 8409 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 8410 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8412 Style); 8413 verifyFormat( 8414 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8416 Style); 8417 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 8418 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8420 Style); 8421 verifyFormat( 8422 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 8423 " aaaaaaaaaaaaa);", 8424 Style); 8425 verifyFormat( 8426 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8427 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8429 " aaaaaaaaaaaaa);", 8430 Style); 8431 verifyFormat( 8432 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8433 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8434 " aaaaaaaaaaaaa);", 8435 Style); 8436 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8441 Style); 8442 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8448 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8449 Style); 8450 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 8452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8454 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8455 Style); 8456 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8457 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8458 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8459 Style); 8460 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 8461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8464 Style); 8465 verifyFormat( 8466 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8467 " aaaaaaaaaaaaaaa :\n" 8468 " aaaaaaaaaaaaaaa;", 8469 Style); 8470 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 8471 " aaaaaaaaa ?\n" 8472 " b :\n" 8473 " c);", 8474 Style); 8475 verifyFormat("unsigned Indent =\n" 8476 " format(TheLine.First,\n" 8477 " IndentForLevel[TheLine.Level] >= 0 ?\n" 8478 " IndentForLevel[TheLine.Level] :\n" 8479 " TheLine * 2,\n" 8480 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 8481 Style); 8482 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8483 " aaaaaaaaaaaaaaa :\n" 8484 " bbbbbbbbbbbbbbb ? //\n" 8485 " ccccccccccccccc :\n" 8486 " ddddddddddddddd;", 8487 Style); 8488 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8489 " aaaaaaaaaaaaaaa :\n" 8490 " (bbbbbbbbbbbbbbb ? //\n" 8491 " ccccccccccccccc :\n" 8492 " ddddddddddddddd);", 8493 Style); 8494 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8495 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 8496 " ccccccccccccccccccccccccccc;", 8497 Style); 8498 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8499 " aaaaa :\n" 8500 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 8501 Style); 8502 8503 // Chained conditionals 8504 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8505 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8506 " 3333333333333333;", 8507 Style); 8508 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8509 " bbbbbbbbbb ? 2222222222222222 :\n" 8510 " 3333333333333333;", 8511 Style); 8512 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 8513 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8514 " 3333333333333333;", 8515 Style); 8516 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8517 " bbbbbbbbbbbbbbbb ? 222222 :\n" 8518 " 333333;", 8519 Style); 8520 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8521 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8522 " cccccccccccccccc ? 3333333333333333 :\n" 8523 " 4444444444444444;", 8524 Style); 8525 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 8526 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8527 " 3333333333333333;", 8528 Style); 8529 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8530 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8531 " (aaa ? bbb : ccc);", 8532 Style); 8533 verifyFormat( 8534 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8535 " cccccccccccccccccc) :\n" 8536 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8537 " 3333333333333333;", 8538 Style); 8539 verifyFormat( 8540 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8541 " cccccccccccccccccc) :\n" 8542 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8543 " 3333333333333333;", 8544 Style); 8545 verifyFormat( 8546 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8547 " dddddddddddddddddd) :\n" 8548 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8549 " 3333333333333333;", 8550 Style); 8551 verifyFormat( 8552 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8553 " dddddddddddddddddd) :\n" 8554 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8555 " 3333333333333333;", 8556 Style); 8557 verifyFormat( 8558 "return aaaaaaaaa ? 1111111111111111 :\n" 8559 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8560 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8561 " dddddddddddddddddd)\n", 8562 Style); 8563 verifyFormat( 8564 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8565 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8566 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8567 " cccccccccccccccccc);", 8568 Style); 8569 verifyFormat( 8570 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8571 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8572 " eeeeeeeeeeeeeeeeee) :\n" 8573 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8574 " 3333333333333333;", 8575 Style); 8576 verifyFormat( 8577 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8578 " ccccccccccccc ? dddddddddddddddddd :\n" 8579 " eeeeeeeeeeeeeeeeee) :\n" 8580 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8581 " 3333333333333333;", 8582 Style); 8583 verifyFormat( 8584 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8585 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8586 " eeeeeeeeeeeeeeeeee) :\n" 8587 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8588 " 3333333333333333;", 8589 Style); 8590 verifyFormat( 8591 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8592 " cccccccccccccccccc :\n" 8593 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8594 " 3333333333333333;", 8595 Style); 8596 verifyFormat( 8597 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8598 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8599 " eeeeeeeeeeeeeeeeee :\n" 8600 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8601 " 3333333333333333;", 8602 Style); 8603 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8604 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8605 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8606 " eeeeeeeeeeeeeeeeee) :\n" 8607 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8608 " 3333333333333333;", 8609 Style); 8610 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8611 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8612 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 8613 " eeeeeeeeeeeeeeeeee :\n" 8614 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8615 " 3333333333333333;", 8616 Style); 8617 } 8618 8619 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 8620 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 8621 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 8622 verifyFormat("bool a = true, b = false;"); 8623 8624 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 8626 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 8627 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 8628 verifyFormat( 8629 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 8630 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 8631 " d = e && f;"); 8632 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 8633 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 8634 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8635 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 8636 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 8637 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 8638 8639 FormatStyle Style = getGoogleStyle(); 8640 Style.PointerAlignment = FormatStyle::PAS_Left; 8641 Style.DerivePointerAlignment = false; 8642 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8643 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 8644 " *b = bbbbbbbbbbbbbbbbbbb;", 8645 Style); 8646 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8647 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 8648 Style); 8649 verifyFormat("vector<int*> a, b;", Style); 8650 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 8651 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style); 8652 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style); 8653 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}", 8654 Style); 8655 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}", 8656 Style); 8657 verifyFormat( 8658 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}", 8659 Style); 8660 8661 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style); 8662 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style); 8663 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style); 8664 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style); 8665 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}", 8666 Style); 8667 } 8668 8669 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 8670 verifyFormat("arr[foo ? bar : baz];"); 8671 verifyFormat("f()[foo ? bar : baz];"); 8672 verifyFormat("(a + b)[foo ? bar : baz];"); 8673 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 8674 } 8675 8676 TEST_F(FormatTest, AlignsStringLiterals) { 8677 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 8678 " \"short literal\");"); 8679 verifyFormat( 8680 "looooooooooooooooooooooooongFunction(\n" 8681 " \"short literal\"\n" 8682 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 8683 verifyFormat("someFunction(\"Always break between multi-line\"\n" 8684 " \" string literals\",\n" 8685 " and, other, parameters);"); 8686 EXPECT_EQ("fun + \"1243\" /* comment */\n" 8687 " \"5678\";", 8688 format("fun + \"1243\" /* comment */\n" 8689 " \"5678\";", 8690 getLLVMStyleWithColumns(28))); 8691 EXPECT_EQ( 8692 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8693 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 8694 " \"aaaaaaaaaaaaaaaa\";", 8695 format("aaaaaa =" 8696 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8697 "aaaaaaaaaaaaaaaaaaaaa\" " 8698 "\"aaaaaaaaaaaaaaaa\";")); 8699 verifyFormat("a = a + \"a\"\n" 8700 " \"a\"\n" 8701 " \"a\";"); 8702 verifyFormat("f(\"a\", \"b\"\n" 8703 " \"c\");"); 8704 8705 verifyFormat( 8706 "#define LL_FORMAT \"ll\"\n" 8707 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 8708 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 8709 8710 verifyFormat("#define A(X) \\\n" 8711 " \"aaaaa\" #X \"bbbbbb\" \\\n" 8712 " \"ccccc\"", 8713 getLLVMStyleWithColumns(23)); 8714 verifyFormat("#define A \"def\"\n" 8715 "f(\"abc\" A \"ghi\"\n" 8716 " \"jkl\");"); 8717 8718 verifyFormat("f(L\"a\"\n" 8719 " L\"b\");"); 8720 verifyFormat("#define A(X) \\\n" 8721 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 8722 " L\"ccccc\"", 8723 getLLVMStyleWithColumns(25)); 8724 8725 verifyFormat("f(@\"a\"\n" 8726 " @\"b\");"); 8727 verifyFormat("NSString s = @\"a\"\n" 8728 " @\"b\"\n" 8729 " @\"c\";"); 8730 verifyFormat("NSString s = @\"a\"\n" 8731 " \"b\"\n" 8732 " \"c\";"); 8733 } 8734 8735 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8736 FormatStyle Style = getLLVMStyle(); 8737 // No declarations or definitions should be moved to own line. 8738 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8739 verifyFormat("class A {\n" 8740 " int f() { return 1; }\n" 8741 " int g();\n" 8742 "};\n" 8743 "int f() { return 1; }\n" 8744 "int g();\n", 8745 Style); 8746 8747 // All declarations and definitions should have the return type moved to its 8748 // own line. 8749 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8750 Style.TypenameMacros = {"LIST"}; 8751 verifyFormat("SomeType\n" 8752 "funcdecl(LIST(uint64_t));", 8753 Style); 8754 verifyFormat("class E {\n" 8755 " int\n" 8756 " f() {\n" 8757 " return 1;\n" 8758 " }\n" 8759 " int\n" 8760 " g();\n" 8761 "};\n" 8762 "int\n" 8763 "f() {\n" 8764 " return 1;\n" 8765 "}\n" 8766 "int\n" 8767 "g();\n", 8768 Style); 8769 8770 // Top-level definitions, and no kinds of declarations should have the 8771 // return type moved to its own line. 8772 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8773 verifyFormat("class B {\n" 8774 " int f() { return 1; }\n" 8775 " int g();\n" 8776 "};\n" 8777 "int\n" 8778 "f() {\n" 8779 " return 1;\n" 8780 "}\n" 8781 "int g();\n", 8782 Style); 8783 8784 // Top-level definitions and declarations should have the return type moved 8785 // to its own line. 8786 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8787 verifyFormat("class C {\n" 8788 " int f() { return 1; }\n" 8789 " int g();\n" 8790 "};\n" 8791 "int\n" 8792 "f() {\n" 8793 " return 1;\n" 8794 "}\n" 8795 "int\n" 8796 "g();\n", 8797 Style); 8798 8799 // All definitions should have the return type moved to its own line, but no 8800 // kinds of declarations. 8801 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8802 verifyFormat("class D {\n" 8803 " int\n" 8804 " f() {\n" 8805 " return 1;\n" 8806 " }\n" 8807 " int g();\n" 8808 "};\n" 8809 "int\n" 8810 "f() {\n" 8811 " return 1;\n" 8812 "}\n" 8813 "int g();\n", 8814 Style); 8815 verifyFormat("const char *\n" 8816 "f(void) {\n" // Break here. 8817 " return \"\";\n" 8818 "}\n" 8819 "const char *bar(void);\n", // No break here. 8820 Style); 8821 verifyFormat("template <class T>\n" 8822 "T *\n" 8823 "f(T &c) {\n" // Break here. 8824 " return NULL;\n" 8825 "}\n" 8826 "template <class T> T *f(T &c);\n", // No break here. 8827 Style); 8828 verifyFormat("class C {\n" 8829 " int\n" 8830 " operator+() {\n" 8831 " return 1;\n" 8832 " }\n" 8833 " int\n" 8834 " operator()() {\n" 8835 " return 1;\n" 8836 " }\n" 8837 "};\n", 8838 Style); 8839 verifyFormat("void\n" 8840 "A::operator()() {}\n" 8841 "void\n" 8842 "A::operator>>() {}\n" 8843 "void\n" 8844 "A::operator+() {}\n" 8845 "void\n" 8846 "A::operator*() {}\n" 8847 "void\n" 8848 "A::operator->() {}\n" 8849 "void\n" 8850 "A::operator void *() {}\n" 8851 "void\n" 8852 "A::operator void &() {}\n" 8853 "void\n" 8854 "A::operator void &&() {}\n" 8855 "void\n" 8856 "A::operator char *() {}\n" 8857 "void\n" 8858 "A::operator[]() {}\n" 8859 "void\n" 8860 "A::operator!() {}\n" 8861 "void\n" 8862 "A::operator**() {}\n" 8863 "void\n" 8864 "A::operator<Foo> *() {}\n" 8865 "void\n" 8866 "A::operator<Foo> **() {}\n" 8867 "void\n" 8868 "A::operator<Foo> &() {}\n" 8869 "void\n" 8870 "A::operator void **() {}\n", 8871 Style); 8872 verifyFormat("constexpr auto\n" 8873 "operator()() const -> reference {}\n" 8874 "constexpr auto\n" 8875 "operator>>() const -> reference {}\n" 8876 "constexpr auto\n" 8877 "operator+() const -> reference {}\n" 8878 "constexpr auto\n" 8879 "operator*() const -> reference {}\n" 8880 "constexpr auto\n" 8881 "operator->() const -> reference {}\n" 8882 "constexpr auto\n" 8883 "operator++() const -> reference {}\n" 8884 "constexpr auto\n" 8885 "operator void *() const -> reference {}\n" 8886 "constexpr auto\n" 8887 "operator void **() const -> reference {}\n" 8888 "constexpr auto\n" 8889 "operator void *() const -> reference {}\n" 8890 "constexpr auto\n" 8891 "operator void &() const -> reference {}\n" 8892 "constexpr auto\n" 8893 "operator void &&() const -> reference {}\n" 8894 "constexpr auto\n" 8895 "operator char *() const -> reference {}\n" 8896 "constexpr auto\n" 8897 "operator!() const -> reference {}\n" 8898 "constexpr auto\n" 8899 "operator[]() const -> reference {}\n", 8900 Style); 8901 verifyFormat("void *operator new(std::size_t s);", // No break here. 8902 Style); 8903 verifyFormat("void *\n" 8904 "operator new(std::size_t s) {}", 8905 Style); 8906 verifyFormat("void *\n" 8907 "operator delete[](void *ptr) {}", 8908 Style); 8909 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8910 verifyFormat("const char *\n" 8911 "f(void)\n" // Break here. 8912 "{\n" 8913 " return \"\";\n" 8914 "}\n" 8915 "const char *bar(void);\n", // No break here. 8916 Style); 8917 verifyFormat("template <class T>\n" 8918 "T *\n" // Problem here: no line break 8919 "f(T &c)\n" // Break here. 8920 "{\n" 8921 " return NULL;\n" 8922 "}\n" 8923 "template <class T> T *f(T &c);\n", // No break here. 8924 Style); 8925 verifyFormat("int\n" 8926 "foo(A<bool> a)\n" 8927 "{\n" 8928 " return a;\n" 8929 "}\n", 8930 Style); 8931 verifyFormat("int\n" 8932 "foo(A<8> a)\n" 8933 "{\n" 8934 " return a;\n" 8935 "}\n", 8936 Style); 8937 verifyFormat("int\n" 8938 "foo(A<B<bool>, 8> a)\n" 8939 "{\n" 8940 " return a;\n" 8941 "}\n", 8942 Style); 8943 verifyFormat("int\n" 8944 "foo(A<B<8>, bool> a)\n" 8945 "{\n" 8946 " return a;\n" 8947 "}\n", 8948 Style); 8949 verifyFormat("int\n" 8950 "foo(A<B<bool>, bool> a)\n" 8951 "{\n" 8952 " return a;\n" 8953 "}\n", 8954 Style); 8955 verifyFormat("int\n" 8956 "foo(A<B<8>, 8> a)\n" 8957 "{\n" 8958 " return a;\n" 8959 "}\n", 8960 Style); 8961 8962 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8963 Style.BraceWrapping.AfterFunction = true; 8964 verifyFormat("int f(i);\n" // No break here. 8965 "int\n" // Break here. 8966 "f(i)\n" 8967 "{\n" 8968 " return i + 1;\n" 8969 "}\n" 8970 "int\n" // Break here. 8971 "f(i)\n" 8972 "{\n" 8973 " return i + 1;\n" 8974 "};", 8975 Style); 8976 verifyFormat("int f(a, b, c);\n" // No break here. 8977 "int\n" // Break here. 8978 "f(a, b, c)\n" // Break here. 8979 "short a, b;\n" 8980 "float c;\n" 8981 "{\n" 8982 " return a + b < c;\n" 8983 "}\n" 8984 "int\n" // Break here. 8985 "f(a, b, c)\n" // Break here. 8986 "short a, b;\n" 8987 "float c;\n" 8988 "{\n" 8989 " return a + b < c;\n" 8990 "};", 8991 Style); 8992 verifyFormat("byte *\n" // Break here. 8993 "f(a)\n" // Break here. 8994 "byte a[];\n" 8995 "{\n" 8996 " return a;\n" 8997 "}", 8998 Style); 8999 verifyFormat("bool f(int a, int) override;\n" 9000 "Bar g(int a, Bar) final;\n" 9001 "Bar h(a, Bar) final;", 9002 Style); 9003 verifyFormat("int\n" 9004 "f(a)", 9005 Style); 9006 verifyFormat("bool\n" 9007 "f(size_t = 0, bool b = false)\n" 9008 "{\n" 9009 " return !b;\n" 9010 "}", 9011 Style); 9012 9013 // The return breaking style doesn't affect: 9014 // * function and object definitions with attribute-like macros 9015 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 9016 " ABSL_GUARDED_BY(mutex) = {};", 9017 getGoogleStyleWithColumns(40)); 9018 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 9019 " ABSL_GUARDED_BY(mutex); // comment", 9020 getGoogleStyleWithColumns(40)); 9021 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 9022 " ABSL_GUARDED_BY(mutex1)\n" 9023 " ABSL_GUARDED_BY(mutex2);", 9024 getGoogleStyleWithColumns(40)); 9025 verifyFormat("Tttttt f(int a, int b)\n" 9026 " ABSL_GUARDED_BY(mutex1)\n" 9027 " ABSL_GUARDED_BY(mutex2);", 9028 getGoogleStyleWithColumns(40)); 9029 // * typedefs 9030 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 9031 9032 Style = getGNUStyle(); 9033 9034 // Test for comments at the end of function declarations. 9035 verifyFormat("void\n" 9036 "foo (int a, /*abc*/ int b) // def\n" 9037 "{\n" 9038 "}\n", 9039 Style); 9040 9041 verifyFormat("void\n" 9042 "foo (int a, /* abc */ int b) /* def */\n" 9043 "{\n" 9044 "}\n", 9045 Style); 9046 9047 // Definitions that should not break after return type 9048 verifyFormat("void foo (int a, int b); // def\n", Style); 9049 verifyFormat("void foo (int a, int b); /* def */\n", Style); 9050 verifyFormat("void foo (int a, int b);\n", Style); 9051 } 9052 9053 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 9054 FormatStyle NoBreak = getLLVMStyle(); 9055 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 9056 FormatStyle Break = getLLVMStyle(); 9057 Break.AlwaysBreakBeforeMultilineStrings = true; 9058 verifyFormat("aaaa = \"bbbb\"\n" 9059 " \"cccc\";", 9060 NoBreak); 9061 verifyFormat("aaaa =\n" 9062 " \"bbbb\"\n" 9063 " \"cccc\";", 9064 Break); 9065 verifyFormat("aaaa(\"bbbb\"\n" 9066 " \"cccc\");", 9067 NoBreak); 9068 verifyFormat("aaaa(\n" 9069 " \"bbbb\"\n" 9070 " \"cccc\");", 9071 Break); 9072 verifyFormat("aaaa(qqq, \"bbbb\"\n" 9073 " \"cccc\");", 9074 NoBreak); 9075 verifyFormat("aaaa(qqq,\n" 9076 " \"bbbb\"\n" 9077 " \"cccc\");", 9078 Break); 9079 verifyFormat("aaaa(qqq,\n" 9080 " L\"bbbb\"\n" 9081 " L\"cccc\");", 9082 Break); 9083 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 9084 " \"bbbb\"));", 9085 Break); 9086 verifyFormat("string s = someFunction(\n" 9087 " \"abc\"\n" 9088 " \"abc\");", 9089 Break); 9090 9091 // As we break before unary operators, breaking right after them is bad. 9092 verifyFormat("string foo = abc ? \"x\"\n" 9093 " \"blah blah blah blah blah blah\"\n" 9094 " : \"y\";", 9095 Break); 9096 9097 // Don't break if there is no column gain. 9098 verifyFormat("f(\"aaaa\"\n" 9099 " \"bbbb\");", 9100 Break); 9101 9102 // Treat literals with escaped newlines like multi-line string literals. 9103 EXPECT_EQ("x = \"a\\\n" 9104 "b\\\n" 9105 "c\";", 9106 format("x = \"a\\\n" 9107 "b\\\n" 9108 "c\";", 9109 NoBreak)); 9110 EXPECT_EQ("xxxx =\n" 9111 " \"a\\\n" 9112 "b\\\n" 9113 "c\";", 9114 format("xxxx = \"a\\\n" 9115 "b\\\n" 9116 "c\";", 9117 Break)); 9118 9119 EXPECT_EQ("NSString *const kString =\n" 9120 " @\"aaaa\"\n" 9121 " @\"bbbb\";", 9122 format("NSString *const kString = @\"aaaa\"\n" 9123 "@\"bbbb\";", 9124 Break)); 9125 9126 Break.ColumnLimit = 0; 9127 verifyFormat("const char *hello = \"hello llvm\";", Break); 9128 } 9129 9130 TEST_F(FormatTest, AlignsPipes) { 9131 verifyFormat( 9132 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9133 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9134 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9135 verifyFormat( 9136 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 9137 " << aaaaaaaaaaaaaaaaaaaa;"); 9138 verifyFormat( 9139 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9140 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9141 verifyFormat( 9142 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9143 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9144 verifyFormat( 9145 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 9146 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 9147 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 9148 verifyFormat( 9149 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9150 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9151 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9152 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9155 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 9156 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 9157 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 9158 verifyFormat( 9159 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9161 verifyFormat( 9162 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 9163 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9164 9165 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 9166 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 9167 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9169 " aaaaaaaaaaaaaaaaaaaaa)\n" 9170 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9171 verifyFormat("LOG_IF(aaa == //\n" 9172 " bbb)\n" 9173 " << a << b;"); 9174 9175 // But sometimes, breaking before the first "<<" is desirable. 9176 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 9177 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 9178 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 9179 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9180 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9181 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 9182 " << BEF << IsTemplate << Description << E->getType();"); 9183 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 9184 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9185 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9186 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 9187 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9188 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9189 " << aaa;"); 9190 9191 verifyFormat( 9192 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9193 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9194 9195 // Incomplete string literal. 9196 EXPECT_EQ("llvm::errs() << \"\n" 9197 " << a;", 9198 format("llvm::errs() << \"\n<<a;")); 9199 9200 verifyFormat("void f() {\n" 9201 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 9202 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 9203 "}"); 9204 9205 // Handle 'endl'. 9206 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 9207 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 9208 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 9209 9210 // Handle '\n'. 9211 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 9212 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 9213 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 9214 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 9215 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 9216 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 9217 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 9218 } 9219 9220 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 9221 verifyFormat("return out << \"somepacket = {\\n\"\n" 9222 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 9223 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 9224 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 9225 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 9226 " << \"}\";"); 9227 9228 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 9229 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 9230 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 9231 verifyFormat( 9232 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 9233 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 9234 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 9235 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 9236 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 9237 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 9238 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 9239 verifyFormat( 9240 "void f() {\n" 9241 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 9242 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 9243 "}"); 9244 9245 // Breaking before the first "<<" is generally not desirable. 9246 verifyFormat( 9247 "llvm::errs()\n" 9248 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9249 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9250 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9251 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 9252 getLLVMStyleWithColumns(70)); 9253 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 9254 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9255 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 9256 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9257 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 9258 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 9259 getLLVMStyleWithColumns(70)); 9260 9261 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 9262 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 9263 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 9264 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 9265 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 9266 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 9267 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 9268 " (aaaa + aaaa);", 9269 getLLVMStyleWithColumns(40)); 9270 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 9271 " (aaaaaaa + aaaaa));", 9272 getLLVMStyleWithColumns(40)); 9273 verifyFormat( 9274 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 9275 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 9276 " bbbbbbbbbbbbbbbbbbbbbbb);"); 9277 } 9278 9279 TEST_F(FormatTest, UnderstandsEquals) { 9280 verifyFormat( 9281 "aaaaaaaaaaaaaaaaa =\n" 9282 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9283 verifyFormat( 9284 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 9286 verifyFormat( 9287 "if (a) {\n" 9288 " f();\n" 9289 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9290 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 9291 "}"); 9292 9293 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9294 " 100000000 + 10000000) {\n}"); 9295 } 9296 9297 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 9298 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 9299 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 9300 9301 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 9302 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 9303 9304 verifyFormat( 9305 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 9306 " Parameter2);"); 9307 9308 verifyFormat( 9309 "ShortObject->shortFunction(\n" 9310 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 9311 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 9312 9313 verifyFormat("loooooooooooooongFunction(\n" 9314 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 9315 9316 verifyFormat( 9317 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 9318 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 9319 9320 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 9321 " .WillRepeatedly(Return(SomeValue));"); 9322 verifyFormat("void f() {\n" 9323 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 9324 " .Times(2)\n" 9325 " .WillRepeatedly(Return(SomeValue));\n" 9326 "}"); 9327 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 9328 " ccccccccccccccccccccccc);"); 9329 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9330 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9331 " .aaaaa(aaaaa),\n" 9332 " aaaaaaaaaaaaaaaaaaaaa);"); 9333 verifyFormat("void f() {\n" 9334 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9335 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 9336 "}"); 9337 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9338 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9339 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9340 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9341 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9342 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9343 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9344 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9345 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 9346 "}"); 9347 9348 // Here, it is not necessary to wrap at "." or "->". 9349 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 9350 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 9351 verifyFormat( 9352 "aaaaaaaaaaa->aaaaaaaaa(\n" 9353 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9354 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 9355 9356 verifyFormat( 9357 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9358 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 9359 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 9360 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 9361 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 9362 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 9363 9364 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9365 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9366 " .a();"); 9367 9368 FormatStyle NoBinPacking = getLLVMStyle(); 9369 NoBinPacking.BinPackParameters = false; 9370 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 9371 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 9372 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 9373 " aaaaaaaaaaaaaaaaaaa,\n" 9374 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9375 NoBinPacking); 9376 9377 // If there is a subsequent call, change to hanging indentation. 9378 verifyFormat( 9379 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9380 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 9381 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9382 verifyFormat( 9383 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9384 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 9385 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9387 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9388 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9390 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9391 } 9392 9393 TEST_F(FormatTest, WrapsTemplateDeclarations) { 9394 verifyFormat("template <typename T>\n" 9395 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9396 verifyFormat("template <typename T>\n" 9397 "// T should be one of {A, B}.\n" 9398 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9399 verifyFormat( 9400 "template <typename T>\n" 9401 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 9402 verifyFormat("template <typename T>\n" 9403 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 9404 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 9405 verifyFormat( 9406 "template <typename T>\n" 9407 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 9408 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 9409 verifyFormat( 9410 "template <typename T>\n" 9411 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 9412 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 9413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9414 verifyFormat("template <typename T>\n" 9415 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9416 " int aaaaaaaaaaaaaaaaaaaaaa);"); 9417 verifyFormat( 9418 "template <typename T1, typename T2 = char, typename T3 = char,\n" 9419 " typename T4 = char>\n" 9420 "void f();"); 9421 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 9422 " template <typename> class cccccccccccccccccccccc,\n" 9423 " typename ddddddddddddd>\n" 9424 "class C {};"); 9425 verifyFormat( 9426 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 9427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9428 9429 verifyFormat("void f() {\n" 9430 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 9431 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 9432 "}"); 9433 9434 verifyFormat("template <typename T> class C {};"); 9435 verifyFormat("template <typename T> void f();"); 9436 verifyFormat("template <typename T> void f() {}"); 9437 verifyFormat( 9438 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 9441 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 9444 " bbbbbbbbbbbbbbbbbbbbbbbb);", 9445 getLLVMStyleWithColumns(72)); 9446 EXPECT_EQ("static_cast<A< //\n" 9447 " B> *>(\n" 9448 "\n" 9449 ");", 9450 format("static_cast<A<//\n" 9451 " B>*>(\n" 9452 "\n" 9453 " );")); 9454 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9455 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 9456 9457 FormatStyle AlwaysBreak = getLLVMStyle(); 9458 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9459 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 9460 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 9461 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 9462 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9463 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9464 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 9465 verifyFormat("template <template <typename> class Fooooooo,\n" 9466 " template <typename> class Baaaaaaar>\n" 9467 "struct C {};", 9468 AlwaysBreak); 9469 verifyFormat("template <typename T> // T can be A, B or C.\n" 9470 "struct C {};", 9471 AlwaysBreak); 9472 verifyFormat("template <enum E> class A {\n" 9473 "public:\n" 9474 " E *f();\n" 9475 "};"); 9476 9477 FormatStyle NeverBreak = getLLVMStyle(); 9478 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 9479 verifyFormat("template <typename T> class C {};", NeverBreak); 9480 verifyFormat("template <typename T> void f();", NeverBreak); 9481 verifyFormat("template <typename T> void f() {}", NeverBreak); 9482 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9483 "bbbbbbbbbbbbbbbbbbbb) {}", 9484 NeverBreak); 9485 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9486 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9487 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 9488 NeverBreak); 9489 verifyFormat("template <template <typename> class Fooooooo,\n" 9490 " template <typename> class Baaaaaaar>\n" 9491 "struct C {};", 9492 NeverBreak); 9493 verifyFormat("template <typename T> // T can be A, B or C.\n" 9494 "struct C {};", 9495 NeverBreak); 9496 verifyFormat("template <enum E> class A {\n" 9497 "public:\n" 9498 " E *f();\n" 9499 "};", 9500 NeverBreak); 9501 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 9502 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9503 "bbbbbbbbbbbbbbbbbbbb) {}", 9504 NeverBreak); 9505 } 9506 9507 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 9508 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 9509 Style.ColumnLimit = 60; 9510 EXPECT_EQ("// Baseline - no comments.\n" 9511 "template <\n" 9512 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9513 "void f() {}", 9514 format("// Baseline - no comments.\n" 9515 "template <\n" 9516 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9517 "void f() {}", 9518 Style)); 9519 9520 EXPECT_EQ("template <\n" 9521 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9522 "void f() {}", 9523 format("template <\n" 9524 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9525 "void f() {}", 9526 Style)); 9527 9528 EXPECT_EQ( 9529 "template <\n" 9530 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9531 "void f() {}", 9532 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9533 "void f() {}", 9534 Style)); 9535 9536 EXPECT_EQ( 9537 "template <\n" 9538 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9539 " // multiline\n" 9540 "void f() {}", 9541 format("template <\n" 9542 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9543 " // multiline\n" 9544 "void f() {}", 9545 Style)); 9546 9547 EXPECT_EQ( 9548 "template <typename aaaaaaaaaa<\n" 9549 " bbbbbbbbbbbb>::value> // trailing loooong\n" 9550 "void f() {}", 9551 format( 9552 "template <\n" 9553 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 9554 "void f() {}", 9555 Style)); 9556 } 9557 9558 TEST_F(FormatTest, WrapsTemplateParameters) { 9559 FormatStyle Style = getLLVMStyle(); 9560 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9561 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9562 verifyFormat( 9563 "template <typename... a> struct q {};\n" 9564 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9565 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9566 " y;", 9567 Style); 9568 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9569 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9570 verifyFormat( 9571 "template <typename... a> struct r {};\n" 9572 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9573 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9574 " y;", 9575 Style); 9576 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9577 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9578 verifyFormat("template <typename... a> struct s {};\n" 9579 "extern s<\n" 9580 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9581 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9582 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9583 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9584 " y;", 9585 Style); 9586 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9587 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9588 verifyFormat("template <typename... a> struct t {};\n" 9589 "extern t<\n" 9590 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9591 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9592 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9593 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9594 " y;", 9595 Style); 9596 } 9597 9598 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 9599 verifyFormat( 9600 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9601 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9602 verifyFormat( 9603 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9606 9607 // FIXME: Should we have the extra indent after the second break? 9608 verifyFormat( 9609 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9611 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9612 9613 verifyFormat( 9614 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 9615 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 9616 9617 // Breaking at nested name specifiers is generally not desirable. 9618 verifyFormat( 9619 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9620 " aaaaaaaaaaaaaaaaaaaaaaa);"); 9621 9622 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 9623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9625 " aaaaaaaaaaaaaaaaaaaaa);", 9626 getLLVMStyleWithColumns(74)); 9627 9628 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9630 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9631 } 9632 9633 TEST_F(FormatTest, UnderstandsTemplateParameters) { 9634 verifyFormat("A<int> a;"); 9635 verifyFormat("A<A<A<int>>> a;"); 9636 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 9637 verifyFormat("bool x = a < 1 || 2 > a;"); 9638 verifyFormat("bool x = 5 < f<int>();"); 9639 verifyFormat("bool x = f<int>() > 5;"); 9640 verifyFormat("bool x = 5 < a<int>::x;"); 9641 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 9642 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 9643 9644 verifyGoogleFormat("A<A<int>> a;"); 9645 verifyGoogleFormat("A<A<A<int>>> a;"); 9646 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 9647 verifyGoogleFormat("A<A<int> > a;"); 9648 verifyGoogleFormat("A<A<A<int> > > a;"); 9649 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 9650 verifyGoogleFormat("A<::A<int>> a;"); 9651 verifyGoogleFormat("A<::A> a;"); 9652 verifyGoogleFormat("A< ::A> a;"); 9653 verifyGoogleFormat("A< ::A<int> > a;"); 9654 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 9655 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 9656 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 9657 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 9658 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 9659 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 9660 9661 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 9662 9663 // template closer followed by a token that starts with > or = 9664 verifyFormat("bool b = a<1> > 1;"); 9665 verifyFormat("bool b = a<1> >= 1;"); 9666 verifyFormat("int i = a<1> >> 1;"); 9667 FormatStyle Style = getLLVMStyle(); 9668 Style.SpaceBeforeAssignmentOperators = false; 9669 verifyFormat("bool b= a<1> == 1;", Style); 9670 verifyFormat("a<int> = 1;", Style); 9671 verifyFormat("a<int> >>= 1;", Style); 9672 9673 verifyFormat("test < a | b >> c;"); 9674 verifyFormat("test<test<a | b>> c;"); 9675 verifyFormat("test >> a >> b;"); 9676 verifyFormat("test << a >> b;"); 9677 9678 verifyFormat("f<int>();"); 9679 verifyFormat("template <typename T> void f() {}"); 9680 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 9681 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 9682 "sizeof(char)>::type>;"); 9683 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 9684 verifyFormat("f(a.operator()<A>());"); 9685 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9686 " .template operator()<A>());", 9687 getLLVMStyleWithColumns(35)); 9688 verifyFormat("bool_constant<a && noexcept(f())>"); 9689 verifyFormat("bool_constant<a || noexcept(f())>"); 9690 9691 // Not template parameters. 9692 verifyFormat("return a < b && c > d;"); 9693 verifyFormat("void f() {\n" 9694 " while (a < b && c > d) {\n" 9695 " }\n" 9696 "}"); 9697 verifyFormat("template <typename... Types>\n" 9698 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 9699 9700 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9701 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 9702 getLLVMStyleWithColumns(60)); 9703 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 9704 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 9705 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 9706 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 9707 } 9708 9709 TEST_F(FormatTest, UnderstandsShiftOperators) { 9710 verifyFormat("if (i < x >> 1)"); 9711 verifyFormat("while (i < x >> 1)"); 9712 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 9713 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 9714 verifyFormat( 9715 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 9716 verifyFormat("Foo.call<Bar<Function>>()"); 9717 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 9718 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 9719 "++i, v = v >> 1)"); 9720 verifyFormat("if (w<u<v<x>>, 1>::t)"); 9721 } 9722 9723 TEST_F(FormatTest, BitshiftOperatorWidth) { 9724 EXPECT_EQ("int a = 1 << 2; /* foo\n" 9725 " bar */", 9726 format("int a=1<<2; /* foo\n" 9727 " bar */")); 9728 9729 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 9730 " bar */", 9731 format("int b =256>>1 ; /* foo\n" 9732 " bar */")); 9733 } 9734 9735 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9736 verifyFormat("COMPARE(a, ==, b);"); 9737 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9738 } 9739 9740 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9741 verifyFormat("int A::*x;"); 9742 verifyFormat("int (S::*func)(void *);"); 9743 verifyFormat("void f() { int (S::*func)(void *); }"); 9744 verifyFormat("typedef bool *(Class::*Member)() const;"); 9745 verifyFormat("void f() {\n" 9746 " (a->*f)();\n" 9747 " a->*x;\n" 9748 " (a.*f)();\n" 9749 " ((*a).*f)();\n" 9750 " a.*x;\n" 9751 "}"); 9752 verifyFormat("void f() {\n" 9753 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9754 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9755 "}"); 9756 verifyFormat( 9757 "(aaaaaaaaaa->*bbbbbbb)(\n" 9758 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9759 FormatStyle Style = getLLVMStyle(); 9760 Style.PointerAlignment = FormatStyle::PAS_Left; 9761 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9762 } 9763 9764 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9765 verifyFormat("int a = -2;"); 9766 verifyFormat("f(-1, -2, -3);"); 9767 verifyFormat("a[-1] = 5;"); 9768 verifyFormat("int a = 5 + -2;"); 9769 verifyFormat("if (i == -1) {\n}"); 9770 verifyFormat("if (i != -1) {\n}"); 9771 verifyFormat("if (i > -1) {\n}"); 9772 verifyFormat("if (i < -1) {\n}"); 9773 verifyFormat("++(a->f());"); 9774 verifyFormat("--(a->f());"); 9775 verifyFormat("(a->f())++;"); 9776 verifyFormat("a[42]++;"); 9777 verifyFormat("if (!(a->f())) {\n}"); 9778 verifyFormat("if (!+i) {\n}"); 9779 verifyFormat("~&a;"); 9780 verifyFormat("for (x = 0; -10 < x; --x) {\n}"); 9781 verifyFormat("sizeof -x"); 9782 verifyFormat("sizeof +x"); 9783 verifyFormat("sizeof *x"); 9784 verifyFormat("sizeof &x"); 9785 verifyFormat("delete +x;"); 9786 verifyFormat("co_await +x;"); 9787 verifyFormat("case *x:"); 9788 verifyFormat("case &x:"); 9789 9790 verifyFormat("a-- > b;"); 9791 verifyFormat("b ? -a : c;"); 9792 verifyFormat("n * sizeof char16;"); 9793 verifyFormat("n * alignof char16;", getGoogleStyle()); 9794 verifyFormat("sizeof(char);"); 9795 verifyFormat("alignof(char);", getGoogleStyle()); 9796 9797 verifyFormat("return -1;"); 9798 verifyFormat("throw -1;"); 9799 verifyFormat("switch (a) {\n" 9800 "case -1:\n" 9801 " break;\n" 9802 "}"); 9803 verifyFormat("#define X -1"); 9804 verifyFormat("#define X -kConstant"); 9805 9806 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9807 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9808 9809 verifyFormat("int a = /* confusing comment */ -1;"); 9810 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9811 verifyFormat("int a = i /* confusing comment */++;"); 9812 9813 verifyFormat("co_yield -1;"); 9814 verifyFormat("co_return -1;"); 9815 9816 // Check that * is not treated as a binary operator when we set 9817 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9818 FormatStyle PASLeftStyle = getLLVMStyle(); 9819 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9820 verifyFormat("co_return *a;", PASLeftStyle); 9821 verifyFormat("co_await *a;", PASLeftStyle); 9822 verifyFormat("co_yield *a", PASLeftStyle); 9823 verifyFormat("return *a;", PASLeftStyle); 9824 } 9825 9826 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9827 verifyFormat("if (!aaaaaaaaaa( // break\n" 9828 " aaaaa)) {\n" 9829 "}"); 9830 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9831 " aaaaa));"); 9832 verifyFormat("*aaa = aaaaaaa( // break\n" 9833 " bbbbbb);"); 9834 } 9835 9836 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9837 verifyFormat("bool operator<();"); 9838 verifyFormat("bool operator>();"); 9839 verifyFormat("bool operator=();"); 9840 verifyFormat("bool operator==();"); 9841 verifyFormat("bool operator!=();"); 9842 verifyFormat("int operator+();"); 9843 verifyFormat("int operator++();"); 9844 verifyFormat("int operator++(int) volatile noexcept;"); 9845 verifyFormat("bool operator,();"); 9846 verifyFormat("bool operator();"); 9847 verifyFormat("bool operator()();"); 9848 verifyFormat("bool operator[]();"); 9849 verifyFormat("operator bool();"); 9850 verifyFormat("operator int();"); 9851 verifyFormat("operator void *();"); 9852 verifyFormat("operator SomeType<int>();"); 9853 verifyFormat("operator SomeType<int, int>();"); 9854 verifyFormat("operator SomeType<SomeType<int>>();"); 9855 verifyFormat("operator< <>();"); 9856 verifyFormat("operator<< <>();"); 9857 verifyFormat("< <>"); 9858 9859 verifyFormat("void *operator new(std::size_t size);"); 9860 verifyFormat("void *operator new[](std::size_t size);"); 9861 verifyFormat("void operator delete(void *ptr);"); 9862 verifyFormat("void operator delete[](void *ptr);"); 9863 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9864 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9865 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9866 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9867 9868 verifyFormat( 9869 "ostream &operator<<(ostream &OutputStream,\n" 9870 " SomeReallyLongType WithSomeReallyLongValue);"); 9871 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9872 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9873 " return left.group < right.group;\n" 9874 "}"); 9875 verifyFormat("SomeType &operator=(const SomeType &S);"); 9876 verifyFormat("f.template operator()<int>();"); 9877 9878 verifyGoogleFormat("operator void*();"); 9879 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9880 verifyGoogleFormat("operator ::A();"); 9881 9882 verifyFormat("using A::operator+;"); 9883 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9884 "int i;"); 9885 9886 // Calling an operator as a member function. 9887 verifyFormat("void f() { a.operator*(); }"); 9888 verifyFormat("void f() { a.operator*(b & b); }"); 9889 verifyFormat("void f() { a->operator&(a * b); }"); 9890 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9891 // TODO: Calling an operator as a non-member function is hard to distinguish. 9892 // https://llvm.org/PR50629 9893 // verifyFormat("void f() { operator*(a & a); }"); 9894 // verifyFormat("void f() { operator&(a, b * b); }"); 9895 9896 verifyFormat("::operator delete(foo);"); 9897 verifyFormat("::operator new(n * sizeof(foo));"); 9898 verifyFormat("foo() { ::operator delete(foo); }"); 9899 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 9900 } 9901 9902 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9903 verifyFormat("void A::b() && {}"); 9904 verifyFormat("void A::b() &&noexcept {}"); 9905 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9906 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9907 verifyFormat("Deleted &operator=(const Deleted &) &noexcept = default;"); 9908 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9909 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9910 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9911 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9912 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9913 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9914 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9915 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9916 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9917 verifyFormat("SomeType MemberFunction(const Deleted &) &&noexcept {}"); 9918 verifyFormat("void Fn(T const &) const &;"); 9919 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9920 verifyFormat("void Fn(T const volatile &&) const volatile &&noexcept;"); 9921 verifyFormat("template <typename T>\n" 9922 "void F(T) && = delete;", 9923 getGoogleStyle()); 9924 verifyFormat("template <typename T> void operator=(T) &;"); 9925 verifyFormat("template <typename T> void operator=(T) const &;"); 9926 verifyFormat("template <typename T> void operator=(T) &noexcept;"); 9927 verifyFormat("template <typename T> void operator=(T) & = default;"); 9928 verifyFormat("template <typename T> void operator=(T) &&;"); 9929 verifyFormat("template <typename T> void operator=(T) && = delete;"); 9930 verifyFormat("template <typename T> void operator=(T) & {}"); 9931 verifyFormat("template <typename T> void operator=(T) && {}"); 9932 9933 FormatStyle AlignLeft = getLLVMStyle(); 9934 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9935 verifyFormat("void A::b() && {}", AlignLeft); 9936 verifyFormat("void A::b() && noexcept {}", AlignLeft); 9937 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9938 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;", 9939 AlignLeft); 9940 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9941 AlignLeft); 9942 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9943 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9944 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9945 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9946 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9947 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9948 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9949 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9950 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;", 9951 AlignLeft); 9952 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft); 9953 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft); 9954 verifyFormat("template <typename T> void operator=(T) & noexcept;", 9955 AlignLeft); 9956 verifyFormat("template <typename T> void operator=(T) & = default;", 9957 AlignLeft); 9958 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft); 9959 verifyFormat("template <typename T> void operator=(T) && = delete;", 9960 AlignLeft); 9961 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft); 9962 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft); 9963 9964 FormatStyle AlignMiddle = getLLVMStyle(); 9965 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9966 verifyFormat("void A::b() && {}", AlignMiddle); 9967 verifyFormat("void A::b() && noexcept {}", AlignMiddle); 9968 verifyFormat("Deleted & operator=(const Deleted &) & = default;", 9969 AlignMiddle); 9970 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;", 9971 AlignMiddle); 9972 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", 9973 AlignMiddle); 9974 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle); 9975 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle); 9976 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle); 9977 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle); 9978 verifyFormat("auto Function(T) & -> void {}", AlignMiddle); 9979 verifyFormat("auto Function(T) & -> void;", AlignMiddle); 9980 verifyFormat("void Fn(T const &) const &;", AlignMiddle); 9981 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle); 9982 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;", 9983 AlignMiddle); 9984 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle); 9985 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle); 9986 verifyFormat("template <typename T> void operator=(T) & noexcept;", 9987 AlignMiddle); 9988 verifyFormat("template <typename T> void operator=(T) & = default;", 9989 AlignMiddle); 9990 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle); 9991 verifyFormat("template <typename T> void operator=(T) && = delete;", 9992 AlignMiddle); 9993 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle); 9994 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle); 9995 9996 FormatStyle Spaces = getLLVMStyle(); 9997 Spaces.SpacesInCStyleCastParentheses = true; 9998 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9999 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 10000 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 10001 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 10002 10003 Spaces.SpacesInCStyleCastParentheses = false; 10004 Spaces.SpacesInParentheses = true; 10005 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 10006 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 10007 Spaces); 10008 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 10009 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 10010 10011 FormatStyle BreakTemplate = getLLVMStyle(); 10012 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 10013 10014 verifyFormat("struct f {\n" 10015 " template <class T>\n" 10016 " int &foo(const std::string &str) &noexcept {}\n" 10017 "};", 10018 BreakTemplate); 10019 10020 verifyFormat("struct f {\n" 10021 " template <class T>\n" 10022 " int &foo(const std::string &str) &&noexcept {}\n" 10023 "};", 10024 BreakTemplate); 10025 10026 verifyFormat("struct f {\n" 10027 " template <class T>\n" 10028 " int &foo(const std::string &str) const &noexcept {}\n" 10029 "};", 10030 BreakTemplate); 10031 10032 verifyFormat("struct f {\n" 10033 " template <class T>\n" 10034 " int &foo(const std::string &str) const &noexcept {}\n" 10035 "};", 10036 BreakTemplate); 10037 10038 verifyFormat("struct f {\n" 10039 " template <class T>\n" 10040 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 10041 "};", 10042 BreakTemplate); 10043 10044 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 10045 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 10046 FormatStyle::BTDS_Yes; 10047 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 10048 10049 verifyFormat("struct f {\n" 10050 " template <class T>\n" 10051 " int& foo(const std::string& str) & noexcept {}\n" 10052 "};", 10053 AlignLeftBreakTemplate); 10054 10055 verifyFormat("struct f {\n" 10056 " template <class T>\n" 10057 " int& foo(const std::string& str) && noexcept {}\n" 10058 "};", 10059 AlignLeftBreakTemplate); 10060 10061 verifyFormat("struct f {\n" 10062 " template <class T>\n" 10063 " int& foo(const std::string& str) const& noexcept {}\n" 10064 "};", 10065 AlignLeftBreakTemplate); 10066 10067 verifyFormat("struct f {\n" 10068 " template <class T>\n" 10069 " int& foo(const std::string& str) const&& noexcept {}\n" 10070 "};", 10071 AlignLeftBreakTemplate); 10072 10073 verifyFormat("struct f {\n" 10074 " template <class T>\n" 10075 " auto foo(const std::string& str) && noexcept -> int& {}\n" 10076 "};", 10077 AlignLeftBreakTemplate); 10078 10079 // The `&` in `Type&` should not be confused with a trailing `&` of 10080 // DEPRECATED(reason) member function. 10081 verifyFormat("struct f {\n" 10082 " template <class T>\n" 10083 " DEPRECATED(reason)\n" 10084 " Type &foo(arguments) {}\n" 10085 "};", 10086 BreakTemplate); 10087 10088 verifyFormat("struct f {\n" 10089 " template <class T>\n" 10090 " DEPRECATED(reason)\n" 10091 " Type& foo(arguments) {}\n" 10092 "};", 10093 AlignLeftBreakTemplate); 10094 10095 verifyFormat("void (*foopt)(int) = &func;"); 10096 10097 FormatStyle DerivePointerAlignment = getLLVMStyle(); 10098 DerivePointerAlignment.DerivePointerAlignment = true; 10099 // There's always a space between the function and its trailing qualifiers. 10100 // This isn't evidence for PAS_Right (or for PAS_Left). 10101 std::string Prefix = "void a() &;\n" 10102 "void b() &;\n"; 10103 verifyFormat(Prefix + "int* x;", DerivePointerAlignment); 10104 verifyFormat(Prefix + "int *x;", DerivePointerAlignment); 10105 // Same if the function is an overloaded operator, and with &&. 10106 Prefix = "void operator()() &&;\n" 10107 "void operator()() &&;\n"; 10108 verifyFormat(Prefix + "int* x;", DerivePointerAlignment); 10109 verifyFormat(Prefix + "int *x;", DerivePointerAlignment); 10110 // However a space between cv-qualifiers and ref-qualifiers *is* evidence. 10111 Prefix = "void a() const &;\n" 10112 "void b() const &;\n"; 10113 EXPECT_EQ(Prefix + "int *x;", 10114 format(Prefix + "int* x;", DerivePointerAlignment)); 10115 } 10116 10117 TEST_F(FormatTest, UnderstandsNewAndDelete) { 10118 verifyFormat("void f() {\n" 10119 " A *a = new A;\n" 10120 " A *a = new (placement) A;\n" 10121 " delete a;\n" 10122 " delete (A *)a;\n" 10123 "}"); 10124 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 10125 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 10126 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10127 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 10128 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 10129 verifyFormat("delete[] h->p;"); 10130 verifyFormat("delete[] (void *)p;"); 10131 10132 verifyFormat("void operator delete(void *foo) ATTRIB;"); 10133 verifyFormat("void operator new(void *foo) ATTRIB;"); 10134 verifyFormat("void operator delete[](void *foo) ATTRIB;"); 10135 verifyFormat("void operator delete(void *ptr) noexcept;"); 10136 10137 EXPECT_EQ("void new(link p);\n" 10138 "void delete(link p);\n", 10139 format("void new (link p);\n" 10140 "void delete (link p);\n")); 10141 } 10142 10143 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 10144 verifyFormat("int *f(int *a) {}"); 10145 verifyFormat("int main(int argc, char **argv) {}"); 10146 verifyFormat("Test::Test(int b) : a(b * b) {}"); 10147 verifyIndependentOfContext("f(a, *a);"); 10148 verifyFormat("void g() { f(*a); }"); 10149 verifyIndependentOfContext("int a = b * 10;"); 10150 verifyIndependentOfContext("int a = 10 * b;"); 10151 verifyIndependentOfContext("int a = b * c;"); 10152 verifyIndependentOfContext("int a += b * c;"); 10153 verifyIndependentOfContext("int a -= b * c;"); 10154 verifyIndependentOfContext("int a *= b * c;"); 10155 verifyIndependentOfContext("int a /= b * c;"); 10156 verifyIndependentOfContext("int a = *b;"); 10157 verifyIndependentOfContext("int a = *b * c;"); 10158 verifyIndependentOfContext("int a = b * *c;"); 10159 verifyIndependentOfContext("int a = b * (10);"); 10160 verifyIndependentOfContext("S << b * (10);"); 10161 verifyIndependentOfContext("return 10 * b;"); 10162 verifyIndependentOfContext("return *b * *c;"); 10163 verifyIndependentOfContext("return a & ~b;"); 10164 verifyIndependentOfContext("f(b ? *c : *d);"); 10165 verifyIndependentOfContext("int a = b ? *c : *d;"); 10166 verifyIndependentOfContext("*b = a;"); 10167 verifyIndependentOfContext("a * ~b;"); 10168 verifyIndependentOfContext("a * !b;"); 10169 verifyIndependentOfContext("a * +b;"); 10170 verifyIndependentOfContext("a * -b;"); 10171 verifyIndependentOfContext("a * ++b;"); 10172 verifyIndependentOfContext("a * --b;"); 10173 verifyIndependentOfContext("a[4] * b;"); 10174 verifyIndependentOfContext("a[a * a] = 1;"); 10175 verifyIndependentOfContext("f() * b;"); 10176 verifyIndependentOfContext("a * [self dostuff];"); 10177 verifyIndependentOfContext("int x = a * (a + b);"); 10178 verifyIndependentOfContext("(a *)(a + b);"); 10179 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 10180 verifyIndependentOfContext("int *pa = (int *)&a;"); 10181 verifyIndependentOfContext("return sizeof(int **);"); 10182 verifyIndependentOfContext("return sizeof(int ******);"); 10183 verifyIndependentOfContext("return (int **&)a;"); 10184 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 10185 verifyFormat("void f(Type (*parameter)[10]) {}"); 10186 verifyFormat("void f(Type (¶meter)[10]) {}"); 10187 verifyGoogleFormat("return sizeof(int**);"); 10188 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 10189 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 10190 verifyFormat("auto a = [](int **&, int ***) {};"); 10191 verifyFormat("auto PointerBinding = [](const char *S) {};"); 10192 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 10193 verifyFormat("[](const decltype(*a) &value) {}"); 10194 verifyFormat("[](const typeof(*a) &value) {}"); 10195 verifyFormat("[](const _Atomic(a *) &value) {}"); 10196 verifyFormat("[](const __underlying_type(a) &value) {}"); 10197 verifyFormat("decltype(a * b) F();"); 10198 verifyFormat("typeof(a * b) F();"); 10199 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 10200 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 10201 verifyIndependentOfContext("typedef void (*f)(int *a);"); 10202 verifyIndependentOfContext("int i{a * b};"); 10203 verifyIndependentOfContext("aaa && aaa->f();"); 10204 verifyIndependentOfContext("int x = ~*p;"); 10205 verifyFormat("Constructor() : a(a), area(width * height) {}"); 10206 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 10207 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 10208 verifyFormat("void f() { f(a, c * d); }"); 10209 verifyFormat("void f() { f(new a(), c * d); }"); 10210 verifyFormat("void f(const MyOverride &override);"); 10211 verifyFormat("void f(const MyFinal &final);"); 10212 verifyIndependentOfContext("bool a = f() && override.f();"); 10213 verifyIndependentOfContext("bool a = f() && final.f();"); 10214 10215 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 10216 10217 verifyIndependentOfContext("A<int *> a;"); 10218 verifyIndependentOfContext("A<int **> a;"); 10219 verifyIndependentOfContext("A<int *, int *> a;"); 10220 verifyIndependentOfContext("A<int *[]> a;"); 10221 verifyIndependentOfContext( 10222 "const char *const p = reinterpret_cast<const char *const>(q);"); 10223 verifyIndependentOfContext("A<int **, int **> a;"); 10224 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 10225 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 10226 verifyFormat("for (; a && b;) {\n}"); 10227 verifyFormat("bool foo = true && [] { return false; }();"); 10228 10229 verifyFormat( 10230 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10232 10233 verifyGoogleFormat("int const* a = &b;"); 10234 verifyGoogleFormat("**outparam = 1;"); 10235 verifyGoogleFormat("*outparam = a * b;"); 10236 verifyGoogleFormat("int main(int argc, char** argv) {}"); 10237 verifyGoogleFormat("A<int*> a;"); 10238 verifyGoogleFormat("A<int**> a;"); 10239 verifyGoogleFormat("A<int*, int*> a;"); 10240 verifyGoogleFormat("A<int**, int**> a;"); 10241 verifyGoogleFormat("f(b ? *c : *d);"); 10242 verifyGoogleFormat("int a = b ? *c : *d;"); 10243 verifyGoogleFormat("Type* t = **x;"); 10244 verifyGoogleFormat("Type* t = *++*x;"); 10245 verifyGoogleFormat("*++*x;"); 10246 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 10247 verifyGoogleFormat("Type* t = x++ * y;"); 10248 verifyGoogleFormat( 10249 "const char* const p = reinterpret_cast<const char* const>(q);"); 10250 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 10251 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 10252 verifyGoogleFormat("template <typename T>\n" 10253 "void f(int i = 0, SomeType** temps = NULL);"); 10254 10255 FormatStyle Left = getLLVMStyle(); 10256 Left.PointerAlignment = FormatStyle::PAS_Left; 10257 verifyFormat("x = *a(x) = *a(y);", Left); 10258 verifyFormat("for (;; *a = b) {\n}", Left); 10259 verifyFormat("return *this += 1;", Left); 10260 verifyFormat("throw *x;", Left); 10261 verifyFormat("delete *x;", Left); 10262 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 10263 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 10264 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 10265 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 10266 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 10267 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 10268 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 10269 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 10270 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 10271 10272 verifyIndependentOfContext("a = *(x + y);"); 10273 verifyIndependentOfContext("a = &(x + y);"); 10274 verifyIndependentOfContext("*(x + y).call();"); 10275 verifyIndependentOfContext("&(x + y)->call();"); 10276 verifyFormat("void f() { &(*I).first; }"); 10277 10278 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 10279 verifyFormat("f(* /* confusing comment */ foo);"); 10280 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 10281 verifyFormat("void foo(int * // this is the first paramters\n" 10282 " ,\n" 10283 " int second);"); 10284 verifyFormat("double term = a * // first\n" 10285 " b;"); 10286 verifyFormat( 10287 "int *MyValues = {\n" 10288 " *A, // Operator detection might be confused by the '{'\n" 10289 " *BB // Operator detection might be confused by previous comment\n" 10290 "};"); 10291 10292 verifyIndependentOfContext("if (int *a = &b)"); 10293 verifyIndependentOfContext("if (int &a = *b)"); 10294 verifyIndependentOfContext("if (a & b[i])"); 10295 verifyIndependentOfContext("if constexpr (a & b[i])"); 10296 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 10297 verifyIndependentOfContext("if (a * (b * c))"); 10298 verifyIndependentOfContext("if constexpr (a * (b * c))"); 10299 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 10300 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 10301 verifyIndependentOfContext("if (*b[i])"); 10302 verifyIndependentOfContext("if (int *a = (&b))"); 10303 verifyIndependentOfContext("while (int *a = &b)"); 10304 verifyIndependentOfContext("while (a * (b * c))"); 10305 verifyIndependentOfContext("size = sizeof *a;"); 10306 verifyIndependentOfContext("if (a && (b = c))"); 10307 verifyFormat("void f() {\n" 10308 " for (const int &v : Values) {\n" 10309 " }\n" 10310 "}"); 10311 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 10312 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 10313 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 10314 10315 verifyFormat("#define A (!a * b)"); 10316 verifyFormat("#define MACRO \\\n" 10317 " int *i = a * b; \\\n" 10318 " void f(a *b);", 10319 getLLVMStyleWithColumns(19)); 10320 10321 verifyIndependentOfContext("A = new SomeType *[Length];"); 10322 verifyIndependentOfContext("A = new SomeType *[Length]();"); 10323 verifyIndependentOfContext("T **t = new T *;"); 10324 verifyIndependentOfContext("T **t = new T *();"); 10325 verifyGoogleFormat("A = new SomeType*[Length]();"); 10326 verifyGoogleFormat("A = new SomeType*[Length];"); 10327 verifyGoogleFormat("T** t = new T*;"); 10328 verifyGoogleFormat("T** t = new T*();"); 10329 10330 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 10331 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 10332 verifyFormat("template <bool a, bool b> " 10333 "typename t::if<x && y>::type f() {}"); 10334 verifyFormat("template <int *y> f() {}"); 10335 verifyFormat("vector<int *> v;"); 10336 verifyFormat("vector<int *const> v;"); 10337 verifyFormat("vector<int *const **const *> v;"); 10338 verifyFormat("vector<int *volatile> v;"); 10339 verifyFormat("vector<a *_Nonnull> v;"); 10340 verifyFormat("vector<a *_Nullable> v;"); 10341 verifyFormat("vector<a *_Null_unspecified> v;"); 10342 verifyFormat("vector<a *__ptr32> v;"); 10343 verifyFormat("vector<a *__ptr64> v;"); 10344 verifyFormat("vector<a *__capability> v;"); 10345 FormatStyle TypeMacros = getLLVMStyle(); 10346 TypeMacros.TypenameMacros = {"LIST"}; 10347 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 10348 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 10349 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 10350 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 10351 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 10352 10353 FormatStyle CustomQualifier = getLLVMStyle(); 10354 // Add identifiers that should not be parsed as a qualifier by default. 10355 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 10356 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 10357 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 10358 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 10359 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 10360 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 10361 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 10362 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 10363 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 10364 verifyFormat("vector<a * _NotAQualifier> v;"); 10365 verifyFormat("vector<a * __not_a_qualifier> v;"); 10366 verifyFormat("vector<a * b> v;"); 10367 verifyFormat("foo<b && false>();"); 10368 verifyFormat("foo<b & 1>();"); 10369 verifyFormat("foo<b & (1)>();"); 10370 verifyFormat("foo<b & (~0)>();"); 10371 verifyFormat("foo<b & (true)>();"); 10372 verifyFormat("foo<b & ((1))>();"); 10373 verifyFormat("foo<b & (/*comment*/ 1)>();"); 10374 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 10375 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 10376 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 10377 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 10378 verifyFormat( 10379 "template <class T, class = typename std::enable_if<\n" 10380 " std::is_integral<T>::value &&\n" 10381 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 10382 "void F();", 10383 getLLVMStyleWithColumns(70)); 10384 verifyFormat("template <class T,\n" 10385 " class = typename std::enable_if<\n" 10386 " std::is_integral<T>::value &&\n" 10387 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 10388 " class U>\n" 10389 "void F();", 10390 getLLVMStyleWithColumns(70)); 10391 verifyFormat( 10392 "template <class T,\n" 10393 " class = typename ::std::enable_if<\n" 10394 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 10395 "void F();", 10396 getGoogleStyleWithColumns(68)); 10397 10398 verifyIndependentOfContext("MACRO(int *i);"); 10399 verifyIndependentOfContext("MACRO(auto *a);"); 10400 verifyIndependentOfContext("MACRO(const A *a);"); 10401 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 10402 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 10403 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 10404 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 10405 verifyIndependentOfContext("MACRO(A *const a);"); 10406 verifyIndependentOfContext("MACRO(A *restrict a);"); 10407 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 10408 verifyIndependentOfContext("MACRO(A *__restrict a);"); 10409 verifyIndependentOfContext("MACRO(A *volatile a);"); 10410 verifyIndependentOfContext("MACRO(A *__volatile a);"); 10411 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 10412 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 10413 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 10414 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 10415 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 10416 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 10417 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 10418 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 10419 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 10420 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 10421 verifyIndependentOfContext("MACRO(A *__capability);"); 10422 verifyIndependentOfContext("MACRO(A &__capability);"); 10423 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 10424 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 10425 // If we add __my_qualifier to AttributeMacros it should always be parsed as 10426 // a type declaration: 10427 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 10428 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 10429 // Also check that TypenameMacros prevents parsing it as multiplication: 10430 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 10431 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 10432 10433 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 10434 verifyFormat("void f() { f(float{1}, a * a); }"); 10435 verifyFormat("void f() { f(float(1), a * a); }"); 10436 10437 verifyFormat("f((void (*)(int))g);"); 10438 verifyFormat("f((void (&)(int))g);"); 10439 verifyFormat("f((void (^)(int))g);"); 10440 10441 // FIXME: Is there a way to make this work? 10442 // verifyIndependentOfContext("MACRO(A *a);"); 10443 verifyFormat("MACRO(A &B);"); 10444 verifyFormat("MACRO(A *B);"); 10445 verifyFormat("void f() { MACRO(A * B); }"); 10446 verifyFormat("void f() { MACRO(A & B); }"); 10447 10448 // This lambda was mis-formatted after D88956 (treating it as a binop): 10449 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 10450 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 10451 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 10452 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 10453 10454 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 10455 verifyFormat("return options != nullptr && operator==(*options);"); 10456 10457 EXPECT_EQ("#define OP(x) \\\n" 10458 " ostream &operator<<(ostream &s, const A &a) { \\\n" 10459 " return s << a.DebugString(); \\\n" 10460 " }", 10461 format("#define OP(x) \\\n" 10462 " ostream &operator<<(ostream &s, const A &a) { \\\n" 10463 " return s << a.DebugString(); \\\n" 10464 " }", 10465 getLLVMStyleWithColumns(50))); 10466 10467 // FIXME: We cannot handle this case yet; we might be able to figure out that 10468 // foo<x> d > v; doesn't make sense. 10469 verifyFormat("foo<a<b && c> d> v;"); 10470 10471 FormatStyle PointerMiddle = getLLVMStyle(); 10472 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 10473 verifyFormat("delete *x;", PointerMiddle); 10474 verifyFormat("int * x;", PointerMiddle); 10475 verifyFormat("int *[] x;", PointerMiddle); 10476 verifyFormat("template <int * y> f() {}", PointerMiddle); 10477 verifyFormat("int * f(int * a) {}", PointerMiddle); 10478 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 10479 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 10480 verifyFormat("A<int *> a;", PointerMiddle); 10481 verifyFormat("A<int **> a;", PointerMiddle); 10482 verifyFormat("A<int *, int *> a;", PointerMiddle); 10483 verifyFormat("A<int *[]> a;", PointerMiddle); 10484 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 10485 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 10486 verifyFormat("T ** t = new T *;", PointerMiddle); 10487 10488 // Member function reference qualifiers aren't binary operators. 10489 verifyFormat("string // break\n" 10490 "operator()() & {}"); 10491 verifyFormat("string // break\n" 10492 "operator()() && {}"); 10493 verifyGoogleFormat("template <typename T>\n" 10494 "auto x() & -> int {}"); 10495 10496 // Should be binary operators when used as an argument expression (overloaded 10497 // operator invoked as a member function). 10498 verifyFormat("void f() { a.operator()(a * a); }"); 10499 verifyFormat("void f() { a->operator()(a & a); }"); 10500 verifyFormat("void f() { a.operator()(*a & *a); }"); 10501 verifyFormat("void f() { a->operator()(*a * *a); }"); 10502 10503 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 10504 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 10505 } 10506 10507 TEST_F(FormatTest, UnderstandsAttributes) { 10508 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 10509 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 10510 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10511 verifyFormat("__attribute__((nodebug)) ::qualified_type f();"); 10512 FormatStyle AfterType = getLLVMStyle(); 10513 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10514 verifyFormat("__attribute__((nodebug)) void\n" 10515 "foo() {}\n", 10516 AfterType); 10517 verifyFormat("__unused void\n" 10518 "foo() {}", 10519 AfterType); 10520 10521 FormatStyle CustomAttrs = getLLVMStyle(); 10522 CustomAttrs.AttributeMacros.push_back("__unused"); 10523 CustomAttrs.AttributeMacros.push_back("__attr1"); 10524 CustomAttrs.AttributeMacros.push_back("__attr2"); 10525 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 10526 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 10527 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 10528 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 10529 // Check that it is parsed as a multiplication without AttributeMacros and 10530 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 10531 verifyFormat("vector<SomeType * __attr1> v;"); 10532 verifyFormat("vector<SomeType __attr1 *> v;"); 10533 verifyFormat("vector<SomeType __attr1 *const> v;"); 10534 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 10535 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 10536 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 10537 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 10538 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 10539 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 10540 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 10541 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 10542 10543 // Check that these are not parsed as function declarations: 10544 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10545 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 10546 verifyFormat("SomeType s(InitValue);", CustomAttrs); 10547 verifyFormat("SomeType s{InitValue};", CustomAttrs); 10548 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 10549 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 10550 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 10551 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 10552 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 10553 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 10554 } 10555 10556 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 10557 // Check that qualifiers on pointers don't break parsing of casts. 10558 verifyFormat("x = (foo *const)*v;"); 10559 verifyFormat("x = (foo *volatile)*v;"); 10560 verifyFormat("x = (foo *restrict)*v;"); 10561 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 10562 verifyFormat("x = (foo *_Nonnull)*v;"); 10563 verifyFormat("x = (foo *_Nullable)*v;"); 10564 verifyFormat("x = (foo *_Null_unspecified)*v;"); 10565 verifyFormat("x = (foo *_Nonnull)*v;"); 10566 verifyFormat("x = (foo *[[clang::attr]])*v;"); 10567 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 10568 verifyFormat("x = (foo *__ptr32)*v;"); 10569 verifyFormat("x = (foo *__ptr64)*v;"); 10570 verifyFormat("x = (foo *__capability)*v;"); 10571 10572 // Check that we handle multiple trailing qualifiers and skip them all to 10573 // determine that the expression is a cast to a pointer type. 10574 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 10575 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 10576 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 10577 StringRef AllQualifiers = 10578 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 10579 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 10580 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 10581 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 10582 10583 // Also check that address-of is not parsed as a binary bitwise-and: 10584 verifyFormat("x = (foo *const)&v;"); 10585 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 10586 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 10587 10588 // Check custom qualifiers: 10589 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 10590 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 10591 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 10592 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 10593 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 10594 CustomQualifier); 10595 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 10596 CustomQualifier); 10597 10598 // Check that unknown identifiers result in binary operator parsing: 10599 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 10600 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 10601 } 10602 10603 TEST_F(FormatTest, UnderstandsSquareAttributes) { 10604 verifyFormat("SomeType s [[unused]] (InitValue);"); 10605 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 10606 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 10607 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 10608 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 10609 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10610 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10611 verifyFormat("[[nodiscard]] bool f() { return false; }"); 10612 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 10613 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 10614 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 10615 verifyFormat("[[nodiscard]] ::qualified_type f();"); 10616 10617 // Make sure we do not mistake attributes for array subscripts. 10618 verifyFormat("int a() {}\n" 10619 "[[unused]] int b() {}\n"); 10620 verifyFormat("NSArray *arr;\n" 10621 "arr[[Foo() bar]];"); 10622 10623 // On the other hand, we still need to correctly find array subscripts. 10624 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 10625 10626 // Make sure that we do not mistake Objective-C method inside array literals 10627 // as attributes, even if those method names are also keywords. 10628 verifyFormat("@[ [foo bar] ];"); 10629 verifyFormat("@[ [NSArray class] ];"); 10630 verifyFormat("@[ [foo enum] ];"); 10631 10632 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 10633 10634 // Make sure we do not parse attributes as lambda introducers. 10635 FormatStyle MultiLineFunctions = getLLVMStyle(); 10636 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10637 verifyFormat("[[unused]] int b() {\n" 10638 " return 42;\n" 10639 "}\n", 10640 MultiLineFunctions); 10641 } 10642 10643 TEST_F(FormatTest, AttributeClass) { 10644 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 10645 verifyFormat("class S {\n" 10646 " S(S&&) = default;\n" 10647 "};", 10648 Style); 10649 verifyFormat("class [[nodiscard]] S {\n" 10650 " S(S&&) = default;\n" 10651 "};", 10652 Style); 10653 verifyFormat("class __attribute((maybeunused)) S {\n" 10654 " S(S&&) = default;\n" 10655 "};", 10656 Style); 10657 verifyFormat("struct S {\n" 10658 " S(S&&) = default;\n" 10659 "};", 10660 Style); 10661 verifyFormat("struct [[nodiscard]] S {\n" 10662 " S(S&&) = default;\n" 10663 "};", 10664 Style); 10665 } 10666 10667 TEST_F(FormatTest, AttributesAfterMacro) { 10668 FormatStyle Style = getLLVMStyle(); 10669 verifyFormat("MACRO;\n" 10670 "__attribute__((maybe_unused)) int foo() {\n" 10671 " //...\n" 10672 "}"); 10673 10674 verifyFormat("MACRO;\n" 10675 "[[nodiscard]] int foo() {\n" 10676 " //...\n" 10677 "}"); 10678 10679 EXPECT_EQ("MACRO\n\n" 10680 "__attribute__((maybe_unused)) int foo() {\n" 10681 " //...\n" 10682 "}", 10683 format("MACRO\n\n" 10684 "__attribute__((maybe_unused)) int foo() {\n" 10685 " //...\n" 10686 "}")); 10687 10688 EXPECT_EQ("MACRO\n\n" 10689 "[[nodiscard]] int foo() {\n" 10690 " //...\n" 10691 "}", 10692 format("MACRO\n\n" 10693 "[[nodiscard]] int foo() {\n" 10694 " //...\n" 10695 "}")); 10696 } 10697 10698 TEST_F(FormatTest, AttributePenaltyBreaking) { 10699 FormatStyle Style = getLLVMStyle(); 10700 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 10701 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10702 Style); 10703 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 10704 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10705 Style); 10706 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 10707 "shared_ptr<ALongTypeName> &C d) {\n}", 10708 Style); 10709 } 10710 10711 TEST_F(FormatTest, UnderstandsEllipsis) { 10712 FormatStyle Style = getLLVMStyle(); 10713 verifyFormat("int printf(const char *fmt, ...);"); 10714 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 10715 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 10716 10717 verifyFormat("template <int *...PP> a;", Style); 10718 10719 Style.PointerAlignment = FormatStyle::PAS_Left; 10720 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 10721 10722 verifyFormat("template <int*... PP> a;", Style); 10723 10724 Style.PointerAlignment = FormatStyle::PAS_Middle; 10725 verifyFormat("template <int *... PP> a;", Style); 10726 } 10727 10728 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 10729 EXPECT_EQ("int *a;\n" 10730 "int *a;\n" 10731 "int *a;", 10732 format("int *a;\n" 10733 "int* a;\n" 10734 "int *a;", 10735 getGoogleStyle())); 10736 EXPECT_EQ("int* a;\n" 10737 "int* a;\n" 10738 "int* a;", 10739 format("int* a;\n" 10740 "int* a;\n" 10741 "int *a;", 10742 getGoogleStyle())); 10743 EXPECT_EQ("int *a;\n" 10744 "int *a;\n" 10745 "int *a;", 10746 format("int *a;\n" 10747 "int * a;\n" 10748 "int * a;", 10749 getGoogleStyle())); 10750 EXPECT_EQ("auto x = [] {\n" 10751 " int *a;\n" 10752 " int *a;\n" 10753 " int *a;\n" 10754 "};", 10755 format("auto x=[]{int *a;\n" 10756 "int * a;\n" 10757 "int * a;};", 10758 getGoogleStyle())); 10759 } 10760 10761 TEST_F(FormatTest, UnderstandsRvalueReferences) { 10762 verifyFormat("int f(int &&a) {}"); 10763 verifyFormat("int f(int a, char &&b) {}"); 10764 verifyFormat("void f() { int &&a = b; }"); 10765 verifyGoogleFormat("int f(int a, char&& b) {}"); 10766 verifyGoogleFormat("void f() { int&& a = b; }"); 10767 10768 verifyIndependentOfContext("A<int &&> a;"); 10769 verifyIndependentOfContext("A<int &&, int &&> a;"); 10770 verifyGoogleFormat("A<int&&> a;"); 10771 verifyGoogleFormat("A<int&&, int&&> a;"); 10772 10773 // Not rvalue references: 10774 verifyFormat("template <bool B, bool C> class A {\n" 10775 " static_assert(B && C, \"Something is wrong\");\n" 10776 "};"); 10777 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 10778 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 10779 verifyFormat("#define A(a, b) (a && b)"); 10780 } 10781 10782 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 10783 verifyFormat("void f() {\n" 10784 " x[aaaaaaaaa -\n" 10785 " b] = 23;\n" 10786 "}", 10787 getLLVMStyleWithColumns(15)); 10788 } 10789 10790 TEST_F(FormatTest, FormatsCasts) { 10791 verifyFormat("Type *A = static_cast<Type *>(P);"); 10792 verifyFormat("static_cast<Type *>(P);"); 10793 verifyFormat("static_cast<Type &>(Fun)(Args);"); 10794 verifyFormat("static_cast<Type &>(*Fun)(Args);"); 10795 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;"); 10796 // Check that static_cast<...>(...) does not require the next token to be on 10797 // the same line. 10798 verifyFormat("some_loooong_output << something_something__ << " 10799 "static_cast<const void *>(R)\n" 10800 " << something;"); 10801 verifyFormat("a = static_cast<Type &>(*Fun)(Args);"); 10802 verifyFormat("const_cast<Type &>(*Fun)(Args);"); 10803 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);"); 10804 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);"); 10805 verifyFormat("Type *A = (Type *)P;"); 10806 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 10807 verifyFormat("int a = (int)(2.0f);"); 10808 verifyFormat("int a = (int)2.0f;"); 10809 verifyFormat("x[(int32)y];"); 10810 verifyFormat("x = (int32)y;"); 10811 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 10812 verifyFormat("int a = (int)*b;"); 10813 verifyFormat("int a = (int)2.0f;"); 10814 verifyFormat("int a = (int)~0;"); 10815 verifyFormat("int a = (int)++a;"); 10816 verifyFormat("int a = (int)sizeof(int);"); 10817 verifyFormat("int a = (int)+2;"); 10818 verifyFormat("my_int a = (my_int)2.0f;"); 10819 verifyFormat("my_int a = (my_int)sizeof(int);"); 10820 verifyFormat("return (my_int)aaa;"); 10821 verifyFormat("#define x ((int)-1)"); 10822 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 10823 verifyFormat("#define p(q) ((int *)&q)"); 10824 verifyFormat("fn(a)(b) + 1;"); 10825 10826 verifyFormat("void f() { my_int a = (my_int)*b; }"); 10827 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 10828 verifyFormat("my_int a = (my_int)~0;"); 10829 verifyFormat("my_int a = (my_int)++a;"); 10830 verifyFormat("my_int a = (my_int)-2;"); 10831 verifyFormat("my_int a = (my_int)1;"); 10832 verifyFormat("my_int a = (my_int *)1;"); 10833 verifyFormat("my_int a = (const my_int)-1;"); 10834 verifyFormat("my_int a = (const my_int *)-1;"); 10835 verifyFormat("my_int a = (my_int)(my_int)-1;"); 10836 verifyFormat("my_int a = (ns::my_int)-2;"); 10837 verifyFormat("case (my_int)ONE:"); 10838 verifyFormat("auto x = (X)this;"); 10839 // Casts in Obj-C style calls used to not be recognized as such. 10840 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 10841 10842 // FIXME: single value wrapped with paren will be treated as cast. 10843 verifyFormat("void f(int i = (kValue)*kMask) {}"); 10844 10845 verifyFormat("{ (void)F; }"); 10846 10847 // Don't break after a cast's 10848 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10849 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 10850 " bbbbbbbbbbbbbbbbbbbbbb);"); 10851 10852 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)"); 10853 verifyFormat("#define CONF_BOOL(x) (bool *)(x)"); 10854 verifyFormat("#define CONF_BOOL(x) (bool)(x)"); 10855 verifyFormat("bool *y = (bool *)(void *)(x);"); 10856 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)"); 10857 verifyFormat("bool *y = (bool *)(void *)(int)(x);"); 10858 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)"); 10859 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);"); 10860 10861 // These are not casts. 10862 verifyFormat("void f(int *) {}"); 10863 verifyFormat("f(foo)->b;"); 10864 verifyFormat("f(foo).b;"); 10865 verifyFormat("f(foo)(b);"); 10866 verifyFormat("f(foo)[b];"); 10867 verifyFormat("[](foo) { return 4; }(bar);"); 10868 verifyFormat("(*funptr)(foo)[4];"); 10869 verifyFormat("funptrs[4](foo)[4];"); 10870 verifyFormat("void f(int *);"); 10871 verifyFormat("void f(int *) = 0;"); 10872 verifyFormat("void f(SmallVector<int>) {}"); 10873 verifyFormat("void f(SmallVector<int>);"); 10874 verifyFormat("void f(SmallVector<int>) = 0;"); 10875 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 10876 verifyFormat("int a = sizeof(int) * b;"); 10877 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 10878 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 10879 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 10880 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 10881 10882 // These are not casts, but at some point were confused with casts. 10883 verifyFormat("virtual void foo(int *) override;"); 10884 verifyFormat("virtual void foo(char &) const;"); 10885 verifyFormat("virtual void foo(int *a, char *) const;"); 10886 verifyFormat("int a = sizeof(int *) + b;"); 10887 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10888 verifyFormat("bool b = f(g<int>) && c;"); 10889 verifyFormat("typedef void (*f)(int i) func;"); 10890 verifyFormat("void operator++(int) noexcept;"); 10891 verifyFormat("void operator++(int &) noexcept;"); 10892 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10893 "&) noexcept;"); 10894 verifyFormat( 10895 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10896 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10897 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10898 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10899 verifyFormat("void operator delete(foo &) noexcept;"); 10900 verifyFormat("void operator delete(foo) noexcept;"); 10901 verifyFormat("void operator delete(int) noexcept;"); 10902 verifyFormat("void operator delete(int &) noexcept;"); 10903 verifyFormat("void operator delete(int &) volatile noexcept;"); 10904 verifyFormat("void operator delete(int &) const"); 10905 verifyFormat("void operator delete(int &) = default"); 10906 verifyFormat("void operator delete(int &) = delete"); 10907 verifyFormat("void operator delete(int &) [[noreturn]]"); 10908 verifyFormat("void operator delete(int &) throw();"); 10909 verifyFormat("void operator delete(int &) throw(int);"); 10910 verifyFormat("auto operator delete(int &) -> int;"); 10911 verifyFormat("auto operator delete(int &) override"); 10912 verifyFormat("auto operator delete(int &) final"); 10913 10914 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10915 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10916 // FIXME: The indentation here is not ideal. 10917 verifyFormat( 10918 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10919 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10920 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10921 } 10922 10923 TEST_F(FormatTest, FormatsFunctionTypes) { 10924 verifyFormat("A<bool()> a;"); 10925 verifyFormat("A<SomeType()> a;"); 10926 verifyFormat("A<void (*)(int, std::string)> a;"); 10927 verifyFormat("A<void *(int)>;"); 10928 verifyFormat("void *(*a)(int *, SomeType *);"); 10929 verifyFormat("int (*func)(void *);"); 10930 verifyFormat("void f() { int (*func)(void *); }"); 10931 verifyFormat("template <class CallbackClass>\n" 10932 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10933 10934 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10935 verifyGoogleFormat("void* (*a)(int);"); 10936 verifyGoogleFormat( 10937 "template <class CallbackClass>\n" 10938 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10939 10940 // Other constructs can look somewhat like function types: 10941 verifyFormat("A<sizeof(*x)> a;"); 10942 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10943 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10944 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10945 verifyFormat("int x = f(&h)();"); 10946 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10947 verifyFormat("std::function<\n" 10948 " LooooooooooongTemplatedType<\n" 10949 " SomeType>*(\n" 10950 " LooooooooooooooooongType type)>\n" 10951 " function;", 10952 getGoogleStyleWithColumns(40)); 10953 } 10954 10955 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10956 verifyFormat("A (*foo_)[6];"); 10957 verifyFormat("vector<int> (*foo_)[6];"); 10958 } 10959 10960 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10961 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10962 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10963 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10964 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10965 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10966 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10967 10968 // Different ways of ()-initializiation. 10969 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10970 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10971 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10972 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10973 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10974 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10975 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10976 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10977 10978 // Lambdas should not confuse the variable declaration heuristic. 10979 verifyFormat("LooooooooooooooooongType\n" 10980 " variable(nullptr, [](A *a) {});", 10981 getLLVMStyleWithColumns(40)); 10982 } 10983 10984 TEST_F(FormatTest, BreaksLongDeclarations) { 10985 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10986 " AnotherNameForTheLongType;"); 10987 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10988 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10989 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10990 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10991 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10992 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10993 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10994 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10995 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10996 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10997 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10998 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10999 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 11000 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 11001 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 11002 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 11003 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 11004 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 11005 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 11006 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 11007 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 11008 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 11009 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 11010 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 11011 FormatStyle Indented = getLLVMStyle(); 11012 Indented.IndentWrappedFunctionNames = true; 11013 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 11014 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 11015 Indented); 11016 verifyFormat( 11017 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 11018 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 11019 Indented); 11020 verifyFormat( 11021 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 11022 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 11023 Indented); 11024 verifyFormat( 11025 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 11026 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 11027 Indented); 11028 11029 // FIXME: Without the comment, this breaks after "(". 11030 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 11031 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 11032 getGoogleStyle()); 11033 11034 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 11035 " int LoooooooooooooooooooongParam2) {}"); 11036 verifyFormat( 11037 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 11038 " SourceLocation L, IdentifierIn *II,\n" 11039 " Type *T) {}"); 11040 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 11041 "ReallyReaaallyLongFunctionName(\n" 11042 " const std::string &SomeParameter,\n" 11043 " const SomeType<string, SomeOtherTemplateParameter>\n" 11044 " &ReallyReallyLongParameterName,\n" 11045 " const SomeType<string, SomeOtherTemplateParameter>\n" 11046 " &AnotherLongParameterName) {}"); 11047 verifyFormat("template <typename A>\n" 11048 "SomeLoooooooooooooooooooooongType<\n" 11049 " typename some_namespace::SomeOtherType<A>::Type>\n" 11050 "Function() {}"); 11051 11052 verifyGoogleFormat( 11053 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 11054 " aaaaaaaaaaaaaaaaaaaaaaa;"); 11055 verifyGoogleFormat( 11056 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 11057 " SourceLocation L) {}"); 11058 verifyGoogleFormat( 11059 "some_namespace::LongReturnType\n" 11060 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 11061 " int first_long_parameter, int second_parameter) {}"); 11062 11063 verifyGoogleFormat("template <typename T>\n" 11064 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 11065 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 11066 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11067 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 11068 11069 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 11070 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11071 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11072 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11073 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 11074 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 11075 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11076 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 11077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 11078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11079 11080 verifyFormat("template <typename T> // Templates on own line.\n" 11081 "static int // Some comment.\n" 11082 "MyFunction(int a);", 11083 getLLVMStyle()); 11084 } 11085 11086 TEST_F(FormatTest, FormatsAccessModifiers) { 11087 FormatStyle Style = getLLVMStyle(); 11088 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 11089 FormatStyle::ELBAMS_LogicalBlock); 11090 verifyFormat("struct foo {\n" 11091 "private:\n" 11092 " void f() {}\n" 11093 "\n" 11094 "private:\n" 11095 " int i;\n" 11096 "\n" 11097 "protected:\n" 11098 " int j;\n" 11099 "};\n", 11100 Style); 11101 verifyFormat("struct foo {\n" 11102 "private:\n" 11103 " void f() {}\n" 11104 "\n" 11105 "private:\n" 11106 " int i;\n" 11107 "\n" 11108 "protected:\n" 11109 " int j;\n" 11110 "};\n", 11111 "struct foo {\n" 11112 "private:\n" 11113 " void f() {}\n" 11114 "private:\n" 11115 " int i;\n" 11116 "protected:\n" 11117 " int j;\n" 11118 "};\n", 11119 Style); 11120 verifyFormat("struct foo { /* comment */\n" 11121 "private:\n" 11122 " int i;\n" 11123 " // comment\n" 11124 "private:\n" 11125 " int j;\n" 11126 "};\n", 11127 Style); 11128 verifyFormat("struct foo {\n" 11129 "#ifdef FOO\n" 11130 "#endif\n" 11131 "private:\n" 11132 " int i;\n" 11133 "#ifdef FOO\n" 11134 "private:\n" 11135 "#endif\n" 11136 " int j;\n" 11137 "};\n", 11138 Style); 11139 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11140 verifyFormat("struct foo {\n" 11141 "private:\n" 11142 " void f() {}\n" 11143 "private:\n" 11144 " int i;\n" 11145 "protected:\n" 11146 " int j;\n" 11147 "};\n", 11148 Style); 11149 verifyFormat("struct foo {\n" 11150 "private:\n" 11151 " void f() {}\n" 11152 "private:\n" 11153 " int i;\n" 11154 "protected:\n" 11155 " int j;\n" 11156 "};\n", 11157 "struct foo {\n" 11158 "\n" 11159 "private:\n" 11160 " void f() {}\n" 11161 "\n" 11162 "private:\n" 11163 " int i;\n" 11164 "\n" 11165 "protected:\n" 11166 " int j;\n" 11167 "};\n", 11168 Style); 11169 verifyFormat("struct foo { /* comment */\n" 11170 "private:\n" 11171 " int i;\n" 11172 " // comment\n" 11173 "private:\n" 11174 " int j;\n" 11175 "};\n", 11176 "struct foo { /* comment */\n" 11177 "\n" 11178 "private:\n" 11179 " int i;\n" 11180 " // comment\n" 11181 "\n" 11182 "private:\n" 11183 " int j;\n" 11184 "};\n", 11185 Style); 11186 verifyFormat("struct foo {\n" 11187 "#ifdef FOO\n" 11188 "#endif\n" 11189 "private:\n" 11190 " int i;\n" 11191 "#ifdef FOO\n" 11192 "private:\n" 11193 "#endif\n" 11194 " int j;\n" 11195 "};\n", 11196 "struct foo {\n" 11197 "#ifdef FOO\n" 11198 "#endif\n" 11199 "\n" 11200 "private:\n" 11201 " int i;\n" 11202 "#ifdef FOO\n" 11203 "\n" 11204 "private:\n" 11205 "#endif\n" 11206 " int j;\n" 11207 "};\n", 11208 Style); 11209 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11210 verifyFormat("struct foo {\n" 11211 "private:\n" 11212 " void f() {}\n" 11213 "\n" 11214 "private:\n" 11215 " int i;\n" 11216 "\n" 11217 "protected:\n" 11218 " int j;\n" 11219 "};\n", 11220 Style); 11221 verifyFormat("struct foo {\n" 11222 "private:\n" 11223 " void f() {}\n" 11224 "\n" 11225 "private:\n" 11226 " int i;\n" 11227 "\n" 11228 "protected:\n" 11229 " int j;\n" 11230 "};\n", 11231 "struct foo {\n" 11232 "private:\n" 11233 " void f() {}\n" 11234 "private:\n" 11235 " int i;\n" 11236 "protected:\n" 11237 " int j;\n" 11238 "};\n", 11239 Style); 11240 verifyFormat("struct foo { /* comment */\n" 11241 "private:\n" 11242 " int i;\n" 11243 " // comment\n" 11244 "\n" 11245 "private:\n" 11246 " int j;\n" 11247 "};\n", 11248 "struct foo { /* comment */\n" 11249 "private:\n" 11250 " int i;\n" 11251 " // comment\n" 11252 "\n" 11253 "private:\n" 11254 " int j;\n" 11255 "};\n", 11256 Style); 11257 verifyFormat("struct foo {\n" 11258 "#ifdef FOO\n" 11259 "#endif\n" 11260 "\n" 11261 "private:\n" 11262 " int i;\n" 11263 "#ifdef FOO\n" 11264 "\n" 11265 "private:\n" 11266 "#endif\n" 11267 " int j;\n" 11268 "};\n", 11269 "struct foo {\n" 11270 "#ifdef FOO\n" 11271 "#endif\n" 11272 "private:\n" 11273 " int i;\n" 11274 "#ifdef FOO\n" 11275 "private:\n" 11276 "#endif\n" 11277 " int j;\n" 11278 "};\n", 11279 Style); 11280 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11281 EXPECT_EQ("struct foo {\n" 11282 "\n" 11283 "private:\n" 11284 " void f() {}\n" 11285 "\n" 11286 "private:\n" 11287 " int i;\n" 11288 "\n" 11289 "protected:\n" 11290 " int j;\n" 11291 "};\n", 11292 format("struct foo {\n" 11293 "\n" 11294 "private:\n" 11295 " void f() {}\n" 11296 "\n" 11297 "private:\n" 11298 " int i;\n" 11299 "\n" 11300 "protected:\n" 11301 " int j;\n" 11302 "};\n", 11303 Style)); 11304 verifyFormat("struct foo {\n" 11305 "private:\n" 11306 " void f() {}\n" 11307 "private:\n" 11308 " int i;\n" 11309 "protected:\n" 11310 " int j;\n" 11311 "};\n", 11312 Style); 11313 EXPECT_EQ("struct foo { /* comment */\n" 11314 "\n" 11315 "private:\n" 11316 " int i;\n" 11317 " // comment\n" 11318 "\n" 11319 "private:\n" 11320 " int j;\n" 11321 "};\n", 11322 format("struct foo { /* comment */\n" 11323 "\n" 11324 "private:\n" 11325 " int i;\n" 11326 " // comment\n" 11327 "\n" 11328 "private:\n" 11329 " int j;\n" 11330 "};\n", 11331 Style)); 11332 verifyFormat("struct foo { /* comment */\n" 11333 "private:\n" 11334 " int i;\n" 11335 " // comment\n" 11336 "private:\n" 11337 " int j;\n" 11338 "};\n", 11339 Style); 11340 EXPECT_EQ("struct foo {\n" 11341 "#ifdef FOO\n" 11342 "#endif\n" 11343 "\n" 11344 "private:\n" 11345 " int i;\n" 11346 "#ifdef FOO\n" 11347 "\n" 11348 "private:\n" 11349 "#endif\n" 11350 " int j;\n" 11351 "};\n", 11352 format("struct foo {\n" 11353 "#ifdef FOO\n" 11354 "#endif\n" 11355 "\n" 11356 "private:\n" 11357 " int i;\n" 11358 "#ifdef FOO\n" 11359 "\n" 11360 "private:\n" 11361 "#endif\n" 11362 " int j;\n" 11363 "};\n", 11364 Style)); 11365 verifyFormat("struct foo {\n" 11366 "#ifdef FOO\n" 11367 "#endif\n" 11368 "private:\n" 11369 " int i;\n" 11370 "#ifdef FOO\n" 11371 "private:\n" 11372 "#endif\n" 11373 " int j;\n" 11374 "};\n", 11375 Style); 11376 11377 FormatStyle NoEmptyLines = getLLVMStyle(); 11378 NoEmptyLines.MaxEmptyLinesToKeep = 0; 11379 verifyFormat("struct foo {\n" 11380 "private:\n" 11381 " void f() {}\n" 11382 "\n" 11383 "private:\n" 11384 " int i;\n" 11385 "\n" 11386 "public:\n" 11387 "protected:\n" 11388 " int j;\n" 11389 "};\n", 11390 NoEmptyLines); 11391 11392 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11393 verifyFormat("struct foo {\n" 11394 "private:\n" 11395 " void f() {}\n" 11396 "private:\n" 11397 " int i;\n" 11398 "public:\n" 11399 "protected:\n" 11400 " int j;\n" 11401 "};\n", 11402 NoEmptyLines); 11403 11404 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11405 verifyFormat("struct foo {\n" 11406 "private:\n" 11407 " void f() {}\n" 11408 "\n" 11409 "private:\n" 11410 " int i;\n" 11411 "\n" 11412 "public:\n" 11413 "\n" 11414 "protected:\n" 11415 " int j;\n" 11416 "};\n", 11417 NoEmptyLines); 11418 } 11419 11420 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 11421 11422 FormatStyle Style = getLLVMStyle(); 11423 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 11424 verifyFormat("struct foo {\n" 11425 "private:\n" 11426 " void f() {}\n" 11427 "\n" 11428 "private:\n" 11429 " int i;\n" 11430 "\n" 11431 "protected:\n" 11432 " int j;\n" 11433 "};\n", 11434 Style); 11435 11436 // Check if lines are removed. 11437 verifyFormat("struct foo {\n" 11438 "private:\n" 11439 " void f() {}\n" 11440 "\n" 11441 "private:\n" 11442 " int i;\n" 11443 "\n" 11444 "protected:\n" 11445 " int j;\n" 11446 "};\n", 11447 "struct foo {\n" 11448 "private:\n" 11449 "\n" 11450 " void f() {}\n" 11451 "\n" 11452 "private:\n" 11453 "\n" 11454 " int i;\n" 11455 "\n" 11456 "protected:\n" 11457 "\n" 11458 " int j;\n" 11459 "};\n", 11460 Style); 11461 11462 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11463 verifyFormat("struct foo {\n" 11464 "private:\n" 11465 "\n" 11466 " void f() {}\n" 11467 "\n" 11468 "private:\n" 11469 "\n" 11470 " int i;\n" 11471 "\n" 11472 "protected:\n" 11473 "\n" 11474 " int j;\n" 11475 "};\n", 11476 Style); 11477 11478 // Check if lines are added. 11479 verifyFormat("struct foo {\n" 11480 "private:\n" 11481 "\n" 11482 " void f() {}\n" 11483 "\n" 11484 "private:\n" 11485 "\n" 11486 " int i;\n" 11487 "\n" 11488 "protected:\n" 11489 "\n" 11490 " int j;\n" 11491 "};\n", 11492 "struct foo {\n" 11493 "private:\n" 11494 " void f() {}\n" 11495 "\n" 11496 "private:\n" 11497 " int i;\n" 11498 "\n" 11499 "protected:\n" 11500 " int j;\n" 11501 "};\n", 11502 Style); 11503 11504 // Leave tests rely on the code layout, test::messUp can not be used. 11505 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11506 Style.MaxEmptyLinesToKeep = 0u; 11507 verifyFormat("struct foo {\n" 11508 "private:\n" 11509 " void f() {}\n" 11510 "\n" 11511 "private:\n" 11512 " int i;\n" 11513 "\n" 11514 "protected:\n" 11515 " int j;\n" 11516 "};\n", 11517 Style); 11518 11519 // Check if MaxEmptyLinesToKeep is respected. 11520 EXPECT_EQ("struct foo {\n" 11521 "private:\n" 11522 " void f() {}\n" 11523 "\n" 11524 "private:\n" 11525 " int i;\n" 11526 "\n" 11527 "protected:\n" 11528 " int j;\n" 11529 "};\n", 11530 format("struct foo {\n" 11531 "private:\n" 11532 "\n\n\n" 11533 " void f() {}\n" 11534 "\n" 11535 "private:\n" 11536 "\n\n\n" 11537 " int i;\n" 11538 "\n" 11539 "protected:\n" 11540 "\n\n\n" 11541 " int j;\n" 11542 "};\n", 11543 Style)); 11544 11545 Style.MaxEmptyLinesToKeep = 1u; 11546 EXPECT_EQ("struct foo {\n" 11547 "private:\n" 11548 "\n" 11549 " void f() {}\n" 11550 "\n" 11551 "private:\n" 11552 "\n" 11553 " int i;\n" 11554 "\n" 11555 "protected:\n" 11556 "\n" 11557 " int j;\n" 11558 "};\n", 11559 format("struct foo {\n" 11560 "private:\n" 11561 "\n" 11562 " void f() {}\n" 11563 "\n" 11564 "private:\n" 11565 "\n" 11566 " int i;\n" 11567 "\n" 11568 "protected:\n" 11569 "\n" 11570 " int j;\n" 11571 "};\n", 11572 Style)); 11573 // Check if no lines are kept. 11574 EXPECT_EQ("struct foo {\n" 11575 "private:\n" 11576 " void f() {}\n" 11577 "\n" 11578 "private:\n" 11579 " int i;\n" 11580 "\n" 11581 "protected:\n" 11582 " int j;\n" 11583 "};\n", 11584 format("struct foo {\n" 11585 "private:\n" 11586 " void f() {}\n" 11587 "\n" 11588 "private:\n" 11589 " int i;\n" 11590 "\n" 11591 "protected:\n" 11592 " int j;\n" 11593 "};\n", 11594 Style)); 11595 // Check if MaxEmptyLinesToKeep is respected. 11596 EXPECT_EQ("struct foo {\n" 11597 "private:\n" 11598 "\n" 11599 " void f() {}\n" 11600 "\n" 11601 "private:\n" 11602 "\n" 11603 " int i;\n" 11604 "\n" 11605 "protected:\n" 11606 "\n" 11607 " int j;\n" 11608 "};\n", 11609 format("struct foo {\n" 11610 "private:\n" 11611 "\n\n\n" 11612 " void f() {}\n" 11613 "\n" 11614 "private:\n" 11615 "\n\n\n" 11616 " int i;\n" 11617 "\n" 11618 "protected:\n" 11619 "\n\n\n" 11620 " int j;\n" 11621 "};\n", 11622 Style)); 11623 11624 Style.MaxEmptyLinesToKeep = 10u; 11625 EXPECT_EQ("struct foo {\n" 11626 "private:\n" 11627 "\n\n\n" 11628 " void f() {}\n" 11629 "\n" 11630 "private:\n" 11631 "\n\n\n" 11632 " int i;\n" 11633 "\n" 11634 "protected:\n" 11635 "\n\n\n" 11636 " int j;\n" 11637 "};\n", 11638 format("struct foo {\n" 11639 "private:\n" 11640 "\n\n\n" 11641 " void f() {}\n" 11642 "\n" 11643 "private:\n" 11644 "\n\n\n" 11645 " int i;\n" 11646 "\n" 11647 "protected:\n" 11648 "\n\n\n" 11649 " int j;\n" 11650 "};\n", 11651 Style)); 11652 11653 // Test with comments. 11654 Style = getLLVMStyle(); 11655 verifyFormat("struct foo {\n" 11656 "private:\n" 11657 " // comment\n" 11658 " void f() {}\n" 11659 "\n" 11660 "private: /* comment */\n" 11661 " int i;\n" 11662 "};\n", 11663 Style); 11664 verifyFormat("struct foo {\n" 11665 "private:\n" 11666 " // comment\n" 11667 " void f() {}\n" 11668 "\n" 11669 "private: /* comment */\n" 11670 " int i;\n" 11671 "};\n", 11672 "struct foo {\n" 11673 "private:\n" 11674 "\n" 11675 " // comment\n" 11676 " void f() {}\n" 11677 "\n" 11678 "private: /* comment */\n" 11679 "\n" 11680 " int i;\n" 11681 "};\n", 11682 Style); 11683 11684 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11685 verifyFormat("struct foo {\n" 11686 "private:\n" 11687 "\n" 11688 " // comment\n" 11689 " void f() {}\n" 11690 "\n" 11691 "private: /* comment */\n" 11692 "\n" 11693 " int i;\n" 11694 "};\n", 11695 "struct foo {\n" 11696 "private:\n" 11697 " // comment\n" 11698 " void f() {}\n" 11699 "\n" 11700 "private: /* comment */\n" 11701 " int i;\n" 11702 "};\n", 11703 Style); 11704 verifyFormat("struct foo {\n" 11705 "private:\n" 11706 "\n" 11707 " // comment\n" 11708 " void f() {}\n" 11709 "\n" 11710 "private: /* comment */\n" 11711 "\n" 11712 " int i;\n" 11713 "};\n", 11714 Style); 11715 11716 // Test with preprocessor defines. 11717 Style = getLLVMStyle(); 11718 verifyFormat("struct foo {\n" 11719 "private:\n" 11720 "#ifdef FOO\n" 11721 "#endif\n" 11722 " void f() {}\n" 11723 "};\n", 11724 Style); 11725 verifyFormat("struct foo {\n" 11726 "private:\n" 11727 "#ifdef FOO\n" 11728 "#endif\n" 11729 " void f() {}\n" 11730 "};\n", 11731 "struct foo {\n" 11732 "private:\n" 11733 "\n" 11734 "#ifdef FOO\n" 11735 "#endif\n" 11736 " void f() {}\n" 11737 "};\n", 11738 Style); 11739 11740 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11741 verifyFormat("struct foo {\n" 11742 "private:\n" 11743 "\n" 11744 "#ifdef FOO\n" 11745 "#endif\n" 11746 " void f() {}\n" 11747 "};\n", 11748 "struct foo {\n" 11749 "private:\n" 11750 "#ifdef FOO\n" 11751 "#endif\n" 11752 " void f() {}\n" 11753 "};\n", 11754 Style); 11755 verifyFormat("struct foo {\n" 11756 "private:\n" 11757 "\n" 11758 "#ifdef FOO\n" 11759 "#endif\n" 11760 " void f() {}\n" 11761 "};\n", 11762 Style); 11763 } 11764 11765 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 11766 // Combined tests of EmptyLineAfterAccessModifier and 11767 // EmptyLineBeforeAccessModifier. 11768 FormatStyle Style = getLLVMStyle(); 11769 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11770 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11771 verifyFormat("struct foo {\n" 11772 "private:\n" 11773 "\n" 11774 "protected:\n" 11775 "};\n", 11776 Style); 11777 11778 Style.MaxEmptyLinesToKeep = 10u; 11779 // Both remove all new lines. 11780 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11781 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11782 verifyFormat("struct foo {\n" 11783 "private:\n" 11784 "protected:\n" 11785 "};\n", 11786 "struct foo {\n" 11787 "private:\n" 11788 "\n\n\n" 11789 "protected:\n" 11790 "};\n", 11791 Style); 11792 11793 // Leave tests rely on the code layout, test::messUp can not be used. 11794 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11795 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11796 Style.MaxEmptyLinesToKeep = 10u; 11797 EXPECT_EQ("struct foo {\n" 11798 "private:\n" 11799 "\n\n\n" 11800 "protected:\n" 11801 "};\n", 11802 format("struct foo {\n" 11803 "private:\n" 11804 "\n\n\n" 11805 "protected:\n" 11806 "};\n", 11807 Style)); 11808 Style.MaxEmptyLinesToKeep = 3u; 11809 EXPECT_EQ("struct foo {\n" 11810 "private:\n" 11811 "\n\n\n" 11812 "protected:\n" 11813 "};\n", 11814 format("struct foo {\n" 11815 "private:\n" 11816 "\n\n\n" 11817 "protected:\n" 11818 "};\n", 11819 Style)); 11820 Style.MaxEmptyLinesToKeep = 1u; 11821 EXPECT_EQ("struct foo {\n" 11822 "private:\n" 11823 "\n\n\n" 11824 "protected:\n" 11825 "};\n", 11826 format("struct foo {\n" 11827 "private:\n" 11828 "\n\n\n" 11829 "protected:\n" 11830 "};\n", 11831 Style)); // Based on new lines in original document and not 11832 // on the setting. 11833 11834 Style.MaxEmptyLinesToKeep = 10u; 11835 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11836 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11837 // Newlines are kept if they are greater than zero, 11838 // test::messUp removes all new lines which changes the logic 11839 EXPECT_EQ("struct foo {\n" 11840 "private:\n" 11841 "\n\n\n" 11842 "protected:\n" 11843 "};\n", 11844 format("struct foo {\n" 11845 "private:\n" 11846 "\n\n\n" 11847 "protected:\n" 11848 "};\n", 11849 Style)); 11850 11851 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11852 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11853 // test::messUp removes all new lines which changes the logic 11854 EXPECT_EQ("struct foo {\n" 11855 "private:\n" 11856 "\n\n\n" 11857 "protected:\n" 11858 "};\n", 11859 format("struct foo {\n" 11860 "private:\n" 11861 "\n\n\n" 11862 "protected:\n" 11863 "};\n", 11864 Style)); 11865 11866 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11867 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11868 EXPECT_EQ("struct foo {\n" 11869 "private:\n" 11870 "\n\n\n" 11871 "protected:\n" 11872 "};\n", 11873 format("struct foo {\n" 11874 "private:\n" 11875 "\n\n\n" 11876 "protected:\n" 11877 "};\n", 11878 Style)); // test::messUp removes all new lines which changes 11879 // the logic. 11880 11881 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11882 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11883 verifyFormat("struct foo {\n" 11884 "private:\n" 11885 "protected:\n" 11886 "};\n", 11887 "struct foo {\n" 11888 "private:\n" 11889 "\n\n\n" 11890 "protected:\n" 11891 "};\n", 11892 Style); 11893 11894 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11895 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11896 EXPECT_EQ("struct foo {\n" 11897 "private:\n" 11898 "\n\n\n" 11899 "protected:\n" 11900 "};\n", 11901 format("struct foo {\n" 11902 "private:\n" 11903 "\n\n\n" 11904 "protected:\n" 11905 "};\n", 11906 Style)); // test::messUp removes all new lines which changes 11907 // the logic. 11908 11909 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11910 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11911 verifyFormat("struct foo {\n" 11912 "private:\n" 11913 "protected:\n" 11914 "};\n", 11915 "struct foo {\n" 11916 "private:\n" 11917 "\n\n\n" 11918 "protected:\n" 11919 "};\n", 11920 Style); 11921 11922 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11923 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11924 verifyFormat("struct foo {\n" 11925 "private:\n" 11926 "protected:\n" 11927 "};\n", 11928 "struct foo {\n" 11929 "private:\n" 11930 "\n\n\n" 11931 "protected:\n" 11932 "};\n", 11933 Style); 11934 11935 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11936 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11937 verifyFormat("struct foo {\n" 11938 "private:\n" 11939 "protected:\n" 11940 "};\n", 11941 "struct foo {\n" 11942 "private:\n" 11943 "\n\n\n" 11944 "protected:\n" 11945 "};\n", 11946 Style); 11947 11948 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11949 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11950 verifyFormat("struct foo {\n" 11951 "private:\n" 11952 "protected:\n" 11953 "};\n", 11954 "struct foo {\n" 11955 "private:\n" 11956 "\n\n\n" 11957 "protected:\n" 11958 "};\n", 11959 Style); 11960 } 11961 11962 TEST_F(FormatTest, FormatsArrays) { 11963 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11964 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11965 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11966 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11967 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11968 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11969 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11970 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11971 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11972 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11973 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11974 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11975 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11976 verifyFormat( 11977 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11978 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11979 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11980 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11981 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11982 11983 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11985 verifyFormat( 11986 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11987 " .aaaaaaa[0]\n" 11988 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11989 verifyFormat("a[::b::c];"); 11990 11991 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11992 11993 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11994 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11995 } 11996 11997 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11998 verifyFormat("(a)->b();"); 11999 verifyFormat("--a;"); 12000 } 12001 12002 TEST_F(FormatTest, HandlesIncludeDirectives) { 12003 verifyFormat("#include <string>\n" 12004 "#include <a/b/c.h>\n" 12005 "#include \"a/b/string\"\n" 12006 "#include \"string.h\"\n" 12007 "#include \"string.h\"\n" 12008 "#include <a-a>\n" 12009 "#include < path with space >\n" 12010 "#include_next <test.h>" 12011 "#include \"abc.h\" // this is included for ABC\n" 12012 "#include \"some long include\" // with a comment\n" 12013 "#include \"some very long include path\"\n" 12014 "#include <some/very/long/include/path>\n", 12015 getLLVMStyleWithColumns(35)); 12016 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 12017 EXPECT_EQ("#include <a>", format("#include<a>")); 12018 12019 verifyFormat("#import <string>"); 12020 verifyFormat("#import <a/b/c.h>"); 12021 verifyFormat("#import \"a/b/string\""); 12022 verifyFormat("#import \"string.h\""); 12023 verifyFormat("#import \"string.h\""); 12024 verifyFormat("#if __has_include(<strstream>)\n" 12025 "#include <strstream>\n" 12026 "#endif"); 12027 12028 verifyFormat("#define MY_IMPORT <a/b>"); 12029 12030 verifyFormat("#if __has_include(<a/b>)"); 12031 verifyFormat("#if __has_include_next(<a/b>)"); 12032 verifyFormat("#define F __has_include(<a/b>)"); 12033 verifyFormat("#define F __has_include_next(<a/b>)"); 12034 12035 // Protocol buffer definition or missing "#". 12036 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 12037 getLLVMStyleWithColumns(30)); 12038 12039 FormatStyle Style = getLLVMStyle(); 12040 Style.AlwaysBreakBeforeMultilineStrings = true; 12041 Style.ColumnLimit = 0; 12042 verifyFormat("#import \"abc.h\"", Style); 12043 12044 // But 'import' might also be a regular C++ namespace. 12045 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12046 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 12047 } 12048 12049 //===----------------------------------------------------------------------===// 12050 // Error recovery tests. 12051 //===----------------------------------------------------------------------===// 12052 12053 TEST_F(FormatTest, IncompleteParameterLists) { 12054 FormatStyle NoBinPacking = getLLVMStyle(); 12055 NoBinPacking.BinPackParameters = false; 12056 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 12057 " double *min_x,\n" 12058 " double *max_x,\n" 12059 " double *min_y,\n" 12060 " double *max_y,\n" 12061 " double *min_z,\n" 12062 " double *max_z, ) {}", 12063 NoBinPacking); 12064 } 12065 12066 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 12067 verifyFormat("void f() { return; }\n42"); 12068 verifyFormat("void f() {\n" 12069 " if (0)\n" 12070 " return;\n" 12071 "}\n" 12072 "42"); 12073 verifyFormat("void f() { return }\n42"); 12074 verifyFormat("void f() {\n" 12075 " if (0)\n" 12076 " return\n" 12077 "}\n" 12078 "42"); 12079 } 12080 12081 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 12082 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 12083 EXPECT_EQ("void f() {\n" 12084 " if (a)\n" 12085 " return\n" 12086 "}", 12087 format("void f ( ) { if ( a ) return }")); 12088 EXPECT_EQ("namespace N {\n" 12089 "void f()\n" 12090 "}", 12091 format("namespace N { void f() }")); 12092 EXPECT_EQ("namespace N {\n" 12093 "void f() {}\n" 12094 "void g()\n" 12095 "} // namespace N", 12096 format("namespace N { void f( ) { } void g( ) }")); 12097 } 12098 12099 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 12100 verifyFormat("int aaaaaaaa =\n" 12101 " // Overlylongcomment\n" 12102 " b;", 12103 getLLVMStyleWithColumns(20)); 12104 verifyFormat("function(\n" 12105 " ShortArgument,\n" 12106 " LoooooooooooongArgument);\n", 12107 getLLVMStyleWithColumns(20)); 12108 } 12109 12110 TEST_F(FormatTest, IncorrectAccessSpecifier) { 12111 verifyFormat("public:"); 12112 verifyFormat("class A {\n" 12113 "public\n" 12114 " void f() {}\n" 12115 "};"); 12116 verifyFormat("public\n" 12117 "int qwerty;"); 12118 verifyFormat("public\n" 12119 "B {}"); 12120 verifyFormat("public\n" 12121 "{}"); 12122 verifyFormat("public\n" 12123 "B { int x; }"); 12124 } 12125 12126 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 12127 verifyFormat("{"); 12128 verifyFormat("#})"); 12129 verifyNoCrash("(/**/[:!] ?[)."); 12130 } 12131 12132 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 12133 // Found by oss-fuzz: 12134 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 12135 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 12136 Style.ColumnLimit = 60; 12137 verifyNoCrash( 12138 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 12139 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 12140 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 12141 Style); 12142 } 12143 12144 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 12145 verifyFormat("do {\n}"); 12146 verifyFormat("do {\n}\n" 12147 "f();"); 12148 verifyFormat("do {\n}\n" 12149 "wheeee(fun);"); 12150 verifyFormat("do {\n" 12151 " f();\n" 12152 "}"); 12153 } 12154 12155 TEST_F(FormatTest, IncorrectCodeMissingParens) { 12156 verifyFormat("if {\n foo;\n foo();\n}"); 12157 verifyFormat("switch {\n foo;\n foo();\n}"); 12158 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 12159 verifyIncompleteFormat("ERROR: for target;"); 12160 verifyFormat("while {\n foo;\n foo();\n}"); 12161 verifyFormat("do {\n foo;\n foo();\n} while;"); 12162 } 12163 12164 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 12165 verifyIncompleteFormat("namespace {\n" 12166 "class Foo { Foo (\n" 12167 "};\n" 12168 "} // namespace"); 12169 } 12170 12171 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 12172 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 12173 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 12174 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 12175 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 12176 12177 EXPECT_EQ("{\n" 12178 " {\n" 12179 " breakme(\n" 12180 " qwe);\n" 12181 " }\n", 12182 format("{\n" 12183 " {\n" 12184 " breakme(qwe);\n" 12185 "}\n", 12186 getLLVMStyleWithColumns(10))); 12187 } 12188 12189 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 12190 verifyFormat("int x = {\n" 12191 " avariable,\n" 12192 " b(alongervariable)};", 12193 getLLVMStyleWithColumns(25)); 12194 } 12195 12196 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 12197 verifyFormat("return (a)(b){1, 2, 3};"); 12198 } 12199 12200 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 12201 verifyFormat("vector<int> x{1, 2, 3, 4};"); 12202 verifyFormat("vector<int> x{\n" 12203 " 1,\n" 12204 " 2,\n" 12205 " 3,\n" 12206 " 4,\n" 12207 "};"); 12208 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 12209 verifyFormat("f({1, 2});"); 12210 verifyFormat("auto v = Foo{-1};"); 12211 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 12212 verifyFormat("Class::Class : member{1, 2, 3} {}"); 12213 verifyFormat("new vector<int>{1, 2, 3};"); 12214 verifyFormat("new int[3]{1, 2, 3};"); 12215 verifyFormat("new int{1};"); 12216 verifyFormat("return {arg1, arg2};"); 12217 verifyFormat("return {arg1, SomeType{parameter}};"); 12218 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 12219 verifyFormat("new T{arg1, arg2};"); 12220 verifyFormat("f(MyMap[{composite, key}]);"); 12221 verifyFormat("class Class {\n" 12222 " T member = {arg1, arg2};\n" 12223 "};"); 12224 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 12225 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 12226 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 12227 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 12228 verifyFormat("int a = std::is_integral<int>{} + 0;"); 12229 12230 verifyFormat("int foo(int i) { return fo1{}(i); }"); 12231 verifyFormat("int foo(int i) { return fo1{}(i); }"); 12232 verifyFormat("auto i = decltype(x){};"); 12233 verifyFormat("auto i = typeof(x){};"); 12234 verifyFormat("auto i = _Atomic(x){};"); 12235 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 12236 verifyFormat("Node n{1, Node{1000}, //\n" 12237 " 2};"); 12238 verifyFormat("Aaaa aaaaaaa{\n" 12239 " {\n" 12240 " aaaa,\n" 12241 " },\n" 12242 "};"); 12243 verifyFormat("class C : public D {\n" 12244 " SomeClass SC{2};\n" 12245 "};"); 12246 verifyFormat("class C : public A {\n" 12247 " class D : public B {\n" 12248 " void f() { int i{2}; }\n" 12249 " };\n" 12250 "};"); 12251 verifyFormat("#define A {a, a},"); 12252 // Don't confuse braced list initializers with compound statements. 12253 verifyFormat( 12254 "class A {\n" 12255 " A() : a{} {}\n" 12256 " A(int b) : b(b) {}\n" 12257 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" 12258 " int a, b;\n" 12259 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" 12260 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " 12261 "{}\n" 12262 "};"); 12263 12264 // Avoid breaking between equal sign and opening brace 12265 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 12266 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 12267 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 12268 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 12269 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 12270 " {\"ccccccccccccccccccccc\", 2}};", 12271 AvoidBreakingFirstArgument); 12272 12273 // Binpacking only if there is no trailing comma 12274 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 12275 " cccccccccc, dddddddddd};", 12276 getLLVMStyleWithColumns(50)); 12277 verifyFormat("const Aaaaaa aaaaa = {\n" 12278 " aaaaaaaaaaa,\n" 12279 " bbbbbbbbbbb,\n" 12280 " ccccccccccc,\n" 12281 " ddddddddddd,\n" 12282 "};", 12283 getLLVMStyleWithColumns(50)); 12284 12285 // Cases where distinguising braced lists and blocks is hard. 12286 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 12287 verifyFormat("void f() {\n" 12288 " return; // comment\n" 12289 "}\n" 12290 "SomeType t;"); 12291 verifyFormat("void f() {\n" 12292 " if (a) {\n" 12293 " f();\n" 12294 " }\n" 12295 "}\n" 12296 "SomeType t;"); 12297 12298 // In combination with BinPackArguments = false. 12299 FormatStyle NoBinPacking = getLLVMStyle(); 12300 NoBinPacking.BinPackArguments = false; 12301 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 12302 " bbbbb,\n" 12303 " ccccc,\n" 12304 " ddddd,\n" 12305 " eeeee,\n" 12306 " ffffff,\n" 12307 " ggggg,\n" 12308 " hhhhhh,\n" 12309 " iiiiii,\n" 12310 " jjjjjj,\n" 12311 " kkkkkk};", 12312 NoBinPacking); 12313 verifyFormat("const Aaaaaa aaaaa = {\n" 12314 " aaaaa,\n" 12315 " bbbbb,\n" 12316 " ccccc,\n" 12317 " ddddd,\n" 12318 " eeeee,\n" 12319 " ffffff,\n" 12320 " ggggg,\n" 12321 " hhhhhh,\n" 12322 " iiiiii,\n" 12323 " jjjjjj,\n" 12324 " kkkkkk,\n" 12325 "};", 12326 NoBinPacking); 12327 verifyFormat( 12328 "const Aaaaaa aaaaa = {\n" 12329 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 12330 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 12331 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 12332 "};", 12333 NoBinPacking); 12334 12335 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12336 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 12337 " CDDDP83848_BMCR_REGISTER,\n" 12338 " CDDDP83848_BMSR_REGISTER,\n" 12339 " CDDDP83848_RBR_REGISTER};", 12340 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 12341 " CDDDP83848_BMSR_REGISTER,\n" 12342 " CDDDP83848_RBR_REGISTER};", 12343 NoBinPacking)); 12344 12345 // FIXME: The alignment of these trailing comments might be bad. Then again, 12346 // this might be utterly useless in real code. 12347 verifyFormat("Constructor::Constructor()\n" 12348 " : some_value{ //\n" 12349 " aaaaaaa, //\n" 12350 " bbbbbbb} {}"); 12351 12352 // In braced lists, the first comment is always assumed to belong to the 12353 // first element. Thus, it can be moved to the next or previous line as 12354 // appropriate. 12355 EXPECT_EQ("function({// First element:\n" 12356 " 1,\n" 12357 " // Second element:\n" 12358 " 2});", 12359 format("function({\n" 12360 " // First element:\n" 12361 " 1,\n" 12362 " // Second element:\n" 12363 " 2});")); 12364 EXPECT_EQ("std::vector<int> MyNumbers{\n" 12365 " // First element:\n" 12366 " 1,\n" 12367 " // Second element:\n" 12368 " 2};", 12369 format("std::vector<int> MyNumbers{// First element:\n" 12370 " 1,\n" 12371 " // Second element:\n" 12372 " 2};", 12373 getLLVMStyleWithColumns(30))); 12374 // A trailing comma should still lead to an enforced line break and no 12375 // binpacking. 12376 EXPECT_EQ("vector<int> SomeVector = {\n" 12377 " // aaa\n" 12378 " 1,\n" 12379 " 2,\n" 12380 "};", 12381 format("vector<int> SomeVector = { // aaa\n" 12382 " 1, 2, };")); 12383 12384 // C++11 brace initializer list l-braces should not be treated any differently 12385 // when breaking before lambda bodies is enabled 12386 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 12387 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 12388 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 12389 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 12390 verifyFormat( 12391 "std::runtime_error{\n" 12392 " \"Long string which will force a break onto the next line...\"};", 12393 BreakBeforeLambdaBody); 12394 12395 FormatStyle ExtraSpaces = getLLVMStyle(); 12396 ExtraSpaces.Cpp11BracedListStyle = false; 12397 ExtraSpaces.ColumnLimit = 75; 12398 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 12399 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 12400 verifyFormat("f({ 1, 2 });", ExtraSpaces); 12401 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 12402 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 12403 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 12404 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 12405 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 12406 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 12407 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 12408 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 12409 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 12410 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 12411 verifyFormat("class Class {\n" 12412 " T member = { arg1, arg2 };\n" 12413 "};", 12414 ExtraSpaces); 12415 verifyFormat( 12416 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12417 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 12418 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 12419 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 12420 ExtraSpaces); 12421 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 12422 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 12423 ExtraSpaces); 12424 verifyFormat( 12425 "someFunction(OtherParam,\n" 12426 " BracedList{ // comment 1 (Forcing interesting break)\n" 12427 " param1, param2,\n" 12428 " // comment 2\n" 12429 " param3, param4 });", 12430 ExtraSpaces); 12431 verifyFormat( 12432 "std::this_thread::sleep_for(\n" 12433 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 12434 ExtraSpaces); 12435 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 12436 " aaaaaaa,\n" 12437 " aaaaaaaaaa,\n" 12438 " aaaaa,\n" 12439 " aaaaaaaaaaaaaaa,\n" 12440 " aaa,\n" 12441 " aaaaaaaaaa,\n" 12442 " a,\n" 12443 " aaaaaaaaaaaaaaaaaaaaa,\n" 12444 " aaaaaaaaaaaa,\n" 12445 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 12446 " aaaaaaa,\n" 12447 " a};"); 12448 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 12449 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 12450 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 12451 12452 // Avoid breaking between initializer/equal sign and opening brace 12453 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 12454 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 12455 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 12456 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 12457 " { \"ccccccccccccccccccccc\", 2 }\n" 12458 "};", 12459 ExtraSpaces); 12460 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 12461 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 12462 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 12463 " { \"ccccccccccccccccccccc\", 2 }\n" 12464 "};", 12465 ExtraSpaces); 12466 12467 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 12468 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 12469 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 12470 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 12471 12472 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 12473 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 12474 SpaceBetweenBraces.SpacesInParentheses = true; 12475 SpaceBetweenBraces.SpacesInSquareBrackets = true; 12476 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 12477 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 12478 verifyFormat("vector< int > x{ // comment 1\n" 12479 " 1, 2, 3, 4 };", 12480 SpaceBetweenBraces); 12481 SpaceBetweenBraces.ColumnLimit = 20; 12482 EXPECT_EQ("vector< int > x{\n" 12483 " 1, 2, 3, 4 };", 12484 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 12485 SpaceBetweenBraces.ColumnLimit = 24; 12486 EXPECT_EQ("vector< int > x{ 1, 2,\n" 12487 " 3, 4 };", 12488 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 12489 EXPECT_EQ("vector< int > x{\n" 12490 " 1,\n" 12491 " 2,\n" 12492 " 3,\n" 12493 " 4,\n" 12494 "};", 12495 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 12496 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 12497 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 12498 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 12499 } 12500 12501 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 12502 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12503 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12504 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12505 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12506 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12507 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12508 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 12509 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12510 " 1, 22, 333, 4444, 55555, //\n" 12511 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12512 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12513 verifyFormat( 12514 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12515 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12516 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 12517 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12518 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12519 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12520 " 7777777};"); 12521 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12522 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12523 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12524 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12525 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12526 " // Separating comment.\n" 12527 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12528 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12529 " // Leading comment\n" 12530 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12531 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12532 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12533 " 1, 1, 1, 1};", 12534 getLLVMStyleWithColumns(39)); 12535 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12536 " 1, 1, 1, 1};", 12537 getLLVMStyleWithColumns(38)); 12538 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 12539 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 12540 getLLVMStyleWithColumns(43)); 12541 verifyFormat( 12542 "static unsigned SomeValues[10][3] = {\n" 12543 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 12544 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 12545 verifyFormat("static auto fields = new vector<string>{\n" 12546 " \"aaaaaaaaaaaaa\",\n" 12547 " \"aaaaaaaaaaaaa\",\n" 12548 " \"aaaaaaaaaaaa\",\n" 12549 " \"aaaaaaaaaaaaaa\",\n" 12550 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12551 " \"aaaaaaaaaaaa\",\n" 12552 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12553 "};"); 12554 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 12555 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 12556 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 12557 " 3, cccccccccccccccccccccc};", 12558 getLLVMStyleWithColumns(60)); 12559 12560 // Trailing commas. 12561 verifyFormat("vector<int> x = {\n" 12562 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 12563 "};", 12564 getLLVMStyleWithColumns(39)); 12565 verifyFormat("vector<int> x = {\n" 12566 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 12567 "};", 12568 getLLVMStyleWithColumns(39)); 12569 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12570 " 1, 1, 1, 1,\n" 12571 " /**/ /**/};", 12572 getLLVMStyleWithColumns(39)); 12573 12574 // Trailing comment in the first line. 12575 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 12576 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 12577 " 111111111, 222222222, 3333333333, 444444444, //\n" 12578 " 11111111, 22222222, 333333333, 44444444};"); 12579 // Trailing comment in the last line. 12580 verifyFormat("int aaaaa[] = {\n" 12581 " 1, 2, 3, // comment\n" 12582 " 4, 5, 6 // comment\n" 12583 "};"); 12584 12585 // With nested lists, we should either format one item per line or all nested 12586 // lists one on line. 12587 // FIXME: For some nested lists, we can do better. 12588 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 12589 " {aaaaaaaaaaaaaaaaaaa},\n" 12590 " {aaaaaaaaaaaaaaaaaaaaa},\n" 12591 " {aaaaaaaaaaaaaaaaa}};", 12592 getLLVMStyleWithColumns(60)); 12593 verifyFormat( 12594 "SomeStruct my_struct_array = {\n" 12595 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 12596 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 12597 " {aaa, aaa},\n" 12598 " {aaa, aaa},\n" 12599 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 12600 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 12601 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 12602 12603 // No column layout should be used here. 12604 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 12605 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 12606 12607 verifyNoCrash("a<,"); 12608 12609 // No braced initializer here. 12610 verifyFormat("void f() {\n" 12611 " struct Dummy {};\n" 12612 " f(v);\n" 12613 "}"); 12614 12615 // Long lists should be formatted in columns even if they are nested. 12616 verifyFormat( 12617 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12618 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12619 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12620 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12621 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12622 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 12623 12624 // Allow "single-column" layout even if that violates the column limit. There 12625 // isn't going to be a better way. 12626 verifyFormat("std::vector<int> a = {\n" 12627 " aaaaaaaa,\n" 12628 " aaaaaaaa,\n" 12629 " aaaaaaaa,\n" 12630 " aaaaaaaa,\n" 12631 " aaaaaaaaaa,\n" 12632 " aaaaaaaa,\n" 12633 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 12634 getLLVMStyleWithColumns(30)); 12635 verifyFormat("vector<int> aaaa = {\n" 12636 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12637 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12638 " aaaaaa.aaaaaaa,\n" 12639 " aaaaaa.aaaaaaa,\n" 12640 " aaaaaa.aaaaaaa,\n" 12641 " aaaaaa.aaaaaaa,\n" 12642 "};"); 12643 12644 // Don't create hanging lists. 12645 verifyFormat("someFunction(Param, {List1, List2,\n" 12646 " List3});", 12647 getLLVMStyleWithColumns(35)); 12648 verifyFormat("someFunction(Param, Param,\n" 12649 " {List1, List2,\n" 12650 " List3});", 12651 getLLVMStyleWithColumns(35)); 12652 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 12653 " aaaaaaaaaaaaaaaaaaaaaaa);"); 12654 } 12655 12656 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 12657 FormatStyle DoNotMerge = getLLVMStyle(); 12658 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12659 12660 verifyFormat("void f() { return 42; }"); 12661 verifyFormat("void f() {\n" 12662 " return 42;\n" 12663 "}", 12664 DoNotMerge); 12665 verifyFormat("void f() {\n" 12666 " // Comment\n" 12667 "}"); 12668 verifyFormat("{\n" 12669 "#error {\n" 12670 " int a;\n" 12671 "}"); 12672 verifyFormat("{\n" 12673 " int a;\n" 12674 "#error {\n" 12675 "}"); 12676 verifyFormat("void f() {} // comment"); 12677 verifyFormat("void f() { int a; } // comment"); 12678 verifyFormat("void f() {\n" 12679 "} // comment", 12680 DoNotMerge); 12681 verifyFormat("void f() {\n" 12682 " int a;\n" 12683 "} // comment", 12684 DoNotMerge); 12685 verifyFormat("void f() {\n" 12686 "} // comment", 12687 getLLVMStyleWithColumns(15)); 12688 12689 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 12690 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 12691 12692 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 12693 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 12694 verifyFormat("class C {\n" 12695 " C()\n" 12696 " : iiiiiiii(nullptr),\n" 12697 " kkkkkkk(nullptr),\n" 12698 " mmmmmmm(nullptr),\n" 12699 " nnnnnnn(nullptr) {}\n" 12700 "};", 12701 getGoogleStyle()); 12702 12703 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 12704 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 12705 EXPECT_EQ("class C {\n" 12706 " A() : b(0) {}\n" 12707 "};", 12708 format("class C{A():b(0){}};", NoColumnLimit)); 12709 EXPECT_EQ("A()\n" 12710 " : b(0) {\n" 12711 "}", 12712 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 12713 12714 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 12715 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 12716 FormatStyle::SFS_None; 12717 EXPECT_EQ("A()\n" 12718 " : b(0) {\n" 12719 "}", 12720 format("A():b(0){}", DoNotMergeNoColumnLimit)); 12721 EXPECT_EQ("A()\n" 12722 " : b(0) {\n" 12723 "}", 12724 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 12725 12726 verifyFormat("#define A \\\n" 12727 " void f() { \\\n" 12728 " int i; \\\n" 12729 " }", 12730 getLLVMStyleWithColumns(20)); 12731 verifyFormat("#define A \\\n" 12732 " void f() { int i; }", 12733 getLLVMStyleWithColumns(21)); 12734 verifyFormat("#define A \\\n" 12735 " void f() { \\\n" 12736 " int i; \\\n" 12737 " } \\\n" 12738 " int j;", 12739 getLLVMStyleWithColumns(22)); 12740 verifyFormat("#define A \\\n" 12741 " void f() { int i; } \\\n" 12742 " int j;", 12743 getLLVMStyleWithColumns(23)); 12744 } 12745 12746 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 12747 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12748 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12749 verifyFormat("class C {\n" 12750 " int f() {}\n" 12751 "};", 12752 MergeEmptyOnly); 12753 verifyFormat("class C {\n" 12754 " int f() {\n" 12755 " return 42;\n" 12756 " }\n" 12757 "};", 12758 MergeEmptyOnly); 12759 verifyFormat("int f() {}", MergeEmptyOnly); 12760 verifyFormat("int f() {\n" 12761 " return 42;\n" 12762 "}", 12763 MergeEmptyOnly); 12764 12765 // Also verify behavior when BraceWrapping.AfterFunction = true 12766 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12767 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 12768 verifyFormat("int f() {}", MergeEmptyOnly); 12769 verifyFormat("class C {\n" 12770 " int f() {}\n" 12771 "};", 12772 MergeEmptyOnly); 12773 } 12774 12775 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 12776 FormatStyle MergeInlineOnly = getLLVMStyle(); 12777 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12778 verifyFormat("class C {\n" 12779 " int f() { return 42; }\n" 12780 "};", 12781 MergeInlineOnly); 12782 verifyFormat("int f() {\n" 12783 " return 42;\n" 12784 "}", 12785 MergeInlineOnly); 12786 12787 // SFS_Inline implies SFS_Empty 12788 verifyFormat("class C {\n" 12789 " int f() {}\n" 12790 "};", 12791 MergeInlineOnly); 12792 verifyFormat("int f() {}", MergeInlineOnly); 12793 // https://llvm.org/PR54147 12794 verifyFormat("auto lambda = []() {\n" 12795 " // comment\n" 12796 " f();\n" 12797 " g();\n" 12798 "};", 12799 MergeInlineOnly); 12800 12801 verifyFormat("class C {\n" 12802 "#ifdef A\n" 12803 " int f() { return 42; }\n" 12804 "#endif\n" 12805 "};", 12806 MergeInlineOnly); 12807 12808 verifyFormat("struct S {\n" 12809 "// comment\n" 12810 "#ifdef FOO\n" 12811 " int foo() { bar(); }\n" 12812 "#endif\n" 12813 "};", 12814 MergeInlineOnly); 12815 12816 // Also verify behavior when BraceWrapping.AfterFunction = true 12817 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12818 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12819 verifyFormat("class C {\n" 12820 " int f() { return 42; }\n" 12821 "};", 12822 MergeInlineOnly); 12823 verifyFormat("int f()\n" 12824 "{\n" 12825 " return 42;\n" 12826 "}", 12827 MergeInlineOnly); 12828 12829 // SFS_Inline implies SFS_Empty 12830 verifyFormat("int f() {}", MergeInlineOnly); 12831 verifyFormat("class C {\n" 12832 " int f() {}\n" 12833 "};", 12834 MergeInlineOnly); 12835 12836 MergeInlineOnly.BraceWrapping.AfterClass = true; 12837 MergeInlineOnly.BraceWrapping.AfterStruct = true; 12838 verifyFormat("class C\n" 12839 "{\n" 12840 " int f() { return 42; }\n" 12841 "};", 12842 MergeInlineOnly); 12843 verifyFormat("struct C\n" 12844 "{\n" 12845 " int f() { return 42; }\n" 12846 "};", 12847 MergeInlineOnly); 12848 verifyFormat("int f()\n" 12849 "{\n" 12850 " return 42;\n" 12851 "}", 12852 MergeInlineOnly); 12853 verifyFormat("int f() {}", MergeInlineOnly); 12854 verifyFormat("class C\n" 12855 "{\n" 12856 " int f() { return 42; }\n" 12857 "};", 12858 MergeInlineOnly); 12859 verifyFormat("struct C\n" 12860 "{\n" 12861 " int f() { return 42; }\n" 12862 "};", 12863 MergeInlineOnly); 12864 verifyFormat("struct C\n" 12865 "// comment\n" 12866 "/* comment */\n" 12867 "// comment\n" 12868 "{\n" 12869 " int f() { return 42; }\n" 12870 "};", 12871 MergeInlineOnly); 12872 verifyFormat("/* comment */ struct C\n" 12873 "{\n" 12874 " int f() { return 42; }\n" 12875 "};", 12876 MergeInlineOnly); 12877 } 12878 12879 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 12880 FormatStyle MergeInlineOnly = getLLVMStyle(); 12881 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 12882 FormatStyle::SFS_InlineOnly; 12883 verifyFormat("class C {\n" 12884 " int f() { return 42; }\n" 12885 "};", 12886 MergeInlineOnly); 12887 verifyFormat("int f() {\n" 12888 " return 42;\n" 12889 "}", 12890 MergeInlineOnly); 12891 12892 // SFS_InlineOnly does not imply SFS_Empty 12893 verifyFormat("class C {\n" 12894 " int f() {}\n" 12895 "};", 12896 MergeInlineOnly); 12897 verifyFormat("int f() {\n" 12898 "}", 12899 MergeInlineOnly); 12900 12901 // Also verify behavior when BraceWrapping.AfterFunction = true 12902 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12903 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12904 verifyFormat("class C {\n" 12905 " int f() { return 42; }\n" 12906 "};", 12907 MergeInlineOnly); 12908 verifyFormat("int f()\n" 12909 "{\n" 12910 " return 42;\n" 12911 "}", 12912 MergeInlineOnly); 12913 12914 // SFS_InlineOnly does not imply SFS_Empty 12915 verifyFormat("int f()\n" 12916 "{\n" 12917 "}", 12918 MergeInlineOnly); 12919 verifyFormat("class C {\n" 12920 " int f() {}\n" 12921 "};", 12922 MergeInlineOnly); 12923 } 12924 12925 TEST_F(FormatTest, SplitEmptyFunction) { 12926 FormatStyle Style = getLLVMStyleWithColumns(40); 12927 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12928 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12929 Style.BraceWrapping.AfterFunction = true; 12930 Style.BraceWrapping.SplitEmptyFunction = false; 12931 12932 verifyFormat("int f()\n" 12933 "{}", 12934 Style); 12935 verifyFormat("int f()\n" 12936 "{\n" 12937 " return 42;\n" 12938 "}", 12939 Style); 12940 verifyFormat("int f()\n" 12941 "{\n" 12942 " // some comment\n" 12943 "}", 12944 Style); 12945 12946 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12947 verifyFormat("int f() {}", Style); 12948 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12949 "{}", 12950 Style); 12951 verifyFormat("int f()\n" 12952 "{\n" 12953 " return 0;\n" 12954 "}", 12955 Style); 12956 12957 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12958 verifyFormat("class Foo {\n" 12959 " int f() {}\n" 12960 "};\n", 12961 Style); 12962 verifyFormat("class Foo {\n" 12963 " int f() { return 0; }\n" 12964 "};\n", 12965 Style); 12966 verifyFormat("class Foo {\n" 12967 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12968 " {}\n" 12969 "};\n", 12970 Style); 12971 verifyFormat("class Foo {\n" 12972 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12973 " {\n" 12974 " return 0;\n" 12975 " }\n" 12976 "};\n", 12977 Style); 12978 12979 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12980 verifyFormat("int f() {}", Style); 12981 verifyFormat("int f() { return 0; }", Style); 12982 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12983 "{}", 12984 Style); 12985 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12986 "{\n" 12987 " return 0;\n" 12988 "}", 12989 Style); 12990 } 12991 12992 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { 12993 FormatStyle Style = getLLVMStyleWithColumns(40); 12994 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12995 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12996 Style.BraceWrapping.AfterFunction = true; 12997 Style.BraceWrapping.SplitEmptyFunction = true; 12998 Style.BraceWrapping.SplitEmptyRecord = false; 12999 13000 verifyFormat("class C {};", Style); 13001 verifyFormat("struct C {};", Style); 13002 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 13003 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 13004 "{\n" 13005 "}", 13006 Style); 13007 verifyFormat("class C {\n" 13008 " C()\n" 13009 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" 13010 " bbbbbbbbbbbbbbbbbbb()\n" 13011 " {\n" 13012 " }\n" 13013 " void\n" 13014 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 13015 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 13016 " {\n" 13017 " }\n" 13018 "};", 13019 Style); 13020 } 13021 13022 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 13023 FormatStyle Style = getLLVMStyle(); 13024 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 13025 verifyFormat("#ifdef A\n" 13026 "int f() {}\n" 13027 "#else\n" 13028 "int g() {}\n" 13029 "#endif", 13030 Style); 13031 } 13032 13033 TEST_F(FormatTest, SplitEmptyClass) { 13034 FormatStyle Style = getLLVMStyle(); 13035 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 13036 Style.BraceWrapping.AfterClass = true; 13037 Style.BraceWrapping.SplitEmptyRecord = false; 13038 13039 verifyFormat("class Foo\n" 13040 "{};", 13041 Style); 13042 verifyFormat("/* something */ class Foo\n" 13043 "{};", 13044 Style); 13045 verifyFormat("template <typename X> class Foo\n" 13046 "{};", 13047 Style); 13048 verifyFormat("class Foo\n" 13049 "{\n" 13050 " Foo();\n" 13051 "};", 13052 Style); 13053 verifyFormat("typedef class Foo\n" 13054 "{\n" 13055 "} Foo_t;", 13056 Style); 13057 13058 Style.BraceWrapping.SplitEmptyRecord = true; 13059 Style.BraceWrapping.AfterStruct = true; 13060 verifyFormat("class rep\n" 13061 "{\n" 13062 "};", 13063 Style); 13064 verifyFormat("struct rep\n" 13065 "{\n" 13066 "};", 13067 Style); 13068 verifyFormat("template <typename T> class rep\n" 13069 "{\n" 13070 "};", 13071 Style); 13072 verifyFormat("template <typename T> struct rep\n" 13073 "{\n" 13074 "};", 13075 Style); 13076 verifyFormat("class rep\n" 13077 "{\n" 13078 " int x;\n" 13079 "};", 13080 Style); 13081 verifyFormat("struct rep\n" 13082 "{\n" 13083 " int x;\n" 13084 "};", 13085 Style); 13086 verifyFormat("template <typename T> class rep\n" 13087 "{\n" 13088 " int x;\n" 13089 "};", 13090 Style); 13091 verifyFormat("template <typename T> struct rep\n" 13092 "{\n" 13093 " int x;\n" 13094 "};", 13095 Style); 13096 verifyFormat("template <typename T> class rep // Foo\n" 13097 "{\n" 13098 " int x;\n" 13099 "};", 13100 Style); 13101 verifyFormat("template <typename T> struct rep // Bar\n" 13102 "{\n" 13103 " int x;\n" 13104 "};", 13105 Style); 13106 13107 verifyFormat("template <typename T> class rep<T>\n" 13108 "{\n" 13109 " int x;\n" 13110 "};", 13111 Style); 13112 13113 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 13114 "{\n" 13115 " int x;\n" 13116 "};", 13117 Style); 13118 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 13119 "{\n" 13120 "};", 13121 Style); 13122 13123 verifyFormat("#include \"stdint.h\"\n" 13124 "namespace rep {}", 13125 Style); 13126 verifyFormat("#include <stdint.h>\n" 13127 "namespace rep {}", 13128 Style); 13129 verifyFormat("#include <stdint.h>\n" 13130 "namespace rep {}", 13131 "#include <stdint.h>\n" 13132 "namespace rep {\n" 13133 "\n" 13134 "\n" 13135 "}", 13136 Style); 13137 } 13138 13139 TEST_F(FormatTest, SplitEmptyStruct) { 13140 FormatStyle Style = getLLVMStyle(); 13141 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 13142 Style.BraceWrapping.AfterStruct = true; 13143 Style.BraceWrapping.SplitEmptyRecord = false; 13144 13145 verifyFormat("struct Foo\n" 13146 "{};", 13147 Style); 13148 verifyFormat("/* something */ struct Foo\n" 13149 "{};", 13150 Style); 13151 verifyFormat("template <typename X> struct Foo\n" 13152 "{};", 13153 Style); 13154 verifyFormat("struct Foo\n" 13155 "{\n" 13156 " Foo();\n" 13157 "};", 13158 Style); 13159 verifyFormat("typedef struct Foo\n" 13160 "{\n" 13161 "} Foo_t;", 13162 Style); 13163 // typedef struct Bar {} Bar_t; 13164 } 13165 13166 TEST_F(FormatTest, SplitEmptyUnion) { 13167 FormatStyle Style = getLLVMStyle(); 13168 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 13169 Style.BraceWrapping.AfterUnion = true; 13170 Style.BraceWrapping.SplitEmptyRecord = false; 13171 13172 verifyFormat("union Foo\n" 13173 "{};", 13174 Style); 13175 verifyFormat("/* something */ union Foo\n" 13176 "{};", 13177 Style); 13178 verifyFormat("union Foo\n" 13179 "{\n" 13180 " A,\n" 13181 "};", 13182 Style); 13183 verifyFormat("typedef union Foo\n" 13184 "{\n" 13185 "} Foo_t;", 13186 Style); 13187 } 13188 13189 TEST_F(FormatTest, SplitEmptyNamespace) { 13190 FormatStyle Style = getLLVMStyle(); 13191 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 13192 Style.BraceWrapping.AfterNamespace = true; 13193 Style.BraceWrapping.SplitEmptyNamespace = false; 13194 13195 verifyFormat("namespace Foo\n" 13196 "{};", 13197 Style); 13198 verifyFormat("/* something */ namespace Foo\n" 13199 "{};", 13200 Style); 13201 verifyFormat("inline namespace Foo\n" 13202 "{};", 13203 Style); 13204 verifyFormat("/* something */ inline namespace Foo\n" 13205 "{};", 13206 Style); 13207 verifyFormat("export namespace Foo\n" 13208 "{};", 13209 Style); 13210 verifyFormat("namespace Foo\n" 13211 "{\n" 13212 "void Bar();\n" 13213 "};", 13214 Style); 13215 } 13216 13217 TEST_F(FormatTest, NeverMergeShortRecords) { 13218 FormatStyle Style = getLLVMStyle(); 13219 13220 verifyFormat("class Foo {\n" 13221 " Foo();\n" 13222 "};", 13223 Style); 13224 verifyFormat("typedef class Foo {\n" 13225 " Foo();\n" 13226 "} Foo_t;", 13227 Style); 13228 verifyFormat("struct Foo {\n" 13229 " Foo();\n" 13230 "};", 13231 Style); 13232 verifyFormat("typedef struct Foo {\n" 13233 " Foo();\n" 13234 "} Foo_t;", 13235 Style); 13236 verifyFormat("union Foo {\n" 13237 " A,\n" 13238 "};", 13239 Style); 13240 verifyFormat("typedef union Foo {\n" 13241 " A,\n" 13242 "} Foo_t;", 13243 Style); 13244 verifyFormat("namespace Foo {\n" 13245 "void Bar();\n" 13246 "};", 13247 Style); 13248 13249 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 13250 Style.BraceWrapping.AfterClass = true; 13251 Style.BraceWrapping.AfterStruct = true; 13252 Style.BraceWrapping.AfterUnion = true; 13253 Style.BraceWrapping.AfterNamespace = true; 13254 verifyFormat("class Foo\n" 13255 "{\n" 13256 " Foo();\n" 13257 "};", 13258 Style); 13259 verifyFormat("typedef class Foo\n" 13260 "{\n" 13261 " Foo();\n" 13262 "} Foo_t;", 13263 Style); 13264 verifyFormat("struct Foo\n" 13265 "{\n" 13266 " Foo();\n" 13267 "};", 13268 Style); 13269 verifyFormat("typedef struct Foo\n" 13270 "{\n" 13271 " Foo();\n" 13272 "} Foo_t;", 13273 Style); 13274 verifyFormat("union Foo\n" 13275 "{\n" 13276 " A,\n" 13277 "};", 13278 Style); 13279 verifyFormat("typedef union Foo\n" 13280 "{\n" 13281 " A,\n" 13282 "} Foo_t;", 13283 Style); 13284 verifyFormat("namespace Foo\n" 13285 "{\n" 13286 "void Bar();\n" 13287 "};", 13288 Style); 13289 } 13290 13291 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 13292 // Elaborate type variable declarations. 13293 verifyFormat("struct foo a = {bar};\nint n;"); 13294 verifyFormat("class foo a = {bar};\nint n;"); 13295 verifyFormat("union foo a = {bar};\nint n;"); 13296 13297 // Elaborate types inside function definitions. 13298 verifyFormat("struct foo f() {}\nint n;"); 13299 verifyFormat("class foo f() {}\nint n;"); 13300 verifyFormat("union foo f() {}\nint n;"); 13301 13302 // Templates. 13303 verifyFormat("template <class X> void f() {}\nint n;"); 13304 verifyFormat("template <struct X> void f() {}\nint n;"); 13305 verifyFormat("template <union X> void f() {}\nint n;"); 13306 13307 // Actual definitions... 13308 verifyFormat("struct {\n} n;"); 13309 verifyFormat( 13310 "template <template <class T, class Y>, class Z> class X {\n} n;"); 13311 verifyFormat("union Z {\n int n;\n} x;"); 13312 verifyFormat("class MACRO Z {\n} n;"); 13313 verifyFormat("class MACRO(X) Z {\n} n;"); 13314 verifyFormat("class __attribute__(X) Z {\n} n;"); 13315 verifyFormat("class __declspec(X) Z {\n} n;"); 13316 verifyFormat("class A##B##C {\n} n;"); 13317 verifyFormat("class alignas(16) Z {\n} n;"); 13318 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 13319 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 13320 13321 // Redefinition from nested context: 13322 verifyFormat("class A::B::C {\n} n;"); 13323 13324 // Template definitions. 13325 verifyFormat( 13326 "template <typename F>\n" 13327 "Matcher(const Matcher<F> &Other,\n" 13328 " typename enable_if_c<is_base_of<F, T>::value &&\n" 13329 " !is_same<F, T>::value>::type * = 0)\n" 13330 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 13331 13332 // FIXME: This is still incorrectly handled at the formatter side. 13333 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 13334 verifyFormat("int i = SomeFunction(a<b, a> b);"); 13335 13336 // FIXME: 13337 // This now gets parsed incorrectly as class definition. 13338 // verifyFormat("class A<int> f() {\n}\nint n;"); 13339 13340 // Elaborate types where incorrectly parsing the structural element would 13341 // break the indent. 13342 verifyFormat("if (true)\n" 13343 " class X x;\n" 13344 "else\n" 13345 " f();\n"); 13346 13347 // This is simply incomplete. Formatting is not important, but must not crash. 13348 verifyFormat("class A:"); 13349 } 13350 13351 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 13352 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 13353 format("#error Leave all white!!!!! space* alone!\n")); 13354 EXPECT_EQ( 13355 "#warning Leave all white!!!!! space* alone!\n", 13356 format("#warning Leave all white!!!!! space* alone!\n")); 13357 EXPECT_EQ("#error 1", format(" # error 1")); 13358 EXPECT_EQ("#warning 1", format(" # warning 1")); 13359 } 13360 13361 TEST_F(FormatTest, FormatHashIfExpressions) { 13362 verifyFormat("#if AAAA && BBBB"); 13363 verifyFormat("#if (AAAA && BBBB)"); 13364 verifyFormat("#elif (AAAA && BBBB)"); 13365 // FIXME: Come up with a better indentation for #elif. 13366 verifyFormat( 13367 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 13368 " defined(BBBBBBBB)\n" 13369 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 13370 " defined(BBBBBBBB)\n" 13371 "#endif", 13372 getLLVMStyleWithColumns(65)); 13373 } 13374 13375 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 13376 FormatStyle AllowsMergedIf = getGoogleStyle(); 13377 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 13378 FormatStyle::SIS_WithoutElse; 13379 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 13380 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 13381 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 13382 EXPECT_EQ("if (true) return 42;", 13383 format("if (true)\nreturn 42;", AllowsMergedIf)); 13384 FormatStyle ShortMergedIf = AllowsMergedIf; 13385 ShortMergedIf.ColumnLimit = 25; 13386 verifyFormat("#define A \\\n" 13387 " if (true) return 42;", 13388 ShortMergedIf); 13389 verifyFormat("#define A \\\n" 13390 " f(); \\\n" 13391 " if (true)\n" 13392 "#define B", 13393 ShortMergedIf); 13394 verifyFormat("#define A \\\n" 13395 " f(); \\\n" 13396 " if (true)\n" 13397 "g();", 13398 ShortMergedIf); 13399 verifyFormat("{\n" 13400 "#ifdef A\n" 13401 " // Comment\n" 13402 " if (true) continue;\n" 13403 "#endif\n" 13404 " // Comment\n" 13405 " if (true) continue;\n" 13406 "}", 13407 ShortMergedIf); 13408 ShortMergedIf.ColumnLimit = 33; 13409 verifyFormat("#define A \\\n" 13410 " if constexpr (true) return 42;", 13411 ShortMergedIf); 13412 verifyFormat("#define A \\\n" 13413 " if CONSTEXPR (true) return 42;", 13414 ShortMergedIf); 13415 ShortMergedIf.ColumnLimit = 29; 13416 verifyFormat("#define A \\\n" 13417 " if (aaaaaaaaaa) return 1; \\\n" 13418 " return 2;", 13419 ShortMergedIf); 13420 ShortMergedIf.ColumnLimit = 28; 13421 verifyFormat("#define A \\\n" 13422 " if (aaaaaaaaaa) \\\n" 13423 " return 1; \\\n" 13424 " return 2;", 13425 ShortMergedIf); 13426 verifyFormat("#define A \\\n" 13427 " if constexpr (aaaaaaa) \\\n" 13428 " return 1; \\\n" 13429 " return 2;", 13430 ShortMergedIf); 13431 verifyFormat("#define A \\\n" 13432 " if CONSTEXPR (aaaaaaa) \\\n" 13433 " return 1; \\\n" 13434 " return 2;", 13435 ShortMergedIf); 13436 13437 verifyFormat("//\n" 13438 "#define a \\\n" 13439 " if \\\n" 13440 " 0", 13441 getChromiumStyle(FormatStyle::LK_Cpp)); 13442 } 13443 13444 TEST_F(FormatTest, FormatStarDependingOnContext) { 13445 verifyFormat("void f(int *a);"); 13446 verifyFormat("void f() { f(fint * b); }"); 13447 verifyFormat("class A {\n void f(int *a);\n};"); 13448 verifyFormat("class A {\n int *a;\n};"); 13449 verifyFormat("namespace a {\n" 13450 "namespace b {\n" 13451 "class A {\n" 13452 " void f() {}\n" 13453 " int *a;\n" 13454 "};\n" 13455 "} // namespace b\n" 13456 "} // namespace a"); 13457 } 13458 13459 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 13460 verifyFormat("while"); 13461 verifyFormat("operator"); 13462 } 13463 13464 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 13465 // This code would be painfully slow to format if we didn't skip it. 13466 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 13467 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13468 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13469 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13470 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13471 "A(1, 1)\n" 13472 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 13473 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13474 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13475 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13476 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13477 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13478 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13479 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13480 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13481 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 13482 // Deeply nested part is untouched, rest is formatted. 13483 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 13484 format(std::string("int i;\n") + Code + "int j;\n", 13485 getLLVMStyle(), SC_ExpectIncomplete)); 13486 } 13487 13488 //===----------------------------------------------------------------------===// 13489 // Objective-C tests. 13490 //===----------------------------------------------------------------------===// 13491 13492 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 13493 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 13494 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 13495 format("-(NSUInteger)indexOfObject:(id)anObject;")); 13496 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 13497 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 13498 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 13499 format("-(NSInteger)Method3:(id)anObject;")); 13500 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 13501 format("-(NSInteger)Method4:(id)anObject;")); 13502 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 13503 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 13504 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 13505 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 13506 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 13507 "forAllCells:(BOOL)flag;", 13508 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 13509 "forAllCells:(BOOL)flag;")); 13510 13511 // Very long objectiveC method declaration. 13512 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 13513 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 13514 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 13515 " inRange:(NSRange)range\n" 13516 " outRange:(NSRange)out_range\n" 13517 " outRange1:(NSRange)out_range1\n" 13518 " outRange2:(NSRange)out_range2\n" 13519 " outRange3:(NSRange)out_range3\n" 13520 " outRange4:(NSRange)out_range4\n" 13521 " outRange5:(NSRange)out_range5\n" 13522 " outRange6:(NSRange)out_range6\n" 13523 " outRange7:(NSRange)out_range7\n" 13524 " outRange8:(NSRange)out_range8\n" 13525 " outRange9:(NSRange)out_range9;"); 13526 13527 // When the function name has to be wrapped. 13528 FormatStyle Style = getLLVMStyle(); 13529 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 13530 // and always indents instead. 13531 Style.IndentWrappedFunctionNames = false; 13532 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13533 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 13534 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 13535 "}", 13536 Style); 13537 Style.IndentWrappedFunctionNames = true; 13538 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13539 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 13540 " anotherName:(NSString)dddddddddddddd {\n" 13541 "}", 13542 Style); 13543 13544 verifyFormat("- (int)sum:(vector<int>)numbers;"); 13545 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 13546 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 13547 // protocol lists (but not for template classes): 13548 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 13549 13550 verifyFormat("- (int (*)())foo:(int (*)())f;"); 13551 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 13552 13553 // If there's no return type (very rare in practice!), LLVM and Google style 13554 // agree. 13555 verifyFormat("- foo;"); 13556 verifyFormat("- foo:(int)f;"); 13557 verifyGoogleFormat("- foo:(int)foo;"); 13558 } 13559 13560 TEST_F(FormatTest, BreaksStringLiterals) { 13561 EXPECT_EQ("\"some text \"\n" 13562 "\"other\";", 13563 format("\"some text other\";", getLLVMStyleWithColumns(12))); 13564 EXPECT_EQ("\"some text \"\n" 13565 "\"other\";", 13566 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 13567 EXPECT_EQ( 13568 "#define A \\\n" 13569 " \"some \" \\\n" 13570 " \"text \" \\\n" 13571 " \"other\";", 13572 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 13573 EXPECT_EQ( 13574 "#define A \\\n" 13575 " \"so \" \\\n" 13576 " \"text \" \\\n" 13577 " \"other\";", 13578 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 13579 13580 EXPECT_EQ("\"some text\"", 13581 format("\"some text\"", getLLVMStyleWithColumns(1))); 13582 EXPECT_EQ("\"some text\"", 13583 format("\"some text\"", getLLVMStyleWithColumns(11))); 13584 EXPECT_EQ("\"some \"\n" 13585 "\"text\"", 13586 format("\"some text\"", getLLVMStyleWithColumns(10))); 13587 EXPECT_EQ("\"some \"\n" 13588 "\"text\"", 13589 format("\"some text\"", getLLVMStyleWithColumns(7))); 13590 EXPECT_EQ("\"some\"\n" 13591 "\" tex\"\n" 13592 "\"t\"", 13593 format("\"some text\"", getLLVMStyleWithColumns(6))); 13594 EXPECT_EQ("\"some\"\n" 13595 "\" tex\"\n" 13596 "\" and\"", 13597 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 13598 EXPECT_EQ("\"some\"\n" 13599 "\"/tex\"\n" 13600 "\"/and\"", 13601 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 13602 13603 EXPECT_EQ("variable =\n" 13604 " \"long string \"\n" 13605 " \"literal\";", 13606 format("variable = \"long string literal\";", 13607 getLLVMStyleWithColumns(20))); 13608 13609 EXPECT_EQ("variable = f(\n" 13610 " \"long string \"\n" 13611 " \"literal\",\n" 13612 " short,\n" 13613 " loooooooooooooooooooong);", 13614 format("variable = f(\"long string literal\", short, " 13615 "loooooooooooooooooooong);", 13616 getLLVMStyleWithColumns(20))); 13617 13618 EXPECT_EQ( 13619 "f(g(\"long string \"\n" 13620 " \"literal\"),\n" 13621 " b);", 13622 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 13623 EXPECT_EQ("f(g(\"long string \"\n" 13624 " \"literal\",\n" 13625 " a),\n" 13626 " b);", 13627 format("f(g(\"long string literal\", a), b);", 13628 getLLVMStyleWithColumns(20))); 13629 EXPECT_EQ( 13630 "f(\"one two\".split(\n" 13631 " variable));", 13632 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 13633 EXPECT_EQ("f(\"one two three four five six \"\n" 13634 " \"seven\".split(\n" 13635 " really_looooong_variable));", 13636 format("f(\"one two three four five six seven\"." 13637 "split(really_looooong_variable));", 13638 getLLVMStyleWithColumns(33))); 13639 13640 EXPECT_EQ("f(\"some \"\n" 13641 " \"text\",\n" 13642 " other);", 13643 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 13644 13645 // Only break as a last resort. 13646 verifyFormat( 13647 "aaaaaaaaaaaaaaaaaaaa(\n" 13648 " aaaaaaaaaaaaaaaaaaaa,\n" 13649 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 13650 13651 EXPECT_EQ("\"splitmea\"\n" 13652 "\"trandomp\"\n" 13653 "\"oint\"", 13654 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 13655 13656 EXPECT_EQ("\"split/\"\n" 13657 "\"pathat/\"\n" 13658 "\"slashes\"", 13659 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13660 13661 EXPECT_EQ("\"split/\"\n" 13662 "\"pathat/\"\n" 13663 "\"slashes\"", 13664 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13665 EXPECT_EQ("\"split at \"\n" 13666 "\"spaces/at/\"\n" 13667 "\"slashes.at.any$\"\n" 13668 "\"non-alphanumeric%\"\n" 13669 "\"1111111111characte\"\n" 13670 "\"rs\"", 13671 format("\"split at " 13672 "spaces/at/" 13673 "slashes.at." 13674 "any$non-" 13675 "alphanumeric%" 13676 "1111111111characte" 13677 "rs\"", 13678 getLLVMStyleWithColumns(20))); 13679 13680 // Verify that splitting the strings understands 13681 // Style::AlwaysBreakBeforeMultilineStrings. 13682 EXPECT_EQ("aaaaaaaaaaaa(\n" 13683 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 13684 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 13685 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 13686 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13687 "aaaaaaaaaaaaaaaaaaaaaa\");", 13688 getGoogleStyle())); 13689 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13690 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 13691 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 13692 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13693 "aaaaaaaaaaaaaaaaaaaaaa\";", 13694 getGoogleStyle())); 13695 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13696 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13697 format("llvm::outs() << " 13698 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 13699 "aaaaaaaaaaaaaaaaaaa\";")); 13700 EXPECT_EQ("ffff(\n" 13701 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13702 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13703 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 13704 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13705 getGoogleStyle())); 13706 13707 FormatStyle Style = getLLVMStyleWithColumns(12); 13708 Style.BreakStringLiterals = false; 13709 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 13710 13711 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 13712 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13713 EXPECT_EQ("#define A \\\n" 13714 " \"some \" \\\n" 13715 " \"text \" \\\n" 13716 " \"other\";", 13717 format("#define A \"some text other\";", AlignLeft)); 13718 } 13719 13720 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 13721 EXPECT_EQ("C a = \"some more \"\n" 13722 " \"text\";", 13723 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 13724 } 13725 13726 TEST_F(FormatTest, FullyRemoveEmptyLines) { 13727 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 13728 NoEmptyLines.MaxEmptyLinesToKeep = 0; 13729 EXPECT_EQ("int i = a(b());", 13730 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 13731 } 13732 13733 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 13734 EXPECT_EQ( 13735 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13736 "(\n" 13737 " \"x\t\");", 13738 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13739 "aaaaaaa(" 13740 "\"x\t\");")); 13741 } 13742 13743 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 13744 EXPECT_EQ( 13745 "u8\"utf8 string \"\n" 13746 "u8\"literal\";", 13747 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 13748 EXPECT_EQ( 13749 "u\"utf16 string \"\n" 13750 "u\"literal\";", 13751 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 13752 EXPECT_EQ( 13753 "U\"utf32 string \"\n" 13754 "U\"literal\";", 13755 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 13756 EXPECT_EQ("L\"wide string \"\n" 13757 "L\"literal\";", 13758 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 13759 EXPECT_EQ("@\"NSString \"\n" 13760 "@\"literal\";", 13761 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 13762 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 13763 13764 // This input makes clang-format try to split the incomplete unicode escape 13765 // sequence, which used to lead to a crasher. 13766 verifyNoCrash( 13767 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 13768 getLLVMStyleWithColumns(60)); 13769 } 13770 13771 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 13772 FormatStyle Style = getGoogleStyleWithColumns(15); 13773 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 13774 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 13775 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 13776 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 13777 EXPECT_EQ("u8R\"x(raw literal)x\";", 13778 format("u8R\"x(raw literal)x\";", Style)); 13779 } 13780 13781 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 13782 FormatStyle Style = getLLVMStyleWithColumns(20); 13783 EXPECT_EQ( 13784 "_T(\"aaaaaaaaaaaaaa\")\n" 13785 "_T(\"aaaaaaaaaaaaaa\")\n" 13786 "_T(\"aaaaaaaaaaaa\")", 13787 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 13788 EXPECT_EQ("f(x,\n" 13789 " _T(\"aaaaaaaaaaaa\")\n" 13790 " _T(\"aaa\"),\n" 13791 " z);", 13792 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 13793 13794 // FIXME: Handle embedded spaces in one iteration. 13795 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 13796 // "_T(\"aaaaaaaaaaaaa\")\n" 13797 // "_T(\"aaaaaaaaaaaaa\")\n" 13798 // "_T(\"a\")", 13799 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13800 // getLLVMStyleWithColumns(20))); 13801 EXPECT_EQ( 13802 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13803 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 13804 EXPECT_EQ("f(\n" 13805 "#if !TEST\n" 13806 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13807 "#endif\n" 13808 ");", 13809 format("f(\n" 13810 "#if !TEST\n" 13811 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13812 "#endif\n" 13813 ");")); 13814 EXPECT_EQ("f(\n" 13815 "\n" 13816 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 13817 format("f(\n" 13818 "\n" 13819 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 13820 // Regression test for accessing tokens past the end of a vector in the 13821 // TokenLexer. 13822 verifyNoCrash(R"(_T( 13823 " 13824 ) 13825 )"); 13826 } 13827 13828 TEST_F(FormatTest, BreaksStringLiteralOperands) { 13829 // In a function call with two operands, the second can be broken with no line 13830 // break before it. 13831 EXPECT_EQ( 13832 "func(a, \"long long \"\n" 13833 " \"long long\");", 13834 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 13835 // In a function call with three operands, the second must be broken with a 13836 // line break before it. 13837 EXPECT_EQ("func(a,\n" 13838 " \"long long long \"\n" 13839 " \"long\",\n" 13840 " c);", 13841 format("func(a, \"long long long long\", c);", 13842 getLLVMStyleWithColumns(24))); 13843 // In a function call with three operands, the third must be broken with a 13844 // line break before it. 13845 EXPECT_EQ("func(a, b,\n" 13846 " \"long long long \"\n" 13847 " \"long\");", 13848 format("func(a, b, \"long long long long\");", 13849 getLLVMStyleWithColumns(24))); 13850 // In a function call with three operands, both the second and the third must 13851 // be broken with a line break before them. 13852 EXPECT_EQ("func(a,\n" 13853 " \"long long long \"\n" 13854 " \"long\",\n" 13855 " \"long long long \"\n" 13856 " \"long\");", 13857 format("func(a, \"long long long long\", \"long long long long\");", 13858 getLLVMStyleWithColumns(24))); 13859 // In a chain of << with two operands, the second can be broken with no line 13860 // break before it. 13861 EXPECT_EQ("a << \"line line \"\n" 13862 " \"line\";", 13863 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 13864 // In a chain of << with three operands, the second can be broken with no line 13865 // break before it. 13866 EXPECT_EQ( 13867 "abcde << \"line \"\n" 13868 " \"line line\"\n" 13869 " << c;", 13870 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 13871 // In a chain of << with three operands, the third must be broken with a line 13872 // break before it. 13873 EXPECT_EQ( 13874 "a << b\n" 13875 " << \"line line \"\n" 13876 " \"line\";", 13877 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 13878 // In a chain of << with three operands, the second can be broken with no line 13879 // break before it and the third must be broken with a line break before it. 13880 EXPECT_EQ("abcd << \"line line \"\n" 13881 " \"line\"\n" 13882 " << \"line line \"\n" 13883 " \"line\";", 13884 format("abcd << \"line line line\" << \"line line line\";", 13885 getLLVMStyleWithColumns(20))); 13886 // In a chain of binary operators with two operands, the second can be broken 13887 // with no line break before it. 13888 EXPECT_EQ( 13889 "abcd + \"line line \"\n" 13890 " \"line line\";", 13891 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 13892 // In a chain of binary operators with three operands, the second must be 13893 // broken with a line break before it. 13894 EXPECT_EQ("abcd +\n" 13895 " \"line line \"\n" 13896 " \"line line\" +\n" 13897 " e;", 13898 format("abcd + \"line line line line\" + e;", 13899 getLLVMStyleWithColumns(20))); 13900 // In a function call with two operands, with AlignAfterOpenBracket enabled, 13901 // the first must be broken with a line break before it. 13902 FormatStyle Style = getLLVMStyleWithColumns(25); 13903 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13904 EXPECT_EQ("someFunction(\n" 13905 " \"long long long \"\n" 13906 " \"long\",\n" 13907 " a);", 13908 format("someFunction(\"long long long long\", a);", Style)); 13909 } 13910 13911 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 13912 EXPECT_EQ( 13913 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13916 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 13919 } 13920 13921 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 13922 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 13923 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 13924 EXPECT_EQ("fffffffffff(g(R\"x(\n" 13925 "multiline raw string literal xxxxxxxxxxxxxx\n" 13926 ")x\",\n" 13927 " a),\n" 13928 " b);", 13929 format("fffffffffff(g(R\"x(\n" 13930 "multiline raw string literal xxxxxxxxxxxxxx\n" 13931 ")x\", a), b);", 13932 getGoogleStyleWithColumns(20))); 13933 EXPECT_EQ("fffffffffff(\n" 13934 " g(R\"x(qqq\n" 13935 "multiline raw string literal xxxxxxxxxxxxxx\n" 13936 ")x\",\n" 13937 " a),\n" 13938 " b);", 13939 format("fffffffffff(g(R\"x(qqq\n" 13940 "multiline raw string literal xxxxxxxxxxxxxx\n" 13941 ")x\", a), b);", 13942 getGoogleStyleWithColumns(20))); 13943 13944 EXPECT_EQ("fffffffffff(R\"x(\n" 13945 "multiline raw string literal xxxxxxxxxxxxxx\n" 13946 ")x\");", 13947 format("fffffffffff(R\"x(\n" 13948 "multiline raw string literal xxxxxxxxxxxxxx\n" 13949 ")x\");", 13950 getGoogleStyleWithColumns(20))); 13951 EXPECT_EQ("fffffffffff(R\"x(\n" 13952 "multiline raw string literal xxxxxxxxxxxxxx\n" 13953 ")x\" + bbbbbb);", 13954 format("fffffffffff(R\"x(\n" 13955 "multiline raw string literal xxxxxxxxxxxxxx\n" 13956 ")x\" + bbbbbb);", 13957 getGoogleStyleWithColumns(20))); 13958 EXPECT_EQ("fffffffffff(\n" 13959 " R\"x(\n" 13960 "multiline raw string literal xxxxxxxxxxxxxx\n" 13961 ")x\" +\n" 13962 " bbbbbb);", 13963 format("fffffffffff(\n" 13964 " R\"x(\n" 13965 "multiline raw string literal xxxxxxxxxxxxxx\n" 13966 ")x\" + bbbbbb);", 13967 getGoogleStyleWithColumns(20))); 13968 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 13969 format("fffffffffff(\n" 13970 " R\"(single line raw string)\" + bbbbbb);")); 13971 } 13972 13973 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13974 verifyFormat("string a = \"unterminated;"); 13975 EXPECT_EQ("function(\"unterminated,\n" 13976 " OtherParameter);", 13977 format("function( \"unterminated,\n" 13978 " OtherParameter);")); 13979 } 13980 13981 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13982 FormatStyle Style = getLLVMStyle(); 13983 Style.Standard = FormatStyle::LS_Cpp03; 13984 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13985 format("#define x(_a) printf(\"foo\"_a);", Style)); 13986 } 13987 13988 TEST_F(FormatTest, CppLexVersion) { 13989 FormatStyle Style = getLLVMStyle(); 13990 // Formatting of x * y differs if x is a type. 13991 verifyFormat("void foo() { MACRO(a * b); }", Style); 13992 verifyFormat("void foo() { MACRO(int *b); }", Style); 13993 13994 // LLVM style uses latest lexer. 13995 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13996 Style.Standard = FormatStyle::LS_Cpp17; 13997 // But in c++17, char8_t isn't a keyword. 13998 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13999 } 14000 14001 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 14002 14003 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 14004 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 14005 " \"ddeeefff\");", 14006 format("someFunction(\"aaabbbcccdddeeefff\");", 14007 getLLVMStyleWithColumns(25))); 14008 EXPECT_EQ("someFunction1234567890(\n" 14009 " \"aaabbbcccdddeeefff\");", 14010 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 14011 getLLVMStyleWithColumns(26))); 14012 EXPECT_EQ("someFunction1234567890(\n" 14013 " \"aaabbbcccdddeeeff\"\n" 14014 " \"f\");", 14015 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 14016 getLLVMStyleWithColumns(25))); 14017 EXPECT_EQ("someFunction1234567890(\n" 14018 " \"aaabbbcccdddeeeff\"\n" 14019 " \"f\");", 14020 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 14021 getLLVMStyleWithColumns(24))); 14022 EXPECT_EQ("someFunction(\n" 14023 " \"aaabbbcc ddde \"\n" 14024 " \"efff\");", 14025 format("someFunction(\"aaabbbcc ddde efff\");", 14026 getLLVMStyleWithColumns(25))); 14027 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 14028 " \"ddeeefff\");", 14029 format("someFunction(\"aaabbbccc ddeeefff\");", 14030 getLLVMStyleWithColumns(25))); 14031 EXPECT_EQ("someFunction1234567890(\n" 14032 " \"aaabb \"\n" 14033 " \"cccdddeeefff\");", 14034 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 14035 getLLVMStyleWithColumns(25))); 14036 EXPECT_EQ("#define A \\\n" 14037 " string s = \\\n" 14038 " \"123456789\" \\\n" 14039 " \"0\"; \\\n" 14040 " int i;", 14041 format("#define A string s = \"1234567890\"; int i;", 14042 getLLVMStyleWithColumns(20))); 14043 EXPECT_EQ("someFunction(\n" 14044 " \"aaabbbcc \"\n" 14045 " \"dddeeefff\");", 14046 format("someFunction(\"aaabbbcc dddeeefff\");", 14047 getLLVMStyleWithColumns(25))); 14048 } 14049 14050 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 14051 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 14052 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 14053 EXPECT_EQ("\"test\"\n" 14054 "\"\\n\"", 14055 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 14056 EXPECT_EQ("\"tes\\\\\"\n" 14057 "\"n\"", 14058 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 14059 EXPECT_EQ("\"\\\\\\\\\"\n" 14060 "\"\\n\"", 14061 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 14062 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 14063 EXPECT_EQ("\"\\uff01\"\n" 14064 "\"test\"", 14065 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 14066 EXPECT_EQ("\"\\Uff01ff02\"", 14067 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 14068 EXPECT_EQ("\"\\x000000000001\"\n" 14069 "\"next\"", 14070 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 14071 EXPECT_EQ("\"\\x000000000001next\"", 14072 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 14073 EXPECT_EQ("\"\\x000000000001\"", 14074 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 14075 EXPECT_EQ("\"test\"\n" 14076 "\"\\000000\"\n" 14077 "\"000001\"", 14078 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 14079 EXPECT_EQ("\"test\\000\"\n" 14080 "\"00000000\"\n" 14081 "\"1\"", 14082 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 14083 } 14084 14085 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 14086 verifyFormat("void f() {\n" 14087 " return g() {}\n" 14088 " void h() {}"); 14089 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 14090 "g();\n" 14091 "}"); 14092 } 14093 14094 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 14095 verifyFormat( 14096 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 14097 } 14098 14099 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 14100 verifyFormat("class X {\n" 14101 " void f() {\n" 14102 " }\n" 14103 "};", 14104 getLLVMStyleWithColumns(12)); 14105 } 14106 14107 TEST_F(FormatTest, ConfigurableIndentWidth) { 14108 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 14109 EightIndent.IndentWidth = 8; 14110 EightIndent.ContinuationIndentWidth = 8; 14111 verifyFormat("void f() {\n" 14112 " someFunction();\n" 14113 " if (true) {\n" 14114 " f();\n" 14115 " }\n" 14116 "}", 14117 EightIndent); 14118 verifyFormat("class X {\n" 14119 " void f() {\n" 14120 " }\n" 14121 "};", 14122 EightIndent); 14123 verifyFormat("int x[] = {\n" 14124 " call(),\n" 14125 " call()};", 14126 EightIndent); 14127 } 14128 14129 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 14130 verifyFormat("double\n" 14131 "f();", 14132 getLLVMStyleWithColumns(8)); 14133 } 14134 14135 TEST_F(FormatTest, ConfigurableUseOfTab) { 14136 FormatStyle Tab = getLLVMStyleWithColumns(42); 14137 Tab.IndentWidth = 8; 14138 Tab.UseTab = FormatStyle::UT_Always; 14139 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 14140 14141 EXPECT_EQ("if (aaaaaaaa && // q\n" 14142 " bb)\t\t// w\n" 14143 "\t;", 14144 format("if (aaaaaaaa &&// q\n" 14145 "bb)// w\n" 14146 ";", 14147 Tab)); 14148 EXPECT_EQ("if (aaa && bbb) // w\n" 14149 "\t;", 14150 format("if(aaa&&bbb)// w\n" 14151 ";", 14152 Tab)); 14153 14154 verifyFormat("class X {\n" 14155 "\tvoid f() {\n" 14156 "\t\tsomeFunction(parameter1,\n" 14157 "\t\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 verifyFormat("int a;\t // x\n" 14169 "int bbbbbbbb; // x\n", 14170 Tab); 14171 14172 FormatStyle TabAlignment = Tab; 14173 TabAlignment.AlignConsecutiveDeclarations.Enabled = true; 14174 TabAlignment.PointerAlignment = FormatStyle::PAS_Left; 14175 verifyFormat("unsigned long long big;\n" 14176 "char*\t\t ptr;", 14177 TabAlignment); 14178 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle; 14179 verifyFormat("unsigned long long big;\n" 14180 "char *\t\t ptr;", 14181 TabAlignment); 14182 TabAlignment.PointerAlignment = FormatStyle::PAS_Right; 14183 verifyFormat("unsigned long long big;\n" 14184 "char\t\t *ptr;", 14185 TabAlignment); 14186 14187 Tab.TabWidth = 4; 14188 Tab.IndentWidth = 8; 14189 verifyFormat("class TabWidth4Indent8 {\n" 14190 "\t\tvoid f() {\n" 14191 "\t\t\t\tsomeFunction(parameter1,\n" 14192 "\t\t\t\t\t\t\t parameter2);\n" 14193 "\t\t}\n" 14194 "};", 14195 Tab); 14196 14197 Tab.TabWidth = 4; 14198 Tab.IndentWidth = 4; 14199 verifyFormat("class TabWidth4Indent4 {\n" 14200 "\tvoid f() {\n" 14201 "\t\tsomeFunction(parameter1,\n" 14202 "\t\t\t\t\t parameter2);\n" 14203 "\t}\n" 14204 "};", 14205 Tab); 14206 14207 Tab.TabWidth = 8; 14208 Tab.IndentWidth = 4; 14209 verifyFormat("class TabWidth8Indent4 {\n" 14210 " void f() {\n" 14211 "\tsomeFunction(parameter1,\n" 14212 "\t\t parameter2);\n" 14213 " }\n" 14214 "};", 14215 Tab); 14216 14217 Tab.TabWidth = 8; 14218 Tab.IndentWidth = 8; 14219 EXPECT_EQ("/*\n" 14220 "\t a\t\tcomment\n" 14221 "\t in multiple lines\n" 14222 " */", 14223 format(" /*\t \t \n" 14224 " \t \t a\t\tcomment\t \t\n" 14225 " \t \t in multiple lines\t\n" 14226 " \t */", 14227 Tab)); 14228 14229 TabAlignment.UseTab = FormatStyle::UT_ForIndentation; 14230 TabAlignment.PointerAlignment = FormatStyle::PAS_Left; 14231 verifyFormat("void f() {\n" 14232 "\tunsigned long long big;\n" 14233 "\tchar* ptr;\n" 14234 "}", 14235 TabAlignment); 14236 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle; 14237 verifyFormat("void f() {\n" 14238 "\tunsigned long long big;\n" 14239 "\tchar * ptr;\n" 14240 "}", 14241 TabAlignment); 14242 TabAlignment.PointerAlignment = FormatStyle::PAS_Right; 14243 verifyFormat("void f() {\n" 14244 "\tunsigned long long big;\n" 14245 "\tchar *ptr;\n" 14246 "}", 14247 TabAlignment); 14248 14249 Tab.UseTab = FormatStyle::UT_ForIndentation; 14250 verifyFormat("{\n" 14251 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14252 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14253 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14254 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14255 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14256 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14257 "};", 14258 Tab); 14259 verifyFormat("enum AA {\n" 14260 "\ta1, // Force multiple lines\n" 14261 "\ta2,\n" 14262 "\ta3\n" 14263 "};", 14264 Tab); 14265 EXPECT_EQ("if (aaaaaaaa && // q\n" 14266 " bb) // w\n" 14267 "\t;", 14268 format("if (aaaaaaaa &&// q\n" 14269 "bb)// w\n" 14270 ";", 14271 Tab)); 14272 verifyFormat("class X {\n" 14273 "\tvoid f() {\n" 14274 "\t\tsomeFunction(parameter1,\n" 14275 "\t\t parameter2);\n" 14276 "\t}\n" 14277 "};", 14278 Tab); 14279 verifyFormat("{\n" 14280 "\tQ(\n" 14281 "\t {\n" 14282 "\t\t int a;\n" 14283 "\t\t someFunction(aaaaaaaa,\n" 14284 "\t\t bbbbbbb);\n" 14285 "\t },\n" 14286 "\t p);\n" 14287 "}", 14288 Tab); 14289 EXPECT_EQ("{\n" 14290 "\t/* aaaa\n" 14291 "\t bbbb */\n" 14292 "}", 14293 format("{\n" 14294 "/* aaaa\n" 14295 " bbbb */\n" 14296 "}", 14297 Tab)); 14298 EXPECT_EQ("{\n" 14299 "\t/*\n" 14300 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14301 "\t bbbbbbbbbbbbb\n" 14302 "\t*/\n" 14303 "}", 14304 format("{\n" 14305 "/*\n" 14306 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14307 "*/\n" 14308 "}", 14309 Tab)); 14310 EXPECT_EQ("{\n" 14311 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14312 "\t// bbbbbbbbbbbbb\n" 14313 "}", 14314 format("{\n" 14315 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14316 "}", 14317 Tab)); 14318 EXPECT_EQ("{\n" 14319 "\t/*\n" 14320 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14321 "\t bbbbbbbbbbbbb\n" 14322 "\t*/\n" 14323 "}", 14324 format("{\n" 14325 "\t/*\n" 14326 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14327 "\t*/\n" 14328 "}", 14329 Tab)); 14330 EXPECT_EQ("{\n" 14331 "\t/*\n" 14332 "\n" 14333 "\t*/\n" 14334 "}", 14335 format("{\n" 14336 "\t/*\n" 14337 "\n" 14338 "\t*/\n" 14339 "}", 14340 Tab)); 14341 EXPECT_EQ("{\n" 14342 "\t/*\n" 14343 " asdf\n" 14344 "\t*/\n" 14345 "}", 14346 format("{\n" 14347 "\t/*\n" 14348 " asdf\n" 14349 "\t*/\n" 14350 "}", 14351 Tab)); 14352 14353 verifyFormat("void f() {\n" 14354 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 14355 "\t : bbbbbbbbbbbbbbbbbb\n" 14356 "}", 14357 Tab); 14358 FormatStyle TabNoBreak = Tab; 14359 TabNoBreak.BreakBeforeTernaryOperators = false; 14360 verifyFormat("void f() {\n" 14361 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 14362 "\t bbbbbbbbbbbbbbbbbb\n" 14363 "}", 14364 TabNoBreak); 14365 verifyFormat("void f() {\n" 14366 "\treturn true ?\n" 14367 "\t aaaaaaaaaaaaaaaaaaaa :\n" 14368 "\t bbbbbbbbbbbbbbbbbbbb\n" 14369 "}", 14370 TabNoBreak); 14371 14372 Tab.UseTab = FormatStyle::UT_Never; 14373 EXPECT_EQ("/*\n" 14374 " a\t\tcomment\n" 14375 " in multiple lines\n" 14376 " */", 14377 format(" /*\t \t \n" 14378 " \t \t a\t\tcomment\t \t\n" 14379 " \t \t in multiple lines\t\n" 14380 " \t */", 14381 Tab)); 14382 EXPECT_EQ("/* some\n" 14383 " comment */", 14384 format(" \t \t /* some\n" 14385 " \t \t comment */", 14386 Tab)); 14387 EXPECT_EQ("int a; /* some\n" 14388 " comment */", 14389 format(" \t \t int a; /* some\n" 14390 " \t \t comment */", 14391 Tab)); 14392 14393 EXPECT_EQ("int a; /* some\n" 14394 "comment */", 14395 format(" \t \t int\ta; /* some\n" 14396 " \t \t comment */", 14397 Tab)); 14398 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14399 " comment */", 14400 format(" \t \t f(\"\t\t\"); /* some\n" 14401 " \t \t comment */", 14402 Tab)); 14403 EXPECT_EQ("{\n" 14404 " /*\n" 14405 " * Comment\n" 14406 " */\n" 14407 " int i;\n" 14408 "}", 14409 format("{\n" 14410 "\t/*\n" 14411 "\t * Comment\n" 14412 "\t */\n" 14413 "\t int i;\n" 14414 "}", 14415 Tab)); 14416 14417 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14418 Tab.TabWidth = 8; 14419 Tab.IndentWidth = 8; 14420 EXPECT_EQ("if (aaaaaaaa && // q\n" 14421 " bb) // w\n" 14422 "\t;", 14423 format("if (aaaaaaaa &&// q\n" 14424 "bb)// w\n" 14425 ";", 14426 Tab)); 14427 EXPECT_EQ("if (aaa && bbb) // w\n" 14428 "\t;", 14429 format("if(aaa&&bbb)// w\n" 14430 ";", 14431 Tab)); 14432 verifyFormat("class X {\n" 14433 "\tvoid f() {\n" 14434 "\t\tsomeFunction(parameter1,\n" 14435 "\t\t\t parameter2);\n" 14436 "\t}\n" 14437 "};", 14438 Tab); 14439 verifyFormat("#define A \\\n" 14440 "\tvoid f() { \\\n" 14441 "\t\tsomeFunction( \\\n" 14442 "\t\t parameter1, \\\n" 14443 "\t\t parameter2); \\\n" 14444 "\t}", 14445 Tab); 14446 Tab.TabWidth = 4; 14447 Tab.IndentWidth = 8; 14448 verifyFormat("class TabWidth4Indent8 {\n" 14449 "\t\tvoid f() {\n" 14450 "\t\t\t\tsomeFunction(parameter1,\n" 14451 "\t\t\t\t\t\t\t parameter2);\n" 14452 "\t\t}\n" 14453 "};", 14454 Tab); 14455 Tab.TabWidth = 4; 14456 Tab.IndentWidth = 4; 14457 verifyFormat("class TabWidth4Indent4 {\n" 14458 "\tvoid f() {\n" 14459 "\t\tsomeFunction(parameter1,\n" 14460 "\t\t\t\t\t parameter2);\n" 14461 "\t}\n" 14462 "};", 14463 Tab); 14464 Tab.TabWidth = 8; 14465 Tab.IndentWidth = 4; 14466 verifyFormat("class TabWidth8Indent4 {\n" 14467 " void f() {\n" 14468 "\tsomeFunction(parameter1,\n" 14469 "\t\t parameter2);\n" 14470 " }\n" 14471 "};", 14472 Tab); 14473 Tab.TabWidth = 8; 14474 Tab.IndentWidth = 8; 14475 EXPECT_EQ("/*\n" 14476 "\t a\t\tcomment\n" 14477 "\t in multiple lines\n" 14478 " */", 14479 format(" /*\t \t \n" 14480 " \t \t a\t\tcomment\t \t\n" 14481 " \t \t in multiple lines\t\n" 14482 " \t */", 14483 Tab)); 14484 verifyFormat("{\n" 14485 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14486 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14487 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14488 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14489 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14490 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14491 "};", 14492 Tab); 14493 verifyFormat("enum AA {\n" 14494 "\ta1, // Force multiple lines\n" 14495 "\ta2,\n" 14496 "\ta3\n" 14497 "};", 14498 Tab); 14499 EXPECT_EQ("if (aaaaaaaa && // q\n" 14500 " bb) // w\n" 14501 "\t;", 14502 format("if (aaaaaaaa &&// q\n" 14503 "bb)// w\n" 14504 ";", 14505 Tab)); 14506 verifyFormat("class X {\n" 14507 "\tvoid f() {\n" 14508 "\t\tsomeFunction(parameter1,\n" 14509 "\t\t\t parameter2);\n" 14510 "\t}\n" 14511 "};", 14512 Tab); 14513 verifyFormat("{\n" 14514 "\tQ(\n" 14515 "\t {\n" 14516 "\t\t int a;\n" 14517 "\t\t someFunction(aaaaaaaa,\n" 14518 "\t\t\t\t bbbbbbb);\n" 14519 "\t },\n" 14520 "\t p);\n" 14521 "}", 14522 Tab); 14523 EXPECT_EQ("{\n" 14524 "\t/* aaaa\n" 14525 "\t bbbb */\n" 14526 "}", 14527 format("{\n" 14528 "/* aaaa\n" 14529 " bbbb */\n" 14530 "}", 14531 Tab)); 14532 EXPECT_EQ("{\n" 14533 "\t/*\n" 14534 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14535 "\t bbbbbbbbbbbbb\n" 14536 "\t*/\n" 14537 "}", 14538 format("{\n" 14539 "/*\n" 14540 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14541 "*/\n" 14542 "}", 14543 Tab)); 14544 EXPECT_EQ("{\n" 14545 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14546 "\t// bbbbbbbbbbbbb\n" 14547 "}", 14548 format("{\n" 14549 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14550 "}", 14551 Tab)); 14552 EXPECT_EQ("{\n" 14553 "\t/*\n" 14554 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14555 "\t bbbbbbbbbbbbb\n" 14556 "\t*/\n" 14557 "}", 14558 format("{\n" 14559 "\t/*\n" 14560 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14561 "\t*/\n" 14562 "}", 14563 Tab)); 14564 EXPECT_EQ("{\n" 14565 "\t/*\n" 14566 "\n" 14567 "\t*/\n" 14568 "}", 14569 format("{\n" 14570 "\t/*\n" 14571 "\n" 14572 "\t*/\n" 14573 "}", 14574 Tab)); 14575 EXPECT_EQ("{\n" 14576 "\t/*\n" 14577 " asdf\n" 14578 "\t*/\n" 14579 "}", 14580 format("{\n" 14581 "\t/*\n" 14582 " asdf\n" 14583 "\t*/\n" 14584 "}", 14585 Tab)); 14586 EXPECT_EQ("/* some\n" 14587 " comment */", 14588 format(" \t \t /* some\n" 14589 " \t \t comment */", 14590 Tab)); 14591 EXPECT_EQ("int a; /* some\n" 14592 " comment */", 14593 format(" \t \t int a; /* some\n" 14594 " \t \t comment */", 14595 Tab)); 14596 EXPECT_EQ("int a; /* some\n" 14597 "comment */", 14598 format(" \t \t int\ta; /* some\n" 14599 " \t \t comment */", 14600 Tab)); 14601 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14602 " comment */", 14603 format(" \t \t f(\"\t\t\"); /* some\n" 14604 " \t \t comment */", 14605 Tab)); 14606 EXPECT_EQ("{\n" 14607 "\t/*\n" 14608 "\t * Comment\n" 14609 "\t */\n" 14610 "\tint i;\n" 14611 "}", 14612 format("{\n" 14613 "\t/*\n" 14614 "\t * Comment\n" 14615 "\t */\n" 14616 "\t int i;\n" 14617 "}", 14618 Tab)); 14619 Tab.TabWidth = 2; 14620 Tab.IndentWidth = 2; 14621 EXPECT_EQ("{\n" 14622 "\t/* aaaa\n" 14623 "\t\t bbbb */\n" 14624 "}", 14625 format("{\n" 14626 "/* aaaa\n" 14627 "\t bbbb */\n" 14628 "}", 14629 Tab)); 14630 EXPECT_EQ("{\n" 14631 "\t/*\n" 14632 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14633 "\t\tbbbbbbbbbbbbb\n" 14634 "\t*/\n" 14635 "}", 14636 format("{\n" 14637 "/*\n" 14638 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14639 "*/\n" 14640 "}", 14641 Tab)); 14642 Tab.AlignConsecutiveAssignments.Enabled = true; 14643 Tab.AlignConsecutiveDeclarations.Enabled = true; 14644 Tab.TabWidth = 4; 14645 Tab.IndentWidth = 4; 14646 verifyFormat("class Assign {\n" 14647 "\tvoid f() {\n" 14648 "\t\tint x = 123;\n" 14649 "\t\tint random = 4;\n" 14650 "\t\tstd::string alphabet =\n" 14651 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14652 "\t}\n" 14653 "};", 14654 Tab); 14655 14656 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14657 Tab.TabWidth = 8; 14658 Tab.IndentWidth = 8; 14659 EXPECT_EQ("if (aaaaaaaa && // q\n" 14660 " bb) // w\n" 14661 "\t;", 14662 format("if (aaaaaaaa &&// q\n" 14663 "bb)// w\n" 14664 ";", 14665 Tab)); 14666 EXPECT_EQ("if (aaa && bbb) // w\n" 14667 "\t;", 14668 format("if(aaa&&bbb)// w\n" 14669 ";", 14670 Tab)); 14671 verifyFormat("class X {\n" 14672 "\tvoid f() {\n" 14673 "\t\tsomeFunction(parameter1,\n" 14674 "\t\t parameter2);\n" 14675 "\t}\n" 14676 "};", 14677 Tab); 14678 verifyFormat("#define A \\\n" 14679 "\tvoid f() { \\\n" 14680 "\t\tsomeFunction( \\\n" 14681 "\t\t parameter1, \\\n" 14682 "\t\t parameter2); \\\n" 14683 "\t}", 14684 Tab); 14685 Tab.TabWidth = 4; 14686 Tab.IndentWidth = 8; 14687 verifyFormat("class TabWidth4Indent8 {\n" 14688 "\t\tvoid f() {\n" 14689 "\t\t\t\tsomeFunction(parameter1,\n" 14690 "\t\t\t\t parameter2);\n" 14691 "\t\t}\n" 14692 "};", 14693 Tab); 14694 Tab.TabWidth = 4; 14695 Tab.IndentWidth = 4; 14696 verifyFormat("class TabWidth4Indent4 {\n" 14697 "\tvoid f() {\n" 14698 "\t\tsomeFunction(parameter1,\n" 14699 "\t\t parameter2);\n" 14700 "\t}\n" 14701 "};", 14702 Tab); 14703 Tab.TabWidth = 8; 14704 Tab.IndentWidth = 4; 14705 verifyFormat("class TabWidth8Indent4 {\n" 14706 " void f() {\n" 14707 "\tsomeFunction(parameter1,\n" 14708 "\t parameter2);\n" 14709 " }\n" 14710 "};", 14711 Tab); 14712 Tab.TabWidth = 8; 14713 Tab.IndentWidth = 8; 14714 EXPECT_EQ("/*\n" 14715 " a\t\tcomment\n" 14716 " in multiple lines\n" 14717 " */", 14718 format(" /*\t \t \n" 14719 " \t \t a\t\tcomment\t \t\n" 14720 " \t \t in multiple lines\t\n" 14721 " \t */", 14722 Tab)); 14723 verifyFormat("{\n" 14724 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14725 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14726 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14727 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14728 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14729 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14730 "};", 14731 Tab); 14732 verifyFormat("enum AA {\n" 14733 "\ta1, // Force multiple lines\n" 14734 "\ta2,\n" 14735 "\ta3\n" 14736 "};", 14737 Tab); 14738 EXPECT_EQ("if (aaaaaaaa && // q\n" 14739 " bb) // w\n" 14740 "\t;", 14741 format("if (aaaaaaaa &&// q\n" 14742 "bb)// w\n" 14743 ";", 14744 Tab)); 14745 verifyFormat("class X {\n" 14746 "\tvoid f() {\n" 14747 "\t\tsomeFunction(parameter1,\n" 14748 "\t\t parameter2);\n" 14749 "\t}\n" 14750 "};", 14751 Tab); 14752 verifyFormat("{\n" 14753 "\tQ(\n" 14754 "\t {\n" 14755 "\t\t int a;\n" 14756 "\t\t someFunction(aaaaaaaa,\n" 14757 "\t\t bbbbbbb);\n" 14758 "\t },\n" 14759 "\t p);\n" 14760 "}", 14761 Tab); 14762 EXPECT_EQ("{\n" 14763 "\t/* aaaa\n" 14764 "\t bbbb */\n" 14765 "}", 14766 format("{\n" 14767 "/* aaaa\n" 14768 " bbbb */\n" 14769 "}", 14770 Tab)); 14771 EXPECT_EQ("{\n" 14772 "\t/*\n" 14773 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14774 "\t bbbbbbbbbbbbb\n" 14775 "\t*/\n" 14776 "}", 14777 format("{\n" 14778 "/*\n" 14779 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14780 "*/\n" 14781 "}", 14782 Tab)); 14783 EXPECT_EQ("{\n" 14784 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14785 "\t// bbbbbbbbbbbbb\n" 14786 "}", 14787 format("{\n" 14788 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14789 "}", 14790 Tab)); 14791 EXPECT_EQ("{\n" 14792 "\t/*\n" 14793 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14794 "\t bbbbbbbbbbbbb\n" 14795 "\t*/\n" 14796 "}", 14797 format("{\n" 14798 "\t/*\n" 14799 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14800 "\t*/\n" 14801 "}", 14802 Tab)); 14803 EXPECT_EQ("{\n" 14804 "\t/*\n" 14805 "\n" 14806 "\t*/\n" 14807 "}", 14808 format("{\n" 14809 "\t/*\n" 14810 "\n" 14811 "\t*/\n" 14812 "}", 14813 Tab)); 14814 EXPECT_EQ("{\n" 14815 "\t/*\n" 14816 " asdf\n" 14817 "\t*/\n" 14818 "}", 14819 format("{\n" 14820 "\t/*\n" 14821 " asdf\n" 14822 "\t*/\n" 14823 "}", 14824 Tab)); 14825 EXPECT_EQ("/* some\n" 14826 " comment */", 14827 format(" \t \t /* some\n" 14828 " \t \t comment */", 14829 Tab)); 14830 EXPECT_EQ("int a; /* some\n" 14831 " comment */", 14832 format(" \t \t int a; /* some\n" 14833 " \t \t comment */", 14834 Tab)); 14835 EXPECT_EQ("int a; /* some\n" 14836 "comment */", 14837 format(" \t \t int\ta; /* some\n" 14838 " \t \t comment */", 14839 Tab)); 14840 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14841 " comment */", 14842 format(" \t \t f(\"\t\t\"); /* some\n" 14843 " \t \t comment */", 14844 Tab)); 14845 EXPECT_EQ("{\n" 14846 "\t/*\n" 14847 "\t * Comment\n" 14848 "\t */\n" 14849 "\tint i;\n" 14850 "}", 14851 format("{\n" 14852 "\t/*\n" 14853 "\t * Comment\n" 14854 "\t */\n" 14855 "\t int i;\n" 14856 "}", 14857 Tab)); 14858 Tab.TabWidth = 2; 14859 Tab.IndentWidth = 2; 14860 EXPECT_EQ("{\n" 14861 "\t/* aaaa\n" 14862 "\t bbbb */\n" 14863 "}", 14864 format("{\n" 14865 "/* aaaa\n" 14866 " bbbb */\n" 14867 "}", 14868 Tab)); 14869 EXPECT_EQ("{\n" 14870 "\t/*\n" 14871 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14872 "\t bbbbbbbbbbbbb\n" 14873 "\t*/\n" 14874 "}", 14875 format("{\n" 14876 "/*\n" 14877 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14878 "*/\n" 14879 "}", 14880 Tab)); 14881 Tab.AlignConsecutiveAssignments.Enabled = true; 14882 Tab.AlignConsecutiveDeclarations.Enabled = true; 14883 Tab.TabWidth = 4; 14884 Tab.IndentWidth = 4; 14885 verifyFormat("class Assign {\n" 14886 "\tvoid f() {\n" 14887 "\t\tint x = 123;\n" 14888 "\t\tint random = 4;\n" 14889 "\t\tstd::string alphabet =\n" 14890 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14891 "\t}\n" 14892 "};", 14893 Tab); 14894 Tab.AlignOperands = FormatStyle::OAS_Align; 14895 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 14896 " cccccccccccccccccccc;", 14897 Tab); 14898 // no alignment 14899 verifyFormat("int aaaaaaaaaa =\n" 14900 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 14901 Tab); 14902 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 14903 " : bbbbbbbbbbbbbb ? 222222222222222\n" 14904 " : 333333333333333;", 14905 Tab); 14906 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14907 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 14908 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 14909 " + cccccccccccccccccccc;", 14910 Tab); 14911 } 14912 14913 TEST_F(FormatTest, ZeroTabWidth) { 14914 FormatStyle Tab = getLLVMStyleWithColumns(42); 14915 Tab.IndentWidth = 8; 14916 Tab.UseTab = FormatStyle::UT_Never; 14917 Tab.TabWidth = 0; 14918 EXPECT_EQ("void a(){\n" 14919 " // line starts with '\t'\n" 14920 "};", 14921 format("void a(){\n" 14922 "\t// line starts with '\t'\n" 14923 "};", 14924 Tab)); 14925 14926 EXPECT_EQ("void a(){\n" 14927 " // line starts with '\t'\n" 14928 "};", 14929 format("void a(){\n" 14930 "\t\t// line starts with '\t'\n" 14931 "};", 14932 Tab)); 14933 14934 Tab.UseTab = FormatStyle::UT_ForIndentation; 14935 EXPECT_EQ("void a(){\n" 14936 " // line starts with '\t'\n" 14937 "};", 14938 format("void a(){\n" 14939 "\t// line starts with '\t'\n" 14940 "};", 14941 Tab)); 14942 14943 EXPECT_EQ("void a(){\n" 14944 " // line starts with '\t'\n" 14945 "};", 14946 format("void a(){\n" 14947 "\t\t// line starts with '\t'\n" 14948 "};", 14949 Tab)); 14950 14951 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14952 EXPECT_EQ("void a(){\n" 14953 " // line starts with '\t'\n" 14954 "};", 14955 format("void a(){\n" 14956 "\t// line starts with '\t'\n" 14957 "};", 14958 Tab)); 14959 14960 EXPECT_EQ("void a(){\n" 14961 " // line starts with '\t'\n" 14962 "};", 14963 format("void a(){\n" 14964 "\t\t// line starts with '\t'\n" 14965 "};", 14966 Tab)); 14967 14968 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14969 EXPECT_EQ("void a(){\n" 14970 " // line starts with '\t'\n" 14971 "};", 14972 format("void a(){\n" 14973 "\t// line starts with '\t'\n" 14974 "};", 14975 Tab)); 14976 14977 EXPECT_EQ("void a(){\n" 14978 " // line starts with '\t'\n" 14979 "};", 14980 format("void a(){\n" 14981 "\t\t// line starts with '\t'\n" 14982 "};", 14983 Tab)); 14984 14985 Tab.UseTab = FormatStyle::UT_Always; 14986 EXPECT_EQ("void a(){\n" 14987 "// line starts with '\t'\n" 14988 "};", 14989 format("void a(){\n" 14990 "\t// line starts with '\t'\n" 14991 "};", 14992 Tab)); 14993 14994 EXPECT_EQ("void a(){\n" 14995 "// line starts with '\t'\n" 14996 "};", 14997 format("void a(){\n" 14998 "\t\t// line starts with '\t'\n" 14999 "};", 15000 Tab)); 15001 } 15002 15003 TEST_F(FormatTest, CalculatesOriginalColumn) { 15004 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 15005 "q\"; /* some\n" 15006 " comment */", 15007 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 15008 "q\"; /* some\n" 15009 " comment */", 15010 getLLVMStyle())); 15011 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 15012 "/* some\n" 15013 " comment */", 15014 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 15015 " /* some\n" 15016 " comment */", 15017 getLLVMStyle())); 15018 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 15019 "qqq\n" 15020 "/* some\n" 15021 " comment */", 15022 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 15023 "qqq\n" 15024 " /* some\n" 15025 " comment */", 15026 getLLVMStyle())); 15027 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 15028 "wwww; /* some\n" 15029 " comment */", 15030 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 15031 "wwww; /* some\n" 15032 " comment */", 15033 getLLVMStyle())); 15034 } 15035 15036 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 15037 FormatStyle NoSpace = getLLVMStyle(); 15038 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 15039 15040 verifyFormat("while(true)\n" 15041 " continue;", 15042 NoSpace); 15043 verifyFormat("for(;;)\n" 15044 " continue;", 15045 NoSpace); 15046 verifyFormat("if(true)\n" 15047 " f();\n" 15048 "else if(true)\n" 15049 " f();", 15050 NoSpace); 15051 verifyFormat("do {\n" 15052 " do_something();\n" 15053 "} while(something());", 15054 NoSpace); 15055 verifyFormat("switch(x) {\n" 15056 "default:\n" 15057 " break;\n" 15058 "}", 15059 NoSpace); 15060 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 15061 verifyFormat("size_t x = sizeof(x);", NoSpace); 15062 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 15063 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 15064 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 15065 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 15066 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 15067 verifyFormat("alignas(128) char a[128];", NoSpace); 15068 verifyFormat("size_t x = alignof(MyType);", NoSpace); 15069 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 15070 verifyFormat("int f() throw(Deprecated);", NoSpace); 15071 verifyFormat("typedef void (*cb)(int);", NoSpace); 15072 verifyFormat("T A::operator()();", NoSpace); 15073 verifyFormat("X A::operator++(T);", NoSpace); 15074 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 15075 15076 FormatStyle Space = getLLVMStyle(); 15077 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 15078 15079 verifyFormat("int f ();", Space); 15080 verifyFormat("void f (int a, T b) {\n" 15081 " while (true)\n" 15082 " continue;\n" 15083 "}", 15084 Space); 15085 verifyFormat("if (true)\n" 15086 " f ();\n" 15087 "else if (true)\n" 15088 " f ();", 15089 Space); 15090 verifyFormat("do {\n" 15091 " do_something ();\n" 15092 "} while (something ());", 15093 Space); 15094 verifyFormat("switch (x) {\n" 15095 "default:\n" 15096 " break;\n" 15097 "}", 15098 Space); 15099 verifyFormat("A::A () : a (1) {}", Space); 15100 verifyFormat("void f () __attribute__ ((asdf));", Space); 15101 verifyFormat("*(&a + 1);\n" 15102 "&((&a)[1]);\n" 15103 "a[(b + c) * d];\n" 15104 "(((a + 1) * 2) + 3) * 4;", 15105 Space); 15106 verifyFormat("#define A(x) x", Space); 15107 verifyFormat("#define A (x) x", Space); 15108 verifyFormat("#if defined(x)\n" 15109 "#endif", 15110 Space); 15111 verifyFormat("auto i = std::make_unique<int> (5);", Space); 15112 verifyFormat("size_t x = sizeof (x);", Space); 15113 verifyFormat("auto f (int x) -> decltype (x);", Space); 15114 verifyFormat("auto f (int x) -> typeof (x);", Space); 15115 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 15116 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 15117 verifyFormat("int f (T x) noexcept (x.create ());", Space); 15118 verifyFormat("alignas (128) char a[128];", Space); 15119 verifyFormat("size_t x = alignof (MyType);", Space); 15120 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 15121 verifyFormat("int f () throw (Deprecated);", Space); 15122 verifyFormat("typedef void (*cb) (int);", Space); 15123 // FIXME these tests regressed behaviour. 15124 // verifyFormat("T A::operator() ();", Space); 15125 // verifyFormat("X A::operator++ (T);", Space); 15126 verifyFormat("auto lambda = [] () { return 0; };", Space); 15127 verifyFormat("int x = int (y);", Space); 15128 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space); 15129 verifyFormat("__builtin_LINE ()", Space); 15130 verifyFormat("__builtin_UNKNOWN ()", Space); 15131 15132 FormatStyle SomeSpace = getLLVMStyle(); 15133 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 15134 15135 verifyFormat("[]() -> float {}", SomeSpace); 15136 verifyFormat("[] (auto foo) {}", SomeSpace); 15137 verifyFormat("[foo]() -> int {}", SomeSpace); 15138 verifyFormat("int f();", SomeSpace); 15139 verifyFormat("void f (int a, T b) {\n" 15140 " while (true)\n" 15141 " continue;\n" 15142 "}", 15143 SomeSpace); 15144 verifyFormat("if (true)\n" 15145 " f();\n" 15146 "else if (true)\n" 15147 " f();", 15148 SomeSpace); 15149 verifyFormat("do {\n" 15150 " do_something();\n" 15151 "} while (something());", 15152 SomeSpace); 15153 verifyFormat("switch (x) {\n" 15154 "default:\n" 15155 " break;\n" 15156 "}", 15157 SomeSpace); 15158 verifyFormat("A::A() : a (1) {}", SomeSpace); 15159 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 15160 verifyFormat("*(&a + 1);\n" 15161 "&((&a)[1]);\n" 15162 "a[(b + c) * d];\n" 15163 "(((a + 1) * 2) + 3) * 4;", 15164 SomeSpace); 15165 verifyFormat("#define A(x) x", SomeSpace); 15166 verifyFormat("#define A (x) x", SomeSpace); 15167 verifyFormat("#if defined(x)\n" 15168 "#endif", 15169 SomeSpace); 15170 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 15171 verifyFormat("size_t x = sizeof (x);", SomeSpace); 15172 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 15173 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 15174 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 15175 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 15176 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 15177 verifyFormat("alignas (128) char a[128];", SomeSpace); 15178 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 15179 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 15180 SomeSpace); 15181 verifyFormat("int f() throw (Deprecated);", SomeSpace); 15182 verifyFormat("typedef void (*cb) (int);", SomeSpace); 15183 verifyFormat("T A::operator()();", SomeSpace); 15184 // FIXME these tests regressed behaviour. 15185 // verifyFormat("X A::operator++ (T);", SomeSpace); 15186 verifyFormat("int x = int (y);", SomeSpace); 15187 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 15188 15189 FormatStyle SpaceControlStatements = getLLVMStyle(); 15190 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15191 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 15192 15193 verifyFormat("while (true)\n" 15194 " continue;", 15195 SpaceControlStatements); 15196 verifyFormat("if (true)\n" 15197 " f();\n" 15198 "else if (true)\n" 15199 " f();", 15200 SpaceControlStatements); 15201 verifyFormat("for (;;) {\n" 15202 " do_something();\n" 15203 "}", 15204 SpaceControlStatements); 15205 verifyFormat("do {\n" 15206 " do_something();\n" 15207 "} while (something());", 15208 SpaceControlStatements); 15209 verifyFormat("switch (x) {\n" 15210 "default:\n" 15211 " break;\n" 15212 "}", 15213 SpaceControlStatements); 15214 15215 FormatStyle SpaceFuncDecl = getLLVMStyle(); 15216 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15217 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 15218 15219 verifyFormat("int f ();", SpaceFuncDecl); 15220 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 15221 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 15222 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 15223 verifyFormat("#define A(x) x", SpaceFuncDecl); 15224 verifyFormat("#define A (x) x", SpaceFuncDecl); 15225 verifyFormat("#if defined(x)\n" 15226 "#endif", 15227 SpaceFuncDecl); 15228 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 15229 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 15230 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 15231 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 15232 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 15233 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 15234 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 15235 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 15236 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 15237 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 15238 SpaceFuncDecl); 15239 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 15240 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 15241 // FIXME these tests regressed behaviour. 15242 // verifyFormat("T A::operator() ();", SpaceFuncDecl); 15243 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 15244 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 15245 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 15246 verifyFormat("int x = int(y);", SpaceFuncDecl); 15247 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 15248 SpaceFuncDecl); 15249 15250 FormatStyle SpaceFuncDef = getLLVMStyle(); 15251 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15252 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 15253 15254 verifyFormat("int f();", SpaceFuncDef); 15255 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 15256 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 15257 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 15258 verifyFormat("#define A(x) x", SpaceFuncDef); 15259 verifyFormat("#define A (x) x", SpaceFuncDef); 15260 verifyFormat("#if defined(x)\n" 15261 "#endif", 15262 SpaceFuncDef); 15263 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 15264 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 15265 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 15266 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 15267 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 15268 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 15269 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 15270 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 15271 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 15272 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 15273 SpaceFuncDef); 15274 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 15275 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 15276 verifyFormat("T A::operator()();", SpaceFuncDef); 15277 verifyFormat("X A::operator++(T);", SpaceFuncDef); 15278 // verifyFormat("T A::operator() () {}", SpaceFuncDef); 15279 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 15280 verifyFormat("int x = int(y);", SpaceFuncDef); 15281 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 15282 SpaceFuncDef); 15283 15284 FormatStyle SpaceIfMacros = getLLVMStyle(); 15285 SpaceIfMacros.IfMacros.clear(); 15286 SpaceIfMacros.IfMacros.push_back("MYIF"); 15287 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15288 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 15289 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 15290 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 15291 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 15292 15293 FormatStyle SpaceForeachMacros = getLLVMStyle(); 15294 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine, 15295 FormatStyle::SBS_Never); 15296 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false); 15297 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15298 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 15299 verifyFormat("for (;;) {\n" 15300 "}", 15301 SpaceForeachMacros); 15302 verifyFormat("foreach (Item *item, itemlist) {\n" 15303 "}", 15304 SpaceForeachMacros); 15305 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n" 15306 "}", 15307 SpaceForeachMacros); 15308 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n" 15309 "}", 15310 SpaceForeachMacros); 15311 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 15312 15313 FormatStyle SomeSpace2 = getLLVMStyle(); 15314 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15315 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 15316 verifyFormat("[]() -> float {}", SomeSpace2); 15317 verifyFormat("[] (auto foo) {}", SomeSpace2); 15318 verifyFormat("[foo]() -> int {}", SomeSpace2); 15319 verifyFormat("int f();", SomeSpace2); 15320 verifyFormat("void f (int a, T b) {\n" 15321 " while (true)\n" 15322 " continue;\n" 15323 "}", 15324 SomeSpace2); 15325 verifyFormat("if (true)\n" 15326 " f();\n" 15327 "else if (true)\n" 15328 " f();", 15329 SomeSpace2); 15330 verifyFormat("do {\n" 15331 " do_something();\n" 15332 "} while (something());", 15333 SomeSpace2); 15334 verifyFormat("switch (x) {\n" 15335 "default:\n" 15336 " break;\n" 15337 "}", 15338 SomeSpace2); 15339 verifyFormat("A::A() : a (1) {}", SomeSpace2); 15340 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 15341 verifyFormat("*(&a + 1);\n" 15342 "&((&a)[1]);\n" 15343 "a[(b + c) * d];\n" 15344 "(((a + 1) * 2) + 3) * 4;", 15345 SomeSpace2); 15346 verifyFormat("#define A(x) x", SomeSpace2); 15347 verifyFormat("#define A (x) x", SomeSpace2); 15348 verifyFormat("#if defined(x)\n" 15349 "#endif", 15350 SomeSpace2); 15351 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 15352 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 15353 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 15354 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 15355 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 15356 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 15357 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 15358 verifyFormat("alignas (128) char a[128];", SomeSpace2); 15359 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 15360 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 15361 SomeSpace2); 15362 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 15363 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 15364 verifyFormat("T A::operator()();", SomeSpace2); 15365 // verifyFormat("X A::operator++ (T);", SomeSpace2); 15366 verifyFormat("int x = int (y);", SomeSpace2); 15367 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 15368 15369 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle(); 15370 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15371 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 15372 .AfterOverloadedOperator = true; 15373 15374 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator); 15375 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator); 15376 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator); 15377 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 15378 15379 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 15380 .AfterOverloadedOperator = false; 15381 15382 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator); 15383 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator); 15384 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator); 15385 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 15386 15387 auto SpaceAfterRequires = getLLVMStyle(); 15388 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15389 EXPECT_FALSE( 15390 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause); 15391 EXPECT_FALSE( 15392 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression); 15393 verifyFormat("void f(auto x)\n" 15394 " requires requires(int i) { x + i; }\n" 15395 "{}", 15396 SpaceAfterRequires); 15397 verifyFormat("void f(auto x)\n" 15398 " requires(requires(int i) { x + i; })\n" 15399 "{}", 15400 SpaceAfterRequires); 15401 verifyFormat("if (requires(int i) { x + i; })\n" 15402 " return;", 15403 SpaceAfterRequires); 15404 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires); 15405 verifyFormat("template <typename T>\n" 15406 " requires(Foo<T>)\n" 15407 "class Bar;", 15408 SpaceAfterRequires); 15409 15410 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true; 15411 verifyFormat("void f(auto x)\n" 15412 " requires requires(int i) { x + i; }\n" 15413 "{}", 15414 SpaceAfterRequires); 15415 verifyFormat("void f(auto x)\n" 15416 " requires (requires(int i) { x + i; })\n" 15417 "{}", 15418 SpaceAfterRequires); 15419 verifyFormat("if (requires(int i) { x + i; })\n" 15420 " return;", 15421 SpaceAfterRequires); 15422 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires); 15423 verifyFormat("template <typename T>\n" 15424 " requires (Foo<T>)\n" 15425 "class Bar;", 15426 SpaceAfterRequires); 15427 15428 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false; 15429 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true; 15430 verifyFormat("void f(auto x)\n" 15431 " requires requires (int i) { x + i; }\n" 15432 "{}", 15433 SpaceAfterRequires); 15434 verifyFormat("void f(auto x)\n" 15435 " requires(requires (int i) { x + i; })\n" 15436 "{}", 15437 SpaceAfterRequires); 15438 verifyFormat("if (requires (int i) { x + i; })\n" 15439 " return;", 15440 SpaceAfterRequires); 15441 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires); 15442 verifyFormat("template <typename T>\n" 15443 " requires(Foo<T>)\n" 15444 "class Bar;", 15445 SpaceAfterRequires); 15446 15447 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true; 15448 verifyFormat("void f(auto x)\n" 15449 " requires requires (int i) { x + i; }\n" 15450 "{}", 15451 SpaceAfterRequires); 15452 verifyFormat("void f(auto x)\n" 15453 " requires (requires (int i) { x + i; })\n" 15454 "{}", 15455 SpaceAfterRequires); 15456 verifyFormat("if (requires (int i) { x + i; })\n" 15457 " return;", 15458 SpaceAfterRequires); 15459 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires); 15460 verifyFormat("template <typename T>\n" 15461 " requires (Foo<T>)\n" 15462 "class Bar;", 15463 SpaceAfterRequires); 15464 } 15465 15466 TEST_F(FormatTest, SpaceAfterLogicalNot) { 15467 FormatStyle Spaces = getLLVMStyle(); 15468 Spaces.SpaceAfterLogicalNot = true; 15469 15470 verifyFormat("bool x = ! y", Spaces); 15471 verifyFormat("if (! isFailure())", Spaces); 15472 verifyFormat("if (! (a && b))", Spaces); 15473 verifyFormat("\"Error!\"", Spaces); 15474 verifyFormat("! ! x", Spaces); 15475 } 15476 15477 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 15478 FormatStyle Spaces = getLLVMStyle(); 15479 15480 Spaces.SpacesInParentheses = true; 15481 verifyFormat("do_something( ::globalVar );", Spaces); 15482 verifyFormat("call( x, y, z );", Spaces); 15483 verifyFormat("call();", Spaces); 15484 verifyFormat("std::function<void( int, int )> callback;", Spaces); 15485 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 15486 Spaces); 15487 verifyFormat("while ( (bool)1 )\n" 15488 " continue;", 15489 Spaces); 15490 verifyFormat("for ( ;; )\n" 15491 " continue;", 15492 Spaces); 15493 verifyFormat("if ( true )\n" 15494 " f();\n" 15495 "else if ( true )\n" 15496 " f();", 15497 Spaces); 15498 verifyFormat("do {\n" 15499 " do_something( (int)i );\n" 15500 "} while ( something() );", 15501 Spaces); 15502 verifyFormat("switch ( x ) {\n" 15503 "default:\n" 15504 " break;\n" 15505 "}", 15506 Spaces); 15507 15508 Spaces.SpacesInParentheses = false; 15509 Spaces.SpacesInCStyleCastParentheses = true; 15510 verifyFormat("Type *A = ( Type * )P;", Spaces); 15511 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 15512 verifyFormat("x = ( int32 )y;", Spaces); 15513 verifyFormat("int a = ( int )(2.0f);", Spaces); 15514 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 15515 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 15516 verifyFormat("#define x (( int )-1)", Spaces); 15517 15518 // Run the first set of tests again with: 15519 Spaces.SpacesInParentheses = false; 15520 Spaces.SpaceInEmptyParentheses = true; 15521 Spaces.SpacesInCStyleCastParentheses = true; 15522 verifyFormat("call(x, y, z);", Spaces); 15523 verifyFormat("call( );", Spaces); 15524 verifyFormat("std::function<void(int, int)> callback;", Spaces); 15525 verifyFormat("while (( bool )1)\n" 15526 " continue;", 15527 Spaces); 15528 verifyFormat("for (;;)\n" 15529 " continue;", 15530 Spaces); 15531 verifyFormat("if (true)\n" 15532 " f( );\n" 15533 "else if (true)\n" 15534 " f( );", 15535 Spaces); 15536 verifyFormat("do {\n" 15537 " do_something(( int )i);\n" 15538 "} while (something( ));", 15539 Spaces); 15540 verifyFormat("switch (x) {\n" 15541 "default:\n" 15542 " break;\n" 15543 "}", 15544 Spaces); 15545 15546 // Run the first set of tests again with: 15547 Spaces.SpaceAfterCStyleCast = true; 15548 verifyFormat("call(x, y, z);", Spaces); 15549 verifyFormat("call( );", Spaces); 15550 verifyFormat("std::function<void(int, int)> callback;", Spaces); 15551 verifyFormat("while (( bool ) 1)\n" 15552 " continue;", 15553 Spaces); 15554 verifyFormat("for (;;)\n" 15555 " continue;", 15556 Spaces); 15557 verifyFormat("if (true)\n" 15558 " f( );\n" 15559 "else if (true)\n" 15560 " f( );", 15561 Spaces); 15562 verifyFormat("do {\n" 15563 " do_something(( int ) i);\n" 15564 "} while (something( ));", 15565 Spaces); 15566 verifyFormat("switch (x) {\n" 15567 "default:\n" 15568 " break;\n" 15569 "}", 15570 Spaces); 15571 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces); 15572 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces); 15573 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces); 15574 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); 15575 verifyFormat("bool *y = ( bool * ) (x);", Spaces); 15576 15577 // Run subset of tests again with: 15578 Spaces.SpacesInCStyleCastParentheses = false; 15579 Spaces.SpaceAfterCStyleCast = true; 15580 verifyFormat("while ((bool) 1)\n" 15581 " continue;", 15582 Spaces); 15583 verifyFormat("do {\n" 15584 " do_something((int) i);\n" 15585 "} while (something( ));", 15586 Spaces); 15587 15588 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 15589 verifyFormat("size_t idx = (size_t) a;", Spaces); 15590 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 15591 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 15592 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 15593 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 15594 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 15595 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces); 15596 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces); 15597 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces); 15598 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); 15599 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); 15600 Spaces.ColumnLimit = 80; 15601 Spaces.IndentWidth = 4; 15602 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 15603 verifyFormat("void foo( ) {\n" 15604 " size_t foo = (*(function))(\n" 15605 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 15606 "BarrrrrrrrrrrrLong,\n" 15607 " FoooooooooLooooong);\n" 15608 "}", 15609 Spaces); 15610 Spaces.SpaceAfterCStyleCast = false; 15611 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 15612 verifyFormat("size_t idx = (size_t)a;", Spaces); 15613 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 15614 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 15615 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 15616 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 15617 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 15618 15619 verifyFormat("void foo( ) {\n" 15620 " size_t foo = (*(function))(\n" 15621 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 15622 "BarrrrrrrrrrrrLong,\n" 15623 " FoooooooooLooooong);\n" 15624 "}", 15625 Spaces); 15626 } 15627 15628 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 15629 verifyFormat("int a[5];"); 15630 verifyFormat("a[3] += 42;"); 15631 15632 FormatStyle Spaces = getLLVMStyle(); 15633 Spaces.SpacesInSquareBrackets = true; 15634 // Not lambdas. 15635 verifyFormat("int a[ 5 ];", Spaces); 15636 verifyFormat("a[ 3 ] += 42;", Spaces); 15637 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 15638 verifyFormat("double &operator[](int i) { return 0; }\n" 15639 "int i;", 15640 Spaces); 15641 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 15642 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 15643 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 15644 // Lambdas. 15645 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 15646 verifyFormat("return [ i, args... ] {};", Spaces); 15647 verifyFormat("int foo = [ &bar ]() {};", Spaces); 15648 verifyFormat("int foo = [ = ]() {};", Spaces); 15649 verifyFormat("int foo = [ & ]() {};", Spaces); 15650 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 15651 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 15652 } 15653 15654 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 15655 FormatStyle NoSpaceStyle = getLLVMStyle(); 15656 verifyFormat("int a[5];", NoSpaceStyle); 15657 verifyFormat("a[3] += 42;", NoSpaceStyle); 15658 15659 verifyFormat("int a[1];", NoSpaceStyle); 15660 verifyFormat("int 1 [a];", NoSpaceStyle); 15661 verifyFormat("int a[1][2];", NoSpaceStyle); 15662 verifyFormat("a[7] = 5;", NoSpaceStyle); 15663 verifyFormat("int a = (f())[23];", NoSpaceStyle); 15664 verifyFormat("f([] {})", NoSpaceStyle); 15665 15666 FormatStyle Space = getLLVMStyle(); 15667 Space.SpaceBeforeSquareBrackets = true; 15668 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 15669 verifyFormat("return [i, args...] {};", Space); 15670 15671 verifyFormat("int a [5];", Space); 15672 verifyFormat("a [3] += 42;", Space); 15673 verifyFormat("constexpr char hello []{\"hello\"};", Space); 15674 verifyFormat("double &operator[](int i) { return 0; }\n" 15675 "int i;", 15676 Space); 15677 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 15678 verifyFormat("int i = a [a][a]->f();", Space); 15679 verifyFormat("int i = (*b) [a]->f();", Space); 15680 15681 verifyFormat("int a [1];", Space); 15682 verifyFormat("int 1 [a];", Space); 15683 verifyFormat("int a [1][2];", Space); 15684 verifyFormat("a [7] = 5;", Space); 15685 verifyFormat("int a = (f()) [23];", Space); 15686 verifyFormat("f([] {})", Space); 15687 } 15688 15689 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 15690 verifyFormat("int a = 5;"); 15691 verifyFormat("a += 42;"); 15692 verifyFormat("a or_eq 8;"); 15693 15694 FormatStyle Spaces = getLLVMStyle(); 15695 Spaces.SpaceBeforeAssignmentOperators = false; 15696 verifyFormat("int a= 5;", Spaces); 15697 verifyFormat("a+= 42;", Spaces); 15698 verifyFormat("a or_eq 8;", Spaces); 15699 } 15700 15701 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 15702 verifyFormat("class Foo : public Bar {};"); 15703 verifyFormat("Foo::Foo() : foo(1) {}"); 15704 verifyFormat("for (auto a : b) {\n}"); 15705 verifyFormat("int x = a ? b : c;"); 15706 verifyFormat("{\n" 15707 "label0:\n" 15708 " int x = 0;\n" 15709 "}"); 15710 verifyFormat("switch (x) {\n" 15711 "case 1:\n" 15712 "default:\n" 15713 "}"); 15714 verifyFormat("switch (allBraces) {\n" 15715 "case 1: {\n" 15716 " break;\n" 15717 "}\n" 15718 "case 2: {\n" 15719 " [[fallthrough]];\n" 15720 "}\n" 15721 "default: {\n" 15722 " break;\n" 15723 "}\n" 15724 "}"); 15725 15726 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 15727 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 15728 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 15729 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 15730 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 15731 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 15732 verifyFormat("{\n" 15733 "label1:\n" 15734 " int x = 0;\n" 15735 "}", 15736 CtorInitializerStyle); 15737 verifyFormat("switch (x) {\n" 15738 "case 1:\n" 15739 "default:\n" 15740 "}", 15741 CtorInitializerStyle); 15742 verifyFormat("switch (allBraces) {\n" 15743 "case 1: {\n" 15744 " break;\n" 15745 "}\n" 15746 "case 2: {\n" 15747 " [[fallthrough]];\n" 15748 "}\n" 15749 "default: {\n" 15750 " break;\n" 15751 "}\n" 15752 "}", 15753 CtorInitializerStyle); 15754 CtorInitializerStyle.BreakConstructorInitializers = 15755 FormatStyle::BCIS_AfterColon; 15756 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 15757 " aaaaaaaaaaaaaaaa(1),\n" 15758 " bbbbbbbbbbbbbbbb(2) {}", 15759 CtorInitializerStyle); 15760 CtorInitializerStyle.BreakConstructorInitializers = 15761 FormatStyle::BCIS_BeforeComma; 15762 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15763 " : aaaaaaaaaaaaaaaa(1)\n" 15764 " , bbbbbbbbbbbbbbbb(2) {}", 15765 CtorInitializerStyle); 15766 CtorInitializerStyle.BreakConstructorInitializers = 15767 FormatStyle::BCIS_BeforeColon; 15768 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15769 " : aaaaaaaaaaaaaaaa(1),\n" 15770 " bbbbbbbbbbbbbbbb(2) {}", 15771 CtorInitializerStyle); 15772 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 15773 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15774 ": aaaaaaaaaaaaaaaa(1),\n" 15775 " bbbbbbbbbbbbbbbb(2) {}", 15776 CtorInitializerStyle); 15777 15778 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 15779 InheritanceStyle.SpaceBeforeInheritanceColon = false; 15780 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 15781 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 15782 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 15783 verifyFormat("int x = a ? b : c;", InheritanceStyle); 15784 verifyFormat("{\n" 15785 "label2:\n" 15786 " int x = 0;\n" 15787 "}", 15788 InheritanceStyle); 15789 verifyFormat("switch (x) {\n" 15790 "case 1:\n" 15791 "default:\n" 15792 "}", 15793 InheritanceStyle); 15794 verifyFormat("switch (allBraces) {\n" 15795 "case 1: {\n" 15796 " break;\n" 15797 "}\n" 15798 "case 2: {\n" 15799 " [[fallthrough]];\n" 15800 "}\n" 15801 "default: {\n" 15802 " break;\n" 15803 "}\n" 15804 "}", 15805 InheritanceStyle); 15806 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 15807 verifyFormat("class Foooooooooooooooooooooo\n" 15808 " : public aaaaaaaaaaaaaaaaaa,\n" 15809 " public bbbbbbbbbbbbbbbbbb {\n" 15810 "}", 15811 InheritanceStyle); 15812 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 15813 verifyFormat("class Foooooooooooooooooooooo:\n" 15814 " public aaaaaaaaaaaaaaaaaa,\n" 15815 " public bbbbbbbbbbbbbbbbbb {\n" 15816 "}", 15817 InheritanceStyle); 15818 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 15819 verifyFormat("class Foooooooooooooooooooooo\n" 15820 " : public aaaaaaaaaaaaaaaaaa\n" 15821 " , public bbbbbbbbbbbbbbbbbb {\n" 15822 "}", 15823 InheritanceStyle); 15824 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 15825 verifyFormat("class Foooooooooooooooooooooo\n" 15826 " : public aaaaaaaaaaaaaaaaaa,\n" 15827 " public bbbbbbbbbbbbbbbbbb {\n" 15828 "}", 15829 InheritanceStyle); 15830 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 15831 verifyFormat("class Foooooooooooooooooooooo\n" 15832 ": public aaaaaaaaaaaaaaaaaa,\n" 15833 " public bbbbbbbbbbbbbbbbbb {}", 15834 InheritanceStyle); 15835 15836 FormatStyle ForLoopStyle = getLLVMStyle(); 15837 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 15838 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 15839 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 15840 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 15841 verifyFormat("int x = a ? b : c;", ForLoopStyle); 15842 verifyFormat("{\n" 15843 "label2:\n" 15844 " int x = 0;\n" 15845 "}", 15846 ForLoopStyle); 15847 verifyFormat("switch (x) {\n" 15848 "case 1:\n" 15849 "default:\n" 15850 "}", 15851 ForLoopStyle); 15852 verifyFormat("switch (allBraces) {\n" 15853 "case 1: {\n" 15854 " break;\n" 15855 "}\n" 15856 "case 2: {\n" 15857 " [[fallthrough]];\n" 15858 "}\n" 15859 "default: {\n" 15860 " break;\n" 15861 "}\n" 15862 "}", 15863 ForLoopStyle); 15864 15865 FormatStyle CaseStyle = getLLVMStyle(); 15866 CaseStyle.SpaceBeforeCaseColon = true; 15867 verifyFormat("class Foo : public Bar {};", CaseStyle); 15868 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 15869 verifyFormat("for (auto a : b) {\n}", CaseStyle); 15870 verifyFormat("int x = a ? b : c;", CaseStyle); 15871 verifyFormat("switch (x) {\n" 15872 "case 1 :\n" 15873 "default :\n" 15874 "}", 15875 CaseStyle); 15876 verifyFormat("switch (allBraces) {\n" 15877 "case 1 : {\n" 15878 " break;\n" 15879 "}\n" 15880 "case 2 : {\n" 15881 " [[fallthrough]];\n" 15882 "}\n" 15883 "default : {\n" 15884 " break;\n" 15885 "}\n" 15886 "}", 15887 CaseStyle); 15888 15889 FormatStyle NoSpaceStyle = getLLVMStyle(); 15890 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 15891 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15892 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 15893 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15894 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 15895 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 15896 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 15897 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 15898 verifyFormat("{\n" 15899 "label3:\n" 15900 " int x = 0;\n" 15901 "}", 15902 NoSpaceStyle); 15903 verifyFormat("switch (x) {\n" 15904 "case 1:\n" 15905 "default:\n" 15906 "}", 15907 NoSpaceStyle); 15908 verifyFormat("switch (allBraces) {\n" 15909 "case 1: {\n" 15910 " break;\n" 15911 "}\n" 15912 "case 2: {\n" 15913 " [[fallthrough]];\n" 15914 "}\n" 15915 "default: {\n" 15916 " break;\n" 15917 "}\n" 15918 "}", 15919 NoSpaceStyle); 15920 15921 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 15922 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 15923 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15924 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 15925 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15926 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 15927 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 15928 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 15929 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 15930 verifyFormat("{\n" 15931 "label3:\n" 15932 " int x = 0;\n" 15933 "}", 15934 InvertedSpaceStyle); 15935 verifyFormat("switch (x) {\n" 15936 "case 1 :\n" 15937 "case 2 : {\n" 15938 " break;\n" 15939 "}\n" 15940 "default :\n" 15941 " break;\n" 15942 "}", 15943 InvertedSpaceStyle); 15944 verifyFormat("switch (allBraces) {\n" 15945 "case 1 : {\n" 15946 " break;\n" 15947 "}\n" 15948 "case 2 : {\n" 15949 " [[fallthrough]];\n" 15950 "}\n" 15951 "default : {\n" 15952 " break;\n" 15953 "}\n" 15954 "}", 15955 InvertedSpaceStyle); 15956 } 15957 15958 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 15959 FormatStyle Style = getLLVMStyle(); 15960 15961 Style.PointerAlignment = FormatStyle::PAS_Left; 15962 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15963 verifyFormat("void* const* x = NULL;", Style); 15964 15965 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 15966 do { \ 15967 Style.PointerAlignment = FormatStyle::Pointers; \ 15968 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 15969 verifyFormat(Code, Style); \ 15970 } while (false) 15971 15972 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 15973 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 15974 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 15975 15976 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 15977 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 15978 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 15979 15980 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 15981 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 15982 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 15983 15984 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 15985 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 15986 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 15987 15988 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 15989 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15990 SAPQ_Default); 15991 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15992 SAPQ_Default); 15993 15994 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 15995 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15996 SAPQ_Before); 15997 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15998 SAPQ_Before); 15999 16000 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 16001 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 16002 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 16003 SAPQ_After); 16004 16005 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 16006 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 16007 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 16008 16009 #undef verifyQualifierSpaces 16010 16011 FormatStyle Spaces = getLLVMStyle(); 16012 Spaces.AttributeMacros.push_back("qualified"); 16013 Spaces.PointerAlignment = FormatStyle::PAS_Right; 16014 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 16015 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 16016 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 16017 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 16018 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 16019 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 16020 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 16021 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 16022 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 16023 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 16024 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 16025 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 16026 16027 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 16028 Spaces.PointerAlignment = FormatStyle::PAS_Left; 16029 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 16030 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 16031 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 16032 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 16033 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 16034 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 16035 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 16036 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 16037 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 16038 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 16039 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 16040 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 16041 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 16042 16043 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 16044 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 16045 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 16046 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 16047 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 16048 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 16049 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 16050 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 16051 } 16052 16053 TEST_F(FormatTest, AlignConsecutiveMacros) { 16054 FormatStyle Style = getLLVMStyle(); 16055 Style.AlignConsecutiveAssignments.Enabled = true; 16056 Style.AlignConsecutiveDeclarations.Enabled = true; 16057 16058 verifyFormat("#define a 3\n" 16059 "#define bbbb 4\n" 16060 "#define ccc (5)", 16061 Style); 16062 16063 verifyFormat("#define f(x) (x * x)\n" 16064 "#define fff(x, y, z) (x * y + z)\n" 16065 "#define ffff(x, y) (x - y)", 16066 Style); 16067 16068 verifyFormat("#define foo(x, y) (x + y)\n" 16069 "#define bar (5, 6)(2 + 2)", 16070 Style); 16071 16072 verifyFormat("#define a 3\n" 16073 "#define bbbb 4\n" 16074 "#define ccc (5)\n" 16075 "#define f(x) (x * x)\n" 16076 "#define fff(x, y, z) (x * y + z)\n" 16077 "#define ffff(x, y) (x - y)", 16078 Style); 16079 16080 Style.AlignConsecutiveMacros.Enabled = true; 16081 verifyFormat("#define a 3\n" 16082 "#define bbbb 4\n" 16083 "#define ccc (5)", 16084 Style); 16085 16086 verifyFormat("#define true 1\n" 16087 "#define false 0", 16088 Style); 16089 16090 verifyFormat("#define f(x) (x * x)\n" 16091 "#define fff(x, y, z) (x * y + z)\n" 16092 "#define ffff(x, y) (x - y)", 16093 Style); 16094 16095 verifyFormat("#define foo(x, y) (x + y)\n" 16096 "#define bar (5, 6)(2 + 2)", 16097 Style); 16098 16099 verifyFormat("#define a 3\n" 16100 "#define bbbb 4\n" 16101 "#define ccc (5)\n" 16102 "#define f(x) (x * x)\n" 16103 "#define fff(x, y, z) (x * y + z)\n" 16104 "#define ffff(x, y) (x - y)", 16105 Style); 16106 16107 verifyFormat("#define a 5\n" 16108 "#define foo(x, y) (x + y)\n" 16109 "#define CCC (6)\n" 16110 "auto lambda = []() {\n" 16111 " auto ii = 0;\n" 16112 " float j = 0;\n" 16113 " return 0;\n" 16114 "};\n" 16115 "int i = 0;\n" 16116 "float i2 = 0;\n" 16117 "auto v = type{\n" 16118 " i = 1, //\n" 16119 " (i = 2), //\n" 16120 " i = 3 //\n" 16121 "};", 16122 Style); 16123 16124 Style.AlignConsecutiveMacros.Enabled = false; 16125 Style.ColumnLimit = 20; 16126 16127 verifyFormat("#define a \\\n" 16128 " \"aabbbbbbbbbbbb\"\n" 16129 "#define D \\\n" 16130 " \"aabbbbbbbbbbbb\" \\\n" 16131 " \"ccddeeeeeeeee\"\n" 16132 "#define B \\\n" 16133 " \"QQQQQQQQQQQQQ\" \\\n" 16134 " \"FFFFFFFFFFFFF\" \\\n" 16135 " \"LLLLLLLL\"\n", 16136 Style); 16137 16138 Style.AlignConsecutiveMacros.Enabled = true; 16139 verifyFormat("#define a \\\n" 16140 " \"aabbbbbbbbbbbb\"\n" 16141 "#define D \\\n" 16142 " \"aabbbbbbbbbbbb\" \\\n" 16143 " \"ccddeeeeeeeee\"\n" 16144 "#define B \\\n" 16145 " \"QQQQQQQQQQQQQ\" \\\n" 16146 " \"FFFFFFFFFFFFF\" \\\n" 16147 " \"LLLLLLLL\"\n", 16148 Style); 16149 16150 // Test across comments 16151 Style.MaxEmptyLinesToKeep = 10; 16152 Style.ReflowComments = false; 16153 Style.AlignConsecutiveMacros.AcrossComments = true; 16154 EXPECT_EQ("#define a 3\n" 16155 "// line comment\n" 16156 "#define bbbb 4\n" 16157 "#define ccc (5)", 16158 format("#define a 3\n" 16159 "// line comment\n" 16160 "#define bbbb 4\n" 16161 "#define ccc (5)", 16162 Style)); 16163 16164 EXPECT_EQ("#define a 3\n" 16165 "/* block comment */\n" 16166 "#define bbbb 4\n" 16167 "#define ccc (5)", 16168 format("#define a 3\n" 16169 "/* block comment */\n" 16170 "#define bbbb 4\n" 16171 "#define ccc (5)", 16172 Style)); 16173 16174 EXPECT_EQ("#define a 3\n" 16175 "/* multi-line *\n" 16176 " * block comment */\n" 16177 "#define bbbb 4\n" 16178 "#define ccc (5)", 16179 format("#define a 3\n" 16180 "/* multi-line *\n" 16181 " * block comment */\n" 16182 "#define bbbb 4\n" 16183 "#define ccc (5)", 16184 Style)); 16185 16186 EXPECT_EQ("#define a 3\n" 16187 "// multi-line line comment\n" 16188 "//\n" 16189 "#define bbbb 4\n" 16190 "#define ccc (5)", 16191 format("#define a 3\n" 16192 "// multi-line line comment\n" 16193 "//\n" 16194 "#define bbbb 4\n" 16195 "#define ccc (5)", 16196 Style)); 16197 16198 EXPECT_EQ("#define a 3\n" 16199 "// empty lines still break.\n" 16200 "\n" 16201 "#define bbbb 4\n" 16202 "#define ccc (5)", 16203 format("#define a 3\n" 16204 "// empty lines still break.\n" 16205 "\n" 16206 "#define bbbb 4\n" 16207 "#define ccc (5)", 16208 Style)); 16209 16210 // Test across empty lines 16211 Style.AlignConsecutiveMacros.AcrossComments = false; 16212 Style.AlignConsecutiveMacros.AcrossEmptyLines = true; 16213 EXPECT_EQ("#define a 3\n" 16214 "\n" 16215 "#define bbbb 4\n" 16216 "#define ccc (5)", 16217 format("#define a 3\n" 16218 "\n" 16219 "#define bbbb 4\n" 16220 "#define ccc (5)", 16221 Style)); 16222 16223 EXPECT_EQ("#define a 3\n" 16224 "\n" 16225 "\n" 16226 "\n" 16227 "#define bbbb 4\n" 16228 "#define ccc (5)", 16229 format("#define a 3\n" 16230 "\n" 16231 "\n" 16232 "\n" 16233 "#define bbbb 4\n" 16234 "#define ccc (5)", 16235 Style)); 16236 16237 EXPECT_EQ("#define a 3\n" 16238 "// comments should break alignment\n" 16239 "//\n" 16240 "#define bbbb 4\n" 16241 "#define ccc (5)", 16242 format("#define a 3\n" 16243 "// comments should break alignment\n" 16244 "//\n" 16245 "#define bbbb 4\n" 16246 "#define ccc (5)", 16247 Style)); 16248 16249 // Test across empty lines and comments 16250 Style.AlignConsecutiveMacros.AcrossComments = true; 16251 verifyFormat("#define a 3\n" 16252 "\n" 16253 "// line comment\n" 16254 "#define bbbb 4\n" 16255 "#define ccc (5)", 16256 Style); 16257 16258 EXPECT_EQ("#define a 3\n" 16259 "\n" 16260 "\n" 16261 "/* multi-line *\n" 16262 " * block comment */\n" 16263 "\n" 16264 "\n" 16265 "#define bbbb 4\n" 16266 "#define ccc (5)", 16267 format("#define a 3\n" 16268 "\n" 16269 "\n" 16270 "/* multi-line *\n" 16271 " * block comment */\n" 16272 "\n" 16273 "\n" 16274 "#define bbbb 4\n" 16275 "#define ccc (5)", 16276 Style)); 16277 16278 EXPECT_EQ("#define a 3\n" 16279 "\n" 16280 "\n" 16281 "/* multi-line *\n" 16282 " * block comment */\n" 16283 "\n" 16284 "\n" 16285 "#define bbbb 4\n" 16286 "#define ccc (5)", 16287 format("#define a 3\n" 16288 "\n" 16289 "\n" 16290 "/* multi-line *\n" 16291 " * block comment */\n" 16292 "\n" 16293 "\n" 16294 "#define bbbb 4\n" 16295 "#define ccc (5)", 16296 Style)); 16297 } 16298 16299 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 16300 FormatStyle Alignment = getLLVMStyle(); 16301 Alignment.AlignConsecutiveMacros.Enabled = true; 16302 Alignment.AlignConsecutiveAssignments.Enabled = true; 16303 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 16304 16305 Alignment.MaxEmptyLinesToKeep = 10; 16306 /* Test alignment across empty lines */ 16307 EXPECT_EQ("int a = 5;\n" 16308 "\n" 16309 "int oneTwoThree = 123;", 16310 format("int a = 5;\n" 16311 "\n" 16312 "int oneTwoThree= 123;", 16313 Alignment)); 16314 EXPECT_EQ("int a = 5;\n" 16315 "int one = 1;\n" 16316 "\n" 16317 "int oneTwoThree = 123;", 16318 format("int a = 5;\n" 16319 "int one = 1;\n" 16320 "\n" 16321 "int oneTwoThree = 123;", 16322 Alignment)); 16323 EXPECT_EQ("int a = 5;\n" 16324 "int one = 1;\n" 16325 "\n" 16326 "int oneTwoThree = 123;\n" 16327 "int oneTwo = 12;", 16328 format("int a = 5;\n" 16329 "int one = 1;\n" 16330 "\n" 16331 "int oneTwoThree = 123;\n" 16332 "int oneTwo = 12;", 16333 Alignment)); 16334 16335 /* Test across comments */ 16336 EXPECT_EQ("int a = 5;\n" 16337 "/* block comment */\n" 16338 "int oneTwoThree = 123;", 16339 format("int a = 5;\n" 16340 "/* block comment */\n" 16341 "int oneTwoThree=123;", 16342 Alignment)); 16343 16344 EXPECT_EQ("int a = 5;\n" 16345 "// line comment\n" 16346 "int oneTwoThree = 123;", 16347 format("int a = 5;\n" 16348 "// line comment\n" 16349 "int oneTwoThree=123;", 16350 Alignment)); 16351 16352 /* Test across comments and newlines */ 16353 EXPECT_EQ("int a = 5;\n" 16354 "\n" 16355 "/* block comment */\n" 16356 "int oneTwoThree = 123;", 16357 format("int a = 5;\n" 16358 "\n" 16359 "/* block comment */\n" 16360 "int oneTwoThree=123;", 16361 Alignment)); 16362 16363 EXPECT_EQ("int a = 5;\n" 16364 "\n" 16365 "// line comment\n" 16366 "int oneTwoThree = 123;", 16367 format("int a = 5;\n" 16368 "\n" 16369 "// line comment\n" 16370 "int oneTwoThree=123;", 16371 Alignment)); 16372 } 16373 16374 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 16375 FormatStyle Alignment = getLLVMStyle(); 16376 Alignment.AlignConsecutiveDeclarations.Enabled = true; 16377 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true; 16378 Alignment.AlignConsecutiveDeclarations.AcrossComments = true; 16379 16380 Alignment.MaxEmptyLinesToKeep = 10; 16381 /* Test alignment across empty lines */ 16382 EXPECT_EQ("int a = 5;\n" 16383 "\n" 16384 "float const oneTwoThree = 123;", 16385 format("int a = 5;\n" 16386 "\n" 16387 "float const oneTwoThree = 123;", 16388 Alignment)); 16389 EXPECT_EQ("int a = 5;\n" 16390 "float const one = 1;\n" 16391 "\n" 16392 "int oneTwoThree = 123;", 16393 format("int a = 5;\n" 16394 "float const one = 1;\n" 16395 "\n" 16396 "int oneTwoThree = 123;", 16397 Alignment)); 16398 16399 /* Test across comments */ 16400 EXPECT_EQ("float const a = 5;\n" 16401 "/* block comment */\n" 16402 "int oneTwoThree = 123;", 16403 format("float const a = 5;\n" 16404 "/* block comment */\n" 16405 "int oneTwoThree=123;", 16406 Alignment)); 16407 16408 EXPECT_EQ("float const a = 5;\n" 16409 "// line comment\n" 16410 "int oneTwoThree = 123;", 16411 format("float const a = 5;\n" 16412 "// line comment\n" 16413 "int oneTwoThree=123;", 16414 Alignment)); 16415 16416 /* Test across comments and newlines */ 16417 EXPECT_EQ("float const a = 5;\n" 16418 "\n" 16419 "/* block comment */\n" 16420 "int oneTwoThree = 123;", 16421 format("float const a = 5;\n" 16422 "\n" 16423 "/* block comment */\n" 16424 "int oneTwoThree=123;", 16425 Alignment)); 16426 16427 EXPECT_EQ("float const a = 5;\n" 16428 "\n" 16429 "// line comment\n" 16430 "int oneTwoThree = 123;", 16431 format("float const a = 5;\n" 16432 "\n" 16433 "// line comment\n" 16434 "int oneTwoThree=123;", 16435 Alignment)); 16436 } 16437 16438 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 16439 FormatStyle Alignment = getLLVMStyle(); 16440 Alignment.AlignConsecutiveBitFields.Enabled = true; 16441 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true; 16442 Alignment.AlignConsecutiveBitFields.AcrossComments = true; 16443 16444 Alignment.MaxEmptyLinesToKeep = 10; 16445 /* Test alignment across empty lines */ 16446 EXPECT_EQ("int a : 5;\n" 16447 "\n" 16448 "int longbitfield : 6;", 16449 format("int a : 5;\n" 16450 "\n" 16451 "int longbitfield : 6;", 16452 Alignment)); 16453 EXPECT_EQ("int a : 5;\n" 16454 "int one : 1;\n" 16455 "\n" 16456 "int longbitfield : 6;", 16457 format("int a : 5;\n" 16458 "int one : 1;\n" 16459 "\n" 16460 "int longbitfield : 6;", 16461 Alignment)); 16462 16463 /* Test across comments */ 16464 EXPECT_EQ("int a : 5;\n" 16465 "/* block comment */\n" 16466 "int longbitfield : 6;", 16467 format("int a : 5;\n" 16468 "/* block comment */\n" 16469 "int longbitfield : 6;", 16470 Alignment)); 16471 EXPECT_EQ("int a : 5;\n" 16472 "int one : 1;\n" 16473 "// line comment\n" 16474 "int longbitfield : 6;", 16475 format("int a : 5;\n" 16476 "int one : 1;\n" 16477 "// line comment\n" 16478 "int longbitfield : 6;", 16479 Alignment)); 16480 16481 /* Test across comments and newlines */ 16482 EXPECT_EQ("int a : 5;\n" 16483 "/* block comment */\n" 16484 "\n" 16485 "int longbitfield : 6;", 16486 format("int a : 5;\n" 16487 "/* block comment */\n" 16488 "\n" 16489 "int longbitfield : 6;", 16490 Alignment)); 16491 EXPECT_EQ("int a : 5;\n" 16492 "int one : 1;\n" 16493 "\n" 16494 "// line comment\n" 16495 "\n" 16496 "int longbitfield : 6;", 16497 format("int a : 5;\n" 16498 "int one : 1;\n" 16499 "\n" 16500 "// line comment \n" 16501 "\n" 16502 "int longbitfield : 6;", 16503 Alignment)); 16504 } 16505 16506 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 16507 FormatStyle Alignment = getLLVMStyle(); 16508 Alignment.AlignConsecutiveMacros.Enabled = true; 16509 Alignment.AlignConsecutiveAssignments.Enabled = true; 16510 Alignment.AlignConsecutiveAssignments.AcrossComments = true; 16511 16512 Alignment.MaxEmptyLinesToKeep = 10; 16513 /* Test alignment across empty lines */ 16514 EXPECT_EQ("int a = 5;\n" 16515 "\n" 16516 "int oneTwoThree = 123;", 16517 format("int a = 5;\n" 16518 "\n" 16519 "int oneTwoThree= 123;", 16520 Alignment)); 16521 EXPECT_EQ("int a = 5;\n" 16522 "int one = 1;\n" 16523 "\n" 16524 "int oneTwoThree = 123;", 16525 format("int a = 5;\n" 16526 "int one = 1;\n" 16527 "\n" 16528 "int oneTwoThree = 123;", 16529 Alignment)); 16530 16531 /* Test across comments */ 16532 EXPECT_EQ("int a = 5;\n" 16533 "/* block comment */\n" 16534 "int oneTwoThree = 123;", 16535 format("int a = 5;\n" 16536 "/* block comment */\n" 16537 "int oneTwoThree=123;", 16538 Alignment)); 16539 16540 EXPECT_EQ("int a = 5;\n" 16541 "// line comment\n" 16542 "int oneTwoThree = 123;", 16543 format("int a = 5;\n" 16544 "// line comment\n" 16545 "int oneTwoThree=123;", 16546 Alignment)); 16547 16548 EXPECT_EQ("int a = 5;\n" 16549 "/*\n" 16550 " * multi-line block comment\n" 16551 " */\n" 16552 "int oneTwoThree = 123;", 16553 format("int a = 5;\n" 16554 "/*\n" 16555 " * multi-line block comment\n" 16556 " */\n" 16557 "int oneTwoThree=123;", 16558 Alignment)); 16559 16560 EXPECT_EQ("int a = 5;\n" 16561 "//\n" 16562 "// multi-line line comment\n" 16563 "//\n" 16564 "int oneTwoThree = 123;", 16565 format("int a = 5;\n" 16566 "//\n" 16567 "// multi-line line comment\n" 16568 "//\n" 16569 "int oneTwoThree=123;", 16570 Alignment)); 16571 16572 /* Test across comments and newlines */ 16573 EXPECT_EQ("int a = 5;\n" 16574 "\n" 16575 "/* block comment */\n" 16576 "int oneTwoThree = 123;", 16577 format("int a = 5;\n" 16578 "\n" 16579 "/* block comment */\n" 16580 "int oneTwoThree=123;", 16581 Alignment)); 16582 16583 EXPECT_EQ("int a = 5;\n" 16584 "\n" 16585 "// line comment\n" 16586 "int oneTwoThree = 123;", 16587 format("int a = 5;\n" 16588 "\n" 16589 "// line comment\n" 16590 "int oneTwoThree=123;", 16591 Alignment)); 16592 } 16593 16594 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 16595 FormatStyle Alignment = getLLVMStyle(); 16596 Alignment.AlignConsecutiveMacros.Enabled = true; 16597 Alignment.AlignConsecutiveAssignments.Enabled = true; 16598 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 16599 Alignment.AlignConsecutiveAssignments.AcrossComments = true; 16600 verifyFormat("int a = 5;\n" 16601 "int oneTwoThree = 123;", 16602 Alignment); 16603 verifyFormat("int a = method();\n" 16604 "int oneTwoThree = 133;", 16605 Alignment); 16606 verifyFormat("a &= 5;\n" 16607 "bcd *= 5;\n" 16608 "ghtyf += 5;\n" 16609 "dvfvdb -= 5;\n" 16610 "a /= 5;\n" 16611 "vdsvsv %= 5;\n" 16612 "sfdbddfbdfbb ^= 5;\n" 16613 "dvsdsv |= 5;\n" 16614 "int dsvvdvsdvvv = 123;", 16615 Alignment); 16616 verifyFormat("int i = 1, j = 10;\n" 16617 "something = 2000;", 16618 Alignment); 16619 verifyFormat("something = 2000;\n" 16620 "int i = 1, j = 10;\n", 16621 Alignment); 16622 verifyFormat("something = 2000;\n" 16623 "another = 911;\n" 16624 "int i = 1, j = 10;\n" 16625 "oneMore = 1;\n" 16626 "i = 2;", 16627 Alignment); 16628 verifyFormat("int a = 5;\n" 16629 "int one = 1;\n" 16630 "method();\n" 16631 "int oneTwoThree = 123;\n" 16632 "int oneTwo = 12;", 16633 Alignment); 16634 verifyFormat("int oneTwoThree = 123;\n" 16635 "int oneTwo = 12;\n" 16636 "method();\n", 16637 Alignment); 16638 verifyFormat("int oneTwoThree = 123; // comment\n" 16639 "int oneTwo = 12; // comment", 16640 Alignment); 16641 16642 // Bug 25167 16643 /* Uncomment when fixed 16644 verifyFormat("#if A\n" 16645 "#else\n" 16646 "int aaaaaaaa = 12;\n" 16647 "#endif\n" 16648 "#if B\n" 16649 "#else\n" 16650 "int a = 12;\n" 16651 "#endif\n", 16652 Alignment); 16653 verifyFormat("enum foo {\n" 16654 "#if A\n" 16655 "#else\n" 16656 " aaaaaaaa = 12;\n" 16657 "#endif\n" 16658 "#if B\n" 16659 "#else\n" 16660 " a = 12;\n" 16661 "#endif\n" 16662 "};\n", 16663 Alignment); 16664 */ 16665 16666 Alignment.MaxEmptyLinesToKeep = 10; 16667 /* Test alignment across empty lines */ 16668 EXPECT_EQ("int a = 5;\n" 16669 "\n" 16670 "int oneTwoThree = 123;", 16671 format("int a = 5;\n" 16672 "\n" 16673 "int oneTwoThree= 123;", 16674 Alignment)); 16675 EXPECT_EQ("int a = 5;\n" 16676 "int one = 1;\n" 16677 "\n" 16678 "int oneTwoThree = 123;", 16679 format("int a = 5;\n" 16680 "int one = 1;\n" 16681 "\n" 16682 "int oneTwoThree = 123;", 16683 Alignment)); 16684 EXPECT_EQ("int a = 5;\n" 16685 "int one = 1;\n" 16686 "\n" 16687 "int oneTwoThree = 123;\n" 16688 "int oneTwo = 12;", 16689 format("int a = 5;\n" 16690 "int one = 1;\n" 16691 "\n" 16692 "int oneTwoThree = 123;\n" 16693 "int oneTwo = 12;", 16694 Alignment)); 16695 16696 /* Test across comments */ 16697 EXPECT_EQ("int a = 5;\n" 16698 "/* block comment */\n" 16699 "int oneTwoThree = 123;", 16700 format("int a = 5;\n" 16701 "/* block comment */\n" 16702 "int oneTwoThree=123;", 16703 Alignment)); 16704 16705 EXPECT_EQ("int a = 5;\n" 16706 "// line comment\n" 16707 "int oneTwoThree = 123;", 16708 format("int a = 5;\n" 16709 "// line comment\n" 16710 "int oneTwoThree=123;", 16711 Alignment)); 16712 16713 /* Test across comments and newlines */ 16714 EXPECT_EQ("int a = 5;\n" 16715 "\n" 16716 "/* block comment */\n" 16717 "int oneTwoThree = 123;", 16718 format("int a = 5;\n" 16719 "\n" 16720 "/* block comment */\n" 16721 "int oneTwoThree=123;", 16722 Alignment)); 16723 16724 EXPECT_EQ("int a = 5;\n" 16725 "\n" 16726 "// line comment\n" 16727 "int oneTwoThree = 123;", 16728 format("int a = 5;\n" 16729 "\n" 16730 "// line comment\n" 16731 "int oneTwoThree=123;", 16732 Alignment)); 16733 16734 EXPECT_EQ("int a = 5;\n" 16735 "//\n" 16736 "// multi-line line comment\n" 16737 "//\n" 16738 "int oneTwoThree = 123;", 16739 format("int a = 5;\n" 16740 "//\n" 16741 "// multi-line line comment\n" 16742 "//\n" 16743 "int oneTwoThree=123;", 16744 Alignment)); 16745 16746 EXPECT_EQ("int a = 5;\n" 16747 "/*\n" 16748 " * multi-line block comment\n" 16749 " */\n" 16750 "int oneTwoThree = 123;", 16751 format("int a = 5;\n" 16752 "/*\n" 16753 " * multi-line block comment\n" 16754 " */\n" 16755 "int oneTwoThree=123;", 16756 Alignment)); 16757 16758 EXPECT_EQ("int a = 5;\n" 16759 "\n" 16760 "/* block comment */\n" 16761 "\n" 16762 "\n" 16763 "\n" 16764 "int oneTwoThree = 123;", 16765 format("int a = 5;\n" 16766 "\n" 16767 "/* block comment */\n" 16768 "\n" 16769 "\n" 16770 "\n" 16771 "int oneTwoThree=123;", 16772 Alignment)); 16773 16774 EXPECT_EQ("int a = 5;\n" 16775 "\n" 16776 "// line comment\n" 16777 "\n" 16778 "\n" 16779 "\n" 16780 "int oneTwoThree = 123;", 16781 format("int a = 5;\n" 16782 "\n" 16783 "// line comment\n" 16784 "\n" 16785 "\n" 16786 "\n" 16787 "int oneTwoThree=123;", 16788 Alignment)); 16789 16790 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16791 verifyFormat("#define A \\\n" 16792 " int aaaa = 12; \\\n" 16793 " int b = 23; \\\n" 16794 " int ccc = 234; \\\n" 16795 " int dddddddddd = 2345;", 16796 Alignment); 16797 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16798 verifyFormat("#define A \\\n" 16799 " int aaaa = 12; \\\n" 16800 " int b = 23; \\\n" 16801 " int ccc = 234; \\\n" 16802 " int dddddddddd = 2345;", 16803 Alignment); 16804 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16805 verifyFormat("#define A " 16806 " \\\n" 16807 " int aaaa = 12; " 16808 " \\\n" 16809 " int b = 23; " 16810 " \\\n" 16811 " int ccc = 234; " 16812 " \\\n" 16813 " int dddddddddd = 2345;", 16814 Alignment); 16815 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16816 "k = 4, int l = 5,\n" 16817 " int m = 6) {\n" 16818 " int j = 10;\n" 16819 " otherThing = 1;\n" 16820 "}", 16821 Alignment); 16822 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16823 " int i = 1;\n" 16824 " int j = 2;\n" 16825 " int big = 10000;\n" 16826 "}", 16827 Alignment); 16828 verifyFormat("class C {\n" 16829 "public:\n" 16830 " int i = 1;\n" 16831 " virtual void f() = 0;\n" 16832 "};", 16833 Alignment); 16834 verifyFormat("int i = 1;\n" 16835 "if (SomeType t = getSomething()) {\n" 16836 "}\n" 16837 "int j = 2;\n" 16838 "int big = 10000;", 16839 Alignment); 16840 verifyFormat("int j = 7;\n" 16841 "for (int k = 0; k < N; ++k) {\n" 16842 "}\n" 16843 "int j = 2;\n" 16844 "int big = 10000;\n" 16845 "}", 16846 Alignment); 16847 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16848 verifyFormat("int i = 1;\n" 16849 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16850 " = someLooooooooooooooooongFunction();\n" 16851 "int j = 2;", 16852 Alignment); 16853 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16854 verifyFormat("int i = 1;\n" 16855 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16856 " someLooooooooooooooooongFunction();\n" 16857 "int j = 2;", 16858 Alignment); 16859 16860 verifyFormat("auto lambda = []() {\n" 16861 " auto i = 0;\n" 16862 " return 0;\n" 16863 "};\n" 16864 "int i = 0;\n" 16865 "auto v = type{\n" 16866 " i = 1, //\n" 16867 " (i = 2), //\n" 16868 " i = 3 //\n" 16869 "};", 16870 Alignment); 16871 16872 verifyFormat( 16873 "int i = 1;\n" 16874 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16875 " loooooooooooooooooooooongParameterB);\n" 16876 "int j = 2;", 16877 Alignment); 16878 16879 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16880 " typename B = very_long_type_name_1,\n" 16881 " typename T_2 = very_long_type_name_2>\n" 16882 "auto foo() {}\n", 16883 Alignment); 16884 verifyFormat("int a, b = 1;\n" 16885 "int c = 2;\n" 16886 "int dd = 3;\n", 16887 Alignment); 16888 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16889 "float b[1][] = {{3.f}};\n", 16890 Alignment); 16891 verifyFormat("for (int i = 0; i < 1; i++)\n" 16892 " int x = 1;\n", 16893 Alignment); 16894 verifyFormat("for (i = 0; i < 1; i++)\n" 16895 " x = 1;\n" 16896 "y = 1;\n", 16897 Alignment); 16898 16899 Alignment.ReflowComments = true; 16900 Alignment.ColumnLimit = 50; 16901 EXPECT_EQ("int x = 0;\n" 16902 "int yy = 1; /// specificlennospace\n" 16903 "int zzz = 2;\n", 16904 format("int x = 0;\n" 16905 "int yy = 1; ///specificlennospace\n" 16906 "int zzz = 2;\n", 16907 Alignment)); 16908 } 16909 16910 TEST_F(FormatTest, AlignCompoundAssignments) { 16911 FormatStyle Alignment = getLLVMStyle(); 16912 Alignment.AlignConsecutiveAssignments.Enabled = true; 16913 Alignment.AlignConsecutiveAssignments.AlignCompound = true; 16914 Alignment.AlignConsecutiveAssignments.PadOperators = false; 16915 verifyFormat("sfdbddfbdfbb = 5;\n" 16916 "dvsdsv = 5;\n" 16917 "int dsvvdvsdvvv = 123;", 16918 Alignment); 16919 verifyFormat("sfdbddfbdfbb ^= 5;\n" 16920 "dvsdsv |= 5;\n" 16921 "int dsvvdvsdvvv = 123;", 16922 Alignment); 16923 verifyFormat("sfdbddfbdfbb ^= 5;\n" 16924 "dvsdsv <<= 5;\n" 16925 "int dsvvdvsdvvv = 123;", 16926 Alignment); 16927 // Test that `<=` is not treated as a compound assignment. 16928 verifyFormat("aa &= 5;\n" 16929 "b <= 10;\n" 16930 "c = 15;", 16931 Alignment); 16932 Alignment.AlignConsecutiveAssignments.PadOperators = true; 16933 verifyFormat("sfdbddfbdfbb = 5;\n" 16934 "dvsdsv = 5;\n" 16935 "int dsvvdvsdvvv = 123;", 16936 Alignment); 16937 verifyFormat("sfdbddfbdfbb ^= 5;\n" 16938 "dvsdsv |= 5;\n" 16939 "int dsvvdvsdvvv = 123;", 16940 Alignment); 16941 verifyFormat("sfdbddfbdfbb ^= 5;\n" 16942 "dvsdsv <<= 5;\n" 16943 "int dsvvdvsdvvv = 123;", 16944 Alignment); 16945 EXPECT_EQ("a += 5;\n" 16946 "one = 1;\n" 16947 "\n" 16948 "oneTwoThree = 123;\n", 16949 format("a += 5;\n" 16950 "one = 1;\n" 16951 "\n" 16952 "oneTwoThree = 123;\n", 16953 Alignment)); 16954 EXPECT_EQ("a += 5;\n" 16955 "one = 1;\n" 16956 "//\n" 16957 "oneTwoThree = 123;\n", 16958 format("a += 5;\n" 16959 "one = 1;\n" 16960 "//\n" 16961 "oneTwoThree = 123;\n", 16962 Alignment)); 16963 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 16964 EXPECT_EQ("a += 5;\n" 16965 "one = 1;\n" 16966 "\n" 16967 "oneTwoThree = 123;\n", 16968 format("a += 5;\n" 16969 "one = 1;\n" 16970 "\n" 16971 "oneTwoThree = 123;\n", 16972 Alignment)); 16973 EXPECT_EQ("a += 5;\n" 16974 "one = 1;\n" 16975 "//\n" 16976 "oneTwoThree = 123;\n", 16977 format("a += 5;\n" 16978 "one = 1;\n" 16979 "//\n" 16980 "oneTwoThree = 123;\n", 16981 Alignment)); 16982 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false; 16983 Alignment.AlignConsecutiveAssignments.AcrossComments = true; 16984 EXPECT_EQ("a += 5;\n" 16985 "one = 1;\n" 16986 "\n" 16987 "oneTwoThree = 123;\n", 16988 format("a += 5;\n" 16989 "one = 1;\n" 16990 "\n" 16991 "oneTwoThree = 123;\n", 16992 Alignment)); 16993 EXPECT_EQ("a += 5;\n" 16994 "one = 1;\n" 16995 "//\n" 16996 "oneTwoThree = 123;\n", 16997 format("a += 5;\n" 16998 "one = 1;\n" 16999 "//\n" 17000 "oneTwoThree = 123;\n", 17001 Alignment)); 17002 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 17003 EXPECT_EQ("a += 5;\n" 17004 "one >>= 1;\n" 17005 "\n" 17006 "oneTwoThree = 123;\n", 17007 format("a += 5;\n" 17008 "one >>= 1;\n" 17009 "\n" 17010 "oneTwoThree = 123;\n", 17011 Alignment)); 17012 EXPECT_EQ("a += 5;\n" 17013 "one = 1;\n" 17014 "//\n" 17015 "oneTwoThree <<= 123;\n", 17016 format("a += 5;\n" 17017 "one = 1;\n" 17018 "//\n" 17019 "oneTwoThree <<= 123;\n", 17020 Alignment)); 17021 } 17022 17023 TEST_F(FormatTest, AlignConsecutiveAssignments) { 17024 FormatStyle Alignment = getLLVMStyle(); 17025 Alignment.AlignConsecutiveMacros.Enabled = true; 17026 verifyFormat("int a = 5;\n" 17027 "int oneTwoThree = 123;", 17028 Alignment); 17029 verifyFormat("int a = 5;\n" 17030 "int oneTwoThree = 123;", 17031 Alignment); 17032 17033 Alignment.AlignConsecutiveAssignments.Enabled = true; 17034 verifyFormat("int a = 5;\n" 17035 "int oneTwoThree = 123;", 17036 Alignment); 17037 verifyFormat("int a = method();\n" 17038 "int oneTwoThree = 133;", 17039 Alignment); 17040 verifyFormat("aa <= 5;\n" 17041 "a &= 5;\n" 17042 "bcd *= 5;\n" 17043 "ghtyf += 5;\n" 17044 "dvfvdb -= 5;\n" 17045 "a /= 5;\n" 17046 "vdsvsv %= 5;\n" 17047 "sfdbddfbdfbb ^= 5;\n" 17048 "dvsdsv |= 5;\n" 17049 "int dsvvdvsdvvv = 123;", 17050 Alignment); 17051 verifyFormat("int i = 1, j = 10;\n" 17052 "something = 2000;", 17053 Alignment); 17054 verifyFormat("something = 2000;\n" 17055 "int i = 1, j = 10;\n", 17056 Alignment); 17057 verifyFormat("something = 2000;\n" 17058 "another = 911;\n" 17059 "int i = 1, j = 10;\n" 17060 "oneMore = 1;\n" 17061 "i = 2;", 17062 Alignment); 17063 verifyFormat("int a = 5;\n" 17064 "int one = 1;\n" 17065 "method();\n" 17066 "int oneTwoThree = 123;\n" 17067 "int oneTwo = 12;", 17068 Alignment); 17069 verifyFormat("int oneTwoThree = 123;\n" 17070 "int oneTwo = 12;\n" 17071 "method();\n", 17072 Alignment); 17073 verifyFormat("int oneTwoThree = 123; // comment\n" 17074 "int oneTwo = 12; // comment", 17075 Alignment); 17076 verifyFormat("int f() = default;\n" 17077 "int &operator() = default;\n" 17078 "int &operator=() {", 17079 Alignment); 17080 verifyFormat("int f() = delete;\n" 17081 "int &operator() = delete;\n" 17082 "int &operator=() {", 17083 Alignment); 17084 verifyFormat("int f() = default; // comment\n" 17085 "int &operator() = default; // comment\n" 17086 "int &operator=() {", 17087 Alignment); 17088 verifyFormat("int f() = default;\n" 17089 "int &operator() = default;\n" 17090 "int &operator==() {", 17091 Alignment); 17092 verifyFormat("int f() = default;\n" 17093 "int &operator() = default;\n" 17094 "int &operator<=() {", 17095 Alignment); 17096 verifyFormat("int f() = default;\n" 17097 "int &operator() = default;\n" 17098 "int &operator!=() {", 17099 Alignment); 17100 verifyFormat("int f() = default;\n" 17101 "int &operator() = default;\n" 17102 "int &operator=();", 17103 Alignment); 17104 verifyFormat("int f() = delete;\n" 17105 "int &operator() = delete;\n" 17106 "int &operator=();", 17107 Alignment); 17108 verifyFormat("/* long long padding */ int f() = default;\n" 17109 "int &operator() = default;\n" 17110 "int &operator/**/ =();", 17111 Alignment); 17112 // https://llvm.org/PR33697 17113 FormatStyle AlignmentWithPenalty = getLLVMStyle(); 17114 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true; 17115 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000; 17116 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n" 17117 " void f() = delete;\n" 17118 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n" 17119 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n" 17120 "};\n", 17121 AlignmentWithPenalty); 17122 17123 // Bug 25167 17124 /* Uncomment when fixed 17125 verifyFormat("#if A\n" 17126 "#else\n" 17127 "int aaaaaaaa = 12;\n" 17128 "#endif\n" 17129 "#if B\n" 17130 "#else\n" 17131 "int a = 12;\n" 17132 "#endif\n", 17133 Alignment); 17134 verifyFormat("enum foo {\n" 17135 "#if A\n" 17136 "#else\n" 17137 " aaaaaaaa = 12;\n" 17138 "#endif\n" 17139 "#if B\n" 17140 "#else\n" 17141 " a = 12;\n" 17142 "#endif\n" 17143 "};\n", 17144 Alignment); 17145 */ 17146 17147 EXPECT_EQ("int a = 5;\n" 17148 "\n" 17149 "int oneTwoThree = 123;", 17150 format("int a = 5;\n" 17151 "\n" 17152 "int oneTwoThree= 123;", 17153 Alignment)); 17154 EXPECT_EQ("int a = 5;\n" 17155 "int one = 1;\n" 17156 "\n" 17157 "int oneTwoThree = 123;", 17158 format("int a = 5;\n" 17159 "int one = 1;\n" 17160 "\n" 17161 "int oneTwoThree = 123;", 17162 Alignment)); 17163 EXPECT_EQ("int a = 5;\n" 17164 "int one = 1;\n" 17165 "\n" 17166 "int oneTwoThree = 123;\n" 17167 "int oneTwo = 12;", 17168 format("int a = 5;\n" 17169 "int one = 1;\n" 17170 "\n" 17171 "int oneTwoThree = 123;\n" 17172 "int oneTwo = 12;", 17173 Alignment)); 17174 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 17175 verifyFormat("#define A \\\n" 17176 " int aaaa = 12; \\\n" 17177 " int b = 23; \\\n" 17178 " int ccc = 234; \\\n" 17179 " int dddddddddd = 2345;", 17180 Alignment); 17181 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 17182 verifyFormat("#define A \\\n" 17183 " int aaaa = 12; \\\n" 17184 " int b = 23; \\\n" 17185 " int ccc = 234; \\\n" 17186 " int dddddddddd = 2345;", 17187 Alignment); 17188 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 17189 verifyFormat("#define A " 17190 " \\\n" 17191 " int aaaa = 12; " 17192 " \\\n" 17193 " int b = 23; " 17194 " \\\n" 17195 " int ccc = 234; " 17196 " \\\n" 17197 " int dddddddddd = 2345;", 17198 Alignment); 17199 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 17200 "k = 4, int l = 5,\n" 17201 " int m = 6) {\n" 17202 " int j = 10;\n" 17203 " otherThing = 1;\n" 17204 "}", 17205 Alignment); 17206 verifyFormat("void SomeFunction(int parameter = 0) {\n" 17207 " int i = 1;\n" 17208 " int j = 2;\n" 17209 " int big = 10000;\n" 17210 "}", 17211 Alignment); 17212 verifyFormat("class C {\n" 17213 "public:\n" 17214 " int i = 1;\n" 17215 " virtual void f() = 0;\n" 17216 "};", 17217 Alignment); 17218 verifyFormat("int i = 1;\n" 17219 "if (SomeType t = getSomething()) {\n" 17220 "}\n" 17221 "int j = 2;\n" 17222 "int big = 10000;", 17223 Alignment); 17224 verifyFormat("int j = 7;\n" 17225 "for (int k = 0; k < N; ++k) {\n" 17226 "}\n" 17227 "int j = 2;\n" 17228 "int big = 10000;\n" 17229 "}", 17230 Alignment); 17231 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 17232 verifyFormat("int i = 1;\n" 17233 "LooooooooooongType loooooooooooooooooooooongVariable\n" 17234 " = someLooooooooooooooooongFunction();\n" 17235 "int j = 2;", 17236 Alignment); 17237 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 17238 verifyFormat("int i = 1;\n" 17239 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 17240 " someLooooooooooooooooongFunction();\n" 17241 "int j = 2;", 17242 Alignment); 17243 17244 verifyFormat("auto lambda = []() {\n" 17245 " auto i = 0;\n" 17246 " return 0;\n" 17247 "};\n" 17248 "int i = 0;\n" 17249 "auto v = type{\n" 17250 " i = 1, //\n" 17251 " (i = 2), //\n" 17252 " i = 3 //\n" 17253 "};", 17254 Alignment); 17255 17256 verifyFormat( 17257 "int i = 1;\n" 17258 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 17259 " loooooooooooooooooooooongParameterB);\n" 17260 "int j = 2;", 17261 Alignment); 17262 17263 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 17264 " typename B = very_long_type_name_1,\n" 17265 " typename T_2 = very_long_type_name_2>\n" 17266 "auto foo() {}\n", 17267 Alignment); 17268 verifyFormat("int a, b = 1;\n" 17269 "int c = 2;\n" 17270 "int dd = 3;\n", 17271 Alignment); 17272 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17273 "float b[1][] = {{3.f}};\n", 17274 Alignment); 17275 verifyFormat("for (int i = 0; i < 1; i++)\n" 17276 " int x = 1;\n", 17277 Alignment); 17278 verifyFormat("for (i = 0; i < 1; i++)\n" 17279 " x = 1;\n" 17280 "y = 1;\n", 17281 Alignment); 17282 17283 EXPECT_EQ(Alignment.ReflowComments, true); 17284 Alignment.ColumnLimit = 50; 17285 EXPECT_EQ("int x = 0;\n" 17286 "int yy = 1; /// specificlennospace\n" 17287 "int zzz = 2;\n", 17288 format("int x = 0;\n" 17289 "int yy = 1; ///specificlennospace\n" 17290 "int zzz = 2;\n", 17291 Alignment)); 17292 17293 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 17294 "auto b = [] {\n" 17295 " f();\n" 17296 " return;\n" 17297 "};", 17298 Alignment); 17299 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 17300 "auto b = g([] {\n" 17301 " f();\n" 17302 " return;\n" 17303 "});", 17304 Alignment); 17305 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 17306 "auto b = g(param, [] {\n" 17307 " f();\n" 17308 " return;\n" 17309 "});", 17310 Alignment); 17311 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 17312 "auto b = [] {\n" 17313 " if (condition) {\n" 17314 " return;\n" 17315 " }\n" 17316 "};", 17317 Alignment); 17318 17319 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 17320 " ccc ? aaaaa : bbbbb,\n" 17321 " dddddddddddddddddddddddddd);", 17322 Alignment); 17323 // FIXME: https://llvm.org/PR53497 17324 // verifyFormat("auto aaaaaaaaaaaa = f();\n" 17325 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 17326 // " ccc ? aaaaa : bbbbb,\n" 17327 // " dddddddddddddddddddddddddd);", 17328 // Alignment); 17329 17330 // Confirm proper handling of AlignConsecutiveAssignments with 17331 // BinPackArguments. 17332 // See https://llvm.org/PR55360 17333 Alignment = getLLVMStyleWithColumns(50); 17334 Alignment.AlignConsecutiveAssignments.Enabled = true; 17335 Alignment.BinPackArguments = false; 17336 verifyFormat("int a_long_name = 1;\n" 17337 "auto b = B({a_long_name, a_long_name},\n" 17338 " {a_longer_name_for_wrap,\n" 17339 " a_longer_name_for_wrap});", 17340 Alignment); 17341 verifyFormat("int a_long_name = 1;\n" 17342 "auto b = B{{a_long_name, a_long_name},\n" 17343 " {a_longer_name_for_wrap,\n" 17344 " a_longer_name_for_wrap}};", 17345 Alignment); 17346 } 17347 17348 TEST_F(FormatTest, AlignConsecutiveBitFields) { 17349 FormatStyle Alignment = getLLVMStyle(); 17350 Alignment.AlignConsecutiveBitFields.Enabled = true; 17351 verifyFormat("int const a : 5;\n" 17352 "int oneTwoThree : 23;", 17353 Alignment); 17354 17355 // Initializers are allowed starting with c++2a 17356 verifyFormat("int const a : 5 = 1;\n" 17357 "int oneTwoThree : 23 = 0;", 17358 Alignment); 17359 17360 Alignment.AlignConsecutiveDeclarations.Enabled = true; 17361 verifyFormat("int const a : 5;\n" 17362 "int oneTwoThree : 23;", 17363 Alignment); 17364 17365 verifyFormat("int const a : 5; // comment\n" 17366 "int oneTwoThree : 23; // comment", 17367 Alignment); 17368 17369 verifyFormat("int const a : 5 = 1;\n" 17370 "int oneTwoThree : 23 = 0;", 17371 Alignment); 17372 17373 Alignment.AlignConsecutiveAssignments.Enabled = true; 17374 verifyFormat("int const a : 5 = 1;\n" 17375 "int oneTwoThree : 23 = 0;", 17376 Alignment); 17377 verifyFormat("int const a : 5 = {1};\n" 17378 "int oneTwoThree : 23 = 0;", 17379 Alignment); 17380 17381 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 17382 verifyFormat("int const a :5;\n" 17383 "int oneTwoThree:23;", 17384 Alignment); 17385 17386 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 17387 verifyFormat("int const a :5;\n" 17388 "int oneTwoThree :23;", 17389 Alignment); 17390 17391 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 17392 verifyFormat("int const a : 5;\n" 17393 "int oneTwoThree: 23;", 17394 Alignment); 17395 17396 // Known limitations: ':' is only recognized as a bitfield colon when 17397 // followed by a number. 17398 /* 17399 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 17400 "int a : 5;", 17401 Alignment); 17402 */ 17403 } 17404 17405 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 17406 FormatStyle Alignment = getLLVMStyle(); 17407 Alignment.AlignConsecutiveMacros.Enabled = true; 17408 Alignment.PointerAlignment = FormatStyle::PAS_Right; 17409 verifyFormat("float const a = 5;\n" 17410 "int oneTwoThree = 123;", 17411 Alignment); 17412 verifyFormat("int a = 5;\n" 17413 "float const oneTwoThree = 123;", 17414 Alignment); 17415 17416 Alignment.AlignConsecutiveDeclarations.Enabled = true; 17417 verifyFormat("float const a = 5;\n" 17418 "int oneTwoThree = 123;", 17419 Alignment); 17420 verifyFormat("int a = method();\n" 17421 "float const oneTwoThree = 133;", 17422 Alignment); 17423 verifyFormat("int i = 1, j = 10;\n" 17424 "something = 2000;", 17425 Alignment); 17426 verifyFormat("something = 2000;\n" 17427 "int i = 1, j = 10;\n", 17428 Alignment); 17429 verifyFormat("float something = 2000;\n" 17430 "double another = 911;\n" 17431 "int i = 1, j = 10;\n" 17432 "const int *oneMore = 1;\n" 17433 "unsigned i = 2;", 17434 Alignment); 17435 verifyFormat("float a = 5;\n" 17436 "int one = 1;\n" 17437 "method();\n" 17438 "const double oneTwoThree = 123;\n" 17439 "const unsigned int oneTwo = 12;", 17440 Alignment); 17441 verifyFormat("int oneTwoThree{0}; // comment\n" 17442 "unsigned oneTwo; // comment", 17443 Alignment); 17444 verifyFormat("unsigned int *a;\n" 17445 "int *b;\n" 17446 "unsigned int Const *c;\n" 17447 "unsigned int const *d;\n" 17448 "unsigned int Const &e;\n" 17449 "unsigned int const &f;", 17450 Alignment); 17451 verifyFormat("Const unsigned int *c;\n" 17452 "const unsigned int *d;\n" 17453 "Const unsigned int &e;\n" 17454 "const unsigned int &f;\n" 17455 "const unsigned g;\n" 17456 "Const unsigned h;", 17457 Alignment); 17458 EXPECT_EQ("float const a = 5;\n" 17459 "\n" 17460 "int oneTwoThree = 123;", 17461 format("float const a = 5;\n" 17462 "\n" 17463 "int oneTwoThree= 123;", 17464 Alignment)); 17465 EXPECT_EQ("float a = 5;\n" 17466 "int one = 1;\n" 17467 "\n" 17468 "unsigned oneTwoThree = 123;", 17469 format("float a = 5;\n" 17470 "int one = 1;\n" 17471 "\n" 17472 "unsigned oneTwoThree = 123;", 17473 Alignment)); 17474 EXPECT_EQ("float a = 5;\n" 17475 "int one = 1;\n" 17476 "\n" 17477 "unsigned oneTwoThree = 123;\n" 17478 "int oneTwo = 12;", 17479 format("float a = 5;\n" 17480 "int one = 1;\n" 17481 "\n" 17482 "unsigned oneTwoThree = 123;\n" 17483 "int oneTwo = 12;", 17484 Alignment)); 17485 // Function prototype alignment 17486 verifyFormat("int a();\n" 17487 "double b();", 17488 Alignment); 17489 verifyFormat("int a(int x);\n" 17490 "double b();", 17491 Alignment); 17492 unsigned OldColumnLimit = Alignment.ColumnLimit; 17493 // We need to set ColumnLimit to zero, in order to stress nested alignments, 17494 // otherwise the function parameters will be re-flowed onto a single line. 17495 Alignment.ColumnLimit = 0; 17496 EXPECT_EQ("int a(int x,\n" 17497 " float y);\n" 17498 "double b(int x,\n" 17499 " double y);", 17500 format("int a(int x,\n" 17501 " float y);\n" 17502 "double b(int x,\n" 17503 " double y);", 17504 Alignment)); 17505 // This ensures that function parameters of function declarations are 17506 // correctly indented when their owning functions are indented. 17507 // The failure case here is for 'double y' to not be indented enough. 17508 EXPECT_EQ("double a(int x);\n" 17509 "int b(int y,\n" 17510 " double z);", 17511 format("double a(int x);\n" 17512 "int b(int y,\n" 17513 " double z);", 17514 Alignment)); 17515 // Set ColumnLimit low so that we induce wrapping immediately after 17516 // the function name and opening paren. 17517 Alignment.ColumnLimit = 13; 17518 verifyFormat("int function(\n" 17519 " int x,\n" 17520 " bool y);", 17521 Alignment); 17522 Alignment.ColumnLimit = OldColumnLimit; 17523 // Ensure function pointers don't screw up recursive alignment 17524 verifyFormat("int a(int x, void (*fp)(int y));\n" 17525 "double b();", 17526 Alignment); 17527 Alignment.AlignConsecutiveAssignments.Enabled = true; 17528 // Ensure recursive alignment is broken by function braces, so that the 17529 // "a = 1" does not align with subsequent assignments inside the function 17530 // body. 17531 verifyFormat("int func(int a = 1) {\n" 17532 " int b = 2;\n" 17533 " int cc = 3;\n" 17534 "}", 17535 Alignment); 17536 verifyFormat("float something = 2000;\n" 17537 "double another = 911;\n" 17538 "int i = 1, j = 10;\n" 17539 "const int *oneMore = 1;\n" 17540 "unsigned i = 2;", 17541 Alignment); 17542 verifyFormat("int oneTwoThree = {0}; // comment\n" 17543 "unsigned oneTwo = 0; // comment", 17544 Alignment); 17545 // Make sure that scope is correctly tracked, in the absence of braces 17546 verifyFormat("for (int i = 0; i < n; i++)\n" 17547 " j = i;\n" 17548 "double x = 1;\n", 17549 Alignment); 17550 verifyFormat("if (int i = 0)\n" 17551 " j = i;\n" 17552 "double x = 1;\n", 17553 Alignment); 17554 // Ensure operator[] and operator() are comprehended 17555 verifyFormat("struct test {\n" 17556 " long long int foo();\n" 17557 " int operator[](int a);\n" 17558 " double bar();\n" 17559 "};\n", 17560 Alignment); 17561 verifyFormat("struct test {\n" 17562 " long long int foo();\n" 17563 " int operator()(int a);\n" 17564 " double bar();\n" 17565 "};\n", 17566 Alignment); 17567 // http://llvm.org/PR52914 17568 verifyFormat("char *a[] = {\"a\", // comment\n" 17569 " \"bb\"};\n" 17570 "int bbbbbbb = 0;", 17571 Alignment); 17572 17573 // PAS_Right 17574 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17575 " int const i = 1;\n" 17576 " int *j = 2;\n" 17577 " int big = 10000;\n" 17578 "\n" 17579 " unsigned oneTwoThree = 123;\n" 17580 " int oneTwo = 12;\n" 17581 " method();\n" 17582 " float k = 2;\n" 17583 " int ll = 10000;\n" 17584 "}", 17585 format("void SomeFunction(int parameter= 0) {\n" 17586 " int const i= 1;\n" 17587 " int *j=2;\n" 17588 " int big = 10000;\n" 17589 "\n" 17590 "unsigned oneTwoThree =123;\n" 17591 "int oneTwo = 12;\n" 17592 " method();\n" 17593 "float k= 2;\n" 17594 "int ll=10000;\n" 17595 "}", 17596 Alignment)); 17597 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17598 " int const i = 1;\n" 17599 " int **j = 2, ***k;\n" 17600 " int &k = i;\n" 17601 " int &&l = i + j;\n" 17602 " int big = 10000;\n" 17603 "\n" 17604 " unsigned oneTwoThree = 123;\n" 17605 " int oneTwo = 12;\n" 17606 " method();\n" 17607 " float k = 2;\n" 17608 " int ll = 10000;\n" 17609 "}", 17610 format("void SomeFunction(int parameter= 0) {\n" 17611 " int const i= 1;\n" 17612 " int **j=2,***k;\n" 17613 "int &k=i;\n" 17614 "int &&l=i+j;\n" 17615 " int big = 10000;\n" 17616 "\n" 17617 "unsigned oneTwoThree =123;\n" 17618 "int oneTwo = 12;\n" 17619 " method();\n" 17620 "float k= 2;\n" 17621 "int ll=10000;\n" 17622 "}", 17623 Alignment)); 17624 // variables are aligned at their name, pointers are at the right most 17625 // position 17626 verifyFormat("int *a;\n" 17627 "int **b;\n" 17628 "int ***c;\n" 17629 "int foobar;\n", 17630 Alignment); 17631 17632 // PAS_Left 17633 FormatStyle AlignmentLeft = Alignment; 17634 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 17635 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17636 " int const i = 1;\n" 17637 " int* j = 2;\n" 17638 " int big = 10000;\n" 17639 "\n" 17640 " unsigned oneTwoThree = 123;\n" 17641 " int oneTwo = 12;\n" 17642 " method();\n" 17643 " float k = 2;\n" 17644 " int ll = 10000;\n" 17645 "}", 17646 format("void SomeFunction(int parameter= 0) {\n" 17647 " int const i= 1;\n" 17648 " int *j=2;\n" 17649 " int big = 10000;\n" 17650 "\n" 17651 "unsigned oneTwoThree =123;\n" 17652 "int oneTwo = 12;\n" 17653 " method();\n" 17654 "float k= 2;\n" 17655 "int ll=10000;\n" 17656 "}", 17657 AlignmentLeft)); 17658 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17659 " int const i = 1;\n" 17660 " int** j = 2;\n" 17661 " int& k = i;\n" 17662 " int&& l = i + j;\n" 17663 " int big = 10000;\n" 17664 "\n" 17665 " unsigned oneTwoThree = 123;\n" 17666 " int oneTwo = 12;\n" 17667 " method();\n" 17668 " float k = 2;\n" 17669 " int ll = 10000;\n" 17670 "}", 17671 format("void SomeFunction(int parameter= 0) {\n" 17672 " int const i= 1;\n" 17673 " int **j=2;\n" 17674 "int &k=i;\n" 17675 "int &&l=i+j;\n" 17676 " int big = 10000;\n" 17677 "\n" 17678 "unsigned oneTwoThree =123;\n" 17679 "int oneTwo = 12;\n" 17680 " method();\n" 17681 "float k= 2;\n" 17682 "int ll=10000;\n" 17683 "}", 17684 AlignmentLeft)); 17685 // variables are aligned at their name, pointers are at the left most position 17686 verifyFormat("int* a;\n" 17687 "int** b;\n" 17688 "int*** c;\n" 17689 "int foobar;\n", 17690 AlignmentLeft); 17691 17692 // PAS_Middle 17693 FormatStyle AlignmentMiddle = Alignment; 17694 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 17695 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17696 " int const i = 1;\n" 17697 " int * j = 2;\n" 17698 " int big = 10000;\n" 17699 "\n" 17700 " unsigned oneTwoThree = 123;\n" 17701 " int oneTwo = 12;\n" 17702 " method();\n" 17703 " float k = 2;\n" 17704 " int ll = 10000;\n" 17705 "}", 17706 format("void SomeFunction(int parameter= 0) {\n" 17707 " int const i= 1;\n" 17708 " int *j=2;\n" 17709 " int big = 10000;\n" 17710 "\n" 17711 "unsigned oneTwoThree =123;\n" 17712 "int oneTwo = 12;\n" 17713 " method();\n" 17714 "float k= 2;\n" 17715 "int ll=10000;\n" 17716 "}", 17717 AlignmentMiddle)); 17718 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17719 " int const i = 1;\n" 17720 " int ** j = 2, ***k;\n" 17721 " int & k = i;\n" 17722 " int && l = i + j;\n" 17723 " int big = 10000;\n" 17724 "\n" 17725 " unsigned oneTwoThree = 123;\n" 17726 " int oneTwo = 12;\n" 17727 " method();\n" 17728 " float k = 2;\n" 17729 " int ll = 10000;\n" 17730 "}", 17731 format("void SomeFunction(int parameter= 0) {\n" 17732 " int const i= 1;\n" 17733 " int **j=2,***k;\n" 17734 "int &k=i;\n" 17735 "int &&l=i+j;\n" 17736 " int big = 10000;\n" 17737 "\n" 17738 "unsigned oneTwoThree =123;\n" 17739 "int oneTwo = 12;\n" 17740 " method();\n" 17741 "float k= 2;\n" 17742 "int ll=10000;\n" 17743 "}", 17744 AlignmentMiddle)); 17745 // variables are aligned at their name, pointers are in the middle 17746 verifyFormat("int * a;\n" 17747 "int * b;\n" 17748 "int *** c;\n" 17749 "int foobar;\n", 17750 AlignmentMiddle); 17751 17752 Alignment.AlignConsecutiveAssignments.Enabled = false; 17753 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 17754 verifyFormat("#define A \\\n" 17755 " int aaaa = 12; \\\n" 17756 " float b = 23; \\\n" 17757 " const int ccc = 234; \\\n" 17758 " unsigned dddddddddd = 2345;", 17759 Alignment); 17760 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 17761 verifyFormat("#define A \\\n" 17762 " int aaaa = 12; \\\n" 17763 " float b = 23; \\\n" 17764 " const int ccc = 234; \\\n" 17765 " unsigned dddddddddd = 2345;", 17766 Alignment); 17767 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 17768 Alignment.ColumnLimit = 30; 17769 verifyFormat("#define A \\\n" 17770 " int aaaa = 12; \\\n" 17771 " float b = 23; \\\n" 17772 " const int ccc = 234; \\\n" 17773 " int dddddddddd = 2345;", 17774 Alignment); 17775 Alignment.ColumnLimit = 80; 17776 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 17777 "k = 4, int l = 5,\n" 17778 " int m = 6) {\n" 17779 " const int j = 10;\n" 17780 " otherThing = 1;\n" 17781 "}", 17782 Alignment); 17783 verifyFormat("void SomeFunction(int parameter = 0) {\n" 17784 " int const i = 1;\n" 17785 " int *j = 2;\n" 17786 " int big = 10000;\n" 17787 "}", 17788 Alignment); 17789 verifyFormat("class C {\n" 17790 "public:\n" 17791 " int i = 1;\n" 17792 " virtual void f() = 0;\n" 17793 "};", 17794 Alignment); 17795 verifyFormat("float i = 1;\n" 17796 "if (SomeType t = getSomething()) {\n" 17797 "}\n" 17798 "const unsigned j = 2;\n" 17799 "int big = 10000;", 17800 Alignment); 17801 verifyFormat("float j = 7;\n" 17802 "for (int k = 0; k < N; ++k) {\n" 17803 "}\n" 17804 "unsigned j = 2;\n" 17805 "int big = 10000;\n" 17806 "}", 17807 Alignment); 17808 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 17809 verifyFormat("float i = 1;\n" 17810 "LooooooooooongType loooooooooooooooooooooongVariable\n" 17811 " = someLooooooooooooooooongFunction();\n" 17812 "int j = 2;", 17813 Alignment); 17814 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 17815 verifyFormat("int i = 1;\n" 17816 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 17817 " someLooooooooooooooooongFunction();\n" 17818 "int j = 2;", 17819 Alignment); 17820 17821 Alignment.AlignConsecutiveAssignments.Enabled = true; 17822 verifyFormat("auto lambda = []() {\n" 17823 " auto ii = 0;\n" 17824 " float j = 0;\n" 17825 " return 0;\n" 17826 "};\n" 17827 "int i = 0;\n" 17828 "float i2 = 0;\n" 17829 "auto v = type{\n" 17830 " i = 1, //\n" 17831 " (i = 2), //\n" 17832 " i = 3 //\n" 17833 "};", 17834 Alignment); 17835 Alignment.AlignConsecutiveAssignments.Enabled = false; 17836 17837 verifyFormat( 17838 "int i = 1;\n" 17839 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 17840 " loooooooooooooooooooooongParameterB);\n" 17841 "int j = 2;", 17842 Alignment); 17843 17844 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 17845 // We expect declarations and assignments to align, as long as it doesn't 17846 // exceed the column limit, starting a new alignment sequence whenever it 17847 // happens. 17848 Alignment.AlignConsecutiveAssignments.Enabled = true; 17849 Alignment.ColumnLimit = 30; 17850 verifyFormat("float ii = 1;\n" 17851 "unsigned j = 2;\n" 17852 "int someVerylongVariable = 1;\n" 17853 "AnotherLongType ll = 123456;\n" 17854 "VeryVeryLongType k = 2;\n" 17855 "int myvar = 1;", 17856 Alignment); 17857 Alignment.ColumnLimit = 80; 17858 Alignment.AlignConsecutiveAssignments.Enabled = false; 17859 17860 verifyFormat( 17861 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 17862 " typename LongType, typename B>\n" 17863 "auto foo() {}\n", 17864 Alignment); 17865 verifyFormat("float a, b = 1;\n" 17866 "int c = 2;\n" 17867 "int dd = 3;\n", 17868 Alignment); 17869 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17870 "float b[1][] = {{3.f}};\n", 17871 Alignment); 17872 Alignment.AlignConsecutiveAssignments.Enabled = true; 17873 verifyFormat("float a, b = 1;\n" 17874 "int c = 2;\n" 17875 "int dd = 3;\n", 17876 Alignment); 17877 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17878 "float b[1][] = {{3.f}};\n", 17879 Alignment); 17880 Alignment.AlignConsecutiveAssignments.Enabled = false; 17881 17882 Alignment.ColumnLimit = 30; 17883 Alignment.BinPackParameters = false; 17884 verifyFormat("void foo(float a,\n" 17885 " float b,\n" 17886 " int c,\n" 17887 " uint32_t *d) {\n" 17888 " int *e = 0;\n" 17889 " float f = 0;\n" 17890 " double g = 0;\n" 17891 "}\n" 17892 "void bar(ino_t a,\n" 17893 " int b,\n" 17894 " uint32_t *c,\n" 17895 " bool d) {}\n", 17896 Alignment); 17897 Alignment.BinPackParameters = true; 17898 Alignment.ColumnLimit = 80; 17899 17900 // Bug 33507 17901 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17902 verifyFormat( 17903 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 17904 " static const Version verVs2017;\n" 17905 " return true;\n" 17906 "});\n", 17907 Alignment); 17908 Alignment.PointerAlignment = FormatStyle::PAS_Right; 17909 17910 // See llvm.org/PR35641 17911 Alignment.AlignConsecutiveDeclarations.Enabled = true; 17912 verifyFormat("int func() { //\n" 17913 " int b;\n" 17914 " unsigned c;\n" 17915 "}", 17916 Alignment); 17917 17918 // See PR37175 17919 FormatStyle Style = getMozillaStyle(); 17920 Style.AlignConsecutiveDeclarations.Enabled = true; 17921 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 17922 "foo(int a);", 17923 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 17924 17925 Alignment.PointerAlignment = FormatStyle::PAS_Left; 17926 verifyFormat("unsigned int* a;\n" 17927 "int* b;\n" 17928 "unsigned int Const* c;\n" 17929 "unsigned int const* d;\n" 17930 "unsigned int Const& e;\n" 17931 "unsigned int const& f;", 17932 Alignment); 17933 verifyFormat("Const unsigned int* c;\n" 17934 "const unsigned int* d;\n" 17935 "Const unsigned int& e;\n" 17936 "const unsigned int& f;\n" 17937 "const unsigned g;\n" 17938 "Const unsigned h;", 17939 Alignment); 17940 17941 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17942 verifyFormat("unsigned int * a;\n" 17943 "int * b;\n" 17944 "unsigned int Const * c;\n" 17945 "unsigned int const * d;\n" 17946 "unsigned int Const & e;\n" 17947 "unsigned int const & f;", 17948 Alignment); 17949 verifyFormat("Const unsigned int * c;\n" 17950 "const unsigned int * d;\n" 17951 "Const unsigned int & e;\n" 17952 "const unsigned int & f;\n" 17953 "const unsigned g;\n" 17954 "Const unsigned h;", 17955 Alignment); 17956 17957 // See PR46529 17958 FormatStyle BracedAlign = getLLVMStyle(); 17959 BracedAlign.AlignConsecutiveDeclarations.Enabled = true; 17960 verifyFormat("const auto result{[]() {\n" 17961 " const auto something = 1;\n" 17962 " return 2;\n" 17963 "}};", 17964 BracedAlign); 17965 verifyFormat("int foo{[]() {\n" 17966 " int bar{0};\n" 17967 " return 0;\n" 17968 "}()};", 17969 BracedAlign); 17970 BracedAlign.Cpp11BracedListStyle = false; 17971 verifyFormat("const auto result{ []() {\n" 17972 " const auto something = 1;\n" 17973 " return 2;\n" 17974 "} };", 17975 BracedAlign); 17976 verifyFormat("int foo{ []() {\n" 17977 " int bar{ 0 };\n" 17978 " return 0;\n" 17979 "}() };", 17980 BracedAlign); 17981 } 17982 17983 TEST_F(FormatTest, AlignWithLineBreaks) { 17984 auto Style = getLLVMStyleWithColumns(120); 17985 17986 EXPECT_EQ(Style.AlignConsecutiveAssignments, 17987 FormatStyle::AlignConsecutiveStyle( 17988 {/*Enabled=*/false, /*AcrossEmptyLines=*/false, 17989 /*AcrossComments=*/false, /*AlignCompound=*/false, 17990 /*PadOperators=*/true})); 17991 EXPECT_EQ(Style.AlignConsecutiveDeclarations, 17992 FormatStyle::AlignConsecutiveStyle({})); 17993 verifyFormat("void foo() {\n" 17994 " int myVar = 5;\n" 17995 " double x = 3.14;\n" 17996 " auto str = \"Hello \"\n" 17997 " \"World\";\n" 17998 " auto s = \"Hello \"\n" 17999 " \"Again\";\n" 18000 "}", 18001 Style); 18002 18003 // clang-format off 18004 verifyFormat("void foo() {\n" 18005 " const int capacityBefore = Entries.capacity();\n" 18006 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 18007 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 18008 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 18009 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 18010 "}", 18011 Style); 18012 // clang-format on 18013 18014 Style.AlignConsecutiveAssignments.Enabled = true; 18015 verifyFormat("void foo() {\n" 18016 " int myVar = 5;\n" 18017 " double x = 3.14;\n" 18018 " auto str = \"Hello \"\n" 18019 " \"World\";\n" 18020 " auto s = \"Hello \"\n" 18021 " \"Again\";\n" 18022 "}", 18023 Style); 18024 18025 // clang-format off 18026 verifyFormat("void foo() {\n" 18027 " const int capacityBefore = Entries.capacity();\n" 18028 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 18029 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 18030 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 18031 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 18032 "}", 18033 Style); 18034 // clang-format on 18035 18036 Style.AlignConsecutiveAssignments.Enabled = false; 18037 Style.AlignConsecutiveDeclarations.Enabled = true; 18038 verifyFormat("void foo() {\n" 18039 " int myVar = 5;\n" 18040 " double x = 3.14;\n" 18041 " auto str = \"Hello \"\n" 18042 " \"World\";\n" 18043 " auto s = \"Hello \"\n" 18044 " \"Again\";\n" 18045 "}", 18046 Style); 18047 18048 // clang-format off 18049 verifyFormat("void foo() {\n" 18050 " const int capacityBefore = Entries.capacity();\n" 18051 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 18052 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 18053 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 18054 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 18055 "}", 18056 Style); 18057 // clang-format on 18058 18059 Style.AlignConsecutiveAssignments.Enabled = true; 18060 Style.AlignConsecutiveDeclarations.Enabled = true; 18061 18062 verifyFormat("void foo() {\n" 18063 " int myVar = 5;\n" 18064 " double x = 3.14;\n" 18065 " auto str = \"Hello \"\n" 18066 " \"World\";\n" 18067 " auto s = \"Hello \"\n" 18068 " \"Again\";\n" 18069 "}", 18070 Style); 18071 18072 // clang-format off 18073 verifyFormat("void foo() {\n" 18074 " const int capacityBefore = Entries.capacity();\n" 18075 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 18076 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 18077 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 18078 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 18079 "}", 18080 Style); 18081 // clang-format on 18082 18083 Style = getLLVMStyleWithColumns(20); 18084 Style.AlignConsecutiveAssignments.Enabled = true; 18085 Style.IndentWidth = 4; 18086 18087 verifyFormat("void foo() {\n" 18088 " int i1 = 1;\n" 18089 " int j = 0;\n" 18090 " int k = bar(\n" 18091 " argument1,\n" 18092 " argument2);\n" 18093 "}", 18094 Style); 18095 18096 verifyFormat("unsigned i = 0;\n" 18097 "int a[] = {\n" 18098 " 1234567890,\n" 18099 " -1234567890};", 18100 Style); 18101 18102 Style.ColumnLimit = 120; 18103 18104 // clang-format off 18105 verifyFormat("void SomeFunc() {\n" 18106 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 18107 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 18108 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 18109 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 18110 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 18111 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 18112 "}", 18113 Style); 18114 // clang-format on 18115 18116 Style.BinPackArguments = false; 18117 18118 // clang-format off 18119 verifyFormat("void SomeFunc() {\n" 18120 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 18121 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 18122 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 18123 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 18124 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 18125 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 18126 "}", 18127 Style); 18128 // clang-format on 18129 } 18130 18131 TEST_F(FormatTest, AlignWithInitializerPeriods) { 18132 auto Style = getLLVMStyleWithColumns(60); 18133 18134 verifyFormat("void foo1(void) {\n" 18135 " BYTE p[1] = 1;\n" 18136 " A B = {.one_foooooooooooooooo = 2,\n" 18137 " .two_fooooooooooooo = 3,\n" 18138 " .three_fooooooooooooo = 4};\n" 18139 " BYTE payload = 2;\n" 18140 "}", 18141 Style); 18142 18143 Style.AlignConsecutiveAssignments.Enabled = true; 18144 Style.AlignConsecutiveDeclarations.Enabled = false; 18145 verifyFormat("void foo2(void) {\n" 18146 " BYTE p[1] = 1;\n" 18147 " A B = {.one_foooooooooooooooo = 2,\n" 18148 " .two_fooooooooooooo = 3,\n" 18149 " .three_fooooooooooooo = 4};\n" 18150 " BYTE payload = 2;\n" 18151 "}", 18152 Style); 18153 18154 Style.AlignConsecutiveAssignments.Enabled = false; 18155 Style.AlignConsecutiveDeclarations.Enabled = true; 18156 verifyFormat("void foo3(void) {\n" 18157 " BYTE p[1] = 1;\n" 18158 " A B = {.one_foooooooooooooooo = 2,\n" 18159 " .two_fooooooooooooo = 3,\n" 18160 " .three_fooooooooooooo = 4};\n" 18161 " BYTE payload = 2;\n" 18162 "}", 18163 Style); 18164 18165 Style.AlignConsecutiveAssignments.Enabled = true; 18166 Style.AlignConsecutiveDeclarations.Enabled = true; 18167 verifyFormat("void foo4(void) {\n" 18168 " BYTE p[1] = 1;\n" 18169 " A B = {.one_foooooooooooooooo = 2,\n" 18170 " .two_fooooooooooooo = 3,\n" 18171 " .three_fooooooooooooo = 4};\n" 18172 " BYTE payload = 2;\n" 18173 "}", 18174 Style); 18175 } 18176 18177 TEST_F(FormatTest, LinuxBraceBreaking) { 18178 FormatStyle LinuxBraceStyle = getLLVMStyle(); 18179 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 18180 verifyFormat("namespace a\n" 18181 "{\n" 18182 "class A\n" 18183 "{\n" 18184 " void f()\n" 18185 " {\n" 18186 " if (true) {\n" 18187 " a();\n" 18188 " b();\n" 18189 " } else {\n" 18190 " a();\n" 18191 " }\n" 18192 " }\n" 18193 " void g() { return; }\n" 18194 "};\n" 18195 "struct B {\n" 18196 " int x;\n" 18197 "};\n" 18198 "} // namespace a\n", 18199 LinuxBraceStyle); 18200 verifyFormat("enum X {\n" 18201 " Y = 0,\n" 18202 "}\n", 18203 LinuxBraceStyle); 18204 verifyFormat("struct S {\n" 18205 " int Type;\n" 18206 " union {\n" 18207 " int x;\n" 18208 " double y;\n" 18209 " } Value;\n" 18210 " class C\n" 18211 " {\n" 18212 " MyFavoriteType Value;\n" 18213 " } Class;\n" 18214 "}\n", 18215 LinuxBraceStyle); 18216 } 18217 18218 TEST_F(FormatTest, MozillaBraceBreaking) { 18219 FormatStyle MozillaBraceStyle = getLLVMStyle(); 18220 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 18221 MozillaBraceStyle.FixNamespaceComments = false; 18222 verifyFormat("namespace a {\n" 18223 "class A\n" 18224 "{\n" 18225 " void f()\n" 18226 " {\n" 18227 " if (true) {\n" 18228 " a();\n" 18229 " b();\n" 18230 " }\n" 18231 " }\n" 18232 " void g() { return; }\n" 18233 "};\n" 18234 "enum E\n" 18235 "{\n" 18236 " A,\n" 18237 " // foo\n" 18238 " B,\n" 18239 " C\n" 18240 "};\n" 18241 "struct B\n" 18242 "{\n" 18243 " int x;\n" 18244 "};\n" 18245 "}\n", 18246 MozillaBraceStyle); 18247 verifyFormat("struct S\n" 18248 "{\n" 18249 " int Type;\n" 18250 " union\n" 18251 " {\n" 18252 " int x;\n" 18253 " double y;\n" 18254 " } Value;\n" 18255 " class C\n" 18256 " {\n" 18257 " MyFavoriteType Value;\n" 18258 " } Class;\n" 18259 "}\n", 18260 MozillaBraceStyle); 18261 } 18262 18263 TEST_F(FormatTest, StroustrupBraceBreaking) { 18264 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 18265 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 18266 verifyFormat("namespace a {\n" 18267 "class A {\n" 18268 " void f()\n" 18269 " {\n" 18270 " if (true) {\n" 18271 " a();\n" 18272 " b();\n" 18273 " }\n" 18274 " }\n" 18275 " void g() { return; }\n" 18276 "};\n" 18277 "struct B {\n" 18278 " int x;\n" 18279 "};\n" 18280 "} // namespace a\n", 18281 StroustrupBraceStyle); 18282 18283 verifyFormat("void foo()\n" 18284 "{\n" 18285 " if (a) {\n" 18286 " a();\n" 18287 " }\n" 18288 " else {\n" 18289 " b();\n" 18290 " }\n" 18291 "}\n", 18292 StroustrupBraceStyle); 18293 18294 verifyFormat("#ifdef _DEBUG\n" 18295 "int foo(int i = 0)\n" 18296 "#else\n" 18297 "int foo(int i = 5)\n" 18298 "#endif\n" 18299 "{\n" 18300 " return i;\n" 18301 "}", 18302 StroustrupBraceStyle); 18303 18304 verifyFormat("void foo() {}\n" 18305 "void bar()\n" 18306 "#ifdef _DEBUG\n" 18307 "{\n" 18308 " foo();\n" 18309 "}\n" 18310 "#else\n" 18311 "{\n" 18312 "}\n" 18313 "#endif", 18314 StroustrupBraceStyle); 18315 18316 verifyFormat("void foobar() { int i = 5; }\n" 18317 "#ifdef _DEBUG\n" 18318 "void bar() {}\n" 18319 "#else\n" 18320 "void bar() { foobar(); }\n" 18321 "#endif", 18322 StroustrupBraceStyle); 18323 } 18324 18325 TEST_F(FormatTest, AllmanBraceBreaking) { 18326 FormatStyle AllmanBraceStyle = getLLVMStyle(); 18327 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 18328 18329 EXPECT_EQ("namespace a\n" 18330 "{\n" 18331 "void f();\n" 18332 "void g();\n" 18333 "} // namespace a\n", 18334 format("namespace a\n" 18335 "{\n" 18336 "void f();\n" 18337 "void g();\n" 18338 "}\n", 18339 AllmanBraceStyle)); 18340 18341 verifyFormat("namespace a\n" 18342 "{\n" 18343 "class A\n" 18344 "{\n" 18345 " void f()\n" 18346 " {\n" 18347 " if (true)\n" 18348 " {\n" 18349 " a();\n" 18350 " b();\n" 18351 " }\n" 18352 " }\n" 18353 " void g() { return; }\n" 18354 "};\n" 18355 "struct B\n" 18356 "{\n" 18357 " int x;\n" 18358 "};\n" 18359 "union C\n" 18360 "{\n" 18361 "};\n" 18362 "} // namespace a", 18363 AllmanBraceStyle); 18364 18365 verifyFormat("void f()\n" 18366 "{\n" 18367 " if (true)\n" 18368 " {\n" 18369 " a();\n" 18370 " }\n" 18371 " else if (false)\n" 18372 " {\n" 18373 " b();\n" 18374 " }\n" 18375 " else\n" 18376 " {\n" 18377 " c();\n" 18378 " }\n" 18379 "}\n", 18380 AllmanBraceStyle); 18381 18382 verifyFormat("void f()\n" 18383 "{\n" 18384 " for (int i = 0; i < 10; ++i)\n" 18385 " {\n" 18386 " a();\n" 18387 " }\n" 18388 " while (false)\n" 18389 " {\n" 18390 " b();\n" 18391 " }\n" 18392 " do\n" 18393 " {\n" 18394 " c();\n" 18395 " } while (false)\n" 18396 "}\n", 18397 AllmanBraceStyle); 18398 18399 verifyFormat("void f(int a)\n" 18400 "{\n" 18401 " switch (a)\n" 18402 " {\n" 18403 " case 0:\n" 18404 " break;\n" 18405 " case 1:\n" 18406 " {\n" 18407 " break;\n" 18408 " }\n" 18409 " case 2:\n" 18410 " {\n" 18411 " }\n" 18412 " break;\n" 18413 " default:\n" 18414 " break;\n" 18415 " }\n" 18416 "}\n", 18417 AllmanBraceStyle); 18418 18419 verifyFormat("enum X\n" 18420 "{\n" 18421 " Y = 0,\n" 18422 "}\n", 18423 AllmanBraceStyle); 18424 verifyFormat("enum X\n" 18425 "{\n" 18426 " Y = 0\n" 18427 "}\n", 18428 AllmanBraceStyle); 18429 18430 verifyFormat("@interface BSApplicationController ()\n" 18431 "{\n" 18432 "@private\n" 18433 " id _extraIvar;\n" 18434 "}\n" 18435 "@end\n", 18436 AllmanBraceStyle); 18437 18438 verifyFormat("#ifdef _DEBUG\n" 18439 "int foo(int i = 0)\n" 18440 "#else\n" 18441 "int foo(int i = 5)\n" 18442 "#endif\n" 18443 "{\n" 18444 " return i;\n" 18445 "}", 18446 AllmanBraceStyle); 18447 18448 verifyFormat("void foo() {}\n" 18449 "void bar()\n" 18450 "#ifdef _DEBUG\n" 18451 "{\n" 18452 " foo();\n" 18453 "}\n" 18454 "#else\n" 18455 "{\n" 18456 "}\n" 18457 "#endif", 18458 AllmanBraceStyle); 18459 18460 verifyFormat("void foobar() { int i = 5; }\n" 18461 "#ifdef _DEBUG\n" 18462 "void bar() {}\n" 18463 "#else\n" 18464 "void bar() { foobar(); }\n" 18465 "#endif", 18466 AllmanBraceStyle); 18467 18468 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 18469 FormatStyle::SLS_All); 18470 18471 verifyFormat("[](int i) { return i + 2; };\n" 18472 "[](int i, int j)\n" 18473 "{\n" 18474 " auto x = i + j;\n" 18475 " auto y = i * j;\n" 18476 " return x ^ y;\n" 18477 "};\n" 18478 "void foo()\n" 18479 "{\n" 18480 " auto shortLambda = [](int i) { return i + 2; };\n" 18481 " auto longLambda = [](int i, int j)\n" 18482 " {\n" 18483 " auto x = i + j;\n" 18484 " auto y = i * j;\n" 18485 " return x ^ y;\n" 18486 " };\n" 18487 "}", 18488 AllmanBraceStyle); 18489 18490 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 18491 18492 verifyFormat("[](int i)\n" 18493 "{\n" 18494 " return i + 2;\n" 18495 "};\n" 18496 "[](int i, int j)\n" 18497 "{\n" 18498 " auto x = i + j;\n" 18499 " auto y = i * j;\n" 18500 " return x ^ y;\n" 18501 "};\n" 18502 "void foo()\n" 18503 "{\n" 18504 " auto shortLambda = [](int i)\n" 18505 " {\n" 18506 " return i + 2;\n" 18507 " };\n" 18508 " auto longLambda = [](int i, int j)\n" 18509 " {\n" 18510 " auto x = i + j;\n" 18511 " auto y = i * j;\n" 18512 " return x ^ y;\n" 18513 " };\n" 18514 "}", 18515 AllmanBraceStyle); 18516 18517 // Reset 18518 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 18519 18520 // This shouldn't affect ObjC blocks.. 18521 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 18522 " // ...\n" 18523 " int i;\n" 18524 "}];", 18525 AllmanBraceStyle); 18526 verifyFormat("void (^block)(void) = ^{\n" 18527 " // ...\n" 18528 " int i;\n" 18529 "};", 18530 AllmanBraceStyle); 18531 // .. or dict literals. 18532 verifyFormat("void f()\n" 18533 "{\n" 18534 " // ...\n" 18535 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 18536 "}", 18537 AllmanBraceStyle); 18538 verifyFormat("void f()\n" 18539 "{\n" 18540 " // ...\n" 18541 " [object someMethod:@{a : @\"b\"}];\n" 18542 "}", 18543 AllmanBraceStyle); 18544 verifyFormat("int f()\n" 18545 "{ // comment\n" 18546 " return 42;\n" 18547 "}", 18548 AllmanBraceStyle); 18549 18550 AllmanBraceStyle.ColumnLimit = 19; 18551 verifyFormat("void f() { int i; }", AllmanBraceStyle); 18552 AllmanBraceStyle.ColumnLimit = 18; 18553 verifyFormat("void f()\n" 18554 "{\n" 18555 " int i;\n" 18556 "}", 18557 AllmanBraceStyle); 18558 AllmanBraceStyle.ColumnLimit = 80; 18559 18560 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 18561 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 18562 FormatStyle::SIS_WithoutElse; 18563 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 18564 verifyFormat("void f(bool b)\n" 18565 "{\n" 18566 " if (b)\n" 18567 " {\n" 18568 " return;\n" 18569 " }\n" 18570 "}\n", 18571 BreakBeforeBraceShortIfs); 18572 verifyFormat("void f(bool b)\n" 18573 "{\n" 18574 " if constexpr (b)\n" 18575 " {\n" 18576 " return;\n" 18577 " }\n" 18578 "}\n", 18579 BreakBeforeBraceShortIfs); 18580 verifyFormat("void f(bool b)\n" 18581 "{\n" 18582 " if CONSTEXPR (b)\n" 18583 " {\n" 18584 " return;\n" 18585 " }\n" 18586 "}\n", 18587 BreakBeforeBraceShortIfs); 18588 verifyFormat("void f(bool b)\n" 18589 "{\n" 18590 " if (b) return;\n" 18591 "}\n", 18592 BreakBeforeBraceShortIfs); 18593 verifyFormat("void f(bool b)\n" 18594 "{\n" 18595 " if constexpr (b) return;\n" 18596 "}\n", 18597 BreakBeforeBraceShortIfs); 18598 verifyFormat("void f(bool b)\n" 18599 "{\n" 18600 " if CONSTEXPR (b) return;\n" 18601 "}\n", 18602 BreakBeforeBraceShortIfs); 18603 verifyFormat("void f(bool b)\n" 18604 "{\n" 18605 " while (b)\n" 18606 " {\n" 18607 " return;\n" 18608 " }\n" 18609 "}\n", 18610 BreakBeforeBraceShortIfs); 18611 } 18612 18613 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 18614 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0); 18615 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 18616 18617 // Make a few changes to the style for testing purposes 18618 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 18619 FormatStyle::SFS_Empty; 18620 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 18621 18622 // FIXME: this test case can't decide whether there should be a blank line 18623 // after the ~D() line or not. It adds one if one doesn't exist in the test 18624 // and it removes the line if one exists. 18625 /* 18626 verifyFormat("class A;\n" 18627 "namespace B\n" 18628 " {\n" 18629 "class C;\n" 18630 "// Comment\n" 18631 "class D\n" 18632 " {\n" 18633 "public:\n" 18634 " D();\n" 18635 " ~D() {}\n" 18636 "private:\n" 18637 " enum E\n" 18638 " {\n" 18639 " F\n" 18640 " }\n" 18641 " };\n" 18642 " } // namespace B\n", 18643 WhitesmithsBraceStyle); 18644 */ 18645 18646 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 18647 verifyFormat("namespace a\n" 18648 " {\n" 18649 "class A\n" 18650 " {\n" 18651 " void f()\n" 18652 " {\n" 18653 " if (true)\n" 18654 " {\n" 18655 " a();\n" 18656 " b();\n" 18657 " }\n" 18658 " }\n" 18659 " void g()\n" 18660 " {\n" 18661 " return;\n" 18662 " }\n" 18663 " };\n" 18664 "struct B\n" 18665 " {\n" 18666 " int x;\n" 18667 " };\n" 18668 " } // namespace a", 18669 WhitesmithsBraceStyle); 18670 18671 verifyFormat("namespace a\n" 18672 " {\n" 18673 "namespace b\n" 18674 " {\n" 18675 "class A\n" 18676 " {\n" 18677 " void f()\n" 18678 " {\n" 18679 " if (true)\n" 18680 " {\n" 18681 " a();\n" 18682 " b();\n" 18683 " }\n" 18684 " }\n" 18685 " void g()\n" 18686 " {\n" 18687 " return;\n" 18688 " }\n" 18689 " };\n" 18690 "struct B\n" 18691 " {\n" 18692 " int x;\n" 18693 " };\n" 18694 " } // namespace b\n" 18695 " } // namespace a", 18696 WhitesmithsBraceStyle); 18697 18698 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 18699 verifyFormat("namespace a\n" 18700 " {\n" 18701 "namespace b\n" 18702 " {\n" 18703 " class A\n" 18704 " {\n" 18705 " void f()\n" 18706 " {\n" 18707 " if (true)\n" 18708 " {\n" 18709 " a();\n" 18710 " b();\n" 18711 " }\n" 18712 " }\n" 18713 " void g()\n" 18714 " {\n" 18715 " return;\n" 18716 " }\n" 18717 " };\n" 18718 " struct B\n" 18719 " {\n" 18720 " int x;\n" 18721 " };\n" 18722 " } // namespace b\n" 18723 " } // namespace a", 18724 WhitesmithsBraceStyle); 18725 18726 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 18727 verifyFormat("namespace a\n" 18728 " {\n" 18729 " namespace b\n" 18730 " {\n" 18731 " class A\n" 18732 " {\n" 18733 " void f()\n" 18734 " {\n" 18735 " if (true)\n" 18736 " {\n" 18737 " a();\n" 18738 " b();\n" 18739 " }\n" 18740 " }\n" 18741 " void g()\n" 18742 " {\n" 18743 " return;\n" 18744 " }\n" 18745 " };\n" 18746 " struct B\n" 18747 " {\n" 18748 " int x;\n" 18749 " };\n" 18750 " } // namespace b\n" 18751 " } // namespace a", 18752 WhitesmithsBraceStyle); 18753 18754 verifyFormat("void f()\n" 18755 " {\n" 18756 " if (true)\n" 18757 " {\n" 18758 " a();\n" 18759 " }\n" 18760 " else if (false)\n" 18761 " {\n" 18762 " b();\n" 18763 " }\n" 18764 " else\n" 18765 " {\n" 18766 " c();\n" 18767 " }\n" 18768 " }\n", 18769 WhitesmithsBraceStyle); 18770 18771 verifyFormat("void f()\n" 18772 " {\n" 18773 " for (int i = 0; i < 10; ++i)\n" 18774 " {\n" 18775 " a();\n" 18776 " }\n" 18777 " while (false)\n" 18778 " {\n" 18779 " b();\n" 18780 " }\n" 18781 " do\n" 18782 " {\n" 18783 " c();\n" 18784 " } while (false)\n" 18785 " }\n", 18786 WhitesmithsBraceStyle); 18787 18788 WhitesmithsBraceStyle.IndentCaseLabels = true; 18789 verifyFormat("void switchTest1(int a)\n" 18790 " {\n" 18791 " switch (a)\n" 18792 " {\n" 18793 " case 2:\n" 18794 " {\n" 18795 " }\n" 18796 " break;\n" 18797 " }\n" 18798 " }\n", 18799 WhitesmithsBraceStyle); 18800 18801 verifyFormat("void switchTest2(int a)\n" 18802 " {\n" 18803 " switch (a)\n" 18804 " {\n" 18805 " case 0:\n" 18806 " break;\n" 18807 " case 1:\n" 18808 " {\n" 18809 " break;\n" 18810 " }\n" 18811 " case 2:\n" 18812 " {\n" 18813 " }\n" 18814 " break;\n" 18815 " default:\n" 18816 " break;\n" 18817 " }\n" 18818 " }\n", 18819 WhitesmithsBraceStyle); 18820 18821 verifyFormat("void switchTest3(int a)\n" 18822 " {\n" 18823 " switch (a)\n" 18824 " {\n" 18825 " case 0:\n" 18826 " {\n" 18827 " foo(x);\n" 18828 " }\n" 18829 " break;\n" 18830 " default:\n" 18831 " {\n" 18832 " foo(1);\n" 18833 " }\n" 18834 " break;\n" 18835 " }\n" 18836 " }\n", 18837 WhitesmithsBraceStyle); 18838 18839 WhitesmithsBraceStyle.IndentCaseLabels = false; 18840 18841 verifyFormat("void switchTest4(int a)\n" 18842 " {\n" 18843 " switch (a)\n" 18844 " {\n" 18845 " case 2:\n" 18846 " {\n" 18847 " }\n" 18848 " break;\n" 18849 " }\n" 18850 " }\n", 18851 WhitesmithsBraceStyle); 18852 18853 verifyFormat("void switchTest5(int a)\n" 18854 " {\n" 18855 " switch (a)\n" 18856 " {\n" 18857 " case 0:\n" 18858 " break;\n" 18859 " case 1:\n" 18860 " {\n" 18861 " foo();\n" 18862 " break;\n" 18863 " }\n" 18864 " case 2:\n" 18865 " {\n" 18866 " }\n" 18867 " break;\n" 18868 " default:\n" 18869 " break;\n" 18870 " }\n" 18871 " }\n", 18872 WhitesmithsBraceStyle); 18873 18874 verifyFormat("void switchTest6(int a)\n" 18875 " {\n" 18876 " switch (a)\n" 18877 " {\n" 18878 " case 0:\n" 18879 " {\n" 18880 " foo(x);\n" 18881 " }\n" 18882 " break;\n" 18883 " default:\n" 18884 " {\n" 18885 " foo(1);\n" 18886 " }\n" 18887 " break;\n" 18888 " }\n" 18889 " }\n", 18890 WhitesmithsBraceStyle); 18891 18892 verifyFormat("enum X\n" 18893 " {\n" 18894 " Y = 0, // testing\n" 18895 " }\n", 18896 WhitesmithsBraceStyle); 18897 18898 verifyFormat("enum X\n" 18899 " {\n" 18900 " Y = 0\n" 18901 " }\n", 18902 WhitesmithsBraceStyle); 18903 verifyFormat("enum X\n" 18904 " {\n" 18905 " Y = 0,\n" 18906 " Z = 1\n" 18907 " };\n", 18908 WhitesmithsBraceStyle); 18909 18910 verifyFormat("@interface BSApplicationController ()\n" 18911 " {\n" 18912 "@private\n" 18913 " id _extraIvar;\n" 18914 " }\n" 18915 "@end\n", 18916 WhitesmithsBraceStyle); 18917 18918 verifyFormat("#ifdef _DEBUG\n" 18919 "int foo(int i = 0)\n" 18920 "#else\n" 18921 "int foo(int i = 5)\n" 18922 "#endif\n" 18923 " {\n" 18924 " return i;\n" 18925 " }", 18926 WhitesmithsBraceStyle); 18927 18928 verifyFormat("void foo() {}\n" 18929 "void bar()\n" 18930 "#ifdef _DEBUG\n" 18931 " {\n" 18932 " foo();\n" 18933 " }\n" 18934 "#else\n" 18935 " {\n" 18936 " }\n" 18937 "#endif", 18938 WhitesmithsBraceStyle); 18939 18940 verifyFormat("void foobar()\n" 18941 " {\n" 18942 " int i = 5;\n" 18943 " }\n" 18944 "#ifdef _DEBUG\n" 18945 "void bar()\n" 18946 " {\n" 18947 " }\n" 18948 "#else\n" 18949 "void bar()\n" 18950 " {\n" 18951 " foobar();\n" 18952 " }\n" 18953 "#endif", 18954 WhitesmithsBraceStyle); 18955 18956 // This shouldn't affect ObjC blocks.. 18957 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 18958 " // ...\n" 18959 " int i;\n" 18960 "}];", 18961 WhitesmithsBraceStyle); 18962 verifyFormat("void (^block)(void) = ^{\n" 18963 " // ...\n" 18964 " int i;\n" 18965 "};", 18966 WhitesmithsBraceStyle); 18967 // .. or dict literals. 18968 verifyFormat("void f()\n" 18969 " {\n" 18970 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 18971 " }", 18972 WhitesmithsBraceStyle); 18973 18974 verifyFormat("int f()\n" 18975 " { // comment\n" 18976 " return 42;\n" 18977 " }", 18978 WhitesmithsBraceStyle); 18979 18980 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 18981 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 18982 FormatStyle::SIS_OnlyFirstIf; 18983 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 18984 verifyFormat("void f(bool b)\n" 18985 " {\n" 18986 " if (b)\n" 18987 " {\n" 18988 " return;\n" 18989 " }\n" 18990 " }\n", 18991 BreakBeforeBraceShortIfs); 18992 verifyFormat("void f(bool b)\n" 18993 " {\n" 18994 " if (b) return;\n" 18995 " }\n", 18996 BreakBeforeBraceShortIfs); 18997 verifyFormat("void f(bool b)\n" 18998 " {\n" 18999 " while (b)\n" 19000 " {\n" 19001 " return;\n" 19002 " }\n" 19003 " }\n", 19004 BreakBeforeBraceShortIfs); 19005 } 19006 19007 TEST_F(FormatTest, GNUBraceBreaking) { 19008 FormatStyle GNUBraceStyle = getLLVMStyle(); 19009 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 19010 verifyFormat("namespace a\n" 19011 "{\n" 19012 "class A\n" 19013 "{\n" 19014 " void f()\n" 19015 " {\n" 19016 " int a;\n" 19017 " {\n" 19018 " int b;\n" 19019 " }\n" 19020 " if (true)\n" 19021 " {\n" 19022 " a();\n" 19023 " b();\n" 19024 " }\n" 19025 " }\n" 19026 " void g() { return; }\n" 19027 "}\n" 19028 "} // namespace a", 19029 GNUBraceStyle); 19030 19031 verifyFormat("void f()\n" 19032 "{\n" 19033 " if (true)\n" 19034 " {\n" 19035 " a();\n" 19036 " }\n" 19037 " else if (false)\n" 19038 " {\n" 19039 " b();\n" 19040 " }\n" 19041 " else\n" 19042 " {\n" 19043 " c();\n" 19044 " }\n" 19045 "}\n", 19046 GNUBraceStyle); 19047 19048 verifyFormat("void f()\n" 19049 "{\n" 19050 " for (int i = 0; i < 10; ++i)\n" 19051 " {\n" 19052 " a();\n" 19053 " }\n" 19054 " while (false)\n" 19055 " {\n" 19056 " b();\n" 19057 " }\n" 19058 " do\n" 19059 " {\n" 19060 " c();\n" 19061 " }\n" 19062 " while (false);\n" 19063 "}\n", 19064 GNUBraceStyle); 19065 19066 verifyFormat("void f(int a)\n" 19067 "{\n" 19068 " switch (a)\n" 19069 " {\n" 19070 " case 0:\n" 19071 " break;\n" 19072 " case 1:\n" 19073 " {\n" 19074 " break;\n" 19075 " }\n" 19076 " case 2:\n" 19077 " {\n" 19078 " }\n" 19079 " break;\n" 19080 " default:\n" 19081 " break;\n" 19082 " }\n" 19083 "}\n", 19084 GNUBraceStyle); 19085 19086 verifyFormat("enum X\n" 19087 "{\n" 19088 " Y = 0,\n" 19089 "}\n", 19090 GNUBraceStyle); 19091 19092 verifyFormat("@interface BSApplicationController ()\n" 19093 "{\n" 19094 "@private\n" 19095 " id _extraIvar;\n" 19096 "}\n" 19097 "@end\n", 19098 GNUBraceStyle); 19099 19100 verifyFormat("#ifdef _DEBUG\n" 19101 "int foo(int i = 0)\n" 19102 "#else\n" 19103 "int foo(int i = 5)\n" 19104 "#endif\n" 19105 "{\n" 19106 " return i;\n" 19107 "}", 19108 GNUBraceStyle); 19109 19110 verifyFormat("void foo() {}\n" 19111 "void bar()\n" 19112 "#ifdef _DEBUG\n" 19113 "{\n" 19114 " foo();\n" 19115 "}\n" 19116 "#else\n" 19117 "{\n" 19118 "}\n" 19119 "#endif", 19120 GNUBraceStyle); 19121 19122 verifyFormat("void foobar() { int i = 5; }\n" 19123 "#ifdef _DEBUG\n" 19124 "void bar() {}\n" 19125 "#else\n" 19126 "void bar() { foobar(); }\n" 19127 "#endif", 19128 GNUBraceStyle); 19129 } 19130 19131 TEST_F(FormatTest, WebKitBraceBreaking) { 19132 FormatStyle WebKitBraceStyle = getLLVMStyle(); 19133 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 19134 WebKitBraceStyle.FixNamespaceComments = false; 19135 verifyFormat("namespace a {\n" 19136 "class A {\n" 19137 " void f()\n" 19138 " {\n" 19139 " if (true) {\n" 19140 " a();\n" 19141 " b();\n" 19142 " }\n" 19143 " }\n" 19144 " void g() { return; }\n" 19145 "};\n" 19146 "enum E {\n" 19147 " A,\n" 19148 " // foo\n" 19149 " B,\n" 19150 " C\n" 19151 "};\n" 19152 "struct B {\n" 19153 " int x;\n" 19154 "};\n" 19155 "}\n", 19156 WebKitBraceStyle); 19157 verifyFormat("struct S {\n" 19158 " int Type;\n" 19159 " union {\n" 19160 " int x;\n" 19161 " double y;\n" 19162 " } Value;\n" 19163 " class C {\n" 19164 " MyFavoriteType Value;\n" 19165 " } Class;\n" 19166 "};\n", 19167 WebKitBraceStyle); 19168 } 19169 19170 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 19171 verifyFormat("void f() {\n" 19172 " try {\n" 19173 " } catch (const Exception &e) {\n" 19174 " }\n" 19175 "}\n", 19176 getLLVMStyle()); 19177 } 19178 19179 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 19180 auto Style = getLLVMStyle(); 19181 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 19182 Style.AlignConsecutiveAssignments.Enabled = true; 19183 Style.AlignConsecutiveDeclarations.Enabled = true; 19184 verifyFormat("struct test demo[] = {\n" 19185 " {56, 23, \"hello\"},\n" 19186 " {-1, 93463, \"world\"},\n" 19187 " { 7, 5, \"!!\"}\n" 19188 "};\n", 19189 Style); 19190 19191 verifyFormat("struct test demo[] = {\n" 19192 " {56, 23, \"hello\"}, // first line\n" 19193 " {-1, 93463, \"world\"}, // second line\n" 19194 " { 7, 5, \"!!\"} // third line\n" 19195 "};\n", 19196 Style); 19197 19198 verifyFormat("struct test demo[4] = {\n" 19199 " { 56, 23, 21, \"oh\"}, // first line\n" 19200 " { -1, 93463, 22, \"my\"}, // second line\n" 19201 " { 7, 5, 1, \"goodness\"} // third line\n" 19202 " {234, 5, 1, \"gracious\"} // fourth line\n" 19203 "};\n", 19204 Style); 19205 19206 verifyFormat("struct test demo[3] = {\n" 19207 " {56, 23, \"hello\"},\n" 19208 " {-1, 93463, \"world\"},\n" 19209 " { 7, 5, \"!!\"}\n" 19210 "};\n", 19211 Style); 19212 19213 verifyFormat("struct test demo[3] = {\n" 19214 " {int{56}, 23, \"hello\"},\n" 19215 " {int{-1}, 93463, \"world\"},\n" 19216 " { int{7}, 5, \"!!\"}\n" 19217 "};\n", 19218 Style); 19219 19220 verifyFormat("struct test demo[] = {\n" 19221 " {56, 23, \"hello\"},\n" 19222 " {-1, 93463, \"world\"},\n" 19223 " { 7, 5, \"!!\"},\n" 19224 "};\n", 19225 Style); 19226 19227 verifyFormat("test demo[] = {\n" 19228 " {56, 23, \"hello\"},\n" 19229 " {-1, 93463, \"world\"},\n" 19230 " { 7, 5, \"!!\"},\n" 19231 "};\n", 19232 Style); 19233 19234 verifyFormat("demo = std::array<struct test, 3>{\n" 19235 " test{56, 23, \"hello\"},\n" 19236 " test{-1, 93463, \"world\"},\n" 19237 " test{ 7, 5, \"!!\"},\n" 19238 "};\n", 19239 Style); 19240 19241 verifyFormat("test demo[] = {\n" 19242 " {56, 23, \"hello\"},\n" 19243 "#if X\n" 19244 " {-1, 93463, \"world\"},\n" 19245 "#endif\n" 19246 " { 7, 5, \"!!\"}\n" 19247 "};\n", 19248 Style); 19249 19250 verifyFormat( 19251 "test demo[] = {\n" 19252 " { 7, 23,\n" 19253 " \"hello world i am a very long line that really, in any\"\n" 19254 " \"just world, ought to be split over multiple lines\"},\n" 19255 " {-1, 93463, \"world\"},\n" 19256 " {56, 5, \"!!\"}\n" 19257 "};\n", 19258 Style); 19259 19260 verifyFormat("return GradForUnaryCwise(g, {\n" 19261 " {{\"sign\"}, \"Sign\", " 19262 " {\"x\", \"dy\"}},\n" 19263 " { {\"dx\"}, \"Mul\", {\"dy\"" 19264 ", \"sign\"}},\n" 19265 "});\n", 19266 Style); 19267 19268 Style.ColumnLimit = 0; 19269 EXPECT_EQ( 19270 "test demo[] = {\n" 19271 " {56, 23, \"hello world i am a very long line that really, " 19272 "in any just world, ought to be split over multiple lines\"},\n" 19273 " {-1, 93463, " 19274 " \"world\"},\n" 19275 " { 7, 5, " 19276 " \"!!\"},\n" 19277 "};", 19278 format("test demo[] = {{56, 23, \"hello world i am a very long line " 19279 "that really, in any just world, ought to be split over multiple " 19280 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 19281 Style)); 19282 19283 Style.ColumnLimit = 80; 19284 verifyFormat("test demo[] = {\n" 19285 " {56, 23, /* a comment */ \"hello\"},\n" 19286 " {-1, 93463, \"world\"},\n" 19287 " { 7, 5, \"!!\"}\n" 19288 "};\n", 19289 Style); 19290 19291 verifyFormat("test demo[] = {\n" 19292 " {56, 23, \"hello\"},\n" 19293 " {-1, 93463, \"world\" /* comment here */},\n" 19294 " { 7, 5, \"!!\"}\n" 19295 "};\n", 19296 Style); 19297 19298 verifyFormat("test demo[] = {\n" 19299 " {56, /* a comment */ 23, \"hello\"},\n" 19300 " {-1, 93463, \"world\"},\n" 19301 " { 7, 5, \"!!\"}\n" 19302 "};\n", 19303 Style); 19304 19305 Style.ColumnLimit = 20; 19306 EXPECT_EQ( 19307 "demo = std::array<\n" 19308 " struct test, 3>{\n" 19309 " test{\n" 19310 " 56, 23,\n" 19311 " \"hello \"\n" 19312 " \"world i \"\n" 19313 " \"am a very \"\n" 19314 " \"long line \"\n" 19315 " \"that \"\n" 19316 " \"really, \"\n" 19317 " \"in any \"\n" 19318 " \"just \"\n" 19319 " \"world, \"\n" 19320 " \"ought to \"\n" 19321 " \"be split \"\n" 19322 " \"over \"\n" 19323 " \"multiple \"\n" 19324 " \"lines\"},\n" 19325 " test{-1, 93463,\n" 19326 " \"world\"},\n" 19327 " test{ 7, 5,\n" 19328 " \"!!\" },\n" 19329 "};", 19330 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 19331 "i am a very long line that really, in any just world, ought " 19332 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 19333 "test{7, 5, \"!!\"},};", 19334 Style)); 19335 // This caused a core dump by enabling Alignment in the LLVMStyle globally 19336 Style = getLLVMStyleWithColumns(50); 19337 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 19338 verifyFormat("static A x = {\n" 19339 " {{init1, init2, init3, init4},\n" 19340 " {init1, init2, init3, init4}}\n" 19341 "};", 19342 Style); 19343 // TODO: Fix the indentations below when this option is fully functional. 19344 verifyFormat("int a[][] = {\n" 19345 " {\n" 19346 " {0, 2}, //\n" 19347 " {1, 2} //\n" 19348 " }\n" 19349 "};", 19350 Style); 19351 Style.ColumnLimit = 100; 19352 EXPECT_EQ( 19353 "test demo[] = {\n" 19354 " {56, 23,\n" 19355 " \"hello world i am a very long line that really, in any just world" 19356 ", ought to be split over \"\n" 19357 " \"multiple lines\" },\n" 19358 " {-1, 93463, \"world\"},\n" 19359 " { 7, 5, \"!!\"},\n" 19360 "};", 19361 format("test demo[] = {{56, 23, \"hello world i am a very long line " 19362 "that really, in any just world, ought to be split over multiple " 19363 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 19364 Style)); 19365 19366 Style = getLLVMStyleWithColumns(50); 19367 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 19368 verifyFormat("struct test demo[] = {\n" 19369 " {56, 23, \"hello\"},\n" 19370 " {-1, 93463, \"world\"},\n" 19371 " { 7, 5, \"!!\"}\n" 19372 "};\n" 19373 "static A x = {\n" 19374 " {{init1, init2, init3, init4},\n" 19375 " {init1, init2, init3, init4}}\n" 19376 "};", 19377 Style); 19378 Style.ColumnLimit = 100; 19379 Style.AlignConsecutiveAssignments.AcrossComments = true; 19380 Style.AlignConsecutiveDeclarations.AcrossComments = true; 19381 verifyFormat("struct test demo[] = {\n" 19382 " {56, 23, \"hello\"},\n" 19383 " {-1, 93463, \"world\"},\n" 19384 " { 7, 5, \"!!\"}\n" 19385 "};\n" 19386 "struct test demo[4] = {\n" 19387 " { 56, 23, 21, \"oh\"}, // first line\n" 19388 " { -1, 93463, 22, \"my\"}, // second line\n" 19389 " { 7, 5, 1, \"goodness\"} // third line\n" 19390 " {234, 5, 1, \"gracious\"} // fourth line\n" 19391 "};\n", 19392 Style); 19393 EXPECT_EQ( 19394 "test demo[] = {\n" 19395 " {56,\n" 19396 " \"hello world i am a very long line that really, in any just world" 19397 ", ought to be split over \"\n" 19398 " \"multiple lines\", 23},\n" 19399 " {-1, \"world\", 93463},\n" 19400 " { 7, \"!!\", 5},\n" 19401 "};", 19402 format("test demo[] = {{56, \"hello world i am a very long line " 19403 "that really, in any just world, ought to be split over multiple " 19404 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 19405 Style)); 19406 } 19407 19408 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 19409 auto Style = getLLVMStyle(); 19410 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 19411 /* FIXME: This case gets misformatted. 19412 verifyFormat("auto foo = Items{\n" 19413 " Section{0, bar(), },\n" 19414 " Section{1, boo() }\n" 19415 "};\n", 19416 Style); 19417 */ 19418 verifyFormat("auto foo = Items{\n" 19419 " Section{\n" 19420 " 0, bar(),\n" 19421 " }\n" 19422 "};\n", 19423 Style); 19424 verifyFormat("struct test demo[] = {\n" 19425 " {56, 23, \"hello\"},\n" 19426 " {-1, 93463, \"world\"},\n" 19427 " {7, 5, \"!!\" }\n" 19428 "};\n", 19429 Style); 19430 verifyFormat("struct test demo[] = {\n" 19431 " {56, 23, \"hello\"}, // first line\n" 19432 " {-1, 93463, \"world\"}, // second line\n" 19433 " {7, 5, \"!!\" } // third line\n" 19434 "};\n", 19435 Style); 19436 verifyFormat("struct test demo[4] = {\n" 19437 " {56, 23, 21, \"oh\" }, // first line\n" 19438 " {-1, 93463, 22, \"my\" }, // second line\n" 19439 " {7, 5, 1, \"goodness\"} // third line\n" 19440 " {234, 5, 1, \"gracious\"} // fourth line\n" 19441 "};\n", 19442 Style); 19443 verifyFormat("struct test demo[3] = {\n" 19444 " {56, 23, \"hello\"},\n" 19445 " {-1, 93463, \"world\"},\n" 19446 " {7, 5, \"!!\" }\n" 19447 "};\n", 19448 Style); 19449 19450 verifyFormat("struct test demo[3] = {\n" 19451 " {int{56}, 23, \"hello\"},\n" 19452 " {int{-1}, 93463, \"world\"},\n" 19453 " {int{7}, 5, \"!!\" }\n" 19454 "};\n", 19455 Style); 19456 verifyFormat("struct test demo[] = {\n" 19457 " {56, 23, \"hello\"},\n" 19458 " {-1, 93463, \"world\"},\n" 19459 " {7, 5, \"!!\" },\n" 19460 "};\n", 19461 Style); 19462 verifyFormat("test demo[] = {\n" 19463 " {56, 23, \"hello\"},\n" 19464 " {-1, 93463, \"world\"},\n" 19465 " {7, 5, \"!!\" },\n" 19466 "};\n", 19467 Style); 19468 verifyFormat("demo = std::array<struct test, 3>{\n" 19469 " test{56, 23, \"hello\"},\n" 19470 " test{-1, 93463, \"world\"},\n" 19471 " test{7, 5, \"!!\" },\n" 19472 "};\n", 19473 Style); 19474 verifyFormat("test demo[] = {\n" 19475 " {56, 23, \"hello\"},\n" 19476 "#if X\n" 19477 " {-1, 93463, \"world\"},\n" 19478 "#endif\n" 19479 " {7, 5, \"!!\" }\n" 19480 "};\n", 19481 Style); 19482 verifyFormat( 19483 "test demo[] = {\n" 19484 " {7, 23,\n" 19485 " \"hello world i am a very long line that really, in any\"\n" 19486 " \"just world, ought to be split over multiple lines\"},\n" 19487 " {-1, 93463, \"world\" },\n" 19488 " {56, 5, \"!!\" }\n" 19489 "};\n", 19490 Style); 19491 19492 verifyFormat("return GradForUnaryCwise(g, {\n" 19493 " {{\"sign\"}, \"Sign\", {\"x\", " 19494 "\"dy\"} },\n" 19495 " {{\"dx\"}, \"Mul\", " 19496 "{\"dy\", \"sign\"}},\n" 19497 "});\n", 19498 Style); 19499 19500 Style.ColumnLimit = 0; 19501 EXPECT_EQ( 19502 "test demo[] = {\n" 19503 " {56, 23, \"hello world i am a very long line that really, in any " 19504 "just world, ought to be split over multiple lines\"},\n" 19505 " {-1, 93463, \"world\" " 19506 " },\n" 19507 " {7, 5, \"!!\" " 19508 " },\n" 19509 "};", 19510 format("test demo[] = {{56, 23, \"hello world i am a very long line " 19511 "that really, in any just world, ought to be split over multiple " 19512 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 19513 Style)); 19514 19515 Style.ColumnLimit = 80; 19516 verifyFormat("test demo[] = {\n" 19517 " {56, 23, /* a comment */ \"hello\"},\n" 19518 " {-1, 93463, \"world\" },\n" 19519 " {7, 5, \"!!\" }\n" 19520 "};\n", 19521 Style); 19522 19523 verifyFormat("test demo[] = {\n" 19524 " {56, 23, \"hello\" },\n" 19525 " {-1, 93463, \"world\" /* comment here */},\n" 19526 " {7, 5, \"!!\" }\n" 19527 "};\n", 19528 Style); 19529 19530 verifyFormat("test demo[] = {\n" 19531 " {56, /* a comment */ 23, \"hello\"},\n" 19532 " {-1, 93463, \"world\"},\n" 19533 " {7, 5, \"!!\" }\n" 19534 "};\n", 19535 Style); 19536 19537 Style.ColumnLimit = 20; 19538 EXPECT_EQ( 19539 "demo = std::array<\n" 19540 " struct test, 3>{\n" 19541 " test{\n" 19542 " 56, 23,\n" 19543 " \"hello \"\n" 19544 " \"world i \"\n" 19545 " \"am a very \"\n" 19546 " \"long line \"\n" 19547 " \"that \"\n" 19548 " \"really, \"\n" 19549 " \"in any \"\n" 19550 " \"just \"\n" 19551 " \"world, \"\n" 19552 " \"ought to \"\n" 19553 " \"be split \"\n" 19554 " \"over \"\n" 19555 " \"multiple \"\n" 19556 " \"lines\"},\n" 19557 " test{-1, 93463,\n" 19558 " \"world\"},\n" 19559 " test{7, 5,\n" 19560 " \"!!\" },\n" 19561 "};", 19562 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 19563 "i am a very long line that really, in any just world, ought " 19564 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 19565 "test{7, 5, \"!!\"},};", 19566 Style)); 19567 19568 // This caused a core dump by enabling Alignment in the LLVMStyle globally 19569 Style = getLLVMStyleWithColumns(50); 19570 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 19571 verifyFormat("static A x = {\n" 19572 " {{init1, init2, init3, init4},\n" 19573 " {init1, init2, init3, init4}}\n" 19574 "};", 19575 Style); 19576 Style.ColumnLimit = 100; 19577 EXPECT_EQ( 19578 "test demo[] = {\n" 19579 " {56, 23,\n" 19580 " \"hello world i am a very long line that really, in any just world" 19581 ", ought to be split over \"\n" 19582 " \"multiple lines\" },\n" 19583 " {-1, 93463, \"world\"},\n" 19584 " {7, 5, \"!!\" },\n" 19585 "};", 19586 format("test demo[] = {{56, 23, \"hello world i am a very long line " 19587 "that really, in any just world, ought to be split over multiple " 19588 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 19589 Style)); 19590 } 19591 19592 TEST_F(FormatTest, UnderstandsPragmas) { 19593 verifyFormat("#pragma omp reduction(| : var)"); 19594 verifyFormat("#pragma omp reduction(+ : var)"); 19595 19596 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 19597 "(including parentheses).", 19598 format("#pragma mark Any non-hyphenated or hyphenated string " 19599 "(including parentheses).")); 19600 } 19601 19602 TEST_F(FormatTest, UnderstandPragmaOption) { 19603 verifyFormat("#pragma option -C -A"); 19604 19605 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 19606 } 19607 19608 TEST_F(FormatTest, UnderstandPragmaRegion) { 19609 auto Style = getLLVMStyleWithColumns(0); 19610 verifyFormat("#pragma region TEST(FOO : BAR)", Style); 19611 19612 EXPECT_EQ("#pragma region TEST(FOO : BAR)", 19613 format("#pragma region TEST(FOO : BAR)", Style)); 19614 } 19615 19616 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 19617 FormatStyle Style = getLLVMStyleWithColumns(20); 19618 19619 // See PR41213 19620 EXPECT_EQ("/*\n" 19621 " *\t9012345\n" 19622 " * /8901\n" 19623 " */", 19624 format("/*\n" 19625 " *\t9012345 /8901\n" 19626 " */", 19627 Style)); 19628 EXPECT_EQ("/*\n" 19629 " *345678\n" 19630 " *\t/8901\n" 19631 " */", 19632 format("/*\n" 19633 " *345678\t/8901\n" 19634 " */", 19635 Style)); 19636 19637 verifyFormat("int a; // the\n" 19638 " // comment", 19639 Style); 19640 EXPECT_EQ("int a; /* first line\n" 19641 " * second\n" 19642 " * line third\n" 19643 " * line\n" 19644 " */", 19645 format("int a; /* first line\n" 19646 " * second\n" 19647 " * line third\n" 19648 " * line\n" 19649 " */", 19650 Style)); 19651 EXPECT_EQ("int a; // first line\n" 19652 " // second\n" 19653 " // line third\n" 19654 " // line", 19655 format("int a; // first line\n" 19656 " // second line\n" 19657 " // third line", 19658 Style)); 19659 19660 Style.PenaltyExcessCharacter = 90; 19661 verifyFormat("int a; // the comment", Style); 19662 EXPECT_EQ("int a; // the comment\n" 19663 " // aaa", 19664 format("int a; // the comment aaa", Style)); 19665 EXPECT_EQ("int a; /* first line\n" 19666 " * second line\n" 19667 " * third line\n" 19668 " */", 19669 format("int a; /* first line\n" 19670 " * second line\n" 19671 " * third line\n" 19672 " */", 19673 Style)); 19674 EXPECT_EQ("int a; // first line\n" 19675 " // second line\n" 19676 " // third line", 19677 format("int a; // first line\n" 19678 " // second line\n" 19679 " // third line", 19680 Style)); 19681 // FIXME: Investigate why this is not getting the same layout as the test 19682 // above. 19683 EXPECT_EQ("int a; /* first line\n" 19684 " * second line\n" 19685 " * third line\n" 19686 " */", 19687 format("int a; /* first line second line third line" 19688 "\n*/", 19689 Style)); 19690 19691 EXPECT_EQ("// foo bar baz bazfoo\n" 19692 "// foo bar foo bar\n", 19693 format("// foo bar baz bazfoo\n" 19694 "// foo bar foo bar\n", 19695 Style)); 19696 EXPECT_EQ("// foo bar baz bazfoo\n" 19697 "// foo bar foo bar\n", 19698 format("// foo bar baz bazfoo\n" 19699 "// foo bar foo bar\n", 19700 Style)); 19701 19702 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 19703 // next one. 19704 EXPECT_EQ("// foo bar baz bazfoo\n" 19705 "// bar foo bar\n", 19706 format("// foo bar baz bazfoo bar\n" 19707 "// foo bar\n", 19708 Style)); 19709 19710 EXPECT_EQ("// foo bar baz bazfoo\n" 19711 "// foo bar baz bazfoo\n" 19712 "// bar foo bar\n", 19713 format("// foo bar baz bazfoo\n" 19714 "// foo bar baz bazfoo bar\n" 19715 "// foo bar\n", 19716 Style)); 19717 19718 EXPECT_EQ("// foo bar baz bazfoo\n" 19719 "// foo bar baz bazfoo\n" 19720 "// bar foo bar\n", 19721 format("// foo bar baz bazfoo\n" 19722 "// foo bar baz bazfoo bar\n" 19723 "// foo bar\n", 19724 Style)); 19725 19726 // Make sure we do not keep protruding characters if strict mode reflow is 19727 // cheaper than keeping protruding characters. 19728 Style.ColumnLimit = 21; 19729 EXPECT_EQ( 19730 "// foo foo foo foo\n" 19731 "// foo foo foo foo\n" 19732 "// foo foo foo foo\n", 19733 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 19734 19735 EXPECT_EQ("int a = /* long block\n" 19736 " comment */\n" 19737 " 42;", 19738 format("int a = /* long block comment */ 42;", Style)); 19739 } 19740 19741 TEST_F(FormatTest, BreakPenaltyAfterLParen) { 19742 FormatStyle Style = getLLVMStyle(); 19743 Style.ColumnLimit = 8; 19744 Style.PenaltyExcessCharacter = 15; 19745 verifyFormat("int foo(\n" 19746 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 19747 Style); 19748 Style.PenaltyBreakOpenParenthesis = 200; 19749 EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", 19750 format("int foo(\n" 19751 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 19752 Style)); 19753 } 19754 19755 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { 19756 FormatStyle Style = getLLVMStyle(); 19757 Style.ColumnLimit = 5; 19758 Style.PenaltyExcessCharacter = 150; 19759 verifyFormat("foo((\n" 19760 " int)aaaaaaaaaaaaaaaaaaaaaaaa);", 19761 19762 Style); 19763 Style.PenaltyBreakOpenParenthesis = 100000; 19764 EXPECT_EQ("foo((int)\n" 19765 " aaaaaaaaaaaaaaaaaaaaaaaa);", 19766 format("foo((\n" 19767 "int)aaaaaaaaaaaaaaaaaaaaaaaa);", 19768 Style)); 19769 } 19770 19771 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { 19772 FormatStyle Style = getLLVMStyle(); 19773 Style.ColumnLimit = 4; 19774 Style.PenaltyExcessCharacter = 100; 19775 verifyFormat("for (\n" 19776 " int iiiiiiiiiiiiiiiii =\n" 19777 " 0;\n" 19778 " iiiiiiiiiiiiiiiii <\n" 19779 " 2;\n" 19780 " iiiiiiiiiiiiiiiii++) {\n" 19781 "}", 19782 19783 Style); 19784 Style.PenaltyBreakOpenParenthesis = 1250; 19785 EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n" 19786 " 0;\n" 19787 " iiiiiiiiiiiiiiiii <\n" 19788 " 2;\n" 19789 " iiiiiiiiiiiiiiiii++) {\n" 19790 "}", 19791 format("for (\n" 19792 " int iiiiiiiiiiiiiiiii =\n" 19793 " 0;\n" 19794 " iiiiiiiiiiiiiiiii <\n" 19795 " 2;\n" 19796 " iiiiiiiiiiiiiiiii++) {\n" 19797 "}", 19798 Style)); 19799 } 19800 19801 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 19802 for (size_t i = 1; i < Styles.size(); ++i) \ 19803 EXPECT_EQ(Styles[0], Styles[i]) \ 19804 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 19805 19806 TEST_F(FormatTest, GetsPredefinedStyleByName) { 19807 SmallVector<FormatStyle, 3> Styles; 19808 Styles.resize(3); 19809 19810 Styles[0] = getLLVMStyle(); 19811 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 19812 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 19813 EXPECT_ALL_STYLES_EQUAL(Styles); 19814 19815 Styles[0] = getGoogleStyle(); 19816 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 19817 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 19818 EXPECT_ALL_STYLES_EQUAL(Styles); 19819 19820 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 19821 EXPECT_TRUE( 19822 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 19823 EXPECT_TRUE( 19824 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 19825 EXPECT_ALL_STYLES_EQUAL(Styles); 19826 19827 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 19828 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 19829 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 19830 EXPECT_ALL_STYLES_EQUAL(Styles); 19831 19832 Styles[0] = getMozillaStyle(); 19833 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 19834 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 19835 EXPECT_ALL_STYLES_EQUAL(Styles); 19836 19837 Styles[0] = getWebKitStyle(); 19838 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 19839 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 19840 EXPECT_ALL_STYLES_EQUAL(Styles); 19841 19842 Styles[0] = getGNUStyle(); 19843 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 19844 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 19845 EXPECT_ALL_STYLES_EQUAL(Styles); 19846 19847 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 19848 } 19849 19850 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 19851 SmallVector<FormatStyle, 8> Styles; 19852 Styles.resize(2); 19853 19854 Styles[0] = getGoogleStyle(); 19855 Styles[1] = getLLVMStyle(); 19856 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 19857 EXPECT_ALL_STYLES_EQUAL(Styles); 19858 19859 Styles.resize(5); 19860 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 19861 Styles[1] = getLLVMStyle(); 19862 Styles[1].Language = FormatStyle::LK_JavaScript; 19863 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 19864 19865 Styles[2] = getLLVMStyle(); 19866 Styles[2].Language = FormatStyle::LK_JavaScript; 19867 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 19868 "BasedOnStyle: Google", 19869 &Styles[2]) 19870 .value()); 19871 19872 Styles[3] = getLLVMStyle(); 19873 Styles[3].Language = FormatStyle::LK_JavaScript; 19874 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 19875 "Language: JavaScript", 19876 &Styles[3]) 19877 .value()); 19878 19879 Styles[4] = getLLVMStyle(); 19880 Styles[4].Language = FormatStyle::LK_JavaScript; 19881 EXPECT_EQ(0, parseConfiguration("---\n" 19882 "BasedOnStyle: LLVM\n" 19883 "IndentWidth: 123\n" 19884 "---\n" 19885 "BasedOnStyle: Google\n" 19886 "Language: JavaScript", 19887 &Styles[4]) 19888 .value()); 19889 EXPECT_ALL_STYLES_EQUAL(Styles); 19890 } 19891 19892 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 19893 Style.FIELD = false; \ 19894 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 19895 EXPECT_TRUE(Style.FIELD); \ 19896 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 19897 EXPECT_FALSE(Style.FIELD); 19898 19899 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 19900 19901 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 19902 Style.STRUCT.FIELD = false; \ 19903 EXPECT_EQ(0, \ 19904 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 19905 .value()); \ 19906 EXPECT_TRUE(Style.STRUCT.FIELD); \ 19907 EXPECT_EQ(0, \ 19908 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 19909 .value()); \ 19910 EXPECT_FALSE(Style.STRUCT.FIELD); 19911 19912 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 19913 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 19914 19915 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 19916 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 19917 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 19918 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 19919 19920 TEST_F(FormatTest, ParsesConfigurationBools) { 19921 FormatStyle Style = {}; 19922 Style.Language = FormatStyle::LK_Cpp; 19923 CHECK_PARSE_BOOL(AlignTrailingComments); 19924 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 19925 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 19926 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 19927 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 19928 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 19929 CHECK_PARSE_BOOL(BinPackArguments); 19930 CHECK_PARSE_BOOL(BinPackParameters); 19931 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 19932 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 19933 CHECK_PARSE_BOOL(BreakStringLiterals); 19934 CHECK_PARSE_BOOL(CompactNamespaces); 19935 CHECK_PARSE_BOOL(DeriveLineEnding); 19936 CHECK_PARSE_BOOL(DerivePointerAlignment); 19937 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 19938 CHECK_PARSE_BOOL(DisableFormat); 19939 CHECK_PARSE_BOOL(IndentAccessModifiers); 19940 CHECK_PARSE_BOOL(IndentCaseLabels); 19941 CHECK_PARSE_BOOL(IndentCaseBlocks); 19942 CHECK_PARSE_BOOL(IndentGotoLabels); 19943 CHECK_PARSE_BOOL_FIELD(IndentRequiresClause, "IndentRequires"); 19944 CHECK_PARSE_BOOL(IndentRequiresClause); 19945 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 19946 CHECK_PARSE_BOOL(InsertBraces); 19947 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 19948 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 19949 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 19950 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 19951 CHECK_PARSE_BOOL(ReflowComments); 19952 CHECK_PARSE_BOOL(RemoveBracesLLVM); 19953 CHECK_PARSE_BOOL(SortUsingDeclarations); 19954 CHECK_PARSE_BOOL(SpacesInParentheses); 19955 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 19956 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 19957 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 19958 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 19959 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 19960 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 19961 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 19962 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 19963 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 19964 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 19965 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 19966 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 19967 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 19968 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 19969 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 19970 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 19971 CHECK_PARSE_BOOL(UseCRLF); 19972 19973 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 19974 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 19975 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 19976 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 19977 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 19978 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 19979 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 19980 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 19981 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 19982 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 19983 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 19984 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 19985 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 19986 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 19987 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 19988 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 19989 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 19990 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements); 19991 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros); 19992 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19993 AfterFunctionDeclarationName); 19994 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19995 AfterFunctionDefinitionName); 19996 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros); 19997 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator); 19998 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses); 19999 } 20000 20001 #undef CHECK_PARSE_BOOL 20002 20003 TEST_F(FormatTest, ParsesConfiguration) { 20004 FormatStyle Style = {}; 20005 Style.Language = FormatStyle::LK_Cpp; 20006 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 20007 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 20008 ConstructorInitializerIndentWidth, 1234u); 20009 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 20010 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 20011 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 20012 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 20013 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 20014 PenaltyBreakBeforeFirstCallParameter, 1234u); 20015 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 20016 PenaltyBreakTemplateDeclaration, 1234u); 20017 CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis, 20018 1234u); 20019 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 20020 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 20021 PenaltyReturnTypeOnItsOwnLine, 1234u); 20022 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 20023 SpacesBeforeTrailingComments, 1234u); 20024 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 20025 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 20026 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 20027 20028 Style.QualifierAlignment = FormatStyle::QAS_Right; 20029 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 20030 FormatStyle::QAS_Leave); 20031 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 20032 FormatStyle::QAS_Right); 20033 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 20034 FormatStyle::QAS_Left); 20035 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 20036 FormatStyle::QAS_Custom); 20037 20038 Style.QualifierOrder.clear(); 20039 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 20040 std::vector<std::string>({"const", "volatile", "type"})); 20041 Style.QualifierOrder.clear(); 20042 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 20043 std::vector<std::string>({"const", "type"})); 20044 Style.QualifierOrder.clear(); 20045 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 20046 std::vector<std::string>({"volatile", "type"})); 20047 20048 #define CHECK_ALIGN_CONSECUTIVE(FIELD) \ 20049 do { \ 20050 Style.FIELD.Enabled = true; \ 20051 CHECK_PARSE(#FIELD ": None", FIELD, \ 20052 FormatStyle::AlignConsecutiveStyle( \ 20053 {/*Enabled=*/false, /*AcrossEmptyLines=*/false, \ 20054 /*AcrossComments=*/false, /*AlignCompound=*/false, \ 20055 /*PadOperators=*/true})); \ 20056 CHECK_PARSE(#FIELD ": Consecutive", FIELD, \ 20057 FormatStyle::AlignConsecutiveStyle( \ 20058 {/*Enabled=*/true, /*AcrossEmptyLines=*/false, \ 20059 /*AcrossComments=*/false, /*AlignCompound=*/false, \ 20060 /*PadOperators=*/true})); \ 20061 CHECK_PARSE(#FIELD ": AcrossEmptyLines", FIELD, \ 20062 FormatStyle::AlignConsecutiveStyle( \ 20063 {/*Enabled=*/true, /*AcrossEmptyLines=*/true, \ 20064 /*AcrossComments=*/false, /*AlignCompound=*/false, \ 20065 /*PadOperators=*/true})); \ 20066 CHECK_PARSE(#FIELD ": AcrossEmptyLinesAndComments", FIELD, \ 20067 FormatStyle::AlignConsecutiveStyle( \ 20068 {/*Enabled=*/true, /*AcrossEmptyLines=*/true, \ 20069 /*AcrossComments=*/true, /*AlignCompound=*/false, \ 20070 /*PadOperators=*/true})); \ 20071 /* For backwards compability, false / true should still parse */ \ 20072 CHECK_PARSE(#FIELD ": false", FIELD, \ 20073 FormatStyle::AlignConsecutiveStyle( \ 20074 {/*Enabled=*/false, /*AcrossEmptyLines=*/false, \ 20075 /*AcrossComments=*/false, /*AlignCompound=*/false, \ 20076 /*PadOperators=*/true})); \ 20077 CHECK_PARSE(#FIELD ": true", FIELD, \ 20078 FormatStyle::AlignConsecutiveStyle( \ 20079 {/*Enabled=*/true, /*AcrossEmptyLines=*/false, \ 20080 /*AcrossComments=*/false, /*AlignCompound=*/false, \ 20081 /*PadOperators=*/true})); \ 20082 \ 20083 CHECK_PARSE_NESTED_BOOL(FIELD, Enabled); \ 20084 CHECK_PARSE_NESTED_BOOL(FIELD, AcrossEmptyLines); \ 20085 CHECK_PARSE_NESTED_BOOL(FIELD, AcrossComments); \ 20086 CHECK_PARSE_NESTED_BOOL(FIELD, AlignCompound); \ 20087 CHECK_PARSE_NESTED_BOOL(FIELD, PadOperators); \ 20088 } while (false) 20089 20090 CHECK_ALIGN_CONSECUTIVE(AlignConsecutiveAssignments); 20091 CHECK_ALIGN_CONSECUTIVE(AlignConsecutiveBitFields); 20092 CHECK_ALIGN_CONSECUTIVE(AlignConsecutiveMacros); 20093 CHECK_ALIGN_CONSECUTIVE(AlignConsecutiveDeclarations); 20094 20095 #undef CHECK_ALIGN_CONSECUTIVE 20096 20097 Style.PointerAlignment = FormatStyle::PAS_Middle; 20098 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 20099 FormatStyle::PAS_Left); 20100 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 20101 FormatStyle::PAS_Right); 20102 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 20103 FormatStyle::PAS_Middle); 20104 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 20105 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 20106 FormatStyle::RAS_Pointer); 20107 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 20108 FormatStyle::RAS_Left); 20109 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 20110 FormatStyle::RAS_Right); 20111 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 20112 FormatStyle::RAS_Middle); 20113 // For backward compatibility: 20114 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 20115 FormatStyle::PAS_Left); 20116 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 20117 FormatStyle::PAS_Right); 20118 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 20119 FormatStyle::PAS_Middle); 20120 20121 Style.Standard = FormatStyle::LS_Auto; 20122 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 20123 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 20124 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 20125 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 20126 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 20127 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 20128 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 20129 // Legacy aliases: 20130 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 20131 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 20132 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 20133 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 20134 20135 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 20136 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 20137 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 20138 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 20139 FormatStyle::BOS_None); 20140 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 20141 FormatStyle::BOS_All); 20142 // For backward compatibility: 20143 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 20144 FormatStyle::BOS_None); 20145 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 20146 FormatStyle::BOS_All); 20147 20148 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 20149 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 20150 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 20151 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 20152 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 20153 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 20154 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 20155 // For backward compatibility: 20156 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 20157 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 20158 20159 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 20160 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 20161 FormatStyle::BILS_AfterComma); 20162 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 20163 FormatStyle::BILS_BeforeComma); 20164 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 20165 FormatStyle::BILS_AfterColon); 20166 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 20167 FormatStyle::BILS_BeforeColon); 20168 // For backward compatibility: 20169 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 20170 FormatStyle::BILS_BeforeComma); 20171 20172 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 20173 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 20174 FormatStyle::PCIS_Never); 20175 CHECK_PARSE("PackConstructorInitializers: BinPack", 20176 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 20177 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 20178 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 20179 CHECK_PARSE("PackConstructorInitializers: NextLine", 20180 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 20181 // For backward compatibility: 20182 CHECK_PARSE("BasedOnStyle: Google\n" 20183 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 20184 "AllowAllConstructorInitializersOnNextLine: false", 20185 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 20186 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 20187 CHECK_PARSE("BasedOnStyle: Google\n" 20188 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 20189 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 20190 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 20191 "AllowAllConstructorInitializersOnNextLine: true", 20192 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 20193 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 20194 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 20195 "AllowAllConstructorInitializersOnNextLine: false", 20196 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 20197 20198 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 20199 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 20200 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 20201 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 20202 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 20203 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 20204 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 20205 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 20206 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 20207 20208 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 20209 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 20210 FormatStyle::BAS_Align); 20211 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 20212 FormatStyle::BAS_DontAlign); 20213 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 20214 FormatStyle::BAS_AlwaysBreak); 20215 CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket, 20216 FormatStyle::BAS_BlockIndent); 20217 // For backward compatibility: 20218 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 20219 FormatStyle::BAS_DontAlign); 20220 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 20221 FormatStyle::BAS_Align); 20222 20223 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 20224 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 20225 FormatStyle::ENAS_DontAlign); 20226 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 20227 FormatStyle::ENAS_Left); 20228 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 20229 FormatStyle::ENAS_Right); 20230 // For backward compatibility: 20231 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 20232 FormatStyle::ENAS_Left); 20233 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 20234 FormatStyle::ENAS_Right); 20235 20236 Style.AlignOperands = FormatStyle::OAS_Align; 20237 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 20238 FormatStyle::OAS_DontAlign); 20239 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 20240 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 20241 FormatStyle::OAS_AlignAfterOperator); 20242 // For backward compatibility: 20243 CHECK_PARSE("AlignOperands: false", AlignOperands, 20244 FormatStyle::OAS_DontAlign); 20245 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 20246 20247 Style.UseTab = FormatStyle::UT_ForIndentation; 20248 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 20249 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 20250 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 20251 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 20252 FormatStyle::UT_ForContinuationAndIndentation); 20253 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 20254 FormatStyle::UT_AlignWithSpaces); 20255 // For backward compatibility: 20256 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 20257 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 20258 20259 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 20260 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 20261 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 20262 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 20263 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 20264 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 20265 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 20266 // For backward compatibility: 20267 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 20268 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 20269 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 20270 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 20271 20272 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 20273 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 20274 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 20275 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 20276 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 20277 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 20278 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 20279 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 20280 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 20281 // For backward compatibility: 20282 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 20283 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 20284 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 20285 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 20286 20287 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 20288 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 20289 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 20290 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 20291 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 20292 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 20293 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 20294 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 20295 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 20296 20297 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 20298 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 20299 FormatStyle::SBPO_Never); 20300 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 20301 FormatStyle::SBPO_Always); 20302 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 20303 FormatStyle::SBPO_ControlStatements); 20304 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 20305 SpaceBeforeParens, 20306 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 20307 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 20308 FormatStyle::SBPO_NonEmptyParentheses); 20309 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 20310 FormatStyle::SBPO_Custom); 20311 // For backward compatibility: 20312 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 20313 FormatStyle::SBPO_Never); 20314 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 20315 FormatStyle::SBPO_ControlStatements); 20316 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 20317 SpaceBeforeParens, 20318 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 20319 20320 Style.ColumnLimit = 123; 20321 FormatStyle BaseStyle = getLLVMStyle(); 20322 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 20323 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 20324 20325 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 20326 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 20327 FormatStyle::BS_Attach); 20328 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 20329 FormatStyle::BS_Linux); 20330 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 20331 FormatStyle::BS_Mozilla); 20332 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 20333 FormatStyle::BS_Stroustrup); 20334 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 20335 FormatStyle::BS_Allman); 20336 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 20337 FormatStyle::BS_Whitesmiths); 20338 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 20339 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 20340 FormatStyle::BS_WebKit); 20341 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 20342 FormatStyle::BS_Custom); 20343 20344 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 20345 CHECK_PARSE("BraceWrapping:\n" 20346 " AfterControlStatement: MultiLine", 20347 BraceWrapping.AfterControlStatement, 20348 FormatStyle::BWACS_MultiLine); 20349 CHECK_PARSE("BraceWrapping:\n" 20350 " AfterControlStatement: Always", 20351 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 20352 CHECK_PARSE("BraceWrapping:\n" 20353 " AfterControlStatement: Never", 20354 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 20355 // For backward compatibility: 20356 CHECK_PARSE("BraceWrapping:\n" 20357 " AfterControlStatement: true", 20358 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 20359 CHECK_PARSE("BraceWrapping:\n" 20360 " AfterControlStatement: false", 20361 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 20362 20363 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 20364 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 20365 FormatStyle::RTBS_None); 20366 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 20367 FormatStyle::RTBS_All); 20368 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 20369 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 20370 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 20371 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 20372 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 20373 AlwaysBreakAfterReturnType, 20374 FormatStyle::RTBS_TopLevelDefinitions); 20375 20376 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 20377 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 20378 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 20379 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 20380 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 20381 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 20382 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 20383 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 20384 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 20385 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 20386 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 20387 20388 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 20389 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 20390 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 20391 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 20392 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 20393 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 20394 AlwaysBreakAfterDefinitionReturnType, 20395 FormatStyle::DRTBS_TopLevel); 20396 20397 Style.NamespaceIndentation = FormatStyle::NI_All; 20398 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 20399 FormatStyle::NI_None); 20400 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 20401 FormatStyle::NI_Inner); 20402 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 20403 FormatStyle::NI_All); 20404 20405 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 20406 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 20407 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 20408 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 20409 AllowShortIfStatementsOnASingleLine, 20410 FormatStyle::SIS_WithoutElse); 20411 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 20412 AllowShortIfStatementsOnASingleLine, 20413 FormatStyle::SIS_OnlyFirstIf); 20414 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 20415 AllowShortIfStatementsOnASingleLine, 20416 FormatStyle::SIS_AllIfsAndElse); 20417 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 20418 AllowShortIfStatementsOnASingleLine, 20419 FormatStyle::SIS_OnlyFirstIf); 20420 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 20421 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 20422 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 20423 AllowShortIfStatementsOnASingleLine, 20424 FormatStyle::SIS_WithoutElse); 20425 20426 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 20427 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 20428 FormatStyle::IEBS_AfterExternBlock); 20429 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 20430 FormatStyle::IEBS_Indent); 20431 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 20432 FormatStyle::IEBS_NoIndent); 20433 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 20434 FormatStyle::IEBS_Indent); 20435 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 20436 FormatStyle::IEBS_NoIndent); 20437 20438 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 20439 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 20440 FormatStyle::BFCS_Both); 20441 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 20442 FormatStyle::BFCS_None); 20443 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 20444 FormatStyle::BFCS_Before); 20445 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 20446 FormatStyle::BFCS_After); 20447 20448 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 20449 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 20450 FormatStyle::SJSIO_After); 20451 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 20452 FormatStyle::SJSIO_Before); 20453 20454 // FIXME: This is required because parsing a configuration simply overwrites 20455 // the first N elements of the list instead of resetting it. 20456 Style.ForEachMacros.clear(); 20457 std::vector<std::string> BoostForeach; 20458 BoostForeach.push_back("BOOST_FOREACH"); 20459 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 20460 std::vector<std::string> BoostAndQForeach; 20461 BoostAndQForeach.push_back("BOOST_FOREACH"); 20462 BoostAndQForeach.push_back("Q_FOREACH"); 20463 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 20464 BoostAndQForeach); 20465 20466 Style.IfMacros.clear(); 20467 std::vector<std::string> CustomIfs; 20468 CustomIfs.push_back("MYIF"); 20469 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 20470 20471 Style.AttributeMacros.clear(); 20472 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 20473 std::vector<std::string>{"__capability"}); 20474 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 20475 std::vector<std::string>({"attr1", "attr2"})); 20476 20477 Style.StatementAttributeLikeMacros.clear(); 20478 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 20479 StatementAttributeLikeMacros, 20480 std::vector<std::string>({"emit", "Q_EMIT"})); 20481 20482 Style.StatementMacros.clear(); 20483 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 20484 std::vector<std::string>{"QUNUSED"}); 20485 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 20486 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 20487 20488 Style.NamespaceMacros.clear(); 20489 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 20490 std::vector<std::string>{"TESTSUITE"}); 20491 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 20492 std::vector<std::string>({"TESTSUITE", "SUITE"})); 20493 20494 Style.WhitespaceSensitiveMacros.clear(); 20495 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 20496 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 20497 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 20498 WhitespaceSensitiveMacros, 20499 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 20500 Style.WhitespaceSensitiveMacros.clear(); 20501 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 20502 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 20503 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 20504 WhitespaceSensitiveMacros, 20505 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 20506 20507 Style.IncludeStyle.IncludeCategories.clear(); 20508 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 20509 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 20510 CHECK_PARSE("IncludeCategories:\n" 20511 " - Regex: abc/.*\n" 20512 " Priority: 2\n" 20513 " - Regex: .*\n" 20514 " Priority: 1\n" 20515 " CaseSensitive: true\n", 20516 IncludeStyle.IncludeCategories, ExpectedCategories); 20517 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 20518 "abc$"); 20519 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 20520 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 20521 20522 Style.SortIncludes = FormatStyle::SI_Never; 20523 CHECK_PARSE("SortIncludes: true", SortIncludes, 20524 FormatStyle::SI_CaseSensitive); 20525 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 20526 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 20527 FormatStyle::SI_CaseInsensitive); 20528 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 20529 FormatStyle::SI_CaseSensitive); 20530 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 20531 20532 Style.RawStringFormats.clear(); 20533 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 20534 { 20535 FormatStyle::LK_TextProto, 20536 {"pb", "proto"}, 20537 {"PARSE_TEXT_PROTO"}, 20538 /*CanonicalDelimiter=*/"", 20539 "llvm", 20540 }, 20541 { 20542 FormatStyle::LK_Cpp, 20543 {"cc", "cpp"}, 20544 {"C_CODEBLOCK", "CPPEVAL"}, 20545 /*CanonicalDelimiter=*/"cc", 20546 /*BasedOnStyle=*/"", 20547 }, 20548 }; 20549 20550 CHECK_PARSE("RawStringFormats:\n" 20551 " - Language: TextProto\n" 20552 " Delimiters:\n" 20553 " - 'pb'\n" 20554 " - 'proto'\n" 20555 " EnclosingFunctions:\n" 20556 " - 'PARSE_TEXT_PROTO'\n" 20557 " BasedOnStyle: llvm\n" 20558 " - Language: Cpp\n" 20559 " Delimiters:\n" 20560 " - 'cc'\n" 20561 " - 'cpp'\n" 20562 " EnclosingFunctions:\n" 20563 " - 'C_CODEBLOCK'\n" 20564 " - 'CPPEVAL'\n" 20565 " CanonicalDelimiter: 'cc'", 20566 RawStringFormats, ExpectedRawStringFormats); 20567 20568 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 20569 " Minimum: 0\n" 20570 " Maximum: 0", 20571 SpacesInLineCommentPrefix.Minimum, 0u); 20572 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 20573 Style.SpacesInLineCommentPrefix.Minimum = 1; 20574 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 20575 " Minimum: 2", 20576 SpacesInLineCommentPrefix.Minimum, 0u); 20577 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 20578 " Maximum: -1", 20579 SpacesInLineCommentPrefix.Maximum, -1u); 20580 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 20581 " Minimum: 2", 20582 SpacesInLineCommentPrefix.Minimum, 2u); 20583 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 20584 " Maximum: 1", 20585 SpacesInLineCommentPrefix.Maximum, 1u); 20586 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 20587 20588 Style.SpacesInAngles = FormatStyle::SIAS_Always; 20589 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 20590 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 20591 FormatStyle::SIAS_Always); 20592 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 20593 // For backward compatibility: 20594 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 20595 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 20596 20597 CHECK_PARSE("RequiresClausePosition: WithPreceding", RequiresClausePosition, 20598 FormatStyle::RCPS_WithPreceding); 20599 CHECK_PARSE("RequiresClausePosition: WithFollowing", RequiresClausePosition, 20600 FormatStyle::RCPS_WithFollowing); 20601 CHECK_PARSE("RequiresClausePosition: SingleLine", RequiresClausePosition, 20602 FormatStyle::RCPS_SingleLine); 20603 CHECK_PARSE("RequiresClausePosition: OwnLine", RequiresClausePosition, 20604 FormatStyle::RCPS_OwnLine); 20605 20606 CHECK_PARSE("BreakBeforeConceptDeclarations: Never", 20607 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Never); 20608 CHECK_PARSE("BreakBeforeConceptDeclarations: Always", 20609 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always); 20610 CHECK_PARSE("BreakBeforeConceptDeclarations: Allowed", 20611 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed); 20612 // For backward compatibility: 20613 CHECK_PARSE("BreakBeforeConceptDeclarations: true", 20614 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always); 20615 CHECK_PARSE("BreakBeforeConceptDeclarations: false", 20616 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed); 20617 } 20618 20619 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 20620 FormatStyle Style = {}; 20621 Style.Language = FormatStyle::LK_Cpp; 20622 CHECK_PARSE("Language: Cpp\n" 20623 "IndentWidth: 12", 20624 IndentWidth, 12u); 20625 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 20626 "IndentWidth: 34", 20627 &Style), 20628 ParseError::Unsuitable); 20629 FormatStyle BinPackedTCS = {}; 20630 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 20631 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 20632 "InsertTrailingCommas: Wrapped", 20633 &BinPackedTCS), 20634 ParseError::BinPackTrailingCommaConflict); 20635 EXPECT_EQ(12u, Style.IndentWidth); 20636 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 20637 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 20638 20639 Style.Language = FormatStyle::LK_JavaScript; 20640 CHECK_PARSE("Language: JavaScript\n" 20641 "IndentWidth: 12", 20642 IndentWidth, 12u); 20643 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 20644 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 20645 "IndentWidth: 34", 20646 &Style), 20647 ParseError::Unsuitable); 20648 EXPECT_EQ(23u, Style.IndentWidth); 20649 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 20650 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 20651 20652 CHECK_PARSE("BasedOnStyle: LLVM\n" 20653 "IndentWidth: 67", 20654 IndentWidth, 67u); 20655 20656 CHECK_PARSE("---\n" 20657 "Language: JavaScript\n" 20658 "IndentWidth: 12\n" 20659 "---\n" 20660 "Language: Cpp\n" 20661 "IndentWidth: 34\n" 20662 "...\n", 20663 IndentWidth, 12u); 20664 20665 Style.Language = FormatStyle::LK_Cpp; 20666 CHECK_PARSE("---\n" 20667 "Language: JavaScript\n" 20668 "IndentWidth: 12\n" 20669 "---\n" 20670 "Language: Cpp\n" 20671 "IndentWidth: 34\n" 20672 "...\n", 20673 IndentWidth, 34u); 20674 CHECK_PARSE("---\n" 20675 "IndentWidth: 78\n" 20676 "---\n" 20677 "Language: JavaScript\n" 20678 "IndentWidth: 56\n" 20679 "...\n", 20680 IndentWidth, 78u); 20681 20682 Style.ColumnLimit = 123; 20683 Style.IndentWidth = 234; 20684 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 20685 Style.TabWidth = 345; 20686 EXPECT_FALSE(parseConfiguration("---\n" 20687 "IndentWidth: 456\n" 20688 "BreakBeforeBraces: Allman\n" 20689 "---\n" 20690 "Language: JavaScript\n" 20691 "IndentWidth: 111\n" 20692 "TabWidth: 111\n" 20693 "---\n" 20694 "Language: Cpp\n" 20695 "BreakBeforeBraces: Stroustrup\n" 20696 "TabWidth: 789\n" 20697 "...\n", 20698 &Style)); 20699 EXPECT_EQ(123u, Style.ColumnLimit); 20700 EXPECT_EQ(456u, Style.IndentWidth); 20701 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 20702 EXPECT_EQ(789u, Style.TabWidth); 20703 20704 EXPECT_EQ(parseConfiguration("---\n" 20705 "Language: JavaScript\n" 20706 "IndentWidth: 56\n" 20707 "---\n" 20708 "IndentWidth: 78\n" 20709 "...\n", 20710 &Style), 20711 ParseError::Error); 20712 EXPECT_EQ(parseConfiguration("---\n" 20713 "Language: JavaScript\n" 20714 "IndentWidth: 56\n" 20715 "---\n" 20716 "Language: JavaScript\n" 20717 "IndentWidth: 78\n" 20718 "...\n", 20719 &Style), 20720 ParseError::Error); 20721 20722 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 20723 } 20724 20725 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 20726 FormatStyle Style = {}; 20727 Style.Language = FormatStyle::LK_JavaScript; 20728 Style.BreakBeforeTernaryOperators = true; 20729 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 20730 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 20731 20732 Style.BreakBeforeTernaryOperators = true; 20733 EXPECT_EQ(0, parseConfiguration("---\n" 20734 "BasedOnStyle: Google\n" 20735 "---\n" 20736 "Language: JavaScript\n" 20737 "IndentWidth: 76\n" 20738 "...\n", 20739 &Style) 20740 .value()); 20741 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 20742 EXPECT_EQ(76u, Style.IndentWidth); 20743 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 20744 } 20745 20746 TEST_F(FormatTest, ConfigurationRoundTripTest) { 20747 FormatStyle Style = getLLVMStyle(); 20748 std::string YAML = configurationAsText(Style); 20749 FormatStyle ParsedStyle = {}; 20750 ParsedStyle.Language = FormatStyle::LK_Cpp; 20751 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 20752 EXPECT_EQ(Style, ParsedStyle); 20753 } 20754 20755 TEST_F(FormatTest, WorksFor8bitEncodings) { 20756 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 20757 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 20758 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 20759 "\"\xef\xee\xf0\xf3...\"", 20760 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 20761 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 20762 "\xef\xee\xf0\xf3...\"", 20763 getLLVMStyleWithColumns(12))); 20764 } 20765 20766 TEST_F(FormatTest, HandlesUTF8BOM) { 20767 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 20768 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 20769 format("\xef\xbb\xbf#include <iostream>")); 20770 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 20771 format("\xef\xbb\xbf\n#include <iostream>")); 20772 } 20773 20774 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 20775 #if !defined(_MSC_VER) 20776 20777 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 20778 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 20779 getLLVMStyleWithColumns(35)); 20780 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 20781 getLLVMStyleWithColumns(31)); 20782 verifyFormat("// Однажды в студёную зимнюю пору...", 20783 getLLVMStyleWithColumns(36)); 20784 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 20785 verifyFormat("/* Однажды в студёную зимнюю пору... */", 20786 getLLVMStyleWithColumns(39)); 20787 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 20788 getLLVMStyleWithColumns(35)); 20789 } 20790 20791 TEST_F(FormatTest, SplitsUTF8Strings) { 20792 // Non-printable characters' width is currently considered to be the length in 20793 // bytes in UTF8. The characters can be displayed in very different manner 20794 // (zero-width, single width with a substitution glyph, expanded to their code 20795 // (e.g. "<8d>"), so there's no single correct way to handle them. 20796 EXPECT_EQ("\"aaaaÄ\"\n" 20797 "\"\xc2\x8d\";", 20798 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 20799 EXPECT_EQ("\"aaaaaaaÄ\"\n" 20800 "\"\xc2\x8d\";", 20801 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 20802 EXPECT_EQ("\"Однажды, в \"\n" 20803 "\"студёную \"\n" 20804 "\"зимнюю \"\n" 20805 "\"пору,\"", 20806 format("\"Однажды, в студёную зимнюю пору,\"", 20807 getLLVMStyleWithColumns(13))); 20808 EXPECT_EQ( 20809 "\"一 二 三 \"\n" 20810 "\"四 五六 \"\n" 20811 "\"七 八 九 \"\n" 20812 "\"十\"", 20813 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 20814 EXPECT_EQ("\"一\t\"\n" 20815 "\"二 \t\"\n" 20816 "\"三 四 \"\n" 20817 "\"五\t\"\n" 20818 "\"六 \t\"\n" 20819 "\"七 \"\n" 20820 "\"八九十\tqq\"", 20821 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 20822 getLLVMStyleWithColumns(11))); 20823 20824 // UTF8 character in an escape sequence. 20825 EXPECT_EQ("\"aaaaaa\"\n" 20826 "\"\\\xC2\x8D\"", 20827 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 20828 } 20829 20830 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 20831 EXPECT_EQ("const char *sssss =\n" 20832 " \"一二三四五六七八\\\n" 20833 " 九 十\";", 20834 format("const char *sssss = \"一二三四五六七八\\\n" 20835 " 九 十\";", 20836 getLLVMStyleWithColumns(30))); 20837 } 20838 20839 TEST_F(FormatTest, SplitsUTF8LineComments) { 20840 EXPECT_EQ("// aaaaÄ\xc2\x8d", 20841 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 20842 EXPECT_EQ("// Я из лесу\n" 20843 "// вышел; был\n" 20844 "// сильный\n" 20845 "// мороз.", 20846 format("// Я из лесу вышел; был сильный мороз.", 20847 getLLVMStyleWithColumns(13))); 20848 EXPECT_EQ("// 一二三\n" 20849 "// 四五六七\n" 20850 "// 八 九\n" 20851 "// 十", 20852 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 20853 } 20854 20855 TEST_F(FormatTest, SplitsUTF8BlockComments) { 20856 EXPECT_EQ("/* Гляжу,\n" 20857 " * поднимается\n" 20858 " * медленно в\n" 20859 " * гору\n" 20860 " * Лошадка,\n" 20861 " * везущая\n" 20862 " * хворосту\n" 20863 " * воз. */", 20864 format("/* Гляжу, поднимается медленно в гору\n" 20865 " * Лошадка, везущая хворосту воз. */", 20866 getLLVMStyleWithColumns(13))); 20867 EXPECT_EQ( 20868 "/* 一二三\n" 20869 " * 四五六七\n" 20870 " * 八 九\n" 20871 " * 十 */", 20872 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 20873 EXPECT_EQ("/* \n" 20874 " * \n" 20875 " * - */", 20876 format("/* - */", getLLVMStyleWithColumns(12))); 20877 } 20878 20879 #endif // _MSC_VER 20880 20881 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 20882 FormatStyle Style = getLLVMStyle(); 20883 20884 Style.ConstructorInitializerIndentWidth = 4; 20885 verifyFormat( 20886 "SomeClass::Constructor()\n" 20887 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20888 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20889 Style); 20890 20891 Style.ConstructorInitializerIndentWidth = 2; 20892 verifyFormat( 20893 "SomeClass::Constructor()\n" 20894 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20895 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20896 Style); 20897 20898 Style.ConstructorInitializerIndentWidth = 0; 20899 verifyFormat( 20900 "SomeClass::Constructor()\n" 20901 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20902 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20903 Style); 20904 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 20905 verifyFormat( 20906 "SomeLongTemplateVariableName<\n" 20907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 20908 Style); 20909 verifyFormat("bool smaller = 1 < " 20910 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 20911 " " 20912 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 20913 Style); 20914 20915 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 20916 verifyFormat("SomeClass::Constructor() :\n" 20917 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 20918 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 20919 Style); 20920 } 20921 20922 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 20923 FormatStyle Style = getLLVMStyle(); 20924 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20925 Style.ConstructorInitializerIndentWidth = 4; 20926 verifyFormat("SomeClass::Constructor()\n" 20927 " : a(a)\n" 20928 " , b(b)\n" 20929 " , c(c) {}", 20930 Style); 20931 verifyFormat("SomeClass::Constructor()\n" 20932 " : a(a) {}", 20933 Style); 20934 20935 Style.ColumnLimit = 0; 20936 verifyFormat("SomeClass::Constructor()\n" 20937 " : a(a) {}", 20938 Style); 20939 verifyFormat("SomeClass::Constructor() noexcept\n" 20940 " : a(a) {}", 20941 Style); 20942 verifyFormat("SomeClass::Constructor()\n" 20943 " : a(a)\n" 20944 " , b(b)\n" 20945 " , c(c) {}", 20946 Style); 20947 verifyFormat("SomeClass::Constructor()\n" 20948 " : a(a) {\n" 20949 " foo();\n" 20950 " bar();\n" 20951 "}", 20952 Style); 20953 20954 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 20955 verifyFormat("SomeClass::Constructor()\n" 20956 " : a(a)\n" 20957 " , b(b)\n" 20958 " , c(c) {\n}", 20959 Style); 20960 verifyFormat("SomeClass::Constructor()\n" 20961 " : a(a) {\n}", 20962 Style); 20963 20964 Style.ColumnLimit = 80; 20965 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 20966 Style.ConstructorInitializerIndentWidth = 2; 20967 verifyFormat("SomeClass::Constructor()\n" 20968 " : a(a)\n" 20969 " , b(b)\n" 20970 " , c(c) {}", 20971 Style); 20972 20973 Style.ConstructorInitializerIndentWidth = 0; 20974 verifyFormat("SomeClass::Constructor()\n" 20975 ": a(a)\n" 20976 ", b(b)\n" 20977 ", c(c) {}", 20978 Style); 20979 20980 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 20981 Style.ConstructorInitializerIndentWidth = 4; 20982 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 20983 verifyFormat( 20984 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 20985 Style); 20986 verifyFormat( 20987 "SomeClass::Constructor()\n" 20988 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 20989 Style); 20990 Style.ConstructorInitializerIndentWidth = 4; 20991 Style.ColumnLimit = 60; 20992 verifyFormat("SomeClass::Constructor()\n" 20993 " : aaaaaaaa(aaaaaaaa)\n" 20994 " , aaaaaaaa(aaaaaaaa)\n" 20995 " , aaaaaaaa(aaaaaaaa) {}", 20996 Style); 20997 } 20998 20999 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 21000 FormatStyle Style = getLLVMStyle(); 21001 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 21002 Style.ConstructorInitializerIndentWidth = 4; 21003 verifyFormat("SomeClass::Constructor()\n" 21004 " : a{a}\n" 21005 " , b{b} {}", 21006 Style); 21007 verifyFormat("SomeClass::Constructor()\n" 21008 " : a{a}\n" 21009 "#if CONDITION\n" 21010 " , b{b}\n" 21011 "#endif\n" 21012 "{\n}", 21013 Style); 21014 Style.ConstructorInitializerIndentWidth = 2; 21015 verifyFormat("SomeClass::Constructor()\n" 21016 "#if CONDITION\n" 21017 " : a{a}\n" 21018 "#endif\n" 21019 " , b{b}\n" 21020 " , c{c} {\n}", 21021 Style); 21022 Style.ConstructorInitializerIndentWidth = 0; 21023 verifyFormat("SomeClass::Constructor()\n" 21024 ": a{a}\n" 21025 "#ifdef CONDITION\n" 21026 ", b{b}\n" 21027 "#else\n" 21028 ", c{c}\n" 21029 "#endif\n" 21030 ", d{d} {\n}", 21031 Style); 21032 Style.ConstructorInitializerIndentWidth = 4; 21033 verifyFormat("SomeClass::Constructor()\n" 21034 " : a{a}\n" 21035 "#if WINDOWS\n" 21036 "#if DEBUG\n" 21037 " , b{0}\n" 21038 "#else\n" 21039 " , b{1}\n" 21040 "#endif\n" 21041 "#else\n" 21042 "#if DEBUG\n" 21043 " , b{2}\n" 21044 "#else\n" 21045 " , b{3}\n" 21046 "#endif\n" 21047 "#endif\n" 21048 "{\n}", 21049 Style); 21050 verifyFormat("SomeClass::Constructor()\n" 21051 " : a{a}\n" 21052 "#if WINDOWS\n" 21053 " , b{0}\n" 21054 "#if DEBUG\n" 21055 " , c{0}\n" 21056 "#else\n" 21057 " , c{1}\n" 21058 "#endif\n" 21059 "#else\n" 21060 "#if DEBUG\n" 21061 " , c{2}\n" 21062 "#else\n" 21063 " , c{3}\n" 21064 "#endif\n" 21065 " , b{1}\n" 21066 "#endif\n" 21067 "{\n}", 21068 Style); 21069 } 21070 21071 TEST_F(FormatTest, Destructors) { 21072 verifyFormat("void F(int &i) { i.~int(); }"); 21073 verifyFormat("void F(int &i) { i->~int(); }"); 21074 } 21075 21076 TEST_F(FormatTest, FormatsWithWebKitStyle) { 21077 FormatStyle Style = getWebKitStyle(); 21078 21079 // Don't indent in outer namespaces. 21080 verifyFormat("namespace outer {\n" 21081 "int i;\n" 21082 "namespace inner {\n" 21083 " int i;\n" 21084 "} // namespace inner\n" 21085 "} // namespace outer\n" 21086 "namespace other_outer {\n" 21087 "int i;\n" 21088 "}", 21089 Style); 21090 21091 // Don't indent case labels. 21092 verifyFormat("switch (variable) {\n" 21093 "case 1:\n" 21094 "case 2:\n" 21095 " doSomething();\n" 21096 " break;\n" 21097 "default:\n" 21098 " ++variable;\n" 21099 "}", 21100 Style); 21101 21102 // Wrap before binary operators. 21103 EXPECT_EQ("void f()\n" 21104 "{\n" 21105 " if (aaaaaaaaaaaaaaaa\n" 21106 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 21107 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 21108 " return;\n" 21109 "}", 21110 format("void f() {\n" 21111 "if (aaaaaaaaaaaaaaaa\n" 21112 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 21113 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 21114 "return;\n" 21115 "}", 21116 Style)); 21117 21118 // Allow functions on a single line. 21119 verifyFormat("void f() { return; }", Style); 21120 21121 // Allow empty blocks on a single line and insert a space in empty blocks. 21122 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 21123 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 21124 // However, don't merge non-empty short loops. 21125 EXPECT_EQ("while (true) {\n" 21126 " continue;\n" 21127 "}", 21128 format("while (true) { continue; }", Style)); 21129 21130 // Constructor initializers are formatted one per line with the "," on the 21131 // new line. 21132 verifyFormat("Constructor()\n" 21133 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 21134 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 21135 " aaaaaaaaaaaaaa)\n" 21136 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 21137 "{\n" 21138 "}", 21139 Style); 21140 verifyFormat("SomeClass::Constructor()\n" 21141 " : a(a)\n" 21142 "{\n" 21143 "}", 21144 Style); 21145 EXPECT_EQ("SomeClass::Constructor()\n" 21146 " : a(a)\n" 21147 "{\n" 21148 "}", 21149 format("SomeClass::Constructor():a(a){}", Style)); 21150 verifyFormat("SomeClass::Constructor()\n" 21151 " : a(a)\n" 21152 " , b(b)\n" 21153 " , c(c)\n" 21154 "{\n" 21155 "}", 21156 Style); 21157 verifyFormat("SomeClass::Constructor()\n" 21158 " : a(a)\n" 21159 "{\n" 21160 " foo();\n" 21161 " bar();\n" 21162 "}", 21163 Style); 21164 21165 // Access specifiers should be aligned left. 21166 verifyFormat("class C {\n" 21167 "public:\n" 21168 " int i;\n" 21169 "};", 21170 Style); 21171 21172 // Do not align comments. 21173 verifyFormat("int a; // Do not\n" 21174 "double b; // align comments.", 21175 Style); 21176 21177 // Do not align operands. 21178 EXPECT_EQ("ASSERT(aaaa\n" 21179 " || bbbb);", 21180 format("ASSERT ( aaaa\n||bbbb);", Style)); 21181 21182 // Accept input's line breaks. 21183 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 21184 " || bbbbbbbbbbbbbbb) {\n" 21185 " i++;\n" 21186 "}", 21187 format("if (aaaaaaaaaaaaaaa\n" 21188 "|| bbbbbbbbbbbbbbb) { i++; }", 21189 Style)); 21190 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 21191 " i++;\n" 21192 "}", 21193 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 21194 21195 // Don't automatically break all macro definitions (llvm.org/PR17842). 21196 verifyFormat("#define aNumber 10", Style); 21197 // However, generally keep the line breaks that the user authored. 21198 EXPECT_EQ("#define aNumber \\\n" 21199 " 10", 21200 format("#define aNumber \\\n" 21201 " 10", 21202 Style)); 21203 21204 // Keep empty and one-element array literals on a single line. 21205 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 21206 " copyItems:YES];", 21207 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 21208 "copyItems:YES];", 21209 Style)); 21210 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 21211 " copyItems:YES];", 21212 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 21213 " copyItems:YES];", 21214 Style)); 21215 // FIXME: This does not seem right, there should be more indentation before 21216 // the array literal's entries. Nested blocks have the same problem. 21217 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 21218 " @\"a\",\n" 21219 " @\"a\"\n" 21220 "]\n" 21221 " copyItems:YES];", 21222 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 21223 " @\"a\",\n" 21224 " @\"a\"\n" 21225 " ]\n" 21226 " copyItems:YES];", 21227 Style)); 21228 EXPECT_EQ( 21229 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 21230 " copyItems:YES];", 21231 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 21232 " copyItems:YES];", 21233 Style)); 21234 21235 verifyFormat("[self.a b:c c:d];", Style); 21236 EXPECT_EQ("[self.a b:c\n" 21237 " c:d];", 21238 format("[self.a b:c\n" 21239 "c:d];", 21240 Style)); 21241 } 21242 21243 TEST_F(FormatTest, FormatsLambdas) { 21244 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 21245 verifyFormat( 21246 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 21247 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 21248 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 21249 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 21250 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 21251 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 21252 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 21253 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 21254 verifyFormat("int x = f(*+[] {});"); 21255 verifyFormat("void f() {\n" 21256 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 21257 "}\n"); 21258 verifyFormat("void f() {\n" 21259 " other(x.begin(), //\n" 21260 " x.end(), //\n" 21261 " [&](int, int) { return 1; });\n" 21262 "}\n"); 21263 verifyFormat("void f() {\n" 21264 " other.other.other.other.other(\n" 21265 " x.begin(), x.end(),\n" 21266 " [something, rather](int, int, int, int, int, int, int) { " 21267 "return 1; });\n" 21268 "}\n"); 21269 verifyFormat( 21270 "void f() {\n" 21271 " other.other.other.other.other(\n" 21272 " x.begin(), x.end(),\n" 21273 " [something, rather](int, int, int, int, int, int, int) {\n" 21274 " //\n" 21275 " });\n" 21276 "}\n"); 21277 verifyFormat("SomeFunction([]() { // A cool function...\n" 21278 " return 43;\n" 21279 "});"); 21280 EXPECT_EQ("SomeFunction([]() {\n" 21281 "#define A a\n" 21282 " return 43;\n" 21283 "});", 21284 format("SomeFunction([](){\n" 21285 "#define A a\n" 21286 "return 43;\n" 21287 "});")); 21288 verifyFormat("void f() {\n" 21289 " SomeFunction([](decltype(x), A *a) {});\n" 21290 " SomeFunction([](typeof(x), A *a) {});\n" 21291 " SomeFunction([](_Atomic(x), A *a) {});\n" 21292 " SomeFunction([](__underlying_type(x), A *a) {});\n" 21293 "}"); 21294 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21295 " [](const aaaaaaaaaa &a) { return a; });"); 21296 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 21297 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 21298 "});"); 21299 verifyFormat("Constructor()\n" 21300 " : Field([] { // comment\n" 21301 " int i;\n" 21302 " }) {}"); 21303 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 21304 " return some_parameter.size();\n" 21305 "};"); 21306 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 21307 " [](const string &s) { return s; };"); 21308 verifyFormat("int i = aaaaaa ? 1 //\n" 21309 " : [] {\n" 21310 " return 2; //\n" 21311 " }();"); 21312 verifyFormat("llvm::errs() << \"number of twos is \"\n" 21313 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 21314 " return x == 2; // force break\n" 21315 " });"); 21316 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21317 " [=](int iiiiiiiiiiii) {\n" 21318 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 21319 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 21320 " });", 21321 getLLVMStyleWithColumns(60)); 21322 21323 verifyFormat("SomeFunction({[&] {\n" 21324 " // comment\n" 21325 " },\n" 21326 " [&] {\n" 21327 " // comment\n" 21328 " }});"); 21329 verifyFormat("SomeFunction({[&] {\n" 21330 " // comment\n" 21331 "}});"); 21332 verifyFormat( 21333 "virtual aaaaaaaaaaaaaaaa(\n" 21334 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 21335 " aaaaa aaaaaaaaa);"); 21336 21337 // Lambdas with return types. 21338 verifyFormat("int c = []() -> int { return 2; }();\n"); 21339 verifyFormat("int c = []() -> int * { return 2; }();\n"); 21340 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 21341 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 21342 verifyFormat("foo([]() noexcept -> int {});"); 21343 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 21344 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 21345 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 21346 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 21347 verifyFormat("[a, a]() -> a<1> {};"); 21348 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 21349 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 21350 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 21351 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 21352 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 21353 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 21354 verifyFormat("[]() -> foo<!5> { return {}; };"); 21355 verifyFormat("[]() -> foo<~5> { return {}; };"); 21356 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 21357 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 21358 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 21359 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 21360 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 21361 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 21362 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 21363 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 21364 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 21365 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 21366 verifyFormat("namespace bar {\n" 21367 "// broken:\n" 21368 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 21369 "} // namespace bar"); 21370 verifyFormat("namespace bar {\n" 21371 "// broken:\n" 21372 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 21373 "} // namespace bar"); 21374 verifyFormat("namespace bar {\n" 21375 "// broken:\n" 21376 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 21377 "} // namespace bar"); 21378 verifyFormat("namespace bar {\n" 21379 "// broken:\n" 21380 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 21381 "} // namespace bar"); 21382 verifyFormat("namespace bar {\n" 21383 "// broken:\n" 21384 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 21385 "} // namespace bar"); 21386 verifyFormat("namespace bar {\n" 21387 "// broken:\n" 21388 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 21389 "} // namespace bar"); 21390 verifyFormat("namespace bar {\n" 21391 "// broken:\n" 21392 "auto foo{[]() -> foo<!5> { return {}; }};\n" 21393 "} // namespace bar"); 21394 verifyFormat("namespace bar {\n" 21395 "// broken:\n" 21396 "auto foo{[]() -> foo<~5> { return {}; }};\n" 21397 "} // namespace bar"); 21398 verifyFormat("namespace bar {\n" 21399 "// broken:\n" 21400 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 21401 "} // namespace bar"); 21402 verifyFormat("namespace bar {\n" 21403 "// broken:\n" 21404 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 21405 "} // namespace bar"); 21406 verifyFormat("namespace bar {\n" 21407 "// broken:\n" 21408 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 21409 "} // namespace bar"); 21410 verifyFormat("namespace bar {\n" 21411 "// broken:\n" 21412 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 21413 "} // namespace bar"); 21414 verifyFormat("namespace bar {\n" 21415 "// broken:\n" 21416 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 21417 "} // namespace bar"); 21418 verifyFormat("namespace bar {\n" 21419 "// broken:\n" 21420 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 21421 "} // namespace bar"); 21422 verifyFormat("namespace bar {\n" 21423 "// broken:\n" 21424 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 21425 "} // namespace bar"); 21426 verifyFormat("namespace bar {\n" 21427 "// broken:\n" 21428 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 21429 "} // namespace bar"); 21430 verifyFormat("namespace bar {\n" 21431 "// broken:\n" 21432 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 21433 "} // namespace bar"); 21434 verifyFormat("namespace bar {\n" 21435 "// broken:\n" 21436 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 21437 "} // namespace bar"); 21438 verifyFormat("[]() -> a<1> {};"); 21439 verifyFormat("[]() -> a<1> { ; };"); 21440 verifyFormat("[]() -> a<1> { ; }();"); 21441 verifyFormat("[a, a]() -> a<true> {};"); 21442 verifyFormat("[]() -> a<true> {};"); 21443 verifyFormat("[]() -> a<true> { ; };"); 21444 verifyFormat("[]() -> a<true> { ; }();"); 21445 verifyFormat("[a, a]() -> a<false> {};"); 21446 verifyFormat("[]() -> a<false> {};"); 21447 verifyFormat("[]() -> a<false> { ; };"); 21448 verifyFormat("[]() -> a<false> { ; }();"); 21449 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 21450 verifyFormat("namespace bar {\n" 21451 "auto foo{[]() -> foo<false> { ; }};\n" 21452 "} // namespace bar"); 21453 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 21454 " int j) -> int {\n" 21455 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 21456 "};"); 21457 verifyFormat( 21458 "aaaaaaaaaaaaaaaaaaaaaa(\n" 21459 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 21460 " return aaaaaaaaaaaaaaaaa;\n" 21461 " });", 21462 getLLVMStyleWithColumns(70)); 21463 verifyFormat("[]() //\n" 21464 " -> int {\n" 21465 " return 1; //\n" 21466 "};"); 21467 verifyFormat("[]() -> Void<T...> {};"); 21468 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 21469 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});"); 21470 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});"); 21471 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});"); 21472 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});"); 21473 verifyFormat("return int{[x = x]() { return x; }()};"); 21474 21475 // Lambdas with explicit template argument lists. 21476 verifyFormat( 21477 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 21478 verifyFormat("auto L = []<class T>(T) {\n" 21479 " {\n" 21480 " f();\n" 21481 " g();\n" 21482 " }\n" 21483 "};\n"); 21484 verifyFormat("auto L = []<class... T>(T...) {\n" 21485 " {\n" 21486 " f();\n" 21487 " g();\n" 21488 " }\n" 21489 "};\n"); 21490 verifyFormat("auto L = []<typename... T>(T...) {\n" 21491 " {\n" 21492 " f();\n" 21493 " g();\n" 21494 " }\n" 21495 "};\n"); 21496 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n" 21497 " {\n" 21498 " f();\n" 21499 " g();\n" 21500 " }\n" 21501 "};\n"); 21502 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n" 21503 " {\n" 21504 " f();\n" 21505 " g();\n" 21506 " }\n" 21507 "};\n"); 21508 21509 // Multiple lambdas in the same parentheses change indentation rules. These 21510 // lambdas are forced to start on new lines. 21511 verifyFormat("SomeFunction(\n" 21512 " []() {\n" 21513 " //\n" 21514 " },\n" 21515 " []() {\n" 21516 " //\n" 21517 " });"); 21518 21519 // A lambda passed as arg0 is always pushed to the next line. 21520 verifyFormat("SomeFunction(\n" 21521 " [this] {\n" 21522 " //\n" 21523 " },\n" 21524 " 1);\n"); 21525 21526 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 21527 // the arg0 case above. 21528 auto Style = getGoogleStyle(); 21529 Style.BinPackArguments = false; 21530 verifyFormat("SomeFunction(\n" 21531 " a,\n" 21532 " [this] {\n" 21533 " //\n" 21534 " },\n" 21535 " b);\n", 21536 Style); 21537 verifyFormat("SomeFunction(\n" 21538 " a,\n" 21539 " [this] {\n" 21540 " //\n" 21541 " },\n" 21542 " b);\n"); 21543 21544 // A lambda with a very long line forces arg0 to be pushed out irrespective of 21545 // the BinPackArguments value (as long as the code is wide enough). 21546 verifyFormat( 21547 "something->SomeFunction(\n" 21548 " a,\n" 21549 " [this] {\n" 21550 " " 21551 "D0000000000000000000000000000000000000000000000000000000000001();\n" 21552 " },\n" 21553 " b);\n"); 21554 21555 // A multi-line lambda is pulled up as long as the introducer fits on the 21556 // previous line and there are no further args. 21557 verifyFormat("function(1, [this, that] {\n" 21558 " //\n" 21559 "});\n"); 21560 verifyFormat("function([this, that] {\n" 21561 " //\n" 21562 "});\n"); 21563 // FIXME: this format is not ideal and we should consider forcing the first 21564 // arg onto its own line. 21565 verifyFormat("function(a, b, c, //\n" 21566 " d, [this, that] {\n" 21567 " //\n" 21568 " });\n"); 21569 21570 // Multiple lambdas are treated correctly even when there is a short arg0. 21571 verifyFormat("SomeFunction(\n" 21572 " 1,\n" 21573 " [this] {\n" 21574 " //\n" 21575 " },\n" 21576 " [this] {\n" 21577 " //\n" 21578 " },\n" 21579 " 1);\n"); 21580 21581 // More complex introducers. 21582 verifyFormat("return [i, args...] {};"); 21583 21584 // Not lambdas. 21585 verifyFormat("constexpr char hello[]{\"hello\"};"); 21586 verifyFormat("double &operator[](int i) { return 0; }\n" 21587 "int i;"); 21588 verifyFormat("std::unique_ptr<int[]> foo() {}"); 21589 verifyFormat("int i = a[a][a]->f();"); 21590 verifyFormat("int i = (*b)[a]->f();"); 21591 21592 // Other corner cases. 21593 verifyFormat("void f() {\n" 21594 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 21595 " );\n" 21596 "}"); 21597 verifyFormat("auto k = *[](int *j) { return j; }(&i);"); 21598 21599 // Lambdas created through weird macros. 21600 verifyFormat("void f() {\n" 21601 " MACRO((const AA &a) { return 1; });\n" 21602 " MACRO((AA &a) { return 1; });\n" 21603 "}"); 21604 21605 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 21606 " doo_dah();\n" 21607 " doo_dah();\n" 21608 " })) {\n" 21609 "}"); 21610 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 21611 " doo_dah();\n" 21612 " doo_dah();\n" 21613 " })) {\n" 21614 "}"); 21615 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 21616 " doo_dah();\n" 21617 " doo_dah();\n" 21618 " })) {\n" 21619 "}"); 21620 verifyFormat("auto lambda = []() {\n" 21621 " int a = 2\n" 21622 "#if A\n" 21623 " + 2\n" 21624 "#endif\n" 21625 " ;\n" 21626 "};"); 21627 21628 // Lambdas with complex multiline introducers. 21629 verifyFormat( 21630 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21631 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 21632 " -> ::std::unordered_set<\n" 21633 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 21634 " //\n" 21635 " });"); 21636 21637 FormatStyle DoNotMerge = getLLVMStyle(); 21638 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 21639 verifyFormat("auto c = []() {\n" 21640 " return b;\n" 21641 "};", 21642 "auto c = []() { return b; };", DoNotMerge); 21643 verifyFormat("auto c = []() {\n" 21644 "};", 21645 " auto c = []() {};", DoNotMerge); 21646 21647 FormatStyle MergeEmptyOnly = getLLVMStyle(); 21648 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 21649 verifyFormat("auto c = []() {\n" 21650 " return b;\n" 21651 "};", 21652 "auto c = []() {\n" 21653 " return b;\n" 21654 " };", 21655 MergeEmptyOnly); 21656 verifyFormat("auto c = []() {};", 21657 "auto c = []() {\n" 21658 "};", 21659 MergeEmptyOnly); 21660 21661 FormatStyle MergeInline = getLLVMStyle(); 21662 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 21663 verifyFormat("auto c = []() {\n" 21664 " return b;\n" 21665 "};", 21666 "auto c = []() { return b; };", MergeInline); 21667 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 21668 MergeInline); 21669 verifyFormat("function([]() { return b; }, a)", 21670 "function([]() { return b; }, a)", MergeInline); 21671 verifyFormat("function(a, []() { return b; })", 21672 "function(a, []() { return b; })", MergeInline); 21673 21674 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 21675 // AllowShortLambdasOnASingleLine 21676 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 21677 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 21678 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 21679 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21680 FormatStyle::ShortLambdaStyle::SLS_None; 21681 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 21682 " []()\n" 21683 " {\n" 21684 " return 17;\n" 21685 " });", 21686 LLVMWithBeforeLambdaBody); 21687 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 21688 " []()\n" 21689 " {\n" 21690 " });", 21691 LLVMWithBeforeLambdaBody); 21692 verifyFormat("auto fct_SLS_None = []()\n" 21693 "{\n" 21694 " return 17;\n" 21695 "};", 21696 LLVMWithBeforeLambdaBody); 21697 verifyFormat("TwoNestedLambdas_SLS_None(\n" 21698 " []()\n" 21699 " {\n" 21700 " return Call(\n" 21701 " []()\n" 21702 " {\n" 21703 " return 17;\n" 21704 " });\n" 21705 " });", 21706 LLVMWithBeforeLambdaBody); 21707 verifyFormat("void Fct() {\n" 21708 " return {[]()\n" 21709 " {\n" 21710 " return 17;\n" 21711 " }};\n" 21712 "}", 21713 LLVMWithBeforeLambdaBody); 21714 21715 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21716 FormatStyle::ShortLambdaStyle::SLS_Empty; 21717 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 21718 " []()\n" 21719 " {\n" 21720 " return 17;\n" 21721 " });", 21722 LLVMWithBeforeLambdaBody); 21723 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 21724 LLVMWithBeforeLambdaBody); 21725 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 21726 "ongFunctionName_SLS_Empty(\n" 21727 " []() {});", 21728 LLVMWithBeforeLambdaBody); 21729 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 21730 " []()\n" 21731 " {\n" 21732 " return 17;\n" 21733 " });", 21734 LLVMWithBeforeLambdaBody); 21735 verifyFormat("auto fct_SLS_Empty = []()\n" 21736 "{\n" 21737 " return 17;\n" 21738 "};", 21739 LLVMWithBeforeLambdaBody); 21740 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 21741 " []()\n" 21742 " {\n" 21743 " return Call([]() {});\n" 21744 " });", 21745 LLVMWithBeforeLambdaBody); 21746 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 21747 " []()\n" 21748 " {\n" 21749 " return Call([]() {});\n" 21750 " });", 21751 LLVMWithBeforeLambdaBody); 21752 verifyFormat( 21753 "FctWithLongLineInLambda_SLS_Empty(\n" 21754 " []()\n" 21755 " {\n" 21756 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21757 " AndShouldNotBeConsiderAsInline,\n" 21758 " LambdaBodyMustBeBreak);\n" 21759 " });", 21760 LLVMWithBeforeLambdaBody); 21761 21762 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21763 FormatStyle::ShortLambdaStyle::SLS_Inline; 21764 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 21765 LLVMWithBeforeLambdaBody); 21766 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 21767 LLVMWithBeforeLambdaBody); 21768 verifyFormat("auto fct_SLS_Inline = []()\n" 21769 "{\n" 21770 " return 17;\n" 21771 "};", 21772 LLVMWithBeforeLambdaBody); 21773 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 21774 "17; }); });", 21775 LLVMWithBeforeLambdaBody); 21776 verifyFormat( 21777 "FctWithLongLineInLambda_SLS_Inline(\n" 21778 " []()\n" 21779 " {\n" 21780 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21781 " AndShouldNotBeConsiderAsInline,\n" 21782 " LambdaBodyMustBeBreak);\n" 21783 " });", 21784 LLVMWithBeforeLambdaBody); 21785 verifyFormat("FctWithMultipleParams_SLS_Inline(" 21786 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 21787 " []() { return 17; });", 21788 LLVMWithBeforeLambdaBody); 21789 verifyFormat( 21790 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 21791 LLVMWithBeforeLambdaBody); 21792 21793 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21794 FormatStyle::ShortLambdaStyle::SLS_All; 21795 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 21796 LLVMWithBeforeLambdaBody); 21797 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 21798 LLVMWithBeforeLambdaBody); 21799 verifyFormat("auto fct_SLS_All = []() { return 17; };", 21800 LLVMWithBeforeLambdaBody); 21801 verifyFormat("FctWithOneParam_SLS_All(\n" 21802 " []()\n" 21803 " {\n" 21804 " // A cool function...\n" 21805 " return 43;\n" 21806 " });", 21807 LLVMWithBeforeLambdaBody); 21808 verifyFormat("FctWithMultipleParams_SLS_All(" 21809 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 21810 " []() { return 17; });", 21811 LLVMWithBeforeLambdaBody); 21812 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 21813 LLVMWithBeforeLambdaBody); 21814 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 21815 LLVMWithBeforeLambdaBody); 21816 verifyFormat( 21817 "FctWithLongLineInLambda_SLS_All(\n" 21818 " []()\n" 21819 " {\n" 21820 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21821 " AndShouldNotBeConsiderAsInline,\n" 21822 " LambdaBodyMustBeBreak);\n" 21823 " });", 21824 LLVMWithBeforeLambdaBody); 21825 verifyFormat( 21826 "auto fct_SLS_All = []()\n" 21827 "{\n" 21828 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21829 " AndShouldNotBeConsiderAsInline,\n" 21830 " LambdaBodyMustBeBreak);\n" 21831 "};", 21832 LLVMWithBeforeLambdaBody); 21833 LLVMWithBeforeLambdaBody.BinPackParameters = false; 21834 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 21835 LLVMWithBeforeLambdaBody); 21836 verifyFormat( 21837 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 21838 " FirstParam,\n" 21839 " SecondParam,\n" 21840 " ThirdParam,\n" 21841 " FourthParam);", 21842 LLVMWithBeforeLambdaBody); 21843 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 21844 " []() { return " 21845 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 21846 " FirstParam,\n" 21847 " SecondParam,\n" 21848 " ThirdParam,\n" 21849 " FourthParam);", 21850 LLVMWithBeforeLambdaBody); 21851 verifyFormat( 21852 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 21853 " SecondParam,\n" 21854 " ThirdParam,\n" 21855 " FourthParam,\n" 21856 " []() { return SomeValueNotSoLong; });", 21857 LLVMWithBeforeLambdaBody); 21858 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 21859 " []()\n" 21860 " {\n" 21861 " return " 21862 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 21863 "eConsiderAsInline;\n" 21864 " });", 21865 LLVMWithBeforeLambdaBody); 21866 verifyFormat( 21867 "FctWithLongLineInLambda_SLS_All(\n" 21868 " []()\n" 21869 " {\n" 21870 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21871 " AndShouldNotBeConsiderAsInline,\n" 21872 " LambdaBodyMustBeBreak);\n" 21873 " });", 21874 LLVMWithBeforeLambdaBody); 21875 verifyFormat("FctWithTwoParams_SLS_All(\n" 21876 " []()\n" 21877 " {\n" 21878 " // A cool function...\n" 21879 " return 43;\n" 21880 " },\n" 21881 " 87);", 21882 LLVMWithBeforeLambdaBody); 21883 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 21884 LLVMWithBeforeLambdaBody); 21885 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 21886 LLVMWithBeforeLambdaBody); 21887 verifyFormat( 21888 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 21889 LLVMWithBeforeLambdaBody); 21890 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 21891 "}); }, x);", 21892 LLVMWithBeforeLambdaBody); 21893 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21894 " []()\n" 21895 " {\n" 21896 " // A cool function...\n" 21897 " return Call([]() { return 17; });\n" 21898 " });", 21899 LLVMWithBeforeLambdaBody); 21900 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21901 " []()\n" 21902 " {\n" 21903 " return Call(\n" 21904 " []()\n" 21905 " {\n" 21906 " // A cool function...\n" 21907 " return 17;\n" 21908 " });\n" 21909 " });", 21910 LLVMWithBeforeLambdaBody); 21911 21912 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21913 FormatStyle::ShortLambdaStyle::SLS_None; 21914 21915 verifyFormat("auto select = [this]() -> const Library::Object *\n" 21916 "{\n" 21917 " return MyAssignment::SelectFromList(this);\n" 21918 "};\n", 21919 LLVMWithBeforeLambdaBody); 21920 21921 verifyFormat("auto select = [this]() -> const Library::Object &\n" 21922 "{\n" 21923 " return MyAssignment::SelectFromList(this);\n" 21924 "};\n", 21925 LLVMWithBeforeLambdaBody); 21926 21927 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 21928 "{\n" 21929 " return MyAssignment::SelectFromList(this);\n" 21930 "};\n", 21931 LLVMWithBeforeLambdaBody); 21932 21933 verifyFormat("namespace test {\n" 21934 "class Test {\n" 21935 "public:\n" 21936 " Test() = default;\n" 21937 "};\n" 21938 "} // namespace test", 21939 LLVMWithBeforeLambdaBody); 21940 21941 // Lambdas with different indentation styles. 21942 Style = getLLVMStyleWithColumns(100); 21943 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21944 " return promise.then(\n" 21945 " [this, &someVariable, someObject = " 21946 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21947 " return someObject.startAsyncAction().then(\n" 21948 " [this, &someVariable](AsyncActionResult result) " 21949 "mutable { result.processMore(); });\n" 21950 " });\n" 21951 "}\n", 21952 format("SomeResult doSomething(SomeObject promise) {\n" 21953 " return promise.then([this, &someVariable, someObject = " 21954 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21955 " return someObject.startAsyncAction().then([this, " 21956 "&someVariable](AsyncActionResult result) mutable {\n" 21957 " result.processMore();\n" 21958 " });\n" 21959 " });\n" 21960 "}\n", 21961 Style)); 21962 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21963 verifyFormat("test() {\n" 21964 " ([]() -> {\n" 21965 " int b = 32;\n" 21966 " return 3;\n" 21967 " }).foo();\n" 21968 "}", 21969 Style); 21970 verifyFormat("test() {\n" 21971 " []() -> {\n" 21972 " int b = 32;\n" 21973 " return 3;\n" 21974 " }\n" 21975 "}", 21976 Style); 21977 verifyFormat("std::sort(v.begin(), v.end(),\n" 21978 " [](const auto &someLongArgumentName, const auto " 21979 "&someOtherLongArgumentName) {\n" 21980 " return someLongArgumentName.someMemberVariable < " 21981 "someOtherLongArgumentName.someMemberVariable;\n" 21982 "});", 21983 Style); 21984 verifyFormat("test() {\n" 21985 " (\n" 21986 " []() -> {\n" 21987 " int b = 32;\n" 21988 " return 3;\n" 21989 " },\n" 21990 " foo, bar)\n" 21991 " .foo();\n" 21992 "}", 21993 Style); 21994 verifyFormat("test() {\n" 21995 " ([]() -> {\n" 21996 " int b = 32;\n" 21997 " return 3;\n" 21998 " })\n" 21999 " .foo()\n" 22000 " .bar();\n" 22001 "}", 22002 Style); 22003 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 22004 " return promise.then(\n" 22005 " [this, &someVariable, someObject = " 22006 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 22007 " return someObject.startAsyncAction().then(\n" 22008 " [this, &someVariable](AsyncActionResult result) mutable { " 22009 "result.processMore(); });\n" 22010 " });\n" 22011 "}\n", 22012 format("SomeResult doSomething(SomeObject promise) {\n" 22013 " return promise.then([this, &someVariable, someObject = " 22014 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 22015 " return someObject.startAsyncAction().then([this, " 22016 "&someVariable](AsyncActionResult result) mutable {\n" 22017 " result.processMore();\n" 22018 " });\n" 22019 " });\n" 22020 "}\n", 22021 Style)); 22022 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 22023 " return promise.then([this, &someVariable] {\n" 22024 " return someObject.startAsyncAction().then(\n" 22025 " [this, &someVariable](AsyncActionResult result) mutable { " 22026 "result.processMore(); });\n" 22027 " });\n" 22028 "}\n", 22029 format("SomeResult doSomething(SomeObject promise) {\n" 22030 " return promise.then([this, &someVariable] {\n" 22031 " return someObject.startAsyncAction().then([this, " 22032 "&someVariable](AsyncActionResult result) mutable {\n" 22033 " result.processMore();\n" 22034 " });\n" 22035 " });\n" 22036 "}\n", 22037 Style)); 22038 Style = getGoogleStyle(); 22039 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 22040 EXPECT_EQ("#define A \\\n" 22041 " [] { \\\n" 22042 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 22043 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 22044 " }", 22045 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 22046 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 22047 Style)); 22048 // TODO: The current formatting has a minor issue that's not worth fixing 22049 // right now whereby the closing brace is indented relative to the signature 22050 // instead of being aligned. This only happens with macros. 22051 } 22052 22053 TEST_F(FormatTest, LambdaWithLineComments) { 22054 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 22055 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 22056 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 22057 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 22058 FormatStyle::ShortLambdaStyle::SLS_All; 22059 22060 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 22061 verifyFormat("auto k = []() // comment\n" 22062 "{ return; }", 22063 LLVMWithBeforeLambdaBody); 22064 verifyFormat("auto k = []() /* comment */ { return; }", 22065 LLVMWithBeforeLambdaBody); 22066 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 22067 LLVMWithBeforeLambdaBody); 22068 verifyFormat("auto k = []() // X\n" 22069 "{ return; }", 22070 LLVMWithBeforeLambdaBody); 22071 verifyFormat( 22072 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 22073 "{ return; }", 22074 LLVMWithBeforeLambdaBody); 22075 22076 LLVMWithBeforeLambdaBody.ColumnLimit = 0; 22077 22078 verifyFormat("foo([]()\n" 22079 " {\n" 22080 " bar(); //\n" 22081 " return 1; // comment\n" 22082 " }());", 22083 "foo([]() {\n" 22084 " bar(); //\n" 22085 " return 1; // comment\n" 22086 "}());", 22087 LLVMWithBeforeLambdaBody); 22088 verifyFormat("foo(\n" 22089 " 1, MACRO {\n" 22090 " baz();\n" 22091 " bar(); // comment\n" 22092 " },\n" 22093 " []() {});", 22094 "foo(\n" 22095 " 1, MACRO { baz(); bar(); // comment\n" 22096 " }, []() {}\n" 22097 ");", 22098 LLVMWithBeforeLambdaBody); 22099 } 22100 22101 TEST_F(FormatTest, EmptyLinesInLambdas) { 22102 verifyFormat("auto lambda = []() {\n" 22103 " x(); //\n" 22104 "};", 22105 "auto lambda = []() {\n" 22106 "\n" 22107 " x(); //\n" 22108 "\n" 22109 "};"); 22110 } 22111 22112 TEST_F(FormatTest, FormatsBlocks) { 22113 FormatStyle ShortBlocks = getLLVMStyle(); 22114 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 22115 verifyFormat("int (^Block)(int, int);", ShortBlocks); 22116 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 22117 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 22118 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 22119 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 22120 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 22121 22122 verifyFormat("foo(^{ bar(); });", ShortBlocks); 22123 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 22124 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 22125 22126 verifyFormat("[operation setCompletionBlock:^{\n" 22127 " [self onOperationDone];\n" 22128 "}];"); 22129 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 22130 " [self onOperationDone];\n" 22131 "}]};"); 22132 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 22133 " f();\n" 22134 "}];"); 22135 verifyFormat("int a = [operation block:^int(int *i) {\n" 22136 " return 1;\n" 22137 "}];"); 22138 verifyFormat("[myObject doSomethingWith:arg1\n" 22139 " aaa:^int(int *a) {\n" 22140 " return 1;\n" 22141 " }\n" 22142 " bbb:f(a * bbbbbbbb)];"); 22143 22144 verifyFormat("[operation setCompletionBlock:^{\n" 22145 " [self.delegate newDataAvailable];\n" 22146 "}];", 22147 getLLVMStyleWithColumns(60)); 22148 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 22149 " NSString *path = [self sessionFilePath];\n" 22150 " if (path) {\n" 22151 " // ...\n" 22152 " }\n" 22153 "});"); 22154 verifyFormat("[[SessionService sharedService]\n" 22155 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 22156 " if (window) {\n" 22157 " [self windowDidLoad:window];\n" 22158 " } else {\n" 22159 " [self errorLoadingWindow];\n" 22160 " }\n" 22161 " }];"); 22162 verifyFormat("void (^largeBlock)(void) = ^{\n" 22163 " // ...\n" 22164 "};\n", 22165 getLLVMStyleWithColumns(40)); 22166 verifyFormat("[[SessionService sharedService]\n" 22167 " loadWindowWithCompletionBlock: //\n" 22168 " ^(SessionWindow *window) {\n" 22169 " if (window) {\n" 22170 " [self windowDidLoad:window];\n" 22171 " } else {\n" 22172 " [self errorLoadingWindow];\n" 22173 " }\n" 22174 " }];", 22175 getLLVMStyleWithColumns(60)); 22176 verifyFormat("[myObject doSomethingWith:arg1\n" 22177 " firstBlock:^(Foo *a) {\n" 22178 " // ...\n" 22179 " int i;\n" 22180 " }\n" 22181 " secondBlock:^(Bar *b) {\n" 22182 " // ...\n" 22183 " int i;\n" 22184 " }\n" 22185 " thirdBlock:^Foo(Bar *b) {\n" 22186 " // ...\n" 22187 " int i;\n" 22188 " }];"); 22189 verifyFormat("[myObject doSomethingWith:arg1\n" 22190 " firstBlock:-1\n" 22191 " secondBlock:^(Bar *b) {\n" 22192 " // ...\n" 22193 " int i;\n" 22194 " }];"); 22195 22196 verifyFormat("f(^{\n" 22197 " @autoreleasepool {\n" 22198 " if (a) {\n" 22199 " g();\n" 22200 " }\n" 22201 " }\n" 22202 "});"); 22203 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 22204 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 22205 "};"); 22206 22207 FormatStyle FourIndent = getLLVMStyle(); 22208 FourIndent.ObjCBlockIndentWidth = 4; 22209 verifyFormat("[operation setCompletionBlock:^{\n" 22210 " [self onOperationDone];\n" 22211 "}];", 22212 FourIndent); 22213 } 22214 22215 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 22216 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 22217 22218 verifyFormat("[[SessionService sharedService] " 22219 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 22220 " if (window) {\n" 22221 " [self windowDidLoad:window];\n" 22222 " } else {\n" 22223 " [self errorLoadingWindow];\n" 22224 " }\n" 22225 "}];", 22226 ZeroColumn); 22227 EXPECT_EQ("[[SessionService sharedService]\n" 22228 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 22229 " if (window) {\n" 22230 " [self windowDidLoad:window];\n" 22231 " } else {\n" 22232 " [self errorLoadingWindow];\n" 22233 " }\n" 22234 " }];", 22235 format("[[SessionService sharedService]\n" 22236 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 22237 " if (window) {\n" 22238 " [self windowDidLoad:window];\n" 22239 " } else {\n" 22240 " [self errorLoadingWindow];\n" 22241 " }\n" 22242 "}];", 22243 ZeroColumn)); 22244 verifyFormat("[myObject doSomethingWith:arg1\n" 22245 " firstBlock:^(Foo *a) {\n" 22246 " // ...\n" 22247 " int i;\n" 22248 " }\n" 22249 " secondBlock:^(Bar *b) {\n" 22250 " // ...\n" 22251 " int i;\n" 22252 " }\n" 22253 " thirdBlock:^Foo(Bar *b) {\n" 22254 " // ...\n" 22255 " int i;\n" 22256 " }];", 22257 ZeroColumn); 22258 verifyFormat("f(^{\n" 22259 " @autoreleasepool {\n" 22260 " if (a) {\n" 22261 " g();\n" 22262 " }\n" 22263 " }\n" 22264 "});", 22265 ZeroColumn); 22266 verifyFormat("void (^largeBlock)(void) = ^{\n" 22267 " // ...\n" 22268 "};", 22269 ZeroColumn); 22270 22271 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 22272 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 22273 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 22274 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 22275 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 22276 " int i;\n" 22277 "};", 22278 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 22279 } 22280 22281 TEST_F(FormatTest, SupportsCRLF) { 22282 EXPECT_EQ("int a;\r\n" 22283 "int b;\r\n" 22284 "int c;\r\n", 22285 format("int a;\r\n" 22286 " int b;\r\n" 22287 " int c;\r\n", 22288 getLLVMStyle())); 22289 EXPECT_EQ("int a;\r\n" 22290 "int b;\r\n" 22291 "int c;\r\n", 22292 format("int a;\r\n" 22293 " int b;\n" 22294 " int c;\r\n", 22295 getLLVMStyle())); 22296 EXPECT_EQ("int a;\n" 22297 "int b;\n" 22298 "int c;\n", 22299 format("int a;\r\n" 22300 " int b;\n" 22301 " int c;\n", 22302 getLLVMStyle())); 22303 EXPECT_EQ("\"aaaaaaa \"\r\n" 22304 "\"bbbbbbb\";\r\n", 22305 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 22306 EXPECT_EQ("#define A \\\r\n" 22307 " b; \\\r\n" 22308 " c; \\\r\n" 22309 " d;\r\n", 22310 format("#define A \\\r\n" 22311 " b; \\\r\n" 22312 " c; d; \r\n", 22313 getGoogleStyle())); 22314 22315 EXPECT_EQ("/*\r\n" 22316 "multi line block comments\r\n" 22317 "should not introduce\r\n" 22318 "an extra carriage return\r\n" 22319 "*/\r\n", 22320 format("/*\r\n" 22321 "multi line block comments\r\n" 22322 "should not introduce\r\n" 22323 "an extra carriage return\r\n" 22324 "*/\r\n")); 22325 EXPECT_EQ("/*\r\n" 22326 "\r\n" 22327 "*/", 22328 format("/*\r\n" 22329 " \r\r\r\n" 22330 "*/")); 22331 22332 FormatStyle style = getLLVMStyle(); 22333 22334 style.DeriveLineEnding = true; 22335 style.UseCRLF = false; 22336 EXPECT_EQ("union FooBarBazQux {\n" 22337 " int foo;\n" 22338 " int bar;\n" 22339 " int baz;\n" 22340 "};", 22341 format("union FooBarBazQux {\r\n" 22342 " int foo;\n" 22343 " int bar;\r\n" 22344 " int baz;\n" 22345 "};", 22346 style)); 22347 style.UseCRLF = true; 22348 EXPECT_EQ("union FooBarBazQux {\r\n" 22349 " int foo;\r\n" 22350 " int bar;\r\n" 22351 " int baz;\r\n" 22352 "};", 22353 format("union FooBarBazQux {\r\n" 22354 " int foo;\n" 22355 " int bar;\r\n" 22356 " int baz;\n" 22357 "};", 22358 style)); 22359 22360 style.DeriveLineEnding = false; 22361 style.UseCRLF = false; 22362 EXPECT_EQ("union FooBarBazQux {\n" 22363 " int foo;\n" 22364 " int bar;\n" 22365 " int baz;\n" 22366 " int qux;\n" 22367 "};", 22368 format("union FooBarBazQux {\r\n" 22369 " int foo;\n" 22370 " int bar;\r\n" 22371 " int baz;\n" 22372 " int qux;\r\n" 22373 "};", 22374 style)); 22375 style.UseCRLF = true; 22376 EXPECT_EQ("union FooBarBazQux {\r\n" 22377 " int foo;\r\n" 22378 " int bar;\r\n" 22379 " int baz;\r\n" 22380 " int qux;\r\n" 22381 "};", 22382 format("union FooBarBazQux {\r\n" 22383 " int foo;\n" 22384 " int bar;\r\n" 22385 " int baz;\n" 22386 " int qux;\n" 22387 "};", 22388 style)); 22389 22390 style.DeriveLineEnding = true; 22391 style.UseCRLF = false; 22392 EXPECT_EQ("union FooBarBazQux {\r\n" 22393 " int foo;\r\n" 22394 " int bar;\r\n" 22395 " int baz;\r\n" 22396 " int qux;\r\n" 22397 "};", 22398 format("union FooBarBazQux {\r\n" 22399 " int foo;\n" 22400 " int bar;\r\n" 22401 " int baz;\n" 22402 " int qux;\r\n" 22403 "};", 22404 style)); 22405 style.UseCRLF = true; 22406 EXPECT_EQ("union FooBarBazQux {\n" 22407 " int foo;\n" 22408 " int bar;\n" 22409 " int baz;\n" 22410 " int qux;\n" 22411 "};", 22412 format("union FooBarBazQux {\r\n" 22413 " int foo;\n" 22414 " int bar;\r\n" 22415 " int baz;\n" 22416 " int qux;\n" 22417 "};", 22418 style)); 22419 } 22420 22421 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 22422 verifyFormat("MY_CLASS(C) {\n" 22423 " int i;\n" 22424 " int j;\n" 22425 "};"); 22426 } 22427 22428 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 22429 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 22430 TwoIndent.ContinuationIndentWidth = 2; 22431 22432 EXPECT_EQ("int i =\n" 22433 " longFunction(\n" 22434 " arg);", 22435 format("int i = longFunction(arg);", TwoIndent)); 22436 22437 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 22438 SixIndent.ContinuationIndentWidth = 6; 22439 22440 EXPECT_EQ("int i =\n" 22441 " longFunction(\n" 22442 " arg);", 22443 format("int i = longFunction(arg);", SixIndent)); 22444 } 22445 22446 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 22447 FormatStyle Style = getLLVMStyle(); 22448 verifyFormat("int Foo::getter(\n" 22449 " //\n" 22450 ") const {\n" 22451 " return foo;\n" 22452 "}", 22453 Style); 22454 verifyFormat("void Foo::setter(\n" 22455 " //\n" 22456 ") {\n" 22457 " foo = 1;\n" 22458 "}", 22459 Style); 22460 } 22461 22462 TEST_F(FormatTest, SpacesInAngles) { 22463 FormatStyle Spaces = getLLVMStyle(); 22464 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 22465 22466 verifyFormat("vector< ::std::string > x1;", Spaces); 22467 verifyFormat("Foo< int, Bar > x2;", Spaces); 22468 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 22469 22470 verifyFormat("static_cast< int >(arg);", Spaces); 22471 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 22472 verifyFormat("f< int, float >();", Spaces); 22473 verifyFormat("template <> g() {}", Spaces); 22474 verifyFormat("template < std::vector< int > > f() {}", Spaces); 22475 verifyFormat("std::function< void(int, int) > fct;", Spaces); 22476 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 22477 Spaces); 22478 22479 Spaces.Standard = FormatStyle::LS_Cpp03; 22480 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 22481 verifyFormat("A< A< int > >();", Spaces); 22482 22483 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 22484 verifyFormat("A<A<int> >();", Spaces); 22485 22486 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 22487 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 22488 Spaces); 22489 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 22490 Spaces); 22491 22492 verifyFormat("A<A<int> >();", Spaces); 22493 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 22494 verifyFormat("A< A< int > >();", Spaces); 22495 22496 Spaces.Standard = FormatStyle::LS_Cpp11; 22497 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 22498 verifyFormat("A< A< int > >();", Spaces); 22499 22500 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 22501 verifyFormat("vector<::std::string> x4;", Spaces); 22502 verifyFormat("vector<int> x5;", Spaces); 22503 verifyFormat("Foo<int, Bar> x6;", Spaces); 22504 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 22505 22506 verifyFormat("A<A<int>>();", Spaces); 22507 22508 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 22509 verifyFormat("vector<::std::string> x4;", Spaces); 22510 verifyFormat("vector< ::std::string > x4;", Spaces); 22511 verifyFormat("vector<int> x5;", Spaces); 22512 verifyFormat("vector< int > x5;", Spaces); 22513 verifyFormat("Foo<int, Bar> x6;", Spaces); 22514 verifyFormat("Foo< int, Bar > x6;", Spaces); 22515 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 22516 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 22517 22518 verifyFormat("A<A<int>>();", Spaces); 22519 verifyFormat("A< A< int > >();", Spaces); 22520 verifyFormat("A<A<int > >();", Spaces); 22521 verifyFormat("A< A< int>>();", Spaces); 22522 22523 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 22524 verifyFormat("// clang-format off\n" 22525 "foo<<<1, 1>>>();\n" 22526 "// clang-format on\n", 22527 Spaces); 22528 verifyFormat("// clang-format off\n" 22529 "foo< < <1, 1> > >();\n" 22530 "// clang-format on\n", 22531 Spaces); 22532 } 22533 22534 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 22535 FormatStyle Style = getLLVMStyle(); 22536 Style.SpaceAfterTemplateKeyword = false; 22537 verifyFormat("template<int> void foo();", Style); 22538 } 22539 22540 TEST_F(FormatTest, TripleAngleBrackets) { 22541 verifyFormat("f<<<1, 1>>>();"); 22542 verifyFormat("f<<<1, 1, 1, s>>>();"); 22543 verifyFormat("f<<<a, b, c, d>>>();"); 22544 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 22545 verifyFormat("f<param><<<1, 1>>>();"); 22546 verifyFormat("f<1><<<1, 1>>>();"); 22547 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 22548 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 22549 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 22550 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 22551 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 22552 } 22553 22554 TEST_F(FormatTest, MergeLessLessAtEnd) { 22555 verifyFormat("<<"); 22556 EXPECT_EQ("< < <", format("\\\n<<<")); 22557 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 22558 "aaallvm::outs() <<"); 22559 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 22560 "aaaallvm::outs()\n <<"); 22561 } 22562 22563 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 22564 std::string code = "#if A\n" 22565 "#if B\n" 22566 "a.\n" 22567 "#endif\n" 22568 " a = 1;\n" 22569 "#else\n" 22570 "#endif\n" 22571 "#if C\n" 22572 "#else\n" 22573 "#endif\n"; 22574 EXPECT_EQ(code, format(code)); 22575 } 22576 22577 TEST_F(FormatTest, HandleConflictMarkers) { 22578 // Git/SVN conflict markers. 22579 EXPECT_EQ("int a;\n" 22580 "void f() {\n" 22581 " callme(some(parameter1,\n" 22582 "<<<<<<< text by the vcs\n" 22583 " parameter2),\n" 22584 "||||||| text by the vcs\n" 22585 " parameter2),\n" 22586 " parameter3,\n" 22587 "======= text by the vcs\n" 22588 " parameter2, parameter3),\n" 22589 ">>>>>>> text by the vcs\n" 22590 " otherparameter);\n", 22591 format("int a;\n" 22592 "void f() {\n" 22593 " callme(some(parameter1,\n" 22594 "<<<<<<< text by the vcs\n" 22595 " parameter2),\n" 22596 "||||||| text by the vcs\n" 22597 " parameter2),\n" 22598 " parameter3,\n" 22599 "======= text by the vcs\n" 22600 " parameter2,\n" 22601 " parameter3),\n" 22602 ">>>>>>> text by the vcs\n" 22603 " otherparameter);\n")); 22604 22605 // Perforce markers. 22606 EXPECT_EQ("void f() {\n" 22607 " function(\n" 22608 ">>>> text by the vcs\n" 22609 " parameter,\n" 22610 "==== text by the vcs\n" 22611 " parameter,\n" 22612 "==== text by the vcs\n" 22613 " parameter,\n" 22614 "<<<< text by the vcs\n" 22615 " parameter);\n", 22616 format("void f() {\n" 22617 " function(\n" 22618 ">>>> text by the vcs\n" 22619 " parameter,\n" 22620 "==== text by the vcs\n" 22621 " parameter,\n" 22622 "==== text by the vcs\n" 22623 " parameter,\n" 22624 "<<<< text by the vcs\n" 22625 " parameter);\n")); 22626 22627 EXPECT_EQ("<<<<<<<\n" 22628 "|||||||\n" 22629 "=======\n" 22630 ">>>>>>>", 22631 format("<<<<<<<\n" 22632 "|||||||\n" 22633 "=======\n" 22634 ">>>>>>>")); 22635 22636 EXPECT_EQ("<<<<<<<\n" 22637 "|||||||\n" 22638 "int i;\n" 22639 "=======\n" 22640 ">>>>>>>", 22641 format("<<<<<<<\n" 22642 "|||||||\n" 22643 "int i;\n" 22644 "=======\n" 22645 ">>>>>>>")); 22646 22647 // FIXME: Handle parsing of macros around conflict markers correctly: 22648 EXPECT_EQ("#define Macro \\\n" 22649 "<<<<<<<\n" 22650 "Something \\\n" 22651 "|||||||\n" 22652 "Else \\\n" 22653 "=======\n" 22654 "Other \\\n" 22655 ">>>>>>>\n" 22656 " End int i;\n", 22657 format("#define Macro \\\n" 22658 "<<<<<<<\n" 22659 " Something \\\n" 22660 "|||||||\n" 22661 " Else \\\n" 22662 "=======\n" 22663 " Other \\\n" 22664 ">>>>>>>\n" 22665 " End\n" 22666 "int i;\n")); 22667 22668 verifyFormat(R"(==== 22669 #ifdef A 22670 a 22671 #else 22672 b 22673 #endif 22674 )"); 22675 } 22676 22677 TEST_F(FormatTest, DisableRegions) { 22678 EXPECT_EQ("int i;\n" 22679 "// clang-format off\n" 22680 " int j;\n" 22681 "// clang-format on\n" 22682 "int k;", 22683 format(" int i;\n" 22684 " // clang-format off\n" 22685 " int j;\n" 22686 " // clang-format on\n" 22687 " int k;")); 22688 EXPECT_EQ("int i;\n" 22689 "/* clang-format off */\n" 22690 " int j;\n" 22691 "/* clang-format on */\n" 22692 "int k;", 22693 format(" int i;\n" 22694 " /* clang-format off */\n" 22695 " int j;\n" 22696 " /* clang-format on */\n" 22697 " int k;")); 22698 22699 // Don't reflow comments within disabled regions. 22700 EXPECT_EQ("// clang-format off\n" 22701 "// long long long long long long line\n" 22702 "/* clang-format on */\n" 22703 "/* long long long\n" 22704 " * long long long\n" 22705 " * line */\n" 22706 "int i;\n" 22707 "/* clang-format off */\n" 22708 "/* long long long long long long line */\n", 22709 format("// clang-format off\n" 22710 "// long long long long long long line\n" 22711 "/* clang-format on */\n" 22712 "/* long long long long long long line */\n" 22713 "int i;\n" 22714 "/* clang-format off */\n" 22715 "/* long long long long long long line */\n", 22716 getLLVMStyleWithColumns(20))); 22717 } 22718 22719 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 22720 format("? ) ="); 22721 verifyNoCrash("#define a\\\n /**/}"); 22722 } 22723 22724 TEST_F(FormatTest, FormatsTableGenCode) { 22725 FormatStyle Style = getLLVMStyle(); 22726 Style.Language = FormatStyle::LK_TableGen; 22727 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 22728 } 22729 22730 TEST_F(FormatTest, ArrayOfTemplates) { 22731 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 22732 format("auto a = new unique_ptr<int > [ 10];")); 22733 22734 FormatStyle Spaces = getLLVMStyle(); 22735 Spaces.SpacesInSquareBrackets = true; 22736 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 22737 format("auto a = new unique_ptr<int > [10];", Spaces)); 22738 } 22739 22740 TEST_F(FormatTest, ArrayAsTemplateType) { 22741 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 22742 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 22743 22744 FormatStyle Spaces = getLLVMStyle(); 22745 Spaces.SpacesInSquareBrackets = true; 22746 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 22747 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 22748 } 22749 22750 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 22751 22752 TEST(FormatStyle, GetStyleWithEmptyFileName) { 22753 llvm::vfs::InMemoryFileSystem FS; 22754 auto Style1 = getStyle("file", "", "Google", "", &FS); 22755 ASSERT_TRUE((bool)Style1); 22756 ASSERT_EQ(*Style1, getGoogleStyle()); 22757 } 22758 22759 TEST(FormatStyle, GetStyleOfFile) { 22760 llvm::vfs::InMemoryFileSystem FS; 22761 // Test 1: format file in the same directory. 22762 ASSERT_TRUE( 22763 FS.addFile("/a/.clang-format", 0, 22764 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 22765 ASSERT_TRUE( 22766 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22767 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 22768 ASSERT_TRUE((bool)Style1); 22769 ASSERT_EQ(*Style1, getLLVMStyle()); 22770 22771 // Test 2.1: fallback to default. 22772 ASSERT_TRUE( 22773 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22774 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 22775 ASSERT_TRUE((bool)Style2); 22776 ASSERT_EQ(*Style2, getMozillaStyle()); 22777 22778 // Test 2.2: no format on 'none' fallback style. 22779 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 22780 ASSERT_TRUE((bool)Style2); 22781 ASSERT_EQ(*Style2, getNoStyle()); 22782 22783 // Test 2.3: format if config is found with no based style while fallback is 22784 // 'none'. 22785 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 22786 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 22787 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 22788 ASSERT_TRUE((bool)Style2); 22789 ASSERT_EQ(*Style2, getLLVMStyle()); 22790 22791 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 22792 Style2 = getStyle("{}", "a.h", "none", "", &FS); 22793 ASSERT_TRUE((bool)Style2); 22794 ASSERT_EQ(*Style2, getLLVMStyle()); 22795 22796 // Test 3: format file in parent directory. 22797 ASSERT_TRUE( 22798 FS.addFile("/c/.clang-format", 0, 22799 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22800 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 22801 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22802 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22803 ASSERT_TRUE((bool)Style3); 22804 ASSERT_EQ(*Style3, getGoogleStyle()); 22805 22806 // Test 4: error on invalid fallback style 22807 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 22808 ASSERT_FALSE((bool)Style4); 22809 llvm::consumeError(Style4.takeError()); 22810 22811 // Test 5: error on invalid yaml on command line 22812 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 22813 ASSERT_FALSE((bool)Style5); 22814 llvm::consumeError(Style5.takeError()); 22815 22816 // Test 6: error on invalid style 22817 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 22818 ASSERT_FALSE((bool)Style6); 22819 llvm::consumeError(Style6.takeError()); 22820 22821 // Test 7: found config file, error on parsing it 22822 ASSERT_TRUE( 22823 FS.addFile("/d/.clang-format", 0, 22824 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 22825 "InvalidKey: InvalidValue"))); 22826 ASSERT_TRUE( 22827 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22828 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 22829 ASSERT_FALSE((bool)Style7a); 22830 llvm::consumeError(Style7a.takeError()); 22831 22832 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 22833 ASSERT_TRUE((bool)Style7b); 22834 22835 // Test 8: inferred per-language defaults apply. 22836 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 22837 ASSERT_TRUE((bool)StyleTd); 22838 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 22839 22840 // Test 9.1.1: overwriting a file style, when no parent file exists with no 22841 // fallback style. 22842 ASSERT_TRUE(FS.addFile( 22843 "/e/sub/.clang-format", 0, 22844 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 22845 "ColumnLimit: 20"))); 22846 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 22847 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22848 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 22849 ASSERT_TRUE(static_cast<bool>(Style9)); 22850 ASSERT_EQ(*Style9, [] { 22851 auto Style = getNoStyle(); 22852 Style.ColumnLimit = 20; 22853 return Style; 22854 }()); 22855 22856 // Test 9.1.2: propagate more than one level with no parent file. 22857 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 22858 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22859 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 22860 llvm::MemoryBuffer::getMemBuffer( 22861 "BasedOnStyle: InheritParentConfig\n" 22862 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 22863 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 22864 22865 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22866 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22867 ASSERT_TRUE(static_cast<bool>(Style9)); 22868 ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] { 22869 auto Style = getNoStyle(); 22870 Style.ColumnLimit = 20; 22871 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22872 return Style; 22873 }()); 22874 22875 // Test 9.2: with LLVM fallback style 22876 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 22877 ASSERT_TRUE(static_cast<bool>(Style9)); 22878 ASSERT_EQ(*Style9, [] { 22879 auto Style = getLLVMStyle(); 22880 Style.ColumnLimit = 20; 22881 return Style; 22882 }()); 22883 22884 // Test 9.3: with a parent file 22885 ASSERT_TRUE( 22886 FS.addFile("/e/.clang-format", 0, 22887 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 22888 "UseTab: Always"))); 22889 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 22890 ASSERT_TRUE(static_cast<bool>(Style9)); 22891 ASSERT_EQ(*Style9, [] { 22892 auto Style = getGoogleStyle(); 22893 Style.ColumnLimit = 20; 22894 Style.UseTab = FormatStyle::UT_Always; 22895 return Style; 22896 }()); 22897 22898 // Test 9.4: propagate more than one level with a parent file. 22899 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 22900 auto Style = getGoogleStyle(); 22901 Style.ColumnLimit = 20; 22902 Style.UseTab = FormatStyle::UT_Always; 22903 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22904 return Style; 22905 }(); 22906 22907 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22908 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22909 ASSERT_TRUE(static_cast<bool>(Style9)); 22910 ASSERT_EQ(*Style9, SubSubStyle); 22911 22912 // Test 9.5: use InheritParentConfig as style name 22913 Style9 = 22914 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 22915 ASSERT_TRUE(static_cast<bool>(Style9)); 22916 ASSERT_EQ(*Style9, SubSubStyle); 22917 22918 // Test 9.6: use command line style with inheritance 22919 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 22920 "none", "", &FS); 22921 ASSERT_TRUE(static_cast<bool>(Style9)); 22922 ASSERT_EQ(*Style9, SubSubStyle); 22923 22924 // Test 9.7: use command line style with inheritance and own config 22925 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 22926 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 22927 "/e/sub/code.cpp", "none", "", &FS); 22928 ASSERT_TRUE(static_cast<bool>(Style9)); 22929 ASSERT_EQ(*Style9, SubSubStyle); 22930 22931 // Test 9.8: use inheritance from a file without BasedOnStyle 22932 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 22933 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 22934 ASSERT_TRUE( 22935 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 22936 llvm::MemoryBuffer::getMemBuffer( 22937 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 22938 // Make sure we do not use the fallback style 22939 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 22940 ASSERT_TRUE(static_cast<bool>(Style9)); 22941 ASSERT_EQ(*Style9, [] { 22942 auto Style = getLLVMStyle(); 22943 Style.ColumnLimit = 123; 22944 return Style; 22945 }()); 22946 22947 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 22948 ASSERT_TRUE(static_cast<bool>(Style9)); 22949 ASSERT_EQ(*Style9, [] { 22950 auto Style = getLLVMStyle(); 22951 Style.ColumnLimit = 123; 22952 Style.IndentWidth = 7; 22953 return Style; 22954 }()); 22955 22956 // Test 9.9: use inheritance from a specific config file. 22957 Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp", 22958 "none", "", &FS); 22959 ASSERT_TRUE(static_cast<bool>(Style9)); 22960 ASSERT_EQ(*Style9, SubSubStyle); 22961 } 22962 22963 TEST(FormatStyle, GetStyleOfSpecificFile) { 22964 llvm::vfs::InMemoryFileSystem FS; 22965 // Specify absolute path to a format file in a parent directory. 22966 ASSERT_TRUE( 22967 FS.addFile("/e/.clang-format", 0, 22968 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 22969 ASSERT_TRUE( 22970 FS.addFile("/e/explicit.clang-format", 0, 22971 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22972 ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0, 22973 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22974 auto Style = getStyle("file:/e/explicit.clang-format", 22975 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22976 ASSERT_TRUE(static_cast<bool>(Style)); 22977 ASSERT_EQ(*Style, getGoogleStyle()); 22978 22979 // Specify relative path to a format file. 22980 ASSERT_TRUE( 22981 FS.addFile("../../e/explicit.clang-format", 0, 22982 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22983 Style = getStyle("file:../../e/explicit.clang-format", 22984 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22985 ASSERT_TRUE(static_cast<bool>(Style)); 22986 ASSERT_EQ(*Style, getGoogleStyle()); 22987 22988 // Specify path to a format file that does not exist. 22989 Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp", 22990 "LLVM", "", &FS); 22991 ASSERT_FALSE(static_cast<bool>(Style)); 22992 llvm::consumeError(Style.takeError()); 22993 22994 // Specify path to a file on the filesystem. 22995 SmallString<128> FormatFilePath; 22996 std::error_code ECF = llvm::sys::fs::createTemporaryFile( 22997 "FormatFileTest", "tpl", FormatFilePath); 22998 EXPECT_FALSE((bool)ECF); 22999 llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF); 23000 EXPECT_FALSE((bool)ECF); 23001 FormatFileTest << "BasedOnStyle: Google\n"; 23002 FormatFileTest.close(); 23003 23004 SmallString<128> TestFilePath; 23005 std::error_code ECT = 23006 llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath); 23007 EXPECT_FALSE((bool)ECT); 23008 llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT); 23009 CodeFileTest << "int i;\n"; 23010 CodeFileTest.close(); 23011 23012 std::string format_file_arg = std::string("file:") + FormatFilePath.c_str(); 23013 Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr); 23014 23015 llvm::sys::fs::remove(FormatFilePath.c_str()); 23016 llvm::sys::fs::remove(TestFilePath.c_str()); 23017 ASSERT_TRUE(static_cast<bool>(Style)); 23018 ASSERT_EQ(*Style, getGoogleStyle()); 23019 } 23020 23021 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 23022 // Column limit is 20. 23023 std::string Code = "Type *a =\n" 23024 " new Type();\n" 23025 "g(iiiii, 0, jjjjj,\n" 23026 " 0, kkkkk, 0, mm);\n" 23027 "int bad = format ;"; 23028 std::string Expected = "auto a = new Type();\n" 23029 "g(iiiii, nullptr,\n" 23030 " jjjjj, nullptr,\n" 23031 " kkkkk, nullptr,\n" 23032 " mm);\n" 23033 "int bad = format ;"; 23034 FileID ID = Context.createInMemoryFile("format.cpp", Code); 23035 tooling::Replacements Replaces = toReplacements( 23036 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 23037 "auto "), 23038 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 23039 "nullptr"), 23040 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 23041 "nullptr"), 23042 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 23043 "nullptr")}); 23044 23045 FormatStyle Style = getLLVMStyle(); 23046 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 23047 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 23048 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 23049 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 23050 auto Result = applyAllReplacements(Code, *FormattedReplaces); 23051 EXPECT_TRUE(static_cast<bool>(Result)); 23052 EXPECT_EQ(Expected, *Result); 23053 } 23054 23055 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 23056 std::string Code = "#include \"a.h\"\n" 23057 "#include \"c.h\"\n" 23058 "\n" 23059 "int main() {\n" 23060 " return 0;\n" 23061 "}"; 23062 std::string Expected = "#include \"a.h\"\n" 23063 "#include \"b.h\"\n" 23064 "#include \"c.h\"\n" 23065 "\n" 23066 "int main() {\n" 23067 " return 0;\n" 23068 "}"; 23069 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 23070 tooling::Replacements Replaces = toReplacements( 23071 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 23072 "#include \"b.h\"\n")}); 23073 23074 FormatStyle Style = getLLVMStyle(); 23075 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 23076 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 23077 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 23078 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 23079 auto Result = applyAllReplacements(Code, *FormattedReplaces); 23080 EXPECT_TRUE(static_cast<bool>(Result)); 23081 EXPECT_EQ(Expected, *Result); 23082 } 23083 23084 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 23085 EXPECT_EQ("using std::cin;\n" 23086 "using std::cout;", 23087 format("using std::cout;\n" 23088 "using std::cin;", 23089 getGoogleStyle())); 23090 } 23091 23092 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 23093 FormatStyle Style = getLLVMStyle(); 23094 Style.Standard = FormatStyle::LS_Cpp03; 23095 // cpp03 recognize this string as identifier u8 and literal character 'a' 23096 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 23097 } 23098 23099 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 23100 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 23101 // all modes, including C++11, C++14 and C++17 23102 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 23103 } 23104 23105 TEST_F(FormatTest, DoNotFormatLikelyXml) { 23106 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 23107 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 23108 } 23109 23110 TEST_F(FormatTest, StructuredBindings) { 23111 // Structured bindings is a C++17 feature. 23112 // all modes, including C++11, C++14 and C++17 23113 verifyFormat("auto [a, b] = f();"); 23114 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 23115 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 23116 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 23117 EXPECT_EQ("auto const volatile [a, b] = f();", 23118 format("auto const volatile[a, b] = f();")); 23119 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 23120 EXPECT_EQ("auto &[a, b, c] = f();", 23121 format("auto &[ a , b,c ] = f();")); 23122 EXPECT_EQ("auto &&[a, b, c] = f();", 23123 format("auto &&[ a , b,c ] = f();")); 23124 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 23125 EXPECT_EQ("auto const volatile &&[a, b] = f();", 23126 format("auto const volatile &&[a, b] = f();")); 23127 EXPECT_EQ("auto const &&[a, b] = f();", 23128 format("auto const && [a, b] = f();")); 23129 EXPECT_EQ("const auto &[a, b] = f();", 23130 format("const auto & [a, b] = f();")); 23131 EXPECT_EQ("const auto volatile &&[a, b] = f();", 23132 format("const auto volatile &&[a, b] = f();")); 23133 EXPECT_EQ("volatile const auto &&[a, b] = f();", 23134 format("volatile const auto &&[a, b] = f();")); 23135 EXPECT_EQ("const auto &&[a, b] = f();", 23136 format("const auto && [a, b] = f();")); 23137 23138 // Make sure we don't mistake structured bindings for lambdas. 23139 FormatStyle PointerMiddle = getLLVMStyle(); 23140 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 23141 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 23142 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 23143 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 23144 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 23145 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 23146 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 23147 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 23148 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 23149 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 23150 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 23151 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 23152 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 23153 23154 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 23155 format("for (const auto && [a, b] : some_range) {\n}")); 23156 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 23157 format("for (const auto & [a, b] : some_range) {\n}")); 23158 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 23159 format("for (const auto[a, b] : some_range) {\n}")); 23160 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 23161 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 23162 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 23163 EXPECT_EQ("auto const &[x, y](expr);", 23164 format("auto const & [x,y] (expr);")); 23165 EXPECT_EQ("auto const &&[x, y](expr);", 23166 format("auto const && [x,y] (expr);")); 23167 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 23168 EXPECT_EQ("auto const &[x, y]{expr};", 23169 format("auto const & [x,y] {expr};")); 23170 EXPECT_EQ("auto const &&[x, y]{expr};", 23171 format("auto const && [x,y] {expr};")); 23172 23173 FormatStyle Spaces = getLLVMStyle(); 23174 Spaces.SpacesInSquareBrackets = true; 23175 verifyFormat("auto [ a, b ] = f();", Spaces); 23176 verifyFormat("auto &&[ a, b ] = f();", Spaces); 23177 verifyFormat("auto &[ a, b ] = f();", Spaces); 23178 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 23179 verifyFormat("auto const &[ a, b ] = f();", Spaces); 23180 } 23181 23182 TEST_F(FormatTest, FileAndCode) { 23183 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 23184 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 23185 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 23186 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 23187 EXPECT_EQ(FormatStyle::LK_ObjC, 23188 guessLanguage("foo.h", "@interface Foo\n@end\n")); 23189 EXPECT_EQ( 23190 FormatStyle::LK_ObjC, 23191 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 23192 EXPECT_EQ(FormatStyle::LK_ObjC, 23193 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 23194 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 23195 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 23196 EXPECT_EQ(FormatStyle::LK_ObjC, 23197 guessLanguage("foo", "@interface Foo\n@end\n")); 23198 EXPECT_EQ(FormatStyle::LK_ObjC, 23199 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 23200 EXPECT_EQ( 23201 FormatStyle::LK_ObjC, 23202 guessLanguage("foo.h", 23203 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 23204 EXPECT_EQ( 23205 FormatStyle::LK_Cpp, 23206 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 23207 // Only one of the two preprocessor regions has ObjC-like code. 23208 EXPECT_EQ(FormatStyle::LK_ObjC, 23209 guessLanguage("foo.h", "#if A\n" 23210 "#define B() C\n" 23211 "#else\n" 23212 "#define B() [NSString a:@\"\"]\n" 23213 "#endif\n")); 23214 } 23215 23216 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 23217 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 23218 EXPECT_EQ(FormatStyle::LK_ObjC, 23219 guessLanguage("foo.h", "array[[calculator getIndex]];")); 23220 EXPECT_EQ(FormatStyle::LK_Cpp, 23221 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 23222 EXPECT_EQ( 23223 FormatStyle::LK_Cpp, 23224 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 23225 EXPECT_EQ(FormatStyle::LK_ObjC, 23226 guessLanguage("foo.h", "[[noreturn foo] bar];")); 23227 EXPECT_EQ(FormatStyle::LK_Cpp, 23228 guessLanguage("foo.h", "[[clang::fallthrough]];")); 23229 EXPECT_EQ(FormatStyle::LK_ObjC, 23230 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 23231 EXPECT_EQ(FormatStyle::LK_Cpp, 23232 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 23233 EXPECT_EQ(FormatStyle::LK_Cpp, 23234 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 23235 EXPECT_EQ(FormatStyle::LK_ObjC, 23236 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 23237 EXPECT_EQ(FormatStyle::LK_Cpp, 23238 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 23239 EXPECT_EQ( 23240 FormatStyle::LK_Cpp, 23241 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 23242 EXPECT_EQ( 23243 FormatStyle::LK_Cpp, 23244 guessLanguage("foo.h", 23245 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 23246 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 23247 } 23248 23249 TEST_F(FormatTest, GuessLanguageWithCaret) { 23250 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 23251 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 23252 EXPECT_EQ(FormatStyle::LK_ObjC, 23253 guessLanguage("foo.h", "int(^)(char, float);")); 23254 EXPECT_EQ(FormatStyle::LK_ObjC, 23255 guessLanguage("foo.h", "int(^foo)(char, float);")); 23256 EXPECT_EQ(FormatStyle::LK_ObjC, 23257 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 23258 EXPECT_EQ(FormatStyle::LK_ObjC, 23259 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 23260 EXPECT_EQ( 23261 FormatStyle::LK_ObjC, 23262 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 23263 } 23264 23265 TEST_F(FormatTest, GuessLanguageWithPragmas) { 23266 EXPECT_EQ(FormatStyle::LK_Cpp, 23267 guessLanguage("foo.h", "__pragma(warning(disable:))")); 23268 EXPECT_EQ(FormatStyle::LK_Cpp, 23269 guessLanguage("foo.h", "#pragma(warning(disable:))")); 23270 EXPECT_EQ(FormatStyle::LK_Cpp, 23271 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 23272 } 23273 23274 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 23275 // ASM symbolic names are identifiers that must be surrounded by [] without 23276 // space in between: 23277 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 23278 23279 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 23280 verifyFormat(R"(// 23281 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 23282 )"); 23283 23284 // A list of several ASM symbolic names. 23285 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 23286 23287 // ASM symbolic names in inline ASM with inputs and outputs. 23288 verifyFormat(R"(// 23289 asm("cmoveq %1, %2, %[result]" 23290 : [result] "=r"(result) 23291 : "r"(test), "r"(new), "[result]"(old)); 23292 )"); 23293 23294 // ASM symbolic names in inline ASM with no outputs. 23295 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 23296 } 23297 23298 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 23299 EXPECT_EQ(FormatStyle::LK_Cpp, 23300 guessLanguage("foo.h", "void f() {\n" 23301 " asm (\"mov %[e], %[d]\"\n" 23302 " : [d] \"=rm\" (d)\n" 23303 " [e] \"rm\" (*e));\n" 23304 "}")); 23305 EXPECT_EQ(FormatStyle::LK_Cpp, 23306 guessLanguage("foo.h", "void f() {\n" 23307 " _asm (\"mov %[e], %[d]\"\n" 23308 " : [d] \"=rm\" (d)\n" 23309 " [e] \"rm\" (*e));\n" 23310 "}")); 23311 EXPECT_EQ(FormatStyle::LK_Cpp, 23312 guessLanguage("foo.h", "void f() {\n" 23313 " __asm (\"mov %[e], %[d]\"\n" 23314 " : [d] \"=rm\" (d)\n" 23315 " [e] \"rm\" (*e));\n" 23316 "}")); 23317 EXPECT_EQ(FormatStyle::LK_Cpp, 23318 guessLanguage("foo.h", "void f() {\n" 23319 " __asm__ (\"mov %[e], %[d]\"\n" 23320 " : [d] \"=rm\" (d)\n" 23321 " [e] \"rm\" (*e));\n" 23322 "}")); 23323 EXPECT_EQ(FormatStyle::LK_Cpp, 23324 guessLanguage("foo.h", "void f() {\n" 23325 " asm (\"mov %[e], %[d]\"\n" 23326 " : [d] \"=rm\" (d),\n" 23327 " [e] \"rm\" (*e));\n" 23328 "}")); 23329 EXPECT_EQ(FormatStyle::LK_Cpp, 23330 guessLanguage("foo.h", "void f() {\n" 23331 " asm volatile (\"mov %[e], %[d]\"\n" 23332 " : [d] \"=rm\" (d)\n" 23333 " [e] \"rm\" (*e));\n" 23334 "}")); 23335 } 23336 23337 TEST_F(FormatTest, GuessLanguageWithChildLines) { 23338 EXPECT_EQ(FormatStyle::LK_Cpp, 23339 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 23340 EXPECT_EQ(FormatStyle::LK_ObjC, 23341 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 23342 EXPECT_EQ( 23343 FormatStyle::LK_Cpp, 23344 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 23345 EXPECT_EQ( 23346 FormatStyle::LK_ObjC, 23347 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 23348 } 23349 23350 TEST_F(FormatTest, TypenameMacros) { 23351 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 23352 23353 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 23354 FormatStyle Google = getGoogleStyleWithColumns(0); 23355 Google.TypenameMacros = TypenameMacros; 23356 verifyFormat("struct foo {\n" 23357 " int bar;\n" 23358 " TAILQ_ENTRY(a) bleh;\n" 23359 "};", 23360 Google); 23361 23362 FormatStyle Macros = getLLVMStyle(); 23363 Macros.TypenameMacros = TypenameMacros; 23364 23365 verifyFormat("STACK_OF(int) a;", Macros); 23366 verifyFormat("STACK_OF(int) *a;", Macros); 23367 verifyFormat("STACK_OF(int const *) *a;", Macros); 23368 verifyFormat("STACK_OF(int *const) *a;", Macros); 23369 verifyFormat("STACK_OF(int, string) a;", Macros); 23370 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 23371 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 23372 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 23373 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 23374 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 23375 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 23376 23377 Macros.PointerAlignment = FormatStyle::PAS_Left; 23378 verifyFormat("STACK_OF(int)* a;", Macros); 23379 verifyFormat("STACK_OF(int*)* a;", Macros); 23380 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 23381 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 23382 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 23383 } 23384 23385 TEST_F(FormatTest, AtomicQualifier) { 23386 // Check that we treate _Atomic as a type and not a function call 23387 FormatStyle Google = getGoogleStyleWithColumns(0); 23388 verifyFormat("struct foo {\n" 23389 " int a1;\n" 23390 " _Atomic(a) a2;\n" 23391 " _Atomic(_Atomic(int) *const) a3;\n" 23392 "};", 23393 Google); 23394 verifyFormat("_Atomic(uint64_t) a;"); 23395 verifyFormat("_Atomic(uint64_t) *a;"); 23396 verifyFormat("_Atomic(uint64_t const *) *a;"); 23397 verifyFormat("_Atomic(uint64_t *const) *a;"); 23398 verifyFormat("_Atomic(const uint64_t *) *a;"); 23399 verifyFormat("_Atomic(uint64_t) a;"); 23400 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 23401 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 23402 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 23403 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 23404 23405 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 23406 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 23407 FormatStyle Style = getLLVMStyle(); 23408 Style.PointerAlignment = FormatStyle::PAS_Left; 23409 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 23410 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 23411 verifyFormat("_Atomic(int)* a;", Style); 23412 verifyFormat("_Atomic(int*)* a;", Style); 23413 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 23414 23415 Style.SpacesInCStyleCastParentheses = true; 23416 Style.SpacesInParentheses = false; 23417 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 23418 Style.SpacesInCStyleCastParentheses = false; 23419 Style.SpacesInParentheses = true; 23420 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 23421 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 23422 } 23423 23424 TEST_F(FormatTest, AmbersandInLamda) { 23425 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 23426 FormatStyle AlignStyle = getLLVMStyle(); 23427 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 23428 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 23429 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 23430 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 23431 } 23432 23433 TEST_F(FormatTest, SpacesInConditionalStatement) { 23434 FormatStyle Spaces = getLLVMStyle(); 23435 Spaces.IfMacros.clear(); 23436 Spaces.IfMacros.push_back("MYIF"); 23437 Spaces.SpacesInConditionalStatement = true; 23438 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 23439 verifyFormat("if ( !a )\n return;", Spaces); 23440 verifyFormat("if ( a )\n return;", Spaces); 23441 verifyFormat("if constexpr ( a )\n return;", Spaces); 23442 verifyFormat("MYIF ( a )\n return;", Spaces); 23443 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 23444 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 23445 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 23446 verifyFormat("while ( a )\n return;", Spaces); 23447 verifyFormat("while ( (a && b) )\n return;", Spaces); 23448 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 23449 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 23450 // Check that space on the left of "::" is inserted as expected at beginning 23451 // of condition. 23452 verifyFormat("while ( ::func() )\n return;", Spaces); 23453 23454 // Check impact of ControlStatementsExceptControlMacros is honored. 23455 Spaces.SpaceBeforeParens = 23456 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 23457 verifyFormat("MYIF( a )\n return;", Spaces); 23458 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 23459 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 23460 } 23461 23462 TEST_F(FormatTest, AlternativeOperators) { 23463 // Test case for ensuring alternate operators are not 23464 // combined with their right most neighbour. 23465 verifyFormat("int a and b;"); 23466 verifyFormat("int a and_eq b;"); 23467 verifyFormat("int a bitand b;"); 23468 verifyFormat("int a bitor b;"); 23469 verifyFormat("int a compl b;"); 23470 verifyFormat("int a not b;"); 23471 verifyFormat("int a not_eq b;"); 23472 verifyFormat("int a or b;"); 23473 verifyFormat("int a xor b;"); 23474 verifyFormat("int a xor_eq b;"); 23475 verifyFormat("return this not_eq bitand other;"); 23476 verifyFormat("bool operator not_eq(const X bitand other)"); 23477 23478 verifyFormat("int a and 5;"); 23479 verifyFormat("int a and_eq 5;"); 23480 verifyFormat("int a bitand 5;"); 23481 verifyFormat("int a bitor 5;"); 23482 verifyFormat("int a compl 5;"); 23483 verifyFormat("int a not 5;"); 23484 verifyFormat("int a not_eq 5;"); 23485 verifyFormat("int a or 5;"); 23486 verifyFormat("int a xor 5;"); 23487 verifyFormat("int a xor_eq 5;"); 23488 23489 verifyFormat("int a compl(5);"); 23490 verifyFormat("int a not(5);"); 23491 23492 /* FIXME handle alternate tokens 23493 * https://en.cppreference.com/w/cpp/language/operator_alternative 23494 // alternative tokens 23495 verifyFormat("compl foo();"); // ~foo(); 23496 verifyFormat("foo() <%%>;"); // foo(); 23497 verifyFormat("void foo() <%%>;"); // void foo(){} 23498 verifyFormat("int a <:1:>;"); // int a[1];[ 23499 verifyFormat("%:define ABC abc"); // #define ABC abc 23500 verifyFormat("%:%:"); // ## 23501 */ 23502 } 23503 23504 TEST_F(FormatTest, STLWhileNotDefineChed) { 23505 verifyFormat("#if defined(while)\n" 23506 "#define while EMIT WARNING C4005\n" 23507 "#endif // while"); 23508 } 23509 23510 TEST_F(FormatTest, OperatorSpacing) { 23511 FormatStyle Style = getLLVMStyle(); 23512 Style.PointerAlignment = FormatStyle::PAS_Right; 23513 verifyFormat("Foo::operator*();", Style); 23514 verifyFormat("Foo::operator void *();", Style); 23515 verifyFormat("Foo::operator void **();", Style); 23516 verifyFormat("Foo::operator void *&();", Style); 23517 verifyFormat("Foo::operator void *&&();", Style); 23518 verifyFormat("Foo::operator void const *();", Style); 23519 verifyFormat("Foo::operator void const **();", Style); 23520 verifyFormat("Foo::operator void const *&();", Style); 23521 verifyFormat("Foo::operator void const *&&();", Style); 23522 verifyFormat("Foo::operator()(void *);", Style); 23523 verifyFormat("Foo::operator*(void *);", Style); 23524 verifyFormat("Foo::operator*();", Style); 23525 verifyFormat("Foo::operator**();", Style); 23526 verifyFormat("Foo::operator&();", Style); 23527 verifyFormat("Foo::operator<int> *();", Style); 23528 verifyFormat("Foo::operator<Foo> *();", Style); 23529 verifyFormat("Foo::operator<int> **();", Style); 23530 verifyFormat("Foo::operator<Foo> **();", Style); 23531 verifyFormat("Foo::operator<int> &();", Style); 23532 verifyFormat("Foo::operator<Foo> &();", Style); 23533 verifyFormat("Foo::operator<int> &&();", Style); 23534 verifyFormat("Foo::operator<Foo> &&();", Style); 23535 verifyFormat("Foo::operator<int> *&();", Style); 23536 verifyFormat("Foo::operator<Foo> *&();", Style); 23537 verifyFormat("Foo::operator<int> *&&();", Style); 23538 verifyFormat("Foo::operator<Foo> *&&();", Style); 23539 verifyFormat("operator*(int (*)(), class Foo);", Style); 23540 23541 verifyFormat("Foo::operator&();", Style); 23542 verifyFormat("Foo::operator void &();", Style); 23543 verifyFormat("Foo::operator void const &();", Style); 23544 verifyFormat("Foo::operator()(void &);", Style); 23545 verifyFormat("Foo::operator&(void &);", Style); 23546 verifyFormat("Foo::operator&();", Style); 23547 verifyFormat("operator&(int (&)(), class Foo);", Style); 23548 verifyFormat("operator&&(int (&)(), class Foo);", Style); 23549 23550 verifyFormat("Foo::operator&&();", Style); 23551 verifyFormat("Foo::operator**();", Style); 23552 verifyFormat("Foo::operator void &&();", Style); 23553 verifyFormat("Foo::operator void const &&();", Style); 23554 verifyFormat("Foo::operator()(void &&);", Style); 23555 verifyFormat("Foo::operator&&(void &&);", Style); 23556 verifyFormat("Foo::operator&&();", Style); 23557 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 23558 verifyFormat("operator const nsTArrayRight<E> &()", Style); 23559 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 23560 Style); 23561 verifyFormat("operator void **()", Style); 23562 verifyFormat("operator const FooRight<Object> &()", Style); 23563 verifyFormat("operator const FooRight<Object> *()", Style); 23564 verifyFormat("operator const FooRight<Object> **()", Style); 23565 verifyFormat("operator const FooRight<Object> *&()", Style); 23566 verifyFormat("operator const FooRight<Object> *&&()", Style); 23567 23568 Style.PointerAlignment = FormatStyle::PAS_Left; 23569 verifyFormat("Foo::operator*();", Style); 23570 verifyFormat("Foo::operator**();", Style); 23571 verifyFormat("Foo::operator void*();", Style); 23572 verifyFormat("Foo::operator void**();", Style); 23573 verifyFormat("Foo::operator void*&();", Style); 23574 verifyFormat("Foo::operator void*&&();", Style); 23575 verifyFormat("Foo::operator void const*();", Style); 23576 verifyFormat("Foo::operator void const**();", Style); 23577 verifyFormat("Foo::operator void const*&();", Style); 23578 verifyFormat("Foo::operator void const*&&();", Style); 23579 verifyFormat("Foo::operator/*comment*/ void*();", Style); 23580 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 23581 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 23582 verifyFormat("Foo::operator()(void*);", Style); 23583 verifyFormat("Foo::operator*(void*);", Style); 23584 verifyFormat("Foo::operator*();", Style); 23585 verifyFormat("Foo::operator<int>*();", Style); 23586 verifyFormat("Foo::operator<Foo>*();", Style); 23587 verifyFormat("Foo::operator<int>**();", Style); 23588 verifyFormat("Foo::operator<Foo>**();", Style); 23589 verifyFormat("Foo::operator<Foo>*&();", Style); 23590 verifyFormat("Foo::operator<int>&();", Style); 23591 verifyFormat("Foo::operator<Foo>&();", Style); 23592 verifyFormat("Foo::operator<int>&&();", Style); 23593 verifyFormat("Foo::operator<Foo>&&();", Style); 23594 verifyFormat("Foo::operator<int>*&();", Style); 23595 verifyFormat("Foo::operator<Foo>*&();", Style); 23596 verifyFormat("operator*(int (*)(), class Foo);", Style); 23597 23598 verifyFormat("Foo::operator&();", Style); 23599 verifyFormat("Foo::operator void&();", Style); 23600 verifyFormat("Foo::operator void const&();", Style); 23601 verifyFormat("Foo::operator/*comment*/ void&();", Style); 23602 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 23603 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 23604 verifyFormat("Foo::operator()(void&);", Style); 23605 verifyFormat("Foo::operator&(void&);", Style); 23606 verifyFormat("Foo::operator&();", Style); 23607 verifyFormat("operator&(int (&)(), class Foo);", Style); 23608 verifyFormat("operator&(int (&&)(), class Foo);", Style); 23609 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 23610 23611 verifyFormat("Foo::operator&&();", Style); 23612 verifyFormat("Foo::operator void&&();", Style); 23613 verifyFormat("Foo::operator void const&&();", Style); 23614 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 23615 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 23616 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 23617 verifyFormat("Foo::operator()(void&&);", Style); 23618 verifyFormat("Foo::operator&&(void&&);", Style); 23619 verifyFormat("Foo::operator&&();", Style); 23620 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 23621 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 23622 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 23623 Style); 23624 verifyFormat("operator void**()", Style); 23625 verifyFormat("operator const FooLeft<Object>&()", Style); 23626 verifyFormat("operator const FooLeft<Object>*()", Style); 23627 verifyFormat("operator const FooLeft<Object>**()", Style); 23628 verifyFormat("operator const FooLeft<Object>*&()", Style); 23629 verifyFormat("operator const FooLeft<Object>*&&()", Style); 23630 23631 // PR45107 23632 verifyFormat("operator Vector<String>&();", Style); 23633 verifyFormat("operator const Vector<String>&();", Style); 23634 verifyFormat("operator foo::Bar*();", Style); 23635 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 23636 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 23637 Style); 23638 23639 Style.PointerAlignment = FormatStyle::PAS_Middle; 23640 verifyFormat("Foo::operator*();", Style); 23641 verifyFormat("Foo::operator void *();", Style); 23642 verifyFormat("Foo::operator()(void *);", Style); 23643 verifyFormat("Foo::operator*(void *);", Style); 23644 verifyFormat("Foo::operator*();", Style); 23645 verifyFormat("operator*(int (*)(), class Foo);", Style); 23646 23647 verifyFormat("Foo::operator&();", Style); 23648 verifyFormat("Foo::operator void &();", Style); 23649 verifyFormat("Foo::operator void const &();", Style); 23650 verifyFormat("Foo::operator()(void &);", Style); 23651 verifyFormat("Foo::operator&(void &);", Style); 23652 verifyFormat("Foo::operator&();", Style); 23653 verifyFormat("operator&(int (&)(), class Foo);", Style); 23654 23655 verifyFormat("Foo::operator&&();", Style); 23656 verifyFormat("Foo::operator void &&();", Style); 23657 verifyFormat("Foo::operator void const &&();", Style); 23658 verifyFormat("Foo::operator()(void &&);", Style); 23659 verifyFormat("Foo::operator&&(void &&);", Style); 23660 verifyFormat("Foo::operator&&();", Style); 23661 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 23662 } 23663 23664 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 23665 FormatStyle Style = getLLVMStyle(); 23666 // PR46157 23667 verifyFormat("foo(operator+, -42);", Style); 23668 verifyFormat("foo(operator++, -42);", Style); 23669 verifyFormat("foo(operator--, -42);", Style); 23670 verifyFormat("foo(-42, operator--);", Style); 23671 verifyFormat("foo(-42, operator, );", Style); 23672 verifyFormat("foo(operator, , -42);", Style); 23673 } 23674 23675 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 23676 FormatStyle Style = getLLVMStyle(); 23677 Style.WhitespaceSensitiveMacros.push_back("FOO"); 23678 23679 // Don't use the helpers here, since 'mess up' will change the whitespace 23680 // and these are all whitespace sensitive by definition 23681 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 23682 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 23683 EXPECT_EQ( 23684 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 23685 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 23686 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 23687 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 23688 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 23689 " Still=Intentional);", 23690 format("FOO(String-ized&Messy+But,: :\n" 23691 " Still=Intentional);", 23692 Style)); 23693 Style.AlignConsecutiveAssignments.Enabled = true; 23694 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 23695 " Still=Intentional);", 23696 format("FOO(String-ized=&Messy+But,: :\n" 23697 " Still=Intentional);", 23698 Style)); 23699 23700 Style.ColumnLimit = 21; 23701 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 23702 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 23703 } 23704 23705 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 23706 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't 23707 // test its interaction with line wrapping 23708 FormatStyle Style = getLLVMStyleWithColumns(80); 23709 verifyFormat("namespace {\n" 23710 "int i;\n" 23711 "int j;\n" 23712 "} // namespace", 23713 Style); 23714 23715 verifyFormat("namespace AAA {\n" 23716 "int i;\n" 23717 "int j;\n" 23718 "} // namespace AAA", 23719 Style); 23720 23721 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 23722 "int i;\n" 23723 "int j;\n" 23724 "} // namespace Averyveryveryverylongnamespace", 23725 format("namespace Averyveryveryverylongnamespace {\n" 23726 "int i;\n" 23727 "int j;\n" 23728 "}", 23729 Style)); 23730 23731 EXPECT_EQ( 23732 "namespace " 23733 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 23734 " went::mad::now {\n" 23735 "int i;\n" 23736 "int j;\n" 23737 "} // namespace\n" 23738 " // " 23739 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 23740 "went::mad::now", 23741 format("namespace " 23742 "would::it::save::you::a::lot::of::time::if_::i::" 23743 "just::gave::up::and_::went::mad::now {\n" 23744 "int i;\n" 23745 "int j;\n" 23746 "}", 23747 Style)); 23748 23749 // This used to duplicate the comment again and again on subsequent runs 23750 EXPECT_EQ( 23751 "namespace " 23752 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 23753 " went::mad::now {\n" 23754 "int i;\n" 23755 "int j;\n" 23756 "} // namespace\n" 23757 " // " 23758 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 23759 "went::mad::now", 23760 format("namespace " 23761 "would::it::save::you::a::lot::of::time::if_::i::" 23762 "just::gave::up::and_::went::mad::now {\n" 23763 "int i;\n" 23764 "int j;\n" 23765 "} // namespace\n" 23766 " // " 23767 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 23768 "and_::went::mad::now", 23769 Style)); 23770 } 23771 23772 TEST_F(FormatTest, LikelyUnlikely) { 23773 FormatStyle Style = getLLVMStyle(); 23774 23775 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23776 " return 29;\n" 23777 "}", 23778 Style); 23779 23780 verifyFormat("if (argc > 5) [[likely]] {\n" 23781 " return 29;\n" 23782 "}", 23783 Style); 23784 23785 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23786 " return 29;\n" 23787 "} else [[likely]] {\n" 23788 " return 42;\n" 23789 "}\n", 23790 Style); 23791 23792 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23793 " return 29;\n" 23794 "} else if (argc > 10) [[likely]] {\n" 23795 " return 99;\n" 23796 "} else {\n" 23797 " return 42;\n" 23798 "}\n", 23799 Style); 23800 23801 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 23802 " return 29;\n" 23803 "}", 23804 Style); 23805 23806 verifyFormat("if (argc > 5) [[unlikely]]\n" 23807 " return 29;\n", 23808 Style); 23809 verifyFormat("if (argc > 5) [[likely]]\n" 23810 " return 29;\n", 23811 Style); 23812 23813 Style.AttributeMacros.push_back("UNLIKELY"); 23814 Style.AttributeMacros.push_back("LIKELY"); 23815 verifyFormat("if (argc > 5) UNLIKELY\n" 23816 " return 29;\n", 23817 Style); 23818 23819 verifyFormat("if (argc > 5) UNLIKELY {\n" 23820 " return 29;\n" 23821 "}", 23822 Style); 23823 verifyFormat("if (argc > 5) UNLIKELY {\n" 23824 " return 29;\n" 23825 "} else [[likely]] {\n" 23826 " return 42;\n" 23827 "}\n", 23828 Style); 23829 verifyFormat("if (argc > 5) UNLIKELY {\n" 23830 " return 29;\n" 23831 "} else LIKELY {\n" 23832 " return 42;\n" 23833 "}\n", 23834 Style); 23835 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23836 " return 29;\n" 23837 "} else LIKELY {\n" 23838 " return 42;\n" 23839 "}\n", 23840 Style); 23841 } 23842 23843 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 23844 verifyFormat("Constructor()\n" 23845 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23846 " aaaa(aaaaaaaaaaaaaaaaaa, " 23847 "aaaaaaaaaaaaaaaaaat))"); 23848 verifyFormat("Constructor()\n" 23849 " : aaaaaaaaaaaaa(aaaaaa), " 23850 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 23851 23852 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 23853 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 23854 verifyFormat("Constructor()\n" 23855 " : aaaaaa(aaaaaa),\n" 23856 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23857 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 23858 StyleWithWhitespacePenalty); 23859 verifyFormat("Constructor()\n" 23860 " : aaaaaaaaaaaaa(aaaaaa), " 23861 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 23862 StyleWithWhitespacePenalty); 23863 } 23864 23865 TEST_F(FormatTest, LLVMDefaultStyle) { 23866 FormatStyle Style = getLLVMStyle(); 23867 verifyFormat("extern \"C\" {\n" 23868 "int foo();\n" 23869 "}", 23870 Style); 23871 } 23872 TEST_F(FormatTest, GNUDefaultStyle) { 23873 FormatStyle Style = getGNUStyle(); 23874 verifyFormat("extern \"C\"\n" 23875 "{\n" 23876 " int foo ();\n" 23877 "}", 23878 Style); 23879 } 23880 TEST_F(FormatTest, MozillaDefaultStyle) { 23881 FormatStyle Style = getMozillaStyle(); 23882 verifyFormat("extern \"C\"\n" 23883 "{\n" 23884 " int foo();\n" 23885 "}", 23886 Style); 23887 } 23888 TEST_F(FormatTest, GoogleDefaultStyle) { 23889 FormatStyle Style = getGoogleStyle(); 23890 verifyFormat("extern \"C\" {\n" 23891 "int foo();\n" 23892 "}", 23893 Style); 23894 } 23895 TEST_F(FormatTest, ChromiumDefaultStyle) { 23896 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 23897 verifyFormat("extern \"C\" {\n" 23898 "int foo();\n" 23899 "}", 23900 Style); 23901 } 23902 TEST_F(FormatTest, MicrosoftDefaultStyle) { 23903 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 23904 verifyFormat("extern \"C\"\n" 23905 "{\n" 23906 " int foo();\n" 23907 "}", 23908 Style); 23909 } 23910 TEST_F(FormatTest, WebKitDefaultStyle) { 23911 FormatStyle Style = getWebKitStyle(); 23912 verifyFormat("extern \"C\" {\n" 23913 "int foo();\n" 23914 "}", 23915 Style); 23916 } 23917 23918 TEST_F(FormatTest, Concepts) { 23919 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations, 23920 FormatStyle::BBCDS_Always); 23921 verifyFormat("template <typename T>\n" 23922 "concept True = true;"); 23923 23924 verifyFormat("template <typename T>\n" 23925 "concept C = ((false || foo()) && C2<T>) ||\n" 23926 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;", 23927 getLLVMStyleWithColumns(60)); 23928 23929 verifyFormat("template <typename T>\n" 23930 "concept DelayedCheck = true && requires(T t) { t.bar(); } && " 23931 "sizeof(T) <= 8;"); 23932 23933 verifyFormat("template <typename T>\n" 23934 "concept DelayedCheck = true && requires(T t) {\n" 23935 " t.bar();\n" 23936 " t.baz();\n" 23937 " } && sizeof(T) <= 8;"); 23938 23939 verifyFormat("template <typename T>\n" 23940 "concept DelayedCheck = true && requires(T t) { // Comment\n" 23941 " t.bar();\n" 23942 " t.baz();\n" 23943 " } && sizeof(T) <= 8;"); 23944 23945 verifyFormat("template <typename T>\n" 23946 "concept DelayedCheck = false || requires(T t) { t.bar(); } && " 23947 "sizeof(T) <= 8;"); 23948 23949 verifyFormat("template <typename T>\n" 23950 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } " 23951 "&& sizeof(T) <= 8;"); 23952 23953 verifyFormat( 23954 "template <typename T>\n" 23955 "concept DelayedCheck = static_cast<bool>(0) ||\n" 23956 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 23957 23958 verifyFormat("template <typename T>\n" 23959 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } " 23960 "&& sizeof(T) <= 8;"); 23961 23962 verifyFormat( 23963 "template <typename T>\n" 23964 "concept DelayedCheck = (bool)(0) ||\n" 23965 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 23966 23967 verifyFormat("template <typename T>\n" 23968 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } " 23969 "&& sizeof(T) <= 8;"); 23970 23971 verifyFormat("template <typename T>\n" 23972 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && " 23973 "sizeof(T) <= 8;"); 23974 23975 verifyFormat("template <typename T>\n" 23976 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n" 23977 " requires(T t) {\n" 23978 " t.bar();\n" 23979 " t.baz();\n" 23980 " } && sizeof(T) <= 8 && !(4 < 3);", 23981 getLLVMStyleWithColumns(60)); 23982 23983 verifyFormat("template <typename T>\n" 23984 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;"); 23985 23986 verifyFormat("template <typename T>\n" 23987 "concept C = foo();"); 23988 23989 verifyFormat("template <typename T>\n" 23990 "concept C = foo(T());"); 23991 23992 verifyFormat("template <typename T>\n" 23993 "concept C = foo(T{});"); 23994 23995 verifyFormat("template <typename T>\n" 23996 "concept Size = V<sizeof(T)>::Value > 5;"); 23997 23998 verifyFormat("template <typename T>\n" 23999 "concept True = S<T>::Value;"); 24000 24001 verifyFormat( 24002 "template <typename T>\n" 24003 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n" 24004 " sizeof(T) <= 8;"); 24005 24006 // FIXME: This is misformatted because the fake l paren starts at bool, not at 24007 // the lambda l square. 24008 verifyFormat("template <typename T>\n" 24009 "concept C = [] -> bool { return true; }() && requires(T t) { " 24010 "t.bar(); } &&\n" 24011 " sizeof(T) <= 8;"); 24012 24013 verifyFormat( 24014 "template <typename T>\n" 24015 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n" 24016 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 24017 24018 verifyFormat("template <typename T>\n" 24019 "concept C = decltype([]() { return std::true_type{}; " 24020 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;", 24021 getLLVMStyleWithColumns(120)); 24022 24023 verifyFormat("template <typename T>\n" 24024 "concept C = decltype([]() -> std::true_type { return {}; " 24025 "}())::value &&\n" 24026 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 24027 24028 verifyFormat("template <typename T>\n" 24029 "concept C = true;\n" 24030 "Foo Bar;"); 24031 24032 verifyFormat("template <typename T>\n" 24033 "concept Hashable = requires(T a) {\n" 24034 " { std::hash<T>{}(a) } -> " 24035 "std::convertible_to<std::size_t>;\n" 24036 " };"); 24037 24038 verifyFormat( 24039 "template <typename T>\n" 24040 "concept EqualityComparable = requires(T a, T b) {\n" 24041 " { a == b } -> std::same_as<bool>;\n" 24042 " };"); 24043 24044 verifyFormat( 24045 "template <typename T>\n" 24046 "concept EqualityComparable = requires(T a, T b) {\n" 24047 " { a == b } -> std::same_as<bool>;\n" 24048 " { a != b } -> std::same_as<bool>;\n" 24049 " };"); 24050 24051 verifyFormat("template <typename T>\n" 24052 "concept WeakEqualityComparable = requires(T a, T b) {\n" 24053 " { a == b };\n" 24054 " { a != b };\n" 24055 " };"); 24056 24057 verifyFormat("template <typename T>\n" 24058 "concept HasSizeT = requires { typename T::size_t; };"); 24059 24060 verifyFormat("template <typename T>\n" 24061 "concept Semiregular =\n" 24062 " DefaultConstructible<T> && CopyConstructible<T> && " 24063 "CopyAssignable<T> &&\n" 24064 " requires(T a, std::size_t n) {\n" 24065 " requires Same<T *, decltype(&a)>;\n" 24066 " { a.~T() } noexcept;\n" 24067 " requires Same<T *, decltype(new T)>;\n" 24068 " requires Same<T *, decltype(new T[n])>;\n" 24069 " { delete new T; };\n" 24070 " { delete new T[n]; };\n" 24071 " };"); 24072 24073 verifyFormat("template <typename T>\n" 24074 "concept Semiregular =\n" 24075 " requires(T a, std::size_t n) {\n" 24076 " requires Same<T *, decltype(&a)>;\n" 24077 " { a.~T() } noexcept;\n" 24078 " requires Same<T *, decltype(new T)>;\n" 24079 " requires Same<T *, decltype(new T[n])>;\n" 24080 " { delete new T; };\n" 24081 " { delete new T[n]; };\n" 24082 " { new T } -> std::same_as<T *>;\n" 24083 " } && DefaultConstructible<T> && CopyConstructible<T> && " 24084 "CopyAssignable<T>;"); 24085 24086 verifyFormat( 24087 "template <typename T>\n" 24088 "concept Semiregular =\n" 24089 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n" 24090 " requires Same<T *, decltype(&a)>;\n" 24091 " { a.~T() } noexcept;\n" 24092 " requires Same<T *, decltype(new T)>;\n" 24093 " requires Same<T *, decltype(new " 24094 "T[n])>;\n" 24095 " { delete new T; };\n" 24096 " { delete new T[n]; };\n" 24097 " } && CopyConstructible<T> && " 24098 "CopyAssignable<T>;"); 24099 24100 verifyFormat("template <typename T>\n" 24101 "concept Two = requires(T t) {\n" 24102 " { t.foo() } -> std::same_as<Bar>;\n" 24103 " } && requires(T &&t) {\n" 24104 " { t.foo() } -> std::same_as<Bar &&>;\n" 24105 " };"); 24106 24107 verifyFormat( 24108 "template <typename T>\n" 24109 "concept C = requires(T x) {\n" 24110 " { *x } -> std::convertible_to<typename T::inner>;\n" 24111 " { x + 1 } noexcept -> std::same_as<int>;\n" 24112 " { x * 1 } -> std::convertible_to<T>;\n" 24113 " };"); 24114 24115 verifyFormat( 24116 "template <typename T, typename U = T>\n" 24117 "concept Swappable = requires(T &&t, U &&u) {\n" 24118 " swap(std::forward<T>(t), std::forward<U>(u));\n" 24119 " swap(std::forward<U>(u), std::forward<T>(t));\n" 24120 " };"); 24121 24122 verifyFormat("template <typename T, typename U>\n" 24123 "concept Common = requires(T &&t, U &&u) {\n" 24124 " typename CommonType<T, U>;\n" 24125 " { CommonType<T, U>(std::forward<T>(t)) };\n" 24126 " };"); 24127 24128 verifyFormat("template <typename T, typename U>\n" 24129 "concept Common = requires(T &&t, U &&u) {\n" 24130 " typename CommonType<T, U>;\n" 24131 " { CommonType<T, U>{std::forward<T>(t)} };\n" 24132 " };"); 24133 24134 verifyFormat( 24135 "template <typename T>\n" 24136 "concept C = requires(T t) {\n" 24137 " requires Bar<T> && Foo<T>;\n" 24138 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 24139 " };"); 24140 24141 verifyFormat("template <typename T>\n" 24142 "concept HasFoo = requires(T t) {\n" 24143 " { t.foo() };\n" 24144 " t.foo();\n" 24145 " };\n" 24146 "template <typename T>\n" 24147 "concept HasBar = requires(T t) {\n" 24148 " { t.bar() };\n" 24149 " t.bar();\n" 24150 " };"); 24151 24152 verifyFormat("template <typename T>\n" 24153 "concept Large = sizeof(T) > 10;"); 24154 24155 verifyFormat("template <typename T, typename U>\n" 24156 "concept FooableWith = requires(T t, U u) {\n" 24157 " typename T::foo_type;\n" 24158 " { t.foo(u) } -> typename T::foo_type;\n" 24159 " t++;\n" 24160 " };\n" 24161 "void doFoo(FooableWith<int> auto t) { t.foo(3); }"); 24162 24163 verifyFormat("template <typename T>\n" 24164 "concept Context = is_specialization_of_v<context, T>;"); 24165 24166 verifyFormat("template <typename T>\n" 24167 "concept Node = std::is_object_v<T>;"); 24168 24169 verifyFormat("template <class T>\n" 24170 "concept integral = __is_integral(T);"); 24171 24172 verifyFormat("template <class T>\n" 24173 "concept is2D = __array_extent(T, 1) == 2;"); 24174 24175 verifyFormat("template <class T>\n" 24176 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)"); 24177 24178 verifyFormat("template <class T, class T2>\n" 24179 "concept Same = __is_same_as<T, T2>;"); 24180 24181 verifyFormat( 24182 "template <class _InIt, class _OutIt>\n" 24183 "concept _Can_reread_dest =\n" 24184 " std::forward_iterator<_OutIt> &&\n" 24185 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;"); 24186 24187 auto Style = getLLVMStyle(); 24188 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed; 24189 24190 verifyFormat( 24191 "template <typename T>\n" 24192 "concept C = requires(T t) {\n" 24193 " requires Bar<T> && Foo<T>;\n" 24194 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 24195 " };", 24196 Style); 24197 24198 verifyFormat("template <typename T>\n" 24199 "concept HasFoo = requires(T t) {\n" 24200 " { t.foo() };\n" 24201 " t.foo();\n" 24202 " };\n" 24203 "template <typename T>\n" 24204 "concept HasBar = requires(T t) {\n" 24205 " { t.bar() };\n" 24206 " t.bar();\n" 24207 " };", 24208 Style); 24209 24210 verifyFormat("template <typename T> concept True = true;", Style); 24211 24212 verifyFormat("template <typename T>\n" 24213 "concept C = decltype([]() -> std::true_type { return {}; " 24214 "}())::value &&\n" 24215 " requires(T t) { t.bar(); } && sizeof(T) <= 8;", 24216 Style); 24217 24218 verifyFormat("template <typename T>\n" 24219 "concept Semiregular =\n" 24220 " DefaultConstructible<T> && CopyConstructible<T> && " 24221 "CopyAssignable<T> &&\n" 24222 " requires(T a, std::size_t n) {\n" 24223 " requires Same<T *, decltype(&a)>;\n" 24224 " { a.~T() } noexcept;\n" 24225 " requires Same<T *, decltype(new T)>;\n" 24226 " requires Same<T *, decltype(new T[n])>;\n" 24227 " { delete new T; };\n" 24228 " { delete new T[n]; };\n" 24229 " };", 24230 Style); 24231 24232 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never; 24233 24234 verifyFormat("template <typename T> concept C =\n" 24235 " requires(T t) {\n" 24236 " requires Bar<T> && Foo<T>;\n" 24237 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 24238 " };", 24239 Style); 24240 24241 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n" 24242 " { t.foo() };\n" 24243 " t.foo();\n" 24244 " };\n" 24245 "template <typename T> concept HasBar = requires(T t) {\n" 24246 " { t.bar() };\n" 24247 " t.bar();\n" 24248 " };", 24249 Style); 24250 24251 verifyFormat("template <typename T> concept True = true;", Style); 24252 24253 verifyFormat( 24254 "template <typename T> concept C = decltype([]() -> std::true_type {\n" 24255 " return {};\n" 24256 " }())::value &&\n" 24257 " requires(T t) { t.bar(); } && " 24258 "sizeof(T) <= 8;", 24259 Style); 24260 24261 verifyFormat("template <typename T> concept Semiregular =\n" 24262 " DefaultConstructible<T> && CopyConstructible<T> && " 24263 "CopyAssignable<T> &&\n" 24264 " requires(T a, std::size_t n) {\n" 24265 " requires Same<T *, decltype(&a)>;\n" 24266 " { a.~T() } noexcept;\n" 24267 " requires Same<T *, decltype(new T)>;\n" 24268 " requires Same<T *, decltype(new T[n])>;\n" 24269 " { delete new T; };\n" 24270 " { delete new T[n]; };\n" 24271 " };", 24272 Style); 24273 24274 // The following tests are invalid C++, we just want to make sure we don't 24275 // assert. 24276 verifyFormat("template <typename T>\n" 24277 "concept C = requires C2<T>;"); 24278 24279 verifyFormat("template <typename T>\n" 24280 "concept C = 5 + 4;"); 24281 24282 verifyFormat("template <typename T>\n" 24283 "concept C =\n" 24284 "class X;"); 24285 24286 verifyFormat("template <typename T>\n" 24287 "concept C = [] && true;"); 24288 24289 verifyFormat("template <typename T>\n" 24290 "concept C = [] && requires(T t) { typename T::size_type; };"); 24291 } 24292 24293 TEST_F(FormatTest, RequiresClausesPositions) { 24294 auto Style = getLLVMStyle(); 24295 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine); 24296 EXPECT_EQ(Style.IndentRequiresClause, true); 24297 24298 verifyFormat("template <typename T>\n" 24299 " requires(Foo<T> && std::trait<T>)\n" 24300 "struct Bar;", 24301 Style); 24302 24303 verifyFormat("template <typename T>\n" 24304 " requires(Foo<T> && std::trait<T>)\n" 24305 "class Bar {\n" 24306 "public:\n" 24307 " Bar(T t);\n" 24308 " bool baz();\n" 24309 "};", 24310 Style); 24311 24312 verifyFormat( 24313 "template <typename T>\n" 24314 " requires requires(T &&t) {\n" 24315 " typename T::I;\n" 24316 " requires(F<typename T::I> && std::trait<typename T::I>);\n" 24317 " }\n" 24318 "Bar(T) -> Bar<typename T::I>;", 24319 Style); 24320 24321 verifyFormat("template <typename T>\n" 24322 " requires(Foo<T> && std::trait<T>)\n" 24323 "constexpr T MyGlobal;", 24324 Style); 24325 24326 verifyFormat("template <typename T>\n" 24327 " requires Foo<T> && requires(T t) {\n" 24328 " { t.baz() } -> std::same_as<bool>;\n" 24329 " requires std::same_as<T::Factor, int>;\n" 24330 " }\n" 24331 "inline int bar(T t) {\n" 24332 " return t.baz() ? T::Factor : 5;\n" 24333 "}", 24334 Style); 24335 24336 verifyFormat("template <typename T>\n" 24337 "inline int bar(T t)\n" 24338 " requires Foo<T> && requires(T t) {\n" 24339 " { t.baz() } -> std::same_as<bool>;\n" 24340 " requires std::same_as<T::Factor, int>;\n" 24341 " }\n" 24342 "{\n" 24343 " return t.baz() ? T::Factor : 5;\n" 24344 "}", 24345 Style); 24346 24347 verifyFormat("template <typename T>\n" 24348 " requires F<T>\n" 24349 "int bar(T t) {\n" 24350 " return 5;\n" 24351 "}", 24352 Style); 24353 24354 verifyFormat("template <typename T>\n" 24355 "int bar(T t)\n" 24356 " requires F<T>\n" 24357 "{\n" 24358 " return 5;\n" 24359 "}", 24360 Style); 24361 24362 verifyFormat("template <typename T>\n" 24363 "int bar(T t)\n" 24364 " requires F<T>;", 24365 Style); 24366 24367 Style.IndentRequiresClause = false; 24368 verifyFormat("template <typename T>\n" 24369 "requires F<T>\n" 24370 "int bar(T t) {\n" 24371 " return 5;\n" 24372 "}", 24373 Style); 24374 24375 verifyFormat("template <typename T>\n" 24376 "int bar(T t)\n" 24377 "requires F<T>\n" 24378 "{\n" 24379 " return 5;\n" 24380 "}", 24381 Style); 24382 24383 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine; 24384 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n" 24385 "template <typename T> requires Foo<T> void bar() {}\n" 24386 "template <typename T> void bar() requires Foo<T> {}\n" 24387 "template <typename T> void bar() requires Foo<T>;\n" 24388 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;", 24389 Style); 24390 24391 auto ColumnStyle = Style; 24392 ColumnStyle.ColumnLimit = 40; 24393 verifyFormat("template <typename AAAAAAA>\n" 24394 "requires Foo<T> struct Bar {};\n" 24395 "template <typename AAAAAAA>\n" 24396 "requires Foo<T> void bar() {}\n" 24397 "template <typename AAAAAAA>\n" 24398 "void bar() requires Foo<T> {}\n" 24399 "template <typename AAAAAAA>\n" 24400 "requires Foo<T> Baz(T) -> Baz<T>;", 24401 ColumnStyle); 24402 24403 verifyFormat("template <typename T>\n" 24404 "requires Foo<AAAAAAA> struct Bar {};\n" 24405 "template <typename T>\n" 24406 "requires Foo<AAAAAAA> void bar() {}\n" 24407 "template <typename T>\n" 24408 "void bar() requires Foo<AAAAAAA> {}\n" 24409 "template <typename T>\n" 24410 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;", 24411 ColumnStyle); 24412 24413 verifyFormat("template <typename AAAAAAA>\n" 24414 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24415 "struct Bar {};\n" 24416 "template <typename AAAAAAA>\n" 24417 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24418 "void bar() {}\n" 24419 "template <typename AAAAAAA>\n" 24420 "void bar()\n" 24421 " requires Foo<AAAAAAAAAAAAAAAA> {}\n" 24422 "template <typename AAAAAAA>\n" 24423 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" 24424 "template <typename AAAAAAA>\n" 24425 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24426 "Bar(T) -> Bar<T>;", 24427 ColumnStyle); 24428 24429 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; 24430 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; 24431 24432 verifyFormat("template <typename T>\n" 24433 "requires Foo<T> struct Bar {};\n" 24434 "template <typename T>\n" 24435 "requires Foo<T> void bar() {}\n" 24436 "template <typename T>\n" 24437 "void bar()\n" 24438 "requires Foo<T> {}\n" 24439 "template <typename T>\n" 24440 "void bar()\n" 24441 "requires Foo<T>;\n" 24442 "template <typename T>\n" 24443 "requires Foo<T> Bar(T) -> Bar<T>;", 24444 Style); 24445 24446 verifyFormat("template <typename AAAAAAA>\n" 24447 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24448 "struct Bar {};\n" 24449 "template <typename AAAAAAA>\n" 24450 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24451 "void bar() {}\n" 24452 "template <typename AAAAAAA>\n" 24453 "void bar()\n" 24454 "requires Foo<AAAAAAAAAAAAAAAA> {}\n" 24455 "template <typename AAAAAAA>\n" 24456 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" 24457 "template <typename AAAAAAA>\n" 24458 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24459 "Bar(T) -> Bar<T>;", 24460 ColumnStyle); 24461 24462 Style.IndentRequiresClause = true; 24463 ColumnStyle.IndentRequiresClause = true; 24464 24465 verifyFormat("template <typename T>\n" 24466 " requires Foo<T> struct Bar {};\n" 24467 "template <typename T>\n" 24468 " requires Foo<T> void bar() {}\n" 24469 "template <typename T>\n" 24470 "void bar()\n" 24471 " requires Foo<T> {}\n" 24472 "template <typename T>\n" 24473 " requires Foo<T> Bar(T) -> Bar<T>;", 24474 Style); 24475 24476 verifyFormat("template <typename AAAAAAA>\n" 24477 " requires Foo<AAAAAAAAAAAAAAAA>\n" 24478 "struct Bar {};\n" 24479 "template <typename AAAAAAA>\n" 24480 " requires Foo<AAAAAAAAAAAAAAAA>\n" 24481 "void bar() {}\n" 24482 "template <typename AAAAAAA>\n" 24483 "void bar()\n" 24484 " requires Foo<AAAAAAAAAAAAAAAA> {}\n" 24485 "template <typename AAAAAAA>\n" 24486 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n" 24487 "template <typename AAAAAAA>\n" 24488 " requires Foo<AAAAAAAAAAAAAAAA>\n" 24489 "Bar(T) -> Bar<T>;", 24490 ColumnStyle); 24491 24492 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 24493 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 24494 24495 verifyFormat("template <typename T> requires Foo<T>\n" 24496 "struct Bar {};\n" 24497 "template <typename T> requires Foo<T>\n" 24498 "void bar() {}\n" 24499 "template <typename T>\n" 24500 "void bar() requires Foo<T>\n" 24501 "{}\n" 24502 "template <typename T> void bar() requires Foo<T>;\n" 24503 "template <typename T> requires Foo<T>\n" 24504 "Bar(T) -> Bar<T>;", 24505 Style); 24506 24507 verifyFormat("template <typename AAAAAAA>\n" 24508 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24509 "struct Bar {};\n" 24510 "template <typename AAAAAAA>\n" 24511 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24512 "void bar() {}\n" 24513 "template <typename AAAAAAA>\n" 24514 "void bar()\n" 24515 " requires Foo<AAAAAAAAAAAAAAAA>\n" 24516 "{}\n" 24517 "template <typename AAAAAAA>\n" 24518 "requires Foo<AAAAAAAA>\n" 24519 "Bar(T) -> Bar<T>;\n" 24520 "template <typename AAAAAAA>\n" 24521 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24522 "Bar(T) -> Bar<T>;", 24523 ColumnStyle); 24524 } 24525 24526 TEST_F(FormatTest, RequiresClauses) { 24527 verifyFormat("struct [[nodiscard]] zero_t {\n" 24528 " template <class T>\n" 24529 " requires requires { number_zero_v<T>; }\n" 24530 " [[nodiscard]] constexpr operator T() const {\n" 24531 " return number_zero_v<T>;\n" 24532 " }\n" 24533 "};"); 24534 24535 auto Style = getLLVMStyle(); 24536 24537 verifyFormat( 24538 "template <typename T>\n" 24539 " requires is_default_constructible_v<hash<T>> and\n" 24540 " is_copy_constructible_v<hash<T>> and\n" 24541 " is_move_constructible_v<hash<T>> and\n" 24542 " is_copy_assignable_v<hash<T>> and " 24543 "is_move_assignable_v<hash<T>> and\n" 24544 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n" 24545 " is_callable_v<hash<T>(T)> and\n" 24546 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n" 24547 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n" 24548 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n" 24549 "struct S {};", 24550 Style); 24551 24552 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 24553 verifyFormat( 24554 "template <typename T>\n" 24555 " requires is_default_constructible_v<hash<T>>\n" 24556 " and is_copy_constructible_v<hash<T>>\n" 24557 " and is_move_constructible_v<hash<T>>\n" 24558 " and is_copy_assignable_v<hash<T>> and " 24559 "is_move_assignable_v<hash<T>>\n" 24560 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n" 24561 " and is_callable_v<hash<T>(T)>\n" 24562 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n" 24563 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n" 24564 " and is_same_v<size_t, decltype(hash<T>(declval<const T " 24565 "&>()))>\n" 24566 "struct S {};", 24567 Style); 24568 24569 // Not a clause, but we once hit an assert. 24570 verifyFormat("#if 0\n" 24571 "#else\n" 24572 "foo();\n" 24573 "#endif\n" 24574 "bar(requires);"); 24575 } 24576 24577 TEST_F(FormatTest, StatementAttributeLikeMacros) { 24578 FormatStyle Style = getLLVMStyle(); 24579 StringRef Source = "void Foo::slot() {\n" 24580 " unsigned char MyChar = 'x';\n" 24581 " emit signal(MyChar);\n" 24582 " Q_EMIT signal(MyChar);\n" 24583 "}"; 24584 24585 EXPECT_EQ(Source, format(Source, Style)); 24586 24587 Style.AlignConsecutiveDeclarations.Enabled = true; 24588 EXPECT_EQ("void Foo::slot() {\n" 24589 " unsigned char MyChar = 'x';\n" 24590 " emit signal(MyChar);\n" 24591 " Q_EMIT signal(MyChar);\n" 24592 "}", 24593 format(Source, Style)); 24594 24595 Style.StatementAttributeLikeMacros.push_back("emit"); 24596 EXPECT_EQ(Source, format(Source, Style)); 24597 24598 Style.StatementAttributeLikeMacros = {}; 24599 EXPECT_EQ("void Foo::slot() {\n" 24600 " unsigned char MyChar = 'x';\n" 24601 " emit signal(MyChar);\n" 24602 " Q_EMIT signal(MyChar);\n" 24603 "}", 24604 format(Source, Style)); 24605 } 24606 24607 TEST_F(FormatTest, IndentAccessModifiers) { 24608 FormatStyle Style = getLLVMStyle(); 24609 Style.IndentAccessModifiers = true; 24610 // Members are *two* levels below the record; 24611 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 24612 verifyFormat("class C {\n" 24613 " int i;\n" 24614 "};\n", 24615 Style); 24616 verifyFormat("union C {\n" 24617 " int i;\n" 24618 " unsigned u;\n" 24619 "};\n", 24620 Style); 24621 // Access modifiers should be indented one level below the record. 24622 verifyFormat("class C {\n" 24623 " public:\n" 24624 " int i;\n" 24625 "};\n", 24626 Style); 24627 verifyFormat("struct S {\n" 24628 " private:\n" 24629 " class C {\n" 24630 " int j;\n" 24631 "\n" 24632 " public:\n" 24633 " C();\n" 24634 " };\n" 24635 "\n" 24636 " public:\n" 24637 " int i;\n" 24638 "};\n", 24639 Style); 24640 // Enumerations are not records and should be unaffected. 24641 Style.AllowShortEnumsOnASingleLine = false; 24642 verifyFormat("enum class E {\n" 24643 " A,\n" 24644 " B\n" 24645 "};\n", 24646 Style); 24647 // Test with a different indentation width; 24648 // also proves that the result is Style.AccessModifierOffset agnostic. 24649 Style.IndentWidth = 3; 24650 verifyFormat("class C {\n" 24651 " public:\n" 24652 " int i;\n" 24653 "};\n", 24654 Style); 24655 } 24656 24657 TEST_F(FormatTest, LimitlessStringsAndComments) { 24658 auto Style = getLLVMStyleWithColumns(0); 24659 constexpr StringRef Code = 24660 "/**\n" 24661 " * This is a multiline comment with quite some long lines, at least for " 24662 "the LLVM Style.\n" 24663 " * We will redo this with strings and line comments. Just to check if " 24664 "everything is working.\n" 24665 " */\n" 24666 "bool foo() {\n" 24667 " /* Single line multi line comment. */\n" 24668 " const std::string String = \"This is a multiline string with quite " 24669 "some long lines, at least for the LLVM Style.\"\n" 24670 " \"We already did it with multi line " 24671 "comments, and we will do it with line comments. Just to check if " 24672 "everything is working.\";\n" 24673 " // This is a line comment (block) with quite some long lines, at " 24674 "least for the LLVM Style.\n" 24675 " // We already did this with multi line comments and strings. Just to " 24676 "check if everything is working.\n" 24677 " const std::string SmallString = \"Hello World\";\n" 24678 " // Small line comment\n" 24679 " return String.size() > SmallString.size();\n" 24680 "}"; 24681 EXPECT_EQ(Code, format(Code, Style)); 24682 } 24683 24684 TEST_F(FormatTest, FormatDecayCopy) { 24685 // error cases from unit tests 24686 verifyFormat("foo(auto())"); 24687 verifyFormat("foo(auto{})"); 24688 verifyFormat("foo(auto({}))"); 24689 verifyFormat("foo(auto{{}})"); 24690 24691 verifyFormat("foo(auto(1))"); 24692 verifyFormat("foo(auto{1})"); 24693 verifyFormat("foo(new auto(1))"); 24694 verifyFormat("foo(new auto{1})"); 24695 verifyFormat("decltype(auto(1)) x;"); 24696 verifyFormat("decltype(auto{1}) x;"); 24697 verifyFormat("auto(x);"); 24698 verifyFormat("auto{x};"); 24699 verifyFormat("new auto{x};"); 24700 verifyFormat("auto{x} = y;"); 24701 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 24702 // the user's own fault 24703 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 24704 // clearly the user's own fault 24705 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 24706 } 24707 24708 TEST_F(FormatTest, Cpp20ModulesSupport) { 24709 FormatStyle Style = getLLVMStyle(); 24710 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 24711 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 24712 24713 verifyFormat("export import foo;", Style); 24714 verifyFormat("export import foo:bar;", Style); 24715 verifyFormat("export import foo.bar;", Style); 24716 verifyFormat("export import foo.bar:baz;", Style); 24717 verifyFormat("export import :bar;", Style); 24718 verifyFormat("export module foo:bar;", Style); 24719 verifyFormat("export module foo;", Style); 24720 verifyFormat("export module foo.bar;", Style); 24721 verifyFormat("export module foo.bar:baz;", Style); 24722 verifyFormat("export import <string_view>;", Style); 24723 24724 verifyFormat("export type_name var;", Style); 24725 verifyFormat("template <class T> export using A = B<T>;", Style); 24726 verifyFormat("export using A = B;", Style); 24727 verifyFormat("export int func() {\n" 24728 " foo();\n" 24729 "}", 24730 Style); 24731 verifyFormat("export struct {\n" 24732 " int foo;\n" 24733 "};", 24734 Style); 24735 verifyFormat("export {\n" 24736 " int foo;\n" 24737 "};", 24738 Style); 24739 verifyFormat("export export char const *hello() { return \"hello\"; }"); 24740 24741 verifyFormat("import bar;", Style); 24742 verifyFormat("import foo.bar;", Style); 24743 verifyFormat("import foo:bar;", Style); 24744 verifyFormat("import :bar;", Style); 24745 verifyFormat("import <ctime>;", Style); 24746 verifyFormat("import \"header\";", Style); 24747 24748 verifyFormat("module foo;", Style); 24749 verifyFormat("module foo:bar;", Style); 24750 verifyFormat("module foo.bar;", Style); 24751 verifyFormat("module;", Style); 24752 24753 verifyFormat("export namespace hi {\n" 24754 "const char *sayhi();\n" 24755 "}", 24756 Style); 24757 24758 verifyFormat("module :private;", Style); 24759 verifyFormat("import <foo/bar.h>;", Style); 24760 verifyFormat("import foo...bar;", Style); 24761 verifyFormat("import ..........;", Style); 24762 verifyFormat("module foo:private;", Style); 24763 verifyFormat("import a", Style); 24764 verifyFormat("module a", Style); 24765 verifyFormat("export import a", Style); 24766 verifyFormat("export module a", Style); 24767 24768 verifyFormat("import", Style); 24769 verifyFormat("module", Style); 24770 verifyFormat("export", Style); 24771 } 24772 24773 TEST_F(FormatTest, CoroutineForCoawait) { 24774 FormatStyle Style = getLLVMStyle(); 24775 verifyFormat("for co_await (auto x : range())\n ;"); 24776 verifyFormat("for (auto i : arr) {\n" 24777 "}", 24778 Style); 24779 verifyFormat("for co_await (auto i : arr) {\n" 24780 "}", 24781 Style); 24782 verifyFormat("for co_await (auto i : foo(T{})) {\n" 24783 "}", 24784 Style); 24785 } 24786 24787 TEST_F(FormatTest, CoroutineCoAwait) { 24788 verifyFormat("int x = co_await foo();"); 24789 verifyFormat("int x = (co_await foo());"); 24790 verifyFormat("co_await (42);"); 24791 verifyFormat("void operator co_await(int);"); 24792 verifyFormat("void operator co_await(a);"); 24793 verifyFormat("co_await a;"); 24794 verifyFormat("co_await missing_await_resume{};"); 24795 verifyFormat("co_await a; // comment"); 24796 verifyFormat("void test0() { co_await a; }"); 24797 verifyFormat("co_await co_await co_await foo();"); 24798 verifyFormat("co_await foo().bar();"); 24799 verifyFormat("co_await [this]() -> Task { co_return x; }"); 24800 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 24801 "foo(); }(x, y);"); 24802 24803 FormatStyle Style = getLLVMStyleWithColumns(40); 24804 verifyFormat("co_await [this](int a, int b) -> Task {\n" 24805 " co_return co_await foo();\n" 24806 "}(x, y);", 24807 Style); 24808 verifyFormat("co_await;"); 24809 } 24810 24811 TEST_F(FormatTest, CoroutineCoYield) { 24812 verifyFormat("int x = co_yield foo();"); 24813 verifyFormat("int x = (co_yield foo());"); 24814 verifyFormat("co_yield (42);"); 24815 verifyFormat("co_yield {42};"); 24816 verifyFormat("co_yield 42;"); 24817 verifyFormat("co_yield n++;"); 24818 verifyFormat("co_yield ++n;"); 24819 verifyFormat("co_yield;"); 24820 } 24821 24822 TEST_F(FormatTest, CoroutineCoReturn) { 24823 verifyFormat("co_return (42);"); 24824 verifyFormat("co_return;"); 24825 verifyFormat("co_return {};"); 24826 verifyFormat("co_return x;"); 24827 verifyFormat("co_return co_await foo();"); 24828 verifyFormat("co_return co_yield foo();"); 24829 } 24830 24831 TEST_F(FormatTest, EmptyShortBlock) { 24832 auto Style = getLLVMStyle(); 24833 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 24834 24835 verifyFormat("try {\n" 24836 " doA();\n" 24837 "} catch (Exception &e) {\n" 24838 " e.printStackTrace();\n" 24839 "}\n", 24840 Style); 24841 24842 verifyFormat("try {\n" 24843 " doA();\n" 24844 "} catch (Exception &e) {}\n", 24845 Style); 24846 } 24847 24848 TEST_F(FormatTest, ShortTemplatedArgumentLists) { 24849 auto Style = getLLVMStyle(); 24850 24851 verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style); 24852 verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style); 24853 verifyFormat("struct Y : X<[] { return 0; }> {};", Style); 24854 verifyFormat("struct Y<[] { return 0; }> {};", Style); 24855 24856 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style); 24857 verifyFormat("template <int N> struct Foo<char[N]> {};", Style); 24858 } 24859 24860 TEST_F(FormatTest, InsertBraces) { 24861 FormatStyle Style = getLLVMStyle(); 24862 Style.InsertBraces = true; 24863 24864 verifyFormat("// clang-format off\n" 24865 "// comment\n" 24866 "if (a) f();\n" 24867 "// clang-format on\n" 24868 "if (b) {\n" 24869 " g();\n" 24870 "}", 24871 "// clang-format off\n" 24872 "// comment\n" 24873 "if (a) f();\n" 24874 "// clang-format on\n" 24875 "if (b) g();", 24876 Style); 24877 24878 verifyFormat("if (a) {\n" 24879 " switch (b) {\n" 24880 " case 1:\n" 24881 " c = 0;\n" 24882 " break;\n" 24883 " default:\n" 24884 " c = 1;\n" 24885 " }\n" 24886 "}", 24887 "if (a)\n" 24888 " switch (b) {\n" 24889 " case 1:\n" 24890 " c = 0;\n" 24891 " break;\n" 24892 " default:\n" 24893 " c = 1;\n" 24894 " }", 24895 Style); 24896 24897 verifyFormat("for (auto node : nodes) {\n" 24898 " if (node) {\n" 24899 " break;\n" 24900 " }\n" 24901 "}", 24902 "for (auto node : nodes)\n" 24903 " if (node)\n" 24904 " break;", 24905 Style); 24906 24907 verifyFormat("for (auto node : nodes) {\n" 24908 " if (node)\n" 24909 "}", 24910 "for (auto node : nodes)\n" 24911 " if (node)", 24912 Style); 24913 24914 verifyFormat("do {\n" 24915 " --a;\n" 24916 "} while (a);", 24917 "do\n" 24918 " --a;\n" 24919 "while (a);", 24920 Style); 24921 24922 verifyFormat("if (i) {\n" 24923 " ++i;\n" 24924 "} else {\n" 24925 " --i;\n" 24926 "}", 24927 "if (i)\n" 24928 " ++i;\n" 24929 "else {\n" 24930 " --i;\n" 24931 "}", 24932 Style); 24933 24934 verifyFormat("void f() {\n" 24935 " while (j--) {\n" 24936 " while (i) {\n" 24937 " --i;\n" 24938 " }\n" 24939 " }\n" 24940 "}", 24941 "void f() {\n" 24942 " while (j--)\n" 24943 " while (i)\n" 24944 " --i;\n" 24945 "}", 24946 Style); 24947 24948 verifyFormat("f({\n" 24949 " if (a) {\n" 24950 " g();\n" 24951 " }\n" 24952 "});", 24953 "f({\n" 24954 " if (a)\n" 24955 " g();\n" 24956 "});", 24957 Style); 24958 24959 verifyFormat("if (a) {\n" 24960 " f();\n" 24961 "} else if (b) {\n" 24962 " g();\n" 24963 "} else {\n" 24964 " h();\n" 24965 "}", 24966 "if (a)\n" 24967 " f();\n" 24968 "else if (b)\n" 24969 " g();\n" 24970 "else\n" 24971 " h();", 24972 Style); 24973 24974 verifyFormat("if (a) {\n" 24975 " f();\n" 24976 "}\n" 24977 "// comment\n" 24978 "/* comment */", 24979 "if (a)\n" 24980 " f();\n" 24981 "// comment\n" 24982 "/* comment */", 24983 Style); 24984 24985 verifyFormat("if (a) {\n" 24986 " // foo\n" 24987 " // bar\n" 24988 " f();\n" 24989 "}", 24990 "if (a)\n" 24991 " // foo\n" 24992 " // bar\n" 24993 " f();", 24994 Style); 24995 24996 verifyFormat("if (a) { // comment\n" 24997 " // comment\n" 24998 " f();\n" 24999 "}", 25000 "if (a) // comment\n" 25001 " // comment\n" 25002 " f();", 25003 Style); 25004 25005 verifyFormat("if (a) {\n" 25006 " f(); // comment\n" 25007 "}", 25008 "if (a)\n" 25009 " f(); // comment", 25010 Style); 25011 25012 verifyFormat("if (a) {\n" 25013 " f();\n" 25014 "}\n" 25015 "#undef A\n" 25016 "#undef B", 25017 "if (a)\n" 25018 " f();\n" 25019 "#undef A\n" 25020 "#undef B", 25021 Style); 25022 25023 verifyFormat("if (a)\n" 25024 "#ifdef A\n" 25025 " f();\n" 25026 "#else\n" 25027 " g();\n" 25028 "#endif", 25029 Style); 25030 25031 verifyFormat("#if 0\n" 25032 "#elif 1\n" 25033 "#endif\n" 25034 "void f() {\n" 25035 " if (a) {\n" 25036 " g();\n" 25037 " }\n" 25038 "}", 25039 "#if 0\n" 25040 "#elif 1\n" 25041 "#endif\n" 25042 "void f() {\n" 25043 " if (a) g();\n" 25044 "}", 25045 Style); 25046 25047 Style.ColumnLimit = 15; 25048 25049 verifyFormat("#define A \\\n" 25050 " if (a) \\\n" 25051 " f();", 25052 Style); 25053 25054 verifyFormat("if (a + b >\n" 25055 " c) {\n" 25056 " f();\n" 25057 "}", 25058 "if (a + b > c)\n" 25059 " f();", 25060 Style); 25061 } 25062 25063 TEST_F(FormatTest, RemoveBraces) { 25064 FormatStyle Style = getLLVMStyle(); 25065 Style.RemoveBracesLLVM = true; 25066 25067 // The following eight test cases are fully-braced versions of the examples at 25068 // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single- 25069 // statement-bodies-of-if-else-loop-statements". 25070 25071 // 1. Omit the braces, since the body is simple and clearly associated with 25072 // the if. 25073 verifyFormat("if (isa<FunctionDecl>(D))\n" 25074 " handleFunctionDecl(D);\n" 25075 "else if (isa<VarDecl>(D))\n" 25076 " handleVarDecl(D);", 25077 "if (isa<FunctionDecl>(D)) {\n" 25078 " handleFunctionDecl(D);\n" 25079 "} else if (isa<VarDecl>(D)) {\n" 25080 " handleVarDecl(D);\n" 25081 "}", 25082 Style); 25083 25084 // 2. Here we document the condition itself and not the body. 25085 verifyFormat("if (isa<VarDecl>(D)) {\n" 25086 " // It is necessary that we explain the situation with this\n" 25087 " // surprisingly long comment, so it would be unclear\n" 25088 " // without the braces whether the following statement is in\n" 25089 " // the scope of the `if`.\n" 25090 " // Because the condition is documented, we can't really\n" 25091 " // hoist this comment that applies to the body above the\n" 25092 " // if.\n" 25093 " handleOtherDecl(D);\n" 25094 "}", 25095 Style); 25096 25097 // 3. Use braces on the outer `if` to avoid a potential dangling else 25098 // situation. 25099 verifyFormat("if (isa<VarDecl>(D)) {\n" 25100 " for (auto *A : D.attrs())\n" 25101 " if (shouldProcessAttr(A))\n" 25102 " handleAttr(A);\n" 25103 "}", 25104 "if (isa<VarDecl>(D)) {\n" 25105 " for (auto *A : D.attrs()) {\n" 25106 " if (shouldProcessAttr(A)) {\n" 25107 " handleAttr(A);\n" 25108 " }\n" 25109 " }\n" 25110 "}", 25111 Style); 25112 25113 // 4. Use braces for the `if` block to keep it uniform with the else block. 25114 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 25115 " handleFunctionDecl(D);\n" 25116 "} else {\n" 25117 " // In this else case, it is necessary that we explain the\n" 25118 " // situation with this surprisingly long comment, so it\n" 25119 " // would be unclear without the braces whether the\n" 25120 " // following statement is in the scope of the `if`.\n" 25121 " handleOtherDecl(D);\n" 25122 "}", 25123 Style); 25124 25125 // 5. This should also omit braces. The `for` loop contains only a single 25126 // statement, so it shouldn't have braces. The `if` also only contains a 25127 // single simple statement (the for loop), so it also should omit braces. 25128 verifyFormat("if (isa<FunctionDecl>(D))\n" 25129 " for (auto *A : D.attrs())\n" 25130 " handleAttr(A);", 25131 "if (isa<FunctionDecl>(D)) {\n" 25132 " for (auto *A : D.attrs()) {\n" 25133 " handleAttr(A);\n" 25134 " }\n" 25135 "}", 25136 Style); 25137 25138 // 6. Use braces for the outer `if` since the nested `for` is braced. 25139 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 25140 " for (auto *A : D.attrs()) {\n" 25141 " // In this for loop body, it is necessary that we explain\n" 25142 " // the situation with this surprisingly long comment,\n" 25143 " // forcing braces on the `for` block.\n" 25144 " handleAttr(A);\n" 25145 " }\n" 25146 "}", 25147 Style); 25148 25149 // 7. Use braces on the outer block because there are more than two levels of 25150 // nesting. 25151 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 25152 " for (auto *A : D.attrs())\n" 25153 " for (ssize_t i : llvm::seq<ssize_t>(count))\n" 25154 " handleAttrOnDecl(D, A, i);\n" 25155 "}", 25156 "if (isa<FunctionDecl>(D)) {\n" 25157 " for (auto *A : D.attrs()) {\n" 25158 " for (ssize_t i : llvm::seq<ssize_t>(count)) {\n" 25159 " handleAttrOnDecl(D, A, i);\n" 25160 " }\n" 25161 " }\n" 25162 "}", 25163 Style); 25164 25165 // 8. Use braces on the outer block because of a nested `if`, otherwise the 25166 // compiler would warn: `add explicit braces to avoid dangling else` 25167 verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 25168 " if (shouldProcess(D))\n" 25169 " handleVarDecl(D);\n" 25170 " else\n" 25171 " markAsIgnored(D);\n" 25172 "}", 25173 "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 25174 " if (shouldProcess(D)) {\n" 25175 " handleVarDecl(D);\n" 25176 " } else {\n" 25177 " markAsIgnored(D);\n" 25178 " }\n" 25179 "}", 25180 Style); 25181 25182 verifyFormat("// clang-format off\n" 25183 "// comment\n" 25184 "while (i > 0) { --i; }\n" 25185 "// clang-format on\n" 25186 "while (j < 0)\n" 25187 " ++j;", 25188 "// clang-format off\n" 25189 "// comment\n" 25190 "while (i > 0) { --i; }\n" 25191 "// clang-format on\n" 25192 "while (j < 0) { ++j; }", 25193 Style); 25194 25195 verifyFormat("if (a)\n" 25196 " b; // comment\n" 25197 "else if (c)\n" 25198 " d; /* comment */\n" 25199 "else\n" 25200 " e;", 25201 "if (a) {\n" 25202 " b; // comment\n" 25203 "} else if (c) {\n" 25204 " d; /* comment */\n" 25205 "} else {\n" 25206 " e;\n" 25207 "}", 25208 Style); 25209 25210 verifyFormat("if (a) {\n" 25211 " b;\n" 25212 " c;\n" 25213 "} else if (d) {\n" 25214 " e;\n" 25215 "}", 25216 Style); 25217 25218 verifyFormat("if (a) {\n" 25219 "#undef NDEBUG\n" 25220 " b;\n" 25221 "} else {\n" 25222 " c;\n" 25223 "}", 25224 Style); 25225 25226 verifyFormat("if (a) {\n" 25227 " // comment\n" 25228 "} else if (b) {\n" 25229 " c;\n" 25230 "}", 25231 Style); 25232 25233 verifyFormat("if (a) {\n" 25234 " b;\n" 25235 "} else {\n" 25236 " { c; }\n" 25237 "}", 25238 Style); 25239 25240 verifyFormat("if (a) {\n" 25241 " if (b) // comment\n" 25242 " c;\n" 25243 "} else if (d) {\n" 25244 " e;\n" 25245 "}", 25246 "if (a) {\n" 25247 " if (b) { // comment\n" 25248 " c;\n" 25249 " }\n" 25250 "} else if (d) {\n" 25251 " e;\n" 25252 "}", 25253 Style); 25254 25255 verifyFormat("if (a) {\n" 25256 " if (b) {\n" 25257 " c;\n" 25258 " // comment\n" 25259 " } else if (d) {\n" 25260 " e;\n" 25261 " }\n" 25262 "}", 25263 Style); 25264 25265 verifyFormat("if (a) {\n" 25266 " if (b)\n" 25267 " c;\n" 25268 "}", 25269 "if (a) {\n" 25270 " if (b) {\n" 25271 " c;\n" 25272 " }\n" 25273 "}", 25274 Style); 25275 25276 verifyFormat("if (a)\n" 25277 " if (b)\n" 25278 " c;\n" 25279 " else\n" 25280 " d;\n" 25281 "else\n" 25282 " e;", 25283 "if (a) {\n" 25284 " if (b) {\n" 25285 " c;\n" 25286 " } else {\n" 25287 " d;\n" 25288 " }\n" 25289 "} else {\n" 25290 " e;\n" 25291 "}", 25292 Style); 25293 25294 verifyFormat("if (a) {\n" 25295 " // comment\n" 25296 " if (b)\n" 25297 " c;\n" 25298 " else if (d)\n" 25299 " e;\n" 25300 "} else {\n" 25301 " g;\n" 25302 "}", 25303 "if (a) {\n" 25304 " // comment\n" 25305 " if (b) {\n" 25306 " c;\n" 25307 " } else if (d) {\n" 25308 " e;\n" 25309 " }\n" 25310 "} else {\n" 25311 " g;\n" 25312 "}", 25313 Style); 25314 25315 verifyFormat("if (a)\n" 25316 " b;\n" 25317 "else if (c)\n" 25318 " d;\n" 25319 "else\n" 25320 " e;", 25321 "if (a) {\n" 25322 " b;\n" 25323 "} else {\n" 25324 " if (c) {\n" 25325 " d;\n" 25326 " } else {\n" 25327 " e;\n" 25328 " }\n" 25329 "}", 25330 Style); 25331 25332 verifyFormat("if (a) {\n" 25333 " if (b)\n" 25334 " c;\n" 25335 " else if (d)\n" 25336 " e;\n" 25337 "} else {\n" 25338 " g;\n" 25339 "}", 25340 "if (a) {\n" 25341 " if (b)\n" 25342 " c;\n" 25343 " else {\n" 25344 " if (d)\n" 25345 " e;\n" 25346 " }\n" 25347 "} else {\n" 25348 " g;\n" 25349 "}", 25350 Style); 25351 25352 verifyFormat("if (a)\n" 25353 " b;\n" 25354 "else if (c)\n" 25355 " while (d)\n" 25356 " e;\n" 25357 "// comment", 25358 "if (a)\n" 25359 "{\n" 25360 " b;\n" 25361 "} else if (c) {\n" 25362 " while (d) {\n" 25363 " e;\n" 25364 " }\n" 25365 "}\n" 25366 "// comment", 25367 Style); 25368 25369 verifyFormat("if (a) {\n" 25370 " b;\n" 25371 "} else if (c) {\n" 25372 " d;\n" 25373 "} else {\n" 25374 " e;\n" 25375 " g;\n" 25376 "}", 25377 Style); 25378 25379 verifyFormat("if (a) {\n" 25380 " b;\n" 25381 "} else if (c) {\n" 25382 " d;\n" 25383 "} else {\n" 25384 " e;\n" 25385 "} // comment", 25386 Style); 25387 25388 verifyFormat("int abs = [](int i) {\n" 25389 " if (i >= 0)\n" 25390 " return i;\n" 25391 " return -i;\n" 25392 "};", 25393 "int abs = [](int i) {\n" 25394 " if (i >= 0) {\n" 25395 " return i;\n" 25396 " }\n" 25397 " return -i;\n" 25398 "};", 25399 Style); 25400 25401 verifyFormat("if (a)\n" 25402 " foo();\n" 25403 "else\n" 25404 " bar();", 25405 "if (a)\n" 25406 "{\n" 25407 " foo();\n" 25408 "}\n" 25409 "else\n" 25410 "{\n" 25411 " bar();\n" 25412 "}", 25413 Style); 25414 25415 verifyFormat("if (a)\n" 25416 " foo();\n" 25417 "// comment\n" 25418 "else\n" 25419 " bar();", 25420 "if (a) {\n" 25421 " foo();\n" 25422 "}\n" 25423 "// comment\n" 25424 "else {\n" 25425 " bar();\n" 25426 "}", 25427 Style); 25428 25429 verifyFormat("if (a) {\n" 25430 "Label:\n" 25431 "}", 25432 Style); 25433 25434 verifyFormat("if (a) {\n" 25435 "Label:\n" 25436 " f();\n" 25437 "}", 25438 Style); 25439 25440 verifyFormat("if (a) {\n" 25441 " f();\n" 25442 "Label:\n" 25443 "}", 25444 Style); 25445 25446 verifyFormat("if consteval {\n" 25447 " f();\n" 25448 "} else {\n" 25449 " g();\n" 25450 "}", 25451 Style); 25452 25453 verifyFormat("if not consteval {\n" 25454 " f();\n" 25455 "} else if (a) {\n" 25456 " g();\n" 25457 "}", 25458 Style); 25459 25460 verifyFormat("if !consteval {\n" 25461 " g();\n" 25462 "}", 25463 Style); 25464 25465 Style.ColumnLimit = 65; 25466 verifyFormat("if (condition) {\n" 25467 " ff(Indices,\n" 25468 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 25469 "} else {\n" 25470 " ff(Indices,\n" 25471 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 25472 "}", 25473 Style); 25474 25475 Style.ColumnLimit = 20; 25476 25477 verifyFormat("int ab = [](int i) {\n" 25478 " if (i > 0) {\n" 25479 " i = 12345678 -\n" 25480 " i;\n" 25481 " }\n" 25482 " return i;\n" 25483 "};", 25484 Style); 25485 25486 verifyFormat("if (a) {\n" 25487 " b = c + // 1 -\n" 25488 " d;\n" 25489 "}", 25490 Style); 25491 25492 verifyFormat("if (a) {\n" 25493 " b = c >= 0 ? d\n" 25494 " : e;\n" 25495 "}", 25496 "if (a) {\n" 25497 " b = c >= 0 ? d : e;\n" 25498 "}", 25499 Style); 25500 25501 verifyFormat("if (a)\n" 25502 " b = c > 0 ? d : e;", 25503 "if (a) {\n" 25504 " b = c > 0 ? d : e;\n" 25505 "}", 25506 Style); 25507 25508 verifyFormat("if (-b >=\n" 25509 " c) { // Keep.\n" 25510 " foo();\n" 25511 "} else {\n" 25512 " bar();\n" 25513 "}", 25514 "if (-b >= c) { // Keep.\n" 25515 " foo();\n" 25516 "} else {\n" 25517 " bar();\n" 25518 "}", 25519 Style); 25520 25521 verifyFormat("if (a) /* Remove. */\n" 25522 " f();\n" 25523 "else\n" 25524 " g();", 25525 "if (a) <% /* Remove. */\n" 25526 " f();\n" 25527 "%> else <%\n" 25528 " g();\n" 25529 "%>", 25530 Style); 25531 25532 verifyFormat("while (\n" 25533 " !i--) <% // Keep.\n" 25534 " foo();\n" 25535 "%>", 25536 "while (!i--) <% // Keep.\n" 25537 " foo();\n" 25538 "%>", 25539 Style); 25540 25541 verifyFormat("for (int &i : chars)\n" 25542 " ++i;", 25543 "for (int &i :\n" 25544 " chars) {\n" 25545 " ++i;\n" 25546 "}", 25547 Style); 25548 25549 Style.ColumnLimit = 0; 25550 verifyFormat("if (a)\n" 25551 " b234567890223456789032345678904234567890 = " 25552 "c234567890223456789032345678904234567890;", 25553 "if (a) {\n" 25554 " b234567890223456789032345678904234567890 = " 25555 "c234567890223456789032345678904234567890;\n" 25556 "}", 25557 Style); 25558 25559 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 25560 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 25561 Style.BraceWrapping.BeforeElse = true; 25562 25563 Style.ColumnLimit = 65; 25564 25565 verifyFormat("if (condition)\n" 25566 "{\n" 25567 " ff(Indices,\n" 25568 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 25569 "}\n" 25570 "else\n" 25571 "{\n" 25572 " ff(Indices,\n" 25573 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 25574 "}", 25575 "if (condition) {\n" 25576 " ff(Indices,\n" 25577 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 25578 "} else {\n" 25579 " ff(Indices,\n" 25580 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 25581 "}", 25582 Style); 25583 25584 verifyFormat("if (a)\n" 25585 "{ //\n" 25586 " foo();\n" 25587 "}", 25588 "if (a) { //\n" 25589 " foo();\n" 25590 "}", 25591 Style); 25592 25593 Style.ColumnLimit = 20; 25594 25595 verifyFormat("int ab = [](int i) {\n" 25596 " if (i > 0)\n" 25597 " {\n" 25598 " i = 12345678 -\n" 25599 " i;\n" 25600 " }\n" 25601 " return i;\n" 25602 "};", 25603 "int ab = [](int i) {\n" 25604 " if (i > 0) {\n" 25605 " i = 12345678 -\n" 25606 " i;\n" 25607 " }\n" 25608 " return i;\n" 25609 "};", 25610 Style); 25611 25612 verifyFormat("if (a)\n" 25613 "{\n" 25614 " b = c + // 1 -\n" 25615 " d;\n" 25616 "}", 25617 "if (a) {\n" 25618 " b = c + // 1 -\n" 25619 " d;\n" 25620 "}", 25621 Style); 25622 25623 verifyFormat("if (a)\n" 25624 "{\n" 25625 " b = c >= 0 ? d\n" 25626 " : e;\n" 25627 "}", 25628 "if (a) {\n" 25629 " b = c >= 0 ? d : e;\n" 25630 "}", 25631 Style); 25632 25633 verifyFormat("if (a)\n" 25634 " b = c > 0 ? d : e;", 25635 "if (a)\n" 25636 "{\n" 25637 " b = c > 0 ? d : e;\n" 25638 "}", 25639 Style); 25640 25641 verifyFormat("if (foo + bar <=\n" 25642 " baz)\n" 25643 "{\n" 25644 " func(arg1, arg2);\n" 25645 "}", 25646 "if (foo + bar <= baz) {\n" 25647 " func(arg1, arg2);\n" 25648 "}", 25649 Style); 25650 25651 verifyFormat("if (foo + bar < baz)\n" 25652 " func(arg1, arg2);\n" 25653 "else\n" 25654 " func();", 25655 "if (foo + bar < baz)\n" 25656 "<%\n" 25657 " func(arg1, arg2);\n" 25658 "%>\n" 25659 "else\n" 25660 "<%\n" 25661 " func();\n" 25662 "%>", 25663 Style); 25664 25665 verifyFormat("while (i--)\n" 25666 "<% // Keep.\n" 25667 " foo();\n" 25668 "%>", 25669 "while (i--) <% // Keep.\n" 25670 " foo();\n" 25671 "%>", 25672 Style); 25673 25674 verifyFormat("for (int &i : chars)\n" 25675 " ++i;", 25676 "for (int &i : chars)\n" 25677 "{\n" 25678 " ++i;\n" 25679 "}", 25680 Style); 25681 } 25682 25683 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { 25684 auto Style = getLLVMStyle(); 25685 25686 StringRef Short = "functionCall(paramA, paramB, paramC);\n" 25687 "void functionDecl(int a, int b, int c);"; 25688 25689 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, " 25690 "paramF, paramG, paramH, paramI);\n" 25691 "void functionDecl(int argumentA, int argumentB, int " 25692 "argumentC, int argumentD, int argumentE);"; 25693 25694 verifyFormat(Short, Style); 25695 25696 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, " 25697 "paramF, paramG, paramH,\n" 25698 " paramI);\n" 25699 "void functionDecl(int argumentA, int argumentB, int " 25700 "argumentC, int argumentD,\n" 25701 " int argumentE);"; 25702 25703 verifyFormat(NoBreak, Medium, Style); 25704 verifyFormat(NoBreak, 25705 "functionCall(\n" 25706 " paramA,\n" 25707 " paramB,\n" 25708 " paramC,\n" 25709 " paramD,\n" 25710 " paramE,\n" 25711 " paramF,\n" 25712 " paramG,\n" 25713 " paramH,\n" 25714 " paramI\n" 25715 ");\n" 25716 "void functionDecl(\n" 25717 " int argumentA,\n" 25718 " int argumentB,\n" 25719 " int argumentC,\n" 25720 " int argumentD,\n" 25721 " int argumentE\n" 25722 ");", 25723 Style); 25724 25725 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n" 25726 " nestedLongFunctionCall(argument1, " 25727 "argument2, argument3,\n" 25728 " argument4, " 25729 "argument5));", 25730 Style); 25731 25732 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 25733 25734 verifyFormat(Short, Style); 25735 verifyFormat( 25736 "functionCall(\n" 25737 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 25738 "paramI\n" 25739 ");\n" 25740 "void functionDecl(\n" 25741 " int argumentA, int argumentB, int argumentC, int argumentD, int " 25742 "argumentE\n" 25743 ");", 25744 Medium, Style); 25745 25746 Style.AllowAllArgumentsOnNextLine = false; 25747 Style.AllowAllParametersOfDeclarationOnNextLine = false; 25748 25749 verifyFormat(Short, Style); 25750 verifyFormat( 25751 "functionCall(\n" 25752 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 25753 "paramI\n" 25754 ");\n" 25755 "void functionDecl(\n" 25756 " int argumentA, int argumentB, int argumentC, int argumentD, int " 25757 "argumentE\n" 25758 ");", 25759 Medium, Style); 25760 25761 Style.BinPackArguments = false; 25762 Style.BinPackParameters = false; 25763 25764 verifyFormat(Short, Style); 25765 25766 verifyFormat("functionCall(\n" 25767 " paramA,\n" 25768 " paramB,\n" 25769 " paramC,\n" 25770 " paramD,\n" 25771 " paramE,\n" 25772 " paramF,\n" 25773 " paramG,\n" 25774 " paramH,\n" 25775 " paramI\n" 25776 ");\n" 25777 "void functionDecl(\n" 25778 " int argumentA,\n" 25779 " int argumentB,\n" 25780 " int argumentC,\n" 25781 " int argumentD,\n" 25782 " int argumentE\n" 25783 ");", 25784 Medium, Style); 25785 25786 verifyFormat("outerFunctionCall(\n" 25787 " nestedFunctionCall(argument1),\n" 25788 " nestedLongFunctionCall(\n" 25789 " argument1,\n" 25790 " argument2,\n" 25791 " argument3,\n" 25792 " argument4,\n" 25793 " argument5\n" 25794 " )\n" 25795 ");", 25796 Style); 25797 25798 verifyFormat("int a = (int)b;", Style); 25799 verifyFormat("int a = (int)b;", 25800 "int a = (\n" 25801 " int\n" 25802 ") b;", 25803 Style); 25804 25805 verifyFormat("return (true);", Style); 25806 verifyFormat("return (true);", 25807 "return (\n" 25808 " true\n" 25809 ");", 25810 Style); 25811 25812 verifyFormat("void foo();", Style); 25813 verifyFormat("void foo();", 25814 "void foo(\n" 25815 ");", 25816 Style); 25817 25818 verifyFormat("void foo() {}", Style); 25819 verifyFormat("void foo() {}", 25820 "void foo(\n" 25821 ") {\n" 25822 "}", 25823 Style); 25824 25825 verifyFormat("auto string = std::string();", Style); 25826 verifyFormat("auto string = std::string();", 25827 "auto string = std::string(\n" 25828 ");", 25829 Style); 25830 25831 verifyFormat("void (*functionPointer)() = nullptr;", Style); 25832 verifyFormat("void (*functionPointer)() = nullptr;", 25833 "void (\n" 25834 " *functionPointer\n" 25835 ")\n" 25836 "(\n" 25837 ") = nullptr;", 25838 Style); 25839 } 25840 25841 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) { 25842 auto Style = getLLVMStyle(); 25843 25844 verifyFormat("if (foo()) {\n" 25845 " return;\n" 25846 "}", 25847 Style); 25848 25849 verifyFormat("if (quitelongarg !=\n" 25850 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 25851 "comment\n" 25852 " return;\n" 25853 "}", 25854 Style); 25855 25856 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 25857 25858 verifyFormat("if (foo()) {\n" 25859 " return;\n" 25860 "}", 25861 Style); 25862 25863 verifyFormat("if (quitelongarg !=\n" 25864 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 25865 "comment\n" 25866 " return;\n" 25867 "}", 25868 Style); 25869 } 25870 25871 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) { 25872 auto Style = getLLVMStyle(); 25873 25874 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 25875 " doSomething();\n" 25876 "}", 25877 Style); 25878 25879 verifyFormat("for (int myReallyLongCountVariable = 0; " 25880 "myReallyLongCountVariable < count;\n" 25881 " myReallyLongCountVariable++) {\n" 25882 " doSomething();\n" 25883 "}", 25884 Style); 25885 25886 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 25887 25888 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 25889 " doSomething();\n" 25890 "}", 25891 Style); 25892 25893 verifyFormat("for (int myReallyLongCountVariable = 0; " 25894 "myReallyLongCountVariable < count;\n" 25895 " myReallyLongCountVariable++) {\n" 25896 " doSomething();\n" 25897 "}", 25898 Style); 25899 } 25900 25901 TEST_F(FormatTest, UnderstandsDigraphs) { 25902 verifyFormat("int arr<:5:> = {};"); 25903 verifyFormat("int arr[5] = <%%>;"); 25904 verifyFormat("int arr<:::qualified_variable:> = {};"); 25905 verifyFormat("int arr[::qualified_variable] = <%%>;"); 25906 verifyFormat("%:include <header>"); 25907 verifyFormat("%:define A x##y"); 25908 verifyFormat("#define A x%:%:y"); 25909 } 25910 25911 TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) { 25912 auto Style = getLLVMStyle(); 25913 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 25914 Style.AlignConsecutiveAssignments.Enabled = true; 25915 Style.AlignConsecutiveDeclarations.Enabled = true; 25916 25917 // The AlignArray code is incorrect for non square Arrays and can cause 25918 // crashes, these tests assert that the array is not changed but will 25919 // also act as regression tests for when it is properly fixed 25920 verifyFormat("struct test demo[] = {\n" 25921 " {1, 2},\n" 25922 " {3, 4, 5},\n" 25923 " {6, 7, 8}\n" 25924 "};", 25925 Style); 25926 verifyFormat("struct test demo[] = {\n" 25927 " {1, 2, 3, 4, 5},\n" 25928 " {3, 4, 5},\n" 25929 " {6, 7, 8}\n" 25930 "};", 25931 Style); 25932 verifyFormat("struct test demo[] = {\n" 25933 " {1, 2, 3, 4, 5},\n" 25934 " {3, 4, 5},\n" 25935 " {6, 7, 8, 9, 10, 11, 12}\n" 25936 "};", 25937 Style); 25938 verifyFormat("struct test demo[] = {\n" 25939 " {1, 2, 3},\n" 25940 " {3, 4, 5},\n" 25941 " {6, 7, 8, 9, 10, 11, 12}\n" 25942 "};", 25943 Style); 25944 25945 verifyFormat("S{\n" 25946 " {},\n" 25947 " {},\n" 25948 " {a, b}\n" 25949 "};", 25950 Style); 25951 verifyFormat("S{\n" 25952 " {},\n" 25953 " {},\n" 25954 " {a, b},\n" 25955 "};", 25956 Style); 25957 verifyFormat("void foo() {\n" 25958 " auto thing = test{\n" 25959 " {\n" 25960 " {13}, {something}, // A\n" 25961 " }\n" 25962 " };\n" 25963 "}", 25964 "void foo() {\n" 25965 " auto thing = test{\n" 25966 " {\n" 25967 " {13},\n" 25968 " {something}, // A\n" 25969 " }\n" 25970 " };\n" 25971 "}", 25972 Style); 25973 } 25974 25975 TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) { 25976 auto Style = getLLVMStyle(); 25977 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 25978 Style.AlignConsecutiveAssignments.Enabled = true; 25979 Style.AlignConsecutiveDeclarations.Enabled = true; 25980 25981 // The AlignArray code is incorrect for non square Arrays and can cause 25982 // crashes, these tests assert that the array is not changed but will 25983 // also act as regression tests for when it is properly fixed 25984 verifyFormat("struct test demo[] = {\n" 25985 " {1, 2},\n" 25986 " {3, 4, 5},\n" 25987 " {6, 7, 8}\n" 25988 "};", 25989 Style); 25990 verifyFormat("struct test demo[] = {\n" 25991 " {1, 2, 3, 4, 5},\n" 25992 " {3, 4, 5},\n" 25993 " {6, 7, 8}\n" 25994 "};", 25995 Style); 25996 verifyFormat("struct test demo[] = {\n" 25997 " {1, 2, 3, 4, 5},\n" 25998 " {3, 4, 5},\n" 25999 " {6, 7, 8, 9, 10, 11, 12}\n" 26000 "};", 26001 Style); 26002 verifyFormat("struct test demo[] = {\n" 26003 " {1, 2, 3},\n" 26004 " {3, 4, 5},\n" 26005 " {6, 7, 8, 9, 10, 11, 12}\n" 26006 "};", 26007 Style); 26008 26009 verifyFormat("S{\n" 26010 " {},\n" 26011 " {},\n" 26012 " {a, b}\n" 26013 "};", 26014 Style); 26015 verifyFormat("S{\n" 26016 " {},\n" 26017 " {},\n" 26018 " {a, b},\n" 26019 "};", 26020 Style); 26021 verifyFormat("void foo() {\n" 26022 " auto thing = test{\n" 26023 " {\n" 26024 " {13}, {something}, // A\n" 26025 " }\n" 26026 " };\n" 26027 "}", 26028 "void foo() {\n" 26029 " auto thing = test{\n" 26030 " {\n" 26031 " {13},\n" 26032 " {something}, // A\n" 26033 " }\n" 26034 " };\n" 26035 "}", 26036 Style); 26037 } 26038 26039 TEST_F(FormatTest, FormatsVariableTemplates) { 26040 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;"); 26041 verifyFormat("template <typename T> " 26042 "inline bool var = is_integral_v<T> && is_signed_v<T>;"); 26043 } 26044 26045 } // namespace 26046 } // namespace format 26047 } // namespace clang 26048