1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "gtest/gtest.h" 17 18 #define DEBUG_TYPE "format-test" 19 20 using clang::tooling::ReplacementTest; 21 using clang::tooling::toReplacements; 22 using testing::ScopedTrace; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 33 34 std::string format(llvm::StringRef Code, 35 const FormatStyle &Style = getLLVMStyle(), 36 StatusCheck CheckComplete = SC_ExpectComplete) { 37 LLVM_DEBUG(llvm::errs() << "---\n"); 38 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 39 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 40 FormattingAttemptStatus Status; 41 tooling::Replacements Replaces = 42 reformat(Style, Code, Ranges, "<stdin>", &Status); 43 if (CheckComplete != SC_DoNotCheck) { 44 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 45 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 46 << Code << "\n\n"; 47 } 48 ReplacementCount = Replaces.size(); 49 auto Result = applyAllReplacements(Code, Replaces); 50 EXPECT_TRUE(static_cast<bool>(Result)); 51 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 52 return *Result; 53 } 54 55 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 56 Style.ColumnLimit = ColumnLimit; 57 return Style; 58 } 59 60 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 61 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 62 } 63 64 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 66 } 67 68 void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, 69 llvm::StringRef Code, 70 const FormatStyle &Style = getLLVMStyle()) { 71 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 72 EXPECT_EQ(Expected.str(), format(Expected, Style)) 73 << "Expected code is not stable"; 74 EXPECT_EQ(Expected.str(), format(Code, Style)); 75 if (Style.Language == FormatStyle::LK_Cpp) { 76 // Objective-C++ is a superset of C++, so everything checked for C++ 77 // needs to be checked for Objective-C++ as well. 78 FormatStyle ObjCStyle = Style; 79 ObjCStyle.Language = FormatStyle::LK_ObjC; 80 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 81 } 82 } 83 84 void _verifyFormat(const char *File, int Line, llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 _verifyFormat(File, Line, Code, test::messUp(Code), Style); 87 } 88 89 void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void _verifyIndependentOfContext(const char *File, int Line, 97 llvm::StringRef Text, 98 const FormatStyle &Style = getLLVMStyle()) { 99 _verifyFormat(File, Line, Text, Style); 100 _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(), 101 Style); 102 } 103 104 /// \brief Verify that clang-format does not crash on the given input. 105 void verifyNoCrash(llvm::StringRef Code, 106 const FormatStyle &Style = getLLVMStyle()) { 107 format(Code, Style, SC_DoNotCheck); 108 } 109 110 int ReplacementCount; 111 }; 112 113 #define verifyIndependentOfContext(...) \ 114 _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__) 115 #define verifyIncompleteFormat(...) \ 116 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__) 117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) 118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle()) 119 120 TEST_F(FormatTest, MessUp) { 121 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 122 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 123 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 124 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 125 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 126 } 127 128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 129 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 130 } 131 132 TEST_F(FormatTest, LLVMStyleOverride) { 133 EXPECT_EQ(FormatStyle::LK_Proto, 134 getLLVMStyle(FormatStyle::LK_Proto).Language); 135 } 136 137 //===----------------------------------------------------------------------===// 138 // Basic function tests. 139 //===----------------------------------------------------------------------===// 140 141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 142 EXPECT_EQ(";", format(";")); 143 } 144 145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 146 EXPECT_EQ("int i;", format(" int i;")); 147 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 148 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 149 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 150 } 151 152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 153 EXPECT_EQ("int i;", format("int\ni;")); 154 } 155 156 TEST_F(FormatTest, FormatsNestedBlockStatements) { 157 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 158 } 159 160 TEST_F(FormatTest, FormatsNestedCall) { 161 verifyFormat("Method(f1, f2(f3));"); 162 verifyFormat("Method(f1(f2, f3()));"); 163 verifyFormat("Method(f1(f2, (f3())));"); 164 } 165 166 TEST_F(FormatTest, NestedNameSpecifiers) { 167 verifyFormat("vector<::Type> v;"); 168 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 169 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 170 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 171 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 172 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 173 verifyFormat("bool a = 2 < ::SomeFunction();"); 174 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 175 verifyFormat("some::string getName();"); 176 } 177 178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 179 EXPECT_EQ("if (a) {\n" 180 " f();\n" 181 "}", 182 format("if(a){f();}")); 183 EXPECT_EQ(4, ReplacementCount); 184 EXPECT_EQ("if (a) {\n" 185 " f();\n" 186 "}", 187 format("if (a) {\n" 188 " f();\n" 189 "}")); 190 EXPECT_EQ(0, ReplacementCount); 191 EXPECT_EQ("/*\r\n" 192 "\r\n" 193 "*/\r\n", 194 format("/*\r\n" 195 "\r\n" 196 "*/\r\n")); 197 EXPECT_EQ(0, ReplacementCount); 198 } 199 200 TEST_F(FormatTest, RemovesEmptyLines) { 201 EXPECT_EQ("class C {\n" 202 " int i;\n" 203 "};", 204 format("class C {\n" 205 " int i;\n" 206 "\n" 207 "};")); 208 209 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 210 EXPECT_EQ("namespace N {\n" 211 "\n" 212 "int i;\n" 213 "}", 214 format("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 getGoogleStyle())); 219 EXPECT_EQ("/* something */ namespace N {\n" 220 "\n" 221 "int i;\n" 222 "}", 223 format("/* something */ namespace N {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 getGoogleStyle())); 228 EXPECT_EQ("inline namespace N {\n" 229 "\n" 230 "int i;\n" 231 "}", 232 format("inline namespace N {\n" 233 "\n" 234 "int i;\n" 235 "}", 236 getGoogleStyle())); 237 EXPECT_EQ("/* something */ inline namespace N {\n" 238 "\n" 239 "int i;\n" 240 "}", 241 format("/* something */ inline namespace N {\n" 242 "\n" 243 "int i;\n" 244 "}", 245 getGoogleStyle())); 246 EXPECT_EQ("export namespace N {\n" 247 "\n" 248 "int i;\n" 249 "}", 250 format("export namespace N {\n" 251 "\n" 252 "int i;\n" 253 "}", 254 getGoogleStyle())); 255 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 256 "\n" 257 "int i;\n" 258 "}", 259 format("extern /**/ \"C\" /**/ {\n" 260 "\n" 261 "int i;\n" 262 "}", 263 getGoogleStyle())); 264 265 auto CustomStyle = getLLVMStyle(); 266 CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom; 267 CustomStyle.BraceWrapping.AfterNamespace = true; 268 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 269 EXPECT_EQ("namespace N\n" 270 "{\n" 271 "\n" 272 "int i;\n" 273 "}", 274 format("namespace N\n" 275 "{\n" 276 "\n" 277 "\n" 278 "int i;\n" 279 "}", 280 CustomStyle)); 281 EXPECT_EQ("/* something */ namespace N\n" 282 "{\n" 283 "\n" 284 "int i;\n" 285 "}", 286 format("/* something */ namespace N {\n" 287 "\n" 288 "\n" 289 "int i;\n" 290 "}", 291 CustomStyle)); 292 EXPECT_EQ("inline namespace N\n" 293 "{\n" 294 "\n" 295 "int i;\n" 296 "}", 297 format("inline namespace N\n" 298 "{\n" 299 "\n" 300 "\n" 301 "int i;\n" 302 "}", 303 CustomStyle)); 304 EXPECT_EQ("/* something */ inline namespace N\n" 305 "{\n" 306 "\n" 307 "int i;\n" 308 "}", 309 format("/* something */ inline namespace N\n" 310 "{\n" 311 "\n" 312 "int i;\n" 313 "}", 314 CustomStyle)); 315 EXPECT_EQ("export namespace N\n" 316 "{\n" 317 "\n" 318 "int i;\n" 319 "}", 320 format("export namespace N\n" 321 "{\n" 322 "\n" 323 "int i;\n" 324 "}", 325 CustomStyle)); 326 EXPECT_EQ("namespace a\n" 327 "{\n" 328 "namespace b\n" 329 "{\n" 330 "\n" 331 "class AA {};\n" 332 "\n" 333 "} // namespace b\n" 334 "} // namespace a\n", 335 format("namespace a\n" 336 "{\n" 337 "namespace b\n" 338 "{\n" 339 "\n" 340 "\n" 341 "class AA {};\n" 342 "\n" 343 "\n" 344 "}\n" 345 "}\n", 346 CustomStyle)); 347 EXPECT_EQ("namespace A /* comment */\n" 348 "{\n" 349 "class B {}\n" 350 "} // namespace A", 351 format("namespace A /* comment */ { class B {} }", CustomStyle)); 352 EXPECT_EQ("namespace A\n" 353 "{ /* comment */\n" 354 "class B {}\n" 355 "} // namespace A", 356 format("namespace A {/* comment */ class B {} }", CustomStyle)); 357 EXPECT_EQ("namespace A\n" 358 "{ /* comment */\n" 359 "\n" 360 "class B {}\n" 361 "\n" 362 "" 363 "} // namespace A", 364 format("namespace A { /* comment */\n" 365 "\n" 366 "\n" 367 "class B {}\n" 368 "\n" 369 "\n" 370 "}", 371 CustomStyle)); 372 EXPECT_EQ("namespace A /* comment */\n" 373 "{\n" 374 "\n" 375 "class B {}\n" 376 "\n" 377 "} // namespace A", 378 format("namespace A/* comment */ {\n" 379 "\n" 380 "\n" 381 "class B {}\n" 382 "\n" 383 "\n" 384 "}", 385 CustomStyle)); 386 387 // ...but do keep inlining and removing empty lines for non-block extern "C" 388 // functions. 389 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 390 EXPECT_EQ("extern \"C\" int f() {\n" 391 " int i = 42;\n" 392 " return i;\n" 393 "}", 394 format("extern \"C\" int f() {\n" 395 "\n" 396 " int i = 42;\n" 397 " return i;\n" 398 "}", 399 getGoogleStyle())); 400 401 // Remove empty lines at the beginning and end of blocks. 402 EXPECT_EQ("void f() {\n" 403 "\n" 404 " if (a) {\n" 405 "\n" 406 " f();\n" 407 " }\n" 408 "}", 409 format("void f() {\n" 410 "\n" 411 " if (a) {\n" 412 "\n" 413 " f();\n" 414 "\n" 415 " }\n" 416 "\n" 417 "}", 418 getLLVMStyle())); 419 EXPECT_EQ("void f() {\n" 420 " if (a) {\n" 421 " f();\n" 422 " }\n" 423 "}", 424 format("void f() {\n" 425 "\n" 426 " if (a) {\n" 427 "\n" 428 " f();\n" 429 "\n" 430 " }\n" 431 "\n" 432 "}", 433 getGoogleStyle())); 434 435 // Don't remove empty lines in more complex control statements. 436 EXPECT_EQ("void f() {\n" 437 " if (a) {\n" 438 " f();\n" 439 "\n" 440 " } else if (b) {\n" 441 " f();\n" 442 " }\n" 443 "}", 444 format("void f() {\n" 445 " if (a) {\n" 446 " f();\n" 447 "\n" 448 " } else if (b) {\n" 449 " f();\n" 450 "\n" 451 " }\n" 452 "\n" 453 "}")); 454 455 // Don't remove empty lines before namespace endings. 456 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 457 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 458 EXPECT_EQ("namespace {\n" 459 "int i;\n" 460 "\n" 461 "}", 462 format("namespace {\n" 463 "int i;\n" 464 "\n" 465 "}", 466 LLVMWithNoNamespaceFix)); 467 EXPECT_EQ("namespace {\n" 468 "int i;\n" 469 "}", 470 format("namespace {\n" 471 "int i;\n" 472 "}", 473 LLVMWithNoNamespaceFix)); 474 EXPECT_EQ("namespace {\n" 475 "int i;\n" 476 "\n" 477 "};", 478 format("namespace {\n" 479 "int i;\n" 480 "\n" 481 "};", 482 LLVMWithNoNamespaceFix)); 483 EXPECT_EQ("namespace {\n" 484 "int i;\n" 485 "};", 486 format("namespace {\n" 487 "int i;\n" 488 "};", 489 LLVMWithNoNamespaceFix)); 490 EXPECT_EQ("namespace {\n" 491 "int i;\n" 492 "\n" 493 "}", 494 format("namespace {\n" 495 "int i;\n" 496 "\n" 497 "}")); 498 EXPECT_EQ("namespace {\n" 499 "int i;\n" 500 "\n" 501 "} // namespace", 502 format("namespace {\n" 503 "int i;\n" 504 "\n" 505 "} // namespace")); 506 507 FormatStyle Style = getLLVMStyle(); 508 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 509 Style.MaxEmptyLinesToKeep = 2; 510 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 511 Style.BraceWrapping.AfterClass = true; 512 Style.BraceWrapping.AfterFunction = true; 513 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 514 515 EXPECT_EQ("class Foo\n" 516 "{\n" 517 " Foo() {}\n" 518 "\n" 519 " void funk() {}\n" 520 "};", 521 format("class Foo\n" 522 "{\n" 523 " Foo()\n" 524 " {\n" 525 " }\n" 526 "\n" 527 " void funk() {}\n" 528 "};", 529 Style)); 530 } 531 532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 533 verifyFormat("x = (a) and (b);"); 534 verifyFormat("x = (a) or (b);"); 535 verifyFormat("x = (a) bitand (b);"); 536 verifyFormat("x = (a) bitor (b);"); 537 verifyFormat("x = (a) not_eq (b);"); 538 verifyFormat("x = (a) and_eq (b);"); 539 verifyFormat("x = (a) or_eq (b);"); 540 verifyFormat("x = (a) xor (b);"); 541 } 542 543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 544 verifyFormat("x = compl(a);"); 545 verifyFormat("x = not(a);"); 546 verifyFormat("x = bitand(a);"); 547 // Unary operator must not be merged with the next identifier 548 verifyFormat("x = compl a;"); 549 verifyFormat("x = not a;"); 550 verifyFormat("x = bitand a;"); 551 } 552 553 //===----------------------------------------------------------------------===// 554 // Tests for control statements. 555 //===----------------------------------------------------------------------===// 556 557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 558 verifyFormat("if (true)\n f();\ng();"); 559 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 560 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 561 verifyFormat("if constexpr (true)\n" 562 " f();\ng();"); 563 verifyFormat("if CONSTEXPR (true)\n" 564 " f();\ng();"); 565 verifyFormat("if constexpr (a)\n" 566 " if constexpr (b)\n" 567 " if constexpr (c)\n" 568 " g();\n" 569 "h();"); 570 verifyFormat("if CONSTEXPR (a)\n" 571 " if CONSTEXPR (b)\n" 572 " if CONSTEXPR (c)\n" 573 " g();\n" 574 "h();"); 575 verifyFormat("if constexpr (a)\n" 576 " if constexpr (b) {\n" 577 " f();\n" 578 " }\n" 579 "g();"); 580 verifyFormat("if CONSTEXPR (a)\n" 581 " if CONSTEXPR (b) {\n" 582 " f();\n" 583 " }\n" 584 "g();"); 585 586 verifyFormat("if (a)\n" 587 " g();"); 588 verifyFormat("if (a) {\n" 589 " g()\n" 590 "};"); 591 verifyFormat("if (a)\n" 592 " g();\n" 593 "else\n" 594 " g();"); 595 verifyFormat("if (a) {\n" 596 " g();\n" 597 "} else\n" 598 " g();"); 599 verifyFormat("if (a)\n" 600 " g();\n" 601 "else {\n" 602 " g();\n" 603 "}"); 604 verifyFormat("if (a) {\n" 605 " g();\n" 606 "} else {\n" 607 " g();\n" 608 "}"); 609 verifyFormat("if (a)\n" 610 " g();\n" 611 "else if (b)\n" 612 " g();\n" 613 "else\n" 614 " g();"); 615 verifyFormat("if (a) {\n" 616 " g();\n" 617 "} else if (b)\n" 618 " g();\n" 619 "else\n" 620 " g();"); 621 verifyFormat("if (a)\n" 622 " g();\n" 623 "else if (b) {\n" 624 " g();\n" 625 "} else\n" 626 " g();"); 627 verifyFormat("if (a)\n" 628 " g();\n" 629 "else if (b)\n" 630 " g();\n" 631 "else {\n" 632 " g();\n" 633 "}"); 634 verifyFormat("if (a)\n" 635 " g();\n" 636 "else if (b) {\n" 637 " g();\n" 638 "} else {\n" 639 " g();\n" 640 "}"); 641 verifyFormat("if (a) {\n" 642 " g();\n" 643 "} else if (b) {\n" 644 " g();\n" 645 "} else {\n" 646 " g();\n" 647 "}"); 648 649 FormatStyle AllowsMergedIf = getLLVMStyle(); 650 AllowsMergedIf.IfMacros.push_back("MYIF"); 651 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 652 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 653 FormatStyle::SIS_WithoutElse; 654 verifyFormat("if (a)\n" 655 " // comment\n" 656 " f();", 657 AllowsMergedIf); 658 verifyFormat("{\n" 659 " if (a)\n" 660 " label:\n" 661 " f();\n" 662 "}", 663 AllowsMergedIf); 664 verifyFormat("#define A \\\n" 665 " if (a) \\\n" 666 " label: \\\n" 667 " f()", 668 AllowsMergedIf); 669 verifyFormat("if (a)\n" 670 " ;", 671 AllowsMergedIf); 672 verifyFormat("if (a)\n" 673 " if (b) return;", 674 AllowsMergedIf); 675 676 verifyFormat("if (a) // Can't merge this\n" 677 " f();\n", 678 AllowsMergedIf); 679 verifyFormat("if (a) /* still don't merge */\n" 680 " f();", 681 AllowsMergedIf); 682 verifyFormat("if (a) { // Never merge this\n" 683 " f();\n" 684 "}", 685 AllowsMergedIf); 686 verifyFormat("if (a) { /* Never merge this */\n" 687 " f();\n" 688 "}", 689 AllowsMergedIf); 690 verifyFormat("MYIF (a)\n" 691 " // comment\n" 692 " f();", 693 AllowsMergedIf); 694 verifyFormat("{\n" 695 " MYIF (a)\n" 696 " label:\n" 697 " f();\n" 698 "}", 699 AllowsMergedIf); 700 verifyFormat("#define A \\\n" 701 " MYIF (a) \\\n" 702 " label: \\\n" 703 " f()", 704 AllowsMergedIf); 705 verifyFormat("MYIF (a)\n" 706 " ;", 707 AllowsMergedIf); 708 verifyFormat("MYIF (a)\n" 709 " MYIF (b) return;", 710 AllowsMergedIf); 711 712 verifyFormat("MYIF (a) // Can't merge this\n" 713 " f();\n", 714 AllowsMergedIf); 715 verifyFormat("MYIF (a) /* still don't merge */\n" 716 " f();", 717 AllowsMergedIf); 718 verifyFormat("MYIF (a) { // Never merge this\n" 719 " f();\n" 720 "}", 721 AllowsMergedIf); 722 verifyFormat("MYIF (a) { /* Never merge this */\n" 723 " f();\n" 724 "}", 725 AllowsMergedIf); 726 727 AllowsMergedIf.ColumnLimit = 14; 728 // Where line-lengths matter, a 2-letter synonym that maintains line length. 729 // Not IF to avoid any confusion that IF is somehow special. 730 AllowsMergedIf.IfMacros.push_back("FI"); 731 verifyFormat("if (a) return;", AllowsMergedIf); 732 verifyFormat("if (aaaaaaaaa)\n" 733 " return;", 734 AllowsMergedIf); 735 verifyFormat("FI (a) return;", AllowsMergedIf); 736 verifyFormat("FI (aaaaaaaaa)\n" 737 " return;", 738 AllowsMergedIf); 739 740 AllowsMergedIf.ColumnLimit = 13; 741 verifyFormat("if (a)\n return;", AllowsMergedIf); 742 verifyFormat("FI (a)\n return;", AllowsMergedIf); 743 744 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 745 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 746 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 747 FormatStyle::SIS_AllIfsAndElse; 748 verifyFormat("if (a)\n" 749 " // comment\n" 750 " f();\n" 751 "else\n" 752 " // comment\n" 753 " f();", 754 AllowsMergedIfElse); 755 verifyFormat("{\n" 756 " if (a)\n" 757 " label:\n" 758 " f();\n" 759 " else\n" 760 " label:\n" 761 " f();\n" 762 "}", 763 AllowsMergedIfElse); 764 verifyFormat("if (a)\n" 765 " ;\n" 766 "else\n" 767 " ;", 768 AllowsMergedIfElse); 769 verifyFormat("if (a) {\n" 770 "} else {\n" 771 "}", 772 AllowsMergedIfElse); 773 verifyFormat("if (a) return;\n" 774 "else if (b) return;\n" 775 "else return;", 776 AllowsMergedIfElse); 777 verifyFormat("if (a) {\n" 778 "} else return;", 779 AllowsMergedIfElse); 780 verifyFormat("if (a) {\n" 781 "} else if (b) return;\n" 782 "else return;", 783 AllowsMergedIfElse); 784 verifyFormat("if (a) return;\n" 785 "else if (b) {\n" 786 "} else return;", 787 AllowsMergedIfElse); 788 verifyFormat("if (a)\n" 789 " if (b) return;\n" 790 " else return;", 791 AllowsMergedIfElse); 792 verifyFormat("if constexpr (a)\n" 793 " if constexpr (b) return;\n" 794 " else if constexpr (c) return;\n" 795 " else return;", 796 AllowsMergedIfElse); 797 verifyFormat("MYIF (a)\n" 798 " // comment\n" 799 " f();\n" 800 "else\n" 801 " // comment\n" 802 " f();", 803 AllowsMergedIfElse); 804 verifyFormat("{\n" 805 " MYIF (a)\n" 806 " label:\n" 807 " f();\n" 808 " else\n" 809 " label:\n" 810 " f();\n" 811 "}", 812 AllowsMergedIfElse); 813 verifyFormat("MYIF (a)\n" 814 " ;\n" 815 "else\n" 816 " ;", 817 AllowsMergedIfElse); 818 verifyFormat("MYIF (a) {\n" 819 "} else {\n" 820 "}", 821 AllowsMergedIfElse); 822 verifyFormat("MYIF (a) return;\n" 823 "else MYIF (b) return;\n" 824 "else return;", 825 AllowsMergedIfElse); 826 verifyFormat("MYIF (a) {\n" 827 "} else return;", 828 AllowsMergedIfElse); 829 verifyFormat("MYIF (a) {\n" 830 "} else MYIF (b) return;\n" 831 "else return;", 832 AllowsMergedIfElse); 833 verifyFormat("MYIF (a) return;\n" 834 "else MYIF (b) {\n" 835 "} else return;", 836 AllowsMergedIfElse); 837 verifyFormat("MYIF (a)\n" 838 " MYIF (b) return;\n" 839 " else return;", 840 AllowsMergedIfElse); 841 verifyFormat("MYIF constexpr (a)\n" 842 " MYIF constexpr (b) return;\n" 843 " else MYIF constexpr (c) return;\n" 844 " else return;", 845 AllowsMergedIfElse); 846 } 847 848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 849 FormatStyle AllowsMergedIf = getLLVMStyle(); 850 AllowsMergedIf.IfMacros.push_back("MYIF"); 851 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 852 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 853 FormatStyle::SIS_WithoutElse; 854 verifyFormat("if (a)\n" 855 " f();\n" 856 "else {\n" 857 " g();\n" 858 "}", 859 AllowsMergedIf); 860 verifyFormat("if (a)\n" 861 " f();\n" 862 "else\n" 863 " g();\n", 864 AllowsMergedIf); 865 866 verifyFormat("if (a) g();", AllowsMergedIf); 867 verifyFormat("if (a) {\n" 868 " g()\n" 869 "};", 870 AllowsMergedIf); 871 verifyFormat("if (a)\n" 872 " g();\n" 873 "else\n" 874 " g();", 875 AllowsMergedIf); 876 verifyFormat("if (a) {\n" 877 " g();\n" 878 "} else\n" 879 " g();", 880 AllowsMergedIf); 881 verifyFormat("if (a)\n" 882 " g();\n" 883 "else {\n" 884 " g();\n" 885 "}", 886 AllowsMergedIf); 887 verifyFormat("if (a) {\n" 888 " g();\n" 889 "} else {\n" 890 " g();\n" 891 "}", 892 AllowsMergedIf); 893 verifyFormat("if (a)\n" 894 " g();\n" 895 "else if (b)\n" 896 " g();\n" 897 "else\n" 898 " g();", 899 AllowsMergedIf); 900 verifyFormat("if (a) {\n" 901 " g();\n" 902 "} else if (b)\n" 903 " g();\n" 904 "else\n" 905 " g();", 906 AllowsMergedIf); 907 verifyFormat("if (a)\n" 908 " g();\n" 909 "else if (b) {\n" 910 " g();\n" 911 "} else\n" 912 " g();", 913 AllowsMergedIf); 914 verifyFormat("if (a)\n" 915 " g();\n" 916 "else if (b)\n" 917 " g();\n" 918 "else {\n" 919 " g();\n" 920 "}", 921 AllowsMergedIf); 922 verifyFormat("if (a)\n" 923 " g();\n" 924 "else if (b) {\n" 925 " g();\n" 926 "} else {\n" 927 " g();\n" 928 "}", 929 AllowsMergedIf); 930 verifyFormat("if (a) {\n" 931 " g();\n" 932 "} else if (b) {\n" 933 " g();\n" 934 "} else {\n" 935 " g();\n" 936 "}", 937 AllowsMergedIf); 938 verifyFormat("MYIF (a)\n" 939 " f();\n" 940 "else {\n" 941 " g();\n" 942 "}", 943 AllowsMergedIf); 944 verifyFormat("MYIF (a)\n" 945 " f();\n" 946 "else\n" 947 " g();\n", 948 AllowsMergedIf); 949 950 verifyFormat("MYIF (a) g();", AllowsMergedIf); 951 verifyFormat("MYIF (a) {\n" 952 " g()\n" 953 "};", 954 AllowsMergedIf); 955 verifyFormat("MYIF (a)\n" 956 " g();\n" 957 "else\n" 958 " g();", 959 AllowsMergedIf); 960 verifyFormat("MYIF (a) {\n" 961 " g();\n" 962 "} else\n" 963 " g();", 964 AllowsMergedIf); 965 verifyFormat("MYIF (a)\n" 966 " g();\n" 967 "else {\n" 968 " g();\n" 969 "}", 970 AllowsMergedIf); 971 verifyFormat("MYIF (a) {\n" 972 " g();\n" 973 "} else {\n" 974 " g();\n" 975 "}", 976 AllowsMergedIf); 977 verifyFormat("MYIF (a)\n" 978 " g();\n" 979 "else MYIF (b)\n" 980 " g();\n" 981 "else\n" 982 " g();", 983 AllowsMergedIf); 984 verifyFormat("MYIF (a)\n" 985 " g();\n" 986 "else if (b)\n" 987 " g();\n" 988 "else\n" 989 " g();", 990 AllowsMergedIf); 991 verifyFormat("MYIF (a) {\n" 992 " g();\n" 993 "} else MYIF (b)\n" 994 " g();\n" 995 "else\n" 996 " g();", 997 AllowsMergedIf); 998 verifyFormat("MYIF (a) {\n" 999 " g();\n" 1000 "} else if (b)\n" 1001 " g();\n" 1002 "else\n" 1003 " g();", 1004 AllowsMergedIf); 1005 verifyFormat("MYIF (a)\n" 1006 " g();\n" 1007 "else MYIF (b) {\n" 1008 " g();\n" 1009 "} else\n" 1010 " g();", 1011 AllowsMergedIf); 1012 verifyFormat("MYIF (a)\n" 1013 " g();\n" 1014 "else if (b) {\n" 1015 " g();\n" 1016 "} else\n" 1017 " g();", 1018 AllowsMergedIf); 1019 verifyFormat("MYIF (a)\n" 1020 " g();\n" 1021 "else MYIF (b)\n" 1022 " g();\n" 1023 "else {\n" 1024 " g();\n" 1025 "}", 1026 AllowsMergedIf); 1027 verifyFormat("MYIF (a)\n" 1028 " g();\n" 1029 "else if (b)\n" 1030 " g();\n" 1031 "else {\n" 1032 " g();\n" 1033 "}", 1034 AllowsMergedIf); 1035 verifyFormat("MYIF (a)\n" 1036 " g();\n" 1037 "else MYIF (b) {\n" 1038 " g();\n" 1039 "} else {\n" 1040 " g();\n" 1041 "}", 1042 AllowsMergedIf); 1043 verifyFormat("MYIF (a)\n" 1044 " g();\n" 1045 "else if (b) {\n" 1046 " g();\n" 1047 "} else {\n" 1048 " g();\n" 1049 "}", 1050 AllowsMergedIf); 1051 verifyFormat("MYIF (a) {\n" 1052 " g();\n" 1053 "} else MYIF (b) {\n" 1054 " g();\n" 1055 "} else {\n" 1056 " g();\n" 1057 "}", 1058 AllowsMergedIf); 1059 verifyFormat("MYIF (a) {\n" 1060 " g();\n" 1061 "} else if (b) {\n" 1062 " g();\n" 1063 "} else {\n" 1064 " g();\n" 1065 "}", 1066 AllowsMergedIf); 1067 1068 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1069 FormatStyle::SIS_OnlyFirstIf; 1070 1071 verifyFormat("if (a) f();\n" 1072 "else {\n" 1073 " g();\n" 1074 "}", 1075 AllowsMergedIf); 1076 verifyFormat("if (a) f();\n" 1077 "else {\n" 1078 " if (a) f();\n" 1079 " else {\n" 1080 " g();\n" 1081 " }\n" 1082 " g();\n" 1083 "}", 1084 AllowsMergedIf); 1085 1086 verifyFormat("if (a) g();", AllowsMergedIf); 1087 verifyFormat("if (a) {\n" 1088 " g()\n" 1089 "};", 1090 AllowsMergedIf); 1091 verifyFormat("if (a) g();\n" 1092 "else\n" 1093 " g();", 1094 AllowsMergedIf); 1095 verifyFormat("if (a) {\n" 1096 " g();\n" 1097 "} else\n" 1098 " g();", 1099 AllowsMergedIf); 1100 verifyFormat("if (a) g();\n" 1101 "else {\n" 1102 " g();\n" 1103 "}", 1104 AllowsMergedIf); 1105 verifyFormat("if (a) {\n" 1106 " g();\n" 1107 "} else {\n" 1108 " g();\n" 1109 "}", 1110 AllowsMergedIf); 1111 verifyFormat("if (a) g();\n" 1112 "else if (b)\n" 1113 " g();\n" 1114 "else\n" 1115 " g();", 1116 AllowsMergedIf); 1117 verifyFormat("if (a) {\n" 1118 " g();\n" 1119 "} else if (b)\n" 1120 " g();\n" 1121 "else\n" 1122 " g();", 1123 AllowsMergedIf); 1124 verifyFormat("if (a) g();\n" 1125 "else if (b) {\n" 1126 " g();\n" 1127 "} else\n" 1128 " g();", 1129 AllowsMergedIf); 1130 verifyFormat("if (a) g();\n" 1131 "else if (b)\n" 1132 " g();\n" 1133 "else {\n" 1134 " g();\n" 1135 "}", 1136 AllowsMergedIf); 1137 verifyFormat("if (a) g();\n" 1138 "else if (b) {\n" 1139 " g();\n" 1140 "} else {\n" 1141 " g();\n" 1142 "}", 1143 AllowsMergedIf); 1144 verifyFormat("if (a) {\n" 1145 " g();\n" 1146 "} else if (b) {\n" 1147 " g();\n" 1148 "} else {\n" 1149 " g();\n" 1150 "}", 1151 AllowsMergedIf); 1152 verifyFormat("MYIF (a) f();\n" 1153 "else {\n" 1154 " g();\n" 1155 "}", 1156 AllowsMergedIf); 1157 verifyFormat("MYIF (a) f();\n" 1158 "else {\n" 1159 " if (a) f();\n" 1160 " else {\n" 1161 " g();\n" 1162 " }\n" 1163 " g();\n" 1164 "}", 1165 AllowsMergedIf); 1166 1167 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1168 verifyFormat("MYIF (a) {\n" 1169 " g()\n" 1170 "};", 1171 AllowsMergedIf); 1172 verifyFormat("MYIF (a) g();\n" 1173 "else\n" 1174 " g();", 1175 AllowsMergedIf); 1176 verifyFormat("MYIF (a) {\n" 1177 " g();\n" 1178 "} else\n" 1179 " g();", 1180 AllowsMergedIf); 1181 verifyFormat("MYIF (a) g();\n" 1182 "else {\n" 1183 " g();\n" 1184 "}", 1185 AllowsMergedIf); 1186 verifyFormat("MYIF (a) {\n" 1187 " g();\n" 1188 "} else {\n" 1189 " g();\n" 1190 "}", 1191 AllowsMergedIf); 1192 verifyFormat("MYIF (a) g();\n" 1193 "else MYIF (b)\n" 1194 " g();\n" 1195 "else\n" 1196 " g();", 1197 AllowsMergedIf); 1198 verifyFormat("MYIF (a) g();\n" 1199 "else if (b)\n" 1200 " g();\n" 1201 "else\n" 1202 " g();", 1203 AllowsMergedIf); 1204 verifyFormat("MYIF (a) {\n" 1205 " g();\n" 1206 "} else MYIF (b)\n" 1207 " g();\n" 1208 "else\n" 1209 " g();", 1210 AllowsMergedIf); 1211 verifyFormat("MYIF (a) {\n" 1212 " g();\n" 1213 "} else if (b)\n" 1214 " g();\n" 1215 "else\n" 1216 " g();", 1217 AllowsMergedIf); 1218 verifyFormat("MYIF (a) g();\n" 1219 "else MYIF (b) {\n" 1220 " g();\n" 1221 "} else\n" 1222 " g();", 1223 AllowsMergedIf); 1224 verifyFormat("MYIF (a) g();\n" 1225 "else if (b) {\n" 1226 " g();\n" 1227 "} else\n" 1228 " g();", 1229 AllowsMergedIf); 1230 verifyFormat("MYIF (a) g();\n" 1231 "else MYIF (b)\n" 1232 " g();\n" 1233 "else {\n" 1234 " g();\n" 1235 "}", 1236 AllowsMergedIf); 1237 verifyFormat("MYIF (a) g();\n" 1238 "else if (b)\n" 1239 " g();\n" 1240 "else {\n" 1241 " g();\n" 1242 "}", 1243 AllowsMergedIf); 1244 verifyFormat("MYIF (a) g();\n" 1245 "else MYIF (b) {\n" 1246 " g();\n" 1247 "} else {\n" 1248 " g();\n" 1249 "}", 1250 AllowsMergedIf); 1251 verifyFormat("MYIF (a) g();\n" 1252 "else if (b) {\n" 1253 " g();\n" 1254 "} else {\n" 1255 " g();\n" 1256 "}", 1257 AllowsMergedIf); 1258 verifyFormat("MYIF (a) {\n" 1259 " g();\n" 1260 "} else MYIF (b) {\n" 1261 " g();\n" 1262 "} else {\n" 1263 " g();\n" 1264 "}", 1265 AllowsMergedIf); 1266 verifyFormat("MYIF (a) {\n" 1267 " g();\n" 1268 "} else if (b) {\n" 1269 " g();\n" 1270 "} else {\n" 1271 " g();\n" 1272 "}", 1273 AllowsMergedIf); 1274 1275 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1276 FormatStyle::SIS_AllIfsAndElse; 1277 1278 verifyFormat("if (a) f();\n" 1279 "else {\n" 1280 " g();\n" 1281 "}", 1282 AllowsMergedIf); 1283 verifyFormat("if (a) f();\n" 1284 "else {\n" 1285 " if (a) f();\n" 1286 " else {\n" 1287 " g();\n" 1288 " }\n" 1289 " g();\n" 1290 "}", 1291 AllowsMergedIf); 1292 1293 verifyFormat("if (a) g();", AllowsMergedIf); 1294 verifyFormat("if (a) {\n" 1295 " g()\n" 1296 "};", 1297 AllowsMergedIf); 1298 verifyFormat("if (a) g();\n" 1299 "else g();", 1300 AllowsMergedIf); 1301 verifyFormat("if (a) {\n" 1302 " g();\n" 1303 "} else g();", 1304 AllowsMergedIf); 1305 verifyFormat("if (a) g();\n" 1306 "else {\n" 1307 " g();\n" 1308 "}", 1309 AllowsMergedIf); 1310 verifyFormat("if (a) {\n" 1311 " g();\n" 1312 "} else {\n" 1313 " g();\n" 1314 "}", 1315 AllowsMergedIf); 1316 verifyFormat("if (a) g();\n" 1317 "else if (b) g();\n" 1318 "else g();", 1319 AllowsMergedIf); 1320 verifyFormat("if (a) {\n" 1321 " g();\n" 1322 "} else if (b) g();\n" 1323 "else g();", 1324 AllowsMergedIf); 1325 verifyFormat("if (a) g();\n" 1326 "else if (b) {\n" 1327 " g();\n" 1328 "} else g();", 1329 AllowsMergedIf); 1330 verifyFormat("if (a) g();\n" 1331 "else if (b) g();\n" 1332 "else {\n" 1333 " g();\n" 1334 "}", 1335 AllowsMergedIf); 1336 verifyFormat("if (a) g();\n" 1337 "else if (b) {\n" 1338 " g();\n" 1339 "} else {\n" 1340 " g();\n" 1341 "}", 1342 AllowsMergedIf); 1343 verifyFormat("if (a) {\n" 1344 " g();\n" 1345 "} else if (b) {\n" 1346 " g();\n" 1347 "} else {\n" 1348 " g();\n" 1349 "}", 1350 AllowsMergedIf); 1351 verifyFormat("MYIF (a) f();\n" 1352 "else {\n" 1353 " g();\n" 1354 "}", 1355 AllowsMergedIf); 1356 verifyFormat("MYIF (a) f();\n" 1357 "else {\n" 1358 " if (a) f();\n" 1359 " else {\n" 1360 " g();\n" 1361 " }\n" 1362 " g();\n" 1363 "}", 1364 AllowsMergedIf); 1365 1366 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1367 verifyFormat("MYIF (a) {\n" 1368 " g()\n" 1369 "};", 1370 AllowsMergedIf); 1371 verifyFormat("MYIF (a) g();\n" 1372 "else g();", 1373 AllowsMergedIf); 1374 verifyFormat("MYIF (a) {\n" 1375 " g();\n" 1376 "} else g();", 1377 AllowsMergedIf); 1378 verifyFormat("MYIF (a) g();\n" 1379 "else {\n" 1380 " g();\n" 1381 "}", 1382 AllowsMergedIf); 1383 verifyFormat("MYIF (a) {\n" 1384 " g();\n" 1385 "} else {\n" 1386 " g();\n" 1387 "}", 1388 AllowsMergedIf); 1389 verifyFormat("MYIF (a) g();\n" 1390 "else MYIF (b) g();\n" 1391 "else g();", 1392 AllowsMergedIf); 1393 verifyFormat("MYIF (a) g();\n" 1394 "else if (b) g();\n" 1395 "else g();", 1396 AllowsMergedIf); 1397 verifyFormat("MYIF (a) {\n" 1398 " g();\n" 1399 "} else MYIF (b) g();\n" 1400 "else g();", 1401 AllowsMergedIf); 1402 verifyFormat("MYIF (a) {\n" 1403 " g();\n" 1404 "} else if (b) g();\n" 1405 "else g();", 1406 AllowsMergedIf); 1407 verifyFormat("MYIF (a) g();\n" 1408 "else MYIF (b) {\n" 1409 " g();\n" 1410 "} else g();", 1411 AllowsMergedIf); 1412 verifyFormat("MYIF (a) g();\n" 1413 "else if (b) {\n" 1414 " g();\n" 1415 "} else g();", 1416 AllowsMergedIf); 1417 verifyFormat("MYIF (a) g();\n" 1418 "else MYIF (b) g();\n" 1419 "else {\n" 1420 " g();\n" 1421 "}", 1422 AllowsMergedIf); 1423 verifyFormat("MYIF (a) g();\n" 1424 "else if (b) g();\n" 1425 "else {\n" 1426 " g();\n" 1427 "}", 1428 AllowsMergedIf); 1429 verifyFormat("MYIF (a) g();\n" 1430 "else MYIF (b) {\n" 1431 " g();\n" 1432 "} else {\n" 1433 " g();\n" 1434 "}", 1435 AllowsMergedIf); 1436 verifyFormat("MYIF (a) g();\n" 1437 "else if (b) {\n" 1438 " g();\n" 1439 "} else {\n" 1440 " g();\n" 1441 "}", 1442 AllowsMergedIf); 1443 verifyFormat("MYIF (a) {\n" 1444 " g();\n" 1445 "} else MYIF (b) {\n" 1446 " g();\n" 1447 "} else {\n" 1448 " g();\n" 1449 "}", 1450 AllowsMergedIf); 1451 verifyFormat("MYIF (a) {\n" 1452 " g();\n" 1453 "} else if (b) {\n" 1454 " g();\n" 1455 "} else {\n" 1456 " g();\n" 1457 "}", 1458 AllowsMergedIf); 1459 } 1460 1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1462 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1463 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1464 verifyFormat("while (true) continue;", AllowsMergedLoops); 1465 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1466 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1467 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops); 1468 verifyFormat("while (true)\n" 1469 " ;", 1470 AllowsMergedLoops); 1471 verifyFormat("for (;;)\n" 1472 " ;", 1473 AllowsMergedLoops); 1474 verifyFormat("for (;;)\n" 1475 " for (;;) continue;", 1476 AllowsMergedLoops); 1477 verifyFormat("for (;;)\n" 1478 " while (true) continue;", 1479 AllowsMergedLoops); 1480 verifyFormat("while (true)\n" 1481 " for (;;) continue;", 1482 AllowsMergedLoops); 1483 verifyFormat("BOOST_FOREACH (int &v, vec)\n" 1484 " for (;;) continue;", 1485 AllowsMergedLoops); 1486 verifyFormat("for (;;)\n" 1487 " BOOST_FOREACH (int &v, vec) continue;", 1488 AllowsMergedLoops); 1489 verifyFormat("for (;;) // Can't merge this\n" 1490 " continue;", 1491 AllowsMergedLoops); 1492 verifyFormat("for (;;) /* still don't merge */\n" 1493 " continue;", 1494 AllowsMergedLoops); 1495 verifyFormat("do a++;\n" 1496 "while (true);", 1497 AllowsMergedLoops); 1498 verifyFormat("do /* Don't merge */\n" 1499 " a++;\n" 1500 "while (true);", 1501 AllowsMergedLoops); 1502 verifyFormat("do // Don't merge\n" 1503 " a++;\n" 1504 "while (true);", 1505 AllowsMergedLoops); 1506 verifyFormat("do\n" 1507 " // Don't merge\n" 1508 " a++;\n" 1509 "while (true);", 1510 AllowsMergedLoops); 1511 // Without braces labels are interpreted differently. 1512 verifyFormat("{\n" 1513 " do\n" 1514 " label:\n" 1515 " a++;\n" 1516 " while (true);\n" 1517 "}", 1518 AllowsMergedLoops); 1519 } 1520 1521 TEST_F(FormatTest, FormatShortBracedStatements) { 1522 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1523 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false); 1524 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine, 1525 FormatStyle::SIS_Never); 1526 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false); 1527 EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false); 1528 verifyFormat("for (;;) {\n" 1529 " f();\n" 1530 "}"); 1531 verifyFormat("/*comment*/ for (;;) {\n" 1532 " f();\n" 1533 "}"); 1534 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1535 " f();\n" 1536 "}"); 1537 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n" 1538 " f();\n" 1539 "}"); 1540 verifyFormat("while (true) {\n" 1541 " f();\n" 1542 "}"); 1543 verifyFormat("/*comment*/ while (true) {\n" 1544 " f();\n" 1545 "}"); 1546 verifyFormat("if (true) {\n" 1547 " f();\n" 1548 "}"); 1549 verifyFormat("/*comment*/ if (true) {\n" 1550 " f();\n" 1551 "}"); 1552 1553 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1554 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1555 // Not IF to avoid any confusion that IF is somehow special. 1556 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1557 AllowSimpleBracedStatements.ColumnLimit = 40; 1558 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1559 FormatStyle::SBS_Always; 1560 1561 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1562 FormatStyle::SIS_WithoutElse; 1563 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1564 1565 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1566 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1567 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1568 1569 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1570 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1571 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1572 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1573 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1574 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1575 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1576 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1577 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1578 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1579 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1580 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1581 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1582 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1583 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1584 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1585 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1586 AllowSimpleBracedStatements); 1587 verifyFormat("if (true) {\n" 1588 " ffffffffffffffffffffffff();\n" 1589 "}", 1590 AllowSimpleBracedStatements); 1591 verifyFormat("if (true) {\n" 1592 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1593 "}", 1594 AllowSimpleBracedStatements); 1595 verifyFormat("if (true) { //\n" 1596 " f();\n" 1597 "}", 1598 AllowSimpleBracedStatements); 1599 verifyFormat("if (true) {\n" 1600 " f();\n" 1601 " f();\n" 1602 "}", 1603 AllowSimpleBracedStatements); 1604 verifyFormat("if (true) {\n" 1605 " f();\n" 1606 "} else {\n" 1607 " f();\n" 1608 "}", 1609 AllowSimpleBracedStatements); 1610 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1611 AllowSimpleBracedStatements); 1612 verifyFormat("MYIF (true) {\n" 1613 " ffffffffffffffffffffffff();\n" 1614 "}", 1615 AllowSimpleBracedStatements); 1616 verifyFormat("MYIF (true) {\n" 1617 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1618 "}", 1619 AllowSimpleBracedStatements); 1620 verifyFormat("MYIF (true) { //\n" 1621 " f();\n" 1622 "}", 1623 AllowSimpleBracedStatements); 1624 verifyFormat("MYIF (true) {\n" 1625 " f();\n" 1626 " f();\n" 1627 "}", 1628 AllowSimpleBracedStatements); 1629 verifyFormat("MYIF (true) {\n" 1630 " f();\n" 1631 "} else {\n" 1632 " f();\n" 1633 "}", 1634 AllowSimpleBracedStatements); 1635 1636 verifyFormat("struct A2 {\n" 1637 " int X;\n" 1638 "};", 1639 AllowSimpleBracedStatements); 1640 verifyFormat("typedef struct A2 {\n" 1641 " int X;\n" 1642 "} A2_t;", 1643 AllowSimpleBracedStatements); 1644 verifyFormat("template <int> struct A2 {\n" 1645 " struct B {};\n" 1646 "};", 1647 AllowSimpleBracedStatements); 1648 1649 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1650 FormatStyle::SIS_Never; 1651 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1652 verifyFormat("if (true) {\n" 1653 " f();\n" 1654 "}", 1655 AllowSimpleBracedStatements); 1656 verifyFormat("if (true) {\n" 1657 " f();\n" 1658 "} else {\n" 1659 " f();\n" 1660 "}", 1661 AllowSimpleBracedStatements); 1662 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1663 verifyFormat("MYIF (true) {\n" 1664 " f();\n" 1665 "}", 1666 AllowSimpleBracedStatements); 1667 verifyFormat("MYIF (true) {\n" 1668 " f();\n" 1669 "} else {\n" 1670 " f();\n" 1671 "}", 1672 AllowSimpleBracedStatements); 1673 1674 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1675 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1676 verifyFormat("while (true) {\n" 1677 " f();\n" 1678 "}", 1679 AllowSimpleBracedStatements); 1680 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1681 verifyFormat("for (;;) {\n" 1682 " f();\n" 1683 "}", 1684 AllowSimpleBracedStatements); 1685 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1686 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1687 " f();\n" 1688 "}", 1689 AllowSimpleBracedStatements); 1690 1691 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1692 FormatStyle::SIS_WithoutElse; 1693 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1694 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1695 FormatStyle::BWACS_Always; 1696 1697 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1698 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1699 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1700 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1701 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1702 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1703 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1704 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1705 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1706 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1707 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1708 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1709 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1710 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1711 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1712 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1713 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1714 AllowSimpleBracedStatements); 1715 verifyFormat("if (true)\n" 1716 "{\n" 1717 " ffffffffffffffffffffffff();\n" 1718 "}", 1719 AllowSimpleBracedStatements); 1720 verifyFormat("if (true)\n" 1721 "{\n" 1722 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1723 "}", 1724 AllowSimpleBracedStatements); 1725 verifyFormat("if (true)\n" 1726 "{ //\n" 1727 " f();\n" 1728 "}", 1729 AllowSimpleBracedStatements); 1730 verifyFormat("if (true)\n" 1731 "{\n" 1732 " f();\n" 1733 " f();\n" 1734 "}", 1735 AllowSimpleBracedStatements); 1736 verifyFormat("if (true)\n" 1737 "{\n" 1738 " f();\n" 1739 "} else\n" 1740 "{\n" 1741 " f();\n" 1742 "}", 1743 AllowSimpleBracedStatements); 1744 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1745 AllowSimpleBracedStatements); 1746 verifyFormat("MYIF (true)\n" 1747 "{\n" 1748 " ffffffffffffffffffffffff();\n" 1749 "}", 1750 AllowSimpleBracedStatements); 1751 verifyFormat("MYIF (true)\n" 1752 "{\n" 1753 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1754 "}", 1755 AllowSimpleBracedStatements); 1756 verifyFormat("MYIF (true)\n" 1757 "{ //\n" 1758 " f();\n" 1759 "}", 1760 AllowSimpleBracedStatements); 1761 verifyFormat("MYIF (true)\n" 1762 "{\n" 1763 " f();\n" 1764 " f();\n" 1765 "}", 1766 AllowSimpleBracedStatements); 1767 verifyFormat("MYIF (true)\n" 1768 "{\n" 1769 " f();\n" 1770 "} else\n" 1771 "{\n" 1772 " f();\n" 1773 "}", 1774 AllowSimpleBracedStatements); 1775 1776 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1777 FormatStyle::SIS_Never; 1778 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1779 verifyFormat("if (true)\n" 1780 "{\n" 1781 " f();\n" 1782 "}", 1783 AllowSimpleBracedStatements); 1784 verifyFormat("if (true)\n" 1785 "{\n" 1786 " f();\n" 1787 "} else\n" 1788 "{\n" 1789 " f();\n" 1790 "}", 1791 AllowSimpleBracedStatements); 1792 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1793 verifyFormat("MYIF (true)\n" 1794 "{\n" 1795 " f();\n" 1796 "}", 1797 AllowSimpleBracedStatements); 1798 verifyFormat("MYIF (true)\n" 1799 "{\n" 1800 " f();\n" 1801 "} else\n" 1802 "{\n" 1803 " f();\n" 1804 "}", 1805 AllowSimpleBracedStatements); 1806 1807 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1808 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1809 verifyFormat("while (true)\n" 1810 "{\n" 1811 " f();\n" 1812 "}", 1813 AllowSimpleBracedStatements); 1814 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1815 verifyFormat("for (;;)\n" 1816 "{\n" 1817 " f();\n" 1818 "}", 1819 AllowSimpleBracedStatements); 1820 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1821 verifyFormat("BOOST_FOREACH (int v, vec)\n" 1822 "{\n" 1823 " f();\n" 1824 "}", 1825 AllowSimpleBracedStatements); 1826 } 1827 1828 TEST_F(FormatTest, UnderstandsMacros) { 1829 verifyFormat("#define A (parentheses)"); 1830 verifyFormat("/* comment */ #define A (parentheses)"); 1831 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)"); 1832 // Even the partial code should never be merged. 1833 EXPECT_EQ("/* comment */ #define A (parentheses)\n" 1834 "#", 1835 format("/* comment */ #define A (parentheses)\n" 1836 "#")); 1837 verifyFormat("/* comment */ #define A (parentheses)\n" 1838 "#\n"); 1839 verifyFormat("/* comment */ #define A (parentheses)\n" 1840 "#define B (parentheses)"); 1841 verifyFormat("#define true ((int)1)"); 1842 verifyFormat("#define and(x)"); 1843 verifyFormat("#define if(x) x"); 1844 verifyFormat("#define return(x) (x)"); 1845 verifyFormat("#define while(x) for (; x;)"); 1846 verifyFormat("#define xor(x) (^(x))"); 1847 verifyFormat("#define __except(x)"); 1848 verifyFormat("#define __try(x)"); 1849 1850 FormatStyle Style = getLLVMStyle(); 1851 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1852 Style.BraceWrapping.AfterFunction = true; 1853 // Test that a macro definition never gets merged with the following 1854 // definition. 1855 // FIXME: The AAA macro definition probably should not be split into 3 lines. 1856 verifyFormat("#define AAA " 1857 " \\\n" 1858 " N " 1859 " \\\n" 1860 " {\n" 1861 "#define BBB }\n", 1862 Style); 1863 // verifyFormat("#define AAA N { //\n", Style); 1864 } 1865 1866 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1867 FormatStyle Style = getLLVMStyleWithColumns(60); 1868 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1869 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1870 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1871 EXPECT_EQ("#define A \\\n" 1872 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1873 " { \\\n" 1874 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1875 " }\n" 1876 "X;", 1877 format("#define A \\\n" 1878 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1879 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1880 " }\n" 1881 "X;", 1882 Style)); 1883 } 1884 1885 TEST_F(FormatTest, ParseIfElse) { 1886 verifyFormat("if (true)\n" 1887 " if (true)\n" 1888 " if (true)\n" 1889 " f();\n" 1890 " else\n" 1891 " g();\n" 1892 " else\n" 1893 " h();\n" 1894 "else\n" 1895 " i();"); 1896 verifyFormat("if (true)\n" 1897 " if (true)\n" 1898 " if (true) {\n" 1899 " if (true)\n" 1900 " f();\n" 1901 " } else {\n" 1902 " g();\n" 1903 " }\n" 1904 " else\n" 1905 " h();\n" 1906 "else {\n" 1907 " i();\n" 1908 "}"); 1909 verifyFormat("if (true)\n" 1910 " if constexpr (true)\n" 1911 " if (true) {\n" 1912 " if constexpr (true)\n" 1913 " f();\n" 1914 " } else {\n" 1915 " g();\n" 1916 " }\n" 1917 " else\n" 1918 " h();\n" 1919 "else {\n" 1920 " i();\n" 1921 "}"); 1922 verifyFormat("if (true)\n" 1923 " if CONSTEXPR (true)\n" 1924 " if (true) {\n" 1925 " if CONSTEXPR (true)\n" 1926 " f();\n" 1927 " } else {\n" 1928 " g();\n" 1929 " }\n" 1930 " else\n" 1931 " h();\n" 1932 "else {\n" 1933 " i();\n" 1934 "}"); 1935 verifyFormat("void f() {\n" 1936 " if (a) {\n" 1937 " } else {\n" 1938 " }\n" 1939 "}"); 1940 } 1941 1942 TEST_F(FormatTest, ElseIf) { 1943 verifyFormat("if (a) {\n} else if (b) {\n}"); 1944 verifyFormat("if (a)\n" 1945 " f();\n" 1946 "else if (b)\n" 1947 " g();\n" 1948 "else\n" 1949 " h();"); 1950 verifyFormat("if (a)\n" 1951 " f();\n" 1952 "else // comment\n" 1953 " if (b) {\n" 1954 " g();\n" 1955 " h();\n" 1956 " }"); 1957 verifyFormat("if constexpr (a)\n" 1958 " f();\n" 1959 "else if constexpr (b)\n" 1960 " g();\n" 1961 "else\n" 1962 " h();"); 1963 verifyFormat("if CONSTEXPR (a)\n" 1964 " f();\n" 1965 "else if CONSTEXPR (b)\n" 1966 " g();\n" 1967 "else\n" 1968 " h();"); 1969 verifyFormat("if (a) {\n" 1970 " f();\n" 1971 "}\n" 1972 "// or else ..\n" 1973 "else {\n" 1974 " g()\n" 1975 "}"); 1976 1977 verifyFormat("if (a) {\n" 1978 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1979 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1980 "}"); 1981 verifyFormat("if (a) {\n" 1982 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1983 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1984 "}"); 1985 verifyFormat("if (a) {\n" 1986 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1987 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1988 "}"); 1989 verifyFormat("if (a) {\n" 1990 "} else if (\n" 1991 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1992 "}", 1993 getLLVMStyleWithColumns(62)); 1994 verifyFormat("if (a) {\n" 1995 "} else if constexpr (\n" 1996 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1997 "}", 1998 getLLVMStyleWithColumns(62)); 1999 verifyFormat("if (a) {\n" 2000 "} else if CONSTEXPR (\n" 2001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2002 "}", 2003 getLLVMStyleWithColumns(62)); 2004 } 2005 2006 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 2007 FormatStyle Style = getLLVMStyle(); 2008 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); 2009 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer); 2010 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 2011 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 2012 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 2013 verifyFormat("int *f1(int &a) const &;", Style); 2014 verifyFormat("int *f1(int &a) const & = 0;", Style); 2015 verifyFormat("int *a = f1();", Style); 2016 verifyFormat("int &b = f2();", Style); 2017 verifyFormat("int &&c = f3();", Style); 2018 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 2019 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); 2020 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 2021 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2022 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 2023 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); 2024 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 2025 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); 2026 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); 2027 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); 2028 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); 2029 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); 2030 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); 2031 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); 2032 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); 2033 verifyFormat("for (f(); int &c : {1, 2, 3})", Style); 2034 verifyFormat( 2035 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n" 2036 " res2 = [](int &a) { return 0000000000000; };", 2037 Style); 2038 2039 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2040 verifyFormat("Const unsigned int *c;\n" 2041 "const unsigned int *d;\n" 2042 "Const unsigned int &e;\n" 2043 "const unsigned int &f;\n" 2044 "const unsigned &&g;\n" 2045 "Const unsigned h;", 2046 Style); 2047 2048 Style.PointerAlignment = FormatStyle::PAS_Left; 2049 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& = 0;", Style); 2054 verifyFormat("int* a = f1();", Style); 2055 verifyFormat("int& b = f2();", Style); 2056 verifyFormat("int&& c = f3();", Style); 2057 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2058 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2059 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2060 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2061 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2062 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2063 verifyFormat("for (int a = 0, b = 0; const Foo& 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 (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2069 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); 2070 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); 2071 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); 2072 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); 2073 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); 2074 verifyFormat("for (f(); int& c : {1, 2, 3})", Style); 2075 verifyFormat( 2076 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n" 2077 " res2 = [](int& a) { return 0000000000000; };", 2078 Style); 2079 2080 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2081 verifyFormat("Const unsigned int* c;\n" 2082 "const unsigned int* d;\n" 2083 "Const unsigned int& e;\n" 2084 "const unsigned int& f;\n" 2085 "const unsigned&& g;\n" 2086 "Const unsigned h;", 2087 Style); 2088 2089 Style.PointerAlignment = FormatStyle::PAS_Right; 2090 Style.ReferenceAlignment = FormatStyle::RAS_Left; 2091 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 2092 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 2093 verifyFormat("int&& f3(int& b, int&& c, int *a);", 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 Foo *c : {1, 2, 3})", Style); 2098 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2099 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); 2100 2101 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2102 verifyFormat("Const unsigned int *c;\n" 2103 "const unsigned int *d;\n" 2104 "Const unsigned int& e;\n" 2105 "const unsigned int& f;\n" 2106 "const unsigned g;\n" 2107 "Const unsigned h;", 2108 Style); 2109 2110 Style.PointerAlignment = FormatStyle::PAS_Left; 2111 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 2112 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 2113 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 2114 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 2115 verifyFormat("int* a = f1();", Style); 2116 verifyFormat("int & b = f2();", Style); 2117 verifyFormat("int && c = f3();", Style); 2118 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); 2119 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); 2120 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); 2121 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2122 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); 2123 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); 2124 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); 2125 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2126 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); 2127 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); 2128 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); 2129 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); 2130 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); 2131 verifyFormat("for (f(); int & c : {1, 2, 3})", Style); 2132 verifyFormat( 2133 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n" 2134 " res2 = [](int & a) { return 0000000000000; };", 2135 Style); 2136 2137 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2138 verifyFormat("Const unsigned int* c;\n" 2139 "const unsigned int* d;\n" 2140 "Const unsigned int & e;\n" 2141 "const unsigned int & f;\n" 2142 "const unsigned && g;\n" 2143 "Const unsigned h;", 2144 Style); 2145 2146 Style.PointerAlignment = FormatStyle::PAS_Middle; 2147 Style.ReferenceAlignment = FormatStyle::RAS_Right; 2148 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 2149 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 2150 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 2151 verifyFormat("int * a = f1();", Style); 2152 verifyFormat("int &b = f2();", Style); 2153 verifyFormat("int &&c = f3();", Style); 2154 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2155 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2156 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); 2157 2158 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2159 // specifically handled 2160 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2161 } 2162 2163 TEST_F(FormatTest, FormatsForLoop) { 2164 verifyFormat( 2165 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2166 " ++VeryVeryLongLoopVariable)\n" 2167 " ;"); 2168 verifyFormat("for (;;)\n" 2169 " f();"); 2170 verifyFormat("for (;;) {\n}"); 2171 verifyFormat("for (;;) {\n" 2172 " f();\n" 2173 "}"); 2174 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2175 2176 verifyFormat( 2177 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2178 " E = UnwrappedLines.end();\n" 2179 " I != E; ++I) {\n}"); 2180 2181 verifyFormat( 2182 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2183 " ++IIIII) {\n}"); 2184 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2185 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2186 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2187 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2188 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2189 " E = FD->getDeclsInPrototypeScope().end();\n" 2190 " I != E; ++I) {\n}"); 2191 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2192 " I = Container.begin(),\n" 2193 " E = Container.end();\n" 2194 " I != E; ++I) {\n}", 2195 getLLVMStyleWithColumns(76)); 2196 2197 verifyFormat( 2198 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2199 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2202 " ++aaaaaaaaaaa) {\n}"); 2203 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2204 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2205 " ++i) {\n}"); 2206 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2207 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2208 "}"); 2209 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2210 " aaaaaaaaaa);\n" 2211 " iter; ++iter) {\n" 2212 "}"); 2213 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2214 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2215 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2216 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2217 2218 // These should not be formatted as Objective-C for-in loops. 2219 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2220 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2221 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2222 verifyFormat( 2223 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2224 2225 FormatStyle NoBinPacking = getLLVMStyle(); 2226 NoBinPacking.BinPackParameters = false; 2227 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2228 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2229 " aaaaaaaaaaaaaaaa,\n" 2230 " aaaaaaaaaaaaaaaa,\n" 2231 " aaaaaaaaaaaaaaaa);\n" 2232 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2233 "}", 2234 NoBinPacking); 2235 verifyFormat( 2236 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2237 " E = UnwrappedLines.end();\n" 2238 " I != E;\n" 2239 " ++I) {\n}", 2240 NoBinPacking); 2241 2242 FormatStyle AlignLeft = getLLVMStyle(); 2243 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2244 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2245 } 2246 2247 TEST_F(FormatTest, RangeBasedForLoops) { 2248 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2249 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2250 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2251 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2252 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2253 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2254 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2255 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2256 } 2257 2258 TEST_F(FormatTest, ForEachLoops) { 2259 FormatStyle Style = getLLVMStyle(); 2260 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2261 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false); 2262 verifyFormat("void f() {\n" 2263 " for (;;) {\n" 2264 " }\n" 2265 " foreach (Item *item, itemlist) {\n" 2266 " }\n" 2267 " Q_FOREACH (Item *item, itemlist) {\n" 2268 " }\n" 2269 " BOOST_FOREACH (Item *item, itemlist) {\n" 2270 " }\n" 2271 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2272 "}", 2273 Style); 2274 verifyFormat("void f() {\n" 2275 " for (;;)\n" 2276 " int j = 1;\n" 2277 " Q_FOREACH (int v, vec)\n" 2278 " v *= 2;\n" 2279 " for (;;) {\n" 2280 " int j = 1;\n" 2281 " }\n" 2282 " Q_FOREACH (int v, vec) {\n" 2283 " v *= 2;\n" 2284 " }\n" 2285 "}", 2286 Style); 2287 2288 FormatStyle ShortBlocks = getLLVMStyle(); 2289 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2290 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false); 2291 verifyFormat("void f() {\n" 2292 " for (;;)\n" 2293 " int j = 1;\n" 2294 " Q_FOREACH (int &v, vec)\n" 2295 " v *= 2;\n" 2296 " for (;;) {\n" 2297 " int j = 1;\n" 2298 " }\n" 2299 " Q_FOREACH (int &v, vec) {\n" 2300 " int j = 1;\n" 2301 " }\n" 2302 "}", 2303 ShortBlocks); 2304 2305 FormatStyle ShortLoops = getLLVMStyle(); 2306 ShortLoops.AllowShortLoopsOnASingleLine = true; 2307 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2308 verifyFormat("void f() {\n" 2309 " for (;;) int j = 1;\n" 2310 " Q_FOREACH (int &v, vec) int j = 1;\n" 2311 " for (;;) {\n" 2312 " int j = 1;\n" 2313 " }\n" 2314 " Q_FOREACH (int &v, vec) {\n" 2315 " int j = 1;\n" 2316 " }\n" 2317 "}", 2318 ShortLoops); 2319 2320 FormatStyle ShortBlocksAndLoops = getLLVMStyle(); 2321 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2322 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true; 2323 verifyFormat("void f() {\n" 2324 " for (;;) int j = 1;\n" 2325 " Q_FOREACH (int &v, vec) int j = 1;\n" 2326 " for (;;) { int j = 1; }\n" 2327 " Q_FOREACH (int &v, vec) { int j = 1; }\n" 2328 "}", 2329 ShortBlocksAndLoops); 2330 2331 Style.SpaceBeforeParens = 2332 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2333 verifyFormat("void f() {\n" 2334 " for (;;) {\n" 2335 " }\n" 2336 " foreach(Item *item, itemlist) {\n" 2337 " }\n" 2338 " Q_FOREACH(Item *item, itemlist) {\n" 2339 " }\n" 2340 " BOOST_FOREACH(Item *item, itemlist) {\n" 2341 " }\n" 2342 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2343 "}", 2344 Style); 2345 2346 // As function-like macros. 2347 verifyFormat("#define foreach(x, y)\n" 2348 "#define Q_FOREACH(x, y)\n" 2349 "#define BOOST_FOREACH(x, y)\n" 2350 "#define UNKNOWN_FOREACH(x, y)\n"); 2351 2352 // Not as function-like macros. 2353 verifyFormat("#define foreach (x, y)\n" 2354 "#define Q_FOREACH (x, y)\n" 2355 "#define BOOST_FOREACH (x, y)\n" 2356 "#define UNKNOWN_FOREACH (x, y)\n"); 2357 2358 // handle microsoft non standard extension 2359 verifyFormat("for each (char c in x->MyStringProperty)"); 2360 } 2361 2362 TEST_F(FormatTest, FormatsWhileLoop) { 2363 verifyFormat("while (true) {\n}"); 2364 verifyFormat("while (true)\n" 2365 " f();"); 2366 verifyFormat("while () {\n}"); 2367 verifyFormat("while () {\n" 2368 " f();\n" 2369 "}"); 2370 } 2371 2372 TEST_F(FormatTest, FormatsDoWhile) { 2373 verifyFormat("do {\n" 2374 " do_something();\n" 2375 "} while (something());"); 2376 verifyFormat("do\n" 2377 " do_something();\n" 2378 "while (something());"); 2379 } 2380 2381 TEST_F(FormatTest, FormatsSwitchStatement) { 2382 verifyFormat("switch (x) {\n" 2383 "case 1:\n" 2384 " f();\n" 2385 " break;\n" 2386 "case kFoo:\n" 2387 "case ns::kBar:\n" 2388 "case kBaz:\n" 2389 " break;\n" 2390 "default:\n" 2391 " g();\n" 2392 " break;\n" 2393 "}"); 2394 verifyFormat("switch (x) {\n" 2395 "case 1: {\n" 2396 " f();\n" 2397 " break;\n" 2398 "}\n" 2399 "case 2: {\n" 2400 " break;\n" 2401 "}\n" 2402 "}"); 2403 verifyFormat("switch (x) {\n" 2404 "case 1: {\n" 2405 " f();\n" 2406 " {\n" 2407 " g();\n" 2408 " h();\n" 2409 " }\n" 2410 " break;\n" 2411 "}\n" 2412 "}"); 2413 verifyFormat("switch (x) {\n" 2414 "case 1: {\n" 2415 " f();\n" 2416 " if (foo) {\n" 2417 " g();\n" 2418 " h();\n" 2419 " }\n" 2420 " break;\n" 2421 "}\n" 2422 "}"); 2423 verifyFormat("switch (x) {\n" 2424 "case 1: {\n" 2425 " f();\n" 2426 " g();\n" 2427 "} break;\n" 2428 "}"); 2429 verifyFormat("switch (test)\n" 2430 " ;"); 2431 verifyFormat("switch (x) {\n" 2432 "default: {\n" 2433 " // Do nothing.\n" 2434 "}\n" 2435 "}"); 2436 verifyFormat("switch (x) {\n" 2437 "// comment\n" 2438 "// if 1, do f()\n" 2439 "case 1:\n" 2440 " f();\n" 2441 "}"); 2442 verifyFormat("switch (x) {\n" 2443 "case 1:\n" 2444 " // Do amazing stuff\n" 2445 " {\n" 2446 " f();\n" 2447 " g();\n" 2448 " }\n" 2449 " break;\n" 2450 "}"); 2451 verifyFormat("#define A \\\n" 2452 " switch (x) { \\\n" 2453 " case a: \\\n" 2454 " foo = b; \\\n" 2455 " }", 2456 getLLVMStyleWithColumns(20)); 2457 verifyFormat("#define OPERATION_CASE(name) \\\n" 2458 " case OP_name: \\\n" 2459 " return operations::Operation##name\n", 2460 getLLVMStyleWithColumns(40)); 2461 verifyFormat("switch (x) {\n" 2462 "case 1:;\n" 2463 "default:;\n" 2464 " int i;\n" 2465 "}"); 2466 2467 verifyGoogleFormat("switch (x) {\n" 2468 " case 1:\n" 2469 " f();\n" 2470 " break;\n" 2471 " case kFoo:\n" 2472 " case ns::kBar:\n" 2473 " case kBaz:\n" 2474 " break;\n" 2475 " default:\n" 2476 " g();\n" 2477 " break;\n" 2478 "}"); 2479 verifyGoogleFormat("switch (x) {\n" 2480 " case 1: {\n" 2481 " f();\n" 2482 " break;\n" 2483 " }\n" 2484 "}"); 2485 verifyGoogleFormat("switch (test)\n" 2486 " ;"); 2487 2488 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2489 " case OP_name: \\\n" 2490 " return operations::Operation##name\n"); 2491 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2492 " // Get the correction operation class.\n" 2493 " switch (OpCode) {\n" 2494 " CASE(Add);\n" 2495 " CASE(Subtract);\n" 2496 " default:\n" 2497 " return operations::Unknown;\n" 2498 " }\n" 2499 "#undef OPERATION_CASE\n" 2500 "}"); 2501 verifyFormat("DEBUG({\n" 2502 " switch (x) {\n" 2503 " case A:\n" 2504 " f();\n" 2505 " break;\n" 2506 " // fallthrough\n" 2507 " case B:\n" 2508 " g();\n" 2509 " break;\n" 2510 " }\n" 2511 "});"); 2512 EXPECT_EQ("DEBUG({\n" 2513 " switch (x) {\n" 2514 " case A:\n" 2515 " f();\n" 2516 " break;\n" 2517 " // On B:\n" 2518 " case B:\n" 2519 " g();\n" 2520 " break;\n" 2521 " }\n" 2522 "});", 2523 format("DEBUG({\n" 2524 " switch (x) {\n" 2525 " case A:\n" 2526 " f();\n" 2527 " break;\n" 2528 " // On B:\n" 2529 " case B:\n" 2530 " g();\n" 2531 " break;\n" 2532 " }\n" 2533 "});", 2534 getLLVMStyle())); 2535 EXPECT_EQ("switch (n) {\n" 2536 "case 0: {\n" 2537 " return false;\n" 2538 "}\n" 2539 "default: {\n" 2540 " return true;\n" 2541 "}\n" 2542 "}", 2543 format("switch (n)\n" 2544 "{\n" 2545 "case 0: {\n" 2546 " return false;\n" 2547 "}\n" 2548 "default: {\n" 2549 " return true;\n" 2550 "}\n" 2551 "}", 2552 getLLVMStyle())); 2553 verifyFormat("switch (a) {\n" 2554 "case (b):\n" 2555 " return;\n" 2556 "}"); 2557 2558 verifyFormat("switch (a) {\n" 2559 "case some_namespace::\n" 2560 " some_constant:\n" 2561 " return;\n" 2562 "}", 2563 getLLVMStyleWithColumns(34)); 2564 2565 FormatStyle Style = getLLVMStyle(); 2566 Style.IndentCaseLabels = true; 2567 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2568 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2569 Style.BraceWrapping.AfterCaseLabel = true; 2570 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2571 EXPECT_EQ("switch (n)\n" 2572 "{\n" 2573 " case 0:\n" 2574 " {\n" 2575 " return false;\n" 2576 " }\n" 2577 " default:\n" 2578 " {\n" 2579 " return true;\n" 2580 " }\n" 2581 "}", 2582 format("switch (n) {\n" 2583 " case 0: {\n" 2584 " return false;\n" 2585 " }\n" 2586 " default: {\n" 2587 " return true;\n" 2588 " }\n" 2589 "}", 2590 Style)); 2591 Style.BraceWrapping.AfterCaseLabel = false; 2592 EXPECT_EQ("switch (n)\n" 2593 "{\n" 2594 " case 0: {\n" 2595 " return false;\n" 2596 " }\n" 2597 " default: {\n" 2598 " return true;\n" 2599 " }\n" 2600 "}", 2601 format("switch (n) {\n" 2602 " case 0:\n" 2603 " {\n" 2604 " return false;\n" 2605 " }\n" 2606 " default:\n" 2607 " {\n" 2608 " return true;\n" 2609 " }\n" 2610 "}", 2611 Style)); 2612 Style.IndentCaseLabels = false; 2613 Style.IndentCaseBlocks = true; 2614 EXPECT_EQ("switch (n)\n" 2615 "{\n" 2616 "case 0:\n" 2617 " {\n" 2618 " return false;\n" 2619 " }\n" 2620 "case 1:\n" 2621 " break;\n" 2622 "default:\n" 2623 " {\n" 2624 " return true;\n" 2625 " }\n" 2626 "}", 2627 format("switch (n) {\n" 2628 "case 0: {\n" 2629 " return false;\n" 2630 "}\n" 2631 "case 1:\n" 2632 " break;\n" 2633 "default: {\n" 2634 " return true;\n" 2635 "}\n" 2636 "}", 2637 Style)); 2638 Style.IndentCaseLabels = true; 2639 Style.IndentCaseBlocks = true; 2640 EXPECT_EQ("switch (n)\n" 2641 "{\n" 2642 " case 0:\n" 2643 " {\n" 2644 " return false;\n" 2645 " }\n" 2646 " case 1:\n" 2647 " break;\n" 2648 " default:\n" 2649 " {\n" 2650 " return true;\n" 2651 " }\n" 2652 "}", 2653 format("switch (n) {\n" 2654 "case 0: {\n" 2655 " return false;\n" 2656 "}\n" 2657 "case 1:\n" 2658 " break;\n" 2659 "default: {\n" 2660 " return true;\n" 2661 "}\n" 2662 "}", 2663 Style)); 2664 } 2665 2666 TEST_F(FormatTest, CaseRanges) { 2667 verifyFormat("switch (x) {\n" 2668 "case 'A' ... 'Z':\n" 2669 "case 1 ... 5:\n" 2670 "case a ... b:\n" 2671 " break;\n" 2672 "}"); 2673 } 2674 2675 TEST_F(FormatTest, ShortEnums) { 2676 FormatStyle Style = getLLVMStyle(); 2677 Style.AllowShortEnumsOnASingleLine = true; 2678 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2679 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2680 Style.AllowShortEnumsOnASingleLine = false; 2681 verifyFormat("enum {\n" 2682 " A,\n" 2683 " B,\n" 2684 " C\n" 2685 "} ShortEnum1, ShortEnum2;", 2686 Style); 2687 verifyFormat("typedef enum {\n" 2688 " A,\n" 2689 " B,\n" 2690 " C\n" 2691 "} ShortEnum1, ShortEnum2;", 2692 Style); 2693 verifyFormat("enum {\n" 2694 " A,\n" 2695 "} ShortEnum1, ShortEnum2;", 2696 Style); 2697 verifyFormat("typedef enum {\n" 2698 " A,\n" 2699 "} ShortEnum1, ShortEnum2;", 2700 Style); 2701 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2702 Style.BraceWrapping.AfterEnum = true; 2703 verifyFormat("enum\n" 2704 "{\n" 2705 " A,\n" 2706 " B,\n" 2707 " C\n" 2708 "} ShortEnum1, ShortEnum2;", 2709 Style); 2710 verifyFormat("typedef enum\n" 2711 "{\n" 2712 " A,\n" 2713 " B,\n" 2714 " C\n" 2715 "} ShortEnum1, ShortEnum2;", 2716 Style); 2717 } 2718 2719 TEST_F(FormatTest, ShortCaseLabels) { 2720 FormatStyle Style = getLLVMStyle(); 2721 Style.AllowShortCaseLabelsOnASingleLine = true; 2722 verifyFormat("switch (a) {\n" 2723 "case 1: x = 1; break;\n" 2724 "case 2: return;\n" 2725 "case 3:\n" 2726 "case 4:\n" 2727 "case 5: return;\n" 2728 "case 6: // comment\n" 2729 " return;\n" 2730 "case 7:\n" 2731 " // comment\n" 2732 " return;\n" 2733 "case 8:\n" 2734 " x = 8; // comment\n" 2735 " break;\n" 2736 "default: y = 1; break;\n" 2737 "}", 2738 Style); 2739 verifyFormat("switch (a) {\n" 2740 "case 0: return; // comment\n" 2741 "case 1: break; // comment\n" 2742 "case 2: return;\n" 2743 "// comment\n" 2744 "case 3: return;\n" 2745 "// comment 1\n" 2746 "// comment 2\n" 2747 "// comment 3\n" 2748 "case 4: break; /* comment */\n" 2749 "case 5:\n" 2750 " // comment\n" 2751 " break;\n" 2752 "case 6: /* comment */ x = 1; break;\n" 2753 "case 7: x = /* comment */ 1; break;\n" 2754 "case 8:\n" 2755 " x = 1; /* comment */\n" 2756 " break;\n" 2757 "case 9:\n" 2758 " break; // comment line 1\n" 2759 " // comment line 2\n" 2760 "}", 2761 Style); 2762 EXPECT_EQ("switch (a) {\n" 2763 "case 1:\n" 2764 " x = 8;\n" 2765 " // fall through\n" 2766 "case 2: x = 8;\n" 2767 "// comment\n" 2768 "case 3:\n" 2769 " return; /* comment line 1\n" 2770 " * comment line 2 */\n" 2771 "case 4: i = 8;\n" 2772 "// something else\n" 2773 "#if FOO\n" 2774 "case 5: break;\n" 2775 "#endif\n" 2776 "}", 2777 format("switch (a) {\n" 2778 "case 1: x = 8;\n" 2779 " // fall through\n" 2780 "case 2:\n" 2781 " x = 8;\n" 2782 "// comment\n" 2783 "case 3:\n" 2784 " return; /* comment line 1\n" 2785 " * comment line 2 */\n" 2786 "case 4:\n" 2787 " i = 8;\n" 2788 "// something else\n" 2789 "#if FOO\n" 2790 "case 5: break;\n" 2791 "#endif\n" 2792 "}", 2793 Style)); 2794 EXPECT_EQ("switch (a) {\n" 2795 "case 0:\n" 2796 " return; // long long long long long long long long long long " 2797 "long long comment\n" 2798 " // line\n" 2799 "}", 2800 format("switch (a) {\n" 2801 "case 0: return; // long long long long long long long long " 2802 "long long long long comment line\n" 2803 "}", 2804 Style)); 2805 EXPECT_EQ("switch (a) {\n" 2806 "case 0:\n" 2807 " return; /* long long long long long long long long long long " 2808 "long long comment\n" 2809 " line */\n" 2810 "}", 2811 format("switch (a) {\n" 2812 "case 0: return; /* long long long long long long long long " 2813 "long long long long comment line */\n" 2814 "}", 2815 Style)); 2816 verifyFormat("switch (a) {\n" 2817 "#if FOO\n" 2818 "case 0: return 0;\n" 2819 "#endif\n" 2820 "}", 2821 Style); 2822 verifyFormat("switch (a) {\n" 2823 "case 1: {\n" 2824 "}\n" 2825 "case 2: {\n" 2826 " return;\n" 2827 "}\n" 2828 "case 3: {\n" 2829 " x = 1;\n" 2830 " return;\n" 2831 "}\n" 2832 "case 4:\n" 2833 " if (x)\n" 2834 " return;\n" 2835 "}", 2836 Style); 2837 Style.ColumnLimit = 21; 2838 verifyFormat("switch (a) {\n" 2839 "case 1: x = 1; break;\n" 2840 "case 2: return;\n" 2841 "case 3:\n" 2842 "case 4:\n" 2843 "case 5: return;\n" 2844 "default:\n" 2845 " y = 1;\n" 2846 " break;\n" 2847 "}", 2848 Style); 2849 Style.ColumnLimit = 80; 2850 Style.AllowShortCaseLabelsOnASingleLine = false; 2851 Style.IndentCaseLabels = true; 2852 EXPECT_EQ("switch (n) {\n" 2853 " default /*comments*/:\n" 2854 " return true;\n" 2855 " case 0:\n" 2856 " return false;\n" 2857 "}", 2858 format("switch (n) {\n" 2859 "default/*comments*/:\n" 2860 " return true;\n" 2861 "case 0:\n" 2862 " return false;\n" 2863 "}", 2864 Style)); 2865 Style.AllowShortCaseLabelsOnASingleLine = true; 2866 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2867 Style.BraceWrapping.AfterCaseLabel = true; 2868 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2869 EXPECT_EQ("switch (n)\n" 2870 "{\n" 2871 " case 0:\n" 2872 " {\n" 2873 " return false;\n" 2874 " }\n" 2875 " default:\n" 2876 " {\n" 2877 " return true;\n" 2878 " }\n" 2879 "}", 2880 format("switch (n) {\n" 2881 " case 0: {\n" 2882 " return false;\n" 2883 " }\n" 2884 " default:\n" 2885 " {\n" 2886 " return true;\n" 2887 " }\n" 2888 "}", 2889 Style)); 2890 } 2891 2892 TEST_F(FormatTest, FormatsLabels) { 2893 verifyFormat("void f() {\n" 2894 " some_code();\n" 2895 "test_label:\n" 2896 " some_other_code();\n" 2897 " {\n" 2898 " some_more_code();\n" 2899 " another_label:\n" 2900 " some_more_code();\n" 2901 " }\n" 2902 "}"); 2903 verifyFormat("{\n" 2904 " some_code();\n" 2905 "test_label:\n" 2906 " some_other_code();\n" 2907 "}"); 2908 verifyFormat("{\n" 2909 " some_code();\n" 2910 "test_label:;\n" 2911 " int i = 0;\n" 2912 "}"); 2913 FormatStyle Style = getLLVMStyle(); 2914 Style.IndentGotoLabels = false; 2915 verifyFormat("void f() {\n" 2916 " some_code();\n" 2917 "test_label:\n" 2918 " some_other_code();\n" 2919 " {\n" 2920 " some_more_code();\n" 2921 "another_label:\n" 2922 " some_more_code();\n" 2923 " }\n" 2924 "}", 2925 Style); 2926 verifyFormat("{\n" 2927 " some_code();\n" 2928 "test_label:\n" 2929 " some_other_code();\n" 2930 "}", 2931 Style); 2932 verifyFormat("{\n" 2933 " some_code();\n" 2934 "test_label:;\n" 2935 " int i = 0;\n" 2936 "}"); 2937 } 2938 2939 TEST_F(FormatTest, MultiLineControlStatements) { 2940 FormatStyle Style = getLLVMStyleWithColumns(20); 2941 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2942 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2943 // Short lines should keep opening brace on same line. 2944 EXPECT_EQ("if (foo) {\n" 2945 " bar();\n" 2946 "}", 2947 format("if(foo){bar();}", Style)); 2948 EXPECT_EQ("if (foo) {\n" 2949 " bar();\n" 2950 "} else {\n" 2951 " baz();\n" 2952 "}", 2953 format("if(foo){bar();}else{baz();}", Style)); 2954 EXPECT_EQ("if (foo && bar) {\n" 2955 " baz();\n" 2956 "}", 2957 format("if(foo&&bar){baz();}", Style)); 2958 EXPECT_EQ("if (foo) {\n" 2959 " bar();\n" 2960 "} else if (baz) {\n" 2961 " quux();\n" 2962 "}", 2963 format("if(foo){bar();}else if(baz){quux();}", Style)); 2964 EXPECT_EQ( 2965 "if (foo) {\n" 2966 " bar();\n" 2967 "} else if (baz) {\n" 2968 " quux();\n" 2969 "} else {\n" 2970 " foobar();\n" 2971 "}", 2972 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2973 EXPECT_EQ("for (;;) {\n" 2974 " foo();\n" 2975 "}", 2976 format("for(;;){foo();}")); 2977 EXPECT_EQ("while (1) {\n" 2978 " foo();\n" 2979 "}", 2980 format("while(1){foo();}", Style)); 2981 EXPECT_EQ("switch (foo) {\n" 2982 "case bar:\n" 2983 " return;\n" 2984 "}", 2985 format("switch(foo){case bar:return;}", Style)); 2986 EXPECT_EQ("try {\n" 2987 " foo();\n" 2988 "} catch (...) {\n" 2989 " bar();\n" 2990 "}", 2991 format("try{foo();}catch(...){bar();}", Style)); 2992 EXPECT_EQ("do {\n" 2993 " foo();\n" 2994 "} while (bar &&\n" 2995 " baz);", 2996 format("do{foo();}while(bar&&baz);", Style)); 2997 // Long lines should put opening brace on new line. 2998 EXPECT_EQ("if (foo && bar &&\n" 2999 " baz)\n" 3000 "{\n" 3001 " quux();\n" 3002 "}", 3003 format("if(foo&&bar&&baz){quux();}", Style)); 3004 EXPECT_EQ("if (foo && bar &&\n" 3005 " baz)\n" 3006 "{\n" 3007 " quux();\n" 3008 "}", 3009 format("if (foo && bar &&\n" 3010 " baz) {\n" 3011 " quux();\n" 3012 "}", 3013 Style)); 3014 EXPECT_EQ("if (foo) {\n" 3015 " bar();\n" 3016 "} else if (baz ||\n" 3017 " quux)\n" 3018 "{\n" 3019 " foobar();\n" 3020 "}", 3021 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 3022 EXPECT_EQ( 3023 "if (foo) {\n" 3024 " bar();\n" 3025 "} else if (baz ||\n" 3026 " quux)\n" 3027 "{\n" 3028 " foobar();\n" 3029 "} else {\n" 3030 " barbaz();\n" 3031 "}", 3032 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3033 Style)); 3034 EXPECT_EQ("for (int i = 0;\n" 3035 " i < 10; ++i)\n" 3036 "{\n" 3037 " foo();\n" 3038 "}", 3039 format("for(int i=0;i<10;++i){foo();}", Style)); 3040 EXPECT_EQ("foreach (int i,\n" 3041 " list)\n" 3042 "{\n" 3043 " foo();\n" 3044 "}", 3045 format("foreach(int i, list){foo();}", Style)); 3046 Style.ColumnLimit = 3047 40; // to concentrate at brace wrapping, not line wrap due to column limit 3048 EXPECT_EQ("foreach (int i, list) {\n" 3049 " foo();\n" 3050 "}", 3051 format("foreach(int i, list){foo();}", Style)); 3052 Style.ColumnLimit = 3053 20; // to concentrate at brace wrapping, not line wrap due to column limit 3054 EXPECT_EQ("while (foo || bar ||\n" 3055 " baz)\n" 3056 "{\n" 3057 " quux();\n" 3058 "}", 3059 format("while(foo||bar||baz){quux();}", Style)); 3060 EXPECT_EQ("switch (\n" 3061 " foo = barbaz)\n" 3062 "{\n" 3063 "case quux:\n" 3064 " return;\n" 3065 "}", 3066 format("switch(foo=barbaz){case quux:return;}", Style)); 3067 EXPECT_EQ("try {\n" 3068 " foo();\n" 3069 "} catch (\n" 3070 " Exception &bar)\n" 3071 "{\n" 3072 " baz();\n" 3073 "}", 3074 format("try{foo();}catch(Exception&bar){baz();}", Style)); 3075 Style.ColumnLimit = 3076 40; // to concentrate at brace wrapping, not line wrap due to column limit 3077 EXPECT_EQ("try {\n" 3078 " foo();\n" 3079 "} catch (Exception &bar) {\n" 3080 " baz();\n" 3081 "}", 3082 format("try{foo();}catch(Exception&bar){baz();}", Style)); 3083 Style.ColumnLimit = 3084 20; // to concentrate at brace wrapping, not line wrap due to column limit 3085 3086 Style.BraceWrapping.BeforeElse = true; 3087 EXPECT_EQ( 3088 "if (foo) {\n" 3089 " bar();\n" 3090 "}\n" 3091 "else if (baz ||\n" 3092 " quux)\n" 3093 "{\n" 3094 " foobar();\n" 3095 "}\n" 3096 "else {\n" 3097 " barbaz();\n" 3098 "}", 3099 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3100 Style)); 3101 3102 Style.BraceWrapping.BeforeCatch = true; 3103 EXPECT_EQ("try {\n" 3104 " foo();\n" 3105 "}\n" 3106 "catch (...) {\n" 3107 " baz();\n" 3108 "}", 3109 format("try{foo();}catch(...){baz();}", Style)); 3110 3111 Style.BraceWrapping.AfterFunction = true; 3112 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3113 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3114 Style.ColumnLimit = 80; 3115 verifyFormat("void shortfunction() { bar(); }", Style); 3116 3117 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3118 verifyFormat("void shortfunction()\n" 3119 "{\n" 3120 " bar();\n" 3121 "}", 3122 Style); 3123 } 3124 3125 TEST_F(FormatTest, BeforeWhile) { 3126 FormatStyle Style = getLLVMStyle(); 3127 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3128 3129 verifyFormat("do {\n" 3130 " foo();\n" 3131 "} while (1);", 3132 Style); 3133 Style.BraceWrapping.BeforeWhile = true; 3134 verifyFormat("do {\n" 3135 " foo();\n" 3136 "}\n" 3137 "while (1);", 3138 Style); 3139 } 3140 3141 //===----------------------------------------------------------------------===// 3142 // Tests for classes, namespaces, etc. 3143 //===----------------------------------------------------------------------===// 3144 3145 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 3146 verifyFormat("class A {};"); 3147 } 3148 3149 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 3150 verifyFormat("class A {\n" 3151 "public:\n" 3152 "public: // comment\n" 3153 "protected:\n" 3154 "private:\n" 3155 " void f() {}\n" 3156 "};"); 3157 verifyFormat("export class A {\n" 3158 "public:\n" 3159 "public: // comment\n" 3160 "protected:\n" 3161 "private:\n" 3162 " void f() {}\n" 3163 "};"); 3164 verifyGoogleFormat("class A {\n" 3165 " public:\n" 3166 " protected:\n" 3167 " private:\n" 3168 " void f() {}\n" 3169 "};"); 3170 verifyGoogleFormat("export class A {\n" 3171 " public:\n" 3172 " protected:\n" 3173 " private:\n" 3174 " void f() {}\n" 3175 "};"); 3176 verifyFormat("class A {\n" 3177 "public slots:\n" 3178 " void f1() {}\n" 3179 "public Q_SLOTS:\n" 3180 " void f2() {}\n" 3181 "protected slots:\n" 3182 " void f3() {}\n" 3183 "protected Q_SLOTS:\n" 3184 " void f4() {}\n" 3185 "private slots:\n" 3186 " void f5() {}\n" 3187 "private Q_SLOTS:\n" 3188 " void f6() {}\n" 3189 "signals:\n" 3190 " void g1();\n" 3191 "Q_SIGNALS:\n" 3192 " void g2();\n" 3193 "};"); 3194 3195 // Don't interpret 'signals' the wrong way. 3196 verifyFormat("signals.set();"); 3197 verifyFormat("for (Signals signals : f()) {\n}"); 3198 verifyFormat("{\n" 3199 " signals.set(); // This needs indentation.\n" 3200 "}"); 3201 verifyFormat("void f() {\n" 3202 "label:\n" 3203 " signals.baz();\n" 3204 "}"); 3205 verifyFormat("private[1];"); 3206 verifyFormat("testArray[public] = 1;"); 3207 verifyFormat("public();"); 3208 verifyFormat("myFunc(public);"); 3209 verifyFormat("std::vector<int> testVec = {private};"); 3210 verifyFormat("private.p = 1;"); 3211 verifyFormat("void function(private...){};"); 3212 verifyFormat("if (private && public)\n"); 3213 verifyFormat("private &= true;"); 3214 verifyFormat("int x = private * public;"); 3215 verifyFormat("public *= private;"); 3216 verifyFormat("int x = public + private;"); 3217 verifyFormat("private++;"); 3218 verifyFormat("++private;"); 3219 verifyFormat("public += private;"); 3220 verifyFormat("public = public - private;"); 3221 verifyFormat("public->foo();"); 3222 verifyFormat("private--;"); 3223 verifyFormat("--private;"); 3224 verifyFormat("public -= 1;"); 3225 verifyFormat("if (!private && !public)\n"); 3226 verifyFormat("public != private;"); 3227 verifyFormat("int x = public / private;"); 3228 verifyFormat("public /= 2;"); 3229 verifyFormat("public = public % 2;"); 3230 verifyFormat("public %= 2;"); 3231 verifyFormat("if (public < private)\n"); 3232 verifyFormat("public << private;"); 3233 verifyFormat("public <<= private;"); 3234 verifyFormat("if (public > private)\n"); 3235 verifyFormat("public >> private;"); 3236 verifyFormat("public >>= private;"); 3237 verifyFormat("public ^ private;"); 3238 verifyFormat("public ^= private;"); 3239 verifyFormat("public | private;"); 3240 verifyFormat("public |= private;"); 3241 verifyFormat("auto x = private ? 1 : 2;"); 3242 verifyFormat("if (public == private)\n"); 3243 verifyFormat("void foo(public, private)"); 3244 verifyFormat("public::foo();"); 3245 } 3246 3247 TEST_F(FormatTest, SeparatesLogicalBlocks) { 3248 EXPECT_EQ("class A {\n" 3249 "public:\n" 3250 " void f();\n" 3251 "\n" 3252 "private:\n" 3253 " void g() {}\n" 3254 " // test\n" 3255 "protected:\n" 3256 " int h;\n" 3257 "};", 3258 format("class A {\n" 3259 "public:\n" 3260 "void f();\n" 3261 "private:\n" 3262 "void g() {}\n" 3263 "// test\n" 3264 "protected:\n" 3265 "int h;\n" 3266 "};")); 3267 EXPECT_EQ("class A {\n" 3268 "protected:\n" 3269 "public:\n" 3270 " void f();\n" 3271 "};", 3272 format("class A {\n" 3273 "protected:\n" 3274 "\n" 3275 "public:\n" 3276 "\n" 3277 " void f();\n" 3278 "};")); 3279 3280 // Even ensure proper spacing inside macros. 3281 EXPECT_EQ("#define B \\\n" 3282 " class A { \\\n" 3283 " protected: \\\n" 3284 " public: \\\n" 3285 " void f(); \\\n" 3286 " };", 3287 format("#define B \\\n" 3288 " class A { \\\n" 3289 " protected: \\\n" 3290 " \\\n" 3291 " public: \\\n" 3292 " \\\n" 3293 " void f(); \\\n" 3294 " };", 3295 getGoogleStyle())); 3296 // But don't remove empty lines after macros ending in access specifiers. 3297 EXPECT_EQ("#define A private:\n" 3298 "\n" 3299 "int i;", 3300 format("#define A private:\n" 3301 "\n" 3302 "int i;")); 3303 } 3304 3305 TEST_F(FormatTest, FormatsClasses) { 3306 verifyFormat("class A : public B {};"); 3307 verifyFormat("class A : public ::B {};"); 3308 3309 verifyFormat( 3310 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3311 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3312 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3313 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3314 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3315 verifyFormat( 3316 "class A : public B, public C, public D, public E, public F {};"); 3317 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3318 " public C,\n" 3319 " public D,\n" 3320 " public E,\n" 3321 " public F,\n" 3322 " public G {};"); 3323 3324 verifyFormat("class\n" 3325 " ReallyReallyLongClassName {\n" 3326 " int i;\n" 3327 "};", 3328 getLLVMStyleWithColumns(32)); 3329 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3330 " aaaaaaaaaaaaaaaa> {};"); 3331 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3332 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3333 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3334 verifyFormat("template <class R, class C>\n" 3335 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3336 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3337 verifyFormat("class ::A::B {};"); 3338 } 3339 3340 TEST_F(FormatTest, BreakInheritanceStyle) { 3341 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3342 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3343 FormatStyle::BILS_BeforeComma; 3344 verifyFormat("class MyClass : public X {};", 3345 StyleWithInheritanceBreakBeforeComma); 3346 verifyFormat("class MyClass\n" 3347 " : public X\n" 3348 " , public Y {};", 3349 StyleWithInheritanceBreakBeforeComma); 3350 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3351 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3352 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3353 StyleWithInheritanceBreakBeforeComma); 3354 verifyFormat("struct aaaaaaaaaaaaa\n" 3355 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3356 " aaaaaaaaaaaaaaaa> {};", 3357 StyleWithInheritanceBreakBeforeComma); 3358 3359 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3360 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3361 FormatStyle::BILS_AfterColon; 3362 verifyFormat("class MyClass : public X {};", 3363 StyleWithInheritanceBreakAfterColon); 3364 verifyFormat("class MyClass : public X, public Y {};", 3365 StyleWithInheritanceBreakAfterColon); 3366 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3367 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3368 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3369 StyleWithInheritanceBreakAfterColon); 3370 verifyFormat("struct aaaaaaaaaaaaa :\n" 3371 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3372 " aaaaaaaaaaaaaaaa> {};", 3373 StyleWithInheritanceBreakAfterColon); 3374 3375 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3376 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3377 FormatStyle::BILS_AfterComma; 3378 verifyFormat("class MyClass : public X {};", 3379 StyleWithInheritanceBreakAfterComma); 3380 verifyFormat("class MyClass : public X,\n" 3381 " public Y {};", 3382 StyleWithInheritanceBreakAfterComma); 3383 verifyFormat( 3384 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3385 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3386 "{};", 3387 StyleWithInheritanceBreakAfterComma); 3388 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3389 " aaaaaaaaaaaaaaaa> {};", 3390 StyleWithInheritanceBreakAfterComma); 3391 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3392 " : public OnceBreak,\n" 3393 " public AlwaysBreak,\n" 3394 " EvenBasesFitInOneLine {};", 3395 StyleWithInheritanceBreakAfterComma); 3396 } 3397 3398 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 3399 verifyFormat("class A {\n} a, b;"); 3400 verifyFormat("struct A {\n} a, b;"); 3401 verifyFormat("union A {\n} a;"); 3402 } 3403 3404 TEST_F(FormatTest, FormatsEnum) { 3405 verifyFormat("enum {\n" 3406 " Zero,\n" 3407 " One = 1,\n" 3408 " Two = One + 1,\n" 3409 " Three = (One + Two),\n" 3410 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3411 " Five = (One, Two, Three, Four, 5)\n" 3412 "};"); 3413 verifyGoogleFormat("enum {\n" 3414 " Zero,\n" 3415 " One = 1,\n" 3416 " Two = One + 1,\n" 3417 " Three = (One + Two),\n" 3418 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3419 " Five = (One, Two, Three, Four, 5)\n" 3420 "};"); 3421 verifyFormat("enum Enum {};"); 3422 verifyFormat("enum {};"); 3423 verifyFormat("enum X E {} d;"); 3424 verifyFormat("enum __attribute__((...)) E {} d;"); 3425 verifyFormat("enum __declspec__((...)) E {} d;"); 3426 verifyFormat("enum {\n" 3427 " Bar = Foo<int, int>::value\n" 3428 "};", 3429 getLLVMStyleWithColumns(30)); 3430 3431 verifyFormat("enum ShortEnum { A, B, C };"); 3432 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3433 3434 EXPECT_EQ("enum KeepEmptyLines {\n" 3435 " ONE,\n" 3436 "\n" 3437 " TWO,\n" 3438 "\n" 3439 " THREE\n" 3440 "}", 3441 format("enum KeepEmptyLines {\n" 3442 " ONE,\n" 3443 "\n" 3444 " TWO,\n" 3445 "\n" 3446 "\n" 3447 " THREE\n" 3448 "}")); 3449 verifyFormat("enum E { // comment\n" 3450 " ONE,\n" 3451 " TWO\n" 3452 "};\n" 3453 "int i;"); 3454 3455 FormatStyle EightIndent = getLLVMStyle(); 3456 EightIndent.IndentWidth = 8; 3457 verifyFormat("enum {\n" 3458 " VOID,\n" 3459 " CHAR,\n" 3460 " SHORT,\n" 3461 " INT,\n" 3462 " LONG,\n" 3463 " SIGNED,\n" 3464 " UNSIGNED,\n" 3465 " BOOL,\n" 3466 " FLOAT,\n" 3467 " DOUBLE,\n" 3468 " COMPLEX\n" 3469 "};", 3470 EightIndent); 3471 3472 // Not enums. 3473 verifyFormat("enum X f() {\n" 3474 " a();\n" 3475 " return 42;\n" 3476 "}"); 3477 verifyFormat("enum X Type::f() {\n" 3478 " a();\n" 3479 " return 42;\n" 3480 "}"); 3481 verifyFormat("enum ::X f() {\n" 3482 " a();\n" 3483 " return 42;\n" 3484 "}"); 3485 verifyFormat("enum ns::X f() {\n" 3486 " a();\n" 3487 " return 42;\n" 3488 "}"); 3489 } 3490 3491 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3492 verifyFormat("enum Type {\n" 3493 " One = 0; // These semicolons should be commas.\n" 3494 " Two = 1;\n" 3495 "};"); 3496 verifyFormat("namespace n {\n" 3497 "enum Type {\n" 3498 " One,\n" 3499 " Two, // missing };\n" 3500 " int i;\n" 3501 "}\n" 3502 "void g() {}"); 3503 } 3504 3505 TEST_F(FormatTest, FormatsEnumStruct) { 3506 verifyFormat("enum struct {\n" 3507 " Zero,\n" 3508 " One = 1,\n" 3509 " Two = One + 1,\n" 3510 " Three = (One + Two),\n" 3511 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3512 " Five = (One, Two, Three, Four, 5)\n" 3513 "};"); 3514 verifyFormat("enum struct Enum {};"); 3515 verifyFormat("enum struct {};"); 3516 verifyFormat("enum struct X E {} d;"); 3517 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3518 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3519 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3520 } 3521 3522 TEST_F(FormatTest, FormatsEnumClass) { 3523 verifyFormat("enum class {\n" 3524 " Zero,\n" 3525 " One = 1,\n" 3526 " Two = One + 1,\n" 3527 " Three = (One + Two),\n" 3528 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3529 " Five = (One, Two, Three, Four, 5)\n" 3530 "};"); 3531 verifyFormat("enum class Enum {};"); 3532 verifyFormat("enum class {};"); 3533 verifyFormat("enum class X E {} d;"); 3534 verifyFormat("enum class __attribute__((...)) E {} d;"); 3535 verifyFormat("enum class __declspec__((...)) E {} d;"); 3536 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3537 } 3538 3539 TEST_F(FormatTest, FormatsEnumTypes) { 3540 verifyFormat("enum X : int {\n" 3541 " A, // Force multiple lines.\n" 3542 " B\n" 3543 "};"); 3544 verifyFormat("enum X : int { A, B };"); 3545 verifyFormat("enum X : std::uint32_t { A, B };"); 3546 } 3547 3548 TEST_F(FormatTest, FormatsTypedefEnum) { 3549 FormatStyle Style = getLLVMStyleWithColumns(40); 3550 verifyFormat("typedef enum {} EmptyEnum;"); 3551 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3552 verifyFormat("typedef enum {\n" 3553 " ZERO = 0,\n" 3554 " ONE = 1,\n" 3555 " TWO = 2,\n" 3556 " THREE = 3\n" 3557 "} LongEnum;", 3558 Style); 3559 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3560 Style.BraceWrapping.AfterEnum = true; 3561 verifyFormat("typedef enum {} EmptyEnum;"); 3562 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3563 verifyFormat("typedef enum\n" 3564 "{\n" 3565 " ZERO = 0,\n" 3566 " ONE = 1,\n" 3567 " TWO = 2,\n" 3568 " THREE = 3\n" 3569 "} LongEnum;", 3570 Style); 3571 } 3572 3573 TEST_F(FormatTest, FormatsNSEnums) { 3574 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3575 verifyGoogleFormat( 3576 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3577 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3578 " // Information about someDecentlyLongValue.\n" 3579 " someDecentlyLongValue,\n" 3580 " // Information about anotherDecentlyLongValue.\n" 3581 " anotherDecentlyLongValue,\n" 3582 " // Information about aThirdDecentlyLongValue.\n" 3583 " aThirdDecentlyLongValue\n" 3584 "};"); 3585 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3586 " // Information about someDecentlyLongValue.\n" 3587 " someDecentlyLongValue,\n" 3588 " // Information about anotherDecentlyLongValue.\n" 3589 " anotherDecentlyLongValue,\n" 3590 " // Information about aThirdDecentlyLongValue.\n" 3591 " aThirdDecentlyLongValue\n" 3592 "};"); 3593 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3594 " a = 1,\n" 3595 " b = 2,\n" 3596 " c = 3,\n" 3597 "};"); 3598 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3599 " a = 1,\n" 3600 " b = 2,\n" 3601 " c = 3,\n" 3602 "};"); 3603 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3604 " a = 1,\n" 3605 " b = 2,\n" 3606 " c = 3,\n" 3607 "};"); 3608 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3609 " a = 1,\n" 3610 " b = 2,\n" 3611 " c = 3,\n" 3612 "};"); 3613 } 3614 3615 TEST_F(FormatTest, FormatsBitfields) { 3616 verifyFormat("struct Bitfields {\n" 3617 " unsigned sClass : 8;\n" 3618 " unsigned ValueKind : 2;\n" 3619 "};"); 3620 verifyFormat("struct A {\n" 3621 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3622 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3623 "};"); 3624 verifyFormat("struct MyStruct {\n" 3625 " uchar data;\n" 3626 " uchar : 8;\n" 3627 " uchar : 8;\n" 3628 " uchar other;\n" 3629 "};"); 3630 FormatStyle Style = getLLVMStyle(); 3631 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3632 verifyFormat("struct Bitfields {\n" 3633 " unsigned sClass:8;\n" 3634 " unsigned ValueKind:2;\n" 3635 " uchar other;\n" 3636 "};", 3637 Style); 3638 verifyFormat("struct A {\n" 3639 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3640 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3641 "};", 3642 Style); 3643 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3644 verifyFormat("struct Bitfields {\n" 3645 " unsigned sClass :8;\n" 3646 " unsigned ValueKind :2;\n" 3647 " uchar other;\n" 3648 "};", 3649 Style); 3650 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3651 verifyFormat("struct Bitfields {\n" 3652 " unsigned sClass: 8;\n" 3653 " unsigned ValueKind: 2;\n" 3654 " uchar other;\n" 3655 "};", 3656 Style); 3657 } 3658 3659 TEST_F(FormatTest, FormatsNamespaces) { 3660 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3661 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3662 3663 verifyFormat("namespace some_namespace {\n" 3664 "class A {};\n" 3665 "void f() { f(); }\n" 3666 "}", 3667 LLVMWithNoNamespaceFix); 3668 verifyFormat("namespace N::inline D {\n" 3669 "class A {};\n" 3670 "void f() { f(); }\n" 3671 "}", 3672 LLVMWithNoNamespaceFix); 3673 verifyFormat("namespace N::inline D::E {\n" 3674 "class A {};\n" 3675 "void f() { f(); }\n" 3676 "}", 3677 LLVMWithNoNamespaceFix); 3678 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3679 "class A {};\n" 3680 "void f() { f(); }\n" 3681 "}", 3682 LLVMWithNoNamespaceFix); 3683 verifyFormat("/* something */ namespace some_namespace {\n" 3684 "class A {};\n" 3685 "void f() { f(); }\n" 3686 "}", 3687 LLVMWithNoNamespaceFix); 3688 verifyFormat("namespace {\n" 3689 "class A {};\n" 3690 "void f() { f(); }\n" 3691 "}", 3692 LLVMWithNoNamespaceFix); 3693 verifyFormat("/* something */ namespace {\n" 3694 "class A {};\n" 3695 "void f() { f(); }\n" 3696 "}", 3697 LLVMWithNoNamespaceFix); 3698 verifyFormat("inline namespace X {\n" 3699 "class A {};\n" 3700 "void f() { f(); }\n" 3701 "}", 3702 LLVMWithNoNamespaceFix); 3703 verifyFormat("/* something */ inline namespace X {\n" 3704 "class A {};\n" 3705 "void f() { f(); }\n" 3706 "}", 3707 LLVMWithNoNamespaceFix); 3708 verifyFormat("export namespace X {\n" 3709 "class A {};\n" 3710 "void f() { f(); }\n" 3711 "}", 3712 LLVMWithNoNamespaceFix); 3713 verifyFormat("using namespace some_namespace;\n" 3714 "class A {};\n" 3715 "void f() { f(); }", 3716 LLVMWithNoNamespaceFix); 3717 3718 // This code is more common than we thought; if we 3719 // layout this correctly the semicolon will go into 3720 // its own line, which is undesirable. 3721 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3722 verifyFormat("namespace {\n" 3723 "class A {};\n" 3724 "};", 3725 LLVMWithNoNamespaceFix); 3726 3727 verifyFormat("namespace {\n" 3728 "int SomeVariable = 0; // comment\n" 3729 "} // namespace", 3730 LLVMWithNoNamespaceFix); 3731 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3732 "#define HEADER_GUARD\n" 3733 "namespace my_namespace {\n" 3734 "int i;\n" 3735 "} // my_namespace\n" 3736 "#endif // HEADER_GUARD", 3737 format("#ifndef HEADER_GUARD\n" 3738 " #define HEADER_GUARD\n" 3739 " namespace my_namespace {\n" 3740 "int i;\n" 3741 "} // my_namespace\n" 3742 "#endif // HEADER_GUARD", 3743 LLVMWithNoNamespaceFix)); 3744 3745 EXPECT_EQ("namespace A::B {\n" 3746 "class C {};\n" 3747 "}", 3748 format("namespace A::B {\n" 3749 "class C {};\n" 3750 "}", 3751 LLVMWithNoNamespaceFix)); 3752 3753 FormatStyle Style = getLLVMStyle(); 3754 Style.NamespaceIndentation = FormatStyle::NI_All; 3755 EXPECT_EQ("namespace out {\n" 3756 " int i;\n" 3757 " namespace in {\n" 3758 " int i;\n" 3759 " } // namespace in\n" 3760 "} // namespace out", 3761 format("namespace out {\n" 3762 "int i;\n" 3763 "namespace in {\n" 3764 "int i;\n" 3765 "} // namespace in\n" 3766 "} // namespace out", 3767 Style)); 3768 3769 FormatStyle ShortInlineFunctions = getLLVMStyle(); 3770 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All; 3771 ShortInlineFunctions.AllowShortFunctionsOnASingleLine = 3772 FormatStyle::SFS_Inline; 3773 verifyFormat("namespace {\n" 3774 " void f() {\n" 3775 " return;\n" 3776 " }\n" 3777 "} // namespace\n", 3778 ShortInlineFunctions); 3779 verifyFormat("namespace {\n" 3780 " int some_int;\n" 3781 " void f() {\n" 3782 " return;\n" 3783 " }\n" 3784 "} // namespace\n", 3785 ShortInlineFunctions); 3786 verifyFormat("namespace interface {\n" 3787 " void f() {\n" 3788 " return;\n" 3789 " }\n" 3790 "} // namespace interface\n", 3791 ShortInlineFunctions); 3792 verifyFormat("namespace {\n" 3793 " class X {\n" 3794 " void f() { return; }\n" 3795 " };\n" 3796 "} // namespace\n", 3797 ShortInlineFunctions); 3798 verifyFormat("namespace {\n" 3799 " struct X {\n" 3800 " void f() { return; }\n" 3801 " };\n" 3802 "} // namespace\n", 3803 ShortInlineFunctions); 3804 verifyFormat("namespace {\n" 3805 " union X {\n" 3806 " void f() { return; }\n" 3807 " };\n" 3808 "} // namespace\n", 3809 ShortInlineFunctions); 3810 verifyFormat("extern \"C\" {\n" 3811 "void f() {\n" 3812 " return;\n" 3813 "}\n" 3814 "} // namespace\n", 3815 ShortInlineFunctions); 3816 verifyFormat("namespace {\n" 3817 " class X {\n" 3818 " void f() { return; }\n" 3819 " } x;\n" 3820 "} // namespace\n", 3821 ShortInlineFunctions); 3822 verifyFormat("namespace {\n" 3823 " [[nodiscard]] class X {\n" 3824 " void f() { return; }\n" 3825 " };\n" 3826 "} // namespace\n", 3827 ShortInlineFunctions); 3828 verifyFormat("namespace {\n" 3829 " static class X {\n" 3830 " void f() { return; }\n" 3831 " } x;\n" 3832 "} // namespace\n", 3833 ShortInlineFunctions); 3834 verifyFormat("namespace {\n" 3835 " constexpr class X {\n" 3836 " void f() { return; }\n" 3837 " } x;\n" 3838 "} // namespace\n", 3839 ShortInlineFunctions); 3840 3841 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent; 3842 verifyFormat("extern \"C\" {\n" 3843 " void f() {\n" 3844 " return;\n" 3845 " }\n" 3846 "} // namespace\n", 3847 ShortInlineFunctions); 3848 3849 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3850 EXPECT_EQ("namespace out {\n" 3851 "int i;\n" 3852 "namespace in {\n" 3853 " int i;\n" 3854 "} // namespace in\n" 3855 "} // namespace out", 3856 format("namespace out {\n" 3857 "int i;\n" 3858 "namespace in {\n" 3859 "int i;\n" 3860 "} // namespace in\n" 3861 "} // namespace out", 3862 Style)); 3863 3864 Style.NamespaceIndentation = FormatStyle::NI_None; 3865 verifyFormat("template <class T>\n" 3866 "concept a_concept = X<>;\n" 3867 "namespace B {\n" 3868 "struct b_struct {};\n" 3869 "} // namespace B\n", 3870 Style); 3871 verifyFormat("template <int I>\n" 3872 "constexpr void foo()\n" 3873 " requires(I == 42)\n" 3874 "{}\n" 3875 "namespace ns {\n" 3876 "void foo() {}\n" 3877 "} // namespace ns\n", 3878 Style); 3879 } 3880 3881 TEST_F(FormatTest, NamespaceMacros) { 3882 FormatStyle Style = getLLVMStyle(); 3883 Style.NamespaceMacros.push_back("TESTSUITE"); 3884 3885 verifyFormat("TESTSUITE(A) {\n" 3886 "int foo();\n" 3887 "} // TESTSUITE(A)", 3888 Style); 3889 3890 verifyFormat("TESTSUITE(A, B) {\n" 3891 "int foo();\n" 3892 "} // TESTSUITE(A)", 3893 Style); 3894 3895 // Properly indent according to NamespaceIndentation style 3896 Style.NamespaceIndentation = FormatStyle::NI_All; 3897 verifyFormat("TESTSUITE(A) {\n" 3898 " int foo();\n" 3899 "} // TESTSUITE(A)", 3900 Style); 3901 verifyFormat("TESTSUITE(A) {\n" 3902 " namespace B {\n" 3903 " int foo();\n" 3904 " } // namespace B\n" 3905 "} // TESTSUITE(A)", 3906 Style); 3907 verifyFormat("namespace A {\n" 3908 " TESTSUITE(B) {\n" 3909 " int foo();\n" 3910 " } // TESTSUITE(B)\n" 3911 "} // namespace A", 3912 Style); 3913 3914 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3915 verifyFormat("TESTSUITE(A) {\n" 3916 "TESTSUITE(B) {\n" 3917 " int foo();\n" 3918 "} // TESTSUITE(B)\n" 3919 "} // TESTSUITE(A)", 3920 Style); 3921 verifyFormat("TESTSUITE(A) {\n" 3922 "namespace B {\n" 3923 " int foo();\n" 3924 "} // namespace B\n" 3925 "} // TESTSUITE(A)", 3926 Style); 3927 verifyFormat("namespace A {\n" 3928 "TESTSUITE(B) {\n" 3929 " int foo();\n" 3930 "} // TESTSUITE(B)\n" 3931 "} // namespace A", 3932 Style); 3933 3934 // Properly merge namespace-macros blocks in CompactNamespaces mode 3935 Style.NamespaceIndentation = FormatStyle::NI_None; 3936 Style.CompactNamespaces = true; 3937 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3938 "}} // TESTSUITE(A::B)", 3939 Style); 3940 3941 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3942 "}} // TESTSUITE(out::in)", 3943 format("TESTSUITE(out) {\n" 3944 "TESTSUITE(in) {\n" 3945 "} // TESTSUITE(in)\n" 3946 "} // TESTSUITE(out)", 3947 Style)); 3948 3949 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3950 "}} // TESTSUITE(out::in)", 3951 format("TESTSUITE(out) {\n" 3952 "TESTSUITE(in) {\n" 3953 "} // TESTSUITE(in)\n" 3954 "} // TESTSUITE(out)", 3955 Style)); 3956 3957 // Do not merge different namespaces/macros 3958 EXPECT_EQ("namespace out {\n" 3959 "TESTSUITE(in) {\n" 3960 "} // TESTSUITE(in)\n" 3961 "} // namespace out", 3962 format("namespace out {\n" 3963 "TESTSUITE(in) {\n" 3964 "} // TESTSUITE(in)\n" 3965 "} // namespace out", 3966 Style)); 3967 EXPECT_EQ("TESTSUITE(out) {\n" 3968 "namespace in {\n" 3969 "} // namespace in\n" 3970 "} // TESTSUITE(out)", 3971 format("TESTSUITE(out) {\n" 3972 "namespace in {\n" 3973 "} // namespace in\n" 3974 "} // TESTSUITE(out)", 3975 Style)); 3976 Style.NamespaceMacros.push_back("FOOBAR"); 3977 EXPECT_EQ("TESTSUITE(out) {\n" 3978 "FOOBAR(in) {\n" 3979 "} // FOOBAR(in)\n" 3980 "} // TESTSUITE(out)", 3981 format("TESTSUITE(out) {\n" 3982 "FOOBAR(in) {\n" 3983 "} // FOOBAR(in)\n" 3984 "} // TESTSUITE(out)", 3985 Style)); 3986 } 3987 3988 TEST_F(FormatTest, FormatsCompactNamespaces) { 3989 FormatStyle Style = getLLVMStyle(); 3990 Style.CompactNamespaces = true; 3991 Style.NamespaceMacros.push_back("TESTSUITE"); 3992 3993 verifyFormat("namespace A { namespace B {\n" 3994 "}} // namespace A::B", 3995 Style); 3996 3997 EXPECT_EQ("namespace out { namespace in {\n" 3998 "}} // namespace out::in", 3999 format("namespace out {\n" 4000 "namespace in {\n" 4001 "} // namespace in\n" 4002 "} // namespace out", 4003 Style)); 4004 4005 // Only namespaces which have both consecutive opening and end get compacted 4006 EXPECT_EQ("namespace out {\n" 4007 "namespace in1 {\n" 4008 "} // namespace in1\n" 4009 "namespace in2 {\n" 4010 "} // namespace in2\n" 4011 "} // namespace out", 4012 format("namespace out {\n" 4013 "namespace in1 {\n" 4014 "} // namespace in1\n" 4015 "namespace in2 {\n" 4016 "} // namespace in2\n" 4017 "} // namespace out", 4018 Style)); 4019 4020 EXPECT_EQ("namespace out {\n" 4021 "int i;\n" 4022 "namespace in {\n" 4023 "int j;\n" 4024 "} // namespace in\n" 4025 "int k;\n" 4026 "} // namespace out", 4027 format("namespace out { int i;\n" 4028 "namespace in { int j; } // namespace in\n" 4029 "int k; } // namespace out", 4030 Style)); 4031 4032 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 4033 "}}} // namespace A::B::C\n", 4034 format("namespace A { namespace B {\n" 4035 "namespace C {\n" 4036 "}} // namespace B::C\n" 4037 "} // namespace A\n", 4038 Style)); 4039 4040 Style.ColumnLimit = 40; 4041 EXPECT_EQ("namespace aaaaaaaaaa {\n" 4042 "namespace bbbbbbbbbb {\n" 4043 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 4044 format("namespace aaaaaaaaaa {\n" 4045 "namespace bbbbbbbbbb {\n" 4046 "} // namespace bbbbbbbbbb\n" 4047 "} // namespace aaaaaaaaaa", 4048 Style)); 4049 4050 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 4051 "namespace cccccc {\n" 4052 "}}} // namespace aaaaaa::bbbbbb::cccccc", 4053 format("namespace aaaaaa {\n" 4054 "namespace bbbbbb {\n" 4055 "namespace cccccc {\n" 4056 "} // namespace cccccc\n" 4057 "} // namespace bbbbbb\n" 4058 "} // namespace aaaaaa", 4059 Style)); 4060 Style.ColumnLimit = 80; 4061 4062 // Extra semicolon after 'inner' closing brace prevents merging 4063 EXPECT_EQ("namespace out { namespace in {\n" 4064 "}; } // namespace out::in", 4065 format("namespace out {\n" 4066 "namespace in {\n" 4067 "}; // namespace in\n" 4068 "} // namespace out", 4069 Style)); 4070 4071 // Extra semicolon after 'outer' closing brace is conserved 4072 EXPECT_EQ("namespace out { namespace in {\n" 4073 "}}; // namespace out::in", 4074 format("namespace out {\n" 4075 "namespace in {\n" 4076 "} // namespace in\n" 4077 "}; // namespace out", 4078 Style)); 4079 4080 Style.NamespaceIndentation = FormatStyle::NI_All; 4081 EXPECT_EQ("namespace out { namespace in {\n" 4082 " int i;\n" 4083 "}} // namespace out::in", 4084 format("namespace out {\n" 4085 "namespace in {\n" 4086 "int i;\n" 4087 "} // namespace in\n" 4088 "} // namespace out", 4089 Style)); 4090 EXPECT_EQ("namespace out { namespace mid {\n" 4091 " namespace in {\n" 4092 " int j;\n" 4093 " } // namespace in\n" 4094 " int k;\n" 4095 "}} // namespace out::mid", 4096 format("namespace out { namespace mid {\n" 4097 "namespace in { int j; } // namespace in\n" 4098 "int k; }} // namespace out::mid", 4099 Style)); 4100 4101 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4102 EXPECT_EQ("namespace out { namespace in {\n" 4103 " int i;\n" 4104 "}} // namespace out::in", 4105 format("namespace out {\n" 4106 "namespace in {\n" 4107 "int i;\n" 4108 "} // namespace in\n" 4109 "} // namespace out", 4110 Style)); 4111 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 4112 " int i;\n" 4113 "}}} // namespace out::mid::in", 4114 format("namespace out {\n" 4115 "namespace mid {\n" 4116 "namespace in {\n" 4117 "int i;\n" 4118 "} // namespace in\n" 4119 "} // namespace mid\n" 4120 "} // namespace out", 4121 Style)); 4122 4123 Style.CompactNamespaces = true; 4124 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 4125 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4126 Style.BraceWrapping.BeforeLambdaBody = true; 4127 verifyFormat("namespace out { namespace in {\n" 4128 "}} // namespace out::in", 4129 Style); 4130 EXPECT_EQ("namespace out { namespace in {\n" 4131 "}} // namespace out::in", 4132 format("namespace out {\n" 4133 "namespace in {\n" 4134 "} // namespace in\n" 4135 "} // namespace out", 4136 Style)); 4137 } 4138 4139 TEST_F(FormatTest, FormatsExternC) { 4140 verifyFormat("extern \"C\" {\nint a;"); 4141 verifyFormat("extern \"C\" {}"); 4142 verifyFormat("extern \"C\" {\n" 4143 "int foo();\n" 4144 "}"); 4145 verifyFormat("extern \"C\" int foo() {}"); 4146 verifyFormat("extern \"C\" int foo();"); 4147 verifyFormat("extern \"C\" int foo() {\n" 4148 " int i = 42;\n" 4149 " return i;\n" 4150 "}"); 4151 4152 FormatStyle Style = getLLVMStyle(); 4153 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4154 Style.BraceWrapping.AfterFunction = true; 4155 verifyFormat("extern \"C\" int foo() {}", Style); 4156 verifyFormat("extern \"C\" int foo();", Style); 4157 verifyFormat("extern \"C\" int foo()\n" 4158 "{\n" 4159 " int i = 42;\n" 4160 " return i;\n" 4161 "}", 4162 Style); 4163 4164 Style.BraceWrapping.AfterExternBlock = true; 4165 Style.BraceWrapping.SplitEmptyRecord = false; 4166 verifyFormat("extern \"C\"\n" 4167 "{}", 4168 Style); 4169 verifyFormat("extern \"C\"\n" 4170 "{\n" 4171 " int foo();\n" 4172 "}", 4173 Style); 4174 } 4175 4176 TEST_F(FormatTest, IndentExternBlockStyle) { 4177 FormatStyle Style = getLLVMStyle(); 4178 Style.IndentWidth = 2; 4179 4180 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4181 verifyFormat("extern \"C\" { /*9*/\n" 4182 "}", 4183 Style); 4184 verifyFormat("extern \"C\" {\n" 4185 " int foo10();\n" 4186 "}", 4187 Style); 4188 4189 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4190 verifyFormat("extern \"C\" { /*11*/\n" 4191 "}", 4192 Style); 4193 verifyFormat("extern \"C\" {\n" 4194 "int foo12();\n" 4195 "}", 4196 Style); 4197 4198 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4199 Style.BraceWrapping.AfterExternBlock = true; 4200 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4201 verifyFormat("extern \"C\"\n" 4202 "{ /*13*/\n" 4203 "}", 4204 Style); 4205 verifyFormat("extern \"C\"\n{\n" 4206 " int foo14();\n" 4207 "}", 4208 Style); 4209 4210 Style.BraceWrapping.AfterExternBlock = false; 4211 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4212 verifyFormat("extern \"C\" { /*15*/\n" 4213 "}", 4214 Style); 4215 verifyFormat("extern \"C\" {\n" 4216 "int foo16();\n" 4217 "}", 4218 Style); 4219 4220 Style.BraceWrapping.AfterExternBlock = true; 4221 verifyFormat("extern \"C\"\n" 4222 "{ /*13*/\n" 4223 "}", 4224 Style); 4225 verifyFormat("extern \"C\"\n" 4226 "{\n" 4227 "int foo14();\n" 4228 "}", 4229 Style); 4230 4231 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4232 verifyFormat("extern \"C\"\n" 4233 "{ /*13*/\n" 4234 "}", 4235 Style); 4236 verifyFormat("extern \"C\"\n" 4237 "{\n" 4238 " int foo14();\n" 4239 "}", 4240 Style); 4241 } 4242 4243 TEST_F(FormatTest, FormatsInlineASM) { 4244 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 4245 verifyFormat("asm(\"nop\" ::: \"memory\");"); 4246 verifyFormat( 4247 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 4248 " \"cpuid\\n\\t\"\n" 4249 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 4250 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 4251 " : \"a\"(value));"); 4252 EXPECT_EQ( 4253 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4254 " __asm {\n" 4255 " mov edx,[that] // vtable in edx\n" 4256 " mov eax,methodIndex\n" 4257 " call [edx][eax*4] // stdcall\n" 4258 " }\n" 4259 "}", 4260 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4261 " __asm {\n" 4262 " mov edx,[that] // vtable in edx\n" 4263 " mov eax,methodIndex\n" 4264 " call [edx][eax*4] // stdcall\n" 4265 " }\n" 4266 "}")); 4267 EXPECT_EQ("_asm {\n" 4268 " xor eax, eax;\n" 4269 " cpuid;\n" 4270 "}", 4271 format("_asm {\n" 4272 " xor eax, eax;\n" 4273 " cpuid;\n" 4274 "}")); 4275 verifyFormat("void function() {\n" 4276 " // comment\n" 4277 " asm(\"\");\n" 4278 "}"); 4279 EXPECT_EQ("__asm {\n" 4280 "}\n" 4281 "int i;", 4282 format("__asm {\n" 4283 "}\n" 4284 "int i;")); 4285 } 4286 4287 TEST_F(FormatTest, FormatTryCatch) { 4288 verifyFormat("try {\n" 4289 " throw a * b;\n" 4290 "} catch (int a) {\n" 4291 " // Do nothing.\n" 4292 "} catch (...) {\n" 4293 " exit(42);\n" 4294 "}"); 4295 4296 // Function-level try statements. 4297 verifyFormat("int f() try { return 4; } catch (...) {\n" 4298 " return 5;\n" 4299 "}"); 4300 verifyFormat("class A {\n" 4301 " int a;\n" 4302 " A() try : a(0) {\n" 4303 " } catch (...) {\n" 4304 " throw;\n" 4305 " }\n" 4306 "};\n"); 4307 verifyFormat("class A {\n" 4308 " int a;\n" 4309 " A() try : a(0), b{1} {\n" 4310 " } catch (...) {\n" 4311 " throw;\n" 4312 " }\n" 4313 "};\n"); 4314 verifyFormat("class A {\n" 4315 " int a;\n" 4316 " A() try : a(0), b{1}, c{2} {\n" 4317 " } catch (...) {\n" 4318 " throw;\n" 4319 " }\n" 4320 "};\n"); 4321 verifyFormat("class A {\n" 4322 " int a;\n" 4323 " A() try : a(0), b{1}, c{2} {\n" 4324 " { // New scope.\n" 4325 " }\n" 4326 " } catch (...) {\n" 4327 " throw;\n" 4328 " }\n" 4329 "};\n"); 4330 4331 // Incomplete try-catch blocks. 4332 verifyIncompleteFormat("try {} catch ("); 4333 } 4334 4335 TEST_F(FormatTest, FormatTryAsAVariable) { 4336 verifyFormat("int try;"); 4337 verifyFormat("int try, size;"); 4338 verifyFormat("try = foo();"); 4339 verifyFormat("if (try < size) {\n return true;\n}"); 4340 4341 verifyFormat("int catch;"); 4342 verifyFormat("int catch, size;"); 4343 verifyFormat("catch = foo();"); 4344 verifyFormat("if (catch < size) {\n return true;\n}"); 4345 4346 FormatStyle Style = getLLVMStyle(); 4347 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4348 Style.BraceWrapping.AfterFunction = true; 4349 Style.BraceWrapping.BeforeCatch = true; 4350 verifyFormat("try {\n" 4351 " int bar = 1;\n" 4352 "}\n" 4353 "catch (...) {\n" 4354 " int bar = 1;\n" 4355 "}", 4356 Style); 4357 verifyFormat("#if NO_EX\n" 4358 "try\n" 4359 "#endif\n" 4360 "{\n" 4361 "}\n" 4362 "#if NO_EX\n" 4363 "catch (...) {\n" 4364 "}", 4365 Style); 4366 verifyFormat("try /* abc */ {\n" 4367 " int bar = 1;\n" 4368 "}\n" 4369 "catch (...) {\n" 4370 " int bar = 1;\n" 4371 "}", 4372 Style); 4373 verifyFormat("try\n" 4374 "// abc\n" 4375 "{\n" 4376 " int bar = 1;\n" 4377 "}\n" 4378 "catch (...) {\n" 4379 " int bar = 1;\n" 4380 "}", 4381 Style); 4382 } 4383 4384 TEST_F(FormatTest, FormatSEHTryCatch) { 4385 verifyFormat("__try {\n" 4386 " int a = b * c;\n" 4387 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 4388 " // Do nothing.\n" 4389 "}"); 4390 4391 verifyFormat("__try {\n" 4392 " int a = b * c;\n" 4393 "} __finally {\n" 4394 " // Do nothing.\n" 4395 "}"); 4396 4397 verifyFormat("DEBUG({\n" 4398 " __try {\n" 4399 " } __finally {\n" 4400 " }\n" 4401 "});\n"); 4402 } 4403 4404 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 4405 verifyFormat("try {\n" 4406 " f();\n" 4407 "} catch {\n" 4408 " g();\n" 4409 "}"); 4410 verifyFormat("try {\n" 4411 " f();\n" 4412 "} catch (A a) MACRO(x) {\n" 4413 " g();\n" 4414 "} catch (B b) MACRO(x) {\n" 4415 " g();\n" 4416 "}"); 4417 } 4418 4419 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 4420 FormatStyle Style = getLLVMStyle(); 4421 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 4422 FormatStyle::BS_WebKit}) { 4423 Style.BreakBeforeBraces = BraceStyle; 4424 verifyFormat("try {\n" 4425 " // something\n" 4426 "} catch (...) {\n" 4427 " // something\n" 4428 "}", 4429 Style); 4430 } 4431 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4432 verifyFormat("try {\n" 4433 " // something\n" 4434 "}\n" 4435 "catch (...) {\n" 4436 " // something\n" 4437 "}", 4438 Style); 4439 verifyFormat("__try {\n" 4440 " // something\n" 4441 "}\n" 4442 "__finally {\n" 4443 " // something\n" 4444 "}", 4445 Style); 4446 verifyFormat("@try {\n" 4447 " // something\n" 4448 "}\n" 4449 "@finally {\n" 4450 " // something\n" 4451 "}", 4452 Style); 4453 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4454 verifyFormat("try\n" 4455 "{\n" 4456 " // something\n" 4457 "}\n" 4458 "catch (...)\n" 4459 "{\n" 4460 " // something\n" 4461 "}", 4462 Style); 4463 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4464 verifyFormat("try\n" 4465 " {\n" 4466 " // something white\n" 4467 " }\n" 4468 "catch (...)\n" 4469 " {\n" 4470 " // something white\n" 4471 " }", 4472 Style); 4473 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4474 verifyFormat("try\n" 4475 " {\n" 4476 " // something\n" 4477 " }\n" 4478 "catch (...)\n" 4479 " {\n" 4480 " // something\n" 4481 " }", 4482 Style); 4483 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4484 Style.BraceWrapping.BeforeCatch = true; 4485 verifyFormat("try {\n" 4486 " // something\n" 4487 "}\n" 4488 "catch (...) {\n" 4489 " // something\n" 4490 "}", 4491 Style); 4492 } 4493 4494 TEST_F(FormatTest, StaticInitializers) { 4495 verifyFormat("static SomeClass SC = {1, 'a'};"); 4496 4497 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4498 " 100000000, " 4499 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4500 4501 // Here, everything other than the "}" would fit on a line. 4502 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4503 " 10000000000000000000000000};"); 4504 EXPECT_EQ("S s = {a,\n" 4505 "\n" 4506 " b};", 4507 format("S s = {\n" 4508 " a,\n" 4509 "\n" 4510 " b\n" 4511 "};")); 4512 4513 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4514 // line. However, the formatting looks a bit off and this probably doesn't 4515 // happen often in practice. 4516 verifyFormat("static int Variable[1] = {\n" 4517 " {1000000000000000000000000000000000000}};", 4518 getLLVMStyleWithColumns(40)); 4519 } 4520 4521 TEST_F(FormatTest, DesignatedInitializers) { 4522 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4523 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4524 " .bbbbbbbbbb = 2,\n" 4525 " .cccccccccc = 3,\n" 4526 " .dddddddddd = 4,\n" 4527 " .eeeeeeeeee = 5};"); 4528 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4529 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4530 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4531 " .ccccccccccccccccccccccccccc = 3,\n" 4532 " .ddddddddddddddddddddddddddd = 4,\n" 4533 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4534 4535 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4536 4537 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4538 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4539 " [2] = bbbbbbbbbb,\n" 4540 " [3] = cccccccccc,\n" 4541 " [4] = dddddddddd,\n" 4542 " [5] = eeeeeeeeee};"); 4543 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4544 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4545 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4546 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4547 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4548 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4549 } 4550 4551 TEST_F(FormatTest, NestedStaticInitializers) { 4552 verifyFormat("static A x = {{{}}};\n"); 4553 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4554 " {init1, init2, init3, init4}}};", 4555 getLLVMStyleWithColumns(50)); 4556 4557 verifyFormat("somes Status::global_reps[3] = {\n" 4558 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4559 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4560 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4561 getLLVMStyleWithColumns(60)); 4562 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4563 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4564 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4565 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4566 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4567 " {rect.fRight - rect.fLeft, rect.fBottom - " 4568 "rect.fTop}};"); 4569 4570 verifyFormat( 4571 "SomeArrayOfSomeType a = {\n" 4572 " {{1, 2, 3},\n" 4573 " {1, 2, 3},\n" 4574 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4575 " 333333333333333333333333333333},\n" 4576 " {1, 2, 3},\n" 4577 " {1, 2, 3}}};"); 4578 verifyFormat( 4579 "SomeArrayOfSomeType a = {\n" 4580 " {{1, 2, 3}},\n" 4581 " {{1, 2, 3}},\n" 4582 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4583 " 333333333333333333333333333333}},\n" 4584 " {{1, 2, 3}},\n" 4585 " {{1, 2, 3}}};"); 4586 4587 verifyFormat("struct {\n" 4588 " unsigned bit;\n" 4589 " const char *const name;\n" 4590 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4591 " {kOsWin, \"Windows\"},\n" 4592 " {kOsLinux, \"Linux\"},\n" 4593 " {kOsCrOS, \"Chrome OS\"}};"); 4594 verifyFormat("struct {\n" 4595 " unsigned bit;\n" 4596 " const char *const name;\n" 4597 "} kBitsToOs[] = {\n" 4598 " {kOsMac, \"Mac\"},\n" 4599 " {kOsWin, \"Windows\"},\n" 4600 " {kOsLinux, \"Linux\"},\n" 4601 " {kOsCrOS, \"Chrome OS\"},\n" 4602 "};"); 4603 } 4604 4605 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4606 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4607 " \\\n" 4608 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4609 } 4610 4611 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4612 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4613 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4614 4615 // Do break defaulted and deleted functions. 4616 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4617 " default;", 4618 getLLVMStyleWithColumns(40)); 4619 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4620 " delete;", 4621 getLLVMStyleWithColumns(40)); 4622 } 4623 4624 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4625 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4626 getLLVMStyleWithColumns(40)); 4627 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4628 getLLVMStyleWithColumns(40)); 4629 EXPECT_EQ("#define Q \\\n" 4630 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4631 " \"aaaaaaaa.cpp\"", 4632 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4633 getLLVMStyleWithColumns(40))); 4634 } 4635 4636 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4637 EXPECT_EQ("# 123 \"A string literal\"", 4638 format(" # 123 \"A string literal\"")); 4639 } 4640 4641 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4642 EXPECT_EQ("#;", format("#;")); 4643 verifyFormat("#\n;\n;\n;"); 4644 } 4645 4646 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4647 EXPECT_EQ("#line 42 \"test\"\n", 4648 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4649 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4650 getLLVMStyleWithColumns(12))); 4651 } 4652 4653 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4654 EXPECT_EQ("#line 42 \"test\"", 4655 format("# \\\n line \\\n 42 \\\n \"test\"")); 4656 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4657 } 4658 4659 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4660 verifyFormat("#define A \\x20"); 4661 verifyFormat("#define A \\ x20"); 4662 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4663 verifyFormat("#define A ''"); 4664 verifyFormat("#define A ''qqq"); 4665 verifyFormat("#define A `qqq"); 4666 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4667 EXPECT_EQ("const char *c = STRINGIFY(\n" 4668 "\\na : b);", 4669 format("const char * c = STRINGIFY(\n" 4670 "\\na : b);")); 4671 4672 verifyFormat("a\r\\"); 4673 verifyFormat("a\v\\"); 4674 verifyFormat("a\f\\"); 4675 } 4676 4677 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4678 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4679 style.IndentWidth = 4; 4680 style.PPIndentWidth = 1; 4681 4682 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4683 verifyFormat("#ifdef __linux__\n" 4684 "void foo() {\n" 4685 " int x = 0;\n" 4686 "}\n" 4687 "#define FOO\n" 4688 "#endif\n" 4689 "void bar() {\n" 4690 " int y = 0;\n" 4691 "}\n", 4692 style); 4693 4694 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4695 verifyFormat("#ifdef __linux__\n" 4696 "void foo() {\n" 4697 " int x = 0;\n" 4698 "}\n" 4699 "# define FOO foo\n" 4700 "#endif\n" 4701 "void bar() {\n" 4702 " int y = 0;\n" 4703 "}\n", 4704 style); 4705 4706 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4707 verifyFormat("#ifdef __linux__\n" 4708 "void foo() {\n" 4709 " int x = 0;\n" 4710 "}\n" 4711 " #define FOO foo\n" 4712 "#endif\n" 4713 "void bar() {\n" 4714 " int y = 0;\n" 4715 "}\n", 4716 style); 4717 } 4718 4719 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4720 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4721 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4722 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4723 // FIXME: We never break before the macro name. 4724 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4725 4726 verifyFormat("#define A A\n#define A A"); 4727 verifyFormat("#define A(X) A\n#define A A"); 4728 4729 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4730 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4731 } 4732 4733 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4734 EXPECT_EQ("// somecomment\n" 4735 "#include \"a.h\"\n" 4736 "#define A( \\\n" 4737 " A, B)\n" 4738 "#include \"b.h\"\n" 4739 "// somecomment\n", 4740 format(" // somecomment\n" 4741 " #include \"a.h\"\n" 4742 "#define A(A,\\\n" 4743 " B)\n" 4744 " #include \"b.h\"\n" 4745 " // somecomment\n", 4746 getLLVMStyleWithColumns(13))); 4747 } 4748 4749 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4750 4751 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4752 EXPECT_EQ("#define A \\\n" 4753 " c; \\\n" 4754 " e;\n" 4755 "f;", 4756 format("#define A c; e;\n" 4757 "f;", 4758 getLLVMStyleWithColumns(14))); 4759 } 4760 4761 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4762 4763 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4764 EXPECT_EQ("int x,\n" 4765 "#define A\n" 4766 " y;", 4767 format("int x,\n#define A\ny;")); 4768 } 4769 4770 TEST_F(FormatTest, HashInMacroDefinition) { 4771 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4772 EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle())); 4773 EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle())); 4774 EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle())); 4775 EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle())); 4776 EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle())); 4777 EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle())); 4778 EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle())); 4779 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4780 verifyFormat("#define A \\\n" 4781 " { \\\n" 4782 " f(#c); \\\n" 4783 " }", 4784 getLLVMStyleWithColumns(11)); 4785 4786 verifyFormat("#define A(X) \\\n" 4787 " void function##X()", 4788 getLLVMStyleWithColumns(22)); 4789 4790 verifyFormat("#define A(a, b, c) \\\n" 4791 " void a##b##c()", 4792 getLLVMStyleWithColumns(22)); 4793 4794 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4795 } 4796 4797 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4798 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4799 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4800 4801 FormatStyle Style = getLLVMStyle(); 4802 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4803 verifyFormat("#define true ((foo)1)", Style); 4804 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4805 verifyFormat("#define false((foo)0)", Style); 4806 } 4807 4808 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4809 EXPECT_EQ("#define A b;", format("#define A \\\n" 4810 " \\\n" 4811 " b;", 4812 getLLVMStyleWithColumns(25))); 4813 EXPECT_EQ("#define A \\\n" 4814 " \\\n" 4815 " a; \\\n" 4816 " b;", 4817 format("#define A \\\n" 4818 " \\\n" 4819 " a; \\\n" 4820 " b;", 4821 getLLVMStyleWithColumns(11))); 4822 EXPECT_EQ("#define A \\\n" 4823 " a; \\\n" 4824 " \\\n" 4825 " b;", 4826 format("#define A \\\n" 4827 " a; \\\n" 4828 " \\\n" 4829 " b;", 4830 getLLVMStyleWithColumns(11))); 4831 } 4832 4833 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4834 verifyIncompleteFormat("#define A :"); 4835 verifyFormat("#define SOMECASES \\\n" 4836 " case 1: \\\n" 4837 " case 2\n", 4838 getLLVMStyleWithColumns(20)); 4839 verifyFormat("#define MACRO(a) \\\n" 4840 " if (a) \\\n" 4841 " f(); \\\n" 4842 " else \\\n" 4843 " g()", 4844 getLLVMStyleWithColumns(18)); 4845 verifyFormat("#define A template <typename T>"); 4846 verifyIncompleteFormat("#define STR(x) #x\n" 4847 "f(STR(this_is_a_string_literal{));"); 4848 verifyFormat("#pragma omp threadprivate( \\\n" 4849 " y)), // expected-warning", 4850 getLLVMStyleWithColumns(28)); 4851 verifyFormat("#d, = };"); 4852 verifyFormat("#if \"a"); 4853 verifyIncompleteFormat("({\n" 4854 "#define b \\\n" 4855 " } \\\n" 4856 " a\n" 4857 "a", 4858 getLLVMStyleWithColumns(15)); 4859 verifyFormat("#define A \\\n" 4860 " { \\\n" 4861 " {\n" 4862 "#define B \\\n" 4863 " } \\\n" 4864 " }", 4865 getLLVMStyleWithColumns(15)); 4866 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4867 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4868 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4869 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4870 } 4871 4872 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4873 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4874 EXPECT_EQ("class A : public QObject {\n" 4875 " Q_OBJECT\n" 4876 "\n" 4877 " A() {}\n" 4878 "};", 4879 format("class A : public QObject {\n" 4880 " Q_OBJECT\n" 4881 "\n" 4882 " A() {\n}\n" 4883 "} ;")); 4884 EXPECT_EQ("MACRO\n" 4885 "/*static*/ int i;", 4886 format("MACRO\n" 4887 " /*static*/ int i;")); 4888 EXPECT_EQ("SOME_MACRO\n" 4889 "namespace {\n" 4890 "void f();\n" 4891 "} // namespace", 4892 format("SOME_MACRO\n" 4893 " namespace {\n" 4894 "void f( );\n" 4895 "} // namespace")); 4896 // Only if the identifier contains at least 5 characters. 4897 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4898 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4899 // Only if everything is upper case. 4900 EXPECT_EQ("class A : public QObject {\n" 4901 " Q_Object A() {}\n" 4902 "};", 4903 format("class A : public QObject {\n" 4904 " Q_Object\n" 4905 " A() {\n}\n" 4906 "} ;")); 4907 4908 // Only if the next line can actually start an unwrapped line. 4909 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4910 format("SOME_WEIRD_LOG_MACRO\n" 4911 "<< SomeThing;")); 4912 4913 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4914 "(n, buffers))\n", 4915 getChromiumStyle(FormatStyle::LK_Cpp)); 4916 4917 // See PR41483 4918 EXPECT_EQ("/**/ FOO(a)\n" 4919 "FOO(b)", 4920 format("/**/ FOO(a)\n" 4921 "FOO(b)")); 4922 } 4923 4924 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4925 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4926 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4927 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4928 "class X {};\n" 4929 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4930 "int *createScopDetectionPass() { return 0; }", 4931 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4932 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4933 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4934 " class X {};\n" 4935 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4936 " int *createScopDetectionPass() { return 0; }")); 4937 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4938 // braces, so that inner block is indented one level more. 4939 EXPECT_EQ("int q() {\n" 4940 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4941 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4942 " IPC_END_MESSAGE_MAP()\n" 4943 "}", 4944 format("int q() {\n" 4945 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4946 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4947 " IPC_END_MESSAGE_MAP()\n" 4948 "}")); 4949 4950 // Same inside macros. 4951 EXPECT_EQ("#define LIST(L) \\\n" 4952 " L(A) \\\n" 4953 " L(B) \\\n" 4954 " L(C)", 4955 format("#define LIST(L) \\\n" 4956 " L(A) \\\n" 4957 " L(B) \\\n" 4958 " L(C)", 4959 getGoogleStyle())); 4960 4961 // These must not be recognized as macros. 4962 EXPECT_EQ("int q() {\n" 4963 " f(x);\n" 4964 " f(x) {}\n" 4965 " f(x)->g();\n" 4966 " f(x)->*g();\n" 4967 " f(x).g();\n" 4968 " f(x) = x;\n" 4969 " f(x) += x;\n" 4970 " f(x) -= x;\n" 4971 " f(x) *= x;\n" 4972 " f(x) /= x;\n" 4973 " f(x) %= x;\n" 4974 " f(x) &= x;\n" 4975 " f(x) |= x;\n" 4976 " f(x) ^= x;\n" 4977 " f(x) >>= x;\n" 4978 " f(x) <<= x;\n" 4979 " f(x)[y].z();\n" 4980 " LOG(INFO) << x;\n" 4981 " ifstream(x) >> x;\n" 4982 "}\n", 4983 format("int q() {\n" 4984 " f(x)\n;\n" 4985 " f(x)\n {}\n" 4986 " f(x)\n->g();\n" 4987 " f(x)\n->*g();\n" 4988 " f(x)\n.g();\n" 4989 " f(x)\n = x;\n" 4990 " f(x)\n += x;\n" 4991 " f(x)\n -= x;\n" 4992 " f(x)\n *= x;\n" 4993 " f(x)\n /= x;\n" 4994 " f(x)\n %= x;\n" 4995 " f(x)\n &= x;\n" 4996 " f(x)\n |= x;\n" 4997 " f(x)\n ^= x;\n" 4998 " f(x)\n >>= x;\n" 4999 " f(x)\n <<= x;\n" 5000 " f(x)\n[y].z();\n" 5001 " LOG(INFO)\n << x;\n" 5002 " ifstream(x)\n >> x;\n" 5003 "}\n")); 5004 EXPECT_EQ("int q() {\n" 5005 " F(x)\n" 5006 " if (1) {\n" 5007 " }\n" 5008 " F(x)\n" 5009 " while (1) {\n" 5010 " }\n" 5011 " F(x)\n" 5012 " G(x);\n" 5013 " F(x)\n" 5014 " try {\n" 5015 " Q();\n" 5016 " } catch (...) {\n" 5017 " }\n" 5018 "}\n", 5019 format("int q() {\n" 5020 "F(x)\n" 5021 "if (1) {}\n" 5022 "F(x)\n" 5023 "while (1) {}\n" 5024 "F(x)\n" 5025 "G(x);\n" 5026 "F(x)\n" 5027 "try { Q(); } catch (...) {}\n" 5028 "}\n")); 5029 EXPECT_EQ("class A {\n" 5030 " A() : t(0) {}\n" 5031 " A(int i) noexcept() : {}\n" 5032 " A(X x)\n" // FIXME: function-level try blocks are broken. 5033 " try : t(0) {\n" 5034 " } catch (...) {\n" 5035 " }\n" 5036 "};", 5037 format("class A {\n" 5038 " A()\n : t(0) {}\n" 5039 " A(int i)\n noexcept() : {}\n" 5040 " A(X x)\n" 5041 " try : t(0) {} catch (...) {}\n" 5042 "};")); 5043 FormatStyle Style = getLLVMStyle(); 5044 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5045 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5046 Style.BraceWrapping.AfterFunction = true; 5047 EXPECT_EQ("void f()\n" 5048 "try\n" 5049 "{\n" 5050 "}", 5051 format("void f() try {\n" 5052 "}", 5053 Style)); 5054 EXPECT_EQ("class SomeClass {\n" 5055 "public:\n" 5056 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5057 "};", 5058 format("class SomeClass {\n" 5059 "public:\n" 5060 " SomeClass()\n" 5061 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5062 "};")); 5063 EXPECT_EQ("class SomeClass {\n" 5064 "public:\n" 5065 " SomeClass()\n" 5066 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5067 "};", 5068 format("class SomeClass {\n" 5069 "public:\n" 5070 " SomeClass()\n" 5071 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5072 "};", 5073 getLLVMStyleWithColumns(40))); 5074 5075 verifyFormat("MACRO(>)"); 5076 5077 // Some macros contain an implicit semicolon. 5078 Style = getLLVMStyle(); 5079 Style.StatementMacros.push_back("FOO"); 5080 verifyFormat("FOO(a) int b = 0;"); 5081 verifyFormat("FOO(a)\n" 5082 "int b = 0;", 5083 Style); 5084 verifyFormat("FOO(a);\n" 5085 "int b = 0;", 5086 Style); 5087 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 5088 "int b = 0;", 5089 Style); 5090 verifyFormat("FOO()\n" 5091 "int b = 0;", 5092 Style); 5093 verifyFormat("FOO\n" 5094 "int b = 0;", 5095 Style); 5096 verifyFormat("void f() {\n" 5097 " FOO(a)\n" 5098 " return a;\n" 5099 "}", 5100 Style); 5101 verifyFormat("FOO(a)\n" 5102 "FOO(b)", 5103 Style); 5104 verifyFormat("int a = 0;\n" 5105 "FOO(b)\n" 5106 "int c = 0;", 5107 Style); 5108 verifyFormat("int a = 0;\n" 5109 "int x = FOO(a)\n" 5110 "int b = 0;", 5111 Style); 5112 verifyFormat("void foo(int a) { FOO(a) }\n" 5113 "uint32_t bar() {}", 5114 Style); 5115 } 5116 5117 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) { 5118 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 5119 5120 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()", 5121 ZeroColumn); 5122 } 5123 5124 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 5125 verifyFormat("#define A \\\n" 5126 " f({ \\\n" 5127 " g(); \\\n" 5128 " });", 5129 getLLVMStyleWithColumns(11)); 5130 } 5131 5132 TEST_F(FormatTest, IndentPreprocessorDirectives) { 5133 FormatStyle Style = getLLVMStyleWithColumns(40); 5134 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 5135 verifyFormat("#ifdef _WIN32\n" 5136 "#define A 0\n" 5137 "#ifdef VAR2\n" 5138 "#define B 1\n" 5139 "#include <someheader.h>\n" 5140 "#define MACRO \\\n" 5141 " some_very_long_func_aaaaaaaaaa();\n" 5142 "#endif\n" 5143 "#else\n" 5144 "#define A 1\n" 5145 "#endif", 5146 Style); 5147 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 5148 verifyFormat("#ifdef _WIN32\n" 5149 "# define A 0\n" 5150 "# ifdef VAR2\n" 5151 "# define B 1\n" 5152 "# include <someheader.h>\n" 5153 "# define MACRO \\\n" 5154 " some_very_long_func_aaaaaaaaaa();\n" 5155 "# endif\n" 5156 "#else\n" 5157 "# define A 1\n" 5158 "#endif", 5159 Style); 5160 verifyFormat("#if A\n" 5161 "# define MACRO \\\n" 5162 " void a(int x) { \\\n" 5163 " b(); \\\n" 5164 " c(); \\\n" 5165 " d(); \\\n" 5166 " e(); \\\n" 5167 " f(); \\\n" 5168 " }\n" 5169 "#endif", 5170 Style); 5171 // Comments before include guard. 5172 verifyFormat("// file comment\n" 5173 "// file comment\n" 5174 "#ifndef HEADER_H\n" 5175 "#define HEADER_H\n" 5176 "code();\n" 5177 "#endif", 5178 Style); 5179 // Test with include guards. 5180 verifyFormat("#ifndef HEADER_H\n" 5181 "#define HEADER_H\n" 5182 "code();\n" 5183 "#endif", 5184 Style); 5185 // Include guards must have a #define with the same variable immediately 5186 // after #ifndef. 5187 verifyFormat("#ifndef NOT_GUARD\n" 5188 "# define FOO\n" 5189 "code();\n" 5190 "#endif", 5191 Style); 5192 5193 // Include guards must cover the entire file. 5194 verifyFormat("code();\n" 5195 "code();\n" 5196 "#ifndef NOT_GUARD\n" 5197 "# define NOT_GUARD\n" 5198 "code();\n" 5199 "#endif", 5200 Style); 5201 verifyFormat("#ifndef NOT_GUARD\n" 5202 "# define NOT_GUARD\n" 5203 "code();\n" 5204 "#endif\n" 5205 "code();", 5206 Style); 5207 // Test with trailing blank lines. 5208 verifyFormat("#ifndef HEADER_H\n" 5209 "#define HEADER_H\n" 5210 "code();\n" 5211 "#endif\n", 5212 Style); 5213 // Include guards don't have #else. 5214 verifyFormat("#ifndef NOT_GUARD\n" 5215 "# define NOT_GUARD\n" 5216 "code();\n" 5217 "#else\n" 5218 "#endif", 5219 Style); 5220 verifyFormat("#ifndef NOT_GUARD\n" 5221 "# define NOT_GUARD\n" 5222 "code();\n" 5223 "#elif FOO\n" 5224 "#endif", 5225 Style); 5226 // Non-identifier #define after potential include guard. 5227 verifyFormat("#ifndef FOO\n" 5228 "# define 1\n" 5229 "#endif\n", 5230 Style); 5231 // #if closes past last non-preprocessor line. 5232 verifyFormat("#ifndef FOO\n" 5233 "#define FOO\n" 5234 "#if 1\n" 5235 "int i;\n" 5236 "# define A 0\n" 5237 "#endif\n" 5238 "#endif\n", 5239 Style); 5240 // Don't crash if there is an #elif directive without a condition. 5241 verifyFormat("#if 1\n" 5242 "int x;\n" 5243 "#elif\n" 5244 "int y;\n" 5245 "#else\n" 5246 "int z;\n" 5247 "#endif", 5248 Style); 5249 // FIXME: This doesn't handle the case where there's code between the 5250 // #ifndef and #define but all other conditions hold. This is because when 5251 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 5252 // previous code line yet, so we can't detect it. 5253 EXPECT_EQ("#ifndef NOT_GUARD\n" 5254 "code();\n" 5255 "#define NOT_GUARD\n" 5256 "code();\n" 5257 "#endif", 5258 format("#ifndef NOT_GUARD\n" 5259 "code();\n" 5260 "# define NOT_GUARD\n" 5261 "code();\n" 5262 "#endif", 5263 Style)); 5264 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 5265 // be outside an include guard. Examples are #pragma once and 5266 // #pragma GCC diagnostic, or anything else that does not change the meaning 5267 // of the file if it's included multiple times. 5268 EXPECT_EQ("#ifdef WIN32\n" 5269 "# pragma once\n" 5270 "#endif\n" 5271 "#ifndef HEADER_H\n" 5272 "# define HEADER_H\n" 5273 "code();\n" 5274 "#endif", 5275 format("#ifdef WIN32\n" 5276 "# pragma once\n" 5277 "#endif\n" 5278 "#ifndef HEADER_H\n" 5279 "#define HEADER_H\n" 5280 "code();\n" 5281 "#endif", 5282 Style)); 5283 // FIXME: This does not detect when there is a single non-preprocessor line 5284 // in front of an include-guard-like structure where other conditions hold 5285 // because ScopedLineState hides the line. 5286 EXPECT_EQ("code();\n" 5287 "#ifndef HEADER_H\n" 5288 "#define HEADER_H\n" 5289 "code();\n" 5290 "#endif", 5291 format("code();\n" 5292 "#ifndef HEADER_H\n" 5293 "# define HEADER_H\n" 5294 "code();\n" 5295 "#endif", 5296 Style)); 5297 // Keep comments aligned with #, otherwise indent comments normally. These 5298 // tests cannot use verifyFormat because messUp manipulates leading 5299 // whitespace. 5300 { 5301 const char *Expected = "" 5302 "void f() {\n" 5303 "#if 1\n" 5304 "// Preprocessor aligned.\n" 5305 "# define A 0\n" 5306 " // Code. Separated by blank line.\n" 5307 "\n" 5308 "# define B 0\n" 5309 " // Code. Not aligned with #\n" 5310 "# define C 0\n" 5311 "#endif"; 5312 const char *ToFormat = "" 5313 "void f() {\n" 5314 "#if 1\n" 5315 "// Preprocessor aligned.\n" 5316 "# define A 0\n" 5317 "// Code. Separated by blank line.\n" 5318 "\n" 5319 "# define B 0\n" 5320 " // Code. Not aligned with #\n" 5321 "# define C 0\n" 5322 "#endif"; 5323 EXPECT_EQ(Expected, format(ToFormat, Style)); 5324 EXPECT_EQ(Expected, format(Expected, Style)); 5325 } 5326 // Keep block quotes aligned. 5327 { 5328 const char *Expected = "" 5329 "void f() {\n" 5330 "#if 1\n" 5331 "/* Preprocessor aligned. */\n" 5332 "# define A 0\n" 5333 " /* Code. Separated by blank line. */\n" 5334 "\n" 5335 "# define B 0\n" 5336 " /* Code. Not aligned with # */\n" 5337 "# define C 0\n" 5338 "#endif"; 5339 const char *ToFormat = "" 5340 "void f() {\n" 5341 "#if 1\n" 5342 "/* Preprocessor aligned. */\n" 5343 "# define A 0\n" 5344 "/* Code. Separated by blank line. */\n" 5345 "\n" 5346 "# define B 0\n" 5347 " /* Code. Not aligned with # */\n" 5348 "# define C 0\n" 5349 "#endif"; 5350 EXPECT_EQ(Expected, format(ToFormat, Style)); 5351 EXPECT_EQ(Expected, format(Expected, Style)); 5352 } 5353 // Keep comments aligned with un-indented directives. 5354 { 5355 const char *Expected = "" 5356 "void f() {\n" 5357 "// Preprocessor aligned.\n" 5358 "#define A 0\n" 5359 " // Code. Separated by blank line.\n" 5360 "\n" 5361 "#define B 0\n" 5362 " // Code. Not aligned with #\n" 5363 "#define C 0\n"; 5364 const char *ToFormat = "" 5365 "void f() {\n" 5366 "// Preprocessor aligned.\n" 5367 "#define A 0\n" 5368 "// Code. Separated by blank line.\n" 5369 "\n" 5370 "#define B 0\n" 5371 " // Code. Not aligned with #\n" 5372 "#define C 0\n"; 5373 EXPECT_EQ(Expected, format(ToFormat, Style)); 5374 EXPECT_EQ(Expected, format(Expected, Style)); 5375 } 5376 // Test AfterHash with tabs. 5377 { 5378 FormatStyle Tabbed = Style; 5379 Tabbed.UseTab = FormatStyle::UT_Always; 5380 Tabbed.IndentWidth = 8; 5381 Tabbed.TabWidth = 8; 5382 verifyFormat("#ifdef _WIN32\n" 5383 "#\tdefine A 0\n" 5384 "#\tifdef VAR2\n" 5385 "#\t\tdefine B 1\n" 5386 "#\t\tinclude <someheader.h>\n" 5387 "#\t\tdefine MACRO \\\n" 5388 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 5389 "#\tendif\n" 5390 "#else\n" 5391 "#\tdefine A 1\n" 5392 "#endif", 5393 Tabbed); 5394 } 5395 5396 // Regression test: Multiline-macro inside include guards. 5397 verifyFormat("#ifndef HEADER_H\n" 5398 "#define HEADER_H\n" 5399 "#define A() \\\n" 5400 " int i; \\\n" 5401 " int j;\n" 5402 "#endif // HEADER_H", 5403 getLLVMStyleWithColumns(20)); 5404 5405 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5406 // Basic before hash indent tests 5407 verifyFormat("#ifdef _WIN32\n" 5408 " #define A 0\n" 5409 " #ifdef VAR2\n" 5410 " #define B 1\n" 5411 " #include <someheader.h>\n" 5412 " #define MACRO \\\n" 5413 " some_very_long_func_aaaaaaaaaa();\n" 5414 " #endif\n" 5415 "#else\n" 5416 " #define A 1\n" 5417 "#endif", 5418 Style); 5419 verifyFormat("#if A\n" 5420 " #define MACRO \\\n" 5421 " void a(int x) { \\\n" 5422 " b(); \\\n" 5423 " c(); \\\n" 5424 " d(); \\\n" 5425 " e(); \\\n" 5426 " f(); \\\n" 5427 " }\n" 5428 "#endif", 5429 Style); 5430 // Keep comments aligned with indented directives. These 5431 // tests cannot use verifyFormat because messUp manipulates leading 5432 // whitespace. 5433 { 5434 const char *Expected = "void f() {\n" 5435 "// Aligned to preprocessor.\n" 5436 "#if 1\n" 5437 " // Aligned to code.\n" 5438 " int a;\n" 5439 " #if 1\n" 5440 " // Aligned to preprocessor.\n" 5441 " #define A 0\n" 5442 " // Aligned to code.\n" 5443 " int b;\n" 5444 " #endif\n" 5445 "#endif\n" 5446 "}"; 5447 const char *ToFormat = "void f() {\n" 5448 "// Aligned to preprocessor.\n" 5449 "#if 1\n" 5450 "// Aligned to code.\n" 5451 "int a;\n" 5452 "#if 1\n" 5453 "// Aligned to preprocessor.\n" 5454 "#define A 0\n" 5455 "// Aligned to code.\n" 5456 "int b;\n" 5457 "#endif\n" 5458 "#endif\n" 5459 "}"; 5460 EXPECT_EQ(Expected, format(ToFormat, Style)); 5461 EXPECT_EQ(Expected, format(Expected, Style)); 5462 } 5463 { 5464 const char *Expected = "void f() {\n" 5465 "/* Aligned to preprocessor. */\n" 5466 "#if 1\n" 5467 " /* Aligned to code. */\n" 5468 " int a;\n" 5469 " #if 1\n" 5470 " /* Aligned to preprocessor. */\n" 5471 " #define A 0\n" 5472 " /* Aligned to code. */\n" 5473 " int b;\n" 5474 " #endif\n" 5475 "#endif\n" 5476 "}"; 5477 const char *ToFormat = "void f() {\n" 5478 "/* Aligned to preprocessor. */\n" 5479 "#if 1\n" 5480 "/* Aligned to code. */\n" 5481 "int a;\n" 5482 "#if 1\n" 5483 "/* Aligned to preprocessor. */\n" 5484 "#define A 0\n" 5485 "/* Aligned to code. */\n" 5486 "int b;\n" 5487 "#endif\n" 5488 "#endif\n" 5489 "}"; 5490 EXPECT_EQ(Expected, format(ToFormat, Style)); 5491 EXPECT_EQ(Expected, format(Expected, Style)); 5492 } 5493 5494 // Test single comment before preprocessor 5495 verifyFormat("// Comment\n" 5496 "\n" 5497 "#if 1\n" 5498 "#endif", 5499 Style); 5500 } 5501 5502 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5503 verifyFormat("{\n { a #c; }\n}"); 5504 } 5505 5506 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5507 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5508 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5509 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5510 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5511 } 5512 5513 TEST_F(FormatTest, EscapedNewlines) { 5514 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5515 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5516 format("#define A \\\nint i;\\\n int j;", Narrow)); 5517 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5518 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5519 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5520 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5521 5522 FormatStyle AlignLeft = getLLVMStyle(); 5523 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5524 EXPECT_EQ("#define MACRO(x) \\\n" 5525 "private: \\\n" 5526 " int x(int a);\n", 5527 format("#define MACRO(x) \\\n" 5528 "private: \\\n" 5529 " int x(int a);\n", 5530 AlignLeft)); 5531 5532 // CRLF line endings 5533 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5534 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5535 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5536 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5537 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5538 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5539 EXPECT_EQ("#define MACRO(x) \\\r\n" 5540 "private: \\\r\n" 5541 " int x(int a);\r\n", 5542 format("#define MACRO(x) \\\r\n" 5543 "private: \\\r\n" 5544 " int x(int a);\r\n", 5545 AlignLeft)); 5546 5547 FormatStyle DontAlign = getLLVMStyle(); 5548 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5549 DontAlign.MaxEmptyLinesToKeep = 3; 5550 // FIXME: can't use verifyFormat here because the newline before 5551 // "public:" is not inserted the first time it's reformatted 5552 EXPECT_EQ("#define A \\\n" 5553 " class Foo { \\\n" 5554 " void bar(); \\\n" 5555 "\\\n" 5556 "\\\n" 5557 "\\\n" 5558 " public: \\\n" 5559 " void baz(); \\\n" 5560 " };", 5561 format("#define A \\\n" 5562 " class Foo { \\\n" 5563 " void bar(); \\\n" 5564 "\\\n" 5565 "\\\n" 5566 "\\\n" 5567 " public: \\\n" 5568 " void baz(); \\\n" 5569 " };", 5570 DontAlign)); 5571 } 5572 5573 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5574 verifyFormat("#define A \\\n" 5575 " int v( \\\n" 5576 " a); \\\n" 5577 " int i;", 5578 getLLVMStyleWithColumns(11)); 5579 } 5580 5581 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5582 EXPECT_EQ( 5583 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5584 " \\\n" 5585 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5586 "\n" 5587 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5588 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5589 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5590 "\\\n" 5591 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5592 " \n" 5593 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5594 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5595 } 5596 5597 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5598 EXPECT_EQ("int\n" 5599 "#define A\n" 5600 " a;", 5601 format("int\n#define A\na;")); 5602 verifyFormat("functionCallTo(\n" 5603 " someOtherFunction(\n" 5604 " withSomeParameters, whichInSequence,\n" 5605 " areLongerThanALine(andAnotherCall,\n" 5606 "#define A B\n" 5607 " withMoreParamters,\n" 5608 " whichStronglyInfluenceTheLayout),\n" 5609 " andMoreParameters),\n" 5610 " trailing);", 5611 getLLVMStyleWithColumns(69)); 5612 verifyFormat("Foo::Foo()\n" 5613 "#ifdef BAR\n" 5614 " : baz(0)\n" 5615 "#endif\n" 5616 "{\n" 5617 "}"); 5618 verifyFormat("void f() {\n" 5619 " if (true)\n" 5620 "#ifdef A\n" 5621 " f(42);\n" 5622 " x();\n" 5623 "#else\n" 5624 " g();\n" 5625 " x();\n" 5626 "#endif\n" 5627 "}"); 5628 verifyFormat("void f(param1, param2,\n" 5629 " param3,\n" 5630 "#ifdef A\n" 5631 " param4(param5,\n" 5632 "#ifdef A1\n" 5633 " param6,\n" 5634 "#ifdef A2\n" 5635 " param7),\n" 5636 "#else\n" 5637 " param8),\n" 5638 " param9,\n" 5639 "#endif\n" 5640 " param10,\n" 5641 "#endif\n" 5642 " param11)\n" 5643 "#else\n" 5644 " param12)\n" 5645 "#endif\n" 5646 "{\n" 5647 " x();\n" 5648 "}", 5649 getLLVMStyleWithColumns(28)); 5650 verifyFormat("#if 1\n" 5651 "int i;"); 5652 verifyFormat("#if 1\n" 5653 "#endif\n" 5654 "#if 1\n" 5655 "#else\n" 5656 "#endif\n"); 5657 verifyFormat("DEBUG({\n" 5658 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5659 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5660 "});\n" 5661 "#if a\n" 5662 "#else\n" 5663 "#endif"); 5664 5665 verifyIncompleteFormat("void f(\n" 5666 "#if A\n" 5667 ");\n" 5668 "#else\n" 5669 "#endif"); 5670 } 5671 5672 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5673 verifyFormat("#endif\n" 5674 "#if B"); 5675 } 5676 5677 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5678 FormatStyle SingleLine = getLLVMStyle(); 5679 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5680 verifyFormat("#if 0\n" 5681 "#elif 1\n" 5682 "#endif\n" 5683 "void foo() {\n" 5684 " if (test) foo2();\n" 5685 "}", 5686 SingleLine); 5687 } 5688 5689 TEST_F(FormatTest, LayoutBlockInsideParens) { 5690 verifyFormat("functionCall({ int i; });"); 5691 verifyFormat("functionCall({\n" 5692 " int i;\n" 5693 " int j;\n" 5694 "});"); 5695 verifyFormat("functionCall(\n" 5696 " {\n" 5697 " int i;\n" 5698 " int j;\n" 5699 " },\n" 5700 " aaaa, bbbb, cccc);"); 5701 verifyFormat("functionA(functionB({\n" 5702 " int i;\n" 5703 " int j;\n" 5704 " }),\n" 5705 " aaaa, bbbb, cccc);"); 5706 verifyFormat("functionCall(\n" 5707 " {\n" 5708 " int i;\n" 5709 " int j;\n" 5710 " },\n" 5711 " aaaa, bbbb, // comment\n" 5712 " cccc);"); 5713 verifyFormat("functionA(functionB({\n" 5714 " int i;\n" 5715 " int j;\n" 5716 " }),\n" 5717 " aaaa, bbbb, // comment\n" 5718 " cccc);"); 5719 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5720 verifyFormat("functionCall(aaaa, bbbb, {\n" 5721 " int i;\n" 5722 " int j;\n" 5723 "});"); 5724 verifyFormat( 5725 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5726 " {\n" 5727 " int i; // break\n" 5728 " },\n" 5729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5730 " ccccccccccccccccc));"); 5731 verifyFormat("DEBUG({\n" 5732 " if (a)\n" 5733 " f();\n" 5734 "});"); 5735 } 5736 5737 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5738 EXPECT_EQ("SOME_MACRO { int i; }\n" 5739 "int i;", 5740 format(" SOME_MACRO {int i;} int i;")); 5741 } 5742 5743 TEST_F(FormatTest, LayoutNestedBlocks) { 5744 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5745 " struct s {\n" 5746 " int i;\n" 5747 " };\n" 5748 " s kBitsToOs[] = {{10}};\n" 5749 " for (int i = 0; i < 10; ++i)\n" 5750 " return;\n" 5751 "}"); 5752 verifyFormat("call(parameter, {\n" 5753 " something();\n" 5754 " // Comment using all columns.\n" 5755 " somethingelse();\n" 5756 "});", 5757 getLLVMStyleWithColumns(40)); 5758 verifyFormat("DEBUG( //\n" 5759 " { f(); }, a);"); 5760 verifyFormat("DEBUG( //\n" 5761 " {\n" 5762 " f(); //\n" 5763 " },\n" 5764 " a);"); 5765 5766 EXPECT_EQ("call(parameter, {\n" 5767 " something();\n" 5768 " // Comment too\n" 5769 " // looooooooooong.\n" 5770 " somethingElse();\n" 5771 "});", 5772 format("call(parameter, {\n" 5773 " something();\n" 5774 " // Comment too looooooooooong.\n" 5775 " somethingElse();\n" 5776 "});", 5777 getLLVMStyleWithColumns(29))); 5778 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5779 EXPECT_EQ("DEBUG({ // comment\n" 5780 " int i;\n" 5781 "});", 5782 format("DEBUG({ // comment\n" 5783 "int i;\n" 5784 "});")); 5785 EXPECT_EQ("DEBUG({\n" 5786 " int i;\n" 5787 "\n" 5788 " // comment\n" 5789 " int j;\n" 5790 "});", 5791 format("DEBUG({\n" 5792 " int i;\n" 5793 "\n" 5794 " // comment\n" 5795 " int j;\n" 5796 "});")); 5797 5798 verifyFormat("DEBUG({\n" 5799 " if (a)\n" 5800 " return;\n" 5801 "});"); 5802 verifyGoogleFormat("DEBUG({\n" 5803 " if (a) return;\n" 5804 "});"); 5805 FormatStyle Style = getGoogleStyle(); 5806 Style.ColumnLimit = 45; 5807 verifyFormat("Debug(\n" 5808 " aaaaa,\n" 5809 " {\n" 5810 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5811 " },\n" 5812 " a);", 5813 Style); 5814 5815 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5816 5817 verifyNoCrash("^{v^{a}}"); 5818 } 5819 5820 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5821 EXPECT_EQ("#define MACRO() \\\n" 5822 " Debug(aaa, /* force line break */ \\\n" 5823 " { \\\n" 5824 " int i; \\\n" 5825 " int j; \\\n" 5826 " })", 5827 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5828 " { int i; int j; })", 5829 getGoogleStyle())); 5830 5831 EXPECT_EQ("#define A \\\n" 5832 " [] { \\\n" 5833 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5834 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5835 " }", 5836 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5837 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5838 getGoogleStyle())); 5839 } 5840 5841 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5842 EXPECT_EQ("{}", format("{}")); 5843 verifyFormat("enum E {};"); 5844 verifyFormat("enum E {}"); 5845 FormatStyle Style = getLLVMStyle(); 5846 Style.SpaceInEmptyBlock = true; 5847 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5848 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5849 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5850 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5851 Style.BraceWrapping.BeforeElse = false; 5852 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5853 verifyFormat("if (a)\n" 5854 "{\n" 5855 "} else if (b)\n" 5856 "{\n" 5857 "} else\n" 5858 "{ }", 5859 Style); 5860 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 5861 verifyFormat("if (a) {\n" 5862 "} else if (b) {\n" 5863 "} else {\n" 5864 "}", 5865 Style); 5866 Style.BraceWrapping.BeforeElse = true; 5867 verifyFormat("if (a) { }\n" 5868 "else if (b) { }\n" 5869 "else { }", 5870 Style); 5871 } 5872 5873 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5874 FormatStyle Style = getLLVMStyle(); 5875 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5876 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5877 verifyFormat("FOO_BEGIN\n" 5878 " FOO_ENTRY\n" 5879 "FOO_END", 5880 Style); 5881 verifyFormat("FOO_BEGIN\n" 5882 " NESTED_FOO_BEGIN\n" 5883 " NESTED_FOO_ENTRY\n" 5884 " NESTED_FOO_END\n" 5885 "FOO_END", 5886 Style); 5887 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5888 " int x;\n" 5889 " x = 1;\n" 5890 "FOO_END(Baz)", 5891 Style); 5892 } 5893 5894 //===----------------------------------------------------------------------===// 5895 // Line break tests. 5896 //===----------------------------------------------------------------------===// 5897 5898 TEST_F(FormatTest, PreventConfusingIndents) { 5899 verifyFormat( 5900 "void f() {\n" 5901 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5902 " parameter, parameter, parameter)),\n" 5903 " SecondLongCall(parameter));\n" 5904 "}"); 5905 verifyFormat( 5906 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5907 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5908 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5909 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5910 verifyFormat( 5911 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5912 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5913 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5914 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5915 verifyFormat( 5916 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5917 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5919 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5920 verifyFormat("int a = bbbb && ccc &&\n" 5921 " fffff(\n" 5922 "#define A Just forcing a new line\n" 5923 " ddd);"); 5924 } 5925 5926 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5927 verifyFormat( 5928 "bool aaaaaaa =\n" 5929 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5930 " bbbbbbbb();"); 5931 verifyFormat( 5932 "bool aaaaaaa =\n" 5933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5934 " bbbbbbbb();"); 5935 5936 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5938 " ccccccccc == ddddddddddd;"); 5939 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5941 " ccccccccc == ddddddddddd;"); 5942 verifyFormat( 5943 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5945 " ccccccccc == ddddddddddd;"); 5946 5947 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5948 " aaaaaa) &&\n" 5949 " bbbbbb && cccccc;"); 5950 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5951 " aaaaaa) >>\n" 5952 " bbbbbb;"); 5953 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5954 " SourceMgr.getSpellingColumnNumber(\n" 5955 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5956 " 1);"); 5957 5958 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5959 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5960 " cccccc) {\n}"); 5961 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5962 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5963 " cccccc) {\n}"); 5964 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5965 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5966 " cccccc) {\n}"); 5967 verifyFormat("b = a &&\n" 5968 " // Comment\n" 5969 " b.c && d;"); 5970 5971 // If the LHS of a comparison is not a binary expression itself, the 5972 // additional linebreak confuses many people. 5973 verifyFormat( 5974 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5976 "}"); 5977 verifyFormat( 5978 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5979 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5980 "}"); 5981 verifyFormat( 5982 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5983 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5984 "}"); 5985 verifyFormat( 5986 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5987 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5988 "}"); 5989 // Even explicit parentheses stress the precedence enough to make the 5990 // additional break unnecessary. 5991 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5992 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5993 "}"); 5994 // This cases is borderline, but with the indentation it is still readable. 5995 verifyFormat( 5996 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5997 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5998 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5999 "}", 6000 getLLVMStyleWithColumns(75)); 6001 6002 // If the LHS is a binary expression, we should still use the additional break 6003 // as otherwise the formatting hides the operator precedence. 6004 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6005 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6006 " 5) {\n" 6007 "}"); 6008 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 6010 " 5) {\n" 6011 "}"); 6012 6013 FormatStyle OnePerLine = getLLVMStyle(); 6014 OnePerLine.BinPackParameters = false; 6015 verifyFormat( 6016 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 6019 OnePerLine); 6020 6021 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 6022 " .aaa(aaaaaaaaaaaaa) *\n" 6023 " aaaaaaa +\n" 6024 " aaaaaaa;", 6025 getLLVMStyleWithColumns(40)); 6026 } 6027 6028 TEST_F(FormatTest, ExpressionIndentation) { 6029 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6033 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 6034 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 6035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6036 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 6037 " ccccccccccccccccccccccccccccccccccccccccc;"); 6038 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6041 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 6042 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6045 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 6046 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 6047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 6048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6049 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 6050 verifyFormat("if () {\n" 6051 "} else if (aaaaa && bbbbb > // break\n" 6052 " ccccc) {\n" 6053 "}"); 6054 verifyFormat("if () {\n" 6055 "} else if constexpr (aaaaa && bbbbb > // break\n" 6056 " ccccc) {\n" 6057 "}"); 6058 verifyFormat("if () {\n" 6059 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 6060 " ccccc) {\n" 6061 "}"); 6062 verifyFormat("if () {\n" 6063 "} else if (aaaaa &&\n" 6064 " bbbbb > // break\n" 6065 " ccccc &&\n" 6066 " ddddd) {\n" 6067 "}"); 6068 6069 // Presence of a trailing comment used to change indentation of b. 6070 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 6071 " b;\n" 6072 "return aaaaaaaaaaaaaaaaaaa +\n" 6073 " b; //", 6074 getLLVMStyleWithColumns(30)); 6075 } 6076 6077 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 6078 // Not sure what the best system is here. Like this, the LHS can be found 6079 // immediately above an operator (everything with the same or a higher 6080 // indent). The RHS is aligned right of the operator and so compasses 6081 // everything until something with the same indent as the operator is found. 6082 // FIXME: Is this a good system? 6083 FormatStyle Style = getLLVMStyle(); 6084 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6085 verifyFormat( 6086 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6087 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6088 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6089 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6090 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6091 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6092 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6093 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6094 " > ccccccccccccccccccccccccccccccccccccccccc;", 6095 Style); 6096 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6097 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6098 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6099 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6100 Style); 6101 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6102 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6103 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6104 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6105 Style); 6106 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6107 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6108 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6109 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6110 Style); 6111 verifyFormat("if () {\n" 6112 "} else if (aaaaa\n" 6113 " && bbbbb // break\n" 6114 " > ccccc) {\n" 6115 "}", 6116 Style); 6117 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6118 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6119 Style); 6120 verifyFormat("return (a)\n" 6121 " // comment\n" 6122 " + b;", 6123 Style); 6124 verifyFormat( 6125 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6126 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6127 " + cc;", 6128 Style); 6129 6130 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6131 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6132 Style); 6133 6134 // Forced by comments. 6135 verifyFormat( 6136 "unsigned ContentSize =\n" 6137 " sizeof(int16_t) // DWARF ARange version number\n" 6138 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6139 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6140 " + sizeof(int8_t); // Segment Size (in bytes)"); 6141 6142 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6143 " == boost::fusion::at_c<1>(iiii).second;", 6144 Style); 6145 6146 Style.ColumnLimit = 60; 6147 verifyFormat("zzzzzzzzzz\n" 6148 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6149 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6150 Style); 6151 6152 Style.ColumnLimit = 80; 6153 Style.IndentWidth = 4; 6154 Style.TabWidth = 4; 6155 Style.UseTab = FormatStyle::UT_Always; 6156 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6157 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6158 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 6159 "\t&& (someOtherLongishConditionPart1\n" 6160 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 6161 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 6162 "(someOtherLongishConditionPart1 || " 6163 "someOtherEvenLongerNestedConditionPart2);", 6164 Style)); 6165 } 6166 6167 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 6168 FormatStyle Style = getLLVMStyle(); 6169 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6170 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 6171 6172 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6173 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6174 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6175 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6176 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6177 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6178 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6179 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6180 " > ccccccccccccccccccccccccccccccccccccccccc;", 6181 Style); 6182 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6183 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6184 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6185 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6186 Style); 6187 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6188 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6189 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6190 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6191 Style); 6192 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6193 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6194 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6195 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6196 Style); 6197 verifyFormat("if () {\n" 6198 "} else if (aaaaa\n" 6199 " && bbbbb // break\n" 6200 " > ccccc) {\n" 6201 "}", 6202 Style); 6203 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6204 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6205 Style); 6206 verifyFormat("return (a)\n" 6207 " // comment\n" 6208 " + b;", 6209 Style); 6210 verifyFormat( 6211 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6212 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6213 " + cc;", 6214 Style); 6215 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6216 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6217 " : 3333333333333333;", 6218 Style); 6219 verifyFormat( 6220 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6221 " : ccccccccccccccc ? dddddddddddddddddd\n" 6222 " : eeeeeeeeeeeeeeeeee)\n" 6223 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6224 " : 3333333333333333;", 6225 Style); 6226 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6227 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6228 Style); 6229 6230 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6231 " == boost::fusion::at_c<1>(iiii).second;", 6232 Style); 6233 6234 Style.ColumnLimit = 60; 6235 verifyFormat("zzzzzzzzzzzzz\n" 6236 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6237 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6238 Style); 6239 6240 // Forced by comments. 6241 Style.ColumnLimit = 80; 6242 verifyFormat( 6243 "unsigned ContentSize\n" 6244 " = sizeof(int16_t) // DWARF ARange version number\n" 6245 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6246 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6247 " + sizeof(int8_t); // Segment Size (in bytes)", 6248 Style); 6249 6250 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6251 verifyFormat( 6252 "unsigned ContentSize =\n" 6253 " sizeof(int16_t) // DWARF ARange version number\n" 6254 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6255 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6256 " + sizeof(int8_t); // Segment Size (in bytes)", 6257 Style); 6258 6259 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6260 verifyFormat( 6261 "unsigned ContentSize =\n" 6262 " sizeof(int16_t) // DWARF ARange version number\n" 6263 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6264 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6265 " + sizeof(int8_t); // Segment Size (in bytes)", 6266 Style); 6267 } 6268 6269 TEST_F(FormatTest, EnforcedOperatorWraps) { 6270 // Here we'd like to wrap after the || operators, but a comment is forcing an 6271 // earlier wrap. 6272 verifyFormat("bool x = aaaaa //\n" 6273 " || bbbbb\n" 6274 " //\n" 6275 " || cccc;"); 6276 } 6277 6278 TEST_F(FormatTest, NoOperandAlignment) { 6279 FormatStyle Style = getLLVMStyle(); 6280 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6281 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 6282 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6284 Style); 6285 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6286 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6287 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6288 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6289 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6290 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6291 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6292 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6293 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6294 " > ccccccccccccccccccccccccccccccccccccccccc;", 6295 Style); 6296 6297 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6298 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6299 " + cc;", 6300 Style); 6301 verifyFormat("int a = aa\n" 6302 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6303 " * cccccccccccccccccccccccccccccccccccc;\n", 6304 Style); 6305 6306 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6307 verifyFormat("return (a > b\n" 6308 " // comment1\n" 6309 " // comment2\n" 6310 " || c);", 6311 Style); 6312 } 6313 6314 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 6315 FormatStyle Style = getLLVMStyle(); 6316 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6317 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6318 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6319 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6320 Style); 6321 } 6322 6323 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 6324 FormatStyle Style = getLLVMStyleWithColumns(40); 6325 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6326 Style.BinPackArguments = false; 6327 verifyFormat("void test() {\n" 6328 " someFunction(\n" 6329 " this + argument + is + quite\n" 6330 " + long + so + it + gets + wrapped\n" 6331 " + but + remains + bin - packed);\n" 6332 "}", 6333 Style); 6334 verifyFormat("void test() {\n" 6335 " someFunction(arg1,\n" 6336 " this + argument + is\n" 6337 " + quite + long + so\n" 6338 " + it + gets + wrapped\n" 6339 " + but + remains + bin\n" 6340 " - packed,\n" 6341 " arg3);\n" 6342 "}", 6343 Style); 6344 verifyFormat("void test() {\n" 6345 " someFunction(\n" 6346 " arg1,\n" 6347 " this + argument + has\n" 6348 " + anotherFunc(nested,\n" 6349 " calls + whose\n" 6350 " + arguments\n" 6351 " + are + also\n" 6352 " + wrapped,\n" 6353 " in + addition)\n" 6354 " + to + being + bin - packed,\n" 6355 " arg3);\n" 6356 "}", 6357 Style); 6358 6359 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6360 verifyFormat("void test() {\n" 6361 " someFunction(\n" 6362 " arg1,\n" 6363 " this + argument + has +\n" 6364 " anotherFunc(nested,\n" 6365 " calls + whose +\n" 6366 " arguments +\n" 6367 " are + also +\n" 6368 " wrapped,\n" 6369 " in + addition) +\n" 6370 " to + being + bin - packed,\n" 6371 " arg3);\n" 6372 "}", 6373 Style); 6374 } 6375 6376 TEST_F(FormatTest, ConstructorInitializers) { 6377 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6378 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 6379 getLLVMStyleWithColumns(45)); 6380 verifyFormat("Constructor()\n" 6381 " : Inttializer(FitsOnTheLine) {}", 6382 getLLVMStyleWithColumns(44)); 6383 verifyFormat("Constructor()\n" 6384 " : Inttializer(FitsOnTheLine) {}", 6385 getLLVMStyleWithColumns(43)); 6386 6387 verifyFormat("template <typename T>\n" 6388 "Constructor() : Initializer(FitsOnTheLine) {}", 6389 getLLVMStyleWithColumns(45)); 6390 6391 verifyFormat( 6392 "SomeClass::Constructor()\n" 6393 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6394 6395 verifyFormat( 6396 "SomeClass::Constructor()\n" 6397 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6398 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 6399 verifyFormat( 6400 "SomeClass::Constructor()\n" 6401 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6402 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6403 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6404 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6405 " : aaaaaaaaaa(aaaaaa) {}"); 6406 6407 verifyFormat("Constructor()\n" 6408 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6409 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6410 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6411 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 6412 6413 verifyFormat("Constructor()\n" 6414 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6416 6417 verifyFormat("Constructor(int Parameter = 0)\n" 6418 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6419 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 6420 verifyFormat("Constructor()\n" 6421 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6422 "}", 6423 getLLVMStyleWithColumns(60)); 6424 verifyFormat("Constructor()\n" 6425 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6426 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 6427 6428 // Here a line could be saved by splitting the second initializer onto two 6429 // lines, but that is not desirable. 6430 verifyFormat("Constructor()\n" 6431 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6432 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6433 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6434 6435 FormatStyle OnePerLine = getLLVMStyle(); 6436 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 6437 verifyFormat("MyClass::MyClass()\n" 6438 " : a(a),\n" 6439 " b(b),\n" 6440 " c(c) {}", 6441 OnePerLine); 6442 verifyFormat("MyClass::MyClass()\n" 6443 " : a(a), // comment\n" 6444 " b(b),\n" 6445 " c(c) {}", 6446 OnePerLine); 6447 verifyFormat("MyClass::MyClass(int a)\n" 6448 " : b(a), // comment\n" 6449 " c(a + 1) { // lined up\n" 6450 "}", 6451 OnePerLine); 6452 verifyFormat("Constructor()\n" 6453 " : a(b, b, b) {}", 6454 OnePerLine); 6455 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6456 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 6457 verifyFormat("SomeClass::Constructor()\n" 6458 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6459 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6460 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6461 OnePerLine); 6462 verifyFormat("SomeClass::Constructor()\n" 6463 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6464 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6465 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6466 OnePerLine); 6467 verifyFormat("MyClass::MyClass(int var)\n" 6468 " : some_var_(var), // 4 space indent\n" 6469 " some_other_var_(var + 1) { // lined up\n" 6470 "}", 6471 OnePerLine); 6472 verifyFormat("Constructor()\n" 6473 " : aaaaa(aaaaaa),\n" 6474 " aaaaa(aaaaaa),\n" 6475 " aaaaa(aaaaaa),\n" 6476 " aaaaa(aaaaaa),\n" 6477 " aaaaa(aaaaaa) {}", 6478 OnePerLine); 6479 verifyFormat("Constructor()\n" 6480 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6481 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6482 OnePerLine); 6483 OnePerLine.BinPackParameters = false; 6484 verifyFormat( 6485 "Constructor()\n" 6486 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6487 " aaaaaaaaaaa().aaa(),\n" 6488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6489 OnePerLine); 6490 OnePerLine.ColumnLimit = 60; 6491 verifyFormat("Constructor()\n" 6492 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6493 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6494 OnePerLine); 6495 6496 EXPECT_EQ("Constructor()\n" 6497 " : // Comment forcing unwanted break.\n" 6498 " aaaa(aaaa) {}", 6499 format("Constructor() :\n" 6500 " // Comment forcing unwanted break.\n" 6501 " aaaa(aaaa) {}")); 6502 } 6503 6504 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6505 FormatStyle Style = getLLVMStyleWithColumns(60); 6506 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6507 Style.BinPackParameters = false; 6508 6509 for (int i = 0; i < 4; ++i) { 6510 // Test all combinations of parameters that should not have an effect. 6511 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6512 Style.AllowAllArgumentsOnNextLine = i & 2; 6513 6514 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6515 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6516 verifyFormat("Constructor()\n" 6517 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6518 Style); 6519 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6520 6521 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6522 verifyFormat("Constructor()\n" 6523 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6524 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6525 Style); 6526 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6527 6528 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6529 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6530 verifyFormat("Constructor()\n" 6531 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6532 Style); 6533 6534 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6535 verifyFormat("Constructor()\n" 6536 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6537 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6538 Style); 6539 6540 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6541 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6542 verifyFormat("Constructor() :\n" 6543 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6544 Style); 6545 6546 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6547 verifyFormat("Constructor() :\n" 6548 " aaaaaaaaaaaaaaaaaa(a),\n" 6549 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6550 Style); 6551 } 6552 6553 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6554 // AllowAllConstructorInitializersOnNextLine in all 6555 // BreakConstructorInitializers modes 6556 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6557 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6558 verifyFormat("SomeClassWithALongName::Constructor(\n" 6559 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6560 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6561 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6562 Style); 6563 6564 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6565 verifyFormat("SomeClassWithALongName::Constructor(\n" 6566 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6567 " int bbbbbbbbbbbbb,\n" 6568 " int cccccccccccccccc)\n" 6569 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6570 Style); 6571 6572 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6573 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6574 verifyFormat("SomeClassWithALongName::Constructor(\n" 6575 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6576 " int bbbbbbbbbbbbb)\n" 6577 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6578 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6579 Style); 6580 6581 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6582 6583 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6584 verifyFormat("SomeClassWithALongName::Constructor(\n" 6585 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6586 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6587 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6588 Style); 6589 6590 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6591 verifyFormat("SomeClassWithALongName::Constructor(\n" 6592 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6593 " int bbbbbbbbbbbbb,\n" 6594 " int cccccccccccccccc)\n" 6595 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6596 Style); 6597 6598 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6599 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6600 verifyFormat("SomeClassWithALongName::Constructor(\n" 6601 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6602 " int bbbbbbbbbbbbb)\n" 6603 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6604 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6605 Style); 6606 6607 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6608 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6609 verifyFormat("SomeClassWithALongName::Constructor(\n" 6610 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6611 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6612 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6613 Style); 6614 6615 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6616 verifyFormat("SomeClassWithALongName::Constructor(\n" 6617 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6618 " int bbbbbbbbbbbbb,\n" 6619 " int cccccccccccccccc) :\n" 6620 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6621 Style); 6622 6623 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6624 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6625 verifyFormat("SomeClassWithALongName::Constructor(\n" 6626 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6627 " int bbbbbbbbbbbbb) :\n" 6628 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6629 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6630 Style); 6631 } 6632 6633 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6634 FormatStyle Style = getLLVMStyleWithColumns(60); 6635 Style.BinPackArguments = false; 6636 for (int i = 0; i < 4; ++i) { 6637 // Test all combinations of parameters that should not have an effect. 6638 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6639 Style.PackConstructorInitializers = 6640 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6641 6642 Style.AllowAllArgumentsOnNextLine = true; 6643 verifyFormat("void foo() {\n" 6644 " FunctionCallWithReallyLongName(\n" 6645 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6646 "}", 6647 Style); 6648 Style.AllowAllArgumentsOnNextLine = false; 6649 verifyFormat("void foo() {\n" 6650 " FunctionCallWithReallyLongName(\n" 6651 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6652 " bbbbbbbbbbbb);\n" 6653 "}", 6654 Style); 6655 6656 Style.AllowAllArgumentsOnNextLine = true; 6657 verifyFormat("void foo() {\n" 6658 " auto VariableWithReallyLongName = {\n" 6659 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6660 "}", 6661 Style); 6662 Style.AllowAllArgumentsOnNextLine = false; 6663 verifyFormat("void foo() {\n" 6664 " auto VariableWithReallyLongName = {\n" 6665 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6666 " bbbbbbbbbbbb};\n" 6667 "}", 6668 Style); 6669 } 6670 6671 // This parameter should not affect declarations. 6672 Style.BinPackParameters = false; 6673 Style.AllowAllArgumentsOnNextLine = false; 6674 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6675 verifyFormat("void FunctionCallWithReallyLongName(\n" 6676 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6677 Style); 6678 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6679 verifyFormat("void FunctionCallWithReallyLongName(\n" 6680 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6681 " int bbbbbbbbbbbb);", 6682 Style); 6683 } 6684 6685 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6686 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6687 // and BAS_Align. 6688 FormatStyle Style = getLLVMStyleWithColumns(35); 6689 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6690 "void functionDecl(int A, int B, int C);"; 6691 Style.AllowAllArgumentsOnNextLine = false; 6692 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6693 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6694 " paramC);\n" 6695 "void functionDecl(int A, int B,\n" 6696 " int C);"), 6697 format(Input, Style)); 6698 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6699 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6700 " paramC);\n" 6701 "void functionDecl(int A, int B,\n" 6702 " int C);"), 6703 format(Input, Style)); 6704 // However, BAS_AlwaysBreak should take precedence over 6705 // AllowAllArgumentsOnNextLine. 6706 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6707 EXPECT_EQ(StringRef("functionCall(\n" 6708 " paramA, paramB, paramC);\n" 6709 "void functionDecl(\n" 6710 " int A, int B, int C);"), 6711 format(Input, Style)); 6712 6713 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6714 // first argument. 6715 Style.AllowAllArgumentsOnNextLine = true; 6716 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6717 EXPECT_EQ(StringRef("functionCall(\n" 6718 " paramA, paramB, paramC);\n" 6719 "void functionDecl(\n" 6720 " int A, int B, int C);"), 6721 format(Input, Style)); 6722 // It wouldn't fit on one line with aligned parameters so this setting 6723 // doesn't change anything for BAS_Align. 6724 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6725 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6726 " paramC);\n" 6727 "void functionDecl(int A, int B,\n" 6728 " int C);"), 6729 format(Input, Style)); 6730 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6731 EXPECT_EQ(StringRef("functionCall(\n" 6732 " paramA, paramB, paramC);\n" 6733 "void functionDecl(\n" 6734 " int A, int B, int C);"), 6735 format(Input, Style)); 6736 } 6737 6738 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6739 FormatStyle Style = getLLVMStyle(); 6740 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6741 6742 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6743 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6744 getStyleWithColumns(Style, 45)); 6745 verifyFormat("Constructor() :\n" 6746 " Initializer(FitsOnTheLine) {}", 6747 getStyleWithColumns(Style, 44)); 6748 verifyFormat("Constructor() :\n" 6749 " Initializer(FitsOnTheLine) {}", 6750 getStyleWithColumns(Style, 43)); 6751 6752 verifyFormat("template <typename T>\n" 6753 "Constructor() : Initializer(FitsOnTheLine) {}", 6754 getStyleWithColumns(Style, 50)); 6755 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6756 verifyFormat( 6757 "SomeClass::Constructor() :\n" 6758 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6759 Style); 6760 6761 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 6762 verifyFormat( 6763 "SomeClass::Constructor() :\n" 6764 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6765 Style); 6766 6767 verifyFormat( 6768 "SomeClass::Constructor() :\n" 6769 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6770 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6771 Style); 6772 verifyFormat( 6773 "SomeClass::Constructor() :\n" 6774 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6775 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6776 Style); 6777 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6778 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6779 " aaaaaaaaaa(aaaaaa) {}", 6780 Style); 6781 6782 verifyFormat("Constructor() :\n" 6783 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6784 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6785 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6786 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6787 Style); 6788 6789 verifyFormat("Constructor() :\n" 6790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6792 Style); 6793 6794 verifyFormat("Constructor(int Parameter = 0) :\n" 6795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6796 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6797 Style); 6798 verifyFormat("Constructor() :\n" 6799 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6800 "}", 6801 getStyleWithColumns(Style, 60)); 6802 verifyFormat("Constructor() :\n" 6803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6804 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6805 Style); 6806 6807 // Here a line could be saved by splitting the second initializer onto two 6808 // lines, but that is not desirable. 6809 verifyFormat("Constructor() :\n" 6810 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6811 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6812 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6813 Style); 6814 6815 FormatStyle OnePerLine = Style; 6816 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6817 verifyFormat("SomeClass::Constructor() :\n" 6818 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6819 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6820 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6821 OnePerLine); 6822 verifyFormat("SomeClass::Constructor() :\n" 6823 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6824 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6825 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6826 OnePerLine); 6827 verifyFormat("MyClass::MyClass(int var) :\n" 6828 " some_var_(var), // 4 space indent\n" 6829 " some_other_var_(var + 1) { // lined up\n" 6830 "}", 6831 OnePerLine); 6832 verifyFormat("Constructor() :\n" 6833 " aaaaa(aaaaaa),\n" 6834 " aaaaa(aaaaaa),\n" 6835 " aaaaa(aaaaaa),\n" 6836 " aaaaa(aaaaaa),\n" 6837 " aaaaa(aaaaaa) {}", 6838 OnePerLine); 6839 verifyFormat("Constructor() :\n" 6840 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6841 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6842 OnePerLine); 6843 OnePerLine.BinPackParameters = false; 6844 verifyFormat("Constructor() :\n" 6845 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6846 " aaaaaaaaaaa().aaa(),\n" 6847 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6848 OnePerLine); 6849 OnePerLine.ColumnLimit = 60; 6850 verifyFormat("Constructor() :\n" 6851 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6852 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6853 OnePerLine); 6854 6855 EXPECT_EQ("Constructor() :\n" 6856 " // Comment forcing unwanted break.\n" 6857 " aaaa(aaaa) {}", 6858 format("Constructor() :\n" 6859 " // Comment forcing unwanted break.\n" 6860 " aaaa(aaaa) {}", 6861 Style)); 6862 6863 Style.ColumnLimit = 0; 6864 verifyFormat("SomeClass::Constructor() :\n" 6865 " a(a) {}", 6866 Style); 6867 verifyFormat("SomeClass::Constructor() noexcept :\n" 6868 " a(a) {}", 6869 Style); 6870 verifyFormat("SomeClass::Constructor() :\n" 6871 " a(a), b(b), c(c) {}", 6872 Style); 6873 verifyFormat("SomeClass::Constructor() :\n" 6874 " a(a) {\n" 6875 " foo();\n" 6876 " bar();\n" 6877 "}", 6878 Style); 6879 6880 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6881 verifyFormat("SomeClass::Constructor() :\n" 6882 " a(a), b(b), c(c) {\n" 6883 "}", 6884 Style); 6885 verifyFormat("SomeClass::Constructor() :\n" 6886 " a(a) {\n" 6887 "}", 6888 Style); 6889 6890 Style.ColumnLimit = 80; 6891 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6892 Style.ConstructorInitializerIndentWidth = 2; 6893 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6894 verifyFormat("SomeClass::Constructor() :\n" 6895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6896 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6897 Style); 6898 6899 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6900 // well 6901 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6902 verifyFormat( 6903 "class SomeClass\n" 6904 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6905 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6906 Style); 6907 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6908 verifyFormat( 6909 "class SomeClass\n" 6910 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6911 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6912 Style); 6913 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6914 verifyFormat( 6915 "class SomeClass :\n" 6916 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6917 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6918 Style); 6919 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6920 verifyFormat( 6921 "class SomeClass\n" 6922 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6923 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6924 Style); 6925 } 6926 6927 #ifndef EXPENSIVE_CHECKS 6928 // Expensive checks enables libstdc++ checking which includes validating the 6929 // state of ranges used in std::priority_queue - this blows out the 6930 // runtime/scalability of the function and makes this test unacceptably slow. 6931 TEST_F(FormatTest, MemoizationTests) { 6932 // This breaks if the memoization lookup does not take \c Indent and 6933 // \c LastSpace into account. 6934 verifyFormat( 6935 "extern CFRunLoopTimerRef\n" 6936 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6937 " CFTimeInterval interval, CFOptionFlags flags,\n" 6938 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6939 " CFRunLoopTimerContext *context) {}"); 6940 6941 // Deep nesting somewhat works around our memoization. 6942 verifyFormat( 6943 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6944 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6945 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6946 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6947 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6948 getLLVMStyleWithColumns(65)); 6949 verifyFormat( 6950 "aaaaa(\n" 6951 " aaaaa,\n" 6952 " aaaaa(\n" 6953 " aaaaa,\n" 6954 " aaaaa(\n" 6955 " aaaaa,\n" 6956 " aaaaa(\n" 6957 " aaaaa,\n" 6958 " aaaaa(\n" 6959 " aaaaa,\n" 6960 " aaaaa(\n" 6961 " aaaaa,\n" 6962 " aaaaa(\n" 6963 " aaaaa,\n" 6964 " aaaaa(\n" 6965 " aaaaa,\n" 6966 " aaaaa(\n" 6967 " aaaaa,\n" 6968 " aaaaa(\n" 6969 " aaaaa,\n" 6970 " aaaaa(\n" 6971 " aaaaa,\n" 6972 " aaaaa(\n" 6973 " aaaaa,\n" 6974 " aaaaa))))))))))));", 6975 getLLVMStyleWithColumns(65)); 6976 verifyFormat( 6977 "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" 6978 " a),\n" 6979 " a),\n" 6980 " a),\n" 6981 " a),\n" 6982 " a),\n" 6983 " a),\n" 6984 " a),\n" 6985 " a),\n" 6986 " a),\n" 6987 " a),\n" 6988 " a),\n" 6989 " a),\n" 6990 " a),\n" 6991 " a),\n" 6992 " a),\n" 6993 " a),\n" 6994 " a)", 6995 getLLVMStyleWithColumns(65)); 6996 6997 // This test takes VERY long when memoization is broken. 6998 FormatStyle OnePerLine = getLLVMStyle(); 6999 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7000 OnePerLine.BinPackParameters = false; 7001 std::string input = "Constructor()\n" 7002 " : aaaa(a,\n"; 7003 for (unsigned i = 0, e = 80; i != e; ++i) { 7004 input += " a,\n"; 7005 } 7006 input += " a) {}"; 7007 verifyFormat(input, OnePerLine); 7008 } 7009 #endif 7010 7011 TEST_F(FormatTest, BreaksAsHighAsPossible) { 7012 verifyFormat( 7013 "void f() {\n" 7014 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 7015 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 7016 " f();\n" 7017 "}"); 7018 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 7019 " Intervals[i - 1].getRange().getLast()) {\n}"); 7020 } 7021 7022 TEST_F(FormatTest, BreaksFunctionDeclarations) { 7023 // Principially, we break function declarations in a certain order: 7024 // 1) break amongst arguments. 7025 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 7026 " Cccccccccccccc cccccccccccccc);"); 7027 verifyFormat("template <class TemplateIt>\n" 7028 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 7029 " TemplateIt *stop) {}"); 7030 7031 // 2) break after return type. 7032 verifyFormat( 7033 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7034 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 7035 getGoogleStyle()); 7036 7037 // 3) break after (. 7038 verifyFormat( 7039 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 7040 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 7041 getGoogleStyle()); 7042 7043 // 4) break before after nested name specifiers. 7044 verifyFormat( 7045 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7046 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 7047 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 7048 getGoogleStyle()); 7049 7050 // However, there are exceptions, if a sufficient amount of lines can be 7051 // saved. 7052 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 7053 // more adjusting. 7054 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 7055 " Cccccccccccccc cccccccccc,\n" 7056 " Cccccccccccccc cccccccccc,\n" 7057 " Cccccccccccccc cccccccccc,\n" 7058 " Cccccccccccccc cccccccccc);"); 7059 verifyFormat( 7060 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7061 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7062 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7063 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 7064 getGoogleStyle()); 7065 verifyFormat( 7066 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 7067 " Cccccccccccccc cccccccccc,\n" 7068 " Cccccccccccccc cccccccccc,\n" 7069 " Cccccccccccccc cccccccccc,\n" 7070 " Cccccccccccccc cccccccccc,\n" 7071 " Cccccccccccccc cccccccccc,\n" 7072 " Cccccccccccccc cccccccccc);"); 7073 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7074 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7075 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7076 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7077 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 7078 7079 // Break after multi-line parameters. 7080 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7083 " bbbb bbbb);"); 7084 verifyFormat("void SomeLoooooooooooongFunction(\n" 7085 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7086 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7087 " int bbbbbbbbbbbbb);"); 7088 7089 // Treat overloaded operators like other functions. 7090 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7091 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 7092 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7093 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 7094 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7095 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 7096 verifyGoogleFormat( 7097 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 7098 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 7099 verifyGoogleFormat( 7100 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 7101 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 7102 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7103 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 7104 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 7105 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 7106 verifyGoogleFormat( 7107 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 7108 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7109 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 7110 verifyGoogleFormat("template <typename T>\n" 7111 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7112 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 7113 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 7114 7115 FormatStyle Style = getLLVMStyle(); 7116 Style.PointerAlignment = FormatStyle::PAS_Left; 7117 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7118 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 7119 Style); 7120 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 7121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7122 Style); 7123 } 7124 7125 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 7126 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 7127 // Prefer keeping `::` followed by `operator` together. 7128 EXPECT_EQ("const aaaa::bbbbbbb &\n" 7129 "ccccccccc::operator++() {\n" 7130 " stuff();\n" 7131 "}", 7132 format("const aaaa::bbbbbbb\n" 7133 "&ccccccccc::operator++() { stuff(); }", 7134 getLLVMStyleWithColumns(40))); 7135 } 7136 7137 TEST_F(FormatTest, TrailingReturnType) { 7138 verifyFormat("auto foo() -> int;\n"); 7139 // correct trailing return type spacing 7140 verifyFormat("auto operator->() -> int;\n"); 7141 verifyFormat("auto operator++(int) -> int;\n"); 7142 7143 verifyFormat("struct S {\n" 7144 " auto bar() const -> int;\n" 7145 "};"); 7146 verifyFormat("template <size_t Order, typename T>\n" 7147 "auto load_img(const std::string &filename)\n" 7148 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 7149 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 7150 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 7151 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 7152 verifyFormat("template <typename T>\n" 7153 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 7154 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 7155 7156 // Not trailing return types. 7157 verifyFormat("void f() { auto a = b->c(); }"); 7158 verifyFormat("auto a = p->foo();"); 7159 verifyFormat("int a = p->foo();"); 7160 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); 7161 } 7162 7163 TEST_F(FormatTest, DeductionGuides) { 7164 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 7165 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 7166 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 7167 verifyFormat( 7168 "template <class... T>\n" 7169 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 7170 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 7171 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 7172 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 7173 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 7174 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 7175 verifyFormat("template <class T> x() -> x<1>;"); 7176 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 7177 7178 // Ensure not deduction guides. 7179 verifyFormat("c()->f<int>();"); 7180 verifyFormat("x()->foo<1>;"); 7181 verifyFormat("x = p->foo<3>();"); 7182 verifyFormat("x()->x<1>();"); 7183 verifyFormat("x()->x<1>;"); 7184 } 7185 7186 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 7187 // Avoid breaking before trailing 'const' or other trailing annotations, if 7188 // they are not function-like. 7189 FormatStyle Style = getGoogleStyleWithColumns(47); 7190 verifyFormat("void someLongFunction(\n" 7191 " int someLoooooooooooooongParameter) const {\n}", 7192 getLLVMStyleWithColumns(47)); 7193 verifyFormat("LoooooongReturnType\n" 7194 "someLoooooooongFunction() const {}", 7195 getLLVMStyleWithColumns(47)); 7196 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 7197 " const {}", 7198 Style); 7199 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7200 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 7201 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7202 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 7203 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7204 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 7205 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 7206 " aaaaaaaaaaa aaaaa) const override;"); 7207 verifyGoogleFormat( 7208 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7209 " const override;"); 7210 7211 // Even if the first parameter has to be wrapped. 7212 verifyFormat("void someLongFunction(\n" 7213 " int someLongParameter) const {}", 7214 getLLVMStyleWithColumns(46)); 7215 verifyFormat("void someLongFunction(\n" 7216 " int someLongParameter) const {}", 7217 Style); 7218 verifyFormat("void someLongFunction(\n" 7219 " int someLongParameter) override {}", 7220 Style); 7221 verifyFormat("void someLongFunction(\n" 7222 " int someLongParameter) OVERRIDE {}", 7223 Style); 7224 verifyFormat("void someLongFunction(\n" 7225 " int someLongParameter) final {}", 7226 Style); 7227 verifyFormat("void someLongFunction(\n" 7228 " int someLongParameter) FINAL {}", 7229 Style); 7230 verifyFormat("void someLongFunction(\n" 7231 " int parameter) const override {}", 7232 Style); 7233 7234 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 7235 verifyFormat("void someLongFunction(\n" 7236 " int someLongParameter) const\n" 7237 "{\n" 7238 "}", 7239 Style); 7240 7241 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 7242 verifyFormat("void someLongFunction(\n" 7243 " int someLongParameter) const\n" 7244 " {\n" 7245 " }", 7246 Style); 7247 7248 // Unless these are unknown annotations. 7249 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 7250 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7251 " LONG_AND_UGLY_ANNOTATION;"); 7252 7253 // Breaking before function-like trailing annotations is fine to keep them 7254 // close to their arguments. 7255 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7256 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7257 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7258 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7259 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7260 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 7261 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 7262 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 7263 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 7264 7265 verifyFormat( 7266 "void aaaaaaaaaaaaaaaaaa()\n" 7267 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 7268 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 7269 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7270 " __attribute__((unused));"); 7271 verifyGoogleFormat( 7272 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7273 " GUARDED_BY(aaaaaaaaaaaa);"); 7274 verifyGoogleFormat( 7275 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7276 " GUARDED_BY(aaaaaaaaaaaa);"); 7277 verifyGoogleFormat( 7278 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7279 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7280 verifyGoogleFormat( 7281 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7282 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7283 } 7284 7285 TEST_F(FormatTest, FunctionAnnotations) { 7286 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7287 "int OldFunction(const string ¶meter) {}"); 7288 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7289 "string OldFunction(const string ¶meter) {}"); 7290 verifyFormat("template <typename T>\n" 7291 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7292 "string OldFunction(const string ¶meter) {}"); 7293 7294 // Not function annotations. 7295 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7296 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 7297 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 7298 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 7299 verifyFormat("MACRO(abc).function() // wrap\n" 7300 " << abc;"); 7301 verifyFormat("MACRO(abc)->function() // wrap\n" 7302 " << abc;"); 7303 verifyFormat("MACRO(abc)::function() // wrap\n" 7304 " << abc;"); 7305 } 7306 7307 TEST_F(FormatTest, BreaksDesireably) { 7308 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7309 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7310 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 7311 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7312 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 7313 "}"); 7314 7315 verifyFormat( 7316 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7318 7319 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7320 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7322 7323 verifyFormat( 7324 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7325 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7327 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7328 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 7329 7330 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7331 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7332 7333 verifyFormat( 7334 "void f() {\n" 7335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7337 "}"); 7338 verifyFormat( 7339 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7341 verifyFormat( 7342 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7344 verifyFormat( 7345 "aaaaaa(aaa,\n" 7346 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7348 " aaaa);"); 7349 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7351 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7352 7353 // Indent consistently independent of call expression and unary operator. 7354 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7355 " dddddddddddddddddddddddddddddd));"); 7356 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7357 " dddddddddddddddddddddddddddddd));"); 7358 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 7359 " dddddddddddddddddddddddddddddd));"); 7360 7361 // This test case breaks on an incorrect memoization, i.e. an optimization not 7362 // taking into account the StopAt value. 7363 verifyFormat( 7364 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7365 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7366 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7367 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7368 7369 verifyFormat("{\n {\n {\n" 7370 " Annotation.SpaceRequiredBefore =\n" 7371 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 7372 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 7373 " }\n }\n}"); 7374 7375 // Break on an outer level if there was a break on an inner level. 7376 EXPECT_EQ("f(g(h(a, // comment\n" 7377 " b, c),\n" 7378 " d, e),\n" 7379 " x, y);", 7380 format("f(g(h(a, // comment\n" 7381 " b, c), d, e), x, y);")); 7382 7383 // Prefer breaking similar line breaks. 7384 verifyFormat( 7385 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 7386 " NSTrackingMouseEnteredAndExited |\n" 7387 " NSTrackingActiveAlways;"); 7388 } 7389 7390 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 7391 FormatStyle NoBinPacking = getGoogleStyle(); 7392 NoBinPacking.BinPackParameters = false; 7393 NoBinPacking.BinPackArguments = true; 7394 verifyFormat("void f() {\n" 7395 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 7396 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7397 "}", 7398 NoBinPacking); 7399 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 7400 " int aaaaaaaaaaaaaaaaaaaa,\n" 7401 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7402 NoBinPacking); 7403 7404 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7405 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7406 " vector<int> bbbbbbbbbbbbbbb);", 7407 NoBinPacking); 7408 // FIXME: This behavior difference is probably not wanted. However, currently 7409 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 7410 // template arguments from BreakBeforeParameter being set because of the 7411 // one-per-line formatting. 7412 verifyFormat( 7413 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7414 " aaaaaaaaaa> aaaaaaaaaa);", 7415 NoBinPacking); 7416 verifyFormat( 7417 "void fffffffffff(\n" 7418 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 7419 " aaaaaaaaaa);"); 7420 } 7421 7422 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 7423 FormatStyle NoBinPacking = getGoogleStyle(); 7424 NoBinPacking.BinPackParameters = false; 7425 NoBinPacking.BinPackArguments = false; 7426 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 7427 " aaaaaaaaaaaaaaaaaaaa,\n" 7428 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 7429 NoBinPacking); 7430 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 7431 " aaaaaaaaaaaaa,\n" 7432 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 7433 NoBinPacking); 7434 verifyFormat( 7435 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7436 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7438 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 7440 NoBinPacking); 7441 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7442 " .aaaaaaaaaaaaaaaaaa();", 7443 NoBinPacking); 7444 verifyFormat("void f() {\n" 7445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7446 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 7447 "}", 7448 NoBinPacking); 7449 7450 verifyFormat( 7451 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7452 " aaaaaaaaaaaa,\n" 7453 " aaaaaaaaaaaa);", 7454 NoBinPacking); 7455 verifyFormat( 7456 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 7457 " ddddddddddddddddddddddddddddd),\n" 7458 " test);", 7459 NoBinPacking); 7460 7461 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7462 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 7463 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 7464 " aaaaaaaaaaaaaaaaaa;", 7465 NoBinPacking); 7466 verifyFormat("a(\"a\"\n" 7467 " \"a\",\n" 7468 " a);"); 7469 7470 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7471 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7472 " aaaaaaaaa,\n" 7473 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7474 NoBinPacking); 7475 verifyFormat( 7476 "void f() {\n" 7477 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7478 " .aaaaaaa();\n" 7479 "}", 7480 NoBinPacking); 7481 verifyFormat( 7482 "template <class SomeType, class SomeOtherType>\n" 7483 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7484 NoBinPacking); 7485 } 7486 7487 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7488 FormatStyle Style = getLLVMStyleWithColumns(15); 7489 Style.ExperimentalAutoDetectBinPacking = true; 7490 EXPECT_EQ("aaa(aaaa,\n" 7491 " aaaa,\n" 7492 " aaaa);\n" 7493 "aaa(aaaa,\n" 7494 " aaaa,\n" 7495 " aaaa);", 7496 format("aaa(aaaa,\n" // one-per-line 7497 " aaaa,\n" 7498 " aaaa );\n" 7499 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7500 Style)); 7501 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7502 " aaaa);\n" 7503 "aaa(aaaa, aaaa,\n" 7504 " aaaa);", 7505 format("aaa(aaaa, aaaa,\n" // bin-packed 7506 " aaaa );\n" 7507 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7508 Style)); 7509 } 7510 7511 TEST_F(FormatTest, FormatsBuilderPattern) { 7512 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7513 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7514 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7515 " .StartsWith(\".init\", ORDER_INIT)\n" 7516 " .StartsWith(\".fini\", ORDER_FINI)\n" 7517 " .StartsWith(\".hash\", ORDER_HASH)\n" 7518 " .Default(ORDER_TEXT);\n"); 7519 7520 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7521 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7522 verifyFormat("aaaaaaa->aaaaaaa\n" 7523 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7525 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7526 verifyFormat( 7527 "aaaaaaa->aaaaaaa\n" 7528 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7529 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7530 verifyFormat( 7531 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7532 " aaaaaaaaaaaaaa);"); 7533 verifyFormat( 7534 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7535 " aaaaaa->aaaaaaaaaaaa()\n" 7536 " ->aaaaaaaaaaaaaaaa(\n" 7537 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7538 " ->aaaaaaaaaaaaaaaaa();"); 7539 verifyGoogleFormat( 7540 "void f() {\n" 7541 " someo->Add((new util::filetools::Handler(dir))\n" 7542 " ->OnEvent1(NewPermanentCallback(\n" 7543 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7544 " ->OnEvent2(NewPermanentCallback(\n" 7545 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7546 " ->OnEvent3(NewPermanentCallback(\n" 7547 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7548 " ->OnEvent5(NewPermanentCallback(\n" 7549 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7550 " ->OnEvent6(NewPermanentCallback(\n" 7551 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7552 "}"); 7553 7554 verifyFormat( 7555 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7556 verifyFormat("aaaaaaaaaaaaaaa()\n" 7557 " .aaaaaaaaaaaaaaa()\n" 7558 " .aaaaaaaaaaaaaaa()\n" 7559 " .aaaaaaaaaaaaaaa()\n" 7560 " .aaaaaaaaaaaaaaa();"); 7561 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7562 " .aaaaaaaaaaaaaaa()\n" 7563 " .aaaaaaaaaaaaaaa()\n" 7564 " .aaaaaaaaaaaaaaa();"); 7565 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7566 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7567 " .aaaaaaaaaaaaaaa();"); 7568 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7569 " ->aaaaaaaaaaaaaae(0)\n" 7570 " ->aaaaaaaaaaaaaaa();"); 7571 7572 // Don't linewrap after very short segments. 7573 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7574 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7575 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7576 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7577 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7578 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7579 verifyFormat("aaa()\n" 7580 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7581 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7582 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7583 7584 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7585 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7586 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7587 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7588 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7589 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7590 7591 // Prefer not to break after empty parentheses. 7592 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7593 " First->LastNewlineOffset);"); 7594 7595 // Prefer not to create "hanging" indents. 7596 verifyFormat( 7597 "return !soooooooooooooome_map\n" 7598 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7599 " .second;"); 7600 verifyFormat( 7601 "return aaaaaaaaaaaaaaaa\n" 7602 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7603 " .aaaa(aaaaaaaaaaaaaa);"); 7604 // No hanging indent here. 7605 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7606 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7607 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7608 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7609 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7610 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7611 getLLVMStyleWithColumns(60)); 7612 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7613 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7614 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7615 getLLVMStyleWithColumns(59)); 7616 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7618 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7619 7620 // Dont break if only closing statements before member call 7621 verifyFormat("test() {\n" 7622 " ([]() -> {\n" 7623 " int b = 32;\n" 7624 " return 3;\n" 7625 " }).foo();\n" 7626 "}"); 7627 verifyFormat("test() {\n" 7628 " (\n" 7629 " []() -> {\n" 7630 " int b = 32;\n" 7631 " return 3;\n" 7632 " },\n" 7633 " foo, bar)\n" 7634 " .foo();\n" 7635 "}"); 7636 verifyFormat("test() {\n" 7637 " ([]() -> {\n" 7638 " int b = 32;\n" 7639 " return 3;\n" 7640 " })\n" 7641 " .foo()\n" 7642 " .bar();\n" 7643 "}"); 7644 verifyFormat("test() {\n" 7645 " ([]() -> {\n" 7646 " int b = 32;\n" 7647 " return 3;\n" 7648 " })\n" 7649 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7650 " \"bbbb\");\n" 7651 "}", 7652 getLLVMStyleWithColumns(30)); 7653 } 7654 7655 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7656 verifyFormat( 7657 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7658 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7659 verifyFormat( 7660 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7661 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7662 7663 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7664 " ccccccccccccccccccccccccc) {\n}"); 7665 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7666 " ccccccccccccccccccccccccc) {\n}"); 7667 7668 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7669 " ccccccccccccccccccccccccc) {\n}"); 7670 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7671 " ccccccccccccccccccccccccc) {\n}"); 7672 7673 verifyFormat( 7674 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7675 " ccccccccccccccccccccccccc) {\n}"); 7676 verifyFormat( 7677 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7678 " ccccccccccccccccccccccccc) {\n}"); 7679 7680 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7681 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7682 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7683 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7684 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7685 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7686 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7687 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7688 7689 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7690 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7691 " aaaaaaaaaaaaaaa != aa) {\n}"); 7692 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7693 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7694 " aaaaaaaaaaaaaaa != aa) {\n}"); 7695 } 7696 7697 TEST_F(FormatTest, BreaksAfterAssignments) { 7698 verifyFormat( 7699 "unsigned Cost =\n" 7700 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7701 " SI->getPointerAddressSpaceee());\n"); 7702 verifyFormat( 7703 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7704 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7705 7706 verifyFormat( 7707 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7708 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7709 verifyFormat("unsigned OriginalStartColumn =\n" 7710 " SourceMgr.getSpellingColumnNumber(\n" 7711 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7712 " 1;"); 7713 } 7714 7715 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7716 FormatStyle Style = getLLVMStyle(); 7717 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7718 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7719 Style); 7720 7721 Style.PenaltyBreakAssignment = 20; 7722 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7723 " cccccccccccccccccccccccccc;", 7724 Style); 7725 } 7726 7727 TEST_F(FormatTest, AlignsAfterAssignments) { 7728 verifyFormat( 7729 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7730 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7731 verifyFormat( 7732 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7733 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7734 verifyFormat( 7735 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7736 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7737 verifyFormat( 7738 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7739 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7740 verifyFormat( 7741 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7742 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7743 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7744 } 7745 7746 TEST_F(FormatTest, AlignsAfterReturn) { 7747 verifyFormat( 7748 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7749 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7750 verifyFormat( 7751 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7752 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7753 verifyFormat( 7754 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7755 " aaaaaaaaaaaaaaaaaaaaaa();"); 7756 verifyFormat( 7757 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7758 " aaaaaaaaaaaaaaaaaaaaaa());"); 7759 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7761 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7764 verifyFormat("return\n" 7765 " // true if code is one of a or b.\n" 7766 " code == a || code == b;"); 7767 } 7768 7769 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7770 verifyFormat( 7771 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7772 " aaaaaaaaa aaaaaaa) {}"); 7773 verifyFormat( 7774 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7775 " aaaaaaaaaaa aaaaaaaaa);"); 7776 verifyFormat( 7777 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7778 " aaaaaaaaaaaaaaaaaaaaa));"); 7779 FormatStyle Style = getLLVMStyle(); 7780 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7781 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7782 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7783 Style); 7784 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7785 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7786 Style); 7787 verifyFormat("SomeLongVariableName->someFunction(\n" 7788 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7789 Style); 7790 verifyFormat( 7791 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7792 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7793 Style); 7794 verifyFormat( 7795 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7796 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7797 Style); 7798 verifyFormat( 7799 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7800 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7801 Style); 7802 7803 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7804 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7805 " b));", 7806 Style); 7807 7808 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7809 Style.BinPackArguments = false; 7810 Style.BinPackParameters = false; 7811 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7812 " aaaaaaaaaaa aaaaaaaa,\n" 7813 " aaaaaaaaa aaaaaaa,\n" 7814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7815 Style); 7816 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7817 " aaaaaaaaaaa aaaaaaaaa,\n" 7818 " aaaaaaaaaaa aaaaaaaaa,\n" 7819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7820 Style); 7821 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7822 " aaaaaaaaaaaaaaa,\n" 7823 " aaaaaaaaaaaaaaaaaaaaa,\n" 7824 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7825 Style); 7826 verifyFormat( 7827 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7828 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7829 Style); 7830 verifyFormat( 7831 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7832 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7833 Style); 7834 verifyFormat( 7835 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7836 " aaaaaaaaaaaaaaaaaaaaa(\n" 7837 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7838 " aaaaaaaaaaaaaaaa);", 7839 Style); 7840 verifyFormat( 7841 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7842 " aaaaaaaaaaaaaaaaaaaaa(\n" 7843 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7844 " aaaaaaaaaaaaaaaa);", 7845 Style); 7846 } 7847 7848 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7849 FormatStyle Style = getLLVMStyleWithColumns(40); 7850 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7851 " bbbbbbbbbbbbbbbbbbbbbb);", 7852 Style); 7853 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7854 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7855 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7856 " bbbbbbbbbbbbbbbbbbbbbb);", 7857 Style); 7858 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7859 Style.AlignOperands = FormatStyle::OAS_Align; 7860 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7861 " bbbbbbbbbbbbbbbbbbbbbb);", 7862 Style); 7863 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7864 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7865 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7866 " bbbbbbbbbbbbbbbbbbbbbb);", 7867 Style); 7868 } 7869 7870 TEST_F(FormatTest, BreaksConditionalExpressions) { 7871 verifyFormat( 7872 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7873 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7874 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7875 verifyFormat( 7876 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7877 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7878 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7879 verifyFormat( 7880 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7881 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7882 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7883 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7884 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7885 verifyFormat( 7886 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7887 " : aaaaaaaaaaaaa);"); 7888 verifyFormat( 7889 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7890 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7891 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7892 " aaaaaaaaaaaaa);"); 7893 verifyFormat( 7894 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7895 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7896 " aaaaaaaaaaaaa);"); 7897 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7898 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7900 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7901 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7902 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7903 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7904 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7906 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7908 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7909 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7911 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7913 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7914 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7915 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7916 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7917 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7919 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7920 " : aaaaaaaaaaaaaaaa;"); 7921 verifyFormat( 7922 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7923 " ? aaaaaaaaaaaaaaa\n" 7924 " : aaaaaaaaaaaaaaa;"); 7925 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7926 " aaaaaaaaa\n" 7927 " ? b\n" 7928 " : c);"); 7929 verifyFormat("return aaaa == bbbb\n" 7930 " // comment\n" 7931 " ? aaaa\n" 7932 " : bbbb;"); 7933 verifyFormat("unsigned Indent =\n" 7934 " format(TheLine.First,\n" 7935 " IndentForLevel[TheLine.Level] >= 0\n" 7936 " ? IndentForLevel[TheLine.Level]\n" 7937 " : TheLine * 2,\n" 7938 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7939 getLLVMStyleWithColumns(60)); 7940 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7941 " ? aaaaaaaaaaaaaaa\n" 7942 " : bbbbbbbbbbbbbbb //\n" 7943 " ? ccccccccccccccc\n" 7944 " : ddddddddddddddd;"); 7945 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7946 " ? aaaaaaaaaaaaaaa\n" 7947 " : (bbbbbbbbbbbbbbb //\n" 7948 " ? ccccccccccccccc\n" 7949 " : ddddddddddddddd);"); 7950 verifyFormat( 7951 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7952 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7953 " aaaaaaaaaaaaaaaaaaaaa +\n" 7954 " aaaaaaaaaaaaaaaaaaaaa\n" 7955 " : aaaaaaaaaa;"); 7956 verifyFormat( 7957 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7958 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7959 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7960 7961 FormatStyle NoBinPacking = getLLVMStyle(); 7962 NoBinPacking.BinPackArguments = false; 7963 verifyFormat( 7964 "void f() {\n" 7965 " g(aaa,\n" 7966 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7967 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7968 " ? aaaaaaaaaaaaaaa\n" 7969 " : aaaaaaaaaaaaaaa);\n" 7970 "}", 7971 NoBinPacking); 7972 verifyFormat( 7973 "void f() {\n" 7974 " g(aaa,\n" 7975 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7976 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7977 " ?: aaaaaaaaaaaaaaa);\n" 7978 "}", 7979 NoBinPacking); 7980 7981 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7982 " // comment.\n" 7983 " ccccccccccccccccccccccccccccccccccccccc\n" 7984 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7985 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7986 7987 // Assignments in conditional expressions. Apparently not uncommon :-(. 7988 verifyFormat("return a != b\n" 7989 " // comment\n" 7990 " ? a = b\n" 7991 " : a = b;"); 7992 verifyFormat("return a != b\n" 7993 " // comment\n" 7994 " ? a = a != b\n" 7995 " // comment\n" 7996 " ? a = b\n" 7997 " : a\n" 7998 " : a;\n"); 7999 verifyFormat("return a != b\n" 8000 " // comment\n" 8001 " ? a\n" 8002 " : a = a != b\n" 8003 " // comment\n" 8004 " ? a = b\n" 8005 " : a;"); 8006 8007 // Chained conditionals 8008 FormatStyle Style = getLLVMStyleWithColumns(70); 8009 Style.AlignOperands = FormatStyle::OAS_Align; 8010 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8011 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8012 " : 3333333333333333;", 8013 Style); 8014 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8015 " : bbbbbbbbbb ? 2222222222222222\n" 8016 " : 3333333333333333;", 8017 Style); 8018 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 8019 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 8020 " : 3333333333333333;", 8021 Style); 8022 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8023 " : bbbbbbbbbbbbbb ? 222222\n" 8024 " : 333333;", 8025 Style); 8026 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8027 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8028 " : cccccccccccccc ? 3333333333333333\n" 8029 " : 4444444444444444;", 8030 Style); 8031 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 8032 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8033 " : 3333333333333333;", 8034 Style); 8035 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8036 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8037 " : (aaa ? bbb : ccc);", 8038 Style); 8039 verifyFormat( 8040 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8041 " : cccccccccccccccccc)\n" 8042 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8043 " : 3333333333333333;", 8044 Style); 8045 verifyFormat( 8046 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8047 " : cccccccccccccccccc)\n" 8048 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8049 " : 3333333333333333;", 8050 Style); 8051 verifyFormat( 8052 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8053 " : dddddddddddddddddd)\n" 8054 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8055 " : 3333333333333333;", 8056 Style); 8057 verifyFormat( 8058 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8059 " : dddddddddddddddddd)\n" 8060 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8061 " : 3333333333333333;", 8062 Style); 8063 verifyFormat( 8064 "return aaaaaaaaa ? 1111111111111111\n" 8065 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8066 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8067 " : dddddddddddddddddd)\n", 8068 Style); 8069 verifyFormat( 8070 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8071 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8072 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8073 " : cccccccccccccccccc);", 8074 Style); 8075 verifyFormat( 8076 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8077 " : ccccccccccccccc ? dddddddddddddddddd\n" 8078 " : eeeeeeeeeeeeeeeeee)\n" 8079 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8080 " : 3333333333333333;", 8081 Style); 8082 verifyFormat( 8083 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8084 " : ccccccccccccccc ? dddddddddddddddddd\n" 8085 " : eeeeeeeeeeeeeeeeee)\n" 8086 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8087 " : 3333333333333333;", 8088 Style); 8089 verifyFormat( 8090 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8091 " : cccccccccccc ? dddddddddddddddddd\n" 8092 " : eeeeeeeeeeeeeeeeee)\n" 8093 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8094 " : 3333333333333333;", 8095 Style); 8096 verifyFormat( 8097 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8098 " : cccccccccccccccccc\n" 8099 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8100 " : 3333333333333333;", 8101 Style); 8102 verifyFormat( 8103 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8104 " : cccccccccccccccc ? dddddddddddddddddd\n" 8105 " : eeeeeeeeeeeeeeeeee\n" 8106 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8107 " : 3333333333333333;", 8108 Style); 8109 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 8110 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8111 " : cccccccccccccccccc ? dddddddddddddddddd\n" 8112 " : eeeeeeeeeeeeeeeeee)\n" 8113 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 8114 " : 3333333333333333;", 8115 Style); 8116 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 8117 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8118 " : cccccccccccccccc ? dddddddddddddddddd\n" 8119 " : eeeeeeeeeeeeeeeeee\n" 8120 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 8121 " : 3333333333333333;", 8122 Style); 8123 8124 Style.AlignOperands = FormatStyle::OAS_DontAlign; 8125 Style.BreakBeforeTernaryOperators = false; 8126 // FIXME: Aligning the question marks is weird given DontAlign. 8127 // Consider disabling this alignment in this case. Also check whether this 8128 // will render the adjustment from https://reviews.llvm.org/D82199 8129 // unnecessary. 8130 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 8131 " bbbb ? cccccccccccccccccc :\n" 8132 " ddddd;\n", 8133 Style); 8134 8135 EXPECT_EQ( 8136 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8137 " /*\n" 8138 " */\n" 8139 " function() {\n" 8140 " try {\n" 8141 " return JJJJJJJJJJJJJJ(\n" 8142 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8143 " }\n" 8144 " } :\n" 8145 " function() {};", 8146 format( 8147 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8148 " /*\n" 8149 " */\n" 8150 " function() {\n" 8151 " try {\n" 8152 " return JJJJJJJJJJJJJJ(\n" 8153 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8154 " }\n" 8155 " } :\n" 8156 " function() {};", 8157 getGoogleStyle(FormatStyle::LK_JavaScript))); 8158 } 8159 8160 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 8161 FormatStyle Style = getLLVMStyleWithColumns(70); 8162 Style.BreakBeforeTernaryOperators = false; 8163 verifyFormat( 8164 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8167 Style); 8168 verifyFormat( 8169 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 8170 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8172 Style); 8173 verifyFormat( 8174 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8176 Style); 8177 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 8178 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8179 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8180 Style); 8181 verifyFormat( 8182 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 8183 " aaaaaaaaaaaaa);", 8184 Style); 8185 verifyFormat( 8186 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8187 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8188 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8189 " aaaaaaaaaaaaa);", 8190 Style); 8191 verifyFormat( 8192 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8193 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8194 " aaaaaaaaaaaaa);", 8195 Style); 8196 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8197 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8199 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8201 Style); 8202 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8203 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8205 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8206 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8208 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8209 Style); 8210 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 8212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8214 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8215 Style); 8216 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8217 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8218 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8219 Style); 8220 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 8221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8224 Style); 8225 verifyFormat( 8226 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8227 " aaaaaaaaaaaaaaa :\n" 8228 " aaaaaaaaaaaaaaa;", 8229 Style); 8230 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 8231 " aaaaaaaaa ?\n" 8232 " b :\n" 8233 " c);", 8234 Style); 8235 verifyFormat("unsigned Indent =\n" 8236 " format(TheLine.First,\n" 8237 " IndentForLevel[TheLine.Level] >= 0 ?\n" 8238 " IndentForLevel[TheLine.Level] :\n" 8239 " TheLine * 2,\n" 8240 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 8241 Style); 8242 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8243 " aaaaaaaaaaaaaaa :\n" 8244 " bbbbbbbbbbbbbbb ? //\n" 8245 " ccccccccccccccc :\n" 8246 " ddddddddddddddd;", 8247 Style); 8248 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8249 " aaaaaaaaaaaaaaa :\n" 8250 " (bbbbbbbbbbbbbbb ? //\n" 8251 " ccccccccccccccc :\n" 8252 " ddddddddddddddd);", 8253 Style); 8254 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8255 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 8256 " ccccccccccccccccccccccccccc;", 8257 Style); 8258 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8259 " aaaaa :\n" 8260 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 8261 Style); 8262 8263 // Chained conditionals 8264 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8265 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8266 " 3333333333333333;", 8267 Style); 8268 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8269 " bbbbbbbbbb ? 2222222222222222 :\n" 8270 " 3333333333333333;", 8271 Style); 8272 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 8273 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8274 " 3333333333333333;", 8275 Style); 8276 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8277 " bbbbbbbbbbbbbbbb ? 222222 :\n" 8278 " 333333;", 8279 Style); 8280 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8281 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8282 " cccccccccccccccc ? 3333333333333333 :\n" 8283 " 4444444444444444;", 8284 Style); 8285 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 8286 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8287 " 3333333333333333;", 8288 Style); 8289 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8290 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8291 " (aaa ? bbb : ccc);", 8292 Style); 8293 verifyFormat( 8294 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8295 " cccccccccccccccccc) :\n" 8296 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8297 " 3333333333333333;", 8298 Style); 8299 verifyFormat( 8300 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8301 " cccccccccccccccccc) :\n" 8302 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8303 " 3333333333333333;", 8304 Style); 8305 verifyFormat( 8306 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8307 " dddddddddddddddddd) :\n" 8308 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8309 " 3333333333333333;", 8310 Style); 8311 verifyFormat( 8312 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8313 " dddddddddddddddddd) :\n" 8314 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8315 " 3333333333333333;", 8316 Style); 8317 verifyFormat( 8318 "return aaaaaaaaa ? 1111111111111111 :\n" 8319 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8320 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8321 " dddddddddddddddddd)\n", 8322 Style); 8323 verifyFormat( 8324 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8325 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8326 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8327 " cccccccccccccccccc);", 8328 Style); 8329 verifyFormat( 8330 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8331 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8332 " eeeeeeeeeeeeeeeeee) :\n" 8333 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8334 " 3333333333333333;", 8335 Style); 8336 verifyFormat( 8337 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8338 " ccccccccccccc ? dddddddddddddddddd :\n" 8339 " eeeeeeeeeeeeeeeeee) :\n" 8340 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8341 " 3333333333333333;", 8342 Style); 8343 verifyFormat( 8344 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8345 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8346 " eeeeeeeeeeeeeeeeee) :\n" 8347 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8348 " 3333333333333333;", 8349 Style); 8350 verifyFormat( 8351 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8352 " cccccccccccccccccc :\n" 8353 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8354 " 3333333333333333;", 8355 Style); 8356 verifyFormat( 8357 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8358 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8359 " eeeeeeeeeeeeeeeeee :\n" 8360 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8361 " 3333333333333333;", 8362 Style); 8363 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8364 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8365 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8366 " eeeeeeeeeeeeeeeeee) :\n" 8367 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8368 " 3333333333333333;", 8369 Style); 8370 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8371 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8372 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 8373 " eeeeeeeeeeeeeeeeee :\n" 8374 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8375 " 3333333333333333;", 8376 Style); 8377 } 8378 8379 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 8380 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 8381 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 8382 verifyFormat("bool a = true, b = false;"); 8383 8384 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 8386 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 8387 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 8388 verifyFormat( 8389 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 8390 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 8391 " d = e && f;"); 8392 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 8393 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 8394 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8395 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 8396 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 8397 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 8398 8399 FormatStyle Style = getGoogleStyle(); 8400 Style.PointerAlignment = FormatStyle::PAS_Left; 8401 Style.DerivePointerAlignment = false; 8402 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8403 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 8404 " *b = bbbbbbbbbbbbbbbbbbb;", 8405 Style); 8406 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8407 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 8408 Style); 8409 verifyFormat("vector<int*> a, b;", Style); 8410 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 8411 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style); 8412 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style); 8413 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}", 8414 Style); 8415 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}", 8416 Style); 8417 verifyFormat( 8418 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}", 8419 Style); 8420 } 8421 8422 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 8423 verifyFormat("arr[foo ? bar : baz];"); 8424 verifyFormat("f()[foo ? bar : baz];"); 8425 verifyFormat("(a + b)[foo ? bar : baz];"); 8426 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 8427 } 8428 8429 TEST_F(FormatTest, AlignsStringLiterals) { 8430 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 8431 " \"short literal\");"); 8432 verifyFormat( 8433 "looooooooooooooooooooooooongFunction(\n" 8434 " \"short literal\"\n" 8435 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 8436 verifyFormat("someFunction(\"Always break between multi-line\"\n" 8437 " \" string literals\",\n" 8438 " and, other, parameters);"); 8439 EXPECT_EQ("fun + \"1243\" /* comment */\n" 8440 " \"5678\";", 8441 format("fun + \"1243\" /* comment */\n" 8442 " \"5678\";", 8443 getLLVMStyleWithColumns(28))); 8444 EXPECT_EQ( 8445 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8446 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 8447 " \"aaaaaaaaaaaaaaaa\";", 8448 format("aaaaaa =" 8449 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8450 "aaaaaaaaaaaaaaaaaaaaa\" " 8451 "\"aaaaaaaaaaaaaaaa\";")); 8452 verifyFormat("a = a + \"a\"\n" 8453 " \"a\"\n" 8454 " \"a\";"); 8455 verifyFormat("f(\"a\", \"b\"\n" 8456 " \"c\");"); 8457 8458 verifyFormat( 8459 "#define LL_FORMAT \"ll\"\n" 8460 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 8461 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 8462 8463 verifyFormat("#define A(X) \\\n" 8464 " \"aaaaa\" #X \"bbbbbb\" \\\n" 8465 " \"ccccc\"", 8466 getLLVMStyleWithColumns(23)); 8467 verifyFormat("#define A \"def\"\n" 8468 "f(\"abc\" A \"ghi\"\n" 8469 " \"jkl\");"); 8470 8471 verifyFormat("f(L\"a\"\n" 8472 " L\"b\");"); 8473 verifyFormat("#define A(X) \\\n" 8474 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 8475 " L\"ccccc\"", 8476 getLLVMStyleWithColumns(25)); 8477 8478 verifyFormat("f(@\"a\"\n" 8479 " @\"b\");"); 8480 verifyFormat("NSString s = @\"a\"\n" 8481 " @\"b\"\n" 8482 " @\"c\";"); 8483 verifyFormat("NSString s = @\"a\"\n" 8484 " \"b\"\n" 8485 " \"c\";"); 8486 } 8487 8488 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8489 FormatStyle Style = getLLVMStyle(); 8490 // No declarations or definitions should be moved to own line. 8491 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8492 verifyFormat("class A {\n" 8493 " int f() { return 1; }\n" 8494 " int g();\n" 8495 "};\n" 8496 "int f() { return 1; }\n" 8497 "int g();\n", 8498 Style); 8499 8500 // All declarations and definitions should have the return type moved to its 8501 // own line. 8502 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8503 Style.TypenameMacros = {"LIST"}; 8504 verifyFormat("SomeType\n" 8505 "funcdecl(LIST(uint64_t));", 8506 Style); 8507 verifyFormat("class E {\n" 8508 " int\n" 8509 " f() {\n" 8510 " return 1;\n" 8511 " }\n" 8512 " int\n" 8513 " g();\n" 8514 "};\n" 8515 "int\n" 8516 "f() {\n" 8517 " return 1;\n" 8518 "}\n" 8519 "int\n" 8520 "g();\n", 8521 Style); 8522 8523 // Top-level definitions, and no kinds of declarations should have the 8524 // return type moved to its own line. 8525 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8526 verifyFormat("class B {\n" 8527 " int f() { return 1; }\n" 8528 " int g();\n" 8529 "};\n" 8530 "int\n" 8531 "f() {\n" 8532 " return 1;\n" 8533 "}\n" 8534 "int g();\n", 8535 Style); 8536 8537 // Top-level definitions and declarations should have the return type moved 8538 // to its own line. 8539 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8540 verifyFormat("class C {\n" 8541 " int f() { return 1; }\n" 8542 " int g();\n" 8543 "};\n" 8544 "int\n" 8545 "f() {\n" 8546 " return 1;\n" 8547 "}\n" 8548 "int\n" 8549 "g();\n", 8550 Style); 8551 8552 // All definitions should have the return type moved to its own line, but no 8553 // kinds of declarations. 8554 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8555 verifyFormat("class D {\n" 8556 " int\n" 8557 " f() {\n" 8558 " return 1;\n" 8559 " }\n" 8560 " int g();\n" 8561 "};\n" 8562 "int\n" 8563 "f() {\n" 8564 " return 1;\n" 8565 "}\n" 8566 "int g();\n", 8567 Style); 8568 verifyFormat("const char *\n" 8569 "f(void) {\n" // Break here. 8570 " return \"\";\n" 8571 "}\n" 8572 "const char *bar(void);\n", // No break here. 8573 Style); 8574 verifyFormat("template <class T>\n" 8575 "T *\n" 8576 "f(T &c) {\n" // Break here. 8577 " return NULL;\n" 8578 "}\n" 8579 "template <class T> T *f(T &c);\n", // No break here. 8580 Style); 8581 verifyFormat("class C {\n" 8582 " int\n" 8583 " operator+() {\n" 8584 " return 1;\n" 8585 " }\n" 8586 " int\n" 8587 " operator()() {\n" 8588 " return 1;\n" 8589 " }\n" 8590 "};\n", 8591 Style); 8592 verifyFormat("void\n" 8593 "A::operator()() {}\n" 8594 "void\n" 8595 "A::operator>>() {}\n" 8596 "void\n" 8597 "A::operator+() {}\n" 8598 "void\n" 8599 "A::operator*() {}\n" 8600 "void\n" 8601 "A::operator->() {}\n" 8602 "void\n" 8603 "A::operator void *() {}\n" 8604 "void\n" 8605 "A::operator void &() {}\n" 8606 "void\n" 8607 "A::operator void &&() {}\n" 8608 "void\n" 8609 "A::operator char *() {}\n" 8610 "void\n" 8611 "A::operator[]() {}\n" 8612 "void\n" 8613 "A::operator!() {}\n" 8614 "void\n" 8615 "A::operator**() {}\n" 8616 "void\n" 8617 "A::operator<Foo> *() {}\n" 8618 "void\n" 8619 "A::operator<Foo> **() {}\n" 8620 "void\n" 8621 "A::operator<Foo> &() {}\n" 8622 "void\n" 8623 "A::operator void **() {}\n", 8624 Style); 8625 verifyFormat("constexpr auto\n" 8626 "operator()() const -> reference {}\n" 8627 "constexpr auto\n" 8628 "operator>>() const -> reference {}\n" 8629 "constexpr auto\n" 8630 "operator+() const -> reference {}\n" 8631 "constexpr auto\n" 8632 "operator*() const -> reference {}\n" 8633 "constexpr auto\n" 8634 "operator->() const -> reference {}\n" 8635 "constexpr auto\n" 8636 "operator++() const -> reference {}\n" 8637 "constexpr auto\n" 8638 "operator void *() const -> reference {}\n" 8639 "constexpr auto\n" 8640 "operator void **() const -> reference {}\n" 8641 "constexpr auto\n" 8642 "operator void *() const -> reference {}\n" 8643 "constexpr auto\n" 8644 "operator void &() const -> reference {}\n" 8645 "constexpr auto\n" 8646 "operator void &&() const -> reference {}\n" 8647 "constexpr auto\n" 8648 "operator char *() const -> reference {}\n" 8649 "constexpr auto\n" 8650 "operator!() const -> reference {}\n" 8651 "constexpr auto\n" 8652 "operator[]() const -> reference {}\n", 8653 Style); 8654 verifyFormat("void *operator new(std::size_t s);", // No break here. 8655 Style); 8656 verifyFormat("void *\n" 8657 "operator new(std::size_t s) {}", 8658 Style); 8659 verifyFormat("void *\n" 8660 "operator delete[](void *ptr) {}", 8661 Style); 8662 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8663 verifyFormat("const char *\n" 8664 "f(void)\n" // Break here. 8665 "{\n" 8666 " return \"\";\n" 8667 "}\n" 8668 "const char *bar(void);\n", // No break here. 8669 Style); 8670 verifyFormat("template <class T>\n" 8671 "T *\n" // Problem here: no line break 8672 "f(T &c)\n" // Break here. 8673 "{\n" 8674 " return NULL;\n" 8675 "}\n" 8676 "template <class T> T *f(T &c);\n", // No break here. 8677 Style); 8678 verifyFormat("int\n" 8679 "foo(A<bool> a)\n" 8680 "{\n" 8681 " return a;\n" 8682 "}\n", 8683 Style); 8684 verifyFormat("int\n" 8685 "foo(A<8> a)\n" 8686 "{\n" 8687 " return a;\n" 8688 "}\n", 8689 Style); 8690 verifyFormat("int\n" 8691 "foo(A<B<bool>, 8> a)\n" 8692 "{\n" 8693 " return a;\n" 8694 "}\n", 8695 Style); 8696 verifyFormat("int\n" 8697 "foo(A<B<8>, bool> a)\n" 8698 "{\n" 8699 " return a;\n" 8700 "}\n", 8701 Style); 8702 verifyFormat("int\n" 8703 "foo(A<B<bool>, bool> a)\n" 8704 "{\n" 8705 " return a;\n" 8706 "}\n", 8707 Style); 8708 verifyFormat("int\n" 8709 "foo(A<B<8>, 8> a)\n" 8710 "{\n" 8711 " return a;\n" 8712 "}\n", 8713 Style); 8714 8715 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8716 Style.BraceWrapping.AfterFunction = true; 8717 verifyFormat("int f(i);\n" // No break here. 8718 "int\n" // Break here. 8719 "f(i)\n" 8720 "{\n" 8721 " return i + 1;\n" 8722 "}\n" 8723 "int\n" // Break here. 8724 "f(i)\n" 8725 "{\n" 8726 " return i + 1;\n" 8727 "};", 8728 Style); 8729 verifyFormat("int f(a, b, c);\n" // No break here. 8730 "int\n" // Break here. 8731 "f(a, b, c)\n" // Break here. 8732 "short a, b;\n" 8733 "float c;\n" 8734 "{\n" 8735 " return a + b < c;\n" 8736 "}\n" 8737 "int\n" // Break here. 8738 "f(a, b, c)\n" // Break here. 8739 "short a, b;\n" 8740 "float c;\n" 8741 "{\n" 8742 " return a + b < c;\n" 8743 "};", 8744 Style); 8745 verifyFormat("byte *\n" // Break here. 8746 "f(a)\n" // Break here. 8747 "byte a[];\n" 8748 "{\n" 8749 " return a;\n" 8750 "}", 8751 Style); 8752 verifyFormat("bool f(int a, int) override;\n" 8753 "Bar g(int a, Bar) final;\n" 8754 "Bar h(a, Bar) final;", 8755 Style); 8756 verifyFormat("int\n" 8757 "f(a)", 8758 Style); 8759 verifyFormat("bool\n" 8760 "f(size_t = 0, bool b = false)\n" 8761 "{\n" 8762 " return !b;\n" 8763 "}", 8764 Style); 8765 8766 // The return breaking style doesn't affect: 8767 // * function and object definitions with attribute-like macros 8768 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8769 " ABSL_GUARDED_BY(mutex) = {};", 8770 getGoogleStyleWithColumns(40)); 8771 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8772 " ABSL_GUARDED_BY(mutex); // comment", 8773 getGoogleStyleWithColumns(40)); 8774 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8775 " ABSL_GUARDED_BY(mutex1)\n" 8776 " ABSL_GUARDED_BY(mutex2);", 8777 getGoogleStyleWithColumns(40)); 8778 verifyFormat("Tttttt f(int a, int b)\n" 8779 " ABSL_GUARDED_BY(mutex1)\n" 8780 " ABSL_GUARDED_BY(mutex2);", 8781 getGoogleStyleWithColumns(40)); 8782 // * typedefs 8783 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8784 8785 Style = getGNUStyle(); 8786 8787 // Test for comments at the end of function declarations. 8788 verifyFormat("void\n" 8789 "foo (int a, /*abc*/ int b) // def\n" 8790 "{\n" 8791 "}\n", 8792 Style); 8793 8794 verifyFormat("void\n" 8795 "foo (int a, /* abc */ int b) /* def */\n" 8796 "{\n" 8797 "}\n", 8798 Style); 8799 8800 // Definitions that should not break after return type 8801 verifyFormat("void foo (int a, int b); // def\n", Style); 8802 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8803 verifyFormat("void foo (int a, int b);\n", Style); 8804 } 8805 8806 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8807 FormatStyle NoBreak = getLLVMStyle(); 8808 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8809 FormatStyle Break = getLLVMStyle(); 8810 Break.AlwaysBreakBeforeMultilineStrings = true; 8811 verifyFormat("aaaa = \"bbbb\"\n" 8812 " \"cccc\";", 8813 NoBreak); 8814 verifyFormat("aaaa =\n" 8815 " \"bbbb\"\n" 8816 " \"cccc\";", 8817 Break); 8818 verifyFormat("aaaa(\"bbbb\"\n" 8819 " \"cccc\");", 8820 NoBreak); 8821 verifyFormat("aaaa(\n" 8822 " \"bbbb\"\n" 8823 " \"cccc\");", 8824 Break); 8825 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8826 " \"cccc\");", 8827 NoBreak); 8828 verifyFormat("aaaa(qqq,\n" 8829 " \"bbbb\"\n" 8830 " \"cccc\");", 8831 Break); 8832 verifyFormat("aaaa(qqq,\n" 8833 " L\"bbbb\"\n" 8834 " L\"cccc\");", 8835 Break); 8836 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8837 " \"bbbb\"));", 8838 Break); 8839 verifyFormat("string s = someFunction(\n" 8840 " \"abc\"\n" 8841 " \"abc\");", 8842 Break); 8843 8844 // As we break before unary operators, breaking right after them is bad. 8845 verifyFormat("string foo = abc ? \"x\"\n" 8846 " \"blah blah blah blah blah blah\"\n" 8847 " : \"y\";", 8848 Break); 8849 8850 // Don't break if there is no column gain. 8851 verifyFormat("f(\"aaaa\"\n" 8852 " \"bbbb\");", 8853 Break); 8854 8855 // Treat literals with escaped newlines like multi-line string literals. 8856 EXPECT_EQ("x = \"a\\\n" 8857 "b\\\n" 8858 "c\";", 8859 format("x = \"a\\\n" 8860 "b\\\n" 8861 "c\";", 8862 NoBreak)); 8863 EXPECT_EQ("xxxx =\n" 8864 " \"a\\\n" 8865 "b\\\n" 8866 "c\";", 8867 format("xxxx = \"a\\\n" 8868 "b\\\n" 8869 "c\";", 8870 Break)); 8871 8872 EXPECT_EQ("NSString *const kString =\n" 8873 " @\"aaaa\"\n" 8874 " @\"bbbb\";", 8875 format("NSString *const kString = @\"aaaa\"\n" 8876 "@\"bbbb\";", 8877 Break)); 8878 8879 Break.ColumnLimit = 0; 8880 verifyFormat("const char *hello = \"hello llvm\";", Break); 8881 } 8882 8883 TEST_F(FormatTest, AlignsPipes) { 8884 verifyFormat( 8885 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8886 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8887 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8888 verifyFormat( 8889 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8890 " << aaaaaaaaaaaaaaaaaaaa;"); 8891 verifyFormat( 8892 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8893 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8894 verifyFormat( 8895 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8896 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8897 verifyFormat( 8898 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8899 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8900 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8901 verifyFormat( 8902 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8903 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8904 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8905 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8908 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8909 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8910 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8911 verifyFormat( 8912 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8913 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8914 verifyFormat( 8915 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8916 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8917 8918 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8919 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8920 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8922 " aaaaaaaaaaaaaaaaaaaaa)\n" 8923 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8924 verifyFormat("LOG_IF(aaa == //\n" 8925 " bbb)\n" 8926 " << a << b;"); 8927 8928 // But sometimes, breaking before the first "<<" is desirable. 8929 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8930 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8931 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8932 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8933 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8934 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8935 " << BEF << IsTemplate << Description << E->getType();"); 8936 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8937 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8939 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8940 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8942 " << aaa;"); 8943 8944 verifyFormat( 8945 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8946 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8947 8948 // Incomplete string literal. 8949 EXPECT_EQ("llvm::errs() << \"\n" 8950 " << a;", 8951 format("llvm::errs() << \"\n<<a;")); 8952 8953 verifyFormat("void f() {\n" 8954 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8955 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8956 "}"); 8957 8958 // Handle 'endl'. 8959 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8960 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8961 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8962 8963 // Handle '\n'. 8964 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8965 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8966 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8967 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8968 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8969 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8970 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8971 } 8972 8973 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8974 verifyFormat("return out << \"somepacket = {\\n\"\n" 8975 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8976 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8977 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8978 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8979 " << \"}\";"); 8980 8981 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8982 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8983 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8984 verifyFormat( 8985 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8986 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8987 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8988 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8989 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8990 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8991 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8992 verifyFormat( 8993 "void f() {\n" 8994 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8995 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8996 "}"); 8997 8998 // Breaking before the first "<<" is generally not desirable. 8999 verifyFormat( 9000 "llvm::errs()\n" 9001 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9002 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9003 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9004 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 9005 getLLVMStyleWithColumns(70)); 9006 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 9007 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9008 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 9009 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9010 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 9011 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 9012 getLLVMStyleWithColumns(70)); 9013 9014 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 9015 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 9016 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 9017 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 9018 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 9019 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 9020 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 9021 " (aaaa + aaaa);", 9022 getLLVMStyleWithColumns(40)); 9023 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 9024 " (aaaaaaa + aaaaa));", 9025 getLLVMStyleWithColumns(40)); 9026 verifyFormat( 9027 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 9028 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 9029 " bbbbbbbbbbbbbbbbbbbbbbb);"); 9030 } 9031 9032 TEST_F(FormatTest, UnderstandsEquals) { 9033 verifyFormat( 9034 "aaaaaaaaaaaaaaaaa =\n" 9035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9036 verifyFormat( 9037 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 9039 verifyFormat( 9040 "if (a) {\n" 9041 " f();\n" 9042 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 9044 "}"); 9045 9046 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9047 " 100000000 + 10000000) {\n}"); 9048 } 9049 9050 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 9051 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 9052 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 9053 9054 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 9055 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 9056 9057 verifyFormat( 9058 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 9059 " Parameter2);"); 9060 9061 verifyFormat( 9062 "ShortObject->shortFunction(\n" 9063 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 9064 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 9065 9066 verifyFormat("loooooooooooooongFunction(\n" 9067 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 9068 9069 verifyFormat( 9070 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 9071 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 9072 9073 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 9074 " .WillRepeatedly(Return(SomeValue));"); 9075 verifyFormat("void f() {\n" 9076 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 9077 " .Times(2)\n" 9078 " .WillRepeatedly(Return(SomeValue));\n" 9079 "}"); 9080 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 9081 " ccccccccccccccccccccccc);"); 9082 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9083 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9084 " .aaaaa(aaaaa),\n" 9085 " aaaaaaaaaaaaaaaaaaaaa);"); 9086 verifyFormat("void f() {\n" 9087 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9088 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 9089 "}"); 9090 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9091 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9092 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9093 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9094 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9095 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9096 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9097 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9098 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 9099 "}"); 9100 9101 // Here, it is not necessary to wrap at "." or "->". 9102 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 9103 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 9104 verifyFormat( 9105 "aaaaaaaaaaa->aaaaaaaaa(\n" 9106 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9107 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 9108 9109 verifyFormat( 9110 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9111 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 9112 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 9113 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 9114 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 9115 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 9116 9117 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9119 " .a();"); 9120 9121 FormatStyle NoBinPacking = getLLVMStyle(); 9122 NoBinPacking.BinPackParameters = false; 9123 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 9124 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 9125 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 9126 " aaaaaaaaaaaaaaaaaaa,\n" 9127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9128 NoBinPacking); 9129 9130 // If there is a subsequent call, change to hanging indentation. 9131 verifyFormat( 9132 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9133 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 9134 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9135 verifyFormat( 9136 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9137 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 9138 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9140 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9141 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9143 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9144 } 9145 9146 TEST_F(FormatTest, WrapsTemplateDeclarations) { 9147 verifyFormat("template <typename T>\n" 9148 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9149 verifyFormat("template <typename T>\n" 9150 "// T should be one of {A, B}.\n" 9151 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9152 verifyFormat( 9153 "template <typename T>\n" 9154 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 9155 verifyFormat("template <typename T>\n" 9156 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 9157 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 9158 verifyFormat( 9159 "template <typename T>\n" 9160 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 9161 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 9162 verifyFormat( 9163 "template <typename T>\n" 9164 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 9165 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 9166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9167 verifyFormat("template <typename T>\n" 9168 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9169 " int aaaaaaaaaaaaaaaaaaaaaa);"); 9170 verifyFormat( 9171 "template <typename T1, typename T2 = char, typename T3 = char,\n" 9172 " typename T4 = char>\n" 9173 "void f();"); 9174 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 9175 " template <typename> class cccccccccccccccccccccc,\n" 9176 " typename ddddddddddddd>\n" 9177 "class C {};"); 9178 verifyFormat( 9179 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 9180 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9181 9182 verifyFormat("void f() {\n" 9183 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 9184 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 9185 "}"); 9186 9187 verifyFormat("template <typename T> class C {};"); 9188 verifyFormat("template <typename T> void f();"); 9189 verifyFormat("template <typename T> void f() {}"); 9190 verifyFormat( 9191 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9193 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 9194 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9195 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 9197 " bbbbbbbbbbbbbbbbbbbbbbbb);", 9198 getLLVMStyleWithColumns(72)); 9199 EXPECT_EQ("static_cast<A< //\n" 9200 " B> *>(\n" 9201 "\n" 9202 ");", 9203 format("static_cast<A<//\n" 9204 " B>*>(\n" 9205 "\n" 9206 " );")); 9207 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9208 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 9209 9210 FormatStyle AlwaysBreak = getLLVMStyle(); 9211 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9212 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 9213 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 9214 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 9215 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9216 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9217 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 9218 verifyFormat("template <template <typename> class Fooooooo,\n" 9219 " template <typename> class Baaaaaaar>\n" 9220 "struct C {};", 9221 AlwaysBreak); 9222 verifyFormat("template <typename T> // T can be A, B or C.\n" 9223 "struct C {};", 9224 AlwaysBreak); 9225 verifyFormat("template <enum E> class A {\n" 9226 "public:\n" 9227 " E *f();\n" 9228 "};"); 9229 9230 FormatStyle NeverBreak = getLLVMStyle(); 9231 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 9232 verifyFormat("template <typename T> class C {};", NeverBreak); 9233 verifyFormat("template <typename T> void f();", NeverBreak); 9234 verifyFormat("template <typename T> void f() {}", NeverBreak); 9235 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9236 "bbbbbbbbbbbbbbbbbbbb) {}", 9237 NeverBreak); 9238 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9239 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9240 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 9241 NeverBreak); 9242 verifyFormat("template <template <typename> class Fooooooo,\n" 9243 " template <typename> class Baaaaaaar>\n" 9244 "struct C {};", 9245 NeverBreak); 9246 verifyFormat("template <typename T> // T can be A, B or C.\n" 9247 "struct C {};", 9248 NeverBreak); 9249 verifyFormat("template <enum E> class A {\n" 9250 "public:\n" 9251 " E *f();\n" 9252 "};", 9253 NeverBreak); 9254 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 9255 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9256 "bbbbbbbbbbbbbbbbbbbb) {}", 9257 NeverBreak); 9258 } 9259 9260 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 9261 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 9262 Style.ColumnLimit = 60; 9263 EXPECT_EQ("// Baseline - no comments.\n" 9264 "template <\n" 9265 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9266 "void f() {}", 9267 format("// Baseline - no comments.\n" 9268 "template <\n" 9269 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9270 "void f() {}", 9271 Style)); 9272 9273 EXPECT_EQ("template <\n" 9274 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9275 "void f() {}", 9276 format("template <\n" 9277 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9278 "void f() {}", 9279 Style)); 9280 9281 EXPECT_EQ( 9282 "template <\n" 9283 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9284 "void f() {}", 9285 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9286 "void f() {}", 9287 Style)); 9288 9289 EXPECT_EQ( 9290 "template <\n" 9291 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9292 " // multiline\n" 9293 "void f() {}", 9294 format("template <\n" 9295 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9296 " // multiline\n" 9297 "void f() {}", 9298 Style)); 9299 9300 EXPECT_EQ( 9301 "template <typename aaaaaaaaaa<\n" 9302 " bbbbbbbbbbbb>::value> // trailing loooong\n" 9303 "void f() {}", 9304 format( 9305 "template <\n" 9306 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 9307 "void f() {}", 9308 Style)); 9309 } 9310 9311 TEST_F(FormatTest, WrapsTemplateParameters) { 9312 FormatStyle Style = getLLVMStyle(); 9313 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9314 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9315 verifyFormat( 9316 "template <typename... a> struct q {};\n" 9317 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9318 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9319 " y;", 9320 Style); 9321 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9322 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9323 verifyFormat( 9324 "template <typename... a> struct r {};\n" 9325 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9326 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9327 " y;", 9328 Style); 9329 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9330 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9331 verifyFormat("template <typename... a> struct s {};\n" 9332 "extern s<\n" 9333 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9334 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9335 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9336 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9337 " y;", 9338 Style); 9339 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9340 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9341 verifyFormat("template <typename... a> struct t {};\n" 9342 "extern t<\n" 9343 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9344 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9345 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9346 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9347 " y;", 9348 Style); 9349 } 9350 9351 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 9352 verifyFormat( 9353 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9355 verifyFormat( 9356 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9358 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9359 9360 // FIXME: Should we have the extra indent after the second break? 9361 verifyFormat( 9362 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9365 9366 verifyFormat( 9367 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 9368 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 9369 9370 // Breaking at nested name specifiers is generally not desirable. 9371 verifyFormat( 9372 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9373 " aaaaaaaaaaaaaaaaaaaaaaa);"); 9374 9375 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 9376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9378 " aaaaaaaaaaaaaaaaaaaaa);", 9379 getLLVMStyleWithColumns(74)); 9380 9381 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9383 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9384 } 9385 9386 TEST_F(FormatTest, UnderstandsTemplateParameters) { 9387 verifyFormat("A<int> a;"); 9388 verifyFormat("A<A<A<int>>> a;"); 9389 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 9390 verifyFormat("bool x = a < 1 || 2 > a;"); 9391 verifyFormat("bool x = 5 < f<int>();"); 9392 verifyFormat("bool x = f<int>() > 5;"); 9393 verifyFormat("bool x = 5 < a<int>::x;"); 9394 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 9395 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 9396 9397 verifyGoogleFormat("A<A<int>> a;"); 9398 verifyGoogleFormat("A<A<A<int>>> a;"); 9399 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 9400 verifyGoogleFormat("A<A<int> > a;"); 9401 verifyGoogleFormat("A<A<A<int> > > a;"); 9402 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 9403 verifyGoogleFormat("A<::A<int>> a;"); 9404 verifyGoogleFormat("A<::A> a;"); 9405 verifyGoogleFormat("A< ::A> a;"); 9406 verifyGoogleFormat("A< ::A<int> > a;"); 9407 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 9408 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 9409 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 9410 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 9411 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 9412 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 9413 9414 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 9415 9416 // template closer followed by a token that starts with > or = 9417 verifyFormat("bool b = a<1> > 1;"); 9418 verifyFormat("bool b = a<1> >= 1;"); 9419 verifyFormat("int i = a<1> >> 1;"); 9420 FormatStyle Style = getLLVMStyle(); 9421 Style.SpaceBeforeAssignmentOperators = false; 9422 verifyFormat("bool b= a<1> == 1;", Style); 9423 verifyFormat("a<int> = 1;", Style); 9424 verifyFormat("a<int> >>= 1;", Style); 9425 9426 verifyFormat("test < a | b >> c;"); 9427 verifyFormat("test<test<a | b>> c;"); 9428 verifyFormat("test >> a >> b;"); 9429 verifyFormat("test << a >> b;"); 9430 9431 verifyFormat("f<int>();"); 9432 verifyFormat("template <typename T> void f() {}"); 9433 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 9434 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 9435 "sizeof(char)>::type>;"); 9436 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 9437 verifyFormat("f(a.operator()<A>());"); 9438 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9439 " .template operator()<A>());", 9440 getLLVMStyleWithColumns(35)); 9441 9442 // Not template parameters. 9443 verifyFormat("return a < b && c > d;"); 9444 verifyFormat("void f() {\n" 9445 " while (a < b && c > d) {\n" 9446 " }\n" 9447 "}"); 9448 verifyFormat("template <typename... Types>\n" 9449 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 9450 9451 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 9453 getLLVMStyleWithColumns(60)); 9454 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 9455 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 9456 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 9457 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 9458 } 9459 9460 TEST_F(FormatTest, UnderstandsShiftOperators) { 9461 verifyFormat("if (i < x >> 1)"); 9462 verifyFormat("while (i < x >> 1)"); 9463 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 9464 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 9465 verifyFormat( 9466 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 9467 verifyFormat("Foo.call<Bar<Function>>()"); 9468 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 9469 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 9470 "++i, v = v >> 1)"); 9471 verifyFormat("if (w<u<v<x>>, 1>::t)"); 9472 } 9473 9474 TEST_F(FormatTest, BitshiftOperatorWidth) { 9475 EXPECT_EQ("int a = 1 << 2; /* foo\n" 9476 " bar */", 9477 format("int a=1<<2; /* foo\n" 9478 " bar */")); 9479 9480 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 9481 " bar */", 9482 format("int b =256>>1 ; /* foo\n" 9483 " bar */")); 9484 } 9485 9486 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9487 verifyFormat("COMPARE(a, ==, b);"); 9488 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9489 } 9490 9491 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9492 verifyFormat("int A::*x;"); 9493 verifyFormat("int (S::*func)(void *);"); 9494 verifyFormat("void f() { int (S::*func)(void *); }"); 9495 verifyFormat("typedef bool *(Class::*Member)() const;"); 9496 verifyFormat("void f() {\n" 9497 " (a->*f)();\n" 9498 " a->*x;\n" 9499 " (a.*f)();\n" 9500 " ((*a).*f)();\n" 9501 " a.*x;\n" 9502 "}"); 9503 verifyFormat("void f() {\n" 9504 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9505 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9506 "}"); 9507 verifyFormat( 9508 "(aaaaaaaaaa->*bbbbbbb)(\n" 9509 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9510 FormatStyle Style = getLLVMStyle(); 9511 Style.PointerAlignment = FormatStyle::PAS_Left; 9512 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9513 } 9514 9515 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9516 verifyFormat("int a = -2;"); 9517 verifyFormat("f(-1, -2, -3);"); 9518 verifyFormat("a[-1] = 5;"); 9519 verifyFormat("int a = 5 + -2;"); 9520 verifyFormat("if (i == -1) {\n}"); 9521 verifyFormat("if (i != -1) {\n}"); 9522 verifyFormat("if (i > -1) {\n}"); 9523 verifyFormat("if (i < -1) {\n}"); 9524 verifyFormat("++(a->f());"); 9525 verifyFormat("--(a->f());"); 9526 verifyFormat("(a->f())++;"); 9527 verifyFormat("a[42]++;"); 9528 verifyFormat("if (!(a->f())) {\n}"); 9529 verifyFormat("if (!+i) {\n}"); 9530 verifyFormat("~&a;"); 9531 9532 verifyFormat("a-- > b;"); 9533 verifyFormat("b ? -a : c;"); 9534 verifyFormat("n * sizeof char16;"); 9535 verifyFormat("n * alignof char16;", getGoogleStyle()); 9536 verifyFormat("sizeof(char);"); 9537 verifyFormat("alignof(char);", getGoogleStyle()); 9538 9539 verifyFormat("return -1;"); 9540 verifyFormat("throw -1;"); 9541 verifyFormat("switch (a) {\n" 9542 "case -1:\n" 9543 " break;\n" 9544 "}"); 9545 verifyFormat("#define X -1"); 9546 verifyFormat("#define X -kConstant"); 9547 9548 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9549 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9550 9551 verifyFormat("int a = /* confusing comment */ -1;"); 9552 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9553 verifyFormat("int a = i /* confusing comment */++;"); 9554 9555 verifyFormat("co_yield -1;"); 9556 verifyFormat("co_return -1;"); 9557 9558 // Check that * is not treated as a binary operator when we set 9559 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9560 FormatStyle PASLeftStyle = getLLVMStyle(); 9561 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9562 verifyFormat("co_return *a;", PASLeftStyle); 9563 verifyFormat("co_await *a;", PASLeftStyle); 9564 verifyFormat("co_yield *a", PASLeftStyle); 9565 verifyFormat("return *a;", PASLeftStyle); 9566 } 9567 9568 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9569 verifyFormat("if (!aaaaaaaaaa( // break\n" 9570 " aaaaa)) {\n" 9571 "}"); 9572 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9573 " aaaaa));"); 9574 verifyFormat("*aaa = aaaaaaa( // break\n" 9575 " bbbbbb);"); 9576 } 9577 9578 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9579 verifyFormat("bool operator<();"); 9580 verifyFormat("bool operator>();"); 9581 verifyFormat("bool operator=();"); 9582 verifyFormat("bool operator==();"); 9583 verifyFormat("bool operator!=();"); 9584 verifyFormat("int operator+();"); 9585 verifyFormat("int operator++();"); 9586 verifyFormat("int operator++(int) volatile noexcept;"); 9587 verifyFormat("bool operator,();"); 9588 verifyFormat("bool operator();"); 9589 verifyFormat("bool operator()();"); 9590 verifyFormat("bool operator[]();"); 9591 verifyFormat("operator bool();"); 9592 verifyFormat("operator int();"); 9593 verifyFormat("operator void *();"); 9594 verifyFormat("operator SomeType<int>();"); 9595 verifyFormat("operator SomeType<int, int>();"); 9596 verifyFormat("operator SomeType<SomeType<int>>();"); 9597 verifyFormat("operator< <>();"); 9598 verifyFormat("operator<< <>();"); 9599 verifyFormat("< <>"); 9600 9601 verifyFormat("void *operator new(std::size_t size);"); 9602 verifyFormat("void *operator new[](std::size_t size);"); 9603 verifyFormat("void operator delete(void *ptr);"); 9604 verifyFormat("void operator delete[](void *ptr);"); 9605 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9606 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9607 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9608 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9609 9610 verifyFormat( 9611 "ostream &operator<<(ostream &OutputStream,\n" 9612 " SomeReallyLongType WithSomeReallyLongValue);"); 9613 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9614 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9615 " return left.group < right.group;\n" 9616 "}"); 9617 verifyFormat("SomeType &operator=(const SomeType &S);"); 9618 verifyFormat("f.template operator()<int>();"); 9619 9620 verifyGoogleFormat("operator void*();"); 9621 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9622 verifyGoogleFormat("operator ::A();"); 9623 9624 verifyFormat("using A::operator+;"); 9625 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9626 "int i;"); 9627 9628 // Calling an operator as a member function. 9629 verifyFormat("void f() { a.operator*(); }"); 9630 verifyFormat("void f() { a.operator*(b & b); }"); 9631 verifyFormat("void f() { a->operator&(a * b); }"); 9632 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9633 // TODO: Calling an operator as a non-member function is hard to distinguish. 9634 // https://llvm.org/PR50629 9635 // verifyFormat("void f() { operator*(a & a); }"); 9636 // verifyFormat("void f() { operator&(a, b * b); }"); 9637 9638 verifyFormat("::operator delete(foo);"); 9639 verifyFormat("::operator new(n * sizeof(foo));"); 9640 verifyFormat("foo() { ::operator delete(foo); }"); 9641 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 9642 } 9643 9644 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9645 verifyFormat("void A::b() && {}"); 9646 verifyFormat("void A::b() &&noexcept {}"); 9647 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9648 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9649 verifyFormat("Deleted &operator=(const Deleted &) &noexcept = default;"); 9650 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9651 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9652 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9653 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9654 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9655 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9656 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9657 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9658 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9659 verifyFormat("SomeType MemberFunction(const Deleted &) &&noexcept {}"); 9660 verifyFormat("void Fn(T const &) const &;"); 9661 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9662 verifyFormat("void Fn(T const volatile &&) const volatile &&noexcept;"); 9663 verifyFormat("template <typename T>\n" 9664 "void F(T) && = delete;", 9665 getGoogleStyle()); 9666 9667 FormatStyle AlignLeft = getLLVMStyle(); 9668 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9669 verifyFormat("void A::b() && {}", AlignLeft); 9670 verifyFormat("void A::b() && noexcept {}", AlignLeft); 9671 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9672 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;", 9673 AlignLeft); 9674 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9675 AlignLeft); 9676 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9677 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9678 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9679 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9680 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9681 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9682 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9683 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9684 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;", 9685 AlignLeft); 9686 9687 FormatStyle AlignMiddle = getLLVMStyle(); 9688 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9689 verifyFormat("void A::b() && {}", AlignMiddle); 9690 verifyFormat("void A::b() && noexcept {}", AlignMiddle); 9691 verifyFormat("Deleted & operator=(const Deleted &) & = default;", 9692 AlignMiddle); 9693 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;", 9694 AlignMiddle); 9695 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", 9696 AlignMiddle); 9697 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle); 9698 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle); 9699 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle); 9700 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle); 9701 verifyFormat("auto Function(T) & -> void {}", AlignMiddle); 9702 verifyFormat("auto Function(T) & -> void;", AlignMiddle); 9703 verifyFormat("void Fn(T const &) const &;", AlignMiddle); 9704 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle); 9705 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;", 9706 AlignMiddle); 9707 9708 FormatStyle Spaces = getLLVMStyle(); 9709 Spaces.SpacesInCStyleCastParentheses = true; 9710 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9711 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9712 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9713 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9714 9715 Spaces.SpacesInCStyleCastParentheses = false; 9716 Spaces.SpacesInParentheses = true; 9717 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9718 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9719 Spaces); 9720 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9721 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9722 9723 FormatStyle BreakTemplate = getLLVMStyle(); 9724 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9725 9726 verifyFormat("struct f {\n" 9727 " template <class T>\n" 9728 " int &foo(const std::string &str) &noexcept {}\n" 9729 "};", 9730 BreakTemplate); 9731 9732 verifyFormat("struct f {\n" 9733 " template <class T>\n" 9734 " int &foo(const std::string &str) &&noexcept {}\n" 9735 "};", 9736 BreakTemplate); 9737 9738 verifyFormat("struct f {\n" 9739 " template <class T>\n" 9740 " int &foo(const std::string &str) const &noexcept {}\n" 9741 "};", 9742 BreakTemplate); 9743 9744 verifyFormat("struct f {\n" 9745 " template <class T>\n" 9746 " int &foo(const std::string &str) const &noexcept {}\n" 9747 "};", 9748 BreakTemplate); 9749 9750 verifyFormat("struct f {\n" 9751 " template <class T>\n" 9752 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9753 "};", 9754 BreakTemplate); 9755 9756 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9757 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9758 FormatStyle::BTDS_Yes; 9759 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9760 9761 verifyFormat("struct f {\n" 9762 " template <class T>\n" 9763 " int& foo(const std::string& str) & noexcept {}\n" 9764 "};", 9765 AlignLeftBreakTemplate); 9766 9767 verifyFormat("struct f {\n" 9768 " template <class T>\n" 9769 " int& foo(const std::string& str) && noexcept {}\n" 9770 "};", 9771 AlignLeftBreakTemplate); 9772 9773 verifyFormat("struct f {\n" 9774 " template <class T>\n" 9775 " int& foo(const std::string& str) const& noexcept {}\n" 9776 "};", 9777 AlignLeftBreakTemplate); 9778 9779 verifyFormat("struct f {\n" 9780 " template <class T>\n" 9781 " int& foo(const std::string& str) const&& noexcept {}\n" 9782 "};", 9783 AlignLeftBreakTemplate); 9784 9785 verifyFormat("struct f {\n" 9786 " template <class T>\n" 9787 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9788 "};", 9789 AlignLeftBreakTemplate); 9790 9791 // The `&` in `Type&` should not be confused with a trailing `&` of 9792 // DEPRECATED(reason) member function. 9793 verifyFormat("struct f {\n" 9794 " template <class T>\n" 9795 " DEPRECATED(reason)\n" 9796 " Type &foo(arguments) {}\n" 9797 "};", 9798 BreakTemplate); 9799 9800 verifyFormat("struct f {\n" 9801 " template <class T>\n" 9802 " DEPRECATED(reason)\n" 9803 " Type& foo(arguments) {}\n" 9804 "};", 9805 AlignLeftBreakTemplate); 9806 9807 verifyFormat("void (*foopt)(int) = &func;"); 9808 9809 FormatStyle DerivePointerAlignment = getLLVMStyle(); 9810 DerivePointerAlignment.DerivePointerAlignment = true; 9811 // There's always a space between the function and its trailing qualifiers. 9812 // This isn't evidence for PAS_Right (or for PAS_Left). 9813 std::string Prefix = "void a() &;\n" 9814 "void b() &;\n"; 9815 verifyFormat(Prefix + "int* x;", DerivePointerAlignment); 9816 verifyFormat(Prefix + "int *x;", DerivePointerAlignment); 9817 // Same if the function is an overloaded operator, and with &&. 9818 Prefix = "void operator()() &&;\n" 9819 "void operator()() &&;\n"; 9820 verifyFormat(Prefix + "int* x;", DerivePointerAlignment); 9821 verifyFormat(Prefix + "int *x;", DerivePointerAlignment); 9822 // However a space between cv-qualifiers and ref-qualifiers *is* evidence. 9823 Prefix = "void a() const &;\n" 9824 "void b() const &;\n"; 9825 EXPECT_EQ(Prefix + "int *x;", 9826 format(Prefix + "int* x;", DerivePointerAlignment)); 9827 } 9828 9829 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9830 verifyFormat("void f() {\n" 9831 " A *a = new A;\n" 9832 " A *a = new (placement) A;\n" 9833 " delete a;\n" 9834 " delete (A *)a;\n" 9835 "}"); 9836 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9837 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9838 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9839 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9840 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9841 verifyFormat("delete[] h->p;"); 9842 verifyFormat("delete[] (void *)p;"); 9843 9844 verifyFormat("void operator delete(void *foo) ATTRIB;"); 9845 verifyFormat("void operator new(void *foo) ATTRIB;"); 9846 verifyFormat("void operator delete[](void *foo) ATTRIB;"); 9847 verifyFormat("void operator delete(void *ptr) noexcept;"); 9848 } 9849 9850 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9851 verifyFormat("int *f(int *a) {}"); 9852 verifyFormat("int main(int argc, char **argv) {}"); 9853 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9854 verifyIndependentOfContext("f(a, *a);"); 9855 verifyFormat("void g() { f(*a); }"); 9856 verifyIndependentOfContext("int a = b * 10;"); 9857 verifyIndependentOfContext("int a = 10 * b;"); 9858 verifyIndependentOfContext("int a = b * c;"); 9859 verifyIndependentOfContext("int a += b * c;"); 9860 verifyIndependentOfContext("int a -= b * c;"); 9861 verifyIndependentOfContext("int a *= b * c;"); 9862 verifyIndependentOfContext("int a /= b * c;"); 9863 verifyIndependentOfContext("int a = *b;"); 9864 verifyIndependentOfContext("int a = *b * c;"); 9865 verifyIndependentOfContext("int a = b * *c;"); 9866 verifyIndependentOfContext("int a = b * (10);"); 9867 verifyIndependentOfContext("S << b * (10);"); 9868 verifyIndependentOfContext("return 10 * b;"); 9869 verifyIndependentOfContext("return *b * *c;"); 9870 verifyIndependentOfContext("return a & ~b;"); 9871 verifyIndependentOfContext("f(b ? *c : *d);"); 9872 verifyIndependentOfContext("int a = b ? *c : *d;"); 9873 verifyIndependentOfContext("*b = a;"); 9874 verifyIndependentOfContext("a * ~b;"); 9875 verifyIndependentOfContext("a * !b;"); 9876 verifyIndependentOfContext("a * +b;"); 9877 verifyIndependentOfContext("a * -b;"); 9878 verifyIndependentOfContext("a * ++b;"); 9879 verifyIndependentOfContext("a * --b;"); 9880 verifyIndependentOfContext("a[4] * b;"); 9881 verifyIndependentOfContext("a[a * a] = 1;"); 9882 verifyIndependentOfContext("f() * b;"); 9883 verifyIndependentOfContext("a * [self dostuff];"); 9884 verifyIndependentOfContext("int x = a * (a + b);"); 9885 verifyIndependentOfContext("(a *)(a + b);"); 9886 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9887 verifyIndependentOfContext("int *pa = (int *)&a;"); 9888 verifyIndependentOfContext("return sizeof(int **);"); 9889 verifyIndependentOfContext("return sizeof(int ******);"); 9890 verifyIndependentOfContext("return (int **&)a;"); 9891 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9892 verifyFormat("void f(Type (*parameter)[10]) {}"); 9893 verifyFormat("void f(Type (¶meter)[10]) {}"); 9894 verifyGoogleFormat("return sizeof(int**);"); 9895 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9896 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9897 verifyFormat("auto a = [](int **&, int ***) {};"); 9898 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9899 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9900 verifyFormat("[](const decltype(*a) &value) {}"); 9901 verifyFormat("[](const typeof(*a) &value) {}"); 9902 verifyFormat("[](const _Atomic(a *) &value) {}"); 9903 verifyFormat("[](const __underlying_type(a) &value) {}"); 9904 verifyFormat("decltype(a * b) F();"); 9905 verifyFormat("typeof(a * b) F();"); 9906 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9907 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9908 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9909 verifyIndependentOfContext("int i{a * b};"); 9910 verifyIndependentOfContext("aaa && aaa->f();"); 9911 verifyIndependentOfContext("int x = ~*p;"); 9912 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9913 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9914 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9915 verifyFormat("void f() { f(a, c * d); }"); 9916 verifyFormat("void f() { f(new a(), c * d); }"); 9917 verifyFormat("void f(const MyOverride &override);"); 9918 verifyFormat("void f(const MyFinal &final);"); 9919 verifyIndependentOfContext("bool a = f() && override.f();"); 9920 verifyIndependentOfContext("bool a = f() && final.f();"); 9921 9922 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9923 9924 verifyIndependentOfContext("A<int *> a;"); 9925 verifyIndependentOfContext("A<int **> a;"); 9926 verifyIndependentOfContext("A<int *, int *> a;"); 9927 verifyIndependentOfContext("A<int *[]> a;"); 9928 verifyIndependentOfContext( 9929 "const char *const p = reinterpret_cast<const char *const>(q);"); 9930 verifyIndependentOfContext("A<int **, int **> a;"); 9931 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9932 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9933 verifyFormat("for (; a && b;) {\n}"); 9934 verifyFormat("bool foo = true && [] { return false; }();"); 9935 9936 verifyFormat( 9937 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9939 9940 verifyGoogleFormat("int const* a = &b;"); 9941 verifyGoogleFormat("**outparam = 1;"); 9942 verifyGoogleFormat("*outparam = a * b;"); 9943 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9944 verifyGoogleFormat("A<int*> a;"); 9945 verifyGoogleFormat("A<int**> a;"); 9946 verifyGoogleFormat("A<int*, int*> a;"); 9947 verifyGoogleFormat("A<int**, int**> a;"); 9948 verifyGoogleFormat("f(b ? *c : *d);"); 9949 verifyGoogleFormat("int a = b ? *c : *d;"); 9950 verifyGoogleFormat("Type* t = **x;"); 9951 verifyGoogleFormat("Type* t = *++*x;"); 9952 verifyGoogleFormat("*++*x;"); 9953 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9954 verifyGoogleFormat("Type* t = x++ * y;"); 9955 verifyGoogleFormat( 9956 "const char* const p = reinterpret_cast<const char* const>(q);"); 9957 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9958 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9959 verifyGoogleFormat("template <typename T>\n" 9960 "void f(int i = 0, SomeType** temps = NULL);"); 9961 9962 FormatStyle Left = getLLVMStyle(); 9963 Left.PointerAlignment = FormatStyle::PAS_Left; 9964 verifyFormat("x = *a(x) = *a(y);", Left); 9965 verifyFormat("for (;; *a = b) {\n}", Left); 9966 verifyFormat("return *this += 1;", Left); 9967 verifyFormat("throw *x;", Left); 9968 verifyFormat("delete *x;", Left); 9969 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9970 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9971 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9972 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9973 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9974 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9975 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9976 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9977 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9978 9979 verifyIndependentOfContext("a = *(x + y);"); 9980 verifyIndependentOfContext("a = &(x + y);"); 9981 verifyIndependentOfContext("*(x + y).call();"); 9982 verifyIndependentOfContext("&(x + y)->call();"); 9983 verifyFormat("void f() { &(*I).first; }"); 9984 9985 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9986 verifyFormat("f(* /* confusing comment */ foo);"); 9987 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 9988 verifyFormat("void foo(int * // this is the first paramters\n" 9989 " ,\n" 9990 " int second);"); 9991 verifyFormat("double term = a * // first\n" 9992 " b;"); 9993 verifyFormat( 9994 "int *MyValues = {\n" 9995 " *A, // Operator detection might be confused by the '{'\n" 9996 " *BB // Operator detection might be confused by previous comment\n" 9997 "};"); 9998 9999 verifyIndependentOfContext("if (int *a = &b)"); 10000 verifyIndependentOfContext("if (int &a = *b)"); 10001 verifyIndependentOfContext("if (a & b[i])"); 10002 verifyIndependentOfContext("if constexpr (a & b[i])"); 10003 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 10004 verifyIndependentOfContext("if (a * (b * c))"); 10005 verifyIndependentOfContext("if constexpr (a * (b * c))"); 10006 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 10007 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 10008 verifyIndependentOfContext("if (*b[i])"); 10009 verifyIndependentOfContext("if (int *a = (&b))"); 10010 verifyIndependentOfContext("while (int *a = &b)"); 10011 verifyIndependentOfContext("while (a * (b * c))"); 10012 verifyIndependentOfContext("size = sizeof *a;"); 10013 verifyIndependentOfContext("if (a && (b = c))"); 10014 verifyFormat("void f() {\n" 10015 " for (const int &v : Values) {\n" 10016 " }\n" 10017 "}"); 10018 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 10019 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 10020 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 10021 10022 verifyFormat("#define A (!a * b)"); 10023 verifyFormat("#define MACRO \\\n" 10024 " int *i = a * b; \\\n" 10025 " void f(a *b);", 10026 getLLVMStyleWithColumns(19)); 10027 10028 verifyIndependentOfContext("A = new SomeType *[Length];"); 10029 verifyIndependentOfContext("A = new SomeType *[Length]();"); 10030 verifyIndependentOfContext("T **t = new T *;"); 10031 verifyIndependentOfContext("T **t = new T *();"); 10032 verifyGoogleFormat("A = new SomeType*[Length]();"); 10033 verifyGoogleFormat("A = new SomeType*[Length];"); 10034 verifyGoogleFormat("T** t = new T*;"); 10035 verifyGoogleFormat("T** t = new T*();"); 10036 10037 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 10038 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 10039 verifyFormat("template <bool a, bool b> " 10040 "typename t::if<x && y>::type f() {}"); 10041 verifyFormat("template <int *y> f() {}"); 10042 verifyFormat("vector<int *> v;"); 10043 verifyFormat("vector<int *const> v;"); 10044 verifyFormat("vector<int *const **const *> v;"); 10045 verifyFormat("vector<int *volatile> v;"); 10046 verifyFormat("vector<a *_Nonnull> v;"); 10047 verifyFormat("vector<a *_Nullable> v;"); 10048 verifyFormat("vector<a *_Null_unspecified> v;"); 10049 verifyFormat("vector<a *__ptr32> v;"); 10050 verifyFormat("vector<a *__ptr64> v;"); 10051 verifyFormat("vector<a *__capability> v;"); 10052 FormatStyle TypeMacros = getLLVMStyle(); 10053 TypeMacros.TypenameMacros = {"LIST"}; 10054 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 10055 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 10056 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 10057 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 10058 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 10059 10060 FormatStyle CustomQualifier = getLLVMStyle(); 10061 // Add identifiers that should not be parsed as a qualifier by default. 10062 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 10063 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 10064 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 10065 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 10066 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 10067 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 10068 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 10069 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 10070 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 10071 verifyFormat("vector<a * _NotAQualifier> v;"); 10072 verifyFormat("vector<a * __not_a_qualifier> v;"); 10073 verifyFormat("vector<a * b> v;"); 10074 verifyFormat("foo<b && false>();"); 10075 verifyFormat("foo<b & 1>();"); 10076 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 10077 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 10078 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 10079 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 10080 verifyFormat( 10081 "template <class T, class = typename std::enable_if<\n" 10082 " std::is_integral<T>::value &&\n" 10083 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 10084 "void F();", 10085 getLLVMStyleWithColumns(70)); 10086 verifyFormat("template <class T,\n" 10087 " class = typename std::enable_if<\n" 10088 " std::is_integral<T>::value &&\n" 10089 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 10090 " class U>\n" 10091 "void F();", 10092 getLLVMStyleWithColumns(70)); 10093 verifyFormat( 10094 "template <class T,\n" 10095 " class = typename ::std::enable_if<\n" 10096 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 10097 "void F();", 10098 getGoogleStyleWithColumns(68)); 10099 10100 verifyIndependentOfContext("MACRO(int *i);"); 10101 verifyIndependentOfContext("MACRO(auto *a);"); 10102 verifyIndependentOfContext("MACRO(const A *a);"); 10103 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 10104 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 10105 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 10106 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 10107 verifyIndependentOfContext("MACRO(A *const a);"); 10108 verifyIndependentOfContext("MACRO(A *restrict a);"); 10109 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 10110 verifyIndependentOfContext("MACRO(A *__restrict a);"); 10111 verifyIndependentOfContext("MACRO(A *volatile a);"); 10112 verifyIndependentOfContext("MACRO(A *__volatile a);"); 10113 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 10114 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 10115 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 10116 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 10117 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 10118 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 10119 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 10120 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 10121 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 10122 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 10123 verifyIndependentOfContext("MACRO(A *__capability);"); 10124 verifyIndependentOfContext("MACRO(A &__capability);"); 10125 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 10126 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 10127 // If we add __my_qualifier to AttributeMacros it should always be parsed as 10128 // a type declaration: 10129 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 10130 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 10131 // Also check that TypenameMacros prevents parsing it as multiplication: 10132 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 10133 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 10134 10135 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 10136 verifyFormat("void f() { f(float{1}, a * a); }"); 10137 verifyFormat("void f() { f(float(1), a * a); }"); 10138 10139 verifyFormat("f((void (*)(int))g);"); 10140 verifyFormat("f((void (&)(int))g);"); 10141 verifyFormat("f((void (^)(int))g);"); 10142 10143 // FIXME: Is there a way to make this work? 10144 // verifyIndependentOfContext("MACRO(A *a);"); 10145 verifyFormat("MACRO(A &B);"); 10146 verifyFormat("MACRO(A *B);"); 10147 verifyFormat("void f() { MACRO(A * B); }"); 10148 verifyFormat("void f() { MACRO(A & B); }"); 10149 10150 // This lambda was mis-formatted after D88956 (treating it as a binop): 10151 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 10152 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 10153 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 10154 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 10155 10156 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 10157 verifyFormat("return options != nullptr && operator==(*options);"); 10158 10159 EXPECT_EQ("#define OP(x) \\\n" 10160 " ostream &operator<<(ostream &s, const A &a) { \\\n" 10161 " return s << a.DebugString(); \\\n" 10162 " }", 10163 format("#define OP(x) \\\n" 10164 " ostream &operator<<(ostream &s, const A &a) { \\\n" 10165 " return s << a.DebugString(); \\\n" 10166 " }", 10167 getLLVMStyleWithColumns(50))); 10168 10169 // FIXME: We cannot handle this case yet; we might be able to figure out that 10170 // foo<x> d > v; doesn't make sense. 10171 verifyFormat("foo<a<b && c> d> v;"); 10172 10173 FormatStyle PointerMiddle = getLLVMStyle(); 10174 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 10175 verifyFormat("delete *x;", PointerMiddle); 10176 verifyFormat("int * x;", PointerMiddle); 10177 verifyFormat("int *[] x;", PointerMiddle); 10178 verifyFormat("template <int * y> f() {}", PointerMiddle); 10179 verifyFormat("int * f(int * a) {}", PointerMiddle); 10180 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 10181 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 10182 verifyFormat("A<int *> a;", PointerMiddle); 10183 verifyFormat("A<int **> a;", PointerMiddle); 10184 verifyFormat("A<int *, int *> a;", PointerMiddle); 10185 verifyFormat("A<int *[]> a;", PointerMiddle); 10186 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 10187 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 10188 verifyFormat("T ** t = new T *;", PointerMiddle); 10189 10190 // Member function reference qualifiers aren't binary operators. 10191 verifyFormat("string // break\n" 10192 "operator()() & {}"); 10193 verifyFormat("string // break\n" 10194 "operator()() && {}"); 10195 verifyGoogleFormat("template <typename T>\n" 10196 "auto x() & -> int {}"); 10197 10198 // Should be binary operators when used as an argument expression (overloaded 10199 // operator invoked as a member function). 10200 verifyFormat("void f() { a.operator()(a * a); }"); 10201 verifyFormat("void f() { a->operator()(a & a); }"); 10202 verifyFormat("void f() { a.operator()(*a & *a); }"); 10203 verifyFormat("void f() { a->operator()(*a * *a); }"); 10204 10205 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 10206 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 10207 } 10208 10209 TEST_F(FormatTest, UnderstandsAttributes) { 10210 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 10211 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 10212 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10213 verifyFormat("__attribute__((nodebug)) ::qualified_type f();"); 10214 FormatStyle AfterType = getLLVMStyle(); 10215 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10216 verifyFormat("__attribute__((nodebug)) void\n" 10217 "foo() {}\n", 10218 AfterType); 10219 verifyFormat("__unused void\n" 10220 "foo() {}", 10221 AfterType); 10222 10223 FormatStyle CustomAttrs = getLLVMStyle(); 10224 CustomAttrs.AttributeMacros.push_back("__unused"); 10225 CustomAttrs.AttributeMacros.push_back("__attr1"); 10226 CustomAttrs.AttributeMacros.push_back("__attr2"); 10227 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 10228 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 10229 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 10230 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 10231 // Check that it is parsed as a multiplication without AttributeMacros and 10232 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 10233 verifyFormat("vector<SomeType * __attr1> v;"); 10234 verifyFormat("vector<SomeType __attr1 *> v;"); 10235 verifyFormat("vector<SomeType __attr1 *const> v;"); 10236 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 10237 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 10238 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 10239 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 10240 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 10241 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 10242 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 10243 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 10244 10245 // Check that these are not parsed as function declarations: 10246 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10247 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 10248 verifyFormat("SomeType s(InitValue);", CustomAttrs); 10249 verifyFormat("SomeType s{InitValue};", CustomAttrs); 10250 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 10251 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 10252 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 10253 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 10254 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 10255 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 10256 } 10257 10258 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 10259 // Check that qualifiers on pointers don't break parsing of casts. 10260 verifyFormat("x = (foo *const)*v;"); 10261 verifyFormat("x = (foo *volatile)*v;"); 10262 verifyFormat("x = (foo *restrict)*v;"); 10263 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 10264 verifyFormat("x = (foo *_Nonnull)*v;"); 10265 verifyFormat("x = (foo *_Nullable)*v;"); 10266 verifyFormat("x = (foo *_Null_unspecified)*v;"); 10267 verifyFormat("x = (foo *_Nonnull)*v;"); 10268 verifyFormat("x = (foo *[[clang::attr]])*v;"); 10269 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 10270 verifyFormat("x = (foo *__ptr32)*v;"); 10271 verifyFormat("x = (foo *__ptr64)*v;"); 10272 verifyFormat("x = (foo *__capability)*v;"); 10273 10274 // Check that we handle multiple trailing qualifiers and skip them all to 10275 // determine that the expression is a cast to a pointer type. 10276 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 10277 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 10278 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 10279 StringRef AllQualifiers = 10280 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 10281 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 10282 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 10283 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 10284 10285 // Also check that address-of is not parsed as a binary bitwise-and: 10286 verifyFormat("x = (foo *const)&v;"); 10287 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 10288 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 10289 10290 // Check custom qualifiers: 10291 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 10292 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 10293 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 10294 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 10295 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 10296 CustomQualifier); 10297 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 10298 CustomQualifier); 10299 10300 // Check that unknown identifiers result in binary operator parsing: 10301 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 10302 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 10303 } 10304 10305 TEST_F(FormatTest, UnderstandsSquareAttributes) { 10306 verifyFormat("SomeType s [[unused]] (InitValue);"); 10307 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 10308 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 10309 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 10310 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 10311 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10312 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10313 verifyFormat("[[nodiscard]] bool f() { return false; }"); 10314 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 10315 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 10316 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 10317 verifyFormat("[[nodiscard]] ::qualified_type f();"); 10318 10319 // Make sure we do not mistake attributes for array subscripts. 10320 verifyFormat("int a() {}\n" 10321 "[[unused]] int b() {}\n"); 10322 verifyFormat("NSArray *arr;\n" 10323 "arr[[Foo() bar]];"); 10324 10325 // On the other hand, we still need to correctly find array subscripts. 10326 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 10327 10328 // Make sure that we do not mistake Objective-C method inside array literals 10329 // as attributes, even if those method names are also keywords. 10330 verifyFormat("@[ [foo bar] ];"); 10331 verifyFormat("@[ [NSArray class] ];"); 10332 verifyFormat("@[ [foo enum] ];"); 10333 10334 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 10335 10336 // Make sure we do not parse attributes as lambda introducers. 10337 FormatStyle MultiLineFunctions = getLLVMStyle(); 10338 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10339 verifyFormat("[[unused]] int b() {\n" 10340 " return 42;\n" 10341 "}\n", 10342 MultiLineFunctions); 10343 } 10344 10345 TEST_F(FormatTest, AttributeClass) { 10346 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 10347 verifyFormat("class S {\n" 10348 " S(S&&) = default;\n" 10349 "};", 10350 Style); 10351 verifyFormat("class [[nodiscard]] S {\n" 10352 " S(S&&) = default;\n" 10353 "};", 10354 Style); 10355 verifyFormat("class __attribute((maybeunused)) S {\n" 10356 " S(S&&) = default;\n" 10357 "};", 10358 Style); 10359 verifyFormat("struct S {\n" 10360 " S(S&&) = default;\n" 10361 "};", 10362 Style); 10363 verifyFormat("struct [[nodiscard]] S {\n" 10364 " S(S&&) = default;\n" 10365 "};", 10366 Style); 10367 } 10368 10369 TEST_F(FormatTest, AttributesAfterMacro) { 10370 FormatStyle Style = getLLVMStyle(); 10371 verifyFormat("MACRO;\n" 10372 "__attribute__((maybe_unused)) int foo() {\n" 10373 " //...\n" 10374 "}"); 10375 10376 verifyFormat("MACRO;\n" 10377 "[[nodiscard]] int foo() {\n" 10378 " //...\n" 10379 "}"); 10380 10381 EXPECT_EQ("MACRO\n\n" 10382 "__attribute__((maybe_unused)) int foo() {\n" 10383 " //...\n" 10384 "}", 10385 format("MACRO\n\n" 10386 "__attribute__((maybe_unused)) int foo() {\n" 10387 " //...\n" 10388 "}")); 10389 10390 EXPECT_EQ("MACRO\n\n" 10391 "[[nodiscard]] int foo() {\n" 10392 " //...\n" 10393 "}", 10394 format("MACRO\n\n" 10395 "[[nodiscard]] int foo() {\n" 10396 " //...\n" 10397 "}")); 10398 } 10399 10400 TEST_F(FormatTest, AttributePenaltyBreaking) { 10401 FormatStyle Style = getLLVMStyle(); 10402 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 10403 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10404 Style); 10405 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 10406 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10407 Style); 10408 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 10409 "shared_ptr<ALongTypeName> &C d) {\n}", 10410 Style); 10411 } 10412 10413 TEST_F(FormatTest, UnderstandsEllipsis) { 10414 FormatStyle Style = getLLVMStyle(); 10415 verifyFormat("int printf(const char *fmt, ...);"); 10416 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 10417 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 10418 10419 verifyFormat("template <int *...PP> a;", Style); 10420 10421 Style.PointerAlignment = FormatStyle::PAS_Left; 10422 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 10423 10424 verifyFormat("template <int*... PP> a;", Style); 10425 10426 Style.PointerAlignment = FormatStyle::PAS_Middle; 10427 verifyFormat("template <int *... PP> a;", Style); 10428 } 10429 10430 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 10431 EXPECT_EQ("int *a;\n" 10432 "int *a;\n" 10433 "int *a;", 10434 format("int *a;\n" 10435 "int* a;\n" 10436 "int *a;", 10437 getGoogleStyle())); 10438 EXPECT_EQ("int* a;\n" 10439 "int* a;\n" 10440 "int* a;", 10441 format("int* a;\n" 10442 "int* a;\n" 10443 "int *a;", 10444 getGoogleStyle())); 10445 EXPECT_EQ("int *a;\n" 10446 "int *a;\n" 10447 "int *a;", 10448 format("int *a;\n" 10449 "int * a;\n" 10450 "int * a;", 10451 getGoogleStyle())); 10452 EXPECT_EQ("auto x = [] {\n" 10453 " int *a;\n" 10454 " int *a;\n" 10455 " int *a;\n" 10456 "};", 10457 format("auto x=[]{int *a;\n" 10458 "int * a;\n" 10459 "int * a;};", 10460 getGoogleStyle())); 10461 } 10462 10463 TEST_F(FormatTest, UnderstandsRvalueReferences) { 10464 verifyFormat("int f(int &&a) {}"); 10465 verifyFormat("int f(int a, char &&b) {}"); 10466 verifyFormat("void f() { int &&a = b; }"); 10467 verifyGoogleFormat("int f(int a, char&& b) {}"); 10468 verifyGoogleFormat("void f() { int&& a = b; }"); 10469 10470 verifyIndependentOfContext("A<int &&> a;"); 10471 verifyIndependentOfContext("A<int &&, int &&> a;"); 10472 verifyGoogleFormat("A<int&&> a;"); 10473 verifyGoogleFormat("A<int&&, int&&> a;"); 10474 10475 // Not rvalue references: 10476 verifyFormat("template <bool B, bool C> class A {\n" 10477 " static_assert(B && C, \"Something is wrong\");\n" 10478 "};"); 10479 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 10480 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 10481 verifyFormat("#define A(a, b) (a && b)"); 10482 } 10483 10484 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 10485 verifyFormat("void f() {\n" 10486 " x[aaaaaaaaa -\n" 10487 " b] = 23;\n" 10488 "}", 10489 getLLVMStyleWithColumns(15)); 10490 } 10491 10492 TEST_F(FormatTest, FormatsCasts) { 10493 verifyFormat("Type *A = static_cast<Type *>(P);"); 10494 verifyFormat("Type *A = (Type *)P;"); 10495 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 10496 verifyFormat("int a = (int)(2.0f);"); 10497 verifyFormat("int a = (int)2.0f;"); 10498 verifyFormat("x[(int32)y];"); 10499 verifyFormat("x = (int32)y;"); 10500 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 10501 verifyFormat("int a = (int)*b;"); 10502 verifyFormat("int a = (int)2.0f;"); 10503 verifyFormat("int a = (int)~0;"); 10504 verifyFormat("int a = (int)++a;"); 10505 verifyFormat("int a = (int)sizeof(int);"); 10506 verifyFormat("int a = (int)+2;"); 10507 verifyFormat("my_int a = (my_int)2.0f;"); 10508 verifyFormat("my_int a = (my_int)sizeof(int);"); 10509 verifyFormat("return (my_int)aaa;"); 10510 verifyFormat("#define x ((int)-1)"); 10511 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 10512 verifyFormat("#define p(q) ((int *)&q)"); 10513 verifyFormat("fn(a)(b) + 1;"); 10514 10515 verifyFormat("void f() { my_int a = (my_int)*b; }"); 10516 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 10517 verifyFormat("my_int a = (my_int)~0;"); 10518 verifyFormat("my_int a = (my_int)++a;"); 10519 verifyFormat("my_int a = (my_int)-2;"); 10520 verifyFormat("my_int a = (my_int)1;"); 10521 verifyFormat("my_int a = (my_int *)1;"); 10522 verifyFormat("my_int a = (const my_int)-1;"); 10523 verifyFormat("my_int a = (const my_int *)-1;"); 10524 verifyFormat("my_int a = (my_int)(my_int)-1;"); 10525 verifyFormat("my_int a = (ns::my_int)-2;"); 10526 verifyFormat("case (my_int)ONE:"); 10527 verifyFormat("auto x = (X)this;"); 10528 // Casts in Obj-C style calls used to not be recognized as such. 10529 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 10530 10531 // FIXME: single value wrapped with paren will be treated as cast. 10532 verifyFormat("void f(int i = (kValue)*kMask) {}"); 10533 10534 verifyFormat("{ (void)F; }"); 10535 10536 // Don't break after a cast's 10537 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10538 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 10539 " bbbbbbbbbbbbbbbbbbbbbb);"); 10540 10541 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)"); 10542 verifyFormat("#define CONF_BOOL(x) (bool *)(x)"); 10543 verifyFormat("#define CONF_BOOL(x) (bool)(x)"); 10544 verifyFormat("bool *y = (bool *)(void *)(x);"); 10545 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)"); 10546 verifyFormat("bool *y = (bool *)(void *)(int)(x);"); 10547 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)"); 10548 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);"); 10549 10550 // These are not casts. 10551 verifyFormat("void f(int *) {}"); 10552 verifyFormat("f(foo)->b;"); 10553 verifyFormat("f(foo).b;"); 10554 verifyFormat("f(foo)(b);"); 10555 verifyFormat("f(foo)[b];"); 10556 verifyFormat("[](foo) { return 4; }(bar);"); 10557 verifyFormat("(*funptr)(foo)[4];"); 10558 verifyFormat("funptrs[4](foo)[4];"); 10559 verifyFormat("void f(int *);"); 10560 verifyFormat("void f(int *) = 0;"); 10561 verifyFormat("void f(SmallVector<int>) {}"); 10562 verifyFormat("void f(SmallVector<int>);"); 10563 verifyFormat("void f(SmallVector<int>) = 0;"); 10564 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 10565 verifyFormat("int a = sizeof(int) * b;"); 10566 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 10567 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 10568 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 10569 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 10570 10571 // These are not casts, but at some point were confused with casts. 10572 verifyFormat("virtual void foo(int *) override;"); 10573 verifyFormat("virtual void foo(char &) const;"); 10574 verifyFormat("virtual void foo(int *a, char *) const;"); 10575 verifyFormat("int a = sizeof(int *) + b;"); 10576 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10577 verifyFormat("bool b = f(g<int>) && c;"); 10578 verifyFormat("typedef void (*f)(int i) func;"); 10579 verifyFormat("void operator++(int) noexcept;"); 10580 verifyFormat("void operator++(int &) noexcept;"); 10581 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10582 "&) noexcept;"); 10583 verifyFormat( 10584 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10585 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10586 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10587 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10588 verifyFormat("void operator delete(foo &) noexcept;"); 10589 verifyFormat("void operator delete(foo) noexcept;"); 10590 verifyFormat("void operator delete(int) noexcept;"); 10591 verifyFormat("void operator delete(int &) noexcept;"); 10592 verifyFormat("void operator delete(int &) volatile noexcept;"); 10593 verifyFormat("void operator delete(int &) const"); 10594 verifyFormat("void operator delete(int &) = default"); 10595 verifyFormat("void operator delete(int &) = delete"); 10596 verifyFormat("void operator delete(int &) [[noreturn]]"); 10597 verifyFormat("void operator delete(int &) throw();"); 10598 verifyFormat("void operator delete(int &) throw(int);"); 10599 verifyFormat("auto operator delete(int &) -> int;"); 10600 verifyFormat("auto operator delete(int &) override"); 10601 verifyFormat("auto operator delete(int &) final"); 10602 10603 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10604 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10605 // FIXME: The indentation here is not ideal. 10606 verifyFormat( 10607 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10608 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10609 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10610 } 10611 10612 TEST_F(FormatTest, FormatsFunctionTypes) { 10613 verifyFormat("A<bool()> a;"); 10614 verifyFormat("A<SomeType()> a;"); 10615 verifyFormat("A<void (*)(int, std::string)> a;"); 10616 verifyFormat("A<void *(int)>;"); 10617 verifyFormat("void *(*a)(int *, SomeType *);"); 10618 verifyFormat("int (*func)(void *);"); 10619 verifyFormat("void f() { int (*func)(void *); }"); 10620 verifyFormat("template <class CallbackClass>\n" 10621 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10622 10623 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10624 verifyGoogleFormat("void* (*a)(int);"); 10625 verifyGoogleFormat( 10626 "template <class CallbackClass>\n" 10627 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10628 10629 // Other constructs can look somewhat like function types: 10630 verifyFormat("A<sizeof(*x)> a;"); 10631 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10632 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10633 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10634 verifyFormat("int x = f(&h)();"); 10635 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10636 verifyFormat("std::function<\n" 10637 " LooooooooooongTemplatedType<\n" 10638 " SomeType>*(\n" 10639 " LooooooooooooooooongType type)>\n" 10640 " function;", 10641 getGoogleStyleWithColumns(40)); 10642 } 10643 10644 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10645 verifyFormat("A (*foo_)[6];"); 10646 verifyFormat("vector<int> (*foo_)[6];"); 10647 } 10648 10649 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10650 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10651 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10652 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10653 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10654 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10655 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10656 10657 // Different ways of ()-initializiation. 10658 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10659 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10660 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10661 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10662 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10663 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10664 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10665 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10666 10667 // Lambdas should not confuse the variable declaration heuristic. 10668 verifyFormat("LooooooooooooooooongType\n" 10669 " variable(nullptr, [](A *a) {});", 10670 getLLVMStyleWithColumns(40)); 10671 } 10672 10673 TEST_F(FormatTest, BreaksLongDeclarations) { 10674 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10675 " AnotherNameForTheLongType;"); 10676 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10677 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10678 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10679 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10680 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10681 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10682 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10683 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10684 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10685 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10686 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10687 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10688 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10689 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10690 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10691 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10692 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10693 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10694 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10695 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10696 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10697 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10698 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10699 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10700 FormatStyle Indented = getLLVMStyle(); 10701 Indented.IndentWrappedFunctionNames = true; 10702 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10703 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10704 Indented); 10705 verifyFormat( 10706 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10707 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10708 Indented); 10709 verifyFormat( 10710 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10711 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10712 Indented); 10713 verifyFormat( 10714 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10715 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10716 Indented); 10717 10718 // FIXME: Without the comment, this breaks after "(". 10719 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10720 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10721 getGoogleStyle()); 10722 10723 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10724 " int LoooooooooooooooooooongParam2) {}"); 10725 verifyFormat( 10726 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10727 " SourceLocation L, IdentifierIn *II,\n" 10728 " Type *T) {}"); 10729 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10730 "ReallyReaaallyLongFunctionName(\n" 10731 " const std::string &SomeParameter,\n" 10732 " const SomeType<string, SomeOtherTemplateParameter>\n" 10733 " &ReallyReallyLongParameterName,\n" 10734 " const SomeType<string, SomeOtherTemplateParameter>\n" 10735 " &AnotherLongParameterName) {}"); 10736 verifyFormat("template <typename A>\n" 10737 "SomeLoooooooooooooooooooooongType<\n" 10738 " typename some_namespace::SomeOtherType<A>::Type>\n" 10739 "Function() {}"); 10740 10741 verifyGoogleFormat( 10742 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10743 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10744 verifyGoogleFormat( 10745 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10746 " SourceLocation L) {}"); 10747 verifyGoogleFormat( 10748 "some_namespace::LongReturnType\n" 10749 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10750 " int first_long_parameter, int second_parameter) {}"); 10751 10752 verifyGoogleFormat("template <typename T>\n" 10753 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10754 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10755 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10756 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10757 10758 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10759 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10760 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10761 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10762 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10763 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10764 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10765 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10768 10769 verifyFormat("template <typename T> // Templates on own line.\n" 10770 "static int // Some comment.\n" 10771 "MyFunction(int a);", 10772 getLLVMStyle()); 10773 } 10774 10775 TEST_F(FormatTest, FormatsAccessModifiers) { 10776 FormatStyle Style = getLLVMStyle(); 10777 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10778 FormatStyle::ELBAMS_LogicalBlock); 10779 verifyFormat("struct foo {\n" 10780 "private:\n" 10781 " void f() {}\n" 10782 "\n" 10783 "private:\n" 10784 " int i;\n" 10785 "\n" 10786 "protected:\n" 10787 " int j;\n" 10788 "};\n", 10789 Style); 10790 verifyFormat("struct foo {\n" 10791 "private:\n" 10792 " void f() {}\n" 10793 "\n" 10794 "private:\n" 10795 " int i;\n" 10796 "\n" 10797 "protected:\n" 10798 " int j;\n" 10799 "};\n", 10800 "struct foo {\n" 10801 "private:\n" 10802 " void f() {}\n" 10803 "private:\n" 10804 " int i;\n" 10805 "protected:\n" 10806 " int j;\n" 10807 "};\n", 10808 Style); 10809 verifyFormat("struct foo { /* comment */\n" 10810 "private:\n" 10811 " int i;\n" 10812 " // comment\n" 10813 "private:\n" 10814 " int j;\n" 10815 "};\n", 10816 Style); 10817 verifyFormat("struct foo {\n" 10818 "#ifdef FOO\n" 10819 "#endif\n" 10820 "private:\n" 10821 " int i;\n" 10822 "#ifdef FOO\n" 10823 "private:\n" 10824 "#endif\n" 10825 " int j;\n" 10826 "};\n", 10827 Style); 10828 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10829 verifyFormat("struct foo {\n" 10830 "private:\n" 10831 " void f() {}\n" 10832 "private:\n" 10833 " int i;\n" 10834 "protected:\n" 10835 " int j;\n" 10836 "};\n", 10837 Style); 10838 verifyFormat("struct foo {\n" 10839 "private:\n" 10840 " void f() {}\n" 10841 "private:\n" 10842 " int i;\n" 10843 "protected:\n" 10844 " int j;\n" 10845 "};\n", 10846 "struct foo {\n" 10847 "\n" 10848 "private:\n" 10849 " void f() {}\n" 10850 "\n" 10851 "private:\n" 10852 " int i;\n" 10853 "\n" 10854 "protected:\n" 10855 " int j;\n" 10856 "};\n", 10857 Style); 10858 verifyFormat("struct foo { /* comment */\n" 10859 "private:\n" 10860 " int i;\n" 10861 " // comment\n" 10862 "private:\n" 10863 " int j;\n" 10864 "};\n", 10865 "struct foo { /* comment */\n" 10866 "\n" 10867 "private:\n" 10868 " int i;\n" 10869 " // comment\n" 10870 "\n" 10871 "private:\n" 10872 " int j;\n" 10873 "};\n", 10874 Style); 10875 verifyFormat("struct foo {\n" 10876 "#ifdef FOO\n" 10877 "#endif\n" 10878 "private:\n" 10879 " int i;\n" 10880 "#ifdef FOO\n" 10881 "private:\n" 10882 "#endif\n" 10883 " int j;\n" 10884 "};\n", 10885 "struct foo {\n" 10886 "#ifdef FOO\n" 10887 "#endif\n" 10888 "\n" 10889 "private:\n" 10890 " int i;\n" 10891 "#ifdef FOO\n" 10892 "\n" 10893 "private:\n" 10894 "#endif\n" 10895 " int j;\n" 10896 "};\n", 10897 Style); 10898 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10899 verifyFormat("struct foo {\n" 10900 "private:\n" 10901 " void f() {}\n" 10902 "\n" 10903 "private:\n" 10904 " int i;\n" 10905 "\n" 10906 "protected:\n" 10907 " int j;\n" 10908 "};\n", 10909 Style); 10910 verifyFormat("struct foo {\n" 10911 "private:\n" 10912 " void f() {}\n" 10913 "\n" 10914 "private:\n" 10915 " int i;\n" 10916 "\n" 10917 "protected:\n" 10918 " int j;\n" 10919 "};\n", 10920 "struct foo {\n" 10921 "private:\n" 10922 " void f() {}\n" 10923 "private:\n" 10924 " int i;\n" 10925 "protected:\n" 10926 " int j;\n" 10927 "};\n", 10928 Style); 10929 verifyFormat("struct foo { /* comment */\n" 10930 "private:\n" 10931 " int i;\n" 10932 " // comment\n" 10933 "\n" 10934 "private:\n" 10935 " int j;\n" 10936 "};\n", 10937 "struct foo { /* comment */\n" 10938 "private:\n" 10939 " int i;\n" 10940 " // comment\n" 10941 "\n" 10942 "private:\n" 10943 " int j;\n" 10944 "};\n", 10945 Style); 10946 verifyFormat("struct foo {\n" 10947 "#ifdef FOO\n" 10948 "#endif\n" 10949 "\n" 10950 "private:\n" 10951 " int i;\n" 10952 "#ifdef FOO\n" 10953 "\n" 10954 "private:\n" 10955 "#endif\n" 10956 " int j;\n" 10957 "};\n", 10958 "struct foo {\n" 10959 "#ifdef FOO\n" 10960 "#endif\n" 10961 "private:\n" 10962 " int i;\n" 10963 "#ifdef FOO\n" 10964 "private:\n" 10965 "#endif\n" 10966 " int j;\n" 10967 "};\n", 10968 Style); 10969 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10970 EXPECT_EQ("struct foo {\n" 10971 "\n" 10972 "private:\n" 10973 " void f() {}\n" 10974 "\n" 10975 "private:\n" 10976 " int i;\n" 10977 "\n" 10978 "protected:\n" 10979 " int j;\n" 10980 "};\n", 10981 format("struct foo {\n" 10982 "\n" 10983 "private:\n" 10984 " void f() {}\n" 10985 "\n" 10986 "private:\n" 10987 " int i;\n" 10988 "\n" 10989 "protected:\n" 10990 " int j;\n" 10991 "};\n", 10992 Style)); 10993 verifyFormat("struct foo {\n" 10994 "private:\n" 10995 " void f() {}\n" 10996 "private:\n" 10997 " int i;\n" 10998 "protected:\n" 10999 " int j;\n" 11000 "};\n", 11001 Style); 11002 EXPECT_EQ("struct foo { /* comment */\n" 11003 "\n" 11004 "private:\n" 11005 " int i;\n" 11006 " // comment\n" 11007 "\n" 11008 "private:\n" 11009 " int j;\n" 11010 "};\n", 11011 format("struct foo { /* comment */\n" 11012 "\n" 11013 "private:\n" 11014 " int i;\n" 11015 " // comment\n" 11016 "\n" 11017 "private:\n" 11018 " int j;\n" 11019 "};\n", 11020 Style)); 11021 verifyFormat("struct foo { /* comment */\n" 11022 "private:\n" 11023 " int i;\n" 11024 " // comment\n" 11025 "private:\n" 11026 " int j;\n" 11027 "};\n", 11028 Style); 11029 EXPECT_EQ("struct foo {\n" 11030 "#ifdef FOO\n" 11031 "#endif\n" 11032 "\n" 11033 "private:\n" 11034 " int i;\n" 11035 "#ifdef FOO\n" 11036 "\n" 11037 "private:\n" 11038 "#endif\n" 11039 " int j;\n" 11040 "};\n", 11041 format("struct foo {\n" 11042 "#ifdef FOO\n" 11043 "#endif\n" 11044 "\n" 11045 "private:\n" 11046 " int i;\n" 11047 "#ifdef FOO\n" 11048 "\n" 11049 "private:\n" 11050 "#endif\n" 11051 " int j;\n" 11052 "};\n", 11053 Style)); 11054 verifyFormat("struct foo {\n" 11055 "#ifdef FOO\n" 11056 "#endif\n" 11057 "private:\n" 11058 " int i;\n" 11059 "#ifdef FOO\n" 11060 "private:\n" 11061 "#endif\n" 11062 " int j;\n" 11063 "};\n", 11064 Style); 11065 11066 FormatStyle NoEmptyLines = getLLVMStyle(); 11067 NoEmptyLines.MaxEmptyLinesToKeep = 0; 11068 verifyFormat("struct foo {\n" 11069 "private:\n" 11070 " void f() {}\n" 11071 "\n" 11072 "private:\n" 11073 " int i;\n" 11074 "\n" 11075 "public:\n" 11076 "protected:\n" 11077 " int j;\n" 11078 "};\n", 11079 NoEmptyLines); 11080 11081 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11082 verifyFormat("struct foo {\n" 11083 "private:\n" 11084 " void f() {}\n" 11085 "private:\n" 11086 " int i;\n" 11087 "public:\n" 11088 "protected:\n" 11089 " int j;\n" 11090 "};\n", 11091 NoEmptyLines); 11092 11093 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11094 verifyFormat("struct foo {\n" 11095 "private:\n" 11096 " void f() {}\n" 11097 "\n" 11098 "private:\n" 11099 " int i;\n" 11100 "\n" 11101 "public:\n" 11102 "\n" 11103 "protected:\n" 11104 " int j;\n" 11105 "};\n", 11106 NoEmptyLines); 11107 } 11108 11109 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 11110 11111 FormatStyle Style = getLLVMStyle(); 11112 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 11113 verifyFormat("struct foo {\n" 11114 "private:\n" 11115 " void f() {}\n" 11116 "\n" 11117 "private:\n" 11118 " int i;\n" 11119 "\n" 11120 "protected:\n" 11121 " int j;\n" 11122 "};\n", 11123 Style); 11124 11125 // Check if lines are removed. 11126 verifyFormat("struct foo {\n" 11127 "private:\n" 11128 " void f() {}\n" 11129 "\n" 11130 "private:\n" 11131 " int i;\n" 11132 "\n" 11133 "protected:\n" 11134 " int j;\n" 11135 "};\n", 11136 "struct foo {\n" 11137 "private:\n" 11138 "\n" 11139 " void f() {}\n" 11140 "\n" 11141 "private:\n" 11142 "\n" 11143 " int i;\n" 11144 "\n" 11145 "protected:\n" 11146 "\n" 11147 " int j;\n" 11148 "};\n", 11149 Style); 11150 11151 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11152 verifyFormat("struct foo {\n" 11153 "private:\n" 11154 "\n" 11155 " void f() {}\n" 11156 "\n" 11157 "private:\n" 11158 "\n" 11159 " int i;\n" 11160 "\n" 11161 "protected:\n" 11162 "\n" 11163 " int j;\n" 11164 "};\n", 11165 Style); 11166 11167 // Check if lines are added. 11168 verifyFormat("struct foo {\n" 11169 "private:\n" 11170 "\n" 11171 " void f() {}\n" 11172 "\n" 11173 "private:\n" 11174 "\n" 11175 " int i;\n" 11176 "\n" 11177 "protected:\n" 11178 "\n" 11179 " int j;\n" 11180 "};\n", 11181 "struct foo {\n" 11182 "private:\n" 11183 " void f() {}\n" 11184 "\n" 11185 "private:\n" 11186 " int i;\n" 11187 "\n" 11188 "protected:\n" 11189 " int j;\n" 11190 "};\n", 11191 Style); 11192 11193 // Leave tests rely on the code layout, test::messUp can not be used. 11194 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11195 Style.MaxEmptyLinesToKeep = 0u; 11196 verifyFormat("struct foo {\n" 11197 "private:\n" 11198 " void f() {}\n" 11199 "\n" 11200 "private:\n" 11201 " int i;\n" 11202 "\n" 11203 "protected:\n" 11204 " int j;\n" 11205 "};\n", 11206 Style); 11207 11208 // Check if MaxEmptyLinesToKeep is respected. 11209 EXPECT_EQ("struct foo {\n" 11210 "private:\n" 11211 " void f() {}\n" 11212 "\n" 11213 "private:\n" 11214 " int i;\n" 11215 "\n" 11216 "protected:\n" 11217 " int j;\n" 11218 "};\n", 11219 format("struct foo {\n" 11220 "private:\n" 11221 "\n\n\n" 11222 " void f() {}\n" 11223 "\n" 11224 "private:\n" 11225 "\n\n\n" 11226 " int i;\n" 11227 "\n" 11228 "protected:\n" 11229 "\n\n\n" 11230 " int j;\n" 11231 "};\n", 11232 Style)); 11233 11234 Style.MaxEmptyLinesToKeep = 1u; 11235 EXPECT_EQ("struct foo {\n" 11236 "private:\n" 11237 "\n" 11238 " void f() {}\n" 11239 "\n" 11240 "private:\n" 11241 "\n" 11242 " int i;\n" 11243 "\n" 11244 "protected:\n" 11245 "\n" 11246 " int j;\n" 11247 "};\n", 11248 format("struct foo {\n" 11249 "private:\n" 11250 "\n" 11251 " void f() {}\n" 11252 "\n" 11253 "private:\n" 11254 "\n" 11255 " int i;\n" 11256 "\n" 11257 "protected:\n" 11258 "\n" 11259 " int j;\n" 11260 "};\n", 11261 Style)); 11262 // Check if no lines are kept. 11263 EXPECT_EQ("struct foo {\n" 11264 "private:\n" 11265 " void f() {}\n" 11266 "\n" 11267 "private:\n" 11268 " int i;\n" 11269 "\n" 11270 "protected:\n" 11271 " int j;\n" 11272 "};\n", 11273 format("struct foo {\n" 11274 "private:\n" 11275 " void f() {}\n" 11276 "\n" 11277 "private:\n" 11278 " int i;\n" 11279 "\n" 11280 "protected:\n" 11281 " int j;\n" 11282 "};\n", 11283 Style)); 11284 // Check if MaxEmptyLinesToKeep is respected. 11285 EXPECT_EQ("struct foo {\n" 11286 "private:\n" 11287 "\n" 11288 " void f() {}\n" 11289 "\n" 11290 "private:\n" 11291 "\n" 11292 " int i;\n" 11293 "\n" 11294 "protected:\n" 11295 "\n" 11296 " int j;\n" 11297 "};\n", 11298 format("struct foo {\n" 11299 "private:\n" 11300 "\n\n\n" 11301 " void f() {}\n" 11302 "\n" 11303 "private:\n" 11304 "\n\n\n" 11305 " int i;\n" 11306 "\n" 11307 "protected:\n" 11308 "\n\n\n" 11309 " int j;\n" 11310 "};\n", 11311 Style)); 11312 11313 Style.MaxEmptyLinesToKeep = 10u; 11314 EXPECT_EQ("struct foo {\n" 11315 "private:\n" 11316 "\n\n\n" 11317 " void f() {}\n" 11318 "\n" 11319 "private:\n" 11320 "\n\n\n" 11321 " int i;\n" 11322 "\n" 11323 "protected:\n" 11324 "\n\n\n" 11325 " int j;\n" 11326 "};\n", 11327 format("struct foo {\n" 11328 "private:\n" 11329 "\n\n\n" 11330 " void f() {}\n" 11331 "\n" 11332 "private:\n" 11333 "\n\n\n" 11334 " int i;\n" 11335 "\n" 11336 "protected:\n" 11337 "\n\n\n" 11338 " int j;\n" 11339 "};\n", 11340 Style)); 11341 11342 // Test with comments. 11343 Style = getLLVMStyle(); 11344 verifyFormat("struct foo {\n" 11345 "private:\n" 11346 " // comment\n" 11347 " void f() {}\n" 11348 "\n" 11349 "private: /* comment */\n" 11350 " int i;\n" 11351 "};\n", 11352 Style); 11353 verifyFormat("struct foo {\n" 11354 "private:\n" 11355 " // comment\n" 11356 " void f() {}\n" 11357 "\n" 11358 "private: /* comment */\n" 11359 " int i;\n" 11360 "};\n", 11361 "struct foo {\n" 11362 "private:\n" 11363 "\n" 11364 " // comment\n" 11365 " void f() {}\n" 11366 "\n" 11367 "private: /* comment */\n" 11368 "\n" 11369 " int i;\n" 11370 "};\n", 11371 Style); 11372 11373 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11374 verifyFormat("struct foo {\n" 11375 "private:\n" 11376 "\n" 11377 " // comment\n" 11378 " void f() {}\n" 11379 "\n" 11380 "private: /* comment */\n" 11381 "\n" 11382 " int i;\n" 11383 "};\n", 11384 "struct foo {\n" 11385 "private:\n" 11386 " // comment\n" 11387 " void f() {}\n" 11388 "\n" 11389 "private: /* comment */\n" 11390 " int i;\n" 11391 "};\n", 11392 Style); 11393 verifyFormat("struct foo {\n" 11394 "private:\n" 11395 "\n" 11396 " // comment\n" 11397 " void f() {}\n" 11398 "\n" 11399 "private: /* comment */\n" 11400 "\n" 11401 " int i;\n" 11402 "};\n", 11403 Style); 11404 11405 // Test with preprocessor defines. 11406 Style = getLLVMStyle(); 11407 verifyFormat("struct foo {\n" 11408 "private:\n" 11409 "#ifdef FOO\n" 11410 "#endif\n" 11411 " void f() {}\n" 11412 "};\n", 11413 Style); 11414 verifyFormat("struct foo {\n" 11415 "private:\n" 11416 "#ifdef FOO\n" 11417 "#endif\n" 11418 " void f() {}\n" 11419 "};\n", 11420 "struct foo {\n" 11421 "private:\n" 11422 "\n" 11423 "#ifdef FOO\n" 11424 "#endif\n" 11425 " void f() {}\n" 11426 "};\n", 11427 Style); 11428 11429 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11430 verifyFormat("struct foo {\n" 11431 "private:\n" 11432 "\n" 11433 "#ifdef FOO\n" 11434 "#endif\n" 11435 " void f() {}\n" 11436 "};\n", 11437 "struct foo {\n" 11438 "private:\n" 11439 "#ifdef FOO\n" 11440 "#endif\n" 11441 " void f() {}\n" 11442 "};\n", 11443 Style); 11444 verifyFormat("struct foo {\n" 11445 "private:\n" 11446 "\n" 11447 "#ifdef FOO\n" 11448 "#endif\n" 11449 " void f() {}\n" 11450 "};\n", 11451 Style); 11452 } 11453 11454 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 11455 // Combined tests of EmptyLineAfterAccessModifier and 11456 // EmptyLineBeforeAccessModifier. 11457 FormatStyle Style = getLLVMStyle(); 11458 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11459 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11460 verifyFormat("struct foo {\n" 11461 "private:\n" 11462 "\n" 11463 "protected:\n" 11464 "};\n", 11465 Style); 11466 11467 Style.MaxEmptyLinesToKeep = 10u; 11468 // Both remove all new lines. 11469 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11470 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11471 verifyFormat("struct foo {\n" 11472 "private:\n" 11473 "protected:\n" 11474 "};\n", 11475 "struct foo {\n" 11476 "private:\n" 11477 "\n\n\n" 11478 "protected:\n" 11479 "};\n", 11480 Style); 11481 11482 // Leave tests rely on the code layout, test::messUp can not be used. 11483 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11484 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11485 Style.MaxEmptyLinesToKeep = 10u; 11486 EXPECT_EQ("struct foo {\n" 11487 "private:\n" 11488 "\n\n\n" 11489 "protected:\n" 11490 "};\n", 11491 format("struct foo {\n" 11492 "private:\n" 11493 "\n\n\n" 11494 "protected:\n" 11495 "};\n", 11496 Style)); 11497 Style.MaxEmptyLinesToKeep = 3u; 11498 EXPECT_EQ("struct foo {\n" 11499 "private:\n" 11500 "\n\n\n" 11501 "protected:\n" 11502 "};\n", 11503 format("struct foo {\n" 11504 "private:\n" 11505 "\n\n\n" 11506 "protected:\n" 11507 "};\n", 11508 Style)); 11509 Style.MaxEmptyLinesToKeep = 1u; 11510 EXPECT_EQ("struct foo {\n" 11511 "private:\n" 11512 "\n\n\n" 11513 "protected:\n" 11514 "};\n", 11515 format("struct foo {\n" 11516 "private:\n" 11517 "\n\n\n" 11518 "protected:\n" 11519 "};\n", 11520 Style)); // Based on new lines in original document and not 11521 // on the setting. 11522 11523 Style.MaxEmptyLinesToKeep = 10u; 11524 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11525 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11526 // Newlines are kept if they are greater than zero, 11527 // test::messUp removes all new lines which changes the logic 11528 EXPECT_EQ("struct foo {\n" 11529 "private:\n" 11530 "\n\n\n" 11531 "protected:\n" 11532 "};\n", 11533 format("struct foo {\n" 11534 "private:\n" 11535 "\n\n\n" 11536 "protected:\n" 11537 "};\n", 11538 Style)); 11539 11540 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11541 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11542 // test::messUp removes all new lines which changes the logic 11543 EXPECT_EQ("struct foo {\n" 11544 "private:\n" 11545 "\n\n\n" 11546 "protected:\n" 11547 "};\n", 11548 format("struct foo {\n" 11549 "private:\n" 11550 "\n\n\n" 11551 "protected:\n" 11552 "};\n", 11553 Style)); 11554 11555 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11556 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11557 EXPECT_EQ("struct foo {\n" 11558 "private:\n" 11559 "\n\n\n" 11560 "protected:\n" 11561 "};\n", 11562 format("struct foo {\n" 11563 "private:\n" 11564 "\n\n\n" 11565 "protected:\n" 11566 "};\n", 11567 Style)); // test::messUp removes all new lines which changes 11568 // the logic. 11569 11570 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11571 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11572 verifyFormat("struct foo {\n" 11573 "private:\n" 11574 "protected:\n" 11575 "};\n", 11576 "struct foo {\n" 11577 "private:\n" 11578 "\n\n\n" 11579 "protected:\n" 11580 "};\n", 11581 Style); 11582 11583 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11584 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11585 EXPECT_EQ("struct foo {\n" 11586 "private:\n" 11587 "\n\n\n" 11588 "protected:\n" 11589 "};\n", 11590 format("struct foo {\n" 11591 "private:\n" 11592 "\n\n\n" 11593 "protected:\n" 11594 "};\n", 11595 Style)); // test::messUp removes all new lines which changes 11596 // the logic. 11597 11598 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11599 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11600 verifyFormat("struct foo {\n" 11601 "private:\n" 11602 "protected:\n" 11603 "};\n", 11604 "struct foo {\n" 11605 "private:\n" 11606 "\n\n\n" 11607 "protected:\n" 11608 "};\n", 11609 Style); 11610 11611 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11612 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11613 verifyFormat("struct foo {\n" 11614 "private:\n" 11615 "protected:\n" 11616 "};\n", 11617 "struct foo {\n" 11618 "private:\n" 11619 "\n\n\n" 11620 "protected:\n" 11621 "};\n", 11622 Style); 11623 11624 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11625 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11626 verifyFormat("struct foo {\n" 11627 "private:\n" 11628 "protected:\n" 11629 "};\n", 11630 "struct foo {\n" 11631 "private:\n" 11632 "\n\n\n" 11633 "protected:\n" 11634 "};\n", 11635 Style); 11636 11637 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11638 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11639 verifyFormat("struct foo {\n" 11640 "private:\n" 11641 "protected:\n" 11642 "};\n", 11643 "struct foo {\n" 11644 "private:\n" 11645 "\n\n\n" 11646 "protected:\n" 11647 "};\n", 11648 Style); 11649 } 11650 11651 TEST_F(FormatTest, FormatsArrays) { 11652 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11653 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11654 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11655 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11656 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11657 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11658 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11659 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11660 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11661 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11662 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11663 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11664 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11665 verifyFormat( 11666 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11667 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11668 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11669 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11670 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11671 11672 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11673 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11674 verifyFormat( 11675 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11676 " .aaaaaaa[0]\n" 11677 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11678 verifyFormat("a[::b::c];"); 11679 11680 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11681 11682 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11683 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11684 } 11685 11686 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11687 verifyFormat("(a)->b();"); 11688 verifyFormat("--a;"); 11689 } 11690 11691 TEST_F(FormatTest, HandlesIncludeDirectives) { 11692 verifyFormat("#include <string>\n" 11693 "#include <a/b/c.h>\n" 11694 "#include \"a/b/string\"\n" 11695 "#include \"string.h\"\n" 11696 "#include \"string.h\"\n" 11697 "#include <a-a>\n" 11698 "#include < path with space >\n" 11699 "#include_next <test.h>" 11700 "#include \"abc.h\" // this is included for ABC\n" 11701 "#include \"some long include\" // with a comment\n" 11702 "#include \"some very long include path\"\n" 11703 "#include <some/very/long/include/path>\n", 11704 getLLVMStyleWithColumns(35)); 11705 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11706 EXPECT_EQ("#include <a>", format("#include<a>")); 11707 11708 verifyFormat("#import <string>"); 11709 verifyFormat("#import <a/b/c.h>"); 11710 verifyFormat("#import \"a/b/string\""); 11711 verifyFormat("#import \"string.h\""); 11712 verifyFormat("#import \"string.h\""); 11713 verifyFormat("#if __has_include(<strstream>)\n" 11714 "#include <strstream>\n" 11715 "#endif"); 11716 11717 verifyFormat("#define MY_IMPORT <a/b>"); 11718 11719 verifyFormat("#if __has_include(<a/b>)"); 11720 verifyFormat("#if __has_include_next(<a/b>)"); 11721 verifyFormat("#define F __has_include(<a/b>)"); 11722 verifyFormat("#define F __has_include_next(<a/b>)"); 11723 11724 // Protocol buffer definition or missing "#". 11725 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11726 getLLVMStyleWithColumns(30)); 11727 11728 FormatStyle Style = getLLVMStyle(); 11729 Style.AlwaysBreakBeforeMultilineStrings = true; 11730 Style.ColumnLimit = 0; 11731 verifyFormat("#import \"abc.h\"", Style); 11732 11733 // But 'import' might also be a regular C++ namespace. 11734 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11736 } 11737 11738 //===----------------------------------------------------------------------===// 11739 // Error recovery tests. 11740 //===----------------------------------------------------------------------===// 11741 11742 TEST_F(FormatTest, IncompleteParameterLists) { 11743 FormatStyle NoBinPacking = getLLVMStyle(); 11744 NoBinPacking.BinPackParameters = false; 11745 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11746 " double *min_x,\n" 11747 " double *max_x,\n" 11748 " double *min_y,\n" 11749 " double *max_y,\n" 11750 " double *min_z,\n" 11751 " double *max_z, ) {}", 11752 NoBinPacking); 11753 } 11754 11755 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11756 verifyFormat("void f() { return; }\n42"); 11757 verifyFormat("void f() {\n" 11758 " if (0)\n" 11759 " return;\n" 11760 "}\n" 11761 "42"); 11762 verifyFormat("void f() { return }\n42"); 11763 verifyFormat("void f() {\n" 11764 " if (0)\n" 11765 " return\n" 11766 "}\n" 11767 "42"); 11768 } 11769 11770 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11771 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11772 EXPECT_EQ("void f() {\n" 11773 " if (a)\n" 11774 " return\n" 11775 "}", 11776 format("void f ( ) { if ( a ) return }")); 11777 EXPECT_EQ("namespace N {\n" 11778 "void f()\n" 11779 "}", 11780 format("namespace N { void f() }")); 11781 EXPECT_EQ("namespace N {\n" 11782 "void f() {}\n" 11783 "void g()\n" 11784 "} // namespace N", 11785 format("namespace N { void f( ) { } void g( ) }")); 11786 } 11787 11788 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11789 verifyFormat("int aaaaaaaa =\n" 11790 " // Overlylongcomment\n" 11791 " b;", 11792 getLLVMStyleWithColumns(20)); 11793 verifyFormat("function(\n" 11794 " ShortArgument,\n" 11795 " LoooooooooooongArgument);\n", 11796 getLLVMStyleWithColumns(20)); 11797 } 11798 11799 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11800 verifyFormat("public:"); 11801 verifyFormat("class A {\n" 11802 "public\n" 11803 " void f() {}\n" 11804 "};"); 11805 verifyFormat("public\n" 11806 "int qwerty;"); 11807 verifyFormat("public\n" 11808 "B {}"); 11809 verifyFormat("public\n" 11810 "{}"); 11811 verifyFormat("public\n" 11812 "B { int x; }"); 11813 } 11814 11815 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11816 verifyFormat("{"); 11817 verifyFormat("#})"); 11818 verifyNoCrash("(/**/[:!] ?[)."); 11819 } 11820 11821 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11822 // Found by oss-fuzz: 11823 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11824 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11825 Style.ColumnLimit = 60; 11826 verifyNoCrash( 11827 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11828 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11829 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11830 Style); 11831 } 11832 11833 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11834 verifyFormat("do {\n}"); 11835 verifyFormat("do {\n}\n" 11836 "f();"); 11837 verifyFormat("do {\n}\n" 11838 "wheeee(fun);"); 11839 verifyFormat("do {\n" 11840 " f();\n" 11841 "}"); 11842 } 11843 11844 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11845 verifyFormat("if {\n foo;\n foo();\n}"); 11846 verifyFormat("switch {\n foo;\n foo();\n}"); 11847 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11848 verifyFormat("while {\n foo;\n foo();\n}"); 11849 verifyFormat("do {\n foo;\n foo();\n} while;"); 11850 } 11851 11852 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11853 verifyIncompleteFormat("namespace {\n" 11854 "class Foo { Foo (\n" 11855 "};\n" 11856 "} // namespace"); 11857 } 11858 11859 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11860 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11861 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11862 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11863 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11864 11865 EXPECT_EQ("{\n" 11866 " {\n" 11867 " breakme(\n" 11868 " qwe);\n" 11869 " }\n", 11870 format("{\n" 11871 " {\n" 11872 " breakme(qwe);\n" 11873 "}\n", 11874 getLLVMStyleWithColumns(10))); 11875 } 11876 11877 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11878 verifyFormat("int x = {\n" 11879 " avariable,\n" 11880 " b(alongervariable)};", 11881 getLLVMStyleWithColumns(25)); 11882 } 11883 11884 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11885 verifyFormat("return (a)(b){1, 2, 3};"); 11886 } 11887 11888 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11889 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11890 verifyFormat("vector<int> x{\n" 11891 " 1,\n" 11892 " 2,\n" 11893 " 3,\n" 11894 " 4,\n" 11895 "};"); 11896 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11897 verifyFormat("f({1, 2});"); 11898 verifyFormat("auto v = Foo{-1};"); 11899 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11900 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11901 verifyFormat("new vector<int>{1, 2, 3};"); 11902 verifyFormat("new int[3]{1, 2, 3};"); 11903 verifyFormat("new int{1};"); 11904 verifyFormat("return {arg1, arg2};"); 11905 verifyFormat("return {arg1, SomeType{parameter}};"); 11906 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11907 verifyFormat("new T{arg1, arg2};"); 11908 verifyFormat("f(MyMap[{composite, key}]);"); 11909 verifyFormat("class Class {\n" 11910 " T member = {arg1, arg2};\n" 11911 "};"); 11912 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11913 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11914 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11915 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11916 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11917 11918 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11919 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11920 verifyFormat("auto i = decltype(x){};"); 11921 verifyFormat("auto i = typeof(x){};"); 11922 verifyFormat("auto i = _Atomic(x){};"); 11923 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11924 verifyFormat("Node n{1, Node{1000}, //\n" 11925 " 2};"); 11926 verifyFormat("Aaaa aaaaaaa{\n" 11927 " {\n" 11928 " aaaa,\n" 11929 " },\n" 11930 "};"); 11931 verifyFormat("class C : public D {\n" 11932 " SomeClass SC{2};\n" 11933 "};"); 11934 verifyFormat("class C : public A {\n" 11935 " class D : public B {\n" 11936 " void f() { int i{2}; }\n" 11937 " };\n" 11938 "};"); 11939 verifyFormat("#define A {a, a},"); 11940 // Don't confuse braced list initializers with compound statements. 11941 verifyFormat( 11942 "class A {\n" 11943 " A() : a{} {}\n" 11944 " A(int b) : b(b) {}\n" 11945 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" 11946 " int a, b;\n" 11947 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" 11948 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " 11949 "{}\n" 11950 "};"); 11951 11952 // Avoid breaking between equal sign and opening brace 11953 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11954 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11955 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11956 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11957 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11958 " {\"ccccccccccccccccccccc\", 2}};", 11959 AvoidBreakingFirstArgument); 11960 11961 // Binpacking only if there is no trailing comma 11962 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11963 " cccccccccc, dddddddddd};", 11964 getLLVMStyleWithColumns(50)); 11965 verifyFormat("const Aaaaaa aaaaa = {\n" 11966 " aaaaaaaaaaa,\n" 11967 " bbbbbbbbbbb,\n" 11968 " ccccccccccc,\n" 11969 " ddddddddddd,\n" 11970 "};", 11971 getLLVMStyleWithColumns(50)); 11972 11973 // Cases where distinguising braced lists and blocks is hard. 11974 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11975 verifyFormat("void f() {\n" 11976 " return; // comment\n" 11977 "}\n" 11978 "SomeType t;"); 11979 verifyFormat("void f() {\n" 11980 " if (a) {\n" 11981 " f();\n" 11982 " }\n" 11983 "}\n" 11984 "SomeType t;"); 11985 11986 // In combination with BinPackArguments = false. 11987 FormatStyle NoBinPacking = getLLVMStyle(); 11988 NoBinPacking.BinPackArguments = false; 11989 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11990 " bbbbb,\n" 11991 " ccccc,\n" 11992 " ddddd,\n" 11993 " eeeee,\n" 11994 " ffffff,\n" 11995 " ggggg,\n" 11996 " hhhhhh,\n" 11997 " iiiiii,\n" 11998 " jjjjjj,\n" 11999 " kkkkkk};", 12000 NoBinPacking); 12001 verifyFormat("const Aaaaaa aaaaa = {\n" 12002 " aaaaa,\n" 12003 " bbbbb,\n" 12004 " ccccc,\n" 12005 " ddddd,\n" 12006 " eeeee,\n" 12007 " ffffff,\n" 12008 " ggggg,\n" 12009 " hhhhhh,\n" 12010 " iiiiii,\n" 12011 " jjjjjj,\n" 12012 " kkkkkk,\n" 12013 "};", 12014 NoBinPacking); 12015 verifyFormat( 12016 "const Aaaaaa aaaaa = {\n" 12017 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 12018 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 12019 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 12020 "};", 12021 NoBinPacking); 12022 12023 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12024 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 12025 " CDDDP83848_BMCR_REGISTER,\n" 12026 " CDDDP83848_BMSR_REGISTER,\n" 12027 " CDDDP83848_RBR_REGISTER};", 12028 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 12029 " CDDDP83848_BMSR_REGISTER,\n" 12030 " CDDDP83848_RBR_REGISTER};", 12031 NoBinPacking)); 12032 12033 // FIXME: The alignment of these trailing comments might be bad. Then again, 12034 // this might be utterly useless in real code. 12035 verifyFormat("Constructor::Constructor()\n" 12036 " : some_value{ //\n" 12037 " aaaaaaa, //\n" 12038 " bbbbbbb} {}"); 12039 12040 // In braced lists, the first comment is always assumed to belong to the 12041 // first element. Thus, it can be moved to the next or previous line as 12042 // appropriate. 12043 EXPECT_EQ("function({// First element:\n" 12044 " 1,\n" 12045 " // Second element:\n" 12046 " 2});", 12047 format("function({\n" 12048 " // First element:\n" 12049 " 1,\n" 12050 " // Second element:\n" 12051 " 2});")); 12052 EXPECT_EQ("std::vector<int> MyNumbers{\n" 12053 " // First element:\n" 12054 " 1,\n" 12055 " // Second element:\n" 12056 " 2};", 12057 format("std::vector<int> MyNumbers{// First element:\n" 12058 " 1,\n" 12059 " // Second element:\n" 12060 " 2};", 12061 getLLVMStyleWithColumns(30))); 12062 // A trailing comma should still lead to an enforced line break and no 12063 // binpacking. 12064 EXPECT_EQ("vector<int> SomeVector = {\n" 12065 " // aaa\n" 12066 " 1,\n" 12067 " 2,\n" 12068 "};", 12069 format("vector<int> SomeVector = { // aaa\n" 12070 " 1, 2, };")); 12071 12072 // C++11 brace initializer list l-braces should not be treated any differently 12073 // when breaking before lambda bodies is enabled 12074 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 12075 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 12076 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 12077 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 12078 verifyFormat( 12079 "std::runtime_error{\n" 12080 " \"Long string which will force a break onto the next line...\"};", 12081 BreakBeforeLambdaBody); 12082 12083 FormatStyle ExtraSpaces = getLLVMStyle(); 12084 ExtraSpaces.Cpp11BracedListStyle = false; 12085 ExtraSpaces.ColumnLimit = 75; 12086 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 12087 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 12088 verifyFormat("f({ 1, 2 });", ExtraSpaces); 12089 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 12090 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 12091 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 12092 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 12093 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 12094 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 12095 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 12096 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 12097 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 12098 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 12099 verifyFormat("class Class {\n" 12100 " T member = { arg1, arg2 };\n" 12101 "};", 12102 ExtraSpaces); 12103 verifyFormat( 12104 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12105 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 12106 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 12107 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 12108 ExtraSpaces); 12109 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 12110 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 12111 ExtraSpaces); 12112 verifyFormat( 12113 "someFunction(OtherParam,\n" 12114 " BracedList{ // comment 1 (Forcing interesting break)\n" 12115 " param1, param2,\n" 12116 " // comment 2\n" 12117 " param3, param4 });", 12118 ExtraSpaces); 12119 verifyFormat( 12120 "std::this_thread::sleep_for(\n" 12121 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 12122 ExtraSpaces); 12123 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 12124 " aaaaaaa,\n" 12125 " aaaaaaaaaa,\n" 12126 " aaaaa,\n" 12127 " aaaaaaaaaaaaaaa,\n" 12128 " aaa,\n" 12129 " aaaaaaaaaa,\n" 12130 " a,\n" 12131 " aaaaaaaaaaaaaaaaaaaaa,\n" 12132 " aaaaaaaaaaaa,\n" 12133 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 12134 " aaaaaaa,\n" 12135 " a};"); 12136 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 12137 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 12138 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 12139 12140 // Avoid breaking between initializer/equal sign and opening brace 12141 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 12142 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 12143 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 12144 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 12145 " { \"ccccccccccccccccccccc\", 2 }\n" 12146 "};", 12147 ExtraSpaces); 12148 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 12149 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 12150 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 12151 " { \"ccccccccccccccccccccc\", 2 }\n" 12152 "};", 12153 ExtraSpaces); 12154 12155 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 12156 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 12157 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 12158 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 12159 12160 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 12161 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 12162 SpaceBetweenBraces.SpacesInParentheses = true; 12163 SpaceBetweenBraces.SpacesInSquareBrackets = true; 12164 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 12165 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 12166 verifyFormat("vector< int > x{ // comment 1\n" 12167 " 1, 2, 3, 4 };", 12168 SpaceBetweenBraces); 12169 SpaceBetweenBraces.ColumnLimit = 20; 12170 EXPECT_EQ("vector< int > x{\n" 12171 " 1, 2, 3, 4 };", 12172 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 12173 SpaceBetweenBraces.ColumnLimit = 24; 12174 EXPECT_EQ("vector< int > x{ 1, 2,\n" 12175 " 3, 4 };", 12176 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 12177 EXPECT_EQ("vector< int > x{\n" 12178 " 1,\n" 12179 " 2,\n" 12180 " 3,\n" 12181 " 4,\n" 12182 "};", 12183 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 12184 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 12185 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 12186 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 12187 } 12188 12189 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 12190 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12191 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12192 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12193 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12194 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12195 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12196 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 12197 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12198 " 1, 22, 333, 4444, 55555, //\n" 12199 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12200 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12201 verifyFormat( 12202 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12203 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12204 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 12205 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12206 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12207 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12208 " 7777777};"); 12209 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12210 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12211 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12212 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12213 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12214 " // Separating comment.\n" 12215 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12216 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12217 " // Leading comment\n" 12218 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12219 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12220 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12221 " 1, 1, 1, 1};", 12222 getLLVMStyleWithColumns(39)); 12223 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12224 " 1, 1, 1, 1};", 12225 getLLVMStyleWithColumns(38)); 12226 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 12227 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 12228 getLLVMStyleWithColumns(43)); 12229 verifyFormat( 12230 "static unsigned SomeValues[10][3] = {\n" 12231 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 12232 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 12233 verifyFormat("static auto fields = new vector<string>{\n" 12234 " \"aaaaaaaaaaaaa\",\n" 12235 " \"aaaaaaaaaaaaa\",\n" 12236 " \"aaaaaaaaaaaa\",\n" 12237 " \"aaaaaaaaaaaaaa\",\n" 12238 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12239 " \"aaaaaaaaaaaa\",\n" 12240 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12241 "};"); 12242 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 12243 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 12244 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 12245 " 3, cccccccccccccccccccccc};", 12246 getLLVMStyleWithColumns(60)); 12247 12248 // Trailing commas. 12249 verifyFormat("vector<int> x = {\n" 12250 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 12251 "};", 12252 getLLVMStyleWithColumns(39)); 12253 verifyFormat("vector<int> x = {\n" 12254 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 12255 "};", 12256 getLLVMStyleWithColumns(39)); 12257 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12258 " 1, 1, 1, 1,\n" 12259 " /**/ /**/};", 12260 getLLVMStyleWithColumns(39)); 12261 12262 // Trailing comment in the first line. 12263 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 12264 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 12265 " 111111111, 222222222, 3333333333, 444444444, //\n" 12266 " 11111111, 22222222, 333333333, 44444444};"); 12267 // Trailing comment in the last line. 12268 verifyFormat("int aaaaa[] = {\n" 12269 " 1, 2, 3, // comment\n" 12270 " 4, 5, 6 // comment\n" 12271 "};"); 12272 12273 // With nested lists, we should either format one item per line or all nested 12274 // lists one on line. 12275 // FIXME: For some nested lists, we can do better. 12276 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 12277 " {aaaaaaaaaaaaaaaaaaa},\n" 12278 " {aaaaaaaaaaaaaaaaaaaaa},\n" 12279 " {aaaaaaaaaaaaaaaaa}};", 12280 getLLVMStyleWithColumns(60)); 12281 verifyFormat( 12282 "SomeStruct my_struct_array = {\n" 12283 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 12284 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 12285 " {aaa, aaa},\n" 12286 " {aaa, aaa},\n" 12287 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 12288 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 12289 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 12290 12291 // No column layout should be used here. 12292 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 12293 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 12294 12295 verifyNoCrash("a<,"); 12296 12297 // No braced initializer here. 12298 verifyFormat("void f() {\n" 12299 " struct Dummy {};\n" 12300 " f(v);\n" 12301 "}"); 12302 12303 // Long lists should be formatted in columns even if they are nested. 12304 verifyFormat( 12305 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12306 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12307 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12308 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12309 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12310 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 12311 12312 // Allow "single-column" layout even if that violates the column limit. There 12313 // isn't going to be a better way. 12314 verifyFormat("std::vector<int> a = {\n" 12315 " aaaaaaaa,\n" 12316 " aaaaaaaa,\n" 12317 " aaaaaaaa,\n" 12318 " aaaaaaaa,\n" 12319 " aaaaaaaaaa,\n" 12320 " aaaaaaaa,\n" 12321 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 12322 getLLVMStyleWithColumns(30)); 12323 verifyFormat("vector<int> aaaa = {\n" 12324 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12325 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12326 " aaaaaa.aaaaaaa,\n" 12327 " aaaaaa.aaaaaaa,\n" 12328 " aaaaaa.aaaaaaa,\n" 12329 " aaaaaa.aaaaaaa,\n" 12330 "};"); 12331 12332 // Don't create hanging lists. 12333 verifyFormat("someFunction(Param, {List1, List2,\n" 12334 " List3});", 12335 getLLVMStyleWithColumns(35)); 12336 verifyFormat("someFunction(Param, Param,\n" 12337 " {List1, List2,\n" 12338 " List3});", 12339 getLLVMStyleWithColumns(35)); 12340 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 12341 " aaaaaaaaaaaaaaaaaaaaaaa);"); 12342 } 12343 12344 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 12345 FormatStyle DoNotMerge = getLLVMStyle(); 12346 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12347 12348 verifyFormat("void f() { return 42; }"); 12349 verifyFormat("void f() {\n" 12350 " return 42;\n" 12351 "}", 12352 DoNotMerge); 12353 verifyFormat("void f() {\n" 12354 " // Comment\n" 12355 "}"); 12356 verifyFormat("{\n" 12357 "#error {\n" 12358 " int a;\n" 12359 "}"); 12360 verifyFormat("{\n" 12361 " int a;\n" 12362 "#error {\n" 12363 "}"); 12364 verifyFormat("void f() {} // comment"); 12365 verifyFormat("void f() { int a; } // comment"); 12366 verifyFormat("void f() {\n" 12367 "} // comment", 12368 DoNotMerge); 12369 verifyFormat("void f() {\n" 12370 " int a;\n" 12371 "} // comment", 12372 DoNotMerge); 12373 verifyFormat("void f() {\n" 12374 "} // comment", 12375 getLLVMStyleWithColumns(15)); 12376 12377 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 12378 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 12379 12380 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 12381 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 12382 verifyFormat("class C {\n" 12383 " C()\n" 12384 " : iiiiiiii(nullptr),\n" 12385 " kkkkkkk(nullptr),\n" 12386 " mmmmmmm(nullptr),\n" 12387 " nnnnnnn(nullptr) {}\n" 12388 "};", 12389 getGoogleStyle()); 12390 12391 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 12392 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 12393 EXPECT_EQ("class C {\n" 12394 " A() : b(0) {}\n" 12395 "};", 12396 format("class C{A():b(0){}};", NoColumnLimit)); 12397 EXPECT_EQ("A()\n" 12398 " : b(0) {\n" 12399 "}", 12400 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 12401 12402 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 12403 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 12404 FormatStyle::SFS_None; 12405 EXPECT_EQ("A()\n" 12406 " : b(0) {\n" 12407 "}", 12408 format("A():b(0){}", DoNotMergeNoColumnLimit)); 12409 EXPECT_EQ("A()\n" 12410 " : b(0) {\n" 12411 "}", 12412 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 12413 12414 verifyFormat("#define A \\\n" 12415 " void f() { \\\n" 12416 " int i; \\\n" 12417 " }", 12418 getLLVMStyleWithColumns(20)); 12419 verifyFormat("#define A \\\n" 12420 " void f() { int i; }", 12421 getLLVMStyleWithColumns(21)); 12422 verifyFormat("#define A \\\n" 12423 " void f() { \\\n" 12424 " int i; \\\n" 12425 " } \\\n" 12426 " int j;", 12427 getLLVMStyleWithColumns(22)); 12428 verifyFormat("#define A \\\n" 12429 " void f() { int i; } \\\n" 12430 " int j;", 12431 getLLVMStyleWithColumns(23)); 12432 } 12433 12434 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 12435 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12436 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12437 verifyFormat("class C {\n" 12438 " int f() {}\n" 12439 "};", 12440 MergeEmptyOnly); 12441 verifyFormat("class C {\n" 12442 " int f() {\n" 12443 " return 42;\n" 12444 " }\n" 12445 "};", 12446 MergeEmptyOnly); 12447 verifyFormat("int f() {}", MergeEmptyOnly); 12448 verifyFormat("int f() {\n" 12449 " return 42;\n" 12450 "}", 12451 MergeEmptyOnly); 12452 12453 // Also verify behavior when BraceWrapping.AfterFunction = true 12454 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12455 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 12456 verifyFormat("int f() {}", MergeEmptyOnly); 12457 verifyFormat("class C {\n" 12458 " int f() {}\n" 12459 "};", 12460 MergeEmptyOnly); 12461 } 12462 12463 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 12464 FormatStyle MergeInlineOnly = getLLVMStyle(); 12465 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12466 verifyFormat("class C {\n" 12467 " int f() { return 42; }\n" 12468 "};", 12469 MergeInlineOnly); 12470 verifyFormat("int f() {\n" 12471 " return 42;\n" 12472 "}", 12473 MergeInlineOnly); 12474 12475 // SFS_Inline implies SFS_Empty 12476 verifyFormat("class C {\n" 12477 " int f() {}\n" 12478 "};", 12479 MergeInlineOnly); 12480 verifyFormat("int f() {}", MergeInlineOnly); 12481 12482 // Also verify behavior when BraceWrapping.AfterFunction = true 12483 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12484 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12485 verifyFormat("class C {\n" 12486 " int f() { return 42; }\n" 12487 "};", 12488 MergeInlineOnly); 12489 verifyFormat("int f()\n" 12490 "{\n" 12491 " return 42;\n" 12492 "}", 12493 MergeInlineOnly); 12494 12495 // SFS_Inline implies SFS_Empty 12496 verifyFormat("int f() {}", MergeInlineOnly); 12497 verifyFormat("class C {\n" 12498 " int f() {}\n" 12499 "};", 12500 MergeInlineOnly); 12501 12502 MergeInlineOnly.BraceWrapping.AfterClass = true; 12503 MergeInlineOnly.BraceWrapping.AfterStruct = true; 12504 verifyFormat("class C\n" 12505 "{\n" 12506 " int f() { return 42; }\n" 12507 "};", 12508 MergeInlineOnly); 12509 verifyFormat("struct C\n" 12510 "{\n" 12511 " int f() { return 42; }\n" 12512 "};", 12513 MergeInlineOnly); 12514 verifyFormat("int f()\n" 12515 "{\n" 12516 " return 42;\n" 12517 "}", 12518 MergeInlineOnly); 12519 verifyFormat("int f() {}", MergeInlineOnly); 12520 verifyFormat("class C\n" 12521 "{\n" 12522 " int f() { return 42; }\n" 12523 "};", 12524 MergeInlineOnly); 12525 verifyFormat("struct C\n" 12526 "{\n" 12527 " int f() { return 42; }\n" 12528 "};", 12529 MergeInlineOnly); 12530 verifyFormat("struct C\n" 12531 "// comment\n" 12532 "/* comment */\n" 12533 "// comment\n" 12534 "{\n" 12535 " int f() { return 42; }\n" 12536 "};", 12537 MergeInlineOnly); 12538 verifyFormat("/* comment */ struct C\n" 12539 "{\n" 12540 " int f() { return 42; }\n" 12541 "};", 12542 MergeInlineOnly); 12543 } 12544 12545 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 12546 FormatStyle MergeInlineOnly = getLLVMStyle(); 12547 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 12548 FormatStyle::SFS_InlineOnly; 12549 verifyFormat("class C {\n" 12550 " int f() { return 42; }\n" 12551 "};", 12552 MergeInlineOnly); 12553 verifyFormat("int f() {\n" 12554 " return 42;\n" 12555 "}", 12556 MergeInlineOnly); 12557 12558 // SFS_InlineOnly does not imply SFS_Empty 12559 verifyFormat("class C {\n" 12560 " int f() {}\n" 12561 "};", 12562 MergeInlineOnly); 12563 verifyFormat("int f() {\n" 12564 "}", 12565 MergeInlineOnly); 12566 12567 // Also verify behavior when BraceWrapping.AfterFunction = true 12568 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12569 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12570 verifyFormat("class C {\n" 12571 " int f() { return 42; }\n" 12572 "};", 12573 MergeInlineOnly); 12574 verifyFormat("int f()\n" 12575 "{\n" 12576 " return 42;\n" 12577 "}", 12578 MergeInlineOnly); 12579 12580 // SFS_InlineOnly does not imply SFS_Empty 12581 verifyFormat("int f()\n" 12582 "{\n" 12583 "}", 12584 MergeInlineOnly); 12585 verifyFormat("class C {\n" 12586 " int f() {}\n" 12587 "};", 12588 MergeInlineOnly); 12589 } 12590 12591 TEST_F(FormatTest, SplitEmptyFunction) { 12592 FormatStyle Style = getLLVMStyleWithColumns(40); 12593 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12594 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12595 Style.BraceWrapping.AfterFunction = true; 12596 Style.BraceWrapping.SplitEmptyFunction = false; 12597 12598 verifyFormat("int f()\n" 12599 "{}", 12600 Style); 12601 verifyFormat("int f()\n" 12602 "{\n" 12603 " return 42;\n" 12604 "}", 12605 Style); 12606 verifyFormat("int f()\n" 12607 "{\n" 12608 " // some comment\n" 12609 "}", 12610 Style); 12611 12612 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12613 verifyFormat("int f() {}", Style); 12614 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12615 "{}", 12616 Style); 12617 verifyFormat("int f()\n" 12618 "{\n" 12619 " return 0;\n" 12620 "}", 12621 Style); 12622 12623 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12624 verifyFormat("class Foo {\n" 12625 " int f() {}\n" 12626 "};\n", 12627 Style); 12628 verifyFormat("class Foo {\n" 12629 " int f() { return 0; }\n" 12630 "};\n", 12631 Style); 12632 verifyFormat("class Foo {\n" 12633 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12634 " {}\n" 12635 "};\n", 12636 Style); 12637 verifyFormat("class Foo {\n" 12638 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12639 " {\n" 12640 " return 0;\n" 12641 " }\n" 12642 "};\n", 12643 Style); 12644 12645 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12646 verifyFormat("int f() {}", Style); 12647 verifyFormat("int f() { return 0; }", Style); 12648 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12649 "{}", 12650 Style); 12651 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12652 "{\n" 12653 " return 0;\n" 12654 "}", 12655 Style); 12656 } 12657 12658 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { 12659 FormatStyle Style = getLLVMStyleWithColumns(40); 12660 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12661 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12662 Style.BraceWrapping.AfterFunction = true; 12663 Style.BraceWrapping.SplitEmptyFunction = true; 12664 Style.BraceWrapping.SplitEmptyRecord = false; 12665 12666 verifyFormat("class C {};", Style); 12667 verifyFormat("struct C {};", Style); 12668 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12669 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12670 "{\n" 12671 "}", 12672 Style); 12673 verifyFormat("class C {\n" 12674 " C()\n" 12675 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" 12676 " bbbbbbbbbbbbbbbbbbb()\n" 12677 " {\n" 12678 " }\n" 12679 " void\n" 12680 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12681 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12682 " {\n" 12683 " }\n" 12684 "};", 12685 Style); 12686 } 12687 12688 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12689 FormatStyle Style = getLLVMStyle(); 12690 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12691 verifyFormat("#ifdef A\n" 12692 "int f() {}\n" 12693 "#else\n" 12694 "int g() {}\n" 12695 "#endif", 12696 Style); 12697 } 12698 12699 TEST_F(FormatTest, SplitEmptyClass) { 12700 FormatStyle Style = getLLVMStyle(); 12701 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12702 Style.BraceWrapping.AfterClass = true; 12703 Style.BraceWrapping.SplitEmptyRecord = false; 12704 12705 verifyFormat("class Foo\n" 12706 "{};", 12707 Style); 12708 verifyFormat("/* something */ class Foo\n" 12709 "{};", 12710 Style); 12711 verifyFormat("template <typename X> class Foo\n" 12712 "{};", 12713 Style); 12714 verifyFormat("class Foo\n" 12715 "{\n" 12716 " Foo();\n" 12717 "};", 12718 Style); 12719 verifyFormat("typedef class Foo\n" 12720 "{\n" 12721 "} Foo_t;", 12722 Style); 12723 12724 Style.BraceWrapping.SplitEmptyRecord = true; 12725 Style.BraceWrapping.AfterStruct = true; 12726 verifyFormat("class rep\n" 12727 "{\n" 12728 "};", 12729 Style); 12730 verifyFormat("struct rep\n" 12731 "{\n" 12732 "};", 12733 Style); 12734 verifyFormat("template <typename T> class rep\n" 12735 "{\n" 12736 "};", 12737 Style); 12738 verifyFormat("template <typename T> struct rep\n" 12739 "{\n" 12740 "};", 12741 Style); 12742 verifyFormat("class rep\n" 12743 "{\n" 12744 " int x;\n" 12745 "};", 12746 Style); 12747 verifyFormat("struct rep\n" 12748 "{\n" 12749 " int x;\n" 12750 "};", 12751 Style); 12752 verifyFormat("template <typename T> class rep\n" 12753 "{\n" 12754 " int x;\n" 12755 "};", 12756 Style); 12757 verifyFormat("template <typename T> struct rep\n" 12758 "{\n" 12759 " int x;\n" 12760 "};", 12761 Style); 12762 verifyFormat("template <typename T> class rep // Foo\n" 12763 "{\n" 12764 " int x;\n" 12765 "};", 12766 Style); 12767 verifyFormat("template <typename T> struct rep // Bar\n" 12768 "{\n" 12769 " int x;\n" 12770 "};", 12771 Style); 12772 12773 verifyFormat("template <typename T> class rep<T>\n" 12774 "{\n" 12775 " int x;\n" 12776 "};", 12777 Style); 12778 12779 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12780 "{\n" 12781 " int x;\n" 12782 "};", 12783 Style); 12784 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12785 "{\n" 12786 "};", 12787 Style); 12788 12789 verifyFormat("#include \"stdint.h\"\n" 12790 "namespace rep {}", 12791 Style); 12792 verifyFormat("#include <stdint.h>\n" 12793 "namespace rep {}", 12794 Style); 12795 verifyFormat("#include <stdint.h>\n" 12796 "namespace rep {}", 12797 "#include <stdint.h>\n" 12798 "namespace rep {\n" 12799 "\n" 12800 "\n" 12801 "}", 12802 Style); 12803 } 12804 12805 TEST_F(FormatTest, SplitEmptyStruct) { 12806 FormatStyle Style = getLLVMStyle(); 12807 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12808 Style.BraceWrapping.AfterStruct = true; 12809 Style.BraceWrapping.SplitEmptyRecord = false; 12810 12811 verifyFormat("struct Foo\n" 12812 "{};", 12813 Style); 12814 verifyFormat("/* something */ struct Foo\n" 12815 "{};", 12816 Style); 12817 verifyFormat("template <typename X> struct Foo\n" 12818 "{};", 12819 Style); 12820 verifyFormat("struct Foo\n" 12821 "{\n" 12822 " Foo();\n" 12823 "};", 12824 Style); 12825 verifyFormat("typedef struct Foo\n" 12826 "{\n" 12827 "} Foo_t;", 12828 Style); 12829 // typedef struct Bar {} Bar_t; 12830 } 12831 12832 TEST_F(FormatTest, SplitEmptyUnion) { 12833 FormatStyle Style = getLLVMStyle(); 12834 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12835 Style.BraceWrapping.AfterUnion = true; 12836 Style.BraceWrapping.SplitEmptyRecord = false; 12837 12838 verifyFormat("union Foo\n" 12839 "{};", 12840 Style); 12841 verifyFormat("/* something */ union Foo\n" 12842 "{};", 12843 Style); 12844 verifyFormat("union Foo\n" 12845 "{\n" 12846 " A,\n" 12847 "};", 12848 Style); 12849 verifyFormat("typedef union Foo\n" 12850 "{\n" 12851 "} Foo_t;", 12852 Style); 12853 } 12854 12855 TEST_F(FormatTest, SplitEmptyNamespace) { 12856 FormatStyle Style = getLLVMStyle(); 12857 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12858 Style.BraceWrapping.AfterNamespace = true; 12859 Style.BraceWrapping.SplitEmptyNamespace = false; 12860 12861 verifyFormat("namespace Foo\n" 12862 "{};", 12863 Style); 12864 verifyFormat("/* something */ namespace Foo\n" 12865 "{};", 12866 Style); 12867 verifyFormat("inline namespace Foo\n" 12868 "{};", 12869 Style); 12870 verifyFormat("/* something */ inline namespace Foo\n" 12871 "{};", 12872 Style); 12873 verifyFormat("export namespace Foo\n" 12874 "{};", 12875 Style); 12876 verifyFormat("namespace Foo\n" 12877 "{\n" 12878 "void Bar();\n" 12879 "};", 12880 Style); 12881 } 12882 12883 TEST_F(FormatTest, NeverMergeShortRecords) { 12884 FormatStyle Style = getLLVMStyle(); 12885 12886 verifyFormat("class Foo {\n" 12887 " Foo();\n" 12888 "};", 12889 Style); 12890 verifyFormat("typedef class Foo {\n" 12891 " Foo();\n" 12892 "} Foo_t;", 12893 Style); 12894 verifyFormat("struct Foo {\n" 12895 " Foo();\n" 12896 "};", 12897 Style); 12898 verifyFormat("typedef struct Foo {\n" 12899 " Foo();\n" 12900 "} Foo_t;", 12901 Style); 12902 verifyFormat("union Foo {\n" 12903 " A,\n" 12904 "};", 12905 Style); 12906 verifyFormat("typedef union Foo {\n" 12907 " A,\n" 12908 "} Foo_t;", 12909 Style); 12910 verifyFormat("namespace Foo {\n" 12911 "void Bar();\n" 12912 "};", 12913 Style); 12914 12915 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12916 Style.BraceWrapping.AfterClass = true; 12917 Style.BraceWrapping.AfterStruct = true; 12918 Style.BraceWrapping.AfterUnion = true; 12919 Style.BraceWrapping.AfterNamespace = true; 12920 verifyFormat("class Foo\n" 12921 "{\n" 12922 " Foo();\n" 12923 "};", 12924 Style); 12925 verifyFormat("typedef class Foo\n" 12926 "{\n" 12927 " Foo();\n" 12928 "} Foo_t;", 12929 Style); 12930 verifyFormat("struct Foo\n" 12931 "{\n" 12932 " Foo();\n" 12933 "};", 12934 Style); 12935 verifyFormat("typedef struct Foo\n" 12936 "{\n" 12937 " Foo();\n" 12938 "} Foo_t;", 12939 Style); 12940 verifyFormat("union Foo\n" 12941 "{\n" 12942 " A,\n" 12943 "};", 12944 Style); 12945 verifyFormat("typedef union Foo\n" 12946 "{\n" 12947 " A,\n" 12948 "} Foo_t;", 12949 Style); 12950 verifyFormat("namespace Foo\n" 12951 "{\n" 12952 "void Bar();\n" 12953 "};", 12954 Style); 12955 } 12956 12957 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12958 // Elaborate type variable declarations. 12959 verifyFormat("struct foo a = {bar};\nint n;"); 12960 verifyFormat("class foo a = {bar};\nint n;"); 12961 verifyFormat("union foo a = {bar};\nint n;"); 12962 12963 // Elaborate types inside function definitions. 12964 verifyFormat("struct foo f() {}\nint n;"); 12965 verifyFormat("class foo f() {}\nint n;"); 12966 verifyFormat("union foo f() {}\nint n;"); 12967 12968 // Templates. 12969 verifyFormat("template <class X> void f() {}\nint n;"); 12970 verifyFormat("template <struct X> void f() {}\nint n;"); 12971 verifyFormat("template <union X> void f() {}\nint n;"); 12972 12973 // Actual definitions... 12974 verifyFormat("struct {\n} n;"); 12975 verifyFormat( 12976 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12977 verifyFormat("union Z {\n int n;\n} x;"); 12978 verifyFormat("class MACRO Z {\n} n;"); 12979 verifyFormat("class MACRO(X) Z {\n} n;"); 12980 verifyFormat("class __attribute__(X) Z {\n} n;"); 12981 verifyFormat("class __declspec(X) Z {\n} n;"); 12982 verifyFormat("class A##B##C {\n} n;"); 12983 verifyFormat("class alignas(16) Z {\n} n;"); 12984 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12985 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12986 12987 // Redefinition from nested context: 12988 verifyFormat("class A::B::C {\n} n;"); 12989 12990 // Template definitions. 12991 verifyFormat( 12992 "template <typename F>\n" 12993 "Matcher(const Matcher<F> &Other,\n" 12994 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12995 " !is_same<F, T>::value>::type * = 0)\n" 12996 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12997 12998 // FIXME: This is still incorrectly handled at the formatter side. 12999 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 13000 verifyFormat("int i = SomeFunction(a<b, a> b);"); 13001 13002 // FIXME: 13003 // This now gets parsed incorrectly as class definition. 13004 // verifyFormat("class A<int> f() {\n}\nint n;"); 13005 13006 // Elaborate types where incorrectly parsing the structural element would 13007 // break the indent. 13008 verifyFormat("if (true)\n" 13009 " class X x;\n" 13010 "else\n" 13011 " f();\n"); 13012 13013 // This is simply incomplete. Formatting is not important, but must not crash. 13014 verifyFormat("class A:"); 13015 } 13016 13017 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 13018 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 13019 format("#error Leave all white!!!!! space* alone!\n")); 13020 EXPECT_EQ( 13021 "#warning Leave all white!!!!! space* alone!\n", 13022 format("#warning Leave all white!!!!! space* alone!\n")); 13023 EXPECT_EQ("#error 1", format(" # error 1")); 13024 EXPECT_EQ("#warning 1", format(" # warning 1")); 13025 } 13026 13027 TEST_F(FormatTest, FormatHashIfExpressions) { 13028 verifyFormat("#if AAAA && BBBB"); 13029 verifyFormat("#if (AAAA && BBBB)"); 13030 verifyFormat("#elif (AAAA && BBBB)"); 13031 // FIXME: Come up with a better indentation for #elif. 13032 verifyFormat( 13033 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 13034 " defined(BBBBBBBB)\n" 13035 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 13036 " defined(BBBBBBBB)\n" 13037 "#endif", 13038 getLLVMStyleWithColumns(65)); 13039 } 13040 13041 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 13042 FormatStyle AllowsMergedIf = getGoogleStyle(); 13043 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 13044 FormatStyle::SIS_WithoutElse; 13045 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 13046 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 13047 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 13048 EXPECT_EQ("if (true) return 42;", 13049 format("if (true)\nreturn 42;", AllowsMergedIf)); 13050 FormatStyle ShortMergedIf = AllowsMergedIf; 13051 ShortMergedIf.ColumnLimit = 25; 13052 verifyFormat("#define A \\\n" 13053 " if (true) return 42;", 13054 ShortMergedIf); 13055 verifyFormat("#define A \\\n" 13056 " f(); \\\n" 13057 " if (true)\n" 13058 "#define B", 13059 ShortMergedIf); 13060 verifyFormat("#define A \\\n" 13061 " f(); \\\n" 13062 " if (true)\n" 13063 "g();", 13064 ShortMergedIf); 13065 verifyFormat("{\n" 13066 "#ifdef A\n" 13067 " // Comment\n" 13068 " if (true) continue;\n" 13069 "#endif\n" 13070 " // Comment\n" 13071 " if (true) continue;\n" 13072 "}", 13073 ShortMergedIf); 13074 ShortMergedIf.ColumnLimit = 33; 13075 verifyFormat("#define A \\\n" 13076 " if constexpr (true) return 42;", 13077 ShortMergedIf); 13078 verifyFormat("#define A \\\n" 13079 " if CONSTEXPR (true) return 42;", 13080 ShortMergedIf); 13081 ShortMergedIf.ColumnLimit = 29; 13082 verifyFormat("#define A \\\n" 13083 " if (aaaaaaaaaa) return 1; \\\n" 13084 " return 2;", 13085 ShortMergedIf); 13086 ShortMergedIf.ColumnLimit = 28; 13087 verifyFormat("#define A \\\n" 13088 " if (aaaaaaaaaa) \\\n" 13089 " return 1; \\\n" 13090 " return 2;", 13091 ShortMergedIf); 13092 verifyFormat("#define A \\\n" 13093 " if constexpr (aaaaaaa) \\\n" 13094 " return 1; \\\n" 13095 " return 2;", 13096 ShortMergedIf); 13097 verifyFormat("#define A \\\n" 13098 " if CONSTEXPR (aaaaaaa) \\\n" 13099 " return 1; \\\n" 13100 " return 2;", 13101 ShortMergedIf); 13102 } 13103 13104 TEST_F(FormatTest, FormatStarDependingOnContext) { 13105 verifyFormat("void f(int *a);"); 13106 verifyFormat("void f() { f(fint * b); }"); 13107 verifyFormat("class A {\n void f(int *a);\n};"); 13108 verifyFormat("class A {\n int *a;\n};"); 13109 verifyFormat("namespace a {\n" 13110 "namespace b {\n" 13111 "class A {\n" 13112 " void f() {}\n" 13113 " int *a;\n" 13114 "};\n" 13115 "} // namespace b\n" 13116 "} // namespace a"); 13117 } 13118 13119 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 13120 verifyFormat("while"); 13121 verifyFormat("operator"); 13122 } 13123 13124 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 13125 // This code would be painfully slow to format if we didn't skip it. 13126 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 13127 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13128 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13129 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13130 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13131 "A(1, 1)\n" 13132 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 13133 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13134 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13135 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13136 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13137 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13138 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13139 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13140 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13141 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 13142 // Deeply nested part is untouched, rest is formatted. 13143 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 13144 format(std::string("int i;\n") + Code + "int j;\n", 13145 getLLVMStyle(), SC_ExpectIncomplete)); 13146 } 13147 13148 //===----------------------------------------------------------------------===// 13149 // Objective-C tests. 13150 //===----------------------------------------------------------------------===// 13151 13152 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 13153 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 13154 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 13155 format("-(NSUInteger)indexOfObject:(id)anObject;")); 13156 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 13157 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 13158 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 13159 format("-(NSInteger)Method3:(id)anObject;")); 13160 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 13161 format("-(NSInteger)Method4:(id)anObject;")); 13162 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 13163 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 13164 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 13165 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 13166 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 13167 "forAllCells:(BOOL)flag;", 13168 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 13169 "forAllCells:(BOOL)flag;")); 13170 13171 // Very long objectiveC method declaration. 13172 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 13173 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 13174 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 13175 " inRange:(NSRange)range\n" 13176 " outRange:(NSRange)out_range\n" 13177 " outRange1:(NSRange)out_range1\n" 13178 " outRange2:(NSRange)out_range2\n" 13179 " outRange3:(NSRange)out_range3\n" 13180 " outRange4:(NSRange)out_range4\n" 13181 " outRange5:(NSRange)out_range5\n" 13182 " outRange6:(NSRange)out_range6\n" 13183 " outRange7:(NSRange)out_range7\n" 13184 " outRange8:(NSRange)out_range8\n" 13185 " outRange9:(NSRange)out_range9;"); 13186 13187 // When the function name has to be wrapped. 13188 FormatStyle Style = getLLVMStyle(); 13189 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 13190 // and always indents instead. 13191 Style.IndentWrappedFunctionNames = false; 13192 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13193 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 13194 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 13195 "}", 13196 Style); 13197 Style.IndentWrappedFunctionNames = true; 13198 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13199 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 13200 " anotherName:(NSString)dddddddddddddd {\n" 13201 "}", 13202 Style); 13203 13204 verifyFormat("- (int)sum:(vector<int>)numbers;"); 13205 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 13206 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 13207 // protocol lists (but not for template classes): 13208 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 13209 13210 verifyFormat("- (int (*)())foo:(int (*)())f;"); 13211 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 13212 13213 // If there's no return type (very rare in practice!), LLVM and Google style 13214 // agree. 13215 verifyFormat("- foo;"); 13216 verifyFormat("- foo:(int)f;"); 13217 verifyGoogleFormat("- foo:(int)foo;"); 13218 } 13219 13220 TEST_F(FormatTest, BreaksStringLiterals) { 13221 EXPECT_EQ("\"some text \"\n" 13222 "\"other\";", 13223 format("\"some text other\";", getLLVMStyleWithColumns(12))); 13224 EXPECT_EQ("\"some text \"\n" 13225 "\"other\";", 13226 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 13227 EXPECT_EQ( 13228 "#define A \\\n" 13229 " \"some \" \\\n" 13230 " \"text \" \\\n" 13231 " \"other\";", 13232 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 13233 EXPECT_EQ( 13234 "#define A \\\n" 13235 " \"so \" \\\n" 13236 " \"text \" \\\n" 13237 " \"other\";", 13238 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 13239 13240 EXPECT_EQ("\"some text\"", 13241 format("\"some text\"", getLLVMStyleWithColumns(1))); 13242 EXPECT_EQ("\"some text\"", 13243 format("\"some text\"", getLLVMStyleWithColumns(11))); 13244 EXPECT_EQ("\"some \"\n" 13245 "\"text\"", 13246 format("\"some text\"", getLLVMStyleWithColumns(10))); 13247 EXPECT_EQ("\"some \"\n" 13248 "\"text\"", 13249 format("\"some text\"", getLLVMStyleWithColumns(7))); 13250 EXPECT_EQ("\"some\"\n" 13251 "\" tex\"\n" 13252 "\"t\"", 13253 format("\"some text\"", getLLVMStyleWithColumns(6))); 13254 EXPECT_EQ("\"some\"\n" 13255 "\" tex\"\n" 13256 "\" and\"", 13257 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 13258 EXPECT_EQ("\"some\"\n" 13259 "\"/tex\"\n" 13260 "\"/and\"", 13261 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 13262 13263 EXPECT_EQ("variable =\n" 13264 " \"long string \"\n" 13265 " \"literal\";", 13266 format("variable = \"long string literal\";", 13267 getLLVMStyleWithColumns(20))); 13268 13269 EXPECT_EQ("variable = f(\n" 13270 " \"long string \"\n" 13271 " \"literal\",\n" 13272 " short,\n" 13273 " loooooooooooooooooooong);", 13274 format("variable = f(\"long string literal\", short, " 13275 "loooooooooooooooooooong);", 13276 getLLVMStyleWithColumns(20))); 13277 13278 EXPECT_EQ( 13279 "f(g(\"long string \"\n" 13280 " \"literal\"),\n" 13281 " b);", 13282 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 13283 EXPECT_EQ("f(g(\"long string \"\n" 13284 " \"literal\",\n" 13285 " a),\n" 13286 " b);", 13287 format("f(g(\"long string literal\", a), b);", 13288 getLLVMStyleWithColumns(20))); 13289 EXPECT_EQ( 13290 "f(\"one two\".split(\n" 13291 " variable));", 13292 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 13293 EXPECT_EQ("f(\"one two three four five six \"\n" 13294 " \"seven\".split(\n" 13295 " really_looooong_variable));", 13296 format("f(\"one two three four five six seven\"." 13297 "split(really_looooong_variable));", 13298 getLLVMStyleWithColumns(33))); 13299 13300 EXPECT_EQ("f(\"some \"\n" 13301 " \"text\",\n" 13302 " other);", 13303 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 13304 13305 // Only break as a last resort. 13306 verifyFormat( 13307 "aaaaaaaaaaaaaaaaaaaa(\n" 13308 " aaaaaaaaaaaaaaaaaaaa,\n" 13309 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 13310 13311 EXPECT_EQ("\"splitmea\"\n" 13312 "\"trandomp\"\n" 13313 "\"oint\"", 13314 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 13315 13316 EXPECT_EQ("\"split/\"\n" 13317 "\"pathat/\"\n" 13318 "\"slashes\"", 13319 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13320 13321 EXPECT_EQ("\"split/\"\n" 13322 "\"pathat/\"\n" 13323 "\"slashes\"", 13324 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13325 EXPECT_EQ("\"split at \"\n" 13326 "\"spaces/at/\"\n" 13327 "\"slashes.at.any$\"\n" 13328 "\"non-alphanumeric%\"\n" 13329 "\"1111111111characte\"\n" 13330 "\"rs\"", 13331 format("\"split at " 13332 "spaces/at/" 13333 "slashes.at." 13334 "any$non-" 13335 "alphanumeric%" 13336 "1111111111characte" 13337 "rs\"", 13338 getLLVMStyleWithColumns(20))); 13339 13340 // Verify that splitting the strings understands 13341 // Style::AlwaysBreakBeforeMultilineStrings. 13342 EXPECT_EQ("aaaaaaaaaaaa(\n" 13343 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 13344 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 13345 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 13346 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13347 "aaaaaaaaaaaaaaaaaaaaaa\");", 13348 getGoogleStyle())); 13349 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13350 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 13351 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 13352 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13353 "aaaaaaaaaaaaaaaaaaaaaa\";", 13354 getGoogleStyle())); 13355 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13356 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13357 format("llvm::outs() << " 13358 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 13359 "aaaaaaaaaaaaaaaaaaa\";")); 13360 EXPECT_EQ("ffff(\n" 13361 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13362 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13363 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 13364 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13365 getGoogleStyle())); 13366 13367 FormatStyle Style = getLLVMStyleWithColumns(12); 13368 Style.BreakStringLiterals = false; 13369 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 13370 13371 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 13372 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13373 EXPECT_EQ("#define A \\\n" 13374 " \"some \" \\\n" 13375 " \"text \" \\\n" 13376 " \"other\";", 13377 format("#define A \"some text other\";", AlignLeft)); 13378 } 13379 13380 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 13381 EXPECT_EQ("C a = \"some more \"\n" 13382 " \"text\";", 13383 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 13384 } 13385 13386 TEST_F(FormatTest, FullyRemoveEmptyLines) { 13387 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 13388 NoEmptyLines.MaxEmptyLinesToKeep = 0; 13389 EXPECT_EQ("int i = a(b());", 13390 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 13391 } 13392 13393 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 13394 EXPECT_EQ( 13395 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13396 "(\n" 13397 " \"x\t\");", 13398 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13399 "aaaaaaa(" 13400 "\"x\t\");")); 13401 } 13402 13403 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 13404 EXPECT_EQ( 13405 "u8\"utf8 string \"\n" 13406 "u8\"literal\";", 13407 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 13408 EXPECT_EQ( 13409 "u\"utf16 string \"\n" 13410 "u\"literal\";", 13411 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 13412 EXPECT_EQ( 13413 "U\"utf32 string \"\n" 13414 "U\"literal\";", 13415 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 13416 EXPECT_EQ("L\"wide string \"\n" 13417 "L\"literal\";", 13418 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 13419 EXPECT_EQ("@\"NSString \"\n" 13420 "@\"literal\";", 13421 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 13422 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 13423 13424 // This input makes clang-format try to split the incomplete unicode escape 13425 // sequence, which used to lead to a crasher. 13426 verifyNoCrash( 13427 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 13428 getLLVMStyleWithColumns(60)); 13429 } 13430 13431 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 13432 FormatStyle Style = getGoogleStyleWithColumns(15); 13433 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 13434 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 13435 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 13436 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 13437 EXPECT_EQ("u8R\"x(raw literal)x\";", 13438 format("u8R\"x(raw literal)x\";", Style)); 13439 } 13440 13441 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 13442 FormatStyle Style = getLLVMStyleWithColumns(20); 13443 EXPECT_EQ( 13444 "_T(\"aaaaaaaaaaaaaa\")\n" 13445 "_T(\"aaaaaaaaaaaaaa\")\n" 13446 "_T(\"aaaaaaaaaaaa\")", 13447 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 13448 EXPECT_EQ("f(x,\n" 13449 " _T(\"aaaaaaaaaaaa\")\n" 13450 " _T(\"aaa\"),\n" 13451 " z);", 13452 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 13453 13454 // FIXME: Handle embedded spaces in one iteration. 13455 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 13456 // "_T(\"aaaaaaaaaaaaa\")\n" 13457 // "_T(\"aaaaaaaaaaaaa\")\n" 13458 // "_T(\"a\")", 13459 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13460 // getLLVMStyleWithColumns(20))); 13461 EXPECT_EQ( 13462 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13463 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 13464 EXPECT_EQ("f(\n" 13465 "#if !TEST\n" 13466 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13467 "#endif\n" 13468 ");", 13469 format("f(\n" 13470 "#if !TEST\n" 13471 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13472 "#endif\n" 13473 ");")); 13474 EXPECT_EQ("f(\n" 13475 "\n" 13476 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 13477 format("f(\n" 13478 "\n" 13479 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 13480 // Regression test for accessing tokens past the end of a vector in the 13481 // TokenLexer. 13482 verifyNoCrash(R"(_T( 13483 " 13484 ) 13485 )"); 13486 } 13487 13488 TEST_F(FormatTest, BreaksStringLiteralOperands) { 13489 // In a function call with two operands, the second can be broken with no line 13490 // break before it. 13491 EXPECT_EQ( 13492 "func(a, \"long long \"\n" 13493 " \"long long\");", 13494 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 13495 // In a function call with three operands, the second must be broken with a 13496 // line break before it. 13497 EXPECT_EQ("func(a,\n" 13498 " \"long long long \"\n" 13499 " \"long\",\n" 13500 " c);", 13501 format("func(a, \"long long long long\", c);", 13502 getLLVMStyleWithColumns(24))); 13503 // In a function call with three operands, the third must be broken with a 13504 // line break before it. 13505 EXPECT_EQ("func(a, b,\n" 13506 " \"long long long \"\n" 13507 " \"long\");", 13508 format("func(a, b, \"long long long long\");", 13509 getLLVMStyleWithColumns(24))); 13510 // In a function call with three operands, both the second and the third must 13511 // be broken with a line break before them. 13512 EXPECT_EQ("func(a,\n" 13513 " \"long long long \"\n" 13514 " \"long\",\n" 13515 " \"long long long \"\n" 13516 " \"long\");", 13517 format("func(a, \"long long long long\", \"long long long long\");", 13518 getLLVMStyleWithColumns(24))); 13519 // In a chain of << with two operands, the second can be broken with no line 13520 // break before it. 13521 EXPECT_EQ("a << \"line line \"\n" 13522 " \"line\";", 13523 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 13524 // In a chain of << with three operands, the second can be broken with no line 13525 // break before it. 13526 EXPECT_EQ( 13527 "abcde << \"line \"\n" 13528 " \"line line\"\n" 13529 " << c;", 13530 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 13531 // In a chain of << with three operands, the third must be broken with a line 13532 // break before it. 13533 EXPECT_EQ( 13534 "a << b\n" 13535 " << \"line line \"\n" 13536 " \"line\";", 13537 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 13538 // In a chain of << with three operands, the second can be broken with no line 13539 // break before it and the third must be broken with a line break before it. 13540 EXPECT_EQ("abcd << \"line line \"\n" 13541 " \"line\"\n" 13542 " << \"line line \"\n" 13543 " \"line\";", 13544 format("abcd << \"line line line\" << \"line line line\";", 13545 getLLVMStyleWithColumns(20))); 13546 // In a chain of binary operators with two operands, the second can be broken 13547 // with no line break before it. 13548 EXPECT_EQ( 13549 "abcd + \"line line \"\n" 13550 " \"line line\";", 13551 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 13552 // In a chain of binary operators with three operands, the second must be 13553 // broken with a line break before it. 13554 EXPECT_EQ("abcd +\n" 13555 " \"line line \"\n" 13556 " \"line line\" +\n" 13557 " e;", 13558 format("abcd + \"line line line line\" + e;", 13559 getLLVMStyleWithColumns(20))); 13560 // In a function call with two operands, with AlignAfterOpenBracket enabled, 13561 // the first must be broken with a line break before it. 13562 FormatStyle Style = getLLVMStyleWithColumns(25); 13563 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13564 EXPECT_EQ("someFunction(\n" 13565 " \"long long long \"\n" 13566 " \"long\",\n" 13567 " a);", 13568 format("someFunction(\"long long long long\", a);", Style)); 13569 } 13570 13571 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 13572 EXPECT_EQ( 13573 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13576 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 13579 } 13580 13581 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 13582 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 13583 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 13584 EXPECT_EQ("fffffffffff(g(R\"x(\n" 13585 "multiline raw string literal xxxxxxxxxxxxxx\n" 13586 ")x\",\n" 13587 " a),\n" 13588 " b);", 13589 format("fffffffffff(g(R\"x(\n" 13590 "multiline raw string literal xxxxxxxxxxxxxx\n" 13591 ")x\", a), b);", 13592 getGoogleStyleWithColumns(20))); 13593 EXPECT_EQ("fffffffffff(\n" 13594 " g(R\"x(qqq\n" 13595 "multiline raw string literal xxxxxxxxxxxxxx\n" 13596 ")x\",\n" 13597 " a),\n" 13598 " b);", 13599 format("fffffffffff(g(R\"x(qqq\n" 13600 "multiline raw string literal xxxxxxxxxxxxxx\n" 13601 ")x\", a), b);", 13602 getGoogleStyleWithColumns(20))); 13603 13604 EXPECT_EQ("fffffffffff(R\"x(\n" 13605 "multiline raw string literal xxxxxxxxxxxxxx\n" 13606 ")x\");", 13607 format("fffffffffff(R\"x(\n" 13608 "multiline raw string literal xxxxxxxxxxxxxx\n" 13609 ")x\");", 13610 getGoogleStyleWithColumns(20))); 13611 EXPECT_EQ("fffffffffff(R\"x(\n" 13612 "multiline raw string literal xxxxxxxxxxxxxx\n" 13613 ")x\" + bbbbbb);", 13614 format("fffffffffff(R\"x(\n" 13615 "multiline raw string literal xxxxxxxxxxxxxx\n" 13616 ")x\" + bbbbbb);", 13617 getGoogleStyleWithColumns(20))); 13618 EXPECT_EQ("fffffffffff(\n" 13619 " R\"x(\n" 13620 "multiline raw string literal xxxxxxxxxxxxxx\n" 13621 ")x\" +\n" 13622 " bbbbbb);", 13623 format("fffffffffff(\n" 13624 " R\"x(\n" 13625 "multiline raw string literal xxxxxxxxxxxxxx\n" 13626 ")x\" + bbbbbb);", 13627 getGoogleStyleWithColumns(20))); 13628 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 13629 format("fffffffffff(\n" 13630 " R\"(single line raw string)\" + bbbbbb);")); 13631 } 13632 13633 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13634 verifyFormat("string a = \"unterminated;"); 13635 EXPECT_EQ("function(\"unterminated,\n" 13636 " OtherParameter);", 13637 format("function( \"unterminated,\n" 13638 " OtherParameter);")); 13639 } 13640 13641 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13642 FormatStyle Style = getLLVMStyle(); 13643 Style.Standard = FormatStyle::LS_Cpp03; 13644 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13645 format("#define x(_a) printf(\"foo\"_a);", Style)); 13646 } 13647 13648 TEST_F(FormatTest, CppLexVersion) { 13649 FormatStyle Style = getLLVMStyle(); 13650 // Formatting of x * y differs if x is a type. 13651 verifyFormat("void foo() { MACRO(a * b); }", Style); 13652 verifyFormat("void foo() { MACRO(int *b); }", Style); 13653 13654 // LLVM style uses latest lexer. 13655 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13656 Style.Standard = FormatStyle::LS_Cpp17; 13657 // But in c++17, char8_t isn't a keyword. 13658 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13659 } 13660 13661 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13662 13663 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13664 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13665 " \"ddeeefff\");", 13666 format("someFunction(\"aaabbbcccdddeeefff\");", 13667 getLLVMStyleWithColumns(25))); 13668 EXPECT_EQ("someFunction1234567890(\n" 13669 " \"aaabbbcccdddeeefff\");", 13670 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13671 getLLVMStyleWithColumns(26))); 13672 EXPECT_EQ("someFunction1234567890(\n" 13673 " \"aaabbbcccdddeeeff\"\n" 13674 " \"f\");", 13675 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13676 getLLVMStyleWithColumns(25))); 13677 EXPECT_EQ("someFunction1234567890(\n" 13678 " \"aaabbbcccdddeeeff\"\n" 13679 " \"f\");", 13680 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13681 getLLVMStyleWithColumns(24))); 13682 EXPECT_EQ("someFunction(\n" 13683 " \"aaabbbcc ddde \"\n" 13684 " \"efff\");", 13685 format("someFunction(\"aaabbbcc ddde efff\");", 13686 getLLVMStyleWithColumns(25))); 13687 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13688 " \"ddeeefff\");", 13689 format("someFunction(\"aaabbbccc ddeeefff\");", 13690 getLLVMStyleWithColumns(25))); 13691 EXPECT_EQ("someFunction1234567890(\n" 13692 " \"aaabb \"\n" 13693 " \"cccdddeeefff\");", 13694 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13695 getLLVMStyleWithColumns(25))); 13696 EXPECT_EQ("#define A \\\n" 13697 " string s = \\\n" 13698 " \"123456789\" \\\n" 13699 " \"0\"; \\\n" 13700 " int i;", 13701 format("#define A string s = \"1234567890\"; int i;", 13702 getLLVMStyleWithColumns(20))); 13703 EXPECT_EQ("someFunction(\n" 13704 " \"aaabbbcc \"\n" 13705 " \"dddeeefff\");", 13706 format("someFunction(\"aaabbbcc dddeeefff\");", 13707 getLLVMStyleWithColumns(25))); 13708 } 13709 13710 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13711 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13712 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13713 EXPECT_EQ("\"test\"\n" 13714 "\"\\n\"", 13715 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13716 EXPECT_EQ("\"tes\\\\\"\n" 13717 "\"n\"", 13718 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13719 EXPECT_EQ("\"\\\\\\\\\"\n" 13720 "\"\\n\"", 13721 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13722 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13723 EXPECT_EQ("\"\\uff01\"\n" 13724 "\"test\"", 13725 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13726 EXPECT_EQ("\"\\Uff01ff02\"", 13727 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13728 EXPECT_EQ("\"\\x000000000001\"\n" 13729 "\"next\"", 13730 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13731 EXPECT_EQ("\"\\x000000000001next\"", 13732 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13733 EXPECT_EQ("\"\\x000000000001\"", 13734 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13735 EXPECT_EQ("\"test\"\n" 13736 "\"\\000000\"\n" 13737 "\"000001\"", 13738 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13739 EXPECT_EQ("\"test\\000\"\n" 13740 "\"00000000\"\n" 13741 "\"1\"", 13742 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13743 } 13744 13745 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13746 verifyFormat("void f() {\n" 13747 " return g() {}\n" 13748 " void h() {}"); 13749 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13750 "g();\n" 13751 "}"); 13752 } 13753 13754 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13755 verifyFormat( 13756 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13757 } 13758 13759 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13760 verifyFormat("class X {\n" 13761 " void f() {\n" 13762 " }\n" 13763 "};", 13764 getLLVMStyleWithColumns(12)); 13765 } 13766 13767 TEST_F(FormatTest, ConfigurableIndentWidth) { 13768 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13769 EightIndent.IndentWidth = 8; 13770 EightIndent.ContinuationIndentWidth = 8; 13771 verifyFormat("void f() {\n" 13772 " someFunction();\n" 13773 " if (true) {\n" 13774 " f();\n" 13775 " }\n" 13776 "}", 13777 EightIndent); 13778 verifyFormat("class X {\n" 13779 " void f() {\n" 13780 " }\n" 13781 "};", 13782 EightIndent); 13783 verifyFormat("int x[] = {\n" 13784 " call(),\n" 13785 " call()};", 13786 EightIndent); 13787 } 13788 13789 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13790 verifyFormat("double\n" 13791 "f();", 13792 getLLVMStyleWithColumns(8)); 13793 } 13794 13795 TEST_F(FormatTest, ConfigurableUseOfTab) { 13796 FormatStyle Tab = getLLVMStyleWithColumns(42); 13797 Tab.IndentWidth = 8; 13798 Tab.UseTab = FormatStyle::UT_Always; 13799 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13800 13801 EXPECT_EQ("if (aaaaaaaa && // q\n" 13802 " bb)\t\t// w\n" 13803 "\t;", 13804 format("if (aaaaaaaa &&// q\n" 13805 "bb)// w\n" 13806 ";", 13807 Tab)); 13808 EXPECT_EQ("if (aaa && bbb) // w\n" 13809 "\t;", 13810 format("if(aaa&&bbb)// w\n" 13811 ";", 13812 Tab)); 13813 13814 verifyFormat("class X {\n" 13815 "\tvoid f() {\n" 13816 "\t\tsomeFunction(parameter1,\n" 13817 "\t\t\t parameter2);\n" 13818 "\t}\n" 13819 "};", 13820 Tab); 13821 verifyFormat("#define A \\\n" 13822 "\tvoid f() { \\\n" 13823 "\t\tsomeFunction( \\\n" 13824 "\t\t parameter1, \\\n" 13825 "\t\t parameter2); \\\n" 13826 "\t}", 13827 Tab); 13828 verifyFormat("int a;\t // x\n" 13829 "int bbbbbbbb; // x\n", 13830 Tab); 13831 13832 Tab.TabWidth = 4; 13833 Tab.IndentWidth = 8; 13834 verifyFormat("class TabWidth4Indent8 {\n" 13835 "\t\tvoid f() {\n" 13836 "\t\t\t\tsomeFunction(parameter1,\n" 13837 "\t\t\t\t\t\t\t parameter2);\n" 13838 "\t\t}\n" 13839 "};", 13840 Tab); 13841 13842 Tab.TabWidth = 4; 13843 Tab.IndentWidth = 4; 13844 verifyFormat("class TabWidth4Indent4 {\n" 13845 "\tvoid f() {\n" 13846 "\t\tsomeFunction(parameter1,\n" 13847 "\t\t\t\t\t parameter2);\n" 13848 "\t}\n" 13849 "};", 13850 Tab); 13851 13852 Tab.TabWidth = 8; 13853 Tab.IndentWidth = 4; 13854 verifyFormat("class TabWidth8Indent4 {\n" 13855 " void f() {\n" 13856 "\tsomeFunction(parameter1,\n" 13857 "\t\t parameter2);\n" 13858 " }\n" 13859 "};", 13860 Tab); 13861 13862 Tab.TabWidth = 8; 13863 Tab.IndentWidth = 8; 13864 EXPECT_EQ("/*\n" 13865 "\t a\t\tcomment\n" 13866 "\t in multiple lines\n" 13867 " */", 13868 format(" /*\t \t \n" 13869 " \t \t a\t\tcomment\t \t\n" 13870 " \t \t in multiple lines\t\n" 13871 " \t */", 13872 Tab)); 13873 13874 Tab.UseTab = FormatStyle::UT_ForIndentation; 13875 verifyFormat("{\n" 13876 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13877 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13878 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13879 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13880 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13881 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13882 "};", 13883 Tab); 13884 verifyFormat("enum AA {\n" 13885 "\ta1, // Force multiple lines\n" 13886 "\ta2,\n" 13887 "\ta3\n" 13888 "};", 13889 Tab); 13890 EXPECT_EQ("if (aaaaaaaa && // q\n" 13891 " bb) // w\n" 13892 "\t;", 13893 format("if (aaaaaaaa &&// q\n" 13894 "bb)// w\n" 13895 ";", 13896 Tab)); 13897 verifyFormat("class X {\n" 13898 "\tvoid f() {\n" 13899 "\t\tsomeFunction(parameter1,\n" 13900 "\t\t parameter2);\n" 13901 "\t}\n" 13902 "};", 13903 Tab); 13904 verifyFormat("{\n" 13905 "\tQ(\n" 13906 "\t {\n" 13907 "\t\t int a;\n" 13908 "\t\t someFunction(aaaaaaaa,\n" 13909 "\t\t bbbbbbb);\n" 13910 "\t },\n" 13911 "\t p);\n" 13912 "}", 13913 Tab); 13914 EXPECT_EQ("{\n" 13915 "\t/* aaaa\n" 13916 "\t bbbb */\n" 13917 "}", 13918 format("{\n" 13919 "/* aaaa\n" 13920 " bbbb */\n" 13921 "}", 13922 Tab)); 13923 EXPECT_EQ("{\n" 13924 "\t/*\n" 13925 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13926 "\t bbbbbbbbbbbbb\n" 13927 "\t*/\n" 13928 "}", 13929 format("{\n" 13930 "/*\n" 13931 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13932 "*/\n" 13933 "}", 13934 Tab)); 13935 EXPECT_EQ("{\n" 13936 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13937 "\t// bbbbbbbbbbbbb\n" 13938 "}", 13939 format("{\n" 13940 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13941 "}", 13942 Tab)); 13943 EXPECT_EQ("{\n" 13944 "\t/*\n" 13945 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13946 "\t bbbbbbbbbbbbb\n" 13947 "\t*/\n" 13948 "}", 13949 format("{\n" 13950 "\t/*\n" 13951 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13952 "\t*/\n" 13953 "}", 13954 Tab)); 13955 EXPECT_EQ("{\n" 13956 "\t/*\n" 13957 "\n" 13958 "\t*/\n" 13959 "}", 13960 format("{\n" 13961 "\t/*\n" 13962 "\n" 13963 "\t*/\n" 13964 "}", 13965 Tab)); 13966 EXPECT_EQ("{\n" 13967 "\t/*\n" 13968 " asdf\n" 13969 "\t*/\n" 13970 "}", 13971 format("{\n" 13972 "\t/*\n" 13973 " asdf\n" 13974 "\t*/\n" 13975 "}", 13976 Tab)); 13977 13978 verifyFormat("void f() {\n" 13979 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 13980 "\t : bbbbbbbbbbbbbbbbbb\n" 13981 "}", 13982 Tab); 13983 FormatStyle TabNoBreak = Tab; 13984 TabNoBreak.BreakBeforeTernaryOperators = false; 13985 verifyFormat("void f() {\n" 13986 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 13987 "\t bbbbbbbbbbbbbbbbbb\n" 13988 "}", 13989 TabNoBreak); 13990 verifyFormat("void f() {\n" 13991 "\treturn true ?\n" 13992 "\t aaaaaaaaaaaaaaaaaaaa :\n" 13993 "\t bbbbbbbbbbbbbbbbbbbb\n" 13994 "}", 13995 TabNoBreak); 13996 13997 Tab.UseTab = FormatStyle::UT_Never; 13998 EXPECT_EQ("/*\n" 13999 " a\t\tcomment\n" 14000 " in multiple lines\n" 14001 " */", 14002 format(" /*\t \t \n" 14003 " \t \t a\t\tcomment\t \t\n" 14004 " \t \t in multiple lines\t\n" 14005 " \t */", 14006 Tab)); 14007 EXPECT_EQ("/* some\n" 14008 " comment */", 14009 format(" \t \t /* some\n" 14010 " \t \t comment */", 14011 Tab)); 14012 EXPECT_EQ("int a; /* some\n" 14013 " comment */", 14014 format(" \t \t int a; /* some\n" 14015 " \t \t comment */", 14016 Tab)); 14017 14018 EXPECT_EQ("int a; /* some\n" 14019 "comment */", 14020 format(" \t \t int\ta; /* some\n" 14021 " \t \t comment */", 14022 Tab)); 14023 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14024 " comment */", 14025 format(" \t \t f(\"\t\t\"); /* some\n" 14026 " \t \t comment */", 14027 Tab)); 14028 EXPECT_EQ("{\n" 14029 " /*\n" 14030 " * Comment\n" 14031 " */\n" 14032 " int i;\n" 14033 "}", 14034 format("{\n" 14035 "\t/*\n" 14036 "\t * Comment\n" 14037 "\t */\n" 14038 "\t int i;\n" 14039 "}", 14040 Tab)); 14041 14042 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14043 Tab.TabWidth = 8; 14044 Tab.IndentWidth = 8; 14045 EXPECT_EQ("if (aaaaaaaa && // q\n" 14046 " bb) // w\n" 14047 "\t;", 14048 format("if (aaaaaaaa &&// q\n" 14049 "bb)// w\n" 14050 ";", 14051 Tab)); 14052 EXPECT_EQ("if (aaa && bbb) // w\n" 14053 "\t;", 14054 format("if(aaa&&bbb)// w\n" 14055 ";", 14056 Tab)); 14057 verifyFormat("class X {\n" 14058 "\tvoid f() {\n" 14059 "\t\tsomeFunction(parameter1,\n" 14060 "\t\t\t parameter2);\n" 14061 "\t}\n" 14062 "};", 14063 Tab); 14064 verifyFormat("#define A \\\n" 14065 "\tvoid f() { \\\n" 14066 "\t\tsomeFunction( \\\n" 14067 "\t\t parameter1, \\\n" 14068 "\t\t parameter2); \\\n" 14069 "\t}", 14070 Tab); 14071 Tab.TabWidth = 4; 14072 Tab.IndentWidth = 8; 14073 verifyFormat("class TabWidth4Indent8 {\n" 14074 "\t\tvoid f() {\n" 14075 "\t\t\t\tsomeFunction(parameter1,\n" 14076 "\t\t\t\t\t\t\t parameter2);\n" 14077 "\t\t}\n" 14078 "};", 14079 Tab); 14080 Tab.TabWidth = 4; 14081 Tab.IndentWidth = 4; 14082 verifyFormat("class TabWidth4Indent4 {\n" 14083 "\tvoid f() {\n" 14084 "\t\tsomeFunction(parameter1,\n" 14085 "\t\t\t\t\t parameter2);\n" 14086 "\t}\n" 14087 "};", 14088 Tab); 14089 Tab.TabWidth = 8; 14090 Tab.IndentWidth = 4; 14091 verifyFormat("class TabWidth8Indent4 {\n" 14092 " void f() {\n" 14093 "\tsomeFunction(parameter1,\n" 14094 "\t\t parameter2);\n" 14095 " }\n" 14096 "};", 14097 Tab); 14098 Tab.TabWidth = 8; 14099 Tab.IndentWidth = 8; 14100 EXPECT_EQ("/*\n" 14101 "\t a\t\tcomment\n" 14102 "\t in multiple lines\n" 14103 " */", 14104 format(" /*\t \t \n" 14105 " \t \t a\t\tcomment\t \t\n" 14106 " \t \t in multiple lines\t\n" 14107 " \t */", 14108 Tab)); 14109 verifyFormat("{\n" 14110 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14111 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14112 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14113 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14114 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14115 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14116 "};", 14117 Tab); 14118 verifyFormat("enum AA {\n" 14119 "\ta1, // Force multiple lines\n" 14120 "\ta2,\n" 14121 "\ta3\n" 14122 "};", 14123 Tab); 14124 EXPECT_EQ("if (aaaaaaaa && // q\n" 14125 " bb) // w\n" 14126 "\t;", 14127 format("if (aaaaaaaa &&// q\n" 14128 "bb)// w\n" 14129 ";", 14130 Tab)); 14131 verifyFormat("class X {\n" 14132 "\tvoid f() {\n" 14133 "\t\tsomeFunction(parameter1,\n" 14134 "\t\t\t parameter2);\n" 14135 "\t}\n" 14136 "};", 14137 Tab); 14138 verifyFormat("{\n" 14139 "\tQ(\n" 14140 "\t {\n" 14141 "\t\t int a;\n" 14142 "\t\t someFunction(aaaaaaaa,\n" 14143 "\t\t\t\t bbbbbbb);\n" 14144 "\t },\n" 14145 "\t p);\n" 14146 "}", 14147 Tab); 14148 EXPECT_EQ("{\n" 14149 "\t/* aaaa\n" 14150 "\t bbbb */\n" 14151 "}", 14152 format("{\n" 14153 "/* aaaa\n" 14154 " bbbb */\n" 14155 "}", 14156 Tab)); 14157 EXPECT_EQ("{\n" 14158 "\t/*\n" 14159 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14160 "\t bbbbbbbbbbbbb\n" 14161 "\t*/\n" 14162 "}", 14163 format("{\n" 14164 "/*\n" 14165 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14166 "*/\n" 14167 "}", 14168 Tab)); 14169 EXPECT_EQ("{\n" 14170 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14171 "\t// bbbbbbbbbbbbb\n" 14172 "}", 14173 format("{\n" 14174 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14175 "}", 14176 Tab)); 14177 EXPECT_EQ("{\n" 14178 "\t/*\n" 14179 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14180 "\t bbbbbbbbbbbbb\n" 14181 "\t*/\n" 14182 "}", 14183 format("{\n" 14184 "\t/*\n" 14185 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14186 "\t*/\n" 14187 "}", 14188 Tab)); 14189 EXPECT_EQ("{\n" 14190 "\t/*\n" 14191 "\n" 14192 "\t*/\n" 14193 "}", 14194 format("{\n" 14195 "\t/*\n" 14196 "\n" 14197 "\t*/\n" 14198 "}", 14199 Tab)); 14200 EXPECT_EQ("{\n" 14201 "\t/*\n" 14202 " asdf\n" 14203 "\t*/\n" 14204 "}", 14205 format("{\n" 14206 "\t/*\n" 14207 " asdf\n" 14208 "\t*/\n" 14209 "}", 14210 Tab)); 14211 EXPECT_EQ("/* some\n" 14212 " comment */", 14213 format(" \t \t /* some\n" 14214 " \t \t comment */", 14215 Tab)); 14216 EXPECT_EQ("int a; /* some\n" 14217 " comment */", 14218 format(" \t \t int a; /* some\n" 14219 " \t \t comment */", 14220 Tab)); 14221 EXPECT_EQ("int a; /* some\n" 14222 "comment */", 14223 format(" \t \t int\ta; /* some\n" 14224 " \t \t comment */", 14225 Tab)); 14226 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14227 " comment */", 14228 format(" \t \t f(\"\t\t\"); /* some\n" 14229 " \t \t comment */", 14230 Tab)); 14231 EXPECT_EQ("{\n" 14232 "\t/*\n" 14233 "\t * Comment\n" 14234 "\t */\n" 14235 "\tint i;\n" 14236 "}", 14237 format("{\n" 14238 "\t/*\n" 14239 "\t * Comment\n" 14240 "\t */\n" 14241 "\t int i;\n" 14242 "}", 14243 Tab)); 14244 Tab.TabWidth = 2; 14245 Tab.IndentWidth = 2; 14246 EXPECT_EQ("{\n" 14247 "\t/* aaaa\n" 14248 "\t\t bbbb */\n" 14249 "}", 14250 format("{\n" 14251 "/* aaaa\n" 14252 "\t bbbb */\n" 14253 "}", 14254 Tab)); 14255 EXPECT_EQ("{\n" 14256 "\t/*\n" 14257 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14258 "\t\tbbbbbbbbbbbbb\n" 14259 "\t*/\n" 14260 "}", 14261 format("{\n" 14262 "/*\n" 14263 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14264 "*/\n" 14265 "}", 14266 Tab)); 14267 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14268 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14269 Tab.TabWidth = 4; 14270 Tab.IndentWidth = 4; 14271 verifyFormat("class Assign {\n" 14272 "\tvoid f() {\n" 14273 "\t\tint x = 123;\n" 14274 "\t\tint random = 4;\n" 14275 "\t\tstd::string alphabet =\n" 14276 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14277 "\t}\n" 14278 "};", 14279 Tab); 14280 14281 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14282 Tab.TabWidth = 8; 14283 Tab.IndentWidth = 8; 14284 EXPECT_EQ("if (aaaaaaaa && // q\n" 14285 " bb) // w\n" 14286 "\t;", 14287 format("if (aaaaaaaa &&// q\n" 14288 "bb)// w\n" 14289 ";", 14290 Tab)); 14291 EXPECT_EQ("if (aaa && bbb) // w\n" 14292 "\t;", 14293 format("if(aaa&&bbb)// w\n" 14294 ";", 14295 Tab)); 14296 verifyFormat("class X {\n" 14297 "\tvoid f() {\n" 14298 "\t\tsomeFunction(parameter1,\n" 14299 "\t\t parameter2);\n" 14300 "\t}\n" 14301 "};", 14302 Tab); 14303 verifyFormat("#define A \\\n" 14304 "\tvoid f() { \\\n" 14305 "\t\tsomeFunction( \\\n" 14306 "\t\t parameter1, \\\n" 14307 "\t\t parameter2); \\\n" 14308 "\t}", 14309 Tab); 14310 Tab.TabWidth = 4; 14311 Tab.IndentWidth = 8; 14312 verifyFormat("class TabWidth4Indent8 {\n" 14313 "\t\tvoid f() {\n" 14314 "\t\t\t\tsomeFunction(parameter1,\n" 14315 "\t\t\t\t parameter2);\n" 14316 "\t\t}\n" 14317 "};", 14318 Tab); 14319 Tab.TabWidth = 4; 14320 Tab.IndentWidth = 4; 14321 verifyFormat("class TabWidth4Indent4 {\n" 14322 "\tvoid f() {\n" 14323 "\t\tsomeFunction(parameter1,\n" 14324 "\t\t parameter2);\n" 14325 "\t}\n" 14326 "};", 14327 Tab); 14328 Tab.TabWidth = 8; 14329 Tab.IndentWidth = 4; 14330 verifyFormat("class TabWidth8Indent4 {\n" 14331 " void f() {\n" 14332 "\tsomeFunction(parameter1,\n" 14333 "\t parameter2);\n" 14334 " }\n" 14335 "};", 14336 Tab); 14337 Tab.TabWidth = 8; 14338 Tab.IndentWidth = 8; 14339 EXPECT_EQ("/*\n" 14340 " a\t\tcomment\n" 14341 " in multiple lines\n" 14342 " */", 14343 format(" /*\t \t \n" 14344 " \t \t a\t\tcomment\t \t\n" 14345 " \t \t in multiple lines\t\n" 14346 " \t */", 14347 Tab)); 14348 verifyFormat("{\n" 14349 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14350 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14351 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14352 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14353 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14354 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14355 "};", 14356 Tab); 14357 verifyFormat("enum AA {\n" 14358 "\ta1, // Force multiple lines\n" 14359 "\ta2,\n" 14360 "\ta3\n" 14361 "};", 14362 Tab); 14363 EXPECT_EQ("if (aaaaaaaa && // q\n" 14364 " bb) // w\n" 14365 "\t;", 14366 format("if (aaaaaaaa &&// q\n" 14367 "bb)// w\n" 14368 ";", 14369 Tab)); 14370 verifyFormat("class X {\n" 14371 "\tvoid f() {\n" 14372 "\t\tsomeFunction(parameter1,\n" 14373 "\t\t parameter2);\n" 14374 "\t}\n" 14375 "};", 14376 Tab); 14377 verifyFormat("{\n" 14378 "\tQ(\n" 14379 "\t {\n" 14380 "\t\t int a;\n" 14381 "\t\t someFunction(aaaaaaaa,\n" 14382 "\t\t bbbbbbb);\n" 14383 "\t },\n" 14384 "\t p);\n" 14385 "}", 14386 Tab); 14387 EXPECT_EQ("{\n" 14388 "\t/* aaaa\n" 14389 "\t bbbb */\n" 14390 "}", 14391 format("{\n" 14392 "/* aaaa\n" 14393 " bbbb */\n" 14394 "}", 14395 Tab)); 14396 EXPECT_EQ("{\n" 14397 "\t/*\n" 14398 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14399 "\t bbbbbbbbbbbbb\n" 14400 "\t*/\n" 14401 "}", 14402 format("{\n" 14403 "/*\n" 14404 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14405 "*/\n" 14406 "}", 14407 Tab)); 14408 EXPECT_EQ("{\n" 14409 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14410 "\t// bbbbbbbbbbbbb\n" 14411 "}", 14412 format("{\n" 14413 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14414 "}", 14415 Tab)); 14416 EXPECT_EQ("{\n" 14417 "\t/*\n" 14418 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14419 "\t bbbbbbbbbbbbb\n" 14420 "\t*/\n" 14421 "}", 14422 format("{\n" 14423 "\t/*\n" 14424 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14425 "\t*/\n" 14426 "}", 14427 Tab)); 14428 EXPECT_EQ("{\n" 14429 "\t/*\n" 14430 "\n" 14431 "\t*/\n" 14432 "}", 14433 format("{\n" 14434 "\t/*\n" 14435 "\n" 14436 "\t*/\n" 14437 "}", 14438 Tab)); 14439 EXPECT_EQ("{\n" 14440 "\t/*\n" 14441 " asdf\n" 14442 "\t*/\n" 14443 "}", 14444 format("{\n" 14445 "\t/*\n" 14446 " asdf\n" 14447 "\t*/\n" 14448 "}", 14449 Tab)); 14450 EXPECT_EQ("/* some\n" 14451 " comment */", 14452 format(" \t \t /* some\n" 14453 " \t \t comment */", 14454 Tab)); 14455 EXPECT_EQ("int a; /* some\n" 14456 " comment */", 14457 format(" \t \t int a; /* some\n" 14458 " \t \t comment */", 14459 Tab)); 14460 EXPECT_EQ("int a; /* some\n" 14461 "comment */", 14462 format(" \t \t int\ta; /* some\n" 14463 " \t \t comment */", 14464 Tab)); 14465 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14466 " comment */", 14467 format(" \t \t f(\"\t\t\"); /* some\n" 14468 " \t \t comment */", 14469 Tab)); 14470 EXPECT_EQ("{\n" 14471 "\t/*\n" 14472 "\t * Comment\n" 14473 "\t */\n" 14474 "\tint i;\n" 14475 "}", 14476 format("{\n" 14477 "\t/*\n" 14478 "\t * Comment\n" 14479 "\t */\n" 14480 "\t int i;\n" 14481 "}", 14482 Tab)); 14483 Tab.TabWidth = 2; 14484 Tab.IndentWidth = 2; 14485 EXPECT_EQ("{\n" 14486 "\t/* aaaa\n" 14487 "\t bbbb */\n" 14488 "}", 14489 format("{\n" 14490 "/* aaaa\n" 14491 " bbbb */\n" 14492 "}", 14493 Tab)); 14494 EXPECT_EQ("{\n" 14495 "\t/*\n" 14496 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14497 "\t bbbbbbbbbbbbb\n" 14498 "\t*/\n" 14499 "}", 14500 format("{\n" 14501 "/*\n" 14502 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14503 "*/\n" 14504 "}", 14505 Tab)); 14506 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14507 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14508 Tab.TabWidth = 4; 14509 Tab.IndentWidth = 4; 14510 verifyFormat("class Assign {\n" 14511 "\tvoid f() {\n" 14512 "\t\tint x = 123;\n" 14513 "\t\tint random = 4;\n" 14514 "\t\tstd::string alphabet =\n" 14515 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14516 "\t}\n" 14517 "};", 14518 Tab); 14519 Tab.AlignOperands = FormatStyle::OAS_Align; 14520 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 14521 " cccccccccccccccccccc;", 14522 Tab); 14523 // no alignment 14524 verifyFormat("int aaaaaaaaaa =\n" 14525 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 14526 Tab); 14527 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 14528 " : bbbbbbbbbbbbbb ? 222222222222222\n" 14529 " : 333333333333333;", 14530 Tab); 14531 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14532 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 14533 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 14534 " + cccccccccccccccccccc;", 14535 Tab); 14536 } 14537 14538 TEST_F(FormatTest, ZeroTabWidth) { 14539 FormatStyle Tab = getLLVMStyleWithColumns(42); 14540 Tab.IndentWidth = 8; 14541 Tab.UseTab = FormatStyle::UT_Never; 14542 Tab.TabWidth = 0; 14543 EXPECT_EQ("void a(){\n" 14544 " // line starts with '\t'\n" 14545 "};", 14546 format("void a(){\n" 14547 "\t// line starts with '\t'\n" 14548 "};", 14549 Tab)); 14550 14551 EXPECT_EQ("void a(){\n" 14552 " // line starts with '\t'\n" 14553 "};", 14554 format("void a(){\n" 14555 "\t\t// line starts with '\t'\n" 14556 "};", 14557 Tab)); 14558 14559 Tab.UseTab = FormatStyle::UT_ForIndentation; 14560 EXPECT_EQ("void a(){\n" 14561 " // line starts with '\t'\n" 14562 "};", 14563 format("void a(){\n" 14564 "\t// line starts with '\t'\n" 14565 "};", 14566 Tab)); 14567 14568 EXPECT_EQ("void a(){\n" 14569 " // line starts with '\t'\n" 14570 "};", 14571 format("void a(){\n" 14572 "\t\t// line starts with '\t'\n" 14573 "};", 14574 Tab)); 14575 14576 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14577 EXPECT_EQ("void a(){\n" 14578 " // line starts with '\t'\n" 14579 "};", 14580 format("void a(){\n" 14581 "\t// line starts with '\t'\n" 14582 "};", 14583 Tab)); 14584 14585 EXPECT_EQ("void a(){\n" 14586 " // line starts with '\t'\n" 14587 "};", 14588 format("void a(){\n" 14589 "\t\t// line starts with '\t'\n" 14590 "};", 14591 Tab)); 14592 14593 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14594 EXPECT_EQ("void a(){\n" 14595 " // line starts with '\t'\n" 14596 "};", 14597 format("void a(){\n" 14598 "\t// line starts with '\t'\n" 14599 "};", 14600 Tab)); 14601 14602 EXPECT_EQ("void a(){\n" 14603 " // line starts with '\t'\n" 14604 "};", 14605 format("void a(){\n" 14606 "\t\t// line starts with '\t'\n" 14607 "};", 14608 Tab)); 14609 14610 Tab.UseTab = FormatStyle::UT_Always; 14611 EXPECT_EQ("void a(){\n" 14612 "// line starts with '\t'\n" 14613 "};", 14614 format("void a(){\n" 14615 "\t// line starts with '\t'\n" 14616 "};", 14617 Tab)); 14618 14619 EXPECT_EQ("void a(){\n" 14620 "// line starts with '\t'\n" 14621 "};", 14622 format("void a(){\n" 14623 "\t\t// line starts with '\t'\n" 14624 "};", 14625 Tab)); 14626 } 14627 14628 TEST_F(FormatTest, CalculatesOriginalColumn) { 14629 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14630 "q\"; /* some\n" 14631 " comment */", 14632 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14633 "q\"; /* some\n" 14634 " comment */", 14635 getLLVMStyle())); 14636 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14637 "/* some\n" 14638 " comment */", 14639 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14640 " /* some\n" 14641 " comment */", 14642 getLLVMStyle())); 14643 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14644 "qqq\n" 14645 "/* some\n" 14646 " comment */", 14647 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14648 "qqq\n" 14649 " /* some\n" 14650 " comment */", 14651 getLLVMStyle())); 14652 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14653 "wwww; /* some\n" 14654 " comment */", 14655 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14656 "wwww; /* some\n" 14657 " comment */", 14658 getLLVMStyle())); 14659 } 14660 14661 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 14662 FormatStyle NoSpace = getLLVMStyle(); 14663 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 14664 14665 verifyFormat("while(true)\n" 14666 " continue;", 14667 NoSpace); 14668 verifyFormat("for(;;)\n" 14669 " continue;", 14670 NoSpace); 14671 verifyFormat("if(true)\n" 14672 " f();\n" 14673 "else if(true)\n" 14674 " f();", 14675 NoSpace); 14676 verifyFormat("do {\n" 14677 " do_something();\n" 14678 "} while(something());", 14679 NoSpace); 14680 verifyFormat("switch(x) {\n" 14681 "default:\n" 14682 " break;\n" 14683 "}", 14684 NoSpace); 14685 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14686 verifyFormat("size_t x = sizeof(x);", NoSpace); 14687 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14688 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14689 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14690 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14691 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14692 verifyFormat("alignas(128) char a[128];", NoSpace); 14693 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14694 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14695 verifyFormat("int f() throw(Deprecated);", NoSpace); 14696 verifyFormat("typedef void (*cb)(int);", NoSpace); 14697 verifyFormat("T A::operator()();", NoSpace); 14698 verifyFormat("X A::operator++(T);", NoSpace); 14699 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14700 14701 FormatStyle Space = getLLVMStyle(); 14702 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14703 14704 verifyFormat("int f ();", Space); 14705 verifyFormat("void f (int a, T b) {\n" 14706 " while (true)\n" 14707 " continue;\n" 14708 "}", 14709 Space); 14710 verifyFormat("if (true)\n" 14711 " f ();\n" 14712 "else if (true)\n" 14713 " f ();", 14714 Space); 14715 verifyFormat("do {\n" 14716 " do_something ();\n" 14717 "} while (something ());", 14718 Space); 14719 verifyFormat("switch (x) {\n" 14720 "default:\n" 14721 " break;\n" 14722 "}", 14723 Space); 14724 verifyFormat("A::A () : a (1) {}", Space); 14725 verifyFormat("void f () __attribute__ ((asdf));", Space); 14726 verifyFormat("*(&a + 1);\n" 14727 "&((&a)[1]);\n" 14728 "a[(b + c) * d];\n" 14729 "(((a + 1) * 2) + 3) * 4;", 14730 Space); 14731 verifyFormat("#define A(x) x", Space); 14732 verifyFormat("#define A (x) x", Space); 14733 verifyFormat("#if defined(x)\n" 14734 "#endif", 14735 Space); 14736 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14737 verifyFormat("size_t x = sizeof (x);", Space); 14738 verifyFormat("auto f (int x) -> decltype (x);", Space); 14739 verifyFormat("auto f (int x) -> typeof (x);", Space); 14740 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14741 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14742 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14743 verifyFormat("alignas (128) char a[128];", Space); 14744 verifyFormat("size_t x = alignof (MyType);", Space); 14745 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14746 verifyFormat("int f () throw (Deprecated);", Space); 14747 verifyFormat("typedef void (*cb) (int);", Space); 14748 // FIXME these tests regressed behaviour. 14749 // verifyFormat("T A::operator() ();", Space); 14750 // verifyFormat("X A::operator++ (T);", Space); 14751 verifyFormat("auto lambda = [] () { return 0; };", Space); 14752 verifyFormat("int x = int (y);", Space); 14753 14754 FormatStyle SomeSpace = getLLVMStyle(); 14755 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14756 14757 verifyFormat("[]() -> float {}", SomeSpace); 14758 verifyFormat("[] (auto foo) {}", SomeSpace); 14759 verifyFormat("[foo]() -> int {}", SomeSpace); 14760 verifyFormat("int f();", SomeSpace); 14761 verifyFormat("void f (int a, T b) {\n" 14762 " while (true)\n" 14763 " continue;\n" 14764 "}", 14765 SomeSpace); 14766 verifyFormat("if (true)\n" 14767 " f();\n" 14768 "else if (true)\n" 14769 " f();", 14770 SomeSpace); 14771 verifyFormat("do {\n" 14772 " do_something();\n" 14773 "} while (something());", 14774 SomeSpace); 14775 verifyFormat("switch (x) {\n" 14776 "default:\n" 14777 " break;\n" 14778 "}", 14779 SomeSpace); 14780 verifyFormat("A::A() : a (1) {}", SomeSpace); 14781 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14782 verifyFormat("*(&a + 1);\n" 14783 "&((&a)[1]);\n" 14784 "a[(b + c) * d];\n" 14785 "(((a + 1) * 2) + 3) * 4;", 14786 SomeSpace); 14787 verifyFormat("#define A(x) x", SomeSpace); 14788 verifyFormat("#define A (x) x", SomeSpace); 14789 verifyFormat("#if defined(x)\n" 14790 "#endif", 14791 SomeSpace); 14792 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14793 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14794 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14795 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14796 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14797 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14798 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14799 verifyFormat("alignas (128) char a[128];", SomeSpace); 14800 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14801 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14802 SomeSpace); 14803 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14804 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14805 verifyFormat("T A::operator()();", SomeSpace); 14806 // FIXME these tests regressed behaviour. 14807 // verifyFormat("X A::operator++ (T);", SomeSpace); 14808 verifyFormat("int x = int (y);", SomeSpace); 14809 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14810 14811 FormatStyle SpaceControlStatements = getLLVMStyle(); 14812 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14813 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 14814 14815 verifyFormat("while (true)\n" 14816 " continue;", 14817 SpaceControlStatements); 14818 verifyFormat("if (true)\n" 14819 " f();\n" 14820 "else if (true)\n" 14821 " f();", 14822 SpaceControlStatements); 14823 verifyFormat("for (;;) {\n" 14824 " do_something();\n" 14825 "}", 14826 SpaceControlStatements); 14827 verifyFormat("do {\n" 14828 " do_something();\n" 14829 "} while (something());", 14830 SpaceControlStatements); 14831 verifyFormat("switch (x) {\n" 14832 "default:\n" 14833 " break;\n" 14834 "}", 14835 SpaceControlStatements); 14836 14837 FormatStyle SpaceFuncDecl = getLLVMStyle(); 14838 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14839 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 14840 14841 verifyFormat("int f ();", SpaceFuncDecl); 14842 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 14843 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 14844 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 14845 verifyFormat("#define A(x) x", SpaceFuncDecl); 14846 verifyFormat("#define A (x) x", SpaceFuncDecl); 14847 verifyFormat("#if defined(x)\n" 14848 "#endif", 14849 SpaceFuncDecl); 14850 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 14851 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 14852 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 14853 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 14854 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 14855 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 14856 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 14857 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 14858 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 14859 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14860 SpaceFuncDecl); 14861 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 14862 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 14863 // FIXME these tests regressed behaviour. 14864 // verifyFormat("T A::operator() ();", SpaceFuncDecl); 14865 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 14866 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 14867 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 14868 verifyFormat("int x = int(y);", SpaceFuncDecl); 14869 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14870 SpaceFuncDecl); 14871 14872 FormatStyle SpaceFuncDef = getLLVMStyle(); 14873 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14874 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 14875 14876 verifyFormat("int f();", SpaceFuncDef); 14877 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 14878 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 14879 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 14880 verifyFormat("#define A(x) x", SpaceFuncDef); 14881 verifyFormat("#define A (x) x", SpaceFuncDef); 14882 verifyFormat("#if defined(x)\n" 14883 "#endif", 14884 SpaceFuncDef); 14885 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 14886 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 14887 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 14888 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 14889 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 14890 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 14891 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 14892 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 14893 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 14894 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14895 SpaceFuncDef); 14896 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 14897 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 14898 verifyFormat("T A::operator()();", SpaceFuncDef); 14899 verifyFormat("X A::operator++(T);", SpaceFuncDef); 14900 // verifyFormat("T A::operator() () {}", SpaceFuncDef); 14901 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 14902 verifyFormat("int x = int(y);", SpaceFuncDef); 14903 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14904 SpaceFuncDef); 14905 14906 FormatStyle SpaceIfMacros = getLLVMStyle(); 14907 SpaceIfMacros.IfMacros.clear(); 14908 SpaceIfMacros.IfMacros.push_back("MYIF"); 14909 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14910 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 14911 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 14912 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 14913 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 14914 14915 FormatStyle SpaceForeachMacros = getLLVMStyle(); 14916 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine, 14917 FormatStyle::SBS_Never); 14918 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false); 14919 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14920 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 14921 verifyFormat("for (;;) {\n" 14922 "}", 14923 SpaceForeachMacros); 14924 verifyFormat("foreach (Item *item, itemlist) {\n" 14925 "}", 14926 SpaceForeachMacros); 14927 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n" 14928 "}", 14929 SpaceForeachMacros); 14930 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n" 14931 "}", 14932 SpaceForeachMacros); 14933 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 14934 14935 FormatStyle SomeSpace2 = getLLVMStyle(); 14936 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14937 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 14938 verifyFormat("[]() -> float {}", SomeSpace2); 14939 verifyFormat("[] (auto foo) {}", SomeSpace2); 14940 verifyFormat("[foo]() -> int {}", SomeSpace2); 14941 verifyFormat("int f();", SomeSpace2); 14942 verifyFormat("void f (int a, T b) {\n" 14943 " while (true)\n" 14944 " continue;\n" 14945 "}", 14946 SomeSpace2); 14947 verifyFormat("if (true)\n" 14948 " f();\n" 14949 "else if (true)\n" 14950 " f();", 14951 SomeSpace2); 14952 verifyFormat("do {\n" 14953 " do_something();\n" 14954 "} while (something());", 14955 SomeSpace2); 14956 verifyFormat("switch (x) {\n" 14957 "default:\n" 14958 " break;\n" 14959 "}", 14960 SomeSpace2); 14961 verifyFormat("A::A() : a (1) {}", SomeSpace2); 14962 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 14963 verifyFormat("*(&a + 1);\n" 14964 "&((&a)[1]);\n" 14965 "a[(b + c) * d];\n" 14966 "(((a + 1) * 2) + 3) * 4;", 14967 SomeSpace2); 14968 verifyFormat("#define A(x) x", SomeSpace2); 14969 verifyFormat("#define A (x) x", SomeSpace2); 14970 verifyFormat("#if defined(x)\n" 14971 "#endif", 14972 SomeSpace2); 14973 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 14974 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 14975 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 14976 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 14977 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 14978 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 14979 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 14980 verifyFormat("alignas (128) char a[128];", SomeSpace2); 14981 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 14982 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14983 SomeSpace2); 14984 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 14985 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 14986 verifyFormat("T A::operator()();", SomeSpace2); 14987 // verifyFormat("X A::operator++ (T);", SomeSpace2); 14988 verifyFormat("int x = int (y);", SomeSpace2); 14989 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 14990 14991 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle(); 14992 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14993 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 14994 .AfterOverloadedOperator = true; 14995 14996 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator); 14997 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator); 14998 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator); 14999 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 15000 15001 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 15002 .AfterOverloadedOperator = false; 15003 15004 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator); 15005 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator); 15006 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator); 15007 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 15008 } 15009 15010 TEST_F(FormatTest, SpaceAfterLogicalNot) { 15011 FormatStyle Spaces = getLLVMStyle(); 15012 Spaces.SpaceAfterLogicalNot = true; 15013 15014 verifyFormat("bool x = ! y", Spaces); 15015 verifyFormat("if (! isFailure())", Spaces); 15016 verifyFormat("if (! (a && b))", Spaces); 15017 verifyFormat("\"Error!\"", Spaces); 15018 verifyFormat("! ! x", Spaces); 15019 } 15020 15021 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 15022 FormatStyle Spaces = getLLVMStyle(); 15023 15024 Spaces.SpacesInParentheses = true; 15025 verifyFormat("do_something( ::globalVar );", Spaces); 15026 verifyFormat("call( x, y, z );", Spaces); 15027 verifyFormat("call();", Spaces); 15028 verifyFormat("std::function<void( int, int )> callback;", Spaces); 15029 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 15030 Spaces); 15031 verifyFormat("while ( (bool)1 )\n" 15032 " continue;", 15033 Spaces); 15034 verifyFormat("for ( ;; )\n" 15035 " continue;", 15036 Spaces); 15037 verifyFormat("if ( true )\n" 15038 " f();\n" 15039 "else if ( true )\n" 15040 " f();", 15041 Spaces); 15042 verifyFormat("do {\n" 15043 " do_something( (int)i );\n" 15044 "} while ( something() );", 15045 Spaces); 15046 verifyFormat("switch ( x ) {\n" 15047 "default:\n" 15048 " break;\n" 15049 "}", 15050 Spaces); 15051 15052 Spaces.SpacesInParentheses = false; 15053 Spaces.SpacesInCStyleCastParentheses = true; 15054 verifyFormat("Type *A = ( Type * )P;", Spaces); 15055 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 15056 verifyFormat("x = ( int32 )y;", Spaces); 15057 verifyFormat("int a = ( int )(2.0f);", Spaces); 15058 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 15059 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 15060 verifyFormat("#define x (( int )-1)", Spaces); 15061 15062 // Run the first set of tests again with: 15063 Spaces.SpacesInParentheses = false; 15064 Spaces.SpaceInEmptyParentheses = true; 15065 Spaces.SpacesInCStyleCastParentheses = true; 15066 verifyFormat("call(x, y, z);", Spaces); 15067 verifyFormat("call( );", Spaces); 15068 verifyFormat("std::function<void(int, int)> callback;", Spaces); 15069 verifyFormat("while (( bool )1)\n" 15070 " continue;", 15071 Spaces); 15072 verifyFormat("for (;;)\n" 15073 " continue;", 15074 Spaces); 15075 verifyFormat("if (true)\n" 15076 " f( );\n" 15077 "else if (true)\n" 15078 " f( );", 15079 Spaces); 15080 verifyFormat("do {\n" 15081 " do_something(( int )i);\n" 15082 "} while (something( ));", 15083 Spaces); 15084 verifyFormat("switch (x) {\n" 15085 "default:\n" 15086 " break;\n" 15087 "}", 15088 Spaces); 15089 15090 // Run the first set of tests again with: 15091 Spaces.SpaceAfterCStyleCast = true; 15092 verifyFormat("call(x, y, z);", Spaces); 15093 verifyFormat("call( );", Spaces); 15094 verifyFormat("std::function<void(int, int)> callback;", Spaces); 15095 verifyFormat("while (( bool ) 1)\n" 15096 " continue;", 15097 Spaces); 15098 verifyFormat("for (;;)\n" 15099 " continue;", 15100 Spaces); 15101 verifyFormat("if (true)\n" 15102 " f( );\n" 15103 "else if (true)\n" 15104 " f( );", 15105 Spaces); 15106 verifyFormat("do {\n" 15107 " do_something(( int ) i);\n" 15108 "} while (something( ));", 15109 Spaces); 15110 verifyFormat("switch (x) {\n" 15111 "default:\n" 15112 " break;\n" 15113 "}", 15114 Spaces); 15115 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces); 15116 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces); 15117 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces); 15118 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); 15119 verifyFormat("bool *y = ( bool * ) (x);", Spaces); 15120 15121 // Run subset of tests again with: 15122 Spaces.SpacesInCStyleCastParentheses = false; 15123 Spaces.SpaceAfterCStyleCast = true; 15124 verifyFormat("while ((bool) 1)\n" 15125 " continue;", 15126 Spaces); 15127 verifyFormat("do {\n" 15128 " do_something((int) i);\n" 15129 "} while (something( ));", 15130 Spaces); 15131 15132 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 15133 verifyFormat("size_t idx = (size_t) a;", Spaces); 15134 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 15135 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 15136 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 15137 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 15138 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 15139 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces); 15140 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces); 15141 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces); 15142 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); 15143 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); 15144 Spaces.ColumnLimit = 80; 15145 Spaces.IndentWidth = 4; 15146 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 15147 verifyFormat("void foo( ) {\n" 15148 " size_t foo = (*(function))(\n" 15149 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 15150 "BarrrrrrrrrrrrLong,\n" 15151 " FoooooooooLooooong);\n" 15152 "}", 15153 Spaces); 15154 Spaces.SpaceAfterCStyleCast = false; 15155 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 15156 verifyFormat("size_t idx = (size_t)a;", Spaces); 15157 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 15158 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 15159 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 15160 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 15161 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 15162 15163 verifyFormat("void foo( ) {\n" 15164 " size_t foo = (*(function))(\n" 15165 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 15166 "BarrrrrrrrrrrrLong,\n" 15167 " FoooooooooLooooong);\n" 15168 "}", 15169 Spaces); 15170 } 15171 15172 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 15173 verifyFormat("int a[5];"); 15174 verifyFormat("a[3] += 42;"); 15175 15176 FormatStyle Spaces = getLLVMStyle(); 15177 Spaces.SpacesInSquareBrackets = true; 15178 // Not lambdas. 15179 verifyFormat("int a[ 5 ];", Spaces); 15180 verifyFormat("a[ 3 ] += 42;", Spaces); 15181 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 15182 verifyFormat("double &operator[](int i) { return 0; }\n" 15183 "int i;", 15184 Spaces); 15185 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 15186 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 15187 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 15188 // Lambdas. 15189 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 15190 verifyFormat("return [ i, args... ] {};", Spaces); 15191 verifyFormat("int foo = [ &bar ]() {};", Spaces); 15192 verifyFormat("int foo = [ = ]() {};", Spaces); 15193 verifyFormat("int foo = [ & ]() {};", Spaces); 15194 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 15195 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 15196 } 15197 15198 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 15199 FormatStyle NoSpaceStyle = getLLVMStyle(); 15200 verifyFormat("int a[5];", NoSpaceStyle); 15201 verifyFormat("a[3] += 42;", NoSpaceStyle); 15202 15203 verifyFormat("int a[1];", NoSpaceStyle); 15204 verifyFormat("int 1 [a];", NoSpaceStyle); 15205 verifyFormat("int a[1][2];", NoSpaceStyle); 15206 verifyFormat("a[7] = 5;", NoSpaceStyle); 15207 verifyFormat("int a = (f())[23];", NoSpaceStyle); 15208 verifyFormat("f([] {})", NoSpaceStyle); 15209 15210 FormatStyle Space = getLLVMStyle(); 15211 Space.SpaceBeforeSquareBrackets = true; 15212 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 15213 verifyFormat("return [i, args...] {};", Space); 15214 15215 verifyFormat("int a [5];", Space); 15216 verifyFormat("a [3] += 42;", Space); 15217 verifyFormat("constexpr char hello []{\"hello\"};", Space); 15218 verifyFormat("double &operator[](int i) { return 0; }\n" 15219 "int i;", 15220 Space); 15221 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 15222 verifyFormat("int i = a [a][a]->f();", Space); 15223 verifyFormat("int i = (*b) [a]->f();", Space); 15224 15225 verifyFormat("int a [1];", Space); 15226 verifyFormat("int 1 [a];", Space); 15227 verifyFormat("int a [1][2];", Space); 15228 verifyFormat("a [7] = 5;", Space); 15229 verifyFormat("int a = (f()) [23];", Space); 15230 verifyFormat("f([] {})", Space); 15231 } 15232 15233 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 15234 verifyFormat("int a = 5;"); 15235 verifyFormat("a += 42;"); 15236 verifyFormat("a or_eq 8;"); 15237 15238 FormatStyle Spaces = getLLVMStyle(); 15239 Spaces.SpaceBeforeAssignmentOperators = false; 15240 verifyFormat("int a= 5;", Spaces); 15241 verifyFormat("a+= 42;", Spaces); 15242 verifyFormat("a or_eq 8;", Spaces); 15243 } 15244 15245 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 15246 verifyFormat("class Foo : public Bar {};"); 15247 verifyFormat("Foo::Foo() : foo(1) {}"); 15248 verifyFormat("for (auto a : b) {\n}"); 15249 verifyFormat("int x = a ? b : c;"); 15250 verifyFormat("{\n" 15251 "label0:\n" 15252 " int x = 0;\n" 15253 "}"); 15254 verifyFormat("switch (x) {\n" 15255 "case 1:\n" 15256 "default:\n" 15257 "}"); 15258 verifyFormat("switch (allBraces) {\n" 15259 "case 1: {\n" 15260 " break;\n" 15261 "}\n" 15262 "case 2: {\n" 15263 " [[fallthrough]];\n" 15264 "}\n" 15265 "default: {\n" 15266 " break;\n" 15267 "}\n" 15268 "}"); 15269 15270 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 15271 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 15272 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 15273 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 15274 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 15275 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 15276 verifyFormat("{\n" 15277 "label1:\n" 15278 " int x = 0;\n" 15279 "}", 15280 CtorInitializerStyle); 15281 verifyFormat("switch (x) {\n" 15282 "case 1:\n" 15283 "default:\n" 15284 "}", 15285 CtorInitializerStyle); 15286 verifyFormat("switch (allBraces) {\n" 15287 "case 1: {\n" 15288 " break;\n" 15289 "}\n" 15290 "case 2: {\n" 15291 " [[fallthrough]];\n" 15292 "}\n" 15293 "default: {\n" 15294 " break;\n" 15295 "}\n" 15296 "}", 15297 CtorInitializerStyle); 15298 CtorInitializerStyle.BreakConstructorInitializers = 15299 FormatStyle::BCIS_AfterColon; 15300 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 15301 " aaaaaaaaaaaaaaaa(1),\n" 15302 " bbbbbbbbbbbbbbbb(2) {}", 15303 CtorInitializerStyle); 15304 CtorInitializerStyle.BreakConstructorInitializers = 15305 FormatStyle::BCIS_BeforeComma; 15306 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15307 " : aaaaaaaaaaaaaaaa(1)\n" 15308 " , bbbbbbbbbbbbbbbb(2) {}", 15309 CtorInitializerStyle); 15310 CtorInitializerStyle.BreakConstructorInitializers = 15311 FormatStyle::BCIS_BeforeColon; 15312 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15313 " : aaaaaaaaaaaaaaaa(1),\n" 15314 " bbbbbbbbbbbbbbbb(2) {}", 15315 CtorInitializerStyle); 15316 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 15317 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15318 ": aaaaaaaaaaaaaaaa(1),\n" 15319 " bbbbbbbbbbbbbbbb(2) {}", 15320 CtorInitializerStyle); 15321 15322 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 15323 InheritanceStyle.SpaceBeforeInheritanceColon = false; 15324 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 15325 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 15326 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 15327 verifyFormat("int x = a ? b : c;", InheritanceStyle); 15328 verifyFormat("{\n" 15329 "label2:\n" 15330 " int x = 0;\n" 15331 "}", 15332 InheritanceStyle); 15333 verifyFormat("switch (x) {\n" 15334 "case 1:\n" 15335 "default:\n" 15336 "}", 15337 InheritanceStyle); 15338 verifyFormat("switch (allBraces) {\n" 15339 "case 1: {\n" 15340 " break;\n" 15341 "}\n" 15342 "case 2: {\n" 15343 " [[fallthrough]];\n" 15344 "}\n" 15345 "default: {\n" 15346 " break;\n" 15347 "}\n" 15348 "}", 15349 InheritanceStyle); 15350 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 15351 verifyFormat("class Foooooooooooooooooooooo\n" 15352 " : public aaaaaaaaaaaaaaaaaa,\n" 15353 " public bbbbbbbbbbbbbbbbbb {\n" 15354 "}", 15355 InheritanceStyle); 15356 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 15357 verifyFormat("class Foooooooooooooooooooooo:\n" 15358 " public aaaaaaaaaaaaaaaaaa,\n" 15359 " public bbbbbbbbbbbbbbbbbb {\n" 15360 "}", 15361 InheritanceStyle); 15362 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 15363 verifyFormat("class Foooooooooooooooooooooo\n" 15364 " : public aaaaaaaaaaaaaaaaaa\n" 15365 " , public bbbbbbbbbbbbbbbbbb {\n" 15366 "}", 15367 InheritanceStyle); 15368 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 15369 verifyFormat("class Foooooooooooooooooooooo\n" 15370 " : public aaaaaaaaaaaaaaaaaa,\n" 15371 " public bbbbbbbbbbbbbbbbbb {\n" 15372 "}", 15373 InheritanceStyle); 15374 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 15375 verifyFormat("class Foooooooooooooooooooooo\n" 15376 ": public aaaaaaaaaaaaaaaaaa,\n" 15377 " public bbbbbbbbbbbbbbbbbb {}", 15378 InheritanceStyle); 15379 15380 FormatStyle ForLoopStyle = getLLVMStyle(); 15381 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 15382 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 15383 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 15384 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 15385 verifyFormat("int x = a ? b : c;", ForLoopStyle); 15386 verifyFormat("{\n" 15387 "label2:\n" 15388 " int x = 0;\n" 15389 "}", 15390 ForLoopStyle); 15391 verifyFormat("switch (x) {\n" 15392 "case 1:\n" 15393 "default:\n" 15394 "}", 15395 ForLoopStyle); 15396 verifyFormat("switch (allBraces) {\n" 15397 "case 1: {\n" 15398 " break;\n" 15399 "}\n" 15400 "case 2: {\n" 15401 " [[fallthrough]];\n" 15402 "}\n" 15403 "default: {\n" 15404 " break;\n" 15405 "}\n" 15406 "}", 15407 ForLoopStyle); 15408 15409 FormatStyle CaseStyle = getLLVMStyle(); 15410 CaseStyle.SpaceBeforeCaseColon = true; 15411 verifyFormat("class Foo : public Bar {};", CaseStyle); 15412 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 15413 verifyFormat("for (auto a : b) {\n}", CaseStyle); 15414 verifyFormat("int x = a ? b : c;", CaseStyle); 15415 verifyFormat("switch (x) {\n" 15416 "case 1 :\n" 15417 "default :\n" 15418 "}", 15419 CaseStyle); 15420 verifyFormat("switch (allBraces) {\n" 15421 "case 1 : {\n" 15422 " break;\n" 15423 "}\n" 15424 "case 2 : {\n" 15425 " [[fallthrough]];\n" 15426 "}\n" 15427 "default : {\n" 15428 " break;\n" 15429 "}\n" 15430 "}", 15431 CaseStyle); 15432 15433 FormatStyle NoSpaceStyle = getLLVMStyle(); 15434 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 15435 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15436 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 15437 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15438 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 15439 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 15440 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 15441 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 15442 verifyFormat("{\n" 15443 "label3:\n" 15444 " int x = 0;\n" 15445 "}", 15446 NoSpaceStyle); 15447 verifyFormat("switch (x) {\n" 15448 "case 1:\n" 15449 "default:\n" 15450 "}", 15451 NoSpaceStyle); 15452 verifyFormat("switch (allBraces) {\n" 15453 "case 1: {\n" 15454 " break;\n" 15455 "}\n" 15456 "case 2: {\n" 15457 " [[fallthrough]];\n" 15458 "}\n" 15459 "default: {\n" 15460 " break;\n" 15461 "}\n" 15462 "}", 15463 NoSpaceStyle); 15464 15465 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 15466 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 15467 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15468 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 15469 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15470 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 15471 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 15472 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 15473 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 15474 verifyFormat("{\n" 15475 "label3:\n" 15476 " int x = 0;\n" 15477 "}", 15478 InvertedSpaceStyle); 15479 verifyFormat("switch (x) {\n" 15480 "case 1 :\n" 15481 "case 2 : {\n" 15482 " break;\n" 15483 "}\n" 15484 "default :\n" 15485 " break;\n" 15486 "}", 15487 InvertedSpaceStyle); 15488 verifyFormat("switch (allBraces) {\n" 15489 "case 1 : {\n" 15490 " break;\n" 15491 "}\n" 15492 "case 2 : {\n" 15493 " [[fallthrough]];\n" 15494 "}\n" 15495 "default : {\n" 15496 " break;\n" 15497 "}\n" 15498 "}", 15499 InvertedSpaceStyle); 15500 } 15501 15502 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 15503 FormatStyle Style = getLLVMStyle(); 15504 15505 Style.PointerAlignment = FormatStyle::PAS_Left; 15506 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15507 verifyFormat("void* const* x = NULL;", Style); 15508 15509 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 15510 do { \ 15511 Style.PointerAlignment = FormatStyle::Pointers; \ 15512 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 15513 verifyFormat(Code, Style); \ 15514 } while (false) 15515 15516 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 15517 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 15518 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 15519 15520 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 15521 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 15522 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 15523 15524 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 15525 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 15526 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 15527 15528 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 15529 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 15530 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 15531 15532 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 15533 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15534 SAPQ_Default); 15535 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15536 SAPQ_Default); 15537 15538 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 15539 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15540 SAPQ_Before); 15541 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15542 SAPQ_Before); 15543 15544 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 15545 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 15546 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15547 SAPQ_After); 15548 15549 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 15550 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 15551 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 15552 15553 #undef verifyQualifierSpaces 15554 15555 FormatStyle Spaces = getLLVMStyle(); 15556 Spaces.AttributeMacros.push_back("qualified"); 15557 Spaces.PointerAlignment = FormatStyle::PAS_Right; 15558 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15559 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 15560 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 15561 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 15562 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 15563 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15564 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15565 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 15566 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 15567 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15568 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15569 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15570 15571 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 15572 Spaces.PointerAlignment = FormatStyle::PAS_Left; 15573 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15574 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 15575 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 15576 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 15577 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 15578 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15579 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 15580 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15581 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 15582 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 15583 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 15584 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 15585 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15586 15587 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 15588 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 15589 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15590 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 15591 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 15592 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15593 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15594 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15595 } 15596 15597 TEST_F(FormatTest, AlignConsecutiveMacros) { 15598 FormatStyle Style = getLLVMStyle(); 15599 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15600 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15601 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15602 15603 verifyFormat("#define a 3\n" 15604 "#define bbbb 4\n" 15605 "#define ccc (5)", 15606 Style); 15607 15608 verifyFormat("#define f(x) (x * x)\n" 15609 "#define fff(x, y, z) (x * y + z)\n" 15610 "#define ffff(x, y) (x - y)", 15611 Style); 15612 15613 verifyFormat("#define foo(x, y) (x + y)\n" 15614 "#define bar (5, 6)(2 + 2)", 15615 Style); 15616 15617 verifyFormat("#define a 3\n" 15618 "#define bbbb 4\n" 15619 "#define ccc (5)\n" 15620 "#define f(x) (x * x)\n" 15621 "#define fff(x, y, z) (x * y + z)\n" 15622 "#define ffff(x, y) (x - y)", 15623 Style); 15624 15625 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15626 verifyFormat("#define a 3\n" 15627 "#define bbbb 4\n" 15628 "#define ccc (5)", 15629 Style); 15630 15631 verifyFormat("#define f(x) (x * x)\n" 15632 "#define fff(x, y, z) (x * y + z)\n" 15633 "#define ffff(x, y) (x - y)", 15634 Style); 15635 15636 verifyFormat("#define foo(x, y) (x + y)\n" 15637 "#define bar (5, 6)(2 + 2)", 15638 Style); 15639 15640 verifyFormat("#define a 3\n" 15641 "#define bbbb 4\n" 15642 "#define ccc (5)\n" 15643 "#define f(x) (x * x)\n" 15644 "#define fff(x, y, z) (x * y + z)\n" 15645 "#define ffff(x, y) (x - y)", 15646 Style); 15647 15648 verifyFormat("#define a 5\n" 15649 "#define foo(x, y) (x + y)\n" 15650 "#define CCC (6)\n" 15651 "auto lambda = []() {\n" 15652 " auto ii = 0;\n" 15653 " float j = 0;\n" 15654 " return 0;\n" 15655 "};\n" 15656 "int i = 0;\n" 15657 "float i2 = 0;\n" 15658 "auto v = type{\n" 15659 " i = 1, //\n" 15660 " (i = 2), //\n" 15661 " i = 3 //\n" 15662 "};", 15663 Style); 15664 15665 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15666 Style.ColumnLimit = 20; 15667 15668 verifyFormat("#define a \\\n" 15669 " \"aabbbbbbbbbbbb\"\n" 15670 "#define D \\\n" 15671 " \"aabbbbbbbbbbbb\" \\\n" 15672 " \"ccddeeeeeeeee\"\n" 15673 "#define B \\\n" 15674 " \"QQQQQQQQQQQQQ\" \\\n" 15675 " \"FFFFFFFFFFFFF\" \\\n" 15676 " \"LLLLLLLL\"\n", 15677 Style); 15678 15679 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15680 verifyFormat("#define a \\\n" 15681 " \"aabbbbbbbbbbbb\"\n" 15682 "#define D \\\n" 15683 " \"aabbbbbbbbbbbb\" \\\n" 15684 " \"ccddeeeeeeeee\"\n" 15685 "#define B \\\n" 15686 " \"QQQQQQQQQQQQQ\" \\\n" 15687 " \"FFFFFFFFFFFFF\" \\\n" 15688 " \"LLLLLLLL\"\n", 15689 Style); 15690 15691 // Test across comments 15692 Style.MaxEmptyLinesToKeep = 10; 15693 Style.ReflowComments = false; 15694 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 15695 EXPECT_EQ("#define a 3\n" 15696 "// line comment\n" 15697 "#define bbbb 4\n" 15698 "#define ccc (5)", 15699 format("#define a 3\n" 15700 "// line comment\n" 15701 "#define bbbb 4\n" 15702 "#define ccc (5)", 15703 Style)); 15704 15705 EXPECT_EQ("#define a 3\n" 15706 "/* block comment */\n" 15707 "#define bbbb 4\n" 15708 "#define ccc (5)", 15709 format("#define a 3\n" 15710 "/* block comment */\n" 15711 "#define bbbb 4\n" 15712 "#define ccc (5)", 15713 Style)); 15714 15715 EXPECT_EQ("#define a 3\n" 15716 "/* multi-line *\n" 15717 " * block comment */\n" 15718 "#define bbbb 4\n" 15719 "#define ccc (5)", 15720 format("#define a 3\n" 15721 "/* multi-line *\n" 15722 " * block comment */\n" 15723 "#define bbbb 4\n" 15724 "#define ccc (5)", 15725 Style)); 15726 15727 EXPECT_EQ("#define a 3\n" 15728 "// multi-line line comment\n" 15729 "//\n" 15730 "#define bbbb 4\n" 15731 "#define ccc (5)", 15732 format("#define a 3\n" 15733 "// multi-line line comment\n" 15734 "//\n" 15735 "#define bbbb 4\n" 15736 "#define ccc (5)", 15737 Style)); 15738 15739 EXPECT_EQ("#define a 3\n" 15740 "// empty lines still break.\n" 15741 "\n" 15742 "#define bbbb 4\n" 15743 "#define ccc (5)", 15744 format("#define a 3\n" 15745 "// empty lines still break.\n" 15746 "\n" 15747 "#define bbbb 4\n" 15748 "#define ccc (5)", 15749 Style)); 15750 15751 // Test across empty lines 15752 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 15753 EXPECT_EQ("#define a 3\n" 15754 "\n" 15755 "#define bbbb 4\n" 15756 "#define ccc (5)", 15757 format("#define a 3\n" 15758 "\n" 15759 "#define bbbb 4\n" 15760 "#define ccc (5)", 15761 Style)); 15762 15763 EXPECT_EQ("#define a 3\n" 15764 "\n" 15765 "\n" 15766 "\n" 15767 "#define bbbb 4\n" 15768 "#define ccc (5)", 15769 format("#define a 3\n" 15770 "\n" 15771 "\n" 15772 "\n" 15773 "#define bbbb 4\n" 15774 "#define ccc (5)", 15775 Style)); 15776 15777 EXPECT_EQ("#define a 3\n" 15778 "// comments should break alignment\n" 15779 "//\n" 15780 "#define bbbb 4\n" 15781 "#define ccc (5)", 15782 format("#define a 3\n" 15783 "// comments should break alignment\n" 15784 "//\n" 15785 "#define bbbb 4\n" 15786 "#define ccc (5)", 15787 Style)); 15788 15789 // Test across empty lines and comments 15790 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 15791 verifyFormat("#define a 3\n" 15792 "\n" 15793 "// line comment\n" 15794 "#define bbbb 4\n" 15795 "#define ccc (5)", 15796 Style); 15797 15798 EXPECT_EQ("#define a 3\n" 15799 "\n" 15800 "\n" 15801 "/* multi-line *\n" 15802 " * block comment */\n" 15803 "\n" 15804 "\n" 15805 "#define bbbb 4\n" 15806 "#define ccc (5)", 15807 format("#define a 3\n" 15808 "\n" 15809 "\n" 15810 "/* multi-line *\n" 15811 " * block comment */\n" 15812 "\n" 15813 "\n" 15814 "#define bbbb 4\n" 15815 "#define ccc (5)", 15816 Style)); 15817 15818 EXPECT_EQ("#define a 3\n" 15819 "\n" 15820 "\n" 15821 "/* multi-line *\n" 15822 " * block comment */\n" 15823 "\n" 15824 "\n" 15825 "#define bbbb 4\n" 15826 "#define ccc (5)", 15827 format("#define a 3\n" 15828 "\n" 15829 "\n" 15830 "/* multi-line *\n" 15831 " * block comment */\n" 15832 "\n" 15833 "\n" 15834 "#define bbbb 4\n" 15835 "#define ccc (5)", 15836 Style)); 15837 } 15838 15839 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 15840 FormatStyle Alignment = getLLVMStyle(); 15841 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15842 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 15843 15844 Alignment.MaxEmptyLinesToKeep = 10; 15845 /* Test alignment across empty lines */ 15846 EXPECT_EQ("int a = 5;\n" 15847 "\n" 15848 "int oneTwoThree = 123;", 15849 format("int a = 5;\n" 15850 "\n" 15851 "int oneTwoThree= 123;", 15852 Alignment)); 15853 EXPECT_EQ("int a = 5;\n" 15854 "int one = 1;\n" 15855 "\n" 15856 "int oneTwoThree = 123;", 15857 format("int a = 5;\n" 15858 "int one = 1;\n" 15859 "\n" 15860 "int oneTwoThree = 123;", 15861 Alignment)); 15862 EXPECT_EQ("int a = 5;\n" 15863 "int one = 1;\n" 15864 "\n" 15865 "int oneTwoThree = 123;\n" 15866 "int oneTwo = 12;", 15867 format("int a = 5;\n" 15868 "int one = 1;\n" 15869 "\n" 15870 "int oneTwoThree = 123;\n" 15871 "int oneTwo = 12;", 15872 Alignment)); 15873 15874 /* Test across comments */ 15875 EXPECT_EQ("int a = 5;\n" 15876 "/* block comment */\n" 15877 "int oneTwoThree = 123;", 15878 format("int a = 5;\n" 15879 "/* block comment */\n" 15880 "int oneTwoThree=123;", 15881 Alignment)); 15882 15883 EXPECT_EQ("int a = 5;\n" 15884 "// line comment\n" 15885 "int oneTwoThree = 123;", 15886 format("int a = 5;\n" 15887 "// line comment\n" 15888 "int oneTwoThree=123;", 15889 Alignment)); 15890 15891 /* Test across comments and newlines */ 15892 EXPECT_EQ("int a = 5;\n" 15893 "\n" 15894 "/* block comment */\n" 15895 "int oneTwoThree = 123;", 15896 format("int a = 5;\n" 15897 "\n" 15898 "/* block comment */\n" 15899 "int oneTwoThree=123;", 15900 Alignment)); 15901 15902 EXPECT_EQ("int a = 5;\n" 15903 "\n" 15904 "// line comment\n" 15905 "int oneTwoThree = 123;", 15906 format("int a = 5;\n" 15907 "\n" 15908 "// line comment\n" 15909 "int oneTwoThree=123;", 15910 Alignment)); 15911 } 15912 15913 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15914 FormatStyle Alignment = getLLVMStyle(); 15915 Alignment.AlignConsecutiveDeclarations = 15916 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15917 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15918 15919 Alignment.MaxEmptyLinesToKeep = 10; 15920 /* Test alignment across empty lines */ 15921 EXPECT_EQ("int a = 5;\n" 15922 "\n" 15923 "float const oneTwoThree = 123;", 15924 format("int a = 5;\n" 15925 "\n" 15926 "float const oneTwoThree = 123;", 15927 Alignment)); 15928 EXPECT_EQ("int a = 5;\n" 15929 "float const one = 1;\n" 15930 "\n" 15931 "int oneTwoThree = 123;", 15932 format("int a = 5;\n" 15933 "float const one = 1;\n" 15934 "\n" 15935 "int oneTwoThree = 123;", 15936 Alignment)); 15937 15938 /* Test across comments */ 15939 EXPECT_EQ("float const a = 5;\n" 15940 "/* block comment */\n" 15941 "int oneTwoThree = 123;", 15942 format("float const a = 5;\n" 15943 "/* block comment */\n" 15944 "int oneTwoThree=123;", 15945 Alignment)); 15946 15947 EXPECT_EQ("float const a = 5;\n" 15948 "// line comment\n" 15949 "int oneTwoThree = 123;", 15950 format("float const a = 5;\n" 15951 "// line comment\n" 15952 "int oneTwoThree=123;", 15953 Alignment)); 15954 15955 /* Test across comments and newlines */ 15956 EXPECT_EQ("float const a = 5;\n" 15957 "\n" 15958 "/* block comment */\n" 15959 "int oneTwoThree = 123;", 15960 format("float const a = 5;\n" 15961 "\n" 15962 "/* block comment */\n" 15963 "int oneTwoThree=123;", 15964 Alignment)); 15965 15966 EXPECT_EQ("float const a = 5;\n" 15967 "\n" 15968 "// line comment\n" 15969 "int oneTwoThree = 123;", 15970 format("float const a = 5;\n" 15971 "\n" 15972 "// line comment\n" 15973 "int oneTwoThree=123;", 15974 Alignment)); 15975 } 15976 15977 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15978 FormatStyle Alignment = getLLVMStyle(); 15979 Alignment.AlignConsecutiveBitFields = 15980 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15981 15982 Alignment.MaxEmptyLinesToKeep = 10; 15983 /* Test alignment across empty lines */ 15984 EXPECT_EQ("int a : 5;\n" 15985 "\n" 15986 "int longbitfield : 6;", 15987 format("int a : 5;\n" 15988 "\n" 15989 "int longbitfield : 6;", 15990 Alignment)); 15991 EXPECT_EQ("int a : 5;\n" 15992 "int one : 1;\n" 15993 "\n" 15994 "int longbitfield : 6;", 15995 format("int a : 5;\n" 15996 "int one : 1;\n" 15997 "\n" 15998 "int longbitfield : 6;", 15999 Alignment)); 16000 16001 /* Test across comments */ 16002 EXPECT_EQ("int a : 5;\n" 16003 "/* block comment */\n" 16004 "int longbitfield : 6;", 16005 format("int a : 5;\n" 16006 "/* block comment */\n" 16007 "int longbitfield : 6;", 16008 Alignment)); 16009 EXPECT_EQ("int a : 5;\n" 16010 "int one : 1;\n" 16011 "// line comment\n" 16012 "int longbitfield : 6;", 16013 format("int a : 5;\n" 16014 "int one : 1;\n" 16015 "// line comment\n" 16016 "int longbitfield : 6;", 16017 Alignment)); 16018 16019 /* Test across comments and newlines */ 16020 EXPECT_EQ("int a : 5;\n" 16021 "/* block comment */\n" 16022 "\n" 16023 "int longbitfield : 6;", 16024 format("int a : 5;\n" 16025 "/* block comment */\n" 16026 "\n" 16027 "int longbitfield : 6;", 16028 Alignment)); 16029 EXPECT_EQ("int a : 5;\n" 16030 "int one : 1;\n" 16031 "\n" 16032 "// line comment\n" 16033 "\n" 16034 "int longbitfield : 6;", 16035 format("int a : 5;\n" 16036 "int one : 1;\n" 16037 "\n" 16038 "// line comment \n" 16039 "\n" 16040 "int longbitfield : 6;", 16041 Alignment)); 16042 } 16043 16044 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 16045 FormatStyle Alignment = getLLVMStyle(); 16046 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16047 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 16048 16049 Alignment.MaxEmptyLinesToKeep = 10; 16050 /* Test alignment across empty lines */ 16051 EXPECT_EQ("int a = 5;\n" 16052 "\n" 16053 "int oneTwoThree = 123;", 16054 format("int a = 5;\n" 16055 "\n" 16056 "int oneTwoThree= 123;", 16057 Alignment)); 16058 EXPECT_EQ("int a = 5;\n" 16059 "int one = 1;\n" 16060 "\n" 16061 "int oneTwoThree = 123;", 16062 format("int a = 5;\n" 16063 "int one = 1;\n" 16064 "\n" 16065 "int oneTwoThree = 123;", 16066 Alignment)); 16067 16068 /* Test across comments */ 16069 EXPECT_EQ("int a = 5;\n" 16070 "/* block comment */\n" 16071 "int oneTwoThree = 123;", 16072 format("int a = 5;\n" 16073 "/* block comment */\n" 16074 "int oneTwoThree=123;", 16075 Alignment)); 16076 16077 EXPECT_EQ("int a = 5;\n" 16078 "// line comment\n" 16079 "int oneTwoThree = 123;", 16080 format("int a = 5;\n" 16081 "// line comment\n" 16082 "int oneTwoThree=123;", 16083 Alignment)); 16084 16085 EXPECT_EQ("int a = 5;\n" 16086 "/*\n" 16087 " * multi-line block comment\n" 16088 " */\n" 16089 "int oneTwoThree = 123;", 16090 format("int a = 5;\n" 16091 "/*\n" 16092 " * multi-line block comment\n" 16093 " */\n" 16094 "int oneTwoThree=123;", 16095 Alignment)); 16096 16097 EXPECT_EQ("int a = 5;\n" 16098 "//\n" 16099 "// multi-line line comment\n" 16100 "//\n" 16101 "int oneTwoThree = 123;", 16102 format("int a = 5;\n" 16103 "//\n" 16104 "// multi-line line comment\n" 16105 "//\n" 16106 "int oneTwoThree=123;", 16107 Alignment)); 16108 16109 /* Test across comments and newlines */ 16110 EXPECT_EQ("int a = 5;\n" 16111 "\n" 16112 "/* block comment */\n" 16113 "int oneTwoThree = 123;", 16114 format("int a = 5;\n" 16115 "\n" 16116 "/* block comment */\n" 16117 "int oneTwoThree=123;", 16118 Alignment)); 16119 16120 EXPECT_EQ("int a = 5;\n" 16121 "\n" 16122 "// line comment\n" 16123 "int oneTwoThree = 123;", 16124 format("int a = 5;\n" 16125 "\n" 16126 "// line comment\n" 16127 "int oneTwoThree=123;", 16128 Alignment)); 16129 } 16130 16131 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 16132 FormatStyle Alignment = getLLVMStyle(); 16133 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16134 Alignment.AlignConsecutiveAssignments = 16135 FormatStyle::ACS_AcrossEmptyLinesAndComments; 16136 verifyFormat("int a = 5;\n" 16137 "int oneTwoThree = 123;", 16138 Alignment); 16139 verifyFormat("int a = method();\n" 16140 "int oneTwoThree = 133;", 16141 Alignment); 16142 verifyFormat("a &= 5;\n" 16143 "bcd *= 5;\n" 16144 "ghtyf += 5;\n" 16145 "dvfvdb -= 5;\n" 16146 "a /= 5;\n" 16147 "vdsvsv %= 5;\n" 16148 "sfdbddfbdfbb ^= 5;\n" 16149 "dvsdsv |= 5;\n" 16150 "int dsvvdvsdvvv = 123;", 16151 Alignment); 16152 verifyFormat("int i = 1, j = 10;\n" 16153 "something = 2000;", 16154 Alignment); 16155 verifyFormat("something = 2000;\n" 16156 "int i = 1, j = 10;\n", 16157 Alignment); 16158 verifyFormat("something = 2000;\n" 16159 "another = 911;\n" 16160 "int i = 1, j = 10;\n" 16161 "oneMore = 1;\n" 16162 "i = 2;", 16163 Alignment); 16164 verifyFormat("int a = 5;\n" 16165 "int one = 1;\n" 16166 "method();\n" 16167 "int oneTwoThree = 123;\n" 16168 "int oneTwo = 12;", 16169 Alignment); 16170 verifyFormat("int oneTwoThree = 123;\n" 16171 "int oneTwo = 12;\n" 16172 "method();\n", 16173 Alignment); 16174 verifyFormat("int oneTwoThree = 123; // comment\n" 16175 "int oneTwo = 12; // comment", 16176 Alignment); 16177 16178 // Bug 25167 16179 /* Uncomment when fixed 16180 verifyFormat("#if A\n" 16181 "#else\n" 16182 "int aaaaaaaa = 12;\n" 16183 "#endif\n" 16184 "#if B\n" 16185 "#else\n" 16186 "int a = 12;\n" 16187 "#endif\n", 16188 Alignment); 16189 verifyFormat("enum foo {\n" 16190 "#if A\n" 16191 "#else\n" 16192 " aaaaaaaa = 12;\n" 16193 "#endif\n" 16194 "#if B\n" 16195 "#else\n" 16196 " a = 12;\n" 16197 "#endif\n" 16198 "};\n", 16199 Alignment); 16200 */ 16201 16202 Alignment.MaxEmptyLinesToKeep = 10; 16203 /* Test alignment across empty lines */ 16204 EXPECT_EQ("int a = 5;\n" 16205 "\n" 16206 "int oneTwoThree = 123;", 16207 format("int a = 5;\n" 16208 "\n" 16209 "int oneTwoThree= 123;", 16210 Alignment)); 16211 EXPECT_EQ("int a = 5;\n" 16212 "int one = 1;\n" 16213 "\n" 16214 "int oneTwoThree = 123;", 16215 format("int a = 5;\n" 16216 "int one = 1;\n" 16217 "\n" 16218 "int oneTwoThree = 123;", 16219 Alignment)); 16220 EXPECT_EQ("int a = 5;\n" 16221 "int one = 1;\n" 16222 "\n" 16223 "int oneTwoThree = 123;\n" 16224 "int oneTwo = 12;", 16225 format("int a = 5;\n" 16226 "int one = 1;\n" 16227 "\n" 16228 "int oneTwoThree = 123;\n" 16229 "int oneTwo = 12;", 16230 Alignment)); 16231 16232 /* Test across comments */ 16233 EXPECT_EQ("int a = 5;\n" 16234 "/* block comment */\n" 16235 "int oneTwoThree = 123;", 16236 format("int a = 5;\n" 16237 "/* block comment */\n" 16238 "int oneTwoThree=123;", 16239 Alignment)); 16240 16241 EXPECT_EQ("int a = 5;\n" 16242 "// line comment\n" 16243 "int oneTwoThree = 123;", 16244 format("int a = 5;\n" 16245 "// line comment\n" 16246 "int oneTwoThree=123;", 16247 Alignment)); 16248 16249 /* Test across comments and newlines */ 16250 EXPECT_EQ("int a = 5;\n" 16251 "\n" 16252 "/* block comment */\n" 16253 "int oneTwoThree = 123;", 16254 format("int a = 5;\n" 16255 "\n" 16256 "/* block comment */\n" 16257 "int oneTwoThree=123;", 16258 Alignment)); 16259 16260 EXPECT_EQ("int a = 5;\n" 16261 "\n" 16262 "// line comment\n" 16263 "int oneTwoThree = 123;", 16264 format("int a = 5;\n" 16265 "\n" 16266 "// line comment\n" 16267 "int oneTwoThree=123;", 16268 Alignment)); 16269 16270 EXPECT_EQ("int a = 5;\n" 16271 "//\n" 16272 "// multi-line line comment\n" 16273 "//\n" 16274 "int oneTwoThree = 123;", 16275 format("int a = 5;\n" 16276 "//\n" 16277 "// multi-line line comment\n" 16278 "//\n" 16279 "int oneTwoThree=123;", 16280 Alignment)); 16281 16282 EXPECT_EQ("int a = 5;\n" 16283 "/*\n" 16284 " * multi-line block comment\n" 16285 " */\n" 16286 "int oneTwoThree = 123;", 16287 format("int a = 5;\n" 16288 "/*\n" 16289 " * multi-line block comment\n" 16290 " */\n" 16291 "int oneTwoThree=123;", 16292 Alignment)); 16293 16294 EXPECT_EQ("int a = 5;\n" 16295 "\n" 16296 "/* block comment */\n" 16297 "\n" 16298 "\n" 16299 "\n" 16300 "int oneTwoThree = 123;", 16301 format("int a = 5;\n" 16302 "\n" 16303 "/* block comment */\n" 16304 "\n" 16305 "\n" 16306 "\n" 16307 "int oneTwoThree=123;", 16308 Alignment)); 16309 16310 EXPECT_EQ("int a = 5;\n" 16311 "\n" 16312 "// line comment\n" 16313 "\n" 16314 "\n" 16315 "\n" 16316 "int oneTwoThree = 123;", 16317 format("int a = 5;\n" 16318 "\n" 16319 "// line comment\n" 16320 "\n" 16321 "\n" 16322 "\n" 16323 "int oneTwoThree=123;", 16324 Alignment)); 16325 16326 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16327 verifyFormat("#define A \\\n" 16328 " int aaaa = 12; \\\n" 16329 " int b = 23; \\\n" 16330 " int ccc = 234; \\\n" 16331 " int dddddddddd = 2345;", 16332 Alignment); 16333 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16334 verifyFormat("#define A \\\n" 16335 " int aaaa = 12; \\\n" 16336 " int b = 23; \\\n" 16337 " int ccc = 234; \\\n" 16338 " int dddddddddd = 2345;", 16339 Alignment); 16340 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16341 verifyFormat("#define A " 16342 " \\\n" 16343 " int aaaa = 12; " 16344 " \\\n" 16345 " int b = 23; " 16346 " \\\n" 16347 " int ccc = 234; " 16348 " \\\n" 16349 " int dddddddddd = 2345;", 16350 Alignment); 16351 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16352 "k = 4, int l = 5,\n" 16353 " int m = 6) {\n" 16354 " int j = 10;\n" 16355 " otherThing = 1;\n" 16356 "}", 16357 Alignment); 16358 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16359 " int i = 1;\n" 16360 " int j = 2;\n" 16361 " int big = 10000;\n" 16362 "}", 16363 Alignment); 16364 verifyFormat("class C {\n" 16365 "public:\n" 16366 " int i = 1;\n" 16367 " virtual void f() = 0;\n" 16368 "};", 16369 Alignment); 16370 verifyFormat("int i = 1;\n" 16371 "if (SomeType t = getSomething()) {\n" 16372 "}\n" 16373 "int j = 2;\n" 16374 "int big = 10000;", 16375 Alignment); 16376 verifyFormat("int j = 7;\n" 16377 "for (int k = 0; k < N; ++k) {\n" 16378 "}\n" 16379 "int j = 2;\n" 16380 "int big = 10000;\n" 16381 "}", 16382 Alignment); 16383 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16384 verifyFormat("int i = 1;\n" 16385 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16386 " = someLooooooooooooooooongFunction();\n" 16387 "int j = 2;", 16388 Alignment); 16389 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16390 verifyFormat("int i = 1;\n" 16391 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16392 " someLooooooooooooooooongFunction();\n" 16393 "int j = 2;", 16394 Alignment); 16395 16396 verifyFormat("auto lambda = []() {\n" 16397 " auto i = 0;\n" 16398 " return 0;\n" 16399 "};\n" 16400 "int i = 0;\n" 16401 "auto v = type{\n" 16402 " i = 1, //\n" 16403 " (i = 2), //\n" 16404 " i = 3 //\n" 16405 "};", 16406 Alignment); 16407 16408 verifyFormat( 16409 "int i = 1;\n" 16410 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16411 " loooooooooooooooooooooongParameterB);\n" 16412 "int j = 2;", 16413 Alignment); 16414 16415 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16416 " typename B = very_long_type_name_1,\n" 16417 " typename T_2 = very_long_type_name_2>\n" 16418 "auto foo() {}\n", 16419 Alignment); 16420 verifyFormat("int a, b = 1;\n" 16421 "int c = 2;\n" 16422 "int dd = 3;\n", 16423 Alignment); 16424 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16425 "float b[1][] = {{3.f}};\n", 16426 Alignment); 16427 verifyFormat("for (int i = 0; i < 1; i++)\n" 16428 " int x = 1;\n", 16429 Alignment); 16430 verifyFormat("for (i = 0; i < 1; i++)\n" 16431 " x = 1;\n" 16432 "y = 1;\n", 16433 Alignment); 16434 16435 Alignment.ReflowComments = true; 16436 Alignment.ColumnLimit = 50; 16437 EXPECT_EQ("int x = 0;\n" 16438 "int yy = 1; /// specificlennospace\n" 16439 "int zzz = 2;\n", 16440 format("int x = 0;\n" 16441 "int yy = 1; ///specificlennospace\n" 16442 "int zzz = 2;\n", 16443 Alignment)); 16444 } 16445 16446 TEST_F(FormatTest, AlignConsecutiveAssignments) { 16447 FormatStyle Alignment = getLLVMStyle(); 16448 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16449 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16450 verifyFormat("int a = 5;\n" 16451 "int oneTwoThree = 123;", 16452 Alignment); 16453 verifyFormat("int a = 5;\n" 16454 "int oneTwoThree = 123;", 16455 Alignment); 16456 16457 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16458 verifyFormat("int a = 5;\n" 16459 "int oneTwoThree = 123;", 16460 Alignment); 16461 verifyFormat("int a = method();\n" 16462 "int oneTwoThree = 133;", 16463 Alignment); 16464 verifyFormat("a &= 5;\n" 16465 "bcd *= 5;\n" 16466 "ghtyf += 5;\n" 16467 "dvfvdb -= 5;\n" 16468 "a /= 5;\n" 16469 "vdsvsv %= 5;\n" 16470 "sfdbddfbdfbb ^= 5;\n" 16471 "dvsdsv |= 5;\n" 16472 "int dsvvdvsdvvv = 123;", 16473 Alignment); 16474 verifyFormat("int i = 1, j = 10;\n" 16475 "something = 2000;", 16476 Alignment); 16477 verifyFormat("something = 2000;\n" 16478 "int i = 1, j = 10;\n", 16479 Alignment); 16480 verifyFormat("something = 2000;\n" 16481 "another = 911;\n" 16482 "int i = 1, j = 10;\n" 16483 "oneMore = 1;\n" 16484 "i = 2;", 16485 Alignment); 16486 verifyFormat("int a = 5;\n" 16487 "int one = 1;\n" 16488 "method();\n" 16489 "int oneTwoThree = 123;\n" 16490 "int oneTwo = 12;", 16491 Alignment); 16492 verifyFormat("int oneTwoThree = 123;\n" 16493 "int oneTwo = 12;\n" 16494 "method();\n", 16495 Alignment); 16496 verifyFormat("int oneTwoThree = 123; // comment\n" 16497 "int oneTwo = 12; // comment", 16498 Alignment); 16499 verifyFormat("int f() = default;\n" 16500 "int &operator() = default;\n" 16501 "int &operator=() {", 16502 Alignment); 16503 verifyFormat("int f() = delete;\n" 16504 "int &operator() = delete;\n" 16505 "int &operator=() {", 16506 Alignment); 16507 verifyFormat("int f() = default; // comment\n" 16508 "int &operator() = default; // comment\n" 16509 "int &operator=() {", 16510 Alignment); 16511 verifyFormat("int f() = default;\n" 16512 "int &operator() = default;\n" 16513 "int &operator==() {", 16514 Alignment); 16515 verifyFormat("int f() = default;\n" 16516 "int &operator() = default;\n" 16517 "int &operator<=() {", 16518 Alignment); 16519 verifyFormat("int f() = default;\n" 16520 "int &operator() = default;\n" 16521 "int &operator!=() {", 16522 Alignment); 16523 verifyFormat("int f() = default;\n" 16524 "int &operator() = default;\n" 16525 "int &operator=();", 16526 Alignment); 16527 verifyFormat("int f() = delete;\n" 16528 "int &operator() = delete;\n" 16529 "int &operator=();", 16530 Alignment); 16531 verifyFormat("/* long long padding */ int f() = default;\n" 16532 "int &operator() = default;\n" 16533 "int &operator/**/ =();", 16534 Alignment); 16535 // https://llvm.org/PR33697 16536 FormatStyle AlignmentWithPenalty = getLLVMStyle(); 16537 AlignmentWithPenalty.AlignConsecutiveAssignments = 16538 FormatStyle::ACS_Consecutive; 16539 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000; 16540 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n" 16541 " void f() = delete;\n" 16542 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n" 16543 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n" 16544 "};\n", 16545 AlignmentWithPenalty); 16546 16547 // Bug 25167 16548 /* Uncomment when fixed 16549 verifyFormat("#if A\n" 16550 "#else\n" 16551 "int aaaaaaaa = 12;\n" 16552 "#endif\n" 16553 "#if B\n" 16554 "#else\n" 16555 "int a = 12;\n" 16556 "#endif\n", 16557 Alignment); 16558 verifyFormat("enum foo {\n" 16559 "#if A\n" 16560 "#else\n" 16561 " aaaaaaaa = 12;\n" 16562 "#endif\n" 16563 "#if B\n" 16564 "#else\n" 16565 " a = 12;\n" 16566 "#endif\n" 16567 "};\n", 16568 Alignment); 16569 */ 16570 16571 EXPECT_EQ("int a = 5;\n" 16572 "\n" 16573 "int oneTwoThree = 123;", 16574 format("int a = 5;\n" 16575 "\n" 16576 "int oneTwoThree= 123;", 16577 Alignment)); 16578 EXPECT_EQ("int a = 5;\n" 16579 "int one = 1;\n" 16580 "\n" 16581 "int oneTwoThree = 123;", 16582 format("int a = 5;\n" 16583 "int one = 1;\n" 16584 "\n" 16585 "int oneTwoThree = 123;", 16586 Alignment)); 16587 EXPECT_EQ("int a = 5;\n" 16588 "int one = 1;\n" 16589 "\n" 16590 "int oneTwoThree = 123;\n" 16591 "int oneTwo = 12;", 16592 format("int a = 5;\n" 16593 "int one = 1;\n" 16594 "\n" 16595 "int oneTwoThree = 123;\n" 16596 "int oneTwo = 12;", 16597 Alignment)); 16598 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16599 verifyFormat("#define A \\\n" 16600 " int aaaa = 12; \\\n" 16601 " int b = 23; \\\n" 16602 " int ccc = 234; \\\n" 16603 " int dddddddddd = 2345;", 16604 Alignment); 16605 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16606 verifyFormat("#define A \\\n" 16607 " int aaaa = 12; \\\n" 16608 " int b = 23; \\\n" 16609 " int ccc = 234; \\\n" 16610 " int dddddddddd = 2345;", 16611 Alignment); 16612 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16613 verifyFormat("#define A " 16614 " \\\n" 16615 " int aaaa = 12; " 16616 " \\\n" 16617 " int b = 23; " 16618 " \\\n" 16619 " int ccc = 234; " 16620 " \\\n" 16621 " int dddddddddd = 2345;", 16622 Alignment); 16623 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16624 "k = 4, int l = 5,\n" 16625 " int m = 6) {\n" 16626 " int j = 10;\n" 16627 " otherThing = 1;\n" 16628 "}", 16629 Alignment); 16630 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16631 " int i = 1;\n" 16632 " int j = 2;\n" 16633 " int big = 10000;\n" 16634 "}", 16635 Alignment); 16636 verifyFormat("class C {\n" 16637 "public:\n" 16638 " int i = 1;\n" 16639 " virtual void f() = 0;\n" 16640 "};", 16641 Alignment); 16642 verifyFormat("int i = 1;\n" 16643 "if (SomeType t = getSomething()) {\n" 16644 "}\n" 16645 "int j = 2;\n" 16646 "int big = 10000;", 16647 Alignment); 16648 verifyFormat("int j = 7;\n" 16649 "for (int k = 0; k < N; ++k) {\n" 16650 "}\n" 16651 "int j = 2;\n" 16652 "int big = 10000;\n" 16653 "}", 16654 Alignment); 16655 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16656 verifyFormat("int i = 1;\n" 16657 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16658 " = someLooooooooooooooooongFunction();\n" 16659 "int j = 2;", 16660 Alignment); 16661 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16662 verifyFormat("int i = 1;\n" 16663 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16664 " someLooooooooooooooooongFunction();\n" 16665 "int j = 2;", 16666 Alignment); 16667 16668 verifyFormat("auto lambda = []() {\n" 16669 " auto i = 0;\n" 16670 " return 0;\n" 16671 "};\n" 16672 "int i = 0;\n" 16673 "auto v = type{\n" 16674 " i = 1, //\n" 16675 " (i = 2), //\n" 16676 " i = 3 //\n" 16677 "};", 16678 Alignment); 16679 16680 verifyFormat( 16681 "int i = 1;\n" 16682 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16683 " loooooooooooooooooooooongParameterB);\n" 16684 "int j = 2;", 16685 Alignment); 16686 16687 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16688 " typename B = very_long_type_name_1,\n" 16689 " typename T_2 = very_long_type_name_2>\n" 16690 "auto foo() {}\n", 16691 Alignment); 16692 verifyFormat("int a, b = 1;\n" 16693 "int c = 2;\n" 16694 "int dd = 3;\n", 16695 Alignment); 16696 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16697 "float b[1][] = {{3.f}};\n", 16698 Alignment); 16699 verifyFormat("for (int i = 0; i < 1; i++)\n" 16700 " int x = 1;\n", 16701 Alignment); 16702 verifyFormat("for (i = 0; i < 1; i++)\n" 16703 " x = 1;\n" 16704 "y = 1;\n", 16705 Alignment); 16706 16707 EXPECT_EQ(Alignment.ReflowComments, true); 16708 Alignment.ColumnLimit = 50; 16709 EXPECT_EQ("int x = 0;\n" 16710 "int yy = 1; /// specificlennospace\n" 16711 "int zzz = 2;\n", 16712 format("int x = 0;\n" 16713 "int yy = 1; ///specificlennospace\n" 16714 "int zzz = 2;\n", 16715 Alignment)); 16716 16717 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16718 "auto b = [] {\n" 16719 " f();\n" 16720 " return;\n" 16721 "};", 16722 Alignment); 16723 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16724 "auto b = g([] {\n" 16725 " f();\n" 16726 " return;\n" 16727 "});", 16728 Alignment); 16729 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16730 "auto b = g(param, [] {\n" 16731 " f();\n" 16732 " return;\n" 16733 "});", 16734 Alignment); 16735 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16736 "auto b = [] {\n" 16737 " if (condition) {\n" 16738 " return;\n" 16739 " }\n" 16740 "};", 16741 Alignment); 16742 16743 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 16744 " ccc ? aaaaa : bbbbb,\n" 16745 " dddddddddddddddddddddddddd);", 16746 Alignment); 16747 // FIXME: https://llvm.org/PR53497 16748 // verifyFormat("auto aaaaaaaaaaaa = f();\n" 16749 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 16750 // " ccc ? aaaaa : bbbbb,\n" 16751 // " dddddddddddddddddddddddddd);", 16752 // Alignment); 16753 } 16754 16755 TEST_F(FormatTest, AlignConsecutiveBitFields) { 16756 FormatStyle Alignment = getLLVMStyle(); 16757 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 16758 verifyFormat("int const a : 5;\n" 16759 "int oneTwoThree : 23;", 16760 Alignment); 16761 16762 // Initializers are allowed starting with c++2a 16763 verifyFormat("int const a : 5 = 1;\n" 16764 "int oneTwoThree : 23 = 0;", 16765 Alignment); 16766 16767 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16768 verifyFormat("int const a : 5;\n" 16769 "int oneTwoThree : 23;", 16770 Alignment); 16771 16772 verifyFormat("int const a : 5; // comment\n" 16773 "int oneTwoThree : 23; // comment", 16774 Alignment); 16775 16776 verifyFormat("int const a : 5 = 1;\n" 16777 "int oneTwoThree : 23 = 0;", 16778 Alignment); 16779 16780 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16781 verifyFormat("int const a : 5 = 1;\n" 16782 "int oneTwoThree : 23 = 0;", 16783 Alignment); 16784 verifyFormat("int const a : 5 = {1};\n" 16785 "int oneTwoThree : 23 = 0;", 16786 Alignment); 16787 16788 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 16789 verifyFormat("int const a :5;\n" 16790 "int oneTwoThree:23;", 16791 Alignment); 16792 16793 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 16794 verifyFormat("int const a :5;\n" 16795 "int oneTwoThree :23;", 16796 Alignment); 16797 16798 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 16799 verifyFormat("int const a : 5;\n" 16800 "int oneTwoThree: 23;", 16801 Alignment); 16802 16803 // Known limitations: ':' is only recognized as a bitfield colon when 16804 // followed by a number. 16805 /* 16806 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 16807 "int a : 5;", 16808 Alignment); 16809 */ 16810 } 16811 16812 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 16813 FormatStyle Alignment = getLLVMStyle(); 16814 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16815 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16816 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16817 verifyFormat("float const a = 5;\n" 16818 "int oneTwoThree = 123;", 16819 Alignment); 16820 verifyFormat("int a = 5;\n" 16821 "float const oneTwoThree = 123;", 16822 Alignment); 16823 16824 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16825 verifyFormat("float const a = 5;\n" 16826 "int oneTwoThree = 123;", 16827 Alignment); 16828 verifyFormat("int a = method();\n" 16829 "float const oneTwoThree = 133;", 16830 Alignment); 16831 verifyFormat("int i = 1, j = 10;\n" 16832 "something = 2000;", 16833 Alignment); 16834 verifyFormat("something = 2000;\n" 16835 "int i = 1, j = 10;\n", 16836 Alignment); 16837 verifyFormat("float something = 2000;\n" 16838 "double another = 911;\n" 16839 "int i = 1, j = 10;\n" 16840 "const int *oneMore = 1;\n" 16841 "unsigned i = 2;", 16842 Alignment); 16843 verifyFormat("float a = 5;\n" 16844 "int one = 1;\n" 16845 "method();\n" 16846 "const double oneTwoThree = 123;\n" 16847 "const unsigned int oneTwo = 12;", 16848 Alignment); 16849 verifyFormat("int oneTwoThree{0}; // comment\n" 16850 "unsigned oneTwo; // comment", 16851 Alignment); 16852 verifyFormat("unsigned int *a;\n" 16853 "int *b;\n" 16854 "unsigned int Const *c;\n" 16855 "unsigned int const *d;\n" 16856 "unsigned int Const &e;\n" 16857 "unsigned int const &f;", 16858 Alignment); 16859 verifyFormat("Const unsigned int *c;\n" 16860 "const unsigned int *d;\n" 16861 "Const unsigned int &e;\n" 16862 "const unsigned int &f;\n" 16863 "const unsigned g;\n" 16864 "Const unsigned h;", 16865 Alignment); 16866 EXPECT_EQ("float const a = 5;\n" 16867 "\n" 16868 "int oneTwoThree = 123;", 16869 format("float const a = 5;\n" 16870 "\n" 16871 "int oneTwoThree= 123;", 16872 Alignment)); 16873 EXPECT_EQ("float a = 5;\n" 16874 "int one = 1;\n" 16875 "\n" 16876 "unsigned oneTwoThree = 123;", 16877 format("float a = 5;\n" 16878 "int one = 1;\n" 16879 "\n" 16880 "unsigned oneTwoThree = 123;", 16881 Alignment)); 16882 EXPECT_EQ("float a = 5;\n" 16883 "int one = 1;\n" 16884 "\n" 16885 "unsigned oneTwoThree = 123;\n" 16886 "int oneTwo = 12;", 16887 format("float a = 5;\n" 16888 "int one = 1;\n" 16889 "\n" 16890 "unsigned oneTwoThree = 123;\n" 16891 "int oneTwo = 12;", 16892 Alignment)); 16893 // Function prototype alignment 16894 verifyFormat("int a();\n" 16895 "double b();", 16896 Alignment); 16897 verifyFormat("int a(int x);\n" 16898 "double b();", 16899 Alignment); 16900 unsigned OldColumnLimit = Alignment.ColumnLimit; 16901 // We need to set ColumnLimit to zero, in order to stress nested alignments, 16902 // otherwise the function parameters will be re-flowed onto a single line. 16903 Alignment.ColumnLimit = 0; 16904 EXPECT_EQ("int a(int x,\n" 16905 " float y);\n" 16906 "double b(int x,\n" 16907 " double y);", 16908 format("int a(int x,\n" 16909 " float y);\n" 16910 "double b(int x,\n" 16911 " double y);", 16912 Alignment)); 16913 // This ensures that function parameters of function declarations are 16914 // correctly indented when their owning functions are indented. 16915 // The failure case here is for 'double y' to not be indented enough. 16916 EXPECT_EQ("double a(int x);\n" 16917 "int b(int y,\n" 16918 " double z);", 16919 format("double a(int x);\n" 16920 "int b(int y,\n" 16921 " double z);", 16922 Alignment)); 16923 // Set ColumnLimit low so that we induce wrapping immediately after 16924 // the function name and opening paren. 16925 Alignment.ColumnLimit = 13; 16926 verifyFormat("int function(\n" 16927 " int x,\n" 16928 " bool y);", 16929 Alignment); 16930 Alignment.ColumnLimit = OldColumnLimit; 16931 // Ensure function pointers don't screw up recursive alignment 16932 verifyFormat("int a(int x, void (*fp)(int y));\n" 16933 "double b();", 16934 Alignment); 16935 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16936 // Ensure recursive alignment is broken by function braces, so that the 16937 // "a = 1" does not align with subsequent assignments inside the function 16938 // body. 16939 verifyFormat("int func(int a = 1) {\n" 16940 " int b = 2;\n" 16941 " int cc = 3;\n" 16942 "}", 16943 Alignment); 16944 verifyFormat("float something = 2000;\n" 16945 "double another = 911;\n" 16946 "int i = 1, j = 10;\n" 16947 "const int *oneMore = 1;\n" 16948 "unsigned i = 2;", 16949 Alignment); 16950 verifyFormat("int oneTwoThree = {0}; // comment\n" 16951 "unsigned oneTwo = 0; // comment", 16952 Alignment); 16953 // Make sure that scope is correctly tracked, in the absence of braces 16954 verifyFormat("for (int i = 0; i < n; i++)\n" 16955 " j = i;\n" 16956 "double x = 1;\n", 16957 Alignment); 16958 verifyFormat("if (int i = 0)\n" 16959 " j = i;\n" 16960 "double x = 1;\n", 16961 Alignment); 16962 // Ensure operator[] and operator() are comprehended 16963 verifyFormat("struct test {\n" 16964 " long long int foo();\n" 16965 " int operator[](int a);\n" 16966 " double bar();\n" 16967 "};\n", 16968 Alignment); 16969 verifyFormat("struct test {\n" 16970 " long long int foo();\n" 16971 " int operator()(int a);\n" 16972 " double bar();\n" 16973 "};\n", 16974 Alignment); 16975 // http://llvm.org/PR52914 16976 verifyFormat("char *a[] = {\"a\", // comment\n" 16977 " \"bb\"};\n" 16978 "int bbbbbbb = 0;", 16979 Alignment); 16980 16981 // PAS_Right 16982 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16983 " int const i = 1;\n" 16984 " int *j = 2;\n" 16985 " int big = 10000;\n" 16986 "\n" 16987 " unsigned oneTwoThree = 123;\n" 16988 " int oneTwo = 12;\n" 16989 " method();\n" 16990 " float k = 2;\n" 16991 " int ll = 10000;\n" 16992 "}", 16993 format("void SomeFunction(int parameter= 0) {\n" 16994 " int const i= 1;\n" 16995 " int *j=2;\n" 16996 " int big = 10000;\n" 16997 "\n" 16998 "unsigned oneTwoThree =123;\n" 16999 "int oneTwo = 12;\n" 17000 " method();\n" 17001 "float k= 2;\n" 17002 "int ll=10000;\n" 17003 "}", 17004 Alignment)); 17005 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17006 " int const i = 1;\n" 17007 " int **j = 2, ***k;\n" 17008 " int &k = i;\n" 17009 " int &&l = i + j;\n" 17010 " int big = 10000;\n" 17011 "\n" 17012 " unsigned oneTwoThree = 123;\n" 17013 " int oneTwo = 12;\n" 17014 " method();\n" 17015 " float k = 2;\n" 17016 " int ll = 10000;\n" 17017 "}", 17018 format("void SomeFunction(int parameter= 0) {\n" 17019 " int const i= 1;\n" 17020 " int **j=2,***k;\n" 17021 "int &k=i;\n" 17022 "int &&l=i+j;\n" 17023 " int big = 10000;\n" 17024 "\n" 17025 "unsigned oneTwoThree =123;\n" 17026 "int oneTwo = 12;\n" 17027 " method();\n" 17028 "float k= 2;\n" 17029 "int ll=10000;\n" 17030 "}", 17031 Alignment)); 17032 // variables are aligned at their name, pointers are at the right most 17033 // position 17034 verifyFormat("int *a;\n" 17035 "int **b;\n" 17036 "int ***c;\n" 17037 "int foobar;\n", 17038 Alignment); 17039 17040 // PAS_Left 17041 FormatStyle AlignmentLeft = Alignment; 17042 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 17043 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17044 " int const i = 1;\n" 17045 " int* j = 2;\n" 17046 " int big = 10000;\n" 17047 "\n" 17048 " unsigned oneTwoThree = 123;\n" 17049 " int oneTwo = 12;\n" 17050 " method();\n" 17051 " float k = 2;\n" 17052 " int ll = 10000;\n" 17053 "}", 17054 format("void SomeFunction(int parameter= 0) {\n" 17055 " int const i= 1;\n" 17056 " int *j=2;\n" 17057 " int big = 10000;\n" 17058 "\n" 17059 "unsigned oneTwoThree =123;\n" 17060 "int oneTwo = 12;\n" 17061 " method();\n" 17062 "float k= 2;\n" 17063 "int ll=10000;\n" 17064 "}", 17065 AlignmentLeft)); 17066 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17067 " int const i = 1;\n" 17068 " int** j = 2;\n" 17069 " int& k = i;\n" 17070 " int&& l = i + j;\n" 17071 " int big = 10000;\n" 17072 "\n" 17073 " unsigned oneTwoThree = 123;\n" 17074 " int oneTwo = 12;\n" 17075 " method();\n" 17076 " float k = 2;\n" 17077 " int ll = 10000;\n" 17078 "}", 17079 format("void SomeFunction(int parameter= 0) {\n" 17080 " int const i= 1;\n" 17081 " int **j=2;\n" 17082 "int &k=i;\n" 17083 "int &&l=i+j;\n" 17084 " int big = 10000;\n" 17085 "\n" 17086 "unsigned oneTwoThree =123;\n" 17087 "int oneTwo = 12;\n" 17088 " method();\n" 17089 "float k= 2;\n" 17090 "int ll=10000;\n" 17091 "}", 17092 AlignmentLeft)); 17093 // variables are aligned at their name, pointers are at the left most position 17094 verifyFormat("int* a;\n" 17095 "int** b;\n" 17096 "int*** c;\n" 17097 "int foobar;\n", 17098 AlignmentLeft); 17099 17100 // PAS_Middle 17101 FormatStyle AlignmentMiddle = Alignment; 17102 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 17103 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17104 " int const i = 1;\n" 17105 " int * j = 2;\n" 17106 " int big = 10000;\n" 17107 "\n" 17108 " unsigned oneTwoThree = 123;\n" 17109 " int oneTwo = 12;\n" 17110 " method();\n" 17111 " float k = 2;\n" 17112 " int ll = 10000;\n" 17113 "}", 17114 format("void SomeFunction(int parameter= 0) {\n" 17115 " int const i= 1;\n" 17116 " int *j=2;\n" 17117 " int big = 10000;\n" 17118 "\n" 17119 "unsigned oneTwoThree =123;\n" 17120 "int oneTwo = 12;\n" 17121 " method();\n" 17122 "float k= 2;\n" 17123 "int ll=10000;\n" 17124 "}", 17125 AlignmentMiddle)); 17126 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17127 " int const i = 1;\n" 17128 " int ** j = 2, ***k;\n" 17129 " int & k = i;\n" 17130 " int && l = i + j;\n" 17131 " int big = 10000;\n" 17132 "\n" 17133 " unsigned oneTwoThree = 123;\n" 17134 " int oneTwo = 12;\n" 17135 " method();\n" 17136 " float k = 2;\n" 17137 " int ll = 10000;\n" 17138 "}", 17139 format("void SomeFunction(int parameter= 0) {\n" 17140 " int const i= 1;\n" 17141 " int **j=2,***k;\n" 17142 "int &k=i;\n" 17143 "int &&l=i+j;\n" 17144 " int big = 10000;\n" 17145 "\n" 17146 "unsigned oneTwoThree =123;\n" 17147 "int oneTwo = 12;\n" 17148 " method();\n" 17149 "float k= 2;\n" 17150 "int ll=10000;\n" 17151 "}", 17152 AlignmentMiddle)); 17153 // variables are aligned at their name, pointers are in the middle 17154 verifyFormat("int * a;\n" 17155 "int * b;\n" 17156 "int *** c;\n" 17157 "int foobar;\n", 17158 AlignmentMiddle); 17159 17160 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17161 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 17162 verifyFormat("#define A \\\n" 17163 " int aaaa = 12; \\\n" 17164 " float b = 23; \\\n" 17165 " const int ccc = 234; \\\n" 17166 " unsigned dddddddddd = 2345;", 17167 Alignment); 17168 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 17169 verifyFormat("#define A \\\n" 17170 " int aaaa = 12; \\\n" 17171 " float b = 23; \\\n" 17172 " const int ccc = 234; \\\n" 17173 " unsigned dddddddddd = 2345;", 17174 Alignment); 17175 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 17176 Alignment.ColumnLimit = 30; 17177 verifyFormat("#define A \\\n" 17178 " int aaaa = 12; \\\n" 17179 " float b = 23; \\\n" 17180 " const int ccc = 234; \\\n" 17181 " int dddddddddd = 2345;", 17182 Alignment); 17183 Alignment.ColumnLimit = 80; 17184 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 17185 "k = 4, int l = 5,\n" 17186 " int m = 6) {\n" 17187 " const int j = 10;\n" 17188 " otherThing = 1;\n" 17189 "}", 17190 Alignment); 17191 verifyFormat("void SomeFunction(int parameter = 0) {\n" 17192 " int const i = 1;\n" 17193 " int *j = 2;\n" 17194 " int big = 10000;\n" 17195 "}", 17196 Alignment); 17197 verifyFormat("class C {\n" 17198 "public:\n" 17199 " int i = 1;\n" 17200 " virtual void f() = 0;\n" 17201 "};", 17202 Alignment); 17203 verifyFormat("float i = 1;\n" 17204 "if (SomeType t = getSomething()) {\n" 17205 "}\n" 17206 "const unsigned j = 2;\n" 17207 "int big = 10000;", 17208 Alignment); 17209 verifyFormat("float j = 7;\n" 17210 "for (int k = 0; k < N; ++k) {\n" 17211 "}\n" 17212 "unsigned j = 2;\n" 17213 "int big = 10000;\n" 17214 "}", 17215 Alignment); 17216 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 17217 verifyFormat("float i = 1;\n" 17218 "LooooooooooongType loooooooooooooooooooooongVariable\n" 17219 " = someLooooooooooooooooongFunction();\n" 17220 "int j = 2;", 17221 Alignment); 17222 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 17223 verifyFormat("int i = 1;\n" 17224 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 17225 " someLooooooooooooooooongFunction();\n" 17226 "int j = 2;", 17227 Alignment); 17228 17229 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17230 verifyFormat("auto lambda = []() {\n" 17231 " auto ii = 0;\n" 17232 " float j = 0;\n" 17233 " return 0;\n" 17234 "};\n" 17235 "int i = 0;\n" 17236 "float i2 = 0;\n" 17237 "auto v = type{\n" 17238 " i = 1, //\n" 17239 " (i = 2), //\n" 17240 " i = 3 //\n" 17241 "};", 17242 Alignment); 17243 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17244 17245 verifyFormat( 17246 "int i = 1;\n" 17247 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 17248 " loooooooooooooooooooooongParameterB);\n" 17249 "int j = 2;", 17250 Alignment); 17251 17252 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 17253 // We expect declarations and assignments to align, as long as it doesn't 17254 // exceed the column limit, starting a new alignment sequence whenever it 17255 // happens. 17256 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17257 Alignment.ColumnLimit = 30; 17258 verifyFormat("float ii = 1;\n" 17259 "unsigned j = 2;\n" 17260 "int someVerylongVariable = 1;\n" 17261 "AnotherLongType ll = 123456;\n" 17262 "VeryVeryLongType k = 2;\n" 17263 "int myvar = 1;", 17264 Alignment); 17265 Alignment.ColumnLimit = 80; 17266 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17267 17268 verifyFormat( 17269 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 17270 " typename LongType, typename B>\n" 17271 "auto foo() {}\n", 17272 Alignment); 17273 verifyFormat("float a, b = 1;\n" 17274 "int c = 2;\n" 17275 "int dd = 3;\n", 17276 Alignment); 17277 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17278 "float b[1][] = {{3.f}};\n", 17279 Alignment); 17280 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17281 verifyFormat("float a, b = 1;\n" 17282 "int c = 2;\n" 17283 "int dd = 3;\n", 17284 Alignment); 17285 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17286 "float b[1][] = {{3.f}};\n", 17287 Alignment); 17288 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17289 17290 Alignment.ColumnLimit = 30; 17291 Alignment.BinPackParameters = false; 17292 verifyFormat("void foo(float a,\n" 17293 " float b,\n" 17294 " int c,\n" 17295 " uint32_t *d) {\n" 17296 " int *e = 0;\n" 17297 " float f = 0;\n" 17298 " double g = 0;\n" 17299 "}\n" 17300 "void bar(ino_t a,\n" 17301 " int b,\n" 17302 " uint32_t *c,\n" 17303 " bool d) {}\n", 17304 Alignment); 17305 Alignment.BinPackParameters = true; 17306 Alignment.ColumnLimit = 80; 17307 17308 // Bug 33507 17309 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17310 verifyFormat( 17311 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 17312 " static const Version verVs2017;\n" 17313 " return true;\n" 17314 "});\n", 17315 Alignment); 17316 Alignment.PointerAlignment = FormatStyle::PAS_Right; 17317 17318 // See llvm.org/PR35641 17319 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17320 verifyFormat("int func() { //\n" 17321 " int b;\n" 17322 " unsigned c;\n" 17323 "}", 17324 Alignment); 17325 17326 // See PR37175 17327 FormatStyle Style = getMozillaStyle(); 17328 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17329 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 17330 "foo(int a);", 17331 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 17332 17333 Alignment.PointerAlignment = FormatStyle::PAS_Left; 17334 verifyFormat("unsigned int* a;\n" 17335 "int* b;\n" 17336 "unsigned int Const* c;\n" 17337 "unsigned int const* d;\n" 17338 "unsigned int Const& e;\n" 17339 "unsigned int const& f;", 17340 Alignment); 17341 verifyFormat("Const unsigned int* c;\n" 17342 "const unsigned int* d;\n" 17343 "Const unsigned int& e;\n" 17344 "const unsigned int& f;\n" 17345 "const unsigned g;\n" 17346 "Const unsigned h;", 17347 Alignment); 17348 17349 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17350 verifyFormat("unsigned int * a;\n" 17351 "int * b;\n" 17352 "unsigned int Const * c;\n" 17353 "unsigned int const * d;\n" 17354 "unsigned int Const & e;\n" 17355 "unsigned int const & f;", 17356 Alignment); 17357 verifyFormat("Const unsigned int * c;\n" 17358 "const unsigned int * d;\n" 17359 "Const unsigned int & e;\n" 17360 "const unsigned int & f;\n" 17361 "const unsigned g;\n" 17362 "Const unsigned h;", 17363 Alignment); 17364 17365 // See PR46529 17366 FormatStyle BracedAlign = getLLVMStyle(); 17367 BracedAlign.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17368 verifyFormat("const auto result{[]() {\n" 17369 " const auto something = 1;\n" 17370 " return 2;\n" 17371 "}};", 17372 BracedAlign); 17373 verifyFormat("int foo{[]() {\n" 17374 " int bar{0};\n" 17375 " return 0;\n" 17376 "}()};", 17377 BracedAlign); 17378 BracedAlign.Cpp11BracedListStyle = false; 17379 verifyFormat("const auto result{ []() {\n" 17380 " const auto something = 1;\n" 17381 " return 2;\n" 17382 "} };", 17383 BracedAlign); 17384 verifyFormat("int foo{ []() {\n" 17385 " int bar{ 0 };\n" 17386 " return 0;\n" 17387 "}() };", 17388 BracedAlign); 17389 } 17390 17391 TEST_F(FormatTest, AlignWithLineBreaks) { 17392 auto Style = getLLVMStyleWithColumns(120); 17393 17394 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 17395 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 17396 verifyFormat("void foo() {\n" 17397 " int myVar = 5;\n" 17398 " double x = 3.14;\n" 17399 " auto str = \"Hello \"\n" 17400 " \"World\";\n" 17401 " auto s = \"Hello \"\n" 17402 " \"Again\";\n" 17403 "}", 17404 Style); 17405 17406 // clang-format off 17407 verifyFormat("void foo() {\n" 17408 " const int capacityBefore = Entries.capacity();\n" 17409 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17410 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17411 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17412 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17413 "}", 17414 Style); 17415 // clang-format on 17416 17417 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17418 verifyFormat("void foo() {\n" 17419 " int myVar = 5;\n" 17420 " double x = 3.14;\n" 17421 " auto str = \"Hello \"\n" 17422 " \"World\";\n" 17423 " auto s = \"Hello \"\n" 17424 " \"Again\";\n" 17425 "}", 17426 Style); 17427 17428 // clang-format off 17429 verifyFormat("void foo() {\n" 17430 " const int capacityBefore = Entries.capacity();\n" 17431 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17432 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17433 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17434 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17435 "}", 17436 Style); 17437 // clang-format on 17438 17439 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17440 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17441 verifyFormat("void foo() {\n" 17442 " int myVar = 5;\n" 17443 " double x = 3.14;\n" 17444 " auto str = \"Hello \"\n" 17445 " \"World\";\n" 17446 " auto s = \"Hello \"\n" 17447 " \"Again\";\n" 17448 "}", 17449 Style); 17450 17451 // clang-format off 17452 verifyFormat("void foo() {\n" 17453 " const int capacityBefore = Entries.capacity();\n" 17454 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17455 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17456 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17457 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17458 "}", 17459 Style); 17460 // clang-format on 17461 17462 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17463 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17464 17465 verifyFormat("void foo() {\n" 17466 " int myVar = 5;\n" 17467 " double x = 3.14;\n" 17468 " auto str = \"Hello \"\n" 17469 " \"World\";\n" 17470 " auto s = \"Hello \"\n" 17471 " \"Again\";\n" 17472 "}", 17473 Style); 17474 17475 // clang-format off 17476 verifyFormat("void foo() {\n" 17477 " const int capacityBefore = Entries.capacity();\n" 17478 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17479 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17480 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17481 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17482 "}", 17483 Style); 17484 // clang-format on 17485 17486 Style = getLLVMStyleWithColumns(120); 17487 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17488 Style.ContinuationIndentWidth = 4; 17489 Style.IndentWidth = 4; 17490 17491 // clang-format off 17492 verifyFormat("void SomeFunc() {\n" 17493 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17494 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17495 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17496 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17497 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17498 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17499 "}", 17500 Style); 17501 // clang-format on 17502 17503 Style.BinPackArguments = false; 17504 17505 // clang-format off 17506 verifyFormat("void SomeFunc() {\n" 17507 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 17508 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17509 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 17510 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17511 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 17512 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17513 "}", 17514 Style); 17515 // clang-format on 17516 } 17517 17518 TEST_F(FormatTest, AlignWithInitializerPeriods) { 17519 auto Style = getLLVMStyleWithColumns(60); 17520 17521 verifyFormat("void foo1(void) {\n" 17522 " BYTE p[1] = 1;\n" 17523 " A B = {.one_foooooooooooooooo = 2,\n" 17524 " .two_fooooooooooooo = 3,\n" 17525 " .three_fooooooooooooo = 4};\n" 17526 " BYTE payload = 2;\n" 17527 "}", 17528 Style); 17529 17530 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17531 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 17532 verifyFormat("void foo2(void) {\n" 17533 " BYTE p[1] = 1;\n" 17534 " A B = {.one_foooooooooooooooo = 2,\n" 17535 " .two_fooooooooooooo = 3,\n" 17536 " .three_fooooooooooooo = 4};\n" 17537 " BYTE payload = 2;\n" 17538 "}", 17539 Style); 17540 17541 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17542 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17543 verifyFormat("void foo3(void) {\n" 17544 " BYTE p[1] = 1;\n" 17545 " A B = {.one_foooooooooooooooo = 2,\n" 17546 " .two_fooooooooooooo = 3,\n" 17547 " .three_fooooooooooooo = 4};\n" 17548 " BYTE payload = 2;\n" 17549 "}", 17550 Style); 17551 17552 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17553 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17554 verifyFormat("void foo4(void) {\n" 17555 " BYTE p[1] = 1;\n" 17556 " A B = {.one_foooooooooooooooo = 2,\n" 17557 " .two_fooooooooooooo = 3,\n" 17558 " .three_fooooooooooooo = 4};\n" 17559 " BYTE payload = 2;\n" 17560 "}", 17561 Style); 17562 } 17563 17564 TEST_F(FormatTest, LinuxBraceBreaking) { 17565 FormatStyle LinuxBraceStyle = getLLVMStyle(); 17566 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 17567 verifyFormat("namespace a\n" 17568 "{\n" 17569 "class A\n" 17570 "{\n" 17571 " void f()\n" 17572 " {\n" 17573 " if (true) {\n" 17574 " a();\n" 17575 " b();\n" 17576 " } else {\n" 17577 " a();\n" 17578 " }\n" 17579 " }\n" 17580 " void g() { return; }\n" 17581 "};\n" 17582 "struct B {\n" 17583 " int x;\n" 17584 "};\n" 17585 "} // namespace a\n", 17586 LinuxBraceStyle); 17587 verifyFormat("enum X {\n" 17588 " Y = 0,\n" 17589 "}\n", 17590 LinuxBraceStyle); 17591 verifyFormat("struct S {\n" 17592 " int Type;\n" 17593 " union {\n" 17594 " int x;\n" 17595 " double y;\n" 17596 " } Value;\n" 17597 " class C\n" 17598 " {\n" 17599 " MyFavoriteType Value;\n" 17600 " } Class;\n" 17601 "}\n", 17602 LinuxBraceStyle); 17603 } 17604 17605 TEST_F(FormatTest, MozillaBraceBreaking) { 17606 FormatStyle MozillaBraceStyle = getLLVMStyle(); 17607 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 17608 MozillaBraceStyle.FixNamespaceComments = false; 17609 verifyFormat("namespace a {\n" 17610 "class A\n" 17611 "{\n" 17612 " void f()\n" 17613 " {\n" 17614 " if (true) {\n" 17615 " a();\n" 17616 " b();\n" 17617 " }\n" 17618 " }\n" 17619 " void g() { return; }\n" 17620 "};\n" 17621 "enum E\n" 17622 "{\n" 17623 " A,\n" 17624 " // foo\n" 17625 " B,\n" 17626 " C\n" 17627 "};\n" 17628 "struct B\n" 17629 "{\n" 17630 " int x;\n" 17631 "};\n" 17632 "}\n", 17633 MozillaBraceStyle); 17634 verifyFormat("struct S\n" 17635 "{\n" 17636 " int Type;\n" 17637 " union\n" 17638 " {\n" 17639 " int x;\n" 17640 " double y;\n" 17641 " } Value;\n" 17642 " class C\n" 17643 " {\n" 17644 " MyFavoriteType Value;\n" 17645 " } Class;\n" 17646 "}\n", 17647 MozillaBraceStyle); 17648 } 17649 17650 TEST_F(FormatTest, StroustrupBraceBreaking) { 17651 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 17652 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 17653 verifyFormat("namespace a {\n" 17654 "class A {\n" 17655 " void f()\n" 17656 " {\n" 17657 " if (true) {\n" 17658 " a();\n" 17659 " b();\n" 17660 " }\n" 17661 " }\n" 17662 " void g() { return; }\n" 17663 "};\n" 17664 "struct B {\n" 17665 " int x;\n" 17666 "};\n" 17667 "} // namespace a\n", 17668 StroustrupBraceStyle); 17669 17670 verifyFormat("void foo()\n" 17671 "{\n" 17672 " if (a) {\n" 17673 " a();\n" 17674 " }\n" 17675 " else {\n" 17676 " b();\n" 17677 " }\n" 17678 "}\n", 17679 StroustrupBraceStyle); 17680 17681 verifyFormat("#ifdef _DEBUG\n" 17682 "int foo(int i = 0)\n" 17683 "#else\n" 17684 "int foo(int i = 5)\n" 17685 "#endif\n" 17686 "{\n" 17687 " return i;\n" 17688 "}", 17689 StroustrupBraceStyle); 17690 17691 verifyFormat("void foo() {}\n" 17692 "void bar()\n" 17693 "#ifdef _DEBUG\n" 17694 "{\n" 17695 " foo();\n" 17696 "}\n" 17697 "#else\n" 17698 "{\n" 17699 "}\n" 17700 "#endif", 17701 StroustrupBraceStyle); 17702 17703 verifyFormat("void foobar() { int i = 5; }\n" 17704 "#ifdef _DEBUG\n" 17705 "void bar() {}\n" 17706 "#else\n" 17707 "void bar() { foobar(); }\n" 17708 "#endif", 17709 StroustrupBraceStyle); 17710 } 17711 17712 TEST_F(FormatTest, AllmanBraceBreaking) { 17713 FormatStyle AllmanBraceStyle = getLLVMStyle(); 17714 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 17715 17716 EXPECT_EQ("namespace a\n" 17717 "{\n" 17718 "void f();\n" 17719 "void g();\n" 17720 "} // namespace a\n", 17721 format("namespace a\n" 17722 "{\n" 17723 "void f();\n" 17724 "void g();\n" 17725 "}\n", 17726 AllmanBraceStyle)); 17727 17728 verifyFormat("namespace a\n" 17729 "{\n" 17730 "class A\n" 17731 "{\n" 17732 " void f()\n" 17733 " {\n" 17734 " if (true)\n" 17735 " {\n" 17736 " a();\n" 17737 " b();\n" 17738 " }\n" 17739 " }\n" 17740 " void g() { return; }\n" 17741 "};\n" 17742 "struct B\n" 17743 "{\n" 17744 " int x;\n" 17745 "};\n" 17746 "union C\n" 17747 "{\n" 17748 "};\n" 17749 "} // namespace a", 17750 AllmanBraceStyle); 17751 17752 verifyFormat("void f()\n" 17753 "{\n" 17754 " if (true)\n" 17755 " {\n" 17756 " a();\n" 17757 " }\n" 17758 " else if (false)\n" 17759 " {\n" 17760 " b();\n" 17761 " }\n" 17762 " else\n" 17763 " {\n" 17764 " c();\n" 17765 " }\n" 17766 "}\n", 17767 AllmanBraceStyle); 17768 17769 verifyFormat("void f()\n" 17770 "{\n" 17771 " for (int i = 0; i < 10; ++i)\n" 17772 " {\n" 17773 " a();\n" 17774 " }\n" 17775 " while (false)\n" 17776 " {\n" 17777 " b();\n" 17778 " }\n" 17779 " do\n" 17780 " {\n" 17781 " c();\n" 17782 " } while (false)\n" 17783 "}\n", 17784 AllmanBraceStyle); 17785 17786 verifyFormat("void f(int a)\n" 17787 "{\n" 17788 " switch (a)\n" 17789 " {\n" 17790 " case 0:\n" 17791 " break;\n" 17792 " case 1:\n" 17793 " {\n" 17794 " break;\n" 17795 " }\n" 17796 " case 2:\n" 17797 " {\n" 17798 " }\n" 17799 " break;\n" 17800 " default:\n" 17801 " break;\n" 17802 " }\n" 17803 "}\n", 17804 AllmanBraceStyle); 17805 17806 verifyFormat("enum X\n" 17807 "{\n" 17808 " Y = 0,\n" 17809 "}\n", 17810 AllmanBraceStyle); 17811 verifyFormat("enum X\n" 17812 "{\n" 17813 " Y = 0\n" 17814 "}\n", 17815 AllmanBraceStyle); 17816 17817 verifyFormat("@interface BSApplicationController ()\n" 17818 "{\n" 17819 "@private\n" 17820 " id _extraIvar;\n" 17821 "}\n" 17822 "@end\n", 17823 AllmanBraceStyle); 17824 17825 verifyFormat("#ifdef _DEBUG\n" 17826 "int foo(int i = 0)\n" 17827 "#else\n" 17828 "int foo(int i = 5)\n" 17829 "#endif\n" 17830 "{\n" 17831 " return i;\n" 17832 "}", 17833 AllmanBraceStyle); 17834 17835 verifyFormat("void foo() {}\n" 17836 "void bar()\n" 17837 "#ifdef _DEBUG\n" 17838 "{\n" 17839 " foo();\n" 17840 "}\n" 17841 "#else\n" 17842 "{\n" 17843 "}\n" 17844 "#endif", 17845 AllmanBraceStyle); 17846 17847 verifyFormat("void foobar() { int i = 5; }\n" 17848 "#ifdef _DEBUG\n" 17849 "void bar() {}\n" 17850 "#else\n" 17851 "void bar() { foobar(); }\n" 17852 "#endif", 17853 AllmanBraceStyle); 17854 17855 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 17856 FormatStyle::SLS_All); 17857 17858 verifyFormat("[](int i) { return i + 2; };\n" 17859 "[](int i, int j)\n" 17860 "{\n" 17861 " auto x = i + j;\n" 17862 " auto y = i * j;\n" 17863 " return x ^ y;\n" 17864 "};\n" 17865 "void foo()\n" 17866 "{\n" 17867 " auto shortLambda = [](int i) { return i + 2; };\n" 17868 " auto longLambda = [](int i, int j)\n" 17869 " {\n" 17870 " auto x = i + j;\n" 17871 " auto y = i * j;\n" 17872 " return x ^ y;\n" 17873 " };\n" 17874 "}", 17875 AllmanBraceStyle); 17876 17877 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17878 17879 verifyFormat("[](int i)\n" 17880 "{\n" 17881 " return i + 2;\n" 17882 "};\n" 17883 "[](int i, int j)\n" 17884 "{\n" 17885 " auto x = i + j;\n" 17886 " auto y = i * j;\n" 17887 " return x ^ y;\n" 17888 "};\n" 17889 "void foo()\n" 17890 "{\n" 17891 " auto shortLambda = [](int i)\n" 17892 " {\n" 17893 " return i + 2;\n" 17894 " };\n" 17895 " auto longLambda = [](int i, int j)\n" 17896 " {\n" 17897 " auto x = i + j;\n" 17898 " auto y = i * j;\n" 17899 " return x ^ y;\n" 17900 " };\n" 17901 "}", 17902 AllmanBraceStyle); 17903 17904 // Reset 17905 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 17906 17907 // This shouldn't affect ObjC blocks.. 17908 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17909 " // ...\n" 17910 " int i;\n" 17911 "}];", 17912 AllmanBraceStyle); 17913 verifyFormat("void (^block)(void) = ^{\n" 17914 " // ...\n" 17915 " int i;\n" 17916 "};", 17917 AllmanBraceStyle); 17918 // .. or dict literals. 17919 verifyFormat("void f()\n" 17920 "{\n" 17921 " // ...\n" 17922 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17923 "}", 17924 AllmanBraceStyle); 17925 verifyFormat("void f()\n" 17926 "{\n" 17927 " // ...\n" 17928 " [object someMethod:@{a : @\"b\"}];\n" 17929 "}", 17930 AllmanBraceStyle); 17931 verifyFormat("int f()\n" 17932 "{ // comment\n" 17933 " return 42;\n" 17934 "}", 17935 AllmanBraceStyle); 17936 17937 AllmanBraceStyle.ColumnLimit = 19; 17938 verifyFormat("void f() { int i; }", AllmanBraceStyle); 17939 AllmanBraceStyle.ColumnLimit = 18; 17940 verifyFormat("void f()\n" 17941 "{\n" 17942 " int i;\n" 17943 "}", 17944 AllmanBraceStyle); 17945 AllmanBraceStyle.ColumnLimit = 80; 17946 17947 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 17948 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17949 FormatStyle::SIS_WithoutElse; 17950 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17951 verifyFormat("void f(bool b)\n" 17952 "{\n" 17953 " if (b)\n" 17954 " {\n" 17955 " return;\n" 17956 " }\n" 17957 "}\n", 17958 BreakBeforeBraceShortIfs); 17959 verifyFormat("void f(bool b)\n" 17960 "{\n" 17961 " if constexpr (b)\n" 17962 " {\n" 17963 " return;\n" 17964 " }\n" 17965 "}\n", 17966 BreakBeforeBraceShortIfs); 17967 verifyFormat("void f(bool b)\n" 17968 "{\n" 17969 " if CONSTEXPR (b)\n" 17970 " {\n" 17971 " return;\n" 17972 " }\n" 17973 "}\n", 17974 BreakBeforeBraceShortIfs); 17975 verifyFormat("void f(bool b)\n" 17976 "{\n" 17977 " if (b) return;\n" 17978 "}\n", 17979 BreakBeforeBraceShortIfs); 17980 verifyFormat("void f(bool b)\n" 17981 "{\n" 17982 " if constexpr (b) return;\n" 17983 "}\n", 17984 BreakBeforeBraceShortIfs); 17985 verifyFormat("void f(bool b)\n" 17986 "{\n" 17987 " if CONSTEXPR (b) return;\n" 17988 "}\n", 17989 BreakBeforeBraceShortIfs); 17990 verifyFormat("void f(bool b)\n" 17991 "{\n" 17992 " while (b)\n" 17993 " {\n" 17994 " return;\n" 17995 " }\n" 17996 "}\n", 17997 BreakBeforeBraceShortIfs); 17998 } 17999 18000 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 18001 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0); 18002 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 18003 18004 // Make a few changes to the style for testing purposes 18005 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 18006 FormatStyle::SFS_Empty; 18007 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 18008 18009 // FIXME: this test case can't decide whether there should be a blank line 18010 // after the ~D() line or not. It adds one if one doesn't exist in the test 18011 // and it removes the line if one exists. 18012 /* 18013 verifyFormat("class A;\n" 18014 "namespace B\n" 18015 " {\n" 18016 "class C;\n" 18017 "// Comment\n" 18018 "class D\n" 18019 " {\n" 18020 "public:\n" 18021 " D();\n" 18022 " ~D() {}\n" 18023 "private:\n" 18024 " enum E\n" 18025 " {\n" 18026 " F\n" 18027 " }\n" 18028 " };\n" 18029 " } // namespace B\n", 18030 WhitesmithsBraceStyle); 18031 */ 18032 18033 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 18034 verifyFormat("namespace a\n" 18035 " {\n" 18036 "class A\n" 18037 " {\n" 18038 " void f()\n" 18039 " {\n" 18040 " if (true)\n" 18041 " {\n" 18042 " a();\n" 18043 " b();\n" 18044 " }\n" 18045 " }\n" 18046 " void g()\n" 18047 " {\n" 18048 " return;\n" 18049 " }\n" 18050 " };\n" 18051 "struct B\n" 18052 " {\n" 18053 " int x;\n" 18054 " };\n" 18055 " } // namespace a", 18056 WhitesmithsBraceStyle); 18057 18058 verifyFormat("namespace a\n" 18059 " {\n" 18060 "namespace b\n" 18061 " {\n" 18062 "class A\n" 18063 " {\n" 18064 " void f()\n" 18065 " {\n" 18066 " if (true)\n" 18067 " {\n" 18068 " a();\n" 18069 " b();\n" 18070 " }\n" 18071 " }\n" 18072 " void g()\n" 18073 " {\n" 18074 " return;\n" 18075 " }\n" 18076 " };\n" 18077 "struct B\n" 18078 " {\n" 18079 " int x;\n" 18080 " };\n" 18081 " } // namespace b\n" 18082 " } // namespace a", 18083 WhitesmithsBraceStyle); 18084 18085 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 18086 verifyFormat("namespace a\n" 18087 " {\n" 18088 "namespace b\n" 18089 " {\n" 18090 " class A\n" 18091 " {\n" 18092 " void f()\n" 18093 " {\n" 18094 " if (true)\n" 18095 " {\n" 18096 " a();\n" 18097 " b();\n" 18098 " }\n" 18099 " }\n" 18100 " void g()\n" 18101 " {\n" 18102 " return;\n" 18103 " }\n" 18104 " };\n" 18105 " struct B\n" 18106 " {\n" 18107 " int x;\n" 18108 " };\n" 18109 " } // namespace b\n" 18110 " } // namespace a", 18111 WhitesmithsBraceStyle); 18112 18113 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 18114 verifyFormat("namespace a\n" 18115 " {\n" 18116 " namespace b\n" 18117 " {\n" 18118 " class A\n" 18119 " {\n" 18120 " void f()\n" 18121 " {\n" 18122 " if (true)\n" 18123 " {\n" 18124 " a();\n" 18125 " b();\n" 18126 " }\n" 18127 " }\n" 18128 " void g()\n" 18129 " {\n" 18130 " return;\n" 18131 " }\n" 18132 " };\n" 18133 " struct B\n" 18134 " {\n" 18135 " int x;\n" 18136 " };\n" 18137 " } // namespace b\n" 18138 " } // namespace a", 18139 WhitesmithsBraceStyle); 18140 18141 verifyFormat("void f()\n" 18142 " {\n" 18143 " if (true)\n" 18144 " {\n" 18145 " a();\n" 18146 " }\n" 18147 " else if (false)\n" 18148 " {\n" 18149 " b();\n" 18150 " }\n" 18151 " else\n" 18152 " {\n" 18153 " c();\n" 18154 " }\n" 18155 " }\n", 18156 WhitesmithsBraceStyle); 18157 18158 verifyFormat("void f()\n" 18159 " {\n" 18160 " for (int i = 0; i < 10; ++i)\n" 18161 " {\n" 18162 " a();\n" 18163 " }\n" 18164 " while (false)\n" 18165 " {\n" 18166 " b();\n" 18167 " }\n" 18168 " do\n" 18169 " {\n" 18170 " c();\n" 18171 " } while (false)\n" 18172 " }\n", 18173 WhitesmithsBraceStyle); 18174 18175 WhitesmithsBraceStyle.IndentCaseLabels = true; 18176 verifyFormat("void switchTest1(int a)\n" 18177 " {\n" 18178 " switch (a)\n" 18179 " {\n" 18180 " case 2:\n" 18181 " {\n" 18182 " }\n" 18183 " break;\n" 18184 " }\n" 18185 " }\n", 18186 WhitesmithsBraceStyle); 18187 18188 verifyFormat("void switchTest2(int a)\n" 18189 " {\n" 18190 " switch (a)\n" 18191 " {\n" 18192 " case 0:\n" 18193 " break;\n" 18194 " case 1:\n" 18195 " {\n" 18196 " break;\n" 18197 " }\n" 18198 " case 2:\n" 18199 " {\n" 18200 " }\n" 18201 " break;\n" 18202 " default:\n" 18203 " break;\n" 18204 " }\n" 18205 " }\n", 18206 WhitesmithsBraceStyle); 18207 18208 verifyFormat("void switchTest3(int a)\n" 18209 " {\n" 18210 " switch (a)\n" 18211 " {\n" 18212 " case 0:\n" 18213 " {\n" 18214 " foo(x);\n" 18215 " }\n" 18216 " break;\n" 18217 " default:\n" 18218 " {\n" 18219 " foo(1);\n" 18220 " }\n" 18221 " break;\n" 18222 " }\n" 18223 " }\n", 18224 WhitesmithsBraceStyle); 18225 18226 WhitesmithsBraceStyle.IndentCaseLabels = false; 18227 18228 verifyFormat("void switchTest4(int a)\n" 18229 " {\n" 18230 " switch (a)\n" 18231 " {\n" 18232 " case 2:\n" 18233 " {\n" 18234 " }\n" 18235 " break;\n" 18236 " }\n" 18237 " }\n", 18238 WhitesmithsBraceStyle); 18239 18240 verifyFormat("void switchTest5(int a)\n" 18241 " {\n" 18242 " switch (a)\n" 18243 " {\n" 18244 " case 0:\n" 18245 " break;\n" 18246 " case 1:\n" 18247 " {\n" 18248 " foo();\n" 18249 " break;\n" 18250 " }\n" 18251 " case 2:\n" 18252 " {\n" 18253 " }\n" 18254 " break;\n" 18255 " default:\n" 18256 " break;\n" 18257 " }\n" 18258 " }\n", 18259 WhitesmithsBraceStyle); 18260 18261 verifyFormat("void switchTest6(int a)\n" 18262 " {\n" 18263 " switch (a)\n" 18264 " {\n" 18265 " case 0:\n" 18266 " {\n" 18267 " foo(x);\n" 18268 " }\n" 18269 " break;\n" 18270 " default:\n" 18271 " {\n" 18272 " foo(1);\n" 18273 " }\n" 18274 " break;\n" 18275 " }\n" 18276 " }\n", 18277 WhitesmithsBraceStyle); 18278 18279 verifyFormat("enum X\n" 18280 " {\n" 18281 " Y = 0, // testing\n" 18282 " }\n", 18283 WhitesmithsBraceStyle); 18284 18285 verifyFormat("enum X\n" 18286 " {\n" 18287 " Y = 0\n" 18288 " }\n", 18289 WhitesmithsBraceStyle); 18290 verifyFormat("enum X\n" 18291 " {\n" 18292 " Y = 0,\n" 18293 " Z = 1\n" 18294 " };\n", 18295 WhitesmithsBraceStyle); 18296 18297 verifyFormat("@interface BSApplicationController ()\n" 18298 " {\n" 18299 "@private\n" 18300 " id _extraIvar;\n" 18301 " }\n" 18302 "@end\n", 18303 WhitesmithsBraceStyle); 18304 18305 verifyFormat("#ifdef _DEBUG\n" 18306 "int foo(int i = 0)\n" 18307 "#else\n" 18308 "int foo(int i = 5)\n" 18309 "#endif\n" 18310 " {\n" 18311 " return i;\n" 18312 " }", 18313 WhitesmithsBraceStyle); 18314 18315 verifyFormat("void foo() {}\n" 18316 "void bar()\n" 18317 "#ifdef _DEBUG\n" 18318 " {\n" 18319 " foo();\n" 18320 " }\n" 18321 "#else\n" 18322 " {\n" 18323 " }\n" 18324 "#endif", 18325 WhitesmithsBraceStyle); 18326 18327 verifyFormat("void foobar()\n" 18328 " {\n" 18329 " int i = 5;\n" 18330 " }\n" 18331 "#ifdef _DEBUG\n" 18332 "void bar()\n" 18333 " {\n" 18334 " }\n" 18335 "#else\n" 18336 "void bar()\n" 18337 " {\n" 18338 " foobar();\n" 18339 " }\n" 18340 "#endif", 18341 WhitesmithsBraceStyle); 18342 18343 // This shouldn't affect ObjC blocks.. 18344 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 18345 " // ...\n" 18346 " int i;\n" 18347 "}];", 18348 WhitesmithsBraceStyle); 18349 verifyFormat("void (^block)(void) = ^{\n" 18350 " // ...\n" 18351 " int i;\n" 18352 "};", 18353 WhitesmithsBraceStyle); 18354 // .. or dict literals. 18355 verifyFormat("void f()\n" 18356 " {\n" 18357 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 18358 " }", 18359 WhitesmithsBraceStyle); 18360 18361 verifyFormat("int f()\n" 18362 " { // comment\n" 18363 " return 42;\n" 18364 " }", 18365 WhitesmithsBraceStyle); 18366 18367 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 18368 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 18369 FormatStyle::SIS_OnlyFirstIf; 18370 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 18371 verifyFormat("void f(bool b)\n" 18372 " {\n" 18373 " if (b)\n" 18374 " {\n" 18375 " return;\n" 18376 " }\n" 18377 " }\n", 18378 BreakBeforeBraceShortIfs); 18379 verifyFormat("void f(bool b)\n" 18380 " {\n" 18381 " if (b) return;\n" 18382 " }\n", 18383 BreakBeforeBraceShortIfs); 18384 verifyFormat("void f(bool b)\n" 18385 " {\n" 18386 " while (b)\n" 18387 " {\n" 18388 " return;\n" 18389 " }\n" 18390 " }\n", 18391 BreakBeforeBraceShortIfs); 18392 } 18393 18394 TEST_F(FormatTest, GNUBraceBreaking) { 18395 FormatStyle GNUBraceStyle = getLLVMStyle(); 18396 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 18397 verifyFormat("namespace a\n" 18398 "{\n" 18399 "class A\n" 18400 "{\n" 18401 " void f()\n" 18402 " {\n" 18403 " int a;\n" 18404 " {\n" 18405 " int b;\n" 18406 " }\n" 18407 " if (true)\n" 18408 " {\n" 18409 " a();\n" 18410 " b();\n" 18411 " }\n" 18412 " }\n" 18413 " void g() { return; }\n" 18414 "}\n" 18415 "} // namespace a", 18416 GNUBraceStyle); 18417 18418 verifyFormat("void f()\n" 18419 "{\n" 18420 " if (true)\n" 18421 " {\n" 18422 " a();\n" 18423 " }\n" 18424 " else if (false)\n" 18425 " {\n" 18426 " b();\n" 18427 " }\n" 18428 " else\n" 18429 " {\n" 18430 " c();\n" 18431 " }\n" 18432 "}\n", 18433 GNUBraceStyle); 18434 18435 verifyFormat("void f()\n" 18436 "{\n" 18437 " for (int i = 0; i < 10; ++i)\n" 18438 " {\n" 18439 " a();\n" 18440 " }\n" 18441 " while (false)\n" 18442 " {\n" 18443 " b();\n" 18444 " }\n" 18445 " do\n" 18446 " {\n" 18447 " c();\n" 18448 " }\n" 18449 " while (false);\n" 18450 "}\n", 18451 GNUBraceStyle); 18452 18453 verifyFormat("void f(int a)\n" 18454 "{\n" 18455 " switch (a)\n" 18456 " {\n" 18457 " case 0:\n" 18458 " break;\n" 18459 " case 1:\n" 18460 " {\n" 18461 " break;\n" 18462 " }\n" 18463 " case 2:\n" 18464 " {\n" 18465 " }\n" 18466 " break;\n" 18467 " default:\n" 18468 " break;\n" 18469 " }\n" 18470 "}\n", 18471 GNUBraceStyle); 18472 18473 verifyFormat("enum X\n" 18474 "{\n" 18475 " Y = 0,\n" 18476 "}\n", 18477 GNUBraceStyle); 18478 18479 verifyFormat("@interface BSApplicationController ()\n" 18480 "{\n" 18481 "@private\n" 18482 " id _extraIvar;\n" 18483 "}\n" 18484 "@end\n", 18485 GNUBraceStyle); 18486 18487 verifyFormat("#ifdef _DEBUG\n" 18488 "int foo(int i = 0)\n" 18489 "#else\n" 18490 "int foo(int i = 5)\n" 18491 "#endif\n" 18492 "{\n" 18493 " return i;\n" 18494 "}", 18495 GNUBraceStyle); 18496 18497 verifyFormat("void foo() {}\n" 18498 "void bar()\n" 18499 "#ifdef _DEBUG\n" 18500 "{\n" 18501 " foo();\n" 18502 "}\n" 18503 "#else\n" 18504 "{\n" 18505 "}\n" 18506 "#endif", 18507 GNUBraceStyle); 18508 18509 verifyFormat("void foobar() { int i = 5; }\n" 18510 "#ifdef _DEBUG\n" 18511 "void bar() {}\n" 18512 "#else\n" 18513 "void bar() { foobar(); }\n" 18514 "#endif", 18515 GNUBraceStyle); 18516 } 18517 18518 TEST_F(FormatTest, WebKitBraceBreaking) { 18519 FormatStyle WebKitBraceStyle = getLLVMStyle(); 18520 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 18521 WebKitBraceStyle.FixNamespaceComments = false; 18522 verifyFormat("namespace a {\n" 18523 "class A {\n" 18524 " void f()\n" 18525 " {\n" 18526 " if (true) {\n" 18527 " a();\n" 18528 " b();\n" 18529 " }\n" 18530 " }\n" 18531 " void g() { return; }\n" 18532 "};\n" 18533 "enum E {\n" 18534 " A,\n" 18535 " // foo\n" 18536 " B,\n" 18537 " C\n" 18538 "};\n" 18539 "struct B {\n" 18540 " int x;\n" 18541 "};\n" 18542 "}\n", 18543 WebKitBraceStyle); 18544 verifyFormat("struct S {\n" 18545 " int Type;\n" 18546 " union {\n" 18547 " int x;\n" 18548 " double y;\n" 18549 " } Value;\n" 18550 " class C {\n" 18551 " MyFavoriteType Value;\n" 18552 " } Class;\n" 18553 "};\n", 18554 WebKitBraceStyle); 18555 } 18556 18557 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 18558 verifyFormat("void f() {\n" 18559 " try {\n" 18560 " } catch (const Exception &e) {\n" 18561 " }\n" 18562 "}\n", 18563 getLLVMStyle()); 18564 } 18565 18566 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 18567 auto Style = getLLVMStyle(); 18568 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18569 Style.AlignConsecutiveAssignments = 18570 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18571 Style.AlignConsecutiveDeclarations = 18572 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18573 verifyFormat("struct test demo[] = {\n" 18574 " {56, 23, \"hello\"},\n" 18575 " {-1, 93463, \"world\"},\n" 18576 " { 7, 5, \"!!\"}\n" 18577 "};\n", 18578 Style); 18579 18580 verifyFormat("struct test demo[] = {\n" 18581 " {56, 23, \"hello\"}, // first line\n" 18582 " {-1, 93463, \"world\"}, // second line\n" 18583 " { 7, 5, \"!!\"} // third line\n" 18584 "};\n", 18585 Style); 18586 18587 verifyFormat("struct test demo[4] = {\n" 18588 " { 56, 23, 21, \"oh\"}, // first line\n" 18589 " { -1, 93463, 22, \"my\"}, // second line\n" 18590 " { 7, 5, 1, \"goodness\"} // third line\n" 18591 " {234, 5, 1, \"gracious\"} // fourth line\n" 18592 "};\n", 18593 Style); 18594 18595 verifyFormat("struct test demo[3] = {\n" 18596 " {56, 23, \"hello\"},\n" 18597 " {-1, 93463, \"world\"},\n" 18598 " { 7, 5, \"!!\"}\n" 18599 "};\n", 18600 Style); 18601 18602 verifyFormat("struct test demo[3] = {\n" 18603 " {int{56}, 23, \"hello\"},\n" 18604 " {int{-1}, 93463, \"world\"},\n" 18605 " { int{7}, 5, \"!!\"}\n" 18606 "};\n", 18607 Style); 18608 18609 verifyFormat("struct test demo[] = {\n" 18610 " {56, 23, \"hello\"},\n" 18611 " {-1, 93463, \"world\"},\n" 18612 " { 7, 5, \"!!\"},\n" 18613 "};\n", 18614 Style); 18615 18616 verifyFormat("test demo[] = {\n" 18617 " {56, 23, \"hello\"},\n" 18618 " {-1, 93463, \"world\"},\n" 18619 " { 7, 5, \"!!\"},\n" 18620 "};\n", 18621 Style); 18622 18623 verifyFormat("demo = std::array<struct test, 3>{\n" 18624 " test{56, 23, \"hello\"},\n" 18625 " test{-1, 93463, \"world\"},\n" 18626 " test{ 7, 5, \"!!\"},\n" 18627 "};\n", 18628 Style); 18629 18630 verifyFormat("test demo[] = {\n" 18631 " {56, 23, \"hello\"},\n" 18632 "#if X\n" 18633 " {-1, 93463, \"world\"},\n" 18634 "#endif\n" 18635 " { 7, 5, \"!!\"}\n" 18636 "};\n", 18637 Style); 18638 18639 verifyFormat( 18640 "test demo[] = {\n" 18641 " { 7, 23,\n" 18642 " \"hello world i am a very long line that really, in any\"\n" 18643 " \"just world, ought to be split over multiple lines\"},\n" 18644 " {-1, 93463, \"world\"},\n" 18645 " {56, 5, \"!!\"}\n" 18646 "};\n", 18647 Style); 18648 18649 verifyFormat("return GradForUnaryCwise(g, {\n" 18650 " {{\"sign\"}, \"Sign\", " 18651 " {\"x\", \"dy\"}},\n" 18652 " { {\"dx\"}, \"Mul\", {\"dy\"" 18653 ", \"sign\"}},\n" 18654 "});\n", 18655 Style); 18656 18657 Style.ColumnLimit = 0; 18658 EXPECT_EQ( 18659 "test demo[] = {\n" 18660 " {56, 23, \"hello world i am a very long line that really, " 18661 "in any just world, ought to be split over multiple lines\"},\n" 18662 " {-1, 93463, " 18663 " \"world\"},\n" 18664 " { 7, 5, " 18665 " \"!!\"},\n" 18666 "};", 18667 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18668 "that really, in any just world, ought to be split over multiple " 18669 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18670 Style)); 18671 18672 Style.ColumnLimit = 80; 18673 verifyFormat("test demo[] = {\n" 18674 " {56, 23, /* a comment */ \"hello\"},\n" 18675 " {-1, 93463, \"world\"},\n" 18676 " { 7, 5, \"!!\"}\n" 18677 "};\n", 18678 Style); 18679 18680 verifyFormat("test demo[] = {\n" 18681 " {56, 23, \"hello\"},\n" 18682 " {-1, 93463, \"world\" /* comment here */},\n" 18683 " { 7, 5, \"!!\"}\n" 18684 "};\n", 18685 Style); 18686 18687 verifyFormat("test demo[] = {\n" 18688 " {56, /* a comment */ 23, \"hello\"},\n" 18689 " {-1, 93463, \"world\"},\n" 18690 " { 7, 5, \"!!\"}\n" 18691 "};\n", 18692 Style); 18693 18694 Style.ColumnLimit = 20; 18695 EXPECT_EQ( 18696 "demo = std::array<\n" 18697 " struct test, 3>{\n" 18698 " test{\n" 18699 " 56, 23,\n" 18700 " \"hello \"\n" 18701 " \"world i \"\n" 18702 " \"am a very \"\n" 18703 " \"long line \"\n" 18704 " \"that \"\n" 18705 " \"really, \"\n" 18706 " \"in any \"\n" 18707 " \"just \"\n" 18708 " \"world, \"\n" 18709 " \"ought to \"\n" 18710 " \"be split \"\n" 18711 " \"over \"\n" 18712 " \"multiple \"\n" 18713 " \"lines\"},\n" 18714 " test{-1, 93463,\n" 18715 " \"world\"},\n" 18716 " test{ 7, 5,\n" 18717 " \"!!\" },\n" 18718 "};", 18719 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18720 "i am a very long line that really, in any just world, ought " 18721 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18722 "test{7, 5, \"!!\"},};", 18723 Style)); 18724 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18725 Style = getLLVMStyleWithColumns(50); 18726 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18727 verifyFormat("static A x = {\n" 18728 " {{init1, init2, init3, init4},\n" 18729 " {init1, init2, init3, init4}}\n" 18730 "};", 18731 Style); 18732 Style.ColumnLimit = 100; 18733 EXPECT_EQ( 18734 "test demo[] = {\n" 18735 " {56, 23,\n" 18736 " \"hello world i am a very long line that really, in any just world" 18737 ", ought to be split over \"\n" 18738 " \"multiple lines\" },\n" 18739 " {-1, 93463, \"world\"},\n" 18740 " { 7, 5, \"!!\"},\n" 18741 "};", 18742 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18743 "that really, in any just world, ought to be split over multiple " 18744 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18745 Style)); 18746 18747 Style = getLLVMStyleWithColumns(50); 18748 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18749 Style.AlignConsecutiveAssignments = 18750 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18751 Style.AlignConsecutiveDeclarations = 18752 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18753 verifyFormat("struct test demo[] = {\n" 18754 " {56, 23, \"hello\"},\n" 18755 " {-1, 93463, \"world\"},\n" 18756 " { 7, 5, \"!!\"}\n" 18757 "};\n" 18758 "static A x = {\n" 18759 " {{init1, init2, init3, init4},\n" 18760 " {init1, init2, init3, init4}}\n" 18761 "};", 18762 Style); 18763 Style.ColumnLimit = 100; 18764 Style.AlignConsecutiveAssignments = 18765 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18766 Style.AlignConsecutiveDeclarations = 18767 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18768 verifyFormat("struct test demo[] = {\n" 18769 " {56, 23, \"hello\"},\n" 18770 " {-1, 93463, \"world\"},\n" 18771 " { 7, 5, \"!!\"}\n" 18772 "};\n" 18773 "struct test demo[4] = {\n" 18774 " { 56, 23, 21, \"oh\"}, // first line\n" 18775 " { -1, 93463, 22, \"my\"}, // second line\n" 18776 " { 7, 5, 1, \"goodness\"} // third line\n" 18777 " {234, 5, 1, \"gracious\"} // fourth line\n" 18778 "};\n", 18779 Style); 18780 EXPECT_EQ( 18781 "test demo[] = {\n" 18782 " {56,\n" 18783 " \"hello world i am a very long line that really, in any just world" 18784 ", ought to be split over \"\n" 18785 " \"multiple lines\", 23},\n" 18786 " {-1, \"world\", 93463},\n" 18787 " { 7, \"!!\", 5},\n" 18788 "};", 18789 format("test demo[] = {{56, \"hello world i am a very long line " 18790 "that really, in any just world, ought to be split over multiple " 18791 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 18792 Style)); 18793 } 18794 18795 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 18796 auto Style = getLLVMStyle(); 18797 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18798 /* FIXME: This case gets misformatted. 18799 verifyFormat("auto foo = Items{\n" 18800 " Section{0, bar(), },\n" 18801 " Section{1, boo() }\n" 18802 "};\n", 18803 Style); 18804 */ 18805 verifyFormat("auto foo = Items{\n" 18806 " Section{\n" 18807 " 0, bar(),\n" 18808 " }\n" 18809 "};\n", 18810 Style); 18811 verifyFormat("struct test demo[] = {\n" 18812 " {56, 23, \"hello\"},\n" 18813 " {-1, 93463, \"world\"},\n" 18814 " {7, 5, \"!!\" }\n" 18815 "};\n", 18816 Style); 18817 verifyFormat("struct test demo[] = {\n" 18818 " {56, 23, \"hello\"}, // first line\n" 18819 " {-1, 93463, \"world\"}, // second line\n" 18820 " {7, 5, \"!!\" } // third line\n" 18821 "};\n", 18822 Style); 18823 verifyFormat("struct test demo[4] = {\n" 18824 " {56, 23, 21, \"oh\" }, // first line\n" 18825 " {-1, 93463, 22, \"my\" }, // second line\n" 18826 " {7, 5, 1, \"goodness\"} // third line\n" 18827 " {234, 5, 1, \"gracious\"} // fourth line\n" 18828 "};\n", 18829 Style); 18830 verifyFormat("struct test demo[3] = {\n" 18831 " {56, 23, \"hello\"},\n" 18832 " {-1, 93463, \"world\"},\n" 18833 " {7, 5, \"!!\" }\n" 18834 "};\n", 18835 Style); 18836 18837 verifyFormat("struct test demo[3] = {\n" 18838 " {int{56}, 23, \"hello\"},\n" 18839 " {int{-1}, 93463, \"world\"},\n" 18840 " {int{7}, 5, \"!!\" }\n" 18841 "};\n", 18842 Style); 18843 verifyFormat("struct test demo[] = {\n" 18844 " {56, 23, \"hello\"},\n" 18845 " {-1, 93463, \"world\"},\n" 18846 " {7, 5, \"!!\" },\n" 18847 "};\n", 18848 Style); 18849 verifyFormat("test demo[] = {\n" 18850 " {56, 23, \"hello\"},\n" 18851 " {-1, 93463, \"world\"},\n" 18852 " {7, 5, \"!!\" },\n" 18853 "};\n", 18854 Style); 18855 verifyFormat("demo = std::array<struct test, 3>{\n" 18856 " test{56, 23, \"hello\"},\n" 18857 " test{-1, 93463, \"world\"},\n" 18858 " test{7, 5, \"!!\" },\n" 18859 "};\n", 18860 Style); 18861 verifyFormat("test demo[] = {\n" 18862 " {56, 23, \"hello\"},\n" 18863 "#if X\n" 18864 " {-1, 93463, \"world\"},\n" 18865 "#endif\n" 18866 " {7, 5, \"!!\" }\n" 18867 "};\n", 18868 Style); 18869 verifyFormat( 18870 "test demo[] = {\n" 18871 " {7, 23,\n" 18872 " \"hello world i am a very long line that really, in any\"\n" 18873 " \"just world, ought to be split over multiple lines\"},\n" 18874 " {-1, 93463, \"world\" },\n" 18875 " {56, 5, \"!!\" }\n" 18876 "};\n", 18877 Style); 18878 18879 verifyFormat("return GradForUnaryCwise(g, {\n" 18880 " {{\"sign\"}, \"Sign\", {\"x\", " 18881 "\"dy\"} },\n" 18882 " {{\"dx\"}, \"Mul\", " 18883 "{\"dy\", \"sign\"}},\n" 18884 "});\n", 18885 Style); 18886 18887 Style.ColumnLimit = 0; 18888 EXPECT_EQ( 18889 "test demo[] = {\n" 18890 " {56, 23, \"hello world i am a very long line that really, in any " 18891 "just world, ought to be split over multiple lines\"},\n" 18892 " {-1, 93463, \"world\" " 18893 " },\n" 18894 " {7, 5, \"!!\" " 18895 " },\n" 18896 "};", 18897 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18898 "that really, in any just world, ought to be split over multiple " 18899 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18900 Style)); 18901 18902 Style.ColumnLimit = 80; 18903 verifyFormat("test demo[] = {\n" 18904 " {56, 23, /* a comment */ \"hello\"},\n" 18905 " {-1, 93463, \"world\" },\n" 18906 " {7, 5, \"!!\" }\n" 18907 "};\n", 18908 Style); 18909 18910 verifyFormat("test demo[] = {\n" 18911 " {56, 23, \"hello\" },\n" 18912 " {-1, 93463, \"world\" /* comment here */},\n" 18913 " {7, 5, \"!!\" }\n" 18914 "};\n", 18915 Style); 18916 18917 verifyFormat("test demo[] = {\n" 18918 " {56, /* a comment */ 23, \"hello\"},\n" 18919 " {-1, 93463, \"world\"},\n" 18920 " {7, 5, \"!!\" }\n" 18921 "};\n", 18922 Style); 18923 18924 Style.ColumnLimit = 20; 18925 EXPECT_EQ( 18926 "demo = std::array<\n" 18927 " struct test, 3>{\n" 18928 " test{\n" 18929 " 56, 23,\n" 18930 " \"hello \"\n" 18931 " \"world i \"\n" 18932 " \"am a very \"\n" 18933 " \"long line \"\n" 18934 " \"that \"\n" 18935 " \"really, \"\n" 18936 " \"in any \"\n" 18937 " \"just \"\n" 18938 " \"world, \"\n" 18939 " \"ought to \"\n" 18940 " \"be split \"\n" 18941 " \"over \"\n" 18942 " \"multiple \"\n" 18943 " \"lines\"},\n" 18944 " test{-1, 93463,\n" 18945 " \"world\"},\n" 18946 " test{7, 5,\n" 18947 " \"!!\" },\n" 18948 "};", 18949 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18950 "i am a very long line that really, in any just world, ought " 18951 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18952 "test{7, 5, \"!!\"},};", 18953 Style)); 18954 18955 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18956 Style = getLLVMStyleWithColumns(50); 18957 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18958 verifyFormat("static A x = {\n" 18959 " {{init1, init2, init3, init4},\n" 18960 " {init1, init2, init3, init4}}\n" 18961 "};", 18962 Style); 18963 Style.ColumnLimit = 100; 18964 EXPECT_EQ( 18965 "test demo[] = {\n" 18966 " {56, 23,\n" 18967 " \"hello world i am a very long line that really, in any just world" 18968 ", ought to be split over \"\n" 18969 " \"multiple lines\" },\n" 18970 " {-1, 93463, \"world\"},\n" 18971 " {7, 5, \"!!\" },\n" 18972 "};", 18973 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18974 "that really, in any just world, ought to be split over multiple " 18975 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18976 Style)); 18977 } 18978 18979 TEST_F(FormatTest, UnderstandsPragmas) { 18980 verifyFormat("#pragma omp reduction(| : var)"); 18981 verifyFormat("#pragma omp reduction(+ : var)"); 18982 18983 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 18984 "(including parentheses).", 18985 format("#pragma mark Any non-hyphenated or hyphenated string " 18986 "(including parentheses).")); 18987 } 18988 18989 TEST_F(FormatTest, UnderstandPragmaOption) { 18990 verifyFormat("#pragma option -C -A"); 18991 18992 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 18993 } 18994 18995 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 18996 FormatStyle Style = getLLVMStyleWithColumns(20); 18997 18998 // See PR41213 18999 EXPECT_EQ("/*\n" 19000 " *\t9012345\n" 19001 " * /8901\n" 19002 " */", 19003 format("/*\n" 19004 " *\t9012345 /8901\n" 19005 " */", 19006 Style)); 19007 EXPECT_EQ("/*\n" 19008 " *345678\n" 19009 " *\t/8901\n" 19010 " */", 19011 format("/*\n" 19012 " *345678\t/8901\n" 19013 " */", 19014 Style)); 19015 19016 verifyFormat("int a; // the\n" 19017 " // comment", 19018 Style); 19019 EXPECT_EQ("int a; /* first line\n" 19020 " * second\n" 19021 " * line third\n" 19022 " * line\n" 19023 " */", 19024 format("int a; /* first line\n" 19025 " * second\n" 19026 " * line third\n" 19027 " * line\n" 19028 " */", 19029 Style)); 19030 EXPECT_EQ("int a; // first line\n" 19031 " // second\n" 19032 " // line third\n" 19033 " // line", 19034 format("int a; // first line\n" 19035 " // second line\n" 19036 " // third line", 19037 Style)); 19038 19039 Style.PenaltyExcessCharacter = 90; 19040 verifyFormat("int a; // the comment", Style); 19041 EXPECT_EQ("int a; // the comment\n" 19042 " // aaa", 19043 format("int a; // the comment aaa", Style)); 19044 EXPECT_EQ("int a; /* first line\n" 19045 " * second line\n" 19046 " * third line\n" 19047 " */", 19048 format("int a; /* first line\n" 19049 " * second line\n" 19050 " * third line\n" 19051 " */", 19052 Style)); 19053 EXPECT_EQ("int a; // first line\n" 19054 " // second line\n" 19055 " // third line", 19056 format("int a; // first line\n" 19057 " // second line\n" 19058 " // third line", 19059 Style)); 19060 // FIXME: Investigate why this is not getting the same layout as the test 19061 // above. 19062 EXPECT_EQ("int a; /* first line\n" 19063 " * second line\n" 19064 " * third line\n" 19065 " */", 19066 format("int a; /* first line second line third line" 19067 "\n*/", 19068 Style)); 19069 19070 EXPECT_EQ("// foo bar baz bazfoo\n" 19071 "// foo bar foo bar\n", 19072 format("// foo bar baz bazfoo\n" 19073 "// foo bar foo bar\n", 19074 Style)); 19075 EXPECT_EQ("// foo bar baz bazfoo\n" 19076 "// foo bar foo bar\n", 19077 format("// foo bar baz bazfoo\n" 19078 "// foo bar foo bar\n", 19079 Style)); 19080 19081 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 19082 // next one. 19083 EXPECT_EQ("// foo bar baz bazfoo\n" 19084 "// bar foo bar\n", 19085 format("// foo bar baz bazfoo bar\n" 19086 "// foo bar\n", 19087 Style)); 19088 19089 EXPECT_EQ("// foo bar baz bazfoo\n" 19090 "// foo bar baz bazfoo\n" 19091 "// bar foo bar\n", 19092 format("// foo bar baz bazfoo\n" 19093 "// foo bar baz bazfoo bar\n" 19094 "// foo bar\n", 19095 Style)); 19096 19097 EXPECT_EQ("// foo bar baz bazfoo\n" 19098 "// foo bar baz bazfoo\n" 19099 "// bar foo bar\n", 19100 format("// foo bar baz bazfoo\n" 19101 "// foo bar baz bazfoo bar\n" 19102 "// foo bar\n", 19103 Style)); 19104 19105 // Make sure we do not keep protruding characters if strict mode reflow is 19106 // cheaper than keeping protruding characters. 19107 Style.ColumnLimit = 21; 19108 EXPECT_EQ( 19109 "// foo foo foo foo\n" 19110 "// foo foo foo foo\n" 19111 "// foo foo foo foo\n", 19112 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 19113 19114 EXPECT_EQ("int a = /* long block\n" 19115 " comment */\n" 19116 " 42;", 19117 format("int a = /* long block comment */ 42;", Style)); 19118 } 19119 19120 TEST_F(FormatTest, BreakPenaltyAfterLParen) { 19121 FormatStyle Style = getLLVMStyle(); 19122 Style.ColumnLimit = 8; 19123 Style.PenaltyExcessCharacter = 15; 19124 verifyFormat("int foo(\n" 19125 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 19126 Style); 19127 Style.PenaltyBreakOpenParenthesis = 200; 19128 EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", 19129 format("int foo(\n" 19130 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 19131 Style)); 19132 } 19133 19134 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { 19135 FormatStyle Style = getLLVMStyle(); 19136 Style.ColumnLimit = 5; 19137 Style.PenaltyExcessCharacter = 150; 19138 verifyFormat("foo((\n" 19139 " int)aaaaaaaaaaaaaaaaaaaaaaaa);", 19140 19141 Style); 19142 Style.PenaltyBreakOpenParenthesis = 100000; 19143 EXPECT_EQ("foo((int)\n" 19144 " aaaaaaaaaaaaaaaaaaaaaaaa);", 19145 format("foo((\n" 19146 "int)aaaaaaaaaaaaaaaaaaaaaaaa);", 19147 Style)); 19148 } 19149 19150 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { 19151 FormatStyle Style = getLLVMStyle(); 19152 Style.ColumnLimit = 4; 19153 Style.PenaltyExcessCharacter = 100; 19154 verifyFormat("for (\n" 19155 " int iiiiiiiiiiiiiiiii =\n" 19156 " 0;\n" 19157 " iiiiiiiiiiiiiiiii <\n" 19158 " 2;\n" 19159 " iiiiiiiiiiiiiiiii++) {\n" 19160 "}", 19161 19162 Style); 19163 Style.PenaltyBreakOpenParenthesis = 1250; 19164 EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n" 19165 " 0;\n" 19166 " iiiiiiiiiiiiiiiii <\n" 19167 " 2;\n" 19168 " iiiiiiiiiiiiiiiii++) {\n" 19169 "}", 19170 format("for (\n" 19171 " int iiiiiiiiiiiiiiiii =\n" 19172 " 0;\n" 19173 " iiiiiiiiiiiiiiiii <\n" 19174 " 2;\n" 19175 " iiiiiiiiiiiiiiiii++) {\n" 19176 "}", 19177 Style)); 19178 } 19179 19180 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 19181 for (size_t i = 1; i < Styles.size(); ++i) \ 19182 EXPECT_EQ(Styles[0], Styles[i]) \ 19183 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 19184 19185 TEST_F(FormatTest, GetsPredefinedStyleByName) { 19186 SmallVector<FormatStyle, 3> Styles; 19187 Styles.resize(3); 19188 19189 Styles[0] = getLLVMStyle(); 19190 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 19191 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 19192 EXPECT_ALL_STYLES_EQUAL(Styles); 19193 19194 Styles[0] = getGoogleStyle(); 19195 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 19196 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 19197 EXPECT_ALL_STYLES_EQUAL(Styles); 19198 19199 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 19200 EXPECT_TRUE( 19201 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 19202 EXPECT_TRUE( 19203 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 19204 EXPECT_ALL_STYLES_EQUAL(Styles); 19205 19206 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 19207 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 19208 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 19209 EXPECT_ALL_STYLES_EQUAL(Styles); 19210 19211 Styles[0] = getMozillaStyle(); 19212 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 19213 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 19214 EXPECT_ALL_STYLES_EQUAL(Styles); 19215 19216 Styles[0] = getWebKitStyle(); 19217 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 19218 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 19219 EXPECT_ALL_STYLES_EQUAL(Styles); 19220 19221 Styles[0] = getGNUStyle(); 19222 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 19223 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 19224 EXPECT_ALL_STYLES_EQUAL(Styles); 19225 19226 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 19227 } 19228 19229 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 19230 SmallVector<FormatStyle, 8> Styles; 19231 Styles.resize(2); 19232 19233 Styles[0] = getGoogleStyle(); 19234 Styles[1] = getLLVMStyle(); 19235 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 19236 EXPECT_ALL_STYLES_EQUAL(Styles); 19237 19238 Styles.resize(5); 19239 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 19240 Styles[1] = getLLVMStyle(); 19241 Styles[1].Language = FormatStyle::LK_JavaScript; 19242 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 19243 19244 Styles[2] = getLLVMStyle(); 19245 Styles[2].Language = FormatStyle::LK_JavaScript; 19246 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 19247 "BasedOnStyle: Google", 19248 &Styles[2]) 19249 .value()); 19250 19251 Styles[3] = getLLVMStyle(); 19252 Styles[3].Language = FormatStyle::LK_JavaScript; 19253 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 19254 "Language: JavaScript", 19255 &Styles[3]) 19256 .value()); 19257 19258 Styles[4] = getLLVMStyle(); 19259 Styles[4].Language = FormatStyle::LK_JavaScript; 19260 EXPECT_EQ(0, parseConfiguration("---\n" 19261 "BasedOnStyle: LLVM\n" 19262 "IndentWidth: 123\n" 19263 "---\n" 19264 "BasedOnStyle: Google\n" 19265 "Language: JavaScript", 19266 &Styles[4]) 19267 .value()); 19268 EXPECT_ALL_STYLES_EQUAL(Styles); 19269 } 19270 19271 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 19272 Style.FIELD = false; \ 19273 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 19274 EXPECT_TRUE(Style.FIELD); \ 19275 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 19276 EXPECT_FALSE(Style.FIELD); 19277 19278 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 19279 19280 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 19281 Style.STRUCT.FIELD = false; \ 19282 EXPECT_EQ(0, \ 19283 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 19284 .value()); \ 19285 EXPECT_TRUE(Style.STRUCT.FIELD); \ 19286 EXPECT_EQ(0, \ 19287 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 19288 .value()); \ 19289 EXPECT_FALSE(Style.STRUCT.FIELD); 19290 19291 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 19292 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 19293 19294 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 19295 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 19296 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 19297 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 19298 19299 TEST_F(FormatTest, ParsesConfigurationBools) { 19300 FormatStyle Style = {}; 19301 Style.Language = FormatStyle::LK_Cpp; 19302 CHECK_PARSE_BOOL(AlignTrailingComments); 19303 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 19304 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 19305 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 19306 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 19307 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 19308 CHECK_PARSE_BOOL(BinPackArguments); 19309 CHECK_PARSE_BOOL(BinPackParameters); 19310 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 19311 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 19312 CHECK_PARSE_BOOL(BreakStringLiterals); 19313 CHECK_PARSE_BOOL(CompactNamespaces); 19314 CHECK_PARSE_BOOL(DeriveLineEnding); 19315 CHECK_PARSE_BOOL(DerivePointerAlignment); 19316 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 19317 CHECK_PARSE_BOOL(DisableFormat); 19318 CHECK_PARSE_BOOL(IndentAccessModifiers); 19319 CHECK_PARSE_BOOL(IndentCaseLabels); 19320 CHECK_PARSE_BOOL(IndentCaseBlocks); 19321 CHECK_PARSE_BOOL(IndentGotoLabels); 19322 CHECK_PARSE_BOOL_FIELD(IndentRequiresClause, "IndentRequires"); 19323 CHECK_PARSE_BOOL(IndentRequiresClause); 19324 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 19325 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 19326 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 19327 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 19328 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 19329 CHECK_PARSE_BOOL(ReflowComments); 19330 CHECK_PARSE_BOOL(RemoveBracesLLVM); 19331 CHECK_PARSE_BOOL(SortUsingDeclarations); 19332 CHECK_PARSE_BOOL(SpacesInParentheses); 19333 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 19334 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 19335 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 19336 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 19337 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 19338 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 19339 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 19340 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 19341 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 19342 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 19343 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 19344 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 19345 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 19346 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 19347 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 19348 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 19349 CHECK_PARSE_BOOL(UseCRLF); 19350 19351 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 19352 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 19353 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 19354 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 19355 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 19356 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 19357 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 19358 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 19359 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 19360 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 19361 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 19362 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 19363 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 19364 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 19365 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 19366 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 19367 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 19368 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements); 19369 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros); 19370 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19371 AfterFunctionDeclarationName); 19372 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19373 AfterFunctionDefinitionName); 19374 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros); 19375 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator); 19376 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses); 19377 } 19378 19379 #undef CHECK_PARSE_BOOL 19380 19381 TEST_F(FormatTest, ParsesConfiguration) { 19382 FormatStyle Style = {}; 19383 Style.Language = FormatStyle::LK_Cpp; 19384 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 19385 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 19386 ConstructorInitializerIndentWidth, 1234u); 19387 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 19388 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 19389 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 19390 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 19391 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 19392 PenaltyBreakBeforeFirstCallParameter, 1234u); 19393 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 19394 PenaltyBreakTemplateDeclaration, 1234u); 19395 CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis, 19396 1234u); 19397 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 19398 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 19399 PenaltyReturnTypeOnItsOwnLine, 1234u); 19400 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 19401 SpacesBeforeTrailingComments, 1234u); 19402 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 19403 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 19404 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 19405 19406 Style.QualifierAlignment = FormatStyle::QAS_Right; 19407 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 19408 FormatStyle::QAS_Leave); 19409 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 19410 FormatStyle::QAS_Right); 19411 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 19412 FormatStyle::QAS_Left); 19413 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 19414 FormatStyle::QAS_Custom); 19415 19416 Style.QualifierOrder.clear(); 19417 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 19418 std::vector<std::string>({"const", "volatile", "type"})); 19419 Style.QualifierOrder.clear(); 19420 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 19421 std::vector<std::string>({"const", "type"})); 19422 Style.QualifierOrder.clear(); 19423 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 19424 std::vector<std::string>({"volatile", "type"})); 19425 19426 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 19427 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 19428 FormatStyle::ACS_None); 19429 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 19430 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 19431 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 19432 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 19433 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 19434 AlignConsecutiveAssignments, 19435 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19436 // For backwards compability, false / true should still parse 19437 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 19438 FormatStyle::ACS_None); 19439 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 19440 FormatStyle::ACS_Consecutive); 19441 19442 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 19443 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 19444 FormatStyle::ACS_None); 19445 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 19446 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 19447 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 19448 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 19449 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 19450 AlignConsecutiveBitFields, 19451 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19452 // For backwards compability, false / true should still parse 19453 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 19454 FormatStyle::ACS_None); 19455 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 19456 FormatStyle::ACS_Consecutive); 19457 19458 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 19459 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 19460 FormatStyle::ACS_None); 19461 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 19462 FormatStyle::ACS_Consecutive); 19463 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 19464 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 19465 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 19466 AlignConsecutiveMacros, 19467 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19468 // For backwards compability, false / true should still parse 19469 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 19470 FormatStyle::ACS_None); 19471 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 19472 FormatStyle::ACS_Consecutive); 19473 19474 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 19475 CHECK_PARSE("AlignConsecutiveDeclarations: None", 19476 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 19477 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 19478 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 19479 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 19480 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 19481 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 19482 AlignConsecutiveDeclarations, 19483 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19484 // For backwards compability, false / true should still parse 19485 CHECK_PARSE("AlignConsecutiveDeclarations: false", 19486 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 19487 CHECK_PARSE("AlignConsecutiveDeclarations: true", 19488 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 19489 19490 Style.PointerAlignment = FormatStyle::PAS_Middle; 19491 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 19492 FormatStyle::PAS_Left); 19493 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 19494 FormatStyle::PAS_Right); 19495 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 19496 FormatStyle::PAS_Middle); 19497 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 19498 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 19499 FormatStyle::RAS_Pointer); 19500 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 19501 FormatStyle::RAS_Left); 19502 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 19503 FormatStyle::RAS_Right); 19504 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 19505 FormatStyle::RAS_Middle); 19506 // For backward compatibility: 19507 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 19508 FormatStyle::PAS_Left); 19509 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 19510 FormatStyle::PAS_Right); 19511 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 19512 FormatStyle::PAS_Middle); 19513 19514 Style.Standard = FormatStyle::LS_Auto; 19515 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 19516 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 19517 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 19518 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 19519 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 19520 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 19521 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 19522 // Legacy aliases: 19523 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 19524 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 19525 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 19526 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 19527 19528 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 19529 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 19530 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 19531 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 19532 FormatStyle::BOS_None); 19533 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 19534 FormatStyle::BOS_All); 19535 // For backward compatibility: 19536 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 19537 FormatStyle::BOS_None); 19538 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 19539 FormatStyle::BOS_All); 19540 19541 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 19542 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 19543 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 19544 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 19545 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 19546 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 19547 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 19548 // For backward compatibility: 19549 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 19550 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 19551 19552 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 19553 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 19554 FormatStyle::BILS_AfterComma); 19555 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 19556 FormatStyle::BILS_BeforeComma); 19557 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 19558 FormatStyle::BILS_AfterColon); 19559 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 19560 FormatStyle::BILS_BeforeColon); 19561 // For backward compatibility: 19562 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 19563 FormatStyle::BILS_BeforeComma); 19564 19565 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19566 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 19567 FormatStyle::PCIS_Never); 19568 CHECK_PARSE("PackConstructorInitializers: BinPack", 19569 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19570 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 19571 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19572 CHECK_PARSE("PackConstructorInitializers: NextLine", 19573 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19574 // For backward compatibility: 19575 CHECK_PARSE("BasedOnStyle: Google\n" 19576 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19577 "AllowAllConstructorInitializersOnNextLine: false", 19578 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19579 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19580 CHECK_PARSE("BasedOnStyle: Google\n" 19581 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 19582 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19583 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19584 "AllowAllConstructorInitializersOnNextLine: true", 19585 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19586 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19587 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19588 "AllowAllConstructorInitializersOnNextLine: false", 19589 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19590 19591 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 19592 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 19593 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 19594 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 19595 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 19596 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 19597 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 19598 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 19599 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 19600 19601 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19602 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 19603 FormatStyle::BAS_Align); 19604 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 19605 FormatStyle::BAS_DontAlign); 19606 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 19607 FormatStyle::BAS_AlwaysBreak); 19608 CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket, 19609 FormatStyle::BAS_BlockIndent); 19610 // For backward compatibility: 19611 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 19612 FormatStyle::BAS_DontAlign); 19613 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 19614 FormatStyle::BAS_Align); 19615 19616 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 19617 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 19618 FormatStyle::ENAS_DontAlign); 19619 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 19620 FormatStyle::ENAS_Left); 19621 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 19622 FormatStyle::ENAS_Right); 19623 // For backward compatibility: 19624 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 19625 FormatStyle::ENAS_Left); 19626 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 19627 FormatStyle::ENAS_Right); 19628 19629 Style.AlignOperands = FormatStyle::OAS_Align; 19630 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 19631 FormatStyle::OAS_DontAlign); 19632 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 19633 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 19634 FormatStyle::OAS_AlignAfterOperator); 19635 // For backward compatibility: 19636 CHECK_PARSE("AlignOperands: false", AlignOperands, 19637 FormatStyle::OAS_DontAlign); 19638 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 19639 19640 Style.UseTab = FormatStyle::UT_ForIndentation; 19641 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 19642 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 19643 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 19644 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 19645 FormatStyle::UT_ForContinuationAndIndentation); 19646 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 19647 FormatStyle::UT_AlignWithSpaces); 19648 // For backward compatibility: 19649 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 19650 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 19651 19652 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 19653 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 19654 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19655 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 19656 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 19657 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 19658 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19659 // For backward compatibility: 19660 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 19661 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19662 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 19663 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19664 19665 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 19666 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 19667 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19668 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 19669 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 19670 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 19671 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 19672 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 19673 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19674 // For backward compatibility: 19675 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 19676 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19677 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 19678 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19679 19680 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 19681 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 19682 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 19683 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 19684 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 19685 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 19686 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 19687 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 19688 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 19689 19690 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 19691 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 19692 FormatStyle::SBPO_Never); 19693 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 19694 FormatStyle::SBPO_Always); 19695 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 19696 FormatStyle::SBPO_ControlStatements); 19697 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 19698 SpaceBeforeParens, 19699 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19700 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 19701 FormatStyle::SBPO_NonEmptyParentheses); 19702 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 19703 FormatStyle::SBPO_Custom); 19704 // For backward compatibility: 19705 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 19706 FormatStyle::SBPO_Never); 19707 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 19708 FormatStyle::SBPO_ControlStatements); 19709 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 19710 SpaceBeforeParens, 19711 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19712 19713 Style.ColumnLimit = 123; 19714 FormatStyle BaseStyle = getLLVMStyle(); 19715 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 19716 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 19717 19718 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 19719 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 19720 FormatStyle::BS_Attach); 19721 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 19722 FormatStyle::BS_Linux); 19723 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 19724 FormatStyle::BS_Mozilla); 19725 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 19726 FormatStyle::BS_Stroustrup); 19727 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 19728 FormatStyle::BS_Allman); 19729 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 19730 FormatStyle::BS_Whitesmiths); 19731 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 19732 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 19733 FormatStyle::BS_WebKit); 19734 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 19735 FormatStyle::BS_Custom); 19736 19737 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 19738 CHECK_PARSE("BraceWrapping:\n" 19739 " AfterControlStatement: MultiLine", 19740 BraceWrapping.AfterControlStatement, 19741 FormatStyle::BWACS_MultiLine); 19742 CHECK_PARSE("BraceWrapping:\n" 19743 " AfterControlStatement: Always", 19744 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19745 CHECK_PARSE("BraceWrapping:\n" 19746 " AfterControlStatement: Never", 19747 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19748 // For backward compatibility: 19749 CHECK_PARSE("BraceWrapping:\n" 19750 " AfterControlStatement: true", 19751 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19752 CHECK_PARSE("BraceWrapping:\n" 19753 " AfterControlStatement: false", 19754 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19755 19756 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 19757 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 19758 FormatStyle::RTBS_None); 19759 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 19760 FormatStyle::RTBS_All); 19761 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 19762 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 19763 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 19764 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 19765 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 19766 AlwaysBreakAfterReturnType, 19767 FormatStyle::RTBS_TopLevelDefinitions); 19768 19769 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 19770 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 19771 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 19772 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 19773 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19774 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 19775 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19776 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 19777 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19778 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 19779 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19780 19781 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 19782 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 19783 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 19784 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 19785 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 19786 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 19787 AlwaysBreakAfterDefinitionReturnType, 19788 FormatStyle::DRTBS_TopLevel); 19789 19790 Style.NamespaceIndentation = FormatStyle::NI_All; 19791 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 19792 FormatStyle::NI_None); 19793 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 19794 FormatStyle::NI_Inner); 19795 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 19796 FormatStyle::NI_All); 19797 19798 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 19799 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 19800 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19801 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 19802 AllowShortIfStatementsOnASingleLine, 19803 FormatStyle::SIS_WithoutElse); 19804 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 19805 AllowShortIfStatementsOnASingleLine, 19806 FormatStyle::SIS_OnlyFirstIf); 19807 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 19808 AllowShortIfStatementsOnASingleLine, 19809 FormatStyle::SIS_AllIfsAndElse); 19810 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 19811 AllowShortIfStatementsOnASingleLine, 19812 FormatStyle::SIS_OnlyFirstIf); 19813 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 19814 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19815 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 19816 AllowShortIfStatementsOnASingleLine, 19817 FormatStyle::SIS_WithoutElse); 19818 19819 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 19820 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 19821 FormatStyle::IEBS_AfterExternBlock); 19822 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 19823 FormatStyle::IEBS_Indent); 19824 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 19825 FormatStyle::IEBS_NoIndent); 19826 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 19827 FormatStyle::IEBS_Indent); 19828 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 19829 FormatStyle::IEBS_NoIndent); 19830 19831 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 19832 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 19833 FormatStyle::BFCS_Both); 19834 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 19835 FormatStyle::BFCS_None); 19836 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 19837 FormatStyle::BFCS_Before); 19838 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 19839 FormatStyle::BFCS_After); 19840 19841 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 19842 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 19843 FormatStyle::SJSIO_After); 19844 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 19845 FormatStyle::SJSIO_Before); 19846 19847 // FIXME: This is required because parsing a configuration simply overwrites 19848 // the first N elements of the list instead of resetting it. 19849 Style.ForEachMacros.clear(); 19850 std::vector<std::string> BoostForeach; 19851 BoostForeach.push_back("BOOST_FOREACH"); 19852 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 19853 std::vector<std::string> BoostAndQForeach; 19854 BoostAndQForeach.push_back("BOOST_FOREACH"); 19855 BoostAndQForeach.push_back("Q_FOREACH"); 19856 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 19857 BoostAndQForeach); 19858 19859 Style.IfMacros.clear(); 19860 std::vector<std::string> CustomIfs; 19861 CustomIfs.push_back("MYIF"); 19862 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 19863 19864 Style.AttributeMacros.clear(); 19865 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 19866 std::vector<std::string>{"__capability"}); 19867 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 19868 std::vector<std::string>({"attr1", "attr2"})); 19869 19870 Style.StatementAttributeLikeMacros.clear(); 19871 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 19872 StatementAttributeLikeMacros, 19873 std::vector<std::string>({"emit", "Q_EMIT"})); 19874 19875 Style.StatementMacros.clear(); 19876 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 19877 std::vector<std::string>{"QUNUSED"}); 19878 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 19879 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 19880 19881 Style.NamespaceMacros.clear(); 19882 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 19883 std::vector<std::string>{"TESTSUITE"}); 19884 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 19885 std::vector<std::string>({"TESTSUITE", "SUITE"})); 19886 19887 Style.WhitespaceSensitiveMacros.clear(); 19888 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 19889 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19890 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 19891 WhitespaceSensitiveMacros, 19892 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19893 Style.WhitespaceSensitiveMacros.clear(); 19894 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 19895 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19896 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 19897 WhitespaceSensitiveMacros, 19898 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19899 19900 Style.IncludeStyle.IncludeCategories.clear(); 19901 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 19902 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 19903 CHECK_PARSE("IncludeCategories:\n" 19904 " - Regex: abc/.*\n" 19905 " Priority: 2\n" 19906 " - Regex: .*\n" 19907 " Priority: 1\n" 19908 " CaseSensitive: true\n", 19909 IncludeStyle.IncludeCategories, ExpectedCategories); 19910 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 19911 "abc$"); 19912 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 19913 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 19914 19915 Style.SortIncludes = FormatStyle::SI_Never; 19916 CHECK_PARSE("SortIncludes: true", SortIncludes, 19917 FormatStyle::SI_CaseSensitive); 19918 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 19919 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 19920 FormatStyle::SI_CaseInsensitive); 19921 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 19922 FormatStyle::SI_CaseSensitive); 19923 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 19924 19925 Style.RawStringFormats.clear(); 19926 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 19927 { 19928 FormatStyle::LK_TextProto, 19929 {"pb", "proto"}, 19930 {"PARSE_TEXT_PROTO"}, 19931 /*CanonicalDelimiter=*/"", 19932 "llvm", 19933 }, 19934 { 19935 FormatStyle::LK_Cpp, 19936 {"cc", "cpp"}, 19937 {"C_CODEBLOCK", "CPPEVAL"}, 19938 /*CanonicalDelimiter=*/"cc", 19939 /*BasedOnStyle=*/"", 19940 }, 19941 }; 19942 19943 CHECK_PARSE("RawStringFormats:\n" 19944 " - Language: TextProto\n" 19945 " Delimiters:\n" 19946 " - 'pb'\n" 19947 " - 'proto'\n" 19948 " EnclosingFunctions:\n" 19949 " - 'PARSE_TEXT_PROTO'\n" 19950 " BasedOnStyle: llvm\n" 19951 " - Language: Cpp\n" 19952 " Delimiters:\n" 19953 " - 'cc'\n" 19954 " - 'cpp'\n" 19955 " EnclosingFunctions:\n" 19956 " - 'C_CODEBLOCK'\n" 19957 " - 'CPPEVAL'\n" 19958 " CanonicalDelimiter: 'cc'", 19959 RawStringFormats, ExpectedRawStringFormats); 19960 19961 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19962 " Minimum: 0\n" 19963 " Maximum: 0", 19964 SpacesInLineCommentPrefix.Minimum, 0u); 19965 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 19966 Style.SpacesInLineCommentPrefix.Minimum = 1; 19967 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19968 " Minimum: 2", 19969 SpacesInLineCommentPrefix.Minimum, 0u); 19970 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19971 " Maximum: -1", 19972 SpacesInLineCommentPrefix.Maximum, -1u); 19973 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19974 " Minimum: 2", 19975 SpacesInLineCommentPrefix.Minimum, 2u); 19976 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19977 " Maximum: 1", 19978 SpacesInLineCommentPrefix.Maximum, 1u); 19979 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 19980 19981 Style.SpacesInAngles = FormatStyle::SIAS_Always; 19982 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 19983 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 19984 FormatStyle::SIAS_Always); 19985 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 19986 // For backward compatibility: 19987 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 19988 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 19989 19990 CHECK_PARSE("RequiresClausePosition: WithPreceding", RequiresClausePosition, 19991 FormatStyle::RCPS_WithPreceding); 19992 CHECK_PARSE("RequiresClausePosition: WithFollowing", RequiresClausePosition, 19993 FormatStyle::RCPS_WithFollowing); 19994 CHECK_PARSE("RequiresClausePosition: SingleLine", RequiresClausePosition, 19995 FormatStyle::RCPS_SingleLine); 19996 CHECK_PARSE("RequiresClausePosition: OwnLine", RequiresClausePosition, 19997 FormatStyle::RCPS_OwnLine); 19998 19999 CHECK_PARSE("BreakBeforeConceptDeclarations: Never", 20000 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Never); 20001 CHECK_PARSE("BreakBeforeConceptDeclarations: Always", 20002 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always); 20003 CHECK_PARSE("BreakBeforeConceptDeclarations: Allowed", 20004 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed); 20005 // For backward compatibility: 20006 CHECK_PARSE("BreakBeforeConceptDeclarations: true", 20007 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Always); 20008 CHECK_PARSE("BreakBeforeConceptDeclarations: false", 20009 BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Allowed); 20010 } 20011 20012 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 20013 FormatStyle Style = {}; 20014 Style.Language = FormatStyle::LK_Cpp; 20015 CHECK_PARSE("Language: Cpp\n" 20016 "IndentWidth: 12", 20017 IndentWidth, 12u); 20018 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 20019 "IndentWidth: 34", 20020 &Style), 20021 ParseError::Unsuitable); 20022 FormatStyle BinPackedTCS = {}; 20023 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 20024 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 20025 "InsertTrailingCommas: Wrapped", 20026 &BinPackedTCS), 20027 ParseError::BinPackTrailingCommaConflict); 20028 EXPECT_EQ(12u, Style.IndentWidth); 20029 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 20030 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 20031 20032 Style.Language = FormatStyle::LK_JavaScript; 20033 CHECK_PARSE("Language: JavaScript\n" 20034 "IndentWidth: 12", 20035 IndentWidth, 12u); 20036 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 20037 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 20038 "IndentWidth: 34", 20039 &Style), 20040 ParseError::Unsuitable); 20041 EXPECT_EQ(23u, Style.IndentWidth); 20042 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 20043 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 20044 20045 CHECK_PARSE("BasedOnStyle: LLVM\n" 20046 "IndentWidth: 67", 20047 IndentWidth, 67u); 20048 20049 CHECK_PARSE("---\n" 20050 "Language: JavaScript\n" 20051 "IndentWidth: 12\n" 20052 "---\n" 20053 "Language: Cpp\n" 20054 "IndentWidth: 34\n" 20055 "...\n", 20056 IndentWidth, 12u); 20057 20058 Style.Language = FormatStyle::LK_Cpp; 20059 CHECK_PARSE("---\n" 20060 "Language: JavaScript\n" 20061 "IndentWidth: 12\n" 20062 "---\n" 20063 "Language: Cpp\n" 20064 "IndentWidth: 34\n" 20065 "...\n", 20066 IndentWidth, 34u); 20067 CHECK_PARSE("---\n" 20068 "IndentWidth: 78\n" 20069 "---\n" 20070 "Language: JavaScript\n" 20071 "IndentWidth: 56\n" 20072 "...\n", 20073 IndentWidth, 78u); 20074 20075 Style.ColumnLimit = 123; 20076 Style.IndentWidth = 234; 20077 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 20078 Style.TabWidth = 345; 20079 EXPECT_FALSE(parseConfiguration("---\n" 20080 "IndentWidth: 456\n" 20081 "BreakBeforeBraces: Allman\n" 20082 "---\n" 20083 "Language: JavaScript\n" 20084 "IndentWidth: 111\n" 20085 "TabWidth: 111\n" 20086 "---\n" 20087 "Language: Cpp\n" 20088 "BreakBeforeBraces: Stroustrup\n" 20089 "TabWidth: 789\n" 20090 "...\n", 20091 &Style)); 20092 EXPECT_EQ(123u, Style.ColumnLimit); 20093 EXPECT_EQ(456u, Style.IndentWidth); 20094 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 20095 EXPECT_EQ(789u, Style.TabWidth); 20096 20097 EXPECT_EQ(parseConfiguration("---\n" 20098 "Language: JavaScript\n" 20099 "IndentWidth: 56\n" 20100 "---\n" 20101 "IndentWidth: 78\n" 20102 "...\n", 20103 &Style), 20104 ParseError::Error); 20105 EXPECT_EQ(parseConfiguration("---\n" 20106 "Language: JavaScript\n" 20107 "IndentWidth: 56\n" 20108 "---\n" 20109 "Language: JavaScript\n" 20110 "IndentWidth: 78\n" 20111 "...\n", 20112 &Style), 20113 ParseError::Error); 20114 20115 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 20116 } 20117 20118 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 20119 FormatStyle Style = {}; 20120 Style.Language = FormatStyle::LK_JavaScript; 20121 Style.BreakBeforeTernaryOperators = true; 20122 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 20123 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 20124 20125 Style.BreakBeforeTernaryOperators = true; 20126 EXPECT_EQ(0, parseConfiguration("---\n" 20127 "BasedOnStyle: Google\n" 20128 "---\n" 20129 "Language: JavaScript\n" 20130 "IndentWidth: 76\n" 20131 "...\n", 20132 &Style) 20133 .value()); 20134 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 20135 EXPECT_EQ(76u, Style.IndentWidth); 20136 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 20137 } 20138 20139 TEST_F(FormatTest, ConfigurationRoundTripTest) { 20140 FormatStyle Style = getLLVMStyle(); 20141 std::string YAML = configurationAsText(Style); 20142 FormatStyle ParsedStyle = {}; 20143 ParsedStyle.Language = FormatStyle::LK_Cpp; 20144 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 20145 EXPECT_EQ(Style, ParsedStyle); 20146 } 20147 20148 TEST_F(FormatTest, WorksFor8bitEncodings) { 20149 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 20150 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 20151 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 20152 "\"\xef\xee\xf0\xf3...\"", 20153 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 20154 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 20155 "\xef\xee\xf0\xf3...\"", 20156 getLLVMStyleWithColumns(12))); 20157 } 20158 20159 TEST_F(FormatTest, HandlesUTF8BOM) { 20160 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 20161 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 20162 format("\xef\xbb\xbf#include <iostream>")); 20163 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 20164 format("\xef\xbb\xbf\n#include <iostream>")); 20165 } 20166 20167 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 20168 #if !defined(_MSC_VER) 20169 20170 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 20171 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 20172 getLLVMStyleWithColumns(35)); 20173 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 20174 getLLVMStyleWithColumns(31)); 20175 verifyFormat("// Однажды в студёную зимнюю пору...", 20176 getLLVMStyleWithColumns(36)); 20177 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 20178 verifyFormat("/* Однажды в студёную зимнюю пору... */", 20179 getLLVMStyleWithColumns(39)); 20180 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 20181 getLLVMStyleWithColumns(35)); 20182 } 20183 20184 TEST_F(FormatTest, SplitsUTF8Strings) { 20185 // Non-printable characters' width is currently considered to be the length in 20186 // bytes in UTF8. The characters can be displayed in very different manner 20187 // (zero-width, single width with a substitution glyph, expanded to their code 20188 // (e.g. "<8d>"), so there's no single correct way to handle them. 20189 EXPECT_EQ("\"aaaaÄ\"\n" 20190 "\"\xc2\x8d\";", 20191 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 20192 EXPECT_EQ("\"aaaaaaaÄ\"\n" 20193 "\"\xc2\x8d\";", 20194 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 20195 EXPECT_EQ("\"Однажды, в \"\n" 20196 "\"студёную \"\n" 20197 "\"зимнюю \"\n" 20198 "\"пору,\"", 20199 format("\"Однажды, в студёную зимнюю пору,\"", 20200 getLLVMStyleWithColumns(13))); 20201 EXPECT_EQ( 20202 "\"一 二 三 \"\n" 20203 "\"四 五六 \"\n" 20204 "\"七 八 九 \"\n" 20205 "\"十\"", 20206 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 20207 EXPECT_EQ("\"一\t\"\n" 20208 "\"二 \t\"\n" 20209 "\"三 四 \"\n" 20210 "\"五\t\"\n" 20211 "\"六 \t\"\n" 20212 "\"七 \"\n" 20213 "\"八九十\tqq\"", 20214 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 20215 getLLVMStyleWithColumns(11))); 20216 20217 // UTF8 character in an escape sequence. 20218 EXPECT_EQ("\"aaaaaa\"\n" 20219 "\"\\\xC2\x8D\"", 20220 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 20221 } 20222 20223 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 20224 EXPECT_EQ("const char *sssss =\n" 20225 " \"一二三四五六七八\\\n" 20226 " 九 十\";", 20227 format("const char *sssss = \"一二三四五六七八\\\n" 20228 " 九 十\";", 20229 getLLVMStyleWithColumns(30))); 20230 } 20231 20232 TEST_F(FormatTest, SplitsUTF8LineComments) { 20233 EXPECT_EQ("// aaaaÄ\xc2\x8d", 20234 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 20235 EXPECT_EQ("// Я из лесу\n" 20236 "// вышел; был\n" 20237 "// сильный\n" 20238 "// мороз.", 20239 format("// Я из лесу вышел; был сильный мороз.", 20240 getLLVMStyleWithColumns(13))); 20241 EXPECT_EQ("// 一二三\n" 20242 "// 四五六七\n" 20243 "// 八 九\n" 20244 "// 十", 20245 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 20246 } 20247 20248 TEST_F(FormatTest, SplitsUTF8BlockComments) { 20249 EXPECT_EQ("/* Гляжу,\n" 20250 " * поднимается\n" 20251 " * медленно в\n" 20252 " * гору\n" 20253 " * Лошадка,\n" 20254 " * везущая\n" 20255 " * хворосту\n" 20256 " * воз. */", 20257 format("/* Гляжу, поднимается медленно в гору\n" 20258 " * Лошадка, везущая хворосту воз. */", 20259 getLLVMStyleWithColumns(13))); 20260 EXPECT_EQ( 20261 "/* 一二三\n" 20262 " * 四五六七\n" 20263 " * 八 九\n" 20264 " * 十 */", 20265 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 20266 EXPECT_EQ("/* \n" 20267 " * \n" 20268 " * - */", 20269 format("/* - */", getLLVMStyleWithColumns(12))); 20270 } 20271 20272 #endif // _MSC_VER 20273 20274 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 20275 FormatStyle Style = getLLVMStyle(); 20276 20277 Style.ConstructorInitializerIndentWidth = 4; 20278 verifyFormat( 20279 "SomeClass::Constructor()\n" 20280 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20281 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20282 Style); 20283 20284 Style.ConstructorInitializerIndentWidth = 2; 20285 verifyFormat( 20286 "SomeClass::Constructor()\n" 20287 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20288 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20289 Style); 20290 20291 Style.ConstructorInitializerIndentWidth = 0; 20292 verifyFormat( 20293 "SomeClass::Constructor()\n" 20294 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20295 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20296 Style); 20297 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 20298 verifyFormat( 20299 "SomeLongTemplateVariableName<\n" 20300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 20301 Style); 20302 verifyFormat("bool smaller = 1 < " 20303 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 20304 " " 20305 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 20306 Style); 20307 20308 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 20309 verifyFormat("SomeClass::Constructor() :\n" 20310 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 20311 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 20312 Style); 20313 } 20314 20315 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 20316 FormatStyle Style = getLLVMStyle(); 20317 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20318 Style.ConstructorInitializerIndentWidth = 4; 20319 verifyFormat("SomeClass::Constructor()\n" 20320 " : a(a)\n" 20321 " , b(b)\n" 20322 " , c(c) {}", 20323 Style); 20324 verifyFormat("SomeClass::Constructor()\n" 20325 " : a(a) {}", 20326 Style); 20327 20328 Style.ColumnLimit = 0; 20329 verifyFormat("SomeClass::Constructor()\n" 20330 " : a(a) {}", 20331 Style); 20332 verifyFormat("SomeClass::Constructor() noexcept\n" 20333 " : a(a) {}", 20334 Style); 20335 verifyFormat("SomeClass::Constructor()\n" 20336 " : a(a)\n" 20337 " , b(b)\n" 20338 " , c(c) {}", 20339 Style); 20340 verifyFormat("SomeClass::Constructor()\n" 20341 " : a(a) {\n" 20342 " foo();\n" 20343 " bar();\n" 20344 "}", 20345 Style); 20346 20347 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 20348 verifyFormat("SomeClass::Constructor()\n" 20349 " : a(a)\n" 20350 " , b(b)\n" 20351 " , c(c) {\n}", 20352 Style); 20353 verifyFormat("SomeClass::Constructor()\n" 20354 " : a(a) {\n}", 20355 Style); 20356 20357 Style.ColumnLimit = 80; 20358 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 20359 Style.ConstructorInitializerIndentWidth = 2; 20360 verifyFormat("SomeClass::Constructor()\n" 20361 " : a(a)\n" 20362 " , b(b)\n" 20363 " , c(c) {}", 20364 Style); 20365 20366 Style.ConstructorInitializerIndentWidth = 0; 20367 verifyFormat("SomeClass::Constructor()\n" 20368 ": a(a)\n" 20369 ", b(b)\n" 20370 ", c(c) {}", 20371 Style); 20372 20373 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 20374 Style.ConstructorInitializerIndentWidth = 4; 20375 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 20376 verifyFormat( 20377 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 20378 Style); 20379 verifyFormat( 20380 "SomeClass::Constructor()\n" 20381 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 20382 Style); 20383 Style.ConstructorInitializerIndentWidth = 4; 20384 Style.ColumnLimit = 60; 20385 verifyFormat("SomeClass::Constructor()\n" 20386 " : aaaaaaaa(aaaaaaaa)\n" 20387 " , aaaaaaaa(aaaaaaaa)\n" 20388 " , aaaaaaaa(aaaaaaaa) {}", 20389 Style); 20390 } 20391 20392 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 20393 FormatStyle Style = getLLVMStyle(); 20394 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20395 Style.ConstructorInitializerIndentWidth = 4; 20396 verifyFormat("SomeClass::Constructor()\n" 20397 " : a{a}\n" 20398 " , b{b} {}", 20399 Style); 20400 verifyFormat("SomeClass::Constructor()\n" 20401 " : a{a}\n" 20402 "#if CONDITION\n" 20403 " , b{b}\n" 20404 "#endif\n" 20405 "{\n}", 20406 Style); 20407 Style.ConstructorInitializerIndentWidth = 2; 20408 verifyFormat("SomeClass::Constructor()\n" 20409 "#if CONDITION\n" 20410 " : a{a}\n" 20411 "#endif\n" 20412 " , b{b}\n" 20413 " , c{c} {\n}", 20414 Style); 20415 Style.ConstructorInitializerIndentWidth = 0; 20416 verifyFormat("SomeClass::Constructor()\n" 20417 ": a{a}\n" 20418 "#ifdef CONDITION\n" 20419 ", b{b}\n" 20420 "#else\n" 20421 ", c{c}\n" 20422 "#endif\n" 20423 ", d{d} {\n}", 20424 Style); 20425 Style.ConstructorInitializerIndentWidth = 4; 20426 verifyFormat("SomeClass::Constructor()\n" 20427 " : a{a}\n" 20428 "#if WINDOWS\n" 20429 "#if DEBUG\n" 20430 " , b{0}\n" 20431 "#else\n" 20432 " , b{1}\n" 20433 "#endif\n" 20434 "#else\n" 20435 "#if DEBUG\n" 20436 " , b{2}\n" 20437 "#else\n" 20438 " , b{3}\n" 20439 "#endif\n" 20440 "#endif\n" 20441 "{\n}", 20442 Style); 20443 verifyFormat("SomeClass::Constructor()\n" 20444 " : a{a}\n" 20445 "#if WINDOWS\n" 20446 " , b{0}\n" 20447 "#if DEBUG\n" 20448 " , c{0}\n" 20449 "#else\n" 20450 " , c{1}\n" 20451 "#endif\n" 20452 "#else\n" 20453 "#if DEBUG\n" 20454 " , c{2}\n" 20455 "#else\n" 20456 " , c{3}\n" 20457 "#endif\n" 20458 " , b{1}\n" 20459 "#endif\n" 20460 "{\n}", 20461 Style); 20462 } 20463 20464 TEST_F(FormatTest, Destructors) { 20465 verifyFormat("void F(int &i) { i.~int(); }"); 20466 verifyFormat("void F(int &i) { i->~int(); }"); 20467 } 20468 20469 TEST_F(FormatTest, FormatsWithWebKitStyle) { 20470 FormatStyle Style = getWebKitStyle(); 20471 20472 // Don't indent in outer namespaces. 20473 verifyFormat("namespace outer {\n" 20474 "int i;\n" 20475 "namespace inner {\n" 20476 " int i;\n" 20477 "} // namespace inner\n" 20478 "} // namespace outer\n" 20479 "namespace other_outer {\n" 20480 "int i;\n" 20481 "}", 20482 Style); 20483 20484 // Don't indent case labels. 20485 verifyFormat("switch (variable) {\n" 20486 "case 1:\n" 20487 "case 2:\n" 20488 " doSomething();\n" 20489 " break;\n" 20490 "default:\n" 20491 " ++variable;\n" 20492 "}", 20493 Style); 20494 20495 // Wrap before binary operators. 20496 EXPECT_EQ("void f()\n" 20497 "{\n" 20498 " if (aaaaaaaaaaaaaaaa\n" 20499 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 20500 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 20501 " return;\n" 20502 "}", 20503 format("void f() {\n" 20504 "if (aaaaaaaaaaaaaaaa\n" 20505 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 20506 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 20507 "return;\n" 20508 "}", 20509 Style)); 20510 20511 // Allow functions on a single line. 20512 verifyFormat("void f() { return; }", Style); 20513 20514 // Allow empty blocks on a single line and insert a space in empty blocks. 20515 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 20516 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 20517 // However, don't merge non-empty short loops. 20518 EXPECT_EQ("while (true) {\n" 20519 " continue;\n" 20520 "}", 20521 format("while (true) { continue; }", Style)); 20522 20523 // Constructor initializers are formatted one per line with the "," on the 20524 // new line. 20525 verifyFormat("Constructor()\n" 20526 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 20527 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 20528 " aaaaaaaaaaaaaa)\n" 20529 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 20530 "{\n" 20531 "}", 20532 Style); 20533 verifyFormat("SomeClass::Constructor()\n" 20534 " : a(a)\n" 20535 "{\n" 20536 "}", 20537 Style); 20538 EXPECT_EQ("SomeClass::Constructor()\n" 20539 " : a(a)\n" 20540 "{\n" 20541 "}", 20542 format("SomeClass::Constructor():a(a){}", Style)); 20543 verifyFormat("SomeClass::Constructor()\n" 20544 " : a(a)\n" 20545 " , b(b)\n" 20546 " , c(c)\n" 20547 "{\n" 20548 "}", 20549 Style); 20550 verifyFormat("SomeClass::Constructor()\n" 20551 " : a(a)\n" 20552 "{\n" 20553 " foo();\n" 20554 " bar();\n" 20555 "}", 20556 Style); 20557 20558 // Access specifiers should be aligned left. 20559 verifyFormat("class C {\n" 20560 "public:\n" 20561 " int i;\n" 20562 "};", 20563 Style); 20564 20565 // Do not align comments. 20566 verifyFormat("int a; // Do not\n" 20567 "double b; // align comments.", 20568 Style); 20569 20570 // Do not align operands. 20571 EXPECT_EQ("ASSERT(aaaa\n" 20572 " || bbbb);", 20573 format("ASSERT ( aaaa\n||bbbb);", Style)); 20574 20575 // Accept input's line breaks. 20576 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 20577 " || bbbbbbbbbbbbbbb) {\n" 20578 " i++;\n" 20579 "}", 20580 format("if (aaaaaaaaaaaaaaa\n" 20581 "|| bbbbbbbbbbbbbbb) { i++; }", 20582 Style)); 20583 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 20584 " i++;\n" 20585 "}", 20586 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 20587 20588 // Don't automatically break all macro definitions (llvm.org/PR17842). 20589 verifyFormat("#define aNumber 10", Style); 20590 // However, generally keep the line breaks that the user authored. 20591 EXPECT_EQ("#define aNumber \\\n" 20592 " 10", 20593 format("#define aNumber \\\n" 20594 " 10", 20595 Style)); 20596 20597 // Keep empty and one-element array literals on a single line. 20598 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 20599 " copyItems:YES];", 20600 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 20601 "copyItems:YES];", 20602 Style)); 20603 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 20604 " copyItems:YES];", 20605 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 20606 " copyItems:YES];", 20607 Style)); 20608 // FIXME: This does not seem right, there should be more indentation before 20609 // the array literal's entries. Nested blocks have the same problem. 20610 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20611 " @\"a\",\n" 20612 " @\"a\"\n" 20613 "]\n" 20614 " copyItems:YES];", 20615 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20616 " @\"a\",\n" 20617 " @\"a\"\n" 20618 " ]\n" 20619 " copyItems:YES];", 20620 Style)); 20621 EXPECT_EQ( 20622 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20623 " copyItems:YES];", 20624 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20625 " copyItems:YES];", 20626 Style)); 20627 20628 verifyFormat("[self.a b:c c:d];", Style); 20629 EXPECT_EQ("[self.a b:c\n" 20630 " c:d];", 20631 format("[self.a b:c\n" 20632 "c:d];", 20633 Style)); 20634 } 20635 20636 TEST_F(FormatTest, FormatsLambdas) { 20637 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 20638 verifyFormat( 20639 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 20640 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 20641 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 20642 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 20643 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 20644 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 20645 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 20646 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 20647 verifyFormat("int x = f(*+[] {});"); 20648 verifyFormat("void f() {\n" 20649 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 20650 "}\n"); 20651 verifyFormat("void f() {\n" 20652 " other(x.begin(), //\n" 20653 " x.end(), //\n" 20654 " [&](int, int) { return 1; });\n" 20655 "}\n"); 20656 verifyFormat("void f() {\n" 20657 " other.other.other.other.other(\n" 20658 " x.begin(), x.end(),\n" 20659 " [something, rather](int, int, int, int, int, int, int) { " 20660 "return 1; });\n" 20661 "}\n"); 20662 verifyFormat( 20663 "void f() {\n" 20664 " other.other.other.other.other(\n" 20665 " x.begin(), x.end(),\n" 20666 " [something, rather](int, int, int, int, int, int, int) {\n" 20667 " //\n" 20668 " });\n" 20669 "}\n"); 20670 verifyFormat("SomeFunction([]() { // A cool function...\n" 20671 " return 43;\n" 20672 "});"); 20673 EXPECT_EQ("SomeFunction([]() {\n" 20674 "#define A a\n" 20675 " return 43;\n" 20676 "});", 20677 format("SomeFunction([](){\n" 20678 "#define A a\n" 20679 "return 43;\n" 20680 "});")); 20681 verifyFormat("void f() {\n" 20682 " SomeFunction([](decltype(x), A *a) {});\n" 20683 " SomeFunction([](typeof(x), A *a) {});\n" 20684 " SomeFunction([](_Atomic(x), A *a) {});\n" 20685 " SomeFunction([](__underlying_type(x), A *a) {});\n" 20686 "}"); 20687 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20688 " [](const aaaaaaaaaa &a) { return a; });"); 20689 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 20690 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 20691 "});"); 20692 verifyFormat("Constructor()\n" 20693 " : Field([] { // comment\n" 20694 " int i;\n" 20695 " }) {}"); 20696 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 20697 " return some_parameter.size();\n" 20698 "};"); 20699 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 20700 " [](const string &s) { return s; };"); 20701 verifyFormat("int i = aaaaaa ? 1 //\n" 20702 " : [] {\n" 20703 " return 2; //\n" 20704 " }();"); 20705 verifyFormat("llvm::errs() << \"number of twos is \"\n" 20706 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 20707 " return x == 2; // force break\n" 20708 " });"); 20709 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20710 " [=](int iiiiiiiiiiii) {\n" 20711 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 20712 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 20713 " });", 20714 getLLVMStyleWithColumns(60)); 20715 20716 verifyFormat("SomeFunction({[&] {\n" 20717 " // comment\n" 20718 " },\n" 20719 " [&] {\n" 20720 " // comment\n" 20721 " }});"); 20722 verifyFormat("SomeFunction({[&] {\n" 20723 " // comment\n" 20724 "}});"); 20725 verifyFormat( 20726 "virtual aaaaaaaaaaaaaaaa(\n" 20727 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 20728 " aaaaa aaaaaaaaa);"); 20729 20730 // Lambdas with return types. 20731 verifyFormat("int c = []() -> int { return 2; }();\n"); 20732 verifyFormat("int c = []() -> int * { return 2; }();\n"); 20733 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 20734 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 20735 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 20736 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 20737 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 20738 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 20739 verifyFormat("[a, a]() -> a<1> {};"); 20740 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 20741 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 20742 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 20743 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 20744 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 20745 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 20746 verifyFormat("[]() -> foo<!5> { return {}; };"); 20747 verifyFormat("[]() -> foo<~5> { return {}; };"); 20748 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 20749 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 20750 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 20751 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 20752 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 20753 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 20754 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 20755 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 20756 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 20757 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 20758 verifyFormat("namespace bar {\n" 20759 "// broken:\n" 20760 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 20761 "} // namespace bar"); 20762 verifyFormat("namespace bar {\n" 20763 "// broken:\n" 20764 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 20765 "} // namespace bar"); 20766 verifyFormat("namespace bar {\n" 20767 "// broken:\n" 20768 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 20769 "} // namespace bar"); 20770 verifyFormat("namespace bar {\n" 20771 "// broken:\n" 20772 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 20773 "} // namespace bar"); 20774 verifyFormat("namespace bar {\n" 20775 "// broken:\n" 20776 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 20777 "} // namespace bar"); 20778 verifyFormat("namespace bar {\n" 20779 "// broken:\n" 20780 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 20781 "} // namespace bar"); 20782 verifyFormat("namespace bar {\n" 20783 "// broken:\n" 20784 "auto foo{[]() -> foo<!5> { return {}; }};\n" 20785 "} // namespace bar"); 20786 verifyFormat("namespace bar {\n" 20787 "// broken:\n" 20788 "auto foo{[]() -> foo<~5> { return {}; }};\n" 20789 "} // namespace bar"); 20790 verifyFormat("namespace bar {\n" 20791 "// broken:\n" 20792 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 20793 "} // namespace bar"); 20794 verifyFormat("namespace bar {\n" 20795 "// broken:\n" 20796 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 20797 "} // namespace bar"); 20798 verifyFormat("namespace bar {\n" 20799 "// broken:\n" 20800 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 20801 "} // namespace bar"); 20802 verifyFormat("namespace bar {\n" 20803 "// broken:\n" 20804 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 20805 "} // namespace bar"); 20806 verifyFormat("namespace bar {\n" 20807 "// broken:\n" 20808 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 20809 "} // namespace bar"); 20810 verifyFormat("namespace bar {\n" 20811 "// broken:\n" 20812 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 20813 "} // namespace bar"); 20814 verifyFormat("namespace bar {\n" 20815 "// broken:\n" 20816 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 20817 "} // namespace bar"); 20818 verifyFormat("namespace bar {\n" 20819 "// broken:\n" 20820 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 20821 "} // namespace bar"); 20822 verifyFormat("namespace bar {\n" 20823 "// broken:\n" 20824 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 20825 "} // namespace bar"); 20826 verifyFormat("namespace bar {\n" 20827 "// broken:\n" 20828 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 20829 "} // namespace bar"); 20830 verifyFormat("[]() -> a<1> {};"); 20831 verifyFormat("[]() -> a<1> { ; };"); 20832 verifyFormat("[]() -> a<1> { ; }();"); 20833 verifyFormat("[a, a]() -> a<true> {};"); 20834 verifyFormat("[]() -> a<true> {};"); 20835 verifyFormat("[]() -> a<true> { ; };"); 20836 verifyFormat("[]() -> a<true> { ; }();"); 20837 verifyFormat("[a, a]() -> a<false> {};"); 20838 verifyFormat("[]() -> a<false> {};"); 20839 verifyFormat("[]() -> a<false> { ; };"); 20840 verifyFormat("[]() -> a<false> { ; }();"); 20841 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 20842 verifyFormat("namespace bar {\n" 20843 "auto foo{[]() -> foo<false> { ; }};\n" 20844 "} // namespace bar"); 20845 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 20846 " int j) -> int {\n" 20847 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 20848 "};"); 20849 verifyFormat( 20850 "aaaaaaaaaaaaaaaaaaaaaa(\n" 20851 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 20852 " return aaaaaaaaaaaaaaaaa;\n" 20853 " });", 20854 getLLVMStyleWithColumns(70)); 20855 verifyFormat("[]() //\n" 20856 " -> int {\n" 20857 " return 1; //\n" 20858 "};"); 20859 verifyFormat("[]() -> Void<T...> {};"); 20860 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 20861 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});"); 20862 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});"); 20863 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});"); 20864 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});"); 20865 verifyFormat("return int{[x = x]() { return x; }()};"); 20866 20867 // Lambdas with explicit template argument lists. 20868 verifyFormat( 20869 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 20870 verifyFormat("auto L = []<class T>(T) {\n" 20871 " {\n" 20872 " f();\n" 20873 " g();\n" 20874 " }\n" 20875 "};\n"); 20876 verifyFormat("auto L = []<class... T>(T...) {\n" 20877 " {\n" 20878 " f();\n" 20879 " g();\n" 20880 " }\n" 20881 "};\n"); 20882 verifyFormat("auto L = []<typename... T>(T...) {\n" 20883 " {\n" 20884 " f();\n" 20885 " g();\n" 20886 " }\n" 20887 "};\n"); 20888 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n" 20889 " {\n" 20890 " f();\n" 20891 " g();\n" 20892 " }\n" 20893 "};\n"); 20894 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n" 20895 " {\n" 20896 " f();\n" 20897 " g();\n" 20898 " }\n" 20899 "};\n"); 20900 20901 // Multiple lambdas in the same parentheses change indentation rules. These 20902 // lambdas are forced to start on new lines. 20903 verifyFormat("SomeFunction(\n" 20904 " []() {\n" 20905 " //\n" 20906 " },\n" 20907 " []() {\n" 20908 " //\n" 20909 " });"); 20910 20911 // A lambda passed as arg0 is always pushed to the next line. 20912 verifyFormat("SomeFunction(\n" 20913 " [this] {\n" 20914 " //\n" 20915 " },\n" 20916 " 1);\n"); 20917 20918 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 20919 // the arg0 case above. 20920 auto Style = getGoogleStyle(); 20921 Style.BinPackArguments = false; 20922 verifyFormat("SomeFunction(\n" 20923 " a,\n" 20924 " [this] {\n" 20925 " //\n" 20926 " },\n" 20927 " b);\n", 20928 Style); 20929 verifyFormat("SomeFunction(\n" 20930 " a,\n" 20931 " [this] {\n" 20932 " //\n" 20933 " },\n" 20934 " b);\n"); 20935 20936 // A lambda with a very long line forces arg0 to be pushed out irrespective of 20937 // the BinPackArguments value (as long as the code is wide enough). 20938 verifyFormat( 20939 "something->SomeFunction(\n" 20940 " a,\n" 20941 " [this] {\n" 20942 " " 20943 "D0000000000000000000000000000000000000000000000000000000000001();\n" 20944 " },\n" 20945 " b);\n"); 20946 20947 // A multi-line lambda is pulled up as long as the introducer fits on the 20948 // previous line and there are no further args. 20949 verifyFormat("function(1, [this, that] {\n" 20950 " //\n" 20951 "});\n"); 20952 verifyFormat("function([this, that] {\n" 20953 " //\n" 20954 "});\n"); 20955 // FIXME: this format is not ideal and we should consider forcing the first 20956 // arg onto its own line. 20957 verifyFormat("function(a, b, c, //\n" 20958 " d, [this, that] {\n" 20959 " //\n" 20960 " });\n"); 20961 20962 // Multiple lambdas are treated correctly even when there is a short arg0. 20963 verifyFormat("SomeFunction(\n" 20964 " 1,\n" 20965 " [this] {\n" 20966 " //\n" 20967 " },\n" 20968 " [this] {\n" 20969 " //\n" 20970 " },\n" 20971 " 1);\n"); 20972 20973 // More complex introducers. 20974 verifyFormat("return [i, args...] {};"); 20975 20976 // Not lambdas. 20977 verifyFormat("constexpr char hello[]{\"hello\"};"); 20978 verifyFormat("double &operator[](int i) { return 0; }\n" 20979 "int i;"); 20980 verifyFormat("std::unique_ptr<int[]> foo() {}"); 20981 verifyFormat("int i = a[a][a]->f();"); 20982 verifyFormat("int i = (*b)[a]->f();"); 20983 20984 // Other corner cases. 20985 verifyFormat("void f() {\n" 20986 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 20987 " );\n" 20988 "}"); 20989 20990 // Lambdas created through weird macros. 20991 verifyFormat("void f() {\n" 20992 " MACRO((const AA &a) { return 1; });\n" 20993 " MACRO((AA &a) { return 1; });\n" 20994 "}"); 20995 20996 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 20997 " doo_dah();\n" 20998 " doo_dah();\n" 20999 " })) {\n" 21000 "}"); 21001 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 21002 " doo_dah();\n" 21003 " doo_dah();\n" 21004 " })) {\n" 21005 "}"); 21006 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 21007 " doo_dah();\n" 21008 " doo_dah();\n" 21009 " })) {\n" 21010 "}"); 21011 verifyFormat("auto lambda = []() {\n" 21012 " int a = 2\n" 21013 "#if A\n" 21014 " + 2\n" 21015 "#endif\n" 21016 " ;\n" 21017 "};"); 21018 21019 // Lambdas with complex multiline introducers. 21020 verifyFormat( 21021 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21022 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 21023 " -> ::std::unordered_set<\n" 21024 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 21025 " //\n" 21026 " });"); 21027 21028 FormatStyle DoNotMerge = getLLVMStyle(); 21029 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 21030 verifyFormat("auto c = []() {\n" 21031 " return b;\n" 21032 "};", 21033 "auto c = []() { return b; };", DoNotMerge); 21034 verifyFormat("auto c = []() {\n" 21035 "};", 21036 " auto c = []() {};", DoNotMerge); 21037 21038 FormatStyle MergeEmptyOnly = getLLVMStyle(); 21039 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 21040 verifyFormat("auto c = []() {\n" 21041 " return b;\n" 21042 "};", 21043 "auto c = []() {\n" 21044 " return b;\n" 21045 " };", 21046 MergeEmptyOnly); 21047 verifyFormat("auto c = []() {};", 21048 "auto c = []() {\n" 21049 "};", 21050 MergeEmptyOnly); 21051 21052 FormatStyle MergeInline = getLLVMStyle(); 21053 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 21054 verifyFormat("auto c = []() {\n" 21055 " return b;\n" 21056 "};", 21057 "auto c = []() { return b; };", MergeInline); 21058 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 21059 MergeInline); 21060 verifyFormat("function([]() { return b; }, a)", 21061 "function([]() { return b; }, a)", MergeInline); 21062 verifyFormat("function(a, []() { return b; })", 21063 "function(a, []() { return b; })", MergeInline); 21064 21065 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 21066 // AllowShortLambdasOnASingleLine 21067 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 21068 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 21069 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 21070 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21071 FormatStyle::ShortLambdaStyle::SLS_None; 21072 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 21073 " []()\n" 21074 " {\n" 21075 " return 17;\n" 21076 " });", 21077 LLVMWithBeforeLambdaBody); 21078 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 21079 " []()\n" 21080 " {\n" 21081 " });", 21082 LLVMWithBeforeLambdaBody); 21083 verifyFormat("auto fct_SLS_None = []()\n" 21084 "{\n" 21085 " return 17;\n" 21086 "};", 21087 LLVMWithBeforeLambdaBody); 21088 verifyFormat("TwoNestedLambdas_SLS_None(\n" 21089 " []()\n" 21090 " {\n" 21091 " return Call(\n" 21092 " []()\n" 21093 " {\n" 21094 " return 17;\n" 21095 " });\n" 21096 " });", 21097 LLVMWithBeforeLambdaBody); 21098 verifyFormat("void Fct() {\n" 21099 " return {[]()\n" 21100 " {\n" 21101 " return 17;\n" 21102 " }};\n" 21103 "}", 21104 LLVMWithBeforeLambdaBody); 21105 21106 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21107 FormatStyle::ShortLambdaStyle::SLS_Empty; 21108 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 21109 " []()\n" 21110 " {\n" 21111 " return 17;\n" 21112 " });", 21113 LLVMWithBeforeLambdaBody); 21114 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 21115 LLVMWithBeforeLambdaBody); 21116 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 21117 "ongFunctionName_SLS_Empty(\n" 21118 " []() {});", 21119 LLVMWithBeforeLambdaBody); 21120 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 21121 " []()\n" 21122 " {\n" 21123 " return 17;\n" 21124 " });", 21125 LLVMWithBeforeLambdaBody); 21126 verifyFormat("auto fct_SLS_Empty = []()\n" 21127 "{\n" 21128 " return 17;\n" 21129 "};", 21130 LLVMWithBeforeLambdaBody); 21131 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 21132 " []()\n" 21133 " {\n" 21134 " return Call([]() {});\n" 21135 " });", 21136 LLVMWithBeforeLambdaBody); 21137 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 21138 " []()\n" 21139 " {\n" 21140 " return Call([]() {});\n" 21141 " });", 21142 LLVMWithBeforeLambdaBody); 21143 verifyFormat( 21144 "FctWithLongLineInLambda_SLS_Empty(\n" 21145 " []()\n" 21146 " {\n" 21147 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21148 " AndShouldNotBeConsiderAsInline,\n" 21149 " LambdaBodyMustBeBreak);\n" 21150 " });", 21151 LLVMWithBeforeLambdaBody); 21152 21153 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21154 FormatStyle::ShortLambdaStyle::SLS_Inline; 21155 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 21156 LLVMWithBeforeLambdaBody); 21157 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 21158 LLVMWithBeforeLambdaBody); 21159 verifyFormat("auto fct_SLS_Inline = []()\n" 21160 "{\n" 21161 " return 17;\n" 21162 "};", 21163 LLVMWithBeforeLambdaBody); 21164 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 21165 "17; }); });", 21166 LLVMWithBeforeLambdaBody); 21167 verifyFormat( 21168 "FctWithLongLineInLambda_SLS_Inline(\n" 21169 " []()\n" 21170 " {\n" 21171 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21172 " AndShouldNotBeConsiderAsInline,\n" 21173 " LambdaBodyMustBeBreak);\n" 21174 " });", 21175 LLVMWithBeforeLambdaBody); 21176 verifyFormat("FctWithMultipleParams_SLS_Inline(" 21177 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 21178 " []() { return 17; });", 21179 LLVMWithBeforeLambdaBody); 21180 verifyFormat( 21181 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 21182 LLVMWithBeforeLambdaBody); 21183 21184 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21185 FormatStyle::ShortLambdaStyle::SLS_All; 21186 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 21187 LLVMWithBeforeLambdaBody); 21188 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 21189 LLVMWithBeforeLambdaBody); 21190 verifyFormat("auto fct_SLS_All = []() { return 17; };", 21191 LLVMWithBeforeLambdaBody); 21192 verifyFormat("FctWithOneParam_SLS_All(\n" 21193 " []()\n" 21194 " {\n" 21195 " // A cool function...\n" 21196 " return 43;\n" 21197 " });", 21198 LLVMWithBeforeLambdaBody); 21199 verifyFormat("FctWithMultipleParams_SLS_All(" 21200 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 21201 " []() { return 17; });", 21202 LLVMWithBeforeLambdaBody); 21203 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 21204 LLVMWithBeforeLambdaBody); 21205 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 21206 LLVMWithBeforeLambdaBody); 21207 verifyFormat( 21208 "FctWithLongLineInLambda_SLS_All(\n" 21209 " []()\n" 21210 " {\n" 21211 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21212 " AndShouldNotBeConsiderAsInline,\n" 21213 " LambdaBodyMustBeBreak);\n" 21214 " });", 21215 LLVMWithBeforeLambdaBody); 21216 verifyFormat( 21217 "auto fct_SLS_All = []()\n" 21218 "{\n" 21219 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21220 " AndShouldNotBeConsiderAsInline,\n" 21221 " LambdaBodyMustBeBreak);\n" 21222 "};", 21223 LLVMWithBeforeLambdaBody); 21224 LLVMWithBeforeLambdaBody.BinPackParameters = false; 21225 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 21226 LLVMWithBeforeLambdaBody); 21227 verifyFormat( 21228 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 21229 " FirstParam,\n" 21230 " SecondParam,\n" 21231 " ThirdParam,\n" 21232 " FourthParam);", 21233 LLVMWithBeforeLambdaBody); 21234 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 21235 " []() { return " 21236 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 21237 " FirstParam,\n" 21238 " SecondParam,\n" 21239 " ThirdParam,\n" 21240 " FourthParam);", 21241 LLVMWithBeforeLambdaBody); 21242 verifyFormat( 21243 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 21244 " SecondParam,\n" 21245 " ThirdParam,\n" 21246 " FourthParam,\n" 21247 " []() { return SomeValueNotSoLong; });", 21248 LLVMWithBeforeLambdaBody); 21249 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 21250 " []()\n" 21251 " {\n" 21252 " return " 21253 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 21254 "eConsiderAsInline;\n" 21255 " });", 21256 LLVMWithBeforeLambdaBody); 21257 verifyFormat( 21258 "FctWithLongLineInLambda_SLS_All(\n" 21259 " []()\n" 21260 " {\n" 21261 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21262 " AndShouldNotBeConsiderAsInline,\n" 21263 " LambdaBodyMustBeBreak);\n" 21264 " });", 21265 LLVMWithBeforeLambdaBody); 21266 verifyFormat("FctWithTwoParams_SLS_All(\n" 21267 " []()\n" 21268 " {\n" 21269 " // A cool function...\n" 21270 " return 43;\n" 21271 " },\n" 21272 " 87);", 21273 LLVMWithBeforeLambdaBody); 21274 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 21275 LLVMWithBeforeLambdaBody); 21276 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 21277 LLVMWithBeforeLambdaBody); 21278 verifyFormat( 21279 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 21280 LLVMWithBeforeLambdaBody); 21281 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 21282 "}); }, x);", 21283 LLVMWithBeforeLambdaBody); 21284 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21285 " []()\n" 21286 " {\n" 21287 " // A cool function...\n" 21288 " return Call([]() { return 17; });\n" 21289 " });", 21290 LLVMWithBeforeLambdaBody); 21291 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21292 " []()\n" 21293 " {\n" 21294 " return Call(\n" 21295 " []()\n" 21296 " {\n" 21297 " // A cool function...\n" 21298 " return 17;\n" 21299 " });\n" 21300 " });", 21301 LLVMWithBeforeLambdaBody); 21302 21303 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21304 FormatStyle::ShortLambdaStyle::SLS_None; 21305 21306 verifyFormat("auto select = [this]() -> const Library::Object *\n" 21307 "{\n" 21308 " return MyAssignment::SelectFromList(this);\n" 21309 "};\n", 21310 LLVMWithBeforeLambdaBody); 21311 21312 verifyFormat("auto select = [this]() -> const Library::Object &\n" 21313 "{\n" 21314 " return MyAssignment::SelectFromList(this);\n" 21315 "};\n", 21316 LLVMWithBeforeLambdaBody); 21317 21318 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 21319 "{\n" 21320 " return MyAssignment::SelectFromList(this);\n" 21321 "};\n", 21322 LLVMWithBeforeLambdaBody); 21323 21324 verifyFormat("namespace test {\n" 21325 "class Test {\n" 21326 "public:\n" 21327 " Test() = default;\n" 21328 "};\n" 21329 "} // namespace test", 21330 LLVMWithBeforeLambdaBody); 21331 21332 // Lambdas with different indentation styles. 21333 Style = getLLVMStyleWithColumns(100); 21334 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21335 " return promise.then(\n" 21336 " [this, &someVariable, someObject = " 21337 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21338 " return someObject.startAsyncAction().then(\n" 21339 " [this, &someVariable](AsyncActionResult result) " 21340 "mutable { result.processMore(); });\n" 21341 " });\n" 21342 "}\n", 21343 format("SomeResult doSomething(SomeObject promise) {\n" 21344 " return promise.then([this, &someVariable, someObject = " 21345 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21346 " return someObject.startAsyncAction().then([this, " 21347 "&someVariable](AsyncActionResult result) mutable {\n" 21348 " result.processMore();\n" 21349 " });\n" 21350 " });\n" 21351 "}\n", 21352 Style)); 21353 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21354 verifyFormat("test() {\n" 21355 " ([]() -> {\n" 21356 " int b = 32;\n" 21357 " return 3;\n" 21358 " }).foo();\n" 21359 "}", 21360 Style); 21361 verifyFormat("test() {\n" 21362 " []() -> {\n" 21363 " int b = 32;\n" 21364 " return 3;\n" 21365 " }\n" 21366 "}", 21367 Style); 21368 verifyFormat("std::sort(v.begin(), v.end(),\n" 21369 " [](const auto &someLongArgumentName, const auto " 21370 "&someOtherLongArgumentName) {\n" 21371 " return someLongArgumentName.someMemberVariable < " 21372 "someOtherLongArgumentName.someMemberVariable;\n" 21373 "});", 21374 Style); 21375 verifyFormat("test() {\n" 21376 " (\n" 21377 " []() -> {\n" 21378 " int b = 32;\n" 21379 " return 3;\n" 21380 " },\n" 21381 " foo, bar)\n" 21382 " .foo();\n" 21383 "}", 21384 Style); 21385 verifyFormat("test() {\n" 21386 " ([]() -> {\n" 21387 " int b = 32;\n" 21388 " return 3;\n" 21389 " })\n" 21390 " .foo()\n" 21391 " .bar();\n" 21392 "}", 21393 Style); 21394 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21395 " return promise.then(\n" 21396 " [this, &someVariable, someObject = " 21397 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21398 " return someObject.startAsyncAction().then(\n" 21399 " [this, &someVariable](AsyncActionResult result) mutable { " 21400 "result.processMore(); });\n" 21401 " });\n" 21402 "}\n", 21403 format("SomeResult doSomething(SomeObject promise) {\n" 21404 " return promise.then([this, &someVariable, someObject = " 21405 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21406 " return someObject.startAsyncAction().then([this, " 21407 "&someVariable](AsyncActionResult result) mutable {\n" 21408 " result.processMore();\n" 21409 " });\n" 21410 " });\n" 21411 "}\n", 21412 Style)); 21413 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21414 " return promise.then([this, &someVariable] {\n" 21415 " return someObject.startAsyncAction().then(\n" 21416 " [this, &someVariable](AsyncActionResult result) mutable { " 21417 "result.processMore(); });\n" 21418 " });\n" 21419 "}\n", 21420 format("SomeResult doSomething(SomeObject promise) {\n" 21421 " return promise.then([this, &someVariable] {\n" 21422 " return someObject.startAsyncAction().then([this, " 21423 "&someVariable](AsyncActionResult result) mutable {\n" 21424 " result.processMore();\n" 21425 " });\n" 21426 " });\n" 21427 "}\n", 21428 Style)); 21429 Style = getGoogleStyle(); 21430 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21431 EXPECT_EQ("#define A \\\n" 21432 " [] { \\\n" 21433 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 21434 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 21435 " }", 21436 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 21437 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 21438 Style)); 21439 // TODO: The current formatting has a minor issue that's not worth fixing 21440 // right now whereby the closing brace is indented relative to the signature 21441 // instead of being aligned. This only happens with macros. 21442 } 21443 21444 TEST_F(FormatTest, LambdaWithLineComments) { 21445 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 21446 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 21447 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 21448 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21449 FormatStyle::ShortLambdaStyle::SLS_All; 21450 21451 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 21452 verifyFormat("auto k = []() // comment\n" 21453 "{ return; }", 21454 LLVMWithBeforeLambdaBody); 21455 verifyFormat("auto k = []() /* comment */ { return; }", 21456 LLVMWithBeforeLambdaBody); 21457 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 21458 LLVMWithBeforeLambdaBody); 21459 verifyFormat("auto k = []() // X\n" 21460 "{ return; }", 21461 LLVMWithBeforeLambdaBody); 21462 verifyFormat( 21463 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 21464 "{ return; }", 21465 LLVMWithBeforeLambdaBody); 21466 } 21467 21468 TEST_F(FormatTest, EmptyLinesInLambdas) { 21469 verifyFormat("auto lambda = []() {\n" 21470 " x(); //\n" 21471 "};", 21472 "auto lambda = []() {\n" 21473 "\n" 21474 " x(); //\n" 21475 "\n" 21476 "};"); 21477 } 21478 21479 TEST_F(FormatTest, FormatsBlocks) { 21480 FormatStyle ShortBlocks = getLLVMStyle(); 21481 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21482 verifyFormat("int (^Block)(int, int);", ShortBlocks); 21483 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 21484 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 21485 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 21486 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 21487 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 21488 21489 verifyFormat("foo(^{ bar(); });", ShortBlocks); 21490 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 21491 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 21492 21493 verifyFormat("[operation setCompletionBlock:^{\n" 21494 " [self onOperationDone];\n" 21495 "}];"); 21496 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 21497 " [self onOperationDone];\n" 21498 "}]};"); 21499 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 21500 " f();\n" 21501 "}];"); 21502 verifyFormat("int a = [operation block:^int(int *i) {\n" 21503 " return 1;\n" 21504 "}];"); 21505 verifyFormat("[myObject doSomethingWith:arg1\n" 21506 " aaa:^int(int *a) {\n" 21507 " return 1;\n" 21508 " }\n" 21509 " bbb:f(a * bbbbbbbb)];"); 21510 21511 verifyFormat("[operation setCompletionBlock:^{\n" 21512 " [self.delegate newDataAvailable];\n" 21513 "}];", 21514 getLLVMStyleWithColumns(60)); 21515 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 21516 " NSString *path = [self sessionFilePath];\n" 21517 " if (path) {\n" 21518 " // ...\n" 21519 " }\n" 21520 "});"); 21521 verifyFormat("[[SessionService sharedService]\n" 21522 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21523 " if (window) {\n" 21524 " [self windowDidLoad:window];\n" 21525 " } else {\n" 21526 " [self errorLoadingWindow];\n" 21527 " }\n" 21528 " }];"); 21529 verifyFormat("void (^largeBlock)(void) = ^{\n" 21530 " // ...\n" 21531 "};\n", 21532 getLLVMStyleWithColumns(40)); 21533 verifyFormat("[[SessionService sharedService]\n" 21534 " loadWindowWithCompletionBlock: //\n" 21535 " ^(SessionWindow *window) {\n" 21536 " if (window) {\n" 21537 " [self windowDidLoad:window];\n" 21538 " } else {\n" 21539 " [self errorLoadingWindow];\n" 21540 " }\n" 21541 " }];", 21542 getLLVMStyleWithColumns(60)); 21543 verifyFormat("[myObject doSomethingWith:arg1\n" 21544 " firstBlock:^(Foo *a) {\n" 21545 " // ...\n" 21546 " int i;\n" 21547 " }\n" 21548 " secondBlock:^(Bar *b) {\n" 21549 " // ...\n" 21550 " int i;\n" 21551 " }\n" 21552 " thirdBlock:^Foo(Bar *b) {\n" 21553 " // ...\n" 21554 " int i;\n" 21555 " }];"); 21556 verifyFormat("[myObject doSomethingWith:arg1\n" 21557 " firstBlock:-1\n" 21558 " secondBlock:^(Bar *b) {\n" 21559 " // ...\n" 21560 " int i;\n" 21561 " }];"); 21562 21563 verifyFormat("f(^{\n" 21564 " @autoreleasepool {\n" 21565 " if (a) {\n" 21566 " g();\n" 21567 " }\n" 21568 " }\n" 21569 "});"); 21570 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 21571 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 21572 "};"); 21573 21574 FormatStyle FourIndent = getLLVMStyle(); 21575 FourIndent.ObjCBlockIndentWidth = 4; 21576 verifyFormat("[operation setCompletionBlock:^{\n" 21577 " [self onOperationDone];\n" 21578 "}];", 21579 FourIndent); 21580 } 21581 21582 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 21583 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 21584 21585 verifyFormat("[[SessionService sharedService] " 21586 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21587 " if (window) {\n" 21588 " [self windowDidLoad:window];\n" 21589 " } else {\n" 21590 " [self errorLoadingWindow];\n" 21591 " }\n" 21592 "}];", 21593 ZeroColumn); 21594 EXPECT_EQ("[[SessionService sharedService]\n" 21595 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21596 " if (window) {\n" 21597 " [self windowDidLoad:window];\n" 21598 " } else {\n" 21599 " [self errorLoadingWindow];\n" 21600 " }\n" 21601 " }];", 21602 format("[[SessionService sharedService]\n" 21603 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21604 " if (window) {\n" 21605 " [self windowDidLoad:window];\n" 21606 " } else {\n" 21607 " [self errorLoadingWindow];\n" 21608 " }\n" 21609 "}];", 21610 ZeroColumn)); 21611 verifyFormat("[myObject doSomethingWith:arg1\n" 21612 " firstBlock:^(Foo *a) {\n" 21613 " // ...\n" 21614 " int i;\n" 21615 " }\n" 21616 " secondBlock:^(Bar *b) {\n" 21617 " // ...\n" 21618 " int i;\n" 21619 " }\n" 21620 " thirdBlock:^Foo(Bar *b) {\n" 21621 " // ...\n" 21622 " int i;\n" 21623 " }];", 21624 ZeroColumn); 21625 verifyFormat("f(^{\n" 21626 " @autoreleasepool {\n" 21627 " if (a) {\n" 21628 " g();\n" 21629 " }\n" 21630 " }\n" 21631 "});", 21632 ZeroColumn); 21633 verifyFormat("void (^largeBlock)(void) = ^{\n" 21634 " // ...\n" 21635 "};", 21636 ZeroColumn); 21637 21638 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21639 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 21640 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21641 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 21642 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 21643 " int i;\n" 21644 "};", 21645 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21646 } 21647 21648 TEST_F(FormatTest, SupportsCRLF) { 21649 EXPECT_EQ("int a;\r\n" 21650 "int b;\r\n" 21651 "int c;\r\n", 21652 format("int a;\r\n" 21653 " int b;\r\n" 21654 " int c;\r\n", 21655 getLLVMStyle())); 21656 EXPECT_EQ("int a;\r\n" 21657 "int b;\r\n" 21658 "int c;\r\n", 21659 format("int a;\r\n" 21660 " int b;\n" 21661 " int c;\r\n", 21662 getLLVMStyle())); 21663 EXPECT_EQ("int a;\n" 21664 "int b;\n" 21665 "int c;\n", 21666 format("int a;\r\n" 21667 " int b;\n" 21668 " int c;\n", 21669 getLLVMStyle())); 21670 EXPECT_EQ("\"aaaaaaa \"\r\n" 21671 "\"bbbbbbb\";\r\n", 21672 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 21673 EXPECT_EQ("#define A \\\r\n" 21674 " b; \\\r\n" 21675 " c; \\\r\n" 21676 " d;\r\n", 21677 format("#define A \\\r\n" 21678 " b; \\\r\n" 21679 " c; d; \r\n", 21680 getGoogleStyle())); 21681 21682 EXPECT_EQ("/*\r\n" 21683 "multi line block comments\r\n" 21684 "should not introduce\r\n" 21685 "an extra carriage return\r\n" 21686 "*/\r\n", 21687 format("/*\r\n" 21688 "multi line block comments\r\n" 21689 "should not introduce\r\n" 21690 "an extra carriage return\r\n" 21691 "*/\r\n")); 21692 EXPECT_EQ("/*\r\n" 21693 "\r\n" 21694 "*/", 21695 format("/*\r\n" 21696 " \r\r\r\n" 21697 "*/")); 21698 21699 FormatStyle style = getLLVMStyle(); 21700 21701 style.DeriveLineEnding = true; 21702 style.UseCRLF = false; 21703 EXPECT_EQ("union FooBarBazQux {\n" 21704 " int foo;\n" 21705 " int bar;\n" 21706 " int baz;\n" 21707 "};", 21708 format("union FooBarBazQux {\r\n" 21709 " int foo;\n" 21710 " int bar;\r\n" 21711 " int baz;\n" 21712 "};", 21713 style)); 21714 style.UseCRLF = true; 21715 EXPECT_EQ("union FooBarBazQux {\r\n" 21716 " int foo;\r\n" 21717 " int bar;\r\n" 21718 " int baz;\r\n" 21719 "};", 21720 format("union FooBarBazQux {\r\n" 21721 " int foo;\n" 21722 " int bar;\r\n" 21723 " int baz;\n" 21724 "};", 21725 style)); 21726 21727 style.DeriveLineEnding = false; 21728 style.UseCRLF = false; 21729 EXPECT_EQ("union FooBarBazQux {\n" 21730 " int foo;\n" 21731 " int bar;\n" 21732 " int baz;\n" 21733 " int qux;\n" 21734 "};", 21735 format("union FooBarBazQux {\r\n" 21736 " int foo;\n" 21737 " int bar;\r\n" 21738 " int baz;\n" 21739 " int qux;\r\n" 21740 "};", 21741 style)); 21742 style.UseCRLF = true; 21743 EXPECT_EQ("union FooBarBazQux {\r\n" 21744 " int foo;\r\n" 21745 " int bar;\r\n" 21746 " int baz;\r\n" 21747 " int qux;\r\n" 21748 "};", 21749 format("union FooBarBazQux {\r\n" 21750 " int foo;\n" 21751 " int bar;\r\n" 21752 " int baz;\n" 21753 " int qux;\n" 21754 "};", 21755 style)); 21756 21757 style.DeriveLineEnding = true; 21758 style.UseCRLF = false; 21759 EXPECT_EQ("union FooBarBazQux {\r\n" 21760 " int foo;\r\n" 21761 " int bar;\r\n" 21762 " int baz;\r\n" 21763 " int qux;\r\n" 21764 "};", 21765 format("union FooBarBazQux {\r\n" 21766 " int foo;\n" 21767 " int bar;\r\n" 21768 " int baz;\n" 21769 " int qux;\r\n" 21770 "};", 21771 style)); 21772 style.UseCRLF = true; 21773 EXPECT_EQ("union FooBarBazQux {\n" 21774 " int foo;\n" 21775 " int bar;\n" 21776 " int baz;\n" 21777 " int qux;\n" 21778 "};", 21779 format("union FooBarBazQux {\r\n" 21780 " int foo;\n" 21781 " int bar;\r\n" 21782 " int baz;\n" 21783 " int qux;\n" 21784 "};", 21785 style)); 21786 } 21787 21788 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 21789 verifyFormat("MY_CLASS(C) {\n" 21790 " int i;\n" 21791 " int j;\n" 21792 "};"); 21793 } 21794 21795 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 21796 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 21797 TwoIndent.ContinuationIndentWidth = 2; 21798 21799 EXPECT_EQ("int i =\n" 21800 " longFunction(\n" 21801 " arg);", 21802 format("int i = longFunction(arg);", TwoIndent)); 21803 21804 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 21805 SixIndent.ContinuationIndentWidth = 6; 21806 21807 EXPECT_EQ("int i =\n" 21808 " longFunction(\n" 21809 " arg);", 21810 format("int i = longFunction(arg);", SixIndent)); 21811 } 21812 21813 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 21814 FormatStyle Style = getLLVMStyle(); 21815 verifyFormat("int Foo::getter(\n" 21816 " //\n" 21817 ") const {\n" 21818 " return foo;\n" 21819 "}", 21820 Style); 21821 verifyFormat("void Foo::setter(\n" 21822 " //\n" 21823 ") {\n" 21824 " foo = 1;\n" 21825 "}", 21826 Style); 21827 } 21828 21829 TEST_F(FormatTest, SpacesInAngles) { 21830 FormatStyle Spaces = getLLVMStyle(); 21831 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21832 21833 verifyFormat("vector< ::std::string > x1;", Spaces); 21834 verifyFormat("Foo< int, Bar > x2;", Spaces); 21835 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 21836 21837 verifyFormat("static_cast< int >(arg);", Spaces); 21838 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 21839 verifyFormat("f< int, float >();", Spaces); 21840 verifyFormat("template <> g() {}", Spaces); 21841 verifyFormat("template < std::vector< int > > f() {}", Spaces); 21842 verifyFormat("std::function< void(int, int) > fct;", Spaces); 21843 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 21844 Spaces); 21845 21846 Spaces.Standard = FormatStyle::LS_Cpp03; 21847 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21848 verifyFormat("A< A< int > >();", Spaces); 21849 21850 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21851 verifyFormat("A<A<int> >();", Spaces); 21852 21853 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21854 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 21855 Spaces); 21856 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 21857 Spaces); 21858 21859 verifyFormat("A<A<int> >();", Spaces); 21860 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 21861 verifyFormat("A< A< int > >();", Spaces); 21862 21863 Spaces.Standard = FormatStyle::LS_Cpp11; 21864 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21865 verifyFormat("A< A< int > >();", Spaces); 21866 21867 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21868 verifyFormat("vector<::std::string> x4;", Spaces); 21869 verifyFormat("vector<int> x5;", Spaces); 21870 verifyFormat("Foo<int, Bar> x6;", Spaces); 21871 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21872 21873 verifyFormat("A<A<int>>();", Spaces); 21874 21875 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21876 verifyFormat("vector<::std::string> x4;", Spaces); 21877 verifyFormat("vector< ::std::string > x4;", Spaces); 21878 verifyFormat("vector<int> x5;", Spaces); 21879 verifyFormat("vector< int > x5;", Spaces); 21880 verifyFormat("Foo<int, Bar> x6;", Spaces); 21881 verifyFormat("Foo< int, Bar > x6;", Spaces); 21882 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21883 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 21884 21885 verifyFormat("A<A<int>>();", Spaces); 21886 verifyFormat("A< A< int > >();", Spaces); 21887 verifyFormat("A<A<int > >();", Spaces); 21888 verifyFormat("A< A< int>>();", Spaces); 21889 21890 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21891 verifyFormat("// clang-format off\n" 21892 "foo<<<1, 1>>>();\n" 21893 "// clang-format on\n", 21894 Spaces); 21895 verifyFormat("// clang-format off\n" 21896 "foo< < <1, 1> > >();\n" 21897 "// clang-format on\n", 21898 Spaces); 21899 } 21900 21901 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 21902 FormatStyle Style = getLLVMStyle(); 21903 Style.SpaceAfterTemplateKeyword = false; 21904 verifyFormat("template<int> void foo();", Style); 21905 } 21906 21907 TEST_F(FormatTest, TripleAngleBrackets) { 21908 verifyFormat("f<<<1, 1>>>();"); 21909 verifyFormat("f<<<1, 1, 1, s>>>();"); 21910 verifyFormat("f<<<a, b, c, d>>>();"); 21911 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 21912 verifyFormat("f<param><<<1, 1>>>();"); 21913 verifyFormat("f<1><<<1, 1>>>();"); 21914 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 21915 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21916 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 21917 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 21918 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 21919 } 21920 21921 TEST_F(FormatTest, MergeLessLessAtEnd) { 21922 verifyFormat("<<"); 21923 EXPECT_EQ("< < <", format("\\\n<<<")); 21924 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21925 "aaallvm::outs() <<"); 21926 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21927 "aaaallvm::outs()\n <<"); 21928 } 21929 21930 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 21931 std::string code = "#if A\n" 21932 "#if B\n" 21933 "a.\n" 21934 "#endif\n" 21935 " a = 1;\n" 21936 "#else\n" 21937 "#endif\n" 21938 "#if C\n" 21939 "#else\n" 21940 "#endif\n"; 21941 EXPECT_EQ(code, format(code)); 21942 } 21943 21944 TEST_F(FormatTest, HandleConflictMarkers) { 21945 // Git/SVN conflict markers. 21946 EXPECT_EQ("int a;\n" 21947 "void f() {\n" 21948 " callme(some(parameter1,\n" 21949 "<<<<<<< text by the vcs\n" 21950 " parameter2),\n" 21951 "||||||| text by the vcs\n" 21952 " parameter2),\n" 21953 " parameter3,\n" 21954 "======= text by the vcs\n" 21955 " parameter2, parameter3),\n" 21956 ">>>>>>> text by the vcs\n" 21957 " otherparameter);\n", 21958 format("int a;\n" 21959 "void f() {\n" 21960 " callme(some(parameter1,\n" 21961 "<<<<<<< text by the vcs\n" 21962 " parameter2),\n" 21963 "||||||| text by the vcs\n" 21964 " parameter2),\n" 21965 " parameter3,\n" 21966 "======= text by the vcs\n" 21967 " parameter2,\n" 21968 " parameter3),\n" 21969 ">>>>>>> text by the vcs\n" 21970 " otherparameter);\n")); 21971 21972 // Perforce markers. 21973 EXPECT_EQ("void f() {\n" 21974 " function(\n" 21975 ">>>> text by the vcs\n" 21976 " parameter,\n" 21977 "==== text by the vcs\n" 21978 " parameter,\n" 21979 "==== text by the vcs\n" 21980 " parameter,\n" 21981 "<<<< text by the vcs\n" 21982 " parameter);\n", 21983 format("void f() {\n" 21984 " function(\n" 21985 ">>>> text by the vcs\n" 21986 " parameter,\n" 21987 "==== text by the vcs\n" 21988 " parameter,\n" 21989 "==== text by the vcs\n" 21990 " parameter,\n" 21991 "<<<< text by the vcs\n" 21992 " parameter);\n")); 21993 21994 EXPECT_EQ("<<<<<<<\n" 21995 "|||||||\n" 21996 "=======\n" 21997 ">>>>>>>", 21998 format("<<<<<<<\n" 21999 "|||||||\n" 22000 "=======\n" 22001 ">>>>>>>")); 22002 22003 EXPECT_EQ("<<<<<<<\n" 22004 "|||||||\n" 22005 "int i;\n" 22006 "=======\n" 22007 ">>>>>>>", 22008 format("<<<<<<<\n" 22009 "|||||||\n" 22010 "int i;\n" 22011 "=======\n" 22012 ">>>>>>>")); 22013 22014 // FIXME: Handle parsing of macros around conflict markers correctly: 22015 EXPECT_EQ("#define Macro \\\n" 22016 "<<<<<<<\n" 22017 "Something \\\n" 22018 "|||||||\n" 22019 "Else \\\n" 22020 "=======\n" 22021 "Other \\\n" 22022 ">>>>>>>\n" 22023 " End int i;\n", 22024 format("#define Macro \\\n" 22025 "<<<<<<<\n" 22026 " Something \\\n" 22027 "|||||||\n" 22028 " Else \\\n" 22029 "=======\n" 22030 " Other \\\n" 22031 ">>>>>>>\n" 22032 " End\n" 22033 "int i;\n")); 22034 22035 verifyFormat(R"(==== 22036 #ifdef A 22037 a 22038 #else 22039 b 22040 #endif 22041 )"); 22042 } 22043 22044 TEST_F(FormatTest, DisableRegions) { 22045 EXPECT_EQ("int i;\n" 22046 "// clang-format off\n" 22047 " int j;\n" 22048 "// clang-format on\n" 22049 "int k;", 22050 format(" int i;\n" 22051 " // clang-format off\n" 22052 " int j;\n" 22053 " // clang-format on\n" 22054 " int k;")); 22055 EXPECT_EQ("int i;\n" 22056 "/* clang-format off */\n" 22057 " int j;\n" 22058 "/* clang-format on */\n" 22059 "int k;", 22060 format(" int i;\n" 22061 " /* clang-format off */\n" 22062 " int j;\n" 22063 " /* clang-format on */\n" 22064 " int k;")); 22065 22066 // Don't reflow comments within disabled regions. 22067 EXPECT_EQ("// clang-format off\n" 22068 "// long long long long long long line\n" 22069 "/* clang-format on */\n" 22070 "/* long long long\n" 22071 " * long long long\n" 22072 " * line */\n" 22073 "int i;\n" 22074 "/* clang-format off */\n" 22075 "/* long long long long long long line */\n", 22076 format("// clang-format off\n" 22077 "// long long long long long long line\n" 22078 "/* clang-format on */\n" 22079 "/* long long long long long long line */\n" 22080 "int i;\n" 22081 "/* clang-format off */\n" 22082 "/* long long long long long long line */\n", 22083 getLLVMStyleWithColumns(20))); 22084 } 22085 22086 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 22087 format("? ) ="); 22088 verifyNoCrash("#define a\\\n /**/}"); 22089 } 22090 22091 TEST_F(FormatTest, FormatsTableGenCode) { 22092 FormatStyle Style = getLLVMStyle(); 22093 Style.Language = FormatStyle::LK_TableGen; 22094 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 22095 } 22096 22097 TEST_F(FormatTest, ArrayOfTemplates) { 22098 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 22099 format("auto a = new unique_ptr<int > [ 10];")); 22100 22101 FormatStyle Spaces = getLLVMStyle(); 22102 Spaces.SpacesInSquareBrackets = true; 22103 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 22104 format("auto a = new unique_ptr<int > [10];", Spaces)); 22105 } 22106 22107 TEST_F(FormatTest, ArrayAsTemplateType) { 22108 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 22109 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 22110 22111 FormatStyle Spaces = getLLVMStyle(); 22112 Spaces.SpacesInSquareBrackets = true; 22113 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 22114 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 22115 } 22116 22117 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 22118 22119 TEST(FormatStyle, GetStyleWithEmptyFileName) { 22120 llvm::vfs::InMemoryFileSystem FS; 22121 auto Style1 = getStyle("file", "", "Google", "", &FS); 22122 ASSERT_TRUE((bool)Style1); 22123 ASSERT_EQ(*Style1, getGoogleStyle()); 22124 } 22125 22126 TEST(FormatStyle, GetStyleOfFile) { 22127 llvm::vfs::InMemoryFileSystem FS; 22128 // Test 1: format file in the same directory. 22129 ASSERT_TRUE( 22130 FS.addFile("/a/.clang-format", 0, 22131 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 22132 ASSERT_TRUE( 22133 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22134 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 22135 ASSERT_TRUE((bool)Style1); 22136 ASSERT_EQ(*Style1, getLLVMStyle()); 22137 22138 // Test 2.1: fallback to default. 22139 ASSERT_TRUE( 22140 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22141 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 22142 ASSERT_TRUE((bool)Style2); 22143 ASSERT_EQ(*Style2, getMozillaStyle()); 22144 22145 // Test 2.2: no format on 'none' fallback style. 22146 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 22147 ASSERT_TRUE((bool)Style2); 22148 ASSERT_EQ(*Style2, getNoStyle()); 22149 22150 // Test 2.3: format if config is found with no based style while fallback is 22151 // 'none'. 22152 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 22153 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 22154 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 22155 ASSERT_TRUE((bool)Style2); 22156 ASSERT_EQ(*Style2, getLLVMStyle()); 22157 22158 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 22159 Style2 = getStyle("{}", "a.h", "none", "", &FS); 22160 ASSERT_TRUE((bool)Style2); 22161 ASSERT_EQ(*Style2, getLLVMStyle()); 22162 22163 // Test 3: format file in parent directory. 22164 ASSERT_TRUE( 22165 FS.addFile("/c/.clang-format", 0, 22166 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22167 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 22168 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22169 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22170 ASSERT_TRUE((bool)Style3); 22171 ASSERT_EQ(*Style3, getGoogleStyle()); 22172 22173 // Test 4: error on invalid fallback style 22174 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 22175 ASSERT_FALSE((bool)Style4); 22176 llvm::consumeError(Style4.takeError()); 22177 22178 // Test 5: error on invalid yaml on command line 22179 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 22180 ASSERT_FALSE((bool)Style5); 22181 llvm::consumeError(Style5.takeError()); 22182 22183 // Test 6: error on invalid style 22184 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 22185 ASSERT_FALSE((bool)Style6); 22186 llvm::consumeError(Style6.takeError()); 22187 22188 // Test 7: found config file, error on parsing it 22189 ASSERT_TRUE( 22190 FS.addFile("/d/.clang-format", 0, 22191 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 22192 "InvalidKey: InvalidValue"))); 22193 ASSERT_TRUE( 22194 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22195 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 22196 ASSERT_FALSE((bool)Style7a); 22197 llvm::consumeError(Style7a.takeError()); 22198 22199 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 22200 ASSERT_TRUE((bool)Style7b); 22201 22202 // Test 8: inferred per-language defaults apply. 22203 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 22204 ASSERT_TRUE((bool)StyleTd); 22205 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 22206 22207 // Test 9.1.1: overwriting a file style, when no parent file exists with no 22208 // fallback style. 22209 ASSERT_TRUE(FS.addFile( 22210 "/e/sub/.clang-format", 0, 22211 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 22212 "ColumnLimit: 20"))); 22213 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 22214 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22215 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 22216 ASSERT_TRUE(static_cast<bool>(Style9)); 22217 ASSERT_EQ(*Style9, [] { 22218 auto Style = getNoStyle(); 22219 Style.ColumnLimit = 20; 22220 return Style; 22221 }()); 22222 22223 // Test 9.1.2: propagate more than one level with no parent file. 22224 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 22225 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22226 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 22227 llvm::MemoryBuffer::getMemBuffer( 22228 "BasedOnStyle: InheritParentConfig\n" 22229 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 22230 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 22231 22232 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22233 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22234 ASSERT_TRUE(static_cast<bool>(Style9)); 22235 ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] { 22236 auto Style = getNoStyle(); 22237 Style.ColumnLimit = 20; 22238 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22239 return Style; 22240 }()); 22241 22242 // Test 9.2: with LLVM fallback style 22243 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 22244 ASSERT_TRUE(static_cast<bool>(Style9)); 22245 ASSERT_EQ(*Style9, [] { 22246 auto Style = getLLVMStyle(); 22247 Style.ColumnLimit = 20; 22248 return Style; 22249 }()); 22250 22251 // Test 9.3: with a parent file 22252 ASSERT_TRUE( 22253 FS.addFile("/e/.clang-format", 0, 22254 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 22255 "UseTab: Always"))); 22256 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 22257 ASSERT_TRUE(static_cast<bool>(Style9)); 22258 ASSERT_EQ(*Style9, [] { 22259 auto Style = getGoogleStyle(); 22260 Style.ColumnLimit = 20; 22261 Style.UseTab = FormatStyle::UT_Always; 22262 return Style; 22263 }()); 22264 22265 // Test 9.4: propagate more than one level with a parent file. 22266 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 22267 auto Style = getGoogleStyle(); 22268 Style.ColumnLimit = 20; 22269 Style.UseTab = FormatStyle::UT_Always; 22270 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22271 return Style; 22272 }(); 22273 22274 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22275 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22276 ASSERT_TRUE(static_cast<bool>(Style9)); 22277 ASSERT_EQ(*Style9, SubSubStyle); 22278 22279 // Test 9.5: use InheritParentConfig as style name 22280 Style9 = 22281 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 22282 ASSERT_TRUE(static_cast<bool>(Style9)); 22283 ASSERT_EQ(*Style9, SubSubStyle); 22284 22285 // Test 9.6: use command line style with inheritance 22286 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 22287 "none", "", &FS); 22288 ASSERT_TRUE(static_cast<bool>(Style9)); 22289 ASSERT_EQ(*Style9, SubSubStyle); 22290 22291 // Test 9.7: use command line style with inheritance and own config 22292 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 22293 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 22294 "/e/sub/code.cpp", "none", "", &FS); 22295 ASSERT_TRUE(static_cast<bool>(Style9)); 22296 ASSERT_EQ(*Style9, SubSubStyle); 22297 22298 // Test 9.8: use inheritance from a file without BasedOnStyle 22299 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 22300 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 22301 ASSERT_TRUE( 22302 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 22303 llvm::MemoryBuffer::getMemBuffer( 22304 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 22305 // Make sure we do not use the fallback style 22306 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 22307 ASSERT_TRUE(static_cast<bool>(Style9)); 22308 ASSERT_EQ(*Style9, [] { 22309 auto Style = getLLVMStyle(); 22310 Style.ColumnLimit = 123; 22311 return Style; 22312 }()); 22313 22314 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 22315 ASSERT_TRUE(static_cast<bool>(Style9)); 22316 ASSERT_EQ(*Style9, [] { 22317 auto Style = getLLVMStyle(); 22318 Style.ColumnLimit = 123; 22319 Style.IndentWidth = 7; 22320 return Style; 22321 }()); 22322 22323 // Test 9.9: use inheritance from a specific config file. 22324 Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp", 22325 "none", "", &FS); 22326 ASSERT_TRUE(static_cast<bool>(Style9)); 22327 ASSERT_EQ(*Style9, SubSubStyle); 22328 } 22329 22330 TEST(FormatStyle, GetStyleOfSpecificFile) { 22331 llvm::vfs::InMemoryFileSystem FS; 22332 // Specify absolute path to a format file in a parent directory. 22333 ASSERT_TRUE( 22334 FS.addFile("/e/.clang-format", 0, 22335 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 22336 ASSERT_TRUE( 22337 FS.addFile("/e/explicit.clang-format", 0, 22338 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22339 ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0, 22340 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22341 auto Style = getStyle("file:/e/explicit.clang-format", 22342 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22343 ASSERT_TRUE(static_cast<bool>(Style)); 22344 ASSERT_EQ(*Style, getGoogleStyle()); 22345 22346 // Specify relative path to a format file. 22347 ASSERT_TRUE( 22348 FS.addFile("../../e/explicit.clang-format", 0, 22349 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22350 Style = getStyle("file:../../e/explicit.clang-format", 22351 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22352 ASSERT_TRUE(static_cast<bool>(Style)); 22353 ASSERT_EQ(*Style, getGoogleStyle()); 22354 22355 // Specify path to a format file that does not exist. 22356 Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp", 22357 "LLVM", "", &FS); 22358 ASSERT_FALSE(static_cast<bool>(Style)); 22359 llvm::consumeError(Style.takeError()); 22360 22361 // Specify path to a file on the filesystem. 22362 SmallString<128> FormatFilePath; 22363 std::error_code ECF = llvm::sys::fs::createTemporaryFile( 22364 "FormatFileTest", "tpl", FormatFilePath); 22365 EXPECT_FALSE((bool)ECF); 22366 llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF); 22367 EXPECT_FALSE((bool)ECF); 22368 FormatFileTest << "BasedOnStyle: Google\n"; 22369 FormatFileTest.close(); 22370 22371 SmallString<128> TestFilePath; 22372 std::error_code ECT = 22373 llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath); 22374 EXPECT_FALSE((bool)ECT); 22375 llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT); 22376 CodeFileTest << "int i;\n"; 22377 CodeFileTest.close(); 22378 22379 std::string format_file_arg = std::string("file:") + FormatFilePath.c_str(); 22380 Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr); 22381 22382 llvm::sys::fs::remove(FormatFilePath.c_str()); 22383 llvm::sys::fs::remove(TestFilePath.c_str()); 22384 ASSERT_TRUE(static_cast<bool>(Style)); 22385 ASSERT_EQ(*Style, getGoogleStyle()); 22386 } 22387 22388 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 22389 // Column limit is 20. 22390 std::string Code = "Type *a =\n" 22391 " new Type();\n" 22392 "g(iiiii, 0, jjjjj,\n" 22393 " 0, kkkkk, 0, mm);\n" 22394 "int bad = format ;"; 22395 std::string Expected = "auto a = new Type();\n" 22396 "g(iiiii, nullptr,\n" 22397 " jjjjj, nullptr,\n" 22398 " kkkkk, nullptr,\n" 22399 " mm);\n" 22400 "int bad = format ;"; 22401 FileID ID = Context.createInMemoryFile("format.cpp", Code); 22402 tooling::Replacements Replaces = toReplacements( 22403 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 22404 "auto "), 22405 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 22406 "nullptr"), 22407 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 22408 "nullptr"), 22409 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 22410 "nullptr")}); 22411 22412 FormatStyle Style = getLLVMStyle(); 22413 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 22414 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 22415 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 22416 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 22417 auto Result = applyAllReplacements(Code, *FormattedReplaces); 22418 EXPECT_TRUE(static_cast<bool>(Result)); 22419 EXPECT_EQ(Expected, *Result); 22420 } 22421 22422 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 22423 std::string Code = "#include \"a.h\"\n" 22424 "#include \"c.h\"\n" 22425 "\n" 22426 "int main() {\n" 22427 " return 0;\n" 22428 "}"; 22429 std::string Expected = "#include \"a.h\"\n" 22430 "#include \"b.h\"\n" 22431 "#include \"c.h\"\n" 22432 "\n" 22433 "int main() {\n" 22434 " return 0;\n" 22435 "}"; 22436 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 22437 tooling::Replacements Replaces = toReplacements( 22438 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 22439 "#include \"b.h\"\n")}); 22440 22441 FormatStyle Style = getLLVMStyle(); 22442 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 22443 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 22444 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 22445 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 22446 auto Result = applyAllReplacements(Code, *FormattedReplaces); 22447 EXPECT_TRUE(static_cast<bool>(Result)); 22448 EXPECT_EQ(Expected, *Result); 22449 } 22450 22451 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 22452 EXPECT_EQ("using std::cin;\n" 22453 "using std::cout;", 22454 format("using std::cout;\n" 22455 "using std::cin;", 22456 getGoogleStyle())); 22457 } 22458 22459 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 22460 FormatStyle Style = getLLVMStyle(); 22461 Style.Standard = FormatStyle::LS_Cpp03; 22462 // cpp03 recognize this string as identifier u8 and literal character 'a' 22463 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 22464 } 22465 22466 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 22467 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 22468 // all modes, including C++11, C++14 and C++17 22469 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 22470 } 22471 22472 TEST_F(FormatTest, DoNotFormatLikelyXml) { 22473 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 22474 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 22475 } 22476 22477 TEST_F(FormatTest, StructuredBindings) { 22478 // Structured bindings is a C++17 feature. 22479 // all modes, including C++11, C++14 and C++17 22480 verifyFormat("auto [a, b] = f();"); 22481 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 22482 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 22483 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 22484 EXPECT_EQ("auto const volatile [a, b] = f();", 22485 format("auto const volatile[a, b] = f();")); 22486 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 22487 EXPECT_EQ("auto &[a, b, c] = f();", 22488 format("auto &[ a , b,c ] = f();")); 22489 EXPECT_EQ("auto &&[a, b, c] = f();", 22490 format("auto &&[ a , b,c ] = f();")); 22491 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 22492 EXPECT_EQ("auto const volatile &&[a, b] = f();", 22493 format("auto const volatile &&[a, b] = f();")); 22494 EXPECT_EQ("auto const &&[a, b] = f();", 22495 format("auto const && [a, b] = f();")); 22496 EXPECT_EQ("const auto &[a, b] = f();", 22497 format("const auto & [a, b] = f();")); 22498 EXPECT_EQ("const auto volatile &&[a, b] = f();", 22499 format("const auto volatile &&[a, b] = f();")); 22500 EXPECT_EQ("volatile const auto &&[a, b] = f();", 22501 format("volatile const auto &&[a, b] = f();")); 22502 EXPECT_EQ("const auto &&[a, b] = f();", 22503 format("const auto && [a, b] = f();")); 22504 22505 // Make sure we don't mistake structured bindings for lambdas. 22506 FormatStyle PointerMiddle = getLLVMStyle(); 22507 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 22508 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 22509 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 22510 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 22511 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 22512 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 22513 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 22514 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 22515 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 22516 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 22517 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 22518 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 22519 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 22520 22521 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 22522 format("for (const auto && [a, b] : some_range) {\n}")); 22523 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 22524 format("for (const auto & [a, b] : some_range) {\n}")); 22525 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 22526 format("for (const auto[a, b] : some_range) {\n}")); 22527 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 22528 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 22529 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 22530 EXPECT_EQ("auto const &[x, y](expr);", 22531 format("auto const & [x,y] (expr);")); 22532 EXPECT_EQ("auto const &&[x, y](expr);", 22533 format("auto const && [x,y] (expr);")); 22534 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 22535 EXPECT_EQ("auto const &[x, y]{expr};", 22536 format("auto const & [x,y] {expr};")); 22537 EXPECT_EQ("auto const &&[x, y]{expr};", 22538 format("auto const && [x,y] {expr};")); 22539 22540 FormatStyle Spaces = getLLVMStyle(); 22541 Spaces.SpacesInSquareBrackets = true; 22542 verifyFormat("auto [ a, b ] = f();", Spaces); 22543 verifyFormat("auto &&[ a, b ] = f();", Spaces); 22544 verifyFormat("auto &[ a, b ] = f();", Spaces); 22545 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 22546 verifyFormat("auto const &[ a, b ] = f();", Spaces); 22547 } 22548 22549 TEST_F(FormatTest, FileAndCode) { 22550 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 22551 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 22552 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 22553 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 22554 EXPECT_EQ(FormatStyle::LK_ObjC, 22555 guessLanguage("foo.h", "@interface Foo\n@end\n")); 22556 EXPECT_EQ( 22557 FormatStyle::LK_ObjC, 22558 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 22559 EXPECT_EQ(FormatStyle::LK_ObjC, 22560 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 22561 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 22562 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 22563 EXPECT_EQ(FormatStyle::LK_ObjC, 22564 guessLanguage("foo", "@interface Foo\n@end\n")); 22565 EXPECT_EQ(FormatStyle::LK_ObjC, 22566 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 22567 EXPECT_EQ( 22568 FormatStyle::LK_ObjC, 22569 guessLanguage("foo.h", 22570 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 22571 EXPECT_EQ( 22572 FormatStyle::LK_Cpp, 22573 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 22574 } 22575 22576 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 22577 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 22578 EXPECT_EQ(FormatStyle::LK_ObjC, 22579 guessLanguage("foo.h", "array[[calculator getIndex]];")); 22580 EXPECT_EQ(FormatStyle::LK_Cpp, 22581 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 22582 EXPECT_EQ( 22583 FormatStyle::LK_Cpp, 22584 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 22585 EXPECT_EQ(FormatStyle::LK_ObjC, 22586 guessLanguage("foo.h", "[[noreturn foo] bar];")); 22587 EXPECT_EQ(FormatStyle::LK_Cpp, 22588 guessLanguage("foo.h", "[[clang::fallthrough]];")); 22589 EXPECT_EQ(FormatStyle::LK_ObjC, 22590 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 22591 EXPECT_EQ(FormatStyle::LK_Cpp, 22592 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 22593 EXPECT_EQ(FormatStyle::LK_Cpp, 22594 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 22595 EXPECT_EQ(FormatStyle::LK_ObjC, 22596 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 22597 EXPECT_EQ(FormatStyle::LK_Cpp, 22598 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 22599 EXPECT_EQ( 22600 FormatStyle::LK_Cpp, 22601 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 22602 EXPECT_EQ( 22603 FormatStyle::LK_Cpp, 22604 guessLanguage("foo.h", 22605 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 22606 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 22607 } 22608 22609 TEST_F(FormatTest, GuessLanguageWithCaret) { 22610 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 22611 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 22612 EXPECT_EQ(FormatStyle::LK_ObjC, 22613 guessLanguage("foo.h", "int(^)(char, float);")); 22614 EXPECT_EQ(FormatStyle::LK_ObjC, 22615 guessLanguage("foo.h", "int(^foo)(char, float);")); 22616 EXPECT_EQ(FormatStyle::LK_ObjC, 22617 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 22618 EXPECT_EQ(FormatStyle::LK_ObjC, 22619 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 22620 EXPECT_EQ( 22621 FormatStyle::LK_ObjC, 22622 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 22623 } 22624 22625 TEST_F(FormatTest, GuessLanguageWithPragmas) { 22626 EXPECT_EQ(FormatStyle::LK_Cpp, 22627 guessLanguage("foo.h", "__pragma(warning(disable:))")); 22628 EXPECT_EQ(FormatStyle::LK_Cpp, 22629 guessLanguage("foo.h", "#pragma(warning(disable:))")); 22630 EXPECT_EQ(FormatStyle::LK_Cpp, 22631 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 22632 } 22633 22634 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 22635 // ASM symbolic names are identifiers that must be surrounded by [] without 22636 // space in between: 22637 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 22638 22639 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 22640 verifyFormat(R"(// 22641 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 22642 )"); 22643 22644 // A list of several ASM symbolic names. 22645 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 22646 22647 // ASM symbolic names in inline ASM with inputs and outputs. 22648 verifyFormat(R"(// 22649 asm("cmoveq %1, %2, %[result]" 22650 : [result] "=r"(result) 22651 : "r"(test), "r"(new), "[result]"(old)); 22652 )"); 22653 22654 // ASM symbolic names in inline ASM with no outputs. 22655 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 22656 } 22657 22658 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 22659 EXPECT_EQ(FormatStyle::LK_Cpp, 22660 guessLanguage("foo.h", "void f() {\n" 22661 " asm (\"mov %[e], %[d]\"\n" 22662 " : [d] \"=rm\" (d)\n" 22663 " [e] \"rm\" (*e));\n" 22664 "}")); 22665 EXPECT_EQ(FormatStyle::LK_Cpp, 22666 guessLanguage("foo.h", "void f() {\n" 22667 " _asm (\"mov %[e], %[d]\"\n" 22668 " : [d] \"=rm\" (d)\n" 22669 " [e] \"rm\" (*e));\n" 22670 "}")); 22671 EXPECT_EQ(FormatStyle::LK_Cpp, 22672 guessLanguage("foo.h", "void f() {\n" 22673 " __asm (\"mov %[e], %[d]\"\n" 22674 " : [d] \"=rm\" (d)\n" 22675 " [e] \"rm\" (*e));\n" 22676 "}")); 22677 EXPECT_EQ(FormatStyle::LK_Cpp, 22678 guessLanguage("foo.h", "void f() {\n" 22679 " __asm__ (\"mov %[e], %[d]\"\n" 22680 " : [d] \"=rm\" (d)\n" 22681 " [e] \"rm\" (*e));\n" 22682 "}")); 22683 EXPECT_EQ(FormatStyle::LK_Cpp, 22684 guessLanguage("foo.h", "void f() {\n" 22685 " asm (\"mov %[e], %[d]\"\n" 22686 " : [d] \"=rm\" (d),\n" 22687 " [e] \"rm\" (*e));\n" 22688 "}")); 22689 EXPECT_EQ(FormatStyle::LK_Cpp, 22690 guessLanguage("foo.h", "void f() {\n" 22691 " asm volatile (\"mov %[e], %[d]\"\n" 22692 " : [d] \"=rm\" (d)\n" 22693 " [e] \"rm\" (*e));\n" 22694 "}")); 22695 } 22696 22697 TEST_F(FormatTest, GuessLanguageWithChildLines) { 22698 EXPECT_EQ(FormatStyle::LK_Cpp, 22699 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 22700 EXPECT_EQ(FormatStyle::LK_ObjC, 22701 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 22702 EXPECT_EQ( 22703 FormatStyle::LK_Cpp, 22704 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 22705 EXPECT_EQ( 22706 FormatStyle::LK_ObjC, 22707 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 22708 } 22709 22710 TEST_F(FormatTest, TypenameMacros) { 22711 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 22712 22713 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 22714 FormatStyle Google = getGoogleStyleWithColumns(0); 22715 Google.TypenameMacros = TypenameMacros; 22716 verifyFormat("struct foo {\n" 22717 " int bar;\n" 22718 " TAILQ_ENTRY(a) bleh;\n" 22719 "};", 22720 Google); 22721 22722 FormatStyle Macros = getLLVMStyle(); 22723 Macros.TypenameMacros = TypenameMacros; 22724 22725 verifyFormat("STACK_OF(int) a;", Macros); 22726 verifyFormat("STACK_OF(int) *a;", Macros); 22727 verifyFormat("STACK_OF(int const *) *a;", Macros); 22728 verifyFormat("STACK_OF(int *const) *a;", Macros); 22729 verifyFormat("STACK_OF(int, string) a;", Macros); 22730 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 22731 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 22732 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 22733 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 22734 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 22735 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 22736 22737 Macros.PointerAlignment = FormatStyle::PAS_Left; 22738 verifyFormat("STACK_OF(int)* a;", Macros); 22739 verifyFormat("STACK_OF(int*)* a;", Macros); 22740 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 22741 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 22742 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 22743 } 22744 22745 TEST_F(FormatTest, AtomicQualifier) { 22746 // Check that we treate _Atomic as a type and not a function call 22747 FormatStyle Google = getGoogleStyleWithColumns(0); 22748 verifyFormat("struct foo {\n" 22749 " int a1;\n" 22750 " _Atomic(a) a2;\n" 22751 " _Atomic(_Atomic(int) *const) a3;\n" 22752 "};", 22753 Google); 22754 verifyFormat("_Atomic(uint64_t) a;"); 22755 verifyFormat("_Atomic(uint64_t) *a;"); 22756 verifyFormat("_Atomic(uint64_t const *) *a;"); 22757 verifyFormat("_Atomic(uint64_t *const) *a;"); 22758 verifyFormat("_Atomic(const uint64_t *) *a;"); 22759 verifyFormat("_Atomic(uint64_t) a;"); 22760 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 22761 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 22762 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 22763 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 22764 22765 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 22766 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 22767 FormatStyle Style = getLLVMStyle(); 22768 Style.PointerAlignment = FormatStyle::PAS_Left; 22769 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 22770 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 22771 verifyFormat("_Atomic(int)* a;", Style); 22772 verifyFormat("_Atomic(int*)* a;", Style); 22773 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 22774 22775 Style.SpacesInCStyleCastParentheses = true; 22776 Style.SpacesInParentheses = false; 22777 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 22778 Style.SpacesInCStyleCastParentheses = false; 22779 Style.SpacesInParentheses = true; 22780 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 22781 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 22782 } 22783 22784 TEST_F(FormatTest, AmbersandInLamda) { 22785 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 22786 FormatStyle AlignStyle = getLLVMStyle(); 22787 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 22788 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 22789 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 22790 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 22791 } 22792 22793 TEST_F(FormatTest, SpacesInConditionalStatement) { 22794 FormatStyle Spaces = getLLVMStyle(); 22795 Spaces.IfMacros.clear(); 22796 Spaces.IfMacros.push_back("MYIF"); 22797 Spaces.SpacesInConditionalStatement = true; 22798 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 22799 verifyFormat("if ( !a )\n return;", Spaces); 22800 verifyFormat("if ( a )\n return;", Spaces); 22801 verifyFormat("if constexpr ( a )\n return;", Spaces); 22802 verifyFormat("MYIF ( a )\n return;", Spaces); 22803 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 22804 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 22805 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 22806 verifyFormat("while ( a )\n return;", Spaces); 22807 verifyFormat("while ( (a && b) )\n return;", Spaces); 22808 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 22809 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 22810 // Check that space on the left of "::" is inserted as expected at beginning 22811 // of condition. 22812 verifyFormat("while ( ::func() )\n return;", Spaces); 22813 22814 // Check impact of ControlStatementsExceptControlMacros is honored. 22815 Spaces.SpaceBeforeParens = 22816 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 22817 verifyFormat("MYIF( a )\n return;", Spaces); 22818 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 22819 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 22820 } 22821 22822 TEST_F(FormatTest, AlternativeOperators) { 22823 // Test case for ensuring alternate operators are not 22824 // combined with their right most neighbour. 22825 verifyFormat("int a and b;"); 22826 verifyFormat("int a and_eq b;"); 22827 verifyFormat("int a bitand b;"); 22828 verifyFormat("int a bitor b;"); 22829 verifyFormat("int a compl b;"); 22830 verifyFormat("int a not b;"); 22831 verifyFormat("int a not_eq b;"); 22832 verifyFormat("int a or b;"); 22833 verifyFormat("int a xor b;"); 22834 verifyFormat("int a xor_eq b;"); 22835 verifyFormat("return this not_eq bitand other;"); 22836 verifyFormat("bool operator not_eq(const X bitand other)"); 22837 22838 verifyFormat("int a and 5;"); 22839 verifyFormat("int a and_eq 5;"); 22840 verifyFormat("int a bitand 5;"); 22841 verifyFormat("int a bitor 5;"); 22842 verifyFormat("int a compl 5;"); 22843 verifyFormat("int a not 5;"); 22844 verifyFormat("int a not_eq 5;"); 22845 verifyFormat("int a or 5;"); 22846 verifyFormat("int a xor 5;"); 22847 verifyFormat("int a xor_eq 5;"); 22848 22849 verifyFormat("int a compl(5);"); 22850 verifyFormat("int a not(5);"); 22851 22852 /* FIXME handle alternate tokens 22853 * https://en.cppreference.com/w/cpp/language/operator_alternative 22854 // alternative tokens 22855 verifyFormat("compl foo();"); // ~foo(); 22856 verifyFormat("foo() <%%>;"); // foo(); 22857 verifyFormat("void foo() <%%>;"); // void foo(){} 22858 verifyFormat("int a <:1:>;"); // int a[1];[ 22859 verifyFormat("%:define ABC abc"); // #define ABC abc 22860 verifyFormat("%:%:"); // ## 22861 */ 22862 } 22863 22864 TEST_F(FormatTest, STLWhileNotDefineChed) { 22865 verifyFormat("#if defined(while)\n" 22866 "#define while EMIT WARNING C4005\n" 22867 "#endif // while"); 22868 } 22869 22870 TEST_F(FormatTest, OperatorSpacing) { 22871 FormatStyle Style = getLLVMStyle(); 22872 Style.PointerAlignment = FormatStyle::PAS_Right; 22873 verifyFormat("Foo::operator*();", Style); 22874 verifyFormat("Foo::operator void *();", Style); 22875 verifyFormat("Foo::operator void **();", Style); 22876 verifyFormat("Foo::operator void *&();", Style); 22877 verifyFormat("Foo::operator void *&&();", Style); 22878 verifyFormat("Foo::operator void const *();", Style); 22879 verifyFormat("Foo::operator void const **();", Style); 22880 verifyFormat("Foo::operator void const *&();", Style); 22881 verifyFormat("Foo::operator void const *&&();", Style); 22882 verifyFormat("Foo::operator()(void *);", Style); 22883 verifyFormat("Foo::operator*(void *);", Style); 22884 verifyFormat("Foo::operator*();", Style); 22885 verifyFormat("Foo::operator**();", Style); 22886 verifyFormat("Foo::operator&();", Style); 22887 verifyFormat("Foo::operator<int> *();", Style); 22888 verifyFormat("Foo::operator<Foo> *();", Style); 22889 verifyFormat("Foo::operator<int> **();", Style); 22890 verifyFormat("Foo::operator<Foo> **();", Style); 22891 verifyFormat("Foo::operator<int> &();", Style); 22892 verifyFormat("Foo::operator<Foo> &();", Style); 22893 verifyFormat("Foo::operator<int> &&();", Style); 22894 verifyFormat("Foo::operator<Foo> &&();", Style); 22895 verifyFormat("Foo::operator<int> *&();", Style); 22896 verifyFormat("Foo::operator<Foo> *&();", Style); 22897 verifyFormat("Foo::operator<int> *&&();", Style); 22898 verifyFormat("Foo::operator<Foo> *&&();", Style); 22899 verifyFormat("operator*(int (*)(), class Foo);", Style); 22900 22901 verifyFormat("Foo::operator&();", Style); 22902 verifyFormat("Foo::operator void &();", Style); 22903 verifyFormat("Foo::operator void const &();", Style); 22904 verifyFormat("Foo::operator()(void &);", Style); 22905 verifyFormat("Foo::operator&(void &);", Style); 22906 verifyFormat("Foo::operator&();", Style); 22907 verifyFormat("operator&(int (&)(), class Foo);", Style); 22908 verifyFormat("operator&&(int (&)(), class Foo);", Style); 22909 22910 verifyFormat("Foo::operator&&();", Style); 22911 verifyFormat("Foo::operator**();", Style); 22912 verifyFormat("Foo::operator void &&();", Style); 22913 verifyFormat("Foo::operator void const &&();", Style); 22914 verifyFormat("Foo::operator()(void &&);", Style); 22915 verifyFormat("Foo::operator&&(void &&);", Style); 22916 verifyFormat("Foo::operator&&();", Style); 22917 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22918 verifyFormat("operator const nsTArrayRight<E> &()", Style); 22919 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 22920 Style); 22921 verifyFormat("operator void **()", Style); 22922 verifyFormat("operator const FooRight<Object> &()", Style); 22923 verifyFormat("operator const FooRight<Object> *()", Style); 22924 verifyFormat("operator const FooRight<Object> **()", Style); 22925 verifyFormat("operator const FooRight<Object> *&()", Style); 22926 verifyFormat("operator const FooRight<Object> *&&()", Style); 22927 22928 Style.PointerAlignment = FormatStyle::PAS_Left; 22929 verifyFormat("Foo::operator*();", Style); 22930 verifyFormat("Foo::operator**();", Style); 22931 verifyFormat("Foo::operator void*();", Style); 22932 verifyFormat("Foo::operator void**();", Style); 22933 verifyFormat("Foo::operator void*&();", Style); 22934 verifyFormat("Foo::operator void*&&();", Style); 22935 verifyFormat("Foo::operator void const*();", Style); 22936 verifyFormat("Foo::operator void const**();", Style); 22937 verifyFormat("Foo::operator void const*&();", Style); 22938 verifyFormat("Foo::operator void const*&&();", Style); 22939 verifyFormat("Foo::operator/*comment*/ void*();", Style); 22940 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 22941 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 22942 verifyFormat("Foo::operator()(void*);", Style); 22943 verifyFormat("Foo::operator*(void*);", Style); 22944 verifyFormat("Foo::operator*();", Style); 22945 verifyFormat("Foo::operator<int>*();", Style); 22946 verifyFormat("Foo::operator<Foo>*();", Style); 22947 verifyFormat("Foo::operator<int>**();", Style); 22948 verifyFormat("Foo::operator<Foo>**();", Style); 22949 verifyFormat("Foo::operator<Foo>*&();", Style); 22950 verifyFormat("Foo::operator<int>&();", Style); 22951 verifyFormat("Foo::operator<Foo>&();", Style); 22952 verifyFormat("Foo::operator<int>&&();", Style); 22953 verifyFormat("Foo::operator<Foo>&&();", Style); 22954 verifyFormat("Foo::operator<int>*&();", Style); 22955 verifyFormat("Foo::operator<Foo>*&();", Style); 22956 verifyFormat("operator*(int (*)(), class Foo);", Style); 22957 22958 verifyFormat("Foo::operator&();", Style); 22959 verifyFormat("Foo::operator void&();", Style); 22960 verifyFormat("Foo::operator void const&();", Style); 22961 verifyFormat("Foo::operator/*comment*/ void&();", Style); 22962 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 22963 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 22964 verifyFormat("Foo::operator()(void&);", Style); 22965 verifyFormat("Foo::operator&(void&);", Style); 22966 verifyFormat("Foo::operator&();", Style); 22967 verifyFormat("operator&(int (&)(), class Foo);", Style); 22968 verifyFormat("operator&(int (&&)(), class Foo);", Style); 22969 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22970 22971 verifyFormat("Foo::operator&&();", Style); 22972 verifyFormat("Foo::operator void&&();", Style); 22973 verifyFormat("Foo::operator void const&&();", Style); 22974 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 22975 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 22976 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 22977 verifyFormat("Foo::operator()(void&&);", Style); 22978 verifyFormat("Foo::operator&&(void&&);", Style); 22979 verifyFormat("Foo::operator&&();", Style); 22980 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22981 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 22982 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 22983 Style); 22984 verifyFormat("operator void**()", Style); 22985 verifyFormat("operator const FooLeft<Object>&()", Style); 22986 verifyFormat("operator const FooLeft<Object>*()", Style); 22987 verifyFormat("operator const FooLeft<Object>**()", Style); 22988 verifyFormat("operator const FooLeft<Object>*&()", Style); 22989 verifyFormat("operator const FooLeft<Object>*&&()", Style); 22990 22991 // PR45107 22992 verifyFormat("operator Vector<String>&();", Style); 22993 verifyFormat("operator const Vector<String>&();", Style); 22994 verifyFormat("operator foo::Bar*();", Style); 22995 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 22996 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 22997 Style); 22998 22999 Style.PointerAlignment = FormatStyle::PAS_Middle; 23000 verifyFormat("Foo::operator*();", Style); 23001 verifyFormat("Foo::operator void *();", Style); 23002 verifyFormat("Foo::operator()(void *);", Style); 23003 verifyFormat("Foo::operator*(void *);", Style); 23004 verifyFormat("Foo::operator*();", Style); 23005 verifyFormat("operator*(int (*)(), class Foo);", Style); 23006 23007 verifyFormat("Foo::operator&();", Style); 23008 verifyFormat("Foo::operator void &();", Style); 23009 verifyFormat("Foo::operator void const &();", Style); 23010 verifyFormat("Foo::operator()(void &);", Style); 23011 verifyFormat("Foo::operator&(void &);", Style); 23012 verifyFormat("Foo::operator&();", Style); 23013 verifyFormat("operator&(int (&)(), class Foo);", Style); 23014 23015 verifyFormat("Foo::operator&&();", Style); 23016 verifyFormat("Foo::operator void &&();", Style); 23017 verifyFormat("Foo::operator void const &&();", Style); 23018 verifyFormat("Foo::operator()(void &&);", Style); 23019 verifyFormat("Foo::operator&&(void &&);", Style); 23020 verifyFormat("Foo::operator&&();", Style); 23021 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 23022 } 23023 23024 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 23025 FormatStyle Style = getLLVMStyle(); 23026 // PR46157 23027 verifyFormat("foo(operator+, -42);", Style); 23028 verifyFormat("foo(operator++, -42);", Style); 23029 verifyFormat("foo(operator--, -42);", Style); 23030 verifyFormat("foo(-42, operator--);", Style); 23031 verifyFormat("foo(-42, operator, );", Style); 23032 verifyFormat("foo(operator, , -42);", Style); 23033 } 23034 23035 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 23036 FormatStyle Style = getLLVMStyle(); 23037 Style.WhitespaceSensitiveMacros.push_back("FOO"); 23038 23039 // Don't use the helpers here, since 'mess up' will change the whitespace 23040 // and these are all whitespace sensitive by definition 23041 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 23042 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 23043 EXPECT_EQ( 23044 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 23045 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 23046 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 23047 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 23048 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 23049 " Still=Intentional);", 23050 format("FOO(String-ized&Messy+But,: :\n" 23051 " Still=Intentional);", 23052 Style)); 23053 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 23054 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 23055 " Still=Intentional);", 23056 format("FOO(String-ized=&Messy+But,: :\n" 23057 " Still=Intentional);", 23058 Style)); 23059 23060 Style.ColumnLimit = 21; 23061 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 23062 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 23063 } 23064 23065 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 23066 // These tests are not in NamespaceFixer because that doesn't 23067 // test its interaction with line wrapping 23068 FormatStyle Style = getLLVMStyleWithColumns(80); 23069 verifyFormat("namespace {\n" 23070 "int i;\n" 23071 "int j;\n" 23072 "} // namespace", 23073 Style); 23074 23075 verifyFormat("namespace AAA {\n" 23076 "int i;\n" 23077 "int j;\n" 23078 "} // namespace AAA", 23079 Style); 23080 23081 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 23082 "int i;\n" 23083 "int j;\n" 23084 "} // namespace Averyveryveryverylongnamespace", 23085 format("namespace Averyveryveryverylongnamespace {\n" 23086 "int i;\n" 23087 "int j;\n" 23088 "}", 23089 Style)); 23090 23091 EXPECT_EQ( 23092 "namespace " 23093 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 23094 " went::mad::now {\n" 23095 "int i;\n" 23096 "int j;\n" 23097 "} // namespace\n" 23098 " // " 23099 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 23100 "went::mad::now", 23101 format("namespace " 23102 "would::it::save::you::a::lot::of::time::if_::i::" 23103 "just::gave::up::and_::went::mad::now {\n" 23104 "int i;\n" 23105 "int j;\n" 23106 "}", 23107 Style)); 23108 23109 // This used to duplicate the comment again and again on subsequent runs 23110 EXPECT_EQ( 23111 "namespace " 23112 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 23113 " went::mad::now {\n" 23114 "int i;\n" 23115 "int j;\n" 23116 "} // namespace\n" 23117 " // " 23118 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 23119 "went::mad::now", 23120 format("namespace " 23121 "would::it::save::you::a::lot::of::time::if_::i::" 23122 "just::gave::up::and_::went::mad::now {\n" 23123 "int i;\n" 23124 "int j;\n" 23125 "} // namespace\n" 23126 " // " 23127 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 23128 "and_::went::mad::now", 23129 Style)); 23130 } 23131 23132 TEST_F(FormatTest, LikelyUnlikely) { 23133 FormatStyle Style = getLLVMStyle(); 23134 23135 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23136 " return 29;\n" 23137 "}", 23138 Style); 23139 23140 verifyFormat("if (argc > 5) [[likely]] {\n" 23141 " return 29;\n" 23142 "}", 23143 Style); 23144 23145 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23146 " return 29;\n" 23147 "} else [[likely]] {\n" 23148 " return 42;\n" 23149 "}\n", 23150 Style); 23151 23152 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23153 " return 29;\n" 23154 "} else if (argc > 10) [[likely]] {\n" 23155 " return 99;\n" 23156 "} else {\n" 23157 " return 42;\n" 23158 "}\n", 23159 Style); 23160 23161 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 23162 " return 29;\n" 23163 "}", 23164 Style); 23165 23166 verifyFormat("if (argc > 5) [[unlikely]]\n" 23167 " return 29;\n", 23168 Style); 23169 verifyFormat("if (argc > 5) [[likely]]\n" 23170 " return 29;\n", 23171 Style); 23172 23173 Style.AttributeMacros.push_back("UNLIKELY"); 23174 Style.AttributeMacros.push_back("LIKELY"); 23175 verifyFormat("if (argc > 5) UNLIKELY\n" 23176 " return 29;\n", 23177 Style); 23178 23179 verifyFormat("if (argc > 5) UNLIKELY {\n" 23180 " return 29;\n" 23181 "}", 23182 Style); 23183 verifyFormat("if (argc > 5) UNLIKELY {\n" 23184 " return 29;\n" 23185 "} else [[likely]] {\n" 23186 " return 42;\n" 23187 "}\n", 23188 Style); 23189 verifyFormat("if (argc > 5) UNLIKELY {\n" 23190 " return 29;\n" 23191 "} else LIKELY {\n" 23192 " return 42;\n" 23193 "}\n", 23194 Style); 23195 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23196 " return 29;\n" 23197 "} else LIKELY {\n" 23198 " return 42;\n" 23199 "}\n", 23200 Style); 23201 } 23202 23203 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 23204 verifyFormat("Constructor()\n" 23205 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23206 " aaaa(aaaaaaaaaaaaaaaaaa, " 23207 "aaaaaaaaaaaaaaaaaat))"); 23208 verifyFormat("Constructor()\n" 23209 " : aaaaaaaaaaaaa(aaaaaa), " 23210 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 23211 23212 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 23213 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 23214 verifyFormat("Constructor()\n" 23215 " : aaaaaa(aaaaaa),\n" 23216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23217 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 23218 StyleWithWhitespacePenalty); 23219 verifyFormat("Constructor()\n" 23220 " : aaaaaaaaaaaaa(aaaaaa), " 23221 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 23222 StyleWithWhitespacePenalty); 23223 } 23224 23225 TEST_F(FormatTest, LLVMDefaultStyle) { 23226 FormatStyle Style = getLLVMStyle(); 23227 verifyFormat("extern \"C\" {\n" 23228 "int foo();\n" 23229 "}", 23230 Style); 23231 } 23232 TEST_F(FormatTest, GNUDefaultStyle) { 23233 FormatStyle Style = getGNUStyle(); 23234 verifyFormat("extern \"C\"\n" 23235 "{\n" 23236 " int foo ();\n" 23237 "}", 23238 Style); 23239 } 23240 TEST_F(FormatTest, MozillaDefaultStyle) { 23241 FormatStyle Style = getMozillaStyle(); 23242 verifyFormat("extern \"C\"\n" 23243 "{\n" 23244 " int foo();\n" 23245 "}", 23246 Style); 23247 } 23248 TEST_F(FormatTest, GoogleDefaultStyle) { 23249 FormatStyle Style = getGoogleStyle(); 23250 verifyFormat("extern \"C\" {\n" 23251 "int foo();\n" 23252 "}", 23253 Style); 23254 } 23255 TEST_F(FormatTest, ChromiumDefaultStyle) { 23256 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 23257 verifyFormat("extern \"C\" {\n" 23258 "int foo();\n" 23259 "}", 23260 Style); 23261 } 23262 TEST_F(FormatTest, MicrosoftDefaultStyle) { 23263 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 23264 verifyFormat("extern \"C\"\n" 23265 "{\n" 23266 " int foo();\n" 23267 "}", 23268 Style); 23269 } 23270 TEST_F(FormatTest, WebKitDefaultStyle) { 23271 FormatStyle Style = getWebKitStyle(); 23272 verifyFormat("extern \"C\" {\n" 23273 "int foo();\n" 23274 "}", 23275 Style); 23276 } 23277 23278 TEST_F(FormatTest, Concepts) { 23279 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations, 23280 FormatStyle::BBCDS_Always); 23281 verifyFormat("template <typename T>\n" 23282 "concept True = true;"); 23283 23284 verifyFormat("template <typename T>\n" 23285 "concept C = ((false || foo()) && C2<T>) ||\n" 23286 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;", 23287 getLLVMStyleWithColumns(60)); 23288 23289 verifyFormat("template <typename T>\n" 23290 "concept DelayedCheck = true && requires(T t) { t.bar(); } && " 23291 "sizeof(T) <= 8;"); 23292 23293 verifyFormat("template <typename T>\n" 23294 "concept DelayedCheck = true && requires(T t) {\n" 23295 " t.bar();\n" 23296 " t.baz();\n" 23297 " } && sizeof(T) <= 8;"); 23298 23299 verifyFormat("template <typename T>\n" 23300 "concept DelayedCheck = true && requires(T t) { // Comment\n" 23301 " t.bar();\n" 23302 " t.baz();\n" 23303 " } && sizeof(T) <= 8;"); 23304 23305 verifyFormat("template <typename T>\n" 23306 "concept DelayedCheck = false || requires(T t) { t.bar(); } && " 23307 "sizeof(T) <= 8;"); 23308 23309 verifyFormat("template <typename T>\n" 23310 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } " 23311 "&& sizeof(T) <= 8;"); 23312 23313 verifyFormat( 23314 "template <typename T>\n" 23315 "concept DelayedCheck = static_cast<bool>(0) ||\n" 23316 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 23317 23318 verifyFormat("template <typename T>\n" 23319 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } " 23320 "&& sizeof(T) <= 8;"); 23321 23322 verifyFormat( 23323 "template <typename T>\n" 23324 "concept DelayedCheck = (bool)(0) ||\n" 23325 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 23326 23327 verifyFormat("template <typename T>\n" 23328 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } " 23329 "&& sizeof(T) <= 8;"); 23330 23331 verifyFormat("template <typename T>\n" 23332 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && " 23333 "sizeof(T) <= 8;"); 23334 23335 verifyFormat("template <typename T>\n" 23336 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n" 23337 " requires(T t) {\n" 23338 " t.bar();\n" 23339 " t.baz();\n" 23340 " } && sizeof(T) <= 8 && !(4 < 3);", 23341 getLLVMStyleWithColumns(60)); 23342 23343 verifyFormat("template <typename T>\n" 23344 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;"); 23345 23346 verifyFormat("template <typename T>\n" 23347 "concept C = foo();"); 23348 23349 verifyFormat("template <typename T>\n" 23350 "concept C = foo(T());"); 23351 23352 verifyFormat("template <typename T>\n" 23353 "concept C = foo(T{});"); 23354 23355 verifyFormat("template <typename T>\n" 23356 "concept Size = V<sizeof(T)>::Value > 5;"); 23357 23358 verifyFormat("template <typename T>\n" 23359 "concept True = S<T>::Value;"); 23360 23361 verifyFormat( 23362 "template <typename T>\n" 23363 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n" 23364 " sizeof(T) <= 8;"); 23365 23366 // FIXME: This is misformatted because the fake l paren starts at bool, not at 23367 // the lambda l square. 23368 verifyFormat("template <typename T>\n" 23369 "concept C = [] -> bool { return true; }() && requires(T t) { " 23370 "t.bar(); } &&\n" 23371 " sizeof(T) <= 8;"); 23372 23373 verifyFormat( 23374 "template <typename T>\n" 23375 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n" 23376 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 23377 23378 verifyFormat("template <typename T>\n" 23379 "concept C = decltype([]() { return std::true_type{}; " 23380 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;", 23381 getLLVMStyleWithColumns(120)); 23382 23383 verifyFormat("template <typename T>\n" 23384 "concept C = decltype([]() -> std::true_type { return {}; " 23385 "}())::value &&\n" 23386 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 23387 23388 verifyFormat("template <typename T>\n" 23389 "concept C = true;\n" 23390 "Foo Bar;"); 23391 23392 verifyFormat("template <typename T>\n" 23393 "concept Hashable = requires(T a) {\n" 23394 " { std::hash<T>{}(a) } -> " 23395 "std::convertible_to<std::size_t>;\n" 23396 " };"); 23397 23398 verifyFormat( 23399 "template <typename T>\n" 23400 "concept EqualityComparable = requires(T a, T b) {\n" 23401 " { a == b } -> std::same_as<bool>;\n" 23402 " };"); 23403 23404 verifyFormat( 23405 "template <typename T>\n" 23406 "concept EqualityComparable = requires(T a, T b) {\n" 23407 " { a == b } -> std::same_as<bool>;\n" 23408 " { a != b } -> std::same_as<bool>;\n" 23409 " };"); 23410 23411 verifyFormat("template <typename T>\n" 23412 "concept WeakEqualityComparable = requires(T a, T b) {\n" 23413 " { a == b };\n" 23414 " { a != b };\n" 23415 " };"); 23416 23417 verifyFormat("template <typename T>\n" 23418 "concept HasSizeT = requires { typename T::size_t; };"); 23419 23420 verifyFormat("template <typename T>\n" 23421 "concept Semiregular =\n" 23422 " DefaultConstructible<T> && CopyConstructible<T> && " 23423 "CopyAssignable<T> &&\n" 23424 " requires(T a, std::size_t n) {\n" 23425 " requires Same<T *, decltype(&a)>;\n" 23426 " { a.~T() } noexcept;\n" 23427 " requires Same<T *, decltype(new T)>;\n" 23428 " requires Same<T *, decltype(new T[n])>;\n" 23429 " { delete new T; };\n" 23430 " { delete new T[n]; };\n" 23431 " };"); 23432 23433 verifyFormat("template <typename T>\n" 23434 "concept Semiregular =\n" 23435 " requires(T a, std::size_t n) {\n" 23436 " requires Same<T *, decltype(&a)>;\n" 23437 " { a.~T() } noexcept;\n" 23438 " requires Same<T *, decltype(new T)>;\n" 23439 " requires Same<T *, decltype(new T[n])>;\n" 23440 " { delete new T; };\n" 23441 " { delete new T[n]; };\n" 23442 " { new T } -> std::same_as<T *>;\n" 23443 " } && DefaultConstructible<T> && CopyConstructible<T> && " 23444 "CopyAssignable<T>;"); 23445 23446 verifyFormat( 23447 "template <typename T>\n" 23448 "concept Semiregular =\n" 23449 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n" 23450 " requires Same<T *, decltype(&a)>;\n" 23451 " { a.~T() } noexcept;\n" 23452 " requires Same<T *, decltype(new T)>;\n" 23453 " requires Same<T *, decltype(new " 23454 "T[n])>;\n" 23455 " { delete new T; };\n" 23456 " { delete new T[n]; };\n" 23457 " } && CopyConstructible<T> && " 23458 "CopyAssignable<T>;"); 23459 23460 verifyFormat("template <typename T>\n" 23461 "concept Two = requires(T t) {\n" 23462 " { t.foo() } -> std::same_as<Bar>;\n" 23463 " } && requires(T &&t) {\n" 23464 " { t.foo() } -> std::same_as<Bar &&>;\n" 23465 " };"); 23466 23467 verifyFormat( 23468 "template <typename T>\n" 23469 "concept C = requires(T x) {\n" 23470 " { *x } -> std::convertible_to<typename T::inner>;\n" 23471 " { x + 1 } noexcept -> std::same_as<int>;\n" 23472 " { x * 1 } -> std::convertible_to<T>;\n" 23473 " };"); 23474 23475 verifyFormat( 23476 "template <typename T, typename U = T>\n" 23477 "concept Swappable = requires(T &&t, U &&u) {\n" 23478 " swap(std::forward<T>(t), std::forward<U>(u));\n" 23479 " swap(std::forward<U>(u), std::forward<T>(t));\n" 23480 " };"); 23481 23482 verifyFormat("template <typename T, typename U>\n" 23483 "concept Common = requires(T &&t, U &&u) {\n" 23484 " typename CommonType<T, U>;\n" 23485 " { CommonType<T, U>(std::forward<T>(t)) };\n" 23486 " };"); 23487 23488 verifyFormat("template <typename T, typename U>\n" 23489 "concept Common = requires(T &&t, U &&u) {\n" 23490 " typename CommonType<T, U>;\n" 23491 " { CommonType<T, U>{std::forward<T>(t)} };\n" 23492 " };"); 23493 23494 verifyFormat( 23495 "template <typename T>\n" 23496 "concept C = requires(T t) {\n" 23497 " requires Bar<T> && Foo<T>;\n" 23498 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 23499 " };"); 23500 23501 verifyFormat("template <typename T>\n" 23502 "concept HasFoo = requires(T t) {\n" 23503 " { t.foo() };\n" 23504 " t.foo();\n" 23505 " };\n" 23506 "template <typename T>\n" 23507 "concept HasBar = requires(T t) {\n" 23508 " { t.bar() };\n" 23509 " t.bar();\n" 23510 " };"); 23511 23512 verifyFormat("template <typename T>\n" 23513 "concept Large = sizeof(T) > 10;"); 23514 23515 verifyFormat("template <typename T, typename U>\n" 23516 "concept FooableWith = requires(T t, U u) {\n" 23517 " typename T::foo_type;\n" 23518 " { t.foo(u) } -> typename T::foo_type;\n" 23519 " t++;\n" 23520 " };\n" 23521 "void doFoo(FooableWith<int> auto t) { t.foo(3); }"); 23522 23523 verifyFormat("template <typename T>\n" 23524 "concept Context = is_specialization_of_v<context, T>;"); 23525 23526 verifyFormat("template <typename T>\n" 23527 "concept Node = std::is_object_v<T>;"); 23528 23529 auto Style = getLLVMStyle(); 23530 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed; 23531 23532 verifyFormat( 23533 "template <typename T>\n" 23534 "concept C = requires(T t) {\n" 23535 " requires Bar<T> && Foo<T>;\n" 23536 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 23537 " };", 23538 Style); 23539 23540 verifyFormat("template <typename T>\n" 23541 "concept HasFoo = requires(T t) {\n" 23542 " { t.foo() };\n" 23543 " t.foo();\n" 23544 " };\n" 23545 "template <typename T>\n" 23546 "concept HasBar = requires(T t) {\n" 23547 " { t.bar() };\n" 23548 " t.bar();\n" 23549 " };", 23550 Style); 23551 23552 verifyFormat("template <typename T> concept True = true;", Style); 23553 23554 verifyFormat("template <typename T>\n" 23555 "concept C = decltype([]() -> std::true_type { return {}; " 23556 "}())::value &&\n" 23557 " requires(T t) { t.bar(); } && sizeof(T) <= 8;", 23558 Style); 23559 23560 verifyFormat("template <typename T>\n" 23561 "concept Semiregular =\n" 23562 " DefaultConstructible<T> && CopyConstructible<T> && " 23563 "CopyAssignable<T> &&\n" 23564 " requires(T a, std::size_t n) {\n" 23565 " requires Same<T *, decltype(&a)>;\n" 23566 " { a.~T() } noexcept;\n" 23567 " requires Same<T *, decltype(new T)>;\n" 23568 " requires Same<T *, decltype(new T[n])>;\n" 23569 " { delete new T; };\n" 23570 " { delete new T[n]; };\n" 23571 " };", 23572 Style); 23573 23574 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never; 23575 23576 verifyFormat("template <typename T> concept C =\n" 23577 " requires(T t) {\n" 23578 " requires Bar<T> && Foo<T>;\n" 23579 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 23580 " };", 23581 Style); 23582 23583 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n" 23584 " { t.foo() };\n" 23585 " t.foo();\n" 23586 " };\n" 23587 "template <typename T> concept HasBar = requires(T t) {\n" 23588 " { t.bar() };\n" 23589 " t.bar();\n" 23590 " };", 23591 Style); 23592 23593 verifyFormat("template <typename T> concept True = true;", Style); 23594 23595 verifyFormat( 23596 "template <typename T> concept C = decltype([]() -> std::true_type {\n" 23597 " return {};\n" 23598 " }())::value\n" 23599 " && requires(T t) { t.bar(); } &&\n" 23600 " sizeof(T) <= 8;", 23601 Style); 23602 23603 verifyFormat("template <typename T> concept Semiregular =\n" 23604 " DefaultConstructible<T> && CopyConstructible<T> && " 23605 "CopyAssignable<T> &&\n" 23606 " requires(T a, std::size_t n) {\n" 23607 " requires Same<T *, decltype(&a)>;\n" 23608 " { a.~T() } noexcept;\n" 23609 " requires Same<T *, decltype(new T)>;\n" 23610 " requires Same<T *, decltype(new T[n])>;\n" 23611 " { delete new T; };\n" 23612 " { delete new T[n]; };\n" 23613 " };", 23614 Style); 23615 23616 // The following tests are invalid C++, we just want to make sure we don't 23617 // assert. 23618 verifyFormat("template <typename T>\n" 23619 "concept C = requires C2<T>;"); 23620 23621 verifyFormat("template <typename T>\n" 23622 "concept C = 5 + 4;"); 23623 23624 verifyFormat("template <typename T>\n" 23625 "concept C =\n" 23626 "class X;"); 23627 23628 verifyFormat("template <typename T>\n" 23629 "concept C = [] && true;"); 23630 23631 verifyFormat("template <typename T>\n" 23632 "concept C = [] && requires(T t) { typename T::size_type; };"); 23633 } 23634 23635 TEST_F(FormatTest, RequiresClauses) { 23636 auto Style = getLLVMStyle(); 23637 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine); 23638 EXPECT_EQ(Style.IndentRequiresClause, true); 23639 23640 verifyFormat("template <typename T>\n" 23641 " requires(Foo<T> && std::trait<T>)\n" 23642 "struct Bar;", 23643 Style); 23644 23645 verifyFormat("template <typename T>\n" 23646 " requires(Foo<T> && std::trait<T>)\n" 23647 "class Bar {\n" 23648 "public:\n" 23649 " Bar(T t);\n" 23650 " bool baz();\n" 23651 "};", 23652 Style); 23653 23654 verifyFormat( 23655 "template <typename T>\n" 23656 " requires requires(T &&t) {\n" 23657 " typename T::I;\n" 23658 " requires(F<typename T::I> && std::trait<typename T::I>);\n" 23659 " }\n" 23660 "Bar(T) -> Bar<typename T::I>;", 23661 Style); 23662 23663 verifyFormat("template <typename T>\n" 23664 " requires(Foo<T> && std::trait<T>)\n" 23665 "constexpr T MyGlobal;", 23666 Style); 23667 23668 verifyFormat("template <typename T>\n" 23669 " requires Foo<T> && requires(T t) {\n" 23670 " { t.baz() } -> std::same_as<bool>;\n" 23671 " requires std::same_as<T::Factor, int>;\n" 23672 " }\n" 23673 "inline int bar(T t) {\n" 23674 " return t.baz() ? T::Factor : 5;\n" 23675 "}", 23676 Style); 23677 23678 verifyFormat("template <typename T>\n" 23679 "inline int bar(T t)\n" 23680 " requires Foo<T> && requires(T t) {\n" 23681 " { t.baz() } -> std::same_as<bool>;\n" 23682 " requires std::same_as<T::Factor, int>;\n" 23683 " }\n" 23684 "{\n" 23685 " return t.baz() ? T::Factor : 5;\n" 23686 "}", 23687 Style); 23688 23689 verifyFormat("template <typename T>\n" 23690 " requires F<T>\n" 23691 "int bar(T t) {\n" 23692 " return 5;\n" 23693 "}", 23694 Style); 23695 23696 verifyFormat("template <typename T>\n" 23697 "int bar(T t)\n" 23698 " requires F<T>\n" 23699 "{\n" 23700 " return 5;\n" 23701 "}", 23702 Style); 23703 23704 Style.IndentRequiresClause = false; 23705 verifyFormat("template <typename T>\n" 23706 "requires F<T>\n" 23707 "int bar(T t) {\n" 23708 " return 5;\n" 23709 "}", 23710 Style); 23711 23712 verifyFormat("template <typename T>\n" 23713 "int bar(T t)\n" 23714 "requires F<T>\n" 23715 "{\n" 23716 " return 5;\n" 23717 "}", 23718 Style); 23719 23720 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine; 23721 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n" 23722 "template <typename T> requires Foo<T> void bar() {}\n" 23723 "template <typename T> void bar() requires Foo<T> {}\n" 23724 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;", 23725 Style); 23726 23727 auto ColumnStyle = Style; 23728 ColumnStyle.ColumnLimit = 40; 23729 verifyFormat("template <typename AAAAAAA>\n" 23730 "requires Foo<T> struct Bar {};\n" 23731 "template <typename AAAAAAA>\n" 23732 "requires Foo<T> void bar() {}\n" 23733 "template <typename AAAAAAA>\n" 23734 "void bar() requires Foo<T> {}\n" 23735 "template <typename AAAAAAA>\n" 23736 "requires Foo<T> Baz(T) -> Baz<T>;", 23737 ColumnStyle); 23738 23739 verifyFormat("template <typename T>\n" 23740 "requires Foo<AAAAAAA> struct Bar {};\n" 23741 "template <typename T>\n" 23742 "requires Foo<AAAAAAA> void bar() {}\n" 23743 "template <typename T>\n" 23744 "void bar() requires Foo<AAAAAAA> {}\n" 23745 "template <typename T>\n" 23746 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;", 23747 ColumnStyle); 23748 23749 verifyFormat("template <typename AAAAAAA>\n" 23750 "requires Foo<AAAAAAAAAAAAAAAA>\n" 23751 "struct Bar {};\n" 23752 "template <typename AAAAAAA>\n" 23753 "requires Foo<AAAAAAAAAAAAAAAA>\n" 23754 "void bar() {}\n" 23755 "template <typename AAAAAAA>\n" 23756 "void bar()\n" 23757 " requires Foo<AAAAAAAAAAAAAAAA> {}\n" 23758 "template <typename AAAAAAA>\n" 23759 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" 23760 "template <typename AAAAAAA>\n" 23761 "requires Foo<AAAAAAAAAAAAAAAA>\n" 23762 "Bar(T) -> Bar<T>;", 23763 ColumnStyle); 23764 23765 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; 23766 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; 23767 23768 verifyFormat("template <typename T>\n" 23769 "requires Foo<T> struct Bar {};\n" 23770 "template <typename T>\n" 23771 "requires Foo<T> void bar() {}\n" 23772 "template <typename T>\n" 23773 "void bar()\n" 23774 "requires Foo<T> {}\n" 23775 "template <typename T>\n" 23776 "requires Foo<T> Bar(T) -> Bar<T>;", 23777 Style); 23778 23779 verifyFormat("template <typename AAAAAAA>\n" 23780 "requires Foo<AAAAAAAAAAAAAAAA>\n" 23781 "struct Bar {};\n" 23782 "template <typename AAAAAAA>\n" 23783 "requires Foo<AAAAAAAAAAAAAAAA>\n" 23784 "void bar() {}\n" 23785 "template <typename AAAAAAA>\n" 23786 "void bar()\n" 23787 "requires Foo<AAAAAAAAAAAAAAAA> {}\n" 23788 "template <typename AAAAAAA>\n" 23789 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" 23790 "template <typename AAAAAAA>\n" 23791 "requires Foo<AAAAAAAAAAAAAAAA>\n" 23792 "Bar(T) -> Bar<T>;", 23793 ColumnStyle); 23794 23795 Style.IndentRequiresClause = true; 23796 ColumnStyle.IndentRequiresClause = true; 23797 23798 verifyFormat("template <typename T>\n" 23799 " requires Foo<T> struct Bar {};\n" 23800 "template <typename T>\n" 23801 " requires Foo<T> void bar() {}\n" 23802 "template <typename T>\n" 23803 "void bar()\n" 23804 " requires Foo<T> {}\n" 23805 "template <typename T>\n" 23806 " requires Foo<T> Bar(T) -> Bar<T>;", 23807 Style); 23808 23809 verifyFormat("template <typename AAAAAAA>\n" 23810 " requires Foo<AAAAAAAAAAAAAAAA>\n" 23811 "struct Bar {};\n" 23812 "template <typename AAAAAAA>\n" 23813 " requires Foo<AAAAAAAAAAAAAAAA>\n" 23814 "void bar() {}\n" 23815 "template <typename AAAAAAA>\n" 23816 "void bar()\n" 23817 " requires Foo<AAAAAAAAAAAAAAAA> {}\n" 23818 "template <typename AAAAAAA>\n" 23819 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n" 23820 "template <typename AAAAAAA>\n" 23821 " requires Foo<AAAAAAAAAAAAAAAA>\n" 23822 "Bar(T) -> Bar<T>;", 23823 ColumnStyle); 23824 23825 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 23826 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 23827 23828 verifyFormat("template <typename T> requires Foo<T>\n" 23829 "struct Bar {};\n" 23830 "template <typename T> requires Foo<T>\n" 23831 "void bar() {}\n" 23832 "template <typename T>\n" 23833 "void bar() requires Foo<T>\n" 23834 "{}\n" 23835 "template <typename T> requires Foo<T>\n" 23836 "Bar(T) -> Bar<T>;", 23837 Style); 23838 23839 verifyFormat("template <typename AAAAAAA>\n" 23840 "requires Foo<AAAAAAAAAAAAAAAA>\n" 23841 "struct Bar {};\n" 23842 "template <typename AAAAAAA>\n" 23843 "requires Foo<AAAAAAAAAAAAAAAA>\n" 23844 "void bar() {}\n" 23845 "template <typename AAAAAAA>\n" 23846 "void bar()\n" 23847 " requires Foo<AAAAAAAAAAAAAAAA>\n" 23848 "{}\n" 23849 "template <typename AAAAAAA>\n" 23850 "requires Foo<AAAAAAAA>\n" 23851 "Bar(T) -> Bar<T>;\n" 23852 "template <typename AAAAAAA>\n" 23853 "requires Foo<AAAAAAAAAAAAAAAA>\n" 23854 "Bar(T) -> Bar<T>;", 23855 ColumnStyle); 23856 } 23857 23858 TEST_F(FormatTest, StatementAttributeLikeMacros) { 23859 FormatStyle Style = getLLVMStyle(); 23860 StringRef Source = "void Foo::slot() {\n" 23861 " unsigned char MyChar = 'x';\n" 23862 " emit signal(MyChar);\n" 23863 " Q_EMIT signal(MyChar);\n" 23864 "}"; 23865 23866 EXPECT_EQ(Source, format(Source, Style)); 23867 23868 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 23869 EXPECT_EQ("void Foo::slot() {\n" 23870 " unsigned char MyChar = 'x';\n" 23871 " emit signal(MyChar);\n" 23872 " Q_EMIT signal(MyChar);\n" 23873 "}", 23874 format(Source, Style)); 23875 23876 Style.StatementAttributeLikeMacros.push_back("emit"); 23877 EXPECT_EQ(Source, format(Source, Style)); 23878 23879 Style.StatementAttributeLikeMacros = {}; 23880 EXPECT_EQ("void Foo::slot() {\n" 23881 " unsigned char MyChar = 'x';\n" 23882 " emit signal(MyChar);\n" 23883 " Q_EMIT signal(MyChar);\n" 23884 "}", 23885 format(Source, Style)); 23886 } 23887 23888 TEST_F(FormatTest, IndentAccessModifiers) { 23889 FormatStyle Style = getLLVMStyle(); 23890 Style.IndentAccessModifiers = true; 23891 // Members are *two* levels below the record; 23892 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 23893 verifyFormat("class C {\n" 23894 " int i;\n" 23895 "};\n", 23896 Style); 23897 verifyFormat("union C {\n" 23898 " int i;\n" 23899 " unsigned u;\n" 23900 "};\n", 23901 Style); 23902 // Access modifiers should be indented one level below the record. 23903 verifyFormat("class C {\n" 23904 " public:\n" 23905 " int i;\n" 23906 "};\n", 23907 Style); 23908 verifyFormat("struct S {\n" 23909 " private:\n" 23910 " class C {\n" 23911 " int j;\n" 23912 "\n" 23913 " public:\n" 23914 " C();\n" 23915 " };\n" 23916 "\n" 23917 " public:\n" 23918 " int i;\n" 23919 "};\n", 23920 Style); 23921 // Enumerations are not records and should be unaffected. 23922 Style.AllowShortEnumsOnASingleLine = false; 23923 verifyFormat("enum class E {\n" 23924 " A,\n" 23925 " B\n" 23926 "};\n", 23927 Style); 23928 // Test with a different indentation width; 23929 // also proves that the result is Style.AccessModifierOffset agnostic. 23930 Style.IndentWidth = 3; 23931 verifyFormat("class C {\n" 23932 " public:\n" 23933 " int i;\n" 23934 "};\n", 23935 Style); 23936 } 23937 23938 TEST_F(FormatTest, LimitlessStringsAndComments) { 23939 auto Style = getLLVMStyleWithColumns(0); 23940 constexpr StringRef Code = 23941 "/**\n" 23942 " * This is a multiline comment with quite some long lines, at least for " 23943 "the LLVM Style.\n" 23944 " * We will redo this with strings and line comments. Just to check if " 23945 "everything is working.\n" 23946 " */\n" 23947 "bool foo() {\n" 23948 " /* Single line multi line comment. */\n" 23949 " const std::string String = \"This is a multiline string with quite " 23950 "some long lines, at least for the LLVM Style.\"\n" 23951 " \"We already did it with multi line " 23952 "comments, and we will do it with line comments. Just to check if " 23953 "everything is working.\";\n" 23954 " // This is a line comment (block) with quite some long lines, at " 23955 "least for the LLVM Style.\n" 23956 " // We already did this with multi line comments and strings. Just to " 23957 "check if everything is working.\n" 23958 " const std::string SmallString = \"Hello World\";\n" 23959 " // Small line comment\n" 23960 " return String.size() > SmallString.size();\n" 23961 "}"; 23962 EXPECT_EQ(Code, format(Code, Style)); 23963 } 23964 23965 TEST_F(FormatTest, FormatDecayCopy) { 23966 // error cases from unit tests 23967 verifyFormat("foo(auto())"); 23968 verifyFormat("foo(auto{})"); 23969 verifyFormat("foo(auto({}))"); 23970 verifyFormat("foo(auto{{}})"); 23971 23972 verifyFormat("foo(auto(1))"); 23973 verifyFormat("foo(auto{1})"); 23974 verifyFormat("foo(new auto(1))"); 23975 verifyFormat("foo(new auto{1})"); 23976 verifyFormat("decltype(auto(1)) x;"); 23977 verifyFormat("decltype(auto{1}) x;"); 23978 verifyFormat("auto(x);"); 23979 verifyFormat("auto{x};"); 23980 verifyFormat("new auto{x};"); 23981 verifyFormat("auto{x} = y;"); 23982 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 23983 // the user's own fault 23984 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 23985 // clearly the user's own fault 23986 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 23987 } 23988 23989 TEST_F(FormatTest, Cpp20ModulesSupport) { 23990 FormatStyle Style = getLLVMStyle(); 23991 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 23992 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 23993 23994 verifyFormat("export import foo;", Style); 23995 verifyFormat("export import foo:bar;", Style); 23996 verifyFormat("export import foo.bar;", Style); 23997 verifyFormat("export import foo.bar:baz;", Style); 23998 verifyFormat("export import :bar;", Style); 23999 verifyFormat("export module foo:bar;", Style); 24000 verifyFormat("export module foo;", Style); 24001 verifyFormat("export module foo.bar;", Style); 24002 verifyFormat("export module foo.bar:baz;", Style); 24003 verifyFormat("export import <string_view>;", Style); 24004 24005 verifyFormat("export type_name var;", Style); 24006 verifyFormat("template <class T> export using A = B<T>;", Style); 24007 verifyFormat("export using A = B;", Style); 24008 verifyFormat("export int func() {\n" 24009 " foo();\n" 24010 "}", 24011 Style); 24012 verifyFormat("export struct {\n" 24013 " int foo;\n" 24014 "};", 24015 Style); 24016 verifyFormat("export {\n" 24017 " int foo;\n" 24018 "};", 24019 Style); 24020 verifyFormat("export export char const *hello() { return \"hello\"; }"); 24021 24022 verifyFormat("import bar;", Style); 24023 verifyFormat("import foo.bar;", Style); 24024 verifyFormat("import foo:bar;", Style); 24025 verifyFormat("import :bar;", Style); 24026 verifyFormat("import <ctime>;", Style); 24027 verifyFormat("import \"header\";", Style); 24028 24029 verifyFormat("module foo;", Style); 24030 verifyFormat("module foo:bar;", Style); 24031 verifyFormat("module foo.bar;", Style); 24032 verifyFormat("module;", Style); 24033 24034 verifyFormat("export namespace hi {\n" 24035 "const char *sayhi();\n" 24036 "}", 24037 Style); 24038 24039 verifyFormat("module :private;", Style); 24040 verifyFormat("import <foo/bar.h>;", Style); 24041 verifyFormat("import foo...bar;", Style); 24042 verifyFormat("import ..........;", Style); 24043 verifyFormat("module foo:private;", Style); 24044 verifyFormat("import a", Style); 24045 verifyFormat("module a", Style); 24046 verifyFormat("export import a", Style); 24047 verifyFormat("export module a", Style); 24048 24049 verifyFormat("import", Style); 24050 verifyFormat("module", Style); 24051 verifyFormat("export", Style); 24052 } 24053 24054 TEST_F(FormatTest, CoroutineForCoawait) { 24055 FormatStyle Style = getLLVMStyle(); 24056 verifyFormat("for co_await (auto x : range())\n ;"); 24057 verifyFormat("for (auto i : arr) {\n" 24058 "}", 24059 Style); 24060 verifyFormat("for co_await (auto i : arr) {\n" 24061 "}", 24062 Style); 24063 verifyFormat("for co_await (auto i : foo(T{})) {\n" 24064 "}", 24065 Style); 24066 } 24067 24068 TEST_F(FormatTest, CoroutineCoAwait) { 24069 verifyFormat("int x = co_await foo();"); 24070 verifyFormat("int x = (co_await foo());"); 24071 verifyFormat("co_await (42);"); 24072 verifyFormat("void operator co_await(int);"); 24073 verifyFormat("void operator co_await(a);"); 24074 verifyFormat("co_await a;"); 24075 verifyFormat("co_await missing_await_resume{};"); 24076 verifyFormat("co_await a; // comment"); 24077 verifyFormat("void test0() { co_await a; }"); 24078 verifyFormat("co_await co_await co_await foo();"); 24079 verifyFormat("co_await foo().bar();"); 24080 verifyFormat("co_await [this]() -> Task { co_return x; }"); 24081 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 24082 "foo(); }(x, y);"); 24083 24084 FormatStyle Style = getLLVMStyleWithColumns(40); 24085 verifyFormat("co_await [this](int a, int b) -> Task {\n" 24086 " co_return co_await foo();\n" 24087 "}(x, y);", 24088 Style); 24089 verifyFormat("co_await;"); 24090 } 24091 24092 TEST_F(FormatTest, CoroutineCoYield) { 24093 verifyFormat("int x = co_yield foo();"); 24094 verifyFormat("int x = (co_yield foo());"); 24095 verifyFormat("co_yield (42);"); 24096 verifyFormat("co_yield {42};"); 24097 verifyFormat("co_yield 42;"); 24098 verifyFormat("co_yield n++;"); 24099 verifyFormat("co_yield ++n;"); 24100 verifyFormat("co_yield;"); 24101 } 24102 24103 TEST_F(FormatTest, CoroutineCoReturn) { 24104 verifyFormat("co_return (42);"); 24105 verifyFormat("co_return;"); 24106 verifyFormat("co_return {};"); 24107 verifyFormat("co_return x;"); 24108 verifyFormat("co_return co_await foo();"); 24109 verifyFormat("co_return co_yield foo();"); 24110 } 24111 24112 TEST_F(FormatTest, EmptyShortBlock) { 24113 auto Style = getLLVMStyle(); 24114 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 24115 24116 verifyFormat("try {\n" 24117 " doA();\n" 24118 "} catch (Exception &e) {\n" 24119 " e.printStackTrace();\n" 24120 "}\n", 24121 Style); 24122 24123 verifyFormat("try {\n" 24124 " doA();\n" 24125 "} catch (Exception &e) {}\n", 24126 Style); 24127 } 24128 24129 TEST_F(FormatTest, ShortTemplatedArgumentLists) { 24130 auto Style = getLLVMStyle(); 24131 24132 verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style); 24133 verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style); 24134 verifyFormat("struct Y : X<[] { return 0; }> {};", Style); 24135 verifyFormat("struct Y<[] { return 0; }> {};", Style); 24136 24137 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style); 24138 verifyFormat("template <int N> struct Foo<char[N]> {};", Style); 24139 } 24140 24141 TEST_F(FormatTest, RemoveBraces) { 24142 FormatStyle Style = getLLVMStyle(); 24143 Style.RemoveBracesLLVM = true; 24144 24145 // The following eight test cases are fully-braced versions of the examples at 24146 // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single- 24147 // statement-bodies-of-if-else-loop-statements". 24148 24149 // 1. Omit the braces, since the body is simple and clearly associated with 24150 // the if. 24151 verifyFormat("if (isa<FunctionDecl>(D))\n" 24152 " handleFunctionDecl(D);\n" 24153 "else if (isa<VarDecl>(D))\n" 24154 " handleVarDecl(D);", 24155 "if (isa<FunctionDecl>(D)) {\n" 24156 " handleFunctionDecl(D);\n" 24157 "} else if (isa<VarDecl>(D)) {\n" 24158 " handleVarDecl(D);\n" 24159 "}", 24160 Style); 24161 24162 // 2. Here we document the condition itself and not the body. 24163 verifyFormat("if (isa<VarDecl>(D)) {\n" 24164 " // It is necessary that we explain the situation with this\n" 24165 " // surprisingly long comment, so it would be unclear\n" 24166 " // without the braces whether the following statement is in\n" 24167 " // the scope of the `if`.\n" 24168 " // Because the condition is documented, we can't really\n" 24169 " // hoist this comment that applies to the body above the\n" 24170 " // if.\n" 24171 " handleOtherDecl(D);\n" 24172 "}", 24173 Style); 24174 24175 // 3. Use braces on the outer `if` to avoid a potential dangling else 24176 // situation. 24177 verifyFormat("if (isa<VarDecl>(D)) {\n" 24178 " for (auto *A : D.attrs())\n" 24179 " if (shouldProcessAttr(A))\n" 24180 " handleAttr(A);\n" 24181 "}", 24182 "if (isa<VarDecl>(D)) {\n" 24183 " for (auto *A : D.attrs()) {\n" 24184 " if (shouldProcessAttr(A)) {\n" 24185 " handleAttr(A);\n" 24186 " }\n" 24187 " }\n" 24188 "}", 24189 Style); 24190 24191 // 4. Use braces for the `if` block to keep it uniform with the else block. 24192 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 24193 " handleFunctionDecl(D);\n" 24194 "} else {\n" 24195 " // In this else case, it is necessary that we explain the\n" 24196 " // situation with this surprisingly long comment, so it\n" 24197 " // would be unclear without the braces whether the\n" 24198 " // following statement is in the scope of the `if`.\n" 24199 " handleOtherDecl(D);\n" 24200 "}", 24201 Style); 24202 24203 // 5. This should also omit braces. The `for` loop contains only a single 24204 // statement, so it shouldn't have braces. The `if` also only contains a 24205 // single simple statement (the for loop), so it also should omit braces. 24206 verifyFormat("if (isa<FunctionDecl>(D))\n" 24207 " for (auto *A : D.attrs())\n" 24208 " handleAttr(A);", 24209 "if (isa<FunctionDecl>(D)) {\n" 24210 " for (auto *A : D.attrs()) {\n" 24211 " handleAttr(A);\n" 24212 " }\n" 24213 "}", 24214 Style); 24215 24216 // 6. Use braces for the outer `if` since the nested `for` is braced. 24217 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 24218 " for (auto *A : D.attrs()) {\n" 24219 " // In this for loop body, it is necessary that we explain\n" 24220 " // the situation with this surprisingly long comment,\n" 24221 " // forcing braces on the `for` block.\n" 24222 " handleAttr(A);\n" 24223 " }\n" 24224 "}", 24225 Style); 24226 24227 // 7. Use braces on the outer block because there are more than two levels of 24228 // nesting. 24229 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 24230 " for (auto *A : D.attrs())\n" 24231 " for (ssize_t i : llvm::seq<ssize_t>(count))\n" 24232 " handleAttrOnDecl(D, A, i);\n" 24233 "}", 24234 "if (isa<FunctionDecl>(D)) {\n" 24235 " for (auto *A : D.attrs()) {\n" 24236 " for (ssize_t i : llvm::seq<ssize_t>(count)) {\n" 24237 " handleAttrOnDecl(D, A, i);\n" 24238 " }\n" 24239 " }\n" 24240 "}", 24241 Style); 24242 24243 // 8. Use braces on the outer block because of a nested `if`, otherwise the 24244 // compiler would warn: `add explicit braces to avoid dangling else` 24245 verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 24246 " if (shouldProcess(D))\n" 24247 " handleVarDecl(D);\n" 24248 " else\n" 24249 " markAsIgnored(D);\n" 24250 "}", 24251 "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 24252 " if (shouldProcess(D)) {\n" 24253 " handleVarDecl(D);\n" 24254 " } else {\n" 24255 " markAsIgnored(D);\n" 24256 " }\n" 24257 "}", 24258 Style); 24259 24260 verifyFormat("if (a)\n" 24261 " b; // comment\n" 24262 "else if (c)\n" 24263 " d; /* comment */\n" 24264 "else\n" 24265 " e;", 24266 "if (a) {\n" 24267 " b; // comment\n" 24268 "} else if (c) {\n" 24269 " d; /* comment */\n" 24270 "} else {\n" 24271 " e;\n" 24272 "}", 24273 Style); 24274 24275 verifyFormat("if (a) {\n" 24276 " b;\n" 24277 " c;\n" 24278 "} else if (d) {\n" 24279 " e;\n" 24280 "}", 24281 Style); 24282 24283 verifyFormat("if (a) {\n" 24284 "#undef NDEBUG\n" 24285 " b;\n" 24286 "} else {\n" 24287 " c;\n" 24288 "}", 24289 Style); 24290 24291 verifyFormat("if (a) {\n" 24292 " // comment\n" 24293 "} else if (b) {\n" 24294 " c;\n" 24295 "}", 24296 Style); 24297 24298 verifyFormat("if (a) {\n" 24299 " b;\n" 24300 "} else {\n" 24301 " { c; }\n" 24302 "}", 24303 Style); 24304 24305 verifyFormat("if (a) {\n" 24306 " if (b) // comment\n" 24307 " c;\n" 24308 "} else if (d) {\n" 24309 " e;\n" 24310 "}", 24311 "if (a) {\n" 24312 " if (b) { // comment\n" 24313 " c;\n" 24314 " }\n" 24315 "} else if (d) {\n" 24316 " e;\n" 24317 "}", 24318 Style); 24319 24320 verifyFormat("if (a) {\n" 24321 " if (b) {\n" 24322 " c;\n" 24323 " // comment\n" 24324 " } else if (d) {\n" 24325 " e;\n" 24326 " }\n" 24327 "}", 24328 Style); 24329 24330 verifyFormat("if (a) {\n" 24331 " if (b)\n" 24332 " c;\n" 24333 "}", 24334 "if (a) {\n" 24335 " if (b) {\n" 24336 " c;\n" 24337 " }\n" 24338 "}", 24339 Style); 24340 24341 verifyFormat("if (a)\n" 24342 " if (b)\n" 24343 " c;\n" 24344 " else\n" 24345 " d;\n" 24346 "else\n" 24347 " e;", 24348 "if (a) {\n" 24349 " if (b) {\n" 24350 " c;\n" 24351 " } else {\n" 24352 " d;\n" 24353 " }\n" 24354 "} else {\n" 24355 " e;\n" 24356 "}", 24357 Style); 24358 24359 verifyFormat("if (a) {\n" 24360 " // comment\n" 24361 " if (b)\n" 24362 " c;\n" 24363 " else if (d)\n" 24364 " e;\n" 24365 "} else {\n" 24366 " g;\n" 24367 "}", 24368 "if (a) {\n" 24369 " // comment\n" 24370 " if (b) {\n" 24371 " c;\n" 24372 " } else if (d) {\n" 24373 " e;\n" 24374 " }\n" 24375 "} else {\n" 24376 " g;\n" 24377 "}", 24378 Style); 24379 24380 verifyFormat("if (a)\n" 24381 " b;\n" 24382 "else if (c)\n" 24383 " d;\n" 24384 "else\n" 24385 " e;", 24386 "if (a) {\n" 24387 " b;\n" 24388 "} else {\n" 24389 " if (c) {\n" 24390 " d;\n" 24391 " } else {\n" 24392 " e;\n" 24393 " }\n" 24394 "}", 24395 Style); 24396 24397 verifyFormat("if (a) {\n" 24398 " if (b)\n" 24399 " c;\n" 24400 " else if (d)\n" 24401 " e;\n" 24402 "} else {\n" 24403 " g;\n" 24404 "}", 24405 "if (a) {\n" 24406 " if (b)\n" 24407 " c;\n" 24408 " else {\n" 24409 " if (d)\n" 24410 " e;\n" 24411 " }\n" 24412 "} else {\n" 24413 " g;\n" 24414 "}", 24415 Style); 24416 24417 verifyFormat("if (a)\n" 24418 " b;\n" 24419 "else if (c)\n" 24420 " while (d)\n" 24421 " e;\n" 24422 "// comment", 24423 "if (a)\n" 24424 "{\n" 24425 " b;\n" 24426 "} else if (c) {\n" 24427 " while (d) {\n" 24428 " e;\n" 24429 " }\n" 24430 "}\n" 24431 "// comment", 24432 Style); 24433 24434 verifyFormat("if (a) {\n" 24435 " b;\n" 24436 "} else if (c) {\n" 24437 " d;\n" 24438 "} else {\n" 24439 " e;\n" 24440 " g;\n" 24441 "}", 24442 Style); 24443 24444 verifyFormat("if (a) {\n" 24445 " b;\n" 24446 "} else if (c) {\n" 24447 " d;\n" 24448 "} else {\n" 24449 " e;\n" 24450 "} // comment", 24451 Style); 24452 24453 verifyFormat("int abs = [](int i) {\n" 24454 " if (i >= 0)\n" 24455 " return i;\n" 24456 " return -i;\n" 24457 "};", 24458 "int abs = [](int i) {\n" 24459 " if (i >= 0) {\n" 24460 " return i;\n" 24461 " }\n" 24462 " return -i;\n" 24463 "};", 24464 Style); 24465 24466 // FIXME: See https://github.com/llvm/llvm-project/issues/53543. 24467 #if 0 24468 Style.ColumnLimit = 65; 24469 24470 verifyFormat("if (condition) {\n" 24471 " ff(Indices,\n" 24472 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 24473 "} else {\n" 24474 " ff(Indices,\n" 24475 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 24476 "}", 24477 Style); 24478 24479 Style.ColumnLimit = 20; 24480 24481 verifyFormat("if (a) {\n" 24482 " b = c + // 1 -\n" 24483 " d;\n" 24484 "}", 24485 Style); 24486 24487 verifyFormat("if (a) {\n" 24488 " b = c >= 0 ? d\n" 24489 " : e;\n" 24490 "}", 24491 "if (a) {\n" 24492 " b = c >= 0 ? d : e;\n" 24493 "}", 24494 Style); 24495 #endif 24496 24497 Style.ColumnLimit = 20; 24498 24499 verifyFormat("if (a)\n" 24500 " b = c > 0 ? d : e;", 24501 "if (a) {\n" 24502 " b = c > 0 ? d : e;\n" 24503 "}", 24504 Style); 24505 24506 Style.ColumnLimit = 0; 24507 24508 verifyFormat("if (a)\n" 24509 " b234567890223456789032345678904234567890 = " 24510 "c234567890223456789032345678904234567890;", 24511 "if (a) {\n" 24512 " b234567890223456789032345678904234567890 = " 24513 "c234567890223456789032345678904234567890;\n" 24514 "}", 24515 Style); 24516 } 24517 24518 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { 24519 auto Style = getLLVMStyle(); 24520 24521 StringRef Short = "functionCall(paramA, paramB, paramC);\n" 24522 "void functionDecl(int a, int b, int c);"; 24523 24524 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, " 24525 "paramF, paramG, paramH, paramI);\n" 24526 "void functionDecl(int argumentA, int argumentB, int " 24527 "argumentC, int argumentD, int argumentE);"; 24528 24529 verifyFormat(Short, Style); 24530 24531 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, " 24532 "paramF, paramG, paramH,\n" 24533 " paramI);\n" 24534 "void functionDecl(int argumentA, int argumentB, int " 24535 "argumentC, int argumentD,\n" 24536 " int argumentE);"; 24537 24538 verifyFormat(NoBreak, Medium, Style); 24539 verifyFormat(NoBreak, 24540 "functionCall(\n" 24541 " paramA,\n" 24542 " paramB,\n" 24543 " paramC,\n" 24544 " paramD,\n" 24545 " paramE,\n" 24546 " paramF,\n" 24547 " paramG,\n" 24548 " paramH,\n" 24549 " paramI\n" 24550 ");\n" 24551 "void functionDecl(\n" 24552 " int argumentA,\n" 24553 " int argumentB,\n" 24554 " int argumentC,\n" 24555 " int argumentD,\n" 24556 " int argumentE\n" 24557 ");", 24558 Style); 24559 24560 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n" 24561 " nestedLongFunctionCall(argument1, " 24562 "argument2, argument3,\n" 24563 " argument4, " 24564 "argument5));", 24565 Style); 24566 24567 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24568 24569 verifyFormat(Short, Style); 24570 verifyFormat( 24571 "functionCall(\n" 24572 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 24573 "paramI\n" 24574 ");\n" 24575 "void functionDecl(\n" 24576 " int argumentA, int argumentB, int argumentC, int argumentD, int " 24577 "argumentE\n" 24578 ");", 24579 Medium, Style); 24580 24581 Style.AllowAllArgumentsOnNextLine = false; 24582 Style.AllowAllParametersOfDeclarationOnNextLine = false; 24583 24584 verifyFormat(Short, Style); 24585 verifyFormat( 24586 "functionCall(\n" 24587 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 24588 "paramI\n" 24589 ");\n" 24590 "void functionDecl(\n" 24591 " int argumentA, int argumentB, int argumentC, int argumentD, int " 24592 "argumentE\n" 24593 ");", 24594 Medium, Style); 24595 24596 Style.BinPackArguments = false; 24597 Style.BinPackParameters = false; 24598 24599 verifyFormat(Short, Style); 24600 24601 verifyFormat("functionCall(\n" 24602 " paramA,\n" 24603 " paramB,\n" 24604 " paramC,\n" 24605 " paramD,\n" 24606 " paramE,\n" 24607 " paramF,\n" 24608 " paramG,\n" 24609 " paramH,\n" 24610 " paramI\n" 24611 ");\n" 24612 "void functionDecl(\n" 24613 " int argumentA,\n" 24614 " int argumentB,\n" 24615 " int argumentC,\n" 24616 " int argumentD,\n" 24617 " int argumentE\n" 24618 ");", 24619 Medium, Style); 24620 24621 verifyFormat("outerFunctionCall(\n" 24622 " nestedFunctionCall(argument1),\n" 24623 " nestedLongFunctionCall(\n" 24624 " argument1,\n" 24625 " argument2,\n" 24626 " argument3,\n" 24627 " argument4,\n" 24628 " argument5\n" 24629 " )\n" 24630 ");", 24631 Style); 24632 24633 verifyFormat("int a = (int)b;", Style); 24634 verifyFormat("int a = (int)b;", 24635 "int a = (\n" 24636 " int\n" 24637 ") b;", 24638 Style); 24639 24640 verifyFormat("return (true);", Style); 24641 verifyFormat("return (true);", 24642 "return (\n" 24643 " true\n" 24644 ");", 24645 Style); 24646 24647 verifyFormat("void foo();", Style); 24648 verifyFormat("void foo();", 24649 "void foo(\n" 24650 ");", 24651 Style); 24652 24653 verifyFormat("void foo() {}", Style); 24654 verifyFormat("void foo() {}", 24655 "void foo(\n" 24656 ") {\n" 24657 "}", 24658 Style); 24659 24660 verifyFormat("auto string = std::string();", Style); 24661 verifyFormat("auto string = std::string();", 24662 "auto string = std::string(\n" 24663 ");", 24664 Style); 24665 24666 verifyFormat("void (*functionPointer)() = nullptr;", Style); 24667 verifyFormat("void (*functionPointer)() = nullptr;", 24668 "void (\n" 24669 " *functionPointer\n" 24670 ")\n" 24671 "(\n" 24672 ") = nullptr;", 24673 Style); 24674 } 24675 24676 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) { 24677 auto Style = getLLVMStyle(); 24678 24679 verifyFormat("if (foo()) {\n" 24680 " return;\n" 24681 "}", 24682 Style); 24683 24684 verifyFormat("if (quitelongarg !=\n" 24685 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 24686 "comment\n" 24687 " return;\n" 24688 "}", 24689 Style); 24690 24691 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24692 24693 verifyFormat("if (foo()) {\n" 24694 " return;\n" 24695 "}", 24696 Style); 24697 24698 verifyFormat("if (quitelongarg !=\n" 24699 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 24700 "comment\n" 24701 " return;\n" 24702 "}", 24703 Style); 24704 } 24705 24706 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) { 24707 auto Style = getLLVMStyle(); 24708 24709 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 24710 " doSomething();\n" 24711 "}", 24712 Style); 24713 24714 verifyFormat("for (int myReallyLongCountVariable = 0; " 24715 "myReallyLongCountVariable < count;\n" 24716 " myReallyLongCountVariable++) {\n" 24717 " doSomething();\n" 24718 "}", 24719 Style); 24720 24721 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24722 24723 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 24724 " doSomething();\n" 24725 "}", 24726 Style); 24727 24728 verifyFormat("for (int myReallyLongCountVariable = 0; " 24729 "myReallyLongCountVariable < count;\n" 24730 " myReallyLongCountVariable++) {\n" 24731 " doSomething();\n" 24732 "}", 24733 Style); 24734 } 24735 24736 TEST_F(FormatTest, UnderstandsDigraphs) { 24737 verifyFormat("int arr<:5:> = {};"); 24738 verifyFormat("int arr[5] = <%%>;"); 24739 verifyFormat("int arr<:::qualified_variable:> = {};"); 24740 verifyFormat("int arr[::qualified_variable] = <%%>;"); 24741 verifyFormat("%:include <header>"); 24742 verifyFormat("%:define A x##y"); 24743 verifyFormat("#define A x%:%:y"); 24744 } 24745 24746 } // namespace 24747 } // namespace format 24748 } // namespace clang 24749