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.IfMacros.push_back("MYIF"); 1577 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1578 // Not IF to avoid any confusion that IF is somehow special. 1579 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1580 AllowSimpleBracedStatements.ColumnLimit = 40; 1581 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1582 FormatStyle::SBS_Always; 1583 1584 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1585 FormatStyle::SIS_WithoutElse; 1586 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1587 1588 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1589 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1590 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1591 1592 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1593 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1594 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1595 verifyFormat("if consteval {}", AllowSimpleBracedStatements); 1596 verifyFormat("if !consteval {}", AllowSimpleBracedStatements); 1597 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements); 1598 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1599 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1600 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1601 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1602 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1603 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1604 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1605 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1606 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements); 1607 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements); 1608 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1609 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1610 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1611 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements); 1612 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements); 1613 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1614 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1615 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1616 AllowSimpleBracedStatements); 1617 verifyFormat("if (true) {\n" 1618 " ffffffffffffffffffffffff();\n" 1619 "}", 1620 AllowSimpleBracedStatements); 1621 verifyFormat("if (true) {\n" 1622 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1623 "}", 1624 AllowSimpleBracedStatements); 1625 verifyFormat("if (true) { //\n" 1626 " f();\n" 1627 "}", 1628 AllowSimpleBracedStatements); 1629 verifyFormat("if (true) {\n" 1630 " f();\n" 1631 " f();\n" 1632 "}", 1633 AllowSimpleBracedStatements); 1634 verifyFormat("if (true) {\n" 1635 " f();\n" 1636 "} else {\n" 1637 " f();\n" 1638 "}", 1639 AllowSimpleBracedStatements); 1640 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1641 AllowSimpleBracedStatements); 1642 verifyFormat("MYIF (true) {\n" 1643 " ffffffffffffffffffffffff();\n" 1644 "}", 1645 AllowSimpleBracedStatements); 1646 verifyFormat("MYIF (true) {\n" 1647 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1648 "}", 1649 AllowSimpleBracedStatements); 1650 verifyFormat("MYIF (true) { //\n" 1651 " f();\n" 1652 "}", 1653 AllowSimpleBracedStatements); 1654 verifyFormat("MYIF (true) {\n" 1655 " f();\n" 1656 " f();\n" 1657 "}", 1658 AllowSimpleBracedStatements); 1659 verifyFormat("MYIF (true) {\n" 1660 " f();\n" 1661 "} else {\n" 1662 " f();\n" 1663 "}", 1664 AllowSimpleBracedStatements); 1665 1666 verifyFormat("struct A2 {\n" 1667 " int X;\n" 1668 "};", 1669 AllowSimpleBracedStatements); 1670 verifyFormat("typedef struct A2 {\n" 1671 " int X;\n" 1672 "} A2_t;", 1673 AllowSimpleBracedStatements); 1674 verifyFormat("template <int> struct A2 {\n" 1675 " struct B {};\n" 1676 "};", 1677 AllowSimpleBracedStatements); 1678 1679 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1680 FormatStyle::SIS_Never; 1681 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1682 verifyFormat("if (true) {\n" 1683 " f();\n" 1684 "}", 1685 AllowSimpleBracedStatements); 1686 verifyFormat("if (true) {\n" 1687 " f();\n" 1688 "} else {\n" 1689 " f();\n" 1690 "}", 1691 AllowSimpleBracedStatements); 1692 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1693 verifyFormat("MYIF (true) {\n" 1694 " f();\n" 1695 "}", 1696 AllowSimpleBracedStatements); 1697 verifyFormat("MYIF (true) {\n" 1698 " f();\n" 1699 "} else {\n" 1700 " f();\n" 1701 "}", 1702 AllowSimpleBracedStatements); 1703 1704 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1705 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1706 verifyFormat("while (true) {\n" 1707 " f();\n" 1708 "}", 1709 AllowSimpleBracedStatements); 1710 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1711 verifyFormat("for (;;) {\n" 1712 " f();\n" 1713 "}", 1714 AllowSimpleBracedStatements); 1715 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1716 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1717 " f();\n" 1718 "}", 1719 AllowSimpleBracedStatements); 1720 1721 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1722 FormatStyle::SIS_WithoutElse; 1723 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1724 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1725 FormatStyle::BWACS_Always; 1726 1727 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1728 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1729 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1730 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1731 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1732 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1733 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1734 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1735 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1736 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1737 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1738 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1739 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1740 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1741 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1742 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1743 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1744 AllowSimpleBracedStatements); 1745 verifyFormat("if (true)\n" 1746 "{\n" 1747 " ffffffffffffffffffffffff();\n" 1748 "}", 1749 AllowSimpleBracedStatements); 1750 verifyFormat("if (true)\n" 1751 "{\n" 1752 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1753 "}", 1754 AllowSimpleBracedStatements); 1755 verifyFormat("if (true)\n" 1756 "{ //\n" 1757 " f();\n" 1758 "}", 1759 AllowSimpleBracedStatements); 1760 verifyFormat("if (true)\n" 1761 "{\n" 1762 " f();\n" 1763 " f();\n" 1764 "}", 1765 AllowSimpleBracedStatements); 1766 verifyFormat("if (true)\n" 1767 "{\n" 1768 " f();\n" 1769 "} else\n" 1770 "{\n" 1771 " f();\n" 1772 "}", 1773 AllowSimpleBracedStatements); 1774 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1775 AllowSimpleBracedStatements); 1776 verifyFormat("MYIF (true)\n" 1777 "{\n" 1778 " ffffffffffffffffffffffff();\n" 1779 "}", 1780 AllowSimpleBracedStatements); 1781 verifyFormat("MYIF (true)\n" 1782 "{\n" 1783 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1784 "}", 1785 AllowSimpleBracedStatements); 1786 verifyFormat("MYIF (true)\n" 1787 "{ //\n" 1788 " f();\n" 1789 "}", 1790 AllowSimpleBracedStatements); 1791 verifyFormat("MYIF (true)\n" 1792 "{\n" 1793 " f();\n" 1794 " f();\n" 1795 "}", 1796 AllowSimpleBracedStatements); 1797 verifyFormat("MYIF (true)\n" 1798 "{\n" 1799 " f();\n" 1800 "} else\n" 1801 "{\n" 1802 " f();\n" 1803 "}", 1804 AllowSimpleBracedStatements); 1805 1806 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1807 FormatStyle::SIS_Never; 1808 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1809 verifyFormat("if (true)\n" 1810 "{\n" 1811 " f();\n" 1812 "}", 1813 AllowSimpleBracedStatements); 1814 verifyFormat("if (true)\n" 1815 "{\n" 1816 " f();\n" 1817 "} else\n" 1818 "{\n" 1819 " f();\n" 1820 "}", 1821 AllowSimpleBracedStatements); 1822 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1823 verifyFormat("MYIF (true)\n" 1824 "{\n" 1825 " f();\n" 1826 "}", 1827 AllowSimpleBracedStatements); 1828 verifyFormat("MYIF (true)\n" 1829 "{\n" 1830 " f();\n" 1831 "} else\n" 1832 "{\n" 1833 " f();\n" 1834 "}", 1835 AllowSimpleBracedStatements); 1836 1837 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1838 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1839 verifyFormat("while (true)\n" 1840 "{\n" 1841 " f();\n" 1842 "}", 1843 AllowSimpleBracedStatements); 1844 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1845 verifyFormat("for (;;)\n" 1846 "{\n" 1847 " f();\n" 1848 "}", 1849 AllowSimpleBracedStatements); 1850 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1851 verifyFormat("BOOST_FOREACH (int v, vec)\n" 1852 "{\n" 1853 " f();\n" 1854 "}", 1855 AllowSimpleBracedStatements); 1856 } 1857 1858 TEST_F(FormatTest, UnderstandsMacros) { 1859 verifyFormat("#define A (parentheses)"); 1860 verifyFormat("/* comment */ #define A (parentheses)"); 1861 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)"); 1862 // Even the partial code should never be merged. 1863 EXPECT_EQ("/* comment */ #define A (parentheses)\n" 1864 "#", 1865 format("/* comment */ #define A (parentheses)\n" 1866 "#")); 1867 verifyFormat("/* comment */ #define A (parentheses)\n" 1868 "#\n"); 1869 verifyFormat("/* comment */ #define A (parentheses)\n" 1870 "#define B (parentheses)"); 1871 verifyFormat("#define true ((int)1)"); 1872 verifyFormat("#define and(x)"); 1873 verifyFormat("#define if(x) x"); 1874 verifyFormat("#define return(x) (x)"); 1875 verifyFormat("#define while(x) for (; x;)"); 1876 verifyFormat("#define xor(x) (^(x))"); 1877 verifyFormat("#define __except(x)"); 1878 verifyFormat("#define __try(x)"); 1879 1880 FormatStyle Style = getLLVMStyle(); 1881 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1882 Style.BraceWrapping.AfterFunction = true; 1883 // Test that a macro definition never gets merged with the following 1884 // definition. 1885 // FIXME: The AAA macro definition probably should not be split into 3 lines. 1886 verifyFormat("#define AAA " 1887 " \\\n" 1888 " N " 1889 " \\\n" 1890 " {\n" 1891 "#define BBB }\n", 1892 Style); 1893 // verifyFormat("#define AAA N { //\n", Style); 1894 1895 verifyFormat("MACRO(return)"); 1896 verifyFormat("MACRO(co_await)"); 1897 verifyFormat("MACRO(co_return)"); 1898 verifyFormat("MACRO(co_yield)"); 1899 verifyFormat("MACRO(return, something)"); 1900 verifyFormat("MACRO(co_return, something)"); 1901 verifyFormat("MACRO(something##something)"); 1902 verifyFormat("MACRO(return##something)"); 1903 verifyFormat("MACRO(co_return##something)"); 1904 } 1905 1906 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1907 FormatStyle Style = getLLVMStyleWithColumns(60); 1908 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1909 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1910 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1911 EXPECT_EQ("#define A \\\n" 1912 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1913 " { \\\n" 1914 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1915 " }\n" 1916 "X;", 1917 format("#define A \\\n" 1918 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1919 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1920 " }\n" 1921 "X;", 1922 Style)); 1923 } 1924 1925 TEST_F(FormatTest, ParseIfElse) { 1926 verifyFormat("if (true)\n" 1927 " if (true)\n" 1928 " if (true)\n" 1929 " f();\n" 1930 " else\n" 1931 " g();\n" 1932 " else\n" 1933 " h();\n" 1934 "else\n" 1935 " i();"); 1936 verifyFormat("if (true)\n" 1937 " if (true)\n" 1938 " if (true) {\n" 1939 " if (true)\n" 1940 " f();\n" 1941 " } else {\n" 1942 " g();\n" 1943 " }\n" 1944 " else\n" 1945 " h();\n" 1946 "else {\n" 1947 " i();\n" 1948 "}"); 1949 verifyFormat("if (true)\n" 1950 " if constexpr (true)\n" 1951 " if (true) {\n" 1952 " if constexpr (true)\n" 1953 " f();\n" 1954 " } else {\n" 1955 " g();\n" 1956 " }\n" 1957 " else\n" 1958 " h();\n" 1959 "else {\n" 1960 " i();\n" 1961 "}"); 1962 verifyFormat("if (true)\n" 1963 " if CONSTEXPR (true)\n" 1964 " if (true) {\n" 1965 " if CONSTEXPR (true)\n" 1966 " f();\n" 1967 " } else {\n" 1968 " g();\n" 1969 " }\n" 1970 " else\n" 1971 " h();\n" 1972 "else {\n" 1973 " i();\n" 1974 "}"); 1975 verifyFormat("void f() {\n" 1976 " if (a) {\n" 1977 " } else {\n" 1978 " }\n" 1979 "}"); 1980 } 1981 1982 TEST_F(FormatTest, ElseIf) { 1983 verifyFormat("if (a) {\n} else if (b) {\n}"); 1984 verifyFormat("if (a)\n" 1985 " f();\n" 1986 "else if (b)\n" 1987 " g();\n" 1988 "else\n" 1989 " h();"); 1990 verifyFormat("if (a)\n" 1991 " f();\n" 1992 "else // comment\n" 1993 " if (b) {\n" 1994 " g();\n" 1995 " h();\n" 1996 " }"); 1997 verifyFormat("if constexpr (a)\n" 1998 " f();\n" 1999 "else if constexpr (b)\n" 2000 " g();\n" 2001 "else\n" 2002 " h();"); 2003 verifyFormat("if CONSTEXPR (a)\n" 2004 " f();\n" 2005 "else if CONSTEXPR (b)\n" 2006 " g();\n" 2007 "else\n" 2008 " h();"); 2009 verifyFormat("if (a) {\n" 2010 " f();\n" 2011 "}\n" 2012 "// or else ..\n" 2013 "else {\n" 2014 " g()\n" 2015 "}"); 2016 2017 verifyFormat("if (a) {\n" 2018 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2019 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2020 "}"); 2021 verifyFormat("if (a) {\n" 2022 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2023 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2024 "}"); 2025 verifyFormat("if (a) {\n" 2026 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2027 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2028 "}"); 2029 verifyFormat("if (a) {\n" 2030 "} else if (\n" 2031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2032 "}", 2033 getLLVMStyleWithColumns(62)); 2034 verifyFormat("if (a) {\n" 2035 "} else if constexpr (\n" 2036 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2037 "}", 2038 getLLVMStyleWithColumns(62)); 2039 verifyFormat("if (a) {\n" 2040 "} else if CONSTEXPR (\n" 2041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2042 "}", 2043 getLLVMStyleWithColumns(62)); 2044 } 2045 2046 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 2047 FormatStyle Style = getLLVMStyle(); 2048 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); 2049 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer); 2050 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 2051 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 2052 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 2053 verifyFormat("int *f1(int &a) const &;", Style); 2054 verifyFormat("int *f1(int &a) const & = 0;", Style); 2055 verifyFormat("int *a = f1();", Style); 2056 verifyFormat("int &b = f2();", Style); 2057 verifyFormat("int &&c = f3();", Style); 2058 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 2059 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); 2060 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 2061 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2062 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 2063 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); 2064 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 2065 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); 2066 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); 2067 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); 2068 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); 2069 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); 2070 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); 2071 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); 2072 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); 2073 verifyFormat("for (f(); int &c : {1, 2, 3})", Style); 2074 verifyFormat( 2075 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n" 2076 " res2 = [](int &a) { return 0000000000000; };", 2077 Style); 2078 2079 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2080 verifyFormat("Const unsigned int *c;\n" 2081 "const unsigned int *d;\n" 2082 "Const unsigned int &e;\n" 2083 "const unsigned int &f;\n" 2084 "const unsigned &&g;\n" 2085 "Const unsigned h;", 2086 Style); 2087 2088 Style.PointerAlignment = FormatStyle::PAS_Left; 2089 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 2090 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 2091 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 2092 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 2093 verifyFormat("int* f1(int& a) const& = 0;", Style); 2094 verifyFormat("int* a = f1();", Style); 2095 verifyFormat("int& b = f2();", Style); 2096 verifyFormat("int&& c = f3();", Style); 2097 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2098 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2099 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2100 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2101 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2102 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2103 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2104 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2105 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style); 2106 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style); 2107 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style); 2108 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2109 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); 2110 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); 2111 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); 2112 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); 2113 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); 2114 verifyFormat("for (f(); int& c : {1, 2, 3})", Style); 2115 verifyFormat( 2116 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n" 2117 " res2 = [](int& a) { return 0000000000000; };", 2118 Style); 2119 2120 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2121 verifyFormat("Const unsigned int* c;\n" 2122 "const unsigned int* d;\n" 2123 "Const unsigned int& e;\n" 2124 "const unsigned int& f;\n" 2125 "const unsigned&& g;\n" 2126 "Const unsigned h;", 2127 Style); 2128 2129 Style.PointerAlignment = FormatStyle::PAS_Right; 2130 Style.ReferenceAlignment = FormatStyle::RAS_Left; 2131 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 2132 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 2133 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 2134 verifyFormat("int *a = f1();", Style); 2135 verifyFormat("int& b = f2();", Style); 2136 verifyFormat("int&& c = f3();", Style); 2137 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2138 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2139 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); 2140 2141 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2142 verifyFormat("Const unsigned int *c;\n" 2143 "const unsigned int *d;\n" 2144 "Const unsigned int& e;\n" 2145 "const unsigned int& f;\n" 2146 "const unsigned g;\n" 2147 "Const unsigned h;", 2148 Style); 2149 2150 Style.PointerAlignment = FormatStyle::PAS_Left; 2151 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 2152 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 2153 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 2154 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 2155 verifyFormat("int* a = f1();", Style); 2156 verifyFormat("int & b = f2();", Style); 2157 verifyFormat("int && c = f3();", Style); 2158 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); 2159 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); 2160 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); 2161 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2162 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); 2163 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); 2164 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); 2165 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2166 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); 2167 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); 2168 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); 2169 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); 2170 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); 2171 verifyFormat("for (f(); int & c : {1, 2, 3})", Style); 2172 verifyFormat( 2173 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n" 2174 " res2 = [](int & a) { return 0000000000000; };", 2175 Style); 2176 2177 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2178 verifyFormat("Const unsigned int* c;\n" 2179 "const unsigned int* d;\n" 2180 "Const unsigned int & e;\n" 2181 "const unsigned int & f;\n" 2182 "const unsigned && g;\n" 2183 "Const unsigned h;", 2184 Style); 2185 2186 Style.PointerAlignment = FormatStyle::PAS_Middle; 2187 Style.ReferenceAlignment = FormatStyle::RAS_Right; 2188 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 2189 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 2190 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 2191 verifyFormat("int * a = f1();", Style); 2192 verifyFormat("int &b = f2();", Style); 2193 verifyFormat("int &&c = f3();", Style); 2194 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2195 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2196 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); 2197 2198 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2199 // specifically handled 2200 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2201 } 2202 2203 TEST_F(FormatTest, FormatsForLoop) { 2204 verifyFormat( 2205 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2206 " ++VeryVeryLongLoopVariable)\n" 2207 " ;"); 2208 verifyFormat("for (;;)\n" 2209 " f();"); 2210 verifyFormat("for (;;) {\n}"); 2211 verifyFormat("for (;;) {\n" 2212 " f();\n" 2213 "}"); 2214 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2215 2216 verifyFormat( 2217 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2218 " E = UnwrappedLines.end();\n" 2219 " I != E; ++I) {\n}"); 2220 2221 verifyFormat( 2222 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2223 " ++IIIII) {\n}"); 2224 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2225 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2226 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2227 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2228 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2229 " E = FD->getDeclsInPrototypeScope().end();\n" 2230 " I != E; ++I) {\n}"); 2231 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2232 " I = Container.begin(),\n" 2233 " E = Container.end();\n" 2234 " I != E; ++I) {\n}", 2235 getLLVMStyleWithColumns(76)); 2236 2237 verifyFormat( 2238 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2239 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2240 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2241 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2242 " ++aaaaaaaaaaa) {\n}"); 2243 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2244 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2245 " ++i) {\n}"); 2246 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2247 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2248 "}"); 2249 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2250 " aaaaaaaaaa);\n" 2251 " iter; ++iter) {\n" 2252 "}"); 2253 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2254 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2255 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2256 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2257 2258 // These should not be formatted as Objective-C for-in loops. 2259 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2260 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2261 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2262 verifyFormat( 2263 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2264 2265 FormatStyle NoBinPacking = getLLVMStyle(); 2266 NoBinPacking.BinPackParameters = false; 2267 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2268 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2269 " aaaaaaaaaaaaaaaa,\n" 2270 " aaaaaaaaaaaaaaaa,\n" 2271 " aaaaaaaaaaaaaaaa);\n" 2272 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2273 "}", 2274 NoBinPacking); 2275 verifyFormat( 2276 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2277 " E = UnwrappedLines.end();\n" 2278 " I != E;\n" 2279 " ++I) {\n}", 2280 NoBinPacking); 2281 2282 FormatStyle AlignLeft = getLLVMStyle(); 2283 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2284 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2285 } 2286 2287 TEST_F(FormatTest, RangeBasedForLoops) { 2288 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2289 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2290 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2291 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2292 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2294 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2295 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2296 } 2297 2298 TEST_F(FormatTest, ForEachLoops) { 2299 FormatStyle Style = getLLVMStyle(); 2300 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2301 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false); 2302 verifyFormat("void f() {\n" 2303 " for (;;) {\n" 2304 " }\n" 2305 " foreach (Item *item, itemlist) {\n" 2306 " }\n" 2307 " Q_FOREACH (Item *item, itemlist) {\n" 2308 " }\n" 2309 " BOOST_FOREACH (Item *item, itemlist) {\n" 2310 " }\n" 2311 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2312 "}", 2313 Style); 2314 verifyFormat("void f() {\n" 2315 " for (;;)\n" 2316 " int j = 1;\n" 2317 " Q_FOREACH (int v, vec)\n" 2318 " v *= 2;\n" 2319 " for (;;) {\n" 2320 " int j = 1;\n" 2321 " }\n" 2322 " Q_FOREACH (int v, vec) {\n" 2323 " v *= 2;\n" 2324 " }\n" 2325 "}", 2326 Style); 2327 2328 FormatStyle ShortBlocks = getLLVMStyle(); 2329 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2330 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false); 2331 verifyFormat("void f() {\n" 2332 " for (;;)\n" 2333 " int j = 1;\n" 2334 " Q_FOREACH (int &v, vec)\n" 2335 " v *= 2;\n" 2336 " for (;;) {\n" 2337 " int j = 1;\n" 2338 " }\n" 2339 " Q_FOREACH (int &v, vec) {\n" 2340 " int j = 1;\n" 2341 " }\n" 2342 "}", 2343 ShortBlocks); 2344 2345 FormatStyle ShortLoops = getLLVMStyle(); 2346 ShortLoops.AllowShortLoopsOnASingleLine = true; 2347 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2348 verifyFormat("void f() {\n" 2349 " for (;;) int j = 1;\n" 2350 " Q_FOREACH (int &v, vec) int j = 1;\n" 2351 " for (;;) {\n" 2352 " int j = 1;\n" 2353 " }\n" 2354 " Q_FOREACH (int &v, vec) {\n" 2355 " int j = 1;\n" 2356 " }\n" 2357 "}", 2358 ShortLoops); 2359 2360 FormatStyle ShortBlocksAndLoops = getLLVMStyle(); 2361 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2362 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true; 2363 verifyFormat("void f() {\n" 2364 " for (;;) int j = 1;\n" 2365 " Q_FOREACH (int &v, vec) int j = 1;\n" 2366 " for (;;) { int j = 1; }\n" 2367 " Q_FOREACH (int &v, vec) { int j = 1; }\n" 2368 "}", 2369 ShortBlocksAndLoops); 2370 2371 Style.SpaceBeforeParens = 2372 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2373 verifyFormat("void f() {\n" 2374 " for (;;) {\n" 2375 " }\n" 2376 " foreach(Item *item, itemlist) {\n" 2377 " }\n" 2378 " Q_FOREACH(Item *item, itemlist) {\n" 2379 " }\n" 2380 " BOOST_FOREACH(Item *item, itemlist) {\n" 2381 " }\n" 2382 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2383 "}", 2384 Style); 2385 2386 // As function-like macros. 2387 verifyFormat("#define foreach(x, y)\n" 2388 "#define Q_FOREACH(x, y)\n" 2389 "#define BOOST_FOREACH(x, y)\n" 2390 "#define UNKNOWN_FOREACH(x, y)\n"); 2391 2392 // Not as function-like macros. 2393 verifyFormat("#define foreach (x, y)\n" 2394 "#define Q_FOREACH (x, y)\n" 2395 "#define BOOST_FOREACH (x, y)\n" 2396 "#define UNKNOWN_FOREACH (x, y)\n"); 2397 2398 // handle microsoft non standard extension 2399 verifyFormat("for each (char c in x->MyStringProperty)"); 2400 } 2401 2402 TEST_F(FormatTest, FormatsWhileLoop) { 2403 verifyFormat("while (true) {\n}"); 2404 verifyFormat("while (true)\n" 2405 " f();"); 2406 verifyFormat("while () {\n}"); 2407 verifyFormat("while () {\n" 2408 " f();\n" 2409 "}"); 2410 } 2411 2412 TEST_F(FormatTest, FormatsDoWhile) { 2413 verifyFormat("do {\n" 2414 " do_something();\n" 2415 "} while (something());"); 2416 verifyFormat("do\n" 2417 " do_something();\n" 2418 "while (something());"); 2419 } 2420 2421 TEST_F(FormatTest, FormatsSwitchStatement) { 2422 verifyFormat("switch (x) {\n" 2423 "case 1:\n" 2424 " f();\n" 2425 " break;\n" 2426 "case kFoo:\n" 2427 "case ns::kBar:\n" 2428 "case kBaz:\n" 2429 " break;\n" 2430 "default:\n" 2431 " g();\n" 2432 " break;\n" 2433 "}"); 2434 verifyFormat("switch (x) {\n" 2435 "case 1: {\n" 2436 " f();\n" 2437 " break;\n" 2438 "}\n" 2439 "case 2: {\n" 2440 " break;\n" 2441 "}\n" 2442 "}"); 2443 verifyFormat("switch (x) {\n" 2444 "case 1: {\n" 2445 " f();\n" 2446 " {\n" 2447 " g();\n" 2448 " h();\n" 2449 " }\n" 2450 " break;\n" 2451 "}\n" 2452 "}"); 2453 verifyFormat("switch (x) {\n" 2454 "case 1: {\n" 2455 " f();\n" 2456 " if (foo) {\n" 2457 " g();\n" 2458 " h();\n" 2459 " }\n" 2460 " break;\n" 2461 "}\n" 2462 "}"); 2463 verifyFormat("switch (x) {\n" 2464 "case 1: {\n" 2465 " f();\n" 2466 " g();\n" 2467 "} break;\n" 2468 "}"); 2469 verifyFormat("switch (test)\n" 2470 " ;"); 2471 verifyFormat("switch (x) {\n" 2472 "default: {\n" 2473 " // Do nothing.\n" 2474 "}\n" 2475 "}"); 2476 verifyFormat("switch (x) {\n" 2477 "// comment\n" 2478 "// if 1, do f()\n" 2479 "case 1:\n" 2480 " f();\n" 2481 "}"); 2482 verifyFormat("switch (x) {\n" 2483 "case 1:\n" 2484 " // Do amazing stuff\n" 2485 " {\n" 2486 " f();\n" 2487 " g();\n" 2488 " }\n" 2489 " break;\n" 2490 "}"); 2491 verifyFormat("#define A \\\n" 2492 " switch (x) { \\\n" 2493 " case a: \\\n" 2494 " foo = b; \\\n" 2495 " }", 2496 getLLVMStyleWithColumns(20)); 2497 verifyFormat("#define OPERATION_CASE(name) \\\n" 2498 " case OP_name: \\\n" 2499 " return operations::Operation##name\n", 2500 getLLVMStyleWithColumns(40)); 2501 verifyFormat("switch (x) {\n" 2502 "case 1:;\n" 2503 "default:;\n" 2504 " int i;\n" 2505 "}"); 2506 2507 verifyGoogleFormat("switch (x) {\n" 2508 " case 1:\n" 2509 " f();\n" 2510 " break;\n" 2511 " case kFoo:\n" 2512 " case ns::kBar:\n" 2513 " case kBaz:\n" 2514 " break;\n" 2515 " default:\n" 2516 " g();\n" 2517 " break;\n" 2518 "}"); 2519 verifyGoogleFormat("switch (x) {\n" 2520 " case 1: {\n" 2521 " f();\n" 2522 " break;\n" 2523 " }\n" 2524 "}"); 2525 verifyGoogleFormat("switch (test)\n" 2526 " ;"); 2527 2528 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2529 " case OP_name: \\\n" 2530 " return operations::Operation##name\n"); 2531 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2532 " // Get the correction operation class.\n" 2533 " switch (OpCode) {\n" 2534 " CASE(Add);\n" 2535 " CASE(Subtract);\n" 2536 " default:\n" 2537 " return operations::Unknown;\n" 2538 " }\n" 2539 "#undef OPERATION_CASE\n" 2540 "}"); 2541 verifyFormat("DEBUG({\n" 2542 " switch (x) {\n" 2543 " case A:\n" 2544 " f();\n" 2545 " break;\n" 2546 " // fallthrough\n" 2547 " case B:\n" 2548 " g();\n" 2549 " break;\n" 2550 " }\n" 2551 "});"); 2552 EXPECT_EQ("DEBUG({\n" 2553 " switch (x) {\n" 2554 " case A:\n" 2555 " f();\n" 2556 " break;\n" 2557 " // On B:\n" 2558 " case B:\n" 2559 " g();\n" 2560 " break;\n" 2561 " }\n" 2562 "});", 2563 format("DEBUG({\n" 2564 " switch (x) {\n" 2565 " case A:\n" 2566 " f();\n" 2567 " break;\n" 2568 " // On B:\n" 2569 " case B:\n" 2570 " g();\n" 2571 " break;\n" 2572 " }\n" 2573 "});", 2574 getLLVMStyle())); 2575 EXPECT_EQ("switch (n) {\n" 2576 "case 0: {\n" 2577 " return false;\n" 2578 "}\n" 2579 "default: {\n" 2580 " return true;\n" 2581 "}\n" 2582 "}", 2583 format("switch (n)\n" 2584 "{\n" 2585 "case 0: {\n" 2586 " return false;\n" 2587 "}\n" 2588 "default: {\n" 2589 " return true;\n" 2590 "}\n" 2591 "}", 2592 getLLVMStyle())); 2593 verifyFormat("switch (a) {\n" 2594 "case (b):\n" 2595 " return;\n" 2596 "}"); 2597 2598 verifyFormat("switch (a) {\n" 2599 "case some_namespace::\n" 2600 " some_constant:\n" 2601 " return;\n" 2602 "}", 2603 getLLVMStyleWithColumns(34)); 2604 2605 FormatStyle Style = getLLVMStyle(); 2606 Style.IndentCaseLabels = true; 2607 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2608 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2609 Style.BraceWrapping.AfterCaseLabel = true; 2610 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2611 EXPECT_EQ("switch (n)\n" 2612 "{\n" 2613 " case 0:\n" 2614 " {\n" 2615 " return false;\n" 2616 " }\n" 2617 " default:\n" 2618 " {\n" 2619 " return true;\n" 2620 " }\n" 2621 "}", 2622 format("switch (n) {\n" 2623 " case 0: {\n" 2624 " return false;\n" 2625 " }\n" 2626 " default: {\n" 2627 " return true;\n" 2628 " }\n" 2629 "}", 2630 Style)); 2631 Style.BraceWrapping.AfterCaseLabel = false; 2632 EXPECT_EQ("switch (n)\n" 2633 "{\n" 2634 " case 0: {\n" 2635 " return false;\n" 2636 " }\n" 2637 " default: {\n" 2638 " return true;\n" 2639 " }\n" 2640 "}", 2641 format("switch (n) {\n" 2642 " case 0:\n" 2643 " {\n" 2644 " return false;\n" 2645 " }\n" 2646 " default:\n" 2647 " {\n" 2648 " return true;\n" 2649 " }\n" 2650 "}", 2651 Style)); 2652 Style.IndentCaseLabels = false; 2653 Style.IndentCaseBlocks = true; 2654 EXPECT_EQ("switch (n)\n" 2655 "{\n" 2656 "case 0:\n" 2657 " {\n" 2658 " return false;\n" 2659 " }\n" 2660 "case 1:\n" 2661 " break;\n" 2662 "default:\n" 2663 " {\n" 2664 " return true;\n" 2665 " }\n" 2666 "}", 2667 format("switch (n) {\n" 2668 "case 0: {\n" 2669 " return false;\n" 2670 "}\n" 2671 "case 1:\n" 2672 " break;\n" 2673 "default: {\n" 2674 " return true;\n" 2675 "}\n" 2676 "}", 2677 Style)); 2678 Style.IndentCaseLabels = true; 2679 Style.IndentCaseBlocks = true; 2680 EXPECT_EQ("switch (n)\n" 2681 "{\n" 2682 " case 0:\n" 2683 " {\n" 2684 " return false;\n" 2685 " }\n" 2686 " case 1:\n" 2687 " break;\n" 2688 " default:\n" 2689 " {\n" 2690 " return true;\n" 2691 " }\n" 2692 "}", 2693 format("switch (n) {\n" 2694 "case 0: {\n" 2695 " return false;\n" 2696 "}\n" 2697 "case 1:\n" 2698 " break;\n" 2699 "default: {\n" 2700 " return true;\n" 2701 "}\n" 2702 "}", 2703 Style)); 2704 } 2705 2706 TEST_F(FormatTest, CaseRanges) { 2707 verifyFormat("switch (x) {\n" 2708 "case 'A' ... 'Z':\n" 2709 "case 1 ... 5:\n" 2710 "case a ... b:\n" 2711 " break;\n" 2712 "}"); 2713 } 2714 2715 TEST_F(FormatTest, ShortEnums) { 2716 FormatStyle Style = getLLVMStyle(); 2717 Style.AllowShortEnumsOnASingleLine = true; 2718 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2719 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2720 Style.AllowShortEnumsOnASingleLine = false; 2721 verifyFormat("enum {\n" 2722 " A,\n" 2723 " B,\n" 2724 " C\n" 2725 "} ShortEnum1, ShortEnum2;", 2726 Style); 2727 verifyFormat("typedef enum {\n" 2728 " A,\n" 2729 " B,\n" 2730 " C\n" 2731 "} ShortEnum1, ShortEnum2;", 2732 Style); 2733 verifyFormat("enum {\n" 2734 " A,\n" 2735 "} ShortEnum1, ShortEnum2;", 2736 Style); 2737 verifyFormat("typedef enum {\n" 2738 " A,\n" 2739 "} ShortEnum1, ShortEnum2;", 2740 Style); 2741 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2742 Style.BraceWrapping.AfterEnum = true; 2743 verifyFormat("enum\n" 2744 "{\n" 2745 " A,\n" 2746 " B,\n" 2747 " C\n" 2748 "} ShortEnum1, ShortEnum2;", 2749 Style); 2750 verifyFormat("typedef enum\n" 2751 "{\n" 2752 " A,\n" 2753 " B,\n" 2754 " C\n" 2755 "} ShortEnum1, ShortEnum2;", 2756 Style); 2757 } 2758 2759 TEST_F(FormatTest, ShortCaseLabels) { 2760 FormatStyle Style = getLLVMStyle(); 2761 Style.AllowShortCaseLabelsOnASingleLine = true; 2762 verifyFormat("switch (a) {\n" 2763 "case 1: x = 1; break;\n" 2764 "case 2: return;\n" 2765 "case 3:\n" 2766 "case 4:\n" 2767 "case 5: return;\n" 2768 "case 6: // comment\n" 2769 " return;\n" 2770 "case 7:\n" 2771 " // comment\n" 2772 " return;\n" 2773 "case 8:\n" 2774 " x = 8; // comment\n" 2775 " break;\n" 2776 "default: y = 1; break;\n" 2777 "}", 2778 Style); 2779 verifyFormat("switch (a) {\n" 2780 "case 0: return; // comment\n" 2781 "case 1: break; // comment\n" 2782 "case 2: return;\n" 2783 "// comment\n" 2784 "case 3: return;\n" 2785 "// comment 1\n" 2786 "// comment 2\n" 2787 "// comment 3\n" 2788 "case 4: break; /* comment */\n" 2789 "case 5:\n" 2790 " // comment\n" 2791 " break;\n" 2792 "case 6: /* comment */ x = 1; break;\n" 2793 "case 7: x = /* comment */ 1; break;\n" 2794 "case 8:\n" 2795 " x = 1; /* comment */\n" 2796 " break;\n" 2797 "case 9:\n" 2798 " break; // comment line 1\n" 2799 " // comment line 2\n" 2800 "}", 2801 Style); 2802 EXPECT_EQ("switch (a) {\n" 2803 "case 1:\n" 2804 " x = 8;\n" 2805 " // fall through\n" 2806 "case 2: x = 8;\n" 2807 "// comment\n" 2808 "case 3:\n" 2809 " return; /* comment line 1\n" 2810 " * comment line 2 */\n" 2811 "case 4: i = 8;\n" 2812 "// something else\n" 2813 "#if FOO\n" 2814 "case 5: break;\n" 2815 "#endif\n" 2816 "}", 2817 format("switch (a) {\n" 2818 "case 1: x = 8;\n" 2819 " // fall through\n" 2820 "case 2:\n" 2821 " x = 8;\n" 2822 "// comment\n" 2823 "case 3:\n" 2824 " return; /* comment line 1\n" 2825 " * comment line 2 */\n" 2826 "case 4:\n" 2827 " i = 8;\n" 2828 "// something else\n" 2829 "#if FOO\n" 2830 "case 5: break;\n" 2831 "#endif\n" 2832 "}", 2833 Style)); 2834 EXPECT_EQ("switch (a) {\n" 2835 "case 0:\n" 2836 " return; // long long long long long long long long long long " 2837 "long long comment\n" 2838 " // line\n" 2839 "}", 2840 format("switch (a) {\n" 2841 "case 0: return; // long long long long long long long long " 2842 "long long long long comment line\n" 2843 "}", 2844 Style)); 2845 EXPECT_EQ("switch (a) {\n" 2846 "case 0:\n" 2847 " return; /* long long long long long long long long long long " 2848 "long long comment\n" 2849 " line */\n" 2850 "}", 2851 format("switch (a) {\n" 2852 "case 0: return; /* long long long long long long long long " 2853 "long long long long comment line */\n" 2854 "}", 2855 Style)); 2856 verifyFormat("switch (a) {\n" 2857 "#if FOO\n" 2858 "case 0: return 0;\n" 2859 "#endif\n" 2860 "}", 2861 Style); 2862 verifyFormat("switch (a) {\n" 2863 "case 1: {\n" 2864 "}\n" 2865 "case 2: {\n" 2866 " return;\n" 2867 "}\n" 2868 "case 3: {\n" 2869 " x = 1;\n" 2870 " return;\n" 2871 "}\n" 2872 "case 4:\n" 2873 " if (x)\n" 2874 " return;\n" 2875 "}", 2876 Style); 2877 Style.ColumnLimit = 21; 2878 verifyFormat("switch (a) {\n" 2879 "case 1: x = 1; break;\n" 2880 "case 2: return;\n" 2881 "case 3:\n" 2882 "case 4:\n" 2883 "case 5: return;\n" 2884 "default:\n" 2885 " y = 1;\n" 2886 " break;\n" 2887 "}", 2888 Style); 2889 Style.ColumnLimit = 80; 2890 Style.AllowShortCaseLabelsOnASingleLine = false; 2891 Style.IndentCaseLabels = true; 2892 EXPECT_EQ("switch (n) {\n" 2893 " default /*comments*/:\n" 2894 " return true;\n" 2895 " case 0:\n" 2896 " return false;\n" 2897 "}", 2898 format("switch (n) {\n" 2899 "default/*comments*/:\n" 2900 " return true;\n" 2901 "case 0:\n" 2902 " return false;\n" 2903 "}", 2904 Style)); 2905 Style.AllowShortCaseLabelsOnASingleLine = true; 2906 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2907 Style.BraceWrapping.AfterCaseLabel = true; 2908 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2909 EXPECT_EQ("switch (n)\n" 2910 "{\n" 2911 " case 0:\n" 2912 " {\n" 2913 " return false;\n" 2914 " }\n" 2915 " default:\n" 2916 " {\n" 2917 " return true;\n" 2918 " }\n" 2919 "}", 2920 format("switch (n) {\n" 2921 " case 0: {\n" 2922 " return false;\n" 2923 " }\n" 2924 " default:\n" 2925 " {\n" 2926 " return true;\n" 2927 " }\n" 2928 "}", 2929 Style)); 2930 } 2931 2932 TEST_F(FormatTest, FormatsLabels) { 2933 verifyFormat("void f() {\n" 2934 " some_code();\n" 2935 "test_label:\n" 2936 " some_other_code();\n" 2937 " {\n" 2938 " some_more_code();\n" 2939 " another_label:\n" 2940 " some_more_code();\n" 2941 " }\n" 2942 "}"); 2943 verifyFormat("{\n" 2944 " some_code();\n" 2945 "test_label:\n" 2946 " some_other_code();\n" 2947 "}"); 2948 verifyFormat("{\n" 2949 " some_code();\n" 2950 "test_label:;\n" 2951 " int i = 0;\n" 2952 "}"); 2953 FormatStyle Style = getLLVMStyle(); 2954 Style.IndentGotoLabels = false; 2955 verifyFormat("void f() {\n" 2956 " some_code();\n" 2957 "test_label:\n" 2958 " some_other_code();\n" 2959 " {\n" 2960 " some_more_code();\n" 2961 "another_label:\n" 2962 " some_more_code();\n" 2963 " }\n" 2964 "}", 2965 Style); 2966 verifyFormat("{\n" 2967 " some_code();\n" 2968 "test_label:\n" 2969 " some_other_code();\n" 2970 "}", 2971 Style); 2972 verifyFormat("{\n" 2973 " some_code();\n" 2974 "test_label:;\n" 2975 " int i = 0;\n" 2976 "}"); 2977 } 2978 2979 TEST_F(FormatTest, MultiLineControlStatements) { 2980 FormatStyle Style = getLLVMStyleWithColumns(20); 2981 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2982 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2983 // Short lines should keep opening brace on same line. 2984 EXPECT_EQ("if (foo) {\n" 2985 " bar();\n" 2986 "}", 2987 format("if(foo){bar();}", Style)); 2988 EXPECT_EQ("if (foo) {\n" 2989 " bar();\n" 2990 "} else {\n" 2991 " baz();\n" 2992 "}", 2993 format("if(foo){bar();}else{baz();}", Style)); 2994 EXPECT_EQ("if (foo && bar) {\n" 2995 " baz();\n" 2996 "}", 2997 format("if(foo&&bar){baz();}", Style)); 2998 EXPECT_EQ("if (foo) {\n" 2999 " bar();\n" 3000 "} else if (baz) {\n" 3001 " quux();\n" 3002 "}", 3003 format("if(foo){bar();}else if(baz){quux();}", Style)); 3004 EXPECT_EQ( 3005 "if (foo) {\n" 3006 " bar();\n" 3007 "} else if (baz) {\n" 3008 " quux();\n" 3009 "} else {\n" 3010 " foobar();\n" 3011 "}", 3012 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 3013 EXPECT_EQ("for (;;) {\n" 3014 " foo();\n" 3015 "}", 3016 format("for(;;){foo();}")); 3017 EXPECT_EQ("while (1) {\n" 3018 " foo();\n" 3019 "}", 3020 format("while(1){foo();}", Style)); 3021 EXPECT_EQ("switch (foo) {\n" 3022 "case bar:\n" 3023 " return;\n" 3024 "}", 3025 format("switch(foo){case bar:return;}", Style)); 3026 EXPECT_EQ("try {\n" 3027 " foo();\n" 3028 "} catch (...) {\n" 3029 " bar();\n" 3030 "}", 3031 format("try{foo();}catch(...){bar();}", Style)); 3032 EXPECT_EQ("do {\n" 3033 " foo();\n" 3034 "} while (bar &&\n" 3035 " baz);", 3036 format("do{foo();}while(bar&&baz);", Style)); 3037 // Long lines should put opening brace on new line. 3038 EXPECT_EQ("if (foo && bar &&\n" 3039 " baz)\n" 3040 "{\n" 3041 " quux();\n" 3042 "}", 3043 format("if(foo&&bar&&baz){quux();}", Style)); 3044 EXPECT_EQ("if (foo && bar &&\n" 3045 " baz)\n" 3046 "{\n" 3047 " quux();\n" 3048 "}", 3049 format("if (foo && bar &&\n" 3050 " baz) {\n" 3051 " quux();\n" 3052 "}", 3053 Style)); 3054 EXPECT_EQ("if (foo) {\n" 3055 " bar();\n" 3056 "} else if (baz ||\n" 3057 " quux)\n" 3058 "{\n" 3059 " foobar();\n" 3060 "}", 3061 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 3062 EXPECT_EQ( 3063 "if (foo) {\n" 3064 " bar();\n" 3065 "} else if (baz ||\n" 3066 " quux)\n" 3067 "{\n" 3068 " foobar();\n" 3069 "} else {\n" 3070 " barbaz();\n" 3071 "}", 3072 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3073 Style)); 3074 EXPECT_EQ("for (int i = 0;\n" 3075 " i < 10; ++i)\n" 3076 "{\n" 3077 " foo();\n" 3078 "}", 3079 format("for(int i=0;i<10;++i){foo();}", Style)); 3080 EXPECT_EQ("foreach (int i,\n" 3081 " list)\n" 3082 "{\n" 3083 " foo();\n" 3084 "}", 3085 format("foreach(int i, list){foo();}", Style)); 3086 Style.ColumnLimit = 3087 40; // to concentrate at brace wrapping, not line wrap due to column limit 3088 EXPECT_EQ("foreach (int i, list) {\n" 3089 " foo();\n" 3090 "}", 3091 format("foreach(int i, list){foo();}", Style)); 3092 Style.ColumnLimit = 3093 20; // to concentrate at brace wrapping, not line wrap due to column limit 3094 EXPECT_EQ("while (foo || bar ||\n" 3095 " baz)\n" 3096 "{\n" 3097 " quux();\n" 3098 "}", 3099 format("while(foo||bar||baz){quux();}", Style)); 3100 EXPECT_EQ("switch (\n" 3101 " foo = barbaz)\n" 3102 "{\n" 3103 "case quux:\n" 3104 " return;\n" 3105 "}", 3106 format("switch(foo=barbaz){case quux:return;}", Style)); 3107 EXPECT_EQ("try {\n" 3108 " foo();\n" 3109 "} catch (\n" 3110 " Exception &bar)\n" 3111 "{\n" 3112 " baz();\n" 3113 "}", 3114 format("try{foo();}catch(Exception&bar){baz();}", Style)); 3115 Style.ColumnLimit = 3116 40; // to concentrate at brace wrapping, not line wrap due to column limit 3117 EXPECT_EQ("try {\n" 3118 " foo();\n" 3119 "} catch (Exception &bar) {\n" 3120 " baz();\n" 3121 "}", 3122 format("try{foo();}catch(Exception&bar){baz();}", Style)); 3123 Style.ColumnLimit = 3124 20; // to concentrate at brace wrapping, not line wrap due to column limit 3125 3126 Style.BraceWrapping.BeforeElse = true; 3127 EXPECT_EQ( 3128 "if (foo) {\n" 3129 " bar();\n" 3130 "}\n" 3131 "else if (baz ||\n" 3132 " quux)\n" 3133 "{\n" 3134 " foobar();\n" 3135 "}\n" 3136 "else {\n" 3137 " barbaz();\n" 3138 "}", 3139 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3140 Style)); 3141 3142 Style.BraceWrapping.BeforeCatch = true; 3143 EXPECT_EQ("try {\n" 3144 " foo();\n" 3145 "}\n" 3146 "catch (...) {\n" 3147 " baz();\n" 3148 "}", 3149 format("try{foo();}catch(...){baz();}", Style)); 3150 3151 Style.BraceWrapping.AfterFunction = true; 3152 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3153 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3154 Style.ColumnLimit = 80; 3155 verifyFormat("void shortfunction() { bar(); }", Style); 3156 3157 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3158 verifyFormat("void shortfunction()\n" 3159 "{\n" 3160 " bar();\n" 3161 "}", 3162 Style); 3163 } 3164 3165 TEST_F(FormatTest, BeforeWhile) { 3166 FormatStyle Style = getLLVMStyle(); 3167 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3168 3169 verifyFormat("do {\n" 3170 " foo();\n" 3171 "} while (1);", 3172 Style); 3173 Style.BraceWrapping.BeforeWhile = true; 3174 verifyFormat("do {\n" 3175 " foo();\n" 3176 "}\n" 3177 "while (1);", 3178 Style); 3179 } 3180 3181 //===----------------------------------------------------------------------===// 3182 // Tests for classes, namespaces, etc. 3183 //===----------------------------------------------------------------------===// 3184 3185 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 3186 verifyFormat("class A {};"); 3187 } 3188 3189 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 3190 verifyFormat("class A {\n" 3191 "public:\n" 3192 "public: // comment\n" 3193 "protected:\n" 3194 "private:\n" 3195 " void f() {}\n" 3196 "};"); 3197 verifyFormat("export class A {\n" 3198 "public:\n" 3199 "public: // comment\n" 3200 "protected:\n" 3201 "private:\n" 3202 " void f() {}\n" 3203 "};"); 3204 verifyGoogleFormat("class A {\n" 3205 " public:\n" 3206 " protected:\n" 3207 " private:\n" 3208 " void f() {}\n" 3209 "};"); 3210 verifyGoogleFormat("export class A {\n" 3211 " public:\n" 3212 " protected:\n" 3213 " private:\n" 3214 " void f() {}\n" 3215 "};"); 3216 verifyFormat("class A {\n" 3217 "public slots:\n" 3218 " void f1() {}\n" 3219 "public Q_SLOTS:\n" 3220 " void f2() {}\n" 3221 "protected slots:\n" 3222 " void f3() {}\n" 3223 "protected Q_SLOTS:\n" 3224 " void f4() {}\n" 3225 "private slots:\n" 3226 " void f5() {}\n" 3227 "private Q_SLOTS:\n" 3228 " void f6() {}\n" 3229 "signals:\n" 3230 " void g1();\n" 3231 "Q_SIGNALS:\n" 3232 " void g2();\n" 3233 "};"); 3234 3235 // Don't interpret 'signals' the wrong way. 3236 verifyFormat("signals.set();"); 3237 verifyFormat("for (Signals signals : f()) {\n}"); 3238 verifyFormat("{\n" 3239 " signals.set(); // This needs indentation.\n" 3240 "}"); 3241 verifyFormat("void f() {\n" 3242 "label:\n" 3243 " signals.baz();\n" 3244 "}"); 3245 verifyFormat("private[1];"); 3246 verifyFormat("testArray[public] = 1;"); 3247 verifyFormat("public();"); 3248 verifyFormat("myFunc(public);"); 3249 verifyFormat("std::vector<int> testVec = {private};"); 3250 verifyFormat("private.p = 1;"); 3251 verifyFormat("void function(private...){};"); 3252 verifyFormat("if (private && public)\n"); 3253 verifyFormat("private &= true;"); 3254 verifyFormat("int x = private * public;"); 3255 verifyFormat("public *= private;"); 3256 verifyFormat("int x = public + private;"); 3257 verifyFormat("private++;"); 3258 verifyFormat("++private;"); 3259 verifyFormat("public += private;"); 3260 verifyFormat("public = public - private;"); 3261 verifyFormat("public->foo();"); 3262 verifyFormat("private--;"); 3263 verifyFormat("--private;"); 3264 verifyFormat("public -= 1;"); 3265 verifyFormat("if (!private && !public)\n"); 3266 verifyFormat("public != private;"); 3267 verifyFormat("int x = public / private;"); 3268 verifyFormat("public /= 2;"); 3269 verifyFormat("public = public % 2;"); 3270 verifyFormat("public %= 2;"); 3271 verifyFormat("if (public < private)\n"); 3272 verifyFormat("public << private;"); 3273 verifyFormat("public <<= private;"); 3274 verifyFormat("if (public > private)\n"); 3275 verifyFormat("public >> private;"); 3276 verifyFormat("public >>= private;"); 3277 verifyFormat("public ^ private;"); 3278 verifyFormat("public ^= private;"); 3279 verifyFormat("public | private;"); 3280 verifyFormat("public |= private;"); 3281 verifyFormat("auto x = private ? 1 : 2;"); 3282 verifyFormat("if (public == private)\n"); 3283 verifyFormat("void foo(public, private)"); 3284 verifyFormat("public::foo();"); 3285 } 3286 3287 TEST_F(FormatTest, SeparatesLogicalBlocks) { 3288 EXPECT_EQ("class A {\n" 3289 "public:\n" 3290 " void f();\n" 3291 "\n" 3292 "private:\n" 3293 " void g() {}\n" 3294 " // test\n" 3295 "protected:\n" 3296 " int h;\n" 3297 "};", 3298 format("class A {\n" 3299 "public:\n" 3300 "void f();\n" 3301 "private:\n" 3302 "void g() {}\n" 3303 "// test\n" 3304 "protected:\n" 3305 "int h;\n" 3306 "};")); 3307 EXPECT_EQ("class A {\n" 3308 "protected:\n" 3309 "public:\n" 3310 " void f();\n" 3311 "};", 3312 format("class A {\n" 3313 "protected:\n" 3314 "\n" 3315 "public:\n" 3316 "\n" 3317 " void f();\n" 3318 "};")); 3319 3320 // Even ensure proper spacing inside macros. 3321 EXPECT_EQ("#define B \\\n" 3322 " class A { \\\n" 3323 " protected: \\\n" 3324 " public: \\\n" 3325 " void f(); \\\n" 3326 " };", 3327 format("#define B \\\n" 3328 " class A { \\\n" 3329 " protected: \\\n" 3330 " \\\n" 3331 " public: \\\n" 3332 " \\\n" 3333 " void f(); \\\n" 3334 " };", 3335 getGoogleStyle())); 3336 // But don't remove empty lines after macros ending in access specifiers. 3337 EXPECT_EQ("#define A private:\n" 3338 "\n" 3339 "int i;", 3340 format("#define A private:\n" 3341 "\n" 3342 "int i;")); 3343 } 3344 3345 TEST_F(FormatTest, FormatsClasses) { 3346 verifyFormat("class A : public B {};"); 3347 verifyFormat("class A : public ::B {};"); 3348 3349 verifyFormat( 3350 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3351 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3352 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3353 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3354 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3355 verifyFormat( 3356 "class A : public B, public C, public D, public E, public F {};"); 3357 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3358 " public C,\n" 3359 " public D,\n" 3360 " public E,\n" 3361 " public F,\n" 3362 " public G {};"); 3363 3364 verifyFormat("class\n" 3365 " ReallyReallyLongClassName {\n" 3366 " int i;\n" 3367 "};", 3368 getLLVMStyleWithColumns(32)); 3369 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3370 " aaaaaaaaaaaaaaaa> {};"); 3371 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3372 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3373 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3374 verifyFormat("template <class R, class C>\n" 3375 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3376 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3377 verifyFormat("class ::A::B {};"); 3378 } 3379 3380 TEST_F(FormatTest, BreakInheritanceStyle) { 3381 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3382 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3383 FormatStyle::BILS_BeforeComma; 3384 verifyFormat("class MyClass : public X {};", 3385 StyleWithInheritanceBreakBeforeComma); 3386 verifyFormat("class MyClass\n" 3387 " : public X\n" 3388 " , public Y {};", 3389 StyleWithInheritanceBreakBeforeComma); 3390 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3391 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3392 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3393 StyleWithInheritanceBreakBeforeComma); 3394 verifyFormat("struct aaaaaaaaaaaaa\n" 3395 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3396 " aaaaaaaaaaaaaaaa> {};", 3397 StyleWithInheritanceBreakBeforeComma); 3398 3399 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3400 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3401 FormatStyle::BILS_AfterColon; 3402 verifyFormat("class MyClass : public X {};", 3403 StyleWithInheritanceBreakAfterColon); 3404 verifyFormat("class MyClass : public X, public Y {};", 3405 StyleWithInheritanceBreakAfterColon); 3406 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3407 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3408 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3409 StyleWithInheritanceBreakAfterColon); 3410 verifyFormat("struct aaaaaaaaaaaaa :\n" 3411 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3412 " aaaaaaaaaaaaaaaa> {};", 3413 StyleWithInheritanceBreakAfterColon); 3414 3415 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3416 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3417 FormatStyle::BILS_AfterComma; 3418 verifyFormat("class MyClass : public X {};", 3419 StyleWithInheritanceBreakAfterComma); 3420 verifyFormat("class MyClass : public X,\n" 3421 " public Y {};", 3422 StyleWithInheritanceBreakAfterComma); 3423 verifyFormat( 3424 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3425 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3426 "{};", 3427 StyleWithInheritanceBreakAfterComma); 3428 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3429 " aaaaaaaaaaaaaaaa> {};", 3430 StyleWithInheritanceBreakAfterComma); 3431 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3432 " : public OnceBreak,\n" 3433 " public AlwaysBreak,\n" 3434 " EvenBasesFitInOneLine {};", 3435 StyleWithInheritanceBreakAfterComma); 3436 } 3437 3438 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) { 3439 verifyFormat("class A {\n} a, b;"); 3440 verifyFormat("struct A {\n} a, b;"); 3441 verifyFormat("union A {\n} a, b;"); 3442 3443 verifyFormat("constexpr class A {\n} a, b;"); 3444 verifyFormat("constexpr struct A {\n} a, b;"); 3445 verifyFormat("constexpr union A {\n} a, b;"); 3446 3447 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace"); 3448 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace"); 3449 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace"); 3450 3451 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace"); 3452 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace"); 3453 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace"); 3454 3455 verifyFormat("namespace ns {\n" 3456 "class {\n" 3457 "} a, b;\n" 3458 "} // namespace ns"); 3459 verifyFormat("namespace ns {\n" 3460 "const class {\n" 3461 "} a, b;\n" 3462 "} // namespace ns"); 3463 verifyFormat("namespace ns {\n" 3464 "constexpr class C {\n" 3465 "} a, b;\n" 3466 "} // namespace ns"); 3467 verifyFormat("namespace ns {\n" 3468 "class { /* comment */\n" 3469 "} a, b;\n" 3470 "} // namespace ns"); 3471 verifyFormat("namespace ns {\n" 3472 "const class { /* comment */\n" 3473 "} a, b;\n" 3474 "} // namespace ns"); 3475 } 3476 3477 TEST_F(FormatTest, FormatsEnum) { 3478 verifyFormat("enum {\n" 3479 " Zero,\n" 3480 " One = 1,\n" 3481 " Two = One + 1,\n" 3482 " Three = (One + Two),\n" 3483 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3484 " Five = (One, Two, Three, Four, 5)\n" 3485 "};"); 3486 verifyGoogleFormat("enum {\n" 3487 " Zero,\n" 3488 " One = 1,\n" 3489 " Two = One + 1,\n" 3490 " Three = (One + Two),\n" 3491 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3492 " Five = (One, Two, Three, Four, 5)\n" 3493 "};"); 3494 verifyFormat("enum Enum {};"); 3495 verifyFormat("enum {};"); 3496 verifyFormat("enum X E {} d;"); 3497 verifyFormat("enum __attribute__((...)) E {} d;"); 3498 verifyFormat("enum __declspec__((...)) E {} d;"); 3499 verifyFormat("enum {\n" 3500 " Bar = Foo<int, int>::value\n" 3501 "};", 3502 getLLVMStyleWithColumns(30)); 3503 3504 verifyFormat("enum ShortEnum { A, B, C };"); 3505 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3506 3507 EXPECT_EQ("enum KeepEmptyLines {\n" 3508 " ONE,\n" 3509 "\n" 3510 " TWO,\n" 3511 "\n" 3512 " THREE\n" 3513 "}", 3514 format("enum KeepEmptyLines {\n" 3515 " ONE,\n" 3516 "\n" 3517 " TWO,\n" 3518 "\n" 3519 "\n" 3520 " THREE\n" 3521 "}")); 3522 verifyFormat("enum E { // comment\n" 3523 " ONE,\n" 3524 " TWO\n" 3525 "};\n" 3526 "int i;"); 3527 3528 FormatStyle EightIndent = getLLVMStyle(); 3529 EightIndent.IndentWidth = 8; 3530 verifyFormat("enum {\n" 3531 " VOID,\n" 3532 " CHAR,\n" 3533 " SHORT,\n" 3534 " INT,\n" 3535 " LONG,\n" 3536 " SIGNED,\n" 3537 " UNSIGNED,\n" 3538 " BOOL,\n" 3539 " FLOAT,\n" 3540 " DOUBLE,\n" 3541 " COMPLEX\n" 3542 "};", 3543 EightIndent); 3544 3545 // Not enums. 3546 verifyFormat("enum X f() {\n" 3547 " a();\n" 3548 " return 42;\n" 3549 "}"); 3550 verifyFormat("enum X Type::f() {\n" 3551 " a();\n" 3552 " return 42;\n" 3553 "}"); 3554 verifyFormat("enum ::X f() {\n" 3555 " a();\n" 3556 " return 42;\n" 3557 "}"); 3558 verifyFormat("enum ns::X f() {\n" 3559 " a();\n" 3560 " return 42;\n" 3561 "}"); 3562 } 3563 3564 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3565 verifyFormat("enum Type {\n" 3566 " One = 0; // These semicolons should be commas.\n" 3567 " Two = 1;\n" 3568 "};"); 3569 verifyFormat("namespace n {\n" 3570 "enum Type {\n" 3571 " One,\n" 3572 " Two, // missing };\n" 3573 " int i;\n" 3574 "}\n" 3575 "void g() {}"); 3576 } 3577 3578 TEST_F(FormatTest, FormatsEnumStruct) { 3579 verifyFormat("enum struct {\n" 3580 " Zero,\n" 3581 " One = 1,\n" 3582 " Two = One + 1,\n" 3583 " Three = (One + Two),\n" 3584 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3585 " Five = (One, Two, Three, Four, 5)\n" 3586 "};"); 3587 verifyFormat("enum struct Enum {};"); 3588 verifyFormat("enum struct {};"); 3589 verifyFormat("enum struct X E {} d;"); 3590 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3591 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3592 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3593 } 3594 3595 TEST_F(FormatTest, FormatsEnumClass) { 3596 verifyFormat("enum class {\n" 3597 " Zero,\n" 3598 " One = 1,\n" 3599 " Two = One + 1,\n" 3600 " Three = (One + Two),\n" 3601 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3602 " Five = (One, Two, Three, Four, 5)\n" 3603 "};"); 3604 verifyFormat("enum class Enum {};"); 3605 verifyFormat("enum class {};"); 3606 verifyFormat("enum class X E {} d;"); 3607 verifyFormat("enum class __attribute__((...)) E {} d;"); 3608 verifyFormat("enum class __declspec__((...)) E {} d;"); 3609 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3610 } 3611 3612 TEST_F(FormatTest, FormatsEnumTypes) { 3613 verifyFormat("enum X : int {\n" 3614 " A, // Force multiple lines.\n" 3615 " B\n" 3616 "};"); 3617 verifyFormat("enum X : int { A, B };"); 3618 verifyFormat("enum X : std::uint32_t { A, B };"); 3619 } 3620 3621 TEST_F(FormatTest, FormatsTypedefEnum) { 3622 FormatStyle Style = getLLVMStyleWithColumns(40); 3623 verifyFormat("typedef enum {} EmptyEnum;"); 3624 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3625 verifyFormat("typedef enum {\n" 3626 " ZERO = 0,\n" 3627 " ONE = 1,\n" 3628 " TWO = 2,\n" 3629 " THREE = 3\n" 3630 "} LongEnum;", 3631 Style); 3632 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3633 Style.BraceWrapping.AfterEnum = true; 3634 verifyFormat("typedef enum {} EmptyEnum;"); 3635 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3636 verifyFormat("typedef enum\n" 3637 "{\n" 3638 " ZERO = 0,\n" 3639 " ONE = 1,\n" 3640 " TWO = 2,\n" 3641 " THREE = 3\n" 3642 "} LongEnum;", 3643 Style); 3644 } 3645 3646 TEST_F(FormatTest, FormatsNSEnums) { 3647 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3648 verifyGoogleFormat( 3649 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3650 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3651 " // Information about someDecentlyLongValue.\n" 3652 " someDecentlyLongValue,\n" 3653 " // Information about anotherDecentlyLongValue.\n" 3654 " anotherDecentlyLongValue,\n" 3655 " // Information about aThirdDecentlyLongValue.\n" 3656 " aThirdDecentlyLongValue\n" 3657 "};"); 3658 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3659 " // Information about someDecentlyLongValue.\n" 3660 " someDecentlyLongValue,\n" 3661 " // Information about anotherDecentlyLongValue.\n" 3662 " anotherDecentlyLongValue,\n" 3663 " // Information about aThirdDecentlyLongValue.\n" 3664 " aThirdDecentlyLongValue\n" 3665 "};"); 3666 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3667 " a = 1,\n" 3668 " b = 2,\n" 3669 " c = 3,\n" 3670 "};"); 3671 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3672 " a = 1,\n" 3673 " b = 2,\n" 3674 " c = 3,\n" 3675 "};"); 3676 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3677 " a = 1,\n" 3678 " b = 2,\n" 3679 " c = 3,\n" 3680 "};"); 3681 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3682 " a = 1,\n" 3683 " b = 2,\n" 3684 " c = 3,\n" 3685 "};"); 3686 } 3687 3688 TEST_F(FormatTest, FormatsBitfields) { 3689 verifyFormat("struct Bitfields {\n" 3690 " unsigned sClass : 8;\n" 3691 " unsigned ValueKind : 2;\n" 3692 "};"); 3693 verifyFormat("struct A {\n" 3694 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3695 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3696 "};"); 3697 verifyFormat("struct MyStruct {\n" 3698 " uchar data;\n" 3699 " uchar : 8;\n" 3700 " uchar : 8;\n" 3701 " uchar other;\n" 3702 "};"); 3703 FormatStyle Style = getLLVMStyle(); 3704 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3705 verifyFormat("struct Bitfields {\n" 3706 " unsigned sClass:8;\n" 3707 " unsigned ValueKind:2;\n" 3708 " uchar other;\n" 3709 "};", 3710 Style); 3711 verifyFormat("struct A {\n" 3712 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3713 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3714 "};", 3715 Style); 3716 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3717 verifyFormat("struct Bitfields {\n" 3718 " unsigned sClass :8;\n" 3719 " unsigned ValueKind :2;\n" 3720 " uchar other;\n" 3721 "};", 3722 Style); 3723 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3724 verifyFormat("struct Bitfields {\n" 3725 " unsigned sClass: 8;\n" 3726 " unsigned ValueKind: 2;\n" 3727 " uchar other;\n" 3728 "};", 3729 Style); 3730 } 3731 3732 TEST_F(FormatTest, FormatsNamespaces) { 3733 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3734 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3735 3736 verifyFormat("namespace some_namespace {\n" 3737 "class A {};\n" 3738 "void f() { f(); }\n" 3739 "}", 3740 LLVMWithNoNamespaceFix); 3741 verifyFormat("namespace N::inline D {\n" 3742 "class A {};\n" 3743 "void f() { f(); }\n" 3744 "}", 3745 LLVMWithNoNamespaceFix); 3746 verifyFormat("namespace N::inline D::E {\n" 3747 "class A {};\n" 3748 "void f() { f(); }\n" 3749 "}", 3750 LLVMWithNoNamespaceFix); 3751 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3752 "class A {};\n" 3753 "void f() { f(); }\n" 3754 "}", 3755 LLVMWithNoNamespaceFix); 3756 verifyFormat("/* something */ namespace some_namespace {\n" 3757 "class A {};\n" 3758 "void f() { f(); }\n" 3759 "}", 3760 LLVMWithNoNamespaceFix); 3761 verifyFormat("namespace {\n" 3762 "class A {};\n" 3763 "void f() { f(); }\n" 3764 "}", 3765 LLVMWithNoNamespaceFix); 3766 verifyFormat("/* something */ namespace {\n" 3767 "class A {};\n" 3768 "void f() { f(); }\n" 3769 "}", 3770 LLVMWithNoNamespaceFix); 3771 verifyFormat("inline namespace X {\n" 3772 "class A {};\n" 3773 "void f() { f(); }\n" 3774 "}", 3775 LLVMWithNoNamespaceFix); 3776 verifyFormat("/* something */ inline namespace X {\n" 3777 "class A {};\n" 3778 "void f() { f(); }\n" 3779 "}", 3780 LLVMWithNoNamespaceFix); 3781 verifyFormat("export namespace X {\n" 3782 "class A {};\n" 3783 "void f() { f(); }\n" 3784 "}", 3785 LLVMWithNoNamespaceFix); 3786 verifyFormat("using namespace some_namespace;\n" 3787 "class A {};\n" 3788 "void f() { f(); }", 3789 LLVMWithNoNamespaceFix); 3790 3791 // This code is more common than we thought; if we 3792 // layout this correctly the semicolon will go into 3793 // its own line, which is undesirable. 3794 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3795 verifyFormat("namespace {\n" 3796 "class A {};\n" 3797 "};", 3798 LLVMWithNoNamespaceFix); 3799 3800 verifyFormat("namespace {\n" 3801 "int SomeVariable = 0; // comment\n" 3802 "} // namespace", 3803 LLVMWithNoNamespaceFix); 3804 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3805 "#define HEADER_GUARD\n" 3806 "namespace my_namespace {\n" 3807 "int i;\n" 3808 "} // my_namespace\n" 3809 "#endif // HEADER_GUARD", 3810 format("#ifndef HEADER_GUARD\n" 3811 " #define HEADER_GUARD\n" 3812 " namespace my_namespace {\n" 3813 "int i;\n" 3814 "} // my_namespace\n" 3815 "#endif // HEADER_GUARD", 3816 LLVMWithNoNamespaceFix)); 3817 3818 EXPECT_EQ("namespace A::B {\n" 3819 "class C {};\n" 3820 "}", 3821 format("namespace A::B {\n" 3822 "class C {};\n" 3823 "}", 3824 LLVMWithNoNamespaceFix)); 3825 3826 FormatStyle Style = getLLVMStyle(); 3827 Style.NamespaceIndentation = FormatStyle::NI_All; 3828 EXPECT_EQ("namespace out {\n" 3829 " int i;\n" 3830 " namespace in {\n" 3831 " int i;\n" 3832 " } // namespace in\n" 3833 "} // namespace out", 3834 format("namespace out {\n" 3835 "int i;\n" 3836 "namespace in {\n" 3837 "int i;\n" 3838 "} // namespace in\n" 3839 "} // namespace out", 3840 Style)); 3841 3842 FormatStyle ShortInlineFunctions = getLLVMStyle(); 3843 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All; 3844 ShortInlineFunctions.AllowShortFunctionsOnASingleLine = 3845 FormatStyle::SFS_Inline; 3846 verifyFormat("namespace {\n" 3847 " void f() {\n" 3848 " return;\n" 3849 " }\n" 3850 "} // namespace\n", 3851 ShortInlineFunctions); 3852 verifyFormat("namespace { /* comment */\n" 3853 " void f() {\n" 3854 " return;\n" 3855 " }\n" 3856 "} // namespace\n", 3857 ShortInlineFunctions); 3858 verifyFormat("namespace { // comment\n" 3859 " void f() {\n" 3860 " return;\n" 3861 " }\n" 3862 "} // namespace\n", 3863 ShortInlineFunctions); 3864 verifyFormat("namespace {\n" 3865 " int some_int;\n" 3866 " void f() {\n" 3867 " return;\n" 3868 " }\n" 3869 "} // namespace\n", 3870 ShortInlineFunctions); 3871 verifyFormat("namespace interface {\n" 3872 " void f() {\n" 3873 " return;\n" 3874 " }\n" 3875 "} // namespace interface\n", 3876 ShortInlineFunctions); 3877 verifyFormat("namespace {\n" 3878 " class X {\n" 3879 " void f() { return; }\n" 3880 " };\n" 3881 "} // namespace\n", 3882 ShortInlineFunctions); 3883 verifyFormat("namespace {\n" 3884 " class X { /* comment */\n" 3885 " void f() { return; }\n" 3886 " };\n" 3887 "} // namespace\n", 3888 ShortInlineFunctions); 3889 verifyFormat("namespace {\n" 3890 " class X { // comment\n" 3891 " void f() { return; }\n" 3892 " };\n" 3893 "} // namespace\n", 3894 ShortInlineFunctions); 3895 verifyFormat("namespace {\n" 3896 " struct X {\n" 3897 " void f() { return; }\n" 3898 " };\n" 3899 "} // namespace\n", 3900 ShortInlineFunctions); 3901 verifyFormat("namespace {\n" 3902 " union X {\n" 3903 " void f() { return; }\n" 3904 " };\n" 3905 "} // namespace\n", 3906 ShortInlineFunctions); 3907 verifyFormat("extern \"C\" {\n" 3908 "void f() {\n" 3909 " return;\n" 3910 "}\n" 3911 "} // namespace\n", 3912 ShortInlineFunctions); 3913 verifyFormat("namespace {\n" 3914 " class X {\n" 3915 " void f() { return; }\n" 3916 " } x;\n" 3917 "} // namespace\n", 3918 ShortInlineFunctions); 3919 verifyFormat("namespace {\n" 3920 " [[nodiscard]] class X {\n" 3921 " void f() { return; }\n" 3922 " };\n" 3923 "} // namespace\n", 3924 ShortInlineFunctions); 3925 verifyFormat("namespace {\n" 3926 " static class X {\n" 3927 " void f() { return; }\n" 3928 " } x;\n" 3929 "} // namespace\n", 3930 ShortInlineFunctions); 3931 verifyFormat("namespace {\n" 3932 " constexpr class X {\n" 3933 " void f() { return; }\n" 3934 " } x;\n" 3935 "} // namespace\n", 3936 ShortInlineFunctions); 3937 3938 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent; 3939 verifyFormat("extern \"C\" {\n" 3940 " void f() {\n" 3941 " return;\n" 3942 " }\n" 3943 "} // namespace\n", 3944 ShortInlineFunctions); 3945 3946 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3947 EXPECT_EQ("namespace out {\n" 3948 "int i;\n" 3949 "namespace in {\n" 3950 " int i;\n" 3951 "} // namespace in\n" 3952 "} // namespace out", 3953 format("namespace out {\n" 3954 "int i;\n" 3955 "namespace in {\n" 3956 "int i;\n" 3957 "} // namespace in\n" 3958 "} // namespace out", 3959 Style)); 3960 3961 Style.NamespaceIndentation = FormatStyle::NI_None; 3962 verifyFormat("template <class T>\n" 3963 "concept a_concept = X<>;\n" 3964 "namespace B {\n" 3965 "struct b_struct {};\n" 3966 "} // namespace B\n", 3967 Style); 3968 verifyFormat("template <int I>\n" 3969 "constexpr void foo()\n" 3970 " requires(I == 42)\n" 3971 "{}\n" 3972 "namespace ns {\n" 3973 "void foo() {}\n" 3974 "} // namespace ns\n", 3975 Style); 3976 } 3977 3978 TEST_F(FormatTest, NamespaceMacros) { 3979 FormatStyle Style = getLLVMStyle(); 3980 Style.NamespaceMacros.push_back("TESTSUITE"); 3981 3982 verifyFormat("TESTSUITE(A) {\n" 3983 "int foo();\n" 3984 "} // TESTSUITE(A)", 3985 Style); 3986 3987 verifyFormat("TESTSUITE(A, B) {\n" 3988 "int foo();\n" 3989 "} // TESTSUITE(A)", 3990 Style); 3991 3992 // Properly indent according to NamespaceIndentation style 3993 Style.NamespaceIndentation = FormatStyle::NI_All; 3994 verifyFormat("TESTSUITE(A) {\n" 3995 " int foo();\n" 3996 "} // TESTSUITE(A)", 3997 Style); 3998 verifyFormat("TESTSUITE(A) {\n" 3999 " namespace B {\n" 4000 " int foo();\n" 4001 " } // namespace B\n" 4002 "} // TESTSUITE(A)", 4003 Style); 4004 verifyFormat("namespace A {\n" 4005 " TESTSUITE(B) {\n" 4006 " int foo();\n" 4007 " } // TESTSUITE(B)\n" 4008 "} // namespace A", 4009 Style); 4010 4011 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4012 verifyFormat("TESTSUITE(A) {\n" 4013 "TESTSUITE(B) {\n" 4014 " int foo();\n" 4015 "} // TESTSUITE(B)\n" 4016 "} // TESTSUITE(A)", 4017 Style); 4018 verifyFormat("TESTSUITE(A) {\n" 4019 "namespace B {\n" 4020 " int foo();\n" 4021 "} // namespace B\n" 4022 "} // TESTSUITE(A)", 4023 Style); 4024 verifyFormat("namespace A {\n" 4025 "TESTSUITE(B) {\n" 4026 " int foo();\n" 4027 "} // TESTSUITE(B)\n" 4028 "} // namespace A", 4029 Style); 4030 4031 // Properly merge namespace-macros blocks in CompactNamespaces mode 4032 Style.NamespaceIndentation = FormatStyle::NI_None; 4033 Style.CompactNamespaces = true; 4034 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 4035 "}} // TESTSUITE(A::B)", 4036 Style); 4037 4038 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 4039 "}} // TESTSUITE(out::in)", 4040 format("TESTSUITE(out) {\n" 4041 "TESTSUITE(in) {\n" 4042 "} // TESTSUITE(in)\n" 4043 "} // TESTSUITE(out)", 4044 Style)); 4045 4046 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 4047 "}} // TESTSUITE(out::in)", 4048 format("TESTSUITE(out) {\n" 4049 "TESTSUITE(in) {\n" 4050 "} // TESTSUITE(in)\n" 4051 "} // TESTSUITE(out)", 4052 Style)); 4053 4054 // Do not merge different namespaces/macros 4055 EXPECT_EQ("namespace out {\n" 4056 "TESTSUITE(in) {\n" 4057 "} // TESTSUITE(in)\n" 4058 "} // namespace out", 4059 format("namespace out {\n" 4060 "TESTSUITE(in) {\n" 4061 "} // TESTSUITE(in)\n" 4062 "} // namespace out", 4063 Style)); 4064 EXPECT_EQ("TESTSUITE(out) {\n" 4065 "namespace in {\n" 4066 "} // namespace in\n" 4067 "} // TESTSUITE(out)", 4068 format("TESTSUITE(out) {\n" 4069 "namespace in {\n" 4070 "} // namespace in\n" 4071 "} // TESTSUITE(out)", 4072 Style)); 4073 Style.NamespaceMacros.push_back("FOOBAR"); 4074 EXPECT_EQ("TESTSUITE(out) {\n" 4075 "FOOBAR(in) {\n" 4076 "} // FOOBAR(in)\n" 4077 "} // TESTSUITE(out)", 4078 format("TESTSUITE(out) {\n" 4079 "FOOBAR(in) {\n" 4080 "} // FOOBAR(in)\n" 4081 "} // TESTSUITE(out)", 4082 Style)); 4083 } 4084 4085 TEST_F(FormatTest, FormatsCompactNamespaces) { 4086 FormatStyle Style = getLLVMStyle(); 4087 Style.CompactNamespaces = true; 4088 Style.NamespaceMacros.push_back("TESTSUITE"); 4089 4090 verifyFormat("namespace A { namespace B {\n" 4091 "}} // namespace A::B", 4092 Style); 4093 4094 EXPECT_EQ("namespace out { namespace in {\n" 4095 "}} // namespace out::in", 4096 format("namespace out {\n" 4097 "namespace in {\n" 4098 "} // namespace in\n" 4099 "} // namespace out", 4100 Style)); 4101 4102 // Only namespaces which have both consecutive opening and end get compacted 4103 EXPECT_EQ("namespace out {\n" 4104 "namespace in1 {\n" 4105 "} // namespace in1\n" 4106 "namespace in2 {\n" 4107 "} // namespace in2\n" 4108 "} // namespace out", 4109 format("namespace out {\n" 4110 "namespace in1 {\n" 4111 "} // namespace in1\n" 4112 "namespace in2 {\n" 4113 "} // namespace in2\n" 4114 "} // namespace out", 4115 Style)); 4116 4117 EXPECT_EQ("namespace out {\n" 4118 "int i;\n" 4119 "namespace in {\n" 4120 "int j;\n" 4121 "} // namespace in\n" 4122 "int k;\n" 4123 "} // namespace out", 4124 format("namespace out { int i;\n" 4125 "namespace in { int j; } // namespace in\n" 4126 "int k; } // namespace out", 4127 Style)); 4128 4129 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 4130 "}}} // namespace A::B::C\n", 4131 format("namespace A { namespace B {\n" 4132 "namespace C {\n" 4133 "}} // namespace B::C\n" 4134 "} // namespace A\n", 4135 Style)); 4136 4137 Style.ColumnLimit = 40; 4138 EXPECT_EQ("namespace aaaaaaaaaa {\n" 4139 "namespace bbbbbbbbbb {\n" 4140 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 4141 format("namespace aaaaaaaaaa {\n" 4142 "namespace bbbbbbbbbb {\n" 4143 "} // namespace bbbbbbbbbb\n" 4144 "} // namespace aaaaaaaaaa", 4145 Style)); 4146 4147 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 4148 "namespace cccccc {\n" 4149 "}}} // namespace aaaaaa::bbbbbb::cccccc", 4150 format("namespace aaaaaa {\n" 4151 "namespace bbbbbb {\n" 4152 "namespace cccccc {\n" 4153 "} // namespace cccccc\n" 4154 "} // namespace bbbbbb\n" 4155 "} // namespace aaaaaa", 4156 Style)); 4157 Style.ColumnLimit = 80; 4158 4159 // Extra semicolon after 'inner' closing brace prevents merging 4160 EXPECT_EQ("namespace out { namespace in {\n" 4161 "}; } // namespace out::in", 4162 format("namespace out {\n" 4163 "namespace in {\n" 4164 "}; // namespace in\n" 4165 "} // namespace out", 4166 Style)); 4167 4168 // Extra semicolon after 'outer' closing brace is conserved 4169 EXPECT_EQ("namespace out { namespace in {\n" 4170 "}}; // namespace out::in", 4171 format("namespace out {\n" 4172 "namespace in {\n" 4173 "} // namespace in\n" 4174 "}; // namespace out", 4175 Style)); 4176 4177 Style.NamespaceIndentation = FormatStyle::NI_All; 4178 EXPECT_EQ("namespace out { namespace in {\n" 4179 " int i;\n" 4180 "}} // namespace out::in", 4181 format("namespace out {\n" 4182 "namespace in {\n" 4183 "int i;\n" 4184 "} // namespace in\n" 4185 "} // namespace out", 4186 Style)); 4187 EXPECT_EQ("namespace out { namespace mid {\n" 4188 " namespace in {\n" 4189 " int j;\n" 4190 " } // namespace in\n" 4191 " int k;\n" 4192 "}} // namespace out::mid", 4193 format("namespace out { namespace mid {\n" 4194 "namespace in { int j; } // namespace in\n" 4195 "int k; }} // namespace out::mid", 4196 Style)); 4197 4198 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4199 EXPECT_EQ("namespace out { namespace in {\n" 4200 " int i;\n" 4201 "}} // namespace out::in", 4202 format("namespace out {\n" 4203 "namespace in {\n" 4204 "int i;\n" 4205 "} // namespace in\n" 4206 "} // namespace out", 4207 Style)); 4208 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 4209 " int i;\n" 4210 "}}} // namespace out::mid::in", 4211 format("namespace out {\n" 4212 "namespace mid {\n" 4213 "namespace in {\n" 4214 "int i;\n" 4215 "} // namespace in\n" 4216 "} // namespace mid\n" 4217 "} // namespace out", 4218 Style)); 4219 4220 Style.CompactNamespaces = true; 4221 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 4222 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4223 Style.BraceWrapping.BeforeLambdaBody = true; 4224 verifyFormat("namespace out { namespace in {\n" 4225 "}} // namespace out::in", 4226 Style); 4227 EXPECT_EQ("namespace out { namespace in {\n" 4228 "}} // namespace out::in", 4229 format("namespace out {\n" 4230 "namespace in {\n" 4231 "} // namespace in\n" 4232 "} // namespace out", 4233 Style)); 4234 } 4235 4236 TEST_F(FormatTest, FormatsExternC) { 4237 verifyFormat("extern \"C\" {\nint a;"); 4238 verifyFormat("extern \"C\" {}"); 4239 verifyFormat("extern \"C\" {\n" 4240 "int foo();\n" 4241 "}"); 4242 verifyFormat("extern \"C\" int foo() {}"); 4243 verifyFormat("extern \"C\" int foo();"); 4244 verifyFormat("extern \"C\" int foo() {\n" 4245 " int i = 42;\n" 4246 " return i;\n" 4247 "}"); 4248 4249 FormatStyle Style = getLLVMStyle(); 4250 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4251 Style.BraceWrapping.AfterFunction = true; 4252 verifyFormat("extern \"C\" int foo() {}", Style); 4253 verifyFormat("extern \"C\" int foo();", Style); 4254 verifyFormat("extern \"C\" int foo()\n" 4255 "{\n" 4256 " int i = 42;\n" 4257 " return i;\n" 4258 "}", 4259 Style); 4260 4261 Style.BraceWrapping.AfterExternBlock = true; 4262 Style.BraceWrapping.SplitEmptyRecord = false; 4263 verifyFormat("extern \"C\"\n" 4264 "{}", 4265 Style); 4266 verifyFormat("extern \"C\"\n" 4267 "{\n" 4268 " int foo();\n" 4269 "}", 4270 Style); 4271 } 4272 4273 TEST_F(FormatTest, IndentExternBlockStyle) { 4274 FormatStyle Style = getLLVMStyle(); 4275 Style.IndentWidth = 2; 4276 4277 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4278 verifyFormat("extern \"C\" { /*9*/\n" 4279 "}", 4280 Style); 4281 verifyFormat("extern \"C\" {\n" 4282 " int foo10();\n" 4283 "}", 4284 Style); 4285 4286 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4287 verifyFormat("extern \"C\" { /*11*/\n" 4288 "}", 4289 Style); 4290 verifyFormat("extern \"C\" {\n" 4291 "int foo12();\n" 4292 "}", 4293 Style); 4294 4295 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4296 Style.BraceWrapping.AfterExternBlock = true; 4297 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4298 verifyFormat("extern \"C\"\n" 4299 "{ /*13*/\n" 4300 "}", 4301 Style); 4302 verifyFormat("extern \"C\"\n{\n" 4303 " int foo14();\n" 4304 "}", 4305 Style); 4306 4307 Style.BraceWrapping.AfterExternBlock = false; 4308 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4309 verifyFormat("extern \"C\" { /*15*/\n" 4310 "}", 4311 Style); 4312 verifyFormat("extern \"C\" {\n" 4313 "int foo16();\n" 4314 "}", 4315 Style); 4316 4317 Style.BraceWrapping.AfterExternBlock = true; 4318 verifyFormat("extern \"C\"\n" 4319 "{ /*13*/\n" 4320 "}", 4321 Style); 4322 verifyFormat("extern \"C\"\n" 4323 "{\n" 4324 "int foo14();\n" 4325 "}", 4326 Style); 4327 4328 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4329 verifyFormat("extern \"C\"\n" 4330 "{ /*13*/\n" 4331 "}", 4332 Style); 4333 verifyFormat("extern \"C\"\n" 4334 "{\n" 4335 " int foo14();\n" 4336 "}", 4337 Style); 4338 } 4339 4340 TEST_F(FormatTest, FormatsInlineASM) { 4341 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 4342 verifyFormat("asm(\"nop\" ::: \"memory\");"); 4343 verifyFormat( 4344 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 4345 " \"cpuid\\n\\t\"\n" 4346 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 4347 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 4348 " : \"a\"(value));"); 4349 EXPECT_EQ( 4350 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4351 " __asm {\n" 4352 " mov edx,[that] // vtable in edx\n" 4353 " mov eax,methodIndex\n" 4354 " call [edx][eax*4] // stdcall\n" 4355 " }\n" 4356 "}", 4357 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4358 " __asm {\n" 4359 " mov edx,[that] // vtable in edx\n" 4360 " mov eax,methodIndex\n" 4361 " call [edx][eax*4] // stdcall\n" 4362 " }\n" 4363 "}")); 4364 EXPECT_EQ("_asm {\n" 4365 " xor eax, eax;\n" 4366 " cpuid;\n" 4367 "}", 4368 format("_asm {\n" 4369 " xor eax, eax;\n" 4370 " cpuid;\n" 4371 "}")); 4372 verifyFormat("void function() {\n" 4373 " // comment\n" 4374 " asm(\"\");\n" 4375 "}"); 4376 EXPECT_EQ("__asm {\n" 4377 "}\n" 4378 "int i;", 4379 format("__asm {\n" 4380 "}\n" 4381 "int i;")); 4382 } 4383 4384 TEST_F(FormatTest, FormatTryCatch) { 4385 verifyFormat("try {\n" 4386 " throw a * b;\n" 4387 "} catch (int a) {\n" 4388 " // Do nothing.\n" 4389 "} catch (...) {\n" 4390 " exit(42);\n" 4391 "}"); 4392 4393 // Function-level try statements. 4394 verifyFormat("int f() try { return 4; } catch (...) {\n" 4395 " return 5;\n" 4396 "}"); 4397 verifyFormat("class A {\n" 4398 " int a;\n" 4399 " A() try : a(0) {\n" 4400 " } catch (...) {\n" 4401 " throw;\n" 4402 " }\n" 4403 "};\n"); 4404 verifyFormat("class A {\n" 4405 " int a;\n" 4406 " A() try : a(0), b{1} {\n" 4407 " } catch (...) {\n" 4408 " throw;\n" 4409 " }\n" 4410 "};\n"); 4411 verifyFormat("class A {\n" 4412 " int a;\n" 4413 " A() try : a(0), b{1}, c{2} {\n" 4414 " } catch (...) {\n" 4415 " throw;\n" 4416 " }\n" 4417 "};\n"); 4418 verifyFormat("class A {\n" 4419 " int a;\n" 4420 " A() try : a(0), b{1}, c{2} {\n" 4421 " { // New scope.\n" 4422 " }\n" 4423 " } catch (...) {\n" 4424 " throw;\n" 4425 " }\n" 4426 "};\n"); 4427 4428 // Incomplete try-catch blocks. 4429 verifyIncompleteFormat("try {} catch ("); 4430 } 4431 4432 TEST_F(FormatTest, FormatTryAsAVariable) { 4433 verifyFormat("int try;"); 4434 verifyFormat("int try, size;"); 4435 verifyFormat("try = foo();"); 4436 verifyFormat("if (try < size) {\n return true;\n}"); 4437 4438 verifyFormat("int catch;"); 4439 verifyFormat("int catch, size;"); 4440 verifyFormat("catch = foo();"); 4441 verifyFormat("if (catch < size) {\n return true;\n}"); 4442 4443 FormatStyle Style = getLLVMStyle(); 4444 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4445 Style.BraceWrapping.AfterFunction = true; 4446 Style.BraceWrapping.BeforeCatch = true; 4447 verifyFormat("try {\n" 4448 " int bar = 1;\n" 4449 "}\n" 4450 "catch (...) {\n" 4451 " int bar = 1;\n" 4452 "}", 4453 Style); 4454 verifyFormat("#if NO_EX\n" 4455 "try\n" 4456 "#endif\n" 4457 "{\n" 4458 "}\n" 4459 "#if NO_EX\n" 4460 "catch (...) {\n" 4461 "}", 4462 Style); 4463 verifyFormat("try /* abc */ {\n" 4464 " int bar = 1;\n" 4465 "}\n" 4466 "catch (...) {\n" 4467 " int bar = 1;\n" 4468 "}", 4469 Style); 4470 verifyFormat("try\n" 4471 "// abc\n" 4472 "{\n" 4473 " int bar = 1;\n" 4474 "}\n" 4475 "catch (...) {\n" 4476 " int bar = 1;\n" 4477 "}", 4478 Style); 4479 } 4480 4481 TEST_F(FormatTest, FormatSEHTryCatch) { 4482 verifyFormat("__try {\n" 4483 " int a = b * c;\n" 4484 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 4485 " // Do nothing.\n" 4486 "}"); 4487 4488 verifyFormat("__try {\n" 4489 " int a = b * c;\n" 4490 "} __finally {\n" 4491 " // Do nothing.\n" 4492 "}"); 4493 4494 verifyFormat("DEBUG({\n" 4495 " __try {\n" 4496 " } __finally {\n" 4497 " }\n" 4498 "});\n"); 4499 } 4500 4501 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 4502 verifyFormat("try {\n" 4503 " f();\n" 4504 "} catch {\n" 4505 " g();\n" 4506 "}"); 4507 verifyFormat("try {\n" 4508 " f();\n" 4509 "} catch (A a) MACRO(x) {\n" 4510 " g();\n" 4511 "} catch (B b) MACRO(x) {\n" 4512 " g();\n" 4513 "}"); 4514 } 4515 4516 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 4517 FormatStyle Style = getLLVMStyle(); 4518 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 4519 FormatStyle::BS_WebKit}) { 4520 Style.BreakBeforeBraces = BraceStyle; 4521 verifyFormat("try {\n" 4522 " // something\n" 4523 "} catch (...) {\n" 4524 " // something\n" 4525 "}", 4526 Style); 4527 } 4528 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4529 verifyFormat("try {\n" 4530 " // something\n" 4531 "}\n" 4532 "catch (...) {\n" 4533 " // something\n" 4534 "}", 4535 Style); 4536 verifyFormat("__try {\n" 4537 " // something\n" 4538 "}\n" 4539 "__finally {\n" 4540 " // something\n" 4541 "}", 4542 Style); 4543 verifyFormat("@try {\n" 4544 " // something\n" 4545 "}\n" 4546 "@finally {\n" 4547 " // something\n" 4548 "}", 4549 Style); 4550 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4551 verifyFormat("try\n" 4552 "{\n" 4553 " // something\n" 4554 "}\n" 4555 "catch (...)\n" 4556 "{\n" 4557 " // something\n" 4558 "}", 4559 Style); 4560 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4561 verifyFormat("try\n" 4562 " {\n" 4563 " // something white\n" 4564 " }\n" 4565 "catch (...)\n" 4566 " {\n" 4567 " // something white\n" 4568 " }", 4569 Style); 4570 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4571 verifyFormat("try\n" 4572 " {\n" 4573 " // something\n" 4574 " }\n" 4575 "catch (...)\n" 4576 " {\n" 4577 " // something\n" 4578 " }", 4579 Style); 4580 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4581 Style.BraceWrapping.BeforeCatch = true; 4582 verifyFormat("try {\n" 4583 " // something\n" 4584 "}\n" 4585 "catch (...) {\n" 4586 " // something\n" 4587 "}", 4588 Style); 4589 } 4590 4591 TEST_F(FormatTest, StaticInitializers) { 4592 verifyFormat("static SomeClass SC = {1, 'a'};"); 4593 4594 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4595 " 100000000, " 4596 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4597 4598 // Here, everything other than the "}" would fit on a line. 4599 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4600 " 10000000000000000000000000};"); 4601 EXPECT_EQ("S s = {a,\n" 4602 "\n" 4603 " b};", 4604 format("S s = {\n" 4605 " a,\n" 4606 "\n" 4607 " b\n" 4608 "};")); 4609 4610 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4611 // line. However, the formatting looks a bit off and this probably doesn't 4612 // happen often in practice. 4613 verifyFormat("static int Variable[1] = {\n" 4614 " {1000000000000000000000000000000000000}};", 4615 getLLVMStyleWithColumns(40)); 4616 } 4617 4618 TEST_F(FormatTest, DesignatedInitializers) { 4619 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4620 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4621 " .bbbbbbbbbb = 2,\n" 4622 " .cccccccccc = 3,\n" 4623 " .dddddddddd = 4,\n" 4624 " .eeeeeeeeee = 5};"); 4625 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4626 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4627 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4628 " .ccccccccccccccccccccccccccc = 3,\n" 4629 " .ddddddddddddddddddddddddddd = 4,\n" 4630 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4631 4632 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4633 4634 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4635 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4636 " [2] = bbbbbbbbbb,\n" 4637 " [3] = cccccccccc,\n" 4638 " [4] = dddddddddd,\n" 4639 " [5] = eeeeeeeeee};"); 4640 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4641 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4642 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4643 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4644 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4645 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4646 } 4647 4648 TEST_F(FormatTest, NestedStaticInitializers) { 4649 verifyFormat("static A x = {{{}}};\n"); 4650 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4651 " {init1, init2, init3, init4}}};", 4652 getLLVMStyleWithColumns(50)); 4653 4654 verifyFormat("somes Status::global_reps[3] = {\n" 4655 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4656 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4657 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4658 getLLVMStyleWithColumns(60)); 4659 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4660 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4661 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4662 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4663 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4664 " {rect.fRight - rect.fLeft, rect.fBottom - " 4665 "rect.fTop}};"); 4666 4667 verifyFormat( 4668 "SomeArrayOfSomeType a = {\n" 4669 " {{1, 2, 3},\n" 4670 " {1, 2, 3},\n" 4671 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4672 " 333333333333333333333333333333},\n" 4673 " {1, 2, 3},\n" 4674 " {1, 2, 3}}};"); 4675 verifyFormat( 4676 "SomeArrayOfSomeType a = {\n" 4677 " {{1, 2, 3}},\n" 4678 " {{1, 2, 3}},\n" 4679 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4680 " 333333333333333333333333333333}},\n" 4681 " {{1, 2, 3}},\n" 4682 " {{1, 2, 3}}};"); 4683 4684 verifyFormat("struct {\n" 4685 " unsigned bit;\n" 4686 " const char *const name;\n" 4687 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4688 " {kOsWin, \"Windows\"},\n" 4689 " {kOsLinux, \"Linux\"},\n" 4690 " {kOsCrOS, \"Chrome OS\"}};"); 4691 verifyFormat("struct {\n" 4692 " unsigned bit;\n" 4693 " const char *const name;\n" 4694 "} kBitsToOs[] = {\n" 4695 " {kOsMac, \"Mac\"},\n" 4696 " {kOsWin, \"Windows\"},\n" 4697 " {kOsLinux, \"Linux\"},\n" 4698 " {kOsCrOS, \"Chrome OS\"},\n" 4699 "};"); 4700 } 4701 4702 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4703 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4704 " \\\n" 4705 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4706 } 4707 4708 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4709 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4710 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4711 4712 // Do break defaulted and deleted functions. 4713 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4714 " default;", 4715 getLLVMStyleWithColumns(40)); 4716 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4717 " delete;", 4718 getLLVMStyleWithColumns(40)); 4719 } 4720 4721 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4722 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4723 getLLVMStyleWithColumns(40)); 4724 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4725 getLLVMStyleWithColumns(40)); 4726 EXPECT_EQ("#define Q \\\n" 4727 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4728 " \"aaaaaaaa.cpp\"", 4729 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4730 getLLVMStyleWithColumns(40))); 4731 } 4732 4733 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4734 EXPECT_EQ("# 123 \"A string literal\"", 4735 format(" # 123 \"A string literal\"")); 4736 } 4737 4738 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4739 EXPECT_EQ("#;", format("#;")); 4740 verifyFormat("#\n;\n;\n;"); 4741 } 4742 4743 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4744 EXPECT_EQ("#line 42 \"test\"\n", 4745 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4746 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4747 getLLVMStyleWithColumns(12))); 4748 } 4749 4750 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4751 EXPECT_EQ("#line 42 \"test\"", 4752 format("# \\\n line \\\n 42 \\\n \"test\"")); 4753 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4754 } 4755 4756 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4757 verifyFormat("#define A \\x20"); 4758 verifyFormat("#define A \\ x20"); 4759 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4760 verifyFormat("#define A ''"); 4761 verifyFormat("#define A ''qqq"); 4762 verifyFormat("#define A `qqq"); 4763 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4764 EXPECT_EQ("const char *c = STRINGIFY(\n" 4765 "\\na : b);", 4766 format("const char * c = STRINGIFY(\n" 4767 "\\na : b);")); 4768 4769 verifyFormat("a\r\\"); 4770 verifyFormat("a\v\\"); 4771 verifyFormat("a\f\\"); 4772 } 4773 4774 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4775 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4776 style.IndentWidth = 4; 4777 style.PPIndentWidth = 1; 4778 4779 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4780 verifyFormat("#ifdef __linux__\n" 4781 "void foo() {\n" 4782 " int x = 0;\n" 4783 "}\n" 4784 "#define FOO\n" 4785 "#endif\n" 4786 "void bar() {\n" 4787 " int y = 0;\n" 4788 "}\n", 4789 style); 4790 4791 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4792 verifyFormat("#ifdef __linux__\n" 4793 "void foo() {\n" 4794 " int x = 0;\n" 4795 "}\n" 4796 "# define FOO foo\n" 4797 "#endif\n" 4798 "void bar() {\n" 4799 " int y = 0;\n" 4800 "}\n", 4801 style); 4802 4803 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4804 verifyFormat("#ifdef __linux__\n" 4805 "void foo() {\n" 4806 " int x = 0;\n" 4807 "}\n" 4808 " #define FOO foo\n" 4809 "#endif\n" 4810 "void bar() {\n" 4811 " int y = 0;\n" 4812 "}\n", 4813 style); 4814 } 4815 4816 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4817 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4818 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4819 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4820 // FIXME: We never break before the macro name. 4821 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4822 4823 verifyFormat("#define A A\n#define A A"); 4824 verifyFormat("#define A(X) A\n#define A A"); 4825 4826 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4827 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4828 } 4829 4830 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4831 EXPECT_EQ("// somecomment\n" 4832 "#include \"a.h\"\n" 4833 "#define A( \\\n" 4834 " A, B)\n" 4835 "#include \"b.h\"\n" 4836 "// somecomment\n", 4837 format(" // somecomment\n" 4838 " #include \"a.h\"\n" 4839 "#define A(A,\\\n" 4840 " B)\n" 4841 " #include \"b.h\"\n" 4842 " // somecomment\n", 4843 getLLVMStyleWithColumns(13))); 4844 } 4845 4846 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4847 4848 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4849 EXPECT_EQ("#define A \\\n" 4850 " c; \\\n" 4851 " e;\n" 4852 "f;", 4853 format("#define A c; e;\n" 4854 "f;", 4855 getLLVMStyleWithColumns(14))); 4856 } 4857 4858 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4859 4860 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4861 EXPECT_EQ("int x,\n" 4862 "#define A\n" 4863 " y;", 4864 format("int x,\n#define A\ny;")); 4865 } 4866 4867 TEST_F(FormatTest, HashInMacroDefinition) { 4868 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4869 EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle())); 4870 EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle())); 4871 EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle())); 4872 EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle())); 4873 EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle())); 4874 EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle())); 4875 EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle())); 4876 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4877 verifyFormat("#define A \\\n" 4878 " { \\\n" 4879 " f(#c); \\\n" 4880 " }", 4881 getLLVMStyleWithColumns(11)); 4882 4883 verifyFormat("#define A(X) \\\n" 4884 " void function##X()", 4885 getLLVMStyleWithColumns(22)); 4886 4887 verifyFormat("#define A(a, b, c) \\\n" 4888 " void a##b##c()", 4889 getLLVMStyleWithColumns(22)); 4890 4891 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4892 } 4893 4894 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4895 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4896 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4897 4898 FormatStyle Style = getLLVMStyle(); 4899 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4900 verifyFormat("#define true ((foo)1)", Style); 4901 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4902 verifyFormat("#define false((foo)0)", Style); 4903 } 4904 4905 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4906 EXPECT_EQ("#define A b;", format("#define A \\\n" 4907 " \\\n" 4908 " b;", 4909 getLLVMStyleWithColumns(25))); 4910 EXPECT_EQ("#define A \\\n" 4911 " \\\n" 4912 " a; \\\n" 4913 " b;", 4914 format("#define A \\\n" 4915 " \\\n" 4916 " a; \\\n" 4917 " b;", 4918 getLLVMStyleWithColumns(11))); 4919 EXPECT_EQ("#define A \\\n" 4920 " a; \\\n" 4921 " \\\n" 4922 " b;", 4923 format("#define A \\\n" 4924 " a; \\\n" 4925 " \\\n" 4926 " b;", 4927 getLLVMStyleWithColumns(11))); 4928 } 4929 4930 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4931 verifyIncompleteFormat("#define A :"); 4932 verifyFormat("#define SOMECASES \\\n" 4933 " case 1: \\\n" 4934 " case 2\n", 4935 getLLVMStyleWithColumns(20)); 4936 verifyFormat("#define MACRO(a) \\\n" 4937 " if (a) \\\n" 4938 " f(); \\\n" 4939 " else \\\n" 4940 " g()", 4941 getLLVMStyleWithColumns(18)); 4942 verifyFormat("#define A template <typename T>"); 4943 verifyIncompleteFormat("#define STR(x) #x\n" 4944 "f(STR(this_is_a_string_literal{));"); 4945 verifyFormat("#pragma omp threadprivate( \\\n" 4946 " y)), // expected-warning", 4947 getLLVMStyleWithColumns(28)); 4948 verifyFormat("#d, = };"); 4949 verifyFormat("#if \"a"); 4950 verifyIncompleteFormat("({\n" 4951 "#define b \\\n" 4952 " } \\\n" 4953 " a\n" 4954 "a", 4955 getLLVMStyleWithColumns(15)); 4956 verifyFormat("#define A \\\n" 4957 " { \\\n" 4958 " {\n" 4959 "#define B \\\n" 4960 " } \\\n" 4961 " }", 4962 getLLVMStyleWithColumns(15)); 4963 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4964 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4965 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4966 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4967 } 4968 4969 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4970 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4971 EXPECT_EQ("class A : public QObject {\n" 4972 " Q_OBJECT\n" 4973 "\n" 4974 " A() {}\n" 4975 "};", 4976 format("class A : public QObject {\n" 4977 " Q_OBJECT\n" 4978 "\n" 4979 " A() {\n}\n" 4980 "} ;")); 4981 EXPECT_EQ("MACRO\n" 4982 "/*static*/ int i;", 4983 format("MACRO\n" 4984 " /*static*/ int i;")); 4985 EXPECT_EQ("SOME_MACRO\n" 4986 "namespace {\n" 4987 "void f();\n" 4988 "} // namespace", 4989 format("SOME_MACRO\n" 4990 " namespace {\n" 4991 "void f( );\n" 4992 "} // namespace")); 4993 // Only if the identifier contains at least 5 characters. 4994 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4995 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4996 // Only if everything is upper case. 4997 EXPECT_EQ("class A : public QObject {\n" 4998 " Q_Object A() {}\n" 4999 "};", 5000 format("class A : public QObject {\n" 5001 " Q_Object\n" 5002 " A() {\n}\n" 5003 "} ;")); 5004 5005 // Only if the next line can actually start an unwrapped line. 5006 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 5007 format("SOME_WEIRD_LOG_MACRO\n" 5008 "<< SomeThing;")); 5009 5010 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 5011 "(n, buffers))\n", 5012 getChromiumStyle(FormatStyle::LK_Cpp)); 5013 5014 // See PR41483 5015 EXPECT_EQ("/**/ FOO(a)\n" 5016 "FOO(b)", 5017 format("/**/ FOO(a)\n" 5018 "FOO(b)")); 5019 } 5020 5021 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 5022 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 5023 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 5024 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 5025 "class X {};\n" 5026 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 5027 "int *createScopDetectionPass() { return 0; }", 5028 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 5029 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 5030 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 5031 " class X {};\n" 5032 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 5033 " int *createScopDetectionPass() { return 0; }")); 5034 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 5035 // braces, so that inner block is indented one level more. 5036 EXPECT_EQ("int q() {\n" 5037 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 5038 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 5039 " IPC_END_MESSAGE_MAP()\n" 5040 "}", 5041 format("int q() {\n" 5042 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 5043 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 5044 " IPC_END_MESSAGE_MAP()\n" 5045 "}")); 5046 5047 // Same inside macros. 5048 EXPECT_EQ("#define LIST(L) \\\n" 5049 " L(A) \\\n" 5050 " L(B) \\\n" 5051 " L(C)", 5052 format("#define LIST(L) \\\n" 5053 " L(A) \\\n" 5054 " L(B) \\\n" 5055 " L(C)", 5056 getGoogleStyle())); 5057 5058 // These must not be recognized as macros. 5059 EXPECT_EQ("int q() {\n" 5060 " f(x);\n" 5061 " f(x) {}\n" 5062 " f(x)->g();\n" 5063 " f(x)->*g();\n" 5064 " f(x).g();\n" 5065 " f(x) = x;\n" 5066 " f(x) += x;\n" 5067 " f(x) -= x;\n" 5068 " f(x) *= x;\n" 5069 " f(x) /= x;\n" 5070 " f(x) %= x;\n" 5071 " f(x) &= x;\n" 5072 " f(x) |= x;\n" 5073 " f(x) ^= x;\n" 5074 " f(x) >>= x;\n" 5075 " f(x) <<= x;\n" 5076 " f(x)[y].z();\n" 5077 " LOG(INFO) << x;\n" 5078 " ifstream(x) >> x;\n" 5079 "}\n", 5080 format("int q() {\n" 5081 " f(x)\n;\n" 5082 " f(x)\n {}\n" 5083 " f(x)\n->g();\n" 5084 " f(x)\n->*g();\n" 5085 " f(x)\n.g();\n" 5086 " f(x)\n = x;\n" 5087 " f(x)\n += x;\n" 5088 " f(x)\n -= x;\n" 5089 " f(x)\n *= x;\n" 5090 " f(x)\n /= x;\n" 5091 " f(x)\n %= x;\n" 5092 " f(x)\n &= x;\n" 5093 " f(x)\n |= x;\n" 5094 " f(x)\n ^= x;\n" 5095 " f(x)\n >>= x;\n" 5096 " f(x)\n <<= x;\n" 5097 " f(x)\n[y].z();\n" 5098 " LOG(INFO)\n << x;\n" 5099 " ifstream(x)\n >> x;\n" 5100 "}\n")); 5101 EXPECT_EQ("int q() {\n" 5102 " F(x)\n" 5103 " if (1) {\n" 5104 " }\n" 5105 " F(x)\n" 5106 " while (1) {\n" 5107 " }\n" 5108 " F(x)\n" 5109 " G(x);\n" 5110 " F(x)\n" 5111 " try {\n" 5112 " Q();\n" 5113 " } catch (...) {\n" 5114 " }\n" 5115 "}\n", 5116 format("int q() {\n" 5117 "F(x)\n" 5118 "if (1) {}\n" 5119 "F(x)\n" 5120 "while (1) {}\n" 5121 "F(x)\n" 5122 "G(x);\n" 5123 "F(x)\n" 5124 "try { Q(); } catch (...) {}\n" 5125 "}\n")); 5126 EXPECT_EQ("class A {\n" 5127 " A() : t(0) {}\n" 5128 " A(int i) noexcept() : {}\n" 5129 " A(X x)\n" // FIXME: function-level try blocks are broken. 5130 " try : t(0) {\n" 5131 " } catch (...) {\n" 5132 " }\n" 5133 "};", 5134 format("class A {\n" 5135 " A()\n : t(0) {}\n" 5136 " A(int i)\n noexcept() : {}\n" 5137 " A(X x)\n" 5138 " try : t(0) {} catch (...) {}\n" 5139 "};")); 5140 FormatStyle Style = getLLVMStyle(); 5141 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5142 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5143 Style.BraceWrapping.AfterFunction = true; 5144 EXPECT_EQ("void f()\n" 5145 "try\n" 5146 "{\n" 5147 "}", 5148 format("void f() try {\n" 5149 "}", 5150 Style)); 5151 EXPECT_EQ("class SomeClass {\n" 5152 "public:\n" 5153 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5154 "};", 5155 format("class SomeClass {\n" 5156 "public:\n" 5157 " SomeClass()\n" 5158 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5159 "};")); 5160 EXPECT_EQ("class SomeClass {\n" 5161 "public:\n" 5162 " SomeClass()\n" 5163 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5164 "};", 5165 format("class SomeClass {\n" 5166 "public:\n" 5167 " SomeClass()\n" 5168 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5169 "};", 5170 getLLVMStyleWithColumns(40))); 5171 5172 verifyFormat("MACRO(>)"); 5173 5174 // Some macros contain an implicit semicolon. 5175 Style = getLLVMStyle(); 5176 Style.StatementMacros.push_back("FOO"); 5177 verifyFormat("FOO(a) int b = 0;"); 5178 verifyFormat("FOO(a)\n" 5179 "int b = 0;", 5180 Style); 5181 verifyFormat("FOO(a);\n" 5182 "int b = 0;", 5183 Style); 5184 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 5185 "int b = 0;", 5186 Style); 5187 verifyFormat("FOO()\n" 5188 "int b = 0;", 5189 Style); 5190 verifyFormat("FOO\n" 5191 "int b = 0;", 5192 Style); 5193 verifyFormat("void f() {\n" 5194 " FOO(a)\n" 5195 " return a;\n" 5196 "}", 5197 Style); 5198 verifyFormat("FOO(a)\n" 5199 "FOO(b)", 5200 Style); 5201 verifyFormat("int a = 0;\n" 5202 "FOO(b)\n" 5203 "int c = 0;", 5204 Style); 5205 verifyFormat("int a = 0;\n" 5206 "int x = FOO(a)\n" 5207 "int b = 0;", 5208 Style); 5209 verifyFormat("void foo(int a) { FOO(a) }\n" 5210 "uint32_t bar() {}", 5211 Style); 5212 } 5213 5214 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) { 5215 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 5216 5217 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()", 5218 ZeroColumn); 5219 } 5220 5221 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 5222 verifyFormat("#define A \\\n" 5223 " f({ \\\n" 5224 " g(); \\\n" 5225 " });", 5226 getLLVMStyleWithColumns(11)); 5227 } 5228 5229 TEST_F(FormatTest, IndentPreprocessorDirectives) { 5230 FormatStyle Style = getLLVMStyleWithColumns(40); 5231 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 5232 verifyFormat("#ifdef _WIN32\n" 5233 "#define A 0\n" 5234 "#ifdef VAR2\n" 5235 "#define B 1\n" 5236 "#include <someheader.h>\n" 5237 "#define MACRO \\\n" 5238 " some_very_long_func_aaaaaaaaaa();\n" 5239 "#endif\n" 5240 "#else\n" 5241 "#define A 1\n" 5242 "#endif", 5243 Style); 5244 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 5245 verifyFormat("#ifdef _WIN32\n" 5246 "# define A 0\n" 5247 "# ifdef VAR2\n" 5248 "# define B 1\n" 5249 "# include <someheader.h>\n" 5250 "# define MACRO \\\n" 5251 " some_very_long_func_aaaaaaaaaa();\n" 5252 "# endif\n" 5253 "#else\n" 5254 "# define A 1\n" 5255 "#endif", 5256 Style); 5257 verifyFormat("#if A\n" 5258 "# define MACRO \\\n" 5259 " void a(int x) { \\\n" 5260 " b(); \\\n" 5261 " c(); \\\n" 5262 " d(); \\\n" 5263 " e(); \\\n" 5264 " f(); \\\n" 5265 " }\n" 5266 "#endif", 5267 Style); 5268 // Comments before include guard. 5269 verifyFormat("// file comment\n" 5270 "// file comment\n" 5271 "#ifndef HEADER_H\n" 5272 "#define HEADER_H\n" 5273 "code();\n" 5274 "#endif", 5275 Style); 5276 // Test with include guards. 5277 verifyFormat("#ifndef HEADER_H\n" 5278 "#define HEADER_H\n" 5279 "code();\n" 5280 "#endif", 5281 Style); 5282 // Include guards must have a #define with the same variable immediately 5283 // after #ifndef. 5284 verifyFormat("#ifndef NOT_GUARD\n" 5285 "# define FOO\n" 5286 "code();\n" 5287 "#endif", 5288 Style); 5289 5290 // Include guards must cover the entire file. 5291 verifyFormat("code();\n" 5292 "code();\n" 5293 "#ifndef NOT_GUARD\n" 5294 "# define NOT_GUARD\n" 5295 "code();\n" 5296 "#endif", 5297 Style); 5298 verifyFormat("#ifndef NOT_GUARD\n" 5299 "# define NOT_GUARD\n" 5300 "code();\n" 5301 "#endif\n" 5302 "code();", 5303 Style); 5304 // Test with trailing blank lines. 5305 verifyFormat("#ifndef HEADER_H\n" 5306 "#define HEADER_H\n" 5307 "code();\n" 5308 "#endif\n", 5309 Style); 5310 // Include guards don't have #else. 5311 verifyFormat("#ifndef NOT_GUARD\n" 5312 "# define NOT_GUARD\n" 5313 "code();\n" 5314 "#else\n" 5315 "#endif", 5316 Style); 5317 verifyFormat("#ifndef NOT_GUARD\n" 5318 "# define NOT_GUARD\n" 5319 "code();\n" 5320 "#elif FOO\n" 5321 "#endif", 5322 Style); 5323 // Non-identifier #define after potential include guard. 5324 verifyFormat("#ifndef FOO\n" 5325 "# define 1\n" 5326 "#endif\n", 5327 Style); 5328 // #if closes past last non-preprocessor line. 5329 verifyFormat("#ifndef FOO\n" 5330 "#define FOO\n" 5331 "#if 1\n" 5332 "int i;\n" 5333 "# define A 0\n" 5334 "#endif\n" 5335 "#endif\n", 5336 Style); 5337 // Don't crash if there is an #elif directive without a condition. 5338 verifyFormat("#if 1\n" 5339 "int x;\n" 5340 "#elif\n" 5341 "int y;\n" 5342 "#else\n" 5343 "int z;\n" 5344 "#endif", 5345 Style); 5346 // FIXME: This doesn't handle the case where there's code between the 5347 // #ifndef and #define but all other conditions hold. This is because when 5348 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 5349 // previous code line yet, so we can't detect it. 5350 EXPECT_EQ("#ifndef NOT_GUARD\n" 5351 "code();\n" 5352 "#define NOT_GUARD\n" 5353 "code();\n" 5354 "#endif", 5355 format("#ifndef NOT_GUARD\n" 5356 "code();\n" 5357 "# define NOT_GUARD\n" 5358 "code();\n" 5359 "#endif", 5360 Style)); 5361 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 5362 // be outside an include guard. Examples are #pragma once and 5363 // #pragma GCC diagnostic, or anything else that does not change the meaning 5364 // of the file if it's included multiple times. 5365 EXPECT_EQ("#ifdef WIN32\n" 5366 "# pragma once\n" 5367 "#endif\n" 5368 "#ifndef HEADER_H\n" 5369 "# define HEADER_H\n" 5370 "code();\n" 5371 "#endif", 5372 format("#ifdef WIN32\n" 5373 "# pragma once\n" 5374 "#endif\n" 5375 "#ifndef HEADER_H\n" 5376 "#define HEADER_H\n" 5377 "code();\n" 5378 "#endif", 5379 Style)); 5380 // FIXME: This does not detect when there is a single non-preprocessor line 5381 // in front of an include-guard-like structure where other conditions hold 5382 // because ScopedLineState hides the line. 5383 EXPECT_EQ("code();\n" 5384 "#ifndef HEADER_H\n" 5385 "#define HEADER_H\n" 5386 "code();\n" 5387 "#endif", 5388 format("code();\n" 5389 "#ifndef HEADER_H\n" 5390 "# define HEADER_H\n" 5391 "code();\n" 5392 "#endif", 5393 Style)); 5394 // Keep comments aligned with #, otherwise indent comments normally. These 5395 // tests cannot use verifyFormat because messUp manipulates leading 5396 // whitespace. 5397 { 5398 const char *Expected = "" 5399 "void f() {\n" 5400 "#if 1\n" 5401 "// Preprocessor aligned.\n" 5402 "# define A 0\n" 5403 " // Code. Separated by blank line.\n" 5404 "\n" 5405 "# define B 0\n" 5406 " // Code. Not aligned with #\n" 5407 "# define C 0\n" 5408 "#endif"; 5409 const char *ToFormat = "" 5410 "void f() {\n" 5411 "#if 1\n" 5412 "// Preprocessor aligned.\n" 5413 "# define A 0\n" 5414 "// Code. Separated by blank line.\n" 5415 "\n" 5416 "# define B 0\n" 5417 " // Code. Not aligned with #\n" 5418 "# define C 0\n" 5419 "#endif"; 5420 EXPECT_EQ(Expected, format(ToFormat, Style)); 5421 EXPECT_EQ(Expected, format(Expected, Style)); 5422 } 5423 // Keep block quotes aligned. 5424 { 5425 const char *Expected = "" 5426 "void f() {\n" 5427 "#if 1\n" 5428 "/* Preprocessor aligned. */\n" 5429 "# define A 0\n" 5430 " /* Code. Separated by blank line. */\n" 5431 "\n" 5432 "# define B 0\n" 5433 " /* Code. Not aligned with # */\n" 5434 "# define C 0\n" 5435 "#endif"; 5436 const char *ToFormat = "" 5437 "void f() {\n" 5438 "#if 1\n" 5439 "/* Preprocessor aligned. */\n" 5440 "# define A 0\n" 5441 "/* Code. Separated by blank line. */\n" 5442 "\n" 5443 "# define B 0\n" 5444 " /* Code. Not aligned with # */\n" 5445 "# define C 0\n" 5446 "#endif"; 5447 EXPECT_EQ(Expected, format(ToFormat, Style)); 5448 EXPECT_EQ(Expected, format(Expected, Style)); 5449 } 5450 // Keep comments aligned with un-indented directives. 5451 { 5452 const char *Expected = "" 5453 "void f() {\n" 5454 "// Preprocessor aligned.\n" 5455 "#define A 0\n" 5456 " // Code. Separated by blank line.\n" 5457 "\n" 5458 "#define B 0\n" 5459 " // Code. Not aligned with #\n" 5460 "#define C 0\n"; 5461 const char *ToFormat = "" 5462 "void f() {\n" 5463 "// Preprocessor aligned.\n" 5464 "#define A 0\n" 5465 "// Code. Separated by blank line.\n" 5466 "\n" 5467 "#define B 0\n" 5468 " // Code. Not aligned with #\n" 5469 "#define C 0\n"; 5470 EXPECT_EQ(Expected, format(ToFormat, Style)); 5471 EXPECT_EQ(Expected, format(Expected, Style)); 5472 } 5473 // Test AfterHash with tabs. 5474 { 5475 FormatStyle Tabbed = Style; 5476 Tabbed.UseTab = FormatStyle::UT_Always; 5477 Tabbed.IndentWidth = 8; 5478 Tabbed.TabWidth = 8; 5479 verifyFormat("#ifdef _WIN32\n" 5480 "#\tdefine A 0\n" 5481 "#\tifdef VAR2\n" 5482 "#\t\tdefine B 1\n" 5483 "#\t\tinclude <someheader.h>\n" 5484 "#\t\tdefine MACRO \\\n" 5485 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 5486 "#\tendif\n" 5487 "#else\n" 5488 "#\tdefine A 1\n" 5489 "#endif", 5490 Tabbed); 5491 } 5492 5493 // Regression test: Multiline-macro inside include guards. 5494 verifyFormat("#ifndef HEADER_H\n" 5495 "#define HEADER_H\n" 5496 "#define A() \\\n" 5497 " int i; \\\n" 5498 " int j;\n" 5499 "#endif // HEADER_H", 5500 getLLVMStyleWithColumns(20)); 5501 5502 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5503 // Basic before hash indent tests 5504 verifyFormat("#ifdef _WIN32\n" 5505 " #define A 0\n" 5506 " #ifdef VAR2\n" 5507 " #define B 1\n" 5508 " #include <someheader.h>\n" 5509 " #define MACRO \\\n" 5510 " some_very_long_func_aaaaaaaaaa();\n" 5511 " #endif\n" 5512 "#else\n" 5513 " #define A 1\n" 5514 "#endif", 5515 Style); 5516 verifyFormat("#if A\n" 5517 " #define MACRO \\\n" 5518 " void a(int x) { \\\n" 5519 " b(); \\\n" 5520 " c(); \\\n" 5521 " d(); \\\n" 5522 " e(); \\\n" 5523 " f(); \\\n" 5524 " }\n" 5525 "#endif", 5526 Style); 5527 // Keep comments aligned with indented directives. These 5528 // tests cannot use verifyFormat because messUp manipulates leading 5529 // whitespace. 5530 { 5531 const char *Expected = "void f() {\n" 5532 "// Aligned to preprocessor.\n" 5533 "#if 1\n" 5534 " // Aligned to code.\n" 5535 " int a;\n" 5536 " #if 1\n" 5537 " // Aligned to preprocessor.\n" 5538 " #define A 0\n" 5539 " // Aligned to code.\n" 5540 " int b;\n" 5541 " #endif\n" 5542 "#endif\n" 5543 "}"; 5544 const char *ToFormat = "void f() {\n" 5545 "// Aligned to preprocessor.\n" 5546 "#if 1\n" 5547 "// Aligned to code.\n" 5548 "int a;\n" 5549 "#if 1\n" 5550 "// Aligned to preprocessor.\n" 5551 "#define A 0\n" 5552 "// Aligned to code.\n" 5553 "int b;\n" 5554 "#endif\n" 5555 "#endif\n" 5556 "}"; 5557 EXPECT_EQ(Expected, format(ToFormat, Style)); 5558 EXPECT_EQ(Expected, format(Expected, Style)); 5559 } 5560 { 5561 const char *Expected = "void f() {\n" 5562 "/* Aligned to preprocessor. */\n" 5563 "#if 1\n" 5564 " /* Aligned to code. */\n" 5565 " int a;\n" 5566 " #if 1\n" 5567 " /* Aligned to preprocessor. */\n" 5568 " #define A 0\n" 5569 " /* Aligned to code. */\n" 5570 " int b;\n" 5571 " #endif\n" 5572 "#endif\n" 5573 "}"; 5574 const char *ToFormat = "void f() {\n" 5575 "/* Aligned to preprocessor. */\n" 5576 "#if 1\n" 5577 "/* Aligned to code. */\n" 5578 "int a;\n" 5579 "#if 1\n" 5580 "/* Aligned to preprocessor. */\n" 5581 "#define A 0\n" 5582 "/* Aligned to code. */\n" 5583 "int b;\n" 5584 "#endif\n" 5585 "#endif\n" 5586 "}"; 5587 EXPECT_EQ(Expected, format(ToFormat, Style)); 5588 EXPECT_EQ(Expected, format(Expected, Style)); 5589 } 5590 5591 // Test single comment before preprocessor 5592 verifyFormat("// Comment\n" 5593 "\n" 5594 "#if 1\n" 5595 "#endif", 5596 Style); 5597 } 5598 5599 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5600 verifyFormat("{\n { a #c; }\n}"); 5601 } 5602 5603 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5604 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5605 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5606 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5607 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5608 } 5609 5610 TEST_F(FormatTest, EscapedNewlines) { 5611 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5612 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5613 format("#define A \\\nint i;\\\n int j;", Narrow)); 5614 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5615 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5616 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5617 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5618 5619 FormatStyle AlignLeft = getLLVMStyle(); 5620 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5621 EXPECT_EQ("#define MACRO(x) \\\n" 5622 "private: \\\n" 5623 " int x(int a);\n", 5624 format("#define MACRO(x) \\\n" 5625 "private: \\\n" 5626 " int x(int a);\n", 5627 AlignLeft)); 5628 5629 // CRLF line endings 5630 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5631 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5632 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5633 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5634 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5635 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5636 EXPECT_EQ("#define MACRO(x) \\\r\n" 5637 "private: \\\r\n" 5638 " int x(int a);\r\n", 5639 format("#define MACRO(x) \\\r\n" 5640 "private: \\\r\n" 5641 " int x(int a);\r\n", 5642 AlignLeft)); 5643 5644 FormatStyle DontAlign = getLLVMStyle(); 5645 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5646 DontAlign.MaxEmptyLinesToKeep = 3; 5647 // FIXME: can't use verifyFormat here because the newline before 5648 // "public:" is not inserted the first time it's reformatted 5649 EXPECT_EQ("#define A \\\n" 5650 " class Foo { \\\n" 5651 " void bar(); \\\n" 5652 "\\\n" 5653 "\\\n" 5654 "\\\n" 5655 " public: \\\n" 5656 " void baz(); \\\n" 5657 " };", 5658 format("#define A \\\n" 5659 " class Foo { \\\n" 5660 " void bar(); \\\n" 5661 "\\\n" 5662 "\\\n" 5663 "\\\n" 5664 " public: \\\n" 5665 " void baz(); \\\n" 5666 " };", 5667 DontAlign)); 5668 } 5669 5670 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5671 verifyFormat("#define A \\\n" 5672 " int v( \\\n" 5673 " a); \\\n" 5674 " int i;", 5675 getLLVMStyleWithColumns(11)); 5676 } 5677 5678 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5679 EXPECT_EQ( 5680 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5681 " \\\n" 5682 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5683 "\n" 5684 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5685 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5686 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5687 "\\\n" 5688 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5689 " \n" 5690 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5691 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5692 } 5693 5694 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5695 EXPECT_EQ("int\n" 5696 "#define A\n" 5697 " a;", 5698 format("int\n#define A\na;")); 5699 verifyFormat("functionCallTo(\n" 5700 " someOtherFunction(\n" 5701 " withSomeParameters, whichInSequence,\n" 5702 " areLongerThanALine(andAnotherCall,\n" 5703 "#define A B\n" 5704 " withMoreParamters,\n" 5705 " whichStronglyInfluenceTheLayout),\n" 5706 " andMoreParameters),\n" 5707 " trailing);", 5708 getLLVMStyleWithColumns(69)); 5709 verifyFormat("Foo::Foo()\n" 5710 "#ifdef BAR\n" 5711 " : baz(0)\n" 5712 "#endif\n" 5713 "{\n" 5714 "}"); 5715 verifyFormat("void f() {\n" 5716 " if (true)\n" 5717 "#ifdef A\n" 5718 " f(42);\n" 5719 " x();\n" 5720 "#else\n" 5721 " g();\n" 5722 " x();\n" 5723 "#endif\n" 5724 "}"); 5725 verifyFormat("void f(param1, param2,\n" 5726 " param3,\n" 5727 "#ifdef A\n" 5728 " param4(param5,\n" 5729 "#ifdef A1\n" 5730 " param6,\n" 5731 "#ifdef A2\n" 5732 " param7),\n" 5733 "#else\n" 5734 " param8),\n" 5735 " param9,\n" 5736 "#endif\n" 5737 " param10,\n" 5738 "#endif\n" 5739 " param11)\n" 5740 "#else\n" 5741 " param12)\n" 5742 "#endif\n" 5743 "{\n" 5744 " x();\n" 5745 "}", 5746 getLLVMStyleWithColumns(28)); 5747 verifyFormat("#if 1\n" 5748 "int i;"); 5749 verifyFormat("#if 1\n" 5750 "#endif\n" 5751 "#if 1\n" 5752 "#else\n" 5753 "#endif\n"); 5754 verifyFormat("DEBUG({\n" 5755 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5757 "});\n" 5758 "#if a\n" 5759 "#else\n" 5760 "#endif"); 5761 5762 verifyIncompleteFormat("void f(\n" 5763 "#if A\n" 5764 ");\n" 5765 "#else\n" 5766 "#endif"); 5767 } 5768 5769 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5770 verifyFormat("#endif\n" 5771 "#if B"); 5772 } 5773 5774 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5775 FormatStyle SingleLine = getLLVMStyle(); 5776 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5777 verifyFormat("#if 0\n" 5778 "#elif 1\n" 5779 "#endif\n" 5780 "void foo() {\n" 5781 " if (test) foo2();\n" 5782 "}", 5783 SingleLine); 5784 } 5785 5786 TEST_F(FormatTest, LayoutBlockInsideParens) { 5787 verifyFormat("functionCall({ int i; });"); 5788 verifyFormat("functionCall({\n" 5789 " int i;\n" 5790 " int j;\n" 5791 "});"); 5792 verifyFormat("functionCall(\n" 5793 " {\n" 5794 " int i;\n" 5795 " int j;\n" 5796 " },\n" 5797 " aaaa, bbbb, cccc);"); 5798 verifyFormat("functionA(functionB({\n" 5799 " int i;\n" 5800 " int j;\n" 5801 " }),\n" 5802 " aaaa, bbbb, cccc);"); 5803 verifyFormat("functionCall(\n" 5804 " {\n" 5805 " int i;\n" 5806 " int j;\n" 5807 " },\n" 5808 " aaaa, bbbb, // comment\n" 5809 " cccc);"); 5810 verifyFormat("functionA(functionB({\n" 5811 " int i;\n" 5812 " int j;\n" 5813 " }),\n" 5814 " aaaa, bbbb, // comment\n" 5815 " cccc);"); 5816 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5817 verifyFormat("functionCall(aaaa, bbbb, {\n" 5818 " int i;\n" 5819 " int j;\n" 5820 "});"); 5821 verifyFormat( 5822 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5823 " {\n" 5824 " int i; // break\n" 5825 " },\n" 5826 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5827 " ccccccccccccccccc));"); 5828 verifyFormat("DEBUG({\n" 5829 " if (a)\n" 5830 " f();\n" 5831 "});"); 5832 } 5833 5834 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5835 EXPECT_EQ("SOME_MACRO { int i; }\n" 5836 "int i;", 5837 format(" SOME_MACRO {int i;} int i;")); 5838 } 5839 5840 TEST_F(FormatTest, LayoutNestedBlocks) { 5841 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5842 " struct s {\n" 5843 " int i;\n" 5844 " };\n" 5845 " s kBitsToOs[] = {{10}};\n" 5846 " for (int i = 0; i < 10; ++i)\n" 5847 " return;\n" 5848 "}"); 5849 verifyFormat("call(parameter, {\n" 5850 " something();\n" 5851 " // Comment using all columns.\n" 5852 " somethingelse();\n" 5853 "});", 5854 getLLVMStyleWithColumns(40)); 5855 verifyFormat("DEBUG( //\n" 5856 " { f(); }, a);"); 5857 verifyFormat("DEBUG( //\n" 5858 " {\n" 5859 " f(); //\n" 5860 " },\n" 5861 " a);"); 5862 5863 EXPECT_EQ("call(parameter, {\n" 5864 " something();\n" 5865 " // Comment too\n" 5866 " // looooooooooong.\n" 5867 " somethingElse();\n" 5868 "});", 5869 format("call(parameter, {\n" 5870 " something();\n" 5871 " // Comment too looooooooooong.\n" 5872 " somethingElse();\n" 5873 "});", 5874 getLLVMStyleWithColumns(29))); 5875 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5876 EXPECT_EQ("DEBUG({ // comment\n" 5877 " int i;\n" 5878 "});", 5879 format("DEBUG({ // comment\n" 5880 "int i;\n" 5881 "});")); 5882 EXPECT_EQ("DEBUG({\n" 5883 " int i;\n" 5884 "\n" 5885 " // comment\n" 5886 " int j;\n" 5887 "});", 5888 format("DEBUG({\n" 5889 " int i;\n" 5890 "\n" 5891 " // comment\n" 5892 " int j;\n" 5893 "});")); 5894 5895 verifyFormat("DEBUG({\n" 5896 " if (a)\n" 5897 " return;\n" 5898 "});"); 5899 verifyGoogleFormat("DEBUG({\n" 5900 " if (a) return;\n" 5901 "});"); 5902 FormatStyle Style = getGoogleStyle(); 5903 Style.ColumnLimit = 45; 5904 verifyFormat("Debug(\n" 5905 " aaaaa,\n" 5906 " {\n" 5907 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5908 " },\n" 5909 " a);", 5910 Style); 5911 5912 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5913 5914 verifyNoCrash("^{v^{a}}"); 5915 } 5916 5917 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5918 EXPECT_EQ("#define MACRO() \\\n" 5919 " Debug(aaa, /* force line break */ \\\n" 5920 " { \\\n" 5921 " int i; \\\n" 5922 " int j; \\\n" 5923 " })", 5924 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5925 " { int i; int j; })", 5926 getGoogleStyle())); 5927 5928 EXPECT_EQ("#define A \\\n" 5929 " [] { \\\n" 5930 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5931 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5932 " }", 5933 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5934 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5935 getGoogleStyle())); 5936 } 5937 5938 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5939 EXPECT_EQ("{}", format("{}")); 5940 verifyFormat("enum E {};"); 5941 verifyFormat("enum E {}"); 5942 FormatStyle Style = getLLVMStyle(); 5943 Style.SpaceInEmptyBlock = true; 5944 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5945 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5946 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5947 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5948 Style.BraceWrapping.BeforeElse = false; 5949 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5950 verifyFormat("if (a)\n" 5951 "{\n" 5952 "} else if (b)\n" 5953 "{\n" 5954 "} else\n" 5955 "{ }", 5956 Style); 5957 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 5958 verifyFormat("if (a) {\n" 5959 "} else if (b) {\n" 5960 "} else {\n" 5961 "}", 5962 Style); 5963 Style.BraceWrapping.BeforeElse = true; 5964 verifyFormat("if (a) { }\n" 5965 "else if (b) { }\n" 5966 "else { }", 5967 Style); 5968 } 5969 5970 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5971 FormatStyle Style = getLLVMStyle(); 5972 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5973 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5974 verifyFormat("FOO_BEGIN\n" 5975 " FOO_ENTRY\n" 5976 "FOO_END", 5977 Style); 5978 verifyFormat("FOO_BEGIN\n" 5979 " NESTED_FOO_BEGIN\n" 5980 " NESTED_FOO_ENTRY\n" 5981 " NESTED_FOO_END\n" 5982 "FOO_END", 5983 Style); 5984 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5985 " int x;\n" 5986 " x = 1;\n" 5987 "FOO_END(Baz)", 5988 Style); 5989 } 5990 5991 //===----------------------------------------------------------------------===// 5992 // Line break tests. 5993 //===----------------------------------------------------------------------===// 5994 5995 TEST_F(FormatTest, PreventConfusingIndents) { 5996 verifyFormat( 5997 "void f() {\n" 5998 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5999 " parameter, parameter, parameter)),\n" 6000 " SecondLongCall(parameter));\n" 6001 "}"); 6002 verifyFormat( 6003 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6004 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6005 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6006 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6007 verifyFormat( 6008 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6009 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 6010 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6011 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 6012 verifyFormat( 6013 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6014 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 6015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 6016 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 6017 verifyFormat("int a = bbbb && ccc &&\n" 6018 " fffff(\n" 6019 "#define A Just forcing a new line\n" 6020 " ddd);"); 6021 } 6022 6023 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 6024 verifyFormat( 6025 "bool aaaaaaa =\n" 6026 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 6027 " bbbbbbbb();"); 6028 verifyFormat( 6029 "bool aaaaaaa =\n" 6030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 6031 " bbbbbbbb();"); 6032 6033 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 6034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 6035 " ccccccccc == ddddddddddd;"); 6036 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 6037 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 6038 " ccccccccc == ddddddddddd;"); 6039 verifyFormat( 6040 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 6041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 6042 " ccccccccc == ddddddddddd;"); 6043 6044 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 6045 " aaaaaa) &&\n" 6046 " bbbbbb && cccccc;"); 6047 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 6048 " aaaaaa) >>\n" 6049 " bbbbbb;"); 6050 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 6051 " SourceMgr.getSpellingColumnNumber(\n" 6052 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 6053 " 1);"); 6054 6055 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6056 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 6057 " cccccc) {\n}"); 6058 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6059 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 6060 " cccccc) {\n}"); 6061 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6062 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 6063 " cccccc) {\n}"); 6064 verifyFormat("b = a &&\n" 6065 " // Comment\n" 6066 " b.c && d;"); 6067 6068 // If the LHS of a comparison is not a binary expression itself, the 6069 // additional linebreak confuses many people. 6070 verifyFormat( 6071 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 6073 "}"); 6074 verifyFormat( 6075 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 6077 "}"); 6078 verifyFormat( 6079 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 6080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 6081 "}"); 6082 verifyFormat( 6083 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6084 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 6085 "}"); 6086 // Even explicit parentheses stress the precedence enough to make the 6087 // additional break unnecessary. 6088 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 6090 "}"); 6091 // This cases is borderline, but with the indentation it is still readable. 6092 verifyFormat( 6093 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6094 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6095 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 6096 "}", 6097 getLLVMStyleWithColumns(75)); 6098 6099 // If the LHS is a binary expression, we should still use the additional break 6100 // as otherwise the formatting hides the operator precedence. 6101 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6102 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6103 " 5) {\n" 6104 "}"); 6105 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 6107 " 5) {\n" 6108 "}"); 6109 6110 FormatStyle OnePerLine = getLLVMStyle(); 6111 OnePerLine.BinPackParameters = false; 6112 verifyFormat( 6113 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6114 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6115 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 6116 OnePerLine); 6117 6118 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 6119 " .aaa(aaaaaaaaaaaaa) *\n" 6120 " aaaaaaa +\n" 6121 " aaaaaaa;", 6122 getLLVMStyleWithColumns(40)); 6123 } 6124 6125 TEST_F(FormatTest, ExpressionIndentation) { 6126 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6128 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6130 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 6131 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 6132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 6134 " ccccccccccccccccccccccccccccccccccccccccc;"); 6135 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6138 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 6139 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6140 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6142 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 6143 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6146 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 6147 verifyFormat("if () {\n" 6148 "} else if (aaaaa && bbbbb > // break\n" 6149 " ccccc) {\n" 6150 "}"); 6151 verifyFormat("if () {\n" 6152 "} else if constexpr (aaaaa && bbbbb > // break\n" 6153 " ccccc) {\n" 6154 "}"); 6155 verifyFormat("if () {\n" 6156 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 6157 " ccccc) {\n" 6158 "}"); 6159 verifyFormat("if () {\n" 6160 "} else if (aaaaa &&\n" 6161 " bbbbb > // break\n" 6162 " ccccc &&\n" 6163 " ddddd) {\n" 6164 "}"); 6165 6166 // Presence of a trailing comment used to change indentation of b. 6167 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 6168 " b;\n" 6169 "return aaaaaaaaaaaaaaaaaaa +\n" 6170 " b; //", 6171 getLLVMStyleWithColumns(30)); 6172 } 6173 6174 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 6175 // Not sure what the best system is here. Like this, the LHS can be found 6176 // immediately above an operator (everything with the same or a higher 6177 // indent). The RHS is aligned right of the operator and so compasses 6178 // everything until something with the same indent as the operator is found. 6179 // FIXME: Is this a good system? 6180 FormatStyle Style = getLLVMStyle(); 6181 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6182 verifyFormat( 6183 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6184 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6185 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6186 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6187 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6188 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6189 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6190 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6191 " > ccccccccccccccccccccccccccccccccccccccccc;", 6192 Style); 6193 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6194 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6195 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6196 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6197 Style); 6198 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6199 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6200 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6201 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6202 Style); 6203 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6204 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6205 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6206 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6207 Style); 6208 verifyFormat("if () {\n" 6209 "} else if (aaaaa\n" 6210 " && bbbbb // break\n" 6211 " > ccccc) {\n" 6212 "}", 6213 Style); 6214 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6215 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6216 Style); 6217 verifyFormat("return (a)\n" 6218 " // comment\n" 6219 " + b;", 6220 Style); 6221 verifyFormat( 6222 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6223 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6224 " + cc;", 6225 Style); 6226 6227 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6228 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6229 Style); 6230 6231 // Forced by comments. 6232 verifyFormat( 6233 "unsigned ContentSize =\n" 6234 " sizeof(int16_t) // DWARF ARange version number\n" 6235 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6236 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6237 " + sizeof(int8_t); // Segment Size (in bytes)"); 6238 6239 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6240 " == boost::fusion::at_c<1>(iiii).second;", 6241 Style); 6242 6243 Style.ColumnLimit = 60; 6244 verifyFormat("zzzzzzzzzz\n" 6245 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6246 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6247 Style); 6248 6249 Style.ColumnLimit = 80; 6250 Style.IndentWidth = 4; 6251 Style.TabWidth = 4; 6252 Style.UseTab = FormatStyle::UT_Always; 6253 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6254 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6255 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 6256 "\t&& (someOtherLongishConditionPart1\n" 6257 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 6258 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 6259 "(someOtherLongishConditionPart1 || " 6260 "someOtherEvenLongerNestedConditionPart2);", 6261 Style)); 6262 } 6263 6264 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 6265 FormatStyle Style = getLLVMStyle(); 6266 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6267 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 6268 6269 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6270 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6271 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6272 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6273 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6274 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6275 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6276 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6277 " > ccccccccccccccccccccccccccccccccccccccccc;", 6278 Style); 6279 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6280 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6281 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6282 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6283 Style); 6284 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6285 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6286 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6287 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6288 Style); 6289 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6290 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6291 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6292 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6293 Style); 6294 verifyFormat("if () {\n" 6295 "} else if (aaaaa\n" 6296 " && bbbbb // break\n" 6297 " > ccccc) {\n" 6298 "}", 6299 Style); 6300 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6301 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6302 Style); 6303 verifyFormat("return (a)\n" 6304 " // comment\n" 6305 " + b;", 6306 Style); 6307 verifyFormat( 6308 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6309 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6310 " + cc;", 6311 Style); 6312 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6313 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6314 " : 3333333333333333;", 6315 Style); 6316 verifyFormat( 6317 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6318 " : ccccccccccccccc ? dddddddddddddddddd\n" 6319 " : eeeeeeeeeeeeeeeeee)\n" 6320 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6321 " : 3333333333333333;", 6322 Style); 6323 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6324 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6325 Style); 6326 6327 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6328 " == boost::fusion::at_c<1>(iiii).second;", 6329 Style); 6330 6331 Style.ColumnLimit = 60; 6332 verifyFormat("zzzzzzzzzzzzz\n" 6333 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6334 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6335 Style); 6336 6337 // Forced by comments. 6338 Style.ColumnLimit = 80; 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 Style); 6346 6347 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6348 verifyFormat( 6349 "unsigned ContentSize =\n" 6350 " sizeof(int16_t) // DWARF ARange version number\n" 6351 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6352 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6353 " + sizeof(int8_t); // Segment Size (in bytes)", 6354 Style); 6355 6356 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6357 verifyFormat( 6358 "unsigned ContentSize =\n" 6359 " sizeof(int16_t) // DWARF ARange version number\n" 6360 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6361 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6362 " + sizeof(int8_t); // Segment Size (in bytes)", 6363 Style); 6364 } 6365 6366 TEST_F(FormatTest, EnforcedOperatorWraps) { 6367 // Here we'd like to wrap after the || operators, but a comment is forcing an 6368 // earlier wrap. 6369 verifyFormat("bool x = aaaaa //\n" 6370 " || bbbbb\n" 6371 " //\n" 6372 " || cccc;"); 6373 } 6374 6375 TEST_F(FormatTest, NoOperandAlignment) { 6376 FormatStyle Style = getLLVMStyle(); 6377 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6378 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 6379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6381 Style); 6382 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6383 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6384 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6385 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6386 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6387 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6388 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6389 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6390 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6391 " > ccccccccccccccccccccccccccccccccccccccccc;", 6392 Style); 6393 6394 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6395 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6396 " + cc;", 6397 Style); 6398 verifyFormat("int a = aa\n" 6399 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6400 " * cccccccccccccccccccccccccccccccccccc;\n", 6401 Style); 6402 6403 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6404 verifyFormat("return (a > b\n" 6405 " // comment1\n" 6406 " // comment2\n" 6407 " || c);", 6408 Style); 6409 } 6410 6411 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 6412 FormatStyle Style = getLLVMStyle(); 6413 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6414 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6416 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6417 Style); 6418 } 6419 6420 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 6421 FormatStyle Style = getLLVMStyleWithColumns(40); 6422 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6423 Style.BinPackArguments = false; 6424 verifyFormat("void test() {\n" 6425 " someFunction(\n" 6426 " this + argument + is + quite\n" 6427 " + long + so + it + gets + wrapped\n" 6428 " + but + remains + bin - packed);\n" 6429 "}", 6430 Style); 6431 verifyFormat("void test() {\n" 6432 " someFunction(arg1,\n" 6433 " this + argument + is\n" 6434 " + quite + long + so\n" 6435 " + it + gets + wrapped\n" 6436 " + but + remains + bin\n" 6437 " - packed,\n" 6438 " arg3);\n" 6439 "}", 6440 Style); 6441 verifyFormat("void test() {\n" 6442 " someFunction(\n" 6443 " arg1,\n" 6444 " this + argument + has\n" 6445 " + anotherFunc(nested,\n" 6446 " calls + whose\n" 6447 " + arguments\n" 6448 " + are + also\n" 6449 " + wrapped,\n" 6450 " in + addition)\n" 6451 " + to + being + bin - packed,\n" 6452 " arg3);\n" 6453 "}", 6454 Style); 6455 6456 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6457 verifyFormat("void test() {\n" 6458 " someFunction(\n" 6459 " arg1,\n" 6460 " this + argument + has +\n" 6461 " anotherFunc(nested,\n" 6462 " calls + whose +\n" 6463 " arguments +\n" 6464 " are + also +\n" 6465 " wrapped,\n" 6466 " in + addition) +\n" 6467 " to + being + bin - packed,\n" 6468 " arg3);\n" 6469 "}", 6470 Style); 6471 } 6472 6473 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) { 6474 auto Style = getLLVMStyleWithColumns(45); 6475 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None); 6476 verifyFormat("bool b =\n" 6477 " is_default_constructible_v<hash<T>> and\n" 6478 " is_copy_constructible_v<hash<T>> and\n" 6479 " is_move_constructible_v<hash<T>> and\n" 6480 " is_copy_assignable_v<hash<T>> and\n" 6481 " is_move_assignable_v<hash<T>> and\n" 6482 " is_destructible_v<hash<T>> and\n" 6483 " is_swappable_v<hash<T>> and\n" 6484 " is_callable_v<hash<T>(T)>;", 6485 Style); 6486 6487 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6488 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n" 6489 " and is_copy_constructible_v<hash<T>>\n" 6490 " and is_move_constructible_v<hash<T>>\n" 6491 " and is_copy_assignable_v<hash<T>>\n" 6492 " and is_move_assignable_v<hash<T>>\n" 6493 " and is_destructible_v<hash<T>>\n" 6494 " and is_swappable_v<hash<T>>\n" 6495 " and is_callable_v<hash<T>(T)>;", 6496 Style); 6497 6498 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6499 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n" 6500 " and is_copy_constructible_v<hash<T>>\n" 6501 " and is_move_constructible_v<hash<T>>\n" 6502 " and is_copy_assignable_v<hash<T>>\n" 6503 " and is_move_assignable_v<hash<T>>\n" 6504 " and is_destructible_v<hash<T>>\n" 6505 " and is_swappable_v<hash<T>>\n" 6506 " and is_callable_v<hash<T>(T)>;", 6507 Style); 6508 } 6509 6510 TEST_F(FormatTest, ConstructorInitializers) { 6511 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6512 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 6513 getLLVMStyleWithColumns(45)); 6514 verifyFormat("Constructor()\n" 6515 " : Inttializer(FitsOnTheLine) {}", 6516 getLLVMStyleWithColumns(44)); 6517 verifyFormat("Constructor()\n" 6518 " : Inttializer(FitsOnTheLine) {}", 6519 getLLVMStyleWithColumns(43)); 6520 6521 verifyFormat("template <typename T>\n" 6522 "Constructor() : Initializer(FitsOnTheLine) {}", 6523 getLLVMStyleWithColumns(45)); 6524 6525 verifyFormat( 6526 "SomeClass::Constructor()\n" 6527 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6528 6529 verifyFormat( 6530 "SomeClass::Constructor()\n" 6531 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6532 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 6533 verifyFormat( 6534 "SomeClass::Constructor()\n" 6535 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6536 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6537 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6538 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6539 " : aaaaaaaaaa(aaaaaa) {}"); 6540 6541 verifyFormat("Constructor()\n" 6542 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6543 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6544 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6545 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 6546 6547 verifyFormat("Constructor()\n" 6548 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6549 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6550 6551 verifyFormat("Constructor(int Parameter = 0)\n" 6552 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6553 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 6554 verifyFormat("Constructor()\n" 6555 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6556 "}", 6557 getLLVMStyleWithColumns(60)); 6558 verifyFormat("Constructor()\n" 6559 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6560 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 6561 6562 // Here a line could be saved by splitting the second initializer onto two 6563 // lines, but that is not desirable. 6564 verifyFormat("Constructor()\n" 6565 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6566 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6567 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6568 6569 FormatStyle OnePerLine = getLLVMStyle(); 6570 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 6571 verifyFormat("MyClass::MyClass()\n" 6572 " : a(a),\n" 6573 " b(b),\n" 6574 " c(c) {}", 6575 OnePerLine); 6576 verifyFormat("MyClass::MyClass()\n" 6577 " : a(a), // comment\n" 6578 " b(b),\n" 6579 " c(c) {}", 6580 OnePerLine); 6581 verifyFormat("MyClass::MyClass(int a)\n" 6582 " : b(a), // comment\n" 6583 " c(a + 1) { // lined up\n" 6584 "}", 6585 OnePerLine); 6586 verifyFormat("Constructor()\n" 6587 " : a(b, b, b) {}", 6588 OnePerLine); 6589 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6590 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 6591 verifyFormat("SomeClass::Constructor()\n" 6592 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6593 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6594 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6595 OnePerLine); 6596 verifyFormat("SomeClass::Constructor()\n" 6597 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6598 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6599 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6600 OnePerLine); 6601 verifyFormat("MyClass::MyClass(int var)\n" 6602 " : some_var_(var), // 4 space indent\n" 6603 " some_other_var_(var + 1) { // lined up\n" 6604 "}", 6605 OnePerLine); 6606 verifyFormat("Constructor()\n" 6607 " : aaaaa(aaaaaa),\n" 6608 " aaaaa(aaaaaa),\n" 6609 " aaaaa(aaaaaa),\n" 6610 " aaaaa(aaaaaa),\n" 6611 " aaaaa(aaaaaa) {}", 6612 OnePerLine); 6613 verifyFormat("Constructor()\n" 6614 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6615 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6616 OnePerLine); 6617 OnePerLine.BinPackParameters = false; 6618 verifyFormat( 6619 "Constructor()\n" 6620 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6621 " aaaaaaaaaaa().aaa(),\n" 6622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6623 OnePerLine); 6624 OnePerLine.ColumnLimit = 60; 6625 verifyFormat("Constructor()\n" 6626 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6627 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6628 OnePerLine); 6629 6630 EXPECT_EQ("Constructor()\n" 6631 " : // Comment forcing unwanted break.\n" 6632 " aaaa(aaaa) {}", 6633 format("Constructor() :\n" 6634 " // Comment forcing unwanted break.\n" 6635 " aaaa(aaaa) {}")); 6636 } 6637 6638 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6639 FormatStyle Style = getLLVMStyleWithColumns(60); 6640 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6641 Style.BinPackParameters = false; 6642 6643 for (int i = 0; i < 4; ++i) { 6644 // Test all combinations of parameters that should not have an effect. 6645 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6646 Style.AllowAllArgumentsOnNextLine = i & 2; 6647 6648 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6649 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6650 verifyFormat("Constructor()\n" 6651 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6652 Style); 6653 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6654 6655 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6656 verifyFormat("Constructor()\n" 6657 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6658 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6659 Style); 6660 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6661 6662 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6663 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6664 verifyFormat("Constructor()\n" 6665 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6666 Style); 6667 6668 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6669 verifyFormat("Constructor()\n" 6670 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6671 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6672 Style); 6673 6674 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6675 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6676 verifyFormat("Constructor() :\n" 6677 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6678 Style); 6679 6680 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6681 verifyFormat("Constructor() :\n" 6682 " aaaaaaaaaaaaaaaaaa(a),\n" 6683 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6684 Style); 6685 } 6686 6687 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6688 // AllowAllConstructorInitializersOnNextLine in all 6689 // BreakConstructorInitializers modes 6690 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6691 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6692 verifyFormat("SomeClassWithALongName::Constructor(\n" 6693 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6694 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6695 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6696 Style); 6697 6698 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6699 verifyFormat("SomeClassWithALongName::Constructor(\n" 6700 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6701 " int bbbbbbbbbbbbb,\n" 6702 " int cccccccccccccccc)\n" 6703 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6704 Style); 6705 6706 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6707 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6708 verifyFormat("SomeClassWithALongName::Constructor(\n" 6709 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6710 " int bbbbbbbbbbbbb)\n" 6711 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6712 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6713 Style); 6714 6715 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6716 6717 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6718 verifyFormat("SomeClassWithALongName::Constructor(\n" 6719 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6720 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6721 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6722 Style); 6723 6724 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6725 verifyFormat("SomeClassWithALongName::Constructor(\n" 6726 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6727 " int bbbbbbbbbbbbb,\n" 6728 " int cccccccccccccccc)\n" 6729 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6730 Style); 6731 6732 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6733 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6734 verifyFormat("SomeClassWithALongName::Constructor(\n" 6735 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6736 " int bbbbbbbbbbbbb)\n" 6737 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6738 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6739 Style); 6740 6741 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6742 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6743 verifyFormat("SomeClassWithALongName::Constructor(\n" 6744 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6745 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6746 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6747 Style); 6748 6749 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6750 verifyFormat("SomeClassWithALongName::Constructor(\n" 6751 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6752 " int bbbbbbbbbbbbb,\n" 6753 " int cccccccccccccccc) :\n" 6754 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6755 Style); 6756 6757 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6758 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6759 verifyFormat("SomeClassWithALongName::Constructor(\n" 6760 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6761 " int bbbbbbbbbbbbb) :\n" 6762 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6763 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6764 Style); 6765 } 6766 6767 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6768 FormatStyle Style = getLLVMStyleWithColumns(60); 6769 Style.BinPackArguments = false; 6770 for (int i = 0; i < 4; ++i) { 6771 // Test all combinations of parameters that should not have an effect. 6772 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6773 Style.PackConstructorInitializers = 6774 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6775 6776 Style.AllowAllArgumentsOnNextLine = true; 6777 verifyFormat("void foo() {\n" 6778 " FunctionCallWithReallyLongName(\n" 6779 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6780 "}", 6781 Style); 6782 Style.AllowAllArgumentsOnNextLine = false; 6783 verifyFormat("void foo() {\n" 6784 " FunctionCallWithReallyLongName(\n" 6785 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6786 " bbbbbbbbbbbb);\n" 6787 "}", 6788 Style); 6789 6790 Style.AllowAllArgumentsOnNextLine = true; 6791 verifyFormat("void foo() {\n" 6792 " auto VariableWithReallyLongName = {\n" 6793 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6794 "}", 6795 Style); 6796 Style.AllowAllArgumentsOnNextLine = false; 6797 verifyFormat("void foo() {\n" 6798 " auto VariableWithReallyLongName = {\n" 6799 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6800 " bbbbbbbbbbbb};\n" 6801 "}", 6802 Style); 6803 } 6804 6805 // This parameter should not affect declarations. 6806 Style.BinPackParameters = false; 6807 Style.AllowAllArgumentsOnNextLine = false; 6808 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6809 verifyFormat("void FunctionCallWithReallyLongName(\n" 6810 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6811 Style); 6812 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6813 verifyFormat("void FunctionCallWithReallyLongName(\n" 6814 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6815 " int bbbbbbbbbbbb);", 6816 Style); 6817 } 6818 6819 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6820 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6821 // and BAS_Align. 6822 FormatStyle Style = getLLVMStyleWithColumns(35); 6823 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6824 "void functionDecl(int A, int B, int C);"; 6825 Style.AllowAllArgumentsOnNextLine = false; 6826 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6827 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6828 " paramC);\n" 6829 "void functionDecl(int A, int B,\n" 6830 " int C);"), 6831 format(Input, Style)); 6832 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6833 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6834 " paramC);\n" 6835 "void functionDecl(int A, int B,\n" 6836 " int C);"), 6837 format(Input, Style)); 6838 // However, BAS_AlwaysBreak should take precedence over 6839 // AllowAllArgumentsOnNextLine. 6840 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6841 EXPECT_EQ(StringRef("functionCall(\n" 6842 " paramA, paramB, paramC);\n" 6843 "void functionDecl(\n" 6844 " int A, int B, int C);"), 6845 format(Input, Style)); 6846 6847 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6848 // first argument. 6849 Style.AllowAllArgumentsOnNextLine = true; 6850 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6851 EXPECT_EQ(StringRef("functionCall(\n" 6852 " paramA, paramB, paramC);\n" 6853 "void functionDecl(\n" 6854 " int A, int B, int C);"), 6855 format(Input, Style)); 6856 // It wouldn't fit on one line with aligned parameters so this setting 6857 // doesn't change anything for BAS_Align. 6858 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6859 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6860 " paramC);\n" 6861 "void functionDecl(int A, int B,\n" 6862 " int C);"), 6863 format(Input, Style)); 6864 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6865 EXPECT_EQ(StringRef("functionCall(\n" 6866 " paramA, paramB, paramC);\n" 6867 "void functionDecl(\n" 6868 " int A, int B, int C);"), 6869 format(Input, Style)); 6870 } 6871 6872 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6873 FormatStyle Style = getLLVMStyle(); 6874 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6875 6876 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6877 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6878 getStyleWithColumns(Style, 45)); 6879 verifyFormat("Constructor() :\n" 6880 " Initializer(FitsOnTheLine) {}", 6881 getStyleWithColumns(Style, 44)); 6882 verifyFormat("Constructor() :\n" 6883 " Initializer(FitsOnTheLine) {}", 6884 getStyleWithColumns(Style, 43)); 6885 6886 verifyFormat("template <typename T>\n" 6887 "Constructor() : Initializer(FitsOnTheLine) {}", 6888 getStyleWithColumns(Style, 50)); 6889 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6890 verifyFormat( 6891 "SomeClass::Constructor() :\n" 6892 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6893 Style); 6894 6895 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 6896 verifyFormat( 6897 "SomeClass::Constructor() :\n" 6898 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6899 Style); 6900 6901 verifyFormat( 6902 "SomeClass::Constructor() :\n" 6903 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6904 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6905 Style); 6906 verifyFormat( 6907 "SomeClass::Constructor() :\n" 6908 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6909 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6910 Style); 6911 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6912 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6913 " aaaaaaaaaa(aaaaaa) {}", 6914 Style); 6915 6916 verifyFormat("Constructor() :\n" 6917 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6918 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6919 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6920 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6921 Style); 6922 6923 verifyFormat("Constructor() :\n" 6924 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6925 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6926 Style); 6927 6928 verifyFormat("Constructor(int Parameter = 0) :\n" 6929 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6930 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6931 Style); 6932 verifyFormat("Constructor() :\n" 6933 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6934 "}", 6935 getStyleWithColumns(Style, 60)); 6936 verifyFormat("Constructor() :\n" 6937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6938 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6939 Style); 6940 6941 // Here a line could be saved by splitting the second initializer onto two 6942 // lines, but that is not desirable. 6943 verifyFormat("Constructor() :\n" 6944 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6945 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6946 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6947 Style); 6948 6949 FormatStyle OnePerLine = Style; 6950 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6951 verifyFormat("SomeClass::Constructor() :\n" 6952 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6953 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6954 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6955 OnePerLine); 6956 verifyFormat("SomeClass::Constructor() :\n" 6957 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6958 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6959 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6960 OnePerLine); 6961 verifyFormat("MyClass::MyClass(int var) :\n" 6962 " some_var_(var), // 4 space indent\n" 6963 " some_other_var_(var + 1) { // lined up\n" 6964 "}", 6965 OnePerLine); 6966 verifyFormat("Constructor() :\n" 6967 " aaaaa(aaaaaa),\n" 6968 " aaaaa(aaaaaa),\n" 6969 " aaaaa(aaaaaa),\n" 6970 " aaaaa(aaaaaa),\n" 6971 " aaaaa(aaaaaa) {}", 6972 OnePerLine); 6973 verifyFormat("Constructor() :\n" 6974 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6975 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6976 OnePerLine); 6977 OnePerLine.BinPackParameters = false; 6978 verifyFormat("Constructor() :\n" 6979 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6980 " aaaaaaaaaaa().aaa(),\n" 6981 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6982 OnePerLine); 6983 OnePerLine.ColumnLimit = 60; 6984 verifyFormat("Constructor() :\n" 6985 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6986 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6987 OnePerLine); 6988 6989 EXPECT_EQ("Constructor() :\n" 6990 " // Comment forcing unwanted break.\n" 6991 " aaaa(aaaa) {}", 6992 format("Constructor() :\n" 6993 " // Comment forcing unwanted break.\n" 6994 " aaaa(aaaa) {}", 6995 Style)); 6996 6997 Style.ColumnLimit = 0; 6998 verifyFormat("SomeClass::Constructor() :\n" 6999 " a(a) {}", 7000 Style); 7001 verifyFormat("SomeClass::Constructor() noexcept :\n" 7002 " a(a) {}", 7003 Style); 7004 verifyFormat("SomeClass::Constructor() :\n" 7005 " a(a), b(b), c(c) {}", 7006 Style); 7007 verifyFormat("SomeClass::Constructor() :\n" 7008 " a(a) {\n" 7009 " foo();\n" 7010 " bar();\n" 7011 "}", 7012 Style); 7013 7014 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7015 verifyFormat("SomeClass::Constructor() :\n" 7016 " a(a), b(b), c(c) {\n" 7017 "}", 7018 Style); 7019 verifyFormat("SomeClass::Constructor() :\n" 7020 " a(a) {\n" 7021 "}", 7022 Style); 7023 7024 Style.ColumnLimit = 80; 7025 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7026 Style.ConstructorInitializerIndentWidth = 2; 7027 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 7028 verifyFormat("SomeClass::Constructor() :\n" 7029 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7030 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 7031 Style); 7032 7033 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 7034 // well 7035 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 7036 verifyFormat( 7037 "class SomeClass\n" 7038 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7039 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 7040 Style); 7041 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 7042 verifyFormat( 7043 "class SomeClass\n" 7044 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7045 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 7046 Style); 7047 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 7048 verifyFormat( 7049 "class SomeClass :\n" 7050 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7051 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 7052 Style); 7053 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 7054 verifyFormat( 7055 "class SomeClass\n" 7056 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7057 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 7058 Style); 7059 } 7060 7061 #ifndef EXPENSIVE_CHECKS 7062 // Expensive checks enables libstdc++ checking which includes validating the 7063 // state of ranges used in std::priority_queue - this blows out the 7064 // runtime/scalability of the function and makes this test unacceptably slow. 7065 TEST_F(FormatTest, MemoizationTests) { 7066 // This breaks if the memoization lookup does not take \c Indent and 7067 // \c LastSpace into account. 7068 verifyFormat( 7069 "extern CFRunLoopTimerRef\n" 7070 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 7071 " CFTimeInterval interval, CFOptionFlags flags,\n" 7072 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 7073 " CFRunLoopTimerContext *context) {}"); 7074 7075 // Deep nesting somewhat works around our memoization. 7076 verifyFormat( 7077 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 7078 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 7079 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 7080 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 7081 " aaaaa())))))))))))))))))))))))))))))))))))))));", 7082 getLLVMStyleWithColumns(65)); 7083 verifyFormat( 7084 "aaaaa(\n" 7085 " aaaaa,\n" 7086 " aaaaa(\n" 7087 " aaaaa,\n" 7088 " aaaaa(\n" 7089 " aaaaa,\n" 7090 " aaaaa(\n" 7091 " aaaaa,\n" 7092 " aaaaa(\n" 7093 " aaaaa,\n" 7094 " aaaaa(\n" 7095 " aaaaa,\n" 7096 " aaaaa(\n" 7097 " aaaaa,\n" 7098 " aaaaa(\n" 7099 " aaaaa,\n" 7100 " aaaaa(\n" 7101 " aaaaa,\n" 7102 " aaaaa(\n" 7103 " aaaaa,\n" 7104 " aaaaa(\n" 7105 " aaaaa,\n" 7106 " aaaaa(\n" 7107 " aaaaa,\n" 7108 " aaaaa))))))))))));", 7109 getLLVMStyleWithColumns(65)); 7110 verifyFormat( 7111 "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" 7112 " a),\n" 7113 " a),\n" 7114 " a),\n" 7115 " a),\n" 7116 " a),\n" 7117 " a),\n" 7118 " a),\n" 7119 " a),\n" 7120 " a),\n" 7121 " a),\n" 7122 " a),\n" 7123 " a),\n" 7124 " a),\n" 7125 " a),\n" 7126 " a),\n" 7127 " a),\n" 7128 " a)", 7129 getLLVMStyleWithColumns(65)); 7130 7131 // This test takes VERY long when memoization is broken. 7132 FormatStyle OnePerLine = getLLVMStyle(); 7133 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7134 OnePerLine.BinPackParameters = false; 7135 std::string input = "Constructor()\n" 7136 " : aaaa(a,\n"; 7137 for (unsigned i = 0, e = 80; i != e; ++i) { 7138 input += " a,\n"; 7139 } 7140 input += " a) {}"; 7141 verifyFormat(input, OnePerLine); 7142 } 7143 #endif 7144 7145 TEST_F(FormatTest, BreaksAsHighAsPossible) { 7146 verifyFormat( 7147 "void f() {\n" 7148 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 7149 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 7150 " f();\n" 7151 "}"); 7152 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 7153 " Intervals[i - 1].getRange().getLast()) {\n}"); 7154 } 7155 7156 TEST_F(FormatTest, BreaksFunctionDeclarations) { 7157 // Principially, we break function declarations in a certain order: 7158 // 1) break amongst arguments. 7159 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 7160 " Cccccccccccccc cccccccccccccc);"); 7161 verifyFormat("template <class TemplateIt>\n" 7162 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 7163 " TemplateIt *stop) {}"); 7164 7165 // 2) break after return type. 7166 verifyFormat( 7167 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7168 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 7169 getGoogleStyle()); 7170 7171 // 3) break after (. 7172 verifyFormat( 7173 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 7174 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 7175 getGoogleStyle()); 7176 7177 // 4) break before after nested name specifiers. 7178 verifyFormat( 7179 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7180 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 7181 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 7182 getGoogleStyle()); 7183 7184 // However, there are exceptions, if a sufficient amount of lines can be 7185 // saved. 7186 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 7187 // more adjusting. 7188 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 7189 " Cccccccccccccc cccccccccc,\n" 7190 " Cccccccccccccc cccccccccc,\n" 7191 " Cccccccccccccc cccccccccc,\n" 7192 " Cccccccccccccc cccccccccc);"); 7193 verifyFormat( 7194 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7195 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7196 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7197 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 7198 getGoogleStyle()); 7199 verifyFormat( 7200 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 7201 " Cccccccccccccc cccccccccc,\n" 7202 " Cccccccccccccc cccccccccc,\n" 7203 " Cccccccccccccc cccccccccc,\n" 7204 " Cccccccccccccc cccccccccc,\n" 7205 " Cccccccccccccc cccccccccc,\n" 7206 " Cccccccccccccc cccccccccc);"); 7207 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7208 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7209 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7210 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7211 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 7212 7213 // Break after multi-line parameters. 7214 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7215 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7217 " bbbb bbbb);"); 7218 verifyFormat("void SomeLoooooooooooongFunction(\n" 7219 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7220 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7221 " int bbbbbbbbbbbbb);"); 7222 7223 // Treat overloaded operators like other functions. 7224 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7225 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 7226 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7227 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 7228 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7229 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 7230 verifyGoogleFormat( 7231 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 7232 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 7233 verifyGoogleFormat( 7234 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 7235 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 7236 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7237 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 7238 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 7239 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 7240 verifyGoogleFormat( 7241 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 7242 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7243 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 7244 verifyGoogleFormat("template <typename T>\n" 7245 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7246 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 7247 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 7248 7249 FormatStyle Style = getLLVMStyle(); 7250 Style.PointerAlignment = FormatStyle::PAS_Left; 7251 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7252 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 7253 Style); 7254 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 7255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7256 Style); 7257 } 7258 7259 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 7260 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 7261 // Prefer keeping `::` followed by `operator` together. 7262 EXPECT_EQ("const aaaa::bbbbbbb &\n" 7263 "ccccccccc::operator++() {\n" 7264 " stuff();\n" 7265 "}", 7266 format("const aaaa::bbbbbbb\n" 7267 "&ccccccccc::operator++() { stuff(); }", 7268 getLLVMStyleWithColumns(40))); 7269 } 7270 7271 TEST_F(FormatTest, TrailingReturnType) { 7272 verifyFormat("auto foo() -> int;\n"); 7273 // correct trailing return type spacing 7274 verifyFormat("auto operator->() -> int;\n"); 7275 verifyFormat("auto operator++(int) -> int;\n"); 7276 7277 verifyFormat("struct S {\n" 7278 " auto bar() const -> int;\n" 7279 "};"); 7280 verifyFormat("template <size_t Order, typename T>\n" 7281 "auto load_img(const std::string &filename)\n" 7282 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 7283 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 7284 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 7285 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 7286 verifyFormat("template <typename T>\n" 7287 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 7288 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 7289 7290 // Not trailing return types. 7291 verifyFormat("void f() { auto a = b->c(); }"); 7292 verifyFormat("auto a = p->foo();"); 7293 verifyFormat("int a = p->foo();"); 7294 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); 7295 } 7296 7297 TEST_F(FormatTest, DeductionGuides) { 7298 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 7299 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 7300 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 7301 verifyFormat( 7302 "template <class... T>\n" 7303 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 7304 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 7305 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 7306 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 7307 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 7308 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 7309 verifyFormat("template <class T> x() -> x<1>;"); 7310 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 7311 7312 // Ensure not deduction guides. 7313 verifyFormat("c()->f<int>();"); 7314 verifyFormat("x()->foo<1>;"); 7315 verifyFormat("x = p->foo<3>();"); 7316 verifyFormat("x()->x<1>();"); 7317 verifyFormat("x()->x<1>;"); 7318 } 7319 7320 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 7321 // Avoid breaking before trailing 'const' or other trailing annotations, if 7322 // they are not function-like. 7323 FormatStyle Style = getGoogleStyleWithColumns(47); 7324 verifyFormat("void someLongFunction(\n" 7325 " int someLoooooooooooooongParameter) const {\n}", 7326 getLLVMStyleWithColumns(47)); 7327 verifyFormat("LoooooongReturnType\n" 7328 "someLoooooooongFunction() const {}", 7329 getLLVMStyleWithColumns(47)); 7330 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 7331 " const {}", 7332 Style); 7333 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7334 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 7335 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7336 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 7337 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7338 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 7339 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 7340 " aaaaaaaaaaa aaaaa) const override;"); 7341 verifyGoogleFormat( 7342 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7343 " const override;"); 7344 7345 // Even if the first parameter has to be wrapped. 7346 verifyFormat("void someLongFunction(\n" 7347 " int someLongParameter) const {}", 7348 getLLVMStyleWithColumns(46)); 7349 verifyFormat("void someLongFunction(\n" 7350 " int someLongParameter) const {}", 7351 Style); 7352 verifyFormat("void someLongFunction(\n" 7353 " int someLongParameter) override {}", 7354 Style); 7355 verifyFormat("void someLongFunction(\n" 7356 " int someLongParameter) OVERRIDE {}", 7357 Style); 7358 verifyFormat("void someLongFunction(\n" 7359 " int someLongParameter) final {}", 7360 Style); 7361 verifyFormat("void someLongFunction(\n" 7362 " int someLongParameter) FINAL {}", 7363 Style); 7364 verifyFormat("void someLongFunction(\n" 7365 " int parameter) const override {}", 7366 Style); 7367 7368 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 7369 verifyFormat("void someLongFunction(\n" 7370 " int someLongParameter) const\n" 7371 "{\n" 7372 "}", 7373 Style); 7374 7375 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 7376 verifyFormat("void someLongFunction(\n" 7377 " int someLongParameter) const\n" 7378 " {\n" 7379 " }", 7380 Style); 7381 7382 // Unless these are unknown annotations. 7383 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 7384 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7385 " LONG_AND_UGLY_ANNOTATION;"); 7386 7387 // Breaking before function-like trailing annotations is fine to keep them 7388 // close to their arguments. 7389 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7390 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7391 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7392 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7393 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7394 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 7395 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 7396 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 7397 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 7398 7399 verifyFormat( 7400 "void aaaaaaaaaaaaaaaaaa()\n" 7401 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 7402 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 7403 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7404 " __attribute__((unused));"); 7405 verifyGoogleFormat( 7406 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7407 " GUARDED_BY(aaaaaaaaaaaa);"); 7408 verifyGoogleFormat( 7409 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7410 " GUARDED_BY(aaaaaaaaaaaa);"); 7411 verifyGoogleFormat( 7412 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7413 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7414 verifyGoogleFormat( 7415 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7416 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7417 } 7418 7419 TEST_F(FormatTest, FunctionAnnotations) { 7420 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7421 "int OldFunction(const string ¶meter) {}"); 7422 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7423 "string OldFunction(const string ¶meter) {}"); 7424 verifyFormat("template <typename T>\n" 7425 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7426 "string OldFunction(const string ¶meter) {}"); 7427 7428 // Not function annotations. 7429 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7430 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 7431 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 7432 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 7433 verifyFormat("MACRO(abc).function() // wrap\n" 7434 " << abc;"); 7435 verifyFormat("MACRO(abc)->function() // wrap\n" 7436 " << abc;"); 7437 verifyFormat("MACRO(abc)::function() // wrap\n" 7438 " << abc;"); 7439 } 7440 7441 TEST_F(FormatTest, BreaksDesireably) { 7442 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7443 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7444 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 7445 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 7447 "}"); 7448 7449 verifyFormat( 7450 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7452 7453 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7456 7457 verifyFormat( 7458 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7459 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7460 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7461 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 7463 7464 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7465 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7466 7467 verifyFormat( 7468 "void f() {\n" 7469 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7471 "}"); 7472 verifyFormat( 7473 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7474 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7475 verifyFormat( 7476 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7478 verifyFormat( 7479 "aaaaaa(aaa,\n" 7480 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7481 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7482 " aaaa);"); 7483 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7484 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7486 7487 // Indent consistently independent of call expression and unary operator. 7488 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7489 " dddddddddddddddddddddddddddddd));"); 7490 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7491 " dddddddddddddddddddddddddddddd));"); 7492 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 7493 " dddddddddddddddddddddddddddddd));"); 7494 7495 // This test case breaks on an incorrect memoization, i.e. an optimization not 7496 // taking into account the StopAt value. 7497 verifyFormat( 7498 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7499 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7500 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7501 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7502 7503 verifyFormat("{\n {\n {\n" 7504 " Annotation.SpaceRequiredBefore =\n" 7505 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 7506 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 7507 " }\n }\n}"); 7508 7509 // Break on an outer level if there was a break on an inner level. 7510 EXPECT_EQ("f(g(h(a, // comment\n" 7511 " b, c),\n" 7512 " d, e),\n" 7513 " x, y);", 7514 format("f(g(h(a, // comment\n" 7515 " b, c), d, e), x, y);")); 7516 7517 // Prefer breaking similar line breaks. 7518 verifyFormat( 7519 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 7520 " NSTrackingMouseEnteredAndExited |\n" 7521 " NSTrackingActiveAlways;"); 7522 } 7523 7524 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 7525 FormatStyle NoBinPacking = getGoogleStyle(); 7526 NoBinPacking.BinPackParameters = false; 7527 NoBinPacking.BinPackArguments = true; 7528 verifyFormat("void f() {\n" 7529 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 7530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7531 "}", 7532 NoBinPacking); 7533 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 7534 " int aaaaaaaaaaaaaaaaaaaa,\n" 7535 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7536 NoBinPacking); 7537 7538 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7539 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7540 " vector<int> bbbbbbbbbbbbbbb);", 7541 NoBinPacking); 7542 // FIXME: This behavior difference is probably not wanted. However, currently 7543 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 7544 // template arguments from BreakBeforeParameter being set because of the 7545 // one-per-line formatting. 7546 verifyFormat( 7547 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7548 " aaaaaaaaaa> aaaaaaaaaa);", 7549 NoBinPacking); 7550 verifyFormat( 7551 "void fffffffffff(\n" 7552 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 7553 " aaaaaaaaaa);"); 7554 } 7555 7556 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 7557 FormatStyle NoBinPacking = getGoogleStyle(); 7558 NoBinPacking.BinPackParameters = false; 7559 NoBinPacking.BinPackArguments = false; 7560 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 7561 " aaaaaaaaaaaaaaaaaaaa,\n" 7562 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 7563 NoBinPacking); 7564 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 7565 " aaaaaaaaaaaaa,\n" 7566 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 7567 NoBinPacking); 7568 verifyFormat( 7569 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7570 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7572 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 7574 NoBinPacking); 7575 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7576 " .aaaaaaaaaaaaaaaaaa();", 7577 NoBinPacking); 7578 verifyFormat("void f() {\n" 7579 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7580 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 7581 "}", 7582 NoBinPacking); 7583 7584 verifyFormat( 7585 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7586 " aaaaaaaaaaaa,\n" 7587 " aaaaaaaaaaaa);", 7588 NoBinPacking); 7589 verifyFormat( 7590 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 7591 " ddddddddddddddddddddddddddddd),\n" 7592 " test);", 7593 NoBinPacking); 7594 7595 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7596 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 7597 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 7598 " aaaaaaaaaaaaaaaaaa;", 7599 NoBinPacking); 7600 verifyFormat("a(\"a\"\n" 7601 " \"a\",\n" 7602 " a);"); 7603 7604 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7605 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7606 " aaaaaaaaa,\n" 7607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7608 NoBinPacking); 7609 verifyFormat( 7610 "void f() {\n" 7611 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7612 " .aaaaaaa();\n" 7613 "}", 7614 NoBinPacking); 7615 verifyFormat( 7616 "template <class SomeType, class SomeOtherType>\n" 7617 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7618 NoBinPacking); 7619 } 7620 7621 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7622 FormatStyle Style = getLLVMStyleWithColumns(15); 7623 Style.ExperimentalAutoDetectBinPacking = true; 7624 EXPECT_EQ("aaa(aaaa,\n" 7625 " aaaa,\n" 7626 " aaaa);\n" 7627 "aaa(aaaa,\n" 7628 " aaaa,\n" 7629 " aaaa);", 7630 format("aaa(aaaa,\n" // one-per-line 7631 " aaaa,\n" 7632 " aaaa );\n" 7633 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7634 Style)); 7635 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7636 " aaaa);\n" 7637 "aaa(aaaa, aaaa,\n" 7638 " aaaa);", 7639 format("aaa(aaaa, aaaa,\n" // bin-packed 7640 " aaaa );\n" 7641 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7642 Style)); 7643 } 7644 7645 TEST_F(FormatTest, FormatsBuilderPattern) { 7646 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7647 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7648 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7649 " .StartsWith(\".init\", ORDER_INIT)\n" 7650 " .StartsWith(\".fini\", ORDER_FINI)\n" 7651 " .StartsWith(\".hash\", ORDER_HASH)\n" 7652 " .Default(ORDER_TEXT);\n"); 7653 7654 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7655 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7656 verifyFormat("aaaaaaa->aaaaaaa\n" 7657 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7659 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7660 verifyFormat( 7661 "aaaaaaa->aaaaaaa\n" 7662 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7663 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7664 verifyFormat( 7665 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7666 " aaaaaaaaaaaaaa);"); 7667 verifyFormat( 7668 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7669 " aaaaaa->aaaaaaaaaaaa()\n" 7670 " ->aaaaaaaaaaaaaaaa(\n" 7671 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7672 " ->aaaaaaaaaaaaaaaaa();"); 7673 verifyGoogleFormat( 7674 "void f() {\n" 7675 " someo->Add((new util::filetools::Handler(dir))\n" 7676 " ->OnEvent1(NewPermanentCallback(\n" 7677 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7678 " ->OnEvent2(NewPermanentCallback(\n" 7679 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7680 " ->OnEvent3(NewPermanentCallback(\n" 7681 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7682 " ->OnEvent5(NewPermanentCallback(\n" 7683 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7684 " ->OnEvent6(NewPermanentCallback(\n" 7685 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7686 "}"); 7687 7688 verifyFormat( 7689 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7690 verifyFormat("aaaaaaaaaaaaaaa()\n" 7691 " .aaaaaaaaaaaaaaa()\n" 7692 " .aaaaaaaaaaaaaaa()\n" 7693 " .aaaaaaaaaaaaaaa()\n" 7694 " .aaaaaaaaaaaaaaa();"); 7695 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7696 " .aaaaaaaaaaaaaaa()\n" 7697 " .aaaaaaaaaaaaaaa()\n" 7698 " .aaaaaaaaaaaaaaa();"); 7699 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7700 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7701 " .aaaaaaaaaaaaaaa();"); 7702 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7703 " ->aaaaaaaaaaaaaae(0)\n" 7704 " ->aaaaaaaaaaaaaaa();"); 7705 7706 // Don't linewrap after very short segments. 7707 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7708 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7709 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7710 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7711 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7712 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7713 verifyFormat("aaa()\n" 7714 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7715 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7716 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7717 7718 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7719 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7720 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7721 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7722 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7724 7725 // Prefer not to break after empty parentheses. 7726 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7727 " First->LastNewlineOffset);"); 7728 7729 // Prefer not to create "hanging" indents. 7730 verifyFormat( 7731 "return !soooooooooooooome_map\n" 7732 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7733 " .second;"); 7734 verifyFormat( 7735 "return aaaaaaaaaaaaaaaa\n" 7736 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7737 " .aaaa(aaaaaaaaaaaaaa);"); 7738 // No hanging indent here. 7739 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7741 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7743 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7744 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7745 getLLVMStyleWithColumns(60)); 7746 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7747 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7748 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7749 getLLVMStyleWithColumns(59)); 7750 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7752 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7753 7754 // Dont break if only closing statements before member call 7755 verifyFormat("test() {\n" 7756 " ([]() -> {\n" 7757 " int b = 32;\n" 7758 " return 3;\n" 7759 " }).foo();\n" 7760 "}"); 7761 verifyFormat("test() {\n" 7762 " (\n" 7763 " []() -> {\n" 7764 " int b = 32;\n" 7765 " return 3;\n" 7766 " },\n" 7767 " foo, bar)\n" 7768 " .foo();\n" 7769 "}"); 7770 verifyFormat("test() {\n" 7771 " ([]() -> {\n" 7772 " int b = 32;\n" 7773 " return 3;\n" 7774 " })\n" 7775 " .foo()\n" 7776 " .bar();\n" 7777 "}"); 7778 verifyFormat("test() {\n" 7779 " ([]() -> {\n" 7780 " int b = 32;\n" 7781 " return 3;\n" 7782 " })\n" 7783 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7784 " \"bbbb\");\n" 7785 "}", 7786 getLLVMStyleWithColumns(30)); 7787 } 7788 7789 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7790 verifyFormat( 7791 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7792 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7793 verifyFormat( 7794 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7795 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7796 7797 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7798 " ccccccccccccccccccccccccc) {\n}"); 7799 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7800 " ccccccccccccccccccccccccc) {\n}"); 7801 7802 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7803 " ccccccccccccccccccccccccc) {\n}"); 7804 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7805 " ccccccccccccccccccccccccc) {\n}"); 7806 7807 verifyFormat( 7808 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7809 " ccccccccccccccccccccccccc) {\n}"); 7810 verifyFormat( 7811 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7812 " ccccccccccccccccccccccccc) {\n}"); 7813 7814 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7815 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7816 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7817 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7818 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7819 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7820 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7821 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7822 7823 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7824 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7825 " aaaaaaaaaaaaaaa != aa) {\n}"); 7826 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7827 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7828 " aaaaaaaaaaaaaaa != aa) {\n}"); 7829 } 7830 7831 TEST_F(FormatTest, BreaksAfterAssignments) { 7832 verifyFormat( 7833 "unsigned Cost =\n" 7834 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7835 " SI->getPointerAddressSpaceee());\n"); 7836 verifyFormat( 7837 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7838 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7839 7840 verifyFormat( 7841 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7842 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7843 verifyFormat("unsigned OriginalStartColumn =\n" 7844 " SourceMgr.getSpellingColumnNumber(\n" 7845 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7846 " 1;"); 7847 } 7848 7849 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7850 FormatStyle Style = getLLVMStyle(); 7851 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7852 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7853 Style); 7854 7855 Style.PenaltyBreakAssignment = 20; 7856 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7857 " cccccccccccccccccccccccccc;", 7858 Style); 7859 } 7860 7861 TEST_F(FormatTest, AlignsAfterAssignments) { 7862 verifyFormat( 7863 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7864 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7865 verifyFormat( 7866 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7867 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7868 verifyFormat( 7869 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7870 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7871 verifyFormat( 7872 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7873 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7874 verifyFormat( 7875 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7876 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7877 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7878 } 7879 7880 TEST_F(FormatTest, AlignsAfterReturn) { 7881 verifyFormat( 7882 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7883 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7884 verifyFormat( 7885 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7886 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7887 verifyFormat( 7888 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7889 " aaaaaaaaaaaaaaaaaaaaaa();"); 7890 verifyFormat( 7891 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7892 " aaaaaaaaaaaaaaaaaaaaaa());"); 7893 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7894 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7895 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7898 verifyFormat("return\n" 7899 " // true if code is one of a or b.\n" 7900 " code == a || code == b;"); 7901 } 7902 7903 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7904 verifyFormat( 7905 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7906 " aaaaaaaaa aaaaaaa) {}"); 7907 verifyFormat( 7908 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7909 " aaaaaaaaaaa aaaaaaaaa);"); 7910 verifyFormat( 7911 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7912 " aaaaaaaaaaaaaaaaaaaaa));"); 7913 FormatStyle Style = getLLVMStyle(); 7914 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7915 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7916 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7917 Style); 7918 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7919 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7920 Style); 7921 verifyFormat("SomeLongVariableName->someFunction(\n" 7922 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7923 Style); 7924 verifyFormat( 7925 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7926 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7927 Style); 7928 verifyFormat( 7929 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7930 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7931 Style); 7932 verifyFormat( 7933 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7934 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7935 Style); 7936 7937 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7938 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7939 " b));", 7940 Style); 7941 7942 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7943 Style.BinPackArguments = false; 7944 Style.BinPackParameters = false; 7945 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7946 " aaaaaaaaaaa aaaaaaaa,\n" 7947 " aaaaaaaaa aaaaaaa,\n" 7948 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7949 Style); 7950 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7951 " aaaaaaaaaaa aaaaaaaaa,\n" 7952 " aaaaaaaaaaa aaaaaaaaa,\n" 7953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7954 Style); 7955 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7956 " aaaaaaaaaaaaaaa,\n" 7957 " aaaaaaaaaaaaaaaaaaaaa,\n" 7958 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7959 Style); 7960 verifyFormat( 7961 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7962 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7963 Style); 7964 verifyFormat( 7965 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7966 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7967 Style); 7968 verifyFormat( 7969 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7970 " aaaaaaaaaaaaaaaaaaaaa(\n" 7971 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7972 " aaaaaaaaaaaaaaaa);", 7973 Style); 7974 verifyFormat( 7975 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7976 " aaaaaaaaaaaaaaaaaaaaa(\n" 7977 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7978 " aaaaaaaaaaaaaaaa);", 7979 Style); 7980 } 7981 7982 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7983 FormatStyle Style = getLLVMStyleWithColumns(40); 7984 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7985 " bbbbbbbbbbbbbbbbbbbbbb);", 7986 Style); 7987 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7988 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7989 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7990 " bbbbbbbbbbbbbbbbbbbbbb);", 7991 Style); 7992 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7993 Style.AlignOperands = FormatStyle::OAS_Align; 7994 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7995 " bbbbbbbbbbbbbbbbbbbbbb);", 7996 Style); 7997 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7998 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7999 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 8000 " bbbbbbbbbbbbbbbbbbbbbb);", 8001 Style); 8002 } 8003 8004 TEST_F(FormatTest, BreaksConditionalExpressions) { 8005 verifyFormat( 8006 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8007 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8008 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8009 verifyFormat( 8010 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 8011 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8012 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8013 verifyFormat( 8014 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8015 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8016 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 8017 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8018 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8019 verifyFormat( 8020 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 8021 " : aaaaaaaaaaaaa);"); 8022 verifyFormat( 8023 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8024 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8025 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8026 " aaaaaaaaaaaaa);"); 8027 verifyFormat( 8028 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8029 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8030 " aaaaaaaaaaaaa);"); 8031 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8032 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8034 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8036 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8037 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8038 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8040 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8042 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8043 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8045 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8046 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8047 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8048 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8049 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8050 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8051 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 8052 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8053 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8054 " : aaaaaaaaaaaaaaaa;"); 8055 verifyFormat( 8056 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8057 " ? aaaaaaaaaaaaaaa\n" 8058 " : aaaaaaaaaaaaaaa;"); 8059 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 8060 " aaaaaaaaa\n" 8061 " ? b\n" 8062 " : c);"); 8063 verifyFormat("return aaaa == bbbb\n" 8064 " // comment\n" 8065 " ? aaaa\n" 8066 " : bbbb;"); 8067 verifyFormat("unsigned Indent =\n" 8068 " format(TheLine.First,\n" 8069 " IndentForLevel[TheLine.Level] >= 0\n" 8070 " ? IndentForLevel[TheLine.Level]\n" 8071 " : TheLine * 2,\n" 8072 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 8073 getLLVMStyleWithColumns(60)); 8074 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 8075 " ? aaaaaaaaaaaaaaa\n" 8076 " : bbbbbbbbbbbbbbb //\n" 8077 " ? ccccccccccccccc\n" 8078 " : ddddddddddddddd;"); 8079 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 8080 " ? aaaaaaaaaaaaaaa\n" 8081 " : (bbbbbbbbbbbbbbb //\n" 8082 " ? ccccccccccccccc\n" 8083 " : ddddddddddddddd);"); 8084 verifyFormat( 8085 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8086 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 8087 " aaaaaaaaaaaaaaaaaaaaa +\n" 8088 " aaaaaaaaaaaaaaaaaaaaa\n" 8089 " : aaaaaaaaaa;"); 8090 verifyFormat( 8091 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8092 " : aaaaaaaaaaaaaaaaaaaaaa\n" 8093 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8094 8095 FormatStyle NoBinPacking = getLLVMStyle(); 8096 NoBinPacking.BinPackArguments = false; 8097 verifyFormat( 8098 "void f() {\n" 8099 " g(aaa,\n" 8100 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 8101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8102 " ? aaaaaaaaaaaaaaa\n" 8103 " : aaaaaaaaaaaaaaa);\n" 8104 "}", 8105 NoBinPacking); 8106 verifyFormat( 8107 "void f() {\n" 8108 " g(aaa,\n" 8109 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 8110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8111 " ?: aaaaaaaaaaaaaaa);\n" 8112 "}", 8113 NoBinPacking); 8114 8115 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 8116 " // comment.\n" 8117 " ccccccccccccccccccccccccccccccccccccccc\n" 8118 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8119 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 8120 8121 // Assignments in conditional expressions. Apparently not uncommon :-(. 8122 verifyFormat("return a != b\n" 8123 " // comment\n" 8124 " ? a = b\n" 8125 " : a = b;"); 8126 verifyFormat("return a != b\n" 8127 " // comment\n" 8128 " ? a = a != b\n" 8129 " // comment\n" 8130 " ? a = b\n" 8131 " : a\n" 8132 " : a;\n"); 8133 verifyFormat("return a != b\n" 8134 " // comment\n" 8135 " ? a\n" 8136 " : a = a != b\n" 8137 " // comment\n" 8138 " ? a = b\n" 8139 " : a;"); 8140 8141 // Chained conditionals 8142 FormatStyle Style = getLLVMStyleWithColumns(70); 8143 Style.AlignOperands = FormatStyle::OAS_Align; 8144 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8145 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8146 " : 3333333333333333;", 8147 Style); 8148 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8149 " : bbbbbbbbbb ? 2222222222222222\n" 8150 " : 3333333333333333;", 8151 Style); 8152 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 8153 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 8154 " : 3333333333333333;", 8155 Style); 8156 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8157 " : bbbbbbbbbbbbbb ? 222222\n" 8158 " : 333333;", 8159 Style); 8160 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8161 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8162 " : cccccccccccccc ? 3333333333333333\n" 8163 " : 4444444444444444;", 8164 Style); 8165 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 8166 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8167 " : 3333333333333333;", 8168 Style); 8169 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8170 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8171 " : (aaa ? bbb : ccc);", 8172 Style); 8173 verifyFormat( 8174 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8175 " : cccccccccccccccccc)\n" 8176 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8177 " : 3333333333333333;", 8178 Style); 8179 verifyFormat( 8180 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8181 " : cccccccccccccccccc)\n" 8182 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8183 " : 3333333333333333;", 8184 Style); 8185 verifyFormat( 8186 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8187 " : dddddddddddddddddd)\n" 8188 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8189 " : 3333333333333333;", 8190 Style); 8191 verifyFormat( 8192 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8193 " : dddddddddddddddddd)\n" 8194 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8195 " : 3333333333333333;", 8196 Style); 8197 verifyFormat( 8198 "return aaaaaaaaa ? 1111111111111111\n" 8199 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8200 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8201 " : dddddddddddddddddd)\n", 8202 Style); 8203 verifyFormat( 8204 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8205 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8206 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8207 " : cccccccccccccccccc);", 8208 Style); 8209 verifyFormat( 8210 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8211 " : ccccccccccccccc ? dddddddddddddddddd\n" 8212 " : eeeeeeeeeeeeeeeeee)\n" 8213 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8214 " : 3333333333333333;", 8215 Style); 8216 verifyFormat( 8217 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8218 " : ccccccccccccccc ? dddddddddddddddddd\n" 8219 " : eeeeeeeeeeeeeeeeee)\n" 8220 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8221 " : 3333333333333333;", 8222 Style); 8223 verifyFormat( 8224 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8225 " : cccccccccccc ? dddddddddddddddddd\n" 8226 " : eeeeeeeeeeeeeeeeee)\n" 8227 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8228 " : 3333333333333333;", 8229 Style); 8230 verifyFormat( 8231 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8232 " : cccccccccccccccccc\n" 8233 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8234 " : 3333333333333333;", 8235 Style); 8236 verifyFormat( 8237 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8238 " : cccccccccccccccc ? dddddddddddddddddd\n" 8239 " : eeeeeeeeeeeeeeeeee\n" 8240 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8241 " : 3333333333333333;", 8242 Style); 8243 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 8244 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8245 " : cccccccccccccccccc ? dddddddddddddddddd\n" 8246 " : eeeeeeeeeeeeeeeeee)\n" 8247 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 8248 " : 3333333333333333;", 8249 Style); 8250 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 8251 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8252 " : cccccccccccccccc ? dddddddddddddddddd\n" 8253 " : eeeeeeeeeeeeeeeeee\n" 8254 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 8255 " : 3333333333333333;", 8256 Style); 8257 8258 Style.AlignOperands = FormatStyle::OAS_DontAlign; 8259 Style.BreakBeforeTernaryOperators = false; 8260 // FIXME: Aligning the question marks is weird given DontAlign. 8261 // Consider disabling this alignment in this case. Also check whether this 8262 // will render the adjustment from https://reviews.llvm.org/D82199 8263 // unnecessary. 8264 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 8265 " bbbb ? cccccccccccccccccc :\n" 8266 " ddddd;\n", 8267 Style); 8268 8269 EXPECT_EQ( 8270 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8271 " /*\n" 8272 " */\n" 8273 " function() {\n" 8274 " try {\n" 8275 " return JJJJJJJJJJJJJJ(\n" 8276 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8277 " }\n" 8278 " } :\n" 8279 " function() {};", 8280 format( 8281 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8282 " /*\n" 8283 " */\n" 8284 " function() {\n" 8285 " try {\n" 8286 " return JJJJJJJJJJJJJJ(\n" 8287 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8288 " }\n" 8289 " } :\n" 8290 " function() {};", 8291 getGoogleStyle(FormatStyle::LK_JavaScript))); 8292 } 8293 8294 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 8295 FormatStyle Style = getLLVMStyleWithColumns(70); 8296 Style.BreakBeforeTernaryOperators = false; 8297 verifyFormat( 8298 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8299 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8301 Style); 8302 verifyFormat( 8303 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 8304 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8305 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8306 Style); 8307 verifyFormat( 8308 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8309 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8310 Style); 8311 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 8312 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8314 Style); 8315 verifyFormat( 8316 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 8317 " aaaaaaaaaaaaa);", 8318 Style); 8319 verifyFormat( 8320 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8321 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8323 " aaaaaaaaaaaaa);", 8324 Style); 8325 verifyFormat( 8326 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8327 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8328 " aaaaaaaaaaaaa);", 8329 Style); 8330 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8331 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8332 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8335 Style); 8336 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8338 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8342 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8343 Style); 8344 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 8346 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8348 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8349 Style); 8350 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8351 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8352 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8353 Style); 8354 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 8355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8358 Style); 8359 verifyFormat( 8360 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8361 " aaaaaaaaaaaaaaa :\n" 8362 " aaaaaaaaaaaaaaa;", 8363 Style); 8364 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 8365 " aaaaaaaaa ?\n" 8366 " b :\n" 8367 " c);", 8368 Style); 8369 verifyFormat("unsigned Indent =\n" 8370 " format(TheLine.First,\n" 8371 " IndentForLevel[TheLine.Level] >= 0 ?\n" 8372 " IndentForLevel[TheLine.Level] :\n" 8373 " TheLine * 2,\n" 8374 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 8375 Style); 8376 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8377 " aaaaaaaaaaaaaaa :\n" 8378 " bbbbbbbbbbbbbbb ? //\n" 8379 " ccccccccccccccc :\n" 8380 " ddddddddddddddd;", 8381 Style); 8382 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8383 " aaaaaaaaaaaaaaa :\n" 8384 " (bbbbbbbbbbbbbbb ? //\n" 8385 " ccccccccccccccc :\n" 8386 " ddddddddddddddd);", 8387 Style); 8388 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8389 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 8390 " ccccccccccccccccccccccccccc;", 8391 Style); 8392 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8393 " aaaaa :\n" 8394 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 8395 Style); 8396 8397 // Chained conditionals 8398 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8399 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8400 " 3333333333333333;", 8401 Style); 8402 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8403 " bbbbbbbbbb ? 2222222222222222 :\n" 8404 " 3333333333333333;", 8405 Style); 8406 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 8407 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8408 " 3333333333333333;", 8409 Style); 8410 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8411 " bbbbbbbbbbbbbbbb ? 222222 :\n" 8412 " 333333;", 8413 Style); 8414 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8415 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8416 " cccccccccccccccc ? 3333333333333333 :\n" 8417 " 4444444444444444;", 8418 Style); 8419 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 8420 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8421 " 3333333333333333;", 8422 Style); 8423 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8424 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8425 " (aaa ? bbb : ccc);", 8426 Style); 8427 verifyFormat( 8428 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8429 " cccccccccccccccccc) :\n" 8430 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8431 " 3333333333333333;", 8432 Style); 8433 verifyFormat( 8434 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8435 " cccccccccccccccccc) :\n" 8436 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8437 " 3333333333333333;", 8438 Style); 8439 verifyFormat( 8440 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8441 " dddddddddddddddddd) :\n" 8442 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8443 " 3333333333333333;", 8444 Style); 8445 verifyFormat( 8446 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8447 " dddddddddddddddddd) :\n" 8448 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8449 " 3333333333333333;", 8450 Style); 8451 verifyFormat( 8452 "return aaaaaaaaa ? 1111111111111111 :\n" 8453 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8454 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8455 " dddddddddddddddddd)\n", 8456 Style); 8457 verifyFormat( 8458 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8459 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8460 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8461 " cccccccccccccccccc);", 8462 Style); 8463 verifyFormat( 8464 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8465 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8466 " eeeeeeeeeeeeeeeeee) :\n" 8467 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8468 " 3333333333333333;", 8469 Style); 8470 verifyFormat( 8471 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8472 " ccccccccccccc ? dddddddddddddddddd :\n" 8473 " eeeeeeeeeeeeeeeeee) :\n" 8474 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8475 " 3333333333333333;", 8476 Style); 8477 verifyFormat( 8478 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8479 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8480 " eeeeeeeeeeeeeeeeee) :\n" 8481 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8482 " 3333333333333333;", 8483 Style); 8484 verifyFormat( 8485 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8486 " cccccccccccccccccc :\n" 8487 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8488 " 3333333333333333;", 8489 Style); 8490 verifyFormat( 8491 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8492 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8493 " eeeeeeeeeeeeeeeeee :\n" 8494 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8495 " 3333333333333333;", 8496 Style); 8497 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8498 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8499 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8500 " eeeeeeeeeeeeeeeeee) :\n" 8501 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8502 " 3333333333333333;", 8503 Style); 8504 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8505 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8506 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 8507 " eeeeeeeeeeeeeeeeee :\n" 8508 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8509 " 3333333333333333;", 8510 Style); 8511 } 8512 8513 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 8514 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 8515 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 8516 verifyFormat("bool a = true, b = false;"); 8517 8518 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8519 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 8520 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 8521 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 8522 verifyFormat( 8523 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 8524 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 8525 " d = e && f;"); 8526 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 8527 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 8528 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8529 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 8530 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 8531 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 8532 8533 FormatStyle Style = getGoogleStyle(); 8534 Style.PointerAlignment = FormatStyle::PAS_Left; 8535 Style.DerivePointerAlignment = false; 8536 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8537 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 8538 " *b = bbbbbbbbbbbbbbbbbbb;", 8539 Style); 8540 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8541 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 8542 Style); 8543 verifyFormat("vector<int*> a, b;", Style); 8544 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 8545 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style); 8546 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style); 8547 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}", 8548 Style); 8549 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}", 8550 Style); 8551 verifyFormat( 8552 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}", 8553 Style); 8554 8555 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style); 8556 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style); 8557 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style); 8558 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style); 8559 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}", 8560 Style); 8561 } 8562 8563 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 8564 verifyFormat("arr[foo ? bar : baz];"); 8565 verifyFormat("f()[foo ? bar : baz];"); 8566 verifyFormat("(a + b)[foo ? bar : baz];"); 8567 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 8568 } 8569 8570 TEST_F(FormatTest, AlignsStringLiterals) { 8571 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 8572 " \"short literal\");"); 8573 verifyFormat( 8574 "looooooooooooooooooooooooongFunction(\n" 8575 " \"short literal\"\n" 8576 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 8577 verifyFormat("someFunction(\"Always break between multi-line\"\n" 8578 " \" string literals\",\n" 8579 " and, other, parameters);"); 8580 EXPECT_EQ("fun + \"1243\" /* comment */\n" 8581 " \"5678\";", 8582 format("fun + \"1243\" /* comment */\n" 8583 " \"5678\";", 8584 getLLVMStyleWithColumns(28))); 8585 EXPECT_EQ( 8586 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8587 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 8588 " \"aaaaaaaaaaaaaaaa\";", 8589 format("aaaaaa =" 8590 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8591 "aaaaaaaaaaaaaaaaaaaaa\" " 8592 "\"aaaaaaaaaaaaaaaa\";")); 8593 verifyFormat("a = a + \"a\"\n" 8594 " \"a\"\n" 8595 " \"a\";"); 8596 verifyFormat("f(\"a\", \"b\"\n" 8597 " \"c\");"); 8598 8599 verifyFormat( 8600 "#define LL_FORMAT \"ll\"\n" 8601 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 8602 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 8603 8604 verifyFormat("#define A(X) \\\n" 8605 " \"aaaaa\" #X \"bbbbbb\" \\\n" 8606 " \"ccccc\"", 8607 getLLVMStyleWithColumns(23)); 8608 verifyFormat("#define A \"def\"\n" 8609 "f(\"abc\" A \"ghi\"\n" 8610 " \"jkl\");"); 8611 8612 verifyFormat("f(L\"a\"\n" 8613 " L\"b\");"); 8614 verifyFormat("#define A(X) \\\n" 8615 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 8616 " L\"ccccc\"", 8617 getLLVMStyleWithColumns(25)); 8618 8619 verifyFormat("f(@\"a\"\n" 8620 " @\"b\");"); 8621 verifyFormat("NSString s = @\"a\"\n" 8622 " @\"b\"\n" 8623 " @\"c\";"); 8624 verifyFormat("NSString s = @\"a\"\n" 8625 " \"b\"\n" 8626 " \"c\";"); 8627 } 8628 8629 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8630 FormatStyle Style = getLLVMStyle(); 8631 // No declarations or definitions should be moved to own line. 8632 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8633 verifyFormat("class A {\n" 8634 " int f() { return 1; }\n" 8635 " int g();\n" 8636 "};\n" 8637 "int f() { return 1; }\n" 8638 "int g();\n", 8639 Style); 8640 8641 // All declarations and definitions should have the return type moved to its 8642 // own line. 8643 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8644 Style.TypenameMacros = {"LIST"}; 8645 verifyFormat("SomeType\n" 8646 "funcdecl(LIST(uint64_t));", 8647 Style); 8648 verifyFormat("class E {\n" 8649 " int\n" 8650 " f() {\n" 8651 " return 1;\n" 8652 " }\n" 8653 " int\n" 8654 " g();\n" 8655 "};\n" 8656 "int\n" 8657 "f() {\n" 8658 " return 1;\n" 8659 "}\n" 8660 "int\n" 8661 "g();\n", 8662 Style); 8663 8664 // Top-level definitions, and no kinds of declarations should have the 8665 // return type moved to its own line. 8666 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8667 verifyFormat("class B {\n" 8668 " int f() { return 1; }\n" 8669 " int g();\n" 8670 "};\n" 8671 "int\n" 8672 "f() {\n" 8673 " return 1;\n" 8674 "}\n" 8675 "int g();\n", 8676 Style); 8677 8678 // Top-level definitions and declarations should have the return type moved 8679 // to its own line. 8680 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8681 verifyFormat("class C {\n" 8682 " int f() { return 1; }\n" 8683 " int g();\n" 8684 "};\n" 8685 "int\n" 8686 "f() {\n" 8687 " return 1;\n" 8688 "}\n" 8689 "int\n" 8690 "g();\n", 8691 Style); 8692 8693 // All definitions should have the return type moved to its own line, but no 8694 // kinds of declarations. 8695 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8696 verifyFormat("class D {\n" 8697 " int\n" 8698 " f() {\n" 8699 " return 1;\n" 8700 " }\n" 8701 " int g();\n" 8702 "};\n" 8703 "int\n" 8704 "f() {\n" 8705 " return 1;\n" 8706 "}\n" 8707 "int g();\n", 8708 Style); 8709 verifyFormat("const char *\n" 8710 "f(void) {\n" // Break here. 8711 " return \"\";\n" 8712 "}\n" 8713 "const char *bar(void);\n", // No break here. 8714 Style); 8715 verifyFormat("template <class T>\n" 8716 "T *\n" 8717 "f(T &c) {\n" // Break here. 8718 " return NULL;\n" 8719 "}\n" 8720 "template <class T> T *f(T &c);\n", // No break here. 8721 Style); 8722 verifyFormat("class C {\n" 8723 " int\n" 8724 " operator+() {\n" 8725 " return 1;\n" 8726 " }\n" 8727 " int\n" 8728 " operator()() {\n" 8729 " return 1;\n" 8730 " }\n" 8731 "};\n", 8732 Style); 8733 verifyFormat("void\n" 8734 "A::operator()() {}\n" 8735 "void\n" 8736 "A::operator>>() {}\n" 8737 "void\n" 8738 "A::operator+() {}\n" 8739 "void\n" 8740 "A::operator*() {}\n" 8741 "void\n" 8742 "A::operator->() {}\n" 8743 "void\n" 8744 "A::operator void *() {}\n" 8745 "void\n" 8746 "A::operator void &() {}\n" 8747 "void\n" 8748 "A::operator void &&() {}\n" 8749 "void\n" 8750 "A::operator char *() {}\n" 8751 "void\n" 8752 "A::operator[]() {}\n" 8753 "void\n" 8754 "A::operator!() {}\n" 8755 "void\n" 8756 "A::operator**() {}\n" 8757 "void\n" 8758 "A::operator<Foo> *() {}\n" 8759 "void\n" 8760 "A::operator<Foo> **() {}\n" 8761 "void\n" 8762 "A::operator<Foo> &() {}\n" 8763 "void\n" 8764 "A::operator void **() {}\n", 8765 Style); 8766 verifyFormat("constexpr auto\n" 8767 "operator()() const -> reference {}\n" 8768 "constexpr auto\n" 8769 "operator>>() const -> reference {}\n" 8770 "constexpr auto\n" 8771 "operator+() const -> reference {}\n" 8772 "constexpr auto\n" 8773 "operator*() const -> reference {}\n" 8774 "constexpr auto\n" 8775 "operator->() const -> reference {}\n" 8776 "constexpr auto\n" 8777 "operator++() const -> reference {}\n" 8778 "constexpr auto\n" 8779 "operator void *() const -> reference {}\n" 8780 "constexpr auto\n" 8781 "operator void **() const -> reference {}\n" 8782 "constexpr auto\n" 8783 "operator void *() const -> reference {}\n" 8784 "constexpr auto\n" 8785 "operator void &() const -> reference {}\n" 8786 "constexpr auto\n" 8787 "operator void &&() const -> reference {}\n" 8788 "constexpr auto\n" 8789 "operator char *() const -> reference {}\n" 8790 "constexpr auto\n" 8791 "operator!() const -> reference {}\n" 8792 "constexpr auto\n" 8793 "operator[]() const -> reference {}\n", 8794 Style); 8795 verifyFormat("void *operator new(std::size_t s);", // No break here. 8796 Style); 8797 verifyFormat("void *\n" 8798 "operator new(std::size_t s) {}", 8799 Style); 8800 verifyFormat("void *\n" 8801 "operator delete[](void *ptr) {}", 8802 Style); 8803 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8804 verifyFormat("const char *\n" 8805 "f(void)\n" // Break here. 8806 "{\n" 8807 " return \"\";\n" 8808 "}\n" 8809 "const char *bar(void);\n", // No break here. 8810 Style); 8811 verifyFormat("template <class T>\n" 8812 "T *\n" // Problem here: no line break 8813 "f(T &c)\n" // Break here. 8814 "{\n" 8815 " return NULL;\n" 8816 "}\n" 8817 "template <class T> T *f(T &c);\n", // No break here. 8818 Style); 8819 verifyFormat("int\n" 8820 "foo(A<bool> a)\n" 8821 "{\n" 8822 " return a;\n" 8823 "}\n", 8824 Style); 8825 verifyFormat("int\n" 8826 "foo(A<8> a)\n" 8827 "{\n" 8828 " return a;\n" 8829 "}\n", 8830 Style); 8831 verifyFormat("int\n" 8832 "foo(A<B<bool>, 8> a)\n" 8833 "{\n" 8834 " return a;\n" 8835 "}\n", 8836 Style); 8837 verifyFormat("int\n" 8838 "foo(A<B<8>, bool> a)\n" 8839 "{\n" 8840 " return a;\n" 8841 "}\n", 8842 Style); 8843 verifyFormat("int\n" 8844 "foo(A<B<bool>, bool> a)\n" 8845 "{\n" 8846 " return a;\n" 8847 "}\n", 8848 Style); 8849 verifyFormat("int\n" 8850 "foo(A<B<8>, 8> a)\n" 8851 "{\n" 8852 " return a;\n" 8853 "}\n", 8854 Style); 8855 8856 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8857 Style.BraceWrapping.AfterFunction = true; 8858 verifyFormat("int f(i);\n" // No break here. 8859 "int\n" // Break here. 8860 "f(i)\n" 8861 "{\n" 8862 " return i + 1;\n" 8863 "}\n" 8864 "int\n" // Break here. 8865 "f(i)\n" 8866 "{\n" 8867 " return i + 1;\n" 8868 "};", 8869 Style); 8870 verifyFormat("int f(a, b, c);\n" // No break here. 8871 "int\n" // Break here. 8872 "f(a, b, c)\n" // Break here. 8873 "short a, b;\n" 8874 "float c;\n" 8875 "{\n" 8876 " return a + b < c;\n" 8877 "}\n" 8878 "int\n" // Break here. 8879 "f(a, b, c)\n" // Break here. 8880 "short a, b;\n" 8881 "float c;\n" 8882 "{\n" 8883 " return a + b < c;\n" 8884 "};", 8885 Style); 8886 verifyFormat("byte *\n" // Break here. 8887 "f(a)\n" // Break here. 8888 "byte a[];\n" 8889 "{\n" 8890 " return a;\n" 8891 "}", 8892 Style); 8893 verifyFormat("bool f(int a, int) override;\n" 8894 "Bar g(int a, Bar) final;\n" 8895 "Bar h(a, Bar) final;", 8896 Style); 8897 verifyFormat("int\n" 8898 "f(a)", 8899 Style); 8900 verifyFormat("bool\n" 8901 "f(size_t = 0, bool b = false)\n" 8902 "{\n" 8903 " return !b;\n" 8904 "}", 8905 Style); 8906 8907 // The return breaking style doesn't affect: 8908 // * function and object definitions with attribute-like macros 8909 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8910 " ABSL_GUARDED_BY(mutex) = {};", 8911 getGoogleStyleWithColumns(40)); 8912 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8913 " ABSL_GUARDED_BY(mutex); // comment", 8914 getGoogleStyleWithColumns(40)); 8915 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8916 " ABSL_GUARDED_BY(mutex1)\n" 8917 " ABSL_GUARDED_BY(mutex2);", 8918 getGoogleStyleWithColumns(40)); 8919 verifyFormat("Tttttt f(int a, int b)\n" 8920 " ABSL_GUARDED_BY(mutex1)\n" 8921 " ABSL_GUARDED_BY(mutex2);", 8922 getGoogleStyleWithColumns(40)); 8923 // * typedefs 8924 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8925 8926 Style = getGNUStyle(); 8927 8928 // Test for comments at the end of function declarations. 8929 verifyFormat("void\n" 8930 "foo (int a, /*abc*/ int b) // def\n" 8931 "{\n" 8932 "}\n", 8933 Style); 8934 8935 verifyFormat("void\n" 8936 "foo (int a, /* abc */ int b) /* def */\n" 8937 "{\n" 8938 "}\n", 8939 Style); 8940 8941 // Definitions that should not break after return type 8942 verifyFormat("void foo (int a, int b); // def\n", Style); 8943 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8944 verifyFormat("void foo (int a, int b);\n", Style); 8945 } 8946 8947 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8948 FormatStyle NoBreak = getLLVMStyle(); 8949 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8950 FormatStyle Break = getLLVMStyle(); 8951 Break.AlwaysBreakBeforeMultilineStrings = true; 8952 verifyFormat("aaaa = \"bbbb\"\n" 8953 " \"cccc\";", 8954 NoBreak); 8955 verifyFormat("aaaa =\n" 8956 " \"bbbb\"\n" 8957 " \"cccc\";", 8958 Break); 8959 verifyFormat("aaaa(\"bbbb\"\n" 8960 " \"cccc\");", 8961 NoBreak); 8962 verifyFormat("aaaa(\n" 8963 " \"bbbb\"\n" 8964 " \"cccc\");", 8965 Break); 8966 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8967 " \"cccc\");", 8968 NoBreak); 8969 verifyFormat("aaaa(qqq,\n" 8970 " \"bbbb\"\n" 8971 " \"cccc\");", 8972 Break); 8973 verifyFormat("aaaa(qqq,\n" 8974 " L\"bbbb\"\n" 8975 " L\"cccc\");", 8976 Break); 8977 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8978 " \"bbbb\"));", 8979 Break); 8980 verifyFormat("string s = someFunction(\n" 8981 " \"abc\"\n" 8982 " \"abc\");", 8983 Break); 8984 8985 // As we break before unary operators, breaking right after them is bad. 8986 verifyFormat("string foo = abc ? \"x\"\n" 8987 " \"blah blah blah blah blah blah\"\n" 8988 " : \"y\";", 8989 Break); 8990 8991 // Don't break if there is no column gain. 8992 verifyFormat("f(\"aaaa\"\n" 8993 " \"bbbb\");", 8994 Break); 8995 8996 // Treat literals with escaped newlines like multi-line string literals. 8997 EXPECT_EQ("x = \"a\\\n" 8998 "b\\\n" 8999 "c\";", 9000 format("x = \"a\\\n" 9001 "b\\\n" 9002 "c\";", 9003 NoBreak)); 9004 EXPECT_EQ("xxxx =\n" 9005 " \"a\\\n" 9006 "b\\\n" 9007 "c\";", 9008 format("xxxx = \"a\\\n" 9009 "b\\\n" 9010 "c\";", 9011 Break)); 9012 9013 EXPECT_EQ("NSString *const kString =\n" 9014 " @\"aaaa\"\n" 9015 " @\"bbbb\";", 9016 format("NSString *const kString = @\"aaaa\"\n" 9017 "@\"bbbb\";", 9018 Break)); 9019 9020 Break.ColumnLimit = 0; 9021 verifyFormat("const char *hello = \"hello llvm\";", Break); 9022 } 9023 9024 TEST_F(FormatTest, AlignsPipes) { 9025 verifyFormat( 9026 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9027 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9028 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9029 verifyFormat( 9030 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 9031 " << aaaaaaaaaaaaaaaaaaaa;"); 9032 verifyFormat( 9033 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9034 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9035 verifyFormat( 9036 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9037 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9038 verifyFormat( 9039 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 9040 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 9041 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 9042 verifyFormat( 9043 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9044 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9045 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9046 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9049 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 9050 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 9051 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 9052 verifyFormat( 9053 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9055 verifyFormat( 9056 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 9057 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9058 9059 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 9060 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 9061 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9063 " aaaaaaaaaaaaaaaaaaaaa)\n" 9064 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9065 verifyFormat("LOG_IF(aaa == //\n" 9066 " bbb)\n" 9067 " << a << b;"); 9068 9069 // But sometimes, breaking before the first "<<" is desirable. 9070 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 9071 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 9072 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 9073 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9074 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9075 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 9076 " << BEF << IsTemplate << Description << E->getType();"); 9077 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 9078 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9079 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9080 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 9081 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9083 " << aaa;"); 9084 9085 verifyFormat( 9086 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9087 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9088 9089 // Incomplete string literal. 9090 EXPECT_EQ("llvm::errs() << \"\n" 9091 " << a;", 9092 format("llvm::errs() << \"\n<<a;")); 9093 9094 verifyFormat("void f() {\n" 9095 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 9096 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 9097 "}"); 9098 9099 // Handle 'endl'. 9100 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 9101 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 9102 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 9103 9104 // Handle '\n'. 9105 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 9106 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 9107 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 9108 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 9109 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 9110 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 9111 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 9112 } 9113 9114 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 9115 verifyFormat("return out << \"somepacket = {\\n\"\n" 9116 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 9117 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 9118 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 9119 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 9120 " << \"}\";"); 9121 9122 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 9123 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 9124 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 9125 verifyFormat( 9126 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 9127 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 9128 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 9129 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 9130 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 9131 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 9132 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 9133 verifyFormat( 9134 "void f() {\n" 9135 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 9136 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 9137 "}"); 9138 9139 // Breaking before the first "<<" is generally not desirable. 9140 verifyFormat( 9141 "llvm::errs()\n" 9142 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9143 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9144 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9145 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 9146 getLLVMStyleWithColumns(70)); 9147 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 9148 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9149 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 9150 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9151 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 9152 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 9153 getLLVMStyleWithColumns(70)); 9154 9155 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 9156 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 9157 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 9158 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 9159 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 9160 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 9161 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 9162 " (aaaa + aaaa);", 9163 getLLVMStyleWithColumns(40)); 9164 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 9165 " (aaaaaaa + aaaaa));", 9166 getLLVMStyleWithColumns(40)); 9167 verifyFormat( 9168 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 9169 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 9170 " bbbbbbbbbbbbbbbbbbbbbbb);"); 9171 } 9172 9173 TEST_F(FormatTest, UnderstandsEquals) { 9174 verifyFormat( 9175 "aaaaaaaaaaaaaaaaa =\n" 9176 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9177 verifyFormat( 9178 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9179 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 9180 verifyFormat( 9181 "if (a) {\n" 9182 " f();\n" 9183 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9184 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 9185 "}"); 9186 9187 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9188 " 100000000 + 10000000) {\n}"); 9189 } 9190 9191 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 9192 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 9193 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 9194 9195 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 9196 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 9197 9198 verifyFormat( 9199 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 9200 " Parameter2);"); 9201 9202 verifyFormat( 9203 "ShortObject->shortFunction(\n" 9204 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 9205 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 9206 9207 verifyFormat("loooooooooooooongFunction(\n" 9208 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 9209 9210 verifyFormat( 9211 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 9212 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 9213 9214 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 9215 " .WillRepeatedly(Return(SomeValue));"); 9216 verifyFormat("void f() {\n" 9217 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 9218 " .Times(2)\n" 9219 " .WillRepeatedly(Return(SomeValue));\n" 9220 "}"); 9221 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 9222 " ccccccccccccccccccccccc);"); 9223 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9224 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9225 " .aaaaa(aaaaa),\n" 9226 " aaaaaaaaaaaaaaaaaaaaa);"); 9227 verifyFormat("void f() {\n" 9228 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9229 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 9230 "}"); 9231 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9233 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9234 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9235 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9236 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9237 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9238 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9239 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 9240 "}"); 9241 9242 // Here, it is not necessary to wrap at "." or "->". 9243 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 9244 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 9245 verifyFormat( 9246 "aaaaaaaaaaa->aaaaaaaaa(\n" 9247 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9248 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 9249 9250 verifyFormat( 9251 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 9253 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 9254 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 9255 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 9256 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 9257 9258 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9259 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9260 " .a();"); 9261 9262 FormatStyle NoBinPacking = getLLVMStyle(); 9263 NoBinPacking.BinPackParameters = false; 9264 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 9265 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 9266 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 9267 " aaaaaaaaaaaaaaaaaaa,\n" 9268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9269 NoBinPacking); 9270 9271 // If there is a subsequent call, change to hanging indentation. 9272 verifyFormat( 9273 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9274 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 9275 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9276 verifyFormat( 9277 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9278 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 9279 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9281 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9282 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9284 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9285 } 9286 9287 TEST_F(FormatTest, WrapsTemplateDeclarations) { 9288 verifyFormat("template <typename T>\n" 9289 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9290 verifyFormat("template <typename T>\n" 9291 "// T should be one of {A, B}.\n" 9292 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9293 verifyFormat( 9294 "template <typename T>\n" 9295 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 9296 verifyFormat("template <typename T>\n" 9297 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 9298 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 9299 verifyFormat( 9300 "template <typename T>\n" 9301 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 9302 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 9303 verifyFormat( 9304 "template <typename T>\n" 9305 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 9306 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 9307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9308 verifyFormat("template <typename T>\n" 9309 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9310 " int aaaaaaaaaaaaaaaaaaaaaa);"); 9311 verifyFormat( 9312 "template <typename T1, typename T2 = char, typename T3 = char,\n" 9313 " typename T4 = char>\n" 9314 "void f();"); 9315 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 9316 " template <typename> class cccccccccccccccccccccc,\n" 9317 " typename ddddddddddddd>\n" 9318 "class C {};"); 9319 verifyFormat( 9320 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 9321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9322 9323 verifyFormat("void f() {\n" 9324 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 9325 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 9326 "}"); 9327 9328 verifyFormat("template <typename T> class C {};"); 9329 verifyFormat("template <typename T> void f();"); 9330 verifyFormat("template <typename T> void f() {}"); 9331 verifyFormat( 9332 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 9335 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 9338 " bbbbbbbbbbbbbbbbbbbbbbbb);", 9339 getLLVMStyleWithColumns(72)); 9340 EXPECT_EQ("static_cast<A< //\n" 9341 " B> *>(\n" 9342 "\n" 9343 ");", 9344 format("static_cast<A<//\n" 9345 " B>*>(\n" 9346 "\n" 9347 " );")); 9348 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9349 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 9350 9351 FormatStyle AlwaysBreak = getLLVMStyle(); 9352 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9353 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 9354 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 9355 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 9356 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9357 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9358 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 9359 verifyFormat("template <template <typename> class Fooooooo,\n" 9360 " template <typename> class Baaaaaaar>\n" 9361 "struct C {};", 9362 AlwaysBreak); 9363 verifyFormat("template <typename T> // T can be A, B or C.\n" 9364 "struct C {};", 9365 AlwaysBreak); 9366 verifyFormat("template <enum E> class A {\n" 9367 "public:\n" 9368 " E *f();\n" 9369 "};"); 9370 9371 FormatStyle NeverBreak = getLLVMStyle(); 9372 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 9373 verifyFormat("template <typename T> class C {};", NeverBreak); 9374 verifyFormat("template <typename T> void f();", NeverBreak); 9375 verifyFormat("template <typename T> void f() {}", NeverBreak); 9376 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9377 "bbbbbbbbbbbbbbbbbbbb) {}", 9378 NeverBreak); 9379 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9380 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9381 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 9382 NeverBreak); 9383 verifyFormat("template <template <typename> class Fooooooo,\n" 9384 " template <typename> class Baaaaaaar>\n" 9385 "struct C {};", 9386 NeverBreak); 9387 verifyFormat("template <typename T> // T can be A, B or C.\n" 9388 "struct C {};", 9389 NeverBreak); 9390 verifyFormat("template <enum E> class A {\n" 9391 "public:\n" 9392 " E *f();\n" 9393 "};", 9394 NeverBreak); 9395 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 9396 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9397 "bbbbbbbbbbbbbbbbbbbb) {}", 9398 NeverBreak); 9399 } 9400 9401 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 9402 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 9403 Style.ColumnLimit = 60; 9404 EXPECT_EQ("// Baseline - no comments.\n" 9405 "template <\n" 9406 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9407 "void f() {}", 9408 format("// Baseline - no comments.\n" 9409 "template <\n" 9410 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9411 "void f() {}", 9412 Style)); 9413 9414 EXPECT_EQ("template <\n" 9415 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9416 "void f() {}", 9417 format("template <\n" 9418 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9419 "void f() {}", 9420 Style)); 9421 9422 EXPECT_EQ( 9423 "template <\n" 9424 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9425 "void f() {}", 9426 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9427 "void f() {}", 9428 Style)); 9429 9430 EXPECT_EQ( 9431 "template <\n" 9432 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9433 " // multiline\n" 9434 "void f() {}", 9435 format("template <\n" 9436 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9437 " // multiline\n" 9438 "void f() {}", 9439 Style)); 9440 9441 EXPECT_EQ( 9442 "template <typename aaaaaaaaaa<\n" 9443 " bbbbbbbbbbbb>::value> // trailing loooong\n" 9444 "void f() {}", 9445 format( 9446 "template <\n" 9447 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 9448 "void f() {}", 9449 Style)); 9450 } 9451 9452 TEST_F(FormatTest, WrapsTemplateParameters) { 9453 FormatStyle Style = getLLVMStyle(); 9454 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9455 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9456 verifyFormat( 9457 "template <typename... a> struct q {};\n" 9458 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9459 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9460 " y;", 9461 Style); 9462 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9463 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9464 verifyFormat( 9465 "template <typename... a> struct r {};\n" 9466 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9467 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9468 " y;", 9469 Style); 9470 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9471 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9472 verifyFormat("template <typename... a> struct s {};\n" 9473 "extern s<\n" 9474 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9475 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9476 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9477 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9478 " y;", 9479 Style); 9480 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9481 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9482 verifyFormat("template <typename... a> struct t {};\n" 9483 "extern t<\n" 9484 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9485 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9486 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9487 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9488 " y;", 9489 Style); 9490 } 9491 9492 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 9493 verifyFormat( 9494 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9496 verifyFormat( 9497 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9499 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9500 9501 // FIXME: Should we have the extra indent after the second break? 9502 verifyFormat( 9503 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9506 9507 verifyFormat( 9508 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 9509 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 9510 9511 // Breaking at nested name specifiers is generally not desirable. 9512 verifyFormat( 9513 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9514 " aaaaaaaaaaaaaaaaaaaaaaa);"); 9515 9516 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 9517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9519 " aaaaaaaaaaaaaaaaaaaaa);", 9520 getLLVMStyleWithColumns(74)); 9521 9522 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9524 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9525 } 9526 9527 TEST_F(FormatTest, UnderstandsTemplateParameters) { 9528 verifyFormat("A<int> a;"); 9529 verifyFormat("A<A<A<int>>> a;"); 9530 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 9531 verifyFormat("bool x = a < 1 || 2 > a;"); 9532 verifyFormat("bool x = 5 < f<int>();"); 9533 verifyFormat("bool x = f<int>() > 5;"); 9534 verifyFormat("bool x = 5 < a<int>::x;"); 9535 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 9536 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 9537 9538 verifyGoogleFormat("A<A<int>> a;"); 9539 verifyGoogleFormat("A<A<A<int>>> a;"); 9540 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 9541 verifyGoogleFormat("A<A<int> > a;"); 9542 verifyGoogleFormat("A<A<A<int> > > a;"); 9543 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 9544 verifyGoogleFormat("A<::A<int>> a;"); 9545 verifyGoogleFormat("A<::A> a;"); 9546 verifyGoogleFormat("A< ::A> a;"); 9547 verifyGoogleFormat("A< ::A<int> > a;"); 9548 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 9549 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 9550 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 9551 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 9552 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 9553 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 9554 9555 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 9556 9557 // template closer followed by a token that starts with > or = 9558 verifyFormat("bool b = a<1> > 1;"); 9559 verifyFormat("bool b = a<1> >= 1;"); 9560 verifyFormat("int i = a<1> >> 1;"); 9561 FormatStyle Style = getLLVMStyle(); 9562 Style.SpaceBeforeAssignmentOperators = false; 9563 verifyFormat("bool b= a<1> == 1;", Style); 9564 verifyFormat("a<int> = 1;", Style); 9565 verifyFormat("a<int> >>= 1;", Style); 9566 9567 verifyFormat("test < a | b >> c;"); 9568 verifyFormat("test<test<a | b>> c;"); 9569 verifyFormat("test >> a >> b;"); 9570 verifyFormat("test << a >> b;"); 9571 9572 verifyFormat("f<int>();"); 9573 verifyFormat("template <typename T> void f() {}"); 9574 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 9575 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 9576 "sizeof(char)>::type>;"); 9577 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 9578 verifyFormat("f(a.operator()<A>());"); 9579 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9580 " .template operator()<A>());", 9581 getLLVMStyleWithColumns(35)); 9582 verifyFormat("bool_constant<a && noexcept(f())>"); 9583 verifyFormat("bool_constant<a || noexcept(f())>"); 9584 9585 // Not template parameters. 9586 verifyFormat("return a < b && c > d;"); 9587 verifyFormat("void f() {\n" 9588 " while (a < b && c > d) {\n" 9589 " }\n" 9590 "}"); 9591 verifyFormat("template <typename... Types>\n" 9592 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 9593 9594 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 9596 getLLVMStyleWithColumns(60)); 9597 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 9598 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 9599 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 9600 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 9601 } 9602 9603 TEST_F(FormatTest, UnderstandsShiftOperators) { 9604 verifyFormat("if (i < x >> 1)"); 9605 verifyFormat("while (i < x >> 1)"); 9606 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 9607 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 9608 verifyFormat( 9609 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 9610 verifyFormat("Foo.call<Bar<Function>>()"); 9611 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 9612 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 9613 "++i, v = v >> 1)"); 9614 verifyFormat("if (w<u<v<x>>, 1>::t)"); 9615 } 9616 9617 TEST_F(FormatTest, BitshiftOperatorWidth) { 9618 EXPECT_EQ("int a = 1 << 2; /* foo\n" 9619 " bar */", 9620 format("int a=1<<2; /* foo\n" 9621 " bar */")); 9622 9623 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 9624 " bar */", 9625 format("int b =256>>1 ; /* foo\n" 9626 " bar */")); 9627 } 9628 9629 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9630 verifyFormat("COMPARE(a, ==, b);"); 9631 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9632 } 9633 9634 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9635 verifyFormat("int A::*x;"); 9636 verifyFormat("int (S::*func)(void *);"); 9637 verifyFormat("void f() { int (S::*func)(void *); }"); 9638 verifyFormat("typedef bool *(Class::*Member)() const;"); 9639 verifyFormat("void f() {\n" 9640 " (a->*f)();\n" 9641 " a->*x;\n" 9642 " (a.*f)();\n" 9643 " ((*a).*f)();\n" 9644 " a.*x;\n" 9645 "}"); 9646 verifyFormat("void f() {\n" 9647 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9648 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9649 "}"); 9650 verifyFormat( 9651 "(aaaaaaaaaa->*bbbbbbb)(\n" 9652 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9653 FormatStyle Style = getLLVMStyle(); 9654 Style.PointerAlignment = FormatStyle::PAS_Left; 9655 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9656 } 9657 9658 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9659 verifyFormat("int a = -2;"); 9660 verifyFormat("f(-1, -2, -3);"); 9661 verifyFormat("a[-1] = 5;"); 9662 verifyFormat("int a = 5 + -2;"); 9663 verifyFormat("if (i == -1) {\n}"); 9664 verifyFormat("if (i != -1) {\n}"); 9665 verifyFormat("if (i > -1) {\n}"); 9666 verifyFormat("if (i < -1) {\n}"); 9667 verifyFormat("++(a->f());"); 9668 verifyFormat("--(a->f());"); 9669 verifyFormat("(a->f())++;"); 9670 verifyFormat("a[42]++;"); 9671 verifyFormat("if (!(a->f())) {\n}"); 9672 verifyFormat("if (!+i) {\n}"); 9673 verifyFormat("~&a;"); 9674 9675 verifyFormat("a-- > b;"); 9676 verifyFormat("b ? -a : c;"); 9677 verifyFormat("n * sizeof char16;"); 9678 verifyFormat("n * alignof char16;", getGoogleStyle()); 9679 verifyFormat("sizeof(char);"); 9680 verifyFormat("alignof(char);", getGoogleStyle()); 9681 9682 verifyFormat("return -1;"); 9683 verifyFormat("throw -1;"); 9684 verifyFormat("switch (a) {\n" 9685 "case -1:\n" 9686 " break;\n" 9687 "}"); 9688 verifyFormat("#define X -1"); 9689 verifyFormat("#define X -kConstant"); 9690 9691 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9692 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9693 9694 verifyFormat("int a = /* confusing comment */ -1;"); 9695 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9696 verifyFormat("int a = i /* confusing comment */++;"); 9697 9698 verifyFormat("co_yield -1;"); 9699 verifyFormat("co_return -1;"); 9700 9701 // Check that * is not treated as a binary operator when we set 9702 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9703 FormatStyle PASLeftStyle = getLLVMStyle(); 9704 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9705 verifyFormat("co_return *a;", PASLeftStyle); 9706 verifyFormat("co_await *a;", PASLeftStyle); 9707 verifyFormat("co_yield *a", PASLeftStyle); 9708 verifyFormat("return *a;", PASLeftStyle); 9709 } 9710 9711 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9712 verifyFormat("if (!aaaaaaaaaa( // break\n" 9713 " aaaaa)) {\n" 9714 "}"); 9715 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9716 " aaaaa));"); 9717 verifyFormat("*aaa = aaaaaaa( // break\n" 9718 " bbbbbb);"); 9719 } 9720 9721 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9722 verifyFormat("bool operator<();"); 9723 verifyFormat("bool operator>();"); 9724 verifyFormat("bool operator=();"); 9725 verifyFormat("bool operator==();"); 9726 verifyFormat("bool operator!=();"); 9727 verifyFormat("int operator+();"); 9728 verifyFormat("int operator++();"); 9729 verifyFormat("int operator++(int) volatile noexcept;"); 9730 verifyFormat("bool operator,();"); 9731 verifyFormat("bool operator();"); 9732 verifyFormat("bool operator()();"); 9733 verifyFormat("bool operator[]();"); 9734 verifyFormat("operator bool();"); 9735 verifyFormat("operator int();"); 9736 verifyFormat("operator void *();"); 9737 verifyFormat("operator SomeType<int>();"); 9738 verifyFormat("operator SomeType<int, int>();"); 9739 verifyFormat("operator SomeType<SomeType<int>>();"); 9740 verifyFormat("operator< <>();"); 9741 verifyFormat("operator<< <>();"); 9742 verifyFormat("< <>"); 9743 9744 verifyFormat("void *operator new(std::size_t size);"); 9745 verifyFormat("void *operator new[](std::size_t size);"); 9746 verifyFormat("void operator delete(void *ptr);"); 9747 verifyFormat("void operator delete[](void *ptr);"); 9748 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9749 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9750 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9751 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9752 9753 verifyFormat( 9754 "ostream &operator<<(ostream &OutputStream,\n" 9755 " SomeReallyLongType WithSomeReallyLongValue);"); 9756 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9757 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9758 " return left.group < right.group;\n" 9759 "}"); 9760 verifyFormat("SomeType &operator=(const SomeType &S);"); 9761 verifyFormat("f.template operator()<int>();"); 9762 9763 verifyGoogleFormat("operator void*();"); 9764 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9765 verifyGoogleFormat("operator ::A();"); 9766 9767 verifyFormat("using A::operator+;"); 9768 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9769 "int i;"); 9770 9771 // Calling an operator as a member function. 9772 verifyFormat("void f() { a.operator*(); }"); 9773 verifyFormat("void f() { a.operator*(b & b); }"); 9774 verifyFormat("void f() { a->operator&(a * b); }"); 9775 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9776 // TODO: Calling an operator as a non-member function is hard to distinguish. 9777 // https://llvm.org/PR50629 9778 // verifyFormat("void f() { operator*(a & a); }"); 9779 // verifyFormat("void f() { operator&(a, b * b); }"); 9780 9781 verifyFormat("::operator delete(foo);"); 9782 verifyFormat("::operator new(n * sizeof(foo));"); 9783 verifyFormat("foo() { ::operator delete(foo); }"); 9784 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 9785 } 9786 9787 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9788 verifyFormat("void A::b() && {}"); 9789 verifyFormat("void A::b() &&noexcept {}"); 9790 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9791 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9792 verifyFormat("Deleted &operator=(const Deleted &) &noexcept = default;"); 9793 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9794 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9795 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9796 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9797 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9798 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9799 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9800 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9801 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9802 verifyFormat("SomeType MemberFunction(const Deleted &) &&noexcept {}"); 9803 verifyFormat("void Fn(T const &) const &;"); 9804 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9805 verifyFormat("void Fn(T const volatile &&) const volatile &&noexcept;"); 9806 verifyFormat("template <typename T>\n" 9807 "void F(T) && = delete;", 9808 getGoogleStyle()); 9809 9810 FormatStyle AlignLeft = getLLVMStyle(); 9811 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9812 verifyFormat("void A::b() && {}", AlignLeft); 9813 verifyFormat("void A::b() && noexcept {}", AlignLeft); 9814 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9815 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;", 9816 AlignLeft); 9817 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9818 AlignLeft); 9819 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9820 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9821 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9822 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9823 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9824 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9825 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9826 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9827 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;", 9828 AlignLeft); 9829 9830 FormatStyle AlignMiddle = getLLVMStyle(); 9831 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9832 verifyFormat("void A::b() && {}", AlignMiddle); 9833 verifyFormat("void A::b() && noexcept {}", AlignMiddle); 9834 verifyFormat("Deleted & operator=(const Deleted &) & = default;", 9835 AlignMiddle); 9836 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;", 9837 AlignMiddle); 9838 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", 9839 AlignMiddle); 9840 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle); 9841 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle); 9842 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle); 9843 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle); 9844 verifyFormat("auto Function(T) & -> void {}", AlignMiddle); 9845 verifyFormat("auto Function(T) & -> void;", AlignMiddle); 9846 verifyFormat("void Fn(T const &) const &;", AlignMiddle); 9847 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle); 9848 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;", 9849 AlignMiddle); 9850 9851 FormatStyle Spaces = getLLVMStyle(); 9852 Spaces.SpacesInCStyleCastParentheses = true; 9853 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9854 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9855 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9856 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9857 9858 Spaces.SpacesInCStyleCastParentheses = false; 9859 Spaces.SpacesInParentheses = true; 9860 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9861 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9862 Spaces); 9863 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9864 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9865 9866 FormatStyle BreakTemplate = getLLVMStyle(); 9867 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9868 9869 verifyFormat("struct f {\n" 9870 " template <class T>\n" 9871 " int &foo(const std::string &str) &noexcept {}\n" 9872 "};", 9873 BreakTemplate); 9874 9875 verifyFormat("struct f {\n" 9876 " template <class T>\n" 9877 " int &foo(const std::string &str) &&noexcept {}\n" 9878 "};", 9879 BreakTemplate); 9880 9881 verifyFormat("struct f {\n" 9882 " template <class T>\n" 9883 " int &foo(const std::string &str) const &noexcept {}\n" 9884 "};", 9885 BreakTemplate); 9886 9887 verifyFormat("struct f {\n" 9888 " template <class T>\n" 9889 " int &foo(const std::string &str) const &noexcept {}\n" 9890 "};", 9891 BreakTemplate); 9892 9893 verifyFormat("struct f {\n" 9894 " template <class T>\n" 9895 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9896 "};", 9897 BreakTemplate); 9898 9899 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9900 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9901 FormatStyle::BTDS_Yes; 9902 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9903 9904 verifyFormat("struct f {\n" 9905 " template <class T>\n" 9906 " int& foo(const std::string& str) & noexcept {}\n" 9907 "};", 9908 AlignLeftBreakTemplate); 9909 9910 verifyFormat("struct f {\n" 9911 " template <class T>\n" 9912 " int& foo(const std::string& str) && noexcept {}\n" 9913 "};", 9914 AlignLeftBreakTemplate); 9915 9916 verifyFormat("struct f {\n" 9917 " template <class T>\n" 9918 " int& foo(const std::string& str) const& noexcept {}\n" 9919 "};", 9920 AlignLeftBreakTemplate); 9921 9922 verifyFormat("struct f {\n" 9923 " template <class T>\n" 9924 " int& foo(const std::string& str) const&& noexcept {}\n" 9925 "};", 9926 AlignLeftBreakTemplate); 9927 9928 verifyFormat("struct f {\n" 9929 " template <class T>\n" 9930 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9931 "};", 9932 AlignLeftBreakTemplate); 9933 9934 // The `&` in `Type&` should not be confused with a trailing `&` of 9935 // DEPRECATED(reason) member function. 9936 verifyFormat("struct f {\n" 9937 " template <class T>\n" 9938 " DEPRECATED(reason)\n" 9939 " Type &foo(arguments) {}\n" 9940 "};", 9941 BreakTemplate); 9942 9943 verifyFormat("struct f {\n" 9944 " template <class T>\n" 9945 " DEPRECATED(reason)\n" 9946 " Type& foo(arguments) {}\n" 9947 "};", 9948 AlignLeftBreakTemplate); 9949 9950 verifyFormat("void (*foopt)(int) = &func;"); 9951 9952 FormatStyle DerivePointerAlignment = getLLVMStyle(); 9953 DerivePointerAlignment.DerivePointerAlignment = true; 9954 // There's always a space between the function and its trailing qualifiers. 9955 // This isn't evidence for PAS_Right (or for PAS_Left). 9956 std::string Prefix = "void a() &;\n" 9957 "void b() &;\n"; 9958 verifyFormat(Prefix + "int* x;", DerivePointerAlignment); 9959 verifyFormat(Prefix + "int *x;", DerivePointerAlignment); 9960 // Same if the function is an overloaded operator, and with &&. 9961 Prefix = "void operator()() &&;\n" 9962 "void operator()() &&;\n"; 9963 verifyFormat(Prefix + "int* x;", DerivePointerAlignment); 9964 verifyFormat(Prefix + "int *x;", DerivePointerAlignment); 9965 // However a space between cv-qualifiers and ref-qualifiers *is* evidence. 9966 Prefix = "void a() const &;\n" 9967 "void b() const &;\n"; 9968 EXPECT_EQ(Prefix + "int *x;", 9969 format(Prefix + "int* x;", DerivePointerAlignment)); 9970 } 9971 9972 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9973 verifyFormat("void f() {\n" 9974 " A *a = new A;\n" 9975 " A *a = new (placement) A;\n" 9976 " delete a;\n" 9977 " delete (A *)a;\n" 9978 "}"); 9979 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9980 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9981 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9982 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9983 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9984 verifyFormat("delete[] h->p;"); 9985 verifyFormat("delete[] (void *)p;"); 9986 9987 verifyFormat("void operator delete(void *foo) ATTRIB;"); 9988 verifyFormat("void operator new(void *foo) ATTRIB;"); 9989 verifyFormat("void operator delete[](void *foo) ATTRIB;"); 9990 verifyFormat("void operator delete(void *ptr) noexcept;"); 9991 9992 EXPECT_EQ("void new(link p);\n" 9993 "void delete(link p);\n", 9994 format("void new (link p);\n" 9995 "void delete (link p);\n")); 9996 } 9997 9998 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9999 verifyFormat("int *f(int *a) {}"); 10000 verifyFormat("int main(int argc, char **argv) {}"); 10001 verifyFormat("Test::Test(int b) : a(b * b) {}"); 10002 verifyIndependentOfContext("f(a, *a);"); 10003 verifyFormat("void g() { f(*a); }"); 10004 verifyIndependentOfContext("int a = b * 10;"); 10005 verifyIndependentOfContext("int a = 10 * b;"); 10006 verifyIndependentOfContext("int a = b * c;"); 10007 verifyIndependentOfContext("int a += b * c;"); 10008 verifyIndependentOfContext("int a -= b * c;"); 10009 verifyIndependentOfContext("int a *= b * c;"); 10010 verifyIndependentOfContext("int a /= b * c;"); 10011 verifyIndependentOfContext("int a = *b;"); 10012 verifyIndependentOfContext("int a = *b * c;"); 10013 verifyIndependentOfContext("int a = b * *c;"); 10014 verifyIndependentOfContext("int a = b * (10);"); 10015 verifyIndependentOfContext("S << b * (10);"); 10016 verifyIndependentOfContext("return 10 * b;"); 10017 verifyIndependentOfContext("return *b * *c;"); 10018 verifyIndependentOfContext("return a & ~b;"); 10019 verifyIndependentOfContext("f(b ? *c : *d);"); 10020 verifyIndependentOfContext("int a = b ? *c : *d;"); 10021 verifyIndependentOfContext("*b = a;"); 10022 verifyIndependentOfContext("a * ~b;"); 10023 verifyIndependentOfContext("a * !b;"); 10024 verifyIndependentOfContext("a * +b;"); 10025 verifyIndependentOfContext("a * -b;"); 10026 verifyIndependentOfContext("a * ++b;"); 10027 verifyIndependentOfContext("a * --b;"); 10028 verifyIndependentOfContext("a[4] * b;"); 10029 verifyIndependentOfContext("a[a * a] = 1;"); 10030 verifyIndependentOfContext("f() * b;"); 10031 verifyIndependentOfContext("a * [self dostuff];"); 10032 verifyIndependentOfContext("int x = a * (a + b);"); 10033 verifyIndependentOfContext("(a *)(a + b);"); 10034 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 10035 verifyIndependentOfContext("int *pa = (int *)&a;"); 10036 verifyIndependentOfContext("return sizeof(int **);"); 10037 verifyIndependentOfContext("return sizeof(int ******);"); 10038 verifyIndependentOfContext("return (int **&)a;"); 10039 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 10040 verifyFormat("void f(Type (*parameter)[10]) {}"); 10041 verifyFormat("void f(Type (¶meter)[10]) {}"); 10042 verifyGoogleFormat("return sizeof(int**);"); 10043 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 10044 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 10045 verifyFormat("auto a = [](int **&, int ***) {};"); 10046 verifyFormat("auto PointerBinding = [](const char *S) {};"); 10047 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 10048 verifyFormat("[](const decltype(*a) &value) {}"); 10049 verifyFormat("[](const typeof(*a) &value) {}"); 10050 verifyFormat("[](const _Atomic(a *) &value) {}"); 10051 verifyFormat("[](const __underlying_type(a) &value) {}"); 10052 verifyFormat("decltype(a * b) F();"); 10053 verifyFormat("typeof(a * b) F();"); 10054 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 10055 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 10056 verifyIndependentOfContext("typedef void (*f)(int *a);"); 10057 verifyIndependentOfContext("int i{a * b};"); 10058 verifyIndependentOfContext("aaa && aaa->f();"); 10059 verifyIndependentOfContext("int x = ~*p;"); 10060 verifyFormat("Constructor() : a(a), area(width * height) {}"); 10061 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 10062 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 10063 verifyFormat("void f() { f(a, c * d); }"); 10064 verifyFormat("void f() { f(new a(), c * d); }"); 10065 verifyFormat("void f(const MyOverride &override);"); 10066 verifyFormat("void f(const MyFinal &final);"); 10067 verifyIndependentOfContext("bool a = f() && override.f();"); 10068 verifyIndependentOfContext("bool a = f() && final.f();"); 10069 10070 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 10071 10072 verifyIndependentOfContext("A<int *> a;"); 10073 verifyIndependentOfContext("A<int **> a;"); 10074 verifyIndependentOfContext("A<int *, int *> a;"); 10075 verifyIndependentOfContext("A<int *[]> a;"); 10076 verifyIndependentOfContext( 10077 "const char *const p = reinterpret_cast<const char *const>(q);"); 10078 verifyIndependentOfContext("A<int **, int **> a;"); 10079 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 10080 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 10081 verifyFormat("for (; a && b;) {\n}"); 10082 verifyFormat("bool foo = true && [] { return false; }();"); 10083 10084 verifyFormat( 10085 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10087 10088 verifyGoogleFormat("int const* a = &b;"); 10089 verifyGoogleFormat("**outparam = 1;"); 10090 verifyGoogleFormat("*outparam = a * b;"); 10091 verifyGoogleFormat("int main(int argc, char** argv) {}"); 10092 verifyGoogleFormat("A<int*> a;"); 10093 verifyGoogleFormat("A<int**> a;"); 10094 verifyGoogleFormat("A<int*, int*> a;"); 10095 verifyGoogleFormat("A<int**, int**> a;"); 10096 verifyGoogleFormat("f(b ? *c : *d);"); 10097 verifyGoogleFormat("int a = b ? *c : *d;"); 10098 verifyGoogleFormat("Type* t = **x;"); 10099 verifyGoogleFormat("Type* t = *++*x;"); 10100 verifyGoogleFormat("*++*x;"); 10101 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 10102 verifyGoogleFormat("Type* t = x++ * y;"); 10103 verifyGoogleFormat( 10104 "const char* const p = reinterpret_cast<const char* const>(q);"); 10105 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 10106 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 10107 verifyGoogleFormat("template <typename T>\n" 10108 "void f(int i = 0, SomeType** temps = NULL);"); 10109 10110 FormatStyle Left = getLLVMStyle(); 10111 Left.PointerAlignment = FormatStyle::PAS_Left; 10112 verifyFormat("x = *a(x) = *a(y);", Left); 10113 verifyFormat("for (;; *a = b) {\n}", Left); 10114 verifyFormat("return *this += 1;", Left); 10115 verifyFormat("throw *x;", Left); 10116 verifyFormat("delete *x;", Left); 10117 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 10118 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 10119 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 10120 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 10121 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 10122 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 10123 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 10124 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 10125 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 10126 10127 verifyIndependentOfContext("a = *(x + y);"); 10128 verifyIndependentOfContext("a = &(x + y);"); 10129 verifyIndependentOfContext("*(x + y).call();"); 10130 verifyIndependentOfContext("&(x + y)->call();"); 10131 verifyFormat("void f() { &(*I).first; }"); 10132 10133 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 10134 verifyFormat("f(* /* confusing comment */ foo);"); 10135 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 10136 verifyFormat("void foo(int * // this is the first paramters\n" 10137 " ,\n" 10138 " int second);"); 10139 verifyFormat("double term = a * // first\n" 10140 " b;"); 10141 verifyFormat( 10142 "int *MyValues = {\n" 10143 " *A, // Operator detection might be confused by the '{'\n" 10144 " *BB // Operator detection might be confused by previous comment\n" 10145 "};"); 10146 10147 verifyIndependentOfContext("if (int *a = &b)"); 10148 verifyIndependentOfContext("if (int &a = *b)"); 10149 verifyIndependentOfContext("if (a & b[i])"); 10150 verifyIndependentOfContext("if constexpr (a & b[i])"); 10151 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 10152 verifyIndependentOfContext("if (a * (b * c))"); 10153 verifyIndependentOfContext("if constexpr (a * (b * c))"); 10154 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 10155 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 10156 verifyIndependentOfContext("if (*b[i])"); 10157 verifyIndependentOfContext("if (int *a = (&b))"); 10158 verifyIndependentOfContext("while (int *a = &b)"); 10159 verifyIndependentOfContext("while (a * (b * c))"); 10160 verifyIndependentOfContext("size = sizeof *a;"); 10161 verifyIndependentOfContext("if (a && (b = c))"); 10162 verifyFormat("void f() {\n" 10163 " for (const int &v : Values) {\n" 10164 " }\n" 10165 "}"); 10166 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 10167 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 10168 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 10169 10170 verifyFormat("#define A (!a * b)"); 10171 verifyFormat("#define MACRO \\\n" 10172 " int *i = a * b; \\\n" 10173 " void f(a *b);", 10174 getLLVMStyleWithColumns(19)); 10175 10176 verifyIndependentOfContext("A = new SomeType *[Length];"); 10177 verifyIndependentOfContext("A = new SomeType *[Length]();"); 10178 verifyIndependentOfContext("T **t = new T *;"); 10179 verifyIndependentOfContext("T **t = new T *();"); 10180 verifyGoogleFormat("A = new SomeType*[Length]();"); 10181 verifyGoogleFormat("A = new SomeType*[Length];"); 10182 verifyGoogleFormat("T** t = new T*;"); 10183 verifyGoogleFormat("T** t = new T*();"); 10184 10185 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 10186 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 10187 verifyFormat("template <bool a, bool b> " 10188 "typename t::if<x && y>::type f() {}"); 10189 verifyFormat("template <int *y> f() {}"); 10190 verifyFormat("vector<int *> v;"); 10191 verifyFormat("vector<int *const> v;"); 10192 verifyFormat("vector<int *const **const *> v;"); 10193 verifyFormat("vector<int *volatile> v;"); 10194 verifyFormat("vector<a *_Nonnull> v;"); 10195 verifyFormat("vector<a *_Nullable> v;"); 10196 verifyFormat("vector<a *_Null_unspecified> v;"); 10197 verifyFormat("vector<a *__ptr32> v;"); 10198 verifyFormat("vector<a *__ptr64> v;"); 10199 verifyFormat("vector<a *__capability> v;"); 10200 FormatStyle TypeMacros = getLLVMStyle(); 10201 TypeMacros.TypenameMacros = {"LIST"}; 10202 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 10203 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 10204 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 10205 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 10206 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 10207 10208 FormatStyle CustomQualifier = getLLVMStyle(); 10209 // Add identifiers that should not be parsed as a qualifier by default. 10210 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 10211 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 10212 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 10213 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 10214 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 10215 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 10216 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 10217 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 10218 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 10219 verifyFormat("vector<a * _NotAQualifier> v;"); 10220 verifyFormat("vector<a * __not_a_qualifier> v;"); 10221 verifyFormat("vector<a * b> v;"); 10222 verifyFormat("foo<b && false>();"); 10223 verifyFormat("foo<b & 1>();"); 10224 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 10225 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 10226 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 10227 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 10228 verifyFormat( 10229 "template <class T, class = typename std::enable_if<\n" 10230 " std::is_integral<T>::value &&\n" 10231 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 10232 "void F();", 10233 getLLVMStyleWithColumns(70)); 10234 verifyFormat("template <class T,\n" 10235 " class = typename std::enable_if<\n" 10236 " std::is_integral<T>::value &&\n" 10237 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 10238 " class U>\n" 10239 "void F();", 10240 getLLVMStyleWithColumns(70)); 10241 verifyFormat( 10242 "template <class T,\n" 10243 " class = typename ::std::enable_if<\n" 10244 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 10245 "void F();", 10246 getGoogleStyleWithColumns(68)); 10247 10248 verifyIndependentOfContext("MACRO(int *i);"); 10249 verifyIndependentOfContext("MACRO(auto *a);"); 10250 verifyIndependentOfContext("MACRO(const A *a);"); 10251 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 10252 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 10253 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 10254 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 10255 verifyIndependentOfContext("MACRO(A *const a);"); 10256 verifyIndependentOfContext("MACRO(A *restrict a);"); 10257 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 10258 verifyIndependentOfContext("MACRO(A *__restrict a);"); 10259 verifyIndependentOfContext("MACRO(A *volatile a);"); 10260 verifyIndependentOfContext("MACRO(A *__volatile a);"); 10261 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 10262 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 10263 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 10264 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 10265 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 10266 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 10267 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 10268 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 10269 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 10270 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 10271 verifyIndependentOfContext("MACRO(A *__capability);"); 10272 verifyIndependentOfContext("MACRO(A &__capability);"); 10273 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 10274 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 10275 // If we add __my_qualifier to AttributeMacros it should always be parsed as 10276 // a type declaration: 10277 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 10278 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 10279 // Also check that TypenameMacros prevents parsing it as multiplication: 10280 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 10281 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 10282 10283 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 10284 verifyFormat("void f() { f(float{1}, a * a); }"); 10285 verifyFormat("void f() { f(float(1), a * a); }"); 10286 10287 verifyFormat("f((void (*)(int))g);"); 10288 verifyFormat("f((void (&)(int))g);"); 10289 verifyFormat("f((void (^)(int))g);"); 10290 10291 // FIXME: Is there a way to make this work? 10292 // verifyIndependentOfContext("MACRO(A *a);"); 10293 verifyFormat("MACRO(A &B);"); 10294 verifyFormat("MACRO(A *B);"); 10295 verifyFormat("void f() { MACRO(A * B); }"); 10296 verifyFormat("void f() { MACRO(A & B); }"); 10297 10298 // This lambda was mis-formatted after D88956 (treating it as a binop): 10299 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 10300 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 10301 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 10302 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 10303 10304 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 10305 verifyFormat("return options != nullptr && operator==(*options);"); 10306 10307 EXPECT_EQ("#define OP(x) \\\n" 10308 " ostream &operator<<(ostream &s, const A &a) { \\\n" 10309 " return s << a.DebugString(); \\\n" 10310 " }", 10311 format("#define OP(x) \\\n" 10312 " ostream &operator<<(ostream &s, const A &a) { \\\n" 10313 " return s << a.DebugString(); \\\n" 10314 " }", 10315 getLLVMStyleWithColumns(50))); 10316 10317 // FIXME: We cannot handle this case yet; we might be able to figure out that 10318 // foo<x> d > v; doesn't make sense. 10319 verifyFormat("foo<a<b && c> d> v;"); 10320 10321 FormatStyle PointerMiddle = getLLVMStyle(); 10322 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 10323 verifyFormat("delete *x;", PointerMiddle); 10324 verifyFormat("int * x;", PointerMiddle); 10325 verifyFormat("int *[] x;", PointerMiddle); 10326 verifyFormat("template <int * y> f() {}", PointerMiddle); 10327 verifyFormat("int * f(int * a) {}", PointerMiddle); 10328 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 10329 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 10330 verifyFormat("A<int *> a;", PointerMiddle); 10331 verifyFormat("A<int **> a;", PointerMiddle); 10332 verifyFormat("A<int *, int *> a;", PointerMiddle); 10333 verifyFormat("A<int *[]> a;", PointerMiddle); 10334 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 10335 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 10336 verifyFormat("T ** t = new T *;", PointerMiddle); 10337 10338 // Member function reference qualifiers aren't binary operators. 10339 verifyFormat("string // break\n" 10340 "operator()() & {}"); 10341 verifyFormat("string // break\n" 10342 "operator()() && {}"); 10343 verifyGoogleFormat("template <typename T>\n" 10344 "auto x() & -> int {}"); 10345 10346 // Should be binary operators when used as an argument expression (overloaded 10347 // operator invoked as a member function). 10348 verifyFormat("void f() { a.operator()(a * a); }"); 10349 verifyFormat("void f() { a->operator()(a & a); }"); 10350 verifyFormat("void f() { a.operator()(*a & *a); }"); 10351 verifyFormat("void f() { a->operator()(*a * *a); }"); 10352 10353 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 10354 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 10355 } 10356 10357 TEST_F(FormatTest, UnderstandsAttributes) { 10358 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 10359 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 10360 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10361 verifyFormat("__attribute__((nodebug)) ::qualified_type f();"); 10362 FormatStyle AfterType = getLLVMStyle(); 10363 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10364 verifyFormat("__attribute__((nodebug)) void\n" 10365 "foo() {}\n", 10366 AfterType); 10367 verifyFormat("__unused void\n" 10368 "foo() {}", 10369 AfterType); 10370 10371 FormatStyle CustomAttrs = getLLVMStyle(); 10372 CustomAttrs.AttributeMacros.push_back("__unused"); 10373 CustomAttrs.AttributeMacros.push_back("__attr1"); 10374 CustomAttrs.AttributeMacros.push_back("__attr2"); 10375 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 10376 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 10377 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 10378 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 10379 // Check that it is parsed as a multiplication without AttributeMacros and 10380 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 10381 verifyFormat("vector<SomeType * __attr1> v;"); 10382 verifyFormat("vector<SomeType __attr1 *> v;"); 10383 verifyFormat("vector<SomeType __attr1 *const> v;"); 10384 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 10385 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 10386 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 10387 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 10388 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 10389 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 10390 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 10391 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 10392 10393 // Check that these are not parsed as function declarations: 10394 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10395 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 10396 verifyFormat("SomeType s(InitValue);", CustomAttrs); 10397 verifyFormat("SomeType s{InitValue};", CustomAttrs); 10398 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 10399 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 10400 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 10401 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 10402 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 10403 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 10404 } 10405 10406 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 10407 // Check that qualifiers on pointers don't break parsing of casts. 10408 verifyFormat("x = (foo *const)*v;"); 10409 verifyFormat("x = (foo *volatile)*v;"); 10410 verifyFormat("x = (foo *restrict)*v;"); 10411 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 10412 verifyFormat("x = (foo *_Nonnull)*v;"); 10413 verifyFormat("x = (foo *_Nullable)*v;"); 10414 verifyFormat("x = (foo *_Null_unspecified)*v;"); 10415 verifyFormat("x = (foo *_Nonnull)*v;"); 10416 verifyFormat("x = (foo *[[clang::attr]])*v;"); 10417 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 10418 verifyFormat("x = (foo *__ptr32)*v;"); 10419 verifyFormat("x = (foo *__ptr64)*v;"); 10420 verifyFormat("x = (foo *__capability)*v;"); 10421 10422 // Check that we handle multiple trailing qualifiers and skip them all to 10423 // determine that the expression is a cast to a pointer type. 10424 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 10425 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 10426 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 10427 StringRef AllQualifiers = 10428 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 10429 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 10430 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 10431 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 10432 10433 // Also check that address-of is not parsed as a binary bitwise-and: 10434 verifyFormat("x = (foo *const)&v;"); 10435 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 10436 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 10437 10438 // Check custom qualifiers: 10439 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 10440 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 10441 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 10442 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 10443 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 10444 CustomQualifier); 10445 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 10446 CustomQualifier); 10447 10448 // Check that unknown identifiers result in binary operator parsing: 10449 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 10450 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 10451 } 10452 10453 TEST_F(FormatTest, UnderstandsSquareAttributes) { 10454 verifyFormat("SomeType s [[unused]] (InitValue);"); 10455 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 10456 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 10457 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 10458 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 10459 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10460 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10461 verifyFormat("[[nodiscard]] bool f() { return false; }"); 10462 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 10463 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 10464 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 10465 verifyFormat("[[nodiscard]] ::qualified_type f();"); 10466 10467 // Make sure we do not mistake attributes for array subscripts. 10468 verifyFormat("int a() {}\n" 10469 "[[unused]] int b() {}\n"); 10470 verifyFormat("NSArray *arr;\n" 10471 "arr[[Foo() bar]];"); 10472 10473 // On the other hand, we still need to correctly find array subscripts. 10474 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 10475 10476 // Make sure that we do not mistake Objective-C method inside array literals 10477 // as attributes, even if those method names are also keywords. 10478 verifyFormat("@[ [foo bar] ];"); 10479 verifyFormat("@[ [NSArray class] ];"); 10480 verifyFormat("@[ [foo enum] ];"); 10481 10482 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 10483 10484 // Make sure we do not parse attributes as lambda introducers. 10485 FormatStyle MultiLineFunctions = getLLVMStyle(); 10486 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10487 verifyFormat("[[unused]] int b() {\n" 10488 " return 42;\n" 10489 "}\n", 10490 MultiLineFunctions); 10491 } 10492 10493 TEST_F(FormatTest, AttributeClass) { 10494 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 10495 verifyFormat("class S {\n" 10496 " S(S&&) = default;\n" 10497 "};", 10498 Style); 10499 verifyFormat("class [[nodiscard]] S {\n" 10500 " S(S&&) = default;\n" 10501 "};", 10502 Style); 10503 verifyFormat("class __attribute((maybeunused)) S {\n" 10504 " S(S&&) = default;\n" 10505 "};", 10506 Style); 10507 verifyFormat("struct S {\n" 10508 " S(S&&) = default;\n" 10509 "};", 10510 Style); 10511 verifyFormat("struct [[nodiscard]] S {\n" 10512 " S(S&&) = default;\n" 10513 "};", 10514 Style); 10515 } 10516 10517 TEST_F(FormatTest, AttributesAfterMacro) { 10518 FormatStyle Style = getLLVMStyle(); 10519 verifyFormat("MACRO;\n" 10520 "__attribute__((maybe_unused)) int foo() {\n" 10521 " //...\n" 10522 "}"); 10523 10524 verifyFormat("MACRO;\n" 10525 "[[nodiscard]] int foo() {\n" 10526 " //...\n" 10527 "}"); 10528 10529 EXPECT_EQ("MACRO\n\n" 10530 "__attribute__((maybe_unused)) int foo() {\n" 10531 " //...\n" 10532 "}", 10533 format("MACRO\n\n" 10534 "__attribute__((maybe_unused)) int foo() {\n" 10535 " //...\n" 10536 "}")); 10537 10538 EXPECT_EQ("MACRO\n\n" 10539 "[[nodiscard]] int foo() {\n" 10540 " //...\n" 10541 "}", 10542 format("MACRO\n\n" 10543 "[[nodiscard]] int foo() {\n" 10544 " //...\n" 10545 "}")); 10546 } 10547 10548 TEST_F(FormatTest, AttributePenaltyBreaking) { 10549 FormatStyle Style = getLLVMStyle(); 10550 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 10551 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10552 Style); 10553 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 10554 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10555 Style); 10556 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 10557 "shared_ptr<ALongTypeName> &C d) {\n}", 10558 Style); 10559 } 10560 10561 TEST_F(FormatTest, UnderstandsEllipsis) { 10562 FormatStyle Style = getLLVMStyle(); 10563 verifyFormat("int printf(const char *fmt, ...);"); 10564 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 10565 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 10566 10567 verifyFormat("template <int *...PP> a;", Style); 10568 10569 Style.PointerAlignment = FormatStyle::PAS_Left; 10570 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 10571 10572 verifyFormat("template <int*... PP> a;", Style); 10573 10574 Style.PointerAlignment = FormatStyle::PAS_Middle; 10575 verifyFormat("template <int *... PP> a;", Style); 10576 } 10577 10578 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 10579 EXPECT_EQ("int *a;\n" 10580 "int *a;\n" 10581 "int *a;", 10582 format("int *a;\n" 10583 "int* a;\n" 10584 "int *a;", 10585 getGoogleStyle())); 10586 EXPECT_EQ("int* a;\n" 10587 "int* a;\n" 10588 "int* a;", 10589 format("int* a;\n" 10590 "int* a;\n" 10591 "int *a;", 10592 getGoogleStyle())); 10593 EXPECT_EQ("int *a;\n" 10594 "int *a;\n" 10595 "int *a;", 10596 format("int *a;\n" 10597 "int * a;\n" 10598 "int * a;", 10599 getGoogleStyle())); 10600 EXPECT_EQ("auto x = [] {\n" 10601 " int *a;\n" 10602 " int *a;\n" 10603 " int *a;\n" 10604 "};", 10605 format("auto x=[]{int *a;\n" 10606 "int * a;\n" 10607 "int * a;};", 10608 getGoogleStyle())); 10609 } 10610 10611 TEST_F(FormatTest, UnderstandsRvalueReferences) { 10612 verifyFormat("int f(int &&a) {}"); 10613 verifyFormat("int f(int a, char &&b) {}"); 10614 verifyFormat("void f() { int &&a = b; }"); 10615 verifyGoogleFormat("int f(int a, char&& b) {}"); 10616 verifyGoogleFormat("void f() { int&& a = b; }"); 10617 10618 verifyIndependentOfContext("A<int &&> a;"); 10619 verifyIndependentOfContext("A<int &&, int &&> a;"); 10620 verifyGoogleFormat("A<int&&> a;"); 10621 verifyGoogleFormat("A<int&&, int&&> a;"); 10622 10623 // Not rvalue references: 10624 verifyFormat("template <bool B, bool C> class A {\n" 10625 " static_assert(B && C, \"Something is wrong\");\n" 10626 "};"); 10627 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 10628 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 10629 verifyFormat("#define A(a, b) (a && b)"); 10630 } 10631 10632 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 10633 verifyFormat("void f() {\n" 10634 " x[aaaaaaaaa -\n" 10635 " b] = 23;\n" 10636 "}", 10637 getLLVMStyleWithColumns(15)); 10638 } 10639 10640 TEST_F(FormatTest, FormatsCasts) { 10641 verifyFormat("Type *A = static_cast<Type *>(P);"); 10642 verifyFormat("static_cast<Type *>(P);"); 10643 verifyFormat("static_cast<Type &>(Fun)(Args);"); 10644 verifyFormat("static_cast<Type &>(*Fun)(Args);"); 10645 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;"); 10646 // Check that static_cast<...>(...) does not require the next token to be on 10647 // the same line. 10648 verifyFormat("some_loooong_output << something_something__ << " 10649 "static_cast<const void *>(R)\n" 10650 " << something;"); 10651 verifyFormat("a = static_cast<Type &>(*Fun)(Args);"); 10652 verifyFormat("const_cast<Type &>(*Fun)(Args);"); 10653 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);"); 10654 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);"); 10655 verifyFormat("Type *A = (Type *)P;"); 10656 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 10657 verifyFormat("int a = (int)(2.0f);"); 10658 verifyFormat("int a = (int)2.0f;"); 10659 verifyFormat("x[(int32)y];"); 10660 verifyFormat("x = (int32)y;"); 10661 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 10662 verifyFormat("int a = (int)*b;"); 10663 verifyFormat("int a = (int)2.0f;"); 10664 verifyFormat("int a = (int)~0;"); 10665 verifyFormat("int a = (int)++a;"); 10666 verifyFormat("int a = (int)sizeof(int);"); 10667 verifyFormat("int a = (int)+2;"); 10668 verifyFormat("my_int a = (my_int)2.0f;"); 10669 verifyFormat("my_int a = (my_int)sizeof(int);"); 10670 verifyFormat("return (my_int)aaa;"); 10671 verifyFormat("#define x ((int)-1)"); 10672 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 10673 verifyFormat("#define p(q) ((int *)&q)"); 10674 verifyFormat("fn(a)(b) + 1;"); 10675 10676 verifyFormat("void f() { my_int a = (my_int)*b; }"); 10677 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 10678 verifyFormat("my_int a = (my_int)~0;"); 10679 verifyFormat("my_int a = (my_int)++a;"); 10680 verifyFormat("my_int a = (my_int)-2;"); 10681 verifyFormat("my_int a = (my_int)1;"); 10682 verifyFormat("my_int a = (my_int *)1;"); 10683 verifyFormat("my_int a = (const my_int)-1;"); 10684 verifyFormat("my_int a = (const my_int *)-1;"); 10685 verifyFormat("my_int a = (my_int)(my_int)-1;"); 10686 verifyFormat("my_int a = (ns::my_int)-2;"); 10687 verifyFormat("case (my_int)ONE:"); 10688 verifyFormat("auto x = (X)this;"); 10689 // Casts in Obj-C style calls used to not be recognized as such. 10690 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 10691 10692 // FIXME: single value wrapped with paren will be treated as cast. 10693 verifyFormat("void f(int i = (kValue)*kMask) {}"); 10694 10695 verifyFormat("{ (void)F; }"); 10696 10697 // Don't break after a cast's 10698 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10699 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 10700 " bbbbbbbbbbbbbbbbbbbbbb);"); 10701 10702 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)"); 10703 verifyFormat("#define CONF_BOOL(x) (bool *)(x)"); 10704 verifyFormat("#define CONF_BOOL(x) (bool)(x)"); 10705 verifyFormat("bool *y = (bool *)(void *)(x);"); 10706 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)"); 10707 verifyFormat("bool *y = (bool *)(void *)(int)(x);"); 10708 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)"); 10709 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);"); 10710 10711 // These are not casts. 10712 verifyFormat("void f(int *) {}"); 10713 verifyFormat("f(foo)->b;"); 10714 verifyFormat("f(foo).b;"); 10715 verifyFormat("f(foo)(b);"); 10716 verifyFormat("f(foo)[b];"); 10717 verifyFormat("[](foo) { return 4; }(bar);"); 10718 verifyFormat("(*funptr)(foo)[4];"); 10719 verifyFormat("funptrs[4](foo)[4];"); 10720 verifyFormat("void f(int *);"); 10721 verifyFormat("void f(int *) = 0;"); 10722 verifyFormat("void f(SmallVector<int>) {}"); 10723 verifyFormat("void f(SmallVector<int>);"); 10724 verifyFormat("void f(SmallVector<int>) = 0;"); 10725 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 10726 verifyFormat("int a = sizeof(int) * b;"); 10727 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 10728 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 10729 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 10730 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 10731 10732 // These are not casts, but at some point were confused with casts. 10733 verifyFormat("virtual void foo(int *) override;"); 10734 verifyFormat("virtual void foo(char &) const;"); 10735 verifyFormat("virtual void foo(int *a, char *) const;"); 10736 verifyFormat("int a = sizeof(int *) + b;"); 10737 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10738 verifyFormat("bool b = f(g<int>) && c;"); 10739 verifyFormat("typedef void (*f)(int i) func;"); 10740 verifyFormat("void operator++(int) noexcept;"); 10741 verifyFormat("void operator++(int &) noexcept;"); 10742 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10743 "&) noexcept;"); 10744 verifyFormat( 10745 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10746 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10747 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10748 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10749 verifyFormat("void operator delete(foo &) noexcept;"); 10750 verifyFormat("void operator delete(foo) noexcept;"); 10751 verifyFormat("void operator delete(int) noexcept;"); 10752 verifyFormat("void operator delete(int &) noexcept;"); 10753 verifyFormat("void operator delete(int &) volatile noexcept;"); 10754 verifyFormat("void operator delete(int &) const"); 10755 verifyFormat("void operator delete(int &) = default"); 10756 verifyFormat("void operator delete(int &) = delete"); 10757 verifyFormat("void operator delete(int &) [[noreturn]]"); 10758 verifyFormat("void operator delete(int &) throw();"); 10759 verifyFormat("void operator delete(int &) throw(int);"); 10760 verifyFormat("auto operator delete(int &) -> int;"); 10761 verifyFormat("auto operator delete(int &) override"); 10762 verifyFormat("auto operator delete(int &) final"); 10763 10764 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10765 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10766 // FIXME: The indentation here is not ideal. 10767 verifyFormat( 10768 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10769 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10770 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10771 } 10772 10773 TEST_F(FormatTest, FormatsFunctionTypes) { 10774 verifyFormat("A<bool()> a;"); 10775 verifyFormat("A<SomeType()> a;"); 10776 verifyFormat("A<void (*)(int, std::string)> a;"); 10777 verifyFormat("A<void *(int)>;"); 10778 verifyFormat("void *(*a)(int *, SomeType *);"); 10779 verifyFormat("int (*func)(void *);"); 10780 verifyFormat("void f() { int (*func)(void *); }"); 10781 verifyFormat("template <class CallbackClass>\n" 10782 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10783 10784 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10785 verifyGoogleFormat("void* (*a)(int);"); 10786 verifyGoogleFormat( 10787 "template <class CallbackClass>\n" 10788 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10789 10790 // Other constructs can look somewhat like function types: 10791 verifyFormat("A<sizeof(*x)> a;"); 10792 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10793 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10794 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10795 verifyFormat("int x = f(&h)();"); 10796 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10797 verifyFormat("std::function<\n" 10798 " LooooooooooongTemplatedType<\n" 10799 " SomeType>*(\n" 10800 " LooooooooooooooooongType type)>\n" 10801 " function;", 10802 getGoogleStyleWithColumns(40)); 10803 } 10804 10805 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10806 verifyFormat("A (*foo_)[6];"); 10807 verifyFormat("vector<int> (*foo_)[6];"); 10808 } 10809 10810 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10811 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10812 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10813 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10814 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10815 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10816 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10817 10818 // Different ways of ()-initializiation. 10819 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10820 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10821 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10822 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10823 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10824 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10825 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10826 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10827 10828 // Lambdas should not confuse the variable declaration heuristic. 10829 verifyFormat("LooooooooooooooooongType\n" 10830 " variable(nullptr, [](A *a) {});", 10831 getLLVMStyleWithColumns(40)); 10832 } 10833 10834 TEST_F(FormatTest, BreaksLongDeclarations) { 10835 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10836 " AnotherNameForTheLongType;"); 10837 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10839 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10840 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10841 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10842 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10843 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10844 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10845 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10846 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10847 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10848 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10849 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10850 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10851 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10852 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10853 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10854 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10855 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10856 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10857 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10858 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10859 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10860 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10861 FormatStyle Indented = getLLVMStyle(); 10862 Indented.IndentWrappedFunctionNames = true; 10863 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10864 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10865 Indented); 10866 verifyFormat( 10867 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10868 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10869 Indented); 10870 verifyFormat( 10871 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10872 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10873 Indented); 10874 verifyFormat( 10875 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10876 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10877 Indented); 10878 10879 // FIXME: Without the comment, this breaks after "(". 10880 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10881 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10882 getGoogleStyle()); 10883 10884 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10885 " int LoooooooooooooooooooongParam2) {}"); 10886 verifyFormat( 10887 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10888 " SourceLocation L, IdentifierIn *II,\n" 10889 " Type *T) {}"); 10890 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10891 "ReallyReaaallyLongFunctionName(\n" 10892 " const std::string &SomeParameter,\n" 10893 " const SomeType<string, SomeOtherTemplateParameter>\n" 10894 " &ReallyReallyLongParameterName,\n" 10895 " const SomeType<string, SomeOtherTemplateParameter>\n" 10896 " &AnotherLongParameterName) {}"); 10897 verifyFormat("template <typename A>\n" 10898 "SomeLoooooooooooooooooooooongType<\n" 10899 " typename some_namespace::SomeOtherType<A>::Type>\n" 10900 "Function() {}"); 10901 10902 verifyGoogleFormat( 10903 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10904 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10905 verifyGoogleFormat( 10906 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10907 " SourceLocation L) {}"); 10908 verifyGoogleFormat( 10909 "some_namespace::LongReturnType\n" 10910 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10911 " int first_long_parameter, int second_parameter) {}"); 10912 10913 verifyGoogleFormat("template <typename T>\n" 10914 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10915 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10916 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10917 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10918 10919 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10920 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10921 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10922 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10923 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10924 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10925 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10926 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10927 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10929 10930 verifyFormat("template <typename T> // Templates on own line.\n" 10931 "static int // Some comment.\n" 10932 "MyFunction(int a);", 10933 getLLVMStyle()); 10934 } 10935 10936 TEST_F(FormatTest, FormatsAccessModifiers) { 10937 FormatStyle Style = getLLVMStyle(); 10938 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10939 FormatStyle::ELBAMS_LogicalBlock); 10940 verifyFormat("struct foo {\n" 10941 "private:\n" 10942 " void f() {}\n" 10943 "\n" 10944 "private:\n" 10945 " int i;\n" 10946 "\n" 10947 "protected:\n" 10948 " int j;\n" 10949 "};\n", 10950 Style); 10951 verifyFormat("struct foo {\n" 10952 "private:\n" 10953 " void f() {}\n" 10954 "\n" 10955 "private:\n" 10956 " int i;\n" 10957 "\n" 10958 "protected:\n" 10959 " int j;\n" 10960 "};\n", 10961 "struct foo {\n" 10962 "private:\n" 10963 " void f() {}\n" 10964 "private:\n" 10965 " int i;\n" 10966 "protected:\n" 10967 " int j;\n" 10968 "};\n", 10969 Style); 10970 verifyFormat("struct foo { /* comment */\n" 10971 "private:\n" 10972 " int i;\n" 10973 " // comment\n" 10974 "private:\n" 10975 " int j;\n" 10976 "};\n", 10977 Style); 10978 verifyFormat("struct foo {\n" 10979 "#ifdef FOO\n" 10980 "#endif\n" 10981 "private:\n" 10982 " int i;\n" 10983 "#ifdef FOO\n" 10984 "private:\n" 10985 "#endif\n" 10986 " int j;\n" 10987 "};\n", 10988 Style); 10989 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10990 verifyFormat("struct foo {\n" 10991 "private:\n" 10992 " void f() {}\n" 10993 "private:\n" 10994 " int i;\n" 10995 "protected:\n" 10996 " int j;\n" 10997 "};\n", 10998 Style); 10999 verifyFormat("struct foo {\n" 11000 "private:\n" 11001 " void f() {}\n" 11002 "private:\n" 11003 " int i;\n" 11004 "protected:\n" 11005 " int j;\n" 11006 "};\n", 11007 "struct foo {\n" 11008 "\n" 11009 "private:\n" 11010 " void f() {}\n" 11011 "\n" 11012 "private:\n" 11013 " int i;\n" 11014 "\n" 11015 "protected:\n" 11016 " int j;\n" 11017 "};\n", 11018 Style); 11019 verifyFormat("struct foo { /* comment */\n" 11020 "private:\n" 11021 " int i;\n" 11022 " // comment\n" 11023 "private:\n" 11024 " int j;\n" 11025 "};\n", 11026 "struct foo { /* comment */\n" 11027 "\n" 11028 "private:\n" 11029 " int i;\n" 11030 " // comment\n" 11031 "\n" 11032 "private:\n" 11033 " int j;\n" 11034 "};\n", 11035 Style); 11036 verifyFormat("struct foo {\n" 11037 "#ifdef FOO\n" 11038 "#endif\n" 11039 "private:\n" 11040 " int i;\n" 11041 "#ifdef FOO\n" 11042 "private:\n" 11043 "#endif\n" 11044 " int j;\n" 11045 "};\n", 11046 "struct foo {\n" 11047 "#ifdef FOO\n" 11048 "#endif\n" 11049 "\n" 11050 "private:\n" 11051 " int i;\n" 11052 "#ifdef FOO\n" 11053 "\n" 11054 "private:\n" 11055 "#endif\n" 11056 " int j;\n" 11057 "};\n", 11058 Style); 11059 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11060 verifyFormat("struct foo {\n" 11061 "private:\n" 11062 " void f() {}\n" 11063 "\n" 11064 "private:\n" 11065 " int i;\n" 11066 "\n" 11067 "protected:\n" 11068 " int j;\n" 11069 "};\n", 11070 Style); 11071 verifyFormat("struct foo {\n" 11072 "private:\n" 11073 " void f() {}\n" 11074 "\n" 11075 "private:\n" 11076 " int i;\n" 11077 "\n" 11078 "protected:\n" 11079 " int j;\n" 11080 "};\n", 11081 "struct foo {\n" 11082 "private:\n" 11083 " void f() {}\n" 11084 "private:\n" 11085 " int i;\n" 11086 "protected:\n" 11087 " int j;\n" 11088 "};\n", 11089 Style); 11090 verifyFormat("struct foo { /* comment */\n" 11091 "private:\n" 11092 " int i;\n" 11093 " // comment\n" 11094 "\n" 11095 "private:\n" 11096 " int j;\n" 11097 "};\n", 11098 "struct foo { /* comment */\n" 11099 "private:\n" 11100 " int i;\n" 11101 " // comment\n" 11102 "\n" 11103 "private:\n" 11104 " int j;\n" 11105 "};\n", 11106 Style); 11107 verifyFormat("struct foo {\n" 11108 "#ifdef FOO\n" 11109 "#endif\n" 11110 "\n" 11111 "private:\n" 11112 " int i;\n" 11113 "#ifdef FOO\n" 11114 "\n" 11115 "private:\n" 11116 "#endif\n" 11117 " int j;\n" 11118 "};\n", 11119 "struct foo {\n" 11120 "#ifdef FOO\n" 11121 "#endif\n" 11122 "private:\n" 11123 " int i;\n" 11124 "#ifdef FOO\n" 11125 "private:\n" 11126 "#endif\n" 11127 " int j;\n" 11128 "};\n", 11129 Style); 11130 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11131 EXPECT_EQ("struct foo {\n" 11132 "\n" 11133 "private:\n" 11134 " void f() {}\n" 11135 "\n" 11136 "private:\n" 11137 " int i;\n" 11138 "\n" 11139 "protected:\n" 11140 " int j;\n" 11141 "};\n", 11142 format("struct foo {\n" 11143 "\n" 11144 "private:\n" 11145 " void f() {}\n" 11146 "\n" 11147 "private:\n" 11148 " int i;\n" 11149 "\n" 11150 "protected:\n" 11151 " int j;\n" 11152 "};\n", 11153 Style)); 11154 verifyFormat("struct foo {\n" 11155 "private:\n" 11156 " void f() {}\n" 11157 "private:\n" 11158 " int i;\n" 11159 "protected:\n" 11160 " int j;\n" 11161 "};\n", 11162 Style); 11163 EXPECT_EQ("struct foo { /* comment */\n" 11164 "\n" 11165 "private:\n" 11166 " int i;\n" 11167 " // comment\n" 11168 "\n" 11169 "private:\n" 11170 " int j;\n" 11171 "};\n", 11172 format("struct foo { /* comment */\n" 11173 "\n" 11174 "private:\n" 11175 " int i;\n" 11176 " // comment\n" 11177 "\n" 11178 "private:\n" 11179 " int j;\n" 11180 "};\n", 11181 Style)); 11182 verifyFormat("struct foo { /* comment */\n" 11183 "private:\n" 11184 " int i;\n" 11185 " // comment\n" 11186 "private:\n" 11187 " int j;\n" 11188 "};\n", 11189 Style); 11190 EXPECT_EQ("struct foo {\n" 11191 "#ifdef FOO\n" 11192 "#endif\n" 11193 "\n" 11194 "private:\n" 11195 " int i;\n" 11196 "#ifdef FOO\n" 11197 "\n" 11198 "private:\n" 11199 "#endif\n" 11200 " int j;\n" 11201 "};\n", 11202 format("struct foo {\n" 11203 "#ifdef FOO\n" 11204 "#endif\n" 11205 "\n" 11206 "private:\n" 11207 " int i;\n" 11208 "#ifdef FOO\n" 11209 "\n" 11210 "private:\n" 11211 "#endif\n" 11212 " int j;\n" 11213 "};\n", 11214 Style)); 11215 verifyFormat("struct foo {\n" 11216 "#ifdef FOO\n" 11217 "#endif\n" 11218 "private:\n" 11219 " int i;\n" 11220 "#ifdef FOO\n" 11221 "private:\n" 11222 "#endif\n" 11223 " int j;\n" 11224 "};\n", 11225 Style); 11226 11227 FormatStyle NoEmptyLines = getLLVMStyle(); 11228 NoEmptyLines.MaxEmptyLinesToKeep = 0; 11229 verifyFormat("struct foo {\n" 11230 "private:\n" 11231 " void f() {}\n" 11232 "\n" 11233 "private:\n" 11234 " int i;\n" 11235 "\n" 11236 "public:\n" 11237 "protected:\n" 11238 " int j;\n" 11239 "};\n", 11240 NoEmptyLines); 11241 11242 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11243 verifyFormat("struct foo {\n" 11244 "private:\n" 11245 " void f() {}\n" 11246 "private:\n" 11247 " int i;\n" 11248 "public:\n" 11249 "protected:\n" 11250 " int j;\n" 11251 "};\n", 11252 NoEmptyLines); 11253 11254 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11255 verifyFormat("struct foo {\n" 11256 "private:\n" 11257 " void f() {}\n" 11258 "\n" 11259 "private:\n" 11260 " int i;\n" 11261 "\n" 11262 "public:\n" 11263 "\n" 11264 "protected:\n" 11265 " int j;\n" 11266 "};\n", 11267 NoEmptyLines); 11268 } 11269 11270 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 11271 11272 FormatStyle Style = getLLVMStyle(); 11273 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 11274 verifyFormat("struct foo {\n" 11275 "private:\n" 11276 " void f() {}\n" 11277 "\n" 11278 "private:\n" 11279 " int i;\n" 11280 "\n" 11281 "protected:\n" 11282 " int j;\n" 11283 "};\n", 11284 Style); 11285 11286 // Check if lines are removed. 11287 verifyFormat("struct foo {\n" 11288 "private:\n" 11289 " void f() {}\n" 11290 "\n" 11291 "private:\n" 11292 " int i;\n" 11293 "\n" 11294 "protected:\n" 11295 " int j;\n" 11296 "};\n", 11297 "struct foo {\n" 11298 "private:\n" 11299 "\n" 11300 " void f() {}\n" 11301 "\n" 11302 "private:\n" 11303 "\n" 11304 " int i;\n" 11305 "\n" 11306 "protected:\n" 11307 "\n" 11308 " int j;\n" 11309 "};\n", 11310 Style); 11311 11312 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11313 verifyFormat("struct foo {\n" 11314 "private:\n" 11315 "\n" 11316 " void f() {}\n" 11317 "\n" 11318 "private:\n" 11319 "\n" 11320 " int i;\n" 11321 "\n" 11322 "protected:\n" 11323 "\n" 11324 " int j;\n" 11325 "};\n", 11326 Style); 11327 11328 // Check if lines are added. 11329 verifyFormat("struct foo {\n" 11330 "private:\n" 11331 "\n" 11332 " void f() {}\n" 11333 "\n" 11334 "private:\n" 11335 "\n" 11336 " int i;\n" 11337 "\n" 11338 "protected:\n" 11339 "\n" 11340 " int j;\n" 11341 "};\n", 11342 "struct foo {\n" 11343 "private:\n" 11344 " void f() {}\n" 11345 "\n" 11346 "private:\n" 11347 " int i;\n" 11348 "\n" 11349 "protected:\n" 11350 " int j;\n" 11351 "};\n", 11352 Style); 11353 11354 // Leave tests rely on the code layout, test::messUp can not be used. 11355 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11356 Style.MaxEmptyLinesToKeep = 0u; 11357 verifyFormat("struct foo {\n" 11358 "private:\n" 11359 " void f() {}\n" 11360 "\n" 11361 "private:\n" 11362 " int i;\n" 11363 "\n" 11364 "protected:\n" 11365 " int j;\n" 11366 "};\n", 11367 Style); 11368 11369 // Check if MaxEmptyLinesToKeep is respected. 11370 EXPECT_EQ("struct foo {\n" 11371 "private:\n" 11372 " void f() {}\n" 11373 "\n" 11374 "private:\n" 11375 " int i;\n" 11376 "\n" 11377 "protected:\n" 11378 " int j;\n" 11379 "};\n", 11380 format("struct foo {\n" 11381 "private:\n" 11382 "\n\n\n" 11383 " void f() {}\n" 11384 "\n" 11385 "private:\n" 11386 "\n\n\n" 11387 " int i;\n" 11388 "\n" 11389 "protected:\n" 11390 "\n\n\n" 11391 " int j;\n" 11392 "};\n", 11393 Style)); 11394 11395 Style.MaxEmptyLinesToKeep = 1u; 11396 EXPECT_EQ("struct foo {\n" 11397 "private:\n" 11398 "\n" 11399 " void f() {}\n" 11400 "\n" 11401 "private:\n" 11402 "\n" 11403 " int i;\n" 11404 "\n" 11405 "protected:\n" 11406 "\n" 11407 " int j;\n" 11408 "};\n", 11409 format("struct foo {\n" 11410 "private:\n" 11411 "\n" 11412 " void f() {}\n" 11413 "\n" 11414 "private:\n" 11415 "\n" 11416 " int i;\n" 11417 "\n" 11418 "protected:\n" 11419 "\n" 11420 " int j;\n" 11421 "};\n", 11422 Style)); 11423 // Check if no lines are kept. 11424 EXPECT_EQ("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 format("struct foo {\n" 11435 "private:\n" 11436 " void f() {}\n" 11437 "\n" 11438 "private:\n" 11439 " int i;\n" 11440 "\n" 11441 "protected:\n" 11442 " int j;\n" 11443 "};\n", 11444 Style)); 11445 // Check if MaxEmptyLinesToKeep is respected. 11446 EXPECT_EQ("struct foo {\n" 11447 "private:\n" 11448 "\n" 11449 " void f() {}\n" 11450 "\n" 11451 "private:\n" 11452 "\n" 11453 " int i;\n" 11454 "\n" 11455 "protected:\n" 11456 "\n" 11457 " int j;\n" 11458 "};\n", 11459 format("struct foo {\n" 11460 "private:\n" 11461 "\n\n\n" 11462 " void f() {}\n" 11463 "\n" 11464 "private:\n" 11465 "\n\n\n" 11466 " int i;\n" 11467 "\n" 11468 "protected:\n" 11469 "\n\n\n" 11470 " int j;\n" 11471 "};\n", 11472 Style)); 11473 11474 Style.MaxEmptyLinesToKeep = 10u; 11475 EXPECT_EQ("struct foo {\n" 11476 "private:\n" 11477 "\n\n\n" 11478 " void f() {}\n" 11479 "\n" 11480 "private:\n" 11481 "\n\n\n" 11482 " int i;\n" 11483 "\n" 11484 "protected:\n" 11485 "\n\n\n" 11486 " int j;\n" 11487 "};\n", 11488 format("struct foo {\n" 11489 "private:\n" 11490 "\n\n\n" 11491 " void f() {}\n" 11492 "\n" 11493 "private:\n" 11494 "\n\n\n" 11495 " int i;\n" 11496 "\n" 11497 "protected:\n" 11498 "\n\n\n" 11499 " int j;\n" 11500 "};\n", 11501 Style)); 11502 11503 // Test with comments. 11504 Style = getLLVMStyle(); 11505 verifyFormat("struct foo {\n" 11506 "private:\n" 11507 " // comment\n" 11508 " void f() {}\n" 11509 "\n" 11510 "private: /* comment */\n" 11511 " int i;\n" 11512 "};\n", 11513 Style); 11514 verifyFormat("struct foo {\n" 11515 "private:\n" 11516 " // comment\n" 11517 " void f() {}\n" 11518 "\n" 11519 "private: /* comment */\n" 11520 " int i;\n" 11521 "};\n", 11522 "struct foo {\n" 11523 "private:\n" 11524 "\n" 11525 " // comment\n" 11526 " void f() {}\n" 11527 "\n" 11528 "private: /* comment */\n" 11529 "\n" 11530 " int i;\n" 11531 "};\n", 11532 Style); 11533 11534 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11535 verifyFormat("struct foo {\n" 11536 "private:\n" 11537 "\n" 11538 " // comment\n" 11539 " void f() {}\n" 11540 "\n" 11541 "private: /* comment */\n" 11542 "\n" 11543 " int i;\n" 11544 "};\n", 11545 "struct foo {\n" 11546 "private:\n" 11547 " // comment\n" 11548 " void f() {}\n" 11549 "\n" 11550 "private: /* comment */\n" 11551 " int i;\n" 11552 "};\n", 11553 Style); 11554 verifyFormat("struct foo {\n" 11555 "private:\n" 11556 "\n" 11557 " // comment\n" 11558 " void f() {}\n" 11559 "\n" 11560 "private: /* comment */\n" 11561 "\n" 11562 " int i;\n" 11563 "};\n", 11564 Style); 11565 11566 // Test with preprocessor defines. 11567 Style = getLLVMStyle(); 11568 verifyFormat("struct foo {\n" 11569 "private:\n" 11570 "#ifdef FOO\n" 11571 "#endif\n" 11572 " void f() {}\n" 11573 "};\n", 11574 Style); 11575 verifyFormat("struct foo {\n" 11576 "private:\n" 11577 "#ifdef FOO\n" 11578 "#endif\n" 11579 " void f() {}\n" 11580 "};\n", 11581 "struct foo {\n" 11582 "private:\n" 11583 "\n" 11584 "#ifdef FOO\n" 11585 "#endif\n" 11586 " void f() {}\n" 11587 "};\n", 11588 Style); 11589 11590 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11591 verifyFormat("struct foo {\n" 11592 "private:\n" 11593 "\n" 11594 "#ifdef FOO\n" 11595 "#endif\n" 11596 " void f() {}\n" 11597 "};\n", 11598 "struct foo {\n" 11599 "private:\n" 11600 "#ifdef FOO\n" 11601 "#endif\n" 11602 " void f() {}\n" 11603 "};\n", 11604 Style); 11605 verifyFormat("struct foo {\n" 11606 "private:\n" 11607 "\n" 11608 "#ifdef FOO\n" 11609 "#endif\n" 11610 " void f() {}\n" 11611 "};\n", 11612 Style); 11613 } 11614 11615 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 11616 // Combined tests of EmptyLineAfterAccessModifier and 11617 // EmptyLineBeforeAccessModifier. 11618 FormatStyle Style = getLLVMStyle(); 11619 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11620 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11621 verifyFormat("struct foo {\n" 11622 "private:\n" 11623 "\n" 11624 "protected:\n" 11625 "};\n", 11626 Style); 11627 11628 Style.MaxEmptyLinesToKeep = 10u; 11629 // Both remove all new lines. 11630 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11631 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11632 verifyFormat("struct foo {\n" 11633 "private:\n" 11634 "protected:\n" 11635 "};\n", 11636 "struct foo {\n" 11637 "private:\n" 11638 "\n\n\n" 11639 "protected:\n" 11640 "};\n", 11641 Style); 11642 11643 // Leave tests rely on the code layout, test::messUp can not be used. 11644 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11645 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11646 Style.MaxEmptyLinesToKeep = 10u; 11647 EXPECT_EQ("struct foo {\n" 11648 "private:\n" 11649 "\n\n\n" 11650 "protected:\n" 11651 "};\n", 11652 format("struct foo {\n" 11653 "private:\n" 11654 "\n\n\n" 11655 "protected:\n" 11656 "};\n", 11657 Style)); 11658 Style.MaxEmptyLinesToKeep = 3u; 11659 EXPECT_EQ("struct foo {\n" 11660 "private:\n" 11661 "\n\n\n" 11662 "protected:\n" 11663 "};\n", 11664 format("struct foo {\n" 11665 "private:\n" 11666 "\n\n\n" 11667 "protected:\n" 11668 "};\n", 11669 Style)); 11670 Style.MaxEmptyLinesToKeep = 1u; 11671 EXPECT_EQ("struct foo {\n" 11672 "private:\n" 11673 "\n\n\n" 11674 "protected:\n" 11675 "};\n", 11676 format("struct foo {\n" 11677 "private:\n" 11678 "\n\n\n" 11679 "protected:\n" 11680 "};\n", 11681 Style)); // Based on new lines in original document and not 11682 // on the setting. 11683 11684 Style.MaxEmptyLinesToKeep = 10u; 11685 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11686 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11687 // Newlines are kept if they are greater than zero, 11688 // test::messUp removes all new lines which changes the logic 11689 EXPECT_EQ("struct foo {\n" 11690 "private:\n" 11691 "\n\n\n" 11692 "protected:\n" 11693 "};\n", 11694 format("struct foo {\n" 11695 "private:\n" 11696 "\n\n\n" 11697 "protected:\n" 11698 "};\n", 11699 Style)); 11700 11701 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11702 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11703 // test::messUp removes all new lines which changes the logic 11704 EXPECT_EQ("struct foo {\n" 11705 "private:\n" 11706 "\n\n\n" 11707 "protected:\n" 11708 "};\n", 11709 format("struct foo {\n" 11710 "private:\n" 11711 "\n\n\n" 11712 "protected:\n" 11713 "};\n", 11714 Style)); 11715 11716 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11717 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11718 EXPECT_EQ("struct foo {\n" 11719 "private:\n" 11720 "\n\n\n" 11721 "protected:\n" 11722 "};\n", 11723 format("struct foo {\n" 11724 "private:\n" 11725 "\n\n\n" 11726 "protected:\n" 11727 "};\n", 11728 Style)); // test::messUp removes all new lines which changes 11729 // the logic. 11730 11731 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11732 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11733 verifyFormat("struct foo {\n" 11734 "private:\n" 11735 "protected:\n" 11736 "};\n", 11737 "struct foo {\n" 11738 "private:\n" 11739 "\n\n\n" 11740 "protected:\n" 11741 "};\n", 11742 Style); 11743 11744 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11745 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11746 EXPECT_EQ("struct foo {\n" 11747 "private:\n" 11748 "\n\n\n" 11749 "protected:\n" 11750 "};\n", 11751 format("struct foo {\n" 11752 "private:\n" 11753 "\n\n\n" 11754 "protected:\n" 11755 "};\n", 11756 Style)); // test::messUp removes all new lines which changes 11757 // the logic. 11758 11759 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11760 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11761 verifyFormat("struct foo {\n" 11762 "private:\n" 11763 "protected:\n" 11764 "};\n", 11765 "struct foo {\n" 11766 "private:\n" 11767 "\n\n\n" 11768 "protected:\n" 11769 "};\n", 11770 Style); 11771 11772 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11773 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11774 verifyFormat("struct foo {\n" 11775 "private:\n" 11776 "protected:\n" 11777 "};\n", 11778 "struct foo {\n" 11779 "private:\n" 11780 "\n\n\n" 11781 "protected:\n" 11782 "};\n", 11783 Style); 11784 11785 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11786 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11787 verifyFormat("struct foo {\n" 11788 "private:\n" 11789 "protected:\n" 11790 "};\n", 11791 "struct foo {\n" 11792 "private:\n" 11793 "\n\n\n" 11794 "protected:\n" 11795 "};\n", 11796 Style); 11797 11798 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11799 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11800 verifyFormat("struct foo {\n" 11801 "private:\n" 11802 "protected:\n" 11803 "};\n", 11804 "struct foo {\n" 11805 "private:\n" 11806 "\n\n\n" 11807 "protected:\n" 11808 "};\n", 11809 Style); 11810 } 11811 11812 TEST_F(FormatTest, FormatsArrays) { 11813 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11814 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11815 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11816 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11817 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11818 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11819 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11820 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11821 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11822 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11823 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11824 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11825 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11826 verifyFormat( 11827 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11828 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11829 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11830 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11831 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11832 11833 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11835 verifyFormat( 11836 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11837 " .aaaaaaa[0]\n" 11838 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11839 verifyFormat("a[::b::c];"); 11840 11841 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11842 11843 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11844 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11845 } 11846 11847 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11848 verifyFormat("(a)->b();"); 11849 verifyFormat("--a;"); 11850 } 11851 11852 TEST_F(FormatTest, HandlesIncludeDirectives) { 11853 verifyFormat("#include <string>\n" 11854 "#include <a/b/c.h>\n" 11855 "#include \"a/b/string\"\n" 11856 "#include \"string.h\"\n" 11857 "#include \"string.h\"\n" 11858 "#include <a-a>\n" 11859 "#include < path with space >\n" 11860 "#include_next <test.h>" 11861 "#include \"abc.h\" // this is included for ABC\n" 11862 "#include \"some long include\" // with a comment\n" 11863 "#include \"some very long include path\"\n" 11864 "#include <some/very/long/include/path>\n", 11865 getLLVMStyleWithColumns(35)); 11866 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11867 EXPECT_EQ("#include <a>", format("#include<a>")); 11868 11869 verifyFormat("#import <string>"); 11870 verifyFormat("#import <a/b/c.h>"); 11871 verifyFormat("#import \"a/b/string\""); 11872 verifyFormat("#import \"string.h\""); 11873 verifyFormat("#import \"string.h\""); 11874 verifyFormat("#if __has_include(<strstream>)\n" 11875 "#include <strstream>\n" 11876 "#endif"); 11877 11878 verifyFormat("#define MY_IMPORT <a/b>"); 11879 11880 verifyFormat("#if __has_include(<a/b>)"); 11881 verifyFormat("#if __has_include_next(<a/b>)"); 11882 verifyFormat("#define F __has_include(<a/b>)"); 11883 verifyFormat("#define F __has_include_next(<a/b>)"); 11884 11885 // Protocol buffer definition or missing "#". 11886 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11887 getLLVMStyleWithColumns(30)); 11888 11889 FormatStyle Style = getLLVMStyle(); 11890 Style.AlwaysBreakBeforeMultilineStrings = true; 11891 Style.ColumnLimit = 0; 11892 verifyFormat("#import \"abc.h\"", Style); 11893 11894 // But 'import' might also be a regular C++ namespace. 11895 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11897 } 11898 11899 //===----------------------------------------------------------------------===// 11900 // Error recovery tests. 11901 //===----------------------------------------------------------------------===// 11902 11903 TEST_F(FormatTest, IncompleteParameterLists) { 11904 FormatStyle NoBinPacking = getLLVMStyle(); 11905 NoBinPacking.BinPackParameters = false; 11906 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11907 " double *min_x,\n" 11908 " double *max_x,\n" 11909 " double *min_y,\n" 11910 " double *max_y,\n" 11911 " double *min_z,\n" 11912 " double *max_z, ) {}", 11913 NoBinPacking); 11914 } 11915 11916 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11917 verifyFormat("void f() { return; }\n42"); 11918 verifyFormat("void f() {\n" 11919 " if (0)\n" 11920 " return;\n" 11921 "}\n" 11922 "42"); 11923 verifyFormat("void f() { return }\n42"); 11924 verifyFormat("void f() {\n" 11925 " if (0)\n" 11926 " return\n" 11927 "}\n" 11928 "42"); 11929 } 11930 11931 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11932 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11933 EXPECT_EQ("void f() {\n" 11934 " if (a)\n" 11935 " return\n" 11936 "}", 11937 format("void f ( ) { if ( a ) return }")); 11938 EXPECT_EQ("namespace N {\n" 11939 "void f()\n" 11940 "}", 11941 format("namespace N { void f() }")); 11942 EXPECT_EQ("namespace N {\n" 11943 "void f() {}\n" 11944 "void g()\n" 11945 "} // namespace N", 11946 format("namespace N { void f( ) { } void g( ) }")); 11947 } 11948 11949 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11950 verifyFormat("int aaaaaaaa =\n" 11951 " // Overlylongcomment\n" 11952 " b;", 11953 getLLVMStyleWithColumns(20)); 11954 verifyFormat("function(\n" 11955 " ShortArgument,\n" 11956 " LoooooooooooongArgument);\n", 11957 getLLVMStyleWithColumns(20)); 11958 } 11959 11960 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11961 verifyFormat("public:"); 11962 verifyFormat("class A {\n" 11963 "public\n" 11964 " void f() {}\n" 11965 "};"); 11966 verifyFormat("public\n" 11967 "int qwerty;"); 11968 verifyFormat("public\n" 11969 "B {}"); 11970 verifyFormat("public\n" 11971 "{}"); 11972 verifyFormat("public\n" 11973 "B { int x; }"); 11974 } 11975 11976 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11977 verifyFormat("{"); 11978 verifyFormat("#})"); 11979 verifyNoCrash("(/**/[:!] ?[)."); 11980 } 11981 11982 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11983 // Found by oss-fuzz: 11984 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11985 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11986 Style.ColumnLimit = 60; 11987 verifyNoCrash( 11988 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11989 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11990 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11991 Style); 11992 } 11993 11994 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11995 verifyFormat("do {\n}"); 11996 verifyFormat("do {\n}\n" 11997 "f();"); 11998 verifyFormat("do {\n}\n" 11999 "wheeee(fun);"); 12000 verifyFormat("do {\n" 12001 " f();\n" 12002 "}"); 12003 } 12004 12005 TEST_F(FormatTest, IncorrectCodeMissingParens) { 12006 verifyFormat("if {\n foo;\n foo();\n}"); 12007 verifyFormat("switch {\n foo;\n foo();\n}"); 12008 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 12009 verifyFormat("while {\n foo;\n foo();\n}"); 12010 verifyFormat("do {\n foo;\n foo();\n} while;"); 12011 } 12012 12013 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 12014 verifyIncompleteFormat("namespace {\n" 12015 "class Foo { Foo (\n" 12016 "};\n" 12017 "} // namespace"); 12018 } 12019 12020 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 12021 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 12022 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 12023 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 12024 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 12025 12026 EXPECT_EQ("{\n" 12027 " {\n" 12028 " breakme(\n" 12029 " qwe);\n" 12030 " }\n", 12031 format("{\n" 12032 " {\n" 12033 " breakme(qwe);\n" 12034 "}\n", 12035 getLLVMStyleWithColumns(10))); 12036 } 12037 12038 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 12039 verifyFormat("int x = {\n" 12040 " avariable,\n" 12041 " b(alongervariable)};", 12042 getLLVMStyleWithColumns(25)); 12043 } 12044 12045 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 12046 verifyFormat("return (a)(b){1, 2, 3};"); 12047 } 12048 12049 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 12050 verifyFormat("vector<int> x{1, 2, 3, 4};"); 12051 verifyFormat("vector<int> x{\n" 12052 " 1,\n" 12053 " 2,\n" 12054 " 3,\n" 12055 " 4,\n" 12056 "};"); 12057 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 12058 verifyFormat("f({1, 2});"); 12059 verifyFormat("auto v = Foo{-1};"); 12060 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 12061 verifyFormat("Class::Class : member{1, 2, 3} {}"); 12062 verifyFormat("new vector<int>{1, 2, 3};"); 12063 verifyFormat("new int[3]{1, 2, 3};"); 12064 verifyFormat("new int{1};"); 12065 verifyFormat("return {arg1, arg2};"); 12066 verifyFormat("return {arg1, SomeType{parameter}};"); 12067 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 12068 verifyFormat("new T{arg1, arg2};"); 12069 verifyFormat("f(MyMap[{composite, key}]);"); 12070 verifyFormat("class Class {\n" 12071 " T member = {arg1, arg2};\n" 12072 "};"); 12073 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 12074 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 12075 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 12076 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 12077 verifyFormat("int a = std::is_integral<int>{} + 0;"); 12078 12079 verifyFormat("int foo(int i) { return fo1{}(i); }"); 12080 verifyFormat("int foo(int i) { return fo1{}(i); }"); 12081 verifyFormat("auto i = decltype(x){};"); 12082 verifyFormat("auto i = typeof(x){};"); 12083 verifyFormat("auto i = _Atomic(x){};"); 12084 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 12085 verifyFormat("Node n{1, Node{1000}, //\n" 12086 " 2};"); 12087 verifyFormat("Aaaa aaaaaaa{\n" 12088 " {\n" 12089 " aaaa,\n" 12090 " },\n" 12091 "};"); 12092 verifyFormat("class C : public D {\n" 12093 " SomeClass SC{2};\n" 12094 "};"); 12095 verifyFormat("class C : public A {\n" 12096 " class D : public B {\n" 12097 " void f() { int i{2}; }\n" 12098 " };\n" 12099 "};"); 12100 verifyFormat("#define A {a, a},"); 12101 // Don't confuse braced list initializers with compound statements. 12102 verifyFormat( 12103 "class A {\n" 12104 " A() : a{} {}\n" 12105 " A(int b) : b(b) {}\n" 12106 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" 12107 " int a, b;\n" 12108 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" 12109 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " 12110 "{}\n" 12111 "};"); 12112 12113 // Avoid breaking between equal sign and opening brace 12114 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 12115 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 12116 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 12117 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 12118 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 12119 " {\"ccccccccccccccccccccc\", 2}};", 12120 AvoidBreakingFirstArgument); 12121 12122 // Binpacking only if there is no trailing comma 12123 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 12124 " cccccccccc, dddddddddd};", 12125 getLLVMStyleWithColumns(50)); 12126 verifyFormat("const Aaaaaa aaaaa = {\n" 12127 " aaaaaaaaaaa,\n" 12128 " bbbbbbbbbbb,\n" 12129 " ccccccccccc,\n" 12130 " ddddddddddd,\n" 12131 "};", 12132 getLLVMStyleWithColumns(50)); 12133 12134 // Cases where distinguising braced lists and blocks is hard. 12135 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 12136 verifyFormat("void f() {\n" 12137 " return; // comment\n" 12138 "}\n" 12139 "SomeType t;"); 12140 verifyFormat("void f() {\n" 12141 " if (a) {\n" 12142 " f();\n" 12143 " }\n" 12144 "}\n" 12145 "SomeType t;"); 12146 12147 // In combination with BinPackArguments = false. 12148 FormatStyle NoBinPacking = getLLVMStyle(); 12149 NoBinPacking.BinPackArguments = false; 12150 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 12151 " bbbbb,\n" 12152 " ccccc,\n" 12153 " ddddd,\n" 12154 " eeeee,\n" 12155 " ffffff,\n" 12156 " ggggg,\n" 12157 " hhhhhh,\n" 12158 " iiiiii,\n" 12159 " jjjjjj,\n" 12160 " kkkkkk};", 12161 NoBinPacking); 12162 verifyFormat("const Aaaaaa aaaaa = {\n" 12163 " aaaaa,\n" 12164 " bbbbb,\n" 12165 " ccccc,\n" 12166 " ddddd,\n" 12167 " eeeee,\n" 12168 " ffffff,\n" 12169 " ggggg,\n" 12170 " hhhhhh,\n" 12171 " iiiiii,\n" 12172 " jjjjjj,\n" 12173 " kkkkkk,\n" 12174 "};", 12175 NoBinPacking); 12176 verifyFormat( 12177 "const Aaaaaa aaaaa = {\n" 12178 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 12179 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 12180 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 12181 "};", 12182 NoBinPacking); 12183 12184 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12185 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 12186 " CDDDP83848_BMCR_REGISTER,\n" 12187 " CDDDP83848_BMSR_REGISTER,\n" 12188 " CDDDP83848_RBR_REGISTER};", 12189 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 12190 " CDDDP83848_BMSR_REGISTER,\n" 12191 " CDDDP83848_RBR_REGISTER};", 12192 NoBinPacking)); 12193 12194 // FIXME: The alignment of these trailing comments might be bad. Then again, 12195 // this might be utterly useless in real code. 12196 verifyFormat("Constructor::Constructor()\n" 12197 " : some_value{ //\n" 12198 " aaaaaaa, //\n" 12199 " bbbbbbb} {}"); 12200 12201 // In braced lists, the first comment is always assumed to belong to the 12202 // first element. Thus, it can be moved to the next or previous line as 12203 // appropriate. 12204 EXPECT_EQ("function({// First element:\n" 12205 " 1,\n" 12206 " // Second element:\n" 12207 " 2});", 12208 format("function({\n" 12209 " // First element:\n" 12210 " 1,\n" 12211 " // Second element:\n" 12212 " 2});")); 12213 EXPECT_EQ("std::vector<int> MyNumbers{\n" 12214 " // First element:\n" 12215 " 1,\n" 12216 " // Second element:\n" 12217 " 2};", 12218 format("std::vector<int> MyNumbers{// First element:\n" 12219 " 1,\n" 12220 " // Second element:\n" 12221 " 2};", 12222 getLLVMStyleWithColumns(30))); 12223 // A trailing comma should still lead to an enforced line break and no 12224 // binpacking. 12225 EXPECT_EQ("vector<int> SomeVector = {\n" 12226 " // aaa\n" 12227 " 1,\n" 12228 " 2,\n" 12229 "};", 12230 format("vector<int> SomeVector = { // aaa\n" 12231 " 1, 2, };")); 12232 12233 // C++11 brace initializer list l-braces should not be treated any differently 12234 // when breaking before lambda bodies is enabled 12235 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 12236 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 12237 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 12238 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 12239 verifyFormat( 12240 "std::runtime_error{\n" 12241 " \"Long string which will force a break onto the next line...\"};", 12242 BreakBeforeLambdaBody); 12243 12244 FormatStyle ExtraSpaces = getLLVMStyle(); 12245 ExtraSpaces.Cpp11BracedListStyle = false; 12246 ExtraSpaces.ColumnLimit = 75; 12247 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 12248 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 12249 verifyFormat("f({ 1, 2 });", ExtraSpaces); 12250 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 12251 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 12252 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 12253 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 12254 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 12255 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 12256 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 12257 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 12258 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 12259 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 12260 verifyFormat("class Class {\n" 12261 " T member = { arg1, arg2 };\n" 12262 "};", 12263 ExtraSpaces); 12264 verifyFormat( 12265 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12266 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 12267 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 12268 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 12269 ExtraSpaces); 12270 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 12271 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 12272 ExtraSpaces); 12273 verifyFormat( 12274 "someFunction(OtherParam,\n" 12275 " BracedList{ // comment 1 (Forcing interesting break)\n" 12276 " param1, param2,\n" 12277 " // comment 2\n" 12278 " param3, param4 });", 12279 ExtraSpaces); 12280 verifyFormat( 12281 "std::this_thread::sleep_for(\n" 12282 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 12283 ExtraSpaces); 12284 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 12285 " aaaaaaa,\n" 12286 " aaaaaaaaaa,\n" 12287 " aaaaa,\n" 12288 " aaaaaaaaaaaaaaa,\n" 12289 " aaa,\n" 12290 " aaaaaaaaaa,\n" 12291 " a,\n" 12292 " aaaaaaaaaaaaaaaaaaaaa,\n" 12293 " aaaaaaaaaaaa,\n" 12294 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 12295 " aaaaaaa,\n" 12296 " a};"); 12297 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 12298 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 12299 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 12300 12301 // Avoid breaking between initializer/equal sign and opening brace 12302 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 12303 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 12304 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 12305 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 12306 " { \"ccccccccccccccccccccc\", 2 }\n" 12307 "};", 12308 ExtraSpaces); 12309 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 12310 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 12311 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 12312 " { \"ccccccccccccccccccccc\", 2 }\n" 12313 "};", 12314 ExtraSpaces); 12315 12316 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 12317 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 12318 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 12319 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 12320 12321 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 12322 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 12323 SpaceBetweenBraces.SpacesInParentheses = true; 12324 SpaceBetweenBraces.SpacesInSquareBrackets = true; 12325 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 12326 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 12327 verifyFormat("vector< int > x{ // comment 1\n" 12328 " 1, 2, 3, 4 };", 12329 SpaceBetweenBraces); 12330 SpaceBetweenBraces.ColumnLimit = 20; 12331 EXPECT_EQ("vector< int > x{\n" 12332 " 1, 2, 3, 4 };", 12333 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 12334 SpaceBetweenBraces.ColumnLimit = 24; 12335 EXPECT_EQ("vector< int > x{ 1, 2,\n" 12336 " 3, 4 };", 12337 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 12338 EXPECT_EQ("vector< int > x{\n" 12339 " 1,\n" 12340 " 2,\n" 12341 " 3,\n" 12342 " 4,\n" 12343 "};", 12344 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 12345 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 12346 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 12347 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 12348 } 12349 12350 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 12351 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12352 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12353 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12354 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12355 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12356 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12357 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 12358 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12359 " 1, 22, 333, 4444, 55555, //\n" 12360 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12361 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12362 verifyFormat( 12363 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12364 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12365 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 12366 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12367 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12368 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12369 " 7777777};"); 12370 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12371 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12372 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12373 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12374 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12375 " // Separating comment.\n" 12376 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12377 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12378 " // Leading comment\n" 12379 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12380 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12381 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12382 " 1, 1, 1, 1};", 12383 getLLVMStyleWithColumns(39)); 12384 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12385 " 1, 1, 1, 1};", 12386 getLLVMStyleWithColumns(38)); 12387 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 12388 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 12389 getLLVMStyleWithColumns(43)); 12390 verifyFormat( 12391 "static unsigned SomeValues[10][3] = {\n" 12392 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 12393 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 12394 verifyFormat("static auto fields = new vector<string>{\n" 12395 " \"aaaaaaaaaaaaa\",\n" 12396 " \"aaaaaaaaaaaaa\",\n" 12397 " \"aaaaaaaaaaaa\",\n" 12398 " \"aaaaaaaaaaaaaa\",\n" 12399 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12400 " \"aaaaaaaaaaaa\",\n" 12401 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12402 "};"); 12403 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 12404 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 12405 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 12406 " 3, cccccccccccccccccccccc};", 12407 getLLVMStyleWithColumns(60)); 12408 12409 // Trailing commas. 12410 verifyFormat("vector<int> x = {\n" 12411 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 12412 "};", 12413 getLLVMStyleWithColumns(39)); 12414 verifyFormat("vector<int> x = {\n" 12415 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 12416 "};", 12417 getLLVMStyleWithColumns(39)); 12418 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12419 " 1, 1, 1, 1,\n" 12420 " /**/ /**/};", 12421 getLLVMStyleWithColumns(39)); 12422 12423 // Trailing comment in the first line. 12424 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 12425 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 12426 " 111111111, 222222222, 3333333333, 444444444, //\n" 12427 " 11111111, 22222222, 333333333, 44444444};"); 12428 // Trailing comment in the last line. 12429 verifyFormat("int aaaaa[] = {\n" 12430 " 1, 2, 3, // comment\n" 12431 " 4, 5, 6 // comment\n" 12432 "};"); 12433 12434 // With nested lists, we should either format one item per line or all nested 12435 // lists one on line. 12436 // FIXME: For some nested lists, we can do better. 12437 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 12438 " {aaaaaaaaaaaaaaaaaaa},\n" 12439 " {aaaaaaaaaaaaaaaaaaaaa},\n" 12440 " {aaaaaaaaaaaaaaaaa}};", 12441 getLLVMStyleWithColumns(60)); 12442 verifyFormat( 12443 "SomeStruct my_struct_array = {\n" 12444 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 12445 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 12446 " {aaa, aaa},\n" 12447 " {aaa, aaa},\n" 12448 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 12449 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 12450 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 12451 12452 // No column layout should be used here. 12453 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 12454 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 12455 12456 verifyNoCrash("a<,"); 12457 12458 // No braced initializer here. 12459 verifyFormat("void f() {\n" 12460 " struct Dummy {};\n" 12461 " f(v);\n" 12462 "}"); 12463 12464 // Long lists should be formatted in columns even if they are nested. 12465 verifyFormat( 12466 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12467 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12468 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12469 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12470 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12471 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 12472 12473 // Allow "single-column" layout even if that violates the column limit. There 12474 // isn't going to be a better way. 12475 verifyFormat("std::vector<int> a = {\n" 12476 " aaaaaaaa,\n" 12477 " aaaaaaaa,\n" 12478 " aaaaaaaa,\n" 12479 " aaaaaaaa,\n" 12480 " aaaaaaaaaa,\n" 12481 " aaaaaaaa,\n" 12482 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 12483 getLLVMStyleWithColumns(30)); 12484 verifyFormat("vector<int> aaaa = {\n" 12485 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12486 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12487 " aaaaaa.aaaaaaa,\n" 12488 " aaaaaa.aaaaaaa,\n" 12489 " aaaaaa.aaaaaaa,\n" 12490 " aaaaaa.aaaaaaa,\n" 12491 "};"); 12492 12493 // Don't create hanging lists. 12494 verifyFormat("someFunction(Param, {List1, List2,\n" 12495 " List3});", 12496 getLLVMStyleWithColumns(35)); 12497 verifyFormat("someFunction(Param, Param,\n" 12498 " {List1, List2,\n" 12499 " List3});", 12500 getLLVMStyleWithColumns(35)); 12501 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 12502 " aaaaaaaaaaaaaaaaaaaaaaa);"); 12503 } 12504 12505 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 12506 FormatStyle DoNotMerge = getLLVMStyle(); 12507 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12508 12509 verifyFormat("void f() { return 42; }"); 12510 verifyFormat("void f() {\n" 12511 " return 42;\n" 12512 "}", 12513 DoNotMerge); 12514 verifyFormat("void f() {\n" 12515 " // Comment\n" 12516 "}"); 12517 verifyFormat("{\n" 12518 "#error {\n" 12519 " int a;\n" 12520 "}"); 12521 verifyFormat("{\n" 12522 " int a;\n" 12523 "#error {\n" 12524 "}"); 12525 verifyFormat("void f() {} // comment"); 12526 verifyFormat("void f() { int a; } // comment"); 12527 verifyFormat("void f() {\n" 12528 "} // comment", 12529 DoNotMerge); 12530 verifyFormat("void f() {\n" 12531 " int a;\n" 12532 "} // comment", 12533 DoNotMerge); 12534 verifyFormat("void f() {\n" 12535 "} // comment", 12536 getLLVMStyleWithColumns(15)); 12537 12538 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 12539 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 12540 12541 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 12542 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 12543 verifyFormat("class C {\n" 12544 " C()\n" 12545 " : iiiiiiii(nullptr),\n" 12546 " kkkkkkk(nullptr),\n" 12547 " mmmmmmm(nullptr),\n" 12548 " nnnnnnn(nullptr) {}\n" 12549 "};", 12550 getGoogleStyle()); 12551 12552 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 12553 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 12554 EXPECT_EQ("class C {\n" 12555 " A() : b(0) {}\n" 12556 "};", 12557 format("class C{A():b(0){}};", NoColumnLimit)); 12558 EXPECT_EQ("A()\n" 12559 " : b(0) {\n" 12560 "}", 12561 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 12562 12563 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 12564 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 12565 FormatStyle::SFS_None; 12566 EXPECT_EQ("A()\n" 12567 " : b(0) {\n" 12568 "}", 12569 format("A():b(0){}", DoNotMergeNoColumnLimit)); 12570 EXPECT_EQ("A()\n" 12571 " : b(0) {\n" 12572 "}", 12573 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 12574 12575 verifyFormat("#define A \\\n" 12576 " void f() { \\\n" 12577 " int i; \\\n" 12578 " }", 12579 getLLVMStyleWithColumns(20)); 12580 verifyFormat("#define A \\\n" 12581 " void f() { int i; }", 12582 getLLVMStyleWithColumns(21)); 12583 verifyFormat("#define A \\\n" 12584 " void f() { \\\n" 12585 " int i; \\\n" 12586 " } \\\n" 12587 " int j;", 12588 getLLVMStyleWithColumns(22)); 12589 verifyFormat("#define A \\\n" 12590 " void f() { int i; } \\\n" 12591 " int j;", 12592 getLLVMStyleWithColumns(23)); 12593 } 12594 12595 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 12596 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12597 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12598 verifyFormat("class C {\n" 12599 " int f() {}\n" 12600 "};", 12601 MergeEmptyOnly); 12602 verifyFormat("class C {\n" 12603 " int f() {\n" 12604 " return 42;\n" 12605 " }\n" 12606 "};", 12607 MergeEmptyOnly); 12608 verifyFormat("int f() {}", MergeEmptyOnly); 12609 verifyFormat("int f() {\n" 12610 " return 42;\n" 12611 "}", 12612 MergeEmptyOnly); 12613 12614 // Also verify behavior when BraceWrapping.AfterFunction = true 12615 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12616 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 12617 verifyFormat("int f() {}", MergeEmptyOnly); 12618 verifyFormat("class C {\n" 12619 " int f() {}\n" 12620 "};", 12621 MergeEmptyOnly); 12622 } 12623 12624 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 12625 FormatStyle MergeInlineOnly = getLLVMStyle(); 12626 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12627 verifyFormat("class C {\n" 12628 " int f() { return 42; }\n" 12629 "};", 12630 MergeInlineOnly); 12631 verifyFormat("int f() {\n" 12632 " return 42;\n" 12633 "}", 12634 MergeInlineOnly); 12635 12636 // SFS_Inline implies SFS_Empty 12637 verifyFormat("class C {\n" 12638 " int f() {}\n" 12639 "};", 12640 MergeInlineOnly); 12641 verifyFormat("int f() {}", MergeInlineOnly); 12642 12643 // Also verify behavior when BraceWrapping.AfterFunction = true 12644 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12645 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12646 verifyFormat("class C {\n" 12647 " int f() { return 42; }\n" 12648 "};", 12649 MergeInlineOnly); 12650 verifyFormat("int f()\n" 12651 "{\n" 12652 " return 42;\n" 12653 "}", 12654 MergeInlineOnly); 12655 12656 // SFS_Inline implies SFS_Empty 12657 verifyFormat("int f() {}", MergeInlineOnly); 12658 verifyFormat("class C {\n" 12659 " int f() {}\n" 12660 "};", 12661 MergeInlineOnly); 12662 12663 MergeInlineOnly.BraceWrapping.AfterClass = true; 12664 MergeInlineOnly.BraceWrapping.AfterStruct = true; 12665 verifyFormat("class C\n" 12666 "{\n" 12667 " int f() { return 42; }\n" 12668 "};", 12669 MergeInlineOnly); 12670 verifyFormat("struct C\n" 12671 "{\n" 12672 " int f() { return 42; }\n" 12673 "};", 12674 MergeInlineOnly); 12675 verifyFormat("int f()\n" 12676 "{\n" 12677 " return 42;\n" 12678 "}", 12679 MergeInlineOnly); 12680 verifyFormat("int f() {}", MergeInlineOnly); 12681 verifyFormat("class C\n" 12682 "{\n" 12683 " int f() { return 42; }\n" 12684 "};", 12685 MergeInlineOnly); 12686 verifyFormat("struct C\n" 12687 "{\n" 12688 " int f() { return 42; }\n" 12689 "};", 12690 MergeInlineOnly); 12691 verifyFormat("struct C\n" 12692 "// comment\n" 12693 "/* comment */\n" 12694 "// comment\n" 12695 "{\n" 12696 " int f() { return 42; }\n" 12697 "};", 12698 MergeInlineOnly); 12699 verifyFormat("/* comment */ struct C\n" 12700 "{\n" 12701 " int f() { return 42; }\n" 12702 "};", 12703 MergeInlineOnly); 12704 } 12705 12706 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 12707 FormatStyle MergeInlineOnly = getLLVMStyle(); 12708 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 12709 FormatStyle::SFS_InlineOnly; 12710 verifyFormat("class C {\n" 12711 " int f() { return 42; }\n" 12712 "};", 12713 MergeInlineOnly); 12714 verifyFormat("int f() {\n" 12715 " return 42;\n" 12716 "}", 12717 MergeInlineOnly); 12718 12719 // SFS_InlineOnly does not imply SFS_Empty 12720 verifyFormat("class C {\n" 12721 " int f() {}\n" 12722 "};", 12723 MergeInlineOnly); 12724 verifyFormat("int f() {\n" 12725 "}", 12726 MergeInlineOnly); 12727 12728 // Also verify behavior when BraceWrapping.AfterFunction = true 12729 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12730 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12731 verifyFormat("class C {\n" 12732 " int f() { return 42; }\n" 12733 "};", 12734 MergeInlineOnly); 12735 verifyFormat("int f()\n" 12736 "{\n" 12737 " return 42;\n" 12738 "}", 12739 MergeInlineOnly); 12740 12741 // SFS_InlineOnly does not imply SFS_Empty 12742 verifyFormat("int f()\n" 12743 "{\n" 12744 "}", 12745 MergeInlineOnly); 12746 verifyFormat("class C {\n" 12747 " int f() {}\n" 12748 "};", 12749 MergeInlineOnly); 12750 } 12751 12752 TEST_F(FormatTest, SplitEmptyFunction) { 12753 FormatStyle Style = getLLVMStyleWithColumns(40); 12754 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12755 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12756 Style.BraceWrapping.AfterFunction = true; 12757 Style.BraceWrapping.SplitEmptyFunction = false; 12758 12759 verifyFormat("int f()\n" 12760 "{}", 12761 Style); 12762 verifyFormat("int f()\n" 12763 "{\n" 12764 " return 42;\n" 12765 "}", 12766 Style); 12767 verifyFormat("int f()\n" 12768 "{\n" 12769 " // some comment\n" 12770 "}", 12771 Style); 12772 12773 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12774 verifyFormat("int f() {}", Style); 12775 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12776 "{}", 12777 Style); 12778 verifyFormat("int f()\n" 12779 "{\n" 12780 " return 0;\n" 12781 "}", 12782 Style); 12783 12784 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12785 verifyFormat("class Foo {\n" 12786 " int f() {}\n" 12787 "};\n", 12788 Style); 12789 verifyFormat("class Foo {\n" 12790 " int f() { return 0; }\n" 12791 "};\n", 12792 Style); 12793 verifyFormat("class Foo {\n" 12794 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12795 " {}\n" 12796 "};\n", 12797 Style); 12798 verifyFormat("class Foo {\n" 12799 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12800 " {\n" 12801 " return 0;\n" 12802 " }\n" 12803 "};\n", 12804 Style); 12805 12806 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12807 verifyFormat("int f() {}", Style); 12808 verifyFormat("int f() { return 0; }", Style); 12809 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12810 "{}", 12811 Style); 12812 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12813 "{\n" 12814 " return 0;\n" 12815 "}", 12816 Style); 12817 } 12818 12819 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { 12820 FormatStyle Style = getLLVMStyleWithColumns(40); 12821 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12822 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12823 Style.BraceWrapping.AfterFunction = true; 12824 Style.BraceWrapping.SplitEmptyFunction = true; 12825 Style.BraceWrapping.SplitEmptyRecord = false; 12826 12827 verifyFormat("class C {};", Style); 12828 verifyFormat("struct C {};", Style); 12829 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12830 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12831 "{\n" 12832 "}", 12833 Style); 12834 verifyFormat("class C {\n" 12835 " C()\n" 12836 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" 12837 " bbbbbbbbbbbbbbbbbbb()\n" 12838 " {\n" 12839 " }\n" 12840 " void\n" 12841 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12842 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12843 " {\n" 12844 " }\n" 12845 "};", 12846 Style); 12847 } 12848 12849 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12850 FormatStyle Style = getLLVMStyle(); 12851 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12852 verifyFormat("#ifdef A\n" 12853 "int f() {}\n" 12854 "#else\n" 12855 "int g() {}\n" 12856 "#endif", 12857 Style); 12858 } 12859 12860 TEST_F(FormatTest, SplitEmptyClass) { 12861 FormatStyle Style = getLLVMStyle(); 12862 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12863 Style.BraceWrapping.AfterClass = true; 12864 Style.BraceWrapping.SplitEmptyRecord = false; 12865 12866 verifyFormat("class Foo\n" 12867 "{};", 12868 Style); 12869 verifyFormat("/* something */ class Foo\n" 12870 "{};", 12871 Style); 12872 verifyFormat("template <typename X> class Foo\n" 12873 "{};", 12874 Style); 12875 verifyFormat("class Foo\n" 12876 "{\n" 12877 " Foo();\n" 12878 "};", 12879 Style); 12880 verifyFormat("typedef class Foo\n" 12881 "{\n" 12882 "} Foo_t;", 12883 Style); 12884 12885 Style.BraceWrapping.SplitEmptyRecord = true; 12886 Style.BraceWrapping.AfterStruct = true; 12887 verifyFormat("class rep\n" 12888 "{\n" 12889 "};", 12890 Style); 12891 verifyFormat("struct rep\n" 12892 "{\n" 12893 "};", 12894 Style); 12895 verifyFormat("template <typename T> class rep\n" 12896 "{\n" 12897 "};", 12898 Style); 12899 verifyFormat("template <typename T> struct rep\n" 12900 "{\n" 12901 "};", 12902 Style); 12903 verifyFormat("class rep\n" 12904 "{\n" 12905 " int x;\n" 12906 "};", 12907 Style); 12908 verifyFormat("struct rep\n" 12909 "{\n" 12910 " int x;\n" 12911 "};", 12912 Style); 12913 verifyFormat("template <typename T> class rep\n" 12914 "{\n" 12915 " int x;\n" 12916 "};", 12917 Style); 12918 verifyFormat("template <typename T> struct rep\n" 12919 "{\n" 12920 " int x;\n" 12921 "};", 12922 Style); 12923 verifyFormat("template <typename T> class rep // Foo\n" 12924 "{\n" 12925 " int x;\n" 12926 "};", 12927 Style); 12928 verifyFormat("template <typename T> struct rep // Bar\n" 12929 "{\n" 12930 " int x;\n" 12931 "};", 12932 Style); 12933 12934 verifyFormat("template <typename T> class rep<T>\n" 12935 "{\n" 12936 " int x;\n" 12937 "};", 12938 Style); 12939 12940 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12941 "{\n" 12942 " int x;\n" 12943 "};", 12944 Style); 12945 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12946 "{\n" 12947 "};", 12948 Style); 12949 12950 verifyFormat("#include \"stdint.h\"\n" 12951 "namespace rep {}", 12952 Style); 12953 verifyFormat("#include <stdint.h>\n" 12954 "namespace rep {}", 12955 Style); 12956 verifyFormat("#include <stdint.h>\n" 12957 "namespace rep {}", 12958 "#include <stdint.h>\n" 12959 "namespace rep {\n" 12960 "\n" 12961 "\n" 12962 "}", 12963 Style); 12964 } 12965 12966 TEST_F(FormatTest, SplitEmptyStruct) { 12967 FormatStyle Style = getLLVMStyle(); 12968 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12969 Style.BraceWrapping.AfterStruct = true; 12970 Style.BraceWrapping.SplitEmptyRecord = false; 12971 12972 verifyFormat("struct Foo\n" 12973 "{};", 12974 Style); 12975 verifyFormat("/* something */ struct Foo\n" 12976 "{};", 12977 Style); 12978 verifyFormat("template <typename X> struct Foo\n" 12979 "{};", 12980 Style); 12981 verifyFormat("struct Foo\n" 12982 "{\n" 12983 " Foo();\n" 12984 "};", 12985 Style); 12986 verifyFormat("typedef struct Foo\n" 12987 "{\n" 12988 "} Foo_t;", 12989 Style); 12990 // typedef struct Bar {} Bar_t; 12991 } 12992 12993 TEST_F(FormatTest, SplitEmptyUnion) { 12994 FormatStyle Style = getLLVMStyle(); 12995 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12996 Style.BraceWrapping.AfterUnion = true; 12997 Style.BraceWrapping.SplitEmptyRecord = false; 12998 12999 verifyFormat("union Foo\n" 13000 "{};", 13001 Style); 13002 verifyFormat("/* something */ union Foo\n" 13003 "{};", 13004 Style); 13005 verifyFormat("union Foo\n" 13006 "{\n" 13007 " A,\n" 13008 "};", 13009 Style); 13010 verifyFormat("typedef union Foo\n" 13011 "{\n" 13012 "} Foo_t;", 13013 Style); 13014 } 13015 13016 TEST_F(FormatTest, SplitEmptyNamespace) { 13017 FormatStyle Style = getLLVMStyle(); 13018 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 13019 Style.BraceWrapping.AfterNamespace = true; 13020 Style.BraceWrapping.SplitEmptyNamespace = false; 13021 13022 verifyFormat("namespace Foo\n" 13023 "{};", 13024 Style); 13025 verifyFormat("/* something */ namespace Foo\n" 13026 "{};", 13027 Style); 13028 verifyFormat("inline namespace Foo\n" 13029 "{};", 13030 Style); 13031 verifyFormat("/* something */ inline namespace Foo\n" 13032 "{};", 13033 Style); 13034 verifyFormat("export namespace Foo\n" 13035 "{};", 13036 Style); 13037 verifyFormat("namespace Foo\n" 13038 "{\n" 13039 "void Bar();\n" 13040 "};", 13041 Style); 13042 } 13043 13044 TEST_F(FormatTest, NeverMergeShortRecords) { 13045 FormatStyle Style = getLLVMStyle(); 13046 13047 verifyFormat("class Foo {\n" 13048 " Foo();\n" 13049 "};", 13050 Style); 13051 verifyFormat("typedef class Foo {\n" 13052 " Foo();\n" 13053 "} Foo_t;", 13054 Style); 13055 verifyFormat("struct Foo {\n" 13056 " Foo();\n" 13057 "};", 13058 Style); 13059 verifyFormat("typedef struct Foo {\n" 13060 " Foo();\n" 13061 "} Foo_t;", 13062 Style); 13063 verifyFormat("union Foo {\n" 13064 " A,\n" 13065 "};", 13066 Style); 13067 verifyFormat("typedef union Foo {\n" 13068 " A,\n" 13069 "} Foo_t;", 13070 Style); 13071 verifyFormat("namespace Foo {\n" 13072 "void Bar();\n" 13073 "};", 13074 Style); 13075 13076 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 13077 Style.BraceWrapping.AfterClass = true; 13078 Style.BraceWrapping.AfterStruct = true; 13079 Style.BraceWrapping.AfterUnion = true; 13080 Style.BraceWrapping.AfterNamespace = true; 13081 verifyFormat("class Foo\n" 13082 "{\n" 13083 " Foo();\n" 13084 "};", 13085 Style); 13086 verifyFormat("typedef class Foo\n" 13087 "{\n" 13088 " Foo();\n" 13089 "} Foo_t;", 13090 Style); 13091 verifyFormat("struct Foo\n" 13092 "{\n" 13093 " Foo();\n" 13094 "};", 13095 Style); 13096 verifyFormat("typedef struct Foo\n" 13097 "{\n" 13098 " Foo();\n" 13099 "} Foo_t;", 13100 Style); 13101 verifyFormat("union Foo\n" 13102 "{\n" 13103 " A,\n" 13104 "};", 13105 Style); 13106 verifyFormat("typedef union Foo\n" 13107 "{\n" 13108 " A,\n" 13109 "} Foo_t;", 13110 Style); 13111 verifyFormat("namespace Foo\n" 13112 "{\n" 13113 "void Bar();\n" 13114 "};", 13115 Style); 13116 } 13117 13118 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 13119 // Elaborate type variable declarations. 13120 verifyFormat("struct foo a = {bar};\nint n;"); 13121 verifyFormat("class foo a = {bar};\nint n;"); 13122 verifyFormat("union foo a = {bar};\nint n;"); 13123 13124 // Elaborate types inside function definitions. 13125 verifyFormat("struct foo f() {}\nint n;"); 13126 verifyFormat("class foo f() {}\nint n;"); 13127 verifyFormat("union foo f() {}\nint n;"); 13128 13129 // Templates. 13130 verifyFormat("template <class X> void f() {}\nint n;"); 13131 verifyFormat("template <struct X> void f() {}\nint n;"); 13132 verifyFormat("template <union X> void f() {}\nint n;"); 13133 13134 // Actual definitions... 13135 verifyFormat("struct {\n} n;"); 13136 verifyFormat( 13137 "template <template <class T, class Y>, class Z> class X {\n} n;"); 13138 verifyFormat("union Z {\n int n;\n} x;"); 13139 verifyFormat("class MACRO Z {\n} n;"); 13140 verifyFormat("class MACRO(X) Z {\n} n;"); 13141 verifyFormat("class __attribute__(X) Z {\n} n;"); 13142 verifyFormat("class __declspec(X) Z {\n} n;"); 13143 verifyFormat("class A##B##C {\n} n;"); 13144 verifyFormat("class alignas(16) Z {\n} n;"); 13145 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 13146 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 13147 13148 // Redefinition from nested context: 13149 verifyFormat("class A::B::C {\n} n;"); 13150 13151 // Template definitions. 13152 verifyFormat( 13153 "template <typename F>\n" 13154 "Matcher(const Matcher<F> &Other,\n" 13155 " typename enable_if_c<is_base_of<F, T>::value &&\n" 13156 " !is_same<F, T>::value>::type * = 0)\n" 13157 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 13158 13159 // FIXME: This is still incorrectly handled at the formatter side. 13160 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 13161 verifyFormat("int i = SomeFunction(a<b, a> b);"); 13162 13163 // FIXME: 13164 // This now gets parsed incorrectly as class definition. 13165 // verifyFormat("class A<int> f() {\n}\nint n;"); 13166 13167 // Elaborate types where incorrectly parsing the structural element would 13168 // break the indent. 13169 verifyFormat("if (true)\n" 13170 " class X x;\n" 13171 "else\n" 13172 " f();\n"); 13173 13174 // This is simply incomplete. Formatting is not important, but must not crash. 13175 verifyFormat("class A:"); 13176 } 13177 13178 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 13179 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 13180 format("#error Leave all white!!!!! space* alone!\n")); 13181 EXPECT_EQ( 13182 "#warning Leave all white!!!!! space* alone!\n", 13183 format("#warning Leave all white!!!!! space* alone!\n")); 13184 EXPECT_EQ("#error 1", format(" # error 1")); 13185 EXPECT_EQ("#warning 1", format(" # warning 1")); 13186 } 13187 13188 TEST_F(FormatTest, FormatHashIfExpressions) { 13189 verifyFormat("#if AAAA && BBBB"); 13190 verifyFormat("#if (AAAA && BBBB)"); 13191 verifyFormat("#elif (AAAA && BBBB)"); 13192 // FIXME: Come up with a better indentation for #elif. 13193 verifyFormat( 13194 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 13195 " defined(BBBBBBBB)\n" 13196 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 13197 " defined(BBBBBBBB)\n" 13198 "#endif", 13199 getLLVMStyleWithColumns(65)); 13200 } 13201 13202 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 13203 FormatStyle AllowsMergedIf = getGoogleStyle(); 13204 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 13205 FormatStyle::SIS_WithoutElse; 13206 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 13207 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 13208 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 13209 EXPECT_EQ("if (true) return 42;", 13210 format("if (true)\nreturn 42;", AllowsMergedIf)); 13211 FormatStyle ShortMergedIf = AllowsMergedIf; 13212 ShortMergedIf.ColumnLimit = 25; 13213 verifyFormat("#define A \\\n" 13214 " if (true) return 42;", 13215 ShortMergedIf); 13216 verifyFormat("#define A \\\n" 13217 " f(); \\\n" 13218 " if (true)\n" 13219 "#define B", 13220 ShortMergedIf); 13221 verifyFormat("#define A \\\n" 13222 " f(); \\\n" 13223 " if (true)\n" 13224 "g();", 13225 ShortMergedIf); 13226 verifyFormat("{\n" 13227 "#ifdef A\n" 13228 " // Comment\n" 13229 " if (true) continue;\n" 13230 "#endif\n" 13231 " // Comment\n" 13232 " if (true) continue;\n" 13233 "}", 13234 ShortMergedIf); 13235 ShortMergedIf.ColumnLimit = 33; 13236 verifyFormat("#define A \\\n" 13237 " if constexpr (true) return 42;", 13238 ShortMergedIf); 13239 verifyFormat("#define A \\\n" 13240 " if CONSTEXPR (true) return 42;", 13241 ShortMergedIf); 13242 ShortMergedIf.ColumnLimit = 29; 13243 verifyFormat("#define A \\\n" 13244 " if (aaaaaaaaaa) return 1; \\\n" 13245 " return 2;", 13246 ShortMergedIf); 13247 ShortMergedIf.ColumnLimit = 28; 13248 verifyFormat("#define A \\\n" 13249 " if (aaaaaaaaaa) \\\n" 13250 " return 1; \\\n" 13251 " return 2;", 13252 ShortMergedIf); 13253 verifyFormat("#define A \\\n" 13254 " if constexpr (aaaaaaa) \\\n" 13255 " return 1; \\\n" 13256 " return 2;", 13257 ShortMergedIf); 13258 verifyFormat("#define A \\\n" 13259 " if CONSTEXPR (aaaaaaa) \\\n" 13260 " return 1; \\\n" 13261 " return 2;", 13262 ShortMergedIf); 13263 } 13264 13265 TEST_F(FormatTest, FormatStarDependingOnContext) { 13266 verifyFormat("void f(int *a);"); 13267 verifyFormat("void f() { f(fint * b); }"); 13268 verifyFormat("class A {\n void f(int *a);\n};"); 13269 verifyFormat("class A {\n int *a;\n};"); 13270 verifyFormat("namespace a {\n" 13271 "namespace b {\n" 13272 "class A {\n" 13273 " void f() {}\n" 13274 " int *a;\n" 13275 "};\n" 13276 "} // namespace b\n" 13277 "} // namespace a"); 13278 } 13279 13280 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 13281 verifyFormat("while"); 13282 verifyFormat("operator"); 13283 } 13284 13285 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 13286 // This code would be painfully slow to format if we didn't skip it. 13287 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 13288 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13289 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13290 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13291 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13292 "A(1, 1)\n" 13293 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 13294 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13295 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13296 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13297 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13298 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13299 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13300 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13301 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13302 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 13303 // Deeply nested part is untouched, rest is formatted. 13304 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 13305 format(std::string("int i;\n") + Code + "int j;\n", 13306 getLLVMStyle(), SC_ExpectIncomplete)); 13307 } 13308 13309 //===----------------------------------------------------------------------===// 13310 // Objective-C tests. 13311 //===----------------------------------------------------------------------===// 13312 13313 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 13314 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 13315 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 13316 format("-(NSUInteger)indexOfObject:(id)anObject;")); 13317 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 13318 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 13319 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 13320 format("-(NSInteger)Method3:(id)anObject;")); 13321 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 13322 format("-(NSInteger)Method4:(id)anObject;")); 13323 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 13324 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 13325 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 13326 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 13327 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 13328 "forAllCells:(BOOL)flag;", 13329 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 13330 "forAllCells:(BOOL)flag;")); 13331 13332 // Very long objectiveC method declaration. 13333 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 13334 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 13335 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 13336 " inRange:(NSRange)range\n" 13337 " outRange:(NSRange)out_range\n" 13338 " outRange1:(NSRange)out_range1\n" 13339 " outRange2:(NSRange)out_range2\n" 13340 " outRange3:(NSRange)out_range3\n" 13341 " outRange4:(NSRange)out_range4\n" 13342 " outRange5:(NSRange)out_range5\n" 13343 " outRange6:(NSRange)out_range6\n" 13344 " outRange7:(NSRange)out_range7\n" 13345 " outRange8:(NSRange)out_range8\n" 13346 " outRange9:(NSRange)out_range9;"); 13347 13348 // When the function name has to be wrapped. 13349 FormatStyle Style = getLLVMStyle(); 13350 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 13351 // and always indents instead. 13352 Style.IndentWrappedFunctionNames = false; 13353 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13354 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 13355 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 13356 "}", 13357 Style); 13358 Style.IndentWrappedFunctionNames = true; 13359 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13360 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 13361 " anotherName:(NSString)dddddddddddddd {\n" 13362 "}", 13363 Style); 13364 13365 verifyFormat("- (int)sum:(vector<int>)numbers;"); 13366 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 13367 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 13368 // protocol lists (but not for template classes): 13369 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 13370 13371 verifyFormat("- (int (*)())foo:(int (*)())f;"); 13372 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 13373 13374 // If there's no return type (very rare in practice!), LLVM and Google style 13375 // agree. 13376 verifyFormat("- foo;"); 13377 verifyFormat("- foo:(int)f;"); 13378 verifyGoogleFormat("- foo:(int)foo;"); 13379 } 13380 13381 TEST_F(FormatTest, BreaksStringLiterals) { 13382 EXPECT_EQ("\"some text \"\n" 13383 "\"other\";", 13384 format("\"some text other\";", getLLVMStyleWithColumns(12))); 13385 EXPECT_EQ("\"some text \"\n" 13386 "\"other\";", 13387 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 13388 EXPECT_EQ( 13389 "#define A \\\n" 13390 " \"some \" \\\n" 13391 " \"text \" \\\n" 13392 " \"other\";", 13393 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 13394 EXPECT_EQ( 13395 "#define A \\\n" 13396 " \"so \" \\\n" 13397 " \"text \" \\\n" 13398 " \"other\";", 13399 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 13400 13401 EXPECT_EQ("\"some text\"", 13402 format("\"some text\"", getLLVMStyleWithColumns(1))); 13403 EXPECT_EQ("\"some text\"", 13404 format("\"some text\"", getLLVMStyleWithColumns(11))); 13405 EXPECT_EQ("\"some \"\n" 13406 "\"text\"", 13407 format("\"some text\"", getLLVMStyleWithColumns(10))); 13408 EXPECT_EQ("\"some \"\n" 13409 "\"text\"", 13410 format("\"some text\"", getLLVMStyleWithColumns(7))); 13411 EXPECT_EQ("\"some\"\n" 13412 "\" tex\"\n" 13413 "\"t\"", 13414 format("\"some text\"", getLLVMStyleWithColumns(6))); 13415 EXPECT_EQ("\"some\"\n" 13416 "\" tex\"\n" 13417 "\" and\"", 13418 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 13419 EXPECT_EQ("\"some\"\n" 13420 "\"/tex\"\n" 13421 "\"/and\"", 13422 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 13423 13424 EXPECT_EQ("variable =\n" 13425 " \"long string \"\n" 13426 " \"literal\";", 13427 format("variable = \"long string literal\";", 13428 getLLVMStyleWithColumns(20))); 13429 13430 EXPECT_EQ("variable = f(\n" 13431 " \"long string \"\n" 13432 " \"literal\",\n" 13433 " short,\n" 13434 " loooooooooooooooooooong);", 13435 format("variable = f(\"long string literal\", short, " 13436 "loooooooooooooooooooong);", 13437 getLLVMStyleWithColumns(20))); 13438 13439 EXPECT_EQ( 13440 "f(g(\"long string \"\n" 13441 " \"literal\"),\n" 13442 " b);", 13443 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 13444 EXPECT_EQ("f(g(\"long string \"\n" 13445 " \"literal\",\n" 13446 " a),\n" 13447 " b);", 13448 format("f(g(\"long string literal\", a), b);", 13449 getLLVMStyleWithColumns(20))); 13450 EXPECT_EQ( 13451 "f(\"one two\".split(\n" 13452 " variable));", 13453 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 13454 EXPECT_EQ("f(\"one two three four five six \"\n" 13455 " \"seven\".split(\n" 13456 " really_looooong_variable));", 13457 format("f(\"one two three four five six seven\"." 13458 "split(really_looooong_variable));", 13459 getLLVMStyleWithColumns(33))); 13460 13461 EXPECT_EQ("f(\"some \"\n" 13462 " \"text\",\n" 13463 " other);", 13464 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 13465 13466 // Only break as a last resort. 13467 verifyFormat( 13468 "aaaaaaaaaaaaaaaaaaaa(\n" 13469 " aaaaaaaaaaaaaaaaaaaa,\n" 13470 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 13471 13472 EXPECT_EQ("\"splitmea\"\n" 13473 "\"trandomp\"\n" 13474 "\"oint\"", 13475 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 13476 13477 EXPECT_EQ("\"split/\"\n" 13478 "\"pathat/\"\n" 13479 "\"slashes\"", 13480 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13481 13482 EXPECT_EQ("\"split/\"\n" 13483 "\"pathat/\"\n" 13484 "\"slashes\"", 13485 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13486 EXPECT_EQ("\"split at \"\n" 13487 "\"spaces/at/\"\n" 13488 "\"slashes.at.any$\"\n" 13489 "\"non-alphanumeric%\"\n" 13490 "\"1111111111characte\"\n" 13491 "\"rs\"", 13492 format("\"split at " 13493 "spaces/at/" 13494 "slashes.at." 13495 "any$non-" 13496 "alphanumeric%" 13497 "1111111111characte" 13498 "rs\"", 13499 getLLVMStyleWithColumns(20))); 13500 13501 // Verify that splitting the strings understands 13502 // Style::AlwaysBreakBeforeMultilineStrings. 13503 EXPECT_EQ("aaaaaaaaaaaa(\n" 13504 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 13505 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 13506 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 13507 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13508 "aaaaaaaaaaaaaaaaaaaaaa\");", 13509 getGoogleStyle())); 13510 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13511 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 13512 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 13513 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13514 "aaaaaaaaaaaaaaaaaaaaaa\";", 13515 getGoogleStyle())); 13516 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13517 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13518 format("llvm::outs() << " 13519 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 13520 "aaaaaaaaaaaaaaaaaaa\";")); 13521 EXPECT_EQ("ffff(\n" 13522 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13523 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13524 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 13525 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13526 getGoogleStyle())); 13527 13528 FormatStyle Style = getLLVMStyleWithColumns(12); 13529 Style.BreakStringLiterals = false; 13530 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 13531 13532 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 13533 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13534 EXPECT_EQ("#define A \\\n" 13535 " \"some \" \\\n" 13536 " \"text \" \\\n" 13537 " \"other\";", 13538 format("#define A \"some text other\";", AlignLeft)); 13539 } 13540 13541 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 13542 EXPECT_EQ("C a = \"some more \"\n" 13543 " \"text\";", 13544 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 13545 } 13546 13547 TEST_F(FormatTest, FullyRemoveEmptyLines) { 13548 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 13549 NoEmptyLines.MaxEmptyLinesToKeep = 0; 13550 EXPECT_EQ("int i = a(b());", 13551 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 13552 } 13553 13554 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 13555 EXPECT_EQ( 13556 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13557 "(\n" 13558 " \"x\t\");", 13559 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13560 "aaaaaaa(" 13561 "\"x\t\");")); 13562 } 13563 13564 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 13565 EXPECT_EQ( 13566 "u8\"utf8 string \"\n" 13567 "u8\"literal\";", 13568 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 13569 EXPECT_EQ( 13570 "u\"utf16 string \"\n" 13571 "u\"literal\";", 13572 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 13573 EXPECT_EQ( 13574 "U\"utf32 string \"\n" 13575 "U\"literal\";", 13576 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 13577 EXPECT_EQ("L\"wide string \"\n" 13578 "L\"literal\";", 13579 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 13580 EXPECT_EQ("@\"NSString \"\n" 13581 "@\"literal\";", 13582 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 13583 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 13584 13585 // This input makes clang-format try to split the incomplete unicode escape 13586 // sequence, which used to lead to a crasher. 13587 verifyNoCrash( 13588 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 13589 getLLVMStyleWithColumns(60)); 13590 } 13591 13592 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 13593 FormatStyle Style = getGoogleStyleWithColumns(15); 13594 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 13595 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 13596 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 13597 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 13598 EXPECT_EQ("u8R\"x(raw literal)x\";", 13599 format("u8R\"x(raw literal)x\";", Style)); 13600 } 13601 13602 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 13603 FormatStyle Style = getLLVMStyleWithColumns(20); 13604 EXPECT_EQ( 13605 "_T(\"aaaaaaaaaaaaaa\")\n" 13606 "_T(\"aaaaaaaaaaaaaa\")\n" 13607 "_T(\"aaaaaaaaaaaa\")", 13608 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 13609 EXPECT_EQ("f(x,\n" 13610 " _T(\"aaaaaaaaaaaa\")\n" 13611 " _T(\"aaa\"),\n" 13612 " z);", 13613 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 13614 13615 // FIXME: Handle embedded spaces in one iteration. 13616 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 13617 // "_T(\"aaaaaaaaaaaaa\")\n" 13618 // "_T(\"aaaaaaaaaaaaa\")\n" 13619 // "_T(\"a\")", 13620 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13621 // getLLVMStyleWithColumns(20))); 13622 EXPECT_EQ( 13623 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13624 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 13625 EXPECT_EQ("f(\n" 13626 "#if !TEST\n" 13627 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13628 "#endif\n" 13629 ");", 13630 format("f(\n" 13631 "#if !TEST\n" 13632 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13633 "#endif\n" 13634 ");")); 13635 EXPECT_EQ("f(\n" 13636 "\n" 13637 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 13638 format("f(\n" 13639 "\n" 13640 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 13641 // Regression test for accessing tokens past the end of a vector in the 13642 // TokenLexer. 13643 verifyNoCrash(R"(_T( 13644 " 13645 ) 13646 )"); 13647 } 13648 13649 TEST_F(FormatTest, BreaksStringLiteralOperands) { 13650 // In a function call with two operands, the second can be broken with no line 13651 // break before it. 13652 EXPECT_EQ( 13653 "func(a, \"long long \"\n" 13654 " \"long long\");", 13655 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 13656 // In a function call with three operands, the second must be broken with a 13657 // line break before it. 13658 EXPECT_EQ("func(a,\n" 13659 " \"long long long \"\n" 13660 " \"long\",\n" 13661 " c);", 13662 format("func(a, \"long long long long\", c);", 13663 getLLVMStyleWithColumns(24))); 13664 // In a function call with three operands, the third must be broken with a 13665 // line break before it. 13666 EXPECT_EQ("func(a, b,\n" 13667 " \"long long long \"\n" 13668 " \"long\");", 13669 format("func(a, b, \"long long long long\");", 13670 getLLVMStyleWithColumns(24))); 13671 // In a function call with three operands, both the second and the third must 13672 // be broken with a line break before them. 13673 EXPECT_EQ("func(a,\n" 13674 " \"long long long \"\n" 13675 " \"long\",\n" 13676 " \"long long long \"\n" 13677 " \"long\");", 13678 format("func(a, \"long long long long\", \"long long long long\");", 13679 getLLVMStyleWithColumns(24))); 13680 // In a chain of << with two operands, the second can be broken with no line 13681 // break before it. 13682 EXPECT_EQ("a << \"line line \"\n" 13683 " \"line\";", 13684 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 13685 // In a chain of << with three operands, the second can be broken with no line 13686 // break before it. 13687 EXPECT_EQ( 13688 "abcde << \"line \"\n" 13689 " \"line line\"\n" 13690 " << c;", 13691 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 13692 // In a chain of << with three operands, the third must be broken with a line 13693 // break before it. 13694 EXPECT_EQ( 13695 "a << b\n" 13696 " << \"line line \"\n" 13697 " \"line\";", 13698 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 13699 // In a chain of << with three operands, the second can be broken with no line 13700 // break before it and the third must be broken with a line break before it. 13701 EXPECT_EQ("abcd << \"line line \"\n" 13702 " \"line\"\n" 13703 " << \"line line \"\n" 13704 " \"line\";", 13705 format("abcd << \"line line line\" << \"line line line\";", 13706 getLLVMStyleWithColumns(20))); 13707 // In a chain of binary operators with two operands, the second can be broken 13708 // with no line break before it. 13709 EXPECT_EQ( 13710 "abcd + \"line line \"\n" 13711 " \"line line\";", 13712 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 13713 // In a chain of binary operators with three operands, the second must be 13714 // broken with a line break before it. 13715 EXPECT_EQ("abcd +\n" 13716 " \"line line \"\n" 13717 " \"line line\" +\n" 13718 " e;", 13719 format("abcd + \"line line line line\" + e;", 13720 getLLVMStyleWithColumns(20))); 13721 // In a function call with two operands, with AlignAfterOpenBracket enabled, 13722 // the first must be broken with a line break before it. 13723 FormatStyle Style = getLLVMStyleWithColumns(25); 13724 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13725 EXPECT_EQ("someFunction(\n" 13726 " \"long long long \"\n" 13727 " \"long\",\n" 13728 " a);", 13729 format("someFunction(\"long long long long\", a);", Style)); 13730 } 13731 13732 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 13733 EXPECT_EQ( 13734 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13737 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13739 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 13740 } 13741 13742 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 13743 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 13744 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 13745 EXPECT_EQ("fffffffffff(g(R\"x(\n" 13746 "multiline raw string literal xxxxxxxxxxxxxx\n" 13747 ")x\",\n" 13748 " a),\n" 13749 " b);", 13750 format("fffffffffff(g(R\"x(\n" 13751 "multiline raw string literal xxxxxxxxxxxxxx\n" 13752 ")x\", a), b);", 13753 getGoogleStyleWithColumns(20))); 13754 EXPECT_EQ("fffffffffff(\n" 13755 " g(R\"x(qqq\n" 13756 "multiline raw string literal xxxxxxxxxxxxxx\n" 13757 ")x\",\n" 13758 " a),\n" 13759 " b);", 13760 format("fffffffffff(g(R\"x(qqq\n" 13761 "multiline raw string literal xxxxxxxxxxxxxx\n" 13762 ")x\", a), b);", 13763 getGoogleStyleWithColumns(20))); 13764 13765 EXPECT_EQ("fffffffffff(R\"x(\n" 13766 "multiline raw string literal xxxxxxxxxxxxxx\n" 13767 ")x\");", 13768 format("fffffffffff(R\"x(\n" 13769 "multiline raw string literal xxxxxxxxxxxxxx\n" 13770 ")x\");", 13771 getGoogleStyleWithColumns(20))); 13772 EXPECT_EQ("fffffffffff(R\"x(\n" 13773 "multiline raw string literal xxxxxxxxxxxxxx\n" 13774 ")x\" + bbbbbb);", 13775 format("fffffffffff(R\"x(\n" 13776 "multiline raw string literal xxxxxxxxxxxxxx\n" 13777 ")x\" + bbbbbb);", 13778 getGoogleStyleWithColumns(20))); 13779 EXPECT_EQ("fffffffffff(\n" 13780 " R\"x(\n" 13781 "multiline raw string literal xxxxxxxxxxxxxx\n" 13782 ")x\" +\n" 13783 " bbbbbb);", 13784 format("fffffffffff(\n" 13785 " R\"x(\n" 13786 "multiline raw string literal xxxxxxxxxxxxxx\n" 13787 ")x\" + bbbbbb);", 13788 getGoogleStyleWithColumns(20))); 13789 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 13790 format("fffffffffff(\n" 13791 " R\"(single line raw string)\" + bbbbbb);")); 13792 } 13793 13794 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13795 verifyFormat("string a = \"unterminated;"); 13796 EXPECT_EQ("function(\"unterminated,\n" 13797 " OtherParameter);", 13798 format("function( \"unterminated,\n" 13799 " OtherParameter);")); 13800 } 13801 13802 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13803 FormatStyle Style = getLLVMStyle(); 13804 Style.Standard = FormatStyle::LS_Cpp03; 13805 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13806 format("#define x(_a) printf(\"foo\"_a);", Style)); 13807 } 13808 13809 TEST_F(FormatTest, CppLexVersion) { 13810 FormatStyle Style = getLLVMStyle(); 13811 // Formatting of x * y differs if x is a type. 13812 verifyFormat("void foo() { MACRO(a * b); }", Style); 13813 verifyFormat("void foo() { MACRO(int *b); }", Style); 13814 13815 // LLVM style uses latest lexer. 13816 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13817 Style.Standard = FormatStyle::LS_Cpp17; 13818 // But in c++17, char8_t isn't a keyword. 13819 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13820 } 13821 13822 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13823 13824 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13825 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13826 " \"ddeeefff\");", 13827 format("someFunction(\"aaabbbcccdddeeefff\");", 13828 getLLVMStyleWithColumns(25))); 13829 EXPECT_EQ("someFunction1234567890(\n" 13830 " \"aaabbbcccdddeeefff\");", 13831 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13832 getLLVMStyleWithColumns(26))); 13833 EXPECT_EQ("someFunction1234567890(\n" 13834 " \"aaabbbcccdddeeeff\"\n" 13835 " \"f\");", 13836 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13837 getLLVMStyleWithColumns(25))); 13838 EXPECT_EQ("someFunction1234567890(\n" 13839 " \"aaabbbcccdddeeeff\"\n" 13840 " \"f\");", 13841 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13842 getLLVMStyleWithColumns(24))); 13843 EXPECT_EQ("someFunction(\n" 13844 " \"aaabbbcc ddde \"\n" 13845 " \"efff\");", 13846 format("someFunction(\"aaabbbcc ddde efff\");", 13847 getLLVMStyleWithColumns(25))); 13848 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13849 " \"ddeeefff\");", 13850 format("someFunction(\"aaabbbccc ddeeefff\");", 13851 getLLVMStyleWithColumns(25))); 13852 EXPECT_EQ("someFunction1234567890(\n" 13853 " \"aaabb \"\n" 13854 " \"cccdddeeefff\");", 13855 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13856 getLLVMStyleWithColumns(25))); 13857 EXPECT_EQ("#define A \\\n" 13858 " string s = \\\n" 13859 " \"123456789\" \\\n" 13860 " \"0\"; \\\n" 13861 " int i;", 13862 format("#define A string s = \"1234567890\"; int i;", 13863 getLLVMStyleWithColumns(20))); 13864 EXPECT_EQ("someFunction(\n" 13865 " \"aaabbbcc \"\n" 13866 " \"dddeeefff\");", 13867 format("someFunction(\"aaabbbcc dddeeefff\");", 13868 getLLVMStyleWithColumns(25))); 13869 } 13870 13871 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13872 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13873 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13874 EXPECT_EQ("\"test\"\n" 13875 "\"\\n\"", 13876 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13877 EXPECT_EQ("\"tes\\\\\"\n" 13878 "\"n\"", 13879 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13880 EXPECT_EQ("\"\\\\\\\\\"\n" 13881 "\"\\n\"", 13882 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13883 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13884 EXPECT_EQ("\"\\uff01\"\n" 13885 "\"test\"", 13886 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13887 EXPECT_EQ("\"\\Uff01ff02\"", 13888 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13889 EXPECT_EQ("\"\\x000000000001\"\n" 13890 "\"next\"", 13891 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13892 EXPECT_EQ("\"\\x000000000001next\"", 13893 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13894 EXPECT_EQ("\"\\x000000000001\"", 13895 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13896 EXPECT_EQ("\"test\"\n" 13897 "\"\\000000\"\n" 13898 "\"000001\"", 13899 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13900 EXPECT_EQ("\"test\\000\"\n" 13901 "\"00000000\"\n" 13902 "\"1\"", 13903 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13904 } 13905 13906 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13907 verifyFormat("void f() {\n" 13908 " return g() {}\n" 13909 " void h() {}"); 13910 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13911 "g();\n" 13912 "}"); 13913 } 13914 13915 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13916 verifyFormat( 13917 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13918 } 13919 13920 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13921 verifyFormat("class X {\n" 13922 " void f() {\n" 13923 " }\n" 13924 "};", 13925 getLLVMStyleWithColumns(12)); 13926 } 13927 13928 TEST_F(FormatTest, ConfigurableIndentWidth) { 13929 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13930 EightIndent.IndentWidth = 8; 13931 EightIndent.ContinuationIndentWidth = 8; 13932 verifyFormat("void f() {\n" 13933 " someFunction();\n" 13934 " if (true) {\n" 13935 " f();\n" 13936 " }\n" 13937 "}", 13938 EightIndent); 13939 verifyFormat("class X {\n" 13940 " void f() {\n" 13941 " }\n" 13942 "};", 13943 EightIndent); 13944 verifyFormat("int x[] = {\n" 13945 " call(),\n" 13946 " call()};", 13947 EightIndent); 13948 } 13949 13950 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13951 verifyFormat("double\n" 13952 "f();", 13953 getLLVMStyleWithColumns(8)); 13954 } 13955 13956 TEST_F(FormatTest, ConfigurableUseOfTab) { 13957 FormatStyle Tab = getLLVMStyleWithColumns(42); 13958 Tab.IndentWidth = 8; 13959 Tab.UseTab = FormatStyle::UT_Always; 13960 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13961 13962 EXPECT_EQ("if (aaaaaaaa && // q\n" 13963 " bb)\t\t// w\n" 13964 "\t;", 13965 format("if (aaaaaaaa &&// q\n" 13966 "bb)// w\n" 13967 ";", 13968 Tab)); 13969 EXPECT_EQ("if (aaa && bbb) // w\n" 13970 "\t;", 13971 format("if(aaa&&bbb)// w\n" 13972 ";", 13973 Tab)); 13974 13975 verifyFormat("class X {\n" 13976 "\tvoid f() {\n" 13977 "\t\tsomeFunction(parameter1,\n" 13978 "\t\t\t parameter2);\n" 13979 "\t}\n" 13980 "};", 13981 Tab); 13982 verifyFormat("#define A \\\n" 13983 "\tvoid f() { \\\n" 13984 "\t\tsomeFunction( \\\n" 13985 "\t\t parameter1, \\\n" 13986 "\t\t parameter2); \\\n" 13987 "\t}", 13988 Tab); 13989 verifyFormat("int a;\t // x\n" 13990 "int bbbbbbbb; // x\n", 13991 Tab); 13992 13993 Tab.TabWidth = 4; 13994 Tab.IndentWidth = 8; 13995 verifyFormat("class TabWidth4Indent8 {\n" 13996 "\t\tvoid f() {\n" 13997 "\t\t\t\tsomeFunction(parameter1,\n" 13998 "\t\t\t\t\t\t\t parameter2);\n" 13999 "\t\t}\n" 14000 "};", 14001 Tab); 14002 14003 Tab.TabWidth = 4; 14004 Tab.IndentWidth = 4; 14005 verifyFormat("class TabWidth4Indent4 {\n" 14006 "\tvoid f() {\n" 14007 "\t\tsomeFunction(parameter1,\n" 14008 "\t\t\t\t\t parameter2);\n" 14009 "\t}\n" 14010 "};", 14011 Tab); 14012 14013 Tab.TabWidth = 8; 14014 Tab.IndentWidth = 4; 14015 verifyFormat("class TabWidth8Indent4 {\n" 14016 " void f() {\n" 14017 "\tsomeFunction(parameter1,\n" 14018 "\t\t parameter2);\n" 14019 " }\n" 14020 "};", 14021 Tab); 14022 14023 Tab.TabWidth = 8; 14024 Tab.IndentWidth = 8; 14025 EXPECT_EQ("/*\n" 14026 "\t a\t\tcomment\n" 14027 "\t in multiple lines\n" 14028 " */", 14029 format(" /*\t \t \n" 14030 " \t \t a\t\tcomment\t \t\n" 14031 " \t \t in multiple lines\t\n" 14032 " \t */", 14033 Tab)); 14034 14035 Tab.UseTab = FormatStyle::UT_ForIndentation; 14036 verifyFormat("{\n" 14037 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14038 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14039 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14040 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14041 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14042 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14043 "};", 14044 Tab); 14045 verifyFormat("enum AA {\n" 14046 "\ta1, // Force multiple lines\n" 14047 "\ta2,\n" 14048 "\ta3\n" 14049 "};", 14050 Tab); 14051 EXPECT_EQ("if (aaaaaaaa && // q\n" 14052 " bb) // w\n" 14053 "\t;", 14054 format("if (aaaaaaaa &&// q\n" 14055 "bb)// w\n" 14056 ";", 14057 Tab)); 14058 verifyFormat("class X {\n" 14059 "\tvoid f() {\n" 14060 "\t\tsomeFunction(parameter1,\n" 14061 "\t\t parameter2);\n" 14062 "\t}\n" 14063 "};", 14064 Tab); 14065 verifyFormat("{\n" 14066 "\tQ(\n" 14067 "\t {\n" 14068 "\t\t int a;\n" 14069 "\t\t someFunction(aaaaaaaa,\n" 14070 "\t\t bbbbbbb);\n" 14071 "\t },\n" 14072 "\t p);\n" 14073 "}", 14074 Tab); 14075 EXPECT_EQ("{\n" 14076 "\t/* aaaa\n" 14077 "\t bbbb */\n" 14078 "}", 14079 format("{\n" 14080 "/* aaaa\n" 14081 " bbbb */\n" 14082 "}", 14083 Tab)); 14084 EXPECT_EQ("{\n" 14085 "\t/*\n" 14086 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14087 "\t bbbbbbbbbbbbb\n" 14088 "\t*/\n" 14089 "}", 14090 format("{\n" 14091 "/*\n" 14092 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14093 "*/\n" 14094 "}", 14095 Tab)); 14096 EXPECT_EQ("{\n" 14097 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14098 "\t// bbbbbbbbbbbbb\n" 14099 "}", 14100 format("{\n" 14101 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14102 "}", 14103 Tab)); 14104 EXPECT_EQ("{\n" 14105 "\t/*\n" 14106 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14107 "\t bbbbbbbbbbbbb\n" 14108 "\t*/\n" 14109 "}", 14110 format("{\n" 14111 "\t/*\n" 14112 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14113 "\t*/\n" 14114 "}", 14115 Tab)); 14116 EXPECT_EQ("{\n" 14117 "\t/*\n" 14118 "\n" 14119 "\t*/\n" 14120 "}", 14121 format("{\n" 14122 "\t/*\n" 14123 "\n" 14124 "\t*/\n" 14125 "}", 14126 Tab)); 14127 EXPECT_EQ("{\n" 14128 "\t/*\n" 14129 " asdf\n" 14130 "\t*/\n" 14131 "}", 14132 format("{\n" 14133 "\t/*\n" 14134 " asdf\n" 14135 "\t*/\n" 14136 "}", 14137 Tab)); 14138 14139 verifyFormat("void f() {\n" 14140 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 14141 "\t : bbbbbbbbbbbbbbbbbb\n" 14142 "}", 14143 Tab); 14144 FormatStyle TabNoBreak = Tab; 14145 TabNoBreak.BreakBeforeTernaryOperators = false; 14146 verifyFormat("void f() {\n" 14147 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 14148 "\t bbbbbbbbbbbbbbbbbb\n" 14149 "}", 14150 TabNoBreak); 14151 verifyFormat("void f() {\n" 14152 "\treturn true ?\n" 14153 "\t aaaaaaaaaaaaaaaaaaaa :\n" 14154 "\t bbbbbbbbbbbbbbbbbbbb\n" 14155 "}", 14156 TabNoBreak); 14157 14158 Tab.UseTab = FormatStyle::UT_Never; 14159 EXPECT_EQ("/*\n" 14160 " a\t\tcomment\n" 14161 " in multiple lines\n" 14162 " */", 14163 format(" /*\t \t \n" 14164 " \t \t a\t\tcomment\t \t\n" 14165 " \t \t in multiple lines\t\n" 14166 " \t */", 14167 Tab)); 14168 EXPECT_EQ("/* some\n" 14169 " comment */", 14170 format(" \t \t /* some\n" 14171 " \t \t comment */", 14172 Tab)); 14173 EXPECT_EQ("int a; /* some\n" 14174 " comment */", 14175 format(" \t \t int a; /* some\n" 14176 " \t \t comment */", 14177 Tab)); 14178 14179 EXPECT_EQ("int a; /* some\n" 14180 "comment */", 14181 format(" \t \t int\ta; /* some\n" 14182 " \t \t comment */", 14183 Tab)); 14184 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14185 " comment */", 14186 format(" \t \t f(\"\t\t\"); /* some\n" 14187 " \t \t comment */", 14188 Tab)); 14189 EXPECT_EQ("{\n" 14190 " /*\n" 14191 " * Comment\n" 14192 " */\n" 14193 " int i;\n" 14194 "}", 14195 format("{\n" 14196 "\t/*\n" 14197 "\t * Comment\n" 14198 "\t */\n" 14199 "\t int i;\n" 14200 "}", 14201 Tab)); 14202 14203 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14204 Tab.TabWidth = 8; 14205 Tab.IndentWidth = 8; 14206 EXPECT_EQ("if (aaaaaaaa && // q\n" 14207 " bb) // w\n" 14208 "\t;", 14209 format("if (aaaaaaaa &&// q\n" 14210 "bb)// w\n" 14211 ";", 14212 Tab)); 14213 EXPECT_EQ("if (aaa && bbb) // w\n" 14214 "\t;", 14215 format("if(aaa&&bbb)// w\n" 14216 ";", 14217 Tab)); 14218 verifyFormat("class X {\n" 14219 "\tvoid f() {\n" 14220 "\t\tsomeFunction(parameter1,\n" 14221 "\t\t\t parameter2);\n" 14222 "\t}\n" 14223 "};", 14224 Tab); 14225 verifyFormat("#define A \\\n" 14226 "\tvoid f() { \\\n" 14227 "\t\tsomeFunction( \\\n" 14228 "\t\t parameter1, \\\n" 14229 "\t\t parameter2); \\\n" 14230 "\t}", 14231 Tab); 14232 Tab.TabWidth = 4; 14233 Tab.IndentWidth = 8; 14234 verifyFormat("class TabWidth4Indent8 {\n" 14235 "\t\tvoid f() {\n" 14236 "\t\t\t\tsomeFunction(parameter1,\n" 14237 "\t\t\t\t\t\t\t parameter2);\n" 14238 "\t\t}\n" 14239 "};", 14240 Tab); 14241 Tab.TabWidth = 4; 14242 Tab.IndentWidth = 4; 14243 verifyFormat("class TabWidth4Indent4 {\n" 14244 "\tvoid f() {\n" 14245 "\t\tsomeFunction(parameter1,\n" 14246 "\t\t\t\t\t parameter2);\n" 14247 "\t}\n" 14248 "};", 14249 Tab); 14250 Tab.TabWidth = 8; 14251 Tab.IndentWidth = 4; 14252 verifyFormat("class TabWidth8Indent4 {\n" 14253 " void f() {\n" 14254 "\tsomeFunction(parameter1,\n" 14255 "\t\t parameter2);\n" 14256 " }\n" 14257 "};", 14258 Tab); 14259 Tab.TabWidth = 8; 14260 Tab.IndentWidth = 8; 14261 EXPECT_EQ("/*\n" 14262 "\t a\t\tcomment\n" 14263 "\t in multiple lines\n" 14264 " */", 14265 format(" /*\t \t \n" 14266 " \t \t a\t\tcomment\t \t\n" 14267 " \t \t in multiple lines\t\n" 14268 " \t */", 14269 Tab)); 14270 verifyFormat("{\n" 14271 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14272 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14273 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14274 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14275 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14276 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14277 "};", 14278 Tab); 14279 verifyFormat("enum AA {\n" 14280 "\ta1, // Force multiple lines\n" 14281 "\ta2,\n" 14282 "\ta3\n" 14283 "};", 14284 Tab); 14285 EXPECT_EQ("if (aaaaaaaa && // q\n" 14286 " bb) // w\n" 14287 "\t;", 14288 format("if (aaaaaaaa &&// q\n" 14289 "bb)// w\n" 14290 ";", 14291 Tab)); 14292 verifyFormat("class X {\n" 14293 "\tvoid f() {\n" 14294 "\t\tsomeFunction(parameter1,\n" 14295 "\t\t\t parameter2);\n" 14296 "\t}\n" 14297 "};", 14298 Tab); 14299 verifyFormat("{\n" 14300 "\tQ(\n" 14301 "\t {\n" 14302 "\t\t int a;\n" 14303 "\t\t someFunction(aaaaaaaa,\n" 14304 "\t\t\t\t bbbbbbb);\n" 14305 "\t },\n" 14306 "\t p);\n" 14307 "}", 14308 Tab); 14309 EXPECT_EQ("{\n" 14310 "\t/* aaaa\n" 14311 "\t bbbb */\n" 14312 "}", 14313 format("{\n" 14314 "/* aaaa\n" 14315 " bbbb */\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 "/*\n" 14326 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14327 "*/\n" 14328 "}", 14329 Tab)); 14330 EXPECT_EQ("{\n" 14331 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14332 "\t// bbbbbbbbbbbbb\n" 14333 "}", 14334 format("{\n" 14335 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14336 "}", 14337 Tab)); 14338 EXPECT_EQ("{\n" 14339 "\t/*\n" 14340 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14341 "\t bbbbbbbbbbbbb\n" 14342 "\t*/\n" 14343 "}", 14344 format("{\n" 14345 "\t/*\n" 14346 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14347 "\t*/\n" 14348 "}", 14349 Tab)); 14350 EXPECT_EQ("{\n" 14351 "\t/*\n" 14352 "\n" 14353 "\t*/\n" 14354 "}", 14355 format("{\n" 14356 "\t/*\n" 14357 "\n" 14358 "\t*/\n" 14359 "}", 14360 Tab)); 14361 EXPECT_EQ("{\n" 14362 "\t/*\n" 14363 " asdf\n" 14364 "\t*/\n" 14365 "}", 14366 format("{\n" 14367 "\t/*\n" 14368 " asdf\n" 14369 "\t*/\n" 14370 "}", 14371 Tab)); 14372 EXPECT_EQ("/* some\n" 14373 " comment */", 14374 format(" \t \t /* some\n" 14375 " \t \t comment */", 14376 Tab)); 14377 EXPECT_EQ("int a; /* some\n" 14378 " comment */", 14379 format(" \t \t int a; /* some\n" 14380 " \t \t comment */", 14381 Tab)); 14382 EXPECT_EQ("int a; /* some\n" 14383 "comment */", 14384 format(" \t \t int\ta; /* some\n" 14385 " \t \t comment */", 14386 Tab)); 14387 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14388 " comment */", 14389 format(" \t \t f(\"\t\t\"); /* some\n" 14390 " \t \t comment */", 14391 Tab)); 14392 EXPECT_EQ("{\n" 14393 "\t/*\n" 14394 "\t * Comment\n" 14395 "\t */\n" 14396 "\tint i;\n" 14397 "}", 14398 format("{\n" 14399 "\t/*\n" 14400 "\t * Comment\n" 14401 "\t */\n" 14402 "\t int i;\n" 14403 "}", 14404 Tab)); 14405 Tab.TabWidth = 2; 14406 Tab.IndentWidth = 2; 14407 EXPECT_EQ("{\n" 14408 "\t/* aaaa\n" 14409 "\t\t bbbb */\n" 14410 "}", 14411 format("{\n" 14412 "/* aaaa\n" 14413 "\t bbbb */\n" 14414 "}", 14415 Tab)); 14416 EXPECT_EQ("{\n" 14417 "\t/*\n" 14418 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14419 "\t\tbbbbbbbbbbbbb\n" 14420 "\t*/\n" 14421 "}", 14422 format("{\n" 14423 "/*\n" 14424 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14425 "*/\n" 14426 "}", 14427 Tab)); 14428 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14429 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14430 Tab.TabWidth = 4; 14431 Tab.IndentWidth = 4; 14432 verifyFormat("class Assign {\n" 14433 "\tvoid f() {\n" 14434 "\t\tint x = 123;\n" 14435 "\t\tint random = 4;\n" 14436 "\t\tstd::string alphabet =\n" 14437 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14438 "\t}\n" 14439 "};", 14440 Tab); 14441 14442 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14443 Tab.TabWidth = 8; 14444 Tab.IndentWidth = 8; 14445 EXPECT_EQ("if (aaaaaaaa && // q\n" 14446 " bb) // w\n" 14447 "\t;", 14448 format("if (aaaaaaaa &&// q\n" 14449 "bb)// w\n" 14450 ";", 14451 Tab)); 14452 EXPECT_EQ("if (aaa && bbb) // w\n" 14453 "\t;", 14454 format("if(aaa&&bbb)// w\n" 14455 ";", 14456 Tab)); 14457 verifyFormat("class X {\n" 14458 "\tvoid f() {\n" 14459 "\t\tsomeFunction(parameter1,\n" 14460 "\t\t parameter2);\n" 14461 "\t}\n" 14462 "};", 14463 Tab); 14464 verifyFormat("#define A \\\n" 14465 "\tvoid f() { \\\n" 14466 "\t\tsomeFunction( \\\n" 14467 "\t\t parameter1, \\\n" 14468 "\t\t parameter2); \\\n" 14469 "\t}", 14470 Tab); 14471 Tab.TabWidth = 4; 14472 Tab.IndentWidth = 8; 14473 verifyFormat("class TabWidth4Indent8 {\n" 14474 "\t\tvoid f() {\n" 14475 "\t\t\t\tsomeFunction(parameter1,\n" 14476 "\t\t\t\t parameter2);\n" 14477 "\t\t}\n" 14478 "};", 14479 Tab); 14480 Tab.TabWidth = 4; 14481 Tab.IndentWidth = 4; 14482 verifyFormat("class TabWidth4Indent4 {\n" 14483 "\tvoid f() {\n" 14484 "\t\tsomeFunction(parameter1,\n" 14485 "\t\t parameter2);\n" 14486 "\t}\n" 14487 "};", 14488 Tab); 14489 Tab.TabWidth = 8; 14490 Tab.IndentWidth = 4; 14491 verifyFormat("class TabWidth8Indent4 {\n" 14492 " void f() {\n" 14493 "\tsomeFunction(parameter1,\n" 14494 "\t parameter2);\n" 14495 " }\n" 14496 "};", 14497 Tab); 14498 Tab.TabWidth = 8; 14499 Tab.IndentWidth = 8; 14500 EXPECT_EQ("/*\n" 14501 " a\t\tcomment\n" 14502 " in multiple lines\n" 14503 " */", 14504 format(" /*\t \t \n" 14505 " \t \t a\t\tcomment\t \t\n" 14506 " \t \t in multiple lines\t\n" 14507 " \t */", 14508 Tab)); 14509 verifyFormat("{\n" 14510 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14511 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14512 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14513 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14514 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14515 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14516 "};", 14517 Tab); 14518 verifyFormat("enum AA {\n" 14519 "\ta1, // Force multiple lines\n" 14520 "\ta2,\n" 14521 "\ta3\n" 14522 "};", 14523 Tab); 14524 EXPECT_EQ("if (aaaaaaaa && // q\n" 14525 " bb) // w\n" 14526 "\t;", 14527 format("if (aaaaaaaa &&// q\n" 14528 "bb)// w\n" 14529 ";", 14530 Tab)); 14531 verifyFormat("class X {\n" 14532 "\tvoid f() {\n" 14533 "\t\tsomeFunction(parameter1,\n" 14534 "\t\t parameter2);\n" 14535 "\t}\n" 14536 "};", 14537 Tab); 14538 verifyFormat("{\n" 14539 "\tQ(\n" 14540 "\t {\n" 14541 "\t\t int a;\n" 14542 "\t\t someFunction(aaaaaaaa,\n" 14543 "\t\t bbbbbbb);\n" 14544 "\t },\n" 14545 "\t p);\n" 14546 "}", 14547 Tab); 14548 EXPECT_EQ("{\n" 14549 "\t/* aaaa\n" 14550 "\t bbbb */\n" 14551 "}", 14552 format("{\n" 14553 "/* aaaa\n" 14554 " bbbb */\n" 14555 "}", 14556 Tab)); 14557 EXPECT_EQ("{\n" 14558 "\t/*\n" 14559 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14560 "\t bbbbbbbbbbbbb\n" 14561 "\t*/\n" 14562 "}", 14563 format("{\n" 14564 "/*\n" 14565 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14566 "*/\n" 14567 "}", 14568 Tab)); 14569 EXPECT_EQ("{\n" 14570 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14571 "\t// bbbbbbbbbbbbb\n" 14572 "}", 14573 format("{\n" 14574 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14575 "}", 14576 Tab)); 14577 EXPECT_EQ("{\n" 14578 "\t/*\n" 14579 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14580 "\t bbbbbbbbbbbbb\n" 14581 "\t*/\n" 14582 "}", 14583 format("{\n" 14584 "\t/*\n" 14585 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14586 "\t*/\n" 14587 "}", 14588 Tab)); 14589 EXPECT_EQ("{\n" 14590 "\t/*\n" 14591 "\n" 14592 "\t*/\n" 14593 "}", 14594 format("{\n" 14595 "\t/*\n" 14596 "\n" 14597 "\t*/\n" 14598 "}", 14599 Tab)); 14600 EXPECT_EQ("{\n" 14601 "\t/*\n" 14602 " asdf\n" 14603 "\t*/\n" 14604 "}", 14605 format("{\n" 14606 "\t/*\n" 14607 " asdf\n" 14608 "\t*/\n" 14609 "}", 14610 Tab)); 14611 EXPECT_EQ("/* some\n" 14612 " comment */", 14613 format(" \t \t /* some\n" 14614 " \t \t comment */", 14615 Tab)); 14616 EXPECT_EQ("int a; /* some\n" 14617 " comment */", 14618 format(" \t \t int a; /* some\n" 14619 " \t \t comment */", 14620 Tab)); 14621 EXPECT_EQ("int a; /* some\n" 14622 "comment */", 14623 format(" \t \t int\ta; /* some\n" 14624 " \t \t comment */", 14625 Tab)); 14626 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14627 " comment */", 14628 format(" \t \t f(\"\t\t\"); /* some\n" 14629 " \t \t comment */", 14630 Tab)); 14631 EXPECT_EQ("{\n" 14632 "\t/*\n" 14633 "\t * Comment\n" 14634 "\t */\n" 14635 "\tint i;\n" 14636 "}", 14637 format("{\n" 14638 "\t/*\n" 14639 "\t * Comment\n" 14640 "\t */\n" 14641 "\t int i;\n" 14642 "}", 14643 Tab)); 14644 Tab.TabWidth = 2; 14645 Tab.IndentWidth = 2; 14646 EXPECT_EQ("{\n" 14647 "\t/* aaaa\n" 14648 "\t bbbb */\n" 14649 "}", 14650 format("{\n" 14651 "/* aaaa\n" 14652 " bbbb */\n" 14653 "}", 14654 Tab)); 14655 EXPECT_EQ("{\n" 14656 "\t/*\n" 14657 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14658 "\t bbbbbbbbbbbbb\n" 14659 "\t*/\n" 14660 "}", 14661 format("{\n" 14662 "/*\n" 14663 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14664 "*/\n" 14665 "}", 14666 Tab)); 14667 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14668 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14669 Tab.TabWidth = 4; 14670 Tab.IndentWidth = 4; 14671 verifyFormat("class Assign {\n" 14672 "\tvoid f() {\n" 14673 "\t\tint x = 123;\n" 14674 "\t\tint random = 4;\n" 14675 "\t\tstd::string alphabet =\n" 14676 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14677 "\t}\n" 14678 "};", 14679 Tab); 14680 Tab.AlignOperands = FormatStyle::OAS_Align; 14681 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 14682 " cccccccccccccccccccc;", 14683 Tab); 14684 // no alignment 14685 verifyFormat("int aaaaaaaaaa =\n" 14686 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 14687 Tab); 14688 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 14689 " : bbbbbbbbbbbbbb ? 222222222222222\n" 14690 " : 333333333333333;", 14691 Tab); 14692 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14693 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 14694 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 14695 " + cccccccccccccccccccc;", 14696 Tab); 14697 } 14698 14699 TEST_F(FormatTest, ZeroTabWidth) { 14700 FormatStyle Tab = getLLVMStyleWithColumns(42); 14701 Tab.IndentWidth = 8; 14702 Tab.UseTab = FormatStyle::UT_Never; 14703 Tab.TabWidth = 0; 14704 EXPECT_EQ("void a(){\n" 14705 " // line starts with '\t'\n" 14706 "};", 14707 format("void a(){\n" 14708 "\t// line starts with '\t'\n" 14709 "};", 14710 Tab)); 14711 14712 EXPECT_EQ("void a(){\n" 14713 " // line starts with '\t'\n" 14714 "};", 14715 format("void a(){\n" 14716 "\t\t// line starts with '\t'\n" 14717 "};", 14718 Tab)); 14719 14720 Tab.UseTab = FormatStyle::UT_ForIndentation; 14721 EXPECT_EQ("void a(){\n" 14722 " // line starts with '\t'\n" 14723 "};", 14724 format("void a(){\n" 14725 "\t// line starts with '\t'\n" 14726 "};", 14727 Tab)); 14728 14729 EXPECT_EQ("void a(){\n" 14730 " // line starts with '\t'\n" 14731 "};", 14732 format("void a(){\n" 14733 "\t\t// line starts with '\t'\n" 14734 "};", 14735 Tab)); 14736 14737 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14738 EXPECT_EQ("void a(){\n" 14739 " // line starts with '\t'\n" 14740 "};", 14741 format("void a(){\n" 14742 "\t// line starts with '\t'\n" 14743 "};", 14744 Tab)); 14745 14746 EXPECT_EQ("void a(){\n" 14747 " // line starts with '\t'\n" 14748 "};", 14749 format("void a(){\n" 14750 "\t\t// line starts with '\t'\n" 14751 "};", 14752 Tab)); 14753 14754 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14755 EXPECT_EQ("void a(){\n" 14756 " // line starts with '\t'\n" 14757 "};", 14758 format("void a(){\n" 14759 "\t// line starts with '\t'\n" 14760 "};", 14761 Tab)); 14762 14763 EXPECT_EQ("void a(){\n" 14764 " // line starts with '\t'\n" 14765 "};", 14766 format("void a(){\n" 14767 "\t\t// line starts with '\t'\n" 14768 "};", 14769 Tab)); 14770 14771 Tab.UseTab = FormatStyle::UT_Always; 14772 EXPECT_EQ("void a(){\n" 14773 "// line starts with '\t'\n" 14774 "};", 14775 format("void a(){\n" 14776 "\t// line starts with '\t'\n" 14777 "};", 14778 Tab)); 14779 14780 EXPECT_EQ("void a(){\n" 14781 "// line starts with '\t'\n" 14782 "};", 14783 format("void a(){\n" 14784 "\t\t// line starts with '\t'\n" 14785 "};", 14786 Tab)); 14787 } 14788 14789 TEST_F(FormatTest, CalculatesOriginalColumn) { 14790 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14791 "q\"; /* some\n" 14792 " comment */", 14793 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14794 "q\"; /* some\n" 14795 " comment */", 14796 getLLVMStyle())); 14797 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14798 "/* some\n" 14799 " comment */", 14800 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14801 " /* some\n" 14802 " comment */", 14803 getLLVMStyle())); 14804 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14805 "qqq\n" 14806 "/* some\n" 14807 " comment */", 14808 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14809 "qqq\n" 14810 " /* some\n" 14811 " comment */", 14812 getLLVMStyle())); 14813 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14814 "wwww; /* some\n" 14815 " comment */", 14816 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14817 "wwww; /* some\n" 14818 " comment */", 14819 getLLVMStyle())); 14820 } 14821 14822 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 14823 FormatStyle NoSpace = getLLVMStyle(); 14824 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 14825 14826 verifyFormat("while(true)\n" 14827 " continue;", 14828 NoSpace); 14829 verifyFormat("for(;;)\n" 14830 " continue;", 14831 NoSpace); 14832 verifyFormat("if(true)\n" 14833 " f();\n" 14834 "else if(true)\n" 14835 " f();", 14836 NoSpace); 14837 verifyFormat("do {\n" 14838 " do_something();\n" 14839 "} while(something());", 14840 NoSpace); 14841 verifyFormat("switch(x) {\n" 14842 "default:\n" 14843 " break;\n" 14844 "}", 14845 NoSpace); 14846 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14847 verifyFormat("size_t x = sizeof(x);", NoSpace); 14848 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14849 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14850 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14851 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14852 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14853 verifyFormat("alignas(128) char a[128];", NoSpace); 14854 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14855 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14856 verifyFormat("int f() throw(Deprecated);", NoSpace); 14857 verifyFormat("typedef void (*cb)(int);", NoSpace); 14858 verifyFormat("T A::operator()();", NoSpace); 14859 verifyFormat("X A::operator++(T);", NoSpace); 14860 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14861 14862 FormatStyle Space = getLLVMStyle(); 14863 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14864 14865 verifyFormat("int f ();", Space); 14866 verifyFormat("void f (int a, T b) {\n" 14867 " while (true)\n" 14868 " continue;\n" 14869 "}", 14870 Space); 14871 verifyFormat("if (true)\n" 14872 " f ();\n" 14873 "else if (true)\n" 14874 " f ();", 14875 Space); 14876 verifyFormat("do {\n" 14877 " do_something ();\n" 14878 "} while (something ());", 14879 Space); 14880 verifyFormat("switch (x) {\n" 14881 "default:\n" 14882 " break;\n" 14883 "}", 14884 Space); 14885 verifyFormat("A::A () : a (1) {}", Space); 14886 verifyFormat("void f () __attribute__ ((asdf));", Space); 14887 verifyFormat("*(&a + 1);\n" 14888 "&((&a)[1]);\n" 14889 "a[(b + c) * d];\n" 14890 "(((a + 1) * 2) + 3) * 4;", 14891 Space); 14892 verifyFormat("#define A(x) x", Space); 14893 verifyFormat("#define A (x) x", Space); 14894 verifyFormat("#if defined(x)\n" 14895 "#endif", 14896 Space); 14897 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14898 verifyFormat("size_t x = sizeof (x);", Space); 14899 verifyFormat("auto f (int x) -> decltype (x);", Space); 14900 verifyFormat("auto f (int x) -> typeof (x);", Space); 14901 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14902 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14903 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14904 verifyFormat("alignas (128) char a[128];", Space); 14905 verifyFormat("size_t x = alignof (MyType);", Space); 14906 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14907 verifyFormat("int f () throw (Deprecated);", Space); 14908 verifyFormat("typedef void (*cb) (int);", Space); 14909 // FIXME these tests regressed behaviour. 14910 // verifyFormat("T A::operator() ();", Space); 14911 // verifyFormat("X A::operator++ (T);", Space); 14912 verifyFormat("auto lambda = [] () { return 0; };", Space); 14913 verifyFormat("int x = int (y);", Space); 14914 14915 FormatStyle SomeSpace = getLLVMStyle(); 14916 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14917 14918 verifyFormat("[]() -> float {}", SomeSpace); 14919 verifyFormat("[] (auto foo) {}", SomeSpace); 14920 verifyFormat("[foo]() -> int {}", SomeSpace); 14921 verifyFormat("int f();", SomeSpace); 14922 verifyFormat("void f (int a, T b) {\n" 14923 " while (true)\n" 14924 " continue;\n" 14925 "}", 14926 SomeSpace); 14927 verifyFormat("if (true)\n" 14928 " f();\n" 14929 "else if (true)\n" 14930 " f();", 14931 SomeSpace); 14932 verifyFormat("do {\n" 14933 " do_something();\n" 14934 "} while (something());", 14935 SomeSpace); 14936 verifyFormat("switch (x) {\n" 14937 "default:\n" 14938 " break;\n" 14939 "}", 14940 SomeSpace); 14941 verifyFormat("A::A() : a (1) {}", SomeSpace); 14942 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14943 verifyFormat("*(&a + 1);\n" 14944 "&((&a)[1]);\n" 14945 "a[(b + c) * d];\n" 14946 "(((a + 1) * 2) + 3) * 4;", 14947 SomeSpace); 14948 verifyFormat("#define A(x) x", SomeSpace); 14949 verifyFormat("#define A (x) x", SomeSpace); 14950 verifyFormat("#if defined(x)\n" 14951 "#endif", 14952 SomeSpace); 14953 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14954 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14955 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14956 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14957 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14958 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14959 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14960 verifyFormat("alignas (128) char a[128];", SomeSpace); 14961 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14962 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14963 SomeSpace); 14964 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14965 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14966 verifyFormat("T A::operator()();", SomeSpace); 14967 // FIXME these tests regressed behaviour. 14968 // verifyFormat("X A::operator++ (T);", SomeSpace); 14969 verifyFormat("int x = int (y);", SomeSpace); 14970 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14971 14972 FormatStyle SpaceControlStatements = getLLVMStyle(); 14973 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14974 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 14975 14976 verifyFormat("while (true)\n" 14977 " continue;", 14978 SpaceControlStatements); 14979 verifyFormat("if (true)\n" 14980 " f();\n" 14981 "else if (true)\n" 14982 " f();", 14983 SpaceControlStatements); 14984 verifyFormat("for (;;) {\n" 14985 " do_something();\n" 14986 "}", 14987 SpaceControlStatements); 14988 verifyFormat("do {\n" 14989 " do_something();\n" 14990 "} while (something());", 14991 SpaceControlStatements); 14992 verifyFormat("switch (x) {\n" 14993 "default:\n" 14994 " break;\n" 14995 "}", 14996 SpaceControlStatements); 14997 14998 FormatStyle SpaceFuncDecl = getLLVMStyle(); 14999 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15000 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 15001 15002 verifyFormat("int f ();", SpaceFuncDecl); 15003 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 15004 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 15005 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 15006 verifyFormat("#define A(x) x", SpaceFuncDecl); 15007 verifyFormat("#define A (x) x", SpaceFuncDecl); 15008 verifyFormat("#if defined(x)\n" 15009 "#endif", 15010 SpaceFuncDecl); 15011 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 15012 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 15013 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 15014 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 15015 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 15016 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 15017 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 15018 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 15019 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 15020 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 15021 SpaceFuncDecl); 15022 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 15023 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 15024 // FIXME these tests regressed behaviour. 15025 // verifyFormat("T A::operator() ();", SpaceFuncDecl); 15026 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 15027 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 15028 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 15029 verifyFormat("int x = int(y);", SpaceFuncDecl); 15030 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 15031 SpaceFuncDecl); 15032 15033 FormatStyle SpaceFuncDef = getLLVMStyle(); 15034 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15035 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 15036 15037 verifyFormat("int f();", SpaceFuncDef); 15038 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 15039 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 15040 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 15041 verifyFormat("#define A(x) x", SpaceFuncDef); 15042 verifyFormat("#define A (x) x", SpaceFuncDef); 15043 verifyFormat("#if defined(x)\n" 15044 "#endif", 15045 SpaceFuncDef); 15046 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 15047 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 15048 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 15049 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 15050 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 15051 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 15052 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 15053 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 15054 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 15055 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 15056 SpaceFuncDef); 15057 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 15058 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 15059 verifyFormat("T A::operator()();", SpaceFuncDef); 15060 verifyFormat("X A::operator++(T);", SpaceFuncDef); 15061 // verifyFormat("T A::operator() () {}", SpaceFuncDef); 15062 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 15063 verifyFormat("int x = int(y);", SpaceFuncDef); 15064 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 15065 SpaceFuncDef); 15066 15067 FormatStyle SpaceIfMacros = getLLVMStyle(); 15068 SpaceIfMacros.IfMacros.clear(); 15069 SpaceIfMacros.IfMacros.push_back("MYIF"); 15070 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15071 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 15072 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 15073 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 15074 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 15075 15076 FormatStyle SpaceForeachMacros = getLLVMStyle(); 15077 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine, 15078 FormatStyle::SBS_Never); 15079 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false); 15080 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15081 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 15082 verifyFormat("for (;;) {\n" 15083 "}", 15084 SpaceForeachMacros); 15085 verifyFormat("foreach (Item *item, itemlist) {\n" 15086 "}", 15087 SpaceForeachMacros); 15088 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n" 15089 "}", 15090 SpaceForeachMacros); 15091 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n" 15092 "}", 15093 SpaceForeachMacros); 15094 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 15095 15096 FormatStyle SomeSpace2 = getLLVMStyle(); 15097 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15098 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 15099 verifyFormat("[]() -> float {}", SomeSpace2); 15100 verifyFormat("[] (auto foo) {}", SomeSpace2); 15101 verifyFormat("[foo]() -> int {}", SomeSpace2); 15102 verifyFormat("int f();", SomeSpace2); 15103 verifyFormat("void f (int a, T b) {\n" 15104 " while (true)\n" 15105 " continue;\n" 15106 "}", 15107 SomeSpace2); 15108 verifyFormat("if (true)\n" 15109 " f();\n" 15110 "else if (true)\n" 15111 " f();", 15112 SomeSpace2); 15113 verifyFormat("do {\n" 15114 " do_something();\n" 15115 "} while (something());", 15116 SomeSpace2); 15117 verifyFormat("switch (x) {\n" 15118 "default:\n" 15119 " break;\n" 15120 "}", 15121 SomeSpace2); 15122 verifyFormat("A::A() : a (1) {}", SomeSpace2); 15123 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 15124 verifyFormat("*(&a + 1);\n" 15125 "&((&a)[1]);\n" 15126 "a[(b + c) * d];\n" 15127 "(((a + 1) * 2) + 3) * 4;", 15128 SomeSpace2); 15129 verifyFormat("#define A(x) x", SomeSpace2); 15130 verifyFormat("#define A (x) x", SomeSpace2); 15131 verifyFormat("#if defined(x)\n" 15132 "#endif", 15133 SomeSpace2); 15134 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 15135 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 15136 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 15137 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 15138 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 15139 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 15140 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 15141 verifyFormat("alignas (128) char a[128];", SomeSpace2); 15142 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 15143 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 15144 SomeSpace2); 15145 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 15146 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 15147 verifyFormat("T A::operator()();", SomeSpace2); 15148 // verifyFormat("X A::operator++ (T);", SomeSpace2); 15149 verifyFormat("int x = int (y);", SomeSpace2); 15150 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 15151 15152 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle(); 15153 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15154 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 15155 .AfterOverloadedOperator = true; 15156 15157 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator); 15158 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator); 15159 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator); 15160 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 15161 15162 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 15163 .AfterOverloadedOperator = false; 15164 15165 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator); 15166 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator); 15167 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator); 15168 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 15169 15170 auto SpaceAfterRequires = getLLVMStyle(); 15171 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom; 15172 EXPECT_FALSE( 15173 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause); 15174 EXPECT_FALSE( 15175 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression); 15176 verifyFormat("void f(auto x)\n" 15177 " requires requires(int i) { x + i; }\n" 15178 "{}", 15179 SpaceAfterRequires); 15180 verifyFormat("void f(auto x)\n" 15181 " requires(requires(int i) { x + i; })\n" 15182 "{}", 15183 SpaceAfterRequires); 15184 verifyFormat("if (requires(int i) { x + i; })\n" 15185 " return;", 15186 SpaceAfterRequires); 15187 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires); 15188 verifyFormat("template <typename T>\n" 15189 " requires(Foo<T>)\n" 15190 "class Bar;", 15191 SpaceAfterRequires); 15192 15193 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true; 15194 verifyFormat("void f(auto x)\n" 15195 " requires requires(int i) { x + i; }\n" 15196 "{}", 15197 SpaceAfterRequires); 15198 verifyFormat("void f(auto x)\n" 15199 " requires (requires(int i) { x + i; })\n" 15200 "{}", 15201 SpaceAfterRequires); 15202 verifyFormat("if (requires(int i) { x + i; })\n" 15203 " return;", 15204 SpaceAfterRequires); 15205 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires); 15206 verifyFormat("template <typename T>\n" 15207 " requires (Foo<T>)\n" 15208 "class Bar;", 15209 SpaceAfterRequires); 15210 15211 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false; 15212 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true; 15213 verifyFormat("void f(auto x)\n" 15214 " requires requires (int i) { x + i; }\n" 15215 "{}", 15216 SpaceAfterRequires); 15217 verifyFormat("void f(auto x)\n" 15218 " requires(requires (int i) { x + i; })\n" 15219 "{}", 15220 SpaceAfterRequires); 15221 verifyFormat("if (requires (int i) { x + i; })\n" 15222 " return;", 15223 SpaceAfterRequires); 15224 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires); 15225 verifyFormat("template <typename T>\n" 15226 " requires(Foo<T>)\n" 15227 "class Bar;", 15228 SpaceAfterRequires); 15229 15230 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true; 15231 verifyFormat("void f(auto x)\n" 15232 " requires requires (int i) { x + i; }\n" 15233 "{}", 15234 SpaceAfterRequires); 15235 verifyFormat("void f(auto x)\n" 15236 " requires (requires (int i) { x + i; })\n" 15237 "{}", 15238 SpaceAfterRequires); 15239 verifyFormat("if (requires (int i) { x + i; })\n" 15240 " return;", 15241 SpaceAfterRequires); 15242 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires); 15243 verifyFormat("template <typename T>\n" 15244 " requires (Foo<T>)\n" 15245 "class Bar;", 15246 SpaceAfterRequires); 15247 } 15248 15249 TEST_F(FormatTest, SpaceAfterLogicalNot) { 15250 FormatStyle Spaces = getLLVMStyle(); 15251 Spaces.SpaceAfterLogicalNot = true; 15252 15253 verifyFormat("bool x = ! y", Spaces); 15254 verifyFormat("if (! isFailure())", Spaces); 15255 verifyFormat("if (! (a && b))", Spaces); 15256 verifyFormat("\"Error!\"", Spaces); 15257 verifyFormat("! ! x", Spaces); 15258 } 15259 15260 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 15261 FormatStyle Spaces = getLLVMStyle(); 15262 15263 Spaces.SpacesInParentheses = true; 15264 verifyFormat("do_something( ::globalVar );", Spaces); 15265 verifyFormat("call( x, y, z );", Spaces); 15266 verifyFormat("call();", Spaces); 15267 verifyFormat("std::function<void( int, int )> callback;", Spaces); 15268 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 15269 Spaces); 15270 verifyFormat("while ( (bool)1 )\n" 15271 " continue;", 15272 Spaces); 15273 verifyFormat("for ( ;; )\n" 15274 " continue;", 15275 Spaces); 15276 verifyFormat("if ( true )\n" 15277 " f();\n" 15278 "else if ( true )\n" 15279 " f();", 15280 Spaces); 15281 verifyFormat("do {\n" 15282 " do_something( (int)i );\n" 15283 "} while ( something() );", 15284 Spaces); 15285 verifyFormat("switch ( x ) {\n" 15286 "default:\n" 15287 " break;\n" 15288 "}", 15289 Spaces); 15290 15291 Spaces.SpacesInParentheses = false; 15292 Spaces.SpacesInCStyleCastParentheses = true; 15293 verifyFormat("Type *A = ( Type * )P;", Spaces); 15294 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 15295 verifyFormat("x = ( int32 )y;", Spaces); 15296 verifyFormat("int a = ( int )(2.0f);", Spaces); 15297 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 15298 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 15299 verifyFormat("#define x (( int )-1)", Spaces); 15300 15301 // Run the first set of tests again with: 15302 Spaces.SpacesInParentheses = false; 15303 Spaces.SpaceInEmptyParentheses = true; 15304 Spaces.SpacesInCStyleCastParentheses = true; 15305 verifyFormat("call(x, y, z);", Spaces); 15306 verifyFormat("call( );", Spaces); 15307 verifyFormat("std::function<void(int, int)> callback;", Spaces); 15308 verifyFormat("while (( bool )1)\n" 15309 " continue;", 15310 Spaces); 15311 verifyFormat("for (;;)\n" 15312 " continue;", 15313 Spaces); 15314 verifyFormat("if (true)\n" 15315 " f( );\n" 15316 "else if (true)\n" 15317 " f( );", 15318 Spaces); 15319 verifyFormat("do {\n" 15320 " do_something(( int )i);\n" 15321 "} while (something( ));", 15322 Spaces); 15323 verifyFormat("switch (x) {\n" 15324 "default:\n" 15325 " break;\n" 15326 "}", 15327 Spaces); 15328 15329 // Run the first set of tests again with: 15330 Spaces.SpaceAfterCStyleCast = true; 15331 verifyFormat("call(x, y, z);", Spaces); 15332 verifyFormat("call( );", Spaces); 15333 verifyFormat("std::function<void(int, int)> callback;", Spaces); 15334 verifyFormat("while (( bool ) 1)\n" 15335 " continue;", 15336 Spaces); 15337 verifyFormat("for (;;)\n" 15338 " continue;", 15339 Spaces); 15340 verifyFormat("if (true)\n" 15341 " f( );\n" 15342 "else if (true)\n" 15343 " f( );", 15344 Spaces); 15345 verifyFormat("do {\n" 15346 " do_something(( int ) i);\n" 15347 "} while (something( ));", 15348 Spaces); 15349 verifyFormat("switch (x) {\n" 15350 "default:\n" 15351 " break;\n" 15352 "}", 15353 Spaces); 15354 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces); 15355 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces); 15356 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces); 15357 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); 15358 verifyFormat("bool *y = ( bool * ) (x);", Spaces); 15359 15360 // Run subset of tests again with: 15361 Spaces.SpacesInCStyleCastParentheses = false; 15362 Spaces.SpaceAfterCStyleCast = true; 15363 verifyFormat("while ((bool) 1)\n" 15364 " continue;", 15365 Spaces); 15366 verifyFormat("do {\n" 15367 " do_something((int) i);\n" 15368 "} while (something( ));", 15369 Spaces); 15370 15371 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 15372 verifyFormat("size_t idx = (size_t) a;", Spaces); 15373 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 15374 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 15375 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 15376 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 15377 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 15378 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces); 15379 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces); 15380 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces); 15381 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); 15382 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); 15383 Spaces.ColumnLimit = 80; 15384 Spaces.IndentWidth = 4; 15385 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 15386 verifyFormat("void foo( ) {\n" 15387 " size_t foo = (*(function))(\n" 15388 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 15389 "BarrrrrrrrrrrrLong,\n" 15390 " FoooooooooLooooong);\n" 15391 "}", 15392 Spaces); 15393 Spaces.SpaceAfterCStyleCast = false; 15394 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 15395 verifyFormat("size_t idx = (size_t)a;", Spaces); 15396 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 15397 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 15398 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 15399 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 15400 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 15401 15402 verifyFormat("void foo( ) {\n" 15403 " size_t foo = (*(function))(\n" 15404 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 15405 "BarrrrrrrrrrrrLong,\n" 15406 " FoooooooooLooooong);\n" 15407 "}", 15408 Spaces); 15409 } 15410 15411 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 15412 verifyFormat("int a[5];"); 15413 verifyFormat("a[3] += 42;"); 15414 15415 FormatStyle Spaces = getLLVMStyle(); 15416 Spaces.SpacesInSquareBrackets = true; 15417 // Not lambdas. 15418 verifyFormat("int a[ 5 ];", Spaces); 15419 verifyFormat("a[ 3 ] += 42;", Spaces); 15420 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 15421 verifyFormat("double &operator[](int i) { return 0; }\n" 15422 "int i;", 15423 Spaces); 15424 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 15425 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 15426 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 15427 // Lambdas. 15428 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 15429 verifyFormat("return [ i, args... ] {};", Spaces); 15430 verifyFormat("int foo = [ &bar ]() {};", Spaces); 15431 verifyFormat("int foo = [ = ]() {};", Spaces); 15432 verifyFormat("int foo = [ & ]() {};", Spaces); 15433 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 15434 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 15435 } 15436 15437 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 15438 FormatStyle NoSpaceStyle = getLLVMStyle(); 15439 verifyFormat("int a[5];", NoSpaceStyle); 15440 verifyFormat("a[3] += 42;", NoSpaceStyle); 15441 15442 verifyFormat("int a[1];", NoSpaceStyle); 15443 verifyFormat("int 1 [a];", NoSpaceStyle); 15444 verifyFormat("int a[1][2];", NoSpaceStyle); 15445 verifyFormat("a[7] = 5;", NoSpaceStyle); 15446 verifyFormat("int a = (f())[23];", NoSpaceStyle); 15447 verifyFormat("f([] {})", NoSpaceStyle); 15448 15449 FormatStyle Space = getLLVMStyle(); 15450 Space.SpaceBeforeSquareBrackets = true; 15451 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 15452 verifyFormat("return [i, args...] {};", Space); 15453 15454 verifyFormat("int a [5];", Space); 15455 verifyFormat("a [3] += 42;", Space); 15456 verifyFormat("constexpr char hello []{\"hello\"};", Space); 15457 verifyFormat("double &operator[](int i) { return 0; }\n" 15458 "int i;", 15459 Space); 15460 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 15461 verifyFormat("int i = a [a][a]->f();", Space); 15462 verifyFormat("int i = (*b) [a]->f();", Space); 15463 15464 verifyFormat("int a [1];", Space); 15465 verifyFormat("int 1 [a];", Space); 15466 verifyFormat("int a [1][2];", Space); 15467 verifyFormat("a [7] = 5;", Space); 15468 verifyFormat("int a = (f()) [23];", Space); 15469 verifyFormat("f([] {})", Space); 15470 } 15471 15472 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 15473 verifyFormat("int a = 5;"); 15474 verifyFormat("a += 42;"); 15475 verifyFormat("a or_eq 8;"); 15476 15477 FormatStyle Spaces = getLLVMStyle(); 15478 Spaces.SpaceBeforeAssignmentOperators = false; 15479 verifyFormat("int a= 5;", Spaces); 15480 verifyFormat("a+= 42;", Spaces); 15481 verifyFormat("a or_eq 8;", Spaces); 15482 } 15483 15484 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 15485 verifyFormat("class Foo : public Bar {};"); 15486 verifyFormat("Foo::Foo() : foo(1) {}"); 15487 verifyFormat("for (auto a : b) {\n}"); 15488 verifyFormat("int x = a ? b : c;"); 15489 verifyFormat("{\n" 15490 "label0:\n" 15491 " int x = 0;\n" 15492 "}"); 15493 verifyFormat("switch (x) {\n" 15494 "case 1:\n" 15495 "default:\n" 15496 "}"); 15497 verifyFormat("switch (allBraces) {\n" 15498 "case 1: {\n" 15499 " break;\n" 15500 "}\n" 15501 "case 2: {\n" 15502 " [[fallthrough]];\n" 15503 "}\n" 15504 "default: {\n" 15505 " break;\n" 15506 "}\n" 15507 "}"); 15508 15509 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 15510 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 15511 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 15512 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 15513 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 15514 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 15515 verifyFormat("{\n" 15516 "label1:\n" 15517 " int x = 0;\n" 15518 "}", 15519 CtorInitializerStyle); 15520 verifyFormat("switch (x) {\n" 15521 "case 1:\n" 15522 "default:\n" 15523 "}", 15524 CtorInitializerStyle); 15525 verifyFormat("switch (allBraces) {\n" 15526 "case 1: {\n" 15527 " break;\n" 15528 "}\n" 15529 "case 2: {\n" 15530 " [[fallthrough]];\n" 15531 "}\n" 15532 "default: {\n" 15533 " break;\n" 15534 "}\n" 15535 "}", 15536 CtorInitializerStyle); 15537 CtorInitializerStyle.BreakConstructorInitializers = 15538 FormatStyle::BCIS_AfterColon; 15539 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 15540 " aaaaaaaaaaaaaaaa(1),\n" 15541 " bbbbbbbbbbbbbbbb(2) {}", 15542 CtorInitializerStyle); 15543 CtorInitializerStyle.BreakConstructorInitializers = 15544 FormatStyle::BCIS_BeforeComma; 15545 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15546 " : aaaaaaaaaaaaaaaa(1)\n" 15547 " , bbbbbbbbbbbbbbbb(2) {}", 15548 CtorInitializerStyle); 15549 CtorInitializerStyle.BreakConstructorInitializers = 15550 FormatStyle::BCIS_BeforeColon; 15551 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15552 " : aaaaaaaaaaaaaaaa(1),\n" 15553 " bbbbbbbbbbbbbbbb(2) {}", 15554 CtorInitializerStyle); 15555 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 15556 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15557 ": aaaaaaaaaaaaaaaa(1),\n" 15558 " bbbbbbbbbbbbbbbb(2) {}", 15559 CtorInitializerStyle); 15560 15561 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 15562 InheritanceStyle.SpaceBeforeInheritanceColon = false; 15563 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 15564 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 15565 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 15566 verifyFormat("int x = a ? b : c;", InheritanceStyle); 15567 verifyFormat("{\n" 15568 "label2:\n" 15569 " int x = 0;\n" 15570 "}", 15571 InheritanceStyle); 15572 verifyFormat("switch (x) {\n" 15573 "case 1:\n" 15574 "default:\n" 15575 "}", 15576 InheritanceStyle); 15577 verifyFormat("switch (allBraces) {\n" 15578 "case 1: {\n" 15579 " break;\n" 15580 "}\n" 15581 "case 2: {\n" 15582 " [[fallthrough]];\n" 15583 "}\n" 15584 "default: {\n" 15585 " break;\n" 15586 "}\n" 15587 "}", 15588 InheritanceStyle); 15589 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 15590 verifyFormat("class Foooooooooooooooooooooo\n" 15591 " : public aaaaaaaaaaaaaaaaaa,\n" 15592 " public bbbbbbbbbbbbbbbbbb {\n" 15593 "}", 15594 InheritanceStyle); 15595 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 15596 verifyFormat("class Foooooooooooooooooooooo:\n" 15597 " public aaaaaaaaaaaaaaaaaa,\n" 15598 " public bbbbbbbbbbbbbbbbbb {\n" 15599 "}", 15600 InheritanceStyle); 15601 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 15602 verifyFormat("class Foooooooooooooooooooooo\n" 15603 " : public aaaaaaaaaaaaaaaaaa\n" 15604 " , public bbbbbbbbbbbbbbbbbb {\n" 15605 "}", 15606 InheritanceStyle); 15607 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 15608 verifyFormat("class Foooooooooooooooooooooo\n" 15609 " : public aaaaaaaaaaaaaaaaaa,\n" 15610 " public bbbbbbbbbbbbbbbbbb {\n" 15611 "}", 15612 InheritanceStyle); 15613 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 15614 verifyFormat("class Foooooooooooooooooooooo\n" 15615 ": public aaaaaaaaaaaaaaaaaa,\n" 15616 " public bbbbbbbbbbbbbbbbbb {}", 15617 InheritanceStyle); 15618 15619 FormatStyle ForLoopStyle = getLLVMStyle(); 15620 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 15621 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 15622 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 15623 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 15624 verifyFormat("int x = a ? b : c;", ForLoopStyle); 15625 verifyFormat("{\n" 15626 "label2:\n" 15627 " int x = 0;\n" 15628 "}", 15629 ForLoopStyle); 15630 verifyFormat("switch (x) {\n" 15631 "case 1:\n" 15632 "default:\n" 15633 "}", 15634 ForLoopStyle); 15635 verifyFormat("switch (allBraces) {\n" 15636 "case 1: {\n" 15637 " break;\n" 15638 "}\n" 15639 "case 2: {\n" 15640 " [[fallthrough]];\n" 15641 "}\n" 15642 "default: {\n" 15643 " break;\n" 15644 "}\n" 15645 "}", 15646 ForLoopStyle); 15647 15648 FormatStyle CaseStyle = getLLVMStyle(); 15649 CaseStyle.SpaceBeforeCaseColon = true; 15650 verifyFormat("class Foo : public Bar {};", CaseStyle); 15651 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 15652 verifyFormat("for (auto a : b) {\n}", CaseStyle); 15653 verifyFormat("int x = a ? b : c;", CaseStyle); 15654 verifyFormat("switch (x) {\n" 15655 "case 1 :\n" 15656 "default :\n" 15657 "}", 15658 CaseStyle); 15659 verifyFormat("switch (allBraces) {\n" 15660 "case 1 : {\n" 15661 " break;\n" 15662 "}\n" 15663 "case 2 : {\n" 15664 " [[fallthrough]];\n" 15665 "}\n" 15666 "default : {\n" 15667 " break;\n" 15668 "}\n" 15669 "}", 15670 CaseStyle); 15671 15672 FormatStyle NoSpaceStyle = getLLVMStyle(); 15673 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 15674 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15675 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 15676 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15677 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 15678 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 15679 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 15680 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 15681 verifyFormat("{\n" 15682 "label3:\n" 15683 " int x = 0;\n" 15684 "}", 15685 NoSpaceStyle); 15686 verifyFormat("switch (x) {\n" 15687 "case 1:\n" 15688 "default:\n" 15689 "}", 15690 NoSpaceStyle); 15691 verifyFormat("switch (allBraces) {\n" 15692 "case 1: {\n" 15693 " break;\n" 15694 "}\n" 15695 "case 2: {\n" 15696 " [[fallthrough]];\n" 15697 "}\n" 15698 "default: {\n" 15699 " break;\n" 15700 "}\n" 15701 "}", 15702 NoSpaceStyle); 15703 15704 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 15705 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 15706 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15707 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 15708 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15709 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 15710 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 15711 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 15712 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 15713 verifyFormat("{\n" 15714 "label3:\n" 15715 " int x = 0;\n" 15716 "}", 15717 InvertedSpaceStyle); 15718 verifyFormat("switch (x) {\n" 15719 "case 1 :\n" 15720 "case 2 : {\n" 15721 " break;\n" 15722 "}\n" 15723 "default :\n" 15724 " break;\n" 15725 "}", 15726 InvertedSpaceStyle); 15727 verifyFormat("switch (allBraces) {\n" 15728 "case 1 : {\n" 15729 " break;\n" 15730 "}\n" 15731 "case 2 : {\n" 15732 " [[fallthrough]];\n" 15733 "}\n" 15734 "default : {\n" 15735 " break;\n" 15736 "}\n" 15737 "}", 15738 InvertedSpaceStyle); 15739 } 15740 15741 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 15742 FormatStyle Style = getLLVMStyle(); 15743 15744 Style.PointerAlignment = FormatStyle::PAS_Left; 15745 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15746 verifyFormat("void* const* x = NULL;", Style); 15747 15748 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 15749 do { \ 15750 Style.PointerAlignment = FormatStyle::Pointers; \ 15751 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 15752 verifyFormat(Code, Style); \ 15753 } while (false) 15754 15755 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 15756 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 15757 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 15758 15759 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 15760 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 15761 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 15762 15763 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 15764 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 15765 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 15766 15767 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 15768 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 15769 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 15770 15771 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 15772 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15773 SAPQ_Default); 15774 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15775 SAPQ_Default); 15776 15777 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 15778 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15779 SAPQ_Before); 15780 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15781 SAPQ_Before); 15782 15783 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 15784 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 15785 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15786 SAPQ_After); 15787 15788 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 15789 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 15790 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 15791 15792 #undef verifyQualifierSpaces 15793 15794 FormatStyle Spaces = getLLVMStyle(); 15795 Spaces.AttributeMacros.push_back("qualified"); 15796 Spaces.PointerAlignment = FormatStyle::PAS_Right; 15797 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15798 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 15799 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 15800 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 15801 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 15802 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15803 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15804 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 15805 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 15806 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15807 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15808 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15809 15810 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 15811 Spaces.PointerAlignment = FormatStyle::PAS_Left; 15812 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15813 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 15814 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 15815 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 15816 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 15817 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15818 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 15819 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15820 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 15821 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 15822 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 15823 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 15824 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15825 15826 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 15827 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 15828 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15829 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 15830 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 15831 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15832 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15833 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15834 } 15835 15836 TEST_F(FormatTest, AlignConsecutiveMacros) { 15837 FormatStyle Style = getLLVMStyle(); 15838 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15839 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15840 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15841 15842 verifyFormat("#define a 3\n" 15843 "#define bbbb 4\n" 15844 "#define ccc (5)", 15845 Style); 15846 15847 verifyFormat("#define f(x) (x * x)\n" 15848 "#define fff(x, y, z) (x * y + z)\n" 15849 "#define ffff(x, y) (x - y)", 15850 Style); 15851 15852 verifyFormat("#define foo(x, y) (x + y)\n" 15853 "#define bar (5, 6)(2 + 2)", 15854 Style); 15855 15856 verifyFormat("#define a 3\n" 15857 "#define bbbb 4\n" 15858 "#define ccc (5)\n" 15859 "#define f(x) (x * x)\n" 15860 "#define fff(x, y, z) (x * y + z)\n" 15861 "#define ffff(x, y) (x - y)", 15862 Style); 15863 15864 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15865 verifyFormat("#define a 3\n" 15866 "#define bbbb 4\n" 15867 "#define ccc (5)", 15868 Style); 15869 15870 verifyFormat("#define f(x) (x * x)\n" 15871 "#define fff(x, y, z) (x * y + z)\n" 15872 "#define ffff(x, y) (x - y)", 15873 Style); 15874 15875 verifyFormat("#define foo(x, y) (x + y)\n" 15876 "#define bar (5, 6)(2 + 2)", 15877 Style); 15878 15879 verifyFormat("#define a 3\n" 15880 "#define bbbb 4\n" 15881 "#define ccc (5)\n" 15882 "#define f(x) (x * x)\n" 15883 "#define fff(x, y, z) (x * y + z)\n" 15884 "#define ffff(x, y) (x - y)", 15885 Style); 15886 15887 verifyFormat("#define a 5\n" 15888 "#define foo(x, y) (x + y)\n" 15889 "#define CCC (6)\n" 15890 "auto lambda = []() {\n" 15891 " auto ii = 0;\n" 15892 " float j = 0;\n" 15893 " return 0;\n" 15894 "};\n" 15895 "int i = 0;\n" 15896 "float i2 = 0;\n" 15897 "auto v = type{\n" 15898 " i = 1, //\n" 15899 " (i = 2), //\n" 15900 " i = 3 //\n" 15901 "};", 15902 Style); 15903 15904 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15905 Style.ColumnLimit = 20; 15906 15907 verifyFormat("#define a \\\n" 15908 " \"aabbbbbbbbbbbb\"\n" 15909 "#define D \\\n" 15910 " \"aabbbbbbbbbbbb\" \\\n" 15911 " \"ccddeeeeeeeee\"\n" 15912 "#define B \\\n" 15913 " \"QQQQQQQQQQQQQ\" \\\n" 15914 " \"FFFFFFFFFFFFF\" \\\n" 15915 " \"LLLLLLLL\"\n", 15916 Style); 15917 15918 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15919 verifyFormat("#define a \\\n" 15920 " \"aabbbbbbbbbbbb\"\n" 15921 "#define D \\\n" 15922 " \"aabbbbbbbbbbbb\" \\\n" 15923 " \"ccddeeeeeeeee\"\n" 15924 "#define B \\\n" 15925 " \"QQQQQQQQQQQQQ\" \\\n" 15926 " \"FFFFFFFFFFFFF\" \\\n" 15927 " \"LLLLLLLL\"\n", 15928 Style); 15929 15930 // Test across comments 15931 Style.MaxEmptyLinesToKeep = 10; 15932 Style.ReflowComments = false; 15933 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 15934 EXPECT_EQ("#define a 3\n" 15935 "// line comment\n" 15936 "#define bbbb 4\n" 15937 "#define ccc (5)", 15938 format("#define a 3\n" 15939 "// line comment\n" 15940 "#define bbbb 4\n" 15941 "#define ccc (5)", 15942 Style)); 15943 15944 EXPECT_EQ("#define a 3\n" 15945 "/* block comment */\n" 15946 "#define bbbb 4\n" 15947 "#define ccc (5)", 15948 format("#define a 3\n" 15949 "/* block comment */\n" 15950 "#define bbbb 4\n" 15951 "#define ccc (5)", 15952 Style)); 15953 15954 EXPECT_EQ("#define a 3\n" 15955 "/* multi-line *\n" 15956 " * block comment */\n" 15957 "#define bbbb 4\n" 15958 "#define ccc (5)", 15959 format("#define a 3\n" 15960 "/* multi-line *\n" 15961 " * block comment */\n" 15962 "#define bbbb 4\n" 15963 "#define ccc (5)", 15964 Style)); 15965 15966 EXPECT_EQ("#define a 3\n" 15967 "// multi-line line comment\n" 15968 "//\n" 15969 "#define bbbb 4\n" 15970 "#define ccc (5)", 15971 format("#define a 3\n" 15972 "// multi-line line comment\n" 15973 "//\n" 15974 "#define bbbb 4\n" 15975 "#define ccc (5)", 15976 Style)); 15977 15978 EXPECT_EQ("#define a 3\n" 15979 "// empty lines still break.\n" 15980 "\n" 15981 "#define bbbb 4\n" 15982 "#define ccc (5)", 15983 format("#define a 3\n" 15984 "// empty lines still break.\n" 15985 "\n" 15986 "#define bbbb 4\n" 15987 "#define ccc (5)", 15988 Style)); 15989 15990 // Test across empty lines 15991 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 15992 EXPECT_EQ("#define a 3\n" 15993 "\n" 15994 "#define bbbb 4\n" 15995 "#define ccc (5)", 15996 format("#define a 3\n" 15997 "\n" 15998 "#define bbbb 4\n" 15999 "#define ccc (5)", 16000 Style)); 16001 16002 EXPECT_EQ("#define a 3\n" 16003 "\n" 16004 "\n" 16005 "\n" 16006 "#define bbbb 4\n" 16007 "#define ccc (5)", 16008 format("#define a 3\n" 16009 "\n" 16010 "\n" 16011 "\n" 16012 "#define bbbb 4\n" 16013 "#define ccc (5)", 16014 Style)); 16015 16016 EXPECT_EQ("#define a 3\n" 16017 "// comments should break alignment\n" 16018 "//\n" 16019 "#define bbbb 4\n" 16020 "#define ccc (5)", 16021 format("#define a 3\n" 16022 "// comments should break alignment\n" 16023 "//\n" 16024 "#define bbbb 4\n" 16025 "#define ccc (5)", 16026 Style)); 16027 16028 // Test across empty lines and comments 16029 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 16030 verifyFormat("#define a 3\n" 16031 "\n" 16032 "// line comment\n" 16033 "#define bbbb 4\n" 16034 "#define ccc (5)", 16035 Style); 16036 16037 EXPECT_EQ("#define a 3\n" 16038 "\n" 16039 "\n" 16040 "/* multi-line *\n" 16041 " * block comment */\n" 16042 "\n" 16043 "\n" 16044 "#define bbbb 4\n" 16045 "#define ccc (5)", 16046 format("#define a 3\n" 16047 "\n" 16048 "\n" 16049 "/* multi-line *\n" 16050 " * block comment */\n" 16051 "\n" 16052 "\n" 16053 "#define bbbb 4\n" 16054 "#define ccc (5)", 16055 Style)); 16056 16057 EXPECT_EQ("#define a 3\n" 16058 "\n" 16059 "\n" 16060 "/* multi-line *\n" 16061 " * block comment */\n" 16062 "\n" 16063 "\n" 16064 "#define bbbb 4\n" 16065 "#define ccc (5)", 16066 format("#define a 3\n" 16067 "\n" 16068 "\n" 16069 "/* multi-line *\n" 16070 " * block comment */\n" 16071 "\n" 16072 "\n" 16073 "#define bbbb 4\n" 16074 "#define ccc (5)", 16075 Style)); 16076 } 16077 16078 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 16079 FormatStyle Alignment = getLLVMStyle(); 16080 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16081 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 16082 16083 Alignment.MaxEmptyLinesToKeep = 10; 16084 /* Test alignment across empty lines */ 16085 EXPECT_EQ("int a = 5;\n" 16086 "\n" 16087 "int oneTwoThree = 123;", 16088 format("int a = 5;\n" 16089 "\n" 16090 "int oneTwoThree= 123;", 16091 Alignment)); 16092 EXPECT_EQ("int a = 5;\n" 16093 "int one = 1;\n" 16094 "\n" 16095 "int oneTwoThree = 123;", 16096 format("int a = 5;\n" 16097 "int one = 1;\n" 16098 "\n" 16099 "int oneTwoThree = 123;", 16100 Alignment)); 16101 EXPECT_EQ("int a = 5;\n" 16102 "int one = 1;\n" 16103 "\n" 16104 "int oneTwoThree = 123;\n" 16105 "int oneTwo = 12;", 16106 format("int a = 5;\n" 16107 "int one = 1;\n" 16108 "\n" 16109 "int oneTwoThree = 123;\n" 16110 "int oneTwo = 12;", 16111 Alignment)); 16112 16113 /* Test across comments */ 16114 EXPECT_EQ("int a = 5;\n" 16115 "/* block comment */\n" 16116 "int oneTwoThree = 123;", 16117 format("int a = 5;\n" 16118 "/* block comment */\n" 16119 "int oneTwoThree=123;", 16120 Alignment)); 16121 16122 EXPECT_EQ("int a = 5;\n" 16123 "// line comment\n" 16124 "int oneTwoThree = 123;", 16125 format("int a = 5;\n" 16126 "// line comment\n" 16127 "int oneTwoThree=123;", 16128 Alignment)); 16129 16130 /* Test across comments and newlines */ 16131 EXPECT_EQ("int a = 5;\n" 16132 "\n" 16133 "/* block comment */\n" 16134 "int oneTwoThree = 123;", 16135 format("int a = 5;\n" 16136 "\n" 16137 "/* block comment */\n" 16138 "int oneTwoThree=123;", 16139 Alignment)); 16140 16141 EXPECT_EQ("int a = 5;\n" 16142 "\n" 16143 "// line comment\n" 16144 "int oneTwoThree = 123;", 16145 format("int a = 5;\n" 16146 "\n" 16147 "// line comment\n" 16148 "int oneTwoThree=123;", 16149 Alignment)); 16150 } 16151 16152 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 16153 FormatStyle Alignment = getLLVMStyle(); 16154 Alignment.AlignConsecutiveDeclarations = 16155 FormatStyle::ACS_AcrossEmptyLinesAndComments; 16156 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16157 16158 Alignment.MaxEmptyLinesToKeep = 10; 16159 /* Test alignment across empty lines */ 16160 EXPECT_EQ("int a = 5;\n" 16161 "\n" 16162 "float const oneTwoThree = 123;", 16163 format("int a = 5;\n" 16164 "\n" 16165 "float const oneTwoThree = 123;", 16166 Alignment)); 16167 EXPECT_EQ("int a = 5;\n" 16168 "float const one = 1;\n" 16169 "\n" 16170 "int oneTwoThree = 123;", 16171 format("int a = 5;\n" 16172 "float const one = 1;\n" 16173 "\n" 16174 "int oneTwoThree = 123;", 16175 Alignment)); 16176 16177 /* Test across comments */ 16178 EXPECT_EQ("float const a = 5;\n" 16179 "/* block comment */\n" 16180 "int oneTwoThree = 123;", 16181 format("float const a = 5;\n" 16182 "/* block comment */\n" 16183 "int oneTwoThree=123;", 16184 Alignment)); 16185 16186 EXPECT_EQ("float const a = 5;\n" 16187 "// line comment\n" 16188 "int oneTwoThree = 123;", 16189 format("float const a = 5;\n" 16190 "// line comment\n" 16191 "int oneTwoThree=123;", 16192 Alignment)); 16193 16194 /* Test across comments and newlines */ 16195 EXPECT_EQ("float const a = 5;\n" 16196 "\n" 16197 "/* block comment */\n" 16198 "int oneTwoThree = 123;", 16199 format("float const a = 5;\n" 16200 "\n" 16201 "/* block comment */\n" 16202 "int oneTwoThree=123;", 16203 Alignment)); 16204 16205 EXPECT_EQ("float const a = 5;\n" 16206 "\n" 16207 "// line comment\n" 16208 "int oneTwoThree = 123;", 16209 format("float const a = 5;\n" 16210 "\n" 16211 "// line comment\n" 16212 "int oneTwoThree=123;", 16213 Alignment)); 16214 } 16215 16216 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 16217 FormatStyle Alignment = getLLVMStyle(); 16218 Alignment.AlignConsecutiveBitFields = 16219 FormatStyle::ACS_AcrossEmptyLinesAndComments; 16220 16221 Alignment.MaxEmptyLinesToKeep = 10; 16222 /* Test alignment across empty lines */ 16223 EXPECT_EQ("int a : 5;\n" 16224 "\n" 16225 "int longbitfield : 6;", 16226 format("int a : 5;\n" 16227 "\n" 16228 "int longbitfield : 6;", 16229 Alignment)); 16230 EXPECT_EQ("int a : 5;\n" 16231 "int one : 1;\n" 16232 "\n" 16233 "int longbitfield : 6;", 16234 format("int a : 5;\n" 16235 "int one : 1;\n" 16236 "\n" 16237 "int longbitfield : 6;", 16238 Alignment)); 16239 16240 /* Test across comments */ 16241 EXPECT_EQ("int a : 5;\n" 16242 "/* block comment */\n" 16243 "int longbitfield : 6;", 16244 format("int a : 5;\n" 16245 "/* block comment */\n" 16246 "int longbitfield : 6;", 16247 Alignment)); 16248 EXPECT_EQ("int a : 5;\n" 16249 "int one : 1;\n" 16250 "// line comment\n" 16251 "int longbitfield : 6;", 16252 format("int a : 5;\n" 16253 "int one : 1;\n" 16254 "// line comment\n" 16255 "int longbitfield : 6;", 16256 Alignment)); 16257 16258 /* Test across comments and newlines */ 16259 EXPECT_EQ("int a : 5;\n" 16260 "/* block comment */\n" 16261 "\n" 16262 "int longbitfield : 6;", 16263 format("int a : 5;\n" 16264 "/* block comment */\n" 16265 "\n" 16266 "int longbitfield : 6;", 16267 Alignment)); 16268 EXPECT_EQ("int a : 5;\n" 16269 "int one : 1;\n" 16270 "\n" 16271 "// line comment\n" 16272 "\n" 16273 "int longbitfield : 6;", 16274 format("int a : 5;\n" 16275 "int one : 1;\n" 16276 "\n" 16277 "// line comment \n" 16278 "\n" 16279 "int longbitfield : 6;", 16280 Alignment)); 16281 } 16282 16283 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 16284 FormatStyle Alignment = getLLVMStyle(); 16285 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16286 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 16287 16288 Alignment.MaxEmptyLinesToKeep = 10; 16289 /* Test alignment across empty lines */ 16290 EXPECT_EQ("int a = 5;\n" 16291 "\n" 16292 "int oneTwoThree = 123;", 16293 format("int a = 5;\n" 16294 "\n" 16295 "int oneTwoThree= 123;", 16296 Alignment)); 16297 EXPECT_EQ("int a = 5;\n" 16298 "int one = 1;\n" 16299 "\n" 16300 "int oneTwoThree = 123;", 16301 format("int a = 5;\n" 16302 "int one = 1;\n" 16303 "\n" 16304 "int oneTwoThree = 123;", 16305 Alignment)); 16306 16307 /* Test across comments */ 16308 EXPECT_EQ("int a = 5;\n" 16309 "/* block comment */\n" 16310 "int oneTwoThree = 123;", 16311 format("int a = 5;\n" 16312 "/* block comment */\n" 16313 "int oneTwoThree=123;", 16314 Alignment)); 16315 16316 EXPECT_EQ("int a = 5;\n" 16317 "// line comment\n" 16318 "int oneTwoThree = 123;", 16319 format("int a = 5;\n" 16320 "// line comment\n" 16321 "int oneTwoThree=123;", 16322 Alignment)); 16323 16324 EXPECT_EQ("int a = 5;\n" 16325 "/*\n" 16326 " * multi-line block comment\n" 16327 " */\n" 16328 "int oneTwoThree = 123;", 16329 format("int a = 5;\n" 16330 "/*\n" 16331 " * multi-line block comment\n" 16332 " */\n" 16333 "int oneTwoThree=123;", 16334 Alignment)); 16335 16336 EXPECT_EQ("int a = 5;\n" 16337 "//\n" 16338 "// multi-line line comment\n" 16339 "//\n" 16340 "int oneTwoThree = 123;", 16341 format("int a = 5;\n" 16342 "//\n" 16343 "// multi-line line comment\n" 16344 "//\n" 16345 "int oneTwoThree=123;", 16346 Alignment)); 16347 16348 /* Test across comments and newlines */ 16349 EXPECT_EQ("int a = 5;\n" 16350 "\n" 16351 "/* block comment */\n" 16352 "int oneTwoThree = 123;", 16353 format("int a = 5;\n" 16354 "\n" 16355 "/* block comment */\n" 16356 "int oneTwoThree=123;", 16357 Alignment)); 16358 16359 EXPECT_EQ("int a = 5;\n" 16360 "\n" 16361 "// line comment\n" 16362 "int oneTwoThree = 123;", 16363 format("int a = 5;\n" 16364 "\n" 16365 "// line comment\n" 16366 "int oneTwoThree=123;", 16367 Alignment)); 16368 } 16369 16370 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 16371 FormatStyle Alignment = getLLVMStyle(); 16372 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16373 Alignment.AlignConsecutiveAssignments = 16374 FormatStyle::ACS_AcrossEmptyLinesAndComments; 16375 verifyFormat("int a = 5;\n" 16376 "int oneTwoThree = 123;", 16377 Alignment); 16378 verifyFormat("int a = method();\n" 16379 "int oneTwoThree = 133;", 16380 Alignment); 16381 verifyFormat("a &= 5;\n" 16382 "bcd *= 5;\n" 16383 "ghtyf += 5;\n" 16384 "dvfvdb -= 5;\n" 16385 "a /= 5;\n" 16386 "vdsvsv %= 5;\n" 16387 "sfdbddfbdfbb ^= 5;\n" 16388 "dvsdsv |= 5;\n" 16389 "int dsvvdvsdvvv = 123;", 16390 Alignment); 16391 verifyFormat("int i = 1, j = 10;\n" 16392 "something = 2000;", 16393 Alignment); 16394 verifyFormat("something = 2000;\n" 16395 "int i = 1, j = 10;\n", 16396 Alignment); 16397 verifyFormat("something = 2000;\n" 16398 "another = 911;\n" 16399 "int i = 1, j = 10;\n" 16400 "oneMore = 1;\n" 16401 "i = 2;", 16402 Alignment); 16403 verifyFormat("int a = 5;\n" 16404 "int one = 1;\n" 16405 "method();\n" 16406 "int oneTwoThree = 123;\n" 16407 "int oneTwo = 12;", 16408 Alignment); 16409 verifyFormat("int oneTwoThree = 123;\n" 16410 "int oneTwo = 12;\n" 16411 "method();\n", 16412 Alignment); 16413 verifyFormat("int oneTwoThree = 123; // comment\n" 16414 "int oneTwo = 12; // comment", 16415 Alignment); 16416 16417 // Bug 25167 16418 /* Uncomment when fixed 16419 verifyFormat("#if A\n" 16420 "#else\n" 16421 "int aaaaaaaa = 12;\n" 16422 "#endif\n" 16423 "#if B\n" 16424 "#else\n" 16425 "int a = 12;\n" 16426 "#endif\n", 16427 Alignment); 16428 verifyFormat("enum foo {\n" 16429 "#if A\n" 16430 "#else\n" 16431 " aaaaaaaa = 12;\n" 16432 "#endif\n" 16433 "#if B\n" 16434 "#else\n" 16435 " a = 12;\n" 16436 "#endif\n" 16437 "};\n", 16438 Alignment); 16439 */ 16440 16441 Alignment.MaxEmptyLinesToKeep = 10; 16442 /* Test alignment across empty lines */ 16443 EXPECT_EQ("int a = 5;\n" 16444 "\n" 16445 "int oneTwoThree = 123;", 16446 format("int a = 5;\n" 16447 "\n" 16448 "int oneTwoThree= 123;", 16449 Alignment)); 16450 EXPECT_EQ("int a = 5;\n" 16451 "int one = 1;\n" 16452 "\n" 16453 "int oneTwoThree = 123;", 16454 format("int a = 5;\n" 16455 "int one = 1;\n" 16456 "\n" 16457 "int oneTwoThree = 123;", 16458 Alignment)); 16459 EXPECT_EQ("int a = 5;\n" 16460 "int one = 1;\n" 16461 "\n" 16462 "int oneTwoThree = 123;\n" 16463 "int oneTwo = 12;", 16464 format("int a = 5;\n" 16465 "int one = 1;\n" 16466 "\n" 16467 "int oneTwoThree = 123;\n" 16468 "int oneTwo = 12;", 16469 Alignment)); 16470 16471 /* Test across comments */ 16472 EXPECT_EQ("int a = 5;\n" 16473 "/* block comment */\n" 16474 "int oneTwoThree = 123;", 16475 format("int a = 5;\n" 16476 "/* block comment */\n" 16477 "int oneTwoThree=123;", 16478 Alignment)); 16479 16480 EXPECT_EQ("int a = 5;\n" 16481 "// line comment\n" 16482 "int oneTwoThree = 123;", 16483 format("int a = 5;\n" 16484 "// line comment\n" 16485 "int oneTwoThree=123;", 16486 Alignment)); 16487 16488 /* Test across comments and newlines */ 16489 EXPECT_EQ("int a = 5;\n" 16490 "\n" 16491 "/* block comment */\n" 16492 "int oneTwoThree = 123;", 16493 format("int a = 5;\n" 16494 "\n" 16495 "/* block comment */\n" 16496 "int oneTwoThree=123;", 16497 Alignment)); 16498 16499 EXPECT_EQ("int a = 5;\n" 16500 "\n" 16501 "// line comment\n" 16502 "int oneTwoThree = 123;", 16503 format("int a = 5;\n" 16504 "\n" 16505 "// line comment\n" 16506 "int oneTwoThree=123;", 16507 Alignment)); 16508 16509 EXPECT_EQ("int a = 5;\n" 16510 "//\n" 16511 "// multi-line line comment\n" 16512 "//\n" 16513 "int oneTwoThree = 123;", 16514 format("int a = 5;\n" 16515 "//\n" 16516 "// multi-line line comment\n" 16517 "//\n" 16518 "int oneTwoThree=123;", 16519 Alignment)); 16520 16521 EXPECT_EQ("int a = 5;\n" 16522 "/*\n" 16523 " * multi-line block comment\n" 16524 " */\n" 16525 "int oneTwoThree = 123;", 16526 format("int a = 5;\n" 16527 "/*\n" 16528 " * multi-line block comment\n" 16529 " */\n" 16530 "int oneTwoThree=123;", 16531 Alignment)); 16532 16533 EXPECT_EQ("int a = 5;\n" 16534 "\n" 16535 "/* block comment */\n" 16536 "\n" 16537 "\n" 16538 "\n" 16539 "int oneTwoThree = 123;", 16540 format("int a = 5;\n" 16541 "\n" 16542 "/* block comment */\n" 16543 "\n" 16544 "\n" 16545 "\n" 16546 "int oneTwoThree=123;", 16547 Alignment)); 16548 16549 EXPECT_EQ("int a = 5;\n" 16550 "\n" 16551 "// line comment\n" 16552 "\n" 16553 "\n" 16554 "\n" 16555 "int oneTwoThree = 123;", 16556 format("int a = 5;\n" 16557 "\n" 16558 "// line comment\n" 16559 "\n" 16560 "\n" 16561 "\n" 16562 "int oneTwoThree=123;", 16563 Alignment)); 16564 16565 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16566 verifyFormat("#define A \\\n" 16567 " int aaaa = 12; \\\n" 16568 " int b = 23; \\\n" 16569 " int ccc = 234; \\\n" 16570 " int dddddddddd = 2345;", 16571 Alignment); 16572 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16573 verifyFormat("#define A \\\n" 16574 " int aaaa = 12; \\\n" 16575 " int b = 23; \\\n" 16576 " int ccc = 234; \\\n" 16577 " int dddddddddd = 2345;", 16578 Alignment); 16579 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16580 verifyFormat("#define A " 16581 " \\\n" 16582 " int aaaa = 12; " 16583 " \\\n" 16584 " int b = 23; " 16585 " \\\n" 16586 " int ccc = 234; " 16587 " \\\n" 16588 " int dddddddddd = 2345;", 16589 Alignment); 16590 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16591 "k = 4, int l = 5,\n" 16592 " int m = 6) {\n" 16593 " int j = 10;\n" 16594 " otherThing = 1;\n" 16595 "}", 16596 Alignment); 16597 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16598 " int i = 1;\n" 16599 " int j = 2;\n" 16600 " int big = 10000;\n" 16601 "}", 16602 Alignment); 16603 verifyFormat("class C {\n" 16604 "public:\n" 16605 " int i = 1;\n" 16606 " virtual void f() = 0;\n" 16607 "};", 16608 Alignment); 16609 verifyFormat("int i = 1;\n" 16610 "if (SomeType t = getSomething()) {\n" 16611 "}\n" 16612 "int j = 2;\n" 16613 "int big = 10000;", 16614 Alignment); 16615 verifyFormat("int j = 7;\n" 16616 "for (int k = 0; k < N; ++k) {\n" 16617 "}\n" 16618 "int j = 2;\n" 16619 "int big = 10000;\n" 16620 "}", 16621 Alignment); 16622 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16623 verifyFormat("int i = 1;\n" 16624 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16625 " = someLooooooooooooooooongFunction();\n" 16626 "int j = 2;", 16627 Alignment); 16628 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16629 verifyFormat("int i = 1;\n" 16630 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16631 " someLooooooooooooooooongFunction();\n" 16632 "int j = 2;", 16633 Alignment); 16634 16635 verifyFormat("auto lambda = []() {\n" 16636 " auto i = 0;\n" 16637 " return 0;\n" 16638 "};\n" 16639 "int i = 0;\n" 16640 "auto v = type{\n" 16641 " i = 1, //\n" 16642 " (i = 2), //\n" 16643 " i = 3 //\n" 16644 "};", 16645 Alignment); 16646 16647 verifyFormat( 16648 "int i = 1;\n" 16649 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16650 " loooooooooooooooooooooongParameterB);\n" 16651 "int j = 2;", 16652 Alignment); 16653 16654 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16655 " typename B = very_long_type_name_1,\n" 16656 " typename T_2 = very_long_type_name_2>\n" 16657 "auto foo() {}\n", 16658 Alignment); 16659 verifyFormat("int a, b = 1;\n" 16660 "int c = 2;\n" 16661 "int dd = 3;\n", 16662 Alignment); 16663 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16664 "float b[1][] = {{3.f}};\n", 16665 Alignment); 16666 verifyFormat("for (int i = 0; i < 1; i++)\n" 16667 " int x = 1;\n", 16668 Alignment); 16669 verifyFormat("for (i = 0; i < 1; i++)\n" 16670 " x = 1;\n" 16671 "y = 1;\n", 16672 Alignment); 16673 16674 Alignment.ReflowComments = true; 16675 Alignment.ColumnLimit = 50; 16676 EXPECT_EQ("int x = 0;\n" 16677 "int yy = 1; /// specificlennospace\n" 16678 "int zzz = 2;\n", 16679 format("int x = 0;\n" 16680 "int yy = 1; ///specificlennospace\n" 16681 "int zzz = 2;\n", 16682 Alignment)); 16683 } 16684 16685 TEST_F(FormatTest, AlignConsecutiveAssignments) { 16686 FormatStyle Alignment = getLLVMStyle(); 16687 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16688 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16689 verifyFormat("int a = 5;\n" 16690 "int oneTwoThree = 123;", 16691 Alignment); 16692 verifyFormat("int a = 5;\n" 16693 "int oneTwoThree = 123;", 16694 Alignment); 16695 16696 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16697 verifyFormat("int a = 5;\n" 16698 "int oneTwoThree = 123;", 16699 Alignment); 16700 verifyFormat("int a = method();\n" 16701 "int oneTwoThree = 133;", 16702 Alignment); 16703 verifyFormat("a &= 5;\n" 16704 "bcd *= 5;\n" 16705 "ghtyf += 5;\n" 16706 "dvfvdb -= 5;\n" 16707 "a /= 5;\n" 16708 "vdsvsv %= 5;\n" 16709 "sfdbddfbdfbb ^= 5;\n" 16710 "dvsdsv |= 5;\n" 16711 "int dsvvdvsdvvv = 123;", 16712 Alignment); 16713 verifyFormat("int i = 1, j = 10;\n" 16714 "something = 2000;", 16715 Alignment); 16716 verifyFormat("something = 2000;\n" 16717 "int i = 1, j = 10;\n", 16718 Alignment); 16719 verifyFormat("something = 2000;\n" 16720 "another = 911;\n" 16721 "int i = 1, j = 10;\n" 16722 "oneMore = 1;\n" 16723 "i = 2;", 16724 Alignment); 16725 verifyFormat("int a = 5;\n" 16726 "int one = 1;\n" 16727 "method();\n" 16728 "int oneTwoThree = 123;\n" 16729 "int oneTwo = 12;", 16730 Alignment); 16731 verifyFormat("int oneTwoThree = 123;\n" 16732 "int oneTwo = 12;\n" 16733 "method();\n", 16734 Alignment); 16735 verifyFormat("int oneTwoThree = 123; // comment\n" 16736 "int oneTwo = 12; // comment", 16737 Alignment); 16738 verifyFormat("int f() = default;\n" 16739 "int &operator() = default;\n" 16740 "int &operator=() {", 16741 Alignment); 16742 verifyFormat("int f() = delete;\n" 16743 "int &operator() = delete;\n" 16744 "int &operator=() {", 16745 Alignment); 16746 verifyFormat("int f() = default; // comment\n" 16747 "int &operator() = default; // comment\n" 16748 "int &operator=() {", 16749 Alignment); 16750 verifyFormat("int f() = default;\n" 16751 "int &operator() = default;\n" 16752 "int &operator==() {", 16753 Alignment); 16754 verifyFormat("int f() = default;\n" 16755 "int &operator() = default;\n" 16756 "int &operator<=() {", 16757 Alignment); 16758 verifyFormat("int f() = default;\n" 16759 "int &operator() = default;\n" 16760 "int &operator!=() {", 16761 Alignment); 16762 verifyFormat("int f() = default;\n" 16763 "int &operator() = default;\n" 16764 "int &operator=();", 16765 Alignment); 16766 verifyFormat("int f() = delete;\n" 16767 "int &operator() = delete;\n" 16768 "int &operator=();", 16769 Alignment); 16770 verifyFormat("/* long long padding */ int f() = default;\n" 16771 "int &operator() = default;\n" 16772 "int &operator/**/ =();", 16773 Alignment); 16774 // https://llvm.org/PR33697 16775 FormatStyle AlignmentWithPenalty = getLLVMStyle(); 16776 AlignmentWithPenalty.AlignConsecutiveAssignments = 16777 FormatStyle::ACS_Consecutive; 16778 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000; 16779 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n" 16780 " void f() = delete;\n" 16781 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n" 16782 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n" 16783 "};\n", 16784 AlignmentWithPenalty); 16785 16786 // Bug 25167 16787 /* Uncomment when fixed 16788 verifyFormat("#if A\n" 16789 "#else\n" 16790 "int aaaaaaaa = 12;\n" 16791 "#endif\n" 16792 "#if B\n" 16793 "#else\n" 16794 "int a = 12;\n" 16795 "#endif\n", 16796 Alignment); 16797 verifyFormat("enum foo {\n" 16798 "#if A\n" 16799 "#else\n" 16800 " aaaaaaaa = 12;\n" 16801 "#endif\n" 16802 "#if B\n" 16803 "#else\n" 16804 " a = 12;\n" 16805 "#endif\n" 16806 "};\n", 16807 Alignment); 16808 */ 16809 16810 EXPECT_EQ("int a = 5;\n" 16811 "\n" 16812 "int oneTwoThree = 123;", 16813 format("int a = 5;\n" 16814 "\n" 16815 "int oneTwoThree= 123;", 16816 Alignment)); 16817 EXPECT_EQ("int a = 5;\n" 16818 "int one = 1;\n" 16819 "\n" 16820 "int oneTwoThree = 123;", 16821 format("int a = 5;\n" 16822 "int one = 1;\n" 16823 "\n" 16824 "int oneTwoThree = 123;", 16825 Alignment)); 16826 EXPECT_EQ("int a = 5;\n" 16827 "int one = 1;\n" 16828 "\n" 16829 "int oneTwoThree = 123;\n" 16830 "int oneTwo = 12;", 16831 format("int a = 5;\n" 16832 "int one = 1;\n" 16833 "\n" 16834 "int oneTwoThree = 123;\n" 16835 "int oneTwo = 12;", 16836 Alignment)); 16837 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16838 verifyFormat("#define A \\\n" 16839 " int aaaa = 12; \\\n" 16840 " int b = 23; \\\n" 16841 " int ccc = 234; \\\n" 16842 " int dddddddddd = 2345;", 16843 Alignment); 16844 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16845 verifyFormat("#define A \\\n" 16846 " int aaaa = 12; \\\n" 16847 " int b = 23; \\\n" 16848 " int ccc = 234; \\\n" 16849 " int dddddddddd = 2345;", 16850 Alignment); 16851 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16852 verifyFormat("#define A " 16853 " \\\n" 16854 " int aaaa = 12; " 16855 " \\\n" 16856 " int b = 23; " 16857 " \\\n" 16858 " int ccc = 234; " 16859 " \\\n" 16860 " int dddddddddd = 2345;", 16861 Alignment); 16862 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16863 "k = 4, int l = 5,\n" 16864 " int m = 6) {\n" 16865 " int j = 10;\n" 16866 " otherThing = 1;\n" 16867 "}", 16868 Alignment); 16869 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16870 " int i = 1;\n" 16871 " int j = 2;\n" 16872 " int big = 10000;\n" 16873 "}", 16874 Alignment); 16875 verifyFormat("class C {\n" 16876 "public:\n" 16877 " int i = 1;\n" 16878 " virtual void f() = 0;\n" 16879 "};", 16880 Alignment); 16881 verifyFormat("int i = 1;\n" 16882 "if (SomeType t = getSomething()) {\n" 16883 "}\n" 16884 "int j = 2;\n" 16885 "int big = 10000;", 16886 Alignment); 16887 verifyFormat("int j = 7;\n" 16888 "for (int k = 0; k < N; ++k) {\n" 16889 "}\n" 16890 "int j = 2;\n" 16891 "int big = 10000;\n" 16892 "}", 16893 Alignment); 16894 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16895 verifyFormat("int i = 1;\n" 16896 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16897 " = someLooooooooooooooooongFunction();\n" 16898 "int j = 2;", 16899 Alignment); 16900 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16901 verifyFormat("int i = 1;\n" 16902 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16903 " someLooooooooooooooooongFunction();\n" 16904 "int j = 2;", 16905 Alignment); 16906 16907 verifyFormat("auto lambda = []() {\n" 16908 " auto i = 0;\n" 16909 " return 0;\n" 16910 "};\n" 16911 "int i = 0;\n" 16912 "auto v = type{\n" 16913 " i = 1, //\n" 16914 " (i = 2), //\n" 16915 " i = 3 //\n" 16916 "};", 16917 Alignment); 16918 16919 verifyFormat( 16920 "int i = 1;\n" 16921 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16922 " loooooooooooooooooooooongParameterB);\n" 16923 "int j = 2;", 16924 Alignment); 16925 16926 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16927 " typename B = very_long_type_name_1,\n" 16928 " typename T_2 = very_long_type_name_2>\n" 16929 "auto foo() {}\n", 16930 Alignment); 16931 verifyFormat("int a, b = 1;\n" 16932 "int c = 2;\n" 16933 "int dd = 3;\n", 16934 Alignment); 16935 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16936 "float b[1][] = {{3.f}};\n", 16937 Alignment); 16938 verifyFormat("for (int i = 0; i < 1; i++)\n" 16939 " int x = 1;\n", 16940 Alignment); 16941 verifyFormat("for (i = 0; i < 1; i++)\n" 16942 " x = 1;\n" 16943 "y = 1;\n", 16944 Alignment); 16945 16946 EXPECT_EQ(Alignment.ReflowComments, true); 16947 Alignment.ColumnLimit = 50; 16948 EXPECT_EQ("int x = 0;\n" 16949 "int yy = 1; /// specificlennospace\n" 16950 "int zzz = 2;\n", 16951 format("int x = 0;\n" 16952 "int yy = 1; ///specificlennospace\n" 16953 "int zzz = 2;\n", 16954 Alignment)); 16955 16956 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16957 "auto b = [] {\n" 16958 " f();\n" 16959 " return;\n" 16960 "};", 16961 Alignment); 16962 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16963 "auto b = g([] {\n" 16964 " f();\n" 16965 " return;\n" 16966 "});", 16967 Alignment); 16968 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16969 "auto b = g(param, [] {\n" 16970 " f();\n" 16971 " return;\n" 16972 "});", 16973 Alignment); 16974 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16975 "auto b = [] {\n" 16976 " if (condition) {\n" 16977 " return;\n" 16978 " }\n" 16979 "};", 16980 Alignment); 16981 16982 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 16983 " ccc ? aaaaa : bbbbb,\n" 16984 " dddddddddddddddddddddddddd);", 16985 Alignment); 16986 // FIXME: https://llvm.org/PR53497 16987 // verifyFormat("auto aaaaaaaaaaaa = f();\n" 16988 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 16989 // " ccc ? aaaaa : bbbbb,\n" 16990 // " dddddddddddddddddddddddddd);", 16991 // Alignment); 16992 } 16993 16994 TEST_F(FormatTest, AlignConsecutiveBitFields) { 16995 FormatStyle Alignment = getLLVMStyle(); 16996 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 16997 verifyFormat("int const a : 5;\n" 16998 "int oneTwoThree : 23;", 16999 Alignment); 17000 17001 // Initializers are allowed starting with c++2a 17002 verifyFormat("int const a : 5 = 1;\n" 17003 "int oneTwoThree : 23 = 0;", 17004 Alignment); 17005 17006 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17007 verifyFormat("int const a : 5;\n" 17008 "int oneTwoThree : 23;", 17009 Alignment); 17010 17011 verifyFormat("int const a : 5; // comment\n" 17012 "int oneTwoThree : 23; // comment", 17013 Alignment); 17014 17015 verifyFormat("int const a : 5 = 1;\n" 17016 "int oneTwoThree : 23 = 0;", 17017 Alignment); 17018 17019 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17020 verifyFormat("int const a : 5 = 1;\n" 17021 "int oneTwoThree : 23 = 0;", 17022 Alignment); 17023 verifyFormat("int const a : 5 = {1};\n" 17024 "int oneTwoThree : 23 = 0;", 17025 Alignment); 17026 17027 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 17028 verifyFormat("int const a :5;\n" 17029 "int oneTwoThree:23;", 17030 Alignment); 17031 17032 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 17033 verifyFormat("int const a :5;\n" 17034 "int oneTwoThree :23;", 17035 Alignment); 17036 17037 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 17038 verifyFormat("int const a : 5;\n" 17039 "int oneTwoThree: 23;", 17040 Alignment); 17041 17042 // Known limitations: ':' is only recognized as a bitfield colon when 17043 // followed by a number. 17044 /* 17045 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 17046 "int a : 5;", 17047 Alignment); 17048 */ 17049 } 17050 17051 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 17052 FormatStyle Alignment = getLLVMStyle(); 17053 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 17054 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 17055 Alignment.PointerAlignment = FormatStyle::PAS_Right; 17056 verifyFormat("float const a = 5;\n" 17057 "int oneTwoThree = 123;", 17058 Alignment); 17059 verifyFormat("int a = 5;\n" 17060 "float const oneTwoThree = 123;", 17061 Alignment); 17062 17063 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17064 verifyFormat("float const a = 5;\n" 17065 "int oneTwoThree = 123;", 17066 Alignment); 17067 verifyFormat("int a = method();\n" 17068 "float const oneTwoThree = 133;", 17069 Alignment); 17070 verifyFormat("int i = 1, j = 10;\n" 17071 "something = 2000;", 17072 Alignment); 17073 verifyFormat("something = 2000;\n" 17074 "int i = 1, j = 10;\n", 17075 Alignment); 17076 verifyFormat("float something = 2000;\n" 17077 "double another = 911;\n" 17078 "int i = 1, j = 10;\n" 17079 "const int *oneMore = 1;\n" 17080 "unsigned i = 2;", 17081 Alignment); 17082 verifyFormat("float a = 5;\n" 17083 "int one = 1;\n" 17084 "method();\n" 17085 "const double oneTwoThree = 123;\n" 17086 "const unsigned int oneTwo = 12;", 17087 Alignment); 17088 verifyFormat("int oneTwoThree{0}; // comment\n" 17089 "unsigned oneTwo; // comment", 17090 Alignment); 17091 verifyFormat("unsigned int *a;\n" 17092 "int *b;\n" 17093 "unsigned int Const *c;\n" 17094 "unsigned int const *d;\n" 17095 "unsigned int Const &e;\n" 17096 "unsigned int const &f;", 17097 Alignment); 17098 verifyFormat("Const unsigned int *c;\n" 17099 "const unsigned int *d;\n" 17100 "Const unsigned int &e;\n" 17101 "const unsigned int &f;\n" 17102 "const unsigned g;\n" 17103 "Const unsigned h;", 17104 Alignment); 17105 EXPECT_EQ("float const a = 5;\n" 17106 "\n" 17107 "int oneTwoThree = 123;", 17108 format("float const a = 5;\n" 17109 "\n" 17110 "int oneTwoThree= 123;", 17111 Alignment)); 17112 EXPECT_EQ("float a = 5;\n" 17113 "int one = 1;\n" 17114 "\n" 17115 "unsigned oneTwoThree = 123;", 17116 format("float a = 5;\n" 17117 "int one = 1;\n" 17118 "\n" 17119 "unsigned oneTwoThree = 123;", 17120 Alignment)); 17121 EXPECT_EQ("float a = 5;\n" 17122 "int one = 1;\n" 17123 "\n" 17124 "unsigned oneTwoThree = 123;\n" 17125 "int oneTwo = 12;", 17126 format("float a = 5;\n" 17127 "int one = 1;\n" 17128 "\n" 17129 "unsigned oneTwoThree = 123;\n" 17130 "int oneTwo = 12;", 17131 Alignment)); 17132 // Function prototype alignment 17133 verifyFormat("int a();\n" 17134 "double b();", 17135 Alignment); 17136 verifyFormat("int a(int x);\n" 17137 "double b();", 17138 Alignment); 17139 unsigned OldColumnLimit = Alignment.ColumnLimit; 17140 // We need to set ColumnLimit to zero, in order to stress nested alignments, 17141 // otherwise the function parameters will be re-flowed onto a single line. 17142 Alignment.ColumnLimit = 0; 17143 EXPECT_EQ("int a(int x,\n" 17144 " float y);\n" 17145 "double b(int x,\n" 17146 " double y);", 17147 format("int a(int x,\n" 17148 " float y);\n" 17149 "double b(int x,\n" 17150 " double y);", 17151 Alignment)); 17152 // This ensures that function parameters of function declarations are 17153 // correctly indented when their owning functions are indented. 17154 // The failure case here is for 'double y' to not be indented enough. 17155 EXPECT_EQ("double a(int x);\n" 17156 "int b(int y,\n" 17157 " double z);", 17158 format("double a(int x);\n" 17159 "int b(int y,\n" 17160 " double z);", 17161 Alignment)); 17162 // Set ColumnLimit low so that we induce wrapping immediately after 17163 // the function name and opening paren. 17164 Alignment.ColumnLimit = 13; 17165 verifyFormat("int function(\n" 17166 " int x,\n" 17167 " bool y);", 17168 Alignment); 17169 Alignment.ColumnLimit = OldColumnLimit; 17170 // Ensure function pointers don't screw up recursive alignment 17171 verifyFormat("int a(int x, void (*fp)(int y));\n" 17172 "double b();", 17173 Alignment); 17174 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17175 // Ensure recursive alignment is broken by function braces, so that the 17176 // "a = 1" does not align with subsequent assignments inside the function 17177 // body. 17178 verifyFormat("int func(int a = 1) {\n" 17179 " int b = 2;\n" 17180 " int cc = 3;\n" 17181 "}", 17182 Alignment); 17183 verifyFormat("float something = 2000;\n" 17184 "double another = 911;\n" 17185 "int i = 1, j = 10;\n" 17186 "const int *oneMore = 1;\n" 17187 "unsigned i = 2;", 17188 Alignment); 17189 verifyFormat("int oneTwoThree = {0}; // comment\n" 17190 "unsigned oneTwo = 0; // comment", 17191 Alignment); 17192 // Make sure that scope is correctly tracked, in the absence of braces 17193 verifyFormat("for (int i = 0; i < n; i++)\n" 17194 " j = i;\n" 17195 "double x = 1;\n", 17196 Alignment); 17197 verifyFormat("if (int i = 0)\n" 17198 " j = i;\n" 17199 "double x = 1;\n", 17200 Alignment); 17201 // Ensure operator[] and operator() are comprehended 17202 verifyFormat("struct test {\n" 17203 " long long int foo();\n" 17204 " int operator[](int a);\n" 17205 " double bar();\n" 17206 "};\n", 17207 Alignment); 17208 verifyFormat("struct test {\n" 17209 " long long int foo();\n" 17210 " int operator()(int a);\n" 17211 " double bar();\n" 17212 "};\n", 17213 Alignment); 17214 // http://llvm.org/PR52914 17215 verifyFormat("char *a[] = {\"a\", // comment\n" 17216 " \"bb\"};\n" 17217 "int bbbbbbb = 0;", 17218 Alignment); 17219 17220 // PAS_Right 17221 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17222 " int const i = 1;\n" 17223 " int *j = 2;\n" 17224 " int big = 10000;\n" 17225 "\n" 17226 " unsigned oneTwoThree = 123;\n" 17227 " int oneTwo = 12;\n" 17228 " method();\n" 17229 " float k = 2;\n" 17230 " int ll = 10000;\n" 17231 "}", 17232 format("void SomeFunction(int parameter= 0) {\n" 17233 " int const i= 1;\n" 17234 " int *j=2;\n" 17235 " int big = 10000;\n" 17236 "\n" 17237 "unsigned oneTwoThree =123;\n" 17238 "int oneTwo = 12;\n" 17239 " method();\n" 17240 "float k= 2;\n" 17241 "int ll=10000;\n" 17242 "}", 17243 Alignment)); 17244 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17245 " int const i = 1;\n" 17246 " int **j = 2, ***k;\n" 17247 " int &k = i;\n" 17248 " int &&l = i + j;\n" 17249 " int big = 10000;\n" 17250 "\n" 17251 " unsigned oneTwoThree = 123;\n" 17252 " int oneTwo = 12;\n" 17253 " method();\n" 17254 " float k = 2;\n" 17255 " int ll = 10000;\n" 17256 "}", 17257 format("void SomeFunction(int parameter= 0) {\n" 17258 " int const i= 1;\n" 17259 " int **j=2,***k;\n" 17260 "int &k=i;\n" 17261 "int &&l=i+j;\n" 17262 " int big = 10000;\n" 17263 "\n" 17264 "unsigned oneTwoThree =123;\n" 17265 "int oneTwo = 12;\n" 17266 " method();\n" 17267 "float k= 2;\n" 17268 "int ll=10000;\n" 17269 "}", 17270 Alignment)); 17271 // variables are aligned at their name, pointers are at the right most 17272 // position 17273 verifyFormat("int *a;\n" 17274 "int **b;\n" 17275 "int ***c;\n" 17276 "int foobar;\n", 17277 Alignment); 17278 17279 // PAS_Left 17280 FormatStyle AlignmentLeft = Alignment; 17281 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 17282 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17283 " int const i = 1;\n" 17284 " int* j = 2;\n" 17285 " int big = 10000;\n" 17286 "\n" 17287 " unsigned oneTwoThree = 123;\n" 17288 " int oneTwo = 12;\n" 17289 " method();\n" 17290 " float k = 2;\n" 17291 " int ll = 10000;\n" 17292 "}", 17293 format("void SomeFunction(int parameter= 0) {\n" 17294 " int const i= 1;\n" 17295 " int *j=2;\n" 17296 " int big = 10000;\n" 17297 "\n" 17298 "unsigned oneTwoThree =123;\n" 17299 "int oneTwo = 12;\n" 17300 " method();\n" 17301 "float k= 2;\n" 17302 "int ll=10000;\n" 17303 "}", 17304 AlignmentLeft)); 17305 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17306 " int const i = 1;\n" 17307 " int** j = 2;\n" 17308 " int& k = i;\n" 17309 " int&& l = i + j;\n" 17310 " int big = 10000;\n" 17311 "\n" 17312 " unsigned oneTwoThree = 123;\n" 17313 " int oneTwo = 12;\n" 17314 " method();\n" 17315 " float k = 2;\n" 17316 " int ll = 10000;\n" 17317 "}", 17318 format("void SomeFunction(int parameter= 0) {\n" 17319 " int const i= 1;\n" 17320 " int **j=2;\n" 17321 "int &k=i;\n" 17322 "int &&l=i+j;\n" 17323 " int big = 10000;\n" 17324 "\n" 17325 "unsigned oneTwoThree =123;\n" 17326 "int oneTwo = 12;\n" 17327 " method();\n" 17328 "float k= 2;\n" 17329 "int ll=10000;\n" 17330 "}", 17331 AlignmentLeft)); 17332 // variables are aligned at their name, pointers are at the left most position 17333 verifyFormat("int* a;\n" 17334 "int** b;\n" 17335 "int*** c;\n" 17336 "int foobar;\n", 17337 AlignmentLeft); 17338 17339 // PAS_Middle 17340 FormatStyle AlignmentMiddle = Alignment; 17341 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 17342 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17343 " int const i = 1;\n" 17344 " int * j = 2;\n" 17345 " int big = 10000;\n" 17346 "\n" 17347 " unsigned oneTwoThree = 123;\n" 17348 " int oneTwo = 12;\n" 17349 " method();\n" 17350 " float k = 2;\n" 17351 " int ll = 10000;\n" 17352 "}", 17353 format("void SomeFunction(int parameter= 0) {\n" 17354 " int const i= 1;\n" 17355 " int *j=2;\n" 17356 " int big = 10000;\n" 17357 "\n" 17358 "unsigned oneTwoThree =123;\n" 17359 "int oneTwo = 12;\n" 17360 " method();\n" 17361 "float k= 2;\n" 17362 "int ll=10000;\n" 17363 "}", 17364 AlignmentMiddle)); 17365 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17366 " int const i = 1;\n" 17367 " int ** j = 2, ***k;\n" 17368 " int & k = i;\n" 17369 " int && l = i + j;\n" 17370 " int big = 10000;\n" 17371 "\n" 17372 " unsigned oneTwoThree = 123;\n" 17373 " int oneTwo = 12;\n" 17374 " method();\n" 17375 " float k = 2;\n" 17376 " int ll = 10000;\n" 17377 "}", 17378 format("void SomeFunction(int parameter= 0) {\n" 17379 " int const i= 1;\n" 17380 " int **j=2,***k;\n" 17381 "int &k=i;\n" 17382 "int &&l=i+j;\n" 17383 " int big = 10000;\n" 17384 "\n" 17385 "unsigned oneTwoThree =123;\n" 17386 "int oneTwo = 12;\n" 17387 " method();\n" 17388 "float k= 2;\n" 17389 "int ll=10000;\n" 17390 "}", 17391 AlignmentMiddle)); 17392 // variables are aligned at their name, pointers are in the middle 17393 verifyFormat("int * a;\n" 17394 "int * b;\n" 17395 "int *** c;\n" 17396 "int foobar;\n", 17397 AlignmentMiddle); 17398 17399 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17400 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 17401 verifyFormat("#define A \\\n" 17402 " int aaaa = 12; \\\n" 17403 " float b = 23; \\\n" 17404 " const int ccc = 234; \\\n" 17405 " unsigned dddddddddd = 2345;", 17406 Alignment); 17407 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 17408 verifyFormat("#define A \\\n" 17409 " int aaaa = 12; \\\n" 17410 " float b = 23; \\\n" 17411 " const int ccc = 234; \\\n" 17412 " unsigned dddddddddd = 2345;", 17413 Alignment); 17414 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 17415 Alignment.ColumnLimit = 30; 17416 verifyFormat("#define A \\\n" 17417 " int aaaa = 12; \\\n" 17418 " float b = 23; \\\n" 17419 " const int ccc = 234; \\\n" 17420 " int dddddddddd = 2345;", 17421 Alignment); 17422 Alignment.ColumnLimit = 80; 17423 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 17424 "k = 4, int l = 5,\n" 17425 " int m = 6) {\n" 17426 " const int j = 10;\n" 17427 " otherThing = 1;\n" 17428 "}", 17429 Alignment); 17430 verifyFormat("void SomeFunction(int parameter = 0) {\n" 17431 " int const i = 1;\n" 17432 " int *j = 2;\n" 17433 " int big = 10000;\n" 17434 "}", 17435 Alignment); 17436 verifyFormat("class C {\n" 17437 "public:\n" 17438 " int i = 1;\n" 17439 " virtual void f() = 0;\n" 17440 "};", 17441 Alignment); 17442 verifyFormat("float i = 1;\n" 17443 "if (SomeType t = getSomething()) {\n" 17444 "}\n" 17445 "const unsigned j = 2;\n" 17446 "int big = 10000;", 17447 Alignment); 17448 verifyFormat("float j = 7;\n" 17449 "for (int k = 0; k < N; ++k) {\n" 17450 "}\n" 17451 "unsigned j = 2;\n" 17452 "int big = 10000;\n" 17453 "}", 17454 Alignment); 17455 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 17456 verifyFormat("float i = 1;\n" 17457 "LooooooooooongType loooooooooooooooooooooongVariable\n" 17458 " = someLooooooooooooooooongFunction();\n" 17459 "int j = 2;", 17460 Alignment); 17461 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 17462 verifyFormat("int i = 1;\n" 17463 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 17464 " someLooooooooooooooooongFunction();\n" 17465 "int j = 2;", 17466 Alignment); 17467 17468 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17469 verifyFormat("auto lambda = []() {\n" 17470 " auto ii = 0;\n" 17471 " float j = 0;\n" 17472 " return 0;\n" 17473 "};\n" 17474 "int i = 0;\n" 17475 "float i2 = 0;\n" 17476 "auto v = type{\n" 17477 " i = 1, //\n" 17478 " (i = 2), //\n" 17479 " i = 3 //\n" 17480 "};", 17481 Alignment); 17482 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17483 17484 verifyFormat( 17485 "int i = 1;\n" 17486 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 17487 " loooooooooooooooooooooongParameterB);\n" 17488 "int j = 2;", 17489 Alignment); 17490 17491 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 17492 // We expect declarations and assignments to align, as long as it doesn't 17493 // exceed the column limit, starting a new alignment sequence whenever it 17494 // happens. 17495 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17496 Alignment.ColumnLimit = 30; 17497 verifyFormat("float ii = 1;\n" 17498 "unsigned j = 2;\n" 17499 "int someVerylongVariable = 1;\n" 17500 "AnotherLongType ll = 123456;\n" 17501 "VeryVeryLongType k = 2;\n" 17502 "int myvar = 1;", 17503 Alignment); 17504 Alignment.ColumnLimit = 80; 17505 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17506 17507 verifyFormat( 17508 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 17509 " typename LongType, typename B>\n" 17510 "auto foo() {}\n", 17511 Alignment); 17512 verifyFormat("float a, b = 1;\n" 17513 "int c = 2;\n" 17514 "int dd = 3;\n", 17515 Alignment); 17516 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17517 "float b[1][] = {{3.f}};\n", 17518 Alignment); 17519 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17520 verifyFormat("float a, b = 1;\n" 17521 "int c = 2;\n" 17522 "int dd = 3;\n", 17523 Alignment); 17524 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17525 "float b[1][] = {{3.f}};\n", 17526 Alignment); 17527 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17528 17529 Alignment.ColumnLimit = 30; 17530 Alignment.BinPackParameters = false; 17531 verifyFormat("void foo(float a,\n" 17532 " float b,\n" 17533 " int c,\n" 17534 " uint32_t *d) {\n" 17535 " int *e = 0;\n" 17536 " float f = 0;\n" 17537 " double g = 0;\n" 17538 "}\n" 17539 "void bar(ino_t a,\n" 17540 " int b,\n" 17541 " uint32_t *c,\n" 17542 " bool d) {}\n", 17543 Alignment); 17544 Alignment.BinPackParameters = true; 17545 Alignment.ColumnLimit = 80; 17546 17547 // Bug 33507 17548 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17549 verifyFormat( 17550 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 17551 " static const Version verVs2017;\n" 17552 " return true;\n" 17553 "});\n", 17554 Alignment); 17555 Alignment.PointerAlignment = FormatStyle::PAS_Right; 17556 17557 // See llvm.org/PR35641 17558 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17559 verifyFormat("int func() { //\n" 17560 " int b;\n" 17561 " unsigned c;\n" 17562 "}", 17563 Alignment); 17564 17565 // See PR37175 17566 FormatStyle Style = getMozillaStyle(); 17567 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17568 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 17569 "foo(int a);", 17570 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 17571 17572 Alignment.PointerAlignment = FormatStyle::PAS_Left; 17573 verifyFormat("unsigned int* a;\n" 17574 "int* b;\n" 17575 "unsigned int Const* c;\n" 17576 "unsigned int const* d;\n" 17577 "unsigned int Const& e;\n" 17578 "unsigned int const& f;", 17579 Alignment); 17580 verifyFormat("Const unsigned int* c;\n" 17581 "const unsigned int* d;\n" 17582 "Const unsigned int& e;\n" 17583 "const unsigned int& f;\n" 17584 "const unsigned g;\n" 17585 "Const unsigned h;", 17586 Alignment); 17587 17588 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17589 verifyFormat("unsigned int * a;\n" 17590 "int * b;\n" 17591 "unsigned int Const * c;\n" 17592 "unsigned int const * d;\n" 17593 "unsigned int Const & e;\n" 17594 "unsigned int const & f;", 17595 Alignment); 17596 verifyFormat("Const unsigned int * c;\n" 17597 "const unsigned int * d;\n" 17598 "Const unsigned int & e;\n" 17599 "const unsigned int & f;\n" 17600 "const unsigned g;\n" 17601 "Const unsigned h;", 17602 Alignment); 17603 17604 // See PR46529 17605 FormatStyle BracedAlign = getLLVMStyle(); 17606 BracedAlign.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17607 verifyFormat("const auto result{[]() {\n" 17608 " const auto something = 1;\n" 17609 " return 2;\n" 17610 "}};", 17611 BracedAlign); 17612 verifyFormat("int foo{[]() {\n" 17613 " int bar{0};\n" 17614 " return 0;\n" 17615 "}()};", 17616 BracedAlign); 17617 BracedAlign.Cpp11BracedListStyle = false; 17618 verifyFormat("const auto result{ []() {\n" 17619 " const auto something = 1;\n" 17620 " return 2;\n" 17621 "} };", 17622 BracedAlign); 17623 verifyFormat("int foo{ []() {\n" 17624 " int bar{ 0 };\n" 17625 " return 0;\n" 17626 "}() };", 17627 BracedAlign); 17628 } 17629 17630 TEST_F(FormatTest, AlignWithLineBreaks) { 17631 auto Style = getLLVMStyleWithColumns(120); 17632 17633 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 17634 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 17635 verifyFormat("void foo() {\n" 17636 " int myVar = 5;\n" 17637 " double x = 3.14;\n" 17638 " auto str = \"Hello \"\n" 17639 " \"World\";\n" 17640 " auto s = \"Hello \"\n" 17641 " \"Again\";\n" 17642 "}", 17643 Style); 17644 17645 // clang-format off 17646 verifyFormat("void foo() {\n" 17647 " const int capacityBefore = Entries.capacity();\n" 17648 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17649 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17650 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17651 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17652 "}", 17653 Style); 17654 // clang-format on 17655 17656 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17657 verifyFormat("void foo() {\n" 17658 " int myVar = 5;\n" 17659 " double x = 3.14;\n" 17660 " auto str = \"Hello \"\n" 17661 " \"World\";\n" 17662 " auto s = \"Hello \"\n" 17663 " \"Again\";\n" 17664 "}", 17665 Style); 17666 17667 // clang-format off 17668 verifyFormat("void foo() {\n" 17669 " const int capacityBefore = Entries.capacity();\n" 17670 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17671 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17672 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17673 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17674 "}", 17675 Style); 17676 // clang-format on 17677 17678 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17679 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17680 verifyFormat("void foo() {\n" 17681 " int myVar = 5;\n" 17682 " double x = 3.14;\n" 17683 " auto str = \"Hello \"\n" 17684 " \"World\";\n" 17685 " auto s = \"Hello \"\n" 17686 " \"Again\";\n" 17687 "}", 17688 Style); 17689 17690 // clang-format off 17691 verifyFormat("void foo() {\n" 17692 " const int capacityBefore = Entries.capacity();\n" 17693 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17694 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17695 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17696 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17697 "}", 17698 Style); 17699 // clang-format on 17700 17701 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17702 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17703 17704 verifyFormat("void foo() {\n" 17705 " int myVar = 5;\n" 17706 " double x = 3.14;\n" 17707 " auto str = \"Hello \"\n" 17708 " \"World\";\n" 17709 " auto s = \"Hello \"\n" 17710 " \"Again\";\n" 17711 "}", 17712 Style); 17713 17714 // clang-format off 17715 verifyFormat("void foo() {\n" 17716 " const int capacityBefore = Entries.capacity();\n" 17717 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17718 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17719 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17720 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17721 "}", 17722 Style); 17723 // clang-format on 17724 17725 Style = getLLVMStyleWithColumns(120); 17726 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17727 Style.ContinuationIndentWidth = 4; 17728 Style.IndentWidth = 4; 17729 17730 // clang-format off 17731 verifyFormat("void SomeFunc() {\n" 17732 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17733 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17734 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17735 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17736 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17737 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17738 "}", 17739 Style); 17740 // clang-format on 17741 17742 Style.BinPackArguments = false; 17743 17744 // clang-format off 17745 verifyFormat("void SomeFunc() {\n" 17746 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 17747 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17748 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 17749 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17750 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 17751 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17752 "}", 17753 Style); 17754 // clang-format on 17755 } 17756 17757 TEST_F(FormatTest, AlignWithInitializerPeriods) { 17758 auto Style = getLLVMStyleWithColumns(60); 17759 17760 verifyFormat("void foo1(void) {\n" 17761 " BYTE p[1] = 1;\n" 17762 " A B = {.one_foooooooooooooooo = 2,\n" 17763 " .two_fooooooooooooo = 3,\n" 17764 " .three_fooooooooooooo = 4};\n" 17765 " BYTE payload = 2;\n" 17766 "}", 17767 Style); 17768 17769 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17770 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 17771 verifyFormat("void foo2(void) {\n" 17772 " BYTE p[1] = 1;\n" 17773 " A B = {.one_foooooooooooooooo = 2,\n" 17774 " .two_fooooooooooooo = 3,\n" 17775 " .three_fooooooooooooo = 4};\n" 17776 " BYTE payload = 2;\n" 17777 "}", 17778 Style); 17779 17780 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17781 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17782 verifyFormat("void foo3(void) {\n" 17783 " BYTE p[1] = 1;\n" 17784 " A B = {.one_foooooooooooooooo = 2,\n" 17785 " .two_fooooooooooooo = 3,\n" 17786 " .three_fooooooooooooo = 4};\n" 17787 " BYTE payload = 2;\n" 17788 "}", 17789 Style); 17790 17791 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17792 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17793 verifyFormat("void foo4(void) {\n" 17794 " BYTE p[1] = 1;\n" 17795 " A B = {.one_foooooooooooooooo = 2,\n" 17796 " .two_fooooooooooooo = 3,\n" 17797 " .three_fooooooooooooo = 4};\n" 17798 " BYTE payload = 2;\n" 17799 "}", 17800 Style); 17801 } 17802 17803 TEST_F(FormatTest, LinuxBraceBreaking) { 17804 FormatStyle LinuxBraceStyle = getLLVMStyle(); 17805 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 17806 verifyFormat("namespace a\n" 17807 "{\n" 17808 "class A\n" 17809 "{\n" 17810 " void f()\n" 17811 " {\n" 17812 " if (true) {\n" 17813 " a();\n" 17814 " b();\n" 17815 " } else {\n" 17816 " a();\n" 17817 " }\n" 17818 " }\n" 17819 " void g() { return; }\n" 17820 "};\n" 17821 "struct B {\n" 17822 " int x;\n" 17823 "};\n" 17824 "} // namespace a\n", 17825 LinuxBraceStyle); 17826 verifyFormat("enum X {\n" 17827 " Y = 0,\n" 17828 "}\n", 17829 LinuxBraceStyle); 17830 verifyFormat("struct S {\n" 17831 " int Type;\n" 17832 " union {\n" 17833 " int x;\n" 17834 " double y;\n" 17835 " } Value;\n" 17836 " class C\n" 17837 " {\n" 17838 " MyFavoriteType Value;\n" 17839 " } Class;\n" 17840 "}\n", 17841 LinuxBraceStyle); 17842 } 17843 17844 TEST_F(FormatTest, MozillaBraceBreaking) { 17845 FormatStyle MozillaBraceStyle = getLLVMStyle(); 17846 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 17847 MozillaBraceStyle.FixNamespaceComments = false; 17848 verifyFormat("namespace a {\n" 17849 "class A\n" 17850 "{\n" 17851 " void f()\n" 17852 " {\n" 17853 " if (true) {\n" 17854 " a();\n" 17855 " b();\n" 17856 " }\n" 17857 " }\n" 17858 " void g() { return; }\n" 17859 "};\n" 17860 "enum E\n" 17861 "{\n" 17862 " A,\n" 17863 " // foo\n" 17864 " B,\n" 17865 " C\n" 17866 "};\n" 17867 "struct B\n" 17868 "{\n" 17869 " int x;\n" 17870 "};\n" 17871 "}\n", 17872 MozillaBraceStyle); 17873 verifyFormat("struct S\n" 17874 "{\n" 17875 " int Type;\n" 17876 " union\n" 17877 " {\n" 17878 " int x;\n" 17879 " double y;\n" 17880 " } Value;\n" 17881 " class C\n" 17882 " {\n" 17883 " MyFavoriteType Value;\n" 17884 " } Class;\n" 17885 "}\n", 17886 MozillaBraceStyle); 17887 } 17888 17889 TEST_F(FormatTest, StroustrupBraceBreaking) { 17890 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 17891 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 17892 verifyFormat("namespace a {\n" 17893 "class A {\n" 17894 " void f()\n" 17895 " {\n" 17896 " if (true) {\n" 17897 " a();\n" 17898 " b();\n" 17899 " }\n" 17900 " }\n" 17901 " void g() { return; }\n" 17902 "};\n" 17903 "struct B {\n" 17904 " int x;\n" 17905 "};\n" 17906 "} // namespace a\n", 17907 StroustrupBraceStyle); 17908 17909 verifyFormat("void foo()\n" 17910 "{\n" 17911 " if (a) {\n" 17912 " a();\n" 17913 " }\n" 17914 " else {\n" 17915 " b();\n" 17916 " }\n" 17917 "}\n", 17918 StroustrupBraceStyle); 17919 17920 verifyFormat("#ifdef _DEBUG\n" 17921 "int foo(int i = 0)\n" 17922 "#else\n" 17923 "int foo(int i = 5)\n" 17924 "#endif\n" 17925 "{\n" 17926 " return i;\n" 17927 "}", 17928 StroustrupBraceStyle); 17929 17930 verifyFormat("void foo() {}\n" 17931 "void bar()\n" 17932 "#ifdef _DEBUG\n" 17933 "{\n" 17934 " foo();\n" 17935 "}\n" 17936 "#else\n" 17937 "{\n" 17938 "}\n" 17939 "#endif", 17940 StroustrupBraceStyle); 17941 17942 verifyFormat("void foobar() { int i = 5; }\n" 17943 "#ifdef _DEBUG\n" 17944 "void bar() {}\n" 17945 "#else\n" 17946 "void bar() { foobar(); }\n" 17947 "#endif", 17948 StroustrupBraceStyle); 17949 } 17950 17951 TEST_F(FormatTest, AllmanBraceBreaking) { 17952 FormatStyle AllmanBraceStyle = getLLVMStyle(); 17953 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 17954 17955 EXPECT_EQ("namespace a\n" 17956 "{\n" 17957 "void f();\n" 17958 "void g();\n" 17959 "} // namespace a\n", 17960 format("namespace a\n" 17961 "{\n" 17962 "void f();\n" 17963 "void g();\n" 17964 "}\n", 17965 AllmanBraceStyle)); 17966 17967 verifyFormat("namespace a\n" 17968 "{\n" 17969 "class A\n" 17970 "{\n" 17971 " void f()\n" 17972 " {\n" 17973 " if (true)\n" 17974 " {\n" 17975 " a();\n" 17976 " b();\n" 17977 " }\n" 17978 " }\n" 17979 " void g() { return; }\n" 17980 "};\n" 17981 "struct B\n" 17982 "{\n" 17983 " int x;\n" 17984 "};\n" 17985 "union C\n" 17986 "{\n" 17987 "};\n" 17988 "} // namespace a", 17989 AllmanBraceStyle); 17990 17991 verifyFormat("void f()\n" 17992 "{\n" 17993 " if (true)\n" 17994 " {\n" 17995 " a();\n" 17996 " }\n" 17997 " else if (false)\n" 17998 " {\n" 17999 " b();\n" 18000 " }\n" 18001 " else\n" 18002 " {\n" 18003 " c();\n" 18004 " }\n" 18005 "}\n", 18006 AllmanBraceStyle); 18007 18008 verifyFormat("void f()\n" 18009 "{\n" 18010 " for (int i = 0; i < 10; ++i)\n" 18011 " {\n" 18012 " a();\n" 18013 " }\n" 18014 " while (false)\n" 18015 " {\n" 18016 " b();\n" 18017 " }\n" 18018 " do\n" 18019 " {\n" 18020 " c();\n" 18021 " } while (false)\n" 18022 "}\n", 18023 AllmanBraceStyle); 18024 18025 verifyFormat("void f(int a)\n" 18026 "{\n" 18027 " switch (a)\n" 18028 " {\n" 18029 " case 0:\n" 18030 " break;\n" 18031 " case 1:\n" 18032 " {\n" 18033 " break;\n" 18034 " }\n" 18035 " case 2:\n" 18036 " {\n" 18037 " }\n" 18038 " break;\n" 18039 " default:\n" 18040 " break;\n" 18041 " }\n" 18042 "}\n", 18043 AllmanBraceStyle); 18044 18045 verifyFormat("enum X\n" 18046 "{\n" 18047 " Y = 0,\n" 18048 "}\n", 18049 AllmanBraceStyle); 18050 verifyFormat("enum X\n" 18051 "{\n" 18052 " Y = 0\n" 18053 "}\n", 18054 AllmanBraceStyle); 18055 18056 verifyFormat("@interface BSApplicationController ()\n" 18057 "{\n" 18058 "@private\n" 18059 " id _extraIvar;\n" 18060 "}\n" 18061 "@end\n", 18062 AllmanBraceStyle); 18063 18064 verifyFormat("#ifdef _DEBUG\n" 18065 "int foo(int i = 0)\n" 18066 "#else\n" 18067 "int foo(int i = 5)\n" 18068 "#endif\n" 18069 "{\n" 18070 " return i;\n" 18071 "}", 18072 AllmanBraceStyle); 18073 18074 verifyFormat("void foo() {}\n" 18075 "void bar()\n" 18076 "#ifdef _DEBUG\n" 18077 "{\n" 18078 " foo();\n" 18079 "}\n" 18080 "#else\n" 18081 "{\n" 18082 "}\n" 18083 "#endif", 18084 AllmanBraceStyle); 18085 18086 verifyFormat("void foobar() { int i = 5; }\n" 18087 "#ifdef _DEBUG\n" 18088 "void bar() {}\n" 18089 "#else\n" 18090 "void bar() { foobar(); }\n" 18091 "#endif", 18092 AllmanBraceStyle); 18093 18094 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 18095 FormatStyle::SLS_All); 18096 18097 verifyFormat("[](int i) { return i + 2; };\n" 18098 "[](int i, int j)\n" 18099 "{\n" 18100 " auto x = i + j;\n" 18101 " auto y = i * j;\n" 18102 " return x ^ y;\n" 18103 "};\n" 18104 "void foo()\n" 18105 "{\n" 18106 " auto shortLambda = [](int i) { return i + 2; };\n" 18107 " auto longLambda = [](int i, int j)\n" 18108 " {\n" 18109 " auto x = i + j;\n" 18110 " auto y = i * j;\n" 18111 " return x ^ y;\n" 18112 " };\n" 18113 "}", 18114 AllmanBraceStyle); 18115 18116 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 18117 18118 verifyFormat("[](int i)\n" 18119 "{\n" 18120 " return i + 2;\n" 18121 "};\n" 18122 "[](int i, int j)\n" 18123 "{\n" 18124 " auto x = i + j;\n" 18125 " auto y = i * j;\n" 18126 " return x ^ y;\n" 18127 "};\n" 18128 "void foo()\n" 18129 "{\n" 18130 " auto shortLambda = [](int i)\n" 18131 " {\n" 18132 " return i + 2;\n" 18133 " };\n" 18134 " auto longLambda = [](int i, int j)\n" 18135 " {\n" 18136 " auto x = i + j;\n" 18137 " auto y = i * j;\n" 18138 " return x ^ y;\n" 18139 " };\n" 18140 "}", 18141 AllmanBraceStyle); 18142 18143 // Reset 18144 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 18145 18146 // This shouldn't affect ObjC blocks.. 18147 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 18148 " // ...\n" 18149 " int i;\n" 18150 "}];", 18151 AllmanBraceStyle); 18152 verifyFormat("void (^block)(void) = ^{\n" 18153 " // ...\n" 18154 " int i;\n" 18155 "};", 18156 AllmanBraceStyle); 18157 // .. or dict literals. 18158 verifyFormat("void f()\n" 18159 "{\n" 18160 " // ...\n" 18161 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 18162 "}", 18163 AllmanBraceStyle); 18164 verifyFormat("void f()\n" 18165 "{\n" 18166 " // ...\n" 18167 " [object someMethod:@{a : @\"b\"}];\n" 18168 "}", 18169 AllmanBraceStyle); 18170 verifyFormat("int f()\n" 18171 "{ // comment\n" 18172 " return 42;\n" 18173 "}", 18174 AllmanBraceStyle); 18175 18176 AllmanBraceStyle.ColumnLimit = 19; 18177 verifyFormat("void f() { int i; }", AllmanBraceStyle); 18178 AllmanBraceStyle.ColumnLimit = 18; 18179 verifyFormat("void f()\n" 18180 "{\n" 18181 " int i;\n" 18182 "}", 18183 AllmanBraceStyle); 18184 AllmanBraceStyle.ColumnLimit = 80; 18185 18186 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 18187 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 18188 FormatStyle::SIS_WithoutElse; 18189 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 18190 verifyFormat("void f(bool b)\n" 18191 "{\n" 18192 " if (b)\n" 18193 " {\n" 18194 " return;\n" 18195 " }\n" 18196 "}\n", 18197 BreakBeforeBraceShortIfs); 18198 verifyFormat("void f(bool b)\n" 18199 "{\n" 18200 " if constexpr (b)\n" 18201 " {\n" 18202 " return;\n" 18203 " }\n" 18204 "}\n", 18205 BreakBeforeBraceShortIfs); 18206 verifyFormat("void f(bool b)\n" 18207 "{\n" 18208 " if CONSTEXPR (b)\n" 18209 " {\n" 18210 " return;\n" 18211 " }\n" 18212 "}\n", 18213 BreakBeforeBraceShortIfs); 18214 verifyFormat("void f(bool b)\n" 18215 "{\n" 18216 " if (b) return;\n" 18217 "}\n", 18218 BreakBeforeBraceShortIfs); 18219 verifyFormat("void f(bool b)\n" 18220 "{\n" 18221 " if constexpr (b) return;\n" 18222 "}\n", 18223 BreakBeforeBraceShortIfs); 18224 verifyFormat("void f(bool b)\n" 18225 "{\n" 18226 " if CONSTEXPR (b) return;\n" 18227 "}\n", 18228 BreakBeforeBraceShortIfs); 18229 verifyFormat("void f(bool b)\n" 18230 "{\n" 18231 " while (b)\n" 18232 " {\n" 18233 " return;\n" 18234 " }\n" 18235 "}\n", 18236 BreakBeforeBraceShortIfs); 18237 } 18238 18239 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 18240 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0); 18241 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 18242 18243 // Make a few changes to the style for testing purposes 18244 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 18245 FormatStyle::SFS_Empty; 18246 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 18247 18248 // FIXME: this test case can't decide whether there should be a blank line 18249 // after the ~D() line or not. It adds one if one doesn't exist in the test 18250 // and it removes the line if one exists. 18251 /* 18252 verifyFormat("class A;\n" 18253 "namespace B\n" 18254 " {\n" 18255 "class C;\n" 18256 "// Comment\n" 18257 "class D\n" 18258 " {\n" 18259 "public:\n" 18260 " D();\n" 18261 " ~D() {}\n" 18262 "private:\n" 18263 " enum E\n" 18264 " {\n" 18265 " F\n" 18266 " }\n" 18267 " };\n" 18268 " } // namespace B\n", 18269 WhitesmithsBraceStyle); 18270 */ 18271 18272 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 18273 verifyFormat("namespace a\n" 18274 " {\n" 18275 "class A\n" 18276 " {\n" 18277 " void f()\n" 18278 " {\n" 18279 " if (true)\n" 18280 " {\n" 18281 " a();\n" 18282 " b();\n" 18283 " }\n" 18284 " }\n" 18285 " void g()\n" 18286 " {\n" 18287 " return;\n" 18288 " }\n" 18289 " };\n" 18290 "struct B\n" 18291 " {\n" 18292 " int x;\n" 18293 " };\n" 18294 " } // namespace a", 18295 WhitesmithsBraceStyle); 18296 18297 verifyFormat("namespace a\n" 18298 " {\n" 18299 "namespace b\n" 18300 " {\n" 18301 "class A\n" 18302 " {\n" 18303 " void f()\n" 18304 " {\n" 18305 " if (true)\n" 18306 " {\n" 18307 " a();\n" 18308 " b();\n" 18309 " }\n" 18310 " }\n" 18311 " void g()\n" 18312 " {\n" 18313 " return;\n" 18314 " }\n" 18315 " };\n" 18316 "struct B\n" 18317 " {\n" 18318 " int x;\n" 18319 " };\n" 18320 " } // namespace b\n" 18321 " } // namespace a", 18322 WhitesmithsBraceStyle); 18323 18324 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 18325 verifyFormat("namespace a\n" 18326 " {\n" 18327 "namespace b\n" 18328 " {\n" 18329 " class A\n" 18330 " {\n" 18331 " void f()\n" 18332 " {\n" 18333 " if (true)\n" 18334 " {\n" 18335 " a();\n" 18336 " b();\n" 18337 " }\n" 18338 " }\n" 18339 " void g()\n" 18340 " {\n" 18341 " return;\n" 18342 " }\n" 18343 " };\n" 18344 " struct B\n" 18345 " {\n" 18346 " int x;\n" 18347 " };\n" 18348 " } // namespace b\n" 18349 " } // namespace a", 18350 WhitesmithsBraceStyle); 18351 18352 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 18353 verifyFormat("namespace a\n" 18354 " {\n" 18355 " namespace b\n" 18356 " {\n" 18357 " class A\n" 18358 " {\n" 18359 " void f()\n" 18360 " {\n" 18361 " if (true)\n" 18362 " {\n" 18363 " a();\n" 18364 " b();\n" 18365 " }\n" 18366 " }\n" 18367 " void g()\n" 18368 " {\n" 18369 " return;\n" 18370 " }\n" 18371 " };\n" 18372 " struct B\n" 18373 " {\n" 18374 " int x;\n" 18375 " };\n" 18376 " } // namespace b\n" 18377 " } // namespace a", 18378 WhitesmithsBraceStyle); 18379 18380 verifyFormat("void f()\n" 18381 " {\n" 18382 " if (true)\n" 18383 " {\n" 18384 " a();\n" 18385 " }\n" 18386 " else if (false)\n" 18387 " {\n" 18388 " b();\n" 18389 " }\n" 18390 " else\n" 18391 " {\n" 18392 " c();\n" 18393 " }\n" 18394 " }\n", 18395 WhitesmithsBraceStyle); 18396 18397 verifyFormat("void f()\n" 18398 " {\n" 18399 " for (int i = 0; i < 10; ++i)\n" 18400 " {\n" 18401 " a();\n" 18402 " }\n" 18403 " while (false)\n" 18404 " {\n" 18405 " b();\n" 18406 " }\n" 18407 " do\n" 18408 " {\n" 18409 " c();\n" 18410 " } while (false)\n" 18411 " }\n", 18412 WhitesmithsBraceStyle); 18413 18414 WhitesmithsBraceStyle.IndentCaseLabels = true; 18415 verifyFormat("void switchTest1(int a)\n" 18416 " {\n" 18417 " switch (a)\n" 18418 " {\n" 18419 " case 2:\n" 18420 " {\n" 18421 " }\n" 18422 " break;\n" 18423 " }\n" 18424 " }\n", 18425 WhitesmithsBraceStyle); 18426 18427 verifyFormat("void switchTest2(int a)\n" 18428 " {\n" 18429 " switch (a)\n" 18430 " {\n" 18431 " case 0:\n" 18432 " break;\n" 18433 " case 1:\n" 18434 " {\n" 18435 " break;\n" 18436 " }\n" 18437 " case 2:\n" 18438 " {\n" 18439 " }\n" 18440 " break;\n" 18441 " default:\n" 18442 " break;\n" 18443 " }\n" 18444 " }\n", 18445 WhitesmithsBraceStyle); 18446 18447 verifyFormat("void switchTest3(int a)\n" 18448 " {\n" 18449 " switch (a)\n" 18450 " {\n" 18451 " case 0:\n" 18452 " {\n" 18453 " foo(x);\n" 18454 " }\n" 18455 " break;\n" 18456 " default:\n" 18457 " {\n" 18458 " foo(1);\n" 18459 " }\n" 18460 " break;\n" 18461 " }\n" 18462 " }\n", 18463 WhitesmithsBraceStyle); 18464 18465 WhitesmithsBraceStyle.IndentCaseLabels = false; 18466 18467 verifyFormat("void switchTest4(int a)\n" 18468 " {\n" 18469 " switch (a)\n" 18470 " {\n" 18471 " case 2:\n" 18472 " {\n" 18473 " }\n" 18474 " break;\n" 18475 " }\n" 18476 " }\n", 18477 WhitesmithsBraceStyle); 18478 18479 verifyFormat("void switchTest5(int a)\n" 18480 " {\n" 18481 " switch (a)\n" 18482 " {\n" 18483 " case 0:\n" 18484 " break;\n" 18485 " case 1:\n" 18486 " {\n" 18487 " foo();\n" 18488 " break;\n" 18489 " }\n" 18490 " case 2:\n" 18491 " {\n" 18492 " }\n" 18493 " break;\n" 18494 " default:\n" 18495 " break;\n" 18496 " }\n" 18497 " }\n", 18498 WhitesmithsBraceStyle); 18499 18500 verifyFormat("void switchTest6(int a)\n" 18501 " {\n" 18502 " switch (a)\n" 18503 " {\n" 18504 " case 0:\n" 18505 " {\n" 18506 " foo(x);\n" 18507 " }\n" 18508 " break;\n" 18509 " default:\n" 18510 " {\n" 18511 " foo(1);\n" 18512 " }\n" 18513 " break;\n" 18514 " }\n" 18515 " }\n", 18516 WhitesmithsBraceStyle); 18517 18518 verifyFormat("enum X\n" 18519 " {\n" 18520 " Y = 0, // testing\n" 18521 " }\n", 18522 WhitesmithsBraceStyle); 18523 18524 verifyFormat("enum X\n" 18525 " {\n" 18526 " Y = 0\n" 18527 " }\n", 18528 WhitesmithsBraceStyle); 18529 verifyFormat("enum X\n" 18530 " {\n" 18531 " Y = 0,\n" 18532 " Z = 1\n" 18533 " };\n", 18534 WhitesmithsBraceStyle); 18535 18536 verifyFormat("@interface BSApplicationController ()\n" 18537 " {\n" 18538 "@private\n" 18539 " id _extraIvar;\n" 18540 " }\n" 18541 "@end\n", 18542 WhitesmithsBraceStyle); 18543 18544 verifyFormat("#ifdef _DEBUG\n" 18545 "int foo(int i = 0)\n" 18546 "#else\n" 18547 "int foo(int i = 5)\n" 18548 "#endif\n" 18549 " {\n" 18550 " return i;\n" 18551 " }", 18552 WhitesmithsBraceStyle); 18553 18554 verifyFormat("void foo() {}\n" 18555 "void bar()\n" 18556 "#ifdef _DEBUG\n" 18557 " {\n" 18558 " foo();\n" 18559 " }\n" 18560 "#else\n" 18561 " {\n" 18562 " }\n" 18563 "#endif", 18564 WhitesmithsBraceStyle); 18565 18566 verifyFormat("void foobar()\n" 18567 " {\n" 18568 " int i = 5;\n" 18569 " }\n" 18570 "#ifdef _DEBUG\n" 18571 "void bar()\n" 18572 " {\n" 18573 " }\n" 18574 "#else\n" 18575 "void bar()\n" 18576 " {\n" 18577 " foobar();\n" 18578 " }\n" 18579 "#endif", 18580 WhitesmithsBraceStyle); 18581 18582 // This shouldn't affect ObjC blocks.. 18583 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 18584 " // ...\n" 18585 " int i;\n" 18586 "}];", 18587 WhitesmithsBraceStyle); 18588 verifyFormat("void (^block)(void) = ^{\n" 18589 " // ...\n" 18590 " int i;\n" 18591 "};", 18592 WhitesmithsBraceStyle); 18593 // .. or dict literals. 18594 verifyFormat("void f()\n" 18595 " {\n" 18596 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 18597 " }", 18598 WhitesmithsBraceStyle); 18599 18600 verifyFormat("int f()\n" 18601 " { // comment\n" 18602 " return 42;\n" 18603 " }", 18604 WhitesmithsBraceStyle); 18605 18606 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 18607 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 18608 FormatStyle::SIS_OnlyFirstIf; 18609 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 18610 verifyFormat("void f(bool b)\n" 18611 " {\n" 18612 " if (b)\n" 18613 " {\n" 18614 " return;\n" 18615 " }\n" 18616 " }\n", 18617 BreakBeforeBraceShortIfs); 18618 verifyFormat("void f(bool b)\n" 18619 " {\n" 18620 " if (b) return;\n" 18621 " }\n", 18622 BreakBeforeBraceShortIfs); 18623 verifyFormat("void f(bool b)\n" 18624 " {\n" 18625 " while (b)\n" 18626 " {\n" 18627 " return;\n" 18628 " }\n" 18629 " }\n", 18630 BreakBeforeBraceShortIfs); 18631 } 18632 18633 TEST_F(FormatTest, GNUBraceBreaking) { 18634 FormatStyle GNUBraceStyle = getLLVMStyle(); 18635 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 18636 verifyFormat("namespace a\n" 18637 "{\n" 18638 "class A\n" 18639 "{\n" 18640 " void f()\n" 18641 " {\n" 18642 " int a;\n" 18643 " {\n" 18644 " int b;\n" 18645 " }\n" 18646 " if (true)\n" 18647 " {\n" 18648 " a();\n" 18649 " b();\n" 18650 " }\n" 18651 " }\n" 18652 " void g() { return; }\n" 18653 "}\n" 18654 "} // namespace a", 18655 GNUBraceStyle); 18656 18657 verifyFormat("void f()\n" 18658 "{\n" 18659 " if (true)\n" 18660 " {\n" 18661 " a();\n" 18662 " }\n" 18663 " else if (false)\n" 18664 " {\n" 18665 " b();\n" 18666 " }\n" 18667 " else\n" 18668 " {\n" 18669 " c();\n" 18670 " }\n" 18671 "}\n", 18672 GNUBraceStyle); 18673 18674 verifyFormat("void f()\n" 18675 "{\n" 18676 " for (int i = 0; i < 10; ++i)\n" 18677 " {\n" 18678 " a();\n" 18679 " }\n" 18680 " while (false)\n" 18681 " {\n" 18682 " b();\n" 18683 " }\n" 18684 " do\n" 18685 " {\n" 18686 " c();\n" 18687 " }\n" 18688 " while (false);\n" 18689 "}\n", 18690 GNUBraceStyle); 18691 18692 verifyFormat("void f(int a)\n" 18693 "{\n" 18694 " switch (a)\n" 18695 " {\n" 18696 " case 0:\n" 18697 " break;\n" 18698 " case 1:\n" 18699 " {\n" 18700 " break;\n" 18701 " }\n" 18702 " case 2:\n" 18703 " {\n" 18704 " }\n" 18705 " break;\n" 18706 " default:\n" 18707 " break;\n" 18708 " }\n" 18709 "}\n", 18710 GNUBraceStyle); 18711 18712 verifyFormat("enum X\n" 18713 "{\n" 18714 " Y = 0,\n" 18715 "}\n", 18716 GNUBraceStyle); 18717 18718 verifyFormat("@interface BSApplicationController ()\n" 18719 "{\n" 18720 "@private\n" 18721 " id _extraIvar;\n" 18722 "}\n" 18723 "@end\n", 18724 GNUBraceStyle); 18725 18726 verifyFormat("#ifdef _DEBUG\n" 18727 "int foo(int i = 0)\n" 18728 "#else\n" 18729 "int foo(int i = 5)\n" 18730 "#endif\n" 18731 "{\n" 18732 " return i;\n" 18733 "}", 18734 GNUBraceStyle); 18735 18736 verifyFormat("void foo() {}\n" 18737 "void bar()\n" 18738 "#ifdef _DEBUG\n" 18739 "{\n" 18740 " foo();\n" 18741 "}\n" 18742 "#else\n" 18743 "{\n" 18744 "}\n" 18745 "#endif", 18746 GNUBraceStyle); 18747 18748 verifyFormat("void foobar() { int i = 5; }\n" 18749 "#ifdef _DEBUG\n" 18750 "void bar() {}\n" 18751 "#else\n" 18752 "void bar() { foobar(); }\n" 18753 "#endif", 18754 GNUBraceStyle); 18755 } 18756 18757 TEST_F(FormatTest, WebKitBraceBreaking) { 18758 FormatStyle WebKitBraceStyle = getLLVMStyle(); 18759 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 18760 WebKitBraceStyle.FixNamespaceComments = false; 18761 verifyFormat("namespace a {\n" 18762 "class A {\n" 18763 " void f()\n" 18764 " {\n" 18765 " if (true) {\n" 18766 " a();\n" 18767 " b();\n" 18768 " }\n" 18769 " }\n" 18770 " void g() { return; }\n" 18771 "};\n" 18772 "enum E {\n" 18773 " A,\n" 18774 " // foo\n" 18775 " B,\n" 18776 " C\n" 18777 "};\n" 18778 "struct B {\n" 18779 " int x;\n" 18780 "};\n" 18781 "}\n", 18782 WebKitBraceStyle); 18783 verifyFormat("struct S {\n" 18784 " int Type;\n" 18785 " union {\n" 18786 " int x;\n" 18787 " double y;\n" 18788 " } Value;\n" 18789 " class C {\n" 18790 " MyFavoriteType Value;\n" 18791 " } Class;\n" 18792 "};\n", 18793 WebKitBraceStyle); 18794 } 18795 18796 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 18797 verifyFormat("void f() {\n" 18798 " try {\n" 18799 " } catch (const Exception &e) {\n" 18800 " }\n" 18801 "}\n", 18802 getLLVMStyle()); 18803 } 18804 18805 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 18806 auto Style = getLLVMStyle(); 18807 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18808 Style.AlignConsecutiveAssignments = 18809 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18810 Style.AlignConsecutiveDeclarations = 18811 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18812 verifyFormat("struct test demo[] = {\n" 18813 " {56, 23, \"hello\"},\n" 18814 " {-1, 93463, \"world\"},\n" 18815 " { 7, 5, \"!!\"}\n" 18816 "};\n", 18817 Style); 18818 18819 verifyFormat("struct test demo[] = {\n" 18820 " {56, 23, \"hello\"}, // first line\n" 18821 " {-1, 93463, \"world\"}, // second line\n" 18822 " { 7, 5, \"!!\"} // third line\n" 18823 "};\n", 18824 Style); 18825 18826 verifyFormat("struct test demo[4] = {\n" 18827 " { 56, 23, 21, \"oh\"}, // first line\n" 18828 " { -1, 93463, 22, \"my\"}, // second line\n" 18829 " { 7, 5, 1, \"goodness\"} // third line\n" 18830 " {234, 5, 1, \"gracious\"} // fourth line\n" 18831 "};\n", 18832 Style); 18833 18834 verifyFormat("struct test demo[3] = {\n" 18835 " {56, 23, \"hello\"},\n" 18836 " {-1, 93463, \"world\"},\n" 18837 " { 7, 5, \"!!\"}\n" 18838 "};\n", 18839 Style); 18840 18841 verifyFormat("struct test demo[3] = {\n" 18842 " {int{56}, 23, \"hello\"},\n" 18843 " {int{-1}, 93463, \"world\"},\n" 18844 " { int{7}, 5, \"!!\"}\n" 18845 "};\n", 18846 Style); 18847 18848 verifyFormat("struct test demo[] = {\n" 18849 " {56, 23, \"hello\"},\n" 18850 " {-1, 93463, \"world\"},\n" 18851 " { 7, 5, \"!!\"},\n" 18852 "};\n", 18853 Style); 18854 18855 verifyFormat("test demo[] = {\n" 18856 " {56, 23, \"hello\"},\n" 18857 " {-1, 93463, \"world\"},\n" 18858 " { 7, 5, \"!!\"},\n" 18859 "};\n", 18860 Style); 18861 18862 verifyFormat("demo = std::array<struct test, 3>{\n" 18863 " test{56, 23, \"hello\"},\n" 18864 " test{-1, 93463, \"world\"},\n" 18865 " test{ 7, 5, \"!!\"},\n" 18866 "};\n", 18867 Style); 18868 18869 verifyFormat("test demo[] = {\n" 18870 " {56, 23, \"hello\"},\n" 18871 "#if X\n" 18872 " {-1, 93463, \"world\"},\n" 18873 "#endif\n" 18874 " { 7, 5, \"!!\"}\n" 18875 "};\n", 18876 Style); 18877 18878 verifyFormat( 18879 "test demo[] = {\n" 18880 " { 7, 23,\n" 18881 " \"hello world i am a very long line that really, in any\"\n" 18882 " \"just world, ought to be split over multiple lines\"},\n" 18883 " {-1, 93463, \"world\"},\n" 18884 " {56, 5, \"!!\"}\n" 18885 "};\n", 18886 Style); 18887 18888 verifyFormat("return GradForUnaryCwise(g, {\n" 18889 " {{\"sign\"}, \"Sign\", " 18890 " {\"x\", \"dy\"}},\n" 18891 " { {\"dx\"}, \"Mul\", {\"dy\"" 18892 ", \"sign\"}},\n" 18893 "});\n", 18894 Style); 18895 18896 Style.ColumnLimit = 0; 18897 EXPECT_EQ( 18898 "test demo[] = {\n" 18899 " {56, 23, \"hello world i am a very long line that really, " 18900 "in any just world, ought to be split over multiple lines\"},\n" 18901 " {-1, 93463, " 18902 " \"world\"},\n" 18903 " { 7, 5, " 18904 " \"!!\"},\n" 18905 "};", 18906 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18907 "that really, in any just world, ought to be split over multiple " 18908 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18909 Style)); 18910 18911 Style.ColumnLimit = 80; 18912 verifyFormat("test demo[] = {\n" 18913 " {56, 23, /* a comment */ \"hello\"},\n" 18914 " {-1, 93463, \"world\"},\n" 18915 " { 7, 5, \"!!\"}\n" 18916 "};\n", 18917 Style); 18918 18919 verifyFormat("test demo[] = {\n" 18920 " {56, 23, \"hello\"},\n" 18921 " {-1, 93463, \"world\" /* comment here */},\n" 18922 " { 7, 5, \"!!\"}\n" 18923 "};\n", 18924 Style); 18925 18926 verifyFormat("test demo[] = {\n" 18927 " {56, /* a comment */ 23, \"hello\"},\n" 18928 " {-1, 93463, \"world\"},\n" 18929 " { 7, 5, \"!!\"}\n" 18930 "};\n", 18931 Style); 18932 18933 Style.ColumnLimit = 20; 18934 EXPECT_EQ( 18935 "demo = std::array<\n" 18936 " struct test, 3>{\n" 18937 " test{\n" 18938 " 56, 23,\n" 18939 " \"hello \"\n" 18940 " \"world i \"\n" 18941 " \"am a very \"\n" 18942 " \"long line \"\n" 18943 " \"that \"\n" 18944 " \"really, \"\n" 18945 " \"in any \"\n" 18946 " \"just \"\n" 18947 " \"world, \"\n" 18948 " \"ought to \"\n" 18949 " \"be split \"\n" 18950 " \"over \"\n" 18951 " \"multiple \"\n" 18952 " \"lines\"},\n" 18953 " test{-1, 93463,\n" 18954 " \"world\"},\n" 18955 " test{ 7, 5,\n" 18956 " \"!!\" },\n" 18957 "};", 18958 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18959 "i am a very long line that really, in any just world, ought " 18960 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18961 "test{7, 5, \"!!\"},};", 18962 Style)); 18963 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18964 Style = getLLVMStyleWithColumns(50); 18965 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18966 verifyFormat("static A x = {\n" 18967 " {{init1, init2, init3, init4},\n" 18968 " {init1, init2, init3, init4}}\n" 18969 "};", 18970 Style); 18971 Style.ColumnLimit = 100; 18972 EXPECT_EQ( 18973 "test demo[] = {\n" 18974 " {56, 23,\n" 18975 " \"hello world i am a very long line that really, in any just world" 18976 ", ought to be split over \"\n" 18977 " \"multiple lines\" },\n" 18978 " {-1, 93463, \"world\"},\n" 18979 " { 7, 5, \"!!\"},\n" 18980 "};", 18981 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18982 "that really, in any just world, ought to be split over multiple " 18983 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18984 Style)); 18985 18986 Style = getLLVMStyleWithColumns(50); 18987 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18988 Style.AlignConsecutiveAssignments = 18989 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18990 Style.AlignConsecutiveDeclarations = 18991 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18992 verifyFormat("struct test demo[] = {\n" 18993 " {56, 23, \"hello\"},\n" 18994 " {-1, 93463, \"world\"},\n" 18995 " { 7, 5, \"!!\"}\n" 18996 "};\n" 18997 "static A x = {\n" 18998 " {{init1, init2, init3, init4},\n" 18999 " {init1, init2, init3, init4}}\n" 19000 "};", 19001 Style); 19002 Style.ColumnLimit = 100; 19003 Style.AlignConsecutiveAssignments = 19004 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 19005 Style.AlignConsecutiveDeclarations = 19006 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 19007 verifyFormat("struct test demo[] = {\n" 19008 " {56, 23, \"hello\"},\n" 19009 " {-1, 93463, \"world\"},\n" 19010 " { 7, 5, \"!!\"}\n" 19011 "};\n" 19012 "struct test demo[4] = {\n" 19013 " { 56, 23, 21, \"oh\"}, // first line\n" 19014 " { -1, 93463, 22, \"my\"}, // second line\n" 19015 " { 7, 5, 1, \"goodness\"} // third line\n" 19016 " {234, 5, 1, \"gracious\"} // fourth line\n" 19017 "};\n", 19018 Style); 19019 EXPECT_EQ( 19020 "test demo[] = {\n" 19021 " {56,\n" 19022 " \"hello world i am a very long line that really, in any just world" 19023 ", ought to be split over \"\n" 19024 " \"multiple lines\", 23},\n" 19025 " {-1, \"world\", 93463},\n" 19026 " { 7, \"!!\", 5},\n" 19027 "};", 19028 format("test demo[] = {{56, \"hello world i am a very long line " 19029 "that really, in any just world, ought to be split over multiple " 19030 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 19031 Style)); 19032 } 19033 19034 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 19035 auto Style = getLLVMStyle(); 19036 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 19037 /* FIXME: This case gets misformatted. 19038 verifyFormat("auto foo = Items{\n" 19039 " Section{0, bar(), },\n" 19040 " Section{1, boo() }\n" 19041 "};\n", 19042 Style); 19043 */ 19044 verifyFormat("auto foo = Items{\n" 19045 " Section{\n" 19046 " 0, bar(),\n" 19047 " }\n" 19048 "};\n", 19049 Style); 19050 verifyFormat("struct test demo[] = {\n" 19051 " {56, 23, \"hello\"},\n" 19052 " {-1, 93463, \"world\"},\n" 19053 " {7, 5, \"!!\" }\n" 19054 "};\n", 19055 Style); 19056 verifyFormat("struct test demo[] = {\n" 19057 " {56, 23, \"hello\"}, // first line\n" 19058 " {-1, 93463, \"world\"}, // second line\n" 19059 " {7, 5, \"!!\" } // third line\n" 19060 "};\n", 19061 Style); 19062 verifyFormat("struct test demo[4] = {\n" 19063 " {56, 23, 21, \"oh\" }, // first line\n" 19064 " {-1, 93463, 22, \"my\" }, // second line\n" 19065 " {7, 5, 1, \"goodness\"} // third line\n" 19066 " {234, 5, 1, \"gracious\"} // fourth line\n" 19067 "};\n", 19068 Style); 19069 verifyFormat("struct test demo[3] = {\n" 19070 " {56, 23, \"hello\"},\n" 19071 " {-1, 93463, \"world\"},\n" 19072 " {7, 5, \"!!\" }\n" 19073 "};\n", 19074 Style); 19075 19076 verifyFormat("struct test demo[3] = {\n" 19077 " {int{56}, 23, \"hello\"},\n" 19078 " {int{-1}, 93463, \"world\"},\n" 19079 " {int{7}, 5, \"!!\" }\n" 19080 "};\n", 19081 Style); 19082 verifyFormat("struct test demo[] = {\n" 19083 " {56, 23, \"hello\"},\n" 19084 " {-1, 93463, \"world\"},\n" 19085 " {7, 5, \"!!\" },\n" 19086 "};\n", 19087 Style); 19088 verifyFormat("test demo[] = {\n" 19089 " {56, 23, \"hello\"},\n" 19090 " {-1, 93463, \"world\"},\n" 19091 " {7, 5, \"!!\" },\n" 19092 "};\n", 19093 Style); 19094 verifyFormat("demo = std::array<struct test, 3>{\n" 19095 " test{56, 23, \"hello\"},\n" 19096 " test{-1, 93463, \"world\"},\n" 19097 " test{7, 5, \"!!\" },\n" 19098 "};\n", 19099 Style); 19100 verifyFormat("test demo[] = {\n" 19101 " {56, 23, \"hello\"},\n" 19102 "#if X\n" 19103 " {-1, 93463, \"world\"},\n" 19104 "#endif\n" 19105 " {7, 5, \"!!\" }\n" 19106 "};\n", 19107 Style); 19108 verifyFormat( 19109 "test demo[] = {\n" 19110 " {7, 23,\n" 19111 " \"hello world i am a very long line that really, in any\"\n" 19112 " \"just world, ought to be split over multiple lines\"},\n" 19113 " {-1, 93463, \"world\" },\n" 19114 " {56, 5, \"!!\" }\n" 19115 "};\n", 19116 Style); 19117 19118 verifyFormat("return GradForUnaryCwise(g, {\n" 19119 " {{\"sign\"}, \"Sign\", {\"x\", " 19120 "\"dy\"} },\n" 19121 " {{\"dx\"}, \"Mul\", " 19122 "{\"dy\", \"sign\"}},\n" 19123 "});\n", 19124 Style); 19125 19126 Style.ColumnLimit = 0; 19127 EXPECT_EQ( 19128 "test demo[] = {\n" 19129 " {56, 23, \"hello world i am a very long line that really, in any " 19130 "just world, ought to be split over multiple lines\"},\n" 19131 " {-1, 93463, \"world\" " 19132 " },\n" 19133 " {7, 5, \"!!\" " 19134 " },\n" 19135 "};", 19136 format("test demo[] = {{56, 23, \"hello world i am a very long line " 19137 "that really, in any just world, ought to be split over multiple " 19138 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 19139 Style)); 19140 19141 Style.ColumnLimit = 80; 19142 verifyFormat("test demo[] = {\n" 19143 " {56, 23, /* a comment */ \"hello\"},\n" 19144 " {-1, 93463, \"world\" },\n" 19145 " {7, 5, \"!!\" }\n" 19146 "};\n", 19147 Style); 19148 19149 verifyFormat("test demo[] = {\n" 19150 " {56, 23, \"hello\" },\n" 19151 " {-1, 93463, \"world\" /* comment here */},\n" 19152 " {7, 5, \"!!\" }\n" 19153 "};\n", 19154 Style); 19155 19156 verifyFormat("test demo[] = {\n" 19157 " {56, /* a comment */ 23, \"hello\"},\n" 19158 " {-1, 93463, \"world\"},\n" 19159 " {7, 5, \"!!\" }\n" 19160 "};\n", 19161 Style); 19162 19163 Style.ColumnLimit = 20; 19164 EXPECT_EQ( 19165 "demo = std::array<\n" 19166 " struct test, 3>{\n" 19167 " test{\n" 19168 " 56, 23,\n" 19169 " \"hello \"\n" 19170 " \"world i \"\n" 19171 " \"am a very \"\n" 19172 " \"long line \"\n" 19173 " \"that \"\n" 19174 " \"really, \"\n" 19175 " \"in any \"\n" 19176 " \"just \"\n" 19177 " \"world, \"\n" 19178 " \"ought to \"\n" 19179 " \"be split \"\n" 19180 " \"over \"\n" 19181 " \"multiple \"\n" 19182 " \"lines\"},\n" 19183 " test{-1, 93463,\n" 19184 " \"world\"},\n" 19185 " test{7, 5,\n" 19186 " \"!!\" },\n" 19187 "};", 19188 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 19189 "i am a very long line that really, in any just world, ought " 19190 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 19191 "test{7, 5, \"!!\"},};", 19192 Style)); 19193 19194 // This caused a core dump by enabling Alignment in the LLVMStyle globally 19195 Style = getLLVMStyleWithColumns(50); 19196 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 19197 verifyFormat("static A x = {\n" 19198 " {{init1, init2, init3, init4},\n" 19199 " {init1, init2, init3, init4}}\n" 19200 "};", 19201 Style); 19202 Style.ColumnLimit = 100; 19203 EXPECT_EQ( 19204 "test demo[] = {\n" 19205 " {56, 23,\n" 19206 " \"hello world i am a very long line that really, in any just world" 19207 ", ought to be split over \"\n" 19208 " \"multiple lines\" },\n" 19209 " {-1, 93463, \"world\"},\n" 19210 " {7, 5, \"!!\" },\n" 19211 "};", 19212 format("test demo[] = {{56, 23, \"hello world i am a very long line " 19213 "that really, in any just world, ought to be split over multiple " 19214 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 19215 Style)); 19216 } 19217 19218 TEST_F(FormatTest, UnderstandsPragmas) { 19219 verifyFormat("#pragma omp reduction(| : var)"); 19220 verifyFormat("#pragma omp reduction(+ : var)"); 19221 19222 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 19223 "(including parentheses).", 19224 format("#pragma mark Any non-hyphenated or hyphenated string " 19225 "(including parentheses).")); 19226 } 19227 19228 TEST_F(FormatTest, UnderstandPragmaOption) { 19229 verifyFormat("#pragma option -C -A"); 19230 19231 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 19232 } 19233 19234 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 19235 FormatStyle Style = getLLVMStyleWithColumns(20); 19236 19237 // See PR41213 19238 EXPECT_EQ("/*\n" 19239 " *\t9012345\n" 19240 " * /8901\n" 19241 " */", 19242 format("/*\n" 19243 " *\t9012345 /8901\n" 19244 " */", 19245 Style)); 19246 EXPECT_EQ("/*\n" 19247 " *345678\n" 19248 " *\t/8901\n" 19249 " */", 19250 format("/*\n" 19251 " *345678\t/8901\n" 19252 " */", 19253 Style)); 19254 19255 verifyFormat("int a; // the\n" 19256 " // comment", 19257 Style); 19258 EXPECT_EQ("int a; /* first line\n" 19259 " * second\n" 19260 " * line third\n" 19261 " * line\n" 19262 " */", 19263 format("int a; /* first line\n" 19264 " * second\n" 19265 " * line third\n" 19266 " * line\n" 19267 " */", 19268 Style)); 19269 EXPECT_EQ("int a; // first line\n" 19270 " // second\n" 19271 " // line third\n" 19272 " // line", 19273 format("int a; // first line\n" 19274 " // second line\n" 19275 " // third line", 19276 Style)); 19277 19278 Style.PenaltyExcessCharacter = 90; 19279 verifyFormat("int a; // the comment", Style); 19280 EXPECT_EQ("int a; // the comment\n" 19281 " // aaa", 19282 format("int a; // the comment aaa", Style)); 19283 EXPECT_EQ("int a; /* first line\n" 19284 " * second line\n" 19285 " * third line\n" 19286 " */", 19287 format("int a; /* first line\n" 19288 " * second line\n" 19289 " * third line\n" 19290 " */", 19291 Style)); 19292 EXPECT_EQ("int a; // first line\n" 19293 " // second line\n" 19294 " // third line", 19295 format("int a; // first line\n" 19296 " // second line\n" 19297 " // third line", 19298 Style)); 19299 // FIXME: Investigate why this is not getting the same layout as the test 19300 // above. 19301 EXPECT_EQ("int a; /* first line\n" 19302 " * second line\n" 19303 " * third line\n" 19304 " */", 19305 format("int a; /* first line second line third line" 19306 "\n*/", 19307 Style)); 19308 19309 EXPECT_EQ("// foo bar baz bazfoo\n" 19310 "// foo bar foo bar\n", 19311 format("// foo bar baz bazfoo\n" 19312 "// foo bar foo bar\n", 19313 Style)); 19314 EXPECT_EQ("// foo bar baz bazfoo\n" 19315 "// foo bar foo bar\n", 19316 format("// foo bar baz bazfoo\n" 19317 "// foo bar foo bar\n", 19318 Style)); 19319 19320 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 19321 // next one. 19322 EXPECT_EQ("// foo bar baz bazfoo\n" 19323 "// bar foo bar\n", 19324 format("// foo bar baz bazfoo bar\n" 19325 "// foo bar\n", 19326 Style)); 19327 19328 EXPECT_EQ("// foo bar baz bazfoo\n" 19329 "// foo bar baz bazfoo\n" 19330 "// bar foo bar\n", 19331 format("// foo bar baz bazfoo\n" 19332 "// foo bar baz bazfoo bar\n" 19333 "// foo bar\n", 19334 Style)); 19335 19336 EXPECT_EQ("// foo bar baz bazfoo\n" 19337 "// foo bar baz bazfoo\n" 19338 "// bar foo bar\n", 19339 format("// foo bar baz bazfoo\n" 19340 "// foo bar baz bazfoo bar\n" 19341 "// foo bar\n", 19342 Style)); 19343 19344 // Make sure we do not keep protruding characters if strict mode reflow is 19345 // cheaper than keeping protruding characters. 19346 Style.ColumnLimit = 21; 19347 EXPECT_EQ( 19348 "// foo foo foo foo\n" 19349 "// foo foo foo foo\n" 19350 "// foo foo foo foo\n", 19351 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 19352 19353 EXPECT_EQ("int a = /* long block\n" 19354 " comment */\n" 19355 " 42;", 19356 format("int a = /* long block comment */ 42;", Style)); 19357 } 19358 19359 TEST_F(FormatTest, BreakPenaltyAfterLParen) { 19360 FormatStyle Style = getLLVMStyle(); 19361 Style.ColumnLimit = 8; 19362 Style.PenaltyExcessCharacter = 15; 19363 verifyFormat("int foo(\n" 19364 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 19365 Style); 19366 Style.PenaltyBreakOpenParenthesis = 200; 19367 EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", 19368 format("int foo(\n" 19369 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 19370 Style)); 19371 } 19372 19373 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { 19374 FormatStyle Style = getLLVMStyle(); 19375 Style.ColumnLimit = 5; 19376 Style.PenaltyExcessCharacter = 150; 19377 verifyFormat("foo((\n" 19378 " int)aaaaaaaaaaaaaaaaaaaaaaaa);", 19379 19380 Style); 19381 Style.PenaltyBreakOpenParenthesis = 100000; 19382 EXPECT_EQ("foo((int)\n" 19383 " aaaaaaaaaaaaaaaaaaaaaaaa);", 19384 format("foo((\n" 19385 "int)aaaaaaaaaaaaaaaaaaaaaaaa);", 19386 Style)); 19387 } 19388 19389 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { 19390 FormatStyle Style = getLLVMStyle(); 19391 Style.ColumnLimit = 4; 19392 Style.PenaltyExcessCharacter = 100; 19393 verifyFormat("for (\n" 19394 " int iiiiiiiiiiiiiiiii =\n" 19395 " 0;\n" 19396 " iiiiiiiiiiiiiiiii <\n" 19397 " 2;\n" 19398 " iiiiiiiiiiiiiiiii++) {\n" 19399 "}", 19400 19401 Style); 19402 Style.PenaltyBreakOpenParenthesis = 1250; 19403 EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n" 19404 " 0;\n" 19405 " iiiiiiiiiiiiiiiii <\n" 19406 " 2;\n" 19407 " iiiiiiiiiiiiiiiii++) {\n" 19408 "}", 19409 format("for (\n" 19410 " int iiiiiiiiiiiiiiiii =\n" 19411 " 0;\n" 19412 " iiiiiiiiiiiiiiiii <\n" 19413 " 2;\n" 19414 " iiiiiiiiiiiiiiiii++) {\n" 19415 "}", 19416 Style)); 19417 } 19418 19419 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 19420 for (size_t i = 1; i < Styles.size(); ++i) \ 19421 EXPECT_EQ(Styles[0], Styles[i]) \ 19422 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 19423 19424 TEST_F(FormatTest, GetsPredefinedStyleByName) { 19425 SmallVector<FormatStyle, 3> Styles; 19426 Styles.resize(3); 19427 19428 Styles[0] = getLLVMStyle(); 19429 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 19430 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 19431 EXPECT_ALL_STYLES_EQUAL(Styles); 19432 19433 Styles[0] = getGoogleStyle(); 19434 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 19435 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 19436 EXPECT_ALL_STYLES_EQUAL(Styles); 19437 19438 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 19439 EXPECT_TRUE( 19440 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 19441 EXPECT_TRUE( 19442 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 19443 EXPECT_ALL_STYLES_EQUAL(Styles); 19444 19445 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 19446 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 19447 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 19448 EXPECT_ALL_STYLES_EQUAL(Styles); 19449 19450 Styles[0] = getMozillaStyle(); 19451 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 19452 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 19453 EXPECT_ALL_STYLES_EQUAL(Styles); 19454 19455 Styles[0] = getWebKitStyle(); 19456 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 19457 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 19458 EXPECT_ALL_STYLES_EQUAL(Styles); 19459 19460 Styles[0] = getGNUStyle(); 19461 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 19462 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 19463 EXPECT_ALL_STYLES_EQUAL(Styles); 19464 19465 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 19466 } 19467 19468 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 19469 SmallVector<FormatStyle, 8> Styles; 19470 Styles.resize(2); 19471 19472 Styles[0] = getGoogleStyle(); 19473 Styles[1] = getLLVMStyle(); 19474 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 19475 EXPECT_ALL_STYLES_EQUAL(Styles); 19476 19477 Styles.resize(5); 19478 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 19479 Styles[1] = getLLVMStyle(); 19480 Styles[1].Language = FormatStyle::LK_JavaScript; 19481 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 19482 19483 Styles[2] = getLLVMStyle(); 19484 Styles[2].Language = FormatStyle::LK_JavaScript; 19485 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 19486 "BasedOnStyle: Google", 19487 &Styles[2]) 19488 .value()); 19489 19490 Styles[3] = getLLVMStyle(); 19491 Styles[3].Language = FormatStyle::LK_JavaScript; 19492 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 19493 "Language: JavaScript", 19494 &Styles[3]) 19495 .value()); 19496 19497 Styles[4] = getLLVMStyle(); 19498 Styles[4].Language = FormatStyle::LK_JavaScript; 19499 EXPECT_EQ(0, parseConfiguration("---\n" 19500 "BasedOnStyle: LLVM\n" 19501 "IndentWidth: 123\n" 19502 "---\n" 19503 "BasedOnStyle: Google\n" 19504 "Language: JavaScript", 19505 &Styles[4]) 19506 .value()); 19507 EXPECT_ALL_STYLES_EQUAL(Styles); 19508 } 19509 19510 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 19511 Style.FIELD = false; \ 19512 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 19513 EXPECT_TRUE(Style.FIELD); \ 19514 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 19515 EXPECT_FALSE(Style.FIELD); 19516 19517 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 19518 19519 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 19520 Style.STRUCT.FIELD = false; \ 19521 EXPECT_EQ(0, \ 19522 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 19523 .value()); \ 19524 EXPECT_TRUE(Style.STRUCT.FIELD); \ 19525 EXPECT_EQ(0, \ 19526 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 19527 .value()); \ 19528 EXPECT_FALSE(Style.STRUCT.FIELD); 19529 19530 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 19531 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 19532 19533 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 19534 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 19535 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 19536 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 19537 19538 TEST_F(FormatTest, ParsesConfigurationBools) { 19539 FormatStyle Style = {}; 19540 Style.Language = FormatStyle::LK_Cpp; 19541 CHECK_PARSE_BOOL(AlignTrailingComments); 19542 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 19543 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 19544 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 19545 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 19546 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 19547 CHECK_PARSE_BOOL(BinPackArguments); 19548 CHECK_PARSE_BOOL(BinPackParameters); 19549 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 19550 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 19551 CHECK_PARSE_BOOL(BreakStringLiterals); 19552 CHECK_PARSE_BOOL(CompactNamespaces); 19553 CHECK_PARSE_BOOL(DeriveLineEnding); 19554 CHECK_PARSE_BOOL(DerivePointerAlignment); 19555 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 19556 CHECK_PARSE_BOOL(DisableFormat); 19557 CHECK_PARSE_BOOL(IndentAccessModifiers); 19558 CHECK_PARSE_BOOL(IndentCaseLabels); 19559 CHECK_PARSE_BOOL(IndentCaseBlocks); 19560 CHECK_PARSE_BOOL(IndentGotoLabels); 19561 CHECK_PARSE_BOOL_FIELD(IndentRequiresClause, "IndentRequires"); 19562 CHECK_PARSE_BOOL(IndentRequiresClause); 19563 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 19564 CHECK_PARSE_BOOL(InsertBraces); 19565 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 19566 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 19567 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 19568 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 19569 CHECK_PARSE_BOOL(ReflowComments); 19570 CHECK_PARSE_BOOL(RemoveBracesLLVM); 19571 CHECK_PARSE_BOOL(SortUsingDeclarations); 19572 CHECK_PARSE_BOOL(SpacesInParentheses); 19573 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 19574 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 19575 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 19576 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 19577 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 19578 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 19579 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 19580 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 19581 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 19582 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 19583 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 19584 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 19585 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 19586 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 19587 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 19588 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 19589 CHECK_PARSE_BOOL(UseCRLF); 19590 19591 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 19592 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 19593 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 19594 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 19595 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 19596 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 19597 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 19598 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 19599 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 19600 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 19601 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 19602 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 19603 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 19604 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 19605 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 19606 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 19607 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 19608 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements); 19609 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros); 19610 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19611 AfterFunctionDeclarationName); 19612 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19613 AfterFunctionDefinitionName); 19614 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros); 19615 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator); 19616 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses); 19617 } 19618 19619 #undef CHECK_PARSE_BOOL 19620 19621 TEST_F(FormatTest, ParsesConfiguration) { 19622 FormatStyle Style = {}; 19623 Style.Language = FormatStyle::LK_Cpp; 19624 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 19625 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 19626 ConstructorInitializerIndentWidth, 1234u); 19627 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 19628 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 19629 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 19630 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 19631 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 19632 PenaltyBreakBeforeFirstCallParameter, 1234u); 19633 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 19634 PenaltyBreakTemplateDeclaration, 1234u); 19635 CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis, 19636 1234u); 19637 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 19638 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 19639 PenaltyReturnTypeOnItsOwnLine, 1234u); 19640 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 19641 SpacesBeforeTrailingComments, 1234u); 19642 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 19643 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 19644 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 19645 19646 Style.QualifierAlignment = FormatStyle::QAS_Right; 19647 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 19648 FormatStyle::QAS_Leave); 19649 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 19650 FormatStyle::QAS_Right); 19651 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 19652 FormatStyle::QAS_Left); 19653 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 19654 FormatStyle::QAS_Custom); 19655 19656 Style.QualifierOrder.clear(); 19657 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 19658 std::vector<std::string>({"const", "volatile", "type"})); 19659 Style.QualifierOrder.clear(); 19660 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 19661 std::vector<std::string>({"const", "type"})); 19662 Style.QualifierOrder.clear(); 19663 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 19664 std::vector<std::string>({"volatile", "type"})); 19665 19666 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 19667 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 19668 FormatStyle::ACS_None); 19669 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 19670 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 19671 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 19672 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 19673 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 19674 AlignConsecutiveAssignments, 19675 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19676 // For backwards compability, false / true should still parse 19677 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 19678 FormatStyle::ACS_None); 19679 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 19680 FormatStyle::ACS_Consecutive); 19681 19682 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 19683 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 19684 FormatStyle::ACS_None); 19685 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 19686 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 19687 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 19688 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 19689 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 19690 AlignConsecutiveBitFields, 19691 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19692 // For backwards compability, false / true should still parse 19693 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 19694 FormatStyle::ACS_None); 19695 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 19696 FormatStyle::ACS_Consecutive); 19697 19698 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 19699 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 19700 FormatStyle::ACS_None); 19701 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 19702 FormatStyle::ACS_Consecutive); 19703 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 19704 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 19705 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 19706 AlignConsecutiveMacros, 19707 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19708 // For backwards compability, false / true should still parse 19709 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 19710 FormatStyle::ACS_None); 19711 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 19712 FormatStyle::ACS_Consecutive); 19713 19714 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 19715 CHECK_PARSE("AlignConsecutiveDeclarations: None", 19716 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 19717 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 19718 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 19719 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 19720 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 19721 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 19722 AlignConsecutiveDeclarations, 19723 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19724 // For backwards compability, false / true should still parse 19725 CHECK_PARSE("AlignConsecutiveDeclarations: false", 19726 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 19727 CHECK_PARSE("AlignConsecutiveDeclarations: true", 19728 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 19729 19730 Style.PointerAlignment = FormatStyle::PAS_Middle; 19731 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 19732 FormatStyle::PAS_Left); 19733 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 19734 FormatStyle::PAS_Right); 19735 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 19736 FormatStyle::PAS_Middle); 19737 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 19738 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 19739 FormatStyle::RAS_Pointer); 19740 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 19741 FormatStyle::RAS_Left); 19742 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 19743 FormatStyle::RAS_Right); 19744 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 19745 FormatStyle::RAS_Middle); 19746 // For backward compatibility: 19747 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 19748 FormatStyle::PAS_Left); 19749 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 19750 FormatStyle::PAS_Right); 19751 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 19752 FormatStyle::PAS_Middle); 19753 19754 Style.Standard = FormatStyle::LS_Auto; 19755 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 19756 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 19757 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 19758 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 19759 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 19760 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 19761 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 19762 // Legacy aliases: 19763 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 19764 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 19765 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 19766 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 19767 19768 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 19769 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 19770 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 19771 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 19772 FormatStyle::BOS_None); 19773 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 19774 FormatStyle::BOS_All); 19775 // For backward compatibility: 19776 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 19777 FormatStyle::BOS_None); 19778 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 19779 FormatStyle::BOS_All); 19780 19781 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 19782 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 19783 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 19784 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 19785 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 19786 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 19787 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 19788 // For backward compatibility: 19789 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 19790 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 19791 19792 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 19793 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 19794 FormatStyle::BILS_AfterComma); 19795 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 19796 FormatStyle::BILS_BeforeComma); 19797 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 19798 FormatStyle::BILS_AfterColon); 19799 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 19800 FormatStyle::BILS_BeforeColon); 19801 // For backward compatibility: 19802 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 19803 FormatStyle::BILS_BeforeComma); 19804 19805 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19806 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 19807 FormatStyle::PCIS_Never); 19808 CHECK_PARSE("PackConstructorInitializers: BinPack", 19809 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19810 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 19811 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19812 CHECK_PARSE("PackConstructorInitializers: NextLine", 19813 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19814 // For backward compatibility: 19815 CHECK_PARSE("BasedOnStyle: Google\n" 19816 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19817 "AllowAllConstructorInitializersOnNextLine: false", 19818 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19819 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19820 CHECK_PARSE("BasedOnStyle: Google\n" 19821 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 19822 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19823 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19824 "AllowAllConstructorInitializersOnNextLine: true", 19825 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19826 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19827 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19828 "AllowAllConstructorInitializersOnNextLine: false", 19829 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19830 19831 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 19832 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 19833 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 19834 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 19835 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 19836 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 19837 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 19838 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 19839 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 19840 19841 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19842 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 19843 FormatStyle::BAS_Align); 19844 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 19845 FormatStyle::BAS_DontAlign); 19846 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 19847 FormatStyle::BAS_AlwaysBreak); 19848 CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket, 19849 FormatStyle::BAS_BlockIndent); 19850 // For backward compatibility: 19851 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 19852 FormatStyle::BAS_DontAlign); 19853 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 19854 FormatStyle::BAS_Align); 19855 19856 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 19857 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 19858 FormatStyle::ENAS_DontAlign); 19859 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 19860 FormatStyle::ENAS_Left); 19861 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 19862 FormatStyle::ENAS_Right); 19863 // For backward compatibility: 19864 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 19865 FormatStyle::ENAS_Left); 19866 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 19867 FormatStyle::ENAS_Right); 19868 19869 Style.AlignOperands = FormatStyle::OAS_Align; 19870 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 19871 FormatStyle::OAS_DontAlign); 19872 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 19873 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 19874 FormatStyle::OAS_AlignAfterOperator); 19875 // For backward compatibility: 19876 CHECK_PARSE("AlignOperands: false", AlignOperands, 19877 FormatStyle::OAS_DontAlign); 19878 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 19879 19880 Style.UseTab = FormatStyle::UT_ForIndentation; 19881 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 19882 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 19883 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 19884 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 19885 FormatStyle::UT_ForContinuationAndIndentation); 19886 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 19887 FormatStyle::UT_AlignWithSpaces); 19888 // For backward compatibility: 19889 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 19890 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 19891 19892 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 19893 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 19894 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19895 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 19896 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 19897 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 19898 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19899 // For backward compatibility: 19900 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 19901 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19902 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 19903 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19904 19905 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 19906 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 19907 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19908 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 19909 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 19910 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 19911 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 19912 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 19913 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19914 // For backward compatibility: 19915 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 19916 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19917 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 19918 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19919 19920 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 19921 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 19922 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 19923 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 19924 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 19925 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 19926 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 19927 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 19928 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 19929 19930 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 19931 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 19932 FormatStyle::SBPO_Never); 19933 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 19934 FormatStyle::SBPO_Always); 19935 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 19936 FormatStyle::SBPO_ControlStatements); 19937 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 19938 SpaceBeforeParens, 19939 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19940 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 19941 FormatStyle::SBPO_NonEmptyParentheses); 19942 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 19943 FormatStyle::SBPO_Custom); 19944 // For backward compatibility: 19945 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 19946 FormatStyle::SBPO_Never); 19947 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 19948 FormatStyle::SBPO_ControlStatements); 19949 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 19950 SpaceBeforeParens, 19951 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19952 19953 Style.ColumnLimit = 123; 19954 FormatStyle BaseStyle = getLLVMStyle(); 19955 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 19956 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 19957 19958 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 19959 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 19960 FormatStyle::BS_Attach); 19961 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 19962 FormatStyle::BS_Linux); 19963 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 19964 FormatStyle::BS_Mozilla); 19965 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 19966 FormatStyle::BS_Stroustrup); 19967 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 19968 FormatStyle::BS_Allman); 19969 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 19970 FormatStyle::BS_Whitesmiths); 19971 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 19972 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 19973 FormatStyle::BS_WebKit); 19974 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 19975 FormatStyle::BS_Custom); 19976 19977 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 19978 CHECK_PARSE("BraceWrapping:\n" 19979 " AfterControlStatement: MultiLine", 19980 BraceWrapping.AfterControlStatement, 19981 FormatStyle::BWACS_MultiLine); 19982 CHECK_PARSE("BraceWrapping:\n" 19983 " AfterControlStatement: Always", 19984 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19985 CHECK_PARSE("BraceWrapping:\n" 19986 " AfterControlStatement: Never", 19987 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19988 // For backward compatibility: 19989 CHECK_PARSE("BraceWrapping:\n" 19990 " AfterControlStatement: true", 19991 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19992 CHECK_PARSE("BraceWrapping:\n" 19993 " AfterControlStatement: false", 19994 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19995 19996 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 19997 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 19998 FormatStyle::RTBS_None); 19999 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 20000 FormatStyle::RTBS_All); 20001 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 20002 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 20003 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 20004 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 20005 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 20006 AlwaysBreakAfterReturnType, 20007 FormatStyle::RTBS_TopLevelDefinitions); 20008 20009 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 20010 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 20011 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 20012 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 20013 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 20014 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 20015 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 20016 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 20017 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 20018 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 20019 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 20020 20021 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 20022 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 20023 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 20024 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 20025 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 20026 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 20027 AlwaysBreakAfterDefinitionReturnType, 20028 FormatStyle::DRTBS_TopLevel); 20029 20030 Style.NamespaceIndentation = FormatStyle::NI_All; 20031 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 20032 FormatStyle::NI_None); 20033 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 20034 FormatStyle::NI_Inner); 20035 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 20036 FormatStyle::NI_All); 20037 20038 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 20039 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 20040 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 20041 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 20042 AllowShortIfStatementsOnASingleLine, 20043 FormatStyle::SIS_WithoutElse); 20044 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 20045 AllowShortIfStatementsOnASingleLine, 20046 FormatStyle::SIS_OnlyFirstIf); 20047 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 20048 AllowShortIfStatementsOnASingleLine, 20049 FormatStyle::SIS_AllIfsAndElse); 20050 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 20051 AllowShortIfStatementsOnASingleLine, 20052 FormatStyle::SIS_OnlyFirstIf); 20053 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 20054 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 20055 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 20056 AllowShortIfStatementsOnASingleLine, 20057 FormatStyle::SIS_WithoutElse); 20058 20059 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 20060 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 20061 FormatStyle::IEBS_AfterExternBlock); 20062 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 20063 FormatStyle::IEBS_Indent); 20064 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 20065 FormatStyle::IEBS_NoIndent); 20066 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 20067 FormatStyle::IEBS_Indent); 20068 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 20069 FormatStyle::IEBS_NoIndent); 20070 20071 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 20072 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 20073 FormatStyle::BFCS_Both); 20074 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 20075 FormatStyle::BFCS_None); 20076 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 20077 FormatStyle::BFCS_Before); 20078 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 20079 FormatStyle::BFCS_After); 20080 20081 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 20082 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 20083 FormatStyle::SJSIO_After); 20084 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 20085 FormatStyle::SJSIO_Before); 20086 20087 // FIXME: This is required because parsing a configuration simply overwrites 20088 // the first N elements of the list instead of resetting it. 20089 Style.ForEachMacros.clear(); 20090 std::vector<std::string> BoostForeach; 20091 BoostForeach.push_back("BOOST_FOREACH"); 20092 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 20093 std::vector<std::string> BoostAndQForeach; 20094 BoostAndQForeach.push_back("BOOST_FOREACH"); 20095 BoostAndQForeach.push_back("Q_FOREACH"); 20096 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 20097 BoostAndQForeach); 20098 20099 Style.IfMacros.clear(); 20100 std::vector<std::string> CustomIfs; 20101 CustomIfs.push_back("MYIF"); 20102 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 20103 20104 Style.AttributeMacros.clear(); 20105 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 20106 std::vector<std::string>{"__capability"}); 20107 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 20108 std::vector<std::string>({"attr1", "attr2"})); 20109 20110 Style.StatementAttributeLikeMacros.clear(); 20111 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 20112 StatementAttributeLikeMacros, 20113 std::vector<std::string>({"emit", "Q_EMIT"})); 20114 20115 Style.StatementMacros.clear(); 20116 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 20117 std::vector<std::string>{"QUNUSED"}); 20118 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 20119 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 20120 20121 Style.NamespaceMacros.clear(); 20122 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 20123 std::vector<std::string>{"TESTSUITE"}); 20124 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 20125 std::vector<std::string>({"TESTSUITE", "SUITE"})); 20126 20127 Style.WhitespaceSensitiveMacros.clear(); 20128 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 20129 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 20130 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 20131 WhitespaceSensitiveMacros, 20132 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 20133 Style.WhitespaceSensitiveMacros.clear(); 20134 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 20135 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 20136 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 20137 WhitespaceSensitiveMacros, 20138 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 20139 20140 Style.IncludeStyle.IncludeCategories.clear(); 20141 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 20142 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 20143 CHECK_PARSE("IncludeCategories:\n" 20144 " - Regex: abc/.*\n" 20145 " Priority: 2\n" 20146 " - Regex: .*\n" 20147 " Priority: 1\n" 20148 " CaseSensitive: true\n", 20149 IncludeStyle.IncludeCategories, ExpectedCategories); 20150 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 20151 "abc$"); 20152 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 20153 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 20154 20155 Style.SortIncludes = FormatStyle::SI_Never; 20156 CHECK_PARSE("SortIncludes: true", SortIncludes, 20157 FormatStyle::SI_CaseSensitive); 20158 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 20159 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 20160 FormatStyle::SI_CaseInsensitive); 20161 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 20162 FormatStyle::SI_CaseSensitive); 20163 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 20164 20165 Style.RawStringFormats.clear(); 20166 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 20167 { 20168 FormatStyle::LK_TextProto, 20169 {"pb", "proto"}, 20170 {"PARSE_TEXT_PROTO"}, 20171 /*CanonicalDelimiter=*/"", 20172 "llvm", 20173 }, 20174 { 20175 FormatStyle::LK_Cpp, 20176 {"cc", "cpp"}, 20177 {"C_CODEBLOCK", "CPPEVAL"}, 20178 /*CanonicalDelimiter=*/"cc", 20179 /*BasedOnStyle=*/"", 20180 }, 20181 }; 20182 20183 CHECK_PARSE("RawStringFormats:\n" 20184 " - Language: TextProto\n" 20185 " Delimiters:\n" 20186 " - 'pb'\n" 20187 " - 'proto'\n" 20188 " EnclosingFunctions:\n" 20189 " - 'PARSE_TEXT_PROTO'\n" 20190 " BasedOnStyle: llvm\n" 20191 " - Language: Cpp\n" 20192 " Delimiters:\n" 20193 " - 'cc'\n" 20194 " - 'cpp'\n" 20195 " EnclosingFunctions:\n" 20196 " - 'C_CODEBLOCK'\n" 20197 " - 'CPPEVAL'\n" 20198 " CanonicalDelimiter: 'cc'", 20199 RawStringFormats, ExpectedRawStringFormats); 20200 20201 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 20202 " Minimum: 0\n" 20203 " Maximum: 0", 20204 SpacesInLineCommentPrefix.Minimum, 0u); 20205 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 20206 Style.SpacesInLineCommentPrefix.Minimum = 1; 20207 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 20208 " Minimum: 2", 20209 SpacesInLineCommentPrefix.Minimum, 0u); 20210 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 20211 " Maximum: -1", 20212 SpacesInLineCommentPrefix.Maximum, -1u); 20213 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 20214 " Minimum: 2", 20215 SpacesInLineCommentPrefix.Minimum, 2u); 20216 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 20217 " Maximum: 1", 20218 SpacesInLineCommentPrefix.Maximum, 1u); 20219 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 20220 20221 Style.SpacesInAngles = FormatStyle::SIAS_Always; 20222 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 20223 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 20224 FormatStyle::SIAS_Always); 20225 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 20226 // For backward compatibility: 20227 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 20228 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 20229 20230 CHECK_PARSE("RequiresClausePosition: WithPreceding", RequiresClausePosition, 20231 FormatStyle::RCPS_WithPreceding); 20232 CHECK_PARSE("RequiresClausePosition: WithFollowing", RequiresClausePosition, 20233 FormatStyle::RCPS_WithFollowing); 20234 CHECK_PARSE("RequiresClausePosition: SingleLine", RequiresClausePosition, 20235 FormatStyle::RCPS_SingleLine); 20236 CHECK_PARSE("RequiresClausePosition: OwnLine", RequiresClausePosition, 20237 FormatStyle::RCPS_OwnLine); 20238 20239 CHECK_PARSE("BreakBeforeConceptDeclarations: Never", 20240 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Never); 20241 CHECK_PARSE("BreakBeforeConceptDeclarations: Always", 20242 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always); 20243 CHECK_PARSE("BreakBeforeConceptDeclarations: Allowed", 20244 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed); 20245 // For backward compatibility: 20246 CHECK_PARSE("BreakBeforeConceptDeclarations: true", 20247 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always); 20248 CHECK_PARSE("BreakBeforeConceptDeclarations: false", 20249 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed); 20250 } 20251 20252 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 20253 FormatStyle Style = {}; 20254 Style.Language = FormatStyle::LK_Cpp; 20255 CHECK_PARSE("Language: Cpp\n" 20256 "IndentWidth: 12", 20257 IndentWidth, 12u); 20258 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 20259 "IndentWidth: 34", 20260 &Style), 20261 ParseError::Unsuitable); 20262 FormatStyle BinPackedTCS = {}; 20263 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 20264 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 20265 "InsertTrailingCommas: Wrapped", 20266 &BinPackedTCS), 20267 ParseError::BinPackTrailingCommaConflict); 20268 EXPECT_EQ(12u, Style.IndentWidth); 20269 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 20270 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 20271 20272 Style.Language = FormatStyle::LK_JavaScript; 20273 CHECK_PARSE("Language: JavaScript\n" 20274 "IndentWidth: 12", 20275 IndentWidth, 12u); 20276 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 20277 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 20278 "IndentWidth: 34", 20279 &Style), 20280 ParseError::Unsuitable); 20281 EXPECT_EQ(23u, Style.IndentWidth); 20282 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 20283 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 20284 20285 CHECK_PARSE("BasedOnStyle: LLVM\n" 20286 "IndentWidth: 67", 20287 IndentWidth, 67u); 20288 20289 CHECK_PARSE("---\n" 20290 "Language: JavaScript\n" 20291 "IndentWidth: 12\n" 20292 "---\n" 20293 "Language: Cpp\n" 20294 "IndentWidth: 34\n" 20295 "...\n", 20296 IndentWidth, 12u); 20297 20298 Style.Language = FormatStyle::LK_Cpp; 20299 CHECK_PARSE("---\n" 20300 "Language: JavaScript\n" 20301 "IndentWidth: 12\n" 20302 "---\n" 20303 "Language: Cpp\n" 20304 "IndentWidth: 34\n" 20305 "...\n", 20306 IndentWidth, 34u); 20307 CHECK_PARSE("---\n" 20308 "IndentWidth: 78\n" 20309 "---\n" 20310 "Language: JavaScript\n" 20311 "IndentWidth: 56\n" 20312 "...\n", 20313 IndentWidth, 78u); 20314 20315 Style.ColumnLimit = 123; 20316 Style.IndentWidth = 234; 20317 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 20318 Style.TabWidth = 345; 20319 EXPECT_FALSE(parseConfiguration("---\n" 20320 "IndentWidth: 456\n" 20321 "BreakBeforeBraces: Allman\n" 20322 "---\n" 20323 "Language: JavaScript\n" 20324 "IndentWidth: 111\n" 20325 "TabWidth: 111\n" 20326 "---\n" 20327 "Language: Cpp\n" 20328 "BreakBeforeBraces: Stroustrup\n" 20329 "TabWidth: 789\n" 20330 "...\n", 20331 &Style)); 20332 EXPECT_EQ(123u, Style.ColumnLimit); 20333 EXPECT_EQ(456u, Style.IndentWidth); 20334 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 20335 EXPECT_EQ(789u, Style.TabWidth); 20336 20337 EXPECT_EQ(parseConfiguration("---\n" 20338 "Language: JavaScript\n" 20339 "IndentWidth: 56\n" 20340 "---\n" 20341 "IndentWidth: 78\n" 20342 "...\n", 20343 &Style), 20344 ParseError::Error); 20345 EXPECT_EQ(parseConfiguration("---\n" 20346 "Language: JavaScript\n" 20347 "IndentWidth: 56\n" 20348 "---\n" 20349 "Language: JavaScript\n" 20350 "IndentWidth: 78\n" 20351 "...\n", 20352 &Style), 20353 ParseError::Error); 20354 20355 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 20356 } 20357 20358 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 20359 FormatStyle Style = {}; 20360 Style.Language = FormatStyle::LK_JavaScript; 20361 Style.BreakBeforeTernaryOperators = true; 20362 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 20363 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 20364 20365 Style.BreakBeforeTernaryOperators = true; 20366 EXPECT_EQ(0, parseConfiguration("---\n" 20367 "BasedOnStyle: Google\n" 20368 "---\n" 20369 "Language: JavaScript\n" 20370 "IndentWidth: 76\n" 20371 "...\n", 20372 &Style) 20373 .value()); 20374 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 20375 EXPECT_EQ(76u, Style.IndentWidth); 20376 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 20377 } 20378 20379 TEST_F(FormatTest, ConfigurationRoundTripTest) { 20380 FormatStyle Style = getLLVMStyle(); 20381 std::string YAML = configurationAsText(Style); 20382 FormatStyle ParsedStyle = {}; 20383 ParsedStyle.Language = FormatStyle::LK_Cpp; 20384 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 20385 EXPECT_EQ(Style, ParsedStyle); 20386 } 20387 20388 TEST_F(FormatTest, WorksFor8bitEncodings) { 20389 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 20390 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 20391 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 20392 "\"\xef\xee\xf0\xf3...\"", 20393 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 20394 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 20395 "\xef\xee\xf0\xf3...\"", 20396 getLLVMStyleWithColumns(12))); 20397 } 20398 20399 TEST_F(FormatTest, HandlesUTF8BOM) { 20400 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 20401 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 20402 format("\xef\xbb\xbf#include <iostream>")); 20403 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 20404 format("\xef\xbb\xbf\n#include <iostream>")); 20405 } 20406 20407 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 20408 #if !defined(_MSC_VER) 20409 20410 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 20411 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 20412 getLLVMStyleWithColumns(35)); 20413 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 20414 getLLVMStyleWithColumns(31)); 20415 verifyFormat("// Однажды в студёную зимнюю пору...", 20416 getLLVMStyleWithColumns(36)); 20417 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 20418 verifyFormat("/* Однажды в студёную зимнюю пору... */", 20419 getLLVMStyleWithColumns(39)); 20420 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 20421 getLLVMStyleWithColumns(35)); 20422 } 20423 20424 TEST_F(FormatTest, SplitsUTF8Strings) { 20425 // Non-printable characters' width is currently considered to be the length in 20426 // bytes in UTF8. The characters can be displayed in very different manner 20427 // (zero-width, single width with a substitution glyph, expanded to their code 20428 // (e.g. "<8d>"), so there's no single correct way to handle them. 20429 EXPECT_EQ("\"aaaaÄ\"\n" 20430 "\"\xc2\x8d\";", 20431 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 20432 EXPECT_EQ("\"aaaaaaaÄ\"\n" 20433 "\"\xc2\x8d\";", 20434 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 20435 EXPECT_EQ("\"Однажды, в \"\n" 20436 "\"студёную \"\n" 20437 "\"зимнюю \"\n" 20438 "\"пору,\"", 20439 format("\"Однажды, в студёную зимнюю пору,\"", 20440 getLLVMStyleWithColumns(13))); 20441 EXPECT_EQ( 20442 "\"一 二 三 \"\n" 20443 "\"四 五六 \"\n" 20444 "\"七 八 九 \"\n" 20445 "\"十\"", 20446 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 20447 EXPECT_EQ("\"一\t\"\n" 20448 "\"二 \t\"\n" 20449 "\"三 四 \"\n" 20450 "\"五\t\"\n" 20451 "\"六 \t\"\n" 20452 "\"七 \"\n" 20453 "\"八九十\tqq\"", 20454 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 20455 getLLVMStyleWithColumns(11))); 20456 20457 // UTF8 character in an escape sequence. 20458 EXPECT_EQ("\"aaaaaa\"\n" 20459 "\"\\\xC2\x8D\"", 20460 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 20461 } 20462 20463 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 20464 EXPECT_EQ("const char *sssss =\n" 20465 " \"一二三四五六七八\\\n" 20466 " 九 十\";", 20467 format("const char *sssss = \"一二三四五六七八\\\n" 20468 " 九 十\";", 20469 getLLVMStyleWithColumns(30))); 20470 } 20471 20472 TEST_F(FormatTest, SplitsUTF8LineComments) { 20473 EXPECT_EQ("// aaaaÄ\xc2\x8d", 20474 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 20475 EXPECT_EQ("// Я из лесу\n" 20476 "// вышел; был\n" 20477 "// сильный\n" 20478 "// мороз.", 20479 format("// Я из лесу вышел; был сильный мороз.", 20480 getLLVMStyleWithColumns(13))); 20481 EXPECT_EQ("// 一二三\n" 20482 "// 四五六七\n" 20483 "// 八 九\n" 20484 "// 十", 20485 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 20486 } 20487 20488 TEST_F(FormatTest, SplitsUTF8BlockComments) { 20489 EXPECT_EQ("/* Гляжу,\n" 20490 " * поднимается\n" 20491 " * медленно в\n" 20492 " * гору\n" 20493 " * Лошадка,\n" 20494 " * везущая\n" 20495 " * хворосту\n" 20496 " * воз. */", 20497 format("/* Гляжу, поднимается медленно в гору\n" 20498 " * Лошадка, везущая хворосту воз. */", 20499 getLLVMStyleWithColumns(13))); 20500 EXPECT_EQ( 20501 "/* 一二三\n" 20502 " * 四五六七\n" 20503 " * 八 九\n" 20504 " * 十 */", 20505 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 20506 EXPECT_EQ("/* \n" 20507 " * \n" 20508 " * - */", 20509 format("/* - */", getLLVMStyleWithColumns(12))); 20510 } 20511 20512 #endif // _MSC_VER 20513 20514 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 20515 FormatStyle Style = getLLVMStyle(); 20516 20517 Style.ConstructorInitializerIndentWidth = 4; 20518 verifyFormat( 20519 "SomeClass::Constructor()\n" 20520 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20521 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20522 Style); 20523 20524 Style.ConstructorInitializerIndentWidth = 2; 20525 verifyFormat( 20526 "SomeClass::Constructor()\n" 20527 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20528 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20529 Style); 20530 20531 Style.ConstructorInitializerIndentWidth = 0; 20532 verifyFormat( 20533 "SomeClass::Constructor()\n" 20534 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20535 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20536 Style); 20537 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 20538 verifyFormat( 20539 "SomeLongTemplateVariableName<\n" 20540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 20541 Style); 20542 verifyFormat("bool smaller = 1 < " 20543 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 20544 " " 20545 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 20546 Style); 20547 20548 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 20549 verifyFormat("SomeClass::Constructor() :\n" 20550 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 20551 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 20552 Style); 20553 } 20554 20555 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 20556 FormatStyle Style = getLLVMStyle(); 20557 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20558 Style.ConstructorInitializerIndentWidth = 4; 20559 verifyFormat("SomeClass::Constructor()\n" 20560 " : a(a)\n" 20561 " , b(b)\n" 20562 " , c(c) {}", 20563 Style); 20564 verifyFormat("SomeClass::Constructor()\n" 20565 " : a(a) {}", 20566 Style); 20567 20568 Style.ColumnLimit = 0; 20569 verifyFormat("SomeClass::Constructor()\n" 20570 " : a(a) {}", 20571 Style); 20572 verifyFormat("SomeClass::Constructor() noexcept\n" 20573 " : a(a) {}", 20574 Style); 20575 verifyFormat("SomeClass::Constructor()\n" 20576 " : a(a)\n" 20577 " , b(b)\n" 20578 " , c(c) {}", 20579 Style); 20580 verifyFormat("SomeClass::Constructor()\n" 20581 " : a(a) {\n" 20582 " foo();\n" 20583 " bar();\n" 20584 "}", 20585 Style); 20586 20587 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 20588 verifyFormat("SomeClass::Constructor()\n" 20589 " : a(a)\n" 20590 " , b(b)\n" 20591 " , c(c) {\n}", 20592 Style); 20593 verifyFormat("SomeClass::Constructor()\n" 20594 " : a(a) {\n}", 20595 Style); 20596 20597 Style.ColumnLimit = 80; 20598 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 20599 Style.ConstructorInitializerIndentWidth = 2; 20600 verifyFormat("SomeClass::Constructor()\n" 20601 " : a(a)\n" 20602 " , b(b)\n" 20603 " , c(c) {}", 20604 Style); 20605 20606 Style.ConstructorInitializerIndentWidth = 0; 20607 verifyFormat("SomeClass::Constructor()\n" 20608 ": a(a)\n" 20609 ", b(b)\n" 20610 ", c(c) {}", 20611 Style); 20612 20613 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 20614 Style.ConstructorInitializerIndentWidth = 4; 20615 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 20616 verifyFormat( 20617 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 20618 Style); 20619 verifyFormat( 20620 "SomeClass::Constructor()\n" 20621 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 20622 Style); 20623 Style.ConstructorInitializerIndentWidth = 4; 20624 Style.ColumnLimit = 60; 20625 verifyFormat("SomeClass::Constructor()\n" 20626 " : aaaaaaaa(aaaaaaaa)\n" 20627 " , aaaaaaaa(aaaaaaaa)\n" 20628 " , aaaaaaaa(aaaaaaaa) {}", 20629 Style); 20630 } 20631 20632 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 20633 FormatStyle Style = getLLVMStyle(); 20634 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20635 Style.ConstructorInitializerIndentWidth = 4; 20636 verifyFormat("SomeClass::Constructor()\n" 20637 " : a{a}\n" 20638 " , b{b} {}", 20639 Style); 20640 verifyFormat("SomeClass::Constructor()\n" 20641 " : a{a}\n" 20642 "#if CONDITION\n" 20643 " , b{b}\n" 20644 "#endif\n" 20645 "{\n}", 20646 Style); 20647 Style.ConstructorInitializerIndentWidth = 2; 20648 verifyFormat("SomeClass::Constructor()\n" 20649 "#if CONDITION\n" 20650 " : a{a}\n" 20651 "#endif\n" 20652 " , b{b}\n" 20653 " , c{c} {\n}", 20654 Style); 20655 Style.ConstructorInitializerIndentWidth = 0; 20656 verifyFormat("SomeClass::Constructor()\n" 20657 ": a{a}\n" 20658 "#ifdef CONDITION\n" 20659 ", b{b}\n" 20660 "#else\n" 20661 ", c{c}\n" 20662 "#endif\n" 20663 ", d{d} {\n}", 20664 Style); 20665 Style.ConstructorInitializerIndentWidth = 4; 20666 verifyFormat("SomeClass::Constructor()\n" 20667 " : a{a}\n" 20668 "#if WINDOWS\n" 20669 "#if DEBUG\n" 20670 " , b{0}\n" 20671 "#else\n" 20672 " , b{1}\n" 20673 "#endif\n" 20674 "#else\n" 20675 "#if DEBUG\n" 20676 " , b{2}\n" 20677 "#else\n" 20678 " , b{3}\n" 20679 "#endif\n" 20680 "#endif\n" 20681 "{\n}", 20682 Style); 20683 verifyFormat("SomeClass::Constructor()\n" 20684 " : a{a}\n" 20685 "#if WINDOWS\n" 20686 " , b{0}\n" 20687 "#if DEBUG\n" 20688 " , c{0}\n" 20689 "#else\n" 20690 " , c{1}\n" 20691 "#endif\n" 20692 "#else\n" 20693 "#if DEBUG\n" 20694 " , c{2}\n" 20695 "#else\n" 20696 " , c{3}\n" 20697 "#endif\n" 20698 " , b{1}\n" 20699 "#endif\n" 20700 "{\n}", 20701 Style); 20702 } 20703 20704 TEST_F(FormatTest, Destructors) { 20705 verifyFormat("void F(int &i) { i.~int(); }"); 20706 verifyFormat("void F(int &i) { i->~int(); }"); 20707 } 20708 20709 TEST_F(FormatTest, FormatsWithWebKitStyle) { 20710 FormatStyle Style = getWebKitStyle(); 20711 20712 // Don't indent in outer namespaces. 20713 verifyFormat("namespace outer {\n" 20714 "int i;\n" 20715 "namespace inner {\n" 20716 " int i;\n" 20717 "} // namespace inner\n" 20718 "} // namespace outer\n" 20719 "namespace other_outer {\n" 20720 "int i;\n" 20721 "}", 20722 Style); 20723 20724 // Don't indent case labels. 20725 verifyFormat("switch (variable) {\n" 20726 "case 1:\n" 20727 "case 2:\n" 20728 " doSomething();\n" 20729 " break;\n" 20730 "default:\n" 20731 " ++variable;\n" 20732 "}", 20733 Style); 20734 20735 // Wrap before binary operators. 20736 EXPECT_EQ("void f()\n" 20737 "{\n" 20738 " if (aaaaaaaaaaaaaaaa\n" 20739 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 20740 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 20741 " return;\n" 20742 "}", 20743 format("void f() {\n" 20744 "if (aaaaaaaaaaaaaaaa\n" 20745 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 20746 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 20747 "return;\n" 20748 "}", 20749 Style)); 20750 20751 // Allow functions on a single line. 20752 verifyFormat("void f() { return; }", Style); 20753 20754 // Allow empty blocks on a single line and insert a space in empty blocks. 20755 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 20756 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 20757 // However, don't merge non-empty short loops. 20758 EXPECT_EQ("while (true) {\n" 20759 " continue;\n" 20760 "}", 20761 format("while (true) { continue; }", Style)); 20762 20763 // Constructor initializers are formatted one per line with the "," on the 20764 // new line. 20765 verifyFormat("Constructor()\n" 20766 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 20767 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 20768 " aaaaaaaaaaaaaa)\n" 20769 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 20770 "{\n" 20771 "}", 20772 Style); 20773 verifyFormat("SomeClass::Constructor()\n" 20774 " : a(a)\n" 20775 "{\n" 20776 "}", 20777 Style); 20778 EXPECT_EQ("SomeClass::Constructor()\n" 20779 " : a(a)\n" 20780 "{\n" 20781 "}", 20782 format("SomeClass::Constructor():a(a){}", Style)); 20783 verifyFormat("SomeClass::Constructor()\n" 20784 " : a(a)\n" 20785 " , b(b)\n" 20786 " , c(c)\n" 20787 "{\n" 20788 "}", 20789 Style); 20790 verifyFormat("SomeClass::Constructor()\n" 20791 " : a(a)\n" 20792 "{\n" 20793 " foo();\n" 20794 " bar();\n" 20795 "}", 20796 Style); 20797 20798 // Access specifiers should be aligned left. 20799 verifyFormat("class C {\n" 20800 "public:\n" 20801 " int i;\n" 20802 "};", 20803 Style); 20804 20805 // Do not align comments. 20806 verifyFormat("int a; // Do not\n" 20807 "double b; // align comments.", 20808 Style); 20809 20810 // Do not align operands. 20811 EXPECT_EQ("ASSERT(aaaa\n" 20812 " || bbbb);", 20813 format("ASSERT ( aaaa\n||bbbb);", Style)); 20814 20815 // Accept input's line breaks. 20816 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 20817 " || bbbbbbbbbbbbbbb) {\n" 20818 " i++;\n" 20819 "}", 20820 format("if (aaaaaaaaaaaaaaa\n" 20821 "|| bbbbbbbbbbbbbbb) { i++; }", 20822 Style)); 20823 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 20824 " i++;\n" 20825 "}", 20826 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 20827 20828 // Don't automatically break all macro definitions (llvm.org/PR17842). 20829 verifyFormat("#define aNumber 10", Style); 20830 // However, generally keep the line breaks that the user authored. 20831 EXPECT_EQ("#define aNumber \\\n" 20832 " 10", 20833 format("#define aNumber \\\n" 20834 " 10", 20835 Style)); 20836 20837 // Keep empty and one-element array literals on a single line. 20838 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 20839 " copyItems:YES];", 20840 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 20841 "copyItems:YES];", 20842 Style)); 20843 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 20844 " copyItems:YES];", 20845 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 20846 " copyItems:YES];", 20847 Style)); 20848 // FIXME: This does not seem right, there should be more indentation before 20849 // the array literal's entries. Nested blocks have the same problem. 20850 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20851 " @\"a\",\n" 20852 " @\"a\"\n" 20853 "]\n" 20854 " copyItems:YES];", 20855 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20856 " @\"a\",\n" 20857 " @\"a\"\n" 20858 " ]\n" 20859 " copyItems:YES];", 20860 Style)); 20861 EXPECT_EQ( 20862 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20863 " copyItems:YES];", 20864 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20865 " copyItems:YES];", 20866 Style)); 20867 20868 verifyFormat("[self.a b:c c:d];", Style); 20869 EXPECT_EQ("[self.a b:c\n" 20870 " c:d];", 20871 format("[self.a b:c\n" 20872 "c:d];", 20873 Style)); 20874 } 20875 20876 TEST_F(FormatTest, FormatsLambdas) { 20877 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 20878 verifyFormat( 20879 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 20880 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 20881 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 20882 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 20883 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 20884 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 20885 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 20886 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 20887 verifyFormat("int x = f(*+[] {});"); 20888 verifyFormat("void f() {\n" 20889 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 20890 "}\n"); 20891 verifyFormat("void f() {\n" 20892 " other(x.begin(), //\n" 20893 " x.end(), //\n" 20894 " [&](int, int) { return 1; });\n" 20895 "}\n"); 20896 verifyFormat("void f() {\n" 20897 " other.other.other.other.other(\n" 20898 " x.begin(), x.end(),\n" 20899 " [something, rather](int, int, int, int, int, int, int) { " 20900 "return 1; });\n" 20901 "}\n"); 20902 verifyFormat( 20903 "void f() {\n" 20904 " other.other.other.other.other(\n" 20905 " x.begin(), x.end(),\n" 20906 " [something, rather](int, int, int, int, int, int, int) {\n" 20907 " //\n" 20908 " });\n" 20909 "}\n"); 20910 verifyFormat("SomeFunction([]() { // A cool function...\n" 20911 " return 43;\n" 20912 "});"); 20913 EXPECT_EQ("SomeFunction([]() {\n" 20914 "#define A a\n" 20915 " return 43;\n" 20916 "});", 20917 format("SomeFunction([](){\n" 20918 "#define A a\n" 20919 "return 43;\n" 20920 "});")); 20921 verifyFormat("void f() {\n" 20922 " SomeFunction([](decltype(x), A *a) {});\n" 20923 " SomeFunction([](typeof(x), A *a) {});\n" 20924 " SomeFunction([](_Atomic(x), A *a) {});\n" 20925 " SomeFunction([](__underlying_type(x), A *a) {});\n" 20926 "}"); 20927 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20928 " [](const aaaaaaaaaa &a) { return a; });"); 20929 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 20930 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 20931 "});"); 20932 verifyFormat("Constructor()\n" 20933 " : Field([] { // comment\n" 20934 " int i;\n" 20935 " }) {}"); 20936 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 20937 " return some_parameter.size();\n" 20938 "};"); 20939 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 20940 " [](const string &s) { return s; };"); 20941 verifyFormat("int i = aaaaaa ? 1 //\n" 20942 " : [] {\n" 20943 " return 2; //\n" 20944 " }();"); 20945 verifyFormat("llvm::errs() << \"number of twos is \"\n" 20946 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 20947 " return x == 2; // force break\n" 20948 " });"); 20949 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20950 " [=](int iiiiiiiiiiii) {\n" 20951 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 20952 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 20953 " });", 20954 getLLVMStyleWithColumns(60)); 20955 20956 verifyFormat("SomeFunction({[&] {\n" 20957 " // comment\n" 20958 " },\n" 20959 " [&] {\n" 20960 " // comment\n" 20961 " }});"); 20962 verifyFormat("SomeFunction({[&] {\n" 20963 " // comment\n" 20964 "}});"); 20965 verifyFormat( 20966 "virtual aaaaaaaaaaaaaaaa(\n" 20967 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 20968 " aaaaa aaaaaaaaa);"); 20969 20970 // Lambdas with return types. 20971 verifyFormat("int c = []() -> int { return 2; }();\n"); 20972 verifyFormat("int c = []() -> int * { return 2; }();\n"); 20973 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 20974 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 20975 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 20976 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 20977 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 20978 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 20979 verifyFormat("[a, a]() -> a<1> {};"); 20980 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 20981 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 20982 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 20983 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 20984 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 20985 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 20986 verifyFormat("[]() -> foo<!5> { return {}; };"); 20987 verifyFormat("[]() -> foo<~5> { return {}; };"); 20988 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 20989 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 20990 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 20991 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 20992 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 20993 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 20994 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 20995 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 20996 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 20997 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 20998 verifyFormat("namespace bar {\n" 20999 "// broken:\n" 21000 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 21001 "} // namespace bar"); 21002 verifyFormat("namespace bar {\n" 21003 "// broken:\n" 21004 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 21005 "} // namespace bar"); 21006 verifyFormat("namespace bar {\n" 21007 "// broken:\n" 21008 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 21009 "} // namespace bar"); 21010 verifyFormat("namespace bar {\n" 21011 "// broken:\n" 21012 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 21013 "} // namespace bar"); 21014 verifyFormat("namespace bar {\n" 21015 "// broken:\n" 21016 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 21017 "} // namespace bar"); 21018 verifyFormat("namespace bar {\n" 21019 "// broken:\n" 21020 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 21021 "} // namespace bar"); 21022 verifyFormat("namespace bar {\n" 21023 "// broken:\n" 21024 "auto foo{[]() -> foo<!5> { return {}; }};\n" 21025 "} // namespace bar"); 21026 verifyFormat("namespace bar {\n" 21027 "// broken:\n" 21028 "auto foo{[]() -> foo<~5> { return {}; }};\n" 21029 "} // namespace bar"); 21030 verifyFormat("namespace bar {\n" 21031 "// broken:\n" 21032 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 21033 "} // namespace bar"); 21034 verifyFormat("namespace bar {\n" 21035 "// broken:\n" 21036 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 21037 "} // namespace bar"); 21038 verifyFormat("namespace bar {\n" 21039 "// broken:\n" 21040 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 21041 "} // namespace bar"); 21042 verifyFormat("namespace bar {\n" 21043 "// broken:\n" 21044 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 21045 "} // namespace bar"); 21046 verifyFormat("namespace bar {\n" 21047 "// broken:\n" 21048 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 21049 "} // namespace bar"); 21050 verifyFormat("namespace bar {\n" 21051 "// broken:\n" 21052 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 21053 "} // namespace bar"); 21054 verifyFormat("namespace bar {\n" 21055 "// broken:\n" 21056 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 21057 "} // namespace bar"); 21058 verifyFormat("namespace bar {\n" 21059 "// broken:\n" 21060 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 21061 "} // namespace bar"); 21062 verifyFormat("namespace bar {\n" 21063 "// broken:\n" 21064 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 21065 "} // namespace bar"); 21066 verifyFormat("namespace bar {\n" 21067 "// broken:\n" 21068 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 21069 "} // namespace bar"); 21070 verifyFormat("[]() -> a<1> {};"); 21071 verifyFormat("[]() -> a<1> { ; };"); 21072 verifyFormat("[]() -> a<1> { ; }();"); 21073 verifyFormat("[a, a]() -> a<true> {};"); 21074 verifyFormat("[]() -> a<true> {};"); 21075 verifyFormat("[]() -> a<true> { ; };"); 21076 verifyFormat("[]() -> a<true> { ; }();"); 21077 verifyFormat("[a, a]() -> a<false> {};"); 21078 verifyFormat("[]() -> a<false> {};"); 21079 verifyFormat("[]() -> a<false> { ; };"); 21080 verifyFormat("[]() -> a<false> { ; }();"); 21081 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 21082 verifyFormat("namespace bar {\n" 21083 "auto foo{[]() -> foo<false> { ; }};\n" 21084 "} // namespace bar"); 21085 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 21086 " int j) -> int {\n" 21087 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 21088 "};"); 21089 verifyFormat( 21090 "aaaaaaaaaaaaaaaaaaaaaa(\n" 21091 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 21092 " return aaaaaaaaaaaaaaaaa;\n" 21093 " });", 21094 getLLVMStyleWithColumns(70)); 21095 verifyFormat("[]() //\n" 21096 " -> int {\n" 21097 " return 1; //\n" 21098 "};"); 21099 verifyFormat("[]() -> Void<T...> {};"); 21100 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 21101 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});"); 21102 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});"); 21103 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});"); 21104 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});"); 21105 verifyFormat("return int{[x = x]() { return x; }()};"); 21106 21107 // Lambdas with explicit template argument lists. 21108 verifyFormat( 21109 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 21110 verifyFormat("auto L = []<class T>(T) {\n" 21111 " {\n" 21112 " f();\n" 21113 " g();\n" 21114 " }\n" 21115 "};\n"); 21116 verifyFormat("auto L = []<class... T>(T...) {\n" 21117 " {\n" 21118 " f();\n" 21119 " g();\n" 21120 " }\n" 21121 "};\n"); 21122 verifyFormat("auto L = []<typename... T>(T...) {\n" 21123 " {\n" 21124 " f();\n" 21125 " g();\n" 21126 " }\n" 21127 "};\n"); 21128 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n" 21129 " {\n" 21130 " f();\n" 21131 " g();\n" 21132 " }\n" 21133 "};\n"); 21134 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n" 21135 " {\n" 21136 " f();\n" 21137 " g();\n" 21138 " }\n" 21139 "};\n"); 21140 21141 // Multiple lambdas in the same parentheses change indentation rules. These 21142 // lambdas are forced to start on new lines. 21143 verifyFormat("SomeFunction(\n" 21144 " []() {\n" 21145 " //\n" 21146 " },\n" 21147 " []() {\n" 21148 " //\n" 21149 " });"); 21150 21151 // A lambda passed as arg0 is always pushed to the next line. 21152 verifyFormat("SomeFunction(\n" 21153 " [this] {\n" 21154 " //\n" 21155 " },\n" 21156 " 1);\n"); 21157 21158 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 21159 // the arg0 case above. 21160 auto Style = getGoogleStyle(); 21161 Style.BinPackArguments = false; 21162 verifyFormat("SomeFunction(\n" 21163 " a,\n" 21164 " [this] {\n" 21165 " //\n" 21166 " },\n" 21167 " b);\n", 21168 Style); 21169 verifyFormat("SomeFunction(\n" 21170 " a,\n" 21171 " [this] {\n" 21172 " //\n" 21173 " },\n" 21174 " b);\n"); 21175 21176 // A lambda with a very long line forces arg0 to be pushed out irrespective of 21177 // the BinPackArguments value (as long as the code is wide enough). 21178 verifyFormat( 21179 "something->SomeFunction(\n" 21180 " a,\n" 21181 " [this] {\n" 21182 " " 21183 "D0000000000000000000000000000000000000000000000000000000000001();\n" 21184 " },\n" 21185 " b);\n"); 21186 21187 // A multi-line lambda is pulled up as long as the introducer fits on the 21188 // previous line and there are no further args. 21189 verifyFormat("function(1, [this, that] {\n" 21190 " //\n" 21191 "});\n"); 21192 verifyFormat("function([this, that] {\n" 21193 " //\n" 21194 "});\n"); 21195 // FIXME: this format is not ideal and we should consider forcing the first 21196 // arg onto its own line. 21197 verifyFormat("function(a, b, c, //\n" 21198 " d, [this, that] {\n" 21199 " //\n" 21200 " });\n"); 21201 21202 // Multiple lambdas are treated correctly even when there is a short arg0. 21203 verifyFormat("SomeFunction(\n" 21204 " 1,\n" 21205 " [this] {\n" 21206 " //\n" 21207 " },\n" 21208 " [this] {\n" 21209 " //\n" 21210 " },\n" 21211 " 1);\n"); 21212 21213 // More complex introducers. 21214 verifyFormat("return [i, args...] {};"); 21215 21216 // Not lambdas. 21217 verifyFormat("constexpr char hello[]{\"hello\"};"); 21218 verifyFormat("double &operator[](int i) { return 0; }\n" 21219 "int i;"); 21220 verifyFormat("std::unique_ptr<int[]> foo() {}"); 21221 verifyFormat("int i = a[a][a]->f();"); 21222 verifyFormat("int i = (*b)[a]->f();"); 21223 21224 // Other corner cases. 21225 verifyFormat("void f() {\n" 21226 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 21227 " );\n" 21228 "}"); 21229 21230 // Lambdas created through weird macros. 21231 verifyFormat("void f() {\n" 21232 " MACRO((const AA &a) { return 1; });\n" 21233 " MACRO((AA &a) { return 1; });\n" 21234 "}"); 21235 21236 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 21237 " doo_dah();\n" 21238 " doo_dah();\n" 21239 " })) {\n" 21240 "}"); 21241 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 21242 " doo_dah();\n" 21243 " doo_dah();\n" 21244 " })) {\n" 21245 "}"); 21246 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 21247 " doo_dah();\n" 21248 " doo_dah();\n" 21249 " })) {\n" 21250 "}"); 21251 verifyFormat("auto lambda = []() {\n" 21252 " int a = 2\n" 21253 "#if A\n" 21254 " + 2\n" 21255 "#endif\n" 21256 " ;\n" 21257 "};"); 21258 21259 // Lambdas with complex multiline introducers. 21260 verifyFormat( 21261 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21262 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 21263 " -> ::std::unordered_set<\n" 21264 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 21265 " //\n" 21266 " });"); 21267 21268 FormatStyle DoNotMerge = getLLVMStyle(); 21269 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 21270 verifyFormat("auto c = []() {\n" 21271 " return b;\n" 21272 "};", 21273 "auto c = []() { return b; };", DoNotMerge); 21274 verifyFormat("auto c = []() {\n" 21275 "};", 21276 " auto c = []() {};", DoNotMerge); 21277 21278 FormatStyle MergeEmptyOnly = getLLVMStyle(); 21279 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 21280 verifyFormat("auto c = []() {\n" 21281 " return b;\n" 21282 "};", 21283 "auto c = []() {\n" 21284 " return b;\n" 21285 " };", 21286 MergeEmptyOnly); 21287 verifyFormat("auto c = []() {};", 21288 "auto c = []() {\n" 21289 "};", 21290 MergeEmptyOnly); 21291 21292 FormatStyle MergeInline = getLLVMStyle(); 21293 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 21294 verifyFormat("auto c = []() {\n" 21295 " return b;\n" 21296 "};", 21297 "auto c = []() { return b; };", MergeInline); 21298 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 21299 MergeInline); 21300 verifyFormat("function([]() { return b; }, a)", 21301 "function([]() { return b; }, a)", MergeInline); 21302 verifyFormat("function(a, []() { return b; })", 21303 "function(a, []() { return b; })", MergeInline); 21304 21305 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 21306 // AllowShortLambdasOnASingleLine 21307 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 21308 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 21309 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 21310 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21311 FormatStyle::ShortLambdaStyle::SLS_None; 21312 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 21313 " []()\n" 21314 " {\n" 21315 " return 17;\n" 21316 " });", 21317 LLVMWithBeforeLambdaBody); 21318 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 21319 " []()\n" 21320 " {\n" 21321 " });", 21322 LLVMWithBeforeLambdaBody); 21323 verifyFormat("auto fct_SLS_None = []()\n" 21324 "{\n" 21325 " return 17;\n" 21326 "};", 21327 LLVMWithBeforeLambdaBody); 21328 verifyFormat("TwoNestedLambdas_SLS_None(\n" 21329 " []()\n" 21330 " {\n" 21331 " return Call(\n" 21332 " []()\n" 21333 " {\n" 21334 " return 17;\n" 21335 " });\n" 21336 " });", 21337 LLVMWithBeforeLambdaBody); 21338 verifyFormat("void Fct() {\n" 21339 " return {[]()\n" 21340 " {\n" 21341 " return 17;\n" 21342 " }};\n" 21343 "}", 21344 LLVMWithBeforeLambdaBody); 21345 21346 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21347 FormatStyle::ShortLambdaStyle::SLS_Empty; 21348 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 21349 " []()\n" 21350 " {\n" 21351 " return 17;\n" 21352 " });", 21353 LLVMWithBeforeLambdaBody); 21354 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 21355 LLVMWithBeforeLambdaBody); 21356 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 21357 "ongFunctionName_SLS_Empty(\n" 21358 " []() {});", 21359 LLVMWithBeforeLambdaBody); 21360 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 21361 " []()\n" 21362 " {\n" 21363 " return 17;\n" 21364 " });", 21365 LLVMWithBeforeLambdaBody); 21366 verifyFormat("auto fct_SLS_Empty = []()\n" 21367 "{\n" 21368 " return 17;\n" 21369 "};", 21370 LLVMWithBeforeLambdaBody); 21371 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 21372 " []()\n" 21373 " {\n" 21374 " return Call([]() {});\n" 21375 " });", 21376 LLVMWithBeforeLambdaBody); 21377 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 21378 " []()\n" 21379 " {\n" 21380 " return Call([]() {});\n" 21381 " });", 21382 LLVMWithBeforeLambdaBody); 21383 verifyFormat( 21384 "FctWithLongLineInLambda_SLS_Empty(\n" 21385 " []()\n" 21386 " {\n" 21387 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21388 " AndShouldNotBeConsiderAsInline,\n" 21389 " LambdaBodyMustBeBreak);\n" 21390 " });", 21391 LLVMWithBeforeLambdaBody); 21392 21393 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21394 FormatStyle::ShortLambdaStyle::SLS_Inline; 21395 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 21396 LLVMWithBeforeLambdaBody); 21397 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 21398 LLVMWithBeforeLambdaBody); 21399 verifyFormat("auto fct_SLS_Inline = []()\n" 21400 "{\n" 21401 " return 17;\n" 21402 "};", 21403 LLVMWithBeforeLambdaBody); 21404 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 21405 "17; }); });", 21406 LLVMWithBeforeLambdaBody); 21407 verifyFormat( 21408 "FctWithLongLineInLambda_SLS_Inline(\n" 21409 " []()\n" 21410 " {\n" 21411 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21412 " AndShouldNotBeConsiderAsInline,\n" 21413 " LambdaBodyMustBeBreak);\n" 21414 " });", 21415 LLVMWithBeforeLambdaBody); 21416 verifyFormat("FctWithMultipleParams_SLS_Inline(" 21417 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 21418 " []() { return 17; });", 21419 LLVMWithBeforeLambdaBody); 21420 verifyFormat( 21421 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 21422 LLVMWithBeforeLambdaBody); 21423 21424 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21425 FormatStyle::ShortLambdaStyle::SLS_All; 21426 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 21427 LLVMWithBeforeLambdaBody); 21428 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 21429 LLVMWithBeforeLambdaBody); 21430 verifyFormat("auto fct_SLS_All = []() { return 17; };", 21431 LLVMWithBeforeLambdaBody); 21432 verifyFormat("FctWithOneParam_SLS_All(\n" 21433 " []()\n" 21434 " {\n" 21435 " // A cool function...\n" 21436 " return 43;\n" 21437 " });", 21438 LLVMWithBeforeLambdaBody); 21439 verifyFormat("FctWithMultipleParams_SLS_All(" 21440 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 21441 " []() { return 17; });", 21442 LLVMWithBeforeLambdaBody); 21443 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 21444 LLVMWithBeforeLambdaBody); 21445 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 21446 LLVMWithBeforeLambdaBody); 21447 verifyFormat( 21448 "FctWithLongLineInLambda_SLS_All(\n" 21449 " []()\n" 21450 " {\n" 21451 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21452 " AndShouldNotBeConsiderAsInline,\n" 21453 " LambdaBodyMustBeBreak);\n" 21454 " });", 21455 LLVMWithBeforeLambdaBody); 21456 verifyFormat( 21457 "auto fct_SLS_All = []()\n" 21458 "{\n" 21459 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21460 " AndShouldNotBeConsiderAsInline,\n" 21461 " LambdaBodyMustBeBreak);\n" 21462 "};", 21463 LLVMWithBeforeLambdaBody); 21464 LLVMWithBeforeLambdaBody.BinPackParameters = false; 21465 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 21466 LLVMWithBeforeLambdaBody); 21467 verifyFormat( 21468 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 21469 " FirstParam,\n" 21470 " SecondParam,\n" 21471 " ThirdParam,\n" 21472 " FourthParam);", 21473 LLVMWithBeforeLambdaBody); 21474 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 21475 " []() { return " 21476 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 21477 " FirstParam,\n" 21478 " SecondParam,\n" 21479 " ThirdParam,\n" 21480 " FourthParam);", 21481 LLVMWithBeforeLambdaBody); 21482 verifyFormat( 21483 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 21484 " SecondParam,\n" 21485 " ThirdParam,\n" 21486 " FourthParam,\n" 21487 " []() { return SomeValueNotSoLong; });", 21488 LLVMWithBeforeLambdaBody); 21489 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 21490 " []()\n" 21491 " {\n" 21492 " return " 21493 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 21494 "eConsiderAsInline;\n" 21495 " });", 21496 LLVMWithBeforeLambdaBody); 21497 verifyFormat( 21498 "FctWithLongLineInLambda_SLS_All(\n" 21499 " []()\n" 21500 " {\n" 21501 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21502 " AndShouldNotBeConsiderAsInline,\n" 21503 " LambdaBodyMustBeBreak);\n" 21504 " });", 21505 LLVMWithBeforeLambdaBody); 21506 verifyFormat("FctWithTwoParams_SLS_All(\n" 21507 " []()\n" 21508 " {\n" 21509 " // A cool function...\n" 21510 " return 43;\n" 21511 " },\n" 21512 " 87);", 21513 LLVMWithBeforeLambdaBody); 21514 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 21515 LLVMWithBeforeLambdaBody); 21516 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 21517 LLVMWithBeforeLambdaBody); 21518 verifyFormat( 21519 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 21520 LLVMWithBeforeLambdaBody); 21521 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 21522 "}); }, x);", 21523 LLVMWithBeforeLambdaBody); 21524 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21525 " []()\n" 21526 " {\n" 21527 " // A cool function...\n" 21528 " return Call([]() { return 17; });\n" 21529 " });", 21530 LLVMWithBeforeLambdaBody); 21531 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21532 " []()\n" 21533 " {\n" 21534 " return Call(\n" 21535 " []()\n" 21536 " {\n" 21537 " // A cool function...\n" 21538 " return 17;\n" 21539 " });\n" 21540 " });", 21541 LLVMWithBeforeLambdaBody); 21542 21543 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21544 FormatStyle::ShortLambdaStyle::SLS_None; 21545 21546 verifyFormat("auto select = [this]() -> const Library::Object *\n" 21547 "{\n" 21548 " return MyAssignment::SelectFromList(this);\n" 21549 "};\n", 21550 LLVMWithBeforeLambdaBody); 21551 21552 verifyFormat("auto select = [this]() -> const Library::Object &\n" 21553 "{\n" 21554 " return MyAssignment::SelectFromList(this);\n" 21555 "};\n", 21556 LLVMWithBeforeLambdaBody); 21557 21558 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 21559 "{\n" 21560 " return MyAssignment::SelectFromList(this);\n" 21561 "};\n", 21562 LLVMWithBeforeLambdaBody); 21563 21564 verifyFormat("namespace test {\n" 21565 "class Test {\n" 21566 "public:\n" 21567 " Test() = default;\n" 21568 "};\n" 21569 "} // namespace test", 21570 LLVMWithBeforeLambdaBody); 21571 21572 // Lambdas with different indentation styles. 21573 Style = getLLVMStyleWithColumns(100); 21574 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21575 " return promise.then(\n" 21576 " [this, &someVariable, someObject = " 21577 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21578 " return someObject.startAsyncAction().then(\n" 21579 " [this, &someVariable](AsyncActionResult result) " 21580 "mutable { result.processMore(); });\n" 21581 " });\n" 21582 "}\n", 21583 format("SomeResult doSomething(SomeObject promise) {\n" 21584 " return promise.then([this, &someVariable, someObject = " 21585 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21586 " return someObject.startAsyncAction().then([this, " 21587 "&someVariable](AsyncActionResult result) mutable {\n" 21588 " result.processMore();\n" 21589 " });\n" 21590 " });\n" 21591 "}\n", 21592 Style)); 21593 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21594 verifyFormat("test() {\n" 21595 " ([]() -> {\n" 21596 " int b = 32;\n" 21597 " return 3;\n" 21598 " }).foo();\n" 21599 "}", 21600 Style); 21601 verifyFormat("test() {\n" 21602 " []() -> {\n" 21603 " int b = 32;\n" 21604 " return 3;\n" 21605 " }\n" 21606 "}", 21607 Style); 21608 verifyFormat("std::sort(v.begin(), v.end(),\n" 21609 " [](const auto &someLongArgumentName, const auto " 21610 "&someOtherLongArgumentName) {\n" 21611 " return someLongArgumentName.someMemberVariable < " 21612 "someOtherLongArgumentName.someMemberVariable;\n" 21613 "});", 21614 Style); 21615 verifyFormat("test() {\n" 21616 " (\n" 21617 " []() -> {\n" 21618 " int b = 32;\n" 21619 " return 3;\n" 21620 " },\n" 21621 " foo, bar)\n" 21622 " .foo();\n" 21623 "}", 21624 Style); 21625 verifyFormat("test() {\n" 21626 " ([]() -> {\n" 21627 " int b = 32;\n" 21628 " return 3;\n" 21629 " })\n" 21630 " .foo()\n" 21631 " .bar();\n" 21632 "}", 21633 Style); 21634 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21635 " return promise.then(\n" 21636 " [this, &someVariable, someObject = " 21637 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21638 " return someObject.startAsyncAction().then(\n" 21639 " [this, &someVariable](AsyncActionResult result) mutable { " 21640 "result.processMore(); });\n" 21641 " });\n" 21642 "}\n", 21643 format("SomeResult doSomething(SomeObject promise) {\n" 21644 " return promise.then([this, &someVariable, someObject = " 21645 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21646 " return someObject.startAsyncAction().then([this, " 21647 "&someVariable](AsyncActionResult result) mutable {\n" 21648 " result.processMore();\n" 21649 " });\n" 21650 " });\n" 21651 "}\n", 21652 Style)); 21653 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21654 " return promise.then([this, &someVariable] {\n" 21655 " return someObject.startAsyncAction().then(\n" 21656 " [this, &someVariable](AsyncActionResult result) mutable { " 21657 "result.processMore(); });\n" 21658 " });\n" 21659 "}\n", 21660 format("SomeResult doSomething(SomeObject promise) {\n" 21661 " return promise.then([this, &someVariable] {\n" 21662 " return someObject.startAsyncAction().then([this, " 21663 "&someVariable](AsyncActionResult result) mutable {\n" 21664 " result.processMore();\n" 21665 " });\n" 21666 " });\n" 21667 "}\n", 21668 Style)); 21669 Style = getGoogleStyle(); 21670 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21671 EXPECT_EQ("#define A \\\n" 21672 " [] { \\\n" 21673 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 21674 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 21675 " }", 21676 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 21677 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 21678 Style)); 21679 // TODO: The current formatting has a minor issue that's not worth fixing 21680 // right now whereby the closing brace is indented relative to the signature 21681 // instead of being aligned. This only happens with macros. 21682 } 21683 21684 TEST_F(FormatTest, LambdaWithLineComments) { 21685 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 21686 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 21687 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 21688 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21689 FormatStyle::ShortLambdaStyle::SLS_All; 21690 21691 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 21692 verifyFormat("auto k = []() // comment\n" 21693 "{ return; }", 21694 LLVMWithBeforeLambdaBody); 21695 verifyFormat("auto k = []() /* comment */ { return; }", 21696 LLVMWithBeforeLambdaBody); 21697 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 21698 LLVMWithBeforeLambdaBody); 21699 verifyFormat("auto k = []() // X\n" 21700 "{ return; }", 21701 LLVMWithBeforeLambdaBody); 21702 verifyFormat( 21703 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 21704 "{ return; }", 21705 LLVMWithBeforeLambdaBody); 21706 } 21707 21708 TEST_F(FormatTest, EmptyLinesInLambdas) { 21709 verifyFormat("auto lambda = []() {\n" 21710 " x(); //\n" 21711 "};", 21712 "auto lambda = []() {\n" 21713 "\n" 21714 " x(); //\n" 21715 "\n" 21716 "};"); 21717 } 21718 21719 TEST_F(FormatTest, FormatsBlocks) { 21720 FormatStyle ShortBlocks = getLLVMStyle(); 21721 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21722 verifyFormat("int (^Block)(int, int);", ShortBlocks); 21723 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 21724 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 21725 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 21726 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 21727 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 21728 21729 verifyFormat("foo(^{ bar(); });", ShortBlocks); 21730 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 21731 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 21732 21733 verifyFormat("[operation setCompletionBlock:^{\n" 21734 " [self onOperationDone];\n" 21735 "}];"); 21736 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 21737 " [self onOperationDone];\n" 21738 "}]};"); 21739 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 21740 " f();\n" 21741 "}];"); 21742 verifyFormat("int a = [operation block:^int(int *i) {\n" 21743 " return 1;\n" 21744 "}];"); 21745 verifyFormat("[myObject doSomethingWith:arg1\n" 21746 " aaa:^int(int *a) {\n" 21747 " return 1;\n" 21748 " }\n" 21749 " bbb:f(a * bbbbbbbb)];"); 21750 21751 verifyFormat("[operation setCompletionBlock:^{\n" 21752 " [self.delegate newDataAvailable];\n" 21753 "}];", 21754 getLLVMStyleWithColumns(60)); 21755 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 21756 " NSString *path = [self sessionFilePath];\n" 21757 " if (path) {\n" 21758 " // ...\n" 21759 " }\n" 21760 "});"); 21761 verifyFormat("[[SessionService sharedService]\n" 21762 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21763 " if (window) {\n" 21764 " [self windowDidLoad:window];\n" 21765 " } else {\n" 21766 " [self errorLoadingWindow];\n" 21767 " }\n" 21768 " }];"); 21769 verifyFormat("void (^largeBlock)(void) = ^{\n" 21770 " // ...\n" 21771 "};\n", 21772 getLLVMStyleWithColumns(40)); 21773 verifyFormat("[[SessionService sharedService]\n" 21774 " loadWindowWithCompletionBlock: //\n" 21775 " ^(SessionWindow *window) {\n" 21776 " if (window) {\n" 21777 " [self windowDidLoad:window];\n" 21778 " } else {\n" 21779 " [self errorLoadingWindow];\n" 21780 " }\n" 21781 " }];", 21782 getLLVMStyleWithColumns(60)); 21783 verifyFormat("[myObject doSomethingWith:arg1\n" 21784 " firstBlock:^(Foo *a) {\n" 21785 " // ...\n" 21786 " int i;\n" 21787 " }\n" 21788 " secondBlock:^(Bar *b) {\n" 21789 " // ...\n" 21790 " int i;\n" 21791 " }\n" 21792 " thirdBlock:^Foo(Bar *b) {\n" 21793 " // ...\n" 21794 " int i;\n" 21795 " }];"); 21796 verifyFormat("[myObject doSomethingWith:arg1\n" 21797 " firstBlock:-1\n" 21798 " secondBlock:^(Bar *b) {\n" 21799 " // ...\n" 21800 " int i;\n" 21801 " }];"); 21802 21803 verifyFormat("f(^{\n" 21804 " @autoreleasepool {\n" 21805 " if (a) {\n" 21806 " g();\n" 21807 " }\n" 21808 " }\n" 21809 "});"); 21810 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 21811 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 21812 "};"); 21813 21814 FormatStyle FourIndent = getLLVMStyle(); 21815 FourIndent.ObjCBlockIndentWidth = 4; 21816 verifyFormat("[operation setCompletionBlock:^{\n" 21817 " [self onOperationDone];\n" 21818 "}];", 21819 FourIndent); 21820 } 21821 21822 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 21823 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 21824 21825 verifyFormat("[[SessionService sharedService] " 21826 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21827 " if (window) {\n" 21828 " [self windowDidLoad:window];\n" 21829 " } else {\n" 21830 " [self errorLoadingWindow];\n" 21831 " }\n" 21832 "}];", 21833 ZeroColumn); 21834 EXPECT_EQ("[[SessionService sharedService]\n" 21835 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21836 " if (window) {\n" 21837 " [self windowDidLoad:window];\n" 21838 " } else {\n" 21839 " [self errorLoadingWindow];\n" 21840 " }\n" 21841 " }];", 21842 format("[[SessionService sharedService]\n" 21843 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21844 " if (window) {\n" 21845 " [self windowDidLoad:window];\n" 21846 " } else {\n" 21847 " [self errorLoadingWindow];\n" 21848 " }\n" 21849 "}];", 21850 ZeroColumn)); 21851 verifyFormat("[myObject doSomethingWith:arg1\n" 21852 " firstBlock:^(Foo *a) {\n" 21853 " // ...\n" 21854 " int i;\n" 21855 " }\n" 21856 " secondBlock:^(Bar *b) {\n" 21857 " // ...\n" 21858 " int i;\n" 21859 " }\n" 21860 " thirdBlock:^Foo(Bar *b) {\n" 21861 " // ...\n" 21862 " int i;\n" 21863 " }];", 21864 ZeroColumn); 21865 verifyFormat("f(^{\n" 21866 " @autoreleasepool {\n" 21867 " if (a) {\n" 21868 " g();\n" 21869 " }\n" 21870 " }\n" 21871 "});", 21872 ZeroColumn); 21873 verifyFormat("void (^largeBlock)(void) = ^{\n" 21874 " // ...\n" 21875 "};", 21876 ZeroColumn); 21877 21878 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21879 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 21880 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21881 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 21882 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 21883 " int i;\n" 21884 "};", 21885 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21886 } 21887 21888 TEST_F(FormatTest, SupportsCRLF) { 21889 EXPECT_EQ("int a;\r\n" 21890 "int b;\r\n" 21891 "int c;\r\n", 21892 format("int a;\r\n" 21893 " int b;\r\n" 21894 " int c;\r\n", 21895 getLLVMStyle())); 21896 EXPECT_EQ("int a;\r\n" 21897 "int b;\r\n" 21898 "int c;\r\n", 21899 format("int a;\r\n" 21900 " int b;\n" 21901 " int c;\r\n", 21902 getLLVMStyle())); 21903 EXPECT_EQ("int a;\n" 21904 "int b;\n" 21905 "int c;\n", 21906 format("int a;\r\n" 21907 " int b;\n" 21908 " int c;\n", 21909 getLLVMStyle())); 21910 EXPECT_EQ("\"aaaaaaa \"\r\n" 21911 "\"bbbbbbb\";\r\n", 21912 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 21913 EXPECT_EQ("#define A \\\r\n" 21914 " b; \\\r\n" 21915 " c; \\\r\n" 21916 " d;\r\n", 21917 format("#define A \\\r\n" 21918 " b; \\\r\n" 21919 " c; d; \r\n", 21920 getGoogleStyle())); 21921 21922 EXPECT_EQ("/*\r\n" 21923 "multi line block comments\r\n" 21924 "should not introduce\r\n" 21925 "an extra carriage return\r\n" 21926 "*/\r\n", 21927 format("/*\r\n" 21928 "multi line block comments\r\n" 21929 "should not introduce\r\n" 21930 "an extra carriage return\r\n" 21931 "*/\r\n")); 21932 EXPECT_EQ("/*\r\n" 21933 "\r\n" 21934 "*/", 21935 format("/*\r\n" 21936 " \r\r\r\n" 21937 "*/")); 21938 21939 FormatStyle style = getLLVMStyle(); 21940 21941 style.DeriveLineEnding = true; 21942 style.UseCRLF = false; 21943 EXPECT_EQ("union FooBarBazQux {\n" 21944 " int foo;\n" 21945 " int bar;\n" 21946 " int baz;\n" 21947 "};", 21948 format("union FooBarBazQux {\r\n" 21949 " int foo;\n" 21950 " int bar;\r\n" 21951 " int baz;\n" 21952 "};", 21953 style)); 21954 style.UseCRLF = true; 21955 EXPECT_EQ("union FooBarBazQux {\r\n" 21956 " int foo;\r\n" 21957 " int bar;\r\n" 21958 " int baz;\r\n" 21959 "};", 21960 format("union FooBarBazQux {\r\n" 21961 " int foo;\n" 21962 " int bar;\r\n" 21963 " int baz;\n" 21964 "};", 21965 style)); 21966 21967 style.DeriveLineEnding = false; 21968 style.UseCRLF = false; 21969 EXPECT_EQ("union FooBarBazQux {\n" 21970 " int foo;\n" 21971 " int bar;\n" 21972 " int baz;\n" 21973 " int qux;\n" 21974 "};", 21975 format("union FooBarBazQux {\r\n" 21976 " int foo;\n" 21977 " int bar;\r\n" 21978 " int baz;\n" 21979 " int qux;\r\n" 21980 "};", 21981 style)); 21982 style.UseCRLF = true; 21983 EXPECT_EQ("union FooBarBazQux {\r\n" 21984 " int foo;\r\n" 21985 " int bar;\r\n" 21986 " int baz;\r\n" 21987 " int qux;\r\n" 21988 "};", 21989 format("union FooBarBazQux {\r\n" 21990 " int foo;\n" 21991 " int bar;\r\n" 21992 " int baz;\n" 21993 " int qux;\n" 21994 "};", 21995 style)); 21996 21997 style.DeriveLineEnding = true; 21998 style.UseCRLF = false; 21999 EXPECT_EQ("union FooBarBazQux {\r\n" 22000 " int foo;\r\n" 22001 " int bar;\r\n" 22002 " int baz;\r\n" 22003 " int qux;\r\n" 22004 "};", 22005 format("union FooBarBazQux {\r\n" 22006 " int foo;\n" 22007 " int bar;\r\n" 22008 " int baz;\n" 22009 " int qux;\r\n" 22010 "};", 22011 style)); 22012 style.UseCRLF = true; 22013 EXPECT_EQ("union FooBarBazQux {\n" 22014 " int foo;\n" 22015 " int bar;\n" 22016 " int baz;\n" 22017 " int qux;\n" 22018 "};", 22019 format("union FooBarBazQux {\r\n" 22020 " int foo;\n" 22021 " int bar;\r\n" 22022 " int baz;\n" 22023 " int qux;\n" 22024 "};", 22025 style)); 22026 } 22027 22028 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 22029 verifyFormat("MY_CLASS(C) {\n" 22030 " int i;\n" 22031 " int j;\n" 22032 "};"); 22033 } 22034 22035 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 22036 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 22037 TwoIndent.ContinuationIndentWidth = 2; 22038 22039 EXPECT_EQ("int i =\n" 22040 " longFunction(\n" 22041 " arg);", 22042 format("int i = longFunction(arg);", TwoIndent)); 22043 22044 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 22045 SixIndent.ContinuationIndentWidth = 6; 22046 22047 EXPECT_EQ("int i =\n" 22048 " longFunction(\n" 22049 " arg);", 22050 format("int i = longFunction(arg);", SixIndent)); 22051 } 22052 22053 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 22054 FormatStyle Style = getLLVMStyle(); 22055 verifyFormat("int Foo::getter(\n" 22056 " //\n" 22057 ") const {\n" 22058 " return foo;\n" 22059 "}", 22060 Style); 22061 verifyFormat("void Foo::setter(\n" 22062 " //\n" 22063 ") {\n" 22064 " foo = 1;\n" 22065 "}", 22066 Style); 22067 } 22068 22069 TEST_F(FormatTest, SpacesInAngles) { 22070 FormatStyle Spaces = getLLVMStyle(); 22071 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 22072 22073 verifyFormat("vector< ::std::string > x1;", Spaces); 22074 verifyFormat("Foo< int, Bar > x2;", Spaces); 22075 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 22076 22077 verifyFormat("static_cast< int >(arg);", Spaces); 22078 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 22079 verifyFormat("f< int, float >();", Spaces); 22080 verifyFormat("template <> g() {}", Spaces); 22081 verifyFormat("template < std::vector< int > > f() {}", Spaces); 22082 verifyFormat("std::function< void(int, int) > fct;", Spaces); 22083 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 22084 Spaces); 22085 22086 Spaces.Standard = FormatStyle::LS_Cpp03; 22087 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 22088 verifyFormat("A< A< int > >();", Spaces); 22089 22090 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 22091 verifyFormat("A<A<int> >();", Spaces); 22092 22093 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 22094 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 22095 Spaces); 22096 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 22097 Spaces); 22098 22099 verifyFormat("A<A<int> >();", Spaces); 22100 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 22101 verifyFormat("A< A< int > >();", Spaces); 22102 22103 Spaces.Standard = FormatStyle::LS_Cpp11; 22104 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 22105 verifyFormat("A< A< int > >();", Spaces); 22106 22107 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 22108 verifyFormat("vector<::std::string> x4;", Spaces); 22109 verifyFormat("vector<int> x5;", Spaces); 22110 verifyFormat("Foo<int, Bar> x6;", Spaces); 22111 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 22112 22113 verifyFormat("A<A<int>>();", Spaces); 22114 22115 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 22116 verifyFormat("vector<::std::string> x4;", Spaces); 22117 verifyFormat("vector< ::std::string > x4;", Spaces); 22118 verifyFormat("vector<int> x5;", Spaces); 22119 verifyFormat("vector< int > x5;", Spaces); 22120 verifyFormat("Foo<int, Bar> x6;", Spaces); 22121 verifyFormat("Foo< int, Bar > x6;", Spaces); 22122 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 22123 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 22124 22125 verifyFormat("A<A<int>>();", Spaces); 22126 verifyFormat("A< A< int > >();", Spaces); 22127 verifyFormat("A<A<int > >();", Spaces); 22128 verifyFormat("A< A< int>>();", Spaces); 22129 22130 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 22131 verifyFormat("// clang-format off\n" 22132 "foo<<<1, 1>>>();\n" 22133 "// clang-format on\n", 22134 Spaces); 22135 verifyFormat("// clang-format off\n" 22136 "foo< < <1, 1> > >();\n" 22137 "// clang-format on\n", 22138 Spaces); 22139 } 22140 22141 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 22142 FormatStyle Style = getLLVMStyle(); 22143 Style.SpaceAfterTemplateKeyword = false; 22144 verifyFormat("template<int> void foo();", Style); 22145 } 22146 22147 TEST_F(FormatTest, TripleAngleBrackets) { 22148 verifyFormat("f<<<1, 1>>>();"); 22149 verifyFormat("f<<<1, 1, 1, s>>>();"); 22150 verifyFormat("f<<<a, b, c, d>>>();"); 22151 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 22152 verifyFormat("f<param><<<1, 1>>>();"); 22153 verifyFormat("f<1><<<1, 1>>>();"); 22154 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 22155 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 22156 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 22157 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 22158 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 22159 } 22160 22161 TEST_F(FormatTest, MergeLessLessAtEnd) { 22162 verifyFormat("<<"); 22163 EXPECT_EQ("< < <", format("\\\n<<<")); 22164 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 22165 "aaallvm::outs() <<"); 22166 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 22167 "aaaallvm::outs()\n <<"); 22168 } 22169 22170 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 22171 std::string code = "#if A\n" 22172 "#if B\n" 22173 "a.\n" 22174 "#endif\n" 22175 " a = 1;\n" 22176 "#else\n" 22177 "#endif\n" 22178 "#if C\n" 22179 "#else\n" 22180 "#endif\n"; 22181 EXPECT_EQ(code, format(code)); 22182 } 22183 22184 TEST_F(FormatTest, HandleConflictMarkers) { 22185 // Git/SVN conflict markers. 22186 EXPECT_EQ("int a;\n" 22187 "void f() {\n" 22188 " callme(some(parameter1,\n" 22189 "<<<<<<< text by the vcs\n" 22190 " parameter2),\n" 22191 "||||||| text by the vcs\n" 22192 " parameter2),\n" 22193 " parameter3,\n" 22194 "======= text by the vcs\n" 22195 " parameter2, parameter3),\n" 22196 ">>>>>>> text by the vcs\n" 22197 " otherparameter);\n", 22198 format("int a;\n" 22199 "void f() {\n" 22200 " callme(some(parameter1,\n" 22201 "<<<<<<< text by the vcs\n" 22202 " parameter2),\n" 22203 "||||||| text by the vcs\n" 22204 " parameter2),\n" 22205 " parameter3,\n" 22206 "======= text by the vcs\n" 22207 " parameter2,\n" 22208 " parameter3),\n" 22209 ">>>>>>> text by the vcs\n" 22210 " otherparameter);\n")); 22211 22212 // Perforce markers. 22213 EXPECT_EQ("void f() {\n" 22214 " function(\n" 22215 ">>>> text by the vcs\n" 22216 " parameter,\n" 22217 "==== text by the vcs\n" 22218 " parameter,\n" 22219 "==== text by the vcs\n" 22220 " parameter,\n" 22221 "<<<< text by the vcs\n" 22222 " parameter);\n", 22223 format("void f() {\n" 22224 " function(\n" 22225 ">>>> text by the vcs\n" 22226 " parameter,\n" 22227 "==== text by the vcs\n" 22228 " parameter,\n" 22229 "==== text by the vcs\n" 22230 " parameter,\n" 22231 "<<<< text by the vcs\n" 22232 " parameter);\n")); 22233 22234 EXPECT_EQ("<<<<<<<\n" 22235 "|||||||\n" 22236 "=======\n" 22237 ">>>>>>>", 22238 format("<<<<<<<\n" 22239 "|||||||\n" 22240 "=======\n" 22241 ">>>>>>>")); 22242 22243 EXPECT_EQ("<<<<<<<\n" 22244 "|||||||\n" 22245 "int i;\n" 22246 "=======\n" 22247 ">>>>>>>", 22248 format("<<<<<<<\n" 22249 "|||||||\n" 22250 "int i;\n" 22251 "=======\n" 22252 ">>>>>>>")); 22253 22254 // FIXME: Handle parsing of macros around conflict markers correctly: 22255 EXPECT_EQ("#define Macro \\\n" 22256 "<<<<<<<\n" 22257 "Something \\\n" 22258 "|||||||\n" 22259 "Else \\\n" 22260 "=======\n" 22261 "Other \\\n" 22262 ">>>>>>>\n" 22263 " End int i;\n", 22264 format("#define Macro \\\n" 22265 "<<<<<<<\n" 22266 " Something \\\n" 22267 "|||||||\n" 22268 " Else \\\n" 22269 "=======\n" 22270 " Other \\\n" 22271 ">>>>>>>\n" 22272 " End\n" 22273 "int i;\n")); 22274 22275 verifyFormat(R"(==== 22276 #ifdef A 22277 a 22278 #else 22279 b 22280 #endif 22281 )"); 22282 } 22283 22284 TEST_F(FormatTest, DisableRegions) { 22285 EXPECT_EQ("int i;\n" 22286 "// clang-format off\n" 22287 " int j;\n" 22288 "// clang-format on\n" 22289 "int k;", 22290 format(" int i;\n" 22291 " // clang-format off\n" 22292 " int j;\n" 22293 " // clang-format on\n" 22294 " int k;")); 22295 EXPECT_EQ("int i;\n" 22296 "/* clang-format off */\n" 22297 " int j;\n" 22298 "/* clang-format on */\n" 22299 "int k;", 22300 format(" int i;\n" 22301 " /* clang-format off */\n" 22302 " int j;\n" 22303 " /* clang-format on */\n" 22304 " int k;")); 22305 22306 // Don't reflow comments within disabled regions. 22307 EXPECT_EQ("// clang-format off\n" 22308 "// long long long long long long line\n" 22309 "/* clang-format on */\n" 22310 "/* long long long\n" 22311 " * long long long\n" 22312 " * line */\n" 22313 "int i;\n" 22314 "/* clang-format off */\n" 22315 "/* long long long long long long line */\n", 22316 format("// clang-format off\n" 22317 "// long long long long long long line\n" 22318 "/* clang-format on */\n" 22319 "/* long long long long long long line */\n" 22320 "int i;\n" 22321 "/* clang-format off */\n" 22322 "/* long long long long long long line */\n", 22323 getLLVMStyleWithColumns(20))); 22324 } 22325 22326 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 22327 format("? ) ="); 22328 verifyNoCrash("#define a\\\n /**/}"); 22329 } 22330 22331 TEST_F(FormatTest, FormatsTableGenCode) { 22332 FormatStyle Style = getLLVMStyle(); 22333 Style.Language = FormatStyle::LK_TableGen; 22334 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 22335 } 22336 22337 TEST_F(FormatTest, ArrayOfTemplates) { 22338 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 22339 format("auto a = new unique_ptr<int > [ 10];")); 22340 22341 FormatStyle Spaces = getLLVMStyle(); 22342 Spaces.SpacesInSquareBrackets = true; 22343 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 22344 format("auto a = new unique_ptr<int > [10];", Spaces)); 22345 } 22346 22347 TEST_F(FormatTest, ArrayAsTemplateType) { 22348 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 22349 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 22350 22351 FormatStyle Spaces = getLLVMStyle(); 22352 Spaces.SpacesInSquareBrackets = true; 22353 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 22354 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 22355 } 22356 22357 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 22358 22359 TEST(FormatStyle, GetStyleWithEmptyFileName) { 22360 llvm::vfs::InMemoryFileSystem FS; 22361 auto Style1 = getStyle("file", "", "Google", "", &FS); 22362 ASSERT_TRUE((bool)Style1); 22363 ASSERT_EQ(*Style1, getGoogleStyle()); 22364 } 22365 22366 TEST(FormatStyle, GetStyleOfFile) { 22367 llvm::vfs::InMemoryFileSystem FS; 22368 // Test 1: format file in the same directory. 22369 ASSERT_TRUE( 22370 FS.addFile("/a/.clang-format", 0, 22371 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 22372 ASSERT_TRUE( 22373 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22374 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 22375 ASSERT_TRUE((bool)Style1); 22376 ASSERT_EQ(*Style1, getLLVMStyle()); 22377 22378 // Test 2.1: fallback to default. 22379 ASSERT_TRUE( 22380 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22381 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 22382 ASSERT_TRUE((bool)Style2); 22383 ASSERT_EQ(*Style2, getMozillaStyle()); 22384 22385 // Test 2.2: no format on 'none' fallback style. 22386 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 22387 ASSERT_TRUE((bool)Style2); 22388 ASSERT_EQ(*Style2, getNoStyle()); 22389 22390 // Test 2.3: format if config is found with no based style while fallback is 22391 // 'none'. 22392 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 22393 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 22394 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 22395 ASSERT_TRUE((bool)Style2); 22396 ASSERT_EQ(*Style2, getLLVMStyle()); 22397 22398 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 22399 Style2 = getStyle("{}", "a.h", "none", "", &FS); 22400 ASSERT_TRUE((bool)Style2); 22401 ASSERT_EQ(*Style2, getLLVMStyle()); 22402 22403 // Test 3: format file in parent directory. 22404 ASSERT_TRUE( 22405 FS.addFile("/c/.clang-format", 0, 22406 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22407 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 22408 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22409 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22410 ASSERT_TRUE((bool)Style3); 22411 ASSERT_EQ(*Style3, getGoogleStyle()); 22412 22413 // Test 4: error on invalid fallback style 22414 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 22415 ASSERT_FALSE((bool)Style4); 22416 llvm::consumeError(Style4.takeError()); 22417 22418 // Test 5: error on invalid yaml on command line 22419 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 22420 ASSERT_FALSE((bool)Style5); 22421 llvm::consumeError(Style5.takeError()); 22422 22423 // Test 6: error on invalid style 22424 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 22425 ASSERT_FALSE((bool)Style6); 22426 llvm::consumeError(Style6.takeError()); 22427 22428 // Test 7: found config file, error on parsing it 22429 ASSERT_TRUE( 22430 FS.addFile("/d/.clang-format", 0, 22431 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 22432 "InvalidKey: InvalidValue"))); 22433 ASSERT_TRUE( 22434 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22435 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 22436 ASSERT_FALSE((bool)Style7a); 22437 llvm::consumeError(Style7a.takeError()); 22438 22439 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 22440 ASSERT_TRUE((bool)Style7b); 22441 22442 // Test 8: inferred per-language defaults apply. 22443 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 22444 ASSERT_TRUE((bool)StyleTd); 22445 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 22446 22447 // Test 9.1.1: overwriting a file style, when no parent file exists with no 22448 // fallback style. 22449 ASSERT_TRUE(FS.addFile( 22450 "/e/sub/.clang-format", 0, 22451 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 22452 "ColumnLimit: 20"))); 22453 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 22454 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22455 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 22456 ASSERT_TRUE(static_cast<bool>(Style9)); 22457 ASSERT_EQ(*Style9, [] { 22458 auto Style = getNoStyle(); 22459 Style.ColumnLimit = 20; 22460 return Style; 22461 }()); 22462 22463 // Test 9.1.2: propagate more than one level with no parent file. 22464 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 22465 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22466 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 22467 llvm::MemoryBuffer::getMemBuffer( 22468 "BasedOnStyle: InheritParentConfig\n" 22469 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 22470 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 22471 22472 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22473 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22474 ASSERT_TRUE(static_cast<bool>(Style9)); 22475 ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] { 22476 auto Style = getNoStyle(); 22477 Style.ColumnLimit = 20; 22478 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22479 return Style; 22480 }()); 22481 22482 // Test 9.2: with LLVM fallback style 22483 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 22484 ASSERT_TRUE(static_cast<bool>(Style9)); 22485 ASSERT_EQ(*Style9, [] { 22486 auto Style = getLLVMStyle(); 22487 Style.ColumnLimit = 20; 22488 return Style; 22489 }()); 22490 22491 // Test 9.3: with a parent file 22492 ASSERT_TRUE( 22493 FS.addFile("/e/.clang-format", 0, 22494 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 22495 "UseTab: Always"))); 22496 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 22497 ASSERT_TRUE(static_cast<bool>(Style9)); 22498 ASSERT_EQ(*Style9, [] { 22499 auto Style = getGoogleStyle(); 22500 Style.ColumnLimit = 20; 22501 Style.UseTab = FormatStyle::UT_Always; 22502 return Style; 22503 }()); 22504 22505 // Test 9.4: propagate more than one level with a parent file. 22506 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 22507 auto Style = getGoogleStyle(); 22508 Style.ColumnLimit = 20; 22509 Style.UseTab = FormatStyle::UT_Always; 22510 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22511 return Style; 22512 }(); 22513 22514 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22515 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22516 ASSERT_TRUE(static_cast<bool>(Style9)); 22517 ASSERT_EQ(*Style9, SubSubStyle); 22518 22519 // Test 9.5: use InheritParentConfig as style name 22520 Style9 = 22521 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 22522 ASSERT_TRUE(static_cast<bool>(Style9)); 22523 ASSERT_EQ(*Style9, SubSubStyle); 22524 22525 // Test 9.6: use command line style with inheritance 22526 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 22527 "none", "", &FS); 22528 ASSERT_TRUE(static_cast<bool>(Style9)); 22529 ASSERT_EQ(*Style9, SubSubStyle); 22530 22531 // Test 9.7: use command line style with inheritance and own config 22532 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 22533 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 22534 "/e/sub/code.cpp", "none", "", &FS); 22535 ASSERT_TRUE(static_cast<bool>(Style9)); 22536 ASSERT_EQ(*Style9, SubSubStyle); 22537 22538 // Test 9.8: use inheritance from a file without BasedOnStyle 22539 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 22540 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 22541 ASSERT_TRUE( 22542 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 22543 llvm::MemoryBuffer::getMemBuffer( 22544 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 22545 // Make sure we do not use the fallback style 22546 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 22547 ASSERT_TRUE(static_cast<bool>(Style9)); 22548 ASSERT_EQ(*Style9, [] { 22549 auto Style = getLLVMStyle(); 22550 Style.ColumnLimit = 123; 22551 return Style; 22552 }()); 22553 22554 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 22555 ASSERT_TRUE(static_cast<bool>(Style9)); 22556 ASSERT_EQ(*Style9, [] { 22557 auto Style = getLLVMStyle(); 22558 Style.ColumnLimit = 123; 22559 Style.IndentWidth = 7; 22560 return Style; 22561 }()); 22562 22563 // Test 9.9: use inheritance from a specific config file. 22564 Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp", 22565 "none", "", &FS); 22566 ASSERT_TRUE(static_cast<bool>(Style9)); 22567 ASSERT_EQ(*Style9, SubSubStyle); 22568 } 22569 22570 TEST(FormatStyle, GetStyleOfSpecificFile) { 22571 llvm::vfs::InMemoryFileSystem FS; 22572 // Specify absolute path to a format file in a parent directory. 22573 ASSERT_TRUE( 22574 FS.addFile("/e/.clang-format", 0, 22575 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 22576 ASSERT_TRUE( 22577 FS.addFile("/e/explicit.clang-format", 0, 22578 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22579 ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0, 22580 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22581 auto Style = getStyle("file:/e/explicit.clang-format", 22582 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22583 ASSERT_TRUE(static_cast<bool>(Style)); 22584 ASSERT_EQ(*Style, getGoogleStyle()); 22585 22586 // Specify relative path to a format file. 22587 ASSERT_TRUE( 22588 FS.addFile("../../e/explicit.clang-format", 0, 22589 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22590 Style = getStyle("file:../../e/explicit.clang-format", 22591 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22592 ASSERT_TRUE(static_cast<bool>(Style)); 22593 ASSERT_EQ(*Style, getGoogleStyle()); 22594 22595 // Specify path to a format file that does not exist. 22596 Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp", 22597 "LLVM", "", &FS); 22598 ASSERT_FALSE(static_cast<bool>(Style)); 22599 llvm::consumeError(Style.takeError()); 22600 22601 // Specify path to a file on the filesystem. 22602 SmallString<128> FormatFilePath; 22603 std::error_code ECF = llvm::sys::fs::createTemporaryFile( 22604 "FormatFileTest", "tpl", FormatFilePath); 22605 EXPECT_FALSE((bool)ECF); 22606 llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF); 22607 EXPECT_FALSE((bool)ECF); 22608 FormatFileTest << "BasedOnStyle: Google\n"; 22609 FormatFileTest.close(); 22610 22611 SmallString<128> TestFilePath; 22612 std::error_code ECT = 22613 llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath); 22614 EXPECT_FALSE((bool)ECT); 22615 llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT); 22616 CodeFileTest << "int i;\n"; 22617 CodeFileTest.close(); 22618 22619 std::string format_file_arg = std::string("file:") + FormatFilePath.c_str(); 22620 Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr); 22621 22622 llvm::sys::fs::remove(FormatFilePath.c_str()); 22623 llvm::sys::fs::remove(TestFilePath.c_str()); 22624 ASSERT_TRUE(static_cast<bool>(Style)); 22625 ASSERT_EQ(*Style, getGoogleStyle()); 22626 } 22627 22628 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 22629 // Column limit is 20. 22630 std::string Code = "Type *a =\n" 22631 " new Type();\n" 22632 "g(iiiii, 0, jjjjj,\n" 22633 " 0, kkkkk, 0, mm);\n" 22634 "int bad = format ;"; 22635 std::string Expected = "auto a = new Type();\n" 22636 "g(iiiii, nullptr,\n" 22637 " jjjjj, nullptr,\n" 22638 " kkkkk, nullptr,\n" 22639 " mm);\n" 22640 "int bad = format ;"; 22641 FileID ID = Context.createInMemoryFile("format.cpp", Code); 22642 tooling::Replacements Replaces = toReplacements( 22643 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 22644 "auto "), 22645 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 22646 "nullptr"), 22647 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 22648 "nullptr"), 22649 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 22650 "nullptr")}); 22651 22652 FormatStyle Style = getLLVMStyle(); 22653 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 22654 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 22655 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 22656 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 22657 auto Result = applyAllReplacements(Code, *FormattedReplaces); 22658 EXPECT_TRUE(static_cast<bool>(Result)); 22659 EXPECT_EQ(Expected, *Result); 22660 } 22661 22662 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 22663 std::string Code = "#include \"a.h\"\n" 22664 "#include \"c.h\"\n" 22665 "\n" 22666 "int main() {\n" 22667 " return 0;\n" 22668 "}"; 22669 std::string Expected = "#include \"a.h\"\n" 22670 "#include \"b.h\"\n" 22671 "#include \"c.h\"\n" 22672 "\n" 22673 "int main() {\n" 22674 " return 0;\n" 22675 "}"; 22676 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 22677 tooling::Replacements Replaces = toReplacements( 22678 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 22679 "#include \"b.h\"\n")}); 22680 22681 FormatStyle Style = getLLVMStyle(); 22682 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 22683 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 22684 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 22685 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 22686 auto Result = applyAllReplacements(Code, *FormattedReplaces); 22687 EXPECT_TRUE(static_cast<bool>(Result)); 22688 EXPECT_EQ(Expected, *Result); 22689 } 22690 22691 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 22692 EXPECT_EQ("using std::cin;\n" 22693 "using std::cout;", 22694 format("using std::cout;\n" 22695 "using std::cin;", 22696 getGoogleStyle())); 22697 } 22698 22699 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 22700 FormatStyle Style = getLLVMStyle(); 22701 Style.Standard = FormatStyle::LS_Cpp03; 22702 // cpp03 recognize this string as identifier u8 and literal character 'a' 22703 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 22704 } 22705 22706 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 22707 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 22708 // all modes, including C++11, C++14 and C++17 22709 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 22710 } 22711 22712 TEST_F(FormatTest, DoNotFormatLikelyXml) { 22713 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 22714 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 22715 } 22716 22717 TEST_F(FormatTest, StructuredBindings) { 22718 // Structured bindings is a C++17 feature. 22719 // all modes, including C++11, C++14 and C++17 22720 verifyFormat("auto [a, b] = f();"); 22721 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 22722 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 22723 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 22724 EXPECT_EQ("auto const volatile [a, b] = f();", 22725 format("auto const volatile[a, b] = f();")); 22726 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 22727 EXPECT_EQ("auto &[a, b, c] = f();", 22728 format("auto &[ a , b,c ] = f();")); 22729 EXPECT_EQ("auto &&[a, b, c] = f();", 22730 format("auto &&[ a , b,c ] = f();")); 22731 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 22732 EXPECT_EQ("auto const volatile &&[a, b] = f();", 22733 format("auto const volatile &&[a, b] = f();")); 22734 EXPECT_EQ("auto const &&[a, b] = f();", 22735 format("auto const && [a, b] = f();")); 22736 EXPECT_EQ("const auto &[a, b] = f();", 22737 format("const auto & [a, b] = f();")); 22738 EXPECT_EQ("const auto volatile &&[a, b] = f();", 22739 format("const auto volatile &&[a, b] = f();")); 22740 EXPECT_EQ("volatile const auto &&[a, b] = f();", 22741 format("volatile const auto &&[a, b] = f();")); 22742 EXPECT_EQ("const auto &&[a, b] = f();", 22743 format("const auto && [a, b] = f();")); 22744 22745 // Make sure we don't mistake structured bindings for lambdas. 22746 FormatStyle PointerMiddle = getLLVMStyle(); 22747 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 22748 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 22749 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 22750 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 22751 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 22752 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 22753 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 22754 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 22755 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 22756 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 22757 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 22758 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 22759 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 22760 22761 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 22762 format("for (const auto && [a, b] : some_range) {\n}")); 22763 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 22764 format("for (const auto & [a, b] : some_range) {\n}")); 22765 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 22766 format("for (const auto[a, b] : some_range) {\n}")); 22767 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 22768 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 22769 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 22770 EXPECT_EQ("auto const &[x, y](expr);", 22771 format("auto const & [x,y] (expr);")); 22772 EXPECT_EQ("auto const &&[x, y](expr);", 22773 format("auto const && [x,y] (expr);")); 22774 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 22775 EXPECT_EQ("auto const &[x, y]{expr};", 22776 format("auto const & [x,y] {expr};")); 22777 EXPECT_EQ("auto const &&[x, y]{expr};", 22778 format("auto const && [x,y] {expr};")); 22779 22780 FormatStyle Spaces = getLLVMStyle(); 22781 Spaces.SpacesInSquareBrackets = true; 22782 verifyFormat("auto [ a, b ] = f();", Spaces); 22783 verifyFormat("auto &&[ a, b ] = f();", Spaces); 22784 verifyFormat("auto &[ a, b ] = f();", Spaces); 22785 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 22786 verifyFormat("auto const &[ a, b ] = f();", Spaces); 22787 } 22788 22789 TEST_F(FormatTest, FileAndCode) { 22790 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 22791 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 22792 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 22793 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 22794 EXPECT_EQ(FormatStyle::LK_ObjC, 22795 guessLanguage("foo.h", "@interface Foo\n@end\n")); 22796 EXPECT_EQ( 22797 FormatStyle::LK_ObjC, 22798 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 22799 EXPECT_EQ(FormatStyle::LK_ObjC, 22800 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 22801 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 22802 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 22803 EXPECT_EQ(FormatStyle::LK_ObjC, 22804 guessLanguage("foo", "@interface Foo\n@end\n")); 22805 EXPECT_EQ(FormatStyle::LK_ObjC, 22806 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 22807 EXPECT_EQ( 22808 FormatStyle::LK_ObjC, 22809 guessLanguage("foo.h", 22810 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 22811 EXPECT_EQ( 22812 FormatStyle::LK_Cpp, 22813 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 22814 // Only one of the two preprocessor regions has ObjC-like code. 22815 EXPECT_EQ(FormatStyle::LK_ObjC, 22816 guessLanguage("foo.h", "#if A\n" 22817 "#define B() C\n" 22818 "#else\n" 22819 "#define B() [NSString a:@\"\"]\n" 22820 "#endif\n")); 22821 } 22822 22823 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 22824 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 22825 EXPECT_EQ(FormatStyle::LK_ObjC, 22826 guessLanguage("foo.h", "array[[calculator getIndex]];")); 22827 EXPECT_EQ(FormatStyle::LK_Cpp, 22828 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 22829 EXPECT_EQ( 22830 FormatStyle::LK_Cpp, 22831 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 22832 EXPECT_EQ(FormatStyle::LK_ObjC, 22833 guessLanguage("foo.h", "[[noreturn foo] bar];")); 22834 EXPECT_EQ(FormatStyle::LK_Cpp, 22835 guessLanguage("foo.h", "[[clang::fallthrough]];")); 22836 EXPECT_EQ(FormatStyle::LK_ObjC, 22837 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 22838 EXPECT_EQ(FormatStyle::LK_Cpp, 22839 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 22840 EXPECT_EQ(FormatStyle::LK_Cpp, 22841 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 22842 EXPECT_EQ(FormatStyle::LK_ObjC, 22843 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 22844 EXPECT_EQ(FormatStyle::LK_Cpp, 22845 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 22846 EXPECT_EQ( 22847 FormatStyle::LK_Cpp, 22848 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 22849 EXPECT_EQ( 22850 FormatStyle::LK_Cpp, 22851 guessLanguage("foo.h", 22852 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 22853 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 22854 } 22855 22856 TEST_F(FormatTest, GuessLanguageWithCaret) { 22857 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 22858 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 22859 EXPECT_EQ(FormatStyle::LK_ObjC, 22860 guessLanguage("foo.h", "int(^)(char, float);")); 22861 EXPECT_EQ(FormatStyle::LK_ObjC, 22862 guessLanguage("foo.h", "int(^foo)(char, float);")); 22863 EXPECT_EQ(FormatStyle::LK_ObjC, 22864 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 22865 EXPECT_EQ(FormatStyle::LK_ObjC, 22866 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 22867 EXPECT_EQ( 22868 FormatStyle::LK_ObjC, 22869 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 22870 } 22871 22872 TEST_F(FormatTest, GuessLanguageWithPragmas) { 22873 EXPECT_EQ(FormatStyle::LK_Cpp, 22874 guessLanguage("foo.h", "__pragma(warning(disable:))")); 22875 EXPECT_EQ(FormatStyle::LK_Cpp, 22876 guessLanguage("foo.h", "#pragma(warning(disable:))")); 22877 EXPECT_EQ(FormatStyle::LK_Cpp, 22878 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 22879 } 22880 22881 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 22882 // ASM symbolic names are identifiers that must be surrounded by [] without 22883 // space in between: 22884 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 22885 22886 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 22887 verifyFormat(R"(// 22888 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 22889 )"); 22890 22891 // A list of several ASM symbolic names. 22892 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 22893 22894 // ASM symbolic names in inline ASM with inputs and outputs. 22895 verifyFormat(R"(// 22896 asm("cmoveq %1, %2, %[result]" 22897 : [result] "=r"(result) 22898 : "r"(test), "r"(new), "[result]"(old)); 22899 )"); 22900 22901 // ASM symbolic names in inline ASM with no outputs. 22902 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 22903 } 22904 22905 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 22906 EXPECT_EQ(FormatStyle::LK_Cpp, 22907 guessLanguage("foo.h", "void f() {\n" 22908 " asm (\"mov %[e], %[d]\"\n" 22909 " : [d] \"=rm\" (d)\n" 22910 " [e] \"rm\" (*e));\n" 22911 "}")); 22912 EXPECT_EQ(FormatStyle::LK_Cpp, 22913 guessLanguage("foo.h", "void f() {\n" 22914 " _asm (\"mov %[e], %[d]\"\n" 22915 " : [d] \"=rm\" (d)\n" 22916 " [e] \"rm\" (*e));\n" 22917 "}")); 22918 EXPECT_EQ(FormatStyle::LK_Cpp, 22919 guessLanguage("foo.h", "void f() {\n" 22920 " __asm (\"mov %[e], %[d]\"\n" 22921 " : [d] \"=rm\" (d)\n" 22922 " [e] \"rm\" (*e));\n" 22923 "}")); 22924 EXPECT_EQ(FormatStyle::LK_Cpp, 22925 guessLanguage("foo.h", "void f() {\n" 22926 " __asm__ (\"mov %[e], %[d]\"\n" 22927 " : [d] \"=rm\" (d)\n" 22928 " [e] \"rm\" (*e));\n" 22929 "}")); 22930 EXPECT_EQ(FormatStyle::LK_Cpp, 22931 guessLanguage("foo.h", "void f() {\n" 22932 " asm (\"mov %[e], %[d]\"\n" 22933 " : [d] \"=rm\" (d),\n" 22934 " [e] \"rm\" (*e));\n" 22935 "}")); 22936 EXPECT_EQ(FormatStyle::LK_Cpp, 22937 guessLanguage("foo.h", "void f() {\n" 22938 " asm volatile (\"mov %[e], %[d]\"\n" 22939 " : [d] \"=rm\" (d)\n" 22940 " [e] \"rm\" (*e));\n" 22941 "}")); 22942 } 22943 22944 TEST_F(FormatTest, GuessLanguageWithChildLines) { 22945 EXPECT_EQ(FormatStyle::LK_Cpp, 22946 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 22947 EXPECT_EQ(FormatStyle::LK_ObjC, 22948 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 22949 EXPECT_EQ( 22950 FormatStyle::LK_Cpp, 22951 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 22952 EXPECT_EQ( 22953 FormatStyle::LK_ObjC, 22954 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 22955 } 22956 22957 TEST_F(FormatTest, TypenameMacros) { 22958 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 22959 22960 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 22961 FormatStyle Google = getGoogleStyleWithColumns(0); 22962 Google.TypenameMacros = TypenameMacros; 22963 verifyFormat("struct foo {\n" 22964 " int bar;\n" 22965 " TAILQ_ENTRY(a) bleh;\n" 22966 "};", 22967 Google); 22968 22969 FormatStyle Macros = getLLVMStyle(); 22970 Macros.TypenameMacros = TypenameMacros; 22971 22972 verifyFormat("STACK_OF(int) a;", Macros); 22973 verifyFormat("STACK_OF(int) *a;", Macros); 22974 verifyFormat("STACK_OF(int const *) *a;", Macros); 22975 verifyFormat("STACK_OF(int *const) *a;", Macros); 22976 verifyFormat("STACK_OF(int, string) a;", Macros); 22977 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 22978 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 22979 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 22980 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 22981 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 22982 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 22983 22984 Macros.PointerAlignment = FormatStyle::PAS_Left; 22985 verifyFormat("STACK_OF(int)* a;", Macros); 22986 verifyFormat("STACK_OF(int*)* a;", Macros); 22987 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 22988 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 22989 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 22990 } 22991 22992 TEST_F(FormatTest, AtomicQualifier) { 22993 // Check that we treate _Atomic as a type and not a function call 22994 FormatStyle Google = getGoogleStyleWithColumns(0); 22995 verifyFormat("struct foo {\n" 22996 " int a1;\n" 22997 " _Atomic(a) a2;\n" 22998 " _Atomic(_Atomic(int) *const) a3;\n" 22999 "};", 23000 Google); 23001 verifyFormat("_Atomic(uint64_t) a;"); 23002 verifyFormat("_Atomic(uint64_t) *a;"); 23003 verifyFormat("_Atomic(uint64_t const *) *a;"); 23004 verifyFormat("_Atomic(uint64_t *const) *a;"); 23005 verifyFormat("_Atomic(const uint64_t *) *a;"); 23006 verifyFormat("_Atomic(uint64_t) a;"); 23007 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 23008 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 23009 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 23010 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 23011 23012 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 23013 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 23014 FormatStyle Style = getLLVMStyle(); 23015 Style.PointerAlignment = FormatStyle::PAS_Left; 23016 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 23017 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 23018 verifyFormat("_Atomic(int)* a;", Style); 23019 verifyFormat("_Atomic(int*)* a;", Style); 23020 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 23021 23022 Style.SpacesInCStyleCastParentheses = true; 23023 Style.SpacesInParentheses = false; 23024 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 23025 Style.SpacesInCStyleCastParentheses = false; 23026 Style.SpacesInParentheses = true; 23027 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 23028 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 23029 } 23030 23031 TEST_F(FormatTest, AmbersandInLamda) { 23032 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 23033 FormatStyle AlignStyle = getLLVMStyle(); 23034 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 23035 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 23036 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 23037 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 23038 } 23039 23040 TEST_F(FormatTest, SpacesInConditionalStatement) { 23041 FormatStyle Spaces = getLLVMStyle(); 23042 Spaces.IfMacros.clear(); 23043 Spaces.IfMacros.push_back("MYIF"); 23044 Spaces.SpacesInConditionalStatement = true; 23045 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 23046 verifyFormat("if ( !a )\n return;", Spaces); 23047 verifyFormat("if ( a )\n return;", Spaces); 23048 verifyFormat("if constexpr ( a )\n return;", Spaces); 23049 verifyFormat("MYIF ( a )\n return;", Spaces); 23050 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 23051 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 23052 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 23053 verifyFormat("while ( a )\n return;", Spaces); 23054 verifyFormat("while ( (a && b) )\n return;", Spaces); 23055 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 23056 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 23057 // Check that space on the left of "::" is inserted as expected at beginning 23058 // of condition. 23059 verifyFormat("while ( ::func() )\n return;", Spaces); 23060 23061 // Check impact of ControlStatementsExceptControlMacros is honored. 23062 Spaces.SpaceBeforeParens = 23063 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 23064 verifyFormat("MYIF( a )\n return;", Spaces); 23065 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 23066 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 23067 } 23068 23069 TEST_F(FormatTest, AlternativeOperators) { 23070 // Test case for ensuring alternate operators are not 23071 // combined with their right most neighbour. 23072 verifyFormat("int a and b;"); 23073 verifyFormat("int a and_eq b;"); 23074 verifyFormat("int a bitand b;"); 23075 verifyFormat("int a bitor b;"); 23076 verifyFormat("int a compl b;"); 23077 verifyFormat("int a not b;"); 23078 verifyFormat("int a not_eq b;"); 23079 verifyFormat("int a or b;"); 23080 verifyFormat("int a xor b;"); 23081 verifyFormat("int a xor_eq b;"); 23082 verifyFormat("return this not_eq bitand other;"); 23083 verifyFormat("bool operator not_eq(const X bitand other)"); 23084 23085 verifyFormat("int a and 5;"); 23086 verifyFormat("int a and_eq 5;"); 23087 verifyFormat("int a bitand 5;"); 23088 verifyFormat("int a bitor 5;"); 23089 verifyFormat("int a compl 5;"); 23090 verifyFormat("int a not 5;"); 23091 verifyFormat("int a not_eq 5;"); 23092 verifyFormat("int a or 5;"); 23093 verifyFormat("int a xor 5;"); 23094 verifyFormat("int a xor_eq 5;"); 23095 23096 verifyFormat("int a compl(5);"); 23097 verifyFormat("int a not(5);"); 23098 23099 /* FIXME handle alternate tokens 23100 * https://en.cppreference.com/w/cpp/language/operator_alternative 23101 // alternative tokens 23102 verifyFormat("compl foo();"); // ~foo(); 23103 verifyFormat("foo() <%%>;"); // foo(); 23104 verifyFormat("void foo() <%%>;"); // void foo(){} 23105 verifyFormat("int a <:1:>;"); // int a[1];[ 23106 verifyFormat("%:define ABC abc"); // #define ABC abc 23107 verifyFormat("%:%:"); // ## 23108 */ 23109 } 23110 23111 TEST_F(FormatTest, STLWhileNotDefineChed) { 23112 verifyFormat("#if defined(while)\n" 23113 "#define while EMIT WARNING C4005\n" 23114 "#endif // while"); 23115 } 23116 23117 TEST_F(FormatTest, OperatorSpacing) { 23118 FormatStyle Style = getLLVMStyle(); 23119 Style.PointerAlignment = FormatStyle::PAS_Right; 23120 verifyFormat("Foo::operator*();", Style); 23121 verifyFormat("Foo::operator void *();", Style); 23122 verifyFormat("Foo::operator void **();", Style); 23123 verifyFormat("Foo::operator void *&();", Style); 23124 verifyFormat("Foo::operator void *&&();", Style); 23125 verifyFormat("Foo::operator void const *();", Style); 23126 verifyFormat("Foo::operator void const **();", Style); 23127 verifyFormat("Foo::operator void const *&();", Style); 23128 verifyFormat("Foo::operator void const *&&();", Style); 23129 verifyFormat("Foo::operator()(void *);", Style); 23130 verifyFormat("Foo::operator*(void *);", Style); 23131 verifyFormat("Foo::operator*();", Style); 23132 verifyFormat("Foo::operator**();", Style); 23133 verifyFormat("Foo::operator&();", Style); 23134 verifyFormat("Foo::operator<int> *();", Style); 23135 verifyFormat("Foo::operator<Foo> *();", Style); 23136 verifyFormat("Foo::operator<int> **();", Style); 23137 verifyFormat("Foo::operator<Foo> **();", Style); 23138 verifyFormat("Foo::operator<int> &();", Style); 23139 verifyFormat("Foo::operator<Foo> &();", Style); 23140 verifyFormat("Foo::operator<int> &&();", Style); 23141 verifyFormat("Foo::operator<Foo> &&();", Style); 23142 verifyFormat("Foo::operator<int> *&();", Style); 23143 verifyFormat("Foo::operator<Foo> *&();", Style); 23144 verifyFormat("Foo::operator<int> *&&();", Style); 23145 verifyFormat("Foo::operator<Foo> *&&();", Style); 23146 verifyFormat("operator*(int (*)(), class Foo);", Style); 23147 23148 verifyFormat("Foo::operator&();", Style); 23149 verifyFormat("Foo::operator void &();", Style); 23150 verifyFormat("Foo::operator void const &();", Style); 23151 verifyFormat("Foo::operator()(void &);", Style); 23152 verifyFormat("Foo::operator&(void &);", Style); 23153 verifyFormat("Foo::operator&();", Style); 23154 verifyFormat("operator&(int (&)(), class Foo);", Style); 23155 verifyFormat("operator&&(int (&)(), class Foo);", Style); 23156 23157 verifyFormat("Foo::operator&&();", Style); 23158 verifyFormat("Foo::operator**();", Style); 23159 verifyFormat("Foo::operator void &&();", Style); 23160 verifyFormat("Foo::operator void const &&();", Style); 23161 verifyFormat("Foo::operator()(void &&);", Style); 23162 verifyFormat("Foo::operator&&(void &&);", Style); 23163 verifyFormat("Foo::operator&&();", Style); 23164 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 23165 verifyFormat("operator const nsTArrayRight<E> &()", Style); 23166 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 23167 Style); 23168 verifyFormat("operator void **()", Style); 23169 verifyFormat("operator const FooRight<Object> &()", Style); 23170 verifyFormat("operator const FooRight<Object> *()", Style); 23171 verifyFormat("operator const FooRight<Object> **()", Style); 23172 verifyFormat("operator const FooRight<Object> *&()", Style); 23173 verifyFormat("operator const FooRight<Object> *&&()", Style); 23174 23175 Style.PointerAlignment = FormatStyle::PAS_Left; 23176 verifyFormat("Foo::operator*();", Style); 23177 verifyFormat("Foo::operator**();", Style); 23178 verifyFormat("Foo::operator void*();", Style); 23179 verifyFormat("Foo::operator void**();", Style); 23180 verifyFormat("Foo::operator void*&();", Style); 23181 verifyFormat("Foo::operator void*&&();", Style); 23182 verifyFormat("Foo::operator void const*();", Style); 23183 verifyFormat("Foo::operator void const**();", Style); 23184 verifyFormat("Foo::operator void const*&();", Style); 23185 verifyFormat("Foo::operator void const*&&();", Style); 23186 verifyFormat("Foo::operator/*comment*/ void*();", Style); 23187 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 23188 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 23189 verifyFormat("Foo::operator()(void*);", Style); 23190 verifyFormat("Foo::operator*(void*);", Style); 23191 verifyFormat("Foo::operator*();", Style); 23192 verifyFormat("Foo::operator<int>*();", Style); 23193 verifyFormat("Foo::operator<Foo>*();", Style); 23194 verifyFormat("Foo::operator<int>**();", Style); 23195 verifyFormat("Foo::operator<Foo>**();", Style); 23196 verifyFormat("Foo::operator<Foo>*&();", Style); 23197 verifyFormat("Foo::operator<int>&();", Style); 23198 verifyFormat("Foo::operator<Foo>&();", Style); 23199 verifyFormat("Foo::operator<int>&&();", Style); 23200 verifyFormat("Foo::operator<Foo>&&();", Style); 23201 verifyFormat("Foo::operator<int>*&();", Style); 23202 verifyFormat("Foo::operator<Foo>*&();", Style); 23203 verifyFormat("operator*(int (*)(), class Foo);", Style); 23204 23205 verifyFormat("Foo::operator&();", Style); 23206 verifyFormat("Foo::operator void&();", Style); 23207 verifyFormat("Foo::operator void const&();", Style); 23208 verifyFormat("Foo::operator/*comment*/ void&();", Style); 23209 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 23210 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 23211 verifyFormat("Foo::operator()(void&);", Style); 23212 verifyFormat("Foo::operator&(void&);", Style); 23213 verifyFormat("Foo::operator&();", Style); 23214 verifyFormat("operator&(int (&)(), class Foo);", Style); 23215 verifyFormat("operator&(int (&&)(), class Foo);", Style); 23216 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 23217 23218 verifyFormat("Foo::operator&&();", Style); 23219 verifyFormat("Foo::operator void&&();", Style); 23220 verifyFormat("Foo::operator void const&&();", Style); 23221 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 23222 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 23223 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 23224 verifyFormat("Foo::operator()(void&&);", Style); 23225 verifyFormat("Foo::operator&&(void&&);", Style); 23226 verifyFormat("Foo::operator&&();", Style); 23227 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 23228 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 23229 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 23230 Style); 23231 verifyFormat("operator void**()", Style); 23232 verifyFormat("operator const FooLeft<Object>&()", Style); 23233 verifyFormat("operator const FooLeft<Object>*()", Style); 23234 verifyFormat("operator const FooLeft<Object>**()", Style); 23235 verifyFormat("operator const FooLeft<Object>*&()", Style); 23236 verifyFormat("operator const FooLeft<Object>*&&()", Style); 23237 23238 // PR45107 23239 verifyFormat("operator Vector<String>&();", Style); 23240 verifyFormat("operator const Vector<String>&();", Style); 23241 verifyFormat("operator foo::Bar*();", Style); 23242 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 23243 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 23244 Style); 23245 23246 Style.PointerAlignment = FormatStyle::PAS_Middle; 23247 verifyFormat("Foo::operator*();", Style); 23248 verifyFormat("Foo::operator void *();", Style); 23249 verifyFormat("Foo::operator()(void *);", Style); 23250 verifyFormat("Foo::operator*(void *);", Style); 23251 verifyFormat("Foo::operator*();", Style); 23252 verifyFormat("operator*(int (*)(), class Foo);", Style); 23253 23254 verifyFormat("Foo::operator&();", Style); 23255 verifyFormat("Foo::operator void &();", Style); 23256 verifyFormat("Foo::operator void const &();", Style); 23257 verifyFormat("Foo::operator()(void &);", Style); 23258 verifyFormat("Foo::operator&(void &);", Style); 23259 verifyFormat("Foo::operator&();", Style); 23260 verifyFormat("operator&(int (&)(), class Foo);", Style); 23261 23262 verifyFormat("Foo::operator&&();", Style); 23263 verifyFormat("Foo::operator void &&();", Style); 23264 verifyFormat("Foo::operator void const &&();", Style); 23265 verifyFormat("Foo::operator()(void &&);", Style); 23266 verifyFormat("Foo::operator&&(void &&);", Style); 23267 verifyFormat("Foo::operator&&();", Style); 23268 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 23269 } 23270 23271 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 23272 FormatStyle Style = getLLVMStyle(); 23273 // PR46157 23274 verifyFormat("foo(operator+, -42);", Style); 23275 verifyFormat("foo(operator++, -42);", Style); 23276 verifyFormat("foo(operator--, -42);", Style); 23277 verifyFormat("foo(-42, operator--);", Style); 23278 verifyFormat("foo(-42, operator, );", Style); 23279 verifyFormat("foo(operator, , -42);", Style); 23280 } 23281 23282 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 23283 FormatStyle Style = getLLVMStyle(); 23284 Style.WhitespaceSensitiveMacros.push_back("FOO"); 23285 23286 // Don't use the helpers here, since 'mess up' will change the whitespace 23287 // and these are all whitespace sensitive by definition 23288 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 23289 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 23290 EXPECT_EQ( 23291 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 23292 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 23293 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 23294 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 23295 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 23296 " Still=Intentional);", 23297 format("FOO(String-ized&Messy+But,: :\n" 23298 " Still=Intentional);", 23299 Style)); 23300 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 23301 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 23302 " Still=Intentional);", 23303 format("FOO(String-ized=&Messy+But,: :\n" 23304 " Still=Intentional);", 23305 Style)); 23306 23307 Style.ColumnLimit = 21; 23308 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 23309 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 23310 } 23311 23312 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 23313 // These tests are not in NamespaceFixer because that doesn't 23314 // test its interaction with line wrapping 23315 FormatStyle Style = getLLVMStyleWithColumns(80); 23316 verifyFormat("namespace {\n" 23317 "int i;\n" 23318 "int j;\n" 23319 "} // namespace", 23320 Style); 23321 23322 verifyFormat("namespace AAA {\n" 23323 "int i;\n" 23324 "int j;\n" 23325 "} // namespace AAA", 23326 Style); 23327 23328 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 23329 "int i;\n" 23330 "int j;\n" 23331 "} // namespace Averyveryveryverylongnamespace", 23332 format("namespace Averyveryveryverylongnamespace {\n" 23333 "int i;\n" 23334 "int j;\n" 23335 "}", 23336 Style)); 23337 23338 EXPECT_EQ( 23339 "namespace " 23340 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 23341 " went::mad::now {\n" 23342 "int i;\n" 23343 "int j;\n" 23344 "} // namespace\n" 23345 " // " 23346 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 23347 "went::mad::now", 23348 format("namespace " 23349 "would::it::save::you::a::lot::of::time::if_::i::" 23350 "just::gave::up::and_::went::mad::now {\n" 23351 "int i;\n" 23352 "int j;\n" 23353 "}", 23354 Style)); 23355 23356 // This used to duplicate the comment again and again on subsequent runs 23357 EXPECT_EQ( 23358 "namespace " 23359 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 23360 " went::mad::now {\n" 23361 "int i;\n" 23362 "int j;\n" 23363 "} // namespace\n" 23364 " // " 23365 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 23366 "went::mad::now", 23367 format("namespace " 23368 "would::it::save::you::a::lot::of::time::if_::i::" 23369 "just::gave::up::and_::went::mad::now {\n" 23370 "int i;\n" 23371 "int j;\n" 23372 "} // namespace\n" 23373 " // " 23374 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 23375 "and_::went::mad::now", 23376 Style)); 23377 } 23378 23379 TEST_F(FormatTest, LikelyUnlikely) { 23380 FormatStyle Style = getLLVMStyle(); 23381 23382 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23383 " return 29;\n" 23384 "}", 23385 Style); 23386 23387 verifyFormat("if (argc > 5) [[likely]] {\n" 23388 " return 29;\n" 23389 "}", 23390 Style); 23391 23392 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23393 " return 29;\n" 23394 "} else [[likely]] {\n" 23395 " return 42;\n" 23396 "}\n", 23397 Style); 23398 23399 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23400 " return 29;\n" 23401 "} else if (argc > 10) [[likely]] {\n" 23402 " return 99;\n" 23403 "} else {\n" 23404 " return 42;\n" 23405 "}\n", 23406 Style); 23407 23408 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 23409 " return 29;\n" 23410 "}", 23411 Style); 23412 23413 verifyFormat("if (argc > 5) [[unlikely]]\n" 23414 " return 29;\n", 23415 Style); 23416 verifyFormat("if (argc > 5) [[likely]]\n" 23417 " return 29;\n", 23418 Style); 23419 23420 Style.AttributeMacros.push_back("UNLIKELY"); 23421 Style.AttributeMacros.push_back("LIKELY"); 23422 verifyFormat("if (argc > 5) UNLIKELY\n" 23423 " return 29;\n", 23424 Style); 23425 23426 verifyFormat("if (argc > 5) UNLIKELY {\n" 23427 " return 29;\n" 23428 "}", 23429 Style); 23430 verifyFormat("if (argc > 5) UNLIKELY {\n" 23431 " return 29;\n" 23432 "} else [[likely]] {\n" 23433 " return 42;\n" 23434 "}\n", 23435 Style); 23436 verifyFormat("if (argc > 5) UNLIKELY {\n" 23437 " return 29;\n" 23438 "} else LIKELY {\n" 23439 " return 42;\n" 23440 "}\n", 23441 Style); 23442 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23443 " return 29;\n" 23444 "} else LIKELY {\n" 23445 " return 42;\n" 23446 "}\n", 23447 Style); 23448 } 23449 23450 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 23451 verifyFormat("Constructor()\n" 23452 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23453 " aaaa(aaaaaaaaaaaaaaaaaa, " 23454 "aaaaaaaaaaaaaaaaaat))"); 23455 verifyFormat("Constructor()\n" 23456 " : aaaaaaaaaaaaa(aaaaaa), " 23457 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 23458 23459 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 23460 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 23461 verifyFormat("Constructor()\n" 23462 " : aaaaaa(aaaaaa),\n" 23463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23464 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 23465 StyleWithWhitespacePenalty); 23466 verifyFormat("Constructor()\n" 23467 " : aaaaaaaaaaaaa(aaaaaa), " 23468 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 23469 StyleWithWhitespacePenalty); 23470 } 23471 23472 TEST_F(FormatTest, LLVMDefaultStyle) { 23473 FormatStyle Style = getLLVMStyle(); 23474 verifyFormat("extern \"C\" {\n" 23475 "int foo();\n" 23476 "}", 23477 Style); 23478 } 23479 TEST_F(FormatTest, GNUDefaultStyle) { 23480 FormatStyle Style = getGNUStyle(); 23481 verifyFormat("extern \"C\"\n" 23482 "{\n" 23483 " int foo ();\n" 23484 "}", 23485 Style); 23486 } 23487 TEST_F(FormatTest, MozillaDefaultStyle) { 23488 FormatStyle Style = getMozillaStyle(); 23489 verifyFormat("extern \"C\"\n" 23490 "{\n" 23491 " int foo();\n" 23492 "}", 23493 Style); 23494 } 23495 TEST_F(FormatTest, GoogleDefaultStyle) { 23496 FormatStyle Style = getGoogleStyle(); 23497 verifyFormat("extern \"C\" {\n" 23498 "int foo();\n" 23499 "}", 23500 Style); 23501 } 23502 TEST_F(FormatTest, ChromiumDefaultStyle) { 23503 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 23504 verifyFormat("extern \"C\" {\n" 23505 "int foo();\n" 23506 "}", 23507 Style); 23508 } 23509 TEST_F(FormatTest, MicrosoftDefaultStyle) { 23510 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 23511 verifyFormat("extern \"C\"\n" 23512 "{\n" 23513 " int foo();\n" 23514 "}", 23515 Style); 23516 } 23517 TEST_F(FormatTest, WebKitDefaultStyle) { 23518 FormatStyle Style = getWebKitStyle(); 23519 verifyFormat("extern \"C\" {\n" 23520 "int foo();\n" 23521 "}", 23522 Style); 23523 } 23524 23525 TEST_F(FormatTest, Concepts) { 23526 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations, 23527 FormatStyle::BBCDS_Always); 23528 verifyFormat("template <typename T>\n" 23529 "concept True = true;"); 23530 23531 verifyFormat("template <typename T>\n" 23532 "concept C = ((false || foo()) && C2<T>) ||\n" 23533 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;", 23534 getLLVMStyleWithColumns(60)); 23535 23536 verifyFormat("template <typename T>\n" 23537 "concept DelayedCheck = true && requires(T t) { t.bar(); } && " 23538 "sizeof(T) <= 8;"); 23539 23540 verifyFormat("template <typename T>\n" 23541 "concept DelayedCheck = true && requires(T t) {\n" 23542 " t.bar();\n" 23543 " t.baz();\n" 23544 " } && sizeof(T) <= 8;"); 23545 23546 verifyFormat("template <typename T>\n" 23547 "concept DelayedCheck = true && requires(T t) { // Comment\n" 23548 " t.bar();\n" 23549 " t.baz();\n" 23550 " } && sizeof(T) <= 8;"); 23551 23552 verifyFormat("template <typename T>\n" 23553 "concept DelayedCheck = false || requires(T t) { t.bar(); } && " 23554 "sizeof(T) <= 8;"); 23555 23556 verifyFormat("template <typename T>\n" 23557 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } " 23558 "&& sizeof(T) <= 8;"); 23559 23560 verifyFormat( 23561 "template <typename T>\n" 23562 "concept DelayedCheck = static_cast<bool>(0) ||\n" 23563 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 23564 23565 verifyFormat("template <typename T>\n" 23566 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } " 23567 "&& sizeof(T) <= 8;"); 23568 23569 verifyFormat( 23570 "template <typename T>\n" 23571 "concept DelayedCheck = (bool)(0) ||\n" 23572 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 23573 23574 verifyFormat("template <typename T>\n" 23575 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } " 23576 "&& sizeof(T) <= 8;"); 23577 23578 verifyFormat("template <typename T>\n" 23579 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && " 23580 "sizeof(T) <= 8;"); 23581 23582 verifyFormat("template <typename T>\n" 23583 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n" 23584 " requires(T t) {\n" 23585 " t.bar();\n" 23586 " t.baz();\n" 23587 " } && sizeof(T) <= 8 && !(4 < 3);", 23588 getLLVMStyleWithColumns(60)); 23589 23590 verifyFormat("template <typename T>\n" 23591 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;"); 23592 23593 verifyFormat("template <typename T>\n" 23594 "concept C = foo();"); 23595 23596 verifyFormat("template <typename T>\n" 23597 "concept C = foo(T());"); 23598 23599 verifyFormat("template <typename T>\n" 23600 "concept C = foo(T{});"); 23601 23602 verifyFormat("template <typename T>\n" 23603 "concept Size = V<sizeof(T)>::Value > 5;"); 23604 23605 verifyFormat("template <typename T>\n" 23606 "concept True = S<T>::Value;"); 23607 23608 verifyFormat( 23609 "template <typename T>\n" 23610 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n" 23611 " sizeof(T) <= 8;"); 23612 23613 // FIXME: This is misformatted because the fake l paren starts at bool, not at 23614 // the lambda l square. 23615 verifyFormat("template <typename T>\n" 23616 "concept C = [] -> bool { return true; }() && requires(T t) { " 23617 "t.bar(); } &&\n" 23618 " sizeof(T) <= 8;"); 23619 23620 verifyFormat( 23621 "template <typename T>\n" 23622 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n" 23623 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 23624 23625 verifyFormat("template <typename T>\n" 23626 "concept C = decltype([]() { return std::true_type{}; " 23627 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;", 23628 getLLVMStyleWithColumns(120)); 23629 23630 verifyFormat("template <typename T>\n" 23631 "concept C = decltype([]() -> std::true_type { return {}; " 23632 "}())::value &&\n" 23633 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 23634 23635 verifyFormat("template <typename T>\n" 23636 "concept C = true;\n" 23637 "Foo Bar;"); 23638 23639 verifyFormat("template <typename T>\n" 23640 "concept Hashable = requires(T a) {\n" 23641 " { std::hash<T>{}(a) } -> " 23642 "std::convertible_to<std::size_t>;\n" 23643 " };"); 23644 23645 verifyFormat( 23646 "template <typename T>\n" 23647 "concept EqualityComparable = requires(T a, T b) {\n" 23648 " { a == b } -> std::same_as<bool>;\n" 23649 " };"); 23650 23651 verifyFormat( 23652 "template <typename T>\n" 23653 "concept EqualityComparable = requires(T a, T b) {\n" 23654 " { a == b } -> std::same_as<bool>;\n" 23655 " { a != b } -> std::same_as<bool>;\n" 23656 " };"); 23657 23658 verifyFormat("template <typename T>\n" 23659 "concept WeakEqualityComparable = requires(T a, T b) {\n" 23660 " { a == b };\n" 23661 " { a != b };\n" 23662 " };"); 23663 23664 verifyFormat("template <typename T>\n" 23665 "concept HasSizeT = requires { typename T::size_t; };"); 23666 23667 verifyFormat("template <typename T>\n" 23668 "concept Semiregular =\n" 23669 " DefaultConstructible<T> && CopyConstructible<T> && " 23670 "CopyAssignable<T> &&\n" 23671 " requires(T a, std::size_t n) {\n" 23672 " requires Same<T *, decltype(&a)>;\n" 23673 " { a.~T() } noexcept;\n" 23674 " requires Same<T *, decltype(new T)>;\n" 23675 " requires Same<T *, decltype(new T[n])>;\n" 23676 " { delete new T; };\n" 23677 " { delete new T[n]; };\n" 23678 " };"); 23679 23680 verifyFormat("template <typename T>\n" 23681 "concept Semiregular =\n" 23682 " requires(T a, std::size_t n) {\n" 23683 " requires Same<T *, decltype(&a)>;\n" 23684 " { a.~T() } noexcept;\n" 23685 " requires Same<T *, decltype(new T)>;\n" 23686 " requires Same<T *, decltype(new T[n])>;\n" 23687 " { delete new T; };\n" 23688 " { delete new T[n]; };\n" 23689 " { new T } -> std::same_as<T *>;\n" 23690 " } && DefaultConstructible<T> && CopyConstructible<T> && " 23691 "CopyAssignable<T>;"); 23692 23693 verifyFormat( 23694 "template <typename T>\n" 23695 "concept Semiregular =\n" 23696 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n" 23697 " requires Same<T *, decltype(&a)>;\n" 23698 " { a.~T() } noexcept;\n" 23699 " requires Same<T *, decltype(new T)>;\n" 23700 " requires Same<T *, decltype(new " 23701 "T[n])>;\n" 23702 " { delete new T; };\n" 23703 " { delete new T[n]; };\n" 23704 " } && CopyConstructible<T> && " 23705 "CopyAssignable<T>;"); 23706 23707 verifyFormat("template <typename T>\n" 23708 "concept Two = requires(T t) {\n" 23709 " { t.foo() } -> std::same_as<Bar>;\n" 23710 " } && requires(T &&t) {\n" 23711 " { t.foo() } -> std::same_as<Bar &&>;\n" 23712 " };"); 23713 23714 verifyFormat( 23715 "template <typename T>\n" 23716 "concept C = requires(T x) {\n" 23717 " { *x } -> std::convertible_to<typename T::inner>;\n" 23718 " { x + 1 } noexcept -> std::same_as<int>;\n" 23719 " { x * 1 } -> std::convertible_to<T>;\n" 23720 " };"); 23721 23722 verifyFormat( 23723 "template <typename T, typename U = T>\n" 23724 "concept Swappable = requires(T &&t, U &&u) {\n" 23725 " swap(std::forward<T>(t), std::forward<U>(u));\n" 23726 " swap(std::forward<U>(u), std::forward<T>(t));\n" 23727 " };"); 23728 23729 verifyFormat("template <typename T, typename U>\n" 23730 "concept Common = requires(T &&t, U &&u) {\n" 23731 " typename CommonType<T, U>;\n" 23732 " { CommonType<T, U>(std::forward<T>(t)) };\n" 23733 " };"); 23734 23735 verifyFormat("template <typename T, typename U>\n" 23736 "concept Common = requires(T &&t, U &&u) {\n" 23737 " typename CommonType<T, U>;\n" 23738 " { CommonType<T, U>{std::forward<T>(t)} };\n" 23739 " };"); 23740 23741 verifyFormat( 23742 "template <typename T>\n" 23743 "concept C = requires(T t) {\n" 23744 " requires Bar<T> && Foo<T>;\n" 23745 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 23746 " };"); 23747 23748 verifyFormat("template <typename T>\n" 23749 "concept HasFoo = requires(T t) {\n" 23750 " { t.foo() };\n" 23751 " t.foo();\n" 23752 " };\n" 23753 "template <typename T>\n" 23754 "concept HasBar = requires(T t) {\n" 23755 " { t.bar() };\n" 23756 " t.bar();\n" 23757 " };"); 23758 23759 verifyFormat("template <typename T>\n" 23760 "concept Large = sizeof(T) > 10;"); 23761 23762 verifyFormat("template <typename T, typename U>\n" 23763 "concept FooableWith = requires(T t, U u) {\n" 23764 " typename T::foo_type;\n" 23765 " { t.foo(u) } -> typename T::foo_type;\n" 23766 " t++;\n" 23767 " };\n" 23768 "void doFoo(FooableWith<int> auto t) { t.foo(3); }"); 23769 23770 verifyFormat("template <typename T>\n" 23771 "concept Context = is_specialization_of_v<context, T>;"); 23772 23773 verifyFormat("template <typename T>\n" 23774 "concept Node = std::is_object_v<T>;"); 23775 23776 auto Style = getLLVMStyle(); 23777 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed; 23778 23779 verifyFormat( 23780 "template <typename T>\n" 23781 "concept C = requires(T t) {\n" 23782 " requires Bar<T> && Foo<T>;\n" 23783 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 23784 " };", 23785 Style); 23786 23787 verifyFormat("template <typename T>\n" 23788 "concept HasFoo = requires(T t) {\n" 23789 " { t.foo() };\n" 23790 " t.foo();\n" 23791 " };\n" 23792 "template <typename T>\n" 23793 "concept HasBar = requires(T t) {\n" 23794 " { t.bar() };\n" 23795 " t.bar();\n" 23796 " };", 23797 Style); 23798 23799 verifyFormat("template <typename T> concept True = true;", Style); 23800 23801 verifyFormat("template <typename T>\n" 23802 "concept C = decltype([]() -> std::true_type { return {}; " 23803 "}())::value &&\n" 23804 " requires(T t) { t.bar(); } && sizeof(T) <= 8;", 23805 Style); 23806 23807 verifyFormat("template <typename T>\n" 23808 "concept Semiregular =\n" 23809 " DefaultConstructible<T> && CopyConstructible<T> && " 23810 "CopyAssignable<T> &&\n" 23811 " requires(T a, std::size_t n) {\n" 23812 " requires Same<T *, decltype(&a)>;\n" 23813 " { a.~T() } noexcept;\n" 23814 " requires Same<T *, decltype(new T)>;\n" 23815 " requires Same<T *, decltype(new T[n])>;\n" 23816 " { delete new T; };\n" 23817 " { delete new T[n]; };\n" 23818 " };", 23819 Style); 23820 23821 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never; 23822 23823 verifyFormat("template <typename T> concept C =\n" 23824 " requires(T t) {\n" 23825 " requires Bar<T> && Foo<T>;\n" 23826 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 23827 " };", 23828 Style); 23829 23830 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n" 23831 " { t.foo() };\n" 23832 " t.foo();\n" 23833 " };\n" 23834 "template <typename T> concept HasBar = requires(T t) {\n" 23835 " { t.bar() };\n" 23836 " t.bar();\n" 23837 " };", 23838 Style); 23839 23840 verifyFormat("template <typename T> concept True = true;", Style); 23841 23842 verifyFormat( 23843 "template <typename T> concept C = decltype([]() -> std::true_type {\n" 23844 " return {};\n" 23845 " }())::value\n" 23846 " && requires(T t) { t.bar(); } &&\n" 23847 " sizeof(T) <= 8;", 23848 Style); 23849 23850 verifyFormat("template <typename T> concept Semiregular =\n" 23851 " DefaultConstructible<T> && CopyConstructible<T> && " 23852 "CopyAssignable<T> &&\n" 23853 " requires(T a, std::size_t n) {\n" 23854 " requires Same<T *, decltype(&a)>;\n" 23855 " { a.~T() } noexcept;\n" 23856 " requires Same<T *, decltype(new T)>;\n" 23857 " requires Same<T *, decltype(new T[n])>;\n" 23858 " { delete new T; };\n" 23859 " { delete new T[n]; };\n" 23860 " };", 23861 Style); 23862 23863 // The following tests are invalid C++, we just want to make sure we don't 23864 // assert. 23865 verifyFormat("template <typename T>\n" 23866 "concept C = requires C2<T>;"); 23867 23868 verifyFormat("template <typename T>\n" 23869 "concept C = 5 + 4;"); 23870 23871 verifyFormat("template <typename T>\n" 23872 "concept C =\n" 23873 "class X;"); 23874 23875 verifyFormat("template <typename T>\n" 23876 "concept C = [] && true;"); 23877 23878 verifyFormat("template <typename T>\n" 23879 "concept C = [] && requires(T t) { typename T::size_type; };"); 23880 } 23881 23882 TEST_F(FormatTest, RequiresClausesPositions) { 23883 auto Style = getLLVMStyle(); 23884 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine); 23885 EXPECT_EQ(Style.IndentRequiresClause, true); 23886 23887 verifyFormat("template <typename T>\n" 23888 " requires(Foo<T> && std::trait<T>)\n" 23889 "struct Bar;", 23890 Style); 23891 23892 verifyFormat("template <typename T>\n" 23893 " requires(Foo<T> && std::trait<T>)\n" 23894 "class Bar {\n" 23895 "public:\n" 23896 " Bar(T t);\n" 23897 " bool baz();\n" 23898 "};", 23899 Style); 23900 23901 verifyFormat( 23902 "template <typename T>\n" 23903 " requires requires(T &&t) {\n" 23904 " typename T::I;\n" 23905 " requires(F<typename T::I> && std::trait<typename T::I>);\n" 23906 " }\n" 23907 "Bar(T) -> Bar<typename T::I>;", 23908 Style); 23909 23910 verifyFormat("template <typename T>\n" 23911 " requires(Foo<T> && std::trait<T>)\n" 23912 "constexpr T MyGlobal;", 23913 Style); 23914 23915 verifyFormat("template <typename T>\n" 23916 " requires Foo<T> && requires(T t) {\n" 23917 " { t.baz() } -> std::same_as<bool>;\n" 23918 " requires std::same_as<T::Factor, int>;\n" 23919 " }\n" 23920 "inline int bar(T t) {\n" 23921 " return t.baz() ? T::Factor : 5;\n" 23922 "}", 23923 Style); 23924 23925 verifyFormat("template <typename T>\n" 23926 "inline int bar(T t)\n" 23927 " requires Foo<T> && requires(T t) {\n" 23928 " { t.baz() } -> std::same_as<bool>;\n" 23929 " requires std::same_as<T::Factor, int>;\n" 23930 " }\n" 23931 "{\n" 23932 " return t.baz() ? T::Factor : 5;\n" 23933 "}", 23934 Style); 23935 23936 verifyFormat("template <typename T>\n" 23937 " requires F<T>\n" 23938 "int bar(T t) {\n" 23939 " return 5;\n" 23940 "}", 23941 Style); 23942 23943 verifyFormat("template <typename T>\n" 23944 "int bar(T t)\n" 23945 " requires F<T>\n" 23946 "{\n" 23947 " return 5;\n" 23948 "}", 23949 Style); 23950 23951 verifyFormat("template <typename T>\n" 23952 "int bar(T t)\n" 23953 " requires F<T>;", 23954 Style); 23955 23956 Style.IndentRequiresClause = false; 23957 verifyFormat("template <typename T>\n" 23958 "requires F<T>\n" 23959 "int bar(T t) {\n" 23960 " return 5;\n" 23961 "}", 23962 Style); 23963 23964 verifyFormat("template <typename T>\n" 23965 "int bar(T t)\n" 23966 "requires F<T>\n" 23967 "{\n" 23968 " return 5;\n" 23969 "}", 23970 Style); 23971 23972 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine; 23973 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n" 23974 "template <typename T> requires Foo<T> void bar() {}\n" 23975 "template <typename T> void bar() requires Foo<T> {}\n" 23976 "template <typename T> void bar() requires Foo<T>;\n" 23977 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;", 23978 Style); 23979 23980 auto ColumnStyle = Style; 23981 ColumnStyle.ColumnLimit = 40; 23982 verifyFormat("template <typename AAAAAAA>\n" 23983 "requires Foo<T> struct Bar {};\n" 23984 "template <typename AAAAAAA>\n" 23985 "requires Foo<T> void bar() {}\n" 23986 "template <typename AAAAAAA>\n" 23987 "void bar() requires Foo<T> {}\n" 23988 "template <typename AAAAAAA>\n" 23989 "requires Foo<T> Baz(T) -> Baz<T>;", 23990 ColumnStyle); 23991 23992 verifyFormat("template <typename T>\n" 23993 "requires Foo<AAAAAAA> struct Bar {};\n" 23994 "template <typename T>\n" 23995 "requires Foo<AAAAAAA> void bar() {}\n" 23996 "template <typename T>\n" 23997 "void bar() requires Foo<AAAAAAA> {}\n" 23998 "template <typename T>\n" 23999 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;", 24000 ColumnStyle); 24001 24002 verifyFormat("template <typename AAAAAAA>\n" 24003 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24004 "struct Bar {};\n" 24005 "template <typename AAAAAAA>\n" 24006 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24007 "void bar() {}\n" 24008 "template <typename AAAAAAA>\n" 24009 "void bar()\n" 24010 " requires Foo<AAAAAAAAAAAAAAAA> {}\n" 24011 "template <typename AAAAAAA>\n" 24012 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" 24013 "template <typename AAAAAAA>\n" 24014 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24015 "Bar(T) -> Bar<T>;", 24016 ColumnStyle); 24017 24018 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; 24019 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; 24020 24021 verifyFormat("template <typename T>\n" 24022 "requires Foo<T> struct Bar {};\n" 24023 "template <typename T>\n" 24024 "requires Foo<T> void bar() {}\n" 24025 "template <typename T>\n" 24026 "void bar()\n" 24027 "requires Foo<T> {}\n" 24028 "template <typename T>\n" 24029 "void bar()\n" 24030 "requires Foo<T>;\n" 24031 "template <typename T>\n" 24032 "requires Foo<T> Bar(T) -> Bar<T>;", 24033 Style); 24034 24035 verifyFormat("template <typename AAAAAAA>\n" 24036 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24037 "struct Bar {};\n" 24038 "template <typename AAAAAAA>\n" 24039 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24040 "void bar() {}\n" 24041 "template <typename AAAAAAA>\n" 24042 "void bar()\n" 24043 "requires Foo<AAAAAAAAAAAAAAAA> {}\n" 24044 "template <typename AAAAAAA>\n" 24045 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" 24046 "template <typename AAAAAAA>\n" 24047 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24048 "Bar(T) -> Bar<T>;", 24049 ColumnStyle); 24050 24051 Style.IndentRequiresClause = true; 24052 ColumnStyle.IndentRequiresClause = true; 24053 24054 verifyFormat("template <typename T>\n" 24055 " requires Foo<T> struct Bar {};\n" 24056 "template <typename T>\n" 24057 " requires Foo<T> void bar() {}\n" 24058 "template <typename T>\n" 24059 "void bar()\n" 24060 " requires Foo<T> {}\n" 24061 "template <typename T>\n" 24062 " requires Foo<T> Bar(T) -> Bar<T>;", 24063 Style); 24064 24065 verifyFormat("template <typename AAAAAAA>\n" 24066 " requires Foo<AAAAAAAAAAAAAAAA>\n" 24067 "struct Bar {};\n" 24068 "template <typename AAAAAAA>\n" 24069 " requires Foo<AAAAAAAAAAAAAAAA>\n" 24070 "void bar() {}\n" 24071 "template <typename AAAAAAA>\n" 24072 "void bar()\n" 24073 " requires Foo<AAAAAAAAAAAAAAAA> {}\n" 24074 "template <typename AAAAAAA>\n" 24075 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n" 24076 "template <typename AAAAAAA>\n" 24077 " requires Foo<AAAAAAAAAAAAAAAA>\n" 24078 "Bar(T) -> Bar<T>;", 24079 ColumnStyle); 24080 24081 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 24082 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 24083 24084 verifyFormat("template <typename T> requires Foo<T>\n" 24085 "struct Bar {};\n" 24086 "template <typename T> requires Foo<T>\n" 24087 "void bar() {}\n" 24088 "template <typename T>\n" 24089 "void bar() requires Foo<T>\n" 24090 "{}\n" 24091 "template <typename T> void bar() requires Foo<T>;\n" 24092 "template <typename T> requires Foo<T>\n" 24093 "Bar(T) -> Bar<T>;", 24094 Style); 24095 24096 verifyFormat("template <typename AAAAAAA>\n" 24097 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24098 "struct Bar {};\n" 24099 "template <typename AAAAAAA>\n" 24100 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24101 "void bar() {}\n" 24102 "template <typename AAAAAAA>\n" 24103 "void bar()\n" 24104 " requires Foo<AAAAAAAAAAAAAAAA>\n" 24105 "{}\n" 24106 "template <typename AAAAAAA>\n" 24107 "requires Foo<AAAAAAAA>\n" 24108 "Bar(T) -> Bar<T>;\n" 24109 "template <typename AAAAAAA>\n" 24110 "requires Foo<AAAAAAAAAAAAAAAA>\n" 24111 "Bar(T) -> Bar<T>;", 24112 ColumnStyle); 24113 } 24114 24115 TEST_F(FormatTest, RequiresClauses) { 24116 verifyFormat("struct [[nodiscard]] zero_t {\n" 24117 " template <class T>\n" 24118 " requires requires { number_zero_v<T>; }\n" 24119 " [[nodiscard]] constexpr operator T() const {\n" 24120 " return number_zero_v<T>;\n" 24121 " }\n" 24122 "};"); 24123 24124 auto Style = getLLVMStyle(); 24125 24126 verifyFormat( 24127 "template <typename T>\n" 24128 " requires is_default_constructible_v<hash<T>> and\n" 24129 " is_copy_constructible_v<hash<T>> and\n" 24130 " is_move_constructible_v<hash<T>> and\n" 24131 " is_copy_assignable_v<hash<T>> and " 24132 "is_move_assignable_v<hash<T>> and\n" 24133 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n" 24134 " is_callable_v<hash<T>(T)> and\n" 24135 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n" 24136 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n" 24137 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n" 24138 "struct S {};", 24139 Style); 24140 24141 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 24142 verifyFormat( 24143 "template <typename T>\n" 24144 " requires is_default_constructible_v<hash<T>>\n" 24145 " and is_copy_constructible_v<hash<T>>\n" 24146 " and is_move_constructible_v<hash<T>>\n" 24147 " and is_copy_assignable_v<hash<T>> and " 24148 "is_move_assignable_v<hash<T>>\n" 24149 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n" 24150 " and is_callable_v<hash<T>(T)>\n" 24151 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n" 24152 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n" 24153 " and is_same_v<size_t, decltype(hash<T>(declval<const T " 24154 "&>()))>\n" 24155 "struct S {};", 24156 Style); 24157 24158 // Not a clause, but we once hit an assert. 24159 verifyFormat("#if 0\n" 24160 "#else\n" 24161 "foo();\n" 24162 "#endif\n" 24163 "bar(requires);"); 24164 } 24165 24166 TEST_F(FormatTest, StatementAttributeLikeMacros) { 24167 FormatStyle Style = getLLVMStyle(); 24168 StringRef Source = "void Foo::slot() {\n" 24169 " unsigned char MyChar = 'x';\n" 24170 " emit signal(MyChar);\n" 24171 " Q_EMIT signal(MyChar);\n" 24172 "}"; 24173 24174 EXPECT_EQ(Source, format(Source, Style)); 24175 24176 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 24177 EXPECT_EQ("void Foo::slot() {\n" 24178 " unsigned char MyChar = 'x';\n" 24179 " emit signal(MyChar);\n" 24180 " Q_EMIT signal(MyChar);\n" 24181 "}", 24182 format(Source, Style)); 24183 24184 Style.StatementAttributeLikeMacros.push_back("emit"); 24185 EXPECT_EQ(Source, format(Source, Style)); 24186 24187 Style.StatementAttributeLikeMacros = {}; 24188 EXPECT_EQ("void Foo::slot() {\n" 24189 " unsigned char MyChar = 'x';\n" 24190 " emit signal(MyChar);\n" 24191 " Q_EMIT signal(MyChar);\n" 24192 "}", 24193 format(Source, Style)); 24194 } 24195 24196 TEST_F(FormatTest, IndentAccessModifiers) { 24197 FormatStyle Style = getLLVMStyle(); 24198 Style.IndentAccessModifiers = true; 24199 // Members are *two* levels below the record; 24200 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 24201 verifyFormat("class C {\n" 24202 " int i;\n" 24203 "};\n", 24204 Style); 24205 verifyFormat("union C {\n" 24206 " int i;\n" 24207 " unsigned u;\n" 24208 "};\n", 24209 Style); 24210 // Access modifiers should be indented one level below the record. 24211 verifyFormat("class C {\n" 24212 " public:\n" 24213 " int i;\n" 24214 "};\n", 24215 Style); 24216 verifyFormat("struct S {\n" 24217 " private:\n" 24218 " class C {\n" 24219 " int j;\n" 24220 "\n" 24221 " public:\n" 24222 " C();\n" 24223 " };\n" 24224 "\n" 24225 " public:\n" 24226 " int i;\n" 24227 "};\n", 24228 Style); 24229 // Enumerations are not records and should be unaffected. 24230 Style.AllowShortEnumsOnASingleLine = false; 24231 verifyFormat("enum class E {\n" 24232 " A,\n" 24233 " B\n" 24234 "};\n", 24235 Style); 24236 // Test with a different indentation width; 24237 // also proves that the result is Style.AccessModifierOffset agnostic. 24238 Style.IndentWidth = 3; 24239 verifyFormat("class C {\n" 24240 " public:\n" 24241 " int i;\n" 24242 "};\n", 24243 Style); 24244 } 24245 24246 TEST_F(FormatTest, LimitlessStringsAndComments) { 24247 auto Style = getLLVMStyleWithColumns(0); 24248 constexpr StringRef Code = 24249 "/**\n" 24250 " * This is a multiline comment with quite some long lines, at least for " 24251 "the LLVM Style.\n" 24252 " * We will redo this with strings and line comments. Just to check if " 24253 "everything is working.\n" 24254 " */\n" 24255 "bool foo() {\n" 24256 " /* Single line multi line comment. */\n" 24257 " const std::string String = \"This is a multiline string with quite " 24258 "some long lines, at least for the LLVM Style.\"\n" 24259 " \"We already did it with multi line " 24260 "comments, and we will do it with line comments. Just to check if " 24261 "everything is working.\";\n" 24262 " // This is a line comment (block) with quite some long lines, at " 24263 "least for the LLVM Style.\n" 24264 " // We already did this with multi line comments and strings. Just to " 24265 "check if everything is working.\n" 24266 " const std::string SmallString = \"Hello World\";\n" 24267 " // Small line comment\n" 24268 " return String.size() > SmallString.size();\n" 24269 "}"; 24270 EXPECT_EQ(Code, format(Code, Style)); 24271 } 24272 24273 TEST_F(FormatTest, FormatDecayCopy) { 24274 // error cases from unit tests 24275 verifyFormat("foo(auto())"); 24276 verifyFormat("foo(auto{})"); 24277 verifyFormat("foo(auto({}))"); 24278 verifyFormat("foo(auto{{}})"); 24279 24280 verifyFormat("foo(auto(1))"); 24281 verifyFormat("foo(auto{1})"); 24282 verifyFormat("foo(new auto(1))"); 24283 verifyFormat("foo(new auto{1})"); 24284 verifyFormat("decltype(auto(1)) x;"); 24285 verifyFormat("decltype(auto{1}) x;"); 24286 verifyFormat("auto(x);"); 24287 verifyFormat("auto{x};"); 24288 verifyFormat("new auto{x};"); 24289 verifyFormat("auto{x} = y;"); 24290 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 24291 // the user's own fault 24292 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 24293 // clearly the user's own fault 24294 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 24295 } 24296 24297 TEST_F(FormatTest, Cpp20ModulesSupport) { 24298 FormatStyle Style = getLLVMStyle(); 24299 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 24300 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 24301 24302 verifyFormat("export import foo;", Style); 24303 verifyFormat("export import foo:bar;", Style); 24304 verifyFormat("export import foo.bar;", Style); 24305 verifyFormat("export import foo.bar:baz;", Style); 24306 verifyFormat("export import :bar;", Style); 24307 verifyFormat("export module foo:bar;", Style); 24308 verifyFormat("export module foo;", Style); 24309 verifyFormat("export module foo.bar;", Style); 24310 verifyFormat("export module foo.bar:baz;", Style); 24311 verifyFormat("export import <string_view>;", Style); 24312 24313 verifyFormat("export type_name var;", Style); 24314 verifyFormat("template <class T> export using A = B<T>;", Style); 24315 verifyFormat("export using A = B;", Style); 24316 verifyFormat("export int func() {\n" 24317 " foo();\n" 24318 "}", 24319 Style); 24320 verifyFormat("export struct {\n" 24321 " int foo;\n" 24322 "};", 24323 Style); 24324 verifyFormat("export {\n" 24325 " int foo;\n" 24326 "};", 24327 Style); 24328 verifyFormat("export export char const *hello() { return \"hello\"; }"); 24329 24330 verifyFormat("import bar;", Style); 24331 verifyFormat("import foo.bar;", Style); 24332 verifyFormat("import foo:bar;", Style); 24333 verifyFormat("import :bar;", Style); 24334 verifyFormat("import <ctime>;", Style); 24335 verifyFormat("import \"header\";", Style); 24336 24337 verifyFormat("module foo;", Style); 24338 verifyFormat("module foo:bar;", Style); 24339 verifyFormat("module foo.bar;", Style); 24340 verifyFormat("module;", Style); 24341 24342 verifyFormat("export namespace hi {\n" 24343 "const char *sayhi();\n" 24344 "}", 24345 Style); 24346 24347 verifyFormat("module :private;", Style); 24348 verifyFormat("import <foo/bar.h>;", Style); 24349 verifyFormat("import foo...bar;", Style); 24350 verifyFormat("import ..........;", Style); 24351 verifyFormat("module foo:private;", Style); 24352 verifyFormat("import a", Style); 24353 verifyFormat("module a", Style); 24354 verifyFormat("export import a", Style); 24355 verifyFormat("export module a", Style); 24356 24357 verifyFormat("import", Style); 24358 verifyFormat("module", Style); 24359 verifyFormat("export", Style); 24360 } 24361 24362 TEST_F(FormatTest, CoroutineForCoawait) { 24363 FormatStyle Style = getLLVMStyle(); 24364 verifyFormat("for co_await (auto x : range())\n ;"); 24365 verifyFormat("for (auto i : arr) {\n" 24366 "}", 24367 Style); 24368 verifyFormat("for co_await (auto i : arr) {\n" 24369 "}", 24370 Style); 24371 verifyFormat("for co_await (auto i : foo(T{})) {\n" 24372 "}", 24373 Style); 24374 } 24375 24376 TEST_F(FormatTest, CoroutineCoAwait) { 24377 verifyFormat("int x = co_await foo();"); 24378 verifyFormat("int x = (co_await foo());"); 24379 verifyFormat("co_await (42);"); 24380 verifyFormat("void operator co_await(int);"); 24381 verifyFormat("void operator co_await(a);"); 24382 verifyFormat("co_await a;"); 24383 verifyFormat("co_await missing_await_resume{};"); 24384 verifyFormat("co_await a; // comment"); 24385 verifyFormat("void test0() { co_await a; }"); 24386 verifyFormat("co_await co_await co_await foo();"); 24387 verifyFormat("co_await foo().bar();"); 24388 verifyFormat("co_await [this]() -> Task { co_return x; }"); 24389 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 24390 "foo(); }(x, y);"); 24391 24392 FormatStyle Style = getLLVMStyleWithColumns(40); 24393 verifyFormat("co_await [this](int a, int b) -> Task {\n" 24394 " co_return co_await foo();\n" 24395 "}(x, y);", 24396 Style); 24397 verifyFormat("co_await;"); 24398 } 24399 24400 TEST_F(FormatTest, CoroutineCoYield) { 24401 verifyFormat("int x = co_yield foo();"); 24402 verifyFormat("int x = (co_yield foo());"); 24403 verifyFormat("co_yield (42);"); 24404 verifyFormat("co_yield {42};"); 24405 verifyFormat("co_yield 42;"); 24406 verifyFormat("co_yield n++;"); 24407 verifyFormat("co_yield ++n;"); 24408 verifyFormat("co_yield;"); 24409 } 24410 24411 TEST_F(FormatTest, CoroutineCoReturn) { 24412 verifyFormat("co_return (42);"); 24413 verifyFormat("co_return;"); 24414 verifyFormat("co_return {};"); 24415 verifyFormat("co_return x;"); 24416 verifyFormat("co_return co_await foo();"); 24417 verifyFormat("co_return co_yield foo();"); 24418 } 24419 24420 TEST_F(FormatTest, EmptyShortBlock) { 24421 auto Style = getLLVMStyle(); 24422 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 24423 24424 verifyFormat("try {\n" 24425 " doA();\n" 24426 "} catch (Exception &e) {\n" 24427 " e.printStackTrace();\n" 24428 "}\n", 24429 Style); 24430 24431 verifyFormat("try {\n" 24432 " doA();\n" 24433 "} catch (Exception &e) {}\n", 24434 Style); 24435 } 24436 24437 TEST_F(FormatTest, ShortTemplatedArgumentLists) { 24438 auto Style = getLLVMStyle(); 24439 24440 verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style); 24441 verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style); 24442 verifyFormat("struct Y : X<[] { return 0; }> {};", Style); 24443 verifyFormat("struct Y<[] { return 0; }> {};", Style); 24444 24445 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style); 24446 verifyFormat("template <int N> struct Foo<char[N]> {};", Style); 24447 } 24448 24449 TEST_F(FormatTest, InsertBraces) { 24450 FormatStyle Style = getLLVMStyle(); 24451 Style.InsertBraces = true; 24452 24453 verifyFormat("// clang-format off\n" 24454 "// comment\n" 24455 "if (a) f();\n" 24456 "// clang-format on\n" 24457 "if (b) {\n" 24458 " g();\n" 24459 "}", 24460 "// clang-format off\n" 24461 "// comment\n" 24462 "if (a) f();\n" 24463 "// clang-format on\n" 24464 "if (b) g();", 24465 Style); 24466 24467 verifyFormat("if (a) {\n" 24468 " switch (b) {\n" 24469 " case 1:\n" 24470 " c = 0;\n" 24471 " break;\n" 24472 " default:\n" 24473 " c = 1;\n" 24474 " }\n" 24475 "}", 24476 "if (a)\n" 24477 " switch (b) {\n" 24478 " case 1:\n" 24479 " c = 0;\n" 24480 " break;\n" 24481 " default:\n" 24482 " c = 1;\n" 24483 " }", 24484 Style); 24485 24486 verifyFormat("for (auto node : nodes) {\n" 24487 " if (node) {\n" 24488 " break;\n" 24489 " }\n" 24490 "}", 24491 "for (auto node : nodes)\n" 24492 " if (node)\n" 24493 " break;", 24494 Style); 24495 24496 verifyFormat("for (auto node : nodes) {\n" 24497 " if (node)\n" 24498 "}", 24499 "for (auto node : nodes)\n" 24500 " if (node)", 24501 Style); 24502 24503 verifyFormat("do {\n" 24504 " --a;\n" 24505 "} while (a);", 24506 "do\n" 24507 " --a;\n" 24508 "while (a);", 24509 Style); 24510 24511 verifyFormat("if (i) {\n" 24512 " ++i;\n" 24513 "} else {\n" 24514 " --i;\n" 24515 "}", 24516 "if (i)\n" 24517 " ++i;\n" 24518 "else {\n" 24519 " --i;\n" 24520 "}", 24521 Style); 24522 24523 verifyFormat("void f() {\n" 24524 " while (j--) {\n" 24525 " while (i) {\n" 24526 " --i;\n" 24527 " }\n" 24528 " }\n" 24529 "}", 24530 "void f() {\n" 24531 " while (j--)\n" 24532 " while (i)\n" 24533 " --i;\n" 24534 "}", 24535 Style); 24536 24537 verifyFormat("f({\n" 24538 " if (a) {\n" 24539 " g();\n" 24540 " }\n" 24541 "});", 24542 "f({\n" 24543 " if (a)\n" 24544 " g();\n" 24545 "});", 24546 Style); 24547 24548 verifyFormat("if (a) {\n" 24549 " f();\n" 24550 "} else if (b) {\n" 24551 " g();\n" 24552 "} else {\n" 24553 " h();\n" 24554 "}", 24555 "if (a)\n" 24556 " f();\n" 24557 "else if (b)\n" 24558 " g();\n" 24559 "else\n" 24560 " h();", 24561 Style); 24562 24563 verifyFormat("if (a) {\n" 24564 " f();\n" 24565 "}\n" 24566 "// comment\n" 24567 "/* comment */", 24568 "if (a)\n" 24569 " f();\n" 24570 "// comment\n" 24571 "/* comment */", 24572 Style); 24573 24574 verifyFormat("if (a) {\n" 24575 " // foo\n" 24576 " // bar\n" 24577 " f();\n" 24578 "}", 24579 "if (a)\n" 24580 " // foo\n" 24581 " // bar\n" 24582 " f();", 24583 Style); 24584 24585 verifyFormat("if (a) { // comment\n" 24586 " // comment\n" 24587 " f();\n" 24588 "}", 24589 "if (a) // comment\n" 24590 " // comment\n" 24591 " f();", 24592 Style); 24593 24594 verifyFormat("if (a) {\n" 24595 " f(); // comment\n" 24596 "}", 24597 "if (a)\n" 24598 " f(); // comment", 24599 Style); 24600 24601 verifyFormat("if (a) {\n" 24602 " f();\n" 24603 "}\n" 24604 "#undef A\n" 24605 "#undef B", 24606 "if (a)\n" 24607 " f();\n" 24608 "#undef A\n" 24609 "#undef B", 24610 Style); 24611 24612 verifyFormat("if (a)\n" 24613 "#ifdef A\n" 24614 " f();\n" 24615 "#else\n" 24616 " g();\n" 24617 "#endif", 24618 Style); 24619 24620 verifyFormat("#if 0\n" 24621 "#elif 1\n" 24622 "#endif\n" 24623 "void f() {\n" 24624 " if (a) {\n" 24625 " g();\n" 24626 " }\n" 24627 "}", 24628 "#if 0\n" 24629 "#elif 1\n" 24630 "#endif\n" 24631 "void f() {\n" 24632 " if (a) g();\n" 24633 "}", 24634 Style); 24635 24636 Style.ColumnLimit = 15; 24637 24638 verifyFormat("#define A \\\n" 24639 " if (a) \\\n" 24640 " f();", 24641 Style); 24642 24643 verifyFormat("if (a + b >\n" 24644 " c) {\n" 24645 " f();\n" 24646 "}", 24647 "if (a + b > c)\n" 24648 " f();", 24649 Style); 24650 } 24651 24652 TEST_F(FormatTest, RemoveBraces) { 24653 FormatStyle Style = getLLVMStyle(); 24654 Style.RemoveBracesLLVM = true; 24655 24656 // The following eight test cases are fully-braced versions of the examples at 24657 // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single- 24658 // statement-bodies-of-if-else-loop-statements". 24659 24660 // 1. Omit the braces, since the body is simple and clearly associated with 24661 // the if. 24662 verifyFormat("if (isa<FunctionDecl>(D))\n" 24663 " handleFunctionDecl(D);\n" 24664 "else if (isa<VarDecl>(D))\n" 24665 " handleVarDecl(D);", 24666 "if (isa<FunctionDecl>(D)) {\n" 24667 " handleFunctionDecl(D);\n" 24668 "} else if (isa<VarDecl>(D)) {\n" 24669 " handleVarDecl(D);\n" 24670 "}", 24671 Style); 24672 24673 // 2. Here we document the condition itself and not the body. 24674 verifyFormat("if (isa<VarDecl>(D)) {\n" 24675 " // It is necessary that we explain the situation with this\n" 24676 " // surprisingly long comment, so it would be unclear\n" 24677 " // without the braces whether the following statement is in\n" 24678 " // the scope of the `if`.\n" 24679 " // Because the condition is documented, we can't really\n" 24680 " // hoist this comment that applies to the body above the\n" 24681 " // if.\n" 24682 " handleOtherDecl(D);\n" 24683 "}", 24684 Style); 24685 24686 // 3. Use braces on the outer `if` to avoid a potential dangling else 24687 // situation. 24688 verifyFormat("if (isa<VarDecl>(D)) {\n" 24689 " for (auto *A : D.attrs())\n" 24690 " if (shouldProcessAttr(A))\n" 24691 " handleAttr(A);\n" 24692 "}", 24693 "if (isa<VarDecl>(D)) {\n" 24694 " for (auto *A : D.attrs()) {\n" 24695 " if (shouldProcessAttr(A)) {\n" 24696 " handleAttr(A);\n" 24697 " }\n" 24698 " }\n" 24699 "}", 24700 Style); 24701 24702 // 4. Use braces for the `if` block to keep it uniform with the else block. 24703 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 24704 " handleFunctionDecl(D);\n" 24705 "} else {\n" 24706 " // In this else case, it is necessary that we explain the\n" 24707 " // situation with this surprisingly long comment, so it\n" 24708 " // would be unclear without the braces whether the\n" 24709 " // following statement is in the scope of the `if`.\n" 24710 " handleOtherDecl(D);\n" 24711 "}", 24712 Style); 24713 24714 // 5. This should also omit braces. The `for` loop contains only a single 24715 // statement, so it shouldn't have braces. The `if` also only contains a 24716 // single simple statement (the for loop), so it also should omit braces. 24717 verifyFormat("if (isa<FunctionDecl>(D))\n" 24718 " for (auto *A : D.attrs())\n" 24719 " handleAttr(A);", 24720 "if (isa<FunctionDecl>(D)) {\n" 24721 " for (auto *A : D.attrs()) {\n" 24722 " handleAttr(A);\n" 24723 " }\n" 24724 "}", 24725 Style); 24726 24727 // 6. Use braces for the outer `if` since the nested `for` is braced. 24728 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 24729 " for (auto *A : D.attrs()) {\n" 24730 " // In this for loop body, it is necessary that we explain\n" 24731 " // the situation with this surprisingly long comment,\n" 24732 " // forcing braces on the `for` block.\n" 24733 " handleAttr(A);\n" 24734 " }\n" 24735 "}", 24736 Style); 24737 24738 // 7. Use braces on the outer block because there are more than two levels of 24739 // nesting. 24740 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 24741 " for (auto *A : D.attrs())\n" 24742 " for (ssize_t i : llvm::seq<ssize_t>(count))\n" 24743 " handleAttrOnDecl(D, A, i);\n" 24744 "}", 24745 "if (isa<FunctionDecl>(D)) {\n" 24746 " for (auto *A : D.attrs()) {\n" 24747 " for (ssize_t i : llvm::seq<ssize_t>(count)) {\n" 24748 " handleAttrOnDecl(D, A, i);\n" 24749 " }\n" 24750 " }\n" 24751 "}", 24752 Style); 24753 24754 // 8. Use braces on the outer block because of a nested `if`, otherwise the 24755 // compiler would warn: `add explicit braces to avoid dangling else` 24756 verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 24757 " if (shouldProcess(D))\n" 24758 " handleVarDecl(D);\n" 24759 " else\n" 24760 " markAsIgnored(D);\n" 24761 "}", 24762 "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 24763 " if (shouldProcess(D)) {\n" 24764 " handleVarDecl(D);\n" 24765 " } else {\n" 24766 " markAsIgnored(D);\n" 24767 " }\n" 24768 "}", 24769 Style); 24770 24771 verifyFormat("if (a)\n" 24772 " b; // comment\n" 24773 "else if (c)\n" 24774 " d; /* comment */\n" 24775 "else\n" 24776 " e;", 24777 "if (a) {\n" 24778 " b; // comment\n" 24779 "} else if (c) {\n" 24780 " d; /* comment */\n" 24781 "} else {\n" 24782 " e;\n" 24783 "}", 24784 Style); 24785 24786 verifyFormat("if (a) {\n" 24787 " b;\n" 24788 " c;\n" 24789 "} else if (d) {\n" 24790 " e;\n" 24791 "}", 24792 Style); 24793 24794 verifyFormat("if (a) {\n" 24795 "#undef NDEBUG\n" 24796 " b;\n" 24797 "} else {\n" 24798 " c;\n" 24799 "}", 24800 Style); 24801 24802 verifyFormat("if (a) {\n" 24803 " // comment\n" 24804 "} else if (b) {\n" 24805 " c;\n" 24806 "}", 24807 Style); 24808 24809 verifyFormat("if (a) {\n" 24810 " b;\n" 24811 "} else {\n" 24812 " { c; }\n" 24813 "}", 24814 Style); 24815 24816 verifyFormat("if (a) {\n" 24817 " if (b) // comment\n" 24818 " c;\n" 24819 "} else if (d) {\n" 24820 " e;\n" 24821 "}", 24822 "if (a) {\n" 24823 " if (b) { // comment\n" 24824 " c;\n" 24825 " }\n" 24826 "} else if (d) {\n" 24827 " e;\n" 24828 "}", 24829 Style); 24830 24831 verifyFormat("if (a) {\n" 24832 " if (b) {\n" 24833 " c;\n" 24834 " // comment\n" 24835 " } else if (d) {\n" 24836 " e;\n" 24837 " }\n" 24838 "}", 24839 Style); 24840 24841 verifyFormat("if (a) {\n" 24842 " if (b)\n" 24843 " c;\n" 24844 "}", 24845 "if (a) {\n" 24846 " if (b) {\n" 24847 " c;\n" 24848 " }\n" 24849 "}", 24850 Style); 24851 24852 verifyFormat("if (a)\n" 24853 " if (b)\n" 24854 " c;\n" 24855 " else\n" 24856 " d;\n" 24857 "else\n" 24858 " e;", 24859 "if (a) {\n" 24860 " if (b) {\n" 24861 " c;\n" 24862 " } else {\n" 24863 " d;\n" 24864 " }\n" 24865 "} else {\n" 24866 " e;\n" 24867 "}", 24868 Style); 24869 24870 verifyFormat("if (a) {\n" 24871 " // comment\n" 24872 " if (b)\n" 24873 " c;\n" 24874 " else if (d)\n" 24875 " e;\n" 24876 "} else {\n" 24877 " g;\n" 24878 "}", 24879 "if (a) {\n" 24880 " // comment\n" 24881 " if (b) {\n" 24882 " c;\n" 24883 " } else if (d) {\n" 24884 " e;\n" 24885 " }\n" 24886 "} else {\n" 24887 " g;\n" 24888 "}", 24889 Style); 24890 24891 verifyFormat("if (a)\n" 24892 " b;\n" 24893 "else if (c)\n" 24894 " d;\n" 24895 "else\n" 24896 " e;", 24897 "if (a) {\n" 24898 " b;\n" 24899 "} else {\n" 24900 " if (c) {\n" 24901 " d;\n" 24902 " } else {\n" 24903 " e;\n" 24904 " }\n" 24905 "}", 24906 Style); 24907 24908 verifyFormat("if (a) {\n" 24909 " if (b)\n" 24910 " c;\n" 24911 " else if (d)\n" 24912 " e;\n" 24913 "} else {\n" 24914 " g;\n" 24915 "}", 24916 "if (a) {\n" 24917 " if (b)\n" 24918 " c;\n" 24919 " else {\n" 24920 " if (d)\n" 24921 " e;\n" 24922 " }\n" 24923 "} else {\n" 24924 " g;\n" 24925 "}", 24926 Style); 24927 24928 verifyFormat("if (a)\n" 24929 " b;\n" 24930 "else if (c)\n" 24931 " while (d)\n" 24932 " e;\n" 24933 "// comment", 24934 "if (a)\n" 24935 "{\n" 24936 " b;\n" 24937 "} else if (c) {\n" 24938 " while (d) {\n" 24939 " e;\n" 24940 " }\n" 24941 "}\n" 24942 "// comment", 24943 Style); 24944 24945 verifyFormat("if (a) {\n" 24946 " b;\n" 24947 "} else if (c) {\n" 24948 " d;\n" 24949 "} else {\n" 24950 " e;\n" 24951 " g;\n" 24952 "}", 24953 Style); 24954 24955 verifyFormat("if (a) {\n" 24956 " b;\n" 24957 "} else if (c) {\n" 24958 " d;\n" 24959 "} else {\n" 24960 " e;\n" 24961 "} // comment", 24962 Style); 24963 24964 verifyFormat("int abs = [](int i) {\n" 24965 " if (i >= 0)\n" 24966 " return i;\n" 24967 " return -i;\n" 24968 "};", 24969 "int abs = [](int i) {\n" 24970 " if (i >= 0) {\n" 24971 " return i;\n" 24972 " }\n" 24973 " return -i;\n" 24974 "};", 24975 Style); 24976 24977 // FIXME: See https://github.com/llvm/llvm-project/issues/53543. 24978 #if 0 24979 Style.ColumnLimit = 65; 24980 24981 verifyFormat("if (condition) {\n" 24982 " ff(Indices,\n" 24983 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 24984 "} else {\n" 24985 " ff(Indices,\n" 24986 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 24987 "}", 24988 Style); 24989 24990 Style.ColumnLimit = 20; 24991 24992 verifyFormat("if (a) {\n" 24993 " b = c + // 1 -\n" 24994 " d;\n" 24995 "}", 24996 Style); 24997 24998 verifyFormat("if (a) {\n" 24999 " b = c >= 0 ? d\n" 25000 " : e;\n" 25001 "}", 25002 "if (a) {\n" 25003 " b = c >= 0 ? d : e;\n" 25004 "}", 25005 Style); 25006 #endif 25007 25008 Style.ColumnLimit = 20; 25009 25010 verifyFormat("if (a)\n" 25011 " b = c > 0 ? d : e;", 25012 "if (a) {\n" 25013 " b = c > 0 ? d : e;\n" 25014 "}", 25015 Style); 25016 25017 Style.ColumnLimit = 0; 25018 25019 verifyFormat("if (a)\n" 25020 " b234567890223456789032345678904234567890 = " 25021 "c234567890223456789032345678904234567890;", 25022 "if (a) {\n" 25023 " b234567890223456789032345678904234567890 = " 25024 "c234567890223456789032345678904234567890;\n" 25025 "}", 25026 Style); 25027 } 25028 25029 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { 25030 auto Style = getLLVMStyle(); 25031 25032 StringRef Short = "functionCall(paramA, paramB, paramC);\n" 25033 "void functionDecl(int a, int b, int c);"; 25034 25035 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, " 25036 "paramF, paramG, paramH, paramI);\n" 25037 "void functionDecl(int argumentA, int argumentB, int " 25038 "argumentC, int argumentD, int argumentE);"; 25039 25040 verifyFormat(Short, Style); 25041 25042 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, " 25043 "paramF, paramG, paramH,\n" 25044 " paramI);\n" 25045 "void functionDecl(int argumentA, int argumentB, int " 25046 "argumentC, int argumentD,\n" 25047 " int argumentE);"; 25048 25049 verifyFormat(NoBreak, Medium, Style); 25050 verifyFormat(NoBreak, 25051 "functionCall(\n" 25052 " paramA,\n" 25053 " paramB,\n" 25054 " paramC,\n" 25055 " paramD,\n" 25056 " paramE,\n" 25057 " paramF,\n" 25058 " paramG,\n" 25059 " paramH,\n" 25060 " paramI\n" 25061 ");\n" 25062 "void functionDecl(\n" 25063 " int argumentA,\n" 25064 " int argumentB,\n" 25065 " int argumentC,\n" 25066 " int argumentD,\n" 25067 " int argumentE\n" 25068 ");", 25069 Style); 25070 25071 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n" 25072 " nestedLongFunctionCall(argument1, " 25073 "argument2, argument3,\n" 25074 " argument4, " 25075 "argument5));", 25076 Style); 25077 25078 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 25079 25080 verifyFormat(Short, Style); 25081 verifyFormat( 25082 "functionCall(\n" 25083 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 25084 "paramI\n" 25085 ");\n" 25086 "void functionDecl(\n" 25087 " int argumentA, int argumentB, int argumentC, int argumentD, int " 25088 "argumentE\n" 25089 ");", 25090 Medium, Style); 25091 25092 Style.AllowAllArgumentsOnNextLine = false; 25093 Style.AllowAllParametersOfDeclarationOnNextLine = false; 25094 25095 verifyFormat(Short, Style); 25096 verifyFormat( 25097 "functionCall(\n" 25098 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 25099 "paramI\n" 25100 ");\n" 25101 "void functionDecl(\n" 25102 " int argumentA, int argumentB, int argumentC, int argumentD, int " 25103 "argumentE\n" 25104 ");", 25105 Medium, Style); 25106 25107 Style.BinPackArguments = false; 25108 Style.BinPackParameters = false; 25109 25110 verifyFormat(Short, Style); 25111 25112 verifyFormat("functionCall(\n" 25113 " paramA,\n" 25114 " paramB,\n" 25115 " paramC,\n" 25116 " paramD,\n" 25117 " paramE,\n" 25118 " paramF,\n" 25119 " paramG,\n" 25120 " paramH,\n" 25121 " paramI\n" 25122 ");\n" 25123 "void functionDecl(\n" 25124 " int argumentA,\n" 25125 " int argumentB,\n" 25126 " int argumentC,\n" 25127 " int argumentD,\n" 25128 " int argumentE\n" 25129 ");", 25130 Medium, Style); 25131 25132 verifyFormat("outerFunctionCall(\n" 25133 " nestedFunctionCall(argument1),\n" 25134 " nestedLongFunctionCall(\n" 25135 " argument1,\n" 25136 " argument2,\n" 25137 " argument3,\n" 25138 " argument4,\n" 25139 " argument5\n" 25140 " )\n" 25141 ");", 25142 Style); 25143 25144 verifyFormat("int a = (int)b;", Style); 25145 verifyFormat("int a = (int)b;", 25146 "int a = (\n" 25147 " int\n" 25148 ") b;", 25149 Style); 25150 25151 verifyFormat("return (true);", Style); 25152 verifyFormat("return (true);", 25153 "return (\n" 25154 " true\n" 25155 ");", 25156 Style); 25157 25158 verifyFormat("void foo();", Style); 25159 verifyFormat("void foo();", 25160 "void foo(\n" 25161 ");", 25162 Style); 25163 25164 verifyFormat("void foo() {}", Style); 25165 verifyFormat("void foo() {}", 25166 "void foo(\n" 25167 ") {\n" 25168 "}", 25169 Style); 25170 25171 verifyFormat("auto string = std::string();", Style); 25172 verifyFormat("auto string = std::string();", 25173 "auto string = std::string(\n" 25174 ");", 25175 Style); 25176 25177 verifyFormat("void (*functionPointer)() = nullptr;", Style); 25178 verifyFormat("void (*functionPointer)() = nullptr;", 25179 "void (\n" 25180 " *functionPointer\n" 25181 ")\n" 25182 "(\n" 25183 ") = nullptr;", 25184 Style); 25185 } 25186 25187 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) { 25188 auto Style = getLLVMStyle(); 25189 25190 verifyFormat("if (foo()) {\n" 25191 " return;\n" 25192 "}", 25193 Style); 25194 25195 verifyFormat("if (quitelongarg !=\n" 25196 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 25197 "comment\n" 25198 " return;\n" 25199 "}", 25200 Style); 25201 25202 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 25203 25204 verifyFormat("if (foo()) {\n" 25205 " return;\n" 25206 "}", 25207 Style); 25208 25209 verifyFormat("if (quitelongarg !=\n" 25210 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 25211 "comment\n" 25212 " return;\n" 25213 "}", 25214 Style); 25215 } 25216 25217 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) { 25218 auto Style = getLLVMStyle(); 25219 25220 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 25221 " doSomething();\n" 25222 "}", 25223 Style); 25224 25225 verifyFormat("for (int myReallyLongCountVariable = 0; " 25226 "myReallyLongCountVariable < count;\n" 25227 " myReallyLongCountVariable++) {\n" 25228 " doSomething();\n" 25229 "}", 25230 Style); 25231 25232 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 25233 25234 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 25235 " doSomething();\n" 25236 "}", 25237 Style); 25238 25239 verifyFormat("for (int myReallyLongCountVariable = 0; " 25240 "myReallyLongCountVariable < count;\n" 25241 " myReallyLongCountVariable++) {\n" 25242 " doSomething();\n" 25243 "}", 25244 Style); 25245 } 25246 25247 TEST_F(FormatTest, UnderstandsDigraphs) { 25248 verifyFormat("int arr<:5:> = {};"); 25249 verifyFormat("int arr[5] = <%%>;"); 25250 verifyFormat("int arr<:::qualified_variable:> = {};"); 25251 verifyFormat("int arr[::qualified_variable] = <%%>;"); 25252 verifyFormat("%:include <header>"); 25253 verifyFormat("%:define A x##y"); 25254 verifyFormat("#define A x%:%:y"); 25255 } 25256 25257 } // namespace 25258 } // namespace format 25259 } // namespace clang 25260