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 = clang::format::getLLVMStyle(); 266 CustomStyle.BreakBeforeBraces = clang::format::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("while (true)\n" 1468 " ;", 1469 AllowsMergedLoops); 1470 verifyFormat("for (;;)\n" 1471 " ;", 1472 AllowsMergedLoops); 1473 verifyFormat("for (;;)\n" 1474 " for (;;) continue;", 1475 AllowsMergedLoops); 1476 verifyFormat("for (;;) // Can't merge this\n" 1477 " continue;", 1478 AllowsMergedLoops); 1479 verifyFormat("for (;;) /* still don't merge */\n" 1480 " continue;", 1481 AllowsMergedLoops); 1482 verifyFormat("do a++;\n" 1483 "while (true);", 1484 AllowsMergedLoops); 1485 verifyFormat("do /* Don't merge */\n" 1486 " a++;\n" 1487 "while (true);", 1488 AllowsMergedLoops); 1489 verifyFormat("do // Don't merge\n" 1490 " a++;\n" 1491 "while (true);", 1492 AllowsMergedLoops); 1493 verifyFormat("do\n" 1494 " // Don't merge\n" 1495 " a++;\n" 1496 "while (true);", 1497 AllowsMergedLoops); 1498 // Without braces labels are interpreted differently. 1499 verifyFormat("{\n" 1500 " do\n" 1501 " label:\n" 1502 " a++;\n" 1503 " while (true);\n" 1504 "}", 1505 AllowsMergedLoops); 1506 } 1507 1508 TEST_F(FormatTest, FormatShortBracedStatements) { 1509 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1510 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1511 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1512 // Not IF to avoid any confusion that IF is somehow special. 1513 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1514 AllowSimpleBracedStatements.ColumnLimit = 40; 1515 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1516 FormatStyle::SBS_Always; 1517 1518 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1519 FormatStyle::SIS_WithoutElse; 1520 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1521 1522 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1523 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1524 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1525 1526 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1527 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1528 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1529 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1530 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1531 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1532 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1533 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1534 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1535 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1536 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1537 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1538 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1539 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1540 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1541 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1542 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1543 AllowSimpleBracedStatements); 1544 verifyFormat("if (true) {\n" 1545 " ffffffffffffffffffffffff();\n" 1546 "}", 1547 AllowSimpleBracedStatements); 1548 verifyFormat("if (true) {\n" 1549 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1550 "}", 1551 AllowSimpleBracedStatements); 1552 verifyFormat("if (true) { //\n" 1553 " f();\n" 1554 "}", 1555 AllowSimpleBracedStatements); 1556 verifyFormat("if (true) {\n" 1557 " f();\n" 1558 " f();\n" 1559 "}", 1560 AllowSimpleBracedStatements); 1561 verifyFormat("if (true) {\n" 1562 " f();\n" 1563 "} else {\n" 1564 " f();\n" 1565 "}", 1566 AllowSimpleBracedStatements); 1567 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1568 AllowSimpleBracedStatements); 1569 verifyFormat("MYIF (true) {\n" 1570 " ffffffffffffffffffffffff();\n" 1571 "}", 1572 AllowSimpleBracedStatements); 1573 verifyFormat("MYIF (true) {\n" 1574 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1575 "}", 1576 AllowSimpleBracedStatements); 1577 verifyFormat("MYIF (true) { //\n" 1578 " f();\n" 1579 "}", 1580 AllowSimpleBracedStatements); 1581 verifyFormat("MYIF (true) {\n" 1582 " f();\n" 1583 " f();\n" 1584 "}", 1585 AllowSimpleBracedStatements); 1586 verifyFormat("MYIF (true) {\n" 1587 " f();\n" 1588 "} else {\n" 1589 " f();\n" 1590 "}", 1591 AllowSimpleBracedStatements); 1592 1593 verifyFormat("struct A2 {\n" 1594 " int X;\n" 1595 "};", 1596 AllowSimpleBracedStatements); 1597 verifyFormat("typedef struct A2 {\n" 1598 " int X;\n" 1599 "} A2_t;", 1600 AllowSimpleBracedStatements); 1601 verifyFormat("template <int> struct A2 {\n" 1602 " struct B {};\n" 1603 "};", 1604 AllowSimpleBracedStatements); 1605 1606 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1607 FormatStyle::SIS_Never; 1608 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1609 verifyFormat("if (true) {\n" 1610 " f();\n" 1611 "}", 1612 AllowSimpleBracedStatements); 1613 verifyFormat("if (true) {\n" 1614 " f();\n" 1615 "} else {\n" 1616 " f();\n" 1617 "}", 1618 AllowSimpleBracedStatements); 1619 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1620 verifyFormat("MYIF (true) {\n" 1621 " f();\n" 1622 "}", 1623 AllowSimpleBracedStatements); 1624 verifyFormat("MYIF (true) {\n" 1625 " f();\n" 1626 "} else {\n" 1627 " f();\n" 1628 "}", 1629 AllowSimpleBracedStatements); 1630 1631 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1632 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1633 verifyFormat("while (true) {\n" 1634 " f();\n" 1635 "}", 1636 AllowSimpleBracedStatements); 1637 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1638 verifyFormat("for (;;) {\n" 1639 " f();\n" 1640 "}", 1641 AllowSimpleBracedStatements); 1642 1643 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1644 FormatStyle::SIS_WithoutElse; 1645 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1646 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1647 FormatStyle::BWACS_Always; 1648 1649 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1650 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1651 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1652 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1653 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1654 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1655 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1656 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1657 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1658 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1659 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1660 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1661 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1662 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1663 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1664 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1665 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1666 AllowSimpleBracedStatements); 1667 verifyFormat("if (true)\n" 1668 "{\n" 1669 " ffffffffffffffffffffffff();\n" 1670 "}", 1671 AllowSimpleBracedStatements); 1672 verifyFormat("if (true)\n" 1673 "{\n" 1674 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1675 "}", 1676 AllowSimpleBracedStatements); 1677 verifyFormat("if (true)\n" 1678 "{ //\n" 1679 " f();\n" 1680 "}", 1681 AllowSimpleBracedStatements); 1682 verifyFormat("if (true)\n" 1683 "{\n" 1684 " f();\n" 1685 " f();\n" 1686 "}", 1687 AllowSimpleBracedStatements); 1688 verifyFormat("if (true)\n" 1689 "{\n" 1690 " f();\n" 1691 "} else\n" 1692 "{\n" 1693 " f();\n" 1694 "}", 1695 AllowSimpleBracedStatements); 1696 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1697 AllowSimpleBracedStatements); 1698 verifyFormat("MYIF (true)\n" 1699 "{\n" 1700 " ffffffffffffffffffffffff();\n" 1701 "}", 1702 AllowSimpleBracedStatements); 1703 verifyFormat("MYIF (true)\n" 1704 "{\n" 1705 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1706 "}", 1707 AllowSimpleBracedStatements); 1708 verifyFormat("MYIF (true)\n" 1709 "{ //\n" 1710 " f();\n" 1711 "}", 1712 AllowSimpleBracedStatements); 1713 verifyFormat("MYIF (true)\n" 1714 "{\n" 1715 " f();\n" 1716 " f();\n" 1717 "}", 1718 AllowSimpleBracedStatements); 1719 verifyFormat("MYIF (true)\n" 1720 "{\n" 1721 " f();\n" 1722 "} else\n" 1723 "{\n" 1724 " f();\n" 1725 "}", 1726 AllowSimpleBracedStatements); 1727 1728 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1729 FormatStyle::SIS_Never; 1730 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1731 verifyFormat("if (true)\n" 1732 "{\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("MYIF (true) {}", AllowSimpleBracedStatements); 1745 verifyFormat("MYIF (true)\n" 1746 "{\n" 1747 " f();\n" 1748 "}", 1749 AllowSimpleBracedStatements); 1750 verifyFormat("MYIF (true)\n" 1751 "{\n" 1752 " f();\n" 1753 "} else\n" 1754 "{\n" 1755 " f();\n" 1756 "}", 1757 AllowSimpleBracedStatements); 1758 1759 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1760 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1761 verifyFormat("while (true)\n" 1762 "{\n" 1763 " f();\n" 1764 "}", 1765 AllowSimpleBracedStatements); 1766 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1767 verifyFormat("for (;;)\n" 1768 "{\n" 1769 " f();\n" 1770 "}", 1771 AllowSimpleBracedStatements); 1772 } 1773 1774 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1775 FormatStyle Style = getLLVMStyleWithColumns(60); 1776 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1777 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1778 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1779 EXPECT_EQ("#define A \\\n" 1780 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1781 " { \\\n" 1782 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1783 " }\n" 1784 "X;", 1785 format("#define A \\\n" 1786 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1787 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1788 " }\n" 1789 "X;", 1790 Style)); 1791 } 1792 1793 TEST_F(FormatTest, ParseIfElse) { 1794 verifyFormat("if (true)\n" 1795 " if (true)\n" 1796 " if (true)\n" 1797 " f();\n" 1798 " else\n" 1799 " g();\n" 1800 " else\n" 1801 " h();\n" 1802 "else\n" 1803 " i();"); 1804 verifyFormat("if (true)\n" 1805 " if (true)\n" 1806 " if (true) {\n" 1807 " if (true)\n" 1808 " f();\n" 1809 " } else {\n" 1810 " g();\n" 1811 " }\n" 1812 " else\n" 1813 " h();\n" 1814 "else {\n" 1815 " i();\n" 1816 "}"); 1817 verifyFormat("if (true)\n" 1818 " if constexpr (true)\n" 1819 " if (true) {\n" 1820 " if constexpr (true)\n" 1821 " f();\n" 1822 " } else {\n" 1823 " g();\n" 1824 " }\n" 1825 " else\n" 1826 " h();\n" 1827 "else {\n" 1828 " i();\n" 1829 "}"); 1830 verifyFormat("if (true)\n" 1831 " if CONSTEXPR (true)\n" 1832 " if (true) {\n" 1833 " if CONSTEXPR (true)\n" 1834 " f();\n" 1835 " } else {\n" 1836 " g();\n" 1837 " }\n" 1838 " else\n" 1839 " h();\n" 1840 "else {\n" 1841 " i();\n" 1842 "}"); 1843 verifyFormat("void f() {\n" 1844 " if (a) {\n" 1845 " } else {\n" 1846 " }\n" 1847 "}"); 1848 } 1849 1850 TEST_F(FormatTest, ElseIf) { 1851 verifyFormat("if (a) {\n} else if (b) {\n}"); 1852 verifyFormat("if (a)\n" 1853 " f();\n" 1854 "else if (b)\n" 1855 " g();\n" 1856 "else\n" 1857 " h();"); 1858 verifyFormat("if (a)\n" 1859 " f();\n" 1860 "else // comment\n" 1861 " if (b) {\n" 1862 " g();\n" 1863 " h();\n" 1864 " }"); 1865 verifyFormat("if constexpr (a)\n" 1866 " f();\n" 1867 "else if constexpr (b)\n" 1868 " g();\n" 1869 "else\n" 1870 " h();"); 1871 verifyFormat("if CONSTEXPR (a)\n" 1872 " f();\n" 1873 "else if CONSTEXPR (b)\n" 1874 " g();\n" 1875 "else\n" 1876 " h();"); 1877 verifyFormat("if (a) {\n" 1878 " f();\n" 1879 "}\n" 1880 "// or else ..\n" 1881 "else {\n" 1882 " g()\n" 1883 "}"); 1884 1885 verifyFormat("if (a) {\n" 1886 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1888 "}"); 1889 verifyFormat("if (a) {\n" 1890 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1892 "}"); 1893 verifyFormat("if (a) {\n" 1894 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1896 "}"); 1897 verifyFormat("if (a) {\n" 1898 "} else if (\n" 1899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1900 "}", 1901 getLLVMStyleWithColumns(62)); 1902 verifyFormat("if (a) {\n" 1903 "} else if constexpr (\n" 1904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1905 "}", 1906 getLLVMStyleWithColumns(62)); 1907 verifyFormat("if (a) {\n" 1908 "} else if CONSTEXPR (\n" 1909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1910 "}", 1911 getLLVMStyleWithColumns(62)); 1912 } 1913 1914 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 1915 FormatStyle Style = getLLVMStyle(); 1916 // Check first the default LLVM style 1917 // Style.PointerAlignment = FormatStyle::PAS_Right; 1918 // Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1919 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 1920 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 1921 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 1922 verifyFormat("int *f1(int &a) const &;", Style); 1923 verifyFormat("int *f1(int &a) const & = 0;", Style); 1924 verifyFormat("int *a = f1();", Style); 1925 verifyFormat("int &b = f2();", Style); 1926 verifyFormat("int &&c = f3();", Style); 1927 1928 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1929 verifyFormat("Const unsigned int *c;\n" 1930 "const unsigned int *d;\n" 1931 "Const unsigned int &e;\n" 1932 "const unsigned int &f;\n" 1933 "const unsigned &&g;\n" 1934 "Const unsigned h;", 1935 Style); 1936 1937 Style.PointerAlignment = FormatStyle::PAS_Left; 1938 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1939 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 1940 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 1941 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 1942 verifyFormat("int* f1(int& a) const& = 0;", Style); 1943 verifyFormat("int* a = f1();", Style); 1944 verifyFormat("int& b = f2();", Style); 1945 verifyFormat("int&& c = f3();", Style); 1946 1947 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1948 verifyFormat("Const unsigned int* c;\n" 1949 "const unsigned int* d;\n" 1950 "Const unsigned int& e;\n" 1951 "const unsigned int& f;\n" 1952 "const unsigned&& g;\n" 1953 "Const unsigned h;", 1954 Style); 1955 1956 Style.PointerAlignment = FormatStyle::PAS_Right; 1957 Style.ReferenceAlignment = FormatStyle::RAS_Left; 1958 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 1959 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 1960 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 1961 verifyFormat("int *a = f1();", Style); 1962 verifyFormat("int& b = f2();", Style); 1963 verifyFormat("int&& c = f3();", Style); 1964 1965 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1966 verifyFormat("Const unsigned int *c;\n" 1967 "const unsigned int *d;\n" 1968 "Const unsigned int& e;\n" 1969 "const unsigned int& f;\n" 1970 "const unsigned g;\n" 1971 "Const unsigned h;", 1972 Style); 1973 1974 Style.PointerAlignment = FormatStyle::PAS_Left; 1975 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 1976 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 1977 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 1978 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 1979 verifyFormat("int* a = f1();", Style); 1980 verifyFormat("int & b = f2();", Style); 1981 verifyFormat("int && c = f3();", Style); 1982 1983 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1984 verifyFormat("Const unsigned int* c;\n" 1985 "const unsigned int* d;\n" 1986 "Const unsigned int & e;\n" 1987 "const unsigned int & f;\n" 1988 "const unsigned && g;\n" 1989 "Const unsigned h;", 1990 Style); 1991 1992 Style.PointerAlignment = FormatStyle::PAS_Middle; 1993 Style.ReferenceAlignment = FormatStyle::RAS_Right; 1994 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 1995 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 1996 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 1997 verifyFormat("int * a = f1();", Style); 1998 verifyFormat("int &b = f2();", Style); 1999 verifyFormat("int &&c = f3();", Style); 2000 2001 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2002 // specifically handled 2003 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2004 } 2005 2006 TEST_F(FormatTest, FormatsForLoop) { 2007 verifyFormat( 2008 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2009 " ++VeryVeryLongLoopVariable)\n" 2010 " ;"); 2011 verifyFormat("for (;;)\n" 2012 " f();"); 2013 verifyFormat("for (;;) {\n}"); 2014 verifyFormat("for (;;) {\n" 2015 " f();\n" 2016 "}"); 2017 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2018 2019 verifyFormat( 2020 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2021 " E = UnwrappedLines.end();\n" 2022 " I != E; ++I) {\n}"); 2023 2024 verifyFormat( 2025 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2026 " ++IIIII) {\n}"); 2027 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2028 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2029 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2030 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2031 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2032 " E = FD->getDeclsInPrototypeScope().end();\n" 2033 " I != E; ++I) {\n}"); 2034 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2035 " I = Container.begin(),\n" 2036 " E = Container.end();\n" 2037 " I != E; ++I) {\n}", 2038 getLLVMStyleWithColumns(76)); 2039 2040 verifyFormat( 2041 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2045 " ++aaaaaaaaaaa) {\n}"); 2046 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2047 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2048 " ++i) {\n}"); 2049 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2050 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2051 "}"); 2052 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2053 " aaaaaaaaaa);\n" 2054 " iter; ++iter) {\n" 2055 "}"); 2056 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2058 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2059 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2060 2061 // These should not be formatted as Objective-C for-in loops. 2062 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2063 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2064 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2065 verifyFormat( 2066 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2067 2068 FormatStyle NoBinPacking = getLLVMStyle(); 2069 NoBinPacking.BinPackParameters = false; 2070 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2071 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2072 " aaaaaaaaaaaaaaaa,\n" 2073 " aaaaaaaaaaaaaaaa,\n" 2074 " aaaaaaaaaaaaaaaa);\n" 2075 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2076 "}", 2077 NoBinPacking); 2078 verifyFormat( 2079 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2080 " E = UnwrappedLines.end();\n" 2081 " I != E;\n" 2082 " ++I) {\n}", 2083 NoBinPacking); 2084 2085 FormatStyle AlignLeft = getLLVMStyle(); 2086 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2087 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2088 } 2089 2090 TEST_F(FormatTest, RangeBasedForLoops) { 2091 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2092 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2093 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2094 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2095 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2097 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2098 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2099 } 2100 2101 TEST_F(FormatTest, ForEachLoops) { 2102 verifyFormat("void f() {\n" 2103 " foreach (Item *item, itemlist) {}\n" 2104 " Q_FOREACH (Item *item, itemlist) {}\n" 2105 " BOOST_FOREACH (Item *item, itemlist) {}\n" 2106 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2107 "}"); 2108 2109 FormatStyle Style = getLLVMStyle(); 2110 Style.SpaceBeforeParens = 2111 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2112 verifyFormat("void f() {\n" 2113 " foreach(Item *item, itemlist) {}\n" 2114 " Q_FOREACH(Item *item, itemlist) {}\n" 2115 " BOOST_FOREACH(Item *item, itemlist) {}\n" 2116 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2117 "}", 2118 Style); 2119 2120 // As function-like macros. 2121 verifyFormat("#define foreach(x, y)\n" 2122 "#define Q_FOREACH(x, y)\n" 2123 "#define BOOST_FOREACH(x, y)\n" 2124 "#define UNKNOWN_FOREACH(x, y)\n"); 2125 2126 // Not as function-like macros. 2127 verifyFormat("#define foreach (x, y)\n" 2128 "#define Q_FOREACH (x, y)\n" 2129 "#define BOOST_FOREACH (x, y)\n" 2130 "#define UNKNOWN_FOREACH (x, y)\n"); 2131 2132 // handle microsoft non standard extension 2133 verifyFormat("for each (char c in x->MyStringProperty)"); 2134 } 2135 2136 TEST_F(FormatTest, FormatsWhileLoop) { 2137 verifyFormat("while (true) {\n}"); 2138 verifyFormat("while (true)\n" 2139 " f();"); 2140 verifyFormat("while () {\n}"); 2141 verifyFormat("while () {\n" 2142 " f();\n" 2143 "}"); 2144 } 2145 2146 TEST_F(FormatTest, FormatsDoWhile) { 2147 verifyFormat("do {\n" 2148 " do_something();\n" 2149 "} while (something());"); 2150 verifyFormat("do\n" 2151 " do_something();\n" 2152 "while (something());"); 2153 } 2154 2155 TEST_F(FormatTest, FormatsSwitchStatement) { 2156 verifyFormat("switch (x) {\n" 2157 "case 1:\n" 2158 " f();\n" 2159 " break;\n" 2160 "case kFoo:\n" 2161 "case ns::kBar:\n" 2162 "case kBaz:\n" 2163 " break;\n" 2164 "default:\n" 2165 " g();\n" 2166 " break;\n" 2167 "}"); 2168 verifyFormat("switch (x) {\n" 2169 "case 1: {\n" 2170 " f();\n" 2171 " break;\n" 2172 "}\n" 2173 "case 2: {\n" 2174 " break;\n" 2175 "}\n" 2176 "}"); 2177 verifyFormat("switch (x) {\n" 2178 "case 1: {\n" 2179 " f();\n" 2180 " {\n" 2181 " g();\n" 2182 " h();\n" 2183 " }\n" 2184 " break;\n" 2185 "}\n" 2186 "}"); 2187 verifyFormat("switch (x) {\n" 2188 "case 1: {\n" 2189 " f();\n" 2190 " if (foo) {\n" 2191 " g();\n" 2192 " h();\n" 2193 " }\n" 2194 " break;\n" 2195 "}\n" 2196 "}"); 2197 verifyFormat("switch (x) {\n" 2198 "case 1: {\n" 2199 " f();\n" 2200 " g();\n" 2201 "} break;\n" 2202 "}"); 2203 verifyFormat("switch (test)\n" 2204 " ;"); 2205 verifyFormat("switch (x) {\n" 2206 "default: {\n" 2207 " // Do nothing.\n" 2208 "}\n" 2209 "}"); 2210 verifyFormat("switch (x) {\n" 2211 "// comment\n" 2212 "// if 1, do f()\n" 2213 "case 1:\n" 2214 " f();\n" 2215 "}"); 2216 verifyFormat("switch (x) {\n" 2217 "case 1:\n" 2218 " // Do amazing stuff\n" 2219 " {\n" 2220 " f();\n" 2221 " g();\n" 2222 " }\n" 2223 " break;\n" 2224 "}"); 2225 verifyFormat("#define A \\\n" 2226 " switch (x) { \\\n" 2227 " case a: \\\n" 2228 " foo = b; \\\n" 2229 " }", 2230 getLLVMStyleWithColumns(20)); 2231 verifyFormat("#define OPERATION_CASE(name) \\\n" 2232 " case OP_name: \\\n" 2233 " return operations::Operation##name\n", 2234 getLLVMStyleWithColumns(40)); 2235 verifyFormat("switch (x) {\n" 2236 "case 1:;\n" 2237 "default:;\n" 2238 " int i;\n" 2239 "}"); 2240 2241 verifyGoogleFormat("switch (x) {\n" 2242 " case 1:\n" 2243 " f();\n" 2244 " break;\n" 2245 " case kFoo:\n" 2246 " case ns::kBar:\n" 2247 " case kBaz:\n" 2248 " break;\n" 2249 " default:\n" 2250 " g();\n" 2251 " break;\n" 2252 "}"); 2253 verifyGoogleFormat("switch (x) {\n" 2254 " case 1: {\n" 2255 " f();\n" 2256 " break;\n" 2257 " }\n" 2258 "}"); 2259 verifyGoogleFormat("switch (test)\n" 2260 " ;"); 2261 2262 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2263 " case OP_name: \\\n" 2264 " return operations::Operation##name\n"); 2265 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2266 " // Get the correction operation class.\n" 2267 " switch (OpCode) {\n" 2268 " CASE(Add);\n" 2269 " CASE(Subtract);\n" 2270 " default:\n" 2271 " return operations::Unknown;\n" 2272 " }\n" 2273 "#undef OPERATION_CASE\n" 2274 "}"); 2275 verifyFormat("DEBUG({\n" 2276 " switch (x) {\n" 2277 " case A:\n" 2278 " f();\n" 2279 " break;\n" 2280 " // fallthrough\n" 2281 " case B:\n" 2282 " g();\n" 2283 " break;\n" 2284 " }\n" 2285 "});"); 2286 EXPECT_EQ("DEBUG({\n" 2287 " switch (x) {\n" 2288 " case A:\n" 2289 " f();\n" 2290 " break;\n" 2291 " // On B:\n" 2292 " case B:\n" 2293 " g();\n" 2294 " break;\n" 2295 " }\n" 2296 "});", 2297 format("DEBUG({\n" 2298 " switch (x) {\n" 2299 " case A:\n" 2300 " f();\n" 2301 " break;\n" 2302 " // On B:\n" 2303 " case B:\n" 2304 " g();\n" 2305 " break;\n" 2306 " }\n" 2307 "});", 2308 getLLVMStyle())); 2309 EXPECT_EQ("switch (n) {\n" 2310 "case 0: {\n" 2311 " return false;\n" 2312 "}\n" 2313 "default: {\n" 2314 " return true;\n" 2315 "}\n" 2316 "}", 2317 format("switch (n)\n" 2318 "{\n" 2319 "case 0: {\n" 2320 " return false;\n" 2321 "}\n" 2322 "default: {\n" 2323 " return true;\n" 2324 "}\n" 2325 "}", 2326 getLLVMStyle())); 2327 verifyFormat("switch (a) {\n" 2328 "case (b):\n" 2329 " return;\n" 2330 "}"); 2331 2332 verifyFormat("switch (a) {\n" 2333 "case some_namespace::\n" 2334 " some_constant:\n" 2335 " return;\n" 2336 "}", 2337 getLLVMStyleWithColumns(34)); 2338 2339 FormatStyle Style = getLLVMStyle(); 2340 Style.IndentCaseLabels = true; 2341 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2342 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2343 Style.BraceWrapping.AfterCaseLabel = true; 2344 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2345 EXPECT_EQ("switch (n)\n" 2346 "{\n" 2347 " case 0:\n" 2348 " {\n" 2349 " return false;\n" 2350 " }\n" 2351 " default:\n" 2352 " {\n" 2353 " return true;\n" 2354 " }\n" 2355 "}", 2356 format("switch (n) {\n" 2357 " case 0: {\n" 2358 " return false;\n" 2359 " }\n" 2360 " default: {\n" 2361 " return true;\n" 2362 " }\n" 2363 "}", 2364 Style)); 2365 Style.BraceWrapping.AfterCaseLabel = false; 2366 EXPECT_EQ("switch (n)\n" 2367 "{\n" 2368 " case 0: {\n" 2369 " return false;\n" 2370 " }\n" 2371 " default: {\n" 2372 " return true;\n" 2373 " }\n" 2374 "}", 2375 format("switch (n) {\n" 2376 " case 0:\n" 2377 " {\n" 2378 " return false;\n" 2379 " }\n" 2380 " default:\n" 2381 " {\n" 2382 " return true;\n" 2383 " }\n" 2384 "}", 2385 Style)); 2386 Style.IndentCaseLabels = false; 2387 Style.IndentCaseBlocks = true; 2388 EXPECT_EQ("switch (n)\n" 2389 "{\n" 2390 "case 0:\n" 2391 " {\n" 2392 " return false;\n" 2393 " }\n" 2394 "case 1:\n" 2395 " break;\n" 2396 "default:\n" 2397 " {\n" 2398 " return true;\n" 2399 " }\n" 2400 "}", 2401 format("switch (n) {\n" 2402 "case 0: {\n" 2403 " return false;\n" 2404 "}\n" 2405 "case 1:\n" 2406 " break;\n" 2407 "default: {\n" 2408 " return true;\n" 2409 "}\n" 2410 "}", 2411 Style)); 2412 Style.IndentCaseLabels = true; 2413 Style.IndentCaseBlocks = true; 2414 EXPECT_EQ("switch (n)\n" 2415 "{\n" 2416 " case 0:\n" 2417 " {\n" 2418 " return false;\n" 2419 " }\n" 2420 " case 1:\n" 2421 " break;\n" 2422 " default:\n" 2423 " {\n" 2424 " return true;\n" 2425 " }\n" 2426 "}", 2427 format("switch (n) {\n" 2428 "case 0: {\n" 2429 " return false;\n" 2430 "}\n" 2431 "case 1:\n" 2432 " break;\n" 2433 "default: {\n" 2434 " return true;\n" 2435 "}\n" 2436 "}", 2437 Style)); 2438 } 2439 2440 TEST_F(FormatTest, CaseRanges) { 2441 verifyFormat("switch (x) {\n" 2442 "case 'A' ... 'Z':\n" 2443 "case 1 ... 5:\n" 2444 "case a ... b:\n" 2445 " break;\n" 2446 "}"); 2447 } 2448 2449 TEST_F(FormatTest, ShortEnums) { 2450 FormatStyle Style = getLLVMStyle(); 2451 Style.AllowShortEnumsOnASingleLine = true; 2452 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2453 Style.AllowShortEnumsOnASingleLine = false; 2454 verifyFormat("enum {\n" 2455 " A,\n" 2456 " B,\n" 2457 " C\n" 2458 "} ShortEnum1, ShortEnum2;", 2459 Style); 2460 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2461 Style.BraceWrapping.AfterEnum = true; 2462 verifyFormat("enum\n" 2463 "{\n" 2464 " A,\n" 2465 " B,\n" 2466 " C\n" 2467 "} ShortEnum1, ShortEnum2;", 2468 Style); 2469 } 2470 2471 TEST_F(FormatTest, ShortCaseLabels) { 2472 FormatStyle Style = getLLVMStyle(); 2473 Style.AllowShortCaseLabelsOnASingleLine = true; 2474 verifyFormat("switch (a) {\n" 2475 "case 1: x = 1; break;\n" 2476 "case 2: return;\n" 2477 "case 3:\n" 2478 "case 4:\n" 2479 "case 5: return;\n" 2480 "case 6: // comment\n" 2481 " return;\n" 2482 "case 7:\n" 2483 " // comment\n" 2484 " return;\n" 2485 "case 8:\n" 2486 " x = 8; // comment\n" 2487 " break;\n" 2488 "default: y = 1; break;\n" 2489 "}", 2490 Style); 2491 verifyFormat("switch (a) {\n" 2492 "case 0: return; // comment\n" 2493 "case 1: break; // comment\n" 2494 "case 2: return;\n" 2495 "// comment\n" 2496 "case 3: return;\n" 2497 "// comment 1\n" 2498 "// comment 2\n" 2499 "// comment 3\n" 2500 "case 4: break; /* comment */\n" 2501 "case 5:\n" 2502 " // comment\n" 2503 " break;\n" 2504 "case 6: /* comment */ x = 1; break;\n" 2505 "case 7: x = /* comment */ 1; break;\n" 2506 "case 8:\n" 2507 " x = 1; /* comment */\n" 2508 " break;\n" 2509 "case 9:\n" 2510 " break; // comment line 1\n" 2511 " // comment line 2\n" 2512 "}", 2513 Style); 2514 EXPECT_EQ("switch (a) {\n" 2515 "case 1:\n" 2516 " x = 8;\n" 2517 " // fall through\n" 2518 "case 2: x = 8;\n" 2519 "// comment\n" 2520 "case 3:\n" 2521 " return; /* comment line 1\n" 2522 " * comment line 2 */\n" 2523 "case 4: i = 8;\n" 2524 "// something else\n" 2525 "#if FOO\n" 2526 "case 5: break;\n" 2527 "#endif\n" 2528 "}", 2529 format("switch (a) {\n" 2530 "case 1: x = 8;\n" 2531 " // fall through\n" 2532 "case 2:\n" 2533 " x = 8;\n" 2534 "// comment\n" 2535 "case 3:\n" 2536 " return; /* comment line 1\n" 2537 " * comment line 2 */\n" 2538 "case 4:\n" 2539 " i = 8;\n" 2540 "// something else\n" 2541 "#if FOO\n" 2542 "case 5: break;\n" 2543 "#endif\n" 2544 "}", 2545 Style)); 2546 EXPECT_EQ("switch (a) {\n" 2547 "case 0:\n" 2548 " return; // long long long long long long long long long long " 2549 "long long comment\n" 2550 " // line\n" 2551 "}", 2552 format("switch (a) {\n" 2553 "case 0: return; // long long long long long long long long " 2554 "long long long long comment line\n" 2555 "}", 2556 Style)); 2557 EXPECT_EQ("switch (a) {\n" 2558 "case 0:\n" 2559 " return; /* long long long long long long long long long long " 2560 "long long comment\n" 2561 " line */\n" 2562 "}", 2563 format("switch (a) {\n" 2564 "case 0: return; /* long long long long long long long long " 2565 "long long long long comment line */\n" 2566 "}", 2567 Style)); 2568 verifyFormat("switch (a) {\n" 2569 "#if FOO\n" 2570 "case 0: return 0;\n" 2571 "#endif\n" 2572 "}", 2573 Style); 2574 verifyFormat("switch (a) {\n" 2575 "case 1: {\n" 2576 "}\n" 2577 "case 2: {\n" 2578 " return;\n" 2579 "}\n" 2580 "case 3: {\n" 2581 " x = 1;\n" 2582 " return;\n" 2583 "}\n" 2584 "case 4:\n" 2585 " if (x)\n" 2586 " return;\n" 2587 "}", 2588 Style); 2589 Style.ColumnLimit = 21; 2590 verifyFormat("switch (a) {\n" 2591 "case 1: x = 1; break;\n" 2592 "case 2: return;\n" 2593 "case 3:\n" 2594 "case 4:\n" 2595 "case 5: return;\n" 2596 "default:\n" 2597 " y = 1;\n" 2598 " break;\n" 2599 "}", 2600 Style); 2601 Style.ColumnLimit = 80; 2602 Style.AllowShortCaseLabelsOnASingleLine = false; 2603 Style.IndentCaseLabels = true; 2604 EXPECT_EQ("switch (n) {\n" 2605 " default /*comments*/:\n" 2606 " return true;\n" 2607 " case 0:\n" 2608 " return false;\n" 2609 "}", 2610 format("switch (n) {\n" 2611 "default/*comments*/:\n" 2612 " return true;\n" 2613 "case 0:\n" 2614 " return false;\n" 2615 "}", 2616 Style)); 2617 Style.AllowShortCaseLabelsOnASingleLine = true; 2618 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2619 Style.BraceWrapping.AfterCaseLabel = true; 2620 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2621 EXPECT_EQ("switch (n)\n" 2622 "{\n" 2623 " case 0:\n" 2624 " {\n" 2625 " return false;\n" 2626 " }\n" 2627 " default:\n" 2628 " {\n" 2629 " return true;\n" 2630 " }\n" 2631 "}", 2632 format("switch (n) {\n" 2633 " case 0: {\n" 2634 " return false;\n" 2635 " }\n" 2636 " default:\n" 2637 " {\n" 2638 " return true;\n" 2639 " }\n" 2640 "}", 2641 Style)); 2642 } 2643 2644 TEST_F(FormatTest, FormatsLabels) { 2645 verifyFormat("void f() {\n" 2646 " some_code();\n" 2647 "test_label:\n" 2648 " some_other_code();\n" 2649 " {\n" 2650 " some_more_code();\n" 2651 " another_label:\n" 2652 " some_more_code();\n" 2653 " }\n" 2654 "}"); 2655 verifyFormat("{\n" 2656 " some_code();\n" 2657 "test_label:\n" 2658 " some_other_code();\n" 2659 "}"); 2660 verifyFormat("{\n" 2661 " some_code();\n" 2662 "test_label:;\n" 2663 " int i = 0;\n" 2664 "}"); 2665 FormatStyle Style = getLLVMStyle(); 2666 Style.IndentGotoLabels = false; 2667 verifyFormat("void f() {\n" 2668 " some_code();\n" 2669 "test_label:\n" 2670 " some_other_code();\n" 2671 " {\n" 2672 " some_more_code();\n" 2673 "another_label:\n" 2674 " some_more_code();\n" 2675 " }\n" 2676 "}", 2677 Style); 2678 verifyFormat("{\n" 2679 " some_code();\n" 2680 "test_label:\n" 2681 " some_other_code();\n" 2682 "}", 2683 Style); 2684 verifyFormat("{\n" 2685 " some_code();\n" 2686 "test_label:;\n" 2687 " int i = 0;\n" 2688 "}"); 2689 } 2690 2691 TEST_F(FormatTest, MultiLineControlStatements) { 2692 FormatStyle Style = getLLVMStyle(); 2693 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2694 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2695 Style.ColumnLimit = 20; 2696 // Short lines should keep opening brace on same line. 2697 EXPECT_EQ("if (foo) {\n" 2698 " bar();\n" 2699 "}", 2700 format("if(foo){bar();}", Style)); 2701 EXPECT_EQ("if (foo) {\n" 2702 " bar();\n" 2703 "} else {\n" 2704 " baz();\n" 2705 "}", 2706 format("if(foo){bar();}else{baz();}", Style)); 2707 EXPECT_EQ("if (foo && bar) {\n" 2708 " baz();\n" 2709 "}", 2710 format("if(foo&&bar){baz();}", Style)); 2711 EXPECT_EQ("if (foo) {\n" 2712 " bar();\n" 2713 "} else if (baz) {\n" 2714 " quux();\n" 2715 "}", 2716 format("if(foo){bar();}else if(baz){quux();}", Style)); 2717 EXPECT_EQ( 2718 "if (foo) {\n" 2719 " bar();\n" 2720 "} else if (baz) {\n" 2721 " quux();\n" 2722 "} else {\n" 2723 " foobar();\n" 2724 "}", 2725 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2726 EXPECT_EQ("for (;;) {\n" 2727 " foo();\n" 2728 "}", 2729 format("for(;;){foo();}")); 2730 EXPECT_EQ("while (1) {\n" 2731 " foo();\n" 2732 "}", 2733 format("while(1){foo();}", Style)); 2734 EXPECT_EQ("switch (foo) {\n" 2735 "case bar:\n" 2736 " return;\n" 2737 "}", 2738 format("switch(foo){case bar:return;}", Style)); 2739 EXPECT_EQ("try {\n" 2740 " foo();\n" 2741 "} catch (...) {\n" 2742 " bar();\n" 2743 "}", 2744 format("try{foo();}catch(...){bar();}", Style)); 2745 EXPECT_EQ("do {\n" 2746 " foo();\n" 2747 "} while (bar &&\n" 2748 " baz);", 2749 format("do{foo();}while(bar&&baz);", Style)); 2750 // Long lines should put opening brace on new line. 2751 EXPECT_EQ("if (foo && bar &&\n" 2752 " baz)\n" 2753 "{\n" 2754 " quux();\n" 2755 "}", 2756 format("if(foo&&bar&&baz){quux();}", Style)); 2757 EXPECT_EQ("if (foo && bar &&\n" 2758 " baz)\n" 2759 "{\n" 2760 " quux();\n" 2761 "}", 2762 format("if (foo && bar &&\n" 2763 " baz) {\n" 2764 " quux();\n" 2765 "}", 2766 Style)); 2767 EXPECT_EQ("if (foo) {\n" 2768 " bar();\n" 2769 "} else if (baz ||\n" 2770 " quux)\n" 2771 "{\n" 2772 " foobar();\n" 2773 "}", 2774 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2775 EXPECT_EQ( 2776 "if (foo) {\n" 2777 " bar();\n" 2778 "} else if (baz ||\n" 2779 " quux)\n" 2780 "{\n" 2781 " foobar();\n" 2782 "} else {\n" 2783 " barbaz();\n" 2784 "}", 2785 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2786 Style)); 2787 EXPECT_EQ("for (int i = 0;\n" 2788 " i < 10; ++i)\n" 2789 "{\n" 2790 " foo();\n" 2791 "}", 2792 format("for(int i=0;i<10;++i){foo();}", Style)); 2793 EXPECT_EQ("foreach (int i,\n" 2794 " list)\n" 2795 "{\n" 2796 " foo();\n" 2797 "}", 2798 format("foreach(int i, list){foo();}", Style)); 2799 Style.ColumnLimit = 2800 40; // to concentrate at brace wrapping, not line wrap due to column limit 2801 EXPECT_EQ("foreach (int i, list) {\n" 2802 " foo();\n" 2803 "}", 2804 format("foreach(int i, list){foo();}", Style)); 2805 Style.ColumnLimit = 2806 20; // to concentrate at brace wrapping, not line wrap due to column limit 2807 EXPECT_EQ("while (foo || bar ||\n" 2808 " baz)\n" 2809 "{\n" 2810 " quux();\n" 2811 "}", 2812 format("while(foo||bar||baz){quux();}", Style)); 2813 EXPECT_EQ("switch (\n" 2814 " foo = barbaz)\n" 2815 "{\n" 2816 "case quux:\n" 2817 " return;\n" 2818 "}", 2819 format("switch(foo=barbaz){case quux:return;}", Style)); 2820 EXPECT_EQ("try {\n" 2821 " foo();\n" 2822 "} catch (\n" 2823 " Exception &bar)\n" 2824 "{\n" 2825 " baz();\n" 2826 "}", 2827 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2828 Style.ColumnLimit = 2829 40; // to concentrate at brace wrapping, not line wrap due to column limit 2830 EXPECT_EQ("try {\n" 2831 " foo();\n" 2832 "} catch (Exception &bar) {\n" 2833 " baz();\n" 2834 "}", 2835 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2836 Style.ColumnLimit = 2837 20; // to concentrate at brace wrapping, not line wrap due to column limit 2838 2839 Style.BraceWrapping.BeforeElse = true; 2840 EXPECT_EQ( 2841 "if (foo) {\n" 2842 " bar();\n" 2843 "}\n" 2844 "else if (baz ||\n" 2845 " quux)\n" 2846 "{\n" 2847 " foobar();\n" 2848 "}\n" 2849 "else {\n" 2850 " barbaz();\n" 2851 "}", 2852 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2853 Style)); 2854 2855 Style.BraceWrapping.BeforeCatch = true; 2856 EXPECT_EQ("try {\n" 2857 " foo();\n" 2858 "}\n" 2859 "catch (...) {\n" 2860 " baz();\n" 2861 "}", 2862 format("try{foo();}catch(...){baz();}", Style)); 2863 2864 Style.BraceWrapping.AfterFunction = true; 2865 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2866 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 2867 Style.ColumnLimit = 80; 2868 verifyFormat("void shortfunction() { bar(); }", Style); 2869 2870 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 2871 verifyFormat("void shortfunction()\n" 2872 "{\n" 2873 " bar();\n" 2874 "}", 2875 Style); 2876 } 2877 2878 TEST_F(FormatTest, BeforeWhile) { 2879 FormatStyle Style = getLLVMStyle(); 2880 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2881 2882 verifyFormat("do {\n" 2883 " foo();\n" 2884 "} while (1);", 2885 Style); 2886 Style.BraceWrapping.BeforeWhile = true; 2887 verifyFormat("do {\n" 2888 " foo();\n" 2889 "}\n" 2890 "while (1);", 2891 Style); 2892 } 2893 2894 //===----------------------------------------------------------------------===// 2895 // Tests for classes, namespaces, etc. 2896 //===----------------------------------------------------------------------===// 2897 2898 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 2899 verifyFormat("class A {};"); 2900 } 2901 2902 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 2903 verifyFormat("class A {\n" 2904 "public:\n" 2905 "public: // comment\n" 2906 "protected:\n" 2907 "private:\n" 2908 " void f() {}\n" 2909 "};"); 2910 verifyFormat("export class A {\n" 2911 "public:\n" 2912 "public: // comment\n" 2913 "protected:\n" 2914 "private:\n" 2915 " void f() {}\n" 2916 "};"); 2917 verifyGoogleFormat("class A {\n" 2918 " public:\n" 2919 " protected:\n" 2920 " private:\n" 2921 " void f() {}\n" 2922 "};"); 2923 verifyGoogleFormat("export class A {\n" 2924 " public:\n" 2925 " protected:\n" 2926 " private:\n" 2927 " void f() {}\n" 2928 "};"); 2929 verifyFormat("class A {\n" 2930 "public slots:\n" 2931 " void f1() {}\n" 2932 "public Q_SLOTS:\n" 2933 " void f2() {}\n" 2934 "protected slots:\n" 2935 " void f3() {}\n" 2936 "protected Q_SLOTS:\n" 2937 " void f4() {}\n" 2938 "private slots:\n" 2939 " void f5() {}\n" 2940 "private Q_SLOTS:\n" 2941 " void f6() {}\n" 2942 "signals:\n" 2943 " void g1();\n" 2944 "Q_SIGNALS:\n" 2945 " void g2();\n" 2946 "};"); 2947 2948 // Don't interpret 'signals' the wrong way. 2949 verifyFormat("signals.set();"); 2950 verifyFormat("for (Signals signals : f()) {\n}"); 2951 verifyFormat("{\n" 2952 " signals.set(); // This needs indentation.\n" 2953 "}"); 2954 verifyFormat("void f() {\n" 2955 "label:\n" 2956 " signals.baz();\n" 2957 "}"); 2958 } 2959 2960 TEST_F(FormatTest, SeparatesLogicalBlocks) { 2961 EXPECT_EQ("class A {\n" 2962 "public:\n" 2963 " void f();\n" 2964 "\n" 2965 "private:\n" 2966 " void g() {}\n" 2967 " // test\n" 2968 "protected:\n" 2969 " int h;\n" 2970 "};", 2971 format("class A {\n" 2972 "public:\n" 2973 "void f();\n" 2974 "private:\n" 2975 "void g() {}\n" 2976 "// test\n" 2977 "protected:\n" 2978 "int h;\n" 2979 "};")); 2980 EXPECT_EQ("class A {\n" 2981 "protected:\n" 2982 "public:\n" 2983 " void f();\n" 2984 "};", 2985 format("class A {\n" 2986 "protected:\n" 2987 "\n" 2988 "public:\n" 2989 "\n" 2990 " void f();\n" 2991 "};")); 2992 2993 // Even ensure proper spacing inside macros. 2994 EXPECT_EQ("#define B \\\n" 2995 " class A { \\\n" 2996 " protected: \\\n" 2997 " public: \\\n" 2998 " void f(); \\\n" 2999 " };", 3000 format("#define B \\\n" 3001 " class A { \\\n" 3002 " protected: \\\n" 3003 " \\\n" 3004 " public: \\\n" 3005 " \\\n" 3006 " void f(); \\\n" 3007 " };", 3008 getGoogleStyle())); 3009 // But don't remove empty lines after macros ending in access specifiers. 3010 EXPECT_EQ("#define A private:\n" 3011 "\n" 3012 "int i;", 3013 format("#define A private:\n" 3014 "\n" 3015 "int i;")); 3016 } 3017 3018 TEST_F(FormatTest, FormatsClasses) { 3019 verifyFormat("class A : public B {};"); 3020 verifyFormat("class A : public ::B {};"); 3021 3022 verifyFormat( 3023 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3024 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3025 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3026 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3027 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3028 verifyFormat( 3029 "class A : public B, public C, public D, public E, public F {};"); 3030 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3031 " public C,\n" 3032 " public D,\n" 3033 " public E,\n" 3034 " public F,\n" 3035 " public G {};"); 3036 3037 verifyFormat("class\n" 3038 " ReallyReallyLongClassName {\n" 3039 " int i;\n" 3040 "};", 3041 getLLVMStyleWithColumns(32)); 3042 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3043 " aaaaaaaaaaaaaaaa> {};"); 3044 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3045 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3046 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3047 verifyFormat("template <class R, class C>\n" 3048 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3049 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3050 verifyFormat("class ::A::B {};"); 3051 } 3052 3053 TEST_F(FormatTest, BreakInheritanceStyle) { 3054 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3055 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3056 FormatStyle::BILS_BeforeComma; 3057 verifyFormat("class MyClass : public X {};", 3058 StyleWithInheritanceBreakBeforeComma); 3059 verifyFormat("class MyClass\n" 3060 " : public X\n" 3061 " , public Y {};", 3062 StyleWithInheritanceBreakBeforeComma); 3063 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3064 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3065 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3066 StyleWithInheritanceBreakBeforeComma); 3067 verifyFormat("struct aaaaaaaaaaaaa\n" 3068 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3069 " aaaaaaaaaaaaaaaa> {};", 3070 StyleWithInheritanceBreakBeforeComma); 3071 3072 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3073 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3074 FormatStyle::BILS_AfterColon; 3075 verifyFormat("class MyClass : public X {};", 3076 StyleWithInheritanceBreakAfterColon); 3077 verifyFormat("class MyClass : public X, public Y {};", 3078 StyleWithInheritanceBreakAfterColon); 3079 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3080 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3081 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3082 StyleWithInheritanceBreakAfterColon); 3083 verifyFormat("struct aaaaaaaaaaaaa :\n" 3084 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3085 " aaaaaaaaaaaaaaaa> {};", 3086 StyleWithInheritanceBreakAfterColon); 3087 3088 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3089 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3090 FormatStyle::BILS_AfterComma; 3091 verifyFormat("class MyClass : public X {};", 3092 StyleWithInheritanceBreakAfterComma); 3093 verifyFormat("class MyClass : public X,\n" 3094 " public Y {};", 3095 StyleWithInheritanceBreakAfterComma); 3096 verifyFormat( 3097 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3098 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3099 "{};", 3100 StyleWithInheritanceBreakAfterComma); 3101 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3102 " aaaaaaaaaaaaaaaa> {};", 3103 StyleWithInheritanceBreakAfterComma); 3104 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3105 " : public OnceBreak,\n" 3106 " public AlwaysBreak,\n" 3107 " EvenBasesFitInOneLine {};", 3108 StyleWithInheritanceBreakAfterComma); 3109 } 3110 3111 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 3112 verifyFormat("class A {\n} a, b;"); 3113 verifyFormat("struct A {\n} a, b;"); 3114 verifyFormat("union A {\n} a;"); 3115 } 3116 3117 TEST_F(FormatTest, FormatsEnum) { 3118 verifyFormat("enum {\n" 3119 " Zero,\n" 3120 " One = 1,\n" 3121 " Two = One + 1,\n" 3122 " Three = (One + Two),\n" 3123 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3124 " Five = (One, Two, Three, Four, 5)\n" 3125 "};"); 3126 verifyGoogleFormat("enum {\n" 3127 " Zero,\n" 3128 " One = 1,\n" 3129 " Two = One + 1,\n" 3130 " Three = (One + Two),\n" 3131 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3132 " Five = (One, Two, Three, Four, 5)\n" 3133 "};"); 3134 verifyFormat("enum Enum {};"); 3135 verifyFormat("enum {};"); 3136 verifyFormat("enum X E {} d;"); 3137 verifyFormat("enum __attribute__((...)) E {} d;"); 3138 verifyFormat("enum __declspec__((...)) E {} d;"); 3139 verifyFormat("enum {\n" 3140 " Bar = Foo<int, int>::value\n" 3141 "};", 3142 getLLVMStyleWithColumns(30)); 3143 3144 verifyFormat("enum ShortEnum { A, B, C };"); 3145 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3146 3147 EXPECT_EQ("enum KeepEmptyLines {\n" 3148 " ONE,\n" 3149 "\n" 3150 " TWO,\n" 3151 "\n" 3152 " THREE\n" 3153 "}", 3154 format("enum KeepEmptyLines {\n" 3155 " ONE,\n" 3156 "\n" 3157 " TWO,\n" 3158 "\n" 3159 "\n" 3160 " THREE\n" 3161 "}")); 3162 verifyFormat("enum E { // comment\n" 3163 " ONE,\n" 3164 " TWO\n" 3165 "};\n" 3166 "int i;"); 3167 3168 FormatStyle EightIndent = getLLVMStyle(); 3169 EightIndent.IndentWidth = 8; 3170 verifyFormat("enum {\n" 3171 " VOID,\n" 3172 " CHAR,\n" 3173 " SHORT,\n" 3174 " INT,\n" 3175 " LONG,\n" 3176 " SIGNED,\n" 3177 " UNSIGNED,\n" 3178 " BOOL,\n" 3179 " FLOAT,\n" 3180 " DOUBLE,\n" 3181 " COMPLEX\n" 3182 "};", 3183 EightIndent); 3184 3185 // Not enums. 3186 verifyFormat("enum X f() {\n" 3187 " a();\n" 3188 " return 42;\n" 3189 "}"); 3190 verifyFormat("enum X Type::f() {\n" 3191 " a();\n" 3192 " return 42;\n" 3193 "}"); 3194 verifyFormat("enum ::X f() {\n" 3195 " a();\n" 3196 " return 42;\n" 3197 "}"); 3198 verifyFormat("enum ns::X f() {\n" 3199 " a();\n" 3200 " return 42;\n" 3201 "}"); 3202 } 3203 3204 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3205 verifyFormat("enum Type {\n" 3206 " One = 0; // These semicolons should be commas.\n" 3207 " Two = 1;\n" 3208 "};"); 3209 verifyFormat("namespace n {\n" 3210 "enum Type {\n" 3211 " One,\n" 3212 " Two, // missing };\n" 3213 " int i;\n" 3214 "}\n" 3215 "void g() {}"); 3216 } 3217 3218 TEST_F(FormatTest, FormatsEnumStruct) { 3219 verifyFormat("enum struct {\n" 3220 " Zero,\n" 3221 " One = 1,\n" 3222 " Two = One + 1,\n" 3223 " Three = (One + Two),\n" 3224 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3225 " Five = (One, Two, Three, Four, 5)\n" 3226 "};"); 3227 verifyFormat("enum struct Enum {};"); 3228 verifyFormat("enum struct {};"); 3229 verifyFormat("enum struct X E {} d;"); 3230 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3231 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3232 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3233 } 3234 3235 TEST_F(FormatTest, FormatsEnumClass) { 3236 verifyFormat("enum class {\n" 3237 " Zero,\n" 3238 " One = 1,\n" 3239 " Two = One + 1,\n" 3240 " Three = (One + Two),\n" 3241 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3242 " Five = (One, Two, Three, Four, 5)\n" 3243 "};"); 3244 verifyFormat("enum class Enum {};"); 3245 verifyFormat("enum class {};"); 3246 verifyFormat("enum class X E {} d;"); 3247 verifyFormat("enum class __attribute__((...)) E {} d;"); 3248 verifyFormat("enum class __declspec__((...)) E {} d;"); 3249 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3250 } 3251 3252 TEST_F(FormatTest, FormatsEnumTypes) { 3253 verifyFormat("enum X : int {\n" 3254 " A, // Force multiple lines.\n" 3255 " B\n" 3256 "};"); 3257 verifyFormat("enum X : int { A, B };"); 3258 verifyFormat("enum X : std::uint32_t { A, B };"); 3259 } 3260 3261 TEST_F(FormatTest, FormatsTypedefEnum) { 3262 FormatStyle Style = getLLVMStyle(); 3263 Style.ColumnLimit = 40; 3264 verifyFormat("typedef enum {} EmptyEnum;"); 3265 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3266 verifyFormat("typedef enum {\n" 3267 " ZERO = 0,\n" 3268 " ONE = 1,\n" 3269 " TWO = 2,\n" 3270 " THREE = 3\n" 3271 "} LongEnum;", 3272 Style); 3273 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3274 Style.BraceWrapping.AfterEnum = true; 3275 verifyFormat("typedef enum {} EmptyEnum;"); 3276 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3277 verifyFormat("typedef enum\n" 3278 "{\n" 3279 " ZERO = 0,\n" 3280 " ONE = 1,\n" 3281 " TWO = 2,\n" 3282 " THREE = 3\n" 3283 "} LongEnum;", 3284 Style); 3285 } 3286 3287 TEST_F(FormatTest, FormatsNSEnums) { 3288 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3289 verifyGoogleFormat( 3290 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3291 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3292 " // Information about someDecentlyLongValue.\n" 3293 " someDecentlyLongValue,\n" 3294 " // Information about anotherDecentlyLongValue.\n" 3295 " anotherDecentlyLongValue,\n" 3296 " // Information about aThirdDecentlyLongValue.\n" 3297 " aThirdDecentlyLongValue\n" 3298 "};"); 3299 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3300 " // Information about someDecentlyLongValue.\n" 3301 " someDecentlyLongValue,\n" 3302 " // Information about anotherDecentlyLongValue.\n" 3303 " anotherDecentlyLongValue,\n" 3304 " // Information about aThirdDecentlyLongValue.\n" 3305 " aThirdDecentlyLongValue\n" 3306 "};"); 3307 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3308 " a = 1,\n" 3309 " b = 2,\n" 3310 " c = 3,\n" 3311 "};"); 3312 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3313 " a = 1,\n" 3314 " b = 2,\n" 3315 " c = 3,\n" 3316 "};"); 3317 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3318 " a = 1,\n" 3319 " b = 2,\n" 3320 " c = 3,\n" 3321 "};"); 3322 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3323 " a = 1,\n" 3324 " b = 2,\n" 3325 " c = 3,\n" 3326 "};"); 3327 } 3328 3329 TEST_F(FormatTest, FormatsBitfields) { 3330 verifyFormat("struct Bitfields {\n" 3331 " unsigned sClass : 8;\n" 3332 " unsigned ValueKind : 2;\n" 3333 "};"); 3334 verifyFormat("struct A {\n" 3335 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3336 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3337 "};"); 3338 verifyFormat("struct MyStruct {\n" 3339 " uchar data;\n" 3340 " uchar : 8;\n" 3341 " uchar : 8;\n" 3342 " uchar other;\n" 3343 "};"); 3344 FormatStyle Style = getLLVMStyle(); 3345 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3346 verifyFormat("struct Bitfields {\n" 3347 " unsigned sClass:8;\n" 3348 " unsigned ValueKind:2;\n" 3349 " uchar other;\n" 3350 "};", 3351 Style); 3352 verifyFormat("struct A {\n" 3353 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3354 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3355 "};", 3356 Style); 3357 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3358 verifyFormat("struct Bitfields {\n" 3359 " unsigned sClass :8;\n" 3360 " unsigned ValueKind :2;\n" 3361 " uchar other;\n" 3362 "};", 3363 Style); 3364 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3365 verifyFormat("struct Bitfields {\n" 3366 " unsigned sClass: 8;\n" 3367 " unsigned ValueKind: 2;\n" 3368 " uchar other;\n" 3369 "};", 3370 Style); 3371 } 3372 3373 TEST_F(FormatTest, FormatsNamespaces) { 3374 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3375 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3376 3377 verifyFormat("namespace some_namespace {\n" 3378 "class A {};\n" 3379 "void f() { f(); }\n" 3380 "}", 3381 LLVMWithNoNamespaceFix); 3382 verifyFormat("namespace N::inline D {\n" 3383 "class A {};\n" 3384 "void f() { f(); }\n" 3385 "}", 3386 LLVMWithNoNamespaceFix); 3387 verifyFormat("namespace N::inline D::E {\n" 3388 "class A {};\n" 3389 "void f() { f(); }\n" 3390 "}", 3391 LLVMWithNoNamespaceFix); 3392 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3393 "class A {};\n" 3394 "void f() { f(); }\n" 3395 "}", 3396 LLVMWithNoNamespaceFix); 3397 verifyFormat("/* something */ namespace some_namespace {\n" 3398 "class A {};\n" 3399 "void f() { f(); }\n" 3400 "}", 3401 LLVMWithNoNamespaceFix); 3402 verifyFormat("namespace {\n" 3403 "class A {};\n" 3404 "void f() { f(); }\n" 3405 "}", 3406 LLVMWithNoNamespaceFix); 3407 verifyFormat("/* something */ namespace {\n" 3408 "class A {};\n" 3409 "void f() { f(); }\n" 3410 "}", 3411 LLVMWithNoNamespaceFix); 3412 verifyFormat("inline namespace X {\n" 3413 "class A {};\n" 3414 "void f() { f(); }\n" 3415 "}", 3416 LLVMWithNoNamespaceFix); 3417 verifyFormat("/* something */ inline namespace X {\n" 3418 "class A {};\n" 3419 "void f() { f(); }\n" 3420 "}", 3421 LLVMWithNoNamespaceFix); 3422 verifyFormat("export namespace X {\n" 3423 "class A {};\n" 3424 "void f() { f(); }\n" 3425 "}", 3426 LLVMWithNoNamespaceFix); 3427 verifyFormat("using namespace some_namespace;\n" 3428 "class A {};\n" 3429 "void f() { f(); }", 3430 LLVMWithNoNamespaceFix); 3431 3432 // This code is more common than we thought; if we 3433 // layout this correctly the semicolon will go into 3434 // its own line, which is undesirable. 3435 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3436 verifyFormat("namespace {\n" 3437 "class A {};\n" 3438 "};", 3439 LLVMWithNoNamespaceFix); 3440 3441 verifyFormat("namespace {\n" 3442 "int SomeVariable = 0; // comment\n" 3443 "} // namespace", 3444 LLVMWithNoNamespaceFix); 3445 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3446 "#define HEADER_GUARD\n" 3447 "namespace my_namespace {\n" 3448 "int i;\n" 3449 "} // my_namespace\n" 3450 "#endif // HEADER_GUARD", 3451 format("#ifndef HEADER_GUARD\n" 3452 " #define HEADER_GUARD\n" 3453 " namespace my_namespace {\n" 3454 "int i;\n" 3455 "} // my_namespace\n" 3456 "#endif // HEADER_GUARD", 3457 LLVMWithNoNamespaceFix)); 3458 3459 EXPECT_EQ("namespace A::B {\n" 3460 "class C {};\n" 3461 "}", 3462 format("namespace A::B {\n" 3463 "class C {};\n" 3464 "}", 3465 LLVMWithNoNamespaceFix)); 3466 3467 FormatStyle Style = getLLVMStyle(); 3468 Style.NamespaceIndentation = FormatStyle::NI_All; 3469 EXPECT_EQ("namespace out {\n" 3470 " int i;\n" 3471 " namespace in {\n" 3472 " int i;\n" 3473 " } // namespace in\n" 3474 "} // namespace out", 3475 format("namespace out {\n" 3476 "int i;\n" 3477 "namespace in {\n" 3478 "int i;\n" 3479 "} // namespace in\n" 3480 "} // namespace out", 3481 Style)); 3482 3483 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3484 EXPECT_EQ("namespace out {\n" 3485 "int i;\n" 3486 "namespace in {\n" 3487 " int i;\n" 3488 "} // namespace in\n" 3489 "} // namespace out", 3490 format("namespace out {\n" 3491 "int i;\n" 3492 "namespace in {\n" 3493 "int i;\n" 3494 "} // namespace in\n" 3495 "} // namespace out", 3496 Style)); 3497 } 3498 3499 TEST_F(FormatTest, NamespaceMacros) { 3500 FormatStyle Style = getLLVMStyle(); 3501 Style.NamespaceMacros.push_back("TESTSUITE"); 3502 3503 verifyFormat("TESTSUITE(A) {\n" 3504 "int foo();\n" 3505 "} // TESTSUITE(A)", 3506 Style); 3507 3508 verifyFormat("TESTSUITE(A, B) {\n" 3509 "int foo();\n" 3510 "} // TESTSUITE(A)", 3511 Style); 3512 3513 // Properly indent according to NamespaceIndentation style 3514 Style.NamespaceIndentation = FormatStyle::NI_All; 3515 verifyFormat("TESTSUITE(A) {\n" 3516 " int foo();\n" 3517 "} // TESTSUITE(A)", 3518 Style); 3519 verifyFormat("TESTSUITE(A) {\n" 3520 " namespace B {\n" 3521 " int foo();\n" 3522 " } // namespace B\n" 3523 "} // TESTSUITE(A)", 3524 Style); 3525 verifyFormat("namespace A {\n" 3526 " TESTSUITE(B) {\n" 3527 " int foo();\n" 3528 " } // TESTSUITE(B)\n" 3529 "} // namespace A", 3530 Style); 3531 3532 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3533 verifyFormat("TESTSUITE(A) {\n" 3534 "TESTSUITE(B) {\n" 3535 " int foo();\n" 3536 "} // TESTSUITE(B)\n" 3537 "} // TESTSUITE(A)", 3538 Style); 3539 verifyFormat("TESTSUITE(A) {\n" 3540 "namespace B {\n" 3541 " int foo();\n" 3542 "} // namespace B\n" 3543 "} // TESTSUITE(A)", 3544 Style); 3545 verifyFormat("namespace A {\n" 3546 "TESTSUITE(B) {\n" 3547 " int foo();\n" 3548 "} // TESTSUITE(B)\n" 3549 "} // namespace A", 3550 Style); 3551 3552 // Properly merge namespace-macros blocks in CompactNamespaces mode 3553 Style.NamespaceIndentation = FormatStyle::NI_None; 3554 Style.CompactNamespaces = true; 3555 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3556 "}} // TESTSUITE(A::B)", 3557 Style); 3558 3559 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3560 "}} // TESTSUITE(out::in)", 3561 format("TESTSUITE(out) {\n" 3562 "TESTSUITE(in) {\n" 3563 "} // TESTSUITE(in)\n" 3564 "} // TESTSUITE(out)", 3565 Style)); 3566 3567 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3568 "}} // TESTSUITE(out::in)", 3569 format("TESTSUITE(out) {\n" 3570 "TESTSUITE(in) {\n" 3571 "} // TESTSUITE(in)\n" 3572 "} // TESTSUITE(out)", 3573 Style)); 3574 3575 // Do not merge different namespaces/macros 3576 EXPECT_EQ("namespace out {\n" 3577 "TESTSUITE(in) {\n" 3578 "} // TESTSUITE(in)\n" 3579 "} // namespace out", 3580 format("namespace out {\n" 3581 "TESTSUITE(in) {\n" 3582 "} // TESTSUITE(in)\n" 3583 "} // namespace out", 3584 Style)); 3585 EXPECT_EQ("TESTSUITE(out) {\n" 3586 "namespace in {\n" 3587 "} // namespace in\n" 3588 "} // TESTSUITE(out)", 3589 format("TESTSUITE(out) {\n" 3590 "namespace in {\n" 3591 "} // namespace in\n" 3592 "} // TESTSUITE(out)", 3593 Style)); 3594 Style.NamespaceMacros.push_back("FOOBAR"); 3595 EXPECT_EQ("TESTSUITE(out) {\n" 3596 "FOOBAR(in) {\n" 3597 "} // FOOBAR(in)\n" 3598 "} // TESTSUITE(out)", 3599 format("TESTSUITE(out) {\n" 3600 "FOOBAR(in) {\n" 3601 "} // FOOBAR(in)\n" 3602 "} // TESTSUITE(out)", 3603 Style)); 3604 } 3605 3606 TEST_F(FormatTest, FormatsCompactNamespaces) { 3607 FormatStyle Style = getLLVMStyle(); 3608 Style.CompactNamespaces = true; 3609 Style.NamespaceMacros.push_back("TESTSUITE"); 3610 3611 verifyFormat("namespace A { namespace B {\n" 3612 "}} // namespace A::B", 3613 Style); 3614 3615 EXPECT_EQ("namespace out { namespace in {\n" 3616 "}} // namespace out::in", 3617 format("namespace out {\n" 3618 "namespace in {\n" 3619 "} // namespace in\n" 3620 "} // namespace out", 3621 Style)); 3622 3623 // Only namespaces which have both consecutive opening and end get compacted 3624 EXPECT_EQ("namespace out {\n" 3625 "namespace in1 {\n" 3626 "} // namespace in1\n" 3627 "namespace in2 {\n" 3628 "} // namespace in2\n" 3629 "} // namespace out", 3630 format("namespace out {\n" 3631 "namespace in1 {\n" 3632 "} // namespace in1\n" 3633 "namespace in2 {\n" 3634 "} // namespace in2\n" 3635 "} // namespace out", 3636 Style)); 3637 3638 EXPECT_EQ("namespace out {\n" 3639 "int i;\n" 3640 "namespace in {\n" 3641 "int j;\n" 3642 "} // namespace in\n" 3643 "int k;\n" 3644 "} // namespace out", 3645 format("namespace out { int i;\n" 3646 "namespace in { int j; } // namespace in\n" 3647 "int k; } // namespace out", 3648 Style)); 3649 3650 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 3651 "}}} // namespace A::B::C\n", 3652 format("namespace A { namespace B {\n" 3653 "namespace C {\n" 3654 "}} // namespace B::C\n" 3655 "} // namespace A\n", 3656 Style)); 3657 3658 Style.ColumnLimit = 40; 3659 EXPECT_EQ("namespace aaaaaaaaaa {\n" 3660 "namespace bbbbbbbbbb {\n" 3661 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 3662 format("namespace aaaaaaaaaa {\n" 3663 "namespace bbbbbbbbbb {\n" 3664 "} // namespace bbbbbbbbbb\n" 3665 "} // namespace aaaaaaaaaa", 3666 Style)); 3667 3668 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 3669 "namespace cccccc {\n" 3670 "}}} // namespace aaaaaa::bbbbbb::cccccc", 3671 format("namespace aaaaaa {\n" 3672 "namespace bbbbbb {\n" 3673 "namespace cccccc {\n" 3674 "} // namespace cccccc\n" 3675 "} // namespace bbbbbb\n" 3676 "} // namespace aaaaaa", 3677 Style)); 3678 Style.ColumnLimit = 80; 3679 3680 // Extra semicolon after 'inner' closing brace prevents merging 3681 EXPECT_EQ("namespace out { namespace in {\n" 3682 "}; } // namespace out::in", 3683 format("namespace out {\n" 3684 "namespace in {\n" 3685 "}; // namespace in\n" 3686 "} // namespace out", 3687 Style)); 3688 3689 // Extra semicolon after 'outer' closing brace is conserved 3690 EXPECT_EQ("namespace out { namespace in {\n" 3691 "}}; // namespace out::in", 3692 format("namespace out {\n" 3693 "namespace in {\n" 3694 "} // namespace in\n" 3695 "}; // namespace out", 3696 Style)); 3697 3698 Style.NamespaceIndentation = FormatStyle::NI_All; 3699 EXPECT_EQ("namespace out { namespace in {\n" 3700 " int i;\n" 3701 "}} // namespace out::in", 3702 format("namespace out {\n" 3703 "namespace in {\n" 3704 "int i;\n" 3705 "} // namespace in\n" 3706 "} // namespace out", 3707 Style)); 3708 EXPECT_EQ("namespace out { namespace mid {\n" 3709 " namespace in {\n" 3710 " int j;\n" 3711 " } // namespace in\n" 3712 " int k;\n" 3713 "}} // namespace out::mid", 3714 format("namespace out { namespace mid {\n" 3715 "namespace in { int j; } // namespace in\n" 3716 "int k; }} // namespace out::mid", 3717 Style)); 3718 3719 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3720 EXPECT_EQ("namespace out { namespace in {\n" 3721 " int i;\n" 3722 "}} // namespace out::in", 3723 format("namespace out {\n" 3724 "namespace in {\n" 3725 "int i;\n" 3726 "} // namespace in\n" 3727 "} // namespace out", 3728 Style)); 3729 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 3730 " int i;\n" 3731 "}}} // namespace out::mid::in", 3732 format("namespace out {\n" 3733 "namespace mid {\n" 3734 "namespace in {\n" 3735 "int i;\n" 3736 "} // namespace in\n" 3737 "} // namespace mid\n" 3738 "} // namespace out", 3739 Style)); 3740 } 3741 3742 TEST_F(FormatTest, FormatsExternC) { 3743 verifyFormat("extern \"C\" {\nint a;"); 3744 verifyFormat("extern \"C\" {}"); 3745 verifyFormat("extern \"C\" {\n" 3746 "int foo();\n" 3747 "}"); 3748 verifyFormat("extern \"C\" int foo() {}"); 3749 verifyFormat("extern \"C\" int foo();"); 3750 verifyFormat("extern \"C\" int foo() {\n" 3751 " int i = 42;\n" 3752 " return i;\n" 3753 "}"); 3754 3755 FormatStyle Style = getLLVMStyle(); 3756 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3757 Style.BraceWrapping.AfterFunction = true; 3758 verifyFormat("extern \"C\" int foo() {}", Style); 3759 verifyFormat("extern \"C\" int foo();", Style); 3760 verifyFormat("extern \"C\" int foo()\n" 3761 "{\n" 3762 " int i = 42;\n" 3763 " return i;\n" 3764 "}", 3765 Style); 3766 3767 Style.BraceWrapping.AfterExternBlock = true; 3768 Style.BraceWrapping.SplitEmptyRecord = false; 3769 verifyFormat("extern \"C\"\n" 3770 "{}", 3771 Style); 3772 verifyFormat("extern \"C\"\n" 3773 "{\n" 3774 " int foo();\n" 3775 "}", 3776 Style); 3777 } 3778 3779 TEST_F(FormatTest, IndentExternBlockStyle) { 3780 FormatStyle Style = getLLVMStyle(); 3781 Style.IndentWidth = 2; 3782 3783 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3784 verifyFormat("extern \"C\" { /*9*/\n}", Style); 3785 verifyFormat("extern \"C\" {\n" 3786 " int foo10();\n" 3787 "}", 3788 Style); 3789 3790 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 3791 verifyFormat("extern \"C\" { /*11*/\n}", Style); 3792 verifyFormat("extern \"C\" {\n" 3793 "int foo12();\n" 3794 "}", 3795 Style); 3796 3797 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3798 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3799 Style.BraceWrapping.AfterExternBlock = true; 3800 verifyFormat("extern \"C\"\n{ /*13*/\n}", Style); 3801 verifyFormat("extern \"C\"\n{\n" 3802 " int foo14();\n" 3803 "}", 3804 Style); 3805 3806 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3807 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3808 Style.BraceWrapping.AfterExternBlock = false; 3809 verifyFormat("extern \"C\" { /*15*/\n}", Style); 3810 verifyFormat("extern \"C\" {\n" 3811 "int foo16();\n" 3812 "}", 3813 Style); 3814 } 3815 3816 TEST_F(FormatTest, FormatsInlineASM) { 3817 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 3818 verifyFormat("asm(\"nop\" ::: \"memory\");"); 3819 verifyFormat( 3820 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 3821 " \"cpuid\\n\\t\"\n" 3822 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 3823 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 3824 " : \"a\"(value));"); 3825 EXPECT_EQ( 3826 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3827 " __asm {\n" 3828 " mov edx,[that] // vtable in edx\n" 3829 " mov eax,methodIndex\n" 3830 " call [edx][eax*4] // stdcall\n" 3831 " }\n" 3832 "}", 3833 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3834 " __asm {\n" 3835 " mov edx,[that] // vtable in edx\n" 3836 " mov eax,methodIndex\n" 3837 " call [edx][eax*4] // stdcall\n" 3838 " }\n" 3839 "}")); 3840 EXPECT_EQ("_asm {\n" 3841 " xor eax, eax;\n" 3842 " cpuid;\n" 3843 "}", 3844 format("_asm {\n" 3845 " xor eax, eax;\n" 3846 " cpuid;\n" 3847 "}")); 3848 verifyFormat("void function() {\n" 3849 " // comment\n" 3850 " asm(\"\");\n" 3851 "}"); 3852 EXPECT_EQ("__asm {\n" 3853 "}\n" 3854 "int i;", 3855 format("__asm {\n" 3856 "}\n" 3857 "int i;")); 3858 } 3859 3860 TEST_F(FormatTest, FormatTryCatch) { 3861 verifyFormat("try {\n" 3862 " throw a * b;\n" 3863 "} catch (int a) {\n" 3864 " // Do nothing.\n" 3865 "} catch (...) {\n" 3866 " exit(42);\n" 3867 "}"); 3868 3869 // Function-level try statements. 3870 verifyFormat("int f() try { return 4; } catch (...) {\n" 3871 " return 5;\n" 3872 "}"); 3873 verifyFormat("class A {\n" 3874 " int a;\n" 3875 " A() try : a(0) {\n" 3876 " } catch (...) {\n" 3877 " throw;\n" 3878 " }\n" 3879 "};\n"); 3880 verifyFormat("class A {\n" 3881 " int a;\n" 3882 " A() try : a(0), b{1} {\n" 3883 " } catch (...) {\n" 3884 " throw;\n" 3885 " }\n" 3886 "};\n"); 3887 verifyFormat("class A {\n" 3888 " int a;\n" 3889 " A() try : a(0), b{1}, c{2} {\n" 3890 " } catch (...) {\n" 3891 " throw;\n" 3892 " }\n" 3893 "};\n"); 3894 verifyFormat("class A {\n" 3895 " int a;\n" 3896 " A() try : a(0), b{1}, c{2} {\n" 3897 " { // New scope.\n" 3898 " }\n" 3899 " } catch (...) {\n" 3900 " throw;\n" 3901 " }\n" 3902 "};\n"); 3903 3904 // Incomplete try-catch blocks. 3905 verifyIncompleteFormat("try {} catch ("); 3906 } 3907 3908 TEST_F(FormatTest, FormatTryAsAVariable) { 3909 verifyFormat("int try;"); 3910 verifyFormat("int try, size;"); 3911 verifyFormat("try = foo();"); 3912 verifyFormat("if (try < size) {\n return true;\n}"); 3913 3914 verifyFormat("int catch;"); 3915 verifyFormat("int catch, size;"); 3916 verifyFormat("catch = foo();"); 3917 verifyFormat("if (catch < size) {\n return true;\n}"); 3918 3919 FormatStyle Style = getLLVMStyle(); 3920 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3921 Style.BraceWrapping.AfterFunction = true; 3922 Style.BraceWrapping.BeforeCatch = true; 3923 verifyFormat("try {\n" 3924 " int bar = 1;\n" 3925 "}\n" 3926 "catch (...) {\n" 3927 " int bar = 1;\n" 3928 "}", 3929 Style); 3930 verifyFormat("#if NO_EX\n" 3931 "try\n" 3932 "#endif\n" 3933 "{\n" 3934 "}\n" 3935 "#if NO_EX\n" 3936 "catch (...) {\n" 3937 "}", 3938 Style); 3939 verifyFormat("try /* abc */ {\n" 3940 " int bar = 1;\n" 3941 "}\n" 3942 "catch (...) {\n" 3943 " int bar = 1;\n" 3944 "}", 3945 Style); 3946 verifyFormat("try\n" 3947 "// abc\n" 3948 "{\n" 3949 " int bar = 1;\n" 3950 "}\n" 3951 "catch (...) {\n" 3952 " int bar = 1;\n" 3953 "}", 3954 Style); 3955 } 3956 3957 TEST_F(FormatTest, FormatSEHTryCatch) { 3958 verifyFormat("__try {\n" 3959 " int a = b * c;\n" 3960 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 3961 " // Do nothing.\n" 3962 "}"); 3963 3964 verifyFormat("__try {\n" 3965 " int a = b * c;\n" 3966 "} __finally {\n" 3967 " // Do nothing.\n" 3968 "}"); 3969 3970 verifyFormat("DEBUG({\n" 3971 " __try {\n" 3972 " } __finally {\n" 3973 " }\n" 3974 "});\n"); 3975 } 3976 3977 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 3978 verifyFormat("try {\n" 3979 " f();\n" 3980 "} catch {\n" 3981 " g();\n" 3982 "}"); 3983 verifyFormat("try {\n" 3984 " f();\n" 3985 "} catch (A a) MACRO(x) {\n" 3986 " g();\n" 3987 "} catch (B b) MACRO(x) {\n" 3988 " g();\n" 3989 "}"); 3990 } 3991 3992 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 3993 FormatStyle Style = getLLVMStyle(); 3994 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 3995 FormatStyle::BS_WebKit}) { 3996 Style.BreakBeforeBraces = BraceStyle; 3997 verifyFormat("try {\n" 3998 " // something\n" 3999 "} catch (...) {\n" 4000 " // something\n" 4001 "}", 4002 Style); 4003 } 4004 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4005 verifyFormat("try {\n" 4006 " // something\n" 4007 "}\n" 4008 "catch (...) {\n" 4009 " // something\n" 4010 "}", 4011 Style); 4012 verifyFormat("__try {\n" 4013 " // something\n" 4014 "}\n" 4015 "__finally {\n" 4016 " // something\n" 4017 "}", 4018 Style); 4019 verifyFormat("@try {\n" 4020 " // something\n" 4021 "}\n" 4022 "@finally {\n" 4023 " // something\n" 4024 "}", 4025 Style); 4026 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4027 verifyFormat("try\n" 4028 "{\n" 4029 " // something\n" 4030 "}\n" 4031 "catch (...)\n" 4032 "{\n" 4033 " // something\n" 4034 "}", 4035 Style); 4036 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4037 verifyFormat("try\n" 4038 " {\n" 4039 " // something white\n" 4040 " }\n" 4041 "catch (...)\n" 4042 " {\n" 4043 " // something white\n" 4044 " }", 4045 Style); 4046 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4047 verifyFormat("try\n" 4048 " {\n" 4049 " // something\n" 4050 " }\n" 4051 "catch (...)\n" 4052 " {\n" 4053 " // something\n" 4054 " }", 4055 Style); 4056 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4057 Style.BraceWrapping.BeforeCatch = true; 4058 verifyFormat("try {\n" 4059 " // something\n" 4060 "}\n" 4061 "catch (...) {\n" 4062 " // something\n" 4063 "}", 4064 Style); 4065 } 4066 4067 TEST_F(FormatTest, StaticInitializers) { 4068 verifyFormat("static SomeClass SC = {1, 'a'};"); 4069 4070 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4071 " 100000000, " 4072 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4073 4074 // Here, everything other than the "}" would fit on a line. 4075 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4076 " 10000000000000000000000000};"); 4077 EXPECT_EQ("S s = {a,\n" 4078 "\n" 4079 " b};", 4080 format("S s = {\n" 4081 " a,\n" 4082 "\n" 4083 " b\n" 4084 "};")); 4085 4086 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4087 // line. However, the formatting looks a bit off and this probably doesn't 4088 // happen often in practice. 4089 verifyFormat("static int Variable[1] = {\n" 4090 " {1000000000000000000000000000000000000}};", 4091 getLLVMStyleWithColumns(40)); 4092 } 4093 4094 TEST_F(FormatTest, DesignatedInitializers) { 4095 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4096 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4097 " .bbbbbbbbbb = 2,\n" 4098 " .cccccccccc = 3,\n" 4099 " .dddddddddd = 4,\n" 4100 " .eeeeeeeeee = 5};"); 4101 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4102 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4103 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4104 " .ccccccccccccccccccccccccccc = 3,\n" 4105 " .ddddddddddddddddddddddddddd = 4,\n" 4106 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4107 4108 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4109 4110 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4111 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4112 " [2] = bbbbbbbbbb,\n" 4113 " [3] = cccccccccc,\n" 4114 " [4] = dddddddddd,\n" 4115 " [5] = eeeeeeeeee};"); 4116 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4117 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4118 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4119 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4120 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4121 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4122 } 4123 4124 TEST_F(FormatTest, NestedStaticInitializers) { 4125 verifyFormat("static A x = {{{}}};\n"); 4126 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4127 " {init1, init2, init3, init4}}};", 4128 getLLVMStyleWithColumns(50)); 4129 4130 verifyFormat("somes Status::global_reps[3] = {\n" 4131 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4132 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4133 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4134 getLLVMStyleWithColumns(60)); 4135 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4136 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4137 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4138 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4139 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4140 " {rect.fRight - rect.fLeft, rect.fBottom - " 4141 "rect.fTop}};"); 4142 4143 verifyFormat( 4144 "SomeArrayOfSomeType a = {\n" 4145 " {{1, 2, 3},\n" 4146 " {1, 2, 3},\n" 4147 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4148 " 333333333333333333333333333333},\n" 4149 " {1, 2, 3},\n" 4150 " {1, 2, 3}}};"); 4151 verifyFormat( 4152 "SomeArrayOfSomeType a = {\n" 4153 " {{1, 2, 3}},\n" 4154 " {{1, 2, 3}},\n" 4155 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4156 " 333333333333333333333333333333}},\n" 4157 " {{1, 2, 3}},\n" 4158 " {{1, 2, 3}}};"); 4159 4160 verifyFormat("struct {\n" 4161 " unsigned bit;\n" 4162 " const char *const name;\n" 4163 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4164 " {kOsWin, \"Windows\"},\n" 4165 " {kOsLinux, \"Linux\"},\n" 4166 " {kOsCrOS, \"Chrome OS\"}};"); 4167 verifyFormat("struct {\n" 4168 " unsigned bit;\n" 4169 " const char *const name;\n" 4170 "} kBitsToOs[] = {\n" 4171 " {kOsMac, \"Mac\"},\n" 4172 " {kOsWin, \"Windows\"},\n" 4173 " {kOsLinux, \"Linux\"},\n" 4174 " {kOsCrOS, \"Chrome OS\"},\n" 4175 "};"); 4176 } 4177 4178 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4179 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4180 " \\\n" 4181 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4182 } 4183 4184 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4185 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4186 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4187 4188 // Do break defaulted and deleted functions. 4189 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4190 " default;", 4191 getLLVMStyleWithColumns(40)); 4192 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4193 " delete;", 4194 getLLVMStyleWithColumns(40)); 4195 } 4196 4197 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4198 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4199 getLLVMStyleWithColumns(40)); 4200 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4201 getLLVMStyleWithColumns(40)); 4202 EXPECT_EQ("#define Q \\\n" 4203 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4204 " \"aaaaaaaa.cpp\"", 4205 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4206 getLLVMStyleWithColumns(40))); 4207 } 4208 4209 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4210 EXPECT_EQ("# 123 \"A string literal\"", 4211 format(" # 123 \"A string literal\"")); 4212 } 4213 4214 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4215 EXPECT_EQ("#;", format("#;")); 4216 verifyFormat("#\n;\n;\n;"); 4217 } 4218 4219 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4220 EXPECT_EQ("#line 42 \"test\"\n", 4221 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4222 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4223 getLLVMStyleWithColumns(12))); 4224 } 4225 4226 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4227 EXPECT_EQ("#line 42 \"test\"", 4228 format("# \\\n line \\\n 42 \\\n \"test\"")); 4229 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4230 } 4231 4232 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4233 verifyFormat("#define A \\x20"); 4234 verifyFormat("#define A \\ x20"); 4235 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4236 verifyFormat("#define A ''"); 4237 verifyFormat("#define A ''qqq"); 4238 verifyFormat("#define A `qqq"); 4239 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4240 EXPECT_EQ("const char *c = STRINGIFY(\n" 4241 "\\na : b);", 4242 format("const char * c = STRINGIFY(\n" 4243 "\\na : b);")); 4244 4245 verifyFormat("a\r\\"); 4246 verifyFormat("a\v\\"); 4247 verifyFormat("a\f\\"); 4248 } 4249 4250 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4251 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4252 style.IndentWidth = 4; 4253 style.PPIndentWidth = 1; 4254 4255 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4256 verifyFormat("#ifdef __linux__\n" 4257 "void foo() {\n" 4258 " int x = 0;\n" 4259 "}\n" 4260 "#define FOO\n" 4261 "#endif\n" 4262 "void bar() {\n" 4263 " int y = 0;\n" 4264 "}\n", 4265 style); 4266 4267 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4268 verifyFormat("#ifdef __linux__\n" 4269 "void foo() {\n" 4270 " int x = 0;\n" 4271 "}\n" 4272 "# define FOO foo\n" 4273 "#endif\n" 4274 "void bar() {\n" 4275 " int y = 0;\n" 4276 "}\n", 4277 style); 4278 4279 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4280 verifyFormat("#ifdef __linux__\n" 4281 "void foo() {\n" 4282 " int x = 0;\n" 4283 "}\n" 4284 " #define FOO foo\n" 4285 "#endif\n" 4286 "void bar() {\n" 4287 " int y = 0;\n" 4288 "}\n", 4289 style); 4290 } 4291 4292 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4293 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4294 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4295 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4296 // FIXME: We never break before the macro name. 4297 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4298 4299 verifyFormat("#define A A\n#define A A"); 4300 verifyFormat("#define A(X) A\n#define A A"); 4301 4302 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4303 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4304 } 4305 4306 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4307 EXPECT_EQ("// somecomment\n" 4308 "#include \"a.h\"\n" 4309 "#define A( \\\n" 4310 " A, B)\n" 4311 "#include \"b.h\"\n" 4312 "// somecomment\n", 4313 format(" // somecomment\n" 4314 " #include \"a.h\"\n" 4315 "#define A(A,\\\n" 4316 " B)\n" 4317 " #include \"b.h\"\n" 4318 " // somecomment\n", 4319 getLLVMStyleWithColumns(13))); 4320 } 4321 4322 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4323 4324 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4325 EXPECT_EQ("#define A \\\n" 4326 " c; \\\n" 4327 " e;\n" 4328 "f;", 4329 format("#define A c; e;\n" 4330 "f;", 4331 getLLVMStyleWithColumns(14))); 4332 } 4333 4334 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4335 4336 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4337 EXPECT_EQ("int x,\n" 4338 "#define A\n" 4339 " y;", 4340 format("int x,\n#define A\ny;")); 4341 } 4342 4343 TEST_F(FormatTest, HashInMacroDefinition) { 4344 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4345 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4346 verifyFormat("#define A \\\n" 4347 " { \\\n" 4348 " f(#c); \\\n" 4349 " }", 4350 getLLVMStyleWithColumns(11)); 4351 4352 verifyFormat("#define A(X) \\\n" 4353 " void function##X()", 4354 getLLVMStyleWithColumns(22)); 4355 4356 verifyFormat("#define A(a, b, c) \\\n" 4357 " void a##b##c()", 4358 getLLVMStyleWithColumns(22)); 4359 4360 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4361 } 4362 4363 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4364 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4365 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4366 4367 FormatStyle Style = getLLVMStyle(); 4368 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4369 verifyFormat("#define true ((foo)1)", Style); 4370 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4371 verifyFormat("#define false((foo)0)", Style); 4372 } 4373 4374 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4375 EXPECT_EQ("#define A b;", format("#define A \\\n" 4376 " \\\n" 4377 " b;", 4378 getLLVMStyleWithColumns(25))); 4379 EXPECT_EQ("#define A \\\n" 4380 " \\\n" 4381 " a; \\\n" 4382 " b;", 4383 format("#define A \\\n" 4384 " \\\n" 4385 " a; \\\n" 4386 " b;", 4387 getLLVMStyleWithColumns(11))); 4388 EXPECT_EQ("#define A \\\n" 4389 " a; \\\n" 4390 " \\\n" 4391 " b;", 4392 format("#define A \\\n" 4393 " a; \\\n" 4394 " \\\n" 4395 " b;", 4396 getLLVMStyleWithColumns(11))); 4397 } 4398 4399 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4400 verifyIncompleteFormat("#define A :"); 4401 verifyFormat("#define SOMECASES \\\n" 4402 " case 1: \\\n" 4403 " case 2\n", 4404 getLLVMStyleWithColumns(20)); 4405 verifyFormat("#define MACRO(a) \\\n" 4406 " if (a) \\\n" 4407 " f(); \\\n" 4408 " else \\\n" 4409 " g()", 4410 getLLVMStyleWithColumns(18)); 4411 verifyFormat("#define A template <typename T>"); 4412 verifyIncompleteFormat("#define STR(x) #x\n" 4413 "f(STR(this_is_a_string_literal{));"); 4414 verifyFormat("#pragma omp threadprivate( \\\n" 4415 " y)), // expected-warning", 4416 getLLVMStyleWithColumns(28)); 4417 verifyFormat("#d, = };"); 4418 verifyFormat("#if \"a"); 4419 verifyIncompleteFormat("({\n" 4420 "#define b \\\n" 4421 " } \\\n" 4422 " a\n" 4423 "a", 4424 getLLVMStyleWithColumns(15)); 4425 verifyFormat("#define A \\\n" 4426 " { \\\n" 4427 " {\n" 4428 "#define B \\\n" 4429 " } \\\n" 4430 " }", 4431 getLLVMStyleWithColumns(15)); 4432 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4433 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4434 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4435 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4436 } 4437 4438 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4439 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4440 EXPECT_EQ("class A : public QObject {\n" 4441 " Q_OBJECT\n" 4442 "\n" 4443 " A() {}\n" 4444 "};", 4445 format("class A : public QObject {\n" 4446 " Q_OBJECT\n" 4447 "\n" 4448 " A() {\n}\n" 4449 "} ;")); 4450 EXPECT_EQ("MACRO\n" 4451 "/*static*/ int i;", 4452 format("MACRO\n" 4453 " /*static*/ int i;")); 4454 EXPECT_EQ("SOME_MACRO\n" 4455 "namespace {\n" 4456 "void f();\n" 4457 "} // namespace", 4458 format("SOME_MACRO\n" 4459 " namespace {\n" 4460 "void f( );\n" 4461 "} // namespace")); 4462 // Only if the identifier contains at least 5 characters. 4463 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4464 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4465 // Only if everything is upper case. 4466 EXPECT_EQ("class A : public QObject {\n" 4467 " Q_Object A() {}\n" 4468 "};", 4469 format("class A : public QObject {\n" 4470 " Q_Object\n" 4471 " A() {\n}\n" 4472 "} ;")); 4473 4474 // Only if the next line can actually start an unwrapped line. 4475 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4476 format("SOME_WEIRD_LOG_MACRO\n" 4477 "<< SomeThing;")); 4478 4479 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4480 "(n, buffers))\n", 4481 getChromiumStyle(FormatStyle::LK_Cpp)); 4482 4483 // See PR41483 4484 EXPECT_EQ("/**/ FOO(a)\n" 4485 "FOO(b)", 4486 format("/**/ FOO(a)\n" 4487 "FOO(b)")); 4488 } 4489 4490 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4491 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4492 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4493 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4494 "class X {};\n" 4495 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4496 "int *createScopDetectionPass() { return 0; }", 4497 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4498 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4499 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4500 " class X {};\n" 4501 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4502 " int *createScopDetectionPass() { return 0; }")); 4503 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4504 // braces, so that inner block is indented one level more. 4505 EXPECT_EQ("int q() {\n" 4506 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4507 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4508 " IPC_END_MESSAGE_MAP()\n" 4509 "}", 4510 format("int q() {\n" 4511 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4512 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4513 " IPC_END_MESSAGE_MAP()\n" 4514 "}")); 4515 4516 // Same inside macros. 4517 EXPECT_EQ("#define LIST(L) \\\n" 4518 " L(A) \\\n" 4519 " L(B) \\\n" 4520 " L(C)", 4521 format("#define LIST(L) \\\n" 4522 " L(A) \\\n" 4523 " L(B) \\\n" 4524 " L(C)", 4525 getGoogleStyle())); 4526 4527 // These must not be recognized as macros. 4528 EXPECT_EQ("int q() {\n" 4529 " f(x);\n" 4530 " f(x) {}\n" 4531 " f(x)->g();\n" 4532 " f(x)->*g();\n" 4533 " f(x).g();\n" 4534 " f(x) = x;\n" 4535 " f(x) += x;\n" 4536 " f(x) -= x;\n" 4537 " f(x) *= x;\n" 4538 " f(x) /= x;\n" 4539 " f(x) %= x;\n" 4540 " f(x) &= x;\n" 4541 " f(x) |= x;\n" 4542 " f(x) ^= x;\n" 4543 " f(x) >>= x;\n" 4544 " f(x) <<= x;\n" 4545 " f(x)[y].z();\n" 4546 " LOG(INFO) << x;\n" 4547 " ifstream(x) >> x;\n" 4548 "}\n", 4549 format("int q() {\n" 4550 " f(x)\n;\n" 4551 " f(x)\n {}\n" 4552 " f(x)\n->g();\n" 4553 " f(x)\n->*g();\n" 4554 " f(x)\n.g();\n" 4555 " f(x)\n = x;\n" 4556 " f(x)\n += x;\n" 4557 " f(x)\n -= x;\n" 4558 " f(x)\n *= x;\n" 4559 " f(x)\n /= x;\n" 4560 " f(x)\n %= x;\n" 4561 " f(x)\n &= x;\n" 4562 " f(x)\n |= x;\n" 4563 " f(x)\n ^= x;\n" 4564 " f(x)\n >>= x;\n" 4565 " f(x)\n <<= x;\n" 4566 " f(x)\n[y].z();\n" 4567 " LOG(INFO)\n << x;\n" 4568 " ifstream(x)\n >> x;\n" 4569 "}\n")); 4570 EXPECT_EQ("int q() {\n" 4571 " F(x)\n" 4572 " if (1) {\n" 4573 " }\n" 4574 " F(x)\n" 4575 " while (1) {\n" 4576 " }\n" 4577 " F(x)\n" 4578 " G(x);\n" 4579 " F(x)\n" 4580 " try {\n" 4581 " Q();\n" 4582 " } catch (...) {\n" 4583 " }\n" 4584 "}\n", 4585 format("int q() {\n" 4586 "F(x)\n" 4587 "if (1) {}\n" 4588 "F(x)\n" 4589 "while (1) {}\n" 4590 "F(x)\n" 4591 "G(x);\n" 4592 "F(x)\n" 4593 "try { Q(); } catch (...) {}\n" 4594 "}\n")); 4595 EXPECT_EQ("class A {\n" 4596 " A() : t(0) {}\n" 4597 " A(int i) noexcept() : {}\n" 4598 " A(X x)\n" // FIXME: function-level try blocks are broken. 4599 " try : t(0) {\n" 4600 " } catch (...) {\n" 4601 " }\n" 4602 "};", 4603 format("class A {\n" 4604 " A()\n : t(0) {}\n" 4605 " A(int i)\n noexcept() : {}\n" 4606 " A(X x)\n" 4607 " try : t(0) {} catch (...) {}\n" 4608 "};")); 4609 FormatStyle Style = getLLVMStyle(); 4610 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4611 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 4612 Style.BraceWrapping.AfterFunction = true; 4613 EXPECT_EQ("void f()\n" 4614 "try\n" 4615 "{\n" 4616 "}", 4617 format("void f() try {\n" 4618 "}", 4619 Style)); 4620 EXPECT_EQ("class SomeClass {\n" 4621 "public:\n" 4622 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4623 "};", 4624 format("class SomeClass {\n" 4625 "public:\n" 4626 " SomeClass()\n" 4627 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4628 "};")); 4629 EXPECT_EQ("class SomeClass {\n" 4630 "public:\n" 4631 " SomeClass()\n" 4632 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4633 "};", 4634 format("class SomeClass {\n" 4635 "public:\n" 4636 " SomeClass()\n" 4637 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4638 "};", 4639 getLLVMStyleWithColumns(40))); 4640 4641 verifyFormat("MACRO(>)"); 4642 4643 // Some macros contain an implicit semicolon. 4644 Style = getLLVMStyle(); 4645 Style.StatementMacros.push_back("FOO"); 4646 verifyFormat("FOO(a) int b = 0;"); 4647 verifyFormat("FOO(a)\n" 4648 "int b = 0;", 4649 Style); 4650 verifyFormat("FOO(a);\n" 4651 "int b = 0;", 4652 Style); 4653 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 4654 "int b = 0;", 4655 Style); 4656 verifyFormat("FOO()\n" 4657 "int b = 0;", 4658 Style); 4659 verifyFormat("FOO\n" 4660 "int b = 0;", 4661 Style); 4662 verifyFormat("void f() {\n" 4663 " FOO(a)\n" 4664 " return a;\n" 4665 "}", 4666 Style); 4667 verifyFormat("FOO(a)\n" 4668 "FOO(b)", 4669 Style); 4670 verifyFormat("int a = 0;\n" 4671 "FOO(b)\n" 4672 "int c = 0;", 4673 Style); 4674 verifyFormat("int a = 0;\n" 4675 "int x = FOO(a)\n" 4676 "int b = 0;", 4677 Style); 4678 verifyFormat("void foo(int a) { FOO(a) }\n" 4679 "uint32_t bar() {}", 4680 Style); 4681 } 4682 4683 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 4684 verifyFormat("#define A \\\n" 4685 " f({ \\\n" 4686 " g(); \\\n" 4687 " });", 4688 getLLVMStyleWithColumns(11)); 4689 } 4690 4691 TEST_F(FormatTest, IndentPreprocessorDirectives) { 4692 FormatStyle Style = getLLVMStyle(); 4693 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 4694 Style.ColumnLimit = 40; 4695 verifyFormat("#ifdef _WIN32\n" 4696 "#define A 0\n" 4697 "#ifdef VAR2\n" 4698 "#define B 1\n" 4699 "#include <someheader.h>\n" 4700 "#define MACRO \\\n" 4701 " some_very_long_func_aaaaaaaaaa();\n" 4702 "#endif\n" 4703 "#else\n" 4704 "#define A 1\n" 4705 "#endif", 4706 Style); 4707 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4708 verifyFormat("#ifdef _WIN32\n" 4709 "# define A 0\n" 4710 "# ifdef VAR2\n" 4711 "# define B 1\n" 4712 "# include <someheader.h>\n" 4713 "# define MACRO \\\n" 4714 " some_very_long_func_aaaaaaaaaa();\n" 4715 "# endif\n" 4716 "#else\n" 4717 "# define A 1\n" 4718 "#endif", 4719 Style); 4720 verifyFormat("#if A\n" 4721 "# define MACRO \\\n" 4722 " void a(int x) { \\\n" 4723 " b(); \\\n" 4724 " c(); \\\n" 4725 " d(); \\\n" 4726 " e(); \\\n" 4727 " f(); \\\n" 4728 " }\n" 4729 "#endif", 4730 Style); 4731 // Comments before include guard. 4732 verifyFormat("// file comment\n" 4733 "// file comment\n" 4734 "#ifndef HEADER_H\n" 4735 "#define HEADER_H\n" 4736 "code();\n" 4737 "#endif", 4738 Style); 4739 // Test with include guards. 4740 verifyFormat("#ifndef HEADER_H\n" 4741 "#define HEADER_H\n" 4742 "code();\n" 4743 "#endif", 4744 Style); 4745 // Include guards must have a #define with the same variable immediately 4746 // after #ifndef. 4747 verifyFormat("#ifndef NOT_GUARD\n" 4748 "# define FOO\n" 4749 "code();\n" 4750 "#endif", 4751 Style); 4752 4753 // Include guards must cover the entire file. 4754 verifyFormat("code();\n" 4755 "code();\n" 4756 "#ifndef NOT_GUARD\n" 4757 "# define NOT_GUARD\n" 4758 "code();\n" 4759 "#endif", 4760 Style); 4761 verifyFormat("#ifndef NOT_GUARD\n" 4762 "# define NOT_GUARD\n" 4763 "code();\n" 4764 "#endif\n" 4765 "code();", 4766 Style); 4767 // Test with trailing blank lines. 4768 verifyFormat("#ifndef HEADER_H\n" 4769 "#define HEADER_H\n" 4770 "code();\n" 4771 "#endif\n", 4772 Style); 4773 // Include guards don't have #else. 4774 verifyFormat("#ifndef NOT_GUARD\n" 4775 "# define NOT_GUARD\n" 4776 "code();\n" 4777 "#else\n" 4778 "#endif", 4779 Style); 4780 verifyFormat("#ifndef NOT_GUARD\n" 4781 "# define NOT_GUARD\n" 4782 "code();\n" 4783 "#elif FOO\n" 4784 "#endif", 4785 Style); 4786 // Non-identifier #define after potential include guard. 4787 verifyFormat("#ifndef FOO\n" 4788 "# define 1\n" 4789 "#endif\n", 4790 Style); 4791 // #if closes past last non-preprocessor line. 4792 verifyFormat("#ifndef FOO\n" 4793 "#define FOO\n" 4794 "#if 1\n" 4795 "int i;\n" 4796 "# define A 0\n" 4797 "#endif\n" 4798 "#endif\n", 4799 Style); 4800 // Don't crash if there is an #elif directive without a condition. 4801 verifyFormat("#if 1\n" 4802 "int x;\n" 4803 "#elif\n" 4804 "int y;\n" 4805 "#else\n" 4806 "int z;\n" 4807 "#endif", 4808 Style); 4809 // FIXME: This doesn't handle the case where there's code between the 4810 // #ifndef and #define but all other conditions hold. This is because when 4811 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 4812 // previous code line yet, so we can't detect it. 4813 EXPECT_EQ("#ifndef NOT_GUARD\n" 4814 "code();\n" 4815 "#define NOT_GUARD\n" 4816 "code();\n" 4817 "#endif", 4818 format("#ifndef NOT_GUARD\n" 4819 "code();\n" 4820 "# define NOT_GUARD\n" 4821 "code();\n" 4822 "#endif", 4823 Style)); 4824 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 4825 // be outside an include guard. Examples are #pragma once and 4826 // #pragma GCC diagnostic, or anything else that does not change the meaning 4827 // of the file if it's included multiple times. 4828 EXPECT_EQ("#ifdef WIN32\n" 4829 "# pragma once\n" 4830 "#endif\n" 4831 "#ifndef HEADER_H\n" 4832 "# define HEADER_H\n" 4833 "code();\n" 4834 "#endif", 4835 format("#ifdef WIN32\n" 4836 "# pragma once\n" 4837 "#endif\n" 4838 "#ifndef HEADER_H\n" 4839 "#define HEADER_H\n" 4840 "code();\n" 4841 "#endif", 4842 Style)); 4843 // FIXME: This does not detect when there is a single non-preprocessor line 4844 // in front of an include-guard-like structure where other conditions hold 4845 // because ScopedLineState hides the line. 4846 EXPECT_EQ("code();\n" 4847 "#ifndef HEADER_H\n" 4848 "#define HEADER_H\n" 4849 "code();\n" 4850 "#endif", 4851 format("code();\n" 4852 "#ifndef HEADER_H\n" 4853 "# define HEADER_H\n" 4854 "code();\n" 4855 "#endif", 4856 Style)); 4857 // Keep comments aligned with #, otherwise indent comments normally. These 4858 // tests cannot use verifyFormat because messUp manipulates leading 4859 // whitespace. 4860 { 4861 const char *Expected = "" 4862 "void f() {\n" 4863 "#if 1\n" 4864 "// Preprocessor aligned.\n" 4865 "# define A 0\n" 4866 " // Code. Separated by blank line.\n" 4867 "\n" 4868 "# define B 0\n" 4869 " // Code. Not aligned with #\n" 4870 "# define C 0\n" 4871 "#endif"; 4872 const char *ToFormat = "" 4873 "void f() {\n" 4874 "#if 1\n" 4875 "// Preprocessor aligned.\n" 4876 "# define A 0\n" 4877 "// Code. Separated by blank line.\n" 4878 "\n" 4879 "# define B 0\n" 4880 " // Code. Not aligned with #\n" 4881 "# define C 0\n" 4882 "#endif"; 4883 EXPECT_EQ(Expected, format(ToFormat, Style)); 4884 EXPECT_EQ(Expected, format(Expected, Style)); 4885 } 4886 // Keep block quotes aligned. 4887 { 4888 const char *Expected = "" 4889 "void f() {\n" 4890 "#if 1\n" 4891 "/* Preprocessor aligned. */\n" 4892 "# define A 0\n" 4893 " /* Code. Separated by blank line. */\n" 4894 "\n" 4895 "# define B 0\n" 4896 " /* Code. Not aligned with # */\n" 4897 "# define C 0\n" 4898 "#endif"; 4899 const char *ToFormat = "" 4900 "void f() {\n" 4901 "#if 1\n" 4902 "/* Preprocessor aligned. */\n" 4903 "# define A 0\n" 4904 "/* Code. Separated by blank line. */\n" 4905 "\n" 4906 "# define B 0\n" 4907 " /* Code. Not aligned with # */\n" 4908 "# define C 0\n" 4909 "#endif"; 4910 EXPECT_EQ(Expected, format(ToFormat, Style)); 4911 EXPECT_EQ(Expected, format(Expected, Style)); 4912 } 4913 // Keep comments aligned with un-indented directives. 4914 { 4915 const char *Expected = "" 4916 "void f() {\n" 4917 "// Preprocessor aligned.\n" 4918 "#define A 0\n" 4919 " // Code. Separated by blank line.\n" 4920 "\n" 4921 "#define B 0\n" 4922 " // Code. Not aligned with #\n" 4923 "#define C 0\n"; 4924 const char *ToFormat = "" 4925 "void f() {\n" 4926 "// Preprocessor aligned.\n" 4927 "#define A 0\n" 4928 "// Code. Separated by blank line.\n" 4929 "\n" 4930 "#define B 0\n" 4931 " // Code. Not aligned with #\n" 4932 "#define C 0\n"; 4933 EXPECT_EQ(Expected, format(ToFormat, Style)); 4934 EXPECT_EQ(Expected, format(Expected, Style)); 4935 } 4936 // Test AfterHash with tabs. 4937 { 4938 FormatStyle Tabbed = Style; 4939 Tabbed.UseTab = FormatStyle::UT_Always; 4940 Tabbed.IndentWidth = 8; 4941 Tabbed.TabWidth = 8; 4942 verifyFormat("#ifdef _WIN32\n" 4943 "#\tdefine A 0\n" 4944 "#\tifdef VAR2\n" 4945 "#\t\tdefine B 1\n" 4946 "#\t\tinclude <someheader.h>\n" 4947 "#\t\tdefine MACRO \\\n" 4948 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 4949 "#\tendif\n" 4950 "#else\n" 4951 "#\tdefine A 1\n" 4952 "#endif", 4953 Tabbed); 4954 } 4955 4956 // Regression test: Multiline-macro inside include guards. 4957 verifyFormat("#ifndef HEADER_H\n" 4958 "#define HEADER_H\n" 4959 "#define A() \\\n" 4960 " int i; \\\n" 4961 " int j;\n" 4962 "#endif // HEADER_H", 4963 getLLVMStyleWithColumns(20)); 4964 4965 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4966 // Basic before hash indent tests 4967 verifyFormat("#ifdef _WIN32\n" 4968 " #define A 0\n" 4969 " #ifdef VAR2\n" 4970 " #define B 1\n" 4971 " #include <someheader.h>\n" 4972 " #define MACRO \\\n" 4973 " some_very_long_func_aaaaaaaaaa();\n" 4974 " #endif\n" 4975 "#else\n" 4976 " #define A 1\n" 4977 "#endif", 4978 Style); 4979 verifyFormat("#if A\n" 4980 " #define MACRO \\\n" 4981 " void a(int x) { \\\n" 4982 " b(); \\\n" 4983 " c(); \\\n" 4984 " d(); \\\n" 4985 " e(); \\\n" 4986 " f(); \\\n" 4987 " }\n" 4988 "#endif", 4989 Style); 4990 // Keep comments aligned with indented directives. These 4991 // tests cannot use verifyFormat because messUp manipulates leading 4992 // whitespace. 4993 { 4994 const char *Expected = "void f() {\n" 4995 "// Aligned to preprocessor.\n" 4996 "#if 1\n" 4997 " // Aligned to code.\n" 4998 " int a;\n" 4999 " #if 1\n" 5000 " // Aligned to preprocessor.\n" 5001 " #define A 0\n" 5002 " // Aligned to code.\n" 5003 " int b;\n" 5004 " #endif\n" 5005 "#endif\n" 5006 "}"; 5007 const char *ToFormat = "void f() {\n" 5008 "// Aligned to preprocessor.\n" 5009 "#if 1\n" 5010 "// Aligned to code.\n" 5011 "int a;\n" 5012 "#if 1\n" 5013 "// Aligned to preprocessor.\n" 5014 "#define A 0\n" 5015 "// Aligned to code.\n" 5016 "int b;\n" 5017 "#endif\n" 5018 "#endif\n" 5019 "}"; 5020 EXPECT_EQ(Expected, format(ToFormat, Style)); 5021 EXPECT_EQ(Expected, format(Expected, Style)); 5022 } 5023 { 5024 const char *Expected = "void f() {\n" 5025 "/* Aligned to preprocessor. */\n" 5026 "#if 1\n" 5027 " /* Aligned to code. */\n" 5028 " int a;\n" 5029 " #if 1\n" 5030 " /* Aligned to preprocessor. */\n" 5031 " #define A 0\n" 5032 " /* Aligned to code. */\n" 5033 " int b;\n" 5034 " #endif\n" 5035 "#endif\n" 5036 "}"; 5037 const char *ToFormat = "void f() {\n" 5038 "/* Aligned to preprocessor. */\n" 5039 "#if 1\n" 5040 "/* Aligned to code. */\n" 5041 "int a;\n" 5042 "#if 1\n" 5043 "/* Aligned to preprocessor. */\n" 5044 "#define A 0\n" 5045 "/* Aligned to code. */\n" 5046 "int b;\n" 5047 "#endif\n" 5048 "#endif\n" 5049 "}"; 5050 EXPECT_EQ(Expected, format(ToFormat, Style)); 5051 EXPECT_EQ(Expected, format(Expected, Style)); 5052 } 5053 5054 // Test single comment before preprocessor 5055 verifyFormat("// Comment\n" 5056 "\n" 5057 "#if 1\n" 5058 "#endif", 5059 Style); 5060 } 5061 5062 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5063 verifyFormat("{\n { a #c; }\n}"); 5064 } 5065 5066 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5067 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5068 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5069 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5070 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5071 } 5072 5073 TEST_F(FormatTest, EscapedNewlines) { 5074 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5075 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5076 format("#define A \\\nint i;\\\n int j;", Narrow)); 5077 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5078 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5079 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5080 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5081 5082 FormatStyle AlignLeft = getLLVMStyle(); 5083 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5084 EXPECT_EQ("#define MACRO(x) \\\n" 5085 "private: \\\n" 5086 " int x(int a);\n", 5087 format("#define MACRO(x) \\\n" 5088 "private: \\\n" 5089 " int x(int a);\n", 5090 AlignLeft)); 5091 5092 // CRLF line endings 5093 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5094 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5095 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5096 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5097 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5098 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5099 EXPECT_EQ("#define MACRO(x) \\\r\n" 5100 "private: \\\r\n" 5101 " int x(int a);\r\n", 5102 format("#define MACRO(x) \\\r\n" 5103 "private: \\\r\n" 5104 " int x(int a);\r\n", 5105 AlignLeft)); 5106 5107 FormatStyle DontAlign = getLLVMStyle(); 5108 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5109 DontAlign.MaxEmptyLinesToKeep = 3; 5110 // FIXME: can't use verifyFormat here because the newline before 5111 // "public:" is not inserted the first time it's reformatted 5112 EXPECT_EQ("#define A \\\n" 5113 " class Foo { \\\n" 5114 " void bar(); \\\n" 5115 "\\\n" 5116 "\\\n" 5117 "\\\n" 5118 " public: \\\n" 5119 " void baz(); \\\n" 5120 " };", 5121 format("#define A \\\n" 5122 " class Foo { \\\n" 5123 " void bar(); \\\n" 5124 "\\\n" 5125 "\\\n" 5126 "\\\n" 5127 " public: \\\n" 5128 " void baz(); \\\n" 5129 " };", 5130 DontAlign)); 5131 } 5132 5133 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5134 verifyFormat("#define A \\\n" 5135 " int v( \\\n" 5136 " a); \\\n" 5137 " int i;", 5138 getLLVMStyleWithColumns(11)); 5139 } 5140 5141 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5142 EXPECT_EQ( 5143 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5144 " \\\n" 5145 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5146 "\n" 5147 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5148 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5149 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5150 "\\\n" 5151 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5152 " \n" 5153 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5154 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5155 } 5156 5157 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5158 EXPECT_EQ("int\n" 5159 "#define A\n" 5160 " a;", 5161 format("int\n#define A\na;")); 5162 verifyFormat("functionCallTo(\n" 5163 " someOtherFunction(\n" 5164 " withSomeParameters, whichInSequence,\n" 5165 " areLongerThanALine(andAnotherCall,\n" 5166 "#define A B\n" 5167 " withMoreParamters,\n" 5168 " whichStronglyInfluenceTheLayout),\n" 5169 " andMoreParameters),\n" 5170 " trailing);", 5171 getLLVMStyleWithColumns(69)); 5172 verifyFormat("Foo::Foo()\n" 5173 "#ifdef BAR\n" 5174 " : baz(0)\n" 5175 "#endif\n" 5176 "{\n" 5177 "}"); 5178 verifyFormat("void f() {\n" 5179 " if (true)\n" 5180 "#ifdef A\n" 5181 " f(42);\n" 5182 " x();\n" 5183 "#else\n" 5184 " g();\n" 5185 " x();\n" 5186 "#endif\n" 5187 "}"); 5188 verifyFormat("void f(param1, param2,\n" 5189 " param3,\n" 5190 "#ifdef A\n" 5191 " param4(param5,\n" 5192 "#ifdef A1\n" 5193 " param6,\n" 5194 "#ifdef A2\n" 5195 " param7),\n" 5196 "#else\n" 5197 " param8),\n" 5198 " param9,\n" 5199 "#endif\n" 5200 " param10,\n" 5201 "#endif\n" 5202 " param11)\n" 5203 "#else\n" 5204 " param12)\n" 5205 "#endif\n" 5206 "{\n" 5207 " x();\n" 5208 "}", 5209 getLLVMStyleWithColumns(28)); 5210 verifyFormat("#if 1\n" 5211 "int i;"); 5212 verifyFormat("#if 1\n" 5213 "#endif\n" 5214 "#if 1\n" 5215 "#else\n" 5216 "#endif\n"); 5217 verifyFormat("DEBUG({\n" 5218 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5220 "});\n" 5221 "#if a\n" 5222 "#else\n" 5223 "#endif"); 5224 5225 verifyIncompleteFormat("void f(\n" 5226 "#if A\n" 5227 ");\n" 5228 "#else\n" 5229 "#endif"); 5230 } 5231 5232 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5233 verifyFormat("#endif\n" 5234 "#if B"); 5235 } 5236 5237 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5238 FormatStyle SingleLine = getLLVMStyle(); 5239 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5240 verifyFormat("#if 0\n" 5241 "#elif 1\n" 5242 "#endif\n" 5243 "void foo() {\n" 5244 " if (test) foo2();\n" 5245 "}", 5246 SingleLine); 5247 } 5248 5249 TEST_F(FormatTest, LayoutBlockInsideParens) { 5250 verifyFormat("functionCall({ int i; });"); 5251 verifyFormat("functionCall({\n" 5252 " int i;\n" 5253 " int j;\n" 5254 "});"); 5255 verifyFormat("functionCall(\n" 5256 " {\n" 5257 " int i;\n" 5258 " int j;\n" 5259 " },\n" 5260 " aaaa, bbbb, cccc);"); 5261 verifyFormat("functionA(functionB({\n" 5262 " int i;\n" 5263 " int j;\n" 5264 " }),\n" 5265 " aaaa, bbbb, cccc);"); 5266 verifyFormat("functionCall(\n" 5267 " {\n" 5268 " int i;\n" 5269 " int j;\n" 5270 " },\n" 5271 " aaaa, bbbb, // comment\n" 5272 " cccc);"); 5273 verifyFormat("functionA(functionB({\n" 5274 " int i;\n" 5275 " int j;\n" 5276 " }),\n" 5277 " aaaa, bbbb, // comment\n" 5278 " cccc);"); 5279 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5280 verifyFormat("functionCall(aaaa, bbbb, {\n" 5281 " int i;\n" 5282 " int j;\n" 5283 "});"); 5284 verifyFormat( 5285 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5286 " {\n" 5287 " int i; // break\n" 5288 " },\n" 5289 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5290 " ccccccccccccccccc));"); 5291 verifyFormat("DEBUG({\n" 5292 " if (a)\n" 5293 " f();\n" 5294 "});"); 5295 } 5296 5297 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5298 EXPECT_EQ("SOME_MACRO { int i; }\n" 5299 "int i;", 5300 format(" SOME_MACRO {int i;} int i;")); 5301 } 5302 5303 TEST_F(FormatTest, LayoutNestedBlocks) { 5304 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5305 " struct s {\n" 5306 " int i;\n" 5307 " };\n" 5308 " s kBitsToOs[] = {{10}};\n" 5309 " for (int i = 0; i < 10; ++i)\n" 5310 " return;\n" 5311 "}"); 5312 verifyFormat("call(parameter, {\n" 5313 " something();\n" 5314 " // Comment using all columns.\n" 5315 " somethingelse();\n" 5316 "});", 5317 getLLVMStyleWithColumns(40)); 5318 verifyFormat("DEBUG( //\n" 5319 " { f(); }, a);"); 5320 verifyFormat("DEBUG( //\n" 5321 " {\n" 5322 " f(); //\n" 5323 " },\n" 5324 " a);"); 5325 5326 EXPECT_EQ("call(parameter, {\n" 5327 " something();\n" 5328 " // Comment too\n" 5329 " // looooooooooong.\n" 5330 " somethingElse();\n" 5331 "});", 5332 format("call(parameter, {\n" 5333 " something();\n" 5334 " // Comment too looooooooooong.\n" 5335 " somethingElse();\n" 5336 "});", 5337 getLLVMStyleWithColumns(29))); 5338 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5339 EXPECT_EQ("DEBUG({ // comment\n" 5340 " int i;\n" 5341 "});", 5342 format("DEBUG({ // comment\n" 5343 "int i;\n" 5344 "});")); 5345 EXPECT_EQ("DEBUG({\n" 5346 " int i;\n" 5347 "\n" 5348 " // comment\n" 5349 " int j;\n" 5350 "});", 5351 format("DEBUG({\n" 5352 " int i;\n" 5353 "\n" 5354 " // comment\n" 5355 " int j;\n" 5356 "});")); 5357 5358 verifyFormat("DEBUG({\n" 5359 " if (a)\n" 5360 " return;\n" 5361 "});"); 5362 verifyGoogleFormat("DEBUG({\n" 5363 " if (a) return;\n" 5364 "});"); 5365 FormatStyle Style = getGoogleStyle(); 5366 Style.ColumnLimit = 45; 5367 verifyFormat("Debug(\n" 5368 " aaaaa,\n" 5369 " {\n" 5370 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5371 " },\n" 5372 " a);", 5373 Style); 5374 5375 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5376 5377 verifyNoCrash("^{v^{a}}"); 5378 } 5379 5380 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5381 EXPECT_EQ("#define MACRO() \\\n" 5382 " Debug(aaa, /* force line break */ \\\n" 5383 " { \\\n" 5384 " int i; \\\n" 5385 " int j; \\\n" 5386 " })", 5387 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5388 " { int i; int j; })", 5389 getGoogleStyle())); 5390 5391 EXPECT_EQ("#define A \\\n" 5392 " [] { \\\n" 5393 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5394 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5395 " }", 5396 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5397 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5398 getGoogleStyle())); 5399 } 5400 5401 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5402 EXPECT_EQ("{}", format("{}")); 5403 verifyFormat("enum E {};"); 5404 verifyFormat("enum E {}"); 5405 FormatStyle Style = getLLVMStyle(); 5406 Style.SpaceInEmptyBlock = true; 5407 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5408 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5409 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5410 } 5411 5412 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5413 FormatStyle Style = getLLVMStyle(); 5414 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5415 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5416 verifyFormat("FOO_BEGIN\n" 5417 " FOO_ENTRY\n" 5418 "FOO_END", 5419 Style); 5420 verifyFormat("FOO_BEGIN\n" 5421 " NESTED_FOO_BEGIN\n" 5422 " NESTED_FOO_ENTRY\n" 5423 " NESTED_FOO_END\n" 5424 "FOO_END", 5425 Style); 5426 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5427 " int x;\n" 5428 " x = 1;\n" 5429 "FOO_END(Baz)", 5430 Style); 5431 } 5432 5433 //===----------------------------------------------------------------------===// 5434 // Line break tests. 5435 //===----------------------------------------------------------------------===// 5436 5437 TEST_F(FormatTest, PreventConfusingIndents) { 5438 verifyFormat( 5439 "void f() {\n" 5440 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5441 " parameter, parameter, parameter)),\n" 5442 " SecondLongCall(parameter));\n" 5443 "}"); 5444 verifyFormat( 5445 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5446 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5448 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5449 verifyFormat( 5450 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5451 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5452 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5453 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5454 verifyFormat( 5455 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5456 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5458 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5459 verifyFormat("int a = bbbb && ccc &&\n" 5460 " fffff(\n" 5461 "#define A Just forcing a new line\n" 5462 " ddd);"); 5463 } 5464 5465 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5466 verifyFormat( 5467 "bool aaaaaaa =\n" 5468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5469 " bbbbbbbb();"); 5470 verifyFormat( 5471 "bool aaaaaaa =\n" 5472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5473 " bbbbbbbb();"); 5474 5475 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5476 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5477 " ccccccccc == ddddddddddd;"); 5478 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5479 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5480 " ccccccccc == ddddddddddd;"); 5481 verifyFormat( 5482 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5483 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5484 " ccccccccc == ddddddddddd;"); 5485 5486 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5487 " aaaaaa) &&\n" 5488 " bbbbbb && cccccc;"); 5489 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5490 " aaaaaa) >>\n" 5491 " bbbbbb;"); 5492 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5493 " SourceMgr.getSpellingColumnNumber(\n" 5494 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5495 " 1);"); 5496 5497 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5498 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5499 " cccccc) {\n}"); 5500 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5501 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5502 " cccccc) {\n}"); 5503 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5504 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5505 " cccccc) {\n}"); 5506 verifyFormat("b = a &&\n" 5507 " // Comment\n" 5508 " b.c && d;"); 5509 5510 // If the LHS of a comparison is not a binary expression itself, the 5511 // additional linebreak confuses many people. 5512 verifyFormat( 5513 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5515 "}"); 5516 verifyFormat( 5517 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5519 "}"); 5520 verifyFormat( 5521 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5523 "}"); 5524 verifyFormat( 5525 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5527 "}"); 5528 // Even explicit parentheses stress the precedence enough to make the 5529 // additional break unnecessary. 5530 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5532 "}"); 5533 // This cases is borderline, but with the indentation it is still readable. 5534 verifyFormat( 5535 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5536 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5537 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5538 "}", 5539 getLLVMStyleWithColumns(75)); 5540 5541 // If the LHS is a binary expression, we should still use the additional break 5542 // as otherwise the formatting hides the operator precedence. 5543 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5545 " 5) {\n" 5546 "}"); 5547 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5549 " 5) {\n" 5550 "}"); 5551 5552 FormatStyle OnePerLine = getLLVMStyle(); 5553 OnePerLine.BinPackParameters = false; 5554 verifyFormat( 5555 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5558 OnePerLine); 5559 5560 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5561 " .aaa(aaaaaaaaaaaaa) *\n" 5562 " aaaaaaa +\n" 5563 " aaaaaaa;", 5564 getLLVMStyleWithColumns(40)); 5565 } 5566 5567 TEST_F(FormatTest, ExpressionIndentation) { 5568 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5570 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5572 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5573 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5576 " ccccccccccccccccccccccccccccccccccccccccc;"); 5577 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5579 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5580 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5581 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5584 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5585 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5588 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5589 verifyFormat("if () {\n" 5590 "} else if (aaaaa && bbbbb > // break\n" 5591 " ccccc) {\n" 5592 "}"); 5593 verifyFormat("if () {\n" 5594 "} else if constexpr (aaaaa && bbbbb > // break\n" 5595 " ccccc) {\n" 5596 "}"); 5597 verifyFormat("if () {\n" 5598 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 5599 " ccccc) {\n" 5600 "}"); 5601 verifyFormat("if () {\n" 5602 "} else if (aaaaa &&\n" 5603 " bbbbb > // break\n" 5604 " ccccc &&\n" 5605 " ddddd) {\n" 5606 "}"); 5607 5608 // Presence of a trailing comment used to change indentation of b. 5609 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 5610 " b;\n" 5611 "return aaaaaaaaaaaaaaaaaaa +\n" 5612 " b; //", 5613 getLLVMStyleWithColumns(30)); 5614 } 5615 5616 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 5617 // Not sure what the best system is here. Like this, the LHS can be found 5618 // immediately above an operator (everything with the same or a higher 5619 // indent). The RHS is aligned right of the operator and so compasses 5620 // everything until something with the same indent as the operator is found. 5621 // FIXME: Is this a good system? 5622 FormatStyle Style = getLLVMStyle(); 5623 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5624 verifyFormat( 5625 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5626 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5627 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5628 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5629 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5630 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5631 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5632 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5633 " > ccccccccccccccccccccccccccccccccccccccccc;", 5634 Style); 5635 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5636 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5637 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5638 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5639 Style); 5640 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5641 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5642 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5643 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5644 Style); 5645 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5646 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5647 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5648 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5649 Style); 5650 verifyFormat("if () {\n" 5651 "} else if (aaaaa\n" 5652 " && bbbbb // break\n" 5653 " > ccccc) {\n" 5654 "}", 5655 Style); 5656 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5657 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5658 Style); 5659 verifyFormat("return (a)\n" 5660 " // comment\n" 5661 " + b;", 5662 Style); 5663 verifyFormat( 5664 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5665 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5666 " + cc;", 5667 Style); 5668 5669 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5670 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5671 Style); 5672 5673 // Forced by comments. 5674 verifyFormat( 5675 "unsigned ContentSize =\n" 5676 " sizeof(int16_t) // DWARF ARange version number\n" 5677 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5678 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5679 " + sizeof(int8_t); // Segment Size (in bytes)"); 5680 5681 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5682 " == boost::fusion::at_c<1>(iiii).second;", 5683 Style); 5684 5685 Style.ColumnLimit = 60; 5686 verifyFormat("zzzzzzzzzz\n" 5687 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5688 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5689 Style); 5690 5691 Style.ColumnLimit = 80; 5692 Style.IndentWidth = 4; 5693 Style.TabWidth = 4; 5694 Style.UseTab = FormatStyle::UT_Always; 5695 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5696 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5697 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 5698 "\t&& (someOtherLongishConditionPart1\n" 5699 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 5700 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 5701 "(someOtherLongishConditionPart1 || " 5702 "someOtherEvenLongerNestedConditionPart2);", 5703 Style)); 5704 } 5705 5706 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 5707 FormatStyle Style = getLLVMStyle(); 5708 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5709 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 5710 5711 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5712 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5713 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5714 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5715 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5716 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5717 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5718 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5719 " > ccccccccccccccccccccccccccccccccccccccccc;", 5720 Style); 5721 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5722 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5723 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5724 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5725 Style); 5726 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5727 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5728 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5729 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5730 Style); 5731 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5732 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5733 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5734 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5735 Style); 5736 verifyFormat("if () {\n" 5737 "} else if (aaaaa\n" 5738 " && bbbbb // break\n" 5739 " > ccccc) {\n" 5740 "}", 5741 Style); 5742 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5743 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5744 Style); 5745 verifyFormat("return (a)\n" 5746 " // comment\n" 5747 " + b;", 5748 Style); 5749 verifyFormat( 5750 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5751 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5752 " + cc;", 5753 Style); 5754 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 5755 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5756 " : 3333333333333333;", 5757 Style); 5758 verifyFormat( 5759 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 5760 " : ccccccccccccccc ? dddddddddddddddddd\n" 5761 " : eeeeeeeeeeeeeeeeee)\n" 5762 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5763 " : 3333333333333333;", 5764 Style); 5765 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5766 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5767 Style); 5768 5769 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5770 " == boost::fusion::at_c<1>(iiii).second;", 5771 Style); 5772 5773 Style.ColumnLimit = 60; 5774 verifyFormat("zzzzzzzzzzzzz\n" 5775 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5776 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5777 Style); 5778 5779 // Forced by comments. 5780 Style.ColumnLimit = 80; 5781 verifyFormat( 5782 "unsigned ContentSize\n" 5783 " = sizeof(int16_t) // DWARF ARange version number\n" 5784 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5785 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5786 " + sizeof(int8_t); // Segment Size (in bytes)", 5787 Style); 5788 5789 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5790 verifyFormat( 5791 "unsigned ContentSize =\n" 5792 " sizeof(int16_t) // DWARF ARange version number\n" 5793 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5794 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5795 " + sizeof(int8_t); // Segment Size (in bytes)", 5796 Style); 5797 5798 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5799 verifyFormat( 5800 "unsigned ContentSize =\n" 5801 " sizeof(int16_t) // DWARF ARange version number\n" 5802 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5803 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5804 " + sizeof(int8_t); // Segment Size (in bytes)", 5805 Style); 5806 } 5807 5808 TEST_F(FormatTest, EnforcedOperatorWraps) { 5809 // Here we'd like to wrap after the || operators, but a comment is forcing an 5810 // earlier wrap. 5811 verifyFormat("bool x = aaaaa //\n" 5812 " || bbbbb\n" 5813 " //\n" 5814 " || cccc;"); 5815 } 5816 5817 TEST_F(FormatTest, NoOperandAlignment) { 5818 FormatStyle Style = getLLVMStyle(); 5819 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5820 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 5821 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5822 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5823 Style); 5824 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5825 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5826 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5827 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5828 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5829 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5830 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5831 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5832 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5833 " > ccccccccccccccccccccccccccccccccccccccccc;", 5834 Style); 5835 5836 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5837 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5838 " + cc;", 5839 Style); 5840 verifyFormat("int a = aa\n" 5841 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5842 " * cccccccccccccccccccccccccccccccccccc;\n", 5843 Style); 5844 5845 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5846 verifyFormat("return (a > b\n" 5847 " // comment1\n" 5848 " // comment2\n" 5849 " || c);", 5850 Style); 5851 } 5852 5853 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 5854 FormatStyle Style = getLLVMStyle(); 5855 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5856 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5858 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5859 Style); 5860 } 5861 5862 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 5863 FormatStyle Style = getLLVMStyle(); 5864 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5865 Style.BinPackArguments = false; 5866 Style.ColumnLimit = 40; 5867 verifyFormat("void test() {\n" 5868 " someFunction(\n" 5869 " this + argument + is + quite\n" 5870 " + long + so + it + gets + wrapped\n" 5871 " + but + remains + bin - packed);\n" 5872 "}", 5873 Style); 5874 verifyFormat("void test() {\n" 5875 " someFunction(arg1,\n" 5876 " this + argument + is\n" 5877 " + quite + long + so\n" 5878 " + it + gets + wrapped\n" 5879 " + but + remains + bin\n" 5880 " - packed,\n" 5881 " arg3);\n" 5882 "}", 5883 Style); 5884 verifyFormat("void test() {\n" 5885 " someFunction(\n" 5886 " arg1,\n" 5887 " this + argument + has\n" 5888 " + anotherFunc(nested,\n" 5889 " calls + whose\n" 5890 " + arguments\n" 5891 " + are + also\n" 5892 " + wrapped,\n" 5893 " in + addition)\n" 5894 " + to + being + bin - packed,\n" 5895 " arg3);\n" 5896 "}", 5897 Style); 5898 5899 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5900 verifyFormat("void test() {\n" 5901 " someFunction(\n" 5902 " arg1,\n" 5903 " this + argument + has +\n" 5904 " anotherFunc(nested,\n" 5905 " calls + whose +\n" 5906 " arguments +\n" 5907 " are + also +\n" 5908 " wrapped,\n" 5909 " in + addition) +\n" 5910 " to + being + bin - packed,\n" 5911 " arg3);\n" 5912 "}", 5913 Style); 5914 } 5915 5916 TEST_F(FormatTest, ConstructorInitializers) { 5917 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5918 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 5919 getLLVMStyleWithColumns(45)); 5920 verifyFormat("Constructor()\n" 5921 " : Inttializer(FitsOnTheLine) {}", 5922 getLLVMStyleWithColumns(44)); 5923 verifyFormat("Constructor()\n" 5924 " : Inttializer(FitsOnTheLine) {}", 5925 getLLVMStyleWithColumns(43)); 5926 5927 verifyFormat("template <typename T>\n" 5928 "Constructor() : Initializer(FitsOnTheLine) {}", 5929 getLLVMStyleWithColumns(45)); 5930 5931 verifyFormat( 5932 "SomeClass::Constructor()\n" 5933 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5934 5935 verifyFormat( 5936 "SomeClass::Constructor()\n" 5937 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5938 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 5939 verifyFormat( 5940 "SomeClass::Constructor()\n" 5941 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5942 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5943 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5944 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5945 " : aaaaaaaaaa(aaaaaa) {}"); 5946 5947 verifyFormat("Constructor()\n" 5948 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5949 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5950 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5951 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 5952 5953 verifyFormat("Constructor()\n" 5954 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5956 5957 verifyFormat("Constructor(int Parameter = 0)\n" 5958 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5959 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 5960 verifyFormat("Constructor()\n" 5961 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5962 "}", 5963 getLLVMStyleWithColumns(60)); 5964 verifyFormat("Constructor()\n" 5965 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5966 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 5967 5968 // Here a line could be saved by splitting the second initializer onto two 5969 // lines, but that is not desirable. 5970 verifyFormat("Constructor()\n" 5971 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5972 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5973 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5974 5975 FormatStyle OnePerLine = getLLVMStyle(); 5976 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 5977 verifyFormat("MyClass::MyClass()\n" 5978 " : a(a),\n" 5979 " b(b),\n" 5980 " c(c) {}", 5981 OnePerLine); 5982 verifyFormat("MyClass::MyClass()\n" 5983 " : a(a), // comment\n" 5984 " b(b),\n" 5985 " c(c) {}", 5986 OnePerLine); 5987 verifyFormat("MyClass::MyClass(int a)\n" 5988 " : b(a), // comment\n" 5989 " c(a + 1) { // lined up\n" 5990 "}", 5991 OnePerLine); 5992 verifyFormat("Constructor()\n" 5993 " : a(b, b, b) {}", 5994 OnePerLine); 5995 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 5996 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 5997 verifyFormat("SomeClass::Constructor()\n" 5998 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5999 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6000 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6001 OnePerLine); 6002 verifyFormat("SomeClass::Constructor()\n" 6003 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6004 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6005 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6006 OnePerLine); 6007 verifyFormat("MyClass::MyClass(int var)\n" 6008 " : some_var_(var), // 4 space indent\n" 6009 " some_other_var_(var + 1) { // lined up\n" 6010 "}", 6011 OnePerLine); 6012 verifyFormat("Constructor()\n" 6013 " : aaaaa(aaaaaa),\n" 6014 " aaaaa(aaaaaa),\n" 6015 " aaaaa(aaaaaa),\n" 6016 " aaaaa(aaaaaa),\n" 6017 " aaaaa(aaaaaa) {}", 6018 OnePerLine); 6019 verifyFormat("Constructor()\n" 6020 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6021 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6022 OnePerLine); 6023 OnePerLine.BinPackParameters = false; 6024 verifyFormat( 6025 "Constructor()\n" 6026 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6027 " aaaaaaaaaaa().aaa(),\n" 6028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6029 OnePerLine); 6030 OnePerLine.ColumnLimit = 60; 6031 verifyFormat("Constructor()\n" 6032 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6033 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6034 OnePerLine); 6035 6036 EXPECT_EQ("Constructor()\n" 6037 " : // Comment forcing unwanted break.\n" 6038 " aaaa(aaaa) {}", 6039 format("Constructor() :\n" 6040 " // Comment forcing unwanted break.\n" 6041 " aaaa(aaaa) {}")); 6042 } 6043 6044 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6045 FormatStyle Style = getLLVMStyle(); 6046 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6047 Style.ColumnLimit = 60; 6048 Style.BinPackParameters = false; 6049 6050 for (int i = 0; i < 4; ++i) { 6051 // Test all combinations of parameters that should not have an effect. 6052 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6053 Style.AllowAllArgumentsOnNextLine = i & 2; 6054 6055 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6056 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6057 verifyFormat("Constructor()\n" 6058 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6059 Style); 6060 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6061 6062 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6063 verifyFormat("Constructor()\n" 6064 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6065 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6066 Style); 6067 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6068 6069 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6070 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6071 verifyFormat("Constructor()\n" 6072 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6073 Style); 6074 6075 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6076 verifyFormat("Constructor()\n" 6077 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6078 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6079 Style); 6080 6081 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6082 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6083 verifyFormat("Constructor() :\n" 6084 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6085 Style); 6086 6087 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6088 verifyFormat("Constructor() :\n" 6089 " aaaaaaaaaaaaaaaaaa(a),\n" 6090 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6091 Style); 6092 } 6093 6094 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6095 // AllowAllConstructorInitializersOnNextLine in all 6096 // BreakConstructorInitializers modes 6097 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6098 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6099 verifyFormat("SomeClassWithALongName::Constructor(\n" 6100 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6101 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6102 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6103 Style); 6104 6105 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6106 verifyFormat("SomeClassWithALongName::Constructor(\n" 6107 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6108 " int bbbbbbbbbbbbb,\n" 6109 " int cccccccccccccccc)\n" 6110 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6111 Style); 6112 6113 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6114 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6115 verifyFormat("SomeClassWithALongName::Constructor(\n" 6116 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6117 " int bbbbbbbbbbbbb)\n" 6118 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6119 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6120 Style); 6121 6122 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6123 6124 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6125 verifyFormat("SomeClassWithALongName::Constructor(\n" 6126 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6127 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6128 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6129 Style); 6130 6131 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6132 verifyFormat("SomeClassWithALongName::Constructor(\n" 6133 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6134 " int bbbbbbbbbbbbb,\n" 6135 " int cccccccccccccccc)\n" 6136 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6137 Style); 6138 6139 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6140 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6141 verifyFormat("SomeClassWithALongName::Constructor(\n" 6142 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6143 " int bbbbbbbbbbbbb)\n" 6144 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6145 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6146 Style); 6147 6148 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6149 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6150 verifyFormat("SomeClassWithALongName::Constructor(\n" 6151 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6152 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6153 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6154 Style); 6155 6156 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6157 verifyFormat("SomeClassWithALongName::Constructor(\n" 6158 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6159 " int bbbbbbbbbbbbb,\n" 6160 " int cccccccccccccccc) :\n" 6161 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6162 Style); 6163 6164 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6165 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6166 verifyFormat("SomeClassWithALongName::Constructor(\n" 6167 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6168 " int bbbbbbbbbbbbb) :\n" 6169 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6170 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6171 Style); 6172 } 6173 6174 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6175 FormatStyle Style = getLLVMStyle(); 6176 Style.ColumnLimit = 60; 6177 Style.BinPackArguments = false; 6178 for (int i = 0; i < 4; ++i) { 6179 // Test all combinations of parameters that should not have an effect. 6180 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6181 Style.PackConstructorInitializers = 6182 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6183 6184 Style.AllowAllArgumentsOnNextLine = true; 6185 verifyFormat("void foo() {\n" 6186 " FunctionCallWithReallyLongName(\n" 6187 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6188 "}", 6189 Style); 6190 Style.AllowAllArgumentsOnNextLine = false; 6191 verifyFormat("void foo() {\n" 6192 " FunctionCallWithReallyLongName(\n" 6193 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6194 " bbbbbbbbbbbb);\n" 6195 "}", 6196 Style); 6197 6198 Style.AllowAllArgumentsOnNextLine = true; 6199 verifyFormat("void foo() {\n" 6200 " auto VariableWithReallyLongName = {\n" 6201 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6202 "}", 6203 Style); 6204 Style.AllowAllArgumentsOnNextLine = false; 6205 verifyFormat("void foo() {\n" 6206 " auto VariableWithReallyLongName = {\n" 6207 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6208 " bbbbbbbbbbbb};\n" 6209 "}", 6210 Style); 6211 } 6212 6213 // This parameter should not affect declarations. 6214 Style.BinPackParameters = false; 6215 Style.AllowAllArgumentsOnNextLine = false; 6216 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6217 verifyFormat("void FunctionCallWithReallyLongName(\n" 6218 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6219 Style); 6220 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6221 verifyFormat("void FunctionCallWithReallyLongName(\n" 6222 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6223 " int bbbbbbbbbbbb);", 6224 Style); 6225 } 6226 6227 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6228 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6229 // and BAS_Align. 6230 auto Style = getLLVMStyle(); 6231 Style.ColumnLimit = 35; 6232 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6233 "void functionDecl(int A, int B, int C);"; 6234 Style.AllowAllArgumentsOnNextLine = false; 6235 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6236 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6237 " paramC);\n" 6238 "void functionDecl(int A, int B,\n" 6239 " int C);"), 6240 format(Input, Style)); 6241 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6242 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6243 " paramC);\n" 6244 "void functionDecl(int A, int B,\n" 6245 " int C);"), 6246 format(Input, Style)); 6247 // However, BAS_AlwaysBreak should take precedence over 6248 // AllowAllArgumentsOnNextLine. 6249 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6250 EXPECT_EQ(StringRef("functionCall(\n" 6251 " paramA, paramB, paramC);\n" 6252 "void functionDecl(\n" 6253 " int A, int B, int C);"), 6254 format(Input, Style)); 6255 6256 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6257 // first argument. 6258 Style.AllowAllArgumentsOnNextLine = true; 6259 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6260 EXPECT_EQ(StringRef("functionCall(\n" 6261 " paramA, paramB, paramC);\n" 6262 "void functionDecl(\n" 6263 " int A, int B, int C);"), 6264 format(Input, Style)); 6265 // It wouldn't fit on one line with aligned parameters so this setting 6266 // doesn't change anything for BAS_Align. 6267 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6268 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6269 " paramC);\n" 6270 "void functionDecl(int A, int B,\n" 6271 " int C);"), 6272 format(Input, Style)); 6273 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6274 EXPECT_EQ(StringRef("functionCall(\n" 6275 " paramA, paramB, paramC);\n" 6276 "void functionDecl(\n" 6277 " int A, int B, int C);"), 6278 format(Input, Style)); 6279 } 6280 6281 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6282 FormatStyle Style = getLLVMStyle(); 6283 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6284 6285 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6286 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6287 getStyleWithColumns(Style, 45)); 6288 verifyFormat("Constructor() :\n" 6289 " Initializer(FitsOnTheLine) {}", 6290 getStyleWithColumns(Style, 44)); 6291 verifyFormat("Constructor() :\n" 6292 " Initializer(FitsOnTheLine) {}", 6293 getStyleWithColumns(Style, 43)); 6294 6295 verifyFormat("template <typename T>\n" 6296 "Constructor() : Initializer(FitsOnTheLine) {}", 6297 getStyleWithColumns(Style, 50)); 6298 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6299 verifyFormat( 6300 "SomeClass::Constructor() :\n" 6301 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6302 Style); 6303 6304 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 6305 verifyFormat( 6306 "SomeClass::Constructor() :\n" 6307 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6308 Style); 6309 6310 verifyFormat( 6311 "SomeClass::Constructor() :\n" 6312 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6313 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6314 Style); 6315 verifyFormat( 6316 "SomeClass::Constructor() :\n" 6317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6318 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6319 Style); 6320 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6321 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6322 " aaaaaaaaaa(aaaaaa) {}", 6323 Style); 6324 6325 verifyFormat("Constructor() :\n" 6326 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6327 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6328 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6329 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6330 Style); 6331 6332 verifyFormat("Constructor() :\n" 6333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6335 Style); 6336 6337 verifyFormat("Constructor(int Parameter = 0) :\n" 6338 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6339 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6340 Style); 6341 verifyFormat("Constructor() :\n" 6342 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6343 "}", 6344 getStyleWithColumns(Style, 60)); 6345 verifyFormat("Constructor() :\n" 6346 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6347 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6348 Style); 6349 6350 // Here a line could be saved by splitting the second initializer onto two 6351 // lines, but that is not desirable. 6352 verifyFormat("Constructor() :\n" 6353 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6354 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6355 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6356 Style); 6357 6358 FormatStyle OnePerLine = Style; 6359 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6360 verifyFormat("SomeClass::Constructor() :\n" 6361 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6362 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6363 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6364 OnePerLine); 6365 verifyFormat("SomeClass::Constructor() :\n" 6366 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6367 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6368 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6369 OnePerLine); 6370 verifyFormat("MyClass::MyClass(int var) :\n" 6371 " some_var_(var), // 4 space indent\n" 6372 " some_other_var_(var + 1) { // lined up\n" 6373 "}", 6374 OnePerLine); 6375 verifyFormat("Constructor() :\n" 6376 " aaaaa(aaaaaa),\n" 6377 " aaaaa(aaaaaa),\n" 6378 " aaaaa(aaaaaa),\n" 6379 " aaaaa(aaaaaa),\n" 6380 " aaaaa(aaaaaa) {}", 6381 OnePerLine); 6382 verifyFormat("Constructor() :\n" 6383 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6384 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6385 OnePerLine); 6386 OnePerLine.BinPackParameters = false; 6387 verifyFormat("Constructor() :\n" 6388 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6389 " aaaaaaaaaaa().aaa(),\n" 6390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6391 OnePerLine); 6392 OnePerLine.ColumnLimit = 60; 6393 verifyFormat("Constructor() :\n" 6394 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6395 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6396 OnePerLine); 6397 6398 EXPECT_EQ("Constructor() :\n" 6399 " // Comment forcing unwanted break.\n" 6400 " aaaa(aaaa) {}", 6401 format("Constructor() :\n" 6402 " // Comment forcing unwanted break.\n" 6403 " aaaa(aaaa) {}", 6404 Style)); 6405 6406 Style.ColumnLimit = 0; 6407 verifyFormat("SomeClass::Constructor() :\n" 6408 " a(a) {}", 6409 Style); 6410 verifyFormat("SomeClass::Constructor() noexcept :\n" 6411 " a(a) {}", 6412 Style); 6413 verifyFormat("SomeClass::Constructor() :\n" 6414 " a(a), b(b), c(c) {}", 6415 Style); 6416 verifyFormat("SomeClass::Constructor() :\n" 6417 " a(a) {\n" 6418 " foo();\n" 6419 " bar();\n" 6420 "}", 6421 Style); 6422 6423 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6424 verifyFormat("SomeClass::Constructor() :\n" 6425 " a(a), b(b), c(c) {\n" 6426 "}", 6427 Style); 6428 verifyFormat("SomeClass::Constructor() :\n" 6429 " a(a) {\n" 6430 "}", 6431 Style); 6432 6433 Style.ColumnLimit = 80; 6434 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6435 Style.ConstructorInitializerIndentWidth = 2; 6436 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6437 verifyFormat("SomeClass::Constructor() :\n" 6438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6439 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6440 Style); 6441 6442 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6443 // well 6444 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6445 verifyFormat( 6446 "class SomeClass\n" 6447 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6448 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6449 Style); 6450 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6451 verifyFormat( 6452 "class SomeClass\n" 6453 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6454 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6455 Style); 6456 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6457 verifyFormat( 6458 "class SomeClass :\n" 6459 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6460 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6461 Style); 6462 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6463 verifyFormat( 6464 "class SomeClass\n" 6465 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6466 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6467 Style); 6468 } 6469 6470 #ifndef EXPENSIVE_CHECKS 6471 // Expensive checks enables libstdc++ checking which includes validating the 6472 // state of ranges used in std::priority_queue - this blows out the 6473 // runtime/scalability of the function and makes this test unacceptably slow. 6474 TEST_F(FormatTest, MemoizationTests) { 6475 // This breaks if the memoization lookup does not take \c Indent and 6476 // \c LastSpace into account. 6477 verifyFormat( 6478 "extern CFRunLoopTimerRef\n" 6479 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6480 " CFTimeInterval interval, CFOptionFlags flags,\n" 6481 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6482 " CFRunLoopTimerContext *context) {}"); 6483 6484 // Deep nesting somewhat works around our memoization. 6485 verifyFormat( 6486 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6487 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6488 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6489 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6490 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6491 getLLVMStyleWithColumns(65)); 6492 verifyFormat( 6493 "aaaaa(\n" 6494 " aaaaa,\n" 6495 " aaaaa(\n" 6496 " aaaaa,\n" 6497 " aaaaa(\n" 6498 " aaaaa,\n" 6499 " aaaaa(\n" 6500 " aaaaa,\n" 6501 " aaaaa(\n" 6502 " aaaaa,\n" 6503 " aaaaa(\n" 6504 " aaaaa,\n" 6505 " aaaaa(\n" 6506 " aaaaa,\n" 6507 " aaaaa(\n" 6508 " aaaaa,\n" 6509 " aaaaa(\n" 6510 " aaaaa,\n" 6511 " aaaaa(\n" 6512 " aaaaa,\n" 6513 " aaaaa(\n" 6514 " aaaaa,\n" 6515 " aaaaa(\n" 6516 " aaaaa,\n" 6517 " aaaaa))))))))))));", 6518 getLLVMStyleWithColumns(65)); 6519 verifyFormat( 6520 "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" 6521 " a),\n" 6522 " a),\n" 6523 " a),\n" 6524 " a),\n" 6525 " a),\n" 6526 " a),\n" 6527 " a),\n" 6528 " a),\n" 6529 " a),\n" 6530 " a),\n" 6531 " a),\n" 6532 " a),\n" 6533 " a),\n" 6534 " a),\n" 6535 " a),\n" 6536 " a),\n" 6537 " a)", 6538 getLLVMStyleWithColumns(65)); 6539 6540 // This test takes VERY long when memoization is broken. 6541 FormatStyle OnePerLine = getLLVMStyle(); 6542 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6543 OnePerLine.BinPackParameters = false; 6544 std::string input = "Constructor()\n" 6545 " : aaaa(a,\n"; 6546 for (unsigned i = 0, e = 80; i != e; ++i) { 6547 input += " a,\n"; 6548 } 6549 input += " a) {}"; 6550 verifyFormat(input, OnePerLine); 6551 } 6552 #endif 6553 6554 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6555 verifyFormat( 6556 "void f() {\n" 6557 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6558 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6559 " f();\n" 6560 "}"); 6561 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6562 " Intervals[i - 1].getRange().getLast()) {\n}"); 6563 } 6564 6565 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6566 // Principially, we break function declarations in a certain order: 6567 // 1) break amongst arguments. 6568 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6569 " Cccccccccccccc cccccccccccccc);"); 6570 verifyFormat("template <class TemplateIt>\n" 6571 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6572 " TemplateIt *stop) {}"); 6573 6574 // 2) break after return type. 6575 verifyFormat( 6576 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6577 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6578 getGoogleStyle()); 6579 6580 // 3) break after (. 6581 verifyFormat( 6582 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6583 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6584 getGoogleStyle()); 6585 6586 // 4) break before after nested name specifiers. 6587 verifyFormat( 6588 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6589 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6590 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6591 getGoogleStyle()); 6592 6593 // However, there are exceptions, if a sufficient amount of lines can be 6594 // saved. 6595 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6596 // more adjusting. 6597 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6598 " Cccccccccccccc cccccccccc,\n" 6599 " Cccccccccccccc cccccccccc,\n" 6600 " Cccccccccccccc cccccccccc,\n" 6601 " Cccccccccccccc cccccccccc);"); 6602 verifyFormat( 6603 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6604 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6605 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6606 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 6607 getGoogleStyle()); 6608 verifyFormat( 6609 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6610 " Cccccccccccccc cccccccccc,\n" 6611 " Cccccccccccccc cccccccccc,\n" 6612 " Cccccccccccccc cccccccccc,\n" 6613 " Cccccccccccccc cccccccccc,\n" 6614 " Cccccccccccccc cccccccccc,\n" 6615 " Cccccccccccccc cccccccccc);"); 6616 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6617 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6618 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6619 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6620 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 6621 6622 // Break after multi-line parameters. 6623 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6626 " bbbb bbbb);"); 6627 verifyFormat("void SomeLoooooooooooongFunction(\n" 6628 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6629 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6630 " int bbbbbbbbbbbbb);"); 6631 6632 // Treat overloaded operators like other functions. 6633 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6634 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 6635 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6636 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 6637 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6638 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 6639 verifyGoogleFormat( 6640 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 6641 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6642 verifyGoogleFormat( 6643 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 6644 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6645 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6646 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6647 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 6648 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6649 verifyGoogleFormat( 6650 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 6651 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6652 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 6653 verifyGoogleFormat("template <typename T>\n" 6654 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6655 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 6656 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 6657 6658 FormatStyle Style = getLLVMStyle(); 6659 Style.PointerAlignment = FormatStyle::PAS_Left; 6660 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6661 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 6662 Style); 6663 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 6664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6665 Style); 6666 } 6667 6668 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 6669 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 6670 // Prefer keeping `::` followed by `operator` together. 6671 EXPECT_EQ("const aaaa::bbbbbbb &\n" 6672 "ccccccccc::operator++() {\n" 6673 " stuff();\n" 6674 "}", 6675 format("const aaaa::bbbbbbb\n" 6676 "&ccccccccc::operator++() { stuff(); }", 6677 getLLVMStyleWithColumns(40))); 6678 } 6679 6680 TEST_F(FormatTest, TrailingReturnType) { 6681 verifyFormat("auto foo() -> int;\n"); 6682 // correct trailing return type spacing 6683 verifyFormat("auto operator->() -> int;\n"); 6684 verifyFormat("auto operator++(int) -> int;\n"); 6685 6686 verifyFormat("struct S {\n" 6687 " auto bar() const -> int;\n" 6688 "};"); 6689 verifyFormat("template <size_t Order, typename T>\n" 6690 "auto load_img(const std::string &filename)\n" 6691 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 6692 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 6693 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 6694 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 6695 verifyFormat("template <typename T>\n" 6696 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 6697 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 6698 6699 // Not trailing return types. 6700 verifyFormat("void f() { auto a = b->c(); }"); 6701 } 6702 6703 TEST_F(FormatTest, DeductionGuides) { 6704 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 6705 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 6706 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 6707 verifyFormat( 6708 "template <class... T>\n" 6709 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 6710 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 6711 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 6712 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 6713 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 6714 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 6715 verifyFormat("template <class T> x() -> x<1>;"); 6716 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 6717 6718 // Ensure not deduction guides. 6719 verifyFormat("c()->f<int>();"); 6720 verifyFormat("x()->foo<1>;"); 6721 verifyFormat("x = p->foo<3>();"); 6722 verifyFormat("x()->x<1>();"); 6723 verifyFormat("x()->x<1>;"); 6724 } 6725 6726 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 6727 // Avoid breaking before trailing 'const' or other trailing annotations, if 6728 // they are not function-like. 6729 FormatStyle Style = getGoogleStyle(); 6730 Style.ColumnLimit = 47; 6731 verifyFormat("void someLongFunction(\n" 6732 " int someLoooooooooooooongParameter) const {\n}", 6733 getLLVMStyleWithColumns(47)); 6734 verifyFormat("LoooooongReturnType\n" 6735 "someLoooooooongFunction() const {}", 6736 getLLVMStyleWithColumns(47)); 6737 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 6738 " const {}", 6739 Style); 6740 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6741 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 6742 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6743 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 6744 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6745 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 6746 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 6747 " aaaaaaaaaaa aaaaa) const override;"); 6748 verifyGoogleFormat( 6749 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6750 " const override;"); 6751 6752 // Even if the first parameter has to be wrapped. 6753 verifyFormat("void someLongFunction(\n" 6754 " int someLongParameter) const {}", 6755 getLLVMStyleWithColumns(46)); 6756 verifyFormat("void someLongFunction(\n" 6757 " int someLongParameter) const {}", 6758 Style); 6759 verifyFormat("void someLongFunction(\n" 6760 " int someLongParameter) override {}", 6761 Style); 6762 verifyFormat("void someLongFunction(\n" 6763 " int someLongParameter) OVERRIDE {}", 6764 Style); 6765 verifyFormat("void someLongFunction(\n" 6766 " int someLongParameter) final {}", 6767 Style); 6768 verifyFormat("void someLongFunction(\n" 6769 " int someLongParameter) FINAL {}", 6770 Style); 6771 verifyFormat("void someLongFunction(\n" 6772 " int parameter) const override {}", 6773 Style); 6774 6775 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 6776 verifyFormat("void someLongFunction(\n" 6777 " int someLongParameter) const\n" 6778 "{\n" 6779 "}", 6780 Style); 6781 6782 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 6783 verifyFormat("void someLongFunction(\n" 6784 " int someLongParameter) const\n" 6785 " {\n" 6786 " }", 6787 Style); 6788 6789 // Unless these are unknown annotations. 6790 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 6791 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6792 " LONG_AND_UGLY_ANNOTATION;"); 6793 6794 // Breaking before function-like trailing annotations is fine to keep them 6795 // close to their arguments. 6796 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6797 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6798 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6799 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6800 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6801 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 6802 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 6803 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 6804 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 6805 6806 verifyFormat( 6807 "void aaaaaaaaaaaaaaaaaa()\n" 6808 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 6809 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 6810 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6811 " __attribute__((unused));"); 6812 verifyGoogleFormat( 6813 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6814 " GUARDED_BY(aaaaaaaaaaaa);"); 6815 verifyGoogleFormat( 6816 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6817 " GUARDED_BY(aaaaaaaaaaaa);"); 6818 verifyGoogleFormat( 6819 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6820 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6821 verifyGoogleFormat( 6822 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6823 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6824 } 6825 6826 TEST_F(FormatTest, FunctionAnnotations) { 6827 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6828 "int OldFunction(const string ¶meter) {}"); 6829 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6830 "string OldFunction(const string ¶meter) {}"); 6831 verifyFormat("template <typename T>\n" 6832 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6833 "string OldFunction(const string ¶meter) {}"); 6834 6835 // Not function annotations. 6836 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6837 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 6838 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 6839 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 6840 verifyFormat("MACRO(abc).function() // wrap\n" 6841 " << abc;"); 6842 verifyFormat("MACRO(abc)->function() // wrap\n" 6843 " << abc;"); 6844 verifyFormat("MACRO(abc)::function() // wrap\n" 6845 " << abc;"); 6846 } 6847 6848 TEST_F(FormatTest, BreaksDesireably) { 6849 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6850 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6851 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 6852 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 6854 "}"); 6855 6856 verifyFormat( 6857 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6859 6860 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6863 6864 verifyFormat( 6865 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6866 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6868 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6869 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 6870 6871 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6872 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6873 6874 verifyFormat( 6875 "void f() {\n" 6876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6878 "}"); 6879 verifyFormat( 6880 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6881 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6882 verifyFormat( 6883 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6884 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6885 verifyFormat( 6886 "aaaaaa(aaa,\n" 6887 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6889 " aaaa);"); 6890 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6892 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6893 6894 // Indent consistently independent of call expression and unary operator. 6895 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6896 " dddddddddddddddddddddddddddddd));"); 6897 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6898 " dddddddddddddddddddddddddddddd));"); 6899 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 6900 " dddddddddddddddddddddddddddddd));"); 6901 6902 // This test case breaks on an incorrect memoization, i.e. an optimization not 6903 // taking into account the StopAt value. 6904 verifyFormat( 6905 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6906 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6907 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6908 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6909 6910 verifyFormat("{\n {\n {\n" 6911 " Annotation.SpaceRequiredBefore =\n" 6912 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 6913 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 6914 " }\n }\n}"); 6915 6916 // Break on an outer level if there was a break on an inner level. 6917 EXPECT_EQ("f(g(h(a, // comment\n" 6918 " b, c),\n" 6919 " d, e),\n" 6920 " x, y);", 6921 format("f(g(h(a, // comment\n" 6922 " b, c), d, e), x, y);")); 6923 6924 // Prefer breaking similar line breaks. 6925 verifyFormat( 6926 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 6927 " NSTrackingMouseEnteredAndExited |\n" 6928 " NSTrackingActiveAlways;"); 6929 } 6930 6931 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 6932 FormatStyle NoBinPacking = getGoogleStyle(); 6933 NoBinPacking.BinPackParameters = false; 6934 NoBinPacking.BinPackArguments = true; 6935 verifyFormat("void f() {\n" 6936 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 6937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6938 "}", 6939 NoBinPacking); 6940 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 6941 " int aaaaaaaaaaaaaaaaaaaa,\n" 6942 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6943 NoBinPacking); 6944 6945 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6946 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6947 " vector<int> bbbbbbbbbbbbbbb);", 6948 NoBinPacking); 6949 // FIXME: This behavior difference is probably not wanted. However, currently 6950 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 6951 // template arguments from BreakBeforeParameter being set because of the 6952 // one-per-line formatting. 6953 verifyFormat( 6954 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6955 " aaaaaaaaaa> aaaaaaaaaa);", 6956 NoBinPacking); 6957 verifyFormat( 6958 "void fffffffffff(\n" 6959 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 6960 " aaaaaaaaaa);"); 6961 } 6962 6963 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 6964 FormatStyle NoBinPacking = getGoogleStyle(); 6965 NoBinPacking.BinPackParameters = false; 6966 NoBinPacking.BinPackArguments = false; 6967 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 6968 " aaaaaaaaaaaaaaaaaaaa,\n" 6969 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 6970 NoBinPacking); 6971 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 6972 " aaaaaaaaaaaaa,\n" 6973 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 6974 NoBinPacking); 6975 verifyFormat( 6976 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6977 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6978 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6979 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6980 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 6981 NoBinPacking); 6982 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6983 " .aaaaaaaaaaaaaaaaaa();", 6984 NoBinPacking); 6985 verifyFormat("void f() {\n" 6986 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6987 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 6988 "}", 6989 NoBinPacking); 6990 6991 verifyFormat( 6992 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6993 " aaaaaaaaaaaa,\n" 6994 " aaaaaaaaaaaa);", 6995 NoBinPacking); 6996 verifyFormat( 6997 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 6998 " ddddddddddddddddddddddddddddd),\n" 6999 " test);", 7000 NoBinPacking); 7001 7002 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7003 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 7004 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 7005 " aaaaaaaaaaaaaaaaaa;", 7006 NoBinPacking); 7007 verifyFormat("a(\"a\"\n" 7008 " \"a\",\n" 7009 " a);"); 7010 7011 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7012 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7013 " aaaaaaaaa,\n" 7014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7015 NoBinPacking); 7016 verifyFormat( 7017 "void f() {\n" 7018 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7019 " .aaaaaaa();\n" 7020 "}", 7021 NoBinPacking); 7022 verifyFormat( 7023 "template <class SomeType, class SomeOtherType>\n" 7024 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7025 NoBinPacking); 7026 } 7027 7028 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7029 FormatStyle Style = getLLVMStyleWithColumns(15); 7030 Style.ExperimentalAutoDetectBinPacking = true; 7031 EXPECT_EQ("aaa(aaaa,\n" 7032 " aaaa,\n" 7033 " aaaa);\n" 7034 "aaa(aaaa,\n" 7035 " aaaa,\n" 7036 " aaaa);", 7037 format("aaa(aaaa,\n" // one-per-line 7038 " aaaa,\n" 7039 " aaaa );\n" 7040 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7041 Style)); 7042 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7043 " aaaa);\n" 7044 "aaa(aaaa, aaaa,\n" 7045 " aaaa);", 7046 format("aaa(aaaa, aaaa,\n" // bin-packed 7047 " aaaa );\n" 7048 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7049 Style)); 7050 } 7051 7052 TEST_F(FormatTest, FormatsBuilderPattern) { 7053 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7054 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7055 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7056 " .StartsWith(\".init\", ORDER_INIT)\n" 7057 " .StartsWith(\".fini\", ORDER_FINI)\n" 7058 " .StartsWith(\".hash\", ORDER_HASH)\n" 7059 " .Default(ORDER_TEXT);\n"); 7060 7061 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7062 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7063 verifyFormat("aaaaaaa->aaaaaaa\n" 7064 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7066 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7067 verifyFormat( 7068 "aaaaaaa->aaaaaaa\n" 7069 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7070 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7071 verifyFormat( 7072 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7073 " aaaaaaaaaaaaaa);"); 7074 verifyFormat( 7075 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7076 " aaaaaa->aaaaaaaaaaaa()\n" 7077 " ->aaaaaaaaaaaaaaaa(\n" 7078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7079 " ->aaaaaaaaaaaaaaaaa();"); 7080 verifyGoogleFormat( 7081 "void f() {\n" 7082 " someo->Add((new util::filetools::Handler(dir))\n" 7083 " ->OnEvent1(NewPermanentCallback(\n" 7084 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7085 " ->OnEvent2(NewPermanentCallback(\n" 7086 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7087 " ->OnEvent3(NewPermanentCallback(\n" 7088 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7089 " ->OnEvent5(NewPermanentCallback(\n" 7090 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7091 " ->OnEvent6(NewPermanentCallback(\n" 7092 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7093 "}"); 7094 7095 verifyFormat( 7096 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7097 verifyFormat("aaaaaaaaaaaaaaa()\n" 7098 " .aaaaaaaaaaaaaaa()\n" 7099 " .aaaaaaaaaaaaaaa()\n" 7100 " .aaaaaaaaaaaaaaa()\n" 7101 " .aaaaaaaaaaaaaaa();"); 7102 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7103 " .aaaaaaaaaaaaaaa()\n" 7104 " .aaaaaaaaaaaaaaa()\n" 7105 " .aaaaaaaaaaaaaaa();"); 7106 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7107 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7108 " .aaaaaaaaaaaaaaa();"); 7109 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7110 " ->aaaaaaaaaaaaaae(0)\n" 7111 " ->aaaaaaaaaaaaaaa();"); 7112 7113 // Don't linewrap after very short segments. 7114 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7115 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7116 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7117 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7118 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7119 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7120 verifyFormat("aaa()\n" 7121 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7122 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7123 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7124 7125 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7126 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7127 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7128 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7129 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7131 7132 // Prefer not to break after empty parentheses. 7133 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7134 " First->LastNewlineOffset);"); 7135 7136 // Prefer not to create "hanging" indents. 7137 verifyFormat( 7138 "return !soooooooooooooome_map\n" 7139 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7140 " .second;"); 7141 verifyFormat( 7142 "return aaaaaaaaaaaaaaaa\n" 7143 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7144 " .aaaa(aaaaaaaaaaaaaa);"); 7145 // No hanging indent here. 7146 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7148 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7149 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7150 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7151 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7152 getLLVMStyleWithColumns(60)); 7153 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7154 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7155 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7156 getLLVMStyleWithColumns(59)); 7157 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7159 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7160 7161 // Dont break if only closing statements before member call 7162 verifyFormat("test() {\n" 7163 " ([]() -> {\n" 7164 " int b = 32;\n" 7165 " return 3;\n" 7166 " }).foo();\n" 7167 "}"); 7168 verifyFormat("test() {\n" 7169 " (\n" 7170 " []() -> {\n" 7171 " int b = 32;\n" 7172 " return 3;\n" 7173 " },\n" 7174 " foo, bar)\n" 7175 " .foo();\n" 7176 "}"); 7177 verifyFormat("test() {\n" 7178 " ([]() -> {\n" 7179 " int b = 32;\n" 7180 " return 3;\n" 7181 " })\n" 7182 " .foo()\n" 7183 " .bar();\n" 7184 "}"); 7185 verifyFormat("test() {\n" 7186 " ([]() -> {\n" 7187 " int b = 32;\n" 7188 " return 3;\n" 7189 " })\n" 7190 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7191 " \"bbbb\");\n" 7192 "}", 7193 getLLVMStyleWithColumns(30)); 7194 } 7195 7196 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7197 verifyFormat( 7198 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7199 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7200 verifyFormat( 7201 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7202 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7203 7204 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7205 " ccccccccccccccccccccccccc) {\n}"); 7206 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7207 " ccccccccccccccccccccccccc) {\n}"); 7208 7209 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7210 " ccccccccccccccccccccccccc) {\n}"); 7211 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7212 " ccccccccccccccccccccccccc) {\n}"); 7213 7214 verifyFormat( 7215 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7216 " ccccccccccccccccccccccccc) {\n}"); 7217 verifyFormat( 7218 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7219 " ccccccccccccccccccccccccc) {\n}"); 7220 7221 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7222 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7223 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7224 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7225 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7226 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7227 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7228 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7229 7230 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7231 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7232 " aaaaaaaaaaaaaaa != aa) {\n}"); 7233 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7234 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7235 " aaaaaaaaaaaaaaa != aa) {\n}"); 7236 } 7237 7238 TEST_F(FormatTest, BreaksAfterAssignments) { 7239 verifyFormat( 7240 "unsigned Cost =\n" 7241 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7242 " SI->getPointerAddressSpaceee());\n"); 7243 verifyFormat( 7244 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7245 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7246 7247 verifyFormat( 7248 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7249 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7250 verifyFormat("unsigned OriginalStartColumn =\n" 7251 " SourceMgr.getSpellingColumnNumber(\n" 7252 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7253 " 1;"); 7254 } 7255 7256 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7257 FormatStyle Style = getLLVMStyle(); 7258 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7259 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7260 Style); 7261 7262 Style.PenaltyBreakAssignment = 20; 7263 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7264 " cccccccccccccccccccccccccc;", 7265 Style); 7266 } 7267 7268 TEST_F(FormatTest, AlignsAfterAssignments) { 7269 verifyFormat( 7270 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7271 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7272 verifyFormat( 7273 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7274 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7275 verifyFormat( 7276 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7277 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7278 verifyFormat( 7279 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7280 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7281 verifyFormat( 7282 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7283 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7284 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7285 } 7286 7287 TEST_F(FormatTest, AlignsAfterReturn) { 7288 verifyFormat( 7289 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7290 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7291 verifyFormat( 7292 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7293 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7294 verifyFormat( 7295 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7296 " aaaaaaaaaaaaaaaaaaaaaa();"); 7297 verifyFormat( 7298 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7299 " aaaaaaaaaaaaaaaaaaaaaa());"); 7300 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7301 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7302 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7305 verifyFormat("return\n" 7306 " // true if code is one of a or b.\n" 7307 " code == a || code == b;"); 7308 } 7309 7310 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7311 verifyFormat( 7312 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7313 " aaaaaaaaa aaaaaaa) {}"); 7314 verifyFormat( 7315 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7316 " aaaaaaaaaaa aaaaaaaaa);"); 7317 verifyFormat( 7318 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7319 " aaaaaaaaaaaaaaaaaaaaa));"); 7320 FormatStyle Style = getLLVMStyle(); 7321 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7322 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7323 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7324 Style); 7325 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7326 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7327 Style); 7328 verifyFormat("SomeLongVariableName->someFunction(\n" 7329 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7330 Style); 7331 verifyFormat( 7332 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7333 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7334 Style); 7335 verifyFormat( 7336 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7337 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7338 Style); 7339 verifyFormat( 7340 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7341 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7342 Style); 7343 7344 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7345 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7346 " b));", 7347 Style); 7348 7349 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7350 Style.BinPackArguments = false; 7351 Style.BinPackParameters = false; 7352 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7353 " aaaaaaaaaaa aaaaaaaa,\n" 7354 " aaaaaaaaa aaaaaaa,\n" 7355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7356 Style); 7357 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7358 " aaaaaaaaaaa aaaaaaaaa,\n" 7359 " aaaaaaaaaaa aaaaaaaaa,\n" 7360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7361 Style); 7362 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7363 " aaaaaaaaaaaaaaa,\n" 7364 " aaaaaaaaaaaaaaaaaaaaa,\n" 7365 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7366 Style); 7367 verifyFormat( 7368 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7369 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7370 Style); 7371 verifyFormat( 7372 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7373 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7374 Style); 7375 verifyFormat( 7376 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7377 " aaaaaaaaaaaaaaaaaaaaa(\n" 7378 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7379 " aaaaaaaaaaaaaaaa);", 7380 Style); 7381 verifyFormat( 7382 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7383 " aaaaaaaaaaaaaaaaaaaaa(\n" 7384 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7385 " aaaaaaaaaaaaaaaa);", 7386 Style); 7387 } 7388 7389 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7390 FormatStyle Style = getLLVMStyleWithColumns(40); 7391 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7392 " bbbbbbbbbbbbbbbbbbbbbb);", 7393 Style); 7394 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7395 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7396 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7397 " bbbbbbbbbbbbbbbbbbbbbb);", 7398 Style); 7399 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7400 Style.AlignOperands = FormatStyle::OAS_Align; 7401 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7402 " bbbbbbbbbbbbbbbbbbbbbb);", 7403 Style); 7404 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7405 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7406 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7407 " bbbbbbbbbbbbbbbbbbbbbb);", 7408 Style); 7409 } 7410 7411 TEST_F(FormatTest, BreaksConditionalExpressions) { 7412 verifyFormat( 7413 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7414 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7415 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7416 verifyFormat( 7417 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7418 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7419 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7420 verifyFormat( 7421 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7422 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7423 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7424 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7425 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7426 verifyFormat( 7427 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7428 " : aaaaaaaaaaaaa);"); 7429 verifyFormat( 7430 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7431 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7432 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7433 " aaaaaaaaaaaaa);"); 7434 verifyFormat( 7435 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7436 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7437 " aaaaaaaaaaaaa);"); 7438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7439 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7441 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7443 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7445 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7447 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7449 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7450 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7452 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7454 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7455 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7456 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7457 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7458 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7460 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7461 " : aaaaaaaaaaaaaaaa;"); 7462 verifyFormat( 7463 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7464 " ? aaaaaaaaaaaaaaa\n" 7465 " : aaaaaaaaaaaaaaa;"); 7466 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7467 " aaaaaaaaa\n" 7468 " ? b\n" 7469 " : c);"); 7470 verifyFormat("return aaaa == bbbb\n" 7471 " // comment\n" 7472 " ? aaaa\n" 7473 " : bbbb;"); 7474 verifyFormat("unsigned Indent =\n" 7475 " format(TheLine.First,\n" 7476 " IndentForLevel[TheLine.Level] >= 0\n" 7477 " ? IndentForLevel[TheLine.Level]\n" 7478 " : TheLine * 2,\n" 7479 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7480 getLLVMStyleWithColumns(60)); 7481 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7482 " ? aaaaaaaaaaaaaaa\n" 7483 " : bbbbbbbbbbbbbbb //\n" 7484 " ? ccccccccccccccc\n" 7485 " : ddddddddddddddd;"); 7486 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7487 " ? aaaaaaaaaaaaaaa\n" 7488 " : (bbbbbbbbbbbbbbb //\n" 7489 " ? ccccccccccccccc\n" 7490 " : ddddddddddddddd);"); 7491 verifyFormat( 7492 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7493 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7494 " aaaaaaaaaaaaaaaaaaaaa +\n" 7495 " aaaaaaaaaaaaaaaaaaaaa\n" 7496 " : aaaaaaaaaa;"); 7497 verifyFormat( 7498 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7499 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7500 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7501 7502 FormatStyle NoBinPacking = getLLVMStyle(); 7503 NoBinPacking.BinPackArguments = false; 7504 verifyFormat( 7505 "void f() {\n" 7506 " g(aaa,\n" 7507 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7509 " ? aaaaaaaaaaaaaaa\n" 7510 " : aaaaaaaaaaaaaaa);\n" 7511 "}", 7512 NoBinPacking); 7513 verifyFormat( 7514 "void f() {\n" 7515 " g(aaa,\n" 7516 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7518 " ?: aaaaaaaaaaaaaaa);\n" 7519 "}", 7520 NoBinPacking); 7521 7522 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7523 " // comment.\n" 7524 " ccccccccccccccccccccccccccccccccccccccc\n" 7525 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7526 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7527 7528 // Assignments in conditional expressions. Apparently not uncommon :-(. 7529 verifyFormat("return a != b\n" 7530 " // comment\n" 7531 " ? a = b\n" 7532 " : a = b;"); 7533 verifyFormat("return a != b\n" 7534 " // comment\n" 7535 " ? a = a != b\n" 7536 " // comment\n" 7537 " ? a = b\n" 7538 " : a\n" 7539 " : a;\n"); 7540 verifyFormat("return a != b\n" 7541 " // comment\n" 7542 " ? a\n" 7543 " : a = a != b\n" 7544 " // comment\n" 7545 " ? a = b\n" 7546 " : a;"); 7547 7548 // Chained conditionals 7549 FormatStyle Style = getLLVMStyle(); 7550 Style.ColumnLimit = 70; 7551 Style.AlignOperands = FormatStyle::OAS_Align; 7552 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7553 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7554 " : 3333333333333333;", 7555 Style); 7556 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7557 " : bbbbbbbbbb ? 2222222222222222\n" 7558 " : 3333333333333333;", 7559 Style); 7560 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7561 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7562 " : 3333333333333333;", 7563 Style); 7564 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7565 " : bbbbbbbbbbbbbb ? 222222\n" 7566 " : 333333;", 7567 Style); 7568 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7569 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7570 " : cccccccccccccc ? 3333333333333333\n" 7571 " : 4444444444444444;", 7572 Style); 7573 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7574 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7575 " : 3333333333333333;", 7576 Style); 7577 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7578 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7579 " : (aaa ? bbb : ccc);", 7580 Style); 7581 verifyFormat( 7582 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7583 " : cccccccccccccccccc)\n" 7584 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7585 " : 3333333333333333;", 7586 Style); 7587 verifyFormat( 7588 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7589 " : cccccccccccccccccc)\n" 7590 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7591 " : 3333333333333333;", 7592 Style); 7593 verifyFormat( 7594 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7595 " : dddddddddddddddddd)\n" 7596 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7597 " : 3333333333333333;", 7598 Style); 7599 verifyFormat( 7600 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7601 " : dddddddddddddddddd)\n" 7602 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7603 " : 3333333333333333;", 7604 Style); 7605 verifyFormat( 7606 "return aaaaaaaaa ? 1111111111111111\n" 7607 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7608 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7609 " : dddddddddddddddddd)\n", 7610 Style); 7611 verifyFormat( 7612 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7613 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7614 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7615 " : cccccccccccccccccc);", 7616 Style); 7617 verifyFormat( 7618 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7619 " : ccccccccccccccc ? dddddddddddddddddd\n" 7620 " : eeeeeeeeeeeeeeeeee)\n" 7621 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7622 " : 3333333333333333;", 7623 Style); 7624 verifyFormat( 7625 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7626 " : ccccccccccccccc ? dddddddddddddddddd\n" 7627 " : eeeeeeeeeeeeeeeeee)\n" 7628 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7629 " : 3333333333333333;", 7630 Style); 7631 verifyFormat( 7632 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7633 " : cccccccccccc ? dddddddddddddddddd\n" 7634 " : eeeeeeeeeeeeeeeeee)\n" 7635 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7636 " : 3333333333333333;", 7637 Style); 7638 verifyFormat( 7639 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7640 " : cccccccccccccccccc\n" 7641 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7642 " : 3333333333333333;", 7643 Style); 7644 verifyFormat( 7645 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7646 " : cccccccccccccccc ? dddddddddddddddddd\n" 7647 " : eeeeeeeeeeeeeeeeee\n" 7648 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7649 " : 3333333333333333;", 7650 Style); 7651 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 7652 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7653 " : cccccccccccccccccc ? dddddddddddddddddd\n" 7654 " : eeeeeeeeeeeeeeeeee)\n" 7655 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7656 " : 3333333333333333;", 7657 Style); 7658 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7659 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7660 " : cccccccccccccccc ? dddddddddddddddddd\n" 7661 " : eeeeeeeeeeeeeeeeee\n" 7662 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7663 " : 3333333333333333;", 7664 Style); 7665 7666 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7667 Style.BreakBeforeTernaryOperators = false; 7668 // FIXME: Aligning the question marks is weird given DontAlign. 7669 // Consider disabling this alignment in this case. Also check whether this 7670 // will render the adjustment from https://reviews.llvm.org/D82199 7671 // unnecessary. 7672 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 7673 " bbbb ? cccccccccccccccccc :\n" 7674 " ddddd;\n", 7675 Style); 7676 7677 EXPECT_EQ( 7678 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7679 " /*\n" 7680 " */\n" 7681 " function() {\n" 7682 " try {\n" 7683 " return JJJJJJJJJJJJJJ(\n" 7684 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7685 " }\n" 7686 " } :\n" 7687 " function() {};", 7688 format( 7689 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7690 " /*\n" 7691 " */\n" 7692 " function() {\n" 7693 " try {\n" 7694 " return JJJJJJJJJJJJJJ(\n" 7695 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7696 " }\n" 7697 " } :\n" 7698 " function() {};", 7699 getGoogleStyle(FormatStyle::LK_JavaScript))); 7700 } 7701 7702 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 7703 FormatStyle Style = getLLVMStyle(); 7704 Style.BreakBeforeTernaryOperators = false; 7705 Style.ColumnLimit = 70; 7706 verifyFormat( 7707 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7710 Style); 7711 verifyFormat( 7712 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7713 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7715 Style); 7716 verifyFormat( 7717 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7719 Style); 7720 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 7721 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7723 Style); 7724 verifyFormat( 7725 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 7726 " aaaaaaaaaaaaa);", 7727 Style); 7728 verifyFormat( 7729 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7730 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7731 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7732 " aaaaaaaaaaaaa);", 7733 Style); 7734 verifyFormat( 7735 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7736 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7737 " aaaaaaaaaaaaa);", 7738 Style); 7739 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7744 Style); 7745 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7746 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7747 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7748 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7751 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7752 Style); 7753 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 7755 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7757 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7758 Style); 7759 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7760 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7761 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7762 Style); 7763 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7767 Style); 7768 verifyFormat( 7769 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7770 " aaaaaaaaaaaaaaa :\n" 7771 " aaaaaaaaaaaaaaa;", 7772 Style); 7773 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7774 " aaaaaaaaa ?\n" 7775 " b :\n" 7776 " c);", 7777 Style); 7778 verifyFormat("unsigned Indent =\n" 7779 " format(TheLine.First,\n" 7780 " IndentForLevel[TheLine.Level] >= 0 ?\n" 7781 " IndentForLevel[TheLine.Level] :\n" 7782 " TheLine * 2,\n" 7783 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7784 Style); 7785 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7786 " aaaaaaaaaaaaaaa :\n" 7787 " bbbbbbbbbbbbbbb ? //\n" 7788 " ccccccccccccccc :\n" 7789 " ddddddddddddddd;", 7790 Style); 7791 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7792 " aaaaaaaaaaaaaaa :\n" 7793 " (bbbbbbbbbbbbbbb ? //\n" 7794 " ccccccccccccccc :\n" 7795 " ddddddddddddddd);", 7796 Style); 7797 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7798 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 7799 " ccccccccccccccccccccccccccc;", 7800 Style); 7801 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7802 " aaaaa :\n" 7803 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 7804 Style); 7805 7806 // Chained conditionals 7807 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7808 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7809 " 3333333333333333;", 7810 Style); 7811 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7812 " bbbbbbbbbb ? 2222222222222222 :\n" 7813 " 3333333333333333;", 7814 Style); 7815 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 7816 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7817 " 3333333333333333;", 7818 Style); 7819 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7820 " bbbbbbbbbbbbbbbb ? 222222 :\n" 7821 " 333333;", 7822 Style); 7823 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7824 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7825 " cccccccccccccccc ? 3333333333333333 :\n" 7826 " 4444444444444444;", 7827 Style); 7828 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 7829 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7830 " 3333333333333333;", 7831 Style); 7832 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7833 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7834 " (aaa ? bbb : ccc);", 7835 Style); 7836 verifyFormat( 7837 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7838 " cccccccccccccccccc) :\n" 7839 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7840 " 3333333333333333;", 7841 Style); 7842 verifyFormat( 7843 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7844 " cccccccccccccccccc) :\n" 7845 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7846 " 3333333333333333;", 7847 Style); 7848 verifyFormat( 7849 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7850 " dddddddddddddddddd) :\n" 7851 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7852 " 3333333333333333;", 7853 Style); 7854 verifyFormat( 7855 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7856 " dddddddddddddddddd) :\n" 7857 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7858 " 3333333333333333;", 7859 Style); 7860 verifyFormat( 7861 "return aaaaaaaaa ? 1111111111111111 :\n" 7862 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7863 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7864 " dddddddddddddddddd)\n", 7865 Style); 7866 verifyFormat( 7867 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7868 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7869 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7870 " cccccccccccccccccc);", 7871 Style); 7872 verifyFormat( 7873 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7874 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7875 " eeeeeeeeeeeeeeeeee) :\n" 7876 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7877 " 3333333333333333;", 7878 Style); 7879 verifyFormat( 7880 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7881 " ccccccccccccc ? dddddddddddddddddd :\n" 7882 " eeeeeeeeeeeeeeeeee) :\n" 7883 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7884 " 3333333333333333;", 7885 Style); 7886 verifyFormat( 7887 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7888 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7889 " eeeeeeeeeeeeeeeeee) :\n" 7890 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7891 " 3333333333333333;", 7892 Style); 7893 verifyFormat( 7894 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7895 " cccccccccccccccccc :\n" 7896 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7897 " 3333333333333333;", 7898 Style); 7899 verifyFormat( 7900 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7901 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7902 " eeeeeeeeeeeeeeeeee :\n" 7903 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7904 " 3333333333333333;", 7905 Style); 7906 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7907 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7908 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7909 " eeeeeeeeeeeeeeeeee) :\n" 7910 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7911 " 3333333333333333;", 7912 Style); 7913 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7914 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7915 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 7916 " eeeeeeeeeeeeeeeeee :\n" 7917 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7918 " 3333333333333333;", 7919 Style); 7920 } 7921 7922 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 7923 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 7924 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 7925 verifyFormat("bool a = true, b = false;"); 7926 7927 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 7929 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 7930 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 7931 verifyFormat( 7932 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 7933 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 7934 " d = e && f;"); 7935 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 7936 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 7937 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7938 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 7939 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 7940 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 7941 7942 FormatStyle Style = getGoogleStyle(); 7943 Style.PointerAlignment = FormatStyle::PAS_Left; 7944 Style.DerivePointerAlignment = false; 7945 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7946 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 7947 " *b = bbbbbbbbbbbbbbbbbbb;", 7948 Style); 7949 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7950 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 7951 Style); 7952 verifyFormat("vector<int*> a, b;", Style); 7953 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 7954 } 7955 7956 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 7957 verifyFormat("arr[foo ? bar : baz];"); 7958 verifyFormat("f()[foo ? bar : baz];"); 7959 verifyFormat("(a + b)[foo ? bar : baz];"); 7960 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 7961 } 7962 7963 TEST_F(FormatTest, AlignsStringLiterals) { 7964 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 7965 " \"short literal\");"); 7966 verifyFormat( 7967 "looooooooooooooooooooooooongFunction(\n" 7968 " \"short literal\"\n" 7969 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 7970 verifyFormat("someFunction(\"Always break between multi-line\"\n" 7971 " \" string literals\",\n" 7972 " and, other, parameters);"); 7973 EXPECT_EQ("fun + \"1243\" /* comment */\n" 7974 " \"5678\";", 7975 format("fun + \"1243\" /* comment */\n" 7976 " \"5678\";", 7977 getLLVMStyleWithColumns(28))); 7978 EXPECT_EQ( 7979 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7980 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 7981 " \"aaaaaaaaaaaaaaaa\";", 7982 format("aaaaaa =" 7983 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7984 "aaaaaaaaaaaaaaaaaaaaa\" " 7985 "\"aaaaaaaaaaaaaaaa\";")); 7986 verifyFormat("a = a + \"a\"\n" 7987 " \"a\"\n" 7988 " \"a\";"); 7989 verifyFormat("f(\"a\", \"b\"\n" 7990 " \"c\");"); 7991 7992 verifyFormat( 7993 "#define LL_FORMAT \"ll\"\n" 7994 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 7995 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 7996 7997 verifyFormat("#define A(X) \\\n" 7998 " \"aaaaa\" #X \"bbbbbb\" \\\n" 7999 " \"ccccc\"", 8000 getLLVMStyleWithColumns(23)); 8001 verifyFormat("#define A \"def\"\n" 8002 "f(\"abc\" A \"ghi\"\n" 8003 " \"jkl\");"); 8004 8005 verifyFormat("f(L\"a\"\n" 8006 " L\"b\");"); 8007 verifyFormat("#define A(X) \\\n" 8008 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 8009 " L\"ccccc\"", 8010 getLLVMStyleWithColumns(25)); 8011 8012 verifyFormat("f(@\"a\"\n" 8013 " @\"b\");"); 8014 verifyFormat("NSString s = @\"a\"\n" 8015 " @\"b\"\n" 8016 " @\"c\";"); 8017 verifyFormat("NSString s = @\"a\"\n" 8018 " \"b\"\n" 8019 " \"c\";"); 8020 } 8021 8022 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8023 FormatStyle Style = getLLVMStyle(); 8024 // No declarations or definitions should be moved to own line. 8025 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8026 verifyFormat("class A {\n" 8027 " int f() { return 1; }\n" 8028 " int g();\n" 8029 "};\n" 8030 "int f() { return 1; }\n" 8031 "int g();\n", 8032 Style); 8033 8034 // All declarations and definitions should have the return type moved to its 8035 // own line. 8036 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8037 Style.TypenameMacros = {"LIST"}; 8038 verifyFormat("SomeType\n" 8039 "funcdecl(LIST(uint64_t));", 8040 Style); 8041 verifyFormat("class E {\n" 8042 " int\n" 8043 " f() {\n" 8044 " return 1;\n" 8045 " }\n" 8046 " int\n" 8047 " g();\n" 8048 "};\n" 8049 "int\n" 8050 "f() {\n" 8051 " return 1;\n" 8052 "}\n" 8053 "int\n" 8054 "g();\n", 8055 Style); 8056 8057 // Top-level definitions, and no kinds of declarations should have the 8058 // return type moved to its own line. 8059 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8060 verifyFormat("class B {\n" 8061 " int f() { return 1; }\n" 8062 " int g();\n" 8063 "};\n" 8064 "int\n" 8065 "f() {\n" 8066 " return 1;\n" 8067 "}\n" 8068 "int g();\n", 8069 Style); 8070 8071 // Top-level definitions and declarations should have the return type moved 8072 // to its own line. 8073 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8074 verifyFormat("class C {\n" 8075 " int f() { return 1; }\n" 8076 " int g();\n" 8077 "};\n" 8078 "int\n" 8079 "f() {\n" 8080 " return 1;\n" 8081 "}\n" 8082 "int\n" 8083 "g();\n", 8084 Style); 8085 8086 // All definitions should have the return type moved to its own line, but no 8087 // kinds of declarations. 8088 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8089 verifyFormat("class D {\n" 8090 " int\n" 8091 " f() {\n" 8092 " return 1;\n" 8093 " }\n" 8094 " int g();\n" 8095 "};\n" 8096 "int\n" 8097 "f() {\n" 8098 " return 1;\n" 8099 "}\n" 8100 "int g();\n", 8101 Style); 8102 verifyFormat("const char *\n" 8103 "f(void) {\n" // Break here. 8104 " return \"\";\n" 8105 "}\n" 8106 "const char *bar(void);\n", // No break here. 8107 Style); 8108 verifyFormat("template <class T>\n" 8109 "T *\n" 8110 "f(T &c) {\n" // Break here. 8111 " return NULL;\n" 8112 "}\n" 8113 "template <class T> T *f(T &c);\n", // No break here. 8114 Style); 8115 verifyFormat("class C {\n" 8116 " int\n" 8117 " operator+() {\n" 8118 " return 1;\n" 8119 " }\n" 8120 " int\n" 8121 " operator()() {\n" 8122 " return 1;\n" 8123 " }\n" 8124 "};\n", 8125 Style); 8126 verifyFormat("void\n" 8127 "A::operator()() {}\n" 8128 "void\n" 8129 "A::operator>>() {}\n" 8130 "void\n" 8131 "A::operator+() {}\n" 8132 "void\n" 8133 "A::operator*() {}\n" 8134 "void\n" 8135 "A::operator->() {}\n" 8136 "void\n" 8137 "A::operator void *() {}\n" 8138 "void\n" 8139 "A::operator void &() {}\n" 8140 "void\n" 8141 "A::operator void &&() {}\n" 8142 "void\n" 8143 "A::operator char *() {}\n" 8144 "void\n" 8145 "A::operator[]() {}\n" 8146 "void\n" 8147 "A::operator!() {}\n" 8148 "void\n" 8149 "A::operator**() {}\n" 8150 "void\n" 8151 "A::operator<Foo> *() {}\n" 8152 "void\n" 8153 "A::operator<Foo> **() {}\n" 8154 "void\n" 8155 "A::operator<Foo> &() {}\n" 8156 "void\n" 8157 "A::operator void **() {}\n", 8158 Style); 8159 verifyFormat("constexpr auto\n" 8160 "operator()() const -> reference {}\n" 8161 "constexpr auto\n" 8162 "operator>>() const -> reference {}\n" 8163 "constexpr auto\n" 8164 "operator+() const -> reference {}\n" 8165 "constexpr auto\n" 8166 "operator*() const -> reference {}\n" 8167 "constexpr auto\n" 8168 "operator->() const -> reference {}\n" 8169 "constexpr auto\n" 8170 "operator++() const -> reference {}\n" 8171 "constexpr auto\n" 8172 "operator void *() const -> reference {}\n" 8173 "constexpr auto\n" 8174 "operator void **() const -> reference {}\n" 8175 "constexpr auto\n" 8176 "operator void *() const -> reference {}\n" 8177 "constexpr auto\n" 8178 "operator void &() const -> reference {}\n" 8179 "constexpr auto\n" 8180 "operator void &&() const -> reference {}\n" 8181 "constexpr auto\n" 8182 "operator char *() const -> reference {}\n" 8183 "constexpr auto\n" 8184 "operator!() const -> reference {}\n" 8185 "constexpr auto\n" 8186 "operator[]() const -> reference {}\n", 8187 Style); 8188 verifyFormat("void *operator new(std::size_t s);", // No break here. 8189 Style); 8190 verifyFormat("void *\n" 8191 "operator new(std::size_t s) {}", 8192 Style); 8193 verifyFormat("void *\n" 8194 "operator delete[](void *ptr) {}", 8195 Style); 8196 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8197 verifyFormat("const char *\n" 8198 "f(void)\n" // Break here. 8199 "{\n" 8200 " return \"\";\n" 8201 "}\n" 8202 "const char *bar(void);\n", // No break here. 8203 Style); 8204 verifyFormat("template <class T>\n" 8205 "T *\n" // Problem here: no line break 8206 "f(T &c)\n" // Break here. 8207 "{\n" 8208 " return NULL;\n" 8209 "}\n" 8210 "template <class T> T *f(T &c);\n", // No break here. 8211 Style); 8212 verifyFormat("int\n" 8213 "foo(A<bool> a)\n" 8214 "{\n" 8215 " return a;\n" 8216 "}\n", 8217 Style); 8218 verifyFormat("int\n" 8219 "foo(A<8> a)\n" 8220 "{\n" 8221 " return a;\n" 8222 "}\n", 8223 Style); 8224 verifyFormat("int\n" 8225 "foo(A<B<bool>, 8> a)\n" 8226 "{\n" 8227 " return a;\n" 8228 "}\n", 8229 Style); 8230 verifyFormat("int\n" 8231 "foo(A<B<8>, bool> a)\n" 8232 "{\n" 8233 " return a;\n" 8234 "}\n", 8235 Style); 8236 verifyFormat("int\n" 8237 "foo(A<B<bool>, bool> a)\n" 8238 "{\n" 8239 " return a;\n" 8240 "}\n", 8241 Style); 8242 verifyFormat("int\n" 8243 "foo(A<B<8>, 8> a)\n" 8244 "{\n" 8245 " return a;\n" 8246 "}\n", 8247 Style); 8248 8249 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8250 Style.BraceWrapping.AfterFunction = true; 8251 verifyFormat("int f(i);\n" // No break here. 8252 "int\n" // Break here. 8253 "f(i)\n" 8254 "{\n" 8255 " return i + 1;\n" 8256 "}\n" 8257 "int\n" // Break here. 8258 "f(i)\n" 8259 "{\n" 8260 " return i + 1;\n" 8261 "};", 8262 Style); 8263 verifyFormat("int f(a, b, c);\n" // No break here. 8264 "int\n" // Break here. 8265 "f(a, b, c)\n" // Break here. 8266 "short a, b;\n" 8267 "float c;\n" 8268 "{\n" 8269 " return a + b < c;\n" 8270 "}\n" 8271 "int\n" // Break here. 8272 "f(a, b, c)\n" // Break here. 8273 "short a, b;\n" 8274 "float c;\n" 8275 "{\n" 8276 " return a + b < c;\n" 8277 "};", 8278 Style); 8279 verifyFormat("byte *\n" // Break here. 8280 "f(a)\n" // Break here. 8281 "byte a[];\n" 8282 "{\n" 8283 " return a;\n" 8284 "}", 8285 Style); 8286 verifyFormat("bool f(int a, int) override;\n" 8287 "Bar g(int a, Bar) final;\n" 8288 "Bar h(a, Bar) final;", 8289 Style); 8290 verifyFormat("int\n" 8291 "f(a)", 8292 Style); 8293 verifyFormat("bool\n" 8294 "f(size_t = 0, bool b = false)\n" 8295 "{\n" 8296 " return !b;\n" 8297 "}", 8298 Style); 8299 8300 // The return breaking style doesn't affect: 8301 // * function and object definitions with attribute-like macros 8302 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8303 " ABSL_GUARDED_BY(mutex) = {};", 8304 getGoogleStyleWithColumns(40)); 8305 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8306 " ABSL_GUARDED_BY(mutex); // comment", 8307 getGoogleStyleWithColumns(40)); 8308 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8309 " ABSL_GUARDED_BY(mutex1)\n" 8310 " ABSL_GUARDED_BY(mutex2);", 8311 getGoogleStyleWithColumns(40)); 8312 verifyFormat("Tttttt f(int a, int b)\n" 8313 " ABSL_GUARDED_BY(mutex1)\n" 8314 " ABSL_GUARDED_BY(mutex2);", 8315 getGoogleStyleWithColumns(40)); 8316 // * typedefs 8317 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8318 8319 Style = getGNUStyle(); 8320 8321 // Test for comments at the end of function declarations. 8322 verifyFormat("void\n" 8323 "foo (int a, /*abc*/ int b) // def\n" 8324 "{\n" 8325 "}\n", 8326 Style); 8327 8328 verifyFormat("void\n" 8329 "foo (int a, /* abc */ int b) /* def */\n" 8330 "{\n" 8331 "}\n", 8332 Style); 8333 8334 // Definitions that should not break after return type 8335 verifyFormat("void foo (int a, int b); // def\n", Style); 8336 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8337 verifyFormat("void foo (int a, int b);\n", Style); 8338 } 8339 8340 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8341 FormatStyle NoBreak = getLLVMStyle(); 8342 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8343 FormatStyle Break = getLLVMStyle(); 8344 Break.AlwaysBreakBeforeMultilineStrings = true; 8345 verifyFormat("aaaa = \"bbbb\"\n" 8346 " \"cccc\";", 8347 NoBreak); 8348 verifyFormat("aaaa =\n" 8349 " \"bbbb\"\n" 8350 " \"cccc\";", 8351 Break); 8352 verifyFormat("aaaa(\"bbbb\"\n" 8353 " \"cccc\");", 8354 NoBreak); 8355 verifyFormat("aaaa(\n" 8356 " \"bbbb\"\n" 8357 " \"cccc\");", 8358 Break); 8359 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8360 " \"cccc\");", 8361 NoBreak); 8362 verifyFormat("aaaa(qqq,\n" 8363 " \"bbbb\"\n" 8364 " \"cccc\");", 8365 Break); 8366 verifyFormat("aaaa(qqq,\n" 8367 " L\"bbbb\"\n" 8368 " L\"cccc\");", 8369 Break); 8370 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8371 " \"bbbb\"));", 8372 Break); 8373 verifyFormat("string s = someFunction(\n" 8374 " \"abc\"\n" 8375 " \"abc\");", 8376 Break); 8377 8378 // As we break before unary operators, breaking right after them is bad. 8379 verifyFormat("string foo = abc ? \"x\"\n" 8380 " \"blah blah blah blah blah blah\"\n" 8381 " : \"y\";", 8382 Break); 8383 8384 // Don't break if there is no column gain. 8385 verifyFormat("f(\"aaaa\"\n" 8386 " \"bbbb\");", 8387 Break); 8388 8389 // Treat literals with escaped newlines like multi-line string literals. 8390 EXPECT_EQ("x = \"a\\\n" 8391 "b\\\n" 8392 "c\";", 8393 format("x = \"a\\\n" 8394 "b\\\n" 8395 "c\";", 8396 NoBreak)); 8397 EXPECT_EQ("xxxx =\n" 8398 " \"a\\\n" 8399 "b\\\n" 8400 "c\";", 8401 format("xxxx = \"a\\\n" 8402 "b\\\n" 8403 "c\";", 8404 Break)); 8405 8406 EXPECT_EQ("NSString *const kString =\n" 8407 " @\"aaaa\"\n" 8408 " @\"bbbb\";", 8409 format("NSString *const kString = @\"aaaa\"\n" 8410 "@\"bbbb\";", 8411 Break)); 8412 8413 Break.ColumnLimit = 0; 8414 verifyFormat("const char *hello = \"hello llvm\";", Break); 8415 } 8416 8417 TEST_F(FormatTest, AlignsPipes) { 8418 verifyFormat( 8419 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8420 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8421 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8422 verifyFormat( 8423 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8424 " << aaaaaaaaaaaaaaaaaaaa;"); 8425 verifyFormat( 8426 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8427 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8428 verifyFormat( 8429 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8430 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8431 verifyFormat( 8432 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8433 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8434 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8435 verifyFormat( 8436 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8437 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8438 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8439 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8442 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8443 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8444 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8445 verifyFormat( 8446 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8448 verifyFormat( 8449 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8450 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8451 8452 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8453 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8454 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8456 " aaaaaaaaaaaaaaaaaaaaa)\n" 8457 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8458 verifyFormat("LOG_IF(aaa == //\n" 8459 " bbb)\n" 8460 " << a << b;"); 8461 8462 // But sometimes, breaking before the first "<<" is desirable. 8463 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8464 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8465 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8466 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8467 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8468 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8469 " << BEF << IsTemplate << Description << E->getType();"); 8470 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8471 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8473 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8474 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8475 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8476 " << aaa;"); 8477 8478 verifyFormat( 8479 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8480 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8481 8482 // Incomplete string literal. 8483 EXPECT_EQ("llvm::errs() << \"\n" 8484 " << a;", 8485 format("llvm::errs() << \"\n<<a;")); 8486 8487 verifyFormat("void f() {\n" 8488 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8489 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8490 "}"); 8491 8492 // Handle 'endl'. 8493 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8494 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8495 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8496 8497 // Handle '\n'. 8498 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8499 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8500 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8501 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8502 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8503 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8504 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8505 } 8506 8507 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8508 verifyFormat("return out << \"somepacket = {\\n\"\n" 8509 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8510 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8511 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8512 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8513 " << \"}\";"); 8514 8515 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8516 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8517 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8518 verifyFormat( 8519 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8520 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8521 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8522 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8523 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8524 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8525 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8526 verifyFormat( 8527 "void f() {\n" 8528 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8529 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8530 "}"); 8531 8532 // Breaking before the first "<<" is generally not desirable. 8533 verifyFormat( 8534 "llvm::errs()\n" 8535 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8536 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8537 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8538 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8539 getLLVMStyleWithColumns(70)); 8540 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8541 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8542 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8543 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8544 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8545 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8546 getLLVMStyleWithColumns(70)); 8547 8548 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8549 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8550 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8551 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8552 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8553 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8554 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8555 " (aaaa + aaaa);", 8556 getLLVMStyleWithColumns(40)); 8557 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8558 " (aaaaaaa + aaaaa));", 8559 getLLVMStyleWithColumns(40)); 8560 verifyFormat( 8561 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8562 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8563 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8564 } 8565 8566 TEST_F(FormatTest, UnderstandsEquals) { 8567 verifyFormat( 8568 "aaaaaaaaaaaaaaaaa =\n" 8569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8570 verifyFormat( 8571 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8573 verifyFormat( 8574 "if (a) {\n" 8575 " f();\n" 8576 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8578 "}"); 8579 8580 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8581 " 100000000 + 10000000) {\n}"); 8582 } 8583 8584 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8585 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8586 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8587 8588 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8589 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8590 8591 verifyFormat( 8592 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8593 " Parameter2);"); 8594 8595 verifyFormat( 8596 "ShortObject->shortFunction(\n" 8597 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8598 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8599 8600 verifyFormat("loooooooooooooongFunction(\n" 8601 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8602 8603 verifyFormat( 8604 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8605 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8606 8607 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8608 " .WillRepeatedly(Return(SomeValue));"); 8609 verifyFormat("void f() {\n" 8610 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8611 " .Times(2)\n" 8612 " .WillRepeatedly(Return(SomeValue));\n" 8613 "}"); 8614 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8615 " ccccccccccccccccccccccc);"); 8616 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8618 " .aaaaa(aaaaa),\n" 8619 " aaaaaaaaaaaaaaaaaaaaa);"); 8620 verifyFormat("void f() {\n" 8621 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8622 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8623 "}"); 8624 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8626 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8627 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8628 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8629 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8630 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8631 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8632 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8633 "}"); 8634 8635 // Here, it is not necessary to wrap at "." or "->". 8636 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8637 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8638 verifyFormat( 8639 "aaaaaaaaaaa->aaaaaaaaa(\n" 8640 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8641 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8642 8643 verifyFormat( 8644 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8645 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8646 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8647 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8648 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8649 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8650 8651 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8653 " .a();"); 8654 8655 FormatStyle NoBinPacking = getLLVMStyle(); 8656 NoBinPacking.BinPackParameters = false; 8657 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8658 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8659 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8660 " aaaaaaaaaaaaaaaaaaa,\n" 8661 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8662 NoBinPacking); 8663 8664 // If there is a subsequent call, change to hanging indentation. 8665 verifyFormat( 8666 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8667 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8668 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8669 verifyFormat( 8670 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8671 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8672 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8673 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8674 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8675 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8677 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8678 } 8679 8680 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8681 verifyFormat("template <typename T>\n" 8682 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8683 verifyFormat("template <typename T>\n" 8684 "// T should be one of {A, B}.\n" 8685 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8686 verifyFormat( 8687 "template <typename T>\n" 8688 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8689 verifyFormat("template <typename T>\n" 8690 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8691 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8692 verifyFormat( 8693 "template <typename T>\n" 8694 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8695 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8696 verifyFormat( 8697 "template <typename T>\n" 8698 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8699 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8701 verifyFormat("template <typename T>\n" 8702 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8703 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8704 verifyFormat( 8705 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8706 " typename T4 = char>\n" 8707 "void f();"); 8708 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8709 " template <typename> class cccccccccccccccccccccc,\n" 8710 " typename ddddddddddddd>\n" 8711 "class C {};"); 8712 verifyFormat( 8713 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8715 8716 verifyFormat("void f() {\n" 8717 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8718 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8719 "}"); 8720 8721 verifyFormat("template <typename T> class C {};"); 8722 verifyFormat("template <typename T> void f();"); 8723 verifyFormat("template <typename T> void f() {}"); 8724 verifyFormat( 8725 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8726 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8728 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8731 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8732 getLLVMStyleWithColumns(72)); 8733 EXPECT_EQ("static_cast<A< //\n" 8734 " B> *>(\n" 8735 "\n" 8736 ");", 8737 format("static_cast<A<//\n" 8738 " B>*>(\n" 8739 "\n" 8740 " );")); 8741 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8742 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8743 8744 FormatStyle AlwaysBreak = getLLVMStyle(); 8745 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8746 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8747 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8748 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8749 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8750 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8751 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8752 verifyFormat("template <template <typename> class Fooooooo,\n" 8753 " template <typename> class Baaaaaaar>\n" 8754 "struct C {};", 8755 AlwaysBreak); 8756 verifyFormat("template <typename T> // T can be A, B or C.\n" 8757 "struct C {};", 8758 AlwaysBreak); 8759 verifyFormat("template <enum E> class A {\n" 8760 "public:\n" 8761 " E *f();\n" 8762 "};"); 8763 8764 FormatStyle NeverBreak = getLLVMStyle(); 8765 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8766 verifyFormat("template <typename T> class C {};", NeverBreak); 8767 verifyFormat("template <typename T> void f();", NeverBreak); 8768 verifyFormat("template <typename T> void f() {}", NeverBreak); 8769 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8770 "bbbbbbbbbbbbbbbbbbbb) {}", 8771 NeverBreak); 8772 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8773 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8774 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8775 NeverBreak); 8776 verifyFormat("template <template <typename> class Fooooooo,\n" 8777 " template <typename> class Baaaaaaar>\n" 8778 "struct C {};", 8779 NeverBreak); 8780 verifyFormat("template <typename T> // T can be A, B or C.\n" 8781 "struct C {};", 8782 NeverBreak); 8783 verifyFormat("template <enum E> class A {\n" 8784 "public:\n" 8785 " E *f();\n" 8786 "};", 8787 NeverBreak); 8788 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8789 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8790 "bbbbbbbbbbbbbbbbbbbb) {}", 8791 NeverBreak); 8792 } 8793 8794 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8795 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8796 Style.ColumnLimit = 60; 8797 EXPECT_EQ("// Baseline - no comments.\n" 8798 "template <\n" 8799 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8800 "void f() {}", 8801 format("// Baseline - no comments.\n" 8802 "template <\n" 8803 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8804 "void f() {}", 8805 Style)); 8806 8807 EXPECT_EQ("template <\n" 8808 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8809 "void f() {}", 8810 format("template <\n" 8811 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8812 "void f() {}", 8813 Style)); 8814 8815 EXPECT_EQ( 8816 "template <\n" 8817 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8818 "void f() {}", 8819 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8820 "void f() {}", 8821 Style)); 8822 8823 EXPECT_EQ( 8824 "template <\n" 8825 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8826 " // multiline\n" 8827 "void f() {}", 8828 format("template <\n" 8829 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8830 " // multiline\n" 8831 "void f() {}", 8832 Style)); 8833 8834 EXPECT_EQ( 8835 "template <typename aaaaaaaaaa<\n" 8836 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8837 "void f() {}", 8838 format( 8839 "template <\n" 8840 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8841 "void f() {}", 8842 Style)); 8843 } 8844 8845 TEST_F(FormatTest, WrapsTemplateParameters) { 8846 FormatStyle Style = getLLVMStyle(); 8847 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8848 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8849 verifyFormat( 8850 "template <typename... a> struct q {};\n" 8851 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8852 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8853 " y;", 8854 Style); 8855 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8856 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8857 verifyFormat( 8858 "template <typename... a> struct r {};\n" 8859 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8860 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8861 " y;", 8862 Style); 8863 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8864 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8865 verifyFormat("template <typename... a> struct s {};\n" 8866 "extern s<\n" 8867 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8868 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8869 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8870 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8871 " y;", 8872 Style); 8873 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8874 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8875 verifyFormat("template <typename... a> struct t {};\n" 8876 "extern t<\n" 8877 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8878 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8879 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8880 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8881 " y;", 8882 Style); 8883 } 8884 8885 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8886 verifyFormat( 8887 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8889 verifyFormat( 8890 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8892 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8893 8894 // FIXME: Should we have the extra indent after the second break? 8895 verifyFormat( 8896 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8899 8900 verifyFormat( 8901 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8902 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8903 8904 // Breaking at nested name specifiers is generally not desirable. 8905 verifyFormat( 8906 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8907 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8908 8909 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8911 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8912 " aaaaaaaaaaaaaaaaaaaaa);", 8913 getLLVMStyleWithColumns(74)); 8914 8915 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8917 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8918 } 8919 8920 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8921 verifyFormat("A<int> a;"); 8922 verifyFormat("A<A<A<int>>> a;"); 8923 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8924 verifyFormat("bool x = a < 1 || 2 > a;"); 8925 verifyFormat("bool x = 5 < f<int>();"); 8926 verifyFormat("bool x = f<int>() > 5;"); 8927 verifyFormat("bool x = 5 < a<int>::x;"); 8928 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8929 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8930 8931 verifyGoogleFormat("A<A<int>> a;"); 8932 verifyGoogleFormat("A<A<A<int>>> a;"); 8933 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8934 verifyGoogleFormat("A<A<int> > a;"); 8935 verifyGoogleFormat("A<A<A<int> > > a;"); 8936 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8937 verifyGoogleFormat("A<::A<int>> a;"); 8938 verifyGoogleFormat("A<::A> a;"); 8939 verifyGoogleFormat("A< ::A> a;"); 8940 verifyGoogleFormat("A< ::A<int> > a;"); 8941 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8942 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8943 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8944 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8945 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8946 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8947 8948 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8949 8950 // template closer followed by a token that starts with > or = 8951 verifyFormat("bool b = a<1> > 1;"); 8952 verifyFormat("bool b = a<1> >= 1;"); 8953 verifyFormat("int i = a<1> >> 1;"); 8954 FormatStyle Style = getLLVMStyle(); 8955 Style.SpaceBeforeAssignmentOperators = false; 8956 verifyFormat("bool b= a<1> == 1;", Style); 8957 verifyFormat("a<int> = 1;", Style); 8958 verifyFormat("a<int> >>= 1;", Style); 8959 8960 verifyFormat("test < a | b >> c;"); 8961 verifyFormat("test<test<a | b>> c;"); 8962 verifyFormat("test >> a >> b;"); 8963 verifyFormat("test << a >> b;"); 8964 8965 verifyFormat("f<int>();"); 8966 verifyFormat("template <typename T> void f() {}"); 8967 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8968 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8969 "sizeof(char)>::type>;"); 8970 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8971 verifyFormat("f(a.operator()<A>());"); 8972 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8973 " .template operator()<A>());", 8974 getLLVMStyleWithColumns(35)); 8975 8976 // Not template parameters. 8977 verifyFormat("return a < b && c > d;"); 8978 verifyFormat("void f() {\n" 8979 " while (a < b && c > d) {\n" 8980 " }\n" 8981 "}"); 8982 verifyFormat("template <typename... Types>\n" 8983 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 8984 8985 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8986 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 8987 getLLVMStyleWithColumns(60)); 8988 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 8989 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 8990 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 8991 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 8992 } 8993 8994 TEST_F(FormatTest, UnderstandsShiftOperators) { 8995 verifyFormat("if (i < x >> 1)"); 8996 verifyFormat("while (i < x >> 1)"); 8997 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 8998 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 8999 verifyFormat( 9000 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 9001 verifyFormat("Foo.call<Bar<Function>>()"); 9002 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 9003 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 9004 "++i, v = v >> 1)"); 9005 verifyFormat("if (w<u<v<x>>, 1>::t)"); 9006 } 9007 9008 TEST_F(FormatTest, BitshiftOperatorWidth) { 9009 EXPECT_EQ("int a = 1 << 2; /* foo\n" 9010 " bar */", 9011 format("int a=1<<2; /* foo\n" 9012 " bar */")); 9013 9014 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 9015 " bar */", 9016 format("int b =256>>1 ; /* foo\n" 9017 " bar */")); 9018 } 9019 9020 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9021 verifyFormat("COMPARE(a, ==, b);"); 9022 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9023 } 9024 9025 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9026 verifyFormat("int A::*x;"); 9027 verifyFormat("int (S::*func)(void *);"); 9028 verifyFormat("void f() { int (S::*func)(void *); }"); 9029 verifyFormat("typedef bool *(Class::*Member)() const;"); 9030 verifyFormat("void f() {\n" 9031 " (a->*f)();\n" 9032 " a->*x;\n" 9033 " (a.*f)();\n" 9034 " ((*a).*f)();\n" 9035 " a.*x;\n" 9036 "}"); 9037 verifyFormat("void f() {\n" 9038 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9039 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9040 "}"); 9041 verifyFormat( 9042 "(aaaaaaaaaa->*bbbbbbb)(\n" 9043 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9044 FormatStyle Style = getLLVMStyle(); 9045 Style.PointerAlignment = FormatStyle::PAS_Left; 9046 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9047 } 9048 9049 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9050 verifyFormat("int a = -2;"); 9051 verifyFormat("f(-1, -2, -3);"); 9052 verifyFormat("a[-1] = 5;"); 9053 verifyFormat("int a = 5 + -2;"); 9054 verifyFormat("if (i == -1) {\n}"); 9055 verifyFormat("if (i != -1) {\n}"); 9056 verifyFormat("if (i > -1) {\n}"); 9057 verifyFormat("if (i < -1) {\n}"); 9058 verifyFormat("++(a->f());"); 9059 verifyFormat("--(a->f());"); 9060 verifyFormat("(a->f())++;"); 9061 verifyFormat("a[42]++;"); 9062 verifyFormat("if (!(a->f())) {\n}"); 9063 verifyFormat("if (!+i) {\n}"); 9064 verifyFormat("~&a;"); 9065 9066 verifyFormat("a-- > b;"); 9067 verifyFormat("b ? -a : c;"); 9068 verifyFormat("n * sizeof char16;"); 9069 verifyFormat("n * alignof char16;", getGoogleStyle()); 9070 verifyFormat("sizeof(char);"); 9071 verifyFormat("alignof(char);", getGoogleStyle()); 9072 9073 verifyFormat("return -1;"); 9074 verifyFormat("throw -1;"); 9075 verifyFormat("switch (a) {\n" 9076 "case -1:\n" 9077 " break;\n" 9078 "}"); 9079 verifyFormat("#define X -1"); 9080 verifyFormat("#define X -kConstant"); 9081 9082 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9083 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9084 9085 verifyFormat("int a = /* confusing comment */ -1;"); 9086 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9087 verifyFormat("int a = i /* confusing comment */++;"); 9088 9089 verifyFormat("co_yield -1;"); 9090 verifyFormat("co_return -1;"); 9091 9092 // Check that * is not treated as a binary operator when we set 9093 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9094 FormatStyle PASLeftStyle = getLLVMStyle(); 9095 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9096 verifyFormat("co_return *a;", PASLeftStyle); 9097 verifyFormat("co_await *a;", PASLeftStyle); 9098 verifyFormat("co_yield *a", PASLeftStyle); 9099 verifyFormat("return *a;", PASLeftStyle); 9100 } 9101 9102 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9103 verifyFormat("if (!aaaaaaaaaa( // break\n" 9104 " aaaaa)) {\n" 9105 "}"); 9106 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9107 " aaaaa));"); 9108 verifyFormat("*aaa = aaaaaaa( // break\n" 9109 " bbbbbb);"); 9110 } 9111 9112 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9113 verifyFormat("bool operator<();"); 9114 verifyFormat("bool operator>();"); 9115 verifyFormat("bool operator=();"); 9116 verifyFormat("bool operator==();"); 9117 verifyFormat("bool operator!=();"); 9118 verifyFormat("int operator+();"); 9119 verifyFormat("int operator++();"); 9120 verifyFormat("int operator++(int) volatile noexcept;"); 9121 verifyFormat("bool operator,();"); 9122 verifyFormat("bool operator();"); 9123 verifyFormat("bool operator()();"); 9124 verifyFormat("bool operator[]();"); 9125 verifyFormat("operator bool();"); 9126 verifyFormat("operator int();"); 9127 verifyFormat("operator void *();"); 9128 verifyFormat("operator SomeType<int>();"); 9129 verifyFormat("operator SomeType<int, int>();"); 9130 verifyFormat("operator SomeType<SomeType<int>>();"); 9131 verifyFormat("void *operator new(std::size_t size);"); 9132 verifyFormat("void *operator new[](std::size_t size);"); 9133 verifyFormat("void operator delete(void *ptr);"); 9134 verifyFormat("void operator delete[](void *ptr);"); 9135 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9136 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9137 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9138 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9139 9140 verifyFormat( 9141 "ostream &operator<<(ostream &OutputStream,\n" 9142 " SomeReallyLongType WithSomeReallyLongValue);"); 9143 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9144 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9145 " return left.group < right.group;\n" 9146 "}"); 9147 verifyFormat("SomeType &operator=(const SomeType &S);"); 9148 verifyFormat("f.template operator()<int>();"); 9149 9150 verifyGoogleFormat("operator void*();"); 9151 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9152 verifyGoogleFormat("operator ::A();"); 9153 9154 verifyFormat("using A::operator+;"); 9155 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9156 "int i;"); 9157 9158 // Calling an operator as a member function. 9159 verifyFormat("void f() { a.operator*(); }"); 9160 verifyFormat("void f() { a.operator*(b & b); }"); 9161 verifyFormat("void f() { a->operator&(a * b); }"); 9162 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9163 // TODO: Calling an operator as a non-member function is hard to distinguish. 9164 // https://llvm.org/PR50629 9165 // verifyFormat("void f() { operator*(a & a); }"); 9166 // verifyFormat("void f() { operator&(a, b * b); }"); 9167 } 9168 9169 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9170 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9171 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9172 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9173 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9174 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9175 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9176 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9177 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9178 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9179 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9180 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9181 verifyFormat("void Fn(T const &) const &;"); 9182 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9183 verifyFormat("template <typename T>\n" 9184 "void F(T) && = delete;", 9185 getGoogleStyle()); 9186 9187 FormatStyle AlignLeft = getLLVMStyle(); 9188 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9189 verifyFormat("void A::b() && {}", AlignLeft); 9190 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9191 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9192 AlignLeft); 9193 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9194 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9195 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9196 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9197 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9198 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9199 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9200 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9201 9202 FormatStyle Spaces = getLLVMStyle(); 9203 Spaces.SpacesInCStyleCastParentheses = true; 9204 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9205 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9206 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9207 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9208 9209 Spaces.SpacesInCStyleCastParentheses = false; 9210 Spaces.SpacesInParentheses = true; 9211 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9212 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9213 Spaces); 9214 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9215 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9216 9217 FormatStyle BreakTemplate = getLLVMStyle(); 9218 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9219 9220 verifyFormat("struct f {\n" 9221 " template <class T>\n" 9222 " int &foo(const std::string &str) &noexcept {}\n" 9223 "};", 9224 BreakTemplate); 9225 9226 verifyFormat("struct f {\n" 9227 " template <class T>\n" 9228 " int &foo(const std::string &str) &&noexcept {}\n" 9229 "};", 9230 BreakTemplate); 9231 9232 verifyFormat("struct f {\n" 9233 " template <class T>\n" 9234 " int &foo(const std::string &str) const &noexcept {}\n" 9235 "};", 9236 BreakTemplate); 9237 9238 verifyFormat("struct f {\n" 9239 " template <class T>\n" 9240 " int &foo(const std::string &str) const &noexcept {}\n" 9241 "};", 9242 BreakTemplate); 9243 9244 verifyFormat("struct f {\n" 9245 " template <class T>\n" 9246 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9247 "};", 9248 BreakTemplate); 9249 9250 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9251 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9252 FormatStyle::BTDS_Yes; 9253 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9254 9255 verifyFormat("struct f {\n" 9256 " template <class T>\n" 9257 " int& foo(const std::string& str) & noexcept {}\n" 9258 "};", 9259 AlignLeftBreakTemplate); 9260 9261 verifyFormat("struct f {\n" 9262 " template <class T>\n" 9263 " int& foo(const std::string& str) && noexcept {}\n" 9264 "};", 9265 AlignLeftBreakTemplate); 9266 9267 verifyFormat("struct f {\n" 9268 " template <class T>\n" 9269 " int& foo(const std::string& str) const& noexcept {}\n" 9270 "};", 9271 AlignLeftBreakTemplate); 9272 9273 verifyFormat("struct f {\n" 9274 " template <class T>\n" 9275 " int& foo(const std::string& str) const&& noexcept {}\n" 9276 "};", 9277 AlignLeftBreakTemplate); 9278 9279 verifyFormat("struct f {\n" 9280 " template <class T>\n" 9281 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9282 "};", 9283 AlignLeftBreakTemplate); 9284 9285 // The `&` in `Type&` should not be confused with a trailing `&` of 9286 // DEPRECATED(reason) member function. 9287 verifyFormat("struct f {\n" 9288 " template <class T>\n" 9289 " DEPRECATED(reason)\n" 9290 " Type &foo(arguments) {}\n" 9291 "};", 9292 BreakTemplate); 9293 9294 verifyFormat("struct f {\n" 9295 " template <class T>\n" 9296 " DEPRECATED(reason)\n" 9297 " Type& foo(arguments) {}\n" 9298 "};", 9299 AlignLeftBreakTemplate); 9300 9301 verifyFormat("void (*foopt)(int) = &func;"); 9302 } 9303 9304 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9305 verifyFormat("void f() {\n" 9306 " A *a = new A;\n" 9307 " A *a = new (placement) A;\n" 9308 " delete a;\n" 9309 " delete (A *)a;\n" 9310 "}"); 9311 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9312 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9313 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9314 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9315 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9316 verifyFormat("delete[] h->p;"); 9317 } 9318 9319 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9320 verifyFormat("int *f(int *a) {}"); 9321 verifyFormat("int main(int argc, char **argv) {}"); 9322 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9323 verifyIndependentOfContext("f(a, *a);"); 9324 verifyFormat("void g() { f(*a); }"); 9325 verifyIndependentOfContext("int a = b * 10;"); 9326 verifyIndependentOfContext("int a = 10 * b;"); 9327 verifyIndependentOfContext("int a = b * c;"); 9328 verifyIndependentOfContext("int a += b * c;"); 9329 verifyIndependentOfContext("int a -= b * c;"); 9330 verifyIndependentOfContext("int a *= b * c;"); 9331 verifyIndependentOfContext("int a /= b * c;"); 9332 verifyIndependentOfContext("int a = *b;"); 9333 verifyIndependentOfContext("int a = *b * c;"); 9334 verifyIndependentOfContext("int a = b * *c;"); 9335 verifyIndependentOfContext("int a = b * (10);"); 9336 verifyIndependentOfContext("S << b * (10);"); 9337 verifyIndependentOfContext("return 10 * b;"); 9338 verifyIndependentOfContext("return *b * *c;"); 9339 verifyIndependentOfContext("return a & ~b;"); 9340 verifyIndependentOfContext("f(b ? *c : *d);"); 9341 verifyIndependentOfContext("int a = b ? *c : *d;"); 9342 verifyIndependentOfContext("*b = a;"); 9343 verifyIndependentOfContext("a * ~b;"); 9344 verifyIndependentOfContext("a * !b;"); 9345 verifyIndependentOfContext("a * +b;"); 9346 verifyIndependentOfContext("a * -b;"); 9347 verifyIndependentOfContext("a * ++b;"); 9348 verifyIndependentOfContext("a * --b;"); 9349 verifyIndependentOfContext("a[4] * b;"); 9350 verifyIndependentOfContext("a[a * a] = 1;"); 9351 verifyIndependentOfContext("f() * b;"); 9352 verifyIndependentOfContext("a * [self dostuff];"); 9353 verifyIndependentOfContext("int x = a * (a + b);"); 9354 verifyIndependentOfContext("(a *)(a + b);"); 9355 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9356 verifyIndependentOfContext("int *pa = (int *)&a;"); 9357 verifyIndependentOfContext("return sizeof(int **);"); 9358 verifyIndependentOfContext("return sizeof(int ******);"); 9359 verifyIndependentOfContext("return (int **&)a;"); 9360 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9361 verifyFormat("void f(Type (*parameter)[10]) {}"); 9362 verifyFormat("void f(Type (¶meter)[10]) {}"); 9363 verifyGoogleFormat("return sizeof(int**);"); 9364 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9365 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9366 verifyFormat("auto a = [](int **&, int ***) {};"); 9367 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9368 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9369 verifyFormat("[](const decltype(*a) &value) {}"); 9370 verifyFormat("[](const typeof(*a) &value) {}"); 9371 verifyFormat("[](const _Atomic(a *) &value) {}"); 9372 verifyFormat("[](const __underlying_type(a) &value) {}"); 9373 verifyFormat("decltype(a * b) F();"); 9374 verifyFormat("typeof(a * b) F();"); 9375 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9376 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9377 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9378 verifyIndependentOfContext("int i{a * b};"); 9379 verifyIndependentOfContext("aaa && aaa->f();"); 9380 verifyIndependentOfContext("int x = ~*p;"); 9381 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9382 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9383 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9384 verifyFormat("void f() { f(a, c * d); }"); 9385 verifyFormat("void f() { f(new a(), c * d); }"); 9386 verifyFormat("void f(const MyOverride &override);"); 9387 verifyFormat("void f(const MyFinal &final);"); 9388 verifyIndependentOfContext("bool a = f() && override.f();"); 9389 verifyIndependentOfContext("bool a = f() && final.f();"); 9390 9391 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9392 9393 verifyIndependentOfContext("A<int *> a;"); 9394 verifyIndependentOfContext("A<int **> a;"); 9395 verifyIndependentOfContext("A<int *, int *> a;"); 9396 verifyIndependentOfContext("A<int *[]> a;"); 9397 verifyIndependentOfContext( 9398 "const char *const p = reinterpret_cast<const char *const>(q);"); 9399 verifyIndependentOfContext("A<int **, int **> a;"); 9400 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9401 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9402 verifyFormat("for (; a && b;) {\n}"); 9403 verifyFormat("bool foo = true && [] { return false; }();"); 9404 9405 verifyFormat( 9406 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9407 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9408 9409 verifyGoogleFormat("int const* a = &b;"); 9410 verifyGoogleFormat("**outparam = 1;"); 9411 verifyGoogleFormat("*outparam = a * b;"); 9412 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9413 verifyGoogleFormat("A<int*> a;"); 9414 verifyGoogleFormat("A<int**> a;"); 9415 verifyGoogleFormat("A<int*, int*> a;"); 9416 verifyGoogleFormat("A<int**, int**> a;"); 9417 verifyGoogleFormat("f(b ? *c : *d);"); 9418 verifyGoogleFormat("int a = b ? *c : *d;"); 9419 verifyGoogleFormat("Type* t = **x;"); 9420 verifyGoogleFormat("Type* t = *++*x;"); 9421 verifyGoogleFormat("*++*x;"); 9422 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9423 verifyGoogleFormat("Type* t = x++ * y;"); 9424 verifyGoogleFormat( 9425 "const char* const p = reinterpret_cast<const char* const>(q);"); 9426 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9427 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9428 verifyGoogleFormat("template <typename T>\n" 9429 "void f(int i = 0, SomeType** temps = NULL);"); 9430 9431 FormatStyle Left = getLLVMStyle(); 9432 Left.PointerAlignment = FormatStyle::PAS_Left; 9433 verifyFormat("x = *a(x) = *a(y);", Left); 9434 verifyFormat("for (;; *a = b) {\n}", Left); 9435 verifyFormat("return *this += 1;", Left); 9436 verifyFormat("throw *x;", Left); 9437 verifyFormat("delete *x;", Left); 9438 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9439 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9440 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9441 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9442 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9443 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9444 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9445 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9446 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9447 9448 verifyIndependentOfContext("a = *(x + y);"); 9449 verifyIndependentOfContext("a = &(x + y);"); 9450 verifyIndependentOfContext("*(x + y).call();"); 9451 verifyIndependentOfContext("&(x + y)->call();"); 9452 verifyFormat("void f() { &(*I).first; }"); 9453 9454 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9455 verifyFormat("f(* /* confusing comment */ foo);"); 9456 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 9457 verifyFormat("void foo(int * // this is the first paramters\n" 9458 " ,\n" 9459 " int second);"); 9460 verifyFormat("double term = a * // first\n" 9461 " b;"); 9462 verifyFormat( 9463 "int *MyValues = {\n" 9464 " *A, // Operator detection might be confused by the '{'\n" 9465 " *BB // Operator detection might be confused by previous comment\n" 9466 "};"); 9467 9468 verifyIndependentOfContext("if (int *a = &b)"); 9469 verifyIndependentOfContext("if (int &a = *b)"); 9470 verifyIndependentOfContext("if (a & b[i])"); 9471 verifyIndependentOfContext("if constexpr (a & b[i])"); 9472 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9473 verifyIndependentOfContext("if (a * (b * c))"); 9474 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9475 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9476 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9477 verifyIndependentOfContext("if (*b[i])"); 9478 verifyIndependentOfContext("if (int *a = (&b))"); 9479 verifyIndependentOfContext("while (int *a = &b)"); 9480 verifyIndependentOfContext("while (a * (b * c))"); 9481 verifyIndependentOfContext("size = sizeof *a;"); 9482 verifyIndependentOfContext("if (a && (b = c))"); 9483 verifyFormat("void f() {\n" 9484 " for (const int &v : Values) {\n" 9485 " }\n" 9486 "}"); 9487 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9488 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9489 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9490 9491 verifyFormat("#define A (!a * b)"); 9492 verifyFormat("#define MACRO \\\n" 9493 " int *i = a * b; \\\n" 9494 " void f(a *b);", 9495 getLLVMStyleWithColumns(19)); 9496 9497 verifyIndependentOfContext("A = new SomeType *[Length];"); 9498 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9499 verifyIndependentOfContext("T **t = new T *;"); 9500 verifyIndependentOfContext("T **t = new T *();"); 9501 verifyGoogleFormat("A = new SomeType*[Length]();"); 9502 verifyGoogleFormat("A = new SomeType*[Length];"); 9503 verifyGoogleFormat("T** t = new T*;"); 9504 verifyGoogleFormat("T** t = new T*();"); 9505 9506 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9507 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9508 verifyFormat("template <bool a, bool b> " 9509 "typename t::if<x && y>::type f() {}"); 9510 verifyFormat("template <int *y> f() {}"); 9511 verifyFormat("vector<int *> v;"); 9512 verifyFormat("vector<int *const> v;"); 9513 verifyFormat("vector<int *const **const *> v;"); 9514 verifyFormat("vector<int *volatile> v;"); 9515 verifyFormat("vector<a *_Nonnull> v;"); 9516 verifyFormat("vector<a *_Nullable> v;"); 9517 verifyFormat("vector<a *_Null_unspecified> v;"); 9518 verifyFormat("vector<a *__ptr32> v;"); 9519 verifyFormat("vector<a *__ptr64> v;"); 9520 verifyFormat("vector<a *__capability> v;"); 9521 FormatStyle TypeMacros = getLLVMStyle(); 9522 TypeMacros.TypenameMacros = {"LIST"}; 9523 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9524 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9525 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9526 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9527 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9528 9529 FormatStyle CustomQualifier = getLLVMStyle(); 9530 // Add identifiers that should not be parsed as a qualifier by default. 9531 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9532 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9533 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9534 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9535 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9536 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9537 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9538 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9539 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9540 verifyFormat("vector<a * _NotAQualifier> v;"); 9541 verifyFormat("vector<a * __not_a_qualifier> v;"); 9542 verifyFormat("vector<a * b> v;"); 9543 verifyFormat("foo<b && false>();"); 9544 verifyFormat("foo<b & 1>();"); 9545 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9546 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9547 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9548 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9549 verifyFormat( 9550 "template <class T, class = typename std::enable_if<\n" 9551 " std::is_integral<T>::value &&\n" 9552 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9553 "void F();", 9554 getLLVMStyleWithColumns(70)); 9555 verifyFormat("template <class T,\n" 9556 " class = typename std::enable_if<\n" 9557 " std::is_integral<T>::value &&\n" 9558 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9559 " class U>\n" 9560 "void F();", 9561 getLLVMStyleWithColumns(70)); 9562 verifyFormat( 9563 "template <class T,\n" 9564 " class = typename ::std::enable_if<\n" 9565 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9566 "void F();", 9567 getGoogleStyleWithColumns(68)); 9568 9569 verifyIndependentOfContext("MACRO(int *i);"); 9570 verifyIndependentOfContext("MACRO(auto *a);"); 9571 verifyIndependentOfContext("MACRO(const A *a);"); 9572 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9573 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9574 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9575 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9576 verifyIndependentOfContext("MACRO(A *const a);"); 9577 verifyIndependentOfContext("MACRO(A *restrict a);"); 9578 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9579 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9580 verifyIndependentOfContext("MACRO(A *volatile a);"); 9581 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9582 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9583 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9584 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9585 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9586 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9587 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9588 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9589 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9590 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9591 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9592 verifyIndependentOfContext("MACRO(A *__capability);"); 9593 verifyIndependentOfContext("MACRO(A &__capability);"); 9594 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9595 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9596 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9597 // a type declaration: 9598 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9599 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9600 // Also check that TypenameMacros prevents parsing it as multiplication: 9601 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9602 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9603 9604 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9605 verifyFormat("void f() { f(float{1}, a * a); }"); 9606 verifyFormat("void f() { f(float(1), a * a); }"); 9607 9608 verifyFormat("f((void (*)(int))g);"); 9609 verifyFormat("f((void (&)(int))g);"); 9610 verifyFormat("f((void (^)(int))g);"); 9611 9612 // FIXME: Is there a way to make this work? 9613 // verifyIndependentOfContext("MACRO(A *a);"); 9614 verifyFormat("MACRO(A &B);"); 9615 verifyFormat("MACRO(A *B);"); 9616 verifyFormat("void f() { MACRO(A * B); }"); 9617 verifyFormat("void f() { MACRO(A & B); }"); 9618 9619 // This lambda was mis-formatted after D88956 (treating it as a binop): 9620 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9621 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9622 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9623 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9624 9625 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9626 verifyFormat("return options != nullptr && operator==(*options);"); 9627 9628 EXPECT_EQ("#define OP(x) \\\n" 9629 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9630 " return s << a.DebugString(); \\\n" 9631 " }", 9632 format("#define OP(x) \\\n" 9633 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9634 " return s << a.DebugString(); \\\n" 9635 " }", 9636 getLLVMStyleWithColumns(50))); 9637 9638 // FIXME: We cannot handle this case yet; we might be able to figure out that 9639 // foo<x> d > v; doesn't make sense. 9640 verifyFormat("foo<a<b && c> d> v;"); 9641 9642 FormatStyle PointerMiddle = getLLVMStyle(); 9643 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9644 verifyFormat("delete *x;", PointerMiddle); 9645 verifyFormat("int * x;", PointerMiddle); 9646 verifyFormat("int *[] x;", PointerMiddle); 9647 verifyFormat("template <int * y> f() {}", PointerMiddle); 9648 verifyFormat("int * f(int * a) {}", PointerMiddle); 9649 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9650 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9651 verifyFormat("A<int *> a;", PointerMiddle); 9652 verifyFormat("A<int **> a;", PointerMiddle); 9653 verifyFormat("A<int *, int *> a;", PointerMiddle); 9654 verifyFormat("A<int *[]> a;", PointerMiddle); 9655 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9656 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9657 verifyFormat("T ** t = new T *;", PointerMiddle); 9658 9659 // Member function reference qualifiers aren't binary operators. 9660 verifyFormat("string // break\n" 9661 "operator()() & {}"); 9662 verifyFormat("string // break\n" 9663 "operator()() && {}"); 9664 verifyGoogleFormat("template <typename T>\n" 9665 "auto x() & -> int {}"); 9666 9667 // Should be binary operators when used as an argument expression (overloaded 9668 // operator invoked as a member function). 9669 verifyFormat("void f() { a.operator()(a * a); }"); 9670 verifyFormat("void f() { a->operator()(a & a); }"); 9671 verifyFormat("void f() { a.operator()(*a & *a); }"); 9672 verifyFormat("void f() { a->operator()(*a * *a); }"); 9673 } 9674 9675 TEST_F(FormatTest, UnderstandsAttributes) { 9676 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9677 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9678 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9679 FormatStyle AfterType = getLLVMStyle(); 9680 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9681 verifyFormat("__attribute__((nodebug)) void\n" 9682 "foo() {}\n", 9683 AfterType); 9684 verifyFormat("__unused void\n" 9685 "foo() {}", 9686 AfterType); 9687 9688 FormatStyle CustomAttrs = getLLVMStyle(); 9689 CustomAttrs.AttributeMacros.push_back("__unused"); 9690 CustomAttrs.AttributeMacros.push_back("__attr1"); 9691 CustomAttrs.AttributeMacros.push_back("__attr2"); 9692 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9693 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9694 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9695 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9696 // Check that it is parsed as a multiplication without AttributeMacros and 9697 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9698 verifyFormat("vector<SomeType * __attr1> v;"); 9699 verifyFormat("vector<SomeType __attr1 *> v;"); 9700 verifyFormat("vector<SomeType __attr1 *const> v;"); 9701 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9702 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9703 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9704 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9705 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9706 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9707 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9708 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9709 9710 // Check that these are not parsed as function declarations: 9711 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9712 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9713 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9714 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9715 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9716 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9717 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9718 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9719 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9720 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9721 } 9722 9723 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9724 // Check that qualifiers on pointers don't break parsing of casts. 9725 verifyFormat("x = (foo *const)*v;"); 9726 verifyFormat("x = (foo *volatile)*v;"); 9727 verifyFormat("x = (foo *restrict)*v;"); 9728 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9729 verifyFormat("x = (foo *_Nonnull)*v;"); 9730 verifyFormat("x = (foo *_Nullable)*v;"); 9731 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9732 verifyFormat("x = (foo *_Nonnull)*v;"); 9733 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9734 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9735 verifyFormat("x = (foo *__ptr32)*v;"); 9736 verifyFormat("x = (foo *__ptr64)*v;"); 9737 verifyFormat("x = (foo *__capability)*v;"); 9738 9739 // Check that we handle multiple trailing qualifiers and skip them all to 9740 // determine that the expression is a cast to a pointer type. 9741 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9742 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9743 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9744 StringRef AllQualifiers = 9745 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9746 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9747 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9748 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9749 9750 // Also check that address-of is not parsed as a binary bitwise-and: 9751 verifyFormat("x = (foo *const)&v;"); 9752 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9753 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9754 9755 // Check custom qualifiers: 9756 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9757 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9758 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9759 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9760 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9761 CustomQualifier); 9762 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9763 CustomQualifier); 9764 9765 // Check that unknown identifiers result in binary operator parsing: 9766 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9767 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9768 } 9769 9770 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9771 verifyFormat("SomeType s [[unused]] (InitValue);"); 9772 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9773 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9774 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9775 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9776 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9777 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9778 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9779 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9780 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9781 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9782 9783 // Make sure we do not mistake attributes for array subscripts. 9784 verifyFormat("int a() {}\n" 9785 "[[unused]] int b() {}\n"); 9786 verifyFormat("NSArray *arr;\n" 9787 "arr[[Foo() bar]];"); 9788 9789 // On the other hand, we still need to correctly find array subscripts. 9790 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9791 9792 // Make sure that we do not mistake Objective-C method inside array literals 9793 // as attributes, even if those method names are also keywords. 9794 verifyFormat("@[ [foo bar] ];"); 9795 verifyFormat("@[ [NSArray class] ];"); 9796 verifyFormat("@[ [foo enum] ];"); 9797 9798 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9799 9800 // Make sure we do not parse attributes as lambda introducers. 9801 FormatStyle MultiLineFunctions = getLLVMStyle(); 9802 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9803 verifyFormat("[[unused]] int b() {\n" 9804 " return 42;\n" 9805 "}\n", 9806 MultiLineFunctions); 9807 } 9808 9809 TEST_F(FormatTest, AttributeClass) { 9810 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9811 verifyFormat("class S {\n" 9812 " S(S&&) = default;\n" 9813 "};", 9814 Style); 9815 verifyFormat("class [[nodiscard]] S {\n" 9816 " S(S&&) = default;\n" 9817 "};", 9818 Style); 9819 verifyFormat("class __attribute((maybeunused)) S {\n" 9820 " S(S&&) = default;\n" 9821 "};", 9822 Style); 9823 verifyFormat("struct S {\n" 9824 " S(S&&) = default;\n" 9825 "};", 9826 Style); 9827 verifyFormat("struct [[nodiscard]] S {\n" 9828 " S(S&&) = default;\n" 9829 "};", 9830 Style); 9831 } 9832 9833 TEST_F(FormatTest, AttributesAfterMacro) { 9834 FormatStyle Style = getLLVMStyle(); 9835 verifyFormat("MACRO;\n" 9836 "__attribute__((maybe_unused)) int foo() {\n" 9837 " //...\n" 9838 "}"); 9839 9840 verifyFormat("MACRO;\n" 9841 "[[nodiscard]] int foo() {\n" 9842 " //...\n" 9843 "}"); 9844 9845 EXPECT_EQ("MACRO\n\n" 9846 "__attribute__((maybe_unused)) int foo() {\n" 9847 " //...\n" 9848 "}", 9849 format("MACRO\n\n" 9850 "__attribute__((maybe_unused)) int foo() {\n" 9851 " //...\n" 9852 "}")); 9853 9854 EXPECT_EQ("MACRO\n\n" 9855 "[[nodiscard]] int foo() {\n" 9856 " //...\n" 9857 "}", 9858 format("MACRO\n\n" 9859 "[[nodiscard]] int foo() {\n" 9860 " //...\n" 9861 "}")); 9862 } 9863 9864 TEST_F(FormatTest, AttributePenaltyBreaking) { 9865 FormatStyle Style = getLLVMStyle(); 9866 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 9867 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9868 Style); 9869 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 9870 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9871 Style); 9872 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 9873 "shared_ptr<ALongTypeName> &C d) {\n}", 9874 Style); 9875 } 9876 9877 TEST_F(FormatTest, UnderstandsEllipsis) { 9878 FormatStyle Style = getLLVMStyle(); 9879 verifyFormat("int printf(const char *fmt, ...);"); 9880 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 9881 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 9882 9883 verifyFormat("template <int *...PP> a;", Style); 9884 9885 Style.PointerAlignment = FormatStyle::PAS_Left; 9886 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9887 9888 verifyFormat("template <int*... PP> a;", Style); 9889 9890 Style.PointerAlignment = FormatStyle::PAS_Middle; 9891 verifyFormat("template <int *... PP> a;", Style); 9892 } 9893 9894 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9895 EXPECT_EQ("int *a;\n" 9896 "int *a;\n" 9897 "int *a;", 9898 format("int *a;\n" 9899 "int* a;\n" 9900 "int *a;", 9901 getGoogleStyle())); 9902 EXPECT_EQ("int* a;\n" 9903 "int* a;\n" 9904 "int* a;", 9905 format("int* a;\n" 9906 "int* a;\n" 9907 "int *a;", 9908 getGoogleStyle())); 9909 EXPECT_EQ("int *a;\n" 9910 "int *a;\n" 9911 "int *a;", 9912 format("int *a;\n" 9913 "int * a;\n" 9914 "int * a;", 9915 getGoogleStyle())); 9916 EXPECT_EQ("auto x = [] {\n" 9917 " int *a;\n" 9918 " int *a;\n" 9919 " int *a;\n" 9920 "};", 9921 format("auto x=[]{int *a;\n" 9922 "int * a;\n" 9923 "int * a;};", 9924 getGoogleStyle())); 9925 } 9926 9927 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9928 verifyFormat("int f(int &&a) {}"); 9929 verifyFormat("int f(int a, char &&b) {}"); 9930 verifyFormat("void f() { int &&a = b; }"); 9931 verifyGoogleFormat("int f(int a, char&& b) {}"); 9932 verifyGoogleFormat("void f() { int&& a = b; }"); 9933 9934 verifyIndependentOfContext("A<int &&> a;"); 9935 verifyIndependentOfContext("A<int &&, int &&> a;"); 9936 verifyGoogleFormat("A<int&&> a;"); 9937 verifyGoogleFormat("A<int&&, int&&> a;"); 9938 9939 // Not rvalue references: 9940 verifyFormat("template <bool B, bool C> class A {\n" 9941 " static_assert(B && C, \"Something is wrong\");\n" 9942 "};"); 9943 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9944 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9945 verifyFormat("#define A(a, b) (a && b)"); 9946 } 9947 9948 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9949 verifyFormat("void f() {\n" 9950 " x[aaaaaaaaa -\n" 9951 " b] = 23;\n" 9952 "}", 9953 getLLVMStyleWithColumns(15)); 9954 } 9955 9956 TEST_F(FormatTest, FormatsCasts) { 9957 verifyFormat("Type *A = static_cast<Type *>(P);"); 9958 verifyFormat("Type *A = (Type *)P;"); 9959 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9960 verifyFormat("int a = (int)(2.0f);"); 9961 verifyFormat("int a = (int)2.0f;"); 9962 verifyFormat("x[(int32)y];"); 9963 verifyFormat("x = (int32)y;"); 9964 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9965 verifyFormat("int a = (int)*b;"); 9966 verifyFormat("int a = (int)2.0f;"); 9967 verifyFormat("int a = (int)~0;"); 9968 verifyFormat("int a = (int)++a;"); 9969 verifyFormat("int a = (int)sizeof(int);"); 9970 verifyFormat("int a = (int)+2;"); 9971 verifyFormat("my_int a = (my_int)2.0f;"); 9972 verifyFormat("my_int a = (my_int)sizeof(int);"); 9973 verifyFormat("return (my_int)aaa;"); 9974 verifyFormat("#define x ((int)-1)"); 9975 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 9976 verifyFormat("#define p(q) ((int *)&q)"); 9977 verifyFormat("fn(a)(b) + 1;"); 9978 9979 verifyFormat("void f() { my_int a = (my_int)*b; }"); 9980 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 9981 verifyFormat("my_int a = (my_int)~0;"); 9982 verifyFormat("my_int a = (my_int)++a;"); 9983 verifyFormat("my_int a = (my_int)-2;"); 9984 verifyFormat("my_int a = (my_int)1;"); 9985 verifyFormat("my_int a = (my_int *)1;"); 9986 verifyFormat("my_int a = (const my_int)-1;"); 9987 verifyFormat("my_int a = (const my_int *)-1;"); 9988 verifyFormat("my_int a = (my_int)(my_int)-1;"); 9989 verifyFormat("my_int a = (ns::my_int)-2;"); 9990 verifyFormat("case (my_int)ONE:"); 9991 verifyFormat("auto x = (X)this;"); 9992 // Casts in Obj-C style calls used to not be recognized as such. 9993 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 9994 9995 // FIXME: single value wrapped with paren will be treated as cast. 9996 verifyFormat("void f(int i = (kValue)*kMask) {}"); 9997 9998 verifyFormat("{ (void)F; }"); 9999 10000 // Don't break after a cast's 10001 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10002 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 10003 " bbbbbbbbbbbbbbbbbbbbbb);"); 10004 10005 // These are not casts. 10006 verifyFormat("void f(int *) {}"); 10007 verifyFormat("f(foo)->b;"); 10008 verifyFormat("f(foo).b;"); 10009 verifyFormat("f(foo)(b);"); 10010 verifyFormat("f(foo)[b];"); 10011 verifyFormat("[](foo) { return 4; }(bar);"); 10012 verifyFormat("(*funptr)(foo)[4];"); 10013 verifyFormat("funptrs[4](foo)[4];"); 10014 verifyFormat("void f(int *);"); 10015 verifyFormat("void f(int *) = 0;"); 10016 verifyFormat("void f(SmallVector<int>) {}"); 10017 verifyFormat("void f(SmallVector<int>);"); 10018 verifyFormat("void f(SmallVector<int>) = 0;"); 10019 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 10020 verifyFormat("int a = sizeof(int) * b;"); 10021 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 10022 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 10023 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 10024 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 10025 10026 // These are not casts, but at some point were confused with casts. 10027 verifyFormat("virtual void foo(int *) override;"); 10028 verifyFormat("virtual void foo(char &) const;"); 10029 verifyFormat("virtual void foo(int *a, char *) const;"); 10030 verifyFormat("int a = sizeof(int *) + b;"); 10031 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10032 verifyFormat("bool b = f(g<int>) && c;"); 10033 verifyFormat("typedef void (*f)(int i) func;"); 10034 verifyFormat("void operator++(int) noexcept;"); 10035 verifyFormat("void operator++(int &) noexcept;"); 10036 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10037 "&) noexcept;"); 10038 verifyFormat( 10039 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10040 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10041 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10042 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10043 verifyFormat("void operator delete(foo &) noexcept;"); 10044 verifyFormat("void operator delete(foo) noexcept;"); 10045 verifyFormat("void operator delete(int) noexcept;"); 10046 verifyFormat("void operator delete(int &) noexcept;"); 10047 verifyFormat("void operator delete(int &) volatile noexcept;"); 10048 verifyFormat("void operator delete(int &) const"); 10049 verifyFormat("void operator delete(int &) = default"); 10050 verifyFormat("void operator delete(int &) = delete"); 10051 verifyFormat("void operator delete(int &) [[noreturn]]"); 10052 verifyFormat("void operator delete(int &) throw();"); 10053 verifyFormat("void operator delete(int &) throw(int);"); 10054 verifyFormat("auto operator delete(int &) -> int;"); 10055 verifyFormat("auto operator delete(int &) override"); 10056 verifyFormat("auto operator delete(int &) final"); 10057 10058 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10059 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10060 // FIXME: The indentation here is not ideal. 10061 verifyFormat( 10062 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10063 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10064 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10065 } 10066 10067 TEST_F(FormatTest, FormatsFunctionTypes) { 10068 verifyFormat("A<bool()> a;"); 10069 verifyFormat("A<SomeType()> a;"); 10070 verifyFormat("A<void (*)(int, std::string)> a;"); 10071 verifyFormat("A<void *(int)>;"); 10072 verifyFormat("void *(*a)(int *, SomeType *);"); 10073 verifyFormat("int (*func)(void *);"); 10074 verifyFormat("void f() { int (*func)(void *); }"); 10075 verifyFormat("template <class CallbackClass>\n" 10076 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10077 10078 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10079 verifyGoogleFormat("void* (*a)(int);"); 10080 verifyGoogleFormat( 10081 "template <class CallbackClass>\n" 10082 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10083 10084 // Other constructs can look somewhat like function types: 10085 verifyFormat("A<sizeof(*x)> a;"); 10086 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10087 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10088 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10089 verifyFormat("int x = f(&h)();"); 10090 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10091 verifyFormat("std::function<\n" 10092 " LooooooooooongTemplatedType<\n" 10093 " SomeType>*(\n" 10094 " LooooooooooooooooongType type)>\n" 10095 " function;", 10096 getGoogleStyleWithColumns(40)); 10097 } 10098 10099 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10100 verifyFormat("A (*foo_)[6];"); 10101 verifyFormat("vector<int> (*foo_)[6];"); 10102 } 10103 10104 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10105 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10106 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10107 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10108 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10109 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10110 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10111 10112 // Different ways of ()-initializiation. 10113 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10114 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10115 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10116 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10117 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10118 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10119 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10120 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10121 10122 // Lambdas should not confuse the variable declaration heuristic. 10123 verifyFormat("LooooooooooooooooongType\n" 10124 " variable(nullptr, [](A *a) {});", 10125 getLLVMStyleWithColumns(40)); 10126 } 10127 10128 TEST_F(FormatTest, BreaksLongDeclarations) { 10129 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10130 " AnotherNameForTheLongType;"); 10131 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10133 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10134 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10135 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10136 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10137 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10138 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10139 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10140 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10141 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10142 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10143 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10144 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10145 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10146 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10147 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10148 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10149 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10150 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10151 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10152 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10153 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10154 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10155 FormatStyle Indented = getLLVMStyle(); 10156 Indented.IndentWrappedFunctionNames = true; 10157 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10158 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10159 Indented); 10160 verifyFormat( 10161 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10162 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10163 Indented); 10164 verifyFormat( 10165 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10166 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10167 Indented); 10168 verifyFormat( 10169 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10170 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10171 Indented); 10172 10173 // FIXME: Without the comment, this breaks after "(". 10174 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10175 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10176 getGoogleStyle()); 10177 10178 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10179 " int LoooooooooooooooooooongParam2) {}"); 10180 verifyFormat( 10181 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10182 " SourceLocation L, IdentifierIn *II,\n" 10183 " Type *T) {}"); 10184 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10185 "ReallyReaaallyLongFunctionName(\n" 10186 " const std::string &SomeParameter,\n" 10187 " const SomeType<string, SomeOtherTemplateParameter>\n" 10188 " &ReallyReallyLongParameterName,\n" 10189 " const SomeType<string, SomeOtherTemplateParameter>\n" 10190 " &AnotherLongParameterName) {}"); 10191 verifyFormat("template <typename A>\n" 10192 "SomeLoooooooooooooooooooooongType<\n" 10193 " typename some_namespace::SomeOtherType<A>::Type>\n" 10194 "Function() {}"); 10195 10196 verifyGoogleFormat( 10197 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10198 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10199 verifyGoogleFormat( 10200 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10201 " SourceLocation L) {}"); 10202 verifyGoogleFormat( 10203 "some_namespace::LongReturnType\n" 10204 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10205 " int first_long_parameter, int second_parameter) {}"); 10206 10207 verifyGoogleFormat("template <typename T>\n" 10208 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10209 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10210 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10211 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10212 10213 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10214 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10215 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10216 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10217 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10218 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10219 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10220 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10223 10224 verifyFormat("template <typename T> // Templates on own line.\n" 10225 "static int // Some comment.\n" 10226 "MyFunction(int a);", 10227 getLLVMStyle()); 10228 } 10229 10230 TEST_F(FormatTest, FormatsAccessModifiers) { 10231 FormatStyle Style = getLLVMStyle(); 10232 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10233 FormatStyle::ELBAMS_LogicalBlock); 10234 verifyFormat("struct foo {\n" 10235 "private:\n" 10236 " void f() {}\n" 10237 "\n" 10238 "private:\n" 10239 " int i;\n" 10240 "\n" 10241 "protected:\n" 10242 " int j;\n" 10243 "};\n", 10244 Style); 10245 verifyFormat("struct foo {\n" 10246 "private:\n" 10247 " void f() {}\n" 10248 "\n" 10249 "private:\n" 10250 " int i;\n" 10251 "\n" 10252 "protected:\n" 10253 " int j;\n" 10254 "};\n", 10255 "struct foo {\n" 10256 "private:\n" 10257 " void f() {}\n" 10258 "private:\n" 10259 " int i;\n" 10260 "protected:\n" 10261 " int j;\n" 10262 "};\n", 10263 Style); 10264 verifyFormat("struct foo { /* comment */\n" 10265 "private:\n" 10266 " int i;\n" 10267 " // comment\n" 10268 "private:\n" 10269 " int j;\n" 10270 "};\n", 10271 Style); 10272 verifyFormat("struct foo {\n" 10273 "#ifdef FOO\n" 10274 "#endif\n" 10275 "private:\n" 10276 " int i;\n" 10277 "#ifdef FOO\n" 10278 "private:\n" 10279 "#endif\n" 10280 " int j;\n" 10281 "};\n", 10282 Style); 10283 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10284 verifyFormat("struct foo {\n" 10285 "private:\n" 10286 " void f() {}\n" 10287 "private:\n" 10288 " int i;\n" 10289 "protected:\n" 10290 " int j;\n" 10291 "};\n", 10292 Style); 10293 verifyFormat("struct foo {\n" 10294 "private:\n" 10295 " void f() {}\n" 10296 "private:\n" 10297 " int i;\n" 10298 "protected:\n" 10299 " int j;\n" 10300 "};\n", 10301 "struct foo {\n" 10302 "\n" 10303 "private:\n" 10304 " void f() {}\n" 10305 "\n" 10306 "private:\n" 10307 " int i;\n" 10308 "\n" 10309 "protected:\n" 10310 " int j;\n" 10311 "};\n", 10312 Style); 10313 verifyFormat("struct foo { /* comment */\n" 10314 "private:\n" 10315 " int i;\n" 10316 " // comment\n" 10317 "private:\n" 10318 " int j;\n" 10319 "};\n", 10320 "struct foo { /* comment */\n" 10321 "\n" 10322 "private:\n" 10323 " int i;\n" 10324 " // comment\n" 10325 "\n" 10326 "private:\n" 10327 " int j;\n" 10328 "};\n", 10329 Style); 10330 verifyFormat("struct foo {\n" 10331 "#ifdef FOO\n" 10332 "#endif\n" 10333 "private:\n" 10334 " int i;\n" 10335 "#ifdef FOO\n" 10336 "private:\n" 10337 "#endif\n" 10338 " int j;\n" 10339 "};\n", 10340 "struct foo {\n" 10341 "#ifdef FOO\n" 10342 "#endif\n" 10343 "\n" 10344 "private:\n" 10345 " int i;\n" 10346 "#ifdef FOO\n" 10347 "\n" 10348 "private:\n" 10349 "#endif\n" 10350 " int j;\n" 10351 "};\n", 10352 Style); 10353 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10354 verifyFormat("struct foo {\n" 10355 "private:\n" 10356 " void f() {}\n" 10357 "\n" 10358 "private:\n" 10359 " int i;\n" 10360 "\n" 10361 "protected:\n" 10362 " int j;\n" 10363 "};\n", 10364 Style); 10365 verifyFormat("struct foo {\n" 10366 "private:\n" 10367 " void f() {}\n" 10368 "\n" 10369 "private:\n" 10370 " int i;\n" 10371 "\n" 10372 "protected:\n" 10373 " int j;\n" 10374 "};\n", 10375 "struct foo {\n" 10376 "private:\n" 10377 " void f() {}\n" 10378 "private:\n" 10379 " int i;\n" 10380 "protected:\n" 10381 " int j;\n" 10382 "};\n", 10383 Style); 10384 verifyFormat("struct foo { /* comment */\n" 10385 "private:\n" 10386 " int i;\n" 10387 " // comment\n" 10388 "\n" 10389 "private:\n" 10390 " int j;\n" 10391 "};\n", 10392 "struct foo { /* comment */\n" 10393 "private:\n" 10394 " int i;\n" 10395 " // comment\n" 10396 "\n" 10397 "private:\n" 10398 " int j;\n" 10399 "};\n", 10400 Style); 10401 verifyFormat("struct foo {\n" 10402 "#ifdef FOO\n" 10403 "#endif\n" 10404 "\n" 10405 "private:\n" 10406 " int i;\n" 10407 "#ifdef FOO\n" 10408 "\n" 10409 "private:\n" 10410 "#endif\n" 10411 " int j;\n" 10412 "};\n", 10413 "struct foo {\n" 10414 "#ifdef FOO\n" 10415 "#endif\n" 10416 "private:\n" 10417 " int i;\n" 10418 "#ifdef FOO\n" 10419 "private:\n" 10420 "#endif\n" 10421 " int j;\n" 10422 "};\n", 10423 Style); 10424 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10425 EXPECT_EQ("struct foo {\n" 10426 "\n" 10427 "private:\n" 10428 " void f() {}\n" 10429 "\n" 10430 "private:\n" 10431 " int i;\n" 10432 "\n" 10433 "protected:\n" 10434 " int j;\n" 10435 "};\n", 10436 format("struct foo {\n" 10437 "\n" 10438 "private:\n" 10439 " void f() {}\n" 10440 "\n" 10441 "private:\n" 10442 " int i;\n" 10443 "\n" 10444 "protected:\n" 10445 " int j;\n" 10446 "};\n", 10447 Style)); 10448 verifyFormat("struct foo {\n" 10449 "private:\n" 10450 " void f() {}\n" 10451 "private:\n" 10452 " int i;\n" 10453 "protected:\n" 10454 " int j;\n" 10455 "};\n", 10456 Style); 10457 EXPECT_EQ("struct foo { /* comment */\n" 10458 "\n" 10459 "private:\n" 10460 " int i;\n" 10461 " // comment\n" 10462 "\n" 10463 "private:\n" 10464 " int j;\n" 10465 "};\n", 10466 format("struct foo { /* comment */\n" 10467 "\n" 10468 "private:\n" 10469 " int i;\n" 10470 " // comment\n" 10471 "\n" 10472 "private:\n" 10473 " int j;\n" 10474 "};\n", 10475 Style)); 10476 verifyFormat("struct foo { /* comment */\n" 10477 "private:\n" 10478 " int i;\n" 10479 " // comment\n" 10480 "private:\n" 10481 " int j;\n" 10482 "};\n", 10483 Style); 10484 EXPECT_EQ("struct foo {\n" 10485 "#ifdef FOO\n" 10486 "#endif\n" 10487 "\n" 10488 "private:\n" 10489 " int i;\n" 10490 "#ifdef FOO\n" 10491 "\n" 10492 "private:\n" 10493 "#endif\n" 10494 " int j;\n" 10495 "};\n", 10496 format("struct foo {\n" 10497 "#ifdef FOO\n" 10498 "#endif\n" 10499 "\n" 10500 "private:\n" 10501 " int i;\n" 10502 "#ifdef FOO\n" 10503 "\n" 10504 "private:\n" 10505 "#endif\n" 10506 " int j;\n" 10507 "};\n", 10508 Style)); 10509 verifyFormat("struct foo {\n" 10510 "#ifdef FOO\n" 10511 "#endif\n" 10512 "private:\n" 10513 " int i;\n" 10514 "#ifdef FOO\n" 10515 "private:\n" 10516 "#endif\n" 10517 " int j;\n" 10518 "};\n", 10519 Style); 10520 10521 FormatStyle NoEmptyLines = getLLVMStyle(); 10522 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10523 verifyFormat("struct foo {\n" 10524 "private:\n" 10525 " void f() {}\n" 10526 "\n" 10527 "private:\n" 10528 " int i;\n" 10529 "\n" 10530 "public:\n" 10531 "protected:\n" 10532 " int j;\n" 10533 "};\n", 10534 NoEmptyLines); 10535 10536 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10537 verifyFormat("struct foo {\n" 10538 "private:\n" 10539 " void f() {}\n" 10540 "private:\n" 10541 " int i;\n" 10542 "public:\n" 10543 "protected:\n" 10544 " int j;\n" 10545 "};\n", 10546 NoEmptyLines); 10547 10548 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10549 verifyFormat("struct foo {\n" 10550 "private:\n" 10551 " void f() {}\n" 10552 "\n" 10553 "private:\n" 10554 " int i;\n" 10555 "\n" 10556 "public:\n" 10557 "\n" 10558 "protected:\n" 10559 " int j;\n" 10560 "};\n", 10561 NoEmptyLines); 10562 } 10563 10564 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10565 10566 FormatStyle Style = getLLVMStyle(); 10567 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10568 verifyFormat("struct foo {\n" 10569 "private:\n" 10570 " void f() {}\n" 10571 "\n" 10572 "private:\n" 10573 " int i;\n" 10574 "\n" 10575 "protected:\n" 10576 " int j;\n" 10577 "};\n", 10578 Style); 10579 10580 // Check if lines are removed. 10581 verifyFormat("struct foo {\n" 10582 "private:\n" 10583 " void f() {}\n" 10584 "\n" 10585 "private:\n" 10586 " int i;\n" 10587 "\n" 10588 "protected:\n" 10589 " int j;\n" 10590 "};\n", 10591 "struct foo {\n" 10592 "private:\n" 10593 "\n" 10594 " void f() {}\n" 10595 "\n" 10596 "private:\n" 10597 "\n" 10598 " int i;\n" 10599 "\n" 10600 "protected:\n" 10601 "\n" 10602 " int j;\n" 10603 "};\n", 10604 Style); 10605 10606 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10607 verifyFormat("struct foo {\n" 10608 "private:\n" 10609 "\n" 10610 " void f() {}\n" 10611 "\n" 10612 "private:\n" 10613 "\n" 10614 " int i;\n" 10615 "\n" 10616 "protected:\n" 10617 "\n" 10618 " int j;\n" 10619 "};\n", 10620 Style); 10621 10622 // Check if lines are added. 10623 verifyFormat("struct foo {\n" 10624 "private:\n" 10625 "\n" 10626 " void f() {}\n" 10627 "\n" 10628 "private:\n" 10629 "\n" 10630 " int i;\n" 10631 "\n" 10632 "protected:\n" 10633 "\n" 10634 " int j;\n" 10635 "};\n", 10636 "struct foo {\n" 10637 "private:\n" 10638 " void f() {}\n" 10639 "\n" 10640 "private:\n" 10641 " int i;\n" 10642 "\n" 10643 "protected:\n" 10644 " int j;\n" 10645 "};\n", 10646 Style); 10647 10648 // Leave tests rely on the code layout, test::messUp can not be used. 10649 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10650 Style.MaxEmptyLinesToKeep = 0u; 10651 verifyFormat("struct foo {\n" 10652 "private:\n" 10653 " void f() {}\n" 10654 "\n" 10655 "private:\n" 10656 " int i;\n" 10657 "\n" 10658 "protected:\n" 10659 " int j;\n" 10660 "};\n", 10661 Style); 10662 10663 // Check if MaxEmptyLinesToKeep is respected. 10664 EXPECT_EQ("struct foo {\n" 10665 "private:\n" 10666 " void f() {}\n" 10667 "\n" 10668 "private:\n" 10669 " int i;\n" 10670 "\n" 10671 "protected:\n" 10672 " int j;\n" 10673 "};\n", 10674 format("struct foo {\n" 10675 "private:\n" 10676 "\n\n\n" 10677 " void f() {}\n" 10678 "\n" 10679 "private:\n" 10680 "\n\n\n" 10681 " int i;\n" 10682 "\n" 10683 "protected:\n" 10684 "\n\n\n" 10685 " int j;\n" 10686 "};\n", 10687 Style)); 10688 10689 Style.MaxEmptyLinesToKeep = 1u; 10690 EXPECT_EQ("struct foo {\n" 10691 "private:\n" 10692 "\n" 10693 " void f() {}\n" 10694 "\n" 10695 "private:\n" 10696 "\n" 10697 " int i;\n" 10698 "\n" 10699 "protected:\n" 10700 "\n" 10701 " int j;\n" 10702 "};\n", 10703 format("struct foo {\n" 10704 "private:\n" 10705 "\n" 10706 " void f() {}\n" 10707 "\n" 10708 "private:\n" 10709 "\n" 10710 " int i;\n" 10711 "\n" 10712 "protected:\n" 10713 "\n" 10714 " int j;\n" 10715 "};\n", 10716 Style)); 10717 // Check if no lines are kept. 10718 EXPECT_EQ("struct foo {\n" 10719 "private:\n" 10720 " void f() {}\n" 10721 "\n" 10722 "private:\n" 10723 " int i;\n" 10724 "\n" 10725 "protected:\n" 10726 " int j;\n" 10727 "};\n", 10728 format("struct foo {\n" 10729 "private:\n" 10730 " void f() {}\n" 10731 "\n" 10732 "private:\n" 10733 " int i;\n" 10734 "\n" 10735 "protected:\n" 10736 " int j;\n" 10737 "};\n", 10738 Style)); 10739 // Check if MaxEmptyLinesToKeep is respected. 10740 EXPECT_EQ("struct foo {\n" 10741 "private:\n" 10742 "\n" 10743 " void f() {}\n" 10744 "\n" 10745 "private:\n" 10746 "\n" 10747 " int i;\n" 10748 "\n" 10749 "protected:\n" 10750 "\n" 10751 " int j;\n" 10752 "};\n", 10753 format("struct foo {\n" 10754 "private:\n" 10755 "\n\n\n" 10756 " void f() {}\n" 10757 "\n" 10758 "private:\n" 10759 "\n\n\n" 10760 " int i;\n" 10761 "\n" 10762 "protected:\n" 10763 "\n\n\n" 10764 " int j;\n" 10765 "};\n", 10766 Style)); 10767 10768 Style.MaxEmptyLinesToKeep = 10u; 10769 EXPECT_EQ("struct foo {\n" 10770 "private:\n" 10771 "\n\n\n" 10772 " void f() {}\n" 10773 "\n" 10774 "private:\n" 10775 "\n\n\n" 10776 " int i;\n" 10777 "\n" 10778 "protected:\n" 10779 "\n\n\n" 10780 " int j;\n" 10781 "};\n", 10782 format("struct foo {\n" 10783 "private:\n" 10784 "\n\n\n" 10785 " void f() {}\n" 10786 "\n" 10787 "private:\n" 10788 "\n\n\n" 10789 " int i;\n" 10790 "\n" 10791 "protected:\n" 10792 "\n\n\n" 10793 " int j;\n" 10794 "};\n", 10795 Style)); 10796 10797 // Test with comments. 10798 Style = getLLVMStyle(); 10799 verifyFormat("struct foo {\n" 10800 "private:\n" 10801 " // comment\n" 10802 " void f() {}\n" 10803 "\n" 10804 "private: /* comment */\n" 10805 " int i;\n" 10806 "};\n", 10807 Style); 10808 verifyFormat("struct foo {\n" 10809 "private:\n" 10810 " // comment\n" 10811 " void f() {}\n" 10812 "\n" 10813 "private: /* comment */\n" 10814 " int i;\n" 10815 "};\n", 10816 "struct foo {\n" 10817 "private:\n" 10818 "\n" 10819 " // comment\n" 10820 " void f() {}\n" 10821 "\n" 10822 "private: /* comment */\n" 10823 "\n" 10824 " int i;\n" 10825 "};\n", 10826 Style); 10827 10828 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10829 verifyFormat("struct foo {\n" 10830 "private:\n" 10831 "\n" 10832 " // comment\n" 10833 " void f() {}\n" 10834 "\n" 10835 "private: /* comment */\n" 10836 "\n" 10837 " int i;\n" 10838 "};\n", 10839 "struct foo {\n" 10840 "private:\n" 10841 " // comment\n" 10842 " void f() {}\n" 10843 "\n" 10844 "private: /* comment */\n" 10845 " int i;\n" 10846 "};\n", 10847 Style); 10848 verifyFormat("struct foo {\n" 10849 "private:\n" 10850 "\n" 10851 " // comment\n" 10852 " void f() {}\n" 10853 "\n" 10854 "private: /* comment */\n" 10855 "\n" 10856 " int i;\n" 10857 "};\n", 10858 Style); 10859 10860 // Test with preprocessor defines. 10861 Style = getLLVMStyle(); 10862 verifyFormat("struct foo {\n" 10863 "private:\n" 10864 "#ifdef FOO\n" 10865 "#endif\n" 10866 " void f() {}\n" 10867 "};\n", 10868 Style); 10869 verifyFormat("struct foo {\n" 10870 "private:\n" 10871 "#ifdef FOO\n" 10872 "#endif\n" 10873 " void f() {}\n" 10874 "};\n", 10875 "struct foo {\n" 10876 "private:\n" 10877 "\n" 10878 "#ifdef FOO\n" 10879 "#endif\n" 10880 " void f() {}\n" 10881 "};\n", 10882 Style); 10883 10884 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10885 verifyFormat("struct foo {\n" 10886 "private:\n" 10887 "\n" 10888 "#ifdef FOO\n" 10889 "#endif\n" 10890 " void f() {}\n" 10891 "};\n", 10892 "struct foo {\n" 10893 "private:\n" 10894 "#ifdef FOO\n" 10895 "#endif\n" 10896 " void f() {}\n" 10897 "};\n", 10898 Style); 10899 verifyFormat("struct foo {\n" 10900 "private:\n" 10901 "\n" 10902 "#ifdef FOO\n" 10903 "#endif\n" 10904 " void f() {}\n" 10905 "};\n", 10906 Style); 10907 } 10908 10909 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10910 // Combined tests of EmptyLineAfterAccessModifier and 10911 // EmptyLineBeforeAccessModifier. 10912 FormatStyle Style = getLLVMStyle(); 10913 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10914 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10915 verifyFormat("struct foo {\n" 10916 "private:\n" 10917 "\n" 10918 "protected:\n" 10919 "};\n", 10920 Style); 10921 10922 Style.MaxEmptyLinesToKeep = 10u; 10923 // Both remove all new lines. 10924 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10925 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10926 verifyFormat("struct foo {\n" 10927 "private:\n" 10928 "protected:\n" 10929 "};\n", 10930 "struct foo {\n" 10931 "private:\n" 10932 "\n\n\n" 10933 "protected:\n" 10934 "};\n", 10935 Style); 10936 10937 // Leave tests rely on the code layout, test::messUp can not be used. 10938 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10939 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10940 Style.MaxEmptyLinesToKeep = 10u; 10941 EXPECT_EQ("struct foo {\n" 10942 "private:\n" 10943 "\n\n\n" 10944 "protected:\n" 10945 "};\n", 10946 format("struct foo {\n" 10947 "private:\n" 10948 "\n\n\n" 10949 "protected:\n" 10950 "};\n", 10951 Style)); 10952 Style.MaxEmptyLinesToKeep = 3u; 10953 EXPECT_EQ("struct foo {\n" 10954 "private:\n" 10955 "\n\n\n" 10956 "protected:\n" 10957 "};\n", 10958 format("struct foo {\n" 10959 "private:\n" 10960 "\n\n\n" 10961 "protected:\n" 10962 "};\n", 10963 Style)); 10964 Style.MaxEmptyLinesToKeep = 1u; 10965 EXPECT_EQ("struct foo {\n" 10966 "private:\n" 10967 "\n\n\n" 10968 "protected:\n" 10969 "};\n", 10970 format("struct foo {\n" 10971 "private:\n" 10972 "\n\n\n" 10973 "protected:\n" 10974 "};\n", 10975 Style)); // Based on new lines in original document and not 10976 // on the setting. 10977 10978 Style.MaxEmptyLinesToKeep = 10u; 10979 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10980 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10981 // Newlines are kept if they are greater than zero, 10982 // test::messUp removes all new lines which changes the logic 10983 EXPECT_EQ("struct foo {\n" 10984 "private:\n" 10985 "\n\n\n" 10986 "protected:\n" 10987 "};\n", 10988 format("struct foo {\n" 10989 "private:\n" 10990 "\n\n\n" 10991 "protected:\n" 10992 "};\n", 10993 Style)); 10994 10995 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10996 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10997 // test::messUp removes all new lines which changes the logic 10998 EXPECT_EQ("struct foo {\n" 10999 "private:\n" 11000 "\n\n\n" 11001 "protected:\n" 11002 "};\n", 11003 format("struct foo {\n" 11004 "private:\n" 11005 "\n\n\n" 11006 "protected:\n" 11007 "};\n", 11008 Style)); 11009 11010 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11011 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11012 EXPECT_EQ("struct foo {\n" 11013 "private:\n" 11014 "\n\n\n" 11015 "protected:\n" 11016 "};\n", 11017 format("struct foo {\n" 11018 "private:\n" 11019 "\n\n\n" 11020 "protected:\n" 11021 "};\n", 11022 Style)); // test::messUp removes all new lines which changes 11023 // the logic. 11024 11025 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11026 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11027 verifyFormat("struct foo {\n" 11028 "private:\n" 11029 "protected:\n" 11030 "};\n", 11031 "struct foo {\n" 11032 "private:\n" 11033 "\n\n\n" 11034 "protected:\n" 11035 "};\n", 11036 Style); 11037 11038 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11039 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11040 EXPECT_EQ("struct foo {\n" 11041 "private:\n" 11042 "\n\n\n" 11043 "protected:\n" 11044 "};\n", 11045 format("struct foo {\n" 11046 "private:\n" 11047 "\n\n\n" 11048 "protected:\n" 11049 "};\n", 11050 Style)); // test::messUp removes all new lines which changes 11051 // the logic. 11052 11053 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11054 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11055 verifyFormat("struct foo {\n" 11056 "private:\n" 11057 "protected:\n" 11058 "};\n", 11059 "struct foo {\n" 11060 "private:\n" 11061 "\n\n\n" 11062 "protected:\n" 11063 "};\n", 11064 Style); 11065 11066 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11067 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11068 verifyFormat("struct foo {\n" 11069 "private:\n" 11070 "protected:\n" 11071 "};\n", 11072 "struct foo {\n" 11073 "private:\n" 11074 "\n\n\n" 11075 "protected:\n" 11076 "};\n", 11077 Style); 11078 11079 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11080 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11081 verifyFormat("struct foo {\n" 11082 "private:\n" 11083 "protected:\n" 11084 "};\n", 11085 "struct foo {\n" 11086 "private:\n" 11087 "\n\n\n" 11088 "protected:\n" 11089 "};\n", 11090 Style); 11091 11092 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11093 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11094 verifyFormat("struct foo {\n" 11095 "private:\n" 11096 "protected:\n" 11097 "};\n", 11098 "struct foo {\n" 11099 "private:\n" 11100 "\n\n\n" 11101 "protected:\n" 11102 "};\n", 11103 Style); 11104 } 11105 11106 TEST_F(FormatTest, FormatsArrays) { 11107 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11108 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11109 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11110 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11111 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11112 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11113 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11114 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11115 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11116 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11117 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11118 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11119 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11120 verifyFormat( 11121 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11122 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11123 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11124 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11125 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11126 11127 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11128 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11129 verifyFormat( 11130 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11131 " .aaaaaaa[0]\n" 11132 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11133 verifyFormat("a[::b::c];"); 11134 11135 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11136 11137 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11138 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11139 } 11140 11141 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11142 verifyFormat("(a)->b();"); 11143 verifyFormat("--a;"); 11144 } 11145 11146 TEST_F(FormatTest, HandlesIncludeDirectives) { 11147 verifyFormat("#include <string>\n" 11148 "#include <a/b/c.h>\n" 11149 "#include \"a/b/string\"\n" 11150 "#include \"string.h\"\n" 11151 "#include \"string.h\"\n" 11152 "#include <a-a>\n" 11153 "#include < path with space >\n" 11154 "#include_next <test.h>" 11155 "#include \"abc.h\" // this is included for ABC\n" 11156 "#include \"some long include\" // with a comment\n" 11157 "#include \"some very long include path\"\n" 11158 "#include <some/very/long/include/path>\n", 11159 getLLVMStyleWithColumns(35)); 11160 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11161 EXPECT_EQ("#include <a>", format("#include<a>")); 11162 11163 verifyFormat("#import <string>"); 11164 verifyFormat("#import <a/b/c.h>"); 11165 verifyFormat("#import \"a/b/string\""); 11166 verifyFormat("#import \"string.h\""); 11167 verifyFormat("#import \"string.h\""); 11168 verifyFormat("#if __has_include(<strstream>)\n" 11169 "#include <strstream>\n" 11170 "#endif"); 11171 11172 verifyFormat("#define MY_IMPORT <a/b>"); 11173 11174 verifyFormat("#if __has_include(<a/b>)"); 11175 verifyFormat("#if __has_include_next(<a/b>)"); 11176 verifyFormat("#define F __has_include(<a/b>)"); 11177 verifyFormat("#define F __has_include_next(<a/b>)"); 11178 11179 // Protocol buffer definition or missing "#". 11180 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11181 getLLVMStyleWithColumns(30)); 11182 11183 FormatStyle Style = getLLVMStyle(); 11184 Style.AlwaysBreakBeforeMultilineStrings = true; 11185 Style.ColumnLimit = 0; 11186 verifyFormat("#import \"abc.h\"", Style); 11187 11188 // But 'import' might also be a regular C++ namespace. 11189 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11191 } 11192 11193 //===----------------------------------------------------------------------===// 11194 // Error recovery tests. 11195 //===----------------------------------------------------------------------===// 11196 11197 TEST_F(FormatTest, IncompleteParameterLists) { 11198 FormatStyle NoBinPacking = getLLVMStyle(); 11199 NoBinPacking.BinPackParameters = false; 11200 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11201 " double *min_x,\n" 11202 " double *max_x,\n" 11203 " double *min_y,\n" 11204 " double *max_y,\n" 11205 " double *min_z,\n" 11206 " double *max_z, ) {}", 11207 NoBinPacking); 11208 } 11209 11210 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11211 verifyFormat("void f() { return; }\n42"); 11212 verifyFormat("void f() {\n" 11213 " if (0)\n" 11214 " return;\n" 11215 "}\n" 11216 "42"); 11217 verifyFormat("void f() { return }\n42"); 11218 verifyFormat("void f() {\n" 11219 " if (0)\n" 11220 " return\n" 11221 "}\n" 11222 "42"); 11223 } 11224 11225 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11226 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11227 EXPECT_EQ("void f() {\n" 11228 " if (a)\n" 11229 " return\n" 11230 "}", 11231 format("void f ( ) { if ( a ) return }")); 11232 EXPECT_EQ("namespace N {\n" 11233 "void f()\n" 11234 "}", 11235 format("namespace N { void f() }")); 11236 EXPECT_EQ("namespace N {\n" 11237 "void f() {}\n" 11238 "void g()\n" 11239 "} // namespace N", 11240 format("namespace N { void f( ) { } void g( ) }")); 11241 } 11242 11243 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11244 verifyFormat("int aaaaaaaa =\n" 11245 " // Overlylongcomment\n" 11246 " b;", 11247 getLLVMStyleWithColumns(20)); 11248 verifyFormat("function(\n" 11249 " ShortArgument,\n" 11250 " LoooooooooooongArgument);\n", 11251 getLLVMStyleWithColumns(20)); 11252 } 11253 11254 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11255 verifyFormat("public:"); 11256 verifyFormat("class A {\n" 11257 "public\n" 11258 " void f() {}\n" 11259 "};"); 11260 verifyFormat("public\n" 11261 "int qwerty;"); 11262 verifyFormat("public\n" 11263 "B {}"); 11264 verifyFormat("public\n" 11265 "{}"); 11266 verifyFormat("public\n" 11267 "B { int x; }"); 11268 } 11269 11270 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11271 verifyFormat("{"); 11272 verifyFormat("#})"); 11273 verifyNoCrash("(/**/[:!] ?[)."); 11274 } 11275 11276 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11277 // Found by oss-fuzz: 11278 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11279 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11280 Style.ColumnLimit = 60; 11281 verifyNoCrash( 11282 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11283 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11284 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11285 Style); 11286 } 11287 11288 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11289 verifyFormat("do {\n}"); 11290 verifyFormat("do {\n}\n" 11291 "f();"); 11292 verifyFormat("do {\n}\n" 11293 "wheeee(fun);"); 11294 verifyFormat("do {\n" 11295 " f();\n" 11296 "}"); 11297 } 11298 11299 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11300 verifyFormat("if {\n foo;\n foo();\n}"); 11301 verifyFormat("switch {\n foo;\n foo();\n}"); 11302 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11303 verifyFormat("while {\n foo;\n foo();\n}"); 11304 verifyFormat("do {\n foo;\n foo();\n} while;"); 11305 } 11306 11307 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11308 verifyIncompleteFormat("namespace {\n" 11309 "class Foo { Foo (\n" 11310 "};\n" 11311 "} // namespace"); 11312 } 11313 11314 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11315 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11316 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11317 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11318 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11319 11320 EXPECT_EQ("{\n" 11321 " {\n" 11322 " breakme(\n" 11323 " qwe);\n" 11324 " }\n", 11325 format("{\n" 11326 " {\n" 11327 " breakme(qwe);\n" 11328 "}\n", 11329 getLLVMStyleWithColumns(10))); 11330 } 11331 11332 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11333 verifyFormat("int x = {\n" 11334 " avariable,\n" 11335 " b(alongervariable)};", 11336 getLLVMStyleWithColumns(25)); 11337 } 11338 11339 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11340 verifyFormat("return (a)(b){1, 2, 3};"); 11341 } 11342 11343 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11344 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11345 verifyFormat("vector<int> x{\n" 11346 " 1,\n" 11347 " 2,\n" 11348 " 3,\n" 11349 " 4,\n" 11350 "};"); 11351 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11352 verifyFormat("f({1, 2});"); 11353 verifyFormat("auto v = Foo{-1};"); 11354 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11355 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11356 verifyFormat("new vector<int>{1, 2, 3};"); 11357 verifyFormat("new int[3]{1, 2, 3};"); 11358 verifyFormat("new int{1};"); 11359 verifyFormat("return {arg1, arg2};"); 11360 verifyFormat("return {arg1, SomeType{parameter}};"); 11361 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11362 verifyFormat("new T{arg1, arg2};"); 11363 verifyFormat("f(MyMap[{composite, key}]);"); 11364 verifyFormat("class Class {\n" 11365 " T member = {arg1, arg2};\n" 11366 "};"); 11367 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11368 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11369 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11370 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11371 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11372 11373 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11374 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11375 verifyFormat("auto i = decltype(x){};"); 11376 verifyFormat("auto i = typeof(x){};"); 11377 verifyFormat("auto i = _Atomic(x){};"); 11378 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11379 verifyFormat("Node n{1, Node{1000}, //\n" 11380 " 2};"); 11381 verifyFormat("Aaaa aaaaaaa{\n" 11382 " {\n" 11383 " aaaa,\n" 11384 " },\n" 11385 "};"); 11386 verifyFormat("class C : public D {\n" 11387 " SomeClass SC{2};\n" 11388 "};"); 11389 verifyFormat("class C : public A {\n" 11390 " class D : public B {\n" 11391 " void f() { int i{2}; }\n" 11392 " };\n" 11393 "};"); 11394 verifyFormat("#define A {a, a},"); 11395 11396 // Avoid breaking between equal sign and opening brace 11397 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11398 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11399 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11400 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11401 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11402 " {\"ccccccccccccccccccccc\", 2}};", 11403 AvoidBreakingFirstArgument); 11404 11405 // Binpacking only if there is no trailing comma 11406 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11407 " cccccccccc, dddddddddd};", 11408 getLLVMStyleWithColumns(50)); 11409 verifyFormat("const Aaaaaa aaaaa = {\n" 11410 " aaaaaaaaaaa,\n" 11411 " bbbbbbbbbbb,\n" 11412 " ccccccccccc,\n" 11413 " ddddddddddd,\n" 11414 "};", 11415 getLLVMStyleWithColumns(50)); 11416 11417 // Cases where distinguising braced lists and blocks is hard. 11418 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11419 verifyFormat("void f() {\n" 11420 " return; // comment\n" 11421 "}\n" 11422 "SomeType t;"); 11423 verifyFormat("void f() {\n" 11424 " if (a) {\n" 11425 " f();\n" 11426 " }\n" 11427 "}\n" 11428 "SomeType t;"); 11429 11430 // In combination with BinPackArguments = false. 11431 FormatStyle NoBinPacking = getLLVMStyle(); 11432 NoBinPacking.BinPackArguments = false; 11433 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11434 " bbbbb,\n" 11435 " ccccc,\n" 11436 " ddddd,\n" 11437 " eeeee,\n" 11438 " ffffff,\n" 11439 " ggggg,\n" 11440 " hhhhhh,\n" 11441 " iiiiii,\n" 11442 " jjjjjj,\n" 11443 " kkkkkk};", 11444 NoBinPacking); 11445 verifyFormat("const Aaaaaa aaaaa = {\n" 11446 " aaaaa,\n" 11447 " bbbbb,\n" 11448 " ccccc,\n" 11449 " ddddd,\n" 11450 " eeeee,\n" 11451 " ffffff,\n" 11452 " ggggg,\n" 11453 " hhhhhh,\n" 11454 " iiiiii,\n" 11455 " jjjjjj,\n" 11456 " kkkkkk,\n" 11457 "};", 11458 NoBinPacking); 11459 verifyFormat( 11460 "const Aaaaaa aaaaa = {\n" 11461 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11462 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11463 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11464 "};", 11465 NoBinPacking); 11466 11467 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11468 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11469 " CDDDP83848_BMCR_REGISTER,\n" 11470 " CDDDP83848_BMSR_REGISTER,\n" 11471 " CDDDP83848_RBR_REGISTER};", 11472 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11473 " CDDDP83848_BMSR_REGISTER,\n" 11474 " CDDDP83848_RBR_REGISTER};", 11475 NoBinPacking)); 11476 11477 // FIXME: The alignment of these trailing comments might be bad. Then again, 11478 // this might be utterly useless in real code. 11479 verifyFormat("Constructor::Constructor()\n" 11480 " : some_value{ //\n" 11481 " aaaaaaa, //\n" 11482 " bbbbbbb} {}"); 11483 11484 // In braced lists, the first comment is always assumed to belong to the 11485 // first element. Thus, it can be moved to the next or previous line as 11486 // appropriate. 11487 EXPECT_EQ("function({// First element:\n" 11488 " 1,\n" 11489 " // Second element:\n" 11490 " 2});", 11491 format("function({\n" 11492 " // First element:\n" 11493 " 1,\n" 11494 " // Second element:\n" 11495 " 2});")); 11496 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11497 " // First element:\n" 11498 " 1,\n" 11499 " // Second element:\n" 11500 " 2};", 11501 format("std::vector<int> MyNumbers{// First element:\n" 11502 " 1,\n" 11503 " // Second element:\n" 11504 " 2};", 11505 getLLVMStyleWithColumns(30))); 11506 // A trailing comma should still lead to an enforced line break and no 11507 // binpacking. 11508 EXPECT_EQ("vector<int> SomeVector = {\n" 11509 " // aaa\n" 11510 " 1,\n" 11511 " 2,\n" 11512 "};", 11513 format("vector<int> SomeVector = { // aaa\n" 11514 " 1, 2, };")); 11515 11516 // C++11 brace initializer list l-braces should not be treated any differently 11517 // when breaking before lambda bodies is enabled 11518 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11519 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11520 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11521 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11522 verifyFormat( 11523 "std::runtime_error{\n" 11524 " \"Long string which will force a break onto the next line...\"};", 11525 BreakBeforeLambdaBody); 11526 11527 FormatStyle ExtraSpaces = getLLVMStyle(); 11528 ExtraSpaces.Cpp11BracedListStyle = false; 11529 ExtraSpaces.ColumnLimit = 75; 11530 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11531 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11532 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11533 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11534 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11535 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11536 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11537 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11538 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11539 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11540 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11541 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11542 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11543 verifyFormat("class Class {\n" 11544 " T member = { arg1, arg2 };\n" 11545 "};", 11546 ExtraSpaces); 11547 verifyFormat( 11548 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11549 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11550 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11551 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11552 ExtraSpaces); 11553 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11554 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11555 ExtraSpaces); 11556 verifyFormat( 11557 "someFunction(OtherParam,\n" 11558 " BracedList{ // comment 1 (Forcing interesting break)\n" 11559 " param1, param2,\n" 11560 " // comment 2\n" 11561 " param3, param4 });", 11562 ExtraSpaces); 11563 verifyFormat( 11564 "std::this_thread::sleep_for(\n" 11565 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11566 ExtraSpaces); 11567 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11568 " aaaaaaa,\n" 11569 " aaaaaaaaaa,\n" 11570 " aaaaa,\n" 11571 " aaaaaaaaaaaaaaa,\n" 11572 " aaa,\n" 11573 " aaaaaaaaaa,\n" 11574 " a,\n" 11575 " aaaaaaaaaaaaaaaaaaaaa,\n" 11576 " aaaaaaaaaaaa,\n" 11577 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11578 " aaaaaaa,\n" 11579 " a};"); 11580 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11581 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11582 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11583 11584 // Avoid breaking between initializer/equal sign and opening brace 11585 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11586 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11587 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11588 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11589 " { \"ccccccccccccccccccccc\", 2 }\n" 11590 "};", 11591 ExtraSpaces); 11592 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11593 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11594 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11595 " { \"ccccccccccccccccccccc\", 2 }\n" 11596 "};", 11597 ExtraSpaces); 11598 11599 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11600 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11601 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11602 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11603 11604 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11605 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11606 SpaceBetweenBraces.SpacesInParentheses = true; 11607 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11608 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11609 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11610 verifyFormat("vector< int > x{ // comment 1\n" 11611 " 1, 2, 3, 4 };", 11612 SpaceBetweenBraces); 11613 SpaceBetweenBraces.ColumnLimit = 20; 11614 EXPECT_EQ("vector< int > x{\n" 11615 " 1, 2, 3, 4 };", 11616 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11617 SpaceBetweenBraces.ColumnLimit = 24; 11618 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11619 " 3, 4 };", 11620 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11621 EXPECT_EQ("vector< int > x{\n" 11622 " 1,\n" 11623 " 2,\n" 11624 " 3,\n" 11625 " 4,\n" 11626 "};", 11627 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11628 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11629 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11630 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11631 } 11632 11633 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11634 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11635 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11636 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11637 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11638 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11639 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11640 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11641 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11642 " 1, 22, 333, 4444, 55555, //\n" 11643 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11644 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11645 verifyFormat( 11646 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11647 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11648 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11649 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11650 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11651 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11652 " 7777777};"); 11653 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11654 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11655 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11656 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11657 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11658 " // Separating comment.\n" 11659 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11660 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11661 " // Leading comment\n" 11662 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11663 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11664 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11665 " 1, 1, 1, 1};", 11666 getLLVMStyleWithColumns(39)); 11667 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11668 " 1, 1, 1, 1};", 11669 getLLVMStyleWithColumns(38)); 11670 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11671 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11672 getLLVMStyleWithColumns(43)); 11673 verifyFormat( 11674 "static unsigned SomeValues[10][3] = {\n" 11675 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11676 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11677 verifyFormat("static auto fields = new vector<string>{\n" 11678 " \"aaaaaaaaaaaaa\",\n" 11679 " \"aaaaaaaaaaaaa\",\n" 11680 " \"aaaaaaaaaaaa\",\n" 11681 " \"aaaaaaaaaaaaaa\",\n" 11682 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11683 " \"aaaaaaaaaaaa\",\n" 11684 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11685 "};"); 11686 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11687 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11688 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11689 " 3, cccccccccccccccccccccc};", 11690 getLLVMStyleWithColumns(60)); 11691 11692 // Trailing commas. 11693 verifyFormat("vector<int> x = {\n" 11694 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11695 "};", 11696 getLLVMStyleWithColumns(39)); 11697 verifyFormat("vector<int> x = {\n" 11698 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11699 "};", 11700 getLLVMStyleWithColumns(39)); 11701 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11702 " 1, 1, 1, 1,\n" 11703 " /**/ /**/};", 11704 getLLVMStyleWithColumns(39)); 11705 11706 // Trailing comment in the first line. 11707 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11708 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11709 " 111111111, 222222222, 3333333333, 444444444, //\n" 11710 " 11111111, 22222222, 333333333, 44444444};"); 11711 // Trailing comment in the last line. 11712 verifyFormat("int aaaaa[] = {\n" 11713 " 1, 2, 3, // comment\n" 11714 " 4, 5, 6 // comment\n" 11715 "};"); 11716 11717 // With nested lists, we should either format one item per line or all nested 11718 // lists one on line. 11719 // FIXME: For some nested lists, we can do better. 11720 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11721 " {aaaaaaaaaaaaaaaaaaa},\n" 11722 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11723 " {aaaaaaaaaaaaaaaaa}};", 11724 getLLVMStyleWithColumns(60)); 11725 verifyFormat( 11726 "SomeStruct my_struct_array = {\n" 11727 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11728 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11729 " {aaa, aaa},\n" 11730 " {aaa, aaa},\n" 11731 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11732 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11733 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11734 11735 // No column layout should be used here. 11736 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11737 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11738 11739 verifyNoCrash("a<,"); 11740 11741 // No braced initializer here. 11742 verifyFormat("void f() {\n" 11743 " struct Dummy {};\n" 11744 " f(v);\n" 11745 "}"); 11746 11747 // Long lists should be formatted in columns even if they are nested. 11748 verifyFormat( 11749 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11750 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11751 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11752 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11753 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11754 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11755 11756 // Allow "single-column" layout even if that violates the column limit. There 11757 // isn't going to be a better way. 11758 verifyFormat("std::vector<int> a = {\n" 11759 " aaaaaaaa,\n" 11760 " aaaaaaaa,\n" 11761 " aaaaaaaa,\n" 11762 " aaaaaaaa,\n" 11763 " aaaaaaaaaa,\n" 11764 " aaaaaaaa,\n" 11765 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11766 getLLVMStyleWithColumns(30)); 11767 verifyFormat("vector<int> aaaa = {\n" 11768 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11769 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11770 " aaaaaa.aaaaaaa,\n" 11771 " aaaaaa.aaaaaaa,\n" 11772 " aaaaaa.aaaaaaa,\n" 11773 " aaaaaa.aaaaaaa,\n" 11774 "};"); 11775 11776 // Don't create hanging lists. 11777 verifyFormat("someFunction(Param, {List1, List2,\n" 11778 " List3});", 11779 getLLVMStyleWithColumns(35)); 11780 verifyFormat("someFunction(Param, Param,\n" 11781 " {List1, List2,\n" 11782 " List3});", 11783 getLLVMStyleWithColumns(35)); 11784 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11785 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11786 } 11787 11788 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11789 FormatStyle DoNotMerge = getLLVMStyle(); 11790 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11791 11792 verifyFormat("void f() { return 42; }"); 11793 verifyFormat("void f() {\n" 11794 " return 42;\n" 11795 "}", 11796 DoNotMerge); 11797 verifyFormat("void f() {\n" 11798 " // Comment\n" 11799 "}"); 11800 verifyFormat("{\n" 11801 "#error {\n" 11802 " int a;\n" 11803 "}"); 11804 verifyFormat("{\n" 11805 " int a;\n" 11806 "#error {\n" 11807 "}"); 11808 verifyFormat("void f() {} // comment"); 11809 verifyFormat("void f() { int a; } // comment"); 11810 verifyFormat("void f() {\n" 11811 "} // comment", 11812 DoNotMerge); 11813 verifyFormat("void f() {\n" 11814 " int a;\n" 11815 "} // comment", 11816 DoNotMerge); 11817 verifyFormat("void f() {\n" 11818 "} // comment", 11819 getLLVMStyleWithColumns(15)); 11820 11821 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11822 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11823 11824 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11825 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11826 verifyFormat("class C {\n" 11827 " C()\n" 11828 " : iiiiiiii(nullptr),\n" 11829 " kkkkkkk(nullptr),\n" 11830 " mmmmmmm(nullptr),\n" 11831 " nnnnnnn(nullptr) {}\n" 11832 "};", 11833 getGoogleStyle()); 11834 11835 FormatStyle NoColumnLimit = getLLVMStyle(); 11836 NoColumnLimit.ColumnLimit = 0; 11837 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 11838 EXPECT_EQ("class C {\n" 11839 " A() : b(0) {}\n" 11840 "};", 11841 format("class C{A():b(0){}};", NoColumnLimit)); 11842 EXPECT_EQ("A()\n" 11843 " : b(0) {\n" 11844 "}", 11845 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 11846 11847 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 11848 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 11849 FormatStyle::SFS_None; 11850 EXPECT_EQ("A()\n" 11851 " : b(0) {\n" 11852 "}", 11853 format("A():b(0){}", DoNotMergeNoColumnLimit)); 11854 EXPECT_EQ("A()\n" 11855 " : b(0) {\n" 11856 "}", 11857 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 11858 11859 verifyFormat("#define A \\\n" 11860 " void f() { \\\n" 11861 " int i; \\\n" 11862 " }", 11863 getLLVMStyleWithColumns(20)); 11864 verifyFormat("#define A \\\n" 11865 " void f() { int i; }", 11866 getLLVMStyleWithColumns(21)); 11867 verifyFormat("#define A \\\n" 11868 " void f() { \\\n" 11869 " int i; \\\n" 11870 " } \\\n" 11871 " int j;", 11872 getLLVMStyleWithColumns(22)); 11873 verifyFormat("#define A \\\n" 11874 " void f() { int i; } \\\n" 11875 " int j;", 11876 getLLVMStyleWithColumns(23)); 11877 } 11878 11879 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 11880 FormatStyle MergeEmptyOnly = getLLVMStyle(); 11881 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11882 verifyFormat("class C {\n" 11883 " int f() {}\n" 11884 "};", 11885 MergeEmptyOnly); 11886 verifyFormat("class C {\n" 11887 " int f() {\n" 11888 " return 42;\n" 11889 " }\n" 11890 "};", 11891 MergeEmptyOnly); 11892 verifyFormat("int f() {}", MergeEmptyOnly); 11893 verifyFormat("int f() {\n" 11894 " return 42;\n" 11895 "}", 11896 MergeEmptyOnly); 11897 11898 // Also verify behavior when BraceWrapping.AfterFunction = true 11899 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11900 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11901 verifyFormat("int f() {}", MergeEmptyOnly); 11902 verifyFormat("class C {\n" 11903 " int f() {}\n" 11904 "};", 11905 MergeEmptyOnly); 11906 } 11907 11908 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11909 FormatStyle MergeInlineOnly = getLLVMStyle(); 11910 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11911 verifyFormat("class C {\n" 11912 " int f() { return 42; }\n" 11913 "};", 11914 MergeInlineOnly); 11915 verifyFormat("int f() {\n" 11916 " return 42;\n" 11917 "}", 11918 MergeInlineOnly); 11919 11920 // SFS_Inline implies SFS_Empty 11921 verifyFormat("class C {\n" 11922 " int f() {}\n" 11923 "};", 11924 MergeInlineOnly); 11925 verifyFormat("int f() {}", MergeInlineOnly); 11926 11927 // Also verify behavior when BraceWrapping.AfterFunction = true 11928 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11929 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11930 verifyFormat("class C {\n" 11931 " int f() { return 42; }\n" 11932 "};", 11933 MergeInlineOnly); 11934 verifyFormat("int f()\n" 11935 "{\n" 11936 " return 42;\n" 11937 "}", 11938 MergeInlineOnly); 11939 11940 // SFS_Inline implies SFS_Empty 11941 verifyFormat("int f() {}", MergeInlineOnly); 11942 verifyFormat("class C {\n" 11943 " int f() {}\n" 11944 "};", 11945 MergeInlineOnly); 11946 } 11947 11948 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11949 FormatStyle MergeInlineOnly = getLLVMStyle(); 11950 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11951 FormatStyle::SFS_InlineOnly; 11952 verifyFormat("class C {\n" 11953 " int f() { return 42; }\n" 11954 "};", 11955 MergeInlineOnly); 11956 verifyFormat("int f() {\n" 11957 " return 42;\n" 11958 "}", 11959 MergeInlineOnly); 11960 11961 // SFS_InlineOnly does not imply SFS_Empty 11962 verifyFormat("class C {\n" 11963 " int f() {}\n" 11964 "};", 11965 MergeInlineOnly); 11966 verifyFormat("int f() {\n" 11967 "}", 11968 MergeInlineOnly); 11969 11970 // Also verify behavior when BraceWrapping.AfterFunction = true 11971 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11972 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11973 verifyFormat("class C {\n" 11974 " int f() { return 42; }\n" 11975 "};", 11976 MergeInlineOnly); 11977 verifyFormat("int f()\n" 11978 "{\n" 11979 " return 42;\n" 11980 "}", 11981 MergeInlineOnly); 11982 11983 // SFS_InlineOnly does not imply SFS_Empty 11984 verifyFormat("int f()\n" 11985 "{\n" 11986 "}", 11987 MergeInlineOnly); 11988 verifyFormat("class C {\n" 11989 " int f() {}\n" 11990 "};", 11991 MergeInlineOnly); 11992 } 11993 11994 TEST_F(FormatTest, SplitEmptyFunction) { 11995 FormatStyle Style = getLLVMStyle(); 11996 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11997 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11998 Style.BraceWrapping.AfterFunction = true; 11999 Style.BraceWrapping.SplitEmptyFunction = false; 12000 Style.ColumnLimit = 40; 12001 12002 verifyFormat("int f()\n" 12003 "{}", 12004 Style); 12005 verifyFormat("int f()\n" 12006 "{\n" 12007 " return 42;\n" 12008 "}", 12009 Style); 12010 verifyFormat("int f()\n" 12011 "{\n" 12012 " // some comment\n" 12013 "}", 12014 Style); 12015 12016 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12017 verifyFormat("int f() {}", Style); 12018 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12019 "{}", 12020 Style); 12021 verifyFormat("int f()\n" 12022 "{\n" 12023 " return 0;\n" 12024 "}", 12025 Style); 12026 12027 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12028 verifyFormat("class Foo {\n" 12029 " int f() {}\n" 12030 "};\n", 12031 Style); 12032 verifyFormat("class Foo {\n" 12033 " int f() { return 0; }\n" 12034 "};\n", 12035 Style); 12036 verifyFormat("class Foo {\n" 12037 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12038 " {}\n" 12039 "};\n", 12040 Style); 12041 verifyFormat("class Foo {\n" 12042 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12043 " {\n" 12044 " return 0;\n" 12045 " }\n" 12046 "};\n", 12047 Style); 12048 12049 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12050 verifyFormat("int f() {}", Style); 12051 verifyFormat("int f() { return 0; }", Style); 12052 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12053 "{}", 12054 Style); 12055 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12056 "{\n" 12057 " return 0;\n" 12058 "}", 12059 Style); 12060 } 12061 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12062 FormatStyle Style = getLLVMStyle(); 12063 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12064 verifyFormat("#ifdef A\n" 12065 "int f() {}\n" 12066 "#else\n" 12067 "int g() {}\n" 12068 "#endif", 12069 Style); 12070 } 12071 12072 TEST_F(FormatTest, SplitEmptyClass) { 12073 FormatStyle Style = getLLVMStyle(); 12074 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12075 Style.BraceWrapping.AfterClass = true; 12076 Style.BraceWrapping.SplitEmptyRecord = false; 12077 12078 verifyFormat("class Foo\n" 12079 "{};", 12080 Style); 12081 verifyFormat("/* something */ class Foo\n" 12082 "{};", 12083 Style); 12084 verifyFormat("template <typename X> class Foo\n" 12085 "{};", 12086 Style); 12087 verifyFormat("class Foo\n" 12088 "{\n" 12089 " Foo();\n" 12090 "};", 12091 Style); 12092 verifyFormat("typedef class Foo\n" 12093 "{\n" 12094 "} Foo_t;", 12095 Style); 12096 12097 Style.BraceWrapping.SplitEmptyRecord = true; 12098 Style.BraceWrapping.AfterStruct = true; 12099 verifyFormat("class rep\n" 12100 "{\n" 12101 "};", 12102 Style); 12103 verifyFormat("struct rep\n" 12104 "{\n" 12105 "};", 12106 Style); 12107 verifyFormat("template <typename T> class rep\n" 12108 "{\n" 12109 "};", 12110 Style); 12111 verifyFormat("template <typename T> struct rep\n" 12112 "{\n" 12113 "};", 12114 Style); 12115 verifyFormat("class rep\n" 12116 "{\n" 12117 " int x;\n" 12118 "};", 12119 Style); 12120 verifyFormat("struct rep\n" 12121 "{\n" 12122 " int x;\n" 12123 "};", 12124 Style); 12125 verifyFormat("template <typename T> class rep\n" 12126 "{\n" 12127 " int x;\n" 12128 "};", 12129 Style); 12130 verifyFormat("template <typename T> struct rep\n" 12131 "{\n" 12132 " int x;\n" 12133 "};", 12134 Style); 12135 verifyFormat("template <typename T> class rep // Foo\n" 12136 "{\n" 12137 " int x;\n" 12138 "};", 12139 Style); 12140 verifyFormat("template <typename T> struct rep // Bar\n" 12141 "{\n" 12142 " int x;\n" 12143 "};", 12144 Style); 12145 12146 verifyFormat("template <typename T> class rep<T>\n" 12147 "{\n" 12148 " int x;\n" 12149 "};", 12150 Style); 12151 12152 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12153 "{\n" 12154 " int x;\n" 12155 "};", 12156 Style); 12157 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12158 "{\n" 12159 "};", 12160 Style); 12161 12162 verifyFormat("#include \"stdint.h\"\n" 12163 "namespace rep {}", 12164 Style); 12165 verifyFormat("#include <stdint.h>\n" 12166 "namespace rep {}", 12167 Style); 12168 verifyFormat("#include <stdint.h>\n" 12169 "namespace rep {}", 12170 "#include <stdint.h>\n" 12171 "namespace rep {\n" 12172 "\n" 12173 "\n" 12174 "}", 12175 Style); 12176 } 12177 12178 TEST_F(FormatTest, SplitEmptyStruct) { 12179 FormatStyle Style = getLLVMStyle(); 12180 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12181 Style.BraceWrapping.AfterStruct = true; 12182 Style.BraceWrapping.SplitEmptyRecord = false; 12183 12184 verifyFormat("struct Foo\n" 12185 "{};", 12186 Style); 12187 verifyFormat("/* something */ struct Foo\n" 12188 "{};", 12189 Style); 12190 verifyFormat("template <typename X> struct Foo\n" 12191 "{};", 12192 Style); 12193 verifyFormat("struct Foo\n" 12194 "{\n" 12195 " Foo();\n" 12196 "};", 12197 Style); 12198 verifyFormat("typedef struct Foo\n" 12199 "{\n" 12200 "} Foo_t;", 12201 Style); 12202 // typedef struct Bar {} Bar_t; 12203 } 12204 12205 TEST_F(FormatTest, SplitEmptyUnion) { 12206 FormatStyle Style = getLLVMStyle(); 12207 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12208 Style.BraceWrapping.AfterUnion = true; 12209 Style.BraceWrapping.SplitEmptyRecord = false; 12210 12211 verifyFormat("union Foo\n" 12212 "{};", 12213 Style); 12214 verifyFormat("/* something */ union Foo\n" 12215 "{};", 12216 Style); 12217 verifyFormat("union Foo\n" 12218 "{\n" 12219 " A,\n" 12220 "};", 12221 Style); 12222 verifyFormat("typedef union Foo\n" 12223 "{\n" 12224 "} Foo_t;", 12225 Style); 12226 } 12227 12228 TEST_F(FormatTest, SplitEmptyNamespace) { 12229 FormatStyle Style = getLLVMStyle(); 12230 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12231 Style.BraceWrapping.AfterNamespace = true; 12232 Style.BraceWrapping.SplitEmptyNamespace = false; 12233 12234 verifyFormat("namespace Foo\n" 12235 "{};", 12236 Style); 12237 verifyFormat("/* something */ namespace Foo\n" 12238 "{};", 12239 Style); 12240 verifyFormat("inline namespace Foo\n" 12241 "{};", 12242 Style); 12243 verifyFormat("/* something */ inline namespace Foo\n" 12244 "{};", 12245 Style); 12246 verifyFormat("export namespace Foo\n" 12247 "{};", 12248 Style); 12249 verifyFormat("namespace Foo\n" 12250 "{\n" 12251 "void Bar();\n" 12252 "};", 12253 Style); 12254 } 12255 12256 TEST_F(FormatTest, NeverMergeShortRecords) { 12257 FormatStyle Style = getLLVMStyle(); 12258 12259 verifyFormat("class Foo {\n" 12260 " Foo();\n" 12261 "};", 12262 Style); 12263 verifyFormat("typedef class Foo {\n" 12264 " Foo();\n" 12265 "} Foo_t;", 12266 Style); 12267 verifyFormat("struct Foo {\n" 12268 " Foo();\n" 12269 "};", 12270 Style); 12271 verifyFormat("typedef struct Foo {\n" 12272 " Foo();\n" 12273 "} Foo_t;", 12274 Style); 12275 verifyFormat("union Foo {\n" 12276 " A,\n" 12277 "};", 12278 Style); 12279 verifyFormat("typedef union Foo {\n" 12280 " A,\n" 12281 "} Foo_t;", 12282 Style); 12283 verifyFormat("namespace Foo {\n" 12284 "void Bar();\n" 12285 "};", 12286 Style); 12287 12288 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12289 Style.BraceWrapping.AfterClass = true; 12290 Style.BraceWrapping.AfterStruct = true; 12291 Style.BraceWrapping.AfterUnion = true; 12292 Style.BraceWrapping.AfterNamespace = true; 12293 verifyFormat("class Foo\n" 12294 "{\n" 12295 " Foo();\n" 12296 "};", 12297 Style); 12298 verifyFormat("typedef class Foo\n" 12299 "{\n" 12300 " Foo();\n" 12301 "} Foo_t;", 12302 Style); 12303 verifyFormat("struct Foo\n" 12304 "{\n" 12305 " Foo();\n" 12306 "};", 12307 Style); 12308 verifyFormat("typedef struct Foo\n" 12309 "{\n" 12310 " Foo();\n" 12311 "} Foo_t;", 12312 Style); 12313 verifyFormat("union Foo\n" 12314 "{\n" 12315 " A,\n" 12316 "};", 12317 Style); 12318 verifyFormat("typedef union Foo\n" 12319 "{\n" 12320 " A,\n" 12321 "} Foo_t;", 12322 Style); 12323 verifyFormat("namespace Foo\n" 12324 "{\n" 12325 "void Bar();\n" 12326 "};", 12327 Style); 12328 } 12329 12330 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12331 // Elaborate type variable declarations. 12332 verifyFormat("struct foo a = {bar};\nint n;"); 12333 verifyFormat("class foo a = {bar};\nint n;"); 12334 verifyFormat("union foo a = {bar};\nint n;"); 12335 12336 // Elaborate types inside function definitions. 12337 verifyFormat("struct foo f() {}\nint n;"); 12338 verifyFormat("class foo f() {}\nint n;"); 12339 verifyFormat("union foo f() {}\nint n;"); 12340 12341 // Templates. 12342 verifyFormat("template <class X> void f() {}\nint n;"); 12343 verifyFormat("template <struct X> void f() {}\nint n;"); 12344 verifyFormat("template <union X> void f() {}\nint n;"); 12345 12346 // Actual definitions... 12347 verifyFormat("struct {\n} n;"); 12348 verifyFormat( 12349 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12350 verifyFormat("union Z {\n int n;\n} x;"); 12351 verifyFormat("class MACRO Z {\n} n;"); 12352 verifyFormat("class MACRO(X) Z {\n} n;"); 12353 verifyFormat("class __attribute__(X) Z {\n} n;"); 12354 verifyFormat("class __declspec(X) Z {\n} n;"); 12355 verifyFormat("class A##B##C {\n} n;"); 12356 verifyFormat("class alignas(16) Z {\n} n;"); 12357 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12358 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12359 12360 // Redefinition from nested context: 12361 verifyFormat("class A::B::C {\n} n;"); 12362 12363 // Template definitions. 12364 verifyFormat( 12365 "template <typename F>\n" 12366 "Matcher(const Matcher<F> &Other,\n" 12367 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12368 " !is_same<F, T>::value>::type * = 0)\n" 12369 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12370 12371 // FIXME: This is still incorrectly handled at the formatter side. 12372 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12373 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12374 12375 // FIXME: 12376 // This now gets parsed incorrectly as class definition. 12377 // verifyFormat("class A<int> f() {\n}\nint n;"); 12378 12379 // Elaborate types where incorrectly parsing the structural element would 12380 // break the indent. 12381 verifyFormat("if (true)\n" 12382 " class X x;\n" 12383 "else\n" 12384 " f();\n"); 12385 12386 // This is simply incomplete. Formatting is not important, but must not crash. 12387 verifyFormat("class A:"); 12388 } 12389 12390 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12391 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12392 format("#error Leave all white!!!!! space* alone!\n")); 12393 EXPECT_EQ( 12394 "#warning Leave all white!!!!! space* alone!\n", 12395 format("#warning Leave all white!!!!! space* alone!\n")); 12396 EXPECT_EQ("#error 1", format(" # error 1")); 12397 EXPECT_EQ("#warning 1", format(" # warning 1")); 12398 } 12399 12400 TEST_F(FormatTest, FormatHashIfExpressions) { 12401 verifyFormat("#if AAAA && BBBB"); 12402 verifyFormat("#if (AAAA && BBBB)"); 12403 verifyFormat("#elif (AAAA && BBBB)"); 12404 // FIXME: Come up with a better indentation for #elif. 12405 verifyFormat( 12406 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12407 " defined(BBBBBBBB)\n" 12408 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12409 " defined(BBBBBBBB)\n" 12410 "#endif", 12411 getLLVMStyleWithColumns(65)); 12412 } 12413 12414 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12415 FormatStyle AllowsMergedIf = getGoogleStyle(); 12416 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12417 FormatStyle::SIS_WithoutElse; 12418 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12419 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12420 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12421 EXPECT_EQ("if (true) return 42;", 12422 format("if (true)\nreturn 42;", AllowsMergedIf)); 12423 FormatStyle ShortMergedIf = AllowsMergedIf; 12424 ShortMergedIf.ColumnLimit = 25; 12425 verifyFormat("#define A \\\n" 12426 " if (true) return 42;", 12427 ShortMergedIf); 12428 verifyFormat("#define A \\\n" 12429 " f(); \\\n" 12430 " if (true)\n" 12431 "#define B", 12432 ShortMergedIf); 12433 verifyFormat("#define A \\\n" 12434 " f(); \\\n" 12435 " if (true)\n" 12436 "g();", 12437 ShortMergedIf); 12438 verifyFormat("{\n" 12439 "#ifdef A\n" 12440 " // Comment\n" 12441 " if (true) continue;\n" 12442 "#endif\n" 12443 " // Comment\n" 12444 " if (true) continue;\n" 12445 "}", 12446 ShortMergedIf); 12447 ShortMergedIf.ColumnLimit = 33; 12448 verifyFormat("#define A \\\n" 12449 " if constexpr (true) return 42;", 12450 ShortMergedIf); 12451 verifyFormat("#define A \\\n" 12452 " if CONSTEXPR (true) return 42;", 12453 ShortMergedIf); 12454 ShortMergedIf.ColumnLimit = 29; 12455 verifyFormat("#define A \\\n" 12456 " if (aaaaaaaaaa) return 1; \\\n" 12457 " return 2;", 12458 ShortMergedIf); 12459 ShortMergedIf.ColumnLimit = 28; 12460 verifyFormat("#define A \\\n" 12461 " if (aaaaaaaaaa) \\\n" 12462 " return 1; \\\n" 12463 " return 2;", 12464 ShortMergedIf); 12465 verifyFormat("#define A \\\n" 12466 " if constexpr (aaaaaaa) \\\n" 12467 " return 1; \\\n" 12468 " return 2;", 12469 ShortMergedIf); 12470 verifyFormat("#define A \\\n" 12471 " if CONSTEXPR (aaaaaaa) \\\n" 12472 " return 1; \\\n" 12473 " return 2;", 12474 ShortMergedIf); 12475 } 12476 12477 TEST_F(FormatTest, FormatStarDependingOnContext) { 12478 verifyFormat("void f(int *a);"); 12479 verifyFormat("void f() { f(fint * b); }"); 12480 verifyFormat("class A {\n void f(int *a);\n};"); 12481 verifyFormat("class A {\n int *a;\n};"); 12482 verifyFormat("namespace a {\n" 12483 "namespace b {\n" 12484 "class A {\n" 12485 " void f() {}\n" 12486 " int *a;\n" 12487 "};\n" 12488 "} // namespace b\n" 12489 "} // namespace a"); 12490 } 12491 12492 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12493 verifyFormat("while"); 12494 verifyFormat("operator"); 12495 } 12496 12497 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12498 // This code would be painfully slow to format if we didn't skip it. 12499 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 12500 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12501 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12502 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12503 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12504 "A(1, 1)\n" 12505 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12506 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12507 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12508 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12509 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12510 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12511 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12512 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12513 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12514 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12515 // Deeply nested part is untouched, rest is formatted. 12516 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12517 format(std::string("int i;\n") + Code + "int j;\n", 12518 getLLVMStyle(), SC_ExpectIncomplete)); 12519 } 12520 12521 //===----------------------------------------------------------------------===// 12522 // Objective-C tests. 12523 //===----------------------------------------------------------------------===// 12524 12525 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12526 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12527 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12528 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12529 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12530 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12531 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12532 format("-(NSInteger)Method3:(id)anObject;")); 12533 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12534 format("-(NSInteger)Method4:(id)anObject;")); 12535 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12536 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12537 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12538 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12539 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12540 "forAllCells:(BOOL)flag;", 12541 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12542 "forAllCells:(BOOL)flag;")); 12543 12544 // Very long objectiveC method declaration. 12545 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12546 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12547 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12548 " inRange:(NSRange)range\n" 12549 " outRange:(NSRange)out_range\n" 12550 " outRange1:(NSRange)out_range1\n" 12551 " outRange2:(NSRange)out_range2\n" 12552 " outRange3:(NSRange)out_range3\n" 12553 " outRange4:(NSRange)out_range4\n" 12554 " outRange5:(NSRange)out_range5\n" 12555 " outRange6:(NSRange)out_range6\n" 12556 " outRange7:(NSRange)out_range7\n" 12557 " outRange8:(NSRange)out_range8\n" 12558 " outRange9:(NSRange)out_range9;"); 12559 12560 // When the function name has to be wrapped. 12561 FormatStyle Style = getLLVMStyle(); 12562 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12563 // and always indents instead. 12564 Style.IndentWrappedFunctionNames = false; 12565 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12566 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12567 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12568 "}", 12569 Style); 12570 Style.IndentWrappedFunctionNames = true; 12571 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12572 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12573 " anotherName:(NSString)dddddddddddddd {\n" 12574 "}", 12575 Style); 12576 12577 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12578 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12579 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12580 // protocol lists (but not for template classes): 12581 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12582 12583 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12584 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12585 12586 // If there's no return type (very rare in practice!), LLVM and Google style 12587 // agree. 12588 verifyFormat("- foo;"); 12589 verifyFormat("- foo:(int)f;"); 12590 verifyGoogleFormat("- foo:(int)foo;"); 12591 } 12592 12593 TEST_F(FormatTest, BreaksStringLiterals) { 12594 EXPECT_EQ("\"some text \"\n" 12595 "\"other\";", 12596 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12597 EXPECT_EQ("\"some text \"\n" 12598 "\"other\";", 12599 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12600 EXPECT_EQ( 12601 "#define A \\\n" 12602 " \"some \" \\\n" 12603 " \"text \" \\\n" 12604 " \"other\";", 12605 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12606 EXPECT_EQ( 12607 "#define A \\\n" 12608 " \"so \" \\\n" 12609 " \"text \" \\\n" 12610 " \"other\";", 12611 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12612 12613 EXPECT_EQ("\"some text\"", 12614 format("\"some text\"", getLLVMStyleWithColumns(1))); 12615 EXPECT_EQ("\"some text\"", 12616 format("\"some text\"", getLLVMStyleWithColumns(11))); 12617 EXPECT_EQ("\"some \"\n" 12618 "\"text\"", 12619 format("\"some text\"", getLLVMStyleWithColumns(10))); 12620 EXPECT_EQ("\"some \"\n" 12621 "\"text\"", 12622 format("\"some text\"", getLLVMStyleWithColumns(7))); 12623 EXPECT_EQ("\"some\"\n" 12624 "\" tex\"\n" 12625 "\"t\"", 12626 format("\"some text\"", getLLVMStyleWithColumns(6))); 12627 EXPECT_EQ("\"some\"\n" 12628 "\" tex\"\n" 12629 "\" and\"", 12630 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12631 EXPECT_EQ("\"some\"\n" 12632 "\"/tex\"\n" 12633 "\"/and\"", 12634 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12635 12636 EXPECT_EQ("variable =\n" 12637 " \"long string \"\n" 12638 " \"literal\";", 12639 format("variable = \"long string literal\";", 12640 getLLVMStyleWithColumns(20))); 12641 12642 EXPECT_EQ("variable = f(\n" 12643 " \"long string \"\n" 12644 " \"literal\",\n" 12645 " short,\n" 12646 " loooooooooooooooooooong);", 12647 format("variable = f(\"long string literal\", short, " 12648 "loooooooooooooooooooong);", 12649 getLLVMStyleWithColumns(20))); 12650 12651 EXPECT_EQ( 12652 "f(g(\"long string \"\n" 12653 " \"literal\"),\n" 12654 " b);", 12655 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12656 EXPECT_EQ("f(g(\"long string \"\n" 12657 " \"literal\",\n" 12658 " a),\n" 12659 " b);", 12660 format("f(g(\"long string literal\", a), b);", 12661 getLLVMStyleWithColumns(20))); 12662 EXPECT_EQ( 12663 "f(\"one two\".split(\n" 12664 " variable));", 12665 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12666 EXPECT_EQ("f(\"one two three four five six \"\n" 12667 " \"seven\".split(\n" 12668 " really_looooong_variable));", 12669 format("f(\"one two three four five six seven\"." 12670 "split(really_looooong_variable));", 12671 getLLVMStyleWithColumns(33))); 12672 12673 EXPECT_EQ("f(\"some \"\n" 12674 " \"text\",\n" 12675 " other);", 12676 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12677 12678 // Only break as a last resort. 12679 verifyFormat( 12680 "aaaaaaaaaaaaaaaaaaaa(\n" 12681 " aaaaaaaaaaaaaaaaaaaa,\n" 12682 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12683 12684 EXPECT_EQ("\"splitmea\"\n" 12685 "\"trandomp\"\n" 12686 "\"oint\"", 12687 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12688 12689 EXPECT_EQ("\"split/\"\n" 12690 "\"pathat/\"\n" 12691 "\"slashes\"", 12692 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12693 12694 EXPECT_EQ("\"split/\"\n" 12695 "\"pathat/\"\n" 12696 "\"slashes\"", 12697 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12698 EXPECT_EQ("\"split at \"\n" 12699 "\"spaces/at/\"\n" 12700 "\"slashes.at.any$\"\n" 12701 "\"non-alphanumeric%\"\n" 12702 "\"1111111111characte\"\n" 12703 "\"rs\"", 12704 format("\"split at " 12705 "spaces/at/" 12706 "slashes.at." 12707 "any$non-" 12708 "alphanumeric%" 12709 "1111111111characte" 12710 "rs\"", 12711 getLLVMStyleWithColumns(20))); 12712 12713 // Verify that splitting the strings understands 12714 // Style::AlwaysBreakBeforeMultilineStrings. 12715 EXPECT_EQ("aaaaaaaaaaaa(\n" 12716 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12717 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12718 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12719 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12720 "aaaaaaaaaaaaaaaaaaaaaa\");", 12721 getGoogleStyle())); 12722 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12723 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12724 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12725 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12726 "aaaaaaaaaaaaaaaaaaaaaa\";", 12727 getGoogleStyle())); 12728 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12729 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12730 format("llvm::outs() << " 12731 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12732 "aaaaaaaaaaaaaaaaaaa\";")); 12733 EXPECT_EQ("ffff(\n" 12734 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12735 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12736 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12737 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12738 getGoogleStyle())); 12739 12740 FormatStyle Style = getLLVMStyleWithColumns(12); 12741 Style.BreakStringLiterals = false; 12742 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12743 12744 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12745 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12746 EXPECT_EQ("#define A \\\n" 12747 " \"some \" \\\n" 12748 " \"text \" \\\n" 12749 " \"other\";", 12750 format("#define A \"some text other\";", AlignLeft)); 12751 } 12752 12753 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12754 EXPECT_EQ("C a = \"some more \"\n" 12755 " \"text\";", 12756 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12757 } 12758 12759 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12760 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12761 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12762 EXPECT_EQ("int i = a(b());", 12763 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12764 } 12765 12766 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12767 EXPECT_EQ( 12768 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12769 "(\n" 12770 " \"x\t\");", 12771 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12772 "aaaaaaa(" 12773 "\"x\t\");")); 12774 } 12775 12776 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12777 EXPECT_EQ( 12778 "u8\"utf8 string \"\n" 12779 "u8\"literal\";", 12780 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12781 EXPECT_EQ( 12782 "u\"utf16 string \"\n" 12783 "u\"literal\";", 12784 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12785 EXPECT_EQ( 12786 "U\"utf32 string \"\n" 12787 "U\"literal\";", 12788 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12789 EXPECT_EQ("L\"wide string \"\n" 12790 "L\"literal\";", 12791 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12792 EXPECT_EQ("@\"NSString \"\n" 12793 "@\"literal\";", 12794 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12795 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12796 12797 // This input makes clang-format try to split the incomplete unicode escape 12798 // sequence, which used to lead to a crasher. 12799 verifyNoCrash( 12800 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12801 getLLVMStyleWithColumns(60)); 12802 } 12803 12804 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12805 FormatStyle Style = getGoogleStyleWithColumns(15); 12806 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12807 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12808 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12809 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12810 EXPECT_EQ("u8R\"x(raw literal)x\";", 12811 format("u8R\"x(raw literal)x\";", Style)); 12812 } 12813 12814 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12815 FormatStyle Style = getLLVMStyleWithColumns(20); 12816 EXPECT_EQ( 12817 "_T(\"aaaaaaaaaaaaaa\")\n" 12818 "_T(\"aaaaaaaaaaaaaa\")\n" 12819 "_T(\"aaaaaaaaaaaa\")", 12820 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12821 EXPECT_EQ("f(x,\n" 12822 " _T(\"aaaaaaaaaaaa\")\n" 12823 " _T(\"aaa\"),\n" 12824 " z);", 12825 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 12826 12827 // FIXME: Handle embedded spaces in one iteration. 12828 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 12829 // "_T(\"aaaaaaaaaaaaa\")\n" 12830 // "_T(\"aaaaaaaaaaaaa\")\n" 12831 // "_T(\"a\")", 12832 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12833 // getLLVMStyleWithColumns(20))); 12834 EXPECT_EQ( 12835 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12836 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 12837 EXPECT_EQ("f(\n" 12838 "#if !TEST\n" 12839 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12840 "#endif\n" 12841 ");", 12842 format("f(\n" 12843 "#if !TEST\n" 12844 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12845 "#endif\n" 12846 ");")); 12847 EXPECT_EQ("f(\n" 12848 "\n" 12849 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 12850 format("f(\n" 12851 "\n" 12852 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 12853 } 12854 12855 TEST_F(FormatTest, BreaksStringLiteralOperands) { 12856 // In a function call with two operands, the second can be broken with no line 12857 // break before it. 12858 EXPECT_EQ( 12859 "func(a, \"long long \"\n" 12860 " \"long long\");", 12861 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 12862 // In a function call with three operands, the second must be broken with a 12863 // line break before it. 12864 EXPECT_EQ("func(a,\n" 12865 " \"long long long \"\n" 12866 " \"long\",\n" 12867 " c);", 12868 format("func(a, \"long long long long\", c);", 12869 getLLVMStyleWithColumns(24))); 12870 // In a function call with three operands, the third must be broken with a 12871 // line break before it. 12872 EXPECT_EQ("func(a, b,\n" 12873 " \"long long long \"\n" 12874 " \"long\");", 12875 format("func(a, b, \"long long long long\");", 12876 getLLVMStyleWithColumns(24))); 12877 // In a function call with three operands, both the second and the third must 12878 // be broken with a line break before them. 12879 EXPECT_EQ("func(a,\n" 12880 " \"long long long \"\n" 12881 " \"long\",\n" 12882 " \"long long long \"\n" 12883 " \"long\");", 12884 format("func(a, \"long long long long\", \"long long long long\");", 12885 getLLVMStyleWithColumns(24))); 12886 // In a chain of << with two operands, the second can be broken with no line 12887 // break before it. 12888 EXPECT_EQ("a << \"line line \"\n" 12889 " \"line\";", 12890 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12891 // In a chain of << with three operands, the second can be broken with no line 12892 // break before it. 12893 EXPECT_EQ( 12894 "abcde << \"line \"\n" 12895 " \"line line\"\n" 12896 " << c;", 12897 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12898 // In a chain of << with three operands, the third must be broken with a line 12899 // break before it. 12900 EXPECT_EQ( 12901 "a << b\n" 12902 " << \"line line \"\n" 12903 " \"line\";", 12904 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 12905 // In a chain of << with three operands, the second can be broken with no line 12906 // break before it and the third must be broken with a line break before it. 12907 EXPECT_EQ("abcd << \"line line \"\n" 12908 " \"line\"\n" 12909 " << \"line line \"\n" 12910 " \"line\";", 12911 format("abcd << \"line line line\" << \"line line line\";", 12912 getLLVMStyleWithColumns(20))); 12913 // In a chain of binary operators with two operands, the second can be broken 12914 // with no line break before it. 12915 EXPECT_EQ( 12916 "abcd + \"line line \"\n" 12917 " \"line line\";", 12918 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12919 // In a chain of binary operators with three operands, the second must be 12920 // broken with a line break before it. 12921 EXPECT_EQ("abcd +\n" 12922 " \"line line \"\n" 12923 " \"line line\" +\n" 12924 " e;", 12925 format("abcd + \"line line line line\" + e;", 12926 getLLVMStyleWithColumns(20))); 12927 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12928 // the first must be broken with a line break before it. 12929 FormatStyle Style = getLLVMStyleWithColumns(25); 12930 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12931 EXPECT_EQ("someFunction(\n" 12932 " \"long long long \"\n" 12933 " \"long\",\n" 12934 " a);", 12935 format("someFunction(\"long long long long\", a);", Style)); 12936 } 12937 12938 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12939 EXPECT_EQ( 12940 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12943 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12946 } 12947 12948 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12949 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12950 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12951 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12952 "multiline raw string literal xxxxxxxxxxxxxx\n" 12953 ")x\",\n" 12954 " a),\n" 12955 " b);", 12956 format("fffffffffff(g(R\"x(\n" 12957 "multiline raw string literal xxxxxxxxxxxxxx\n" 12958 ")x\", a), b);", 12959 getGoogleStyleWithColumns(20))); 12960 EXPECT_EQ("fffffffffff(\n" 12961 " g(R\"x(qqq\n" 12962 "multiline raw string literal xxxxxxxxxxxxxx\n" 12963 ")x\",\n" 12964 " a),\n" 12965 " b);", 12966 format("fffffffffff(g(R\"x(qqq\n" 12967 "multiline raw string literal xxxxxxxxxxxxxx\n" 12968 ")x\", a), b);", 12969 getGoogleStyleWithColumns(20))); 12970 12971 EXPECT_EQ("fffffffffff(R\"x(\n" 12972 "multiline raw string literal xxxxxxxxxxxxxx\n" 12973 ")x\");", 12974 format("fffffffffff(R\"x(\n" 12975 "multiline raw string literal xxxxxxxxxxxxxx\n" 12976 ")x\");", 12977 getGoogleStyleWithColumns(20))); 12978 EXPECT_EQ("fffffffffff(R\"x(\n" 12979 "multiline raw string literal xxxxxxxxxxxxxx\n" 12980 ")x\" + bbbbbb);", 12981 format("fffffffffff(R\"x(\n" 12982 "multiline raw string literal xxxxxxxxxxxxxx\n" 12983 ")x\" + bbbbbb);", 12984 getGoogleStyleWithColumns(20))); 12985 EXPECT_EQ("fffffffffff(\n" 12986 " R\"x(\n" 12987 "multiline raw string literal xxxxxxxxxxxxxx\n" 12988 ")x\" +\n" 12989 " bbbbbb);", 12990 format("fffffffffff(\n" 12991 " R\"x(\n" 12992 "multiline raw string literal xxxxxxxxxxxxxx\n" 12993 ")x\" + bbbbbb);", 12994 getGoogleStyleWithColumns(20))); 12995 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 12996 format("fffffffffff(\n" 12997 " R\"(single line raw string)\" + bbbbbb);")); 12998 } 12999 13000 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13001 verifyFormat("string a = \"unterminated;"); 13002 EXPECT_EQ("function(\"unterminated,\n" 13003 " OtherParameter);", 13004 format("function( \"unterminated,\n" 13005 " OtherParameter);")); 13006 } 13007 13008 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13009 FormatStyle Style = getLLVMStyle(); 13010 Style.Standard = FormatStyle::LS_Cpp03; 13011 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13012 format("#define x(_a) printf(\"foo\"_a);", Style)); 13013 } 13014 13015 TEST_F(FormatTest, CppLexVersion) { 13016 FormatStyle Style = getLLVMStyle(); 13017 // Formatting of x * y differs if x is a type. 13018 verifyFormat("void foo() { MACRO(a * b); }", Style); 13019 verifyFormat("void foo() { MACRO(int *b); }", Style); 13020 13021 // LLVM style uses latest lexer. 13022 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13023 Style.Standard = FormatStyle::LS_Cpp17; 13024 // But in c++17, char8_t isn't a keyword. 13025 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13026 } 13027 13028 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13029 13030 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13031 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13032 " \"ddeeefff\");", 13033 format("someFunction(\"aaabbbcccdddeeefff\");", 13034 getLLVMStyleWithColumns(25))); 13035 EXPECT_EQ("someFunction1234567890(\n" 13036 " \"aaabbbcccdddeeefff\");", 13037 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13038 getLLVMStyleWithColumns(26))); 13039 EXPECT_EQ("someFunction1234567890(\n" 13040 " \"aaabbbcccdddeeeff\"\n" 13041 " \"f\");", 13042 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13043 getLLVMStyleWithColumns(25))); 13044 EXPECT_EQ("someFunction1234567890(\n" 13045 " \"aaabbbcccdddeeeff\"\n" 13046 " \"f\");", 13047 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13048 getLLVMStyleWithColumns(24))); 13049 EXPECT_EQ("someFunction(\n" 13050 " \"aaabbbcc ddde \"\n" 13051 " \"efff\");", 13052 format("someFunction(\"aaabbbcc ddde efff\");", 13053 getLLVMStyleWithColumns(25))); 13054 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13055 " \"ddeeefff\");", 13056 format("someFunction(\"aaabbbccc ddeeefff\");", 13057 getLLVMStyleWithColumns(25))); 13058 EXPECT_EQ("someFunction1234567890(\n" 13059 " \"aaabb \"\n" 13060 " \"cccdddeeefff\");", 13061 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13062 getLLVMStyleWithColumns(25))); 13063 EXPECT_EQ("#define A \\\n" 13064 " string s = \\\n" 13065 " \"123456789\" \\\n" 13066 " \"0\"; \\\n" 13067 " int i;", 13068 format("#define A string s = \"1234567890\"; int i;", 13069 getLLVMStyleWithColumns(20))); 13070 EXPECT_EQ("someFunction(\n" 13071 " \"aaabbbcc \"\n" 13072 " \"dddeeefff\");", 13073 format("someFunction(\"aaabbbcc dddeeefff\");", 13074 getLLVMStyleWithColumns(25))); 13075 } 13076 13077 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13078 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13079 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13080 EXPECT_EQ("\"test\"\n" 13081 "\"\\n\"", 13082 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13083 EXPECT_EQ("\"tes\\\\\"\n" 13084 "\"n\"", 13085 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13086 EXPECT_EQ("\"\\\\\\\\\"\n" 13087 "\"\\n\"", 13088 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13089 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13090 EXPECT_EQ("\"\\uff01\"\n" 13091 "\"test\"", 13092 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13093 EXPECT_EQ("\"\\Uff01ff02\"", 13094 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13095 EXPECT_EQ("\"\\x000000000001\"\n" 13096 "\"next\"", 13097 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13098 EXPECT_EQ("\"\\x000000000001next\"", 13099 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13100 EXPECT_EQ("\"\\x000000000001\"", 13101 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13102 EXPECT_EQ("\"test\"\n" 13103 "\"\\000000\"\n" 13104 "\"000001\"", 13105 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13106 EXPECT_EQ("\"test\\000\"\n" 13107 "\"00000000\"\n" 13108 "\"1\"", 13109 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13110 } 13111 13112 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13113 verifyFormat("void f() {\n" 13114 " return g() {}\n" 13115 " void h() {}"); 13116 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13117 "g();\n" 13118 "}"); 13119 } 13120 13121 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13122 verifyFormat( 13123 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13124 } 13125 13126 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13127 verifyFormat("class X {\n" 13128 " void f() {\n" 13129 " }\n" 13130 "};", 13131 getLLVMStyleWithColumns(12)); 13132 } 13133 13134 TEST_F(FormatTest, ConfigurableIndentWidth) { 13135 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13136 EightIndent.IndentWidth = 8; 13137 EightIndent.ContinuationIndentWidth = 8; 13138 verifyFormat("void f() {\n" 13139 " someFunction();\n" 13140 " if (true) {\n" 13141 " f();\n" 13142 " }\n" 13143 "}", 13144 EightIndent); 13145 verifyFormat("class X {\n" 13146 " void f() {\n" 13147 " }\n" 13148 "};", 13149 EightIndent); 13150 verifyFormat("int x[] = {\n" 13151 " call(),\n" 13152 " call()};", 13153 EightIndent); 13154 } 13155 13156 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13157 verifyFormat("double\n" 13158 "f();", 13159 getLLVMStyleWithColumns(8)); 13160 } 13161 13162 TEST_F(FormatTest, ConfigurableUseOfTab) { 13163 FormatStyle Tab = getLLVMStyleWithColumns(42); 13164 Tab.IndentWidth = 8; 13165 Tab.UseTab = FormatStyle::UT_Always; 13166 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13167 13168 EXPECT_EQ("if (aaaaaaaa && // q\n" 13169 " bb)\t\t// w\n" 13170 "\t;", 13171 format("if (aaaaaaaa &&// q\n" 13172 "bb)// w\n" 13173 ";", 13174 Tab)); 13175 EXPECT_EQ("if (aaa && bbb) // w\n" 13176 "\t;", 13177 format("if(aaa&&bbb)// w\n" 13178 ";", 13179 Tab)); 13180 13181 verifyFormat("class X {\n" 13182 "\tvoid f() {\n" 13183 "\t\tsomeFunction(parameter1,\n" 13184 "\t\t\t parameter2);\n" 13185 "\t}\n" 13186 "};", 13187 Tab); 13188 verifyFormat("#define A \\\n" 13189 "\tvoid f() { \\\n" 13190 "\t\tsomeFunction( \\\n" 13191 "\t\t parameter1, \\\n" 13192 "\t\t parameter2); \\\n" 13193 "\t}", 13194 Tab); 13195 verifyFormat("int a;\t // x\n" 13196 "int bbbbbbbb; // x\n", 13197 Tab); 13198 13199 Tab.TabWidth = 4; 13200 Tab.IndentWidth = 8; 13201 verifyFormat("class TabWidth4Indent8 {\n" 13202 "\t\tvoid f() {\n" 13203 "\t\t\t\tsomeFunction(parameter1,\n" 13204 "\t\t\t\t\t\t\t parameter2);\n" 13205 "\t\t}\n" 13206 "};", 13207 Tab); 13208 13209 Tab.TabWidth = 4; 13210 Tab.IndentWidth = 4; 13211 verifyFormat("class TabWidth4Indent4 {\n" 13212 "\tvoid f() {\n" 13213 "\t\tsomeFunction(parameter1,\n" 13214 "\t\t\t\t\t parameter2);\n" 13215 "\t}\n" 13216 "};", 13217 Tab); 13218 13219 Tab.TabWidth = 8; 13220 Tab.IndentWidth = 4; 13221 verifyFormat("class TabWidth8Indent4 {\n" 13222 " void f() {\n" 13223 "\tsomeFunction(parameter1,\n" 13224 "\t\t parameter2);\n" 13225 " }\n" 13226 "};", 13227 Tab); 13228 13229 Tab.TabWidth = 8; 13230 Tab.IndentWidth = 8; 13231 EXPECT_EQ("/*\n" 13232 "\t a\t\tcomment\n" 13233 "\t in multiple lines\n" 13234 " */", 13235 format(" /*\t \t \n" 13236 " \t \t a\t\tcomment\t \t\n" 13237 " \t \t in multiple lines\t\n" 13238 " \t */", 13239 Tab)); 13240 13241 Tab.UseTab = FormatStyle::UT_ForIndentation; 13242 verifyFormat("{\n" 13243 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13244 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13245 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13246 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13247 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13248 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13249 "};", 13250 Tab); 13251 verifyFormat("enum AA {\n" 13252 "\ta1, // Force multiple lines\n" 13253 "\ta2,\n" 13254 "\ta3\n" 13255 "};", 13256 Tab); 13257 EXPECT_EQ("if (aaaaaaaa && // q\n" 13258 " bb) // w\n" 13259 "\t;", 13260 format("if (aaaaaaaa &&// q\n" 13261 "bb)// w\n" 13262 ";", 13263 Tab)); 13264 verifyFormat("class X {\n" 13265 "\tvoid f() {\n" 13266 "\t\tsomeFunction(parameter1,\n" 13267 "\t\t parameter2);\n" 13268 "\t}\n" 13269 "};", 13270 Tab); 13271 verifyFormat("{\n" 13272 "\tQ(\n" 13273 "\t {\n" 13274 "\t\t int a;\n" 13275 "\t\t someFunction(aaaaaaaa,\n" 13276 "\t\t bbbbbbb);\n" 13277 "\t },\n" 13278 "\t p);\n" 13279 "}", 13280 Tab); 13281 EXPECT_EQ("{\n" 13282 "\t/* aaaa\n" 13283 "\t bbbb */\n" 13284 "}", 13285 format("{\n" 13286 "/* aaaa\n" 13287 " bbbb */\n" 13288 "}", 13289 Tab)); 13290 EXPECT_EQ("{\n" 13291 "\t/*\n" 13292 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13293 "\t bbbbbbbbbbbbb\n" 13294 "\t*/\n" 13295 "}", 13296 format("{\n" 13297 "/*\n" 13298 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13299 "*/\n" 13300 "}", 13301 Tab)); 13302 EXPECT_EQ("{\n" 13303 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13304 "\t// bbbbbbbbbbbbb\n" 13305 "}", 13306 format("{\n" 13307 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13308 "}", 13309 Tab)); 13310 EXPECT_EQ("{\n" 13311 "\t/*\n" 13312 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13313 "\t bbbbbbbbbbbbb\n" 13314 "\t*/\n" 13315 "}", 13316 format("{\n" 13317 "\t/*\n" 13318 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13319 "\t*/\n" 13320 "}", 13321 Tab)); 13322 EXPECT_EQ("{\n" 13323 "\t/*\n" 13324 "\n" 13325 "\t*/\n" 13326 "}", 13327 format("{\n" 13328 "\t/*\n" 13329 "\n" 13330 "\t*/\n" 13331 "}", 13332 Tab)); 13333 EXPECT_EQ("{\n" 13334 "\t/*\n" 13335 " asdf\n" 13336 "\t*/\n" 13337 "}", 13338 format("{\n" 13339 "\t/*\n" 13340 " asdf\n" 13341 "\t*/\n" 13342 "}", 13343 Tab)); 13344 13345 Tab.UseTab = FormatStyle::UT_Never; 13346 EXPECT_EQ("/*\n" 13347 " a\t\tcomment\n" 13348 " in multiple lines\n" 13349 " */", 13350 format(" /*\t \t \n" 13351 " \t \t a\t\tcomment\t \t\n" 13352 " \t \t in multiple lines\t\n" 13353 " \t */", 13354 Tab)); 13355 EXPECT_EQ("/* some\n" 13356 " comment */", 13357 format(" \t \t /* some\n" 13358 " \t \t comment */", 13359 Tab)); 13360 EXPECT_EQ("int a; /* some\n" 13361 " comment */", 13362 format(" \t \t int a; /* some\n" 13363 " \t \t comment */", 13364 Tab)); 13365 13366 EXPECT_EQ("int a; /* some\n" 13367 "comment */", 13368 format(" \t \t int\ta; /* some\n" 13369 " \t \t comment */", 13370 Tab)); 13371 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13372 " comment */", 13373 format(" \t \t f(\"\t\t\"); /* some\n" 13374 " \t \t comment */", 13375 Tab)); 13376 EXPECT_EQ("{\n" 13377 " /*\n" 13378 " * Comment\n" 13379 " */\n" 13380 " int i;\n" 13381 "}", 13382 format("{\n" 13383 "\t/*\n" 13384 "\t * Comment\n" 13385 "\t */\n" 13386 "\t int i;\n" 13387 "}", 13388 Tab)); 13389 13390 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13391 Tab.TabWidth = 8; 13392 Tab.IndentWidth = 8; 13393 EXPECT_EQ("if (aaaaaaaa && // q\n" 13394 " bb) // w\n" 13395 "\t;", 13396 format("if (aaaaaaaa &&// q\n" 13397 "bb)// w\n" 13398 ";", 13399 Tab)); 13400 EXPECT_EQ("if (aaa && bbb) // w\n" 13401 "\t;", 13402 format("if(aaa&&bbb)// w\n" 13403 ";", 13404 Tab)); 13405 verifyFormat("class X {\n" 13406 "\tvoid f() {\n" 13407 "\t\tsomeFunction(parameter1,\n" 13408 "\t\t\t parameter2);\n" 13409 "\t}\n" 13410 "};", 13411 Tab); 13412 verifyFormat("#define A \\\n" 13413 "\tvoid f() { \\\n" 13414 "\t\tsomeFunction( \\\n" 13415 "\t\t parameter1, \\\n" 13416 "\t\t parameter2); \\\n" 13417 "\t}", 13418 Tab); 13419 Tab.TabWidth = 4; 13420 Tab.IndentWidth = 8; 13421 verifyFormat("class TabWidth4Indent8 {\n" 13422 "\t\tvoid f() {\n" 13423 "\t\t\t\tsomeFunction(parameter1,\n" 13424 "\t\t\t\t\t\t\t parameter2);\n" 13425 "\t\t}\n" 13426 "};", 13427 Tab); 13428 Tab.TabWidth = 4; 13429 Tab.IndentWidth = 4; 13430 verifyFormat("class TabWidth4Indent4 {\n" 13431 "\tvoid f() {\n" 13432 "\t\tsomeFunction(parameter1,\n" 13433 "\t\t\t\t\t parameter2);\n" 13434 "\t}\n" 13435 "};", 13436 Tab); 13437 Tab.TabWidth = 8; 13438 Tab.IndentWidth = 4; 13439 verifyFormat("class TabWidth8Indent4 {\n" 13440 " void f() {\n" 13441 "\tsomeFunction(parameter1,\n" 13442 "\t\t parameter2);\n" 13443 " }\n" 13444 "};", 13445 Tab); 13446 Tab.TabWidth = 8; 13447 Tab.IndentWidth = 8; 13448 EXPECT_EQ("/*\n" 13449 "\t a\t\tcomment\n" 13450 "\t in multiple lines\n" 13451 " */", 13452 format(" /*\t \t \n" 13453 " \t \t a\t\tcomment\t \t\n" 13454 " \t \t in multiple lines\t\n" 13455 " \t */", 13456 Tab)); 13457 verifyFormat("{\n" 13458 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13459 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13460 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13461 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13462 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13463 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13464 "};", 13465 Tab); 13466 verifyFormat("enum AA {\n" 13467 "\ta1, // Force multiple lines\n" 13468 "\ta2,\n" 13469 "\ta3\n" 13470 "};", 13471 Tab); 13472 EXPECT_EQ("if (aaaaaaaa && // q\n" 13473 " bb) // w\n" 13474 "\t;", 13475 format("if (aaaaaaaa &&// q\n" 13476 "bb)// w\n" 13477 ";", 13478 Tab)); 13479 verifyFormat("class X {\n" 13480 "\tvoid f() {\n" 13481 "\t\tsomeFunction(parameter1,\n" 13482 "\t\t\t parameter2);\n" 13483 "\t}\n" 13484 "};", 13485 Tab); 13486 verifyFormat("{\n" 13487 "\tQ(\n" 13488 "\t {\n" 13489 "\t\t int a;\n" 13490 "\t\t someFunction(aaaaaaaa,\n" 13491 "\t\t\t\t bbbbbbb);\n" 13492 "\t },\n" 13493 "\t p);\n" 13494 "}", 13495 Tab); 13496 EXPECT_EQ("{\n" 13497 "\t/* aaaa\n" 13498 "\t bbbb */\n" 13499 "}", 13500 format("{\n" 13501 "/* aaaa\n" 13502 " bbbb */\n" 13503 "}", 13504 Tab)); 13505 EXPECT_EQ("{\n" 13506 "\t/*\n" 13507 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13508 "\t bbbbbbbbbbbbb\n" 13509 "\t*/\n" 13510 "}", 13511 format("{\n" 13512 "/*\n" 13513 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13514 "*/\n" 13515 "}", 13516 Tab)); 13517 EXPECT_EQ("{\n" 13518 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13519 "\t// bbbbbbbbbbbbb\n" 13520 "}", 13521 format("{\n" 13522 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13523 "}", 13524 Tab)); 13525 EXPECT_EQ("{\n" 13526 "\t/*\n" 13527 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13528 "\t bbbbbbbbbbbbb\n" 13529 "\t*/\n" 13530 "}", 13531 format("{\n" 13532 "\t/*\n" 13533 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13534 "\t*/\n" 13535 "}", 13536 Tab)); 13537 EXPECT_EQ("{\n" 13538 "\t/*\n" 13539 "\n" 13540 "\t*/\n" 13541 "}", 13542 format("{\n" 13543 "\t/*\n" 13544 "\n" 13545 "\t*/\n" 13546 "}", 13547 Tab)); 13548 EXPECT_EQ("{\n" 13549 "\t/*\n" 13550 " asdf\n" 13551 "\t*/\n" 13552 "}", 13553 format("{\n" 13554 "\t/*\n" 13555 " asdf\n" 13556 "\t*/\n" 13557 "}", 13558 Tab)); 13559 EXPECT_EQ("/* some\n" 13560 " comment */", 13561 format(" \t \t /* some\n" 13562 " \t \t comment */", 13563 Tab)); 13564 EXPECT_EQ("int a; /* some\n" 13565 " comment */", 13566 format(" \t \t int a; /* some\n" 13567 " \t \t comment */", 13568 Tab)); 13569 EXPECT_EQ("int a; /* some\n" 13570 "comment */", 13571 format(" \t \t int\ta; /* some\n" 13572 " \t \t comment */", 13573 Tab)); 13574 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13575 " comment */", 13576 format(" \t \t f(\"\t\t\"); /* some\n" 13577 " \t \t comment */", 13578 Tab)); 13579 EXPECT_EQ("{\n" 13580 "\t/*\n" 13581 "\t * Comment\n" 13582 "\t */\n" 13583 "\tint i;\n" 13584 "}", 13585 format("{\n" 13586 "\t/*\n" 13587 "\t * Comment\n" 13588 "\t */\n" 13589 "\t int i;\n" 13590 "}", 13591 Tab)); 13592 Tab.TabWidth = 2; 13593 Tab.IndentWidth = 2; 13594 EXPECT_EQ("{\n" 13595 "\t/* aaaa\n" 13596 "\t\t bbbb */\n" 13597 "}", 13598 format("{\n" 13599 "/* aaaa\n" 13600 "\t bbbb */\n" 13601 "}", 13602 Tab)); 13603 EXPECT_EQ("{\n" 13604 "\t/*\n" 13605 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13606 "\t\tbbbbbbbbbbbbb\n" 13607 "\t*/\n" 13608 "}", 13609 format("{\n" 13610 "/*\n" 13611 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13612 "*/\n" 13613 "}", 13614 Tab)); 13615 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13616 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13617 Tab.TabWidth = 4; 13618 Tab.IndentWidth = 4; 13619 verifyFormat("class Assign {\n" 13620 "\tvoid f() {\n" 13621 "\t\tint x = 123;\n" 13622 "\t\tint random = 4;\n" 13623 "\t\tstd::string alphabet =\n" 13624 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13625 "\t}\n" 13626 "};", 13627 Tab); 13628 13629 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13630 Tab.TabWidth = 8; 13631 Tab.IndentWidth = 8; 13632 EXPECT_EQ("if (aaaaaaaa && // q\n" 13633 " bb) // w\n" 13634 "\t;", 13635 format("if (aaaaaaaa &&// q\n" 13636 "bb)// w\n" 13637 ";", 13638 Tab)); 13639 EXPECT_EQ("if (aaa && bbb) // w\n" 13640 "\t;", 13641 format("if(aaa&&bbb)// w\n" 13642 ";", 13643 Tab)); 13644 verifyFormat("class X {\n" 13645 "\tvoid f() {\n" 13646 "\t\tsomeFunction(parameter1,\n" 13647 "\t\t parameter2);\n" 13648 "\t}\n" 13649 "};", 13650 Tab); 13651 verifyFormat("#define A \\\n" 13652 "\tvoid f() { \\\n" 13653 "\t\tsomeFunction( \\\n" 13654 "\t\t parameter1, \\\n" 13655 "\t\t parameter2); \\\n" 13656 "\t}", 13657 Tab); 13658 Tab.TabWidth = 4; 13659 Tab.IndentWidth = 8; 13660 verifyFormat("class TabWidth4Indent8 {\n" 13661 "\t\tvoid f() {\n" 13662 "\t\t\t\tsomeFunction(parameter1,\n" 13663 "\t\t\t\t parameter2);\n" 13664 "\t\t}\n" 13665 "};", 13666 Tab); 13667 Tab.TabWidth = 4; 13668 Tab.IndentWidth = 4; 13669 verifyFormat("class TabWidth4Indent4 {\n" 13670 "\tvoid f() {\n" 13671 "\t\tsomeFunction(parameter1,\n" 13672 "\t\t parameter2);\n" 13673 "\t}\n" 13674 "};", 13675 Tab); 13676 Tab.TabWidth = 8; 13677 Tab.IndentWidth = 4; 13678 verifyFormat("class TabWidth8Indent4 {\n" 13679 " void f() {\n" 13680 "\tsomeFunction(parameter1,\n" 13681 "\t parameter2);\n" 13682 " }\n" 13683 "};", 13684 Tab); 13685 Tab.TabWidth = 8; 13686 Tab.IndentWidth = 8; 13687 EXPECT_EQ("/*\n" 13688 " a\t\tcomment\n" 13689 " in multiple lines\n" 13690 " */", 13691 format(" /*\t \t \n" 13692 " \t \t a\t\tcomment\t \t\n" 13693 " \t \t in multiple lines\t\n" 13694 " \t */", 13695 Tab)); 13696 verifyFormat("{\n" 13697 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13698 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13699 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13700 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13701 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13702 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13703 "};", 13704 Tab); 13705 verifyFormat("enum AA {\n" 13706 "\ta1, // Force multiple lines\n" 13707 "\ta2,\n" 13708 "\ta3\n" 13709 "};", 13710 Tab); 13711 EXPECT_EQ("if (aaaaaaaa && // q\n" 13712 " bb) // w\n" 13713 "\t;", 13714 format("if (aaaaaaaa &&// q\n" 13715 "bb)// w\n" 13716 ";", 13717 Tab)); 13718 verifyFormat("class X {\n" 13719 "\tvoid f() {\n" 13720 "\t\tsomeFunction(parameter1,\n" 13721 "\t\t parameter2);\n" 13722 "\t}\n" 13723 "};", 13724 Tab); 13725 verifyFormat("{\n" 13726 "\tQ(\n" 13727 "\t {\n" 13728 "\t\t int a;\n" 13729 "\t\t someFunction(aaaaaaaa,\n" 13730 "\t\t bbbbbbb);\n" 13731 "\t },\n" 13732 "\t p);\n" 13733 "}", 13734 Tab); 13735 EXPECT_EQ("{\n" 13736 "\t/* aaaa\n" 13737 "\t bbbb */\n" 13738 "}", 13739 format("{\n" 13740 "/* aaaa\n" 13741 " bbbb */\n" 13742 "}", 13743 Tab)); 13744 EXPECT_EQ("{\n" 13745 "\t/*\n" 13746 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13747 "\t bbbbbbbbbbbbb\n" 13748 "\t*/\n" 13749 "}", 13750 format("{\n" 13751 "/*\n" 13752 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13753 "*/\n" 13754 "}", 13755 Tab)); 13756 EXPECT_EQ("{\n" 13757 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13758 "\t// bbbbbbbbbbbbb\n" 13759 "}", 13760 format("{\n" 13761 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13762 "}", 13763 Tab)); 13764 EXPECT_EQ("{\n" 13765 "\t/*\n" 13766 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13767 "\t bbbbbbbbbbbbb\n" 13768 "\t*/\n" 13769 "}", 13770 format("{\n" 13771 "\t/*\n" 13772 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13773 "\t*/\n" 13774 "}", 13775 Tab)); 13776 EXPECT_EQ("{\n" 13777 "\t/*\n" 13778 "\n" 13779 "\t*/\n" 13780 "}", 13781 format("{\n" 13782 "\t/*\n" 13783 "\n" 13784 "\t*/\n" 13785 "}", 13786 Tab)); 13787 EXPECT_EQ("{\n" 13788 "\t/*\n" 13789 " asdf\n" 13790 "\t*/\n" 13791 "}", 13792 format("{\n" 13793 "\t/*\n" 13794 " asdf\n" 13795 "\t*/\n" 13796 "}", 13797 Tab)); 13798 EXPECT_EQ("/* some\n" 13799 " comment */", 13800 format(" \t \t /* some\n" 13801 " \t \t comment */", 13802 Tab)); 13803 EXPECT_EQ("int a; /* some\n" 13804 " comment */", 13805 format(" \t \t int a; /* some\n" 13806 " \t \t comment */", 13807 Tab)); 13808 EXPECT_EQ("int a; /* some\n" 13809 "comment */", 13810 format(" \t \t int\ta; /* some\n" 13811 " \t \t comment */", 13812 Tab)); 13813 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13814 " comment */", 13815 format(" \t \t f(\"\t\t\"); /* some\n" 13816 " \t \t comment */", 13817 Tab)); 13818 EXPECT_EQ("{\n" 13819 "\t/*\n" 13820 "\t * Comment\n" 13821 "\t */\n" 13822 "\tint i;\n" 13823 "}", 13824 format("{\n" 13825 "\t/*\n" 13826 "\t * Comment\n" 13827 "\t */\n" 13828 "\t int i;\n" 13829 "}", 13830 Tab)); 13831 Tab.TabWidth = 2; 13832 Tab.IndentWidth = 2; 13833 EXPECT_EQ("{\n" 13834 "\t/* aaaa\n" 13835 "\t bbbb */\n" 13836 "}", 13837 format("{\n" 13838 "/* aaaa\n" 13839 " bbbb */\n" 13840 "}", 13841 Tab)); 13842 EXPECT_EQ("{\n" 13843 "\t/*\n" 13844 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13845 "\t bbbbbbbbbbbbb\n" 13846 "\t*/\n" 13847 "}", 13848 format("{\n" 13849 "/*\n" 13850 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13851 "*/\n" 13852 "}", 13853 Tab)); 13854 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13855 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13856 Tab.TabWidth = 4; 13857 Tab.IndentWidth = 4; 13858 verifyFormat("class Assign {\n" 13859 "\tvoid f() {\n" 13860 "\t\tint x = 123;\n" 13861 "\t\tint random = 4;\n" 13862 "\t\tstd::string alphabet =\n" 13863 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13864 "\t}\n" 13865 "};", 13866 Tab); 13867 Tab.AlignOperands = FormatStyle::OAS_Align; 13868 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 13869 " cccccccccccccccccccc;", 13870 Tab); 13871 // no alignment 13872 verifyFormat("int aaaaaaaaaa =\n" 13873 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 13874 Tab); 13875 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 13876 " : bbbbbbbbbbbbbb ? 222222222222222\n" 13877 " : 333333333333333;", 13878 Tab); 13879 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 13880 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 13881 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 13882 " + cccccccccccccccccccc;", 13883 Tab); 13884 } 13885 13886 TEST_F(FormatTest, ZeroTabWidth) { 13887 FormatStyle Tab = getLLVMStyleWithColumns(42); 13888 Tab.IndentWidth = 8; 13889 Tab.UseTab = FormatStyle::UT_Never; 13890 Tab.TabWidth = 0; 13891 EXPECT_EQ("void a(){\n" 13892 " // line starts with '\t'\n" 13893 "};", 13894 format("void a(){\n" 13895 "\t// line starts with '\t'\n" 13896 "};", 13897 Tab)); 13898 13899 EXPECT_EQ("void a(){\n" 13900 " // line starts with '\t'\n" 13901 "};", 13902 format("void a(){\n" 13903 "\t\t// line starts with '\t'\n" 13904 "};", 13905 Tab)); 13906 13907 Tab.UseTab = FormatStyle::UT_ForIndentation; 13908 EXPECT_EQ("void a(){\n" 13909 " // line starts with '\t'\n" 13910 "};", 13911 format("void a(){\n" 13912 "\t// line starts with '\t'\n" 13913 "};", 13914 Tab)); 13915 13916 EXPECT_EQ("void a(){\n" 13917 " // line starts with '\t'\n" 13918 "};", 13919 format("void a(){\n" 13920 "\t\t// line starts with '\t'\n" 13921 "};", 13922 Tab)); 13923 13924 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13925 EXPECT_EQ("void a(){\n" 13926 " // line starts with '\t'\n" 13927 "};", 13928 format("void a(){\n" 13929 "\t// line starts with '\t'\n" 13930 "};", 13931 Tab)); 13932 13933 EXPECT_EQ("void a(){\n" 13934 " // line starts with '\t'\n" 13935 "};", 13936 format("void a(){\n" 13937 "\t\t// line starts with '\t'\n" 13938 "};", 13939 Tab)); 13940 13941 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13942 EXPECT_EQ("void a(){\n" 13943 " // line starts with '\t'\n" 13944 "};", 13945 format("void a(){\n" 13946 "\t// line starts with '\t'\n" 13947 "};", 13948 Tab)); 13949 13950 EXPECT_EQ("void a(){\n" 13951 " // line starts with '\t'\n" 13952 "};", 13953 format("void a(){\n" 13954 "\t\t// line starts with '\t'\n" 13955 "};", 13956 Tab)); 13957 13958 Tab.UseTab = FormatStyle::UT_Always; 13959 EXPECT_EQ("void a(){\n" 13960 "// line starts with '\t'\n" 13961 "};", 13962 format("void a(){\n" 13963 "\t// line starts with '\t'\n" 13964 "};", 13965 Tab)); 13966 13967 EXPECT_EQ("void a(){\n" 13968 "// line starts with '\t'\n" 13969 "};", 13970 format("void a(){\n" 13971 "\t\t// line starts with '\t'\n" 13972 "};", 13973 Tab)); 13974 } 13975 13976 TEST_F(FormatTest, CalculatesOriginalColumn) { 13977 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13978 "q\"; /* some\n" 13979 " comment */", 13980 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13981 "q\"; /* some\n" 13982 " comment */", 13983 getLLVMStyle())); 13984 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13985 "/* some\n" 13986 " comment */", 13987 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13988 " /* some\n" 13989 " comment */", 13990 getLLVMStyle())); 13991 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13992 "qqq\n" 13993 "/* some\n" 13994 " comment */", 13995 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13996 "qqq\n" 13997 " /* some\n" 13998 " comment */", 13999 getLLVMStyle())); 14000 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14001 "wwww; /* some\n" 14002 " comment */", 14003 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14004 "wwww; /* some\n" 14005 " comment */", 14006 getLLVMStyle())); 14007 } 14008 14009 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 14010 FormatStyle NoSpace = getLLVMStyle(); 14011 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 14012 14013 verifyFormat("while(true)\n" 14014 " continue;", 14015 NoSpace); 14016 verifyFormat("for(;;)\n" 14017 " continue;", 14018 NoSpace); 14019 verifyFormat("if(true)\n" 14020 " f();\n" 14021 "else if(true)\n" 14022 " f();", 14023 NoSpace); 14024 verifyFormat("do {\n" 14025 " do_something();\n" 14026 "} while(something());", 14027 NoSpace); 14028 verifyFormat("switch(x) {\n" 14029 "default:\n" 14030 " break;\n" 14031 "}", 14032 NoSpace); 14033 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14034 verifyFormat("size_t x = sizeof(x);", NoSpace); 14035 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14036 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14037 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14038 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14039 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14040 verifyFormat("alignas(128) char a[128];", NoSpace); 14041 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14042 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14043 verifyFormat("int f() throw(Deprecated);", NoSpace); 14044 verifyFormat("typedef void (*cb)(int);", NoSpace); 14045 verifyFormat("T A::operator()();", NoSpace); 14046 verifyFormat("X A::operator++(T);", NoSpace); 14047 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14048 14049 FormatStyle Space = getLLVMStyle(); 14050 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14051 14052 verifyFormat("int f ();", Space); 14053 verifyFormat("void f (int a, T b) {\n" 14054 " while (true)\n" 14055 " continue;\n" 14056 "}", 14057 Space); 14058 verifyFormat("if (true)\n" 14059 " f ();\n" 14060 "else if (true)\n" 14061 " f ();", 14062 Space); 14063 verifyFormat("do {\n" 14064 " do_something ();\n" 14065 "} while (something ());", 14066 Space); 14067 verifyFormat("switch (x) {\n" 14068 "default:\n" 14069 " break;\n" 14070 "}", 14071 Space); 14072 verifyFormat("A::A () : a (1) {}", Space); 14073 verifyFormat("void f () __attribute__ ((asdf));", Space); 14074 verifyFormat("*(&a + 1);\n" 14075 "&((&a)[1]);\n" 14076 "a[(b + c) * d];\n" 14077 "(((a + 1) * 2) + 3) * 4;", 14078 Space); 14079 verifyFormat("#define A(x) x", Space); 14080 verifyFormat("#define A (x) x", Space); 14081 verifyFormat("#if defined(x)\n" 14082 "#endif", 14083 Space); 14084 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14085 verifyFormat("size_t x = sizeof (x);", Space); 14086 verifyFormat("auto f (int x) -> decltype (x);", Space); 14087 verifyFormat("auto f (int x) -> typeof (x);", Space); 14088 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14089 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14090 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14091 verifyFormat("alignas (128) char a[128];", Space); 14092 verifyFormat("size_t x = alignof (MyType);", Space); 14093 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14094 verifyFormat("int f () throw (Deprecated);", Space); 14095 verifyFormat("typedef void (*cb) (int);", Space); 14096 verifyFormat("T A::operator() ();", Space); 14097 verifyFormat("X A::operator++ (T);", Space); 14098 verifyFormat("auto lambda = [] () { return 0; };", Space); 14099 verifyFormat("int x = int (y);", Space); 14100 14101 FormatStyle SomeSpace = getLLVMStyle(); 14102 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14103 14104 verifyFormat("[]() -> float {}", SomeSpace); 14105 verifyFormat("[] (auto foo) {}", SomeSpace); 14106 verifyFormat("[foo]() -> int {}", SomeSpace); 14107 verifyFormat("int f();", SomeSpace); 14108 verifyFormat("void f (int a, T b) {\n" 14109 " while (true)\n" 14110 " continue;\n" 14111 "}", 14112 SomeSpace); 14113 verifyFormat("if (true)\n" 14114 " f();\n" 14115 "else if (true)\n" 14116 " f();", 14117 SomeSpace); 14118 verifyFormat("do {\n" 14119 " do_something();\n" 14120 "} while (something());", 14121 SomeSpace); 14122 verifyFormat("switch (x) {\n" 14123 "default:\n" 14124 " break;\n" 14125 "}", 14126 SomeSpace); 14127 verifyFormat("A::A() : a (1) {}", SomeSpace); 14128 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14129 verifyFormat("*(&a + 1);\n" 14130 "&((&a)[1]);\n" 14131 "a[(b + c) * d];\n" 14132 "(((a + 1) * 2) + 3) * 4;", 14133 SomeSpace); 14134 verifyFormat("#define A(x) x", SomeSpace); 14135 verifyFormat("#define A (x) x", SomeSpace); 14136 verifyFormat("#if defined(x)\n" 14137 "#endif", 14138 SomeSpace); 14139 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14140 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14141 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14142 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14143 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14144 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14145 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14146 verifyFormat("alignas (128) char a[128];", SomeSpace); 14147 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14148 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14149 SomeSpace); 14150 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14151 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14152 verifyFormat("T A::operator()();", SomeSpace); 14153 verifyFormat("X A::operator++ (T);", SomeSpace); 14154 verifyFormat("int x = int (y);", SomeSpace); 14155 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14156 14157 FormatStyle SpaceControlStatements = getLLVMStyle(); 14158 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14159 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 14160 14161 verifyFormat("while (true)\n" 14162 " continue;", 14163 SpaceControlStatements); 14164 verifyFormat("if (true)\n" 14165 " f();\n" 14166 "else if (true)\n" 14167 " f();", 14168 SpaceControlStatements); 14169 verifyFormat("for (;;) {\n" 14170 " do_something();\n" 14171 "}", 14172 SpaceControlStatements); 14173 verifyFormat("do {\n" 14174 " do_something();\n" 14175 "} while (something());", 14176 SpaceControlStatements); 14177 verifyFormat("switch (x) {\n" 14178 "default:\n" 14179 " break;\n" 14180 "}", 14181 SpaceControlStatements); 14182 14183 FormatStyle SpaceFuncDecl = getLLVMStyle(); 14184 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14185 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 14186 14187 verifyFormat("int f ();", SpaceFuncDecl); 14188 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 14189 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 14190 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 14191 verifyFormat("#define A(x) x", SpaceFuncDecl); 14192 verifyFormat("#define A (x) x", SpaceFuncDecl); 14193 verifyFormat("#if defined(x)\n" 14194 "#endif", 14195 SpaceFuncDecl); 14196 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 14197 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 14198 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 14199 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 14200 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 14201 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 14202 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 14203 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 14204 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 14205 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14206 SpaceFuncDecl); 14207 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 14208 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 14209 verifyFormat("T A::operator() ();", SpaceFuncDecl); 14210 verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 14211 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 14212 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 14213 verifyFormat("int x = int(y);", SpaceFuncDecl); 14214 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14215 SpaceFuncDecl); 14216 14217 FormatStyle SpaceFuncDef = getLLVMStyle(); 14218 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14219 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 14220 14221 verifyFormat("int f();", SpaceFuncDef); 14222 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 14223 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 14224 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 14225 verifyFormat("#define A(x) x", SpaceFuncDef); 14226 verifyFormat("#define A (x) x", SpaceFuncDef); 14227 verifyFormat("#if defined(x)\n" 14228 "#endif", 14229 SpaceFuncDef); 14230 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 14231 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 14232 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 14233 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 14234 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 14235 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 14236 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 14237 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 14238 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 14239 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14240 SpaceFuncDef); 14241 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 14242 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 14243 verifyFormat("T A::operator()();", SpaceFuncDef); 14244 verifyFormat("X A::operator++(T);", SpaceFuncDef); 14245 verifyFormat("T A::operator() () {}", SpaceFuncDef); 14246 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 14247 verifyFormat("int x = int(y);", SpaceFuncDef); 14248 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14249 SpaceFuncDef); 14250 14251 FormatStyle SpaceIfMacros = getLLVMStyle(); 14252 SpaceIfMacros.IfMacros.clear(); 14253 SpaceIfMacros.IfMacros.push_back("MYIF"); 14254 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14255 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 14256 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 14257 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 14258 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 14259 14260 FormatStyle SpaceForeachMacros = getLLVMStyle(); 14261 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14262 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 14263 verifyFormat("foreach (Item *item, itemlist) {}", SpaceForeachMacros); 14264 verifyFormat("Q_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros); 14265 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros); 14266 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 14267 14268 FormatStyle SomeSpace2 = getLLVMStyle(); 14269 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14270 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 14271 verifyFormat("[]() -> float {}", SomeSpace2); 14272 verifyFormat("[] (auto foo) {}", SomeSpace2); 14273 verifyFormat("[foo]() -> int {}", SomeSpace2); 14274 verifyFormat("int f();", SomeSpace2); 14275 verifyFormat("void f (int a, T b) {\n" 14276 " while (true)\n" 14277 " continue;\n" 14278 "}", 14279 SomeSpace2); 14280 verifyFormat("if (true)\n" 14281 " f();\n" 14282 "else if (true)\n" 14283 " f();", 14284 SomeSpace2); 14285 verifyFormat("do {\n" 14286 " do_something();\n" 14287 "} while (something());", 14288 SomeSpace2); 14289 verifyFormat("switch (x) {\n" 14290 "default:\n" 14291 " break;\n" 14292 "}", 14293 SomeSpace2); 14294 verifyFormat("A::A() : a (1) {}", SomeSpace2); 14295 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 14296 verifyFormat("*(&a + 1);\n" 14297 "&((&a)[1]);\n" 14298 "a[(b + c) * d];\n" 14299 "(((a + 1) * 2) + 3) * 4;", 14300 SomeSpace2); 14301 verifyFormat("#define A(x) x", SomeSpace2); 14302 verifyFormat("#define A (x) x", SomeSpace2); 14303 verifyFormat("#if defined(x)\n" 14304 "#endif", 14305 SomeSpace2); 14306 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 14307 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 14308 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 14309 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 14310 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 14311 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 14312 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 14313 verifyFormat("alignas (128) char a[128];", SomeSpace2); 14314 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 14315 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14316 SomeSpace2); 14317 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 14318 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 14319 verifyFormat("T A::operator()();", SomeSpace2); 14320 verifyFormat("X A::operator++ (T);", SomeSpace2); 14321 verifyFormat("int x = int (y);", SomeSpace2); 14322 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 14323 } 14324 14325 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14326 FormatStyle Spaces = getLLVMStyle(); 14327 Spaces.SpaceAfterLogicalNot = true; 14328 14329 verifyFormat("bool x = ! y", Spaces); 14330 verifyFormat("if (! isFailure())", Spaces); 14331 verifyFormat("if (! (a && b))", Spaces); 14332 verifyFormat("\"Error!\"", Spaces); 14333 verifyFormat("! ! x", Spaces); 14334 } 14335 14336 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14337 FormatStyle Spaces = getLLVMStyle(); 14338 14339 Spaces.SpacesInParentheses = true; 14340 verifyFormat("do_something( ::globalVar );", Spaces); 14341 verifyFormat("call( x, y, z );", Spaces); 14342 verifyFormat("call();", Spaces); 14343 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14344 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14345 Spaces); 14346 verifyFormat("while ( (bool)1 )\n" 14347 " continue;", 14348 Spaces); 14349 verifyFormat("for ( ;; )\n" 14350 " continue;", 14351 Spaces); 14352 verifyFormat("if ( true )\n" 14353 " f();\n" 14354 "else if ( true )\n" 14355 " f();", 14356 Spaces); 14357 verifyFormat("do {\n" 14358 " do_something( (int)i );\n" 14359 "} while ( something() );", 14360 Spaces); 14361 verifyFormat("switch ( x ) {\n" 14362 "default:\n" 14363 " break;\n" 14364 "}", 14365 Spaces); 14366 14367 Spaces.SpacesInParentheses = false; 14368 Spaces.SpacesInCStyleCastParentheses = true; 14369 verifyFormat("Type *A = ( Type * )P;", Spaces); 14370 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14371 verifyFormat("x = ( int32 )y;", Spaces); 14372 verifyFormat("int a = ( int )(2.0f);", Spaces); 14373 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14374 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14375 verifyFormat("#define x (( int )-1)", Spaces); 14376 14377 // Run the first set of tests again with: 14378 Spaces.SpacesInParentheses = false; 14379 Spaces.SpaceInEmptyParentheses = true; 14380 Spaces.SpacesInCStyleCastParentheses = true; 14381 verifyFormat("call(x, y, z);", Spaces); 14382 verifyFormat("call( );", Spaces); 14383 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14384 verifyFormat("while (( bool )1)\n" 14385 " continue;", 14386 Spaces); 14387 verifyFormat("for (;;)\n" 14388 " continue;", 14389 Spaces); 14390 verifyFormat("if (true)\n" 14391 " f( );\n" 14392 "else if (true)\n" 14393 " f( );", 14394 Spaces); 14395 verifyFormat("do {\n" 14396 " do_something(( int )i);\n" 14397 "} while (something( ));", 14398 Spaces); 14399 verifyFormat("switch (x) {\n" 14400 "default:\n" 14401 " break;\n" 14402 "}", 14403 Spaces); 14404 14405 // Run the first set of tests again with: 14406 Spaces.SpaceAfterCStyleCast = true; 14407 verifyFormat("call(x, y, z);", Spaces); 14408 verifyFormat("call( );", Spaces); 14409 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14410 verifyFormat("while (( bool ) 1)\n" 14411 " continue;", 14412 Spaces); 14413 verifyFormat("for (;;)\n" 14414 " continue;", 14415 Spaces); 14416 verifyFormat("if (true)\n" 14417 " f( );\n" 14418 "else if (true)\n" 14419 " f( );", 14420 Spaces); 14421 verifyFormat("do {\n" 14422 " do_something(( int ) i);\n" 14423 "} while (something( ));", 14424 Spaces); 14425 verifyFormat("switch (x) {\n" 14426 "default:\n" 14427 " break;\n" 14428 "}", 14429 Spaces); 14430 14431 // Run subset of tests again with: 14432 Spaces.SpacesInCStyleCastParentheses = false; 14433 Spaces.SpaceAfterCStyleCast = true; 14434 verifyFormat("while ((bool) 1)\n" 14435 " continue;", 14436 Spaces); 14437 verifyFormat("do {\n" 14438 " do_something((int) i);\n" 14439 "} while (something( ));", 14440 Spaces); 14441 14442 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14443 verifyFormat("size_t idx = (size_t) a;", Spaces); 14444 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14445 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14446 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14447 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14448 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14449 Spaces.ColumnLimit = 80; 14450 Spaces.IndentWidth = 4; 14451 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14452 verifyFormat("void foo( ) {\n" 14453 " size_t foo = (*(function))(\n" 14454 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14455 "BarrrrrrrrrrrrLong,\n" 14456 " FoooooooooLooooong);\n" 14457 "}", 14458 Spaces); 14459 Spaces.SpaceAfterCStyleCast = false; 14460 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14461 verifyFormat("size_t idx = (size_t)a;", Spaces); 14462 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14463 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14464 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14465 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14466 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14467 14468 verifyFormat("void foo( ) {\n" 14469 " size_t foo = (*(function))(\n" 14470 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14471 "BarrrrrrrrrrrrLong,\n" 14472 " FoooooooooLooooong);\n" 14473 "}", 14474 Spaces); 14475 } 14476 14477 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14478 verifyFormat("int a[5];"); 14479 verifyFormat("a[3] += 42;"); 14480 14481 FormatStyle Spaces = getLLVMStyle(); 14482 Spaces.SpacesInSquareBrackets = true; 14483 // Not lambdas. 14484 verifyFormat("int a[ 5 ];", Spaces); 14485 verifyFormat("a[ 3 ] += 42;", Spaces); 14486 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14487 verifyFormat("double &operator[](int i) { return 0; }\n" 14488 "int i;", 14489 Spaces); 14490 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14491 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14492 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14493 // Lambdas. 14494 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14495 verifyFormat("return [ i, args... ] {};", Spaces); 14496 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14497 verifyFormat("int foo = [ = ]() {};", Spaces); 14498 verifyFormat("int foo = [ & ]() {};", Spaces); 14499 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14500 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14501 } 14502 14503 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14504 FormatStyle NoSpaceStyle = getLLVMStyle(); 14505 verifyFormat("int a[5];", NoSpaceStyle); 14506 verifyFormat("a[3] += 42;", NoSpaceStyle); 14507 14508 verifyFormat("int a[1];", NoSpaceStyle); 14509 verifyFormat("int 1 [a];", NoSpaceStyle); 14510 verifyFormat("int a[1][2];", NoSpaceStyle); 14511 verifyFormat("a[7] = 5;", NoSpaceStyle); 14512 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14513 verifyFormat("f([] {})", NoSpaceStyle); 14514 14515 FormatStyle Space = getLLVMStyle(); 14516 Space.SpaceBeforeSquareBrackets = true; 14517 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14518 verifyFormat("return [i, args...] {};", Space); 14519 14520 verifyFormat("int a [5];", Space); 14521 verifyFormat("a [3] += 42;", Space); 14522 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14523 verifyFormat("double &operator[](int i) { return 0; }\n" 14524 "int i;", 14525 Space); 14526 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14527 verifyFormat("int i = a [a][a]->f();", Space); 14528 verifyFormat("int i = (*b) [a]->f();", Space); 14529 14530 verifyFormat("int a [1];", Space); 14531 verifyFormat("int 1 [a];", Space); 14532 verifyFormat("int a [1][2];", Space); 14533 verifyFormat("a [7] = 5;", Space); 14534 verifyFormat("int a = (f()) [23];", Space); 14535 verifyFormat("f([] {})", Space); 14536 } 14537 14538 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14539 verifyFormat("int a = 5;"); 14540 verifyFormat("a += 42;"); 14541 verifyFormat("a or_eq 8;"); 14542 14543 FormatStyle Spaces = getLLVMStyle(); 14544 Spaces.SpaceBeforeAssignmentOperators = false; 14545 verifyFormat("int a= 5;", Spaces); 14546 verifyFormat("a+= 42;", Spaces); 14547 verifyFormat("a or_eq 8;", Spaces); 14548 } 14549 14550 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14551 verifyFormat("class Foo : public Bar {};"); 14552 verifyFormat("Foo::Foo() : foo(1) {}"); 14553 verifyFormat("for (auto a : b) {\n}"); 14554 verifyFormat("int x = a ? b : c;"); 14555 verifyFormat("{\n" 14556 "label0:\n" 14557 " int x = 0;\n" 14558 "}"); 14559 verifyFormat("switch (x) {\n" 14560 "case 1:\n" 14561 "default:\n" 14562 "}"); 14563 verifyFormat("switch (allBraces) {\n" 14564 "case 1: {\n" 14565 " break;\n" 14566 "}\n" 14567 "case 2: {\n" 14568 " [[fallthrough]];\n" 14569 "}\n" 14570 "default: {\n" 14571 " break;\n" 14572 "}\n" 14573 "}"); 14574 14575 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14576 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14577 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14578 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14579 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14580 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14581 verifyFormat("{\n" 14582 "label1:\n" 14583 " int x = 0;\n" 14584 "}", 14585 CtorInitializerStyle); 14586 verifyFormat("switch (x) {\n" 14587 "case 1:\n" 14588 "default:\n" 14589 "}", 14590 CtorInitializerStyle); 14591 verifyFormat("switch (allBraces) {\n" 14592 "case 1: {\n" 14593 " break;\n" 14594 "}\n" 14595 "case 2: {\n" 14596 " [[fallthrough]];\n" 14597 "}\n" 14598 "default: {\n" 14599 " break;\n" 14600 "}\n" 14601 "}", 14602 CtorInitializerStyle); 14603 CtorInitializerStyle.BreakConstructorInitializers = 14604 FormatStyle::BCIS_AfterColon; 14605 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14606 " aaaaaaaaaaaaaaaa(1),\n" 14607 " bbbbbbbbbbbbbbbb(2) {}", 14608 CtorInitializerStyle); 14609 CtorInitializerStyle.BreakConstructorInitializers = 14610 FormatStyle::BCIS_BeforeComma; 14611 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14612 " : aaaaaaaaaaaaaaaa(1)\n" 14613 " , bbbbbbbbbbbbbbbb(2) {}", 14614 CtorInitializerStyle); 14615 CtorInitializerStyle.BreakConstructorInitializers = 14616 FormatStyle::BCIS_BeforeColon; 14617 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14618 " : aaaaaaaaaaaaaaaa(1),\n" 14619 " bbbbbbbbbbbbbbbb(2) {}", 14620 CtorInitializerStyle); 14621 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14622 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14623 ": aaaaaaaaaaaaaaaa(1),\n" 14624 " bbbbbbbbbbbbbbbb(2) {}", 14625 CtorInitializerStyle); 14626 14627 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14628 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14629 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14630 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14631 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14632 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14633 verifyFormat("{\n" 14634 "label2:\n" 14635 " int x = 0;\n" 14636 "}", 14637 InheritanceStyle); 14638 verifyFormat("switch (x) {\n" 14639 "case 1:\n" 14640 "default:\n" 14641 "}", 14642 InheritanceStyle); 14643 verifyFormat("switch (allBraces) {\n" 14644 "case 1: {\n" 14645 " break;\n" 14646 "}\n" 14647 "case 2: {\n" 14648 " [[fallthrough]];\n" 14649 "}\n" 14650 "default: {\n" 14651 " break;\n" 14652 "}\n" 14653 "}", 14654 InheritanceStyle); 14655 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14656 verifyFormat("class Foooooooooooooooooooooo\n" 14657 " : public aaaaaaaaaaaaaaaaaa,\n" 14658 " public bbbbbbbbbbbbbbbbbb {\n" 14659 "}", 14660 InheritanceStyle); 14661 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14662 verifyFormat("class Foooooooooooooooooooooo:\n" 14663 " public aaaaaaaaaaaaaaaaaa,\n" 14664 " public bbbbbbbbbbbbbbbbbb {\n" 14665 "}", 14666 InheritanceStyle); 14667 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14668 verifyFormat("class Foooooooooooooooooooooo\n" 14669 " : public aaaaaaaaaaaaaaaaaa\n" 14670 " , public bbbbbbbbbbbbbbbbbb {\n" 14671 "}", 14672 InheritanceStyle); 14673 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14674 verifyFormat("class Foooooooooooooooooooooo\n" 14675 " : public aaaaaaaaaaaaaaaaaa,\n" 14676 " public bbbbbbbbbbbbbbbbbb {\n" 14677 "}", 14678 InheritanceStyle); 14679 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14680 verifyFormat("class Foooooooooooooooooooooo\n" 14681 ": public aaaaaaaaaaaaaaaaaa,\n" 14682 " public bbbbbbbbbbbbbbbbbb {}", 14683 InheritanceStyle); 14684 14685 FormatStyle ForLoopStyle = getLLVMStyle(); 14686 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14687 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14688 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14689 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14690 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14691 verifyFormat("{\n" 14692 "label2:\n" 14693 " int x = 0;\n" 14694 "}", 14695 ForLoopStyle); 14696 verifyFormat("switch (x) {\n" 14697 "case 1:\n" 14698 "default:\n" 14699 "}", 14700 ForLoopStyle); 14701 verifyFormat("switch (allBraces) {\n" 14702 "case 1: {\n" 14703 " break;\n" 14704 "}\n" 14705 "case 2: {\n" 14706 " [[fallthrough]];\n" 14707 "}\n" 14708 "default: {\n" 14709 " break;\n" 14710 "}\n" 14711 "}", 14712 ForLoopStyle); 14713 14714 FormatStyle CaseStyle = getLLVMStyle(); 14715 CaseStyle.SpaceBeforeCaseColon = true; 14716 verifyFormat("class Foo : public Bar {};", CaseStyle); 14717 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14718 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14719 verifyFormat("int x = a ? b : c;", CaseStyle); 14720 verifyFormat("switch (x) {\n" 14721 "case 1 :\n" 14722 "default :\n" 14723 "}", 14724 CaseStyle); 14725 verifyFormat("switch (allBraces) {\n" 14726 "case 1 : {\n" 14727 " break;\n" 14728 "}\n" 14729 "case 2 : {\n" 14730 " [[fallthrough]];\n" 14731 "}\n" 14732 "default : {\n" 14733 " break;\n" 14734 "}\n" 14735 "}", 14736 CaseStyle); 14737 14738 FormatStyle NoSpaceStyle = getLLVMStyle(); 14739 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14740 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14741 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14742 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14743 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14744 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14745 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14746 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14747 verifyFormat("{\n" 14748 "label3:\n" 14749 " int x = 0;\n" 14750 "}", 14751 NoSpaceStyle); 14752 verifyFormat("switch (x) {\n" 14753 "case 1:\n" 14754 "default:\n" 14755 "}", 14756 NoSpaceStyle); 14757 verifyFormat("switch (allBraces) {\n" 14758 "case 1: {\n" 14759 " break;\n" 14760 "}\n" 14761 "case 2: {\n" 14762 " [[fallthrough]];\n" 14763 "}\n" 14764 "default: {\n" 14765 " break;\n" 14766 "}\n" 14767 "}", 14768 NoSpaceStyle); 14769 14770 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14771 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14772 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14773 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14774 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14775 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14776 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14777 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14778 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14779 verifyFormat("{\n" 14780 "label3:\n" 14781 " int x = 0;\n" 14782 "}", 14783 InvertedSpaceStyle); 14784 verifyFormat("switch (x) {\n" 14785 "case 1 :\n" 14786 "case 2 : {\n" 14787 " break;\n" 14788 "}\n" 14789 "default :\n" 14790 " break;\n" 14791 "}", 14792 InvertedSpaceStyle); 14793 verifyFormat("switch (allBraces) {\n" 14794 "case 1 : {\n" 14795 " break;\n" 14796 "}\n" 14797 "case 2 : {\n" 14798 " [[fallthrough]];\n" 14799 "}\n" 14800 "default : {\n" 14801 " break;\n" 14802 "}\n" 14803 "}", 14804 InvertedSpaceStyle); 14805 } 14806 14807 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 14808 FormatStyle Style = getLLVMStyle(); 14809 14810 Style.PointerAlignment = FormatStyle::PAS_Left; 14811 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14812 verifyFormat("void* const* x = NULL;", Style); 14813 14814 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 14815 do { \ 14816 Style.PointerAlignment = FormatStyle::Pointers; \ 14817 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 14818 verifyFormat(Code, Style); \ 14819 } while (false) 14820 14821 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 14822 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 14823 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 14824 14825 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 14826 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 14827 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 14828 14829 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 14830 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 14831 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 14832 14833 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 14834 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 14835 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 14836 14837 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 14838 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14839 SAPQ_Default); 14840 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14841 SAPQ_Default); 14842 14843 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 14844 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14845 SAPQ_Before); 14846 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14847 SAPQ_Before); 14848 14849 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 14850 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 14851 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14852 SAPQ_After); 14853 14854 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 14855 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 14856 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 14857 14858 #undef verifyQualifierSpaces 14859 14860 FormatStyle Spaces = getLLVMStyle(); 14861 Spaces.AttributeMacros.push_back("qualified"); 14862 Spaces.PointerAlignment = FormatStyle::PAS_Right; 14863 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14864 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 14865 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 14866 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 14867 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 14868 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14869 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14870 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 14871 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 14872 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14873 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14874 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14875 14876 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 14877 Spaces.PointerAlignment = FormatStyle::PAS_Left; 14878 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14879 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 14880 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 14881 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 14882 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 14883 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14884 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 14885 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14886 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 14887 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 14888 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 14889 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 14890 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14891 14892 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 14893 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 14894 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14895 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 14896 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 14897 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14898 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14899 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14900 } 14901 14902 TEST_F(FormatTest, AlignConsecutiveMacros) { 14903 FormatStyle Style = getLLVMStyle(); 14904 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14905 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14906 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14907 14908 verifyFormat("#define a 3\n" 14909 "#define bbbb 4\n" 14910 "#define ccc (5)", 14911 Style); 14912 14913 verifyFormat("#define f(x) (x * x)\n" 14914 "#define fff(x, y, z) (x * y + z)\n" 14915 "#define ffff(x, y) (x - y)", 14916 Style); 14917 14918 verifyFormat("#define foo(x, y) (x + y)\n" 14919 "#define bar (5, 6)(2 + 2)", 14920 Style); 14921 14922 verifyFormat("#define a 3\n" 14923 "#define bbbb 4\n" 14924 "#define ccc (5)\n" 14925 "#define f(x) (x * x)\n" 14926 "#define fff(x, y, z) (x * y + z)\n" 14927 "#define ffff(x, y) (x - y)", 14928 Style); 14929 14930 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14931 verifyFormat("#define a 3\n" 14932 "#define bbbb 4\n" 14933 "#define ccc (5)", 14934 Style); 14935 14936 verifyFormat("#define f(x) (x * x)\n" 14937 "#define fff(x, y, z) (x * y + z)\n" 14938 "#define ffff(x, y) (x - y)", 14939 Style); 14940 14941 verifyFormat("#define foo(x, y) (x + y)\n" 14942 "#define bar (5, 6)(2 + 2)", 14943 Style); 14944 14945 verifyFormat("#define a 3\n" 14946 "#define bbbb 4\n" 14947 "#define ccc (5)\n" 14948 "#define f(x) (x * x)\n" 14949 "#define fff(x, y, z) (x * y + z)\n" 14950 "#define ffff(x, y) (x - y)", 14951 Style); 14952 14953 verifyFormat("#define a 5\n" 14954 "#define foo(x, y) (x + y)\n" 14955 "#define CCC (6)\n" 14956 "auto lambda = []() {\n" 14957 " auto ii = 0;\n" 14958 " float j = 0;\n" 14959 " return 0;\n" 14960 "};\n" 14961 "int i = 0;\n" 14962 "float i2 = 0;\n" 14963 "auto v = type{\n" 14964 " i = 1, //\n" 14965 " (i = 2), //\n" 14966 " i = 3 //\n" 14967 "};", 14968 Style); 14969 14970 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14971 Style.ColumnLimit = 20; 14972 14973 verifyFormat("#define a \\\n" 14974 " \"aabbbbbbbbbbbb\"\n" 14975 "#define D \\\n" 14976 " \"aabbbbbbbbbbbb\" \\\n" 14977 " \"ccddeeeeeeeee\"\n" 14978 "#define B \\\n" 14979 " \"QQQQQQQQQQQQQ\" \\\n" 14980 " \"FFFFFFFFFFFFF\" \\\n" 14981 " \"LLLLLLLL\"\n", 14982 Style); 14983 14984 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14985 verifyFormat("#define a \\\n" 14986 " \"aabbbbbbbbbbbb\"\n" 14987 "#define D \\\n" 14988 " \"aabbbbbbbbbbbb\" \\\n" 14989 " \"ccddeeeeeeeee\"\n" 14990 "#define B \\\n" 14991 " \"QQQQQQQQQQQQQ\" \\\n" 14992 " \"FFFFFFFFFFFFF\" \\\n" 14993 " \"LLLLLLLL\"\n", 14994 Style); 14995 14996 // Test across comments 14997 Style.MaxEmptyLinesToKeep = 10; 14998 Style.ReflowComments = false; 14999 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 15000 EXPECT_EQ("#define a 3\n" 15001 "// line comment\n" 15002 "#define bbbb 4\n" 15003 "#define ccc (5)", 15004 format("#define a 3\n" 15005 "// line comment\n" 15006 "#define bbbb 4\n" 15007 "#define ccc (5)", 15008 Style)); 15009 15010 EXPECT_EQ("#define a 3\n" 15011 "/* block comment */\n" 15012 "#define bbbb 4\n" 15013 "#define ccc (5)", 15014 format("#define a 3\n" 15015 "/* block comment */\n" 15016 "#define bbbb 4\n" 15017 "#define ccc (5)", 15018 Style)); 15019 15020 EXPECT_EQ("#define a 3\n" 15021 "/* multi-line *\n" 15022 " * block comment */\n" 15023 "#define bbbb 4\n" 15024 "#define ccc (5)", 15025 format("#define a 3\n" 15026 "/* multi-line *\n" 15027 " * block comment */\n" 15028 "#define bbbb 4\n" 15029 "#define ccc (5)", 15030 Style)); 15031 15032 EXPECT_EQ("#define a 3\n" 15033 "// multi-line line comment\n" 15034 "//\n" 15035 "#define bbbb 4\n" 15036 "#define ccc (5)", 15037 format("#define a 3\n" 15038 "// multi-line line comment\n" 15039 "//\n" 15040 "#define bbbb 4\n" 15041 "#define ccc (5)", 15042 Style)); 15043 15044 EXPECT_EQ("#define a 3\n" 15045 "// empty lines still break.\n" 15046 "\n" 15047 "#define bbbb 4\n" 15048 "#define ccc (5)", 15049 format("#define a 3\n" 15050 "// empty lines still break.\n" 15051 "\n" 15052 "#define bbbb 4\n" 15053 "#define ccc (5)", 15054 Style)); 15055 15056 // Test across empty lines 15057 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 15058 EXPECT_EQ("#define a 3\n" 15059 "\n" 15060 "#define bbbb 4\n" 15061 "#define ccc (5)", 15062 format("#define a 3\n" 15063 "\n" 15064 "#define bbbb 4\n" 15065 "#define ccc (5)", 15066 Style)); 15067 15068 EXPECT_EQ("#define a 3\n" 15069 "\n" 15070 "\n" 15071 "\n" 15072 "#define bbbb 4\n" 15073 "#define ccc (5)", 15074 format("#define a 3\n" 15075 "\n" 15076 "\n" 15077 "\n" 15078 "#define bbbb 4\n" 15079 "#define ccc (5)", 15080 Style)); 15081 15082 EXPECT_EQ("#define a 3\n" 15083 "// comments should break alignment\n" 15084 "//\n" 15085 "#define bbbb 4\n" 15086 "#define ccc (5)", 15087 format("#define a 3\n" 15088 "// comments should break alignment\n" 15089 "//\n" 15090 "#define bbbb 4\n" 15091 "#define ccc (5)", 15092 Style)); 15093 15094 // Test across empty lines and comments 15095 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 15096 verifyFormat("#define a 3\n" 15097 "\n" 15098 "// line comment\n" 15099 "#define bbbb 4\n" 15100 "#define ccc (5)", 15101 Style); 15102 15103 EXPECT_EQ("#define a 3\n" 15104 "\n" 15105 "\n" 15106 "/* multi-line *\n" 15107 " * block comment */\n" 15108 "\n" 15109 "\n" 15110 "#define bbbb 4\n" 15111 "#define ccc (5)", 15112 format("#define a 3\n" 15113 "\n" 15114 "\n" 15115 "/* multi-line *\n" 15116 " * block comment */\n" 15117 "\n" 15118 "\n" 15119 "#define bbbb 4\n" 15120 "#define ccc (5)", 15121 Style)); 15122 15123 EXPECT_EQ("#define a 3\n" 15124 "\n" 15125 "\n" 15126 "/* multi-line *\n" 15127 " * block comment */\n" 15128 "\n" 15129 "\n" 15130 "#define bbbb 4\n" 15131 "#define ccc (5)", 15132 format("#define a 3\n" 15133 "\n" 15134 "\n" 15135 "/* multi-line *\n" 15136 " * block comment */\n" 15137 "\n" 15138 "\n" 15139 "#define bbbb 4\n" 15140 "#define ccc (5)", 15141 Style)); 15142 } 15143 15144 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 15145 FormatStyle Alignment = getLLVMStyle(); 15146 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15147 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 15148 15149 Alignment.MaxEmptyLinesToKeep = 10; 15150 /* Test alignment across empty lines */ 15151 EXPECT_EQ("int a = 5;\n" 15152 "\n" 15153 "int oneTwoThree = 123;", 15154 format("int a = 5;\n" 15155 "\n" 15156 "int oneTwoThree= 123;", 15157 Alignment)); 15158 EXPECT_EQ("int a = 5;\n" 15159 "int one = 1;\n" 15160 "\n" 15161 "int oneTwoThree = 123;", 15162 format("int a = 5;\n" 15163 "int one = 1;\n" 15164 "\n" 15165 "int oneTwoThree = 123;", 15166 Alignment)); 15167 EXPECT_EQ("int a = 5;\n" 15168 "int one = 1;\n" 15169 "\n" 15170 "int oneTwoThree = 123;\n" 15171 "int oneTwo = 12;", 15172 format("int a = 5;\n" 15173 "int one = 1;\n" 15174 "\n" 15175 "int oneTwoThree = 123;\n" 15176 "int oneTwo = 12;", 15177 Alignment)); 15178 15179 /* Test across comments */ 15180 EXPECT_EQ("int a = 5;\n" 15181 "/* block comment */\n" 15182 "int oneTwoThree = 123;", 15183 format("int a = 5;\n" 15184 "/* block comment */\n" 15185 "int oneTwoThree=123;", 15186 Alignment)); 15187 15188 EXPECT_EQ("int a = 5;\n" 15189 "// line comment\n" 15190 "int oneTwoThree = 123;", 15191 format("int a = 5;\n" 15192 "// line comment\n" 15193 "int oneTwoThree=123;", 15194 Alignment)); 15195 15196 /* Test across comments and newlines */ 15197 EXPECT_EQ("int a = 5;\n" 15198 "\n" 15199 "/* block comment */\n" 15200 "int oneTwoThree = 123;", 15201 format("int a = 5;\n" 15202 "\n" 15203 "/* block comment */\n" 15204 "int oneTwoThree=123;", 15205 Alignment)); 15206 15207 EXPECT_EQ("int a = 5;\n" 15208 "\n" 15209 "// line comment\n" 15210 "int oneTwoThree = 123;", 15211 format("int a = 5;\n" 15212 "\n" 15213 "// line comment\n" 15214 "int oneTwoThree=123;", 15215 Alignment)); 15216 } 15217 15218 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15219 FormatStyle Alignment = getLLVMStyle(); 15220 Alignment.AlignConsecutiveDeclarations = 15221 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15222 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15223 15224 Alignment.MaxEmptyLinesToKeep = 10; 15225 /* Test alignment across empty lines */ 15226 EXPECT_EQ("int a = 5;\n" 15227 "\n" 15228 "float const oneTwoThree = 123;", 15229 format("int a = 5;\n" 15230 "\n" 15231 "float const oneTwoThree = 123;", 15232 Alignment)); 15233 EXPECT_EQ("int a = 5;\n" 15234 "float const one = 1;\n" 15235 "\n" 15236 "int oneTwoThree = 123;", 15237 format("int a = 5;\n" 15238 "float const one = 1;\n" 15239 "\n" 15240 "int oneTwoThree = 123;", 15241 Alignment)); 15242 15243 /* Test across comments */ 15244 EXPECT_EQ("float const a = 5;\n" 15245 "/* block comment */\n" 15246 "int oneTwoThree = 123;", 15247 format("float const a = 5;\n" 15248 "/* block comment */\n" 15249 "int oneTwoThree=123;", 15250 Alignment)); 15251 15252 EXPECT_EQ("float const a = 5;\n" 15253 "// line comment\n" 15254 "int oneTwoThree = 123;", 15255 format("float const a = 5;\n" 15256 "// line comment\n" 15257 "int oneTwoThree=123;", 15258 Alignment)); 15259 15260 /* Test across comments and newlines */ 15261 EXPECT_EQ("float const a = 5;\n" 15262 "\n" 15263 "/* block comment */\n" 15264 "int oneTwoThree = 123;", 15265 format("float const a = 5;\n" 15266 "\n" 15267 "/* block comment */\n" 15268 "int oneTwoThree=123;", 15269 Alignment)); 15270 15271 EXPECT_EQ("float const a = 5;\n" 15272 "\n" 15273 "// line comment\n" 15274 "int oneTwoThree = 123;", 15275 format("float const a = 5;\n" 15276 "\n" 15277 "// line comment\n" 15278 "int oneTwoThree=123;", 15279 Alignment)); 15280 } 15281 15282 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15283 FormatStyle Alignment = getLLVMStyle(); 15284 Alignment.AlignConsecutiveBitFields = 15285 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15286 15287 Alignment.MaxEmptyLinesToKeep = 10; 15288 /* Test alignment across empty lines */ 15289 EXPECT_EQ("int a : 5;\n" 15290 "\n" 15291 "int longbitfield : 6;", 15292 format("int a : 5;\n" 15293 "\n" 15294 "int longbitfield : 6;", 15295 Alignment)); 15296 EXPECT_EQ("int a : 5;\n" 15297 "int one : 1;\n" 15298 "\n" 15299 "int longbitfield : 6;", 15300 format("int a : 5;\n" 15301 "int one : 1;\n" 15302 "\n" 15303 "int longbitfield : 6;", 15304 Alignment)); 15305 15306 /* Test across comments */ 15307 EXPECT_EQ("int a : 5;\n" 15308 "/* block comment */\n" 15309 "int longbitfield : 6;", 15310 format("int a : 5;\n" 15311 "/* block comment */\n" 15312 "int longbitfield : 6;", 15313 Alignment)); 15314 EXPECT_EQ("int a : 5;\n" 15315 "int one : 1;\n" 15316 "// line comment\n" 15317 "int longbitfield : 6;", 15318 format("int a : 5;\n" 15319 "int one : 1;\n" 15320 "// line comment\n" 15321 "int longbitfield : 6;", 15322 Alignment)); 15323 15324 /* Test across comments and newlines */ 15325 EXPECT_EQ("int a : 5;\n" 15326 "/* block comment */\n" 15327 "\n" 15328 "int longbitfield : 6;", 15329 format("int a : 5;\n" 15330 "/* block comment */\n" 15331 "\n" 15332 "int longbitfield : 6;", 15333 Alignment)); 15334 EXPECT_EQ("int a : 5;\n" 15335 "int one : 1;\n" 15336 "\n" 15337 "// line comment\n" 15338 "\n" 15339 "int longbitfield : 6;", 15340 format("int a : 5;\n" 15341 "int one : 1;\n" 15342 "\n" 15343 "// line comment \n" 15344 "\n" 15345 "int longbitfield : 6;", 15346 Alignment)); 15347 } 15348 15349 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15350 FormatStyle Alignment = getLLVMStyle(); 15351 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15352 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15353 15354 Alignment.MaxEmptyLinesToKeep = 10; 15355 /* Test alignment across empty lines */ 15356 EXPECT_EQ("int a = 5;\n" 15357 "\n" 15358 "int oneTwoThree = 123;", 15359 format("int a = 5;\n" 15360 "\n" 15361 "int oneTwoThree= 123;", 15362 Alignment)); 15363 EXPECT_EQ("int a = 5;\n" 15364 "int one = 1;\n" 15365 "\n" 15366 "int oneTwoThree = 123;", 15367 format("int a = 5;\n" 15368 "int one = 1;\n" 15369 "\n" 15370 "int oneTwoThree = 123;", 15371 Alignment)); 15372 15373 /* Test across comments */ 15374 EXPECT_EQ("int a = 5;\n" 15375 "/* block comment */\n" 15376 "int oneTwoThree = 123;", 15377 format("int a = 5;\n" 15378 "/* block comment */\n" 15379 "int oneTwoThree=123;", 15380 Alignment)); 15381 15382 EXPECT_EQ("int a = 5;\n" 15383 "// line comment\n" 15384 "int oneTwoThree = 123;", 15385 format("int a = 5;\n" 15386 "// line comment\n" 15387 "int oneTwoThree=123;", 15388 Alignment)); 15389 15390 EXPECT_EQ("int a = 5;\n" 15391 "/*\n" 15392 " * multi-line block comment\n" 15393 " */\n" 15394 "int oneTwoThree = 123;", 15395 format("int a = 5;\n" 15396 "/*\n" 15397 " * multi-line block comment\n" 15398 " */\n" 15399 "int oneTwoThree=123;", 15400 Alignment)); 15401 15402 EXPECT_EQ("int a = 5;\n" 15403 "//\n" 15404 "// multi-line line comment\n" 15405 "//\n" 15406 "int oneTwoThree = 123;", 15407 format("int a = 5;\n" 15408 "//\n" 15409 "// multi-line line comment\n" 15410 "//\n" 15411 "int oneTwoThree=123;", 15412 Alignment)); 15413 15414 /* Test across comments and newlines */ 15415 EXPECT_EQ("int a = 5;\n" 15416 "\n" 15417 "/* block comment */\n" 15418 "int oneTwoThree = 123;", 15419 format("int a = 5;\n" 15420 "\n" 15421 "/* block comment */\n" 15422 "int oneTwoThree=123;", 15423 Alignment)); 15424 15425 EXPECT_EQ("int a = 5;\n" 15426 "\n" 15427 "// line comment\n" 15428 "int oneTwoThree = 123;", 15429 format("int a = 5;\n" 15430 "\n" 15431 "// line comment\n" 15432 "int oneTwoThree=123;", 15433 Alignment)); 15434 } 15435 15436 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15437 FormatStyle Alignment = getLLVMStyle(); 15438 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15439 Alignment.AlignConsecutiveAssignments = 15440 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15441 verifyFormat("int a = 5;\n" 15442 "int oneTwoThree = 123;", 15443 Alignment); 15444 verifyFormat("int a = method();\n" 15445 "int oneTwoThree = 133;", 15446 Alignment); 15447 verifyFormat("a &= 5;\n" 15448 "bcd *= 5;\n" 15449 "ghtyf += 5;\n" 15450 "dvfvdb -= 5;\n" 15451 "a /= 5;\n" 15452 "vdsvsv %= 5;\n" 15453 "sfdbddfbdfbb ^= 5;\n" 15454 "dvsdsv |= 5;\n" 15455 "int dsvvdvsdvvv = 123;", 15456 Alignment); 15457 verifyFormat("int i = 1, j = 10;\n" 15458 "something = 2000;", 15459 Alignment); 15460 verifyFormat("something = 2000;\n" 15461 "int i = 1, j = 10;\n", 15462 Alignment); 15463 verifyFormat("something = 2000;\n" 15464 "another = 911;\n" 15465 "int i = 1, j = 10;\n" 15466 "oneMore = 1;\n" 15467 "i = 2;", 15468 Alignment); 15469 verifyFormat("int a = 5;\n" 15470 "int one = 1;\n" 15471 "method();\n" 15472 "int oneTwoThree = 123;\n" 15473 "int oneTwo = 12;", 15474 Alignment); 15475 verifyFormat("int oneTwoThree = 123;\n" 15476 "int oneTwo = 12;\n" 15477 "method();\n", 15478 Alignment); 15479 verifyFormat("int oneTwoThree = 123; // comment\n" 15480 "int oneTwo = 12; // comment", 15481 Alignment); 15482 15483 // Bug 25167 15484 /* Uncomment when fixed 15485 verifyFormat("#if A\n" 15486 "#else\n" 15487 "int aaaaaaaa = 12;\n" 15488 "#endif\n" 15489 "#if B\n" 15490 "#else\n" 15491 "int a = 12;\n" 15492 "#endif\n", 15493 Alignment); 15494 verifyFormat("enum foo {\n" 15495 "#if A\n" 15496 "#else\n" 15497 " aaaaaaaa = 12;\n" 15498 "#endif\n" 15499 "#if B\n" 15500 "#else\n" 15501 " a = 12;\n" 15502 "#endif\n" 15503 "};\n", 15504 Alignment); 15505 */ 15506 15507 Alignment.MaxEmptyLinesToKeep = 10; 15508 /* Test alignment across empty lines */ 15509 EXPECT_EQ("int a = 5;\n" 15510 "\n" 15511 "int oneTwoThree = 123;", 15512 format("int a = 5;\n" 15513 "\n" 15514 "int oneTwoThree= 123;", 15515 Alignment)); 15516 EXPECT_EQ("int a = 5;\n" 15517 "int one = 1;\n" 15518 "\n" 15519 "int oneTwoThree = 123;", 15520 format("int a = 5;\n" 15521 "int one = 1;\n" 15522 "\n" 15523 "int oneTwoThree = 123;", 15524 Alignment)); 15525 EXPECT_EQ("int a = 5;\n" 15526 "int one = 1;\n" 15527 "\n" 15528 "int oneTwoThree = 123;\n" 15529 "int oneTwo = 12;", 15530 format("int a = 5;\n" 15531 "int one = 1;\n" 15532 "\n" 15533 "int oneTwoThree = 123;\n" 15534 "int oneTwo = 12;", 15535 Alignment)); 15536 15537 /* Test across comments */ 15538 EXPECT_EQ("int a = 5;\n" 15539 "/* block comment */\n" 15540 "int oneTwoThree = 123;", 15541 format("int a = 5;\n" 15542 "/* block comment */\n" 15543 "int oneTwoThree=123;", 15544 Alignment)); 15545 15546 EXPECT_EQ("int a = 5;\n" 15547 "// line comment\n" 15548 "int oneTwoThree = 123;", 15549 format("int a = 5;\n" 15550 "// line comment\n" 15551 "int oneTwoThree=123;", 15552 Alignment)); 15553 15554 /* Test across comments and newlines */ 15555 EXPECT_EQ("int a = 5;\n" 15556 "\n" 15557 "/* block comment */\n" 15558 "int oneTwoThree = 123;", 15559 format("int a = 5;\n" 15560 "\n" 15561 "/* block comment */\n" 15562 "int oneTwoThree=123;", 15563 Alignment)); 15564 15565 EXPECT_EQ("int a = 5;\n" 15566 "\n" 15567 "// line comment\n" 15568 "int oneTwoThree = 123;", 15569 format("int a = 5;\n" 15570 "\n" 15571 "// line comment\n" 15572 "int oneTwoThree=123;", 15573 Alignment)); 15574 15575 EXPECT_EQ("int a = 5;\n" 15576 "//\n" 15577 "// multi-line line comment\n" 15578 "//\n" 15579 "int oneTwoThree = 123;", 15580 format("int a = 5;\n" 15581 "//\n" 15582 "// multi-line line comment\n" 15583 "//\n" 15584 "int oneTwoThree=123;", 15585 Alignment)); 15586 15587 EXPECT_EQ("int a = 5;\n" 15588 "/*\n" 15589 " * multi-line block comment\n" 15590 " */\n" 15591 "int oneTwoThree = 123;", 15592 format("int a = 5;\n" 15593 "/*\n" 15594 " * multi-line block comment\n" 15595 " */\n" 15596 "int oneTwoThree=123;", 15597 Alignment)); 15598 15599 EXPECT_EQ("int a = 5;\n" 15600 "\n" 15601 "/* block comment */\n" 15602 "\n" 15603 "\n" 15604 "\n" 15605 "int oneTwoThree = 123;", 15606 format("int a = 5;\n" 15607 "\n" 15608 "/* block comment */\n" 15609 "\n" 15610 "\n" 15611 "\n" 15612 "int oneTwoThree=123;", 15613 Alignment)); 15614 15615 EXPECT_EQ("int a = 5;\n" 15616 "\n" 15617 "// line comment\n" 15618 "\n" 15619 "\n" 15620 "\n" 15621 "int oneTwoThree = 123;", 15622 format("int a = 5;\n" 15623 "\n" 15624 "// line comment\n" 15625 "\n" 15626 "\n" 15627 "\n" 15628 "int oneTwoThree=123;", 15629 Alignment)); 15630 15631 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15632 verifyFormat("#define A \\\n" 15633 " int aaaa = 12; \\\n" 15634 " int b = 23; \\\n" 15635 " int ccc = 234; \\\n" 15636 " int dddddddddd = 2345;", 15637 Alignment); 15638 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15639 verifyFormat("#define A \\\n" 15640 " int aaaa = 12; \\\n" 15641 " int b = 23; \\\n" 15642 " int ccc = 234; \\\n" 15643 " int dddddddddd = 2345;", 15644 Alignment); 15645 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15646 verifyFormat("#define A " 15647 " \\\n" 15648 " int aaaa = 12; " 15649 " \\\n" 15650 " int b = 23; " 15651 " \\\n" 15652 " int ccc = 234; " 15653 " \\\n" 15654 " int dddddddddd = 2345;", 15655 Alignment); 15656 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15657 "k = 4, int l = 5,\n" 15658 " int m = 6) {\n" 15659 " int j = 10;\n" 15660 " otherThing = 1;\n" 15661 "}", 15662 Alignment); 15663 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15664 " int i = 1;\n" 15665 " int j = 2;\n" 15666 " int big = 10000;\n" 15667 "}", 15668 Alignment); 15669 verifyFormat("class C {\n" 15670 "public:\n" 15671 " int i = 1;\n" 15672 " virtual void f() = 0;\n" 15673 "};", 15674 Alignment); 15675 verifyFormat("int i = 1;\n" 15676 "if (SomeType t = getSomething()) {\n" 15677 "}\n" 15678 "int j = 2;\n" 15679 "int big = 10000;", 15680 Alignment); 15681 verifyFormat("int j = 7;\n" 15682 "for (int k = 0; k < N; ++k) {\n" 15683 "}\n" 15684 "int j = 2;\n" 15685 "int big = 10000;\n" 15686 "}", 15687 Alignment); 15688 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15689 verifyFormat("int i = 1;\n" 15690 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15691 " = someLooooooooooooooooongFunction();\n" 15692 "int j = 2;", 15693 Alignment); 15694 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15695 verifyFormat("int i = 1;\n" 15696 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15697 " someLooooooooooooooooongFunction();\n" 15698 "int j = 2;", 15699 Alignment); 15700 15701 verifyFormat("auto lambda = []() {\n" 15702 " auto i = 0;\n" 15703 " return 0;\n" 15704 "};\n" 15705 "int i = 0;\n" 15706 "auto v = type{\n" 15707 " i = 1, //\n" 15708 " (i = 2), //\n" 15709 " i = 3 //\n" 15710 "};", 15711 Alignment); 15712 15713 verifyFormat( 15714 "int i = 1;\n" 15715 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15716 " loooooooooooooooooooooongParameterB);\n" 15717 "int j = 2;", 15718 Alignment); 15719 15720 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15721 " typename B = very_long_type_name_1,\n" 15722 " typename T_2 = very_long_type_name_2>\n" 15723 "auto foo() {}\n", 15724 Alignment); 15725 verifyFormat("int a, b = 1;\n" 15726 "int c = 2;\n" 15727 "int dd = 3;\n", 15728 Alignment); 15729 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15730 "float b[1][] = {{3.f}};\n", 15731 Alignment); 15732 verifyFormat("for (int i = 0; i < 1; i++)\n" 15733 " int x = 1;\n", 15734 Alignment); 15735 verifyFormat("for (i = 0; i < 1; i++)\n" 15736 " x = 1;\n" 15737 "y = 1;\n", 15738 Alignment); 15739 15740 Alignment.ReflowComments = true; 15741 Alignment.ColumnLimit = 50; 15742 EXPECT_EQ("int x = 0;\n" 15743 "int yy = 1; /// specificlennospace\n" 15744 "int zzz = 2;\n", 15745 format("int x = 0;\n" 15746 "int yy = 1; ///specificlennospace\n" 15747 "int zzz = 2;\n", 15748 Alignment)); 15749 } 15750 15751 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15752 FormatStyle Alignment = getLLVMStyle(); 15753 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15754 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15755 verifyFormat("int a = 5;\n" 15756 "int oneTwoThree = 123;", 15757 Alignment); 15758 verifyFormat("int a = 5;\n" 15759 "int oneTwoThree = 123;", 15760 Alignment); 15761 15762 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15763 verifyFormat("int a = 5;\n" 15764 "int oneTwoThree = 123;", 15765 Alignment); 15766 verifyFormat("int a = method();\n" 15767 "int oneTwoThree = 133;", 15768 Alignment); 15769 verifyFormat("a &= 5;\n" 15770 "bcd *= 5;\n" 15771 "ghtyf += 5;\n" 15772 "dvfvdb -= 5;\n" 15773 "a /= 5;\n" 15774 "vdsvsv %= 5;\n" 15775 "sfdbddfbdfbb ^= 5;\n" 15776 "dvsdsv |= 5;\n" 15777 "int dsvvdvsdvvv = 123;", 15778 Alignment); 15779 verifyFormat("int i = 1, j = 10;\n" 15780 "something = 2000;", 15781 Alignment); 15782 verifyFormat("something = 2000;\n" 15783 "int i = 1, j = 10;\n", 15784 Alignment); 15785 verifyFormat("something = 2000;\n" 15786 "another = 911;\n" 15787 "int i = 1, j = 10;\n" 15788 "oneMore = 1;\n" 15789 "i = 2;", 15790 Alignment); 15791 verifyFormat("int a = 5;\n" 15792 "int one = 1;\n" 15793 "method();\n" 15794 "int oneTwoThree = 123;\n" 15795 "int oneTwo = 12;", 15796 Alignment); 15797 verifyFormat("int oneTwoThree = 123;\n" 15798 "int oneTwo = 12;\n" 15799 "method();\n", 15800 Alignment); 15801 verifyFormat("int oneTwoThree = 123; // comment\n" 15802 "int oneTwo = 12; // comment", 15803 Alignment); 15804 15805 // Bug 25167 15806 /* Uncomment when fixed 15807 verifyFormat("#if A\n" 15808 "#else\n" 15809 "int aaaaaaaa = 12;\n" 15810 "#endif\n" 15811 "#if B\n" 15812 "#else\n" 15813 "int a = 12;\n" 15814 "#endif\n", 15815 Alignment); 15816 verifyFormat("enum foo {\n" 15817 "#if A\n" 15818 "#else\n" 15819 " aaaaaaaa = 12;\n" 15820 "#endif\n" 15821 "#if B\n" 15822 "#else\n" 15823 " a = 12;\n" 15824 "#endif\n" 15825 "};\n", 15826 Alignment); 15827 */ 15828 15829 EXPECT_EQ("int a = 5;\n" 15830 "\n" 15831 "int oneTwoThree = 123;", 15832 format("int a = 5;\n" 15833 "\n" 15834 "int oneTwoThree= 123;", 15835 Alignment)); 15836 EXPECT_EQ("int a = 5;\n" 15837 "int one = 1;\n" 15838 "\n" 15839 "int oneTwoThree = 123;", 15840 format("int a = 5;\n" 15841 "int one = 1;\n" 15842 "\n" 15843 "int oneTwoThree = 123;", 15844 Alignment)); 15845 EXPECT_EQ("int a = 5;\n" 15846 "int one = 1;\n" 15847 "\n" 15848 "int oneTwoThree = 123;\n" 15849 "int oneTwo = 12;", 15850 format("int a = 5;\n" 15851 "int one = 1;\n" 15852 "\n" 15853 "int oneTwoThree = 123;\n" 15854 "int oneTwo = 12;", 15855 Alignment)); 15856 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15857 verifyFormat("#define A \\\n" 15858 " int aaaa = 12; \\\n" 15859 " int b = 23; \\\n" 15860 " int ccc = 234; \\\n" 15861 " int dddddddddd = 2345;", 15862 Alignment); 15863 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15864 verifyFormat("#define A \\\n" 15865 " int aaaa = 12; \\\n" 15866 " int b = 23; \\\n" 15867 " int ccc = 234; \\\n" 15868 " int dddddddddd = 2345;", 15869 Alignment); 15870 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15871 verifyFormat("#define A " 15872 " \\\n" 15873 " int aaaa = 12; " 15874 " \\\n" 15875 " int b = 23; " 15876 " \\\n" 15877 " int ccc = 234; " 15878 " \\\n" 15879 " int dddddddddd = 2345;", 15880 Alignment); 15881 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15882 "k = 4, int l = 5,\n" 15883 " int m = 6) {\n" 15884 " int j = 10;\n" 15885 " otherThing = 1;\n" 15886 "}", 15887 Alignment); 15888 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15889 " int i = 1;\n" 15890 " int j = 2;\n" 15891 " int big = 10000;\n" 15892 "}", 15893 Alignment); 15894 verifyFormat("class C {\n" 15895 "public:\n" 15896 " int i = 1;\n" 15897 " virtual void f() = 0;\n" 15898 "};", 15899 Alignment); 15900 verifyFormat("int i = 1;\n" 15901 "if (SomeType t = getSomething()) {\n" 15902 "}\n" 15903 "int j = 2;\n" 15904 "int big = 10000;", 15905 Alignment); 15906 verifyFormat("int j = 7;\n" 15907 "for (int k = 0; k < N; ++k) {\n" 15908 "}\n" 15909 "int j = 2;\n" 15910 "int big = 10000;\n" 15911 "}", 15912 Alignment); 15913 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15914 verifyFormat("int i = 1;\n" 15915 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15916 " = someLooooooooooooooooongFunction();\n" 15917 "int j = 2;", 15918 Alignment); 15919 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15920 verifyFormat("int i = 1;\n" 15921 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15922 " someLooooooooooooooooongFunction();\n" 15923 "int j = 2;", 15924 Alignment); 15925 15926 verifyFormat("auto lambda = []() {\n" 15927 " auto i = 0;\n" 15928 " return 0;\n" 15929 "};\n" 15930 "int i = 0;\n" 15931 "auto v = type{\n" 15932 " i = 1, //\n" 15933 " (i = 2), //\n" 15934 " i = 3 //\n" 15935 "};", 15936 Alignment); 15937 15938 verifyFormat( 15939 "int i = 1;\n" 15940 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15941 " loooooooooooooooooooooongParameterB);\n" 15942 "int j = 2;", 15943 Alignment); 15944 15945 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15946 " typename B = very_long_type_name_1,\n" 15947 " typename T_2 = very_long_type_name_2>\n" 15948 "auto foo() {}\n", 15949 Alignment); 15950 verifyFormat("int a, b = 1;\n" 15951 "int c = 2;\n" 15952 "int dd = 3;\n", 15953 Alignment); 15954 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15955 "float b[1][] = {{3.f}};\n", 15956 Alignment); 15957 verifyFormat("for (int i = 0; i < 1; i++)\n" 15958 " int x = 1;\n", 15959 Alignment); 15960 verifyFormat("for (i = 0; i < 1; i++)\n" 15961 " x = 1;\n" 15962 "y = 1;\n", 15963 Alignment); 15964 15965 Alignment.ReflowComments = true; 15966 Alignment.ColumnLimit = 50; 15967 EXPECT_EQ("int x = 0;\n" 15968 "int yy = 1; /// specificlennospace\n" 15969 "int zzz = 2;\n", 15970 format("int x = 0;\n" 15971 "int yy = 1; ///specificlennospace\n" 15972 "int zzz = 2;\n", 15973 Alignment)); 15974 } 15975 15976 TEST_F(FormatTest, AlignConsecutiveBitFields) { 15977 FormatStyle Alignment = getLLVMStyle(); 15978 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 15979 verifyFormat("int const a : 5;\n" 15980 "int oneTwoThree : 23;", 15981 Alignment); 15982 15983 // Initializers are allowed starting with c++2a 15984 verifyFormat("int const a : 5 = 1;\n" 15985 "int oneTwoThree : 23 = 0;", 15986 Alignment); 15987 15988 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15989 verifyFormat("int const a : 5;\n" 15990 "int oneTwoThree : 23;", 15991 Alignment); 15992 15993 verifyFormat("int const a : 5; // comment\n" 15994 "int oneTwoThree : 23; // comment", 15995 Alignment); 15996 15997 verifyFormat("int const a : 5 = 1;\n" 15998 "int oneTwoThree : 23 = 0;", 15999 Alignment); 16000 16001 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16002 verifyFormat("int const a : 5 = 1;\n" 16003 "int oneTwoThree : 23 = 0;", 16004 Alignment); 16005 verifyFormat("int const a : 5 = {1};\n" 16006 "int oneTwoThree : 23 = 0;", 16007 Alignment); 16008 16009 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 16010 verifyFormat("int const a :5;\n" 16011 "int oneTwoThree:23;", 16012 Alignment); 16013 16014 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 16015 verifyFormat("int const a :5;\n" 16016 "int oneTwoThree :23;", 16017 Alignment); 16018 16019 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 16020 verifyFormat("int const a : 5;\n" 16021 "int oneTwoThree: 23;", 16022 Alignment); 16023 16024 // Known limitations: ':' is only recognized as a bitfield colon when 16025 // followed by a number. 16026 /* 16027 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 16028 "int a : 5;", 16029 Alignment); 16030 */ 16031 } 16032 16033 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 16034 FormatStyle Alignment = getLLVMStyle(); 16035 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16036 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16037 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16038 verifyFormat("float const a = 5;\n" 16039 "int oneTwoThree = 123;", 16040 Alignment); 16041 verifyFormat("int a = 5;\n" 16042 "float const oneTwoThree = 123;", 16043 Alignment); 16044 16045 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16046 verifyFormat("float const a = 5;\n" 16047 "int oneTwoThree = 123;", 16048 Alignment); 16049 verifyFormat("int a = method();\n" 16050 "float const oneTwoThree = 133;", 16051 Alignment); 16052 verifyFormat("int i = 1, j = 10;\n" 16053 "something = 2000;", 16054 Alignment); 16055 verifyFormat("something = 2000;\n" 16056 "int i = 1, j = 10;\n", 16057 Alignment); 16058 verifyFormat("float something = 2000;\n" 16059 "double another = 911;\n" 16060 "int i = 1, j = 10;\n" 16061 "const int *oneMore = 1;\n" 16062 "unsigned i = 2;", 16063 Alignment); 16064 verifyFormat("float a = 5;\n" 16065 "int one = 1;\n" 16066 "method();\n" 16067 "const double oneTwoThree = 123;\n" 16068 "const unsigned int oneTwo = 12;", 16069 Alignment); 16070 verifyFormat("int oneTwoThree{0}; // comment\n" 16071 "unsigned oneTwo; // comment", 16072 Alignment); 16073 verifyFormat("unsigned int *a;\n" 16074 "int *b;\n" 16075 "unsigned int Const *c;\n" 16076 "unsigned int const *d;\n" 16077 "unsigned int Const &e;\n" 16078 "unsigned int const &f;", 16079 Alignment); 16080 verifyFormat("Const unsigned int *c;\n" 16081 "const unsigned int *d;\n" 16082 "Const unsigned int &e;\n" 16083 "const unsigned int &f;\n" 16084 "const unsigned g;\n" 16085 "Const unsigned h;", 16086 Alignment); 16087 EXPECT_EQ("float const a = 5;\n" 16088 "\n" 16089 "int oneTwoThree = 123;", 16090 format("float const a = 5;\n" 16091 "\n" 16092 "int oneTwoThree= 123;", 16093 Alignment)); 16094 EXPECT_EQ("float a = 5;\n" 16095 "int one = 1;\n" 16096 "\n" 16097 "unsigned oneTwoThree = 123;", 16098 format("float a = 5;\n" 16099 "int one = 1;\n" 16100 "\n" 16101 "unsigned oneTwoThree = 123;", 16102 Alignment)); 16103 EXPECT_EQ("float a = 5;\n" 16104 "int one = 1;\n" 16105 "\n" 16106 "unsigned oneTwoThree = 123;\n" 16107 "int oneTwo = 12;", 16108 format("float a = 5;\n" 16109 "int one = 1;\n" 16110 "\n" 16111 "unsigned oneTwoThree = 123;\n" 16112 "int oneTwo = 12;", 16113 Alignment)); 16114 // Function prototype alignment 16115 verifyFormat("int a();\n" 16116 "double b();", 16117 Alignment); 16118 verifyFormat("int a(int x);\n" 16119 "double b();", 16120 Alignment); 16121 unsigned OldColumnLimit = Alignment.ColumnLimit; 16122 // We need to set ColumnLimit to zero, in order to stress nested alignments, 16123 // otherwise the function parameters will be re-flowed onto a single line. 16124 Alignment.ColumnLimit = 0; 16125 EXPECT_EQ("int a(int x,\n" 16126 " float y);\n" 16127 "double b(int x,\n" 16128 " double y);", 16129 format("int a(int x,\n" 16130 " float y);\n" 16131 "double b(int x,\n" 16132 " double y);", 16133 Alignment)); 16134 // This ensures that function parameters of function declarations are 16135 // correctly indented when their owning functions are indented. 16136 // The failure case here is for 'double y' to not be indented enough. 16137 EXPECT_EQ("double a(int x);\n" 16138 "int b(int y,\n" 16139 " double z);", 16140 format("double a(int x);\n" 16141 "int b(int y,\n" 16142 " double z);", 16143 Alignment)); 16144 // Set ColumnLimit low so that we induce wrapping immediately after 16145 // the function name and opening paren. 16146 Alignment.ColumnLimit = 13; 16147 verifyFormat("int function(\n" 16148 " int x,\n" 16149 " bool y);", 16150 Alignment); 16151 Alignment.ColumnLimit = OldColumnLimit; 16152 // Ensure function pointers don't screw up recursive alignment 16153 verifyFormat("int a(int x, void (*fp)(int y));\n" 16154 "double b();", 16155 Alignment); 16156 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16157 // Ensure recursive alignment is broken by function braces, so that the 16158 // "a = 1" does not align with subsequent assignments inside the function 16159 // body. 16160 verifyFormat("int func(int a = 1) {\n" 16161 " int b = 2;\n" 16162 " int cc = 3;\n" 16163 "}", 16164 Alignment); 16165 verifyFormat("float something = 2000;\n" 16166 "double another = 911;\n" 16167 "int i = 1, j = 10;\n" 16168 "const int *oneMore = 1;\n" 16169 "unsigned i = 2;", 16170 Alignment); 16171 verifyFormat("int oneTwoThree = {0}; // comment\n" 16172 "unsigned oneTwo = 0; // comment", 16173 Alignment); 16174 // Make sure that scope is correctly tracked, in the absence of braces 16175 verifyFormat("for (int i = 0; i < n; i++)\n" 16176 " j = i;\n" 16177 "double x = 1;\n", 16178 Alignment); 16179 verifyFormat("if (int i = 0)\n" 16180 " j = i;\n" 16181 "double x = 1;\n", 16182 Alignment); 16183 // Ensure operator[] and operator() are comprehended 16184 verifyFormat("struct test {\n" 16185 " long long int foo();\n" 16186 " int operator[](int a);\n" 16187 " double bar();\n" 16188 "};\n", 16189 Alignment); 16190 verifyFormat("struct test {\n" 16191 " long long int foo();\n" 16192 " int operator()(int a);\n" 16193 " double bar();\n" 16194 "};\n", 16195 Alignment); 16196 16197 // PAS_Right 16198 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16199 " int const i = 1;\n" 16200 " int *j = 2;\n" 16201 " int big = 10000;\n" 16202 "\n" 16203 " unsigned oneTwoThree = 123;\n" 16204 " int oneTwo = 12;\n" 16205 " method();\n" 16206 " float k = 2;\n" 16207 " int ll = 10000;\n" 16208 "}", 16209 format("void SomeFunction(int parameter= 0) {\n" 16210 " int const i= 1;\n" 16211 " int *j=2;\n" 16212 " int big = 10000;\n" 16213 "\n" 16214 "unsigned oneTwoThree =123;\n" 16215 "int oneTwo = 12;\n" 16216 " method();\n" 16217 "float k= 2;\n" 16218 "int ll=10000;\n" 16219 "}", 16220 Alignment)); 16221 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16222 " int const i = 1;\n" 16223 " int **j = 2, ***k;\n" 16224 " int &k = i;\n" 16225 " int &&l = i + j;\n" 16226 " int big = 10000;\n" 16227 "\n" 16228 " unsigned oneTwoThree = 123;\n" 16229 " int oneTwo = 12;\n" 16230 " method();\n" 16231 " float k = 2;\n" 16232 " int ll = 10000;\n" 16233 "}", 16234 format("void SomeFunction(int parameter= 0) {\n" 16235 " int const i= 1;\n" 16236 " int **j=2,***k;\n" 16237 "int &k=i;\n" 16238 "int &&l=i+j;\n" 16239 " int big = 10000;\n" 16240 "\n" 16241 "unsigned oneTwoThree =123;\n" 16242 "int oneTwo = 12;\n" 16243 " method();\n" 16244 "float k= 2;\n" 16245 "int ll=10000;\n" 16246 "}", 16247 Alignment)); 16248 // variables are aligned at their name, pointers are at the right most 16249 // position 16250 verifyFormat("int *a;\n" 16251 "int **b;\n" 16252 "int ***c;\n" 16253 "int foobar;\n", 16254 Alignment); 16255 16256 // PAS_Left 16257 FormatStyle AlignmentLeft = Alignment; 16258 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16259 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16260 " int const i = 1;\n" 16261 " int* j = 2;\n" 16262 " int big = 10000;\n" 16263 "\n" 16264 " unsigned oneTwoThree = 123;\n" 16265 " int oneTwo = 12;\n" 16266 " method();\n" 16267 " float k = 2;\n" 16268 " int ll = 10000;\n" 16269 "}", 16270 format("void SomeFunction(int parameter= 0) {\n" 16271 " int const i= 1;\n" 16272 " int *j=2;\n" 16273 " int big = 10000;\n" 16274 "\n" 16275 "unsigned oneTwoThree =123;\n" 16276 "int oneTwo = 12;\n" 16277 " method();\n" 16278 "float k= 2;\n" 16279 "int ll=10000;\n" 16280 "}", 16281 AlignmentLeft)); 16282 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16283 " int const i = 1;\n" 16284 " int** j = 2;\n" 16285 " int& k = i;\n" 16286 " int&& l = i + j;\n" 16287 " int big = 10000;\n" 16288 "\n" 16289 " unsigned oneTwoThree = 123;\n" 16290 " int oneTwo = 12;\n" 16291 " method();\n" 16292 " float k = 2;\n" 16293 " int ll = 10000;\n" 16294 "}", 16295 format("void SomeFunction(int parameter= 0) {\n" 16296 " int const i= 1;\n" 16297 " int **j=2;\n" 16298 "int &k=i;\n" 16299 "int &&l=i+j;\n" 16300 " int big = 10000;\n" 16301 "\n" 16302 "unsigned oneTwoThree =123;\n" 16303 "int oneTwo = 12;\n" 16304 " method();\n" 16305 "float k= 2;\n" 16306 "int ll=10000;\n" 16307 "}", 16308 AlignmentLeft)); 16309 // variables are aligned at their name, pointers are at the left most position 16310 verifyFormat("int* a;\n" 16311 "int** b;\n" 16312 "int*** c;\n" 16313 "int foobar;\n", 16314 AlignmentLeft); 16315 16316 // PAS_Middle 16317 FormatStyle AlignmentMiddle = Alignment; 16318 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16319 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16320 " int const i = 1;\n" 16321 " int * j = 2;\n" 16322 " int big = 10000;\n" 16323 "\n" 16324 " unsigned oneTwoThree = 123;\n" 16325 " int oneTwo = 12;\n" 16326 " method();\n" 16327 " float k = 2;\n" 16328 " int ll = 10000;\n" 16329 "}", 16330 format("void SomeFunction(int parameter= 0) {\n" 16331 " int const i= 1;\n" 16332 " int *j=2;\n" 16333 " int big = 10000;\n" 16334 "\n" 16335 "unsigned oneTwoThree =123;\n" 16336 "int oneTwo = 12;\n" 16337 " method();\n" 16338 "float k= 2;\n" 16339 "int ll=10000;\n" 16340 "}", 16341 AlignmentMiddle)); 16342 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16343 " int const i = 1;\n" 16344 " int ** j = 2, ***k;\n" 16345 " int & k = i;\n" 16346 " int && l = i + j;\n" 16347 " int big = 10000;\n" 16348 "\n" 16349 " unsigned oneTwoThree = 123;\n" 16350 " int oneTwo = 12;\n" 16351 " method();\n" 16352 " float k = 2;\n" 16353 " int ll = 10000;\n" 16354 "}", 16355 format("void SomeFunction(int parameter= 0) {\n" 16356 " int const i= 1;\n" 16357 " int **j=2,***k;\n" 16358 "int &k=i;\n" 16359 "int &&l=i+j;\n" 16360 " int big = 10000;\n" 16361 "\n" 16362 "unsigned oneTwoThree =123;\n" 16363 "int oneTwo = 12;\n" 16364 " method();\n" 16365 "float k= 2;\n" 16366 "int ll=10000;\n" 16367 "}", 16368 AlignmentMiddle)); 16369 // variables are aligned at their name, pointers are in the middle 16370 verifyFormat("int * a;\n" 16371 "int * b;\n" 16372 "int *** c;\n" 16373 "int foobar;\n", 16374 AlignmentMiddle); 16375 16376 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16377 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16378 verifyFormat("#define A \\\n" 16379 " int aaaa = 12; \\\n" 16380 " float b = 23; \\\n" 16381 " const int ccc = 234; \\\n" 16382 " unsigned dddddddddd = 2345;", 16383 Alignment); 16384 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16385 verifyFormat("#define A \\\n" 16386 " int aaaa = 12; \\\n" 16387 " float b = 23; \\\n" 16388 " const int ccc = 234; \\\n" 16389 " unsigned dddddddddd = 2345;", 16390 Alignment); 16391 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16392 Alignment.ColumnLimit = 30; 16393 verifyFormat("#define A \\\n" 16394 " int aaaa = 12; \\\n" 16395 " float b = 23; \\\n" 16396 " const int ccc = 234; \\\n" 16397 " int dddddddddd = 2345;", 16398 Alignment); 16399 Alignment.ColumnLimit = 80; 16400 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16401 "k = 4, int l = 5,\n" 16402 " int m = 6) {\n" 16403 " const int j = 10;\n" 16404 " otherThing = 1;\n" 16405 "}", 16406 Alignment); 16407 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16408 " int const i = 1;\n" 16409 " int *j = 2;\n" 16410 " int big = 10000;\n" 16411 "}", 16412 Alignment); 16413 verifyFormat("class C {\n" 16414 "public:\n" 16415 " int i = 1;\n" 16416 " virtual void f() = 0;\n" 16417 "};", 16418 Alignment); 16419 verifyFormat("float i = 1;\n" 16420 "if (SomeType t = getSomething()) {\n" 16421 "}\n" 16422 "const unsigned j = 2;\n" 16423 "int big = 10000;", 16424 Alignment); 16425 verifyFormat("float j = 7;\n" 16426 "for (int k = 0; k < N; ++k) {\n" 16427 "}\n" 16428 "unsigned j = 2;\n" 16429 "int big = 10000;\n" 16430 "}", 16431 Alignment); 16432 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16433 verifyFormat("float i = 1;\n" 16434 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16435 " = someLooooooooooooooooongFunction();\n" 16436 "int j = 2;", 16437 Alignment); 16438 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16439 verifyFormat("int i = 1;\n" 16440 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16441 " someLooooooooooooooooongFunction();\n" 16442 "int j = 2;", 16443 Alignment); 16444 16445 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16446 verifyFormat("auto lambda = []() {\n" 16447 " auto ii = 0;\n" 16448 " float j = 0;\n" 16449 " return 0;\n" 16450 "};\n" 16451 "int i = 0;\n" 16452 "float i2 = 0;\n" 16453 "auto v = type{\n" 16454 " i = 1, //\n" 16455 " (i = 2), //\n" 16456 " i = 3 //\n" 16457 "};", 16458 Alignment); 16459 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16460 16461 verifyFormat( 16462 "int i = 1;\n" 16463 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16464 " loooooooooooooooooooooongParameterB);\n" 16465 "int j = 2;", 16466 Alignment); 16467 16468 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16469 // We expect declarations and assignments to align, as long as it doesn't 16470 // exceed the column limit, starting a new alignment sequence whenever it 16471 // happens. 16472 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16473 Alignment.ColumnLimit = 30; 16474 verifyFormat("float ii = 1;\n" 16475 "unsigned j = 2;\n" 16476 "int someVerylongVariable = 1;\n" 16477 "AnotherLongType ll = 123456;\n" 16478 "VeryVeryLongType k = 2;\n" 16479 "int myvar = 1;", 16480 Alignment); 16481 Alignment.ColumnLimit = 80; 16482 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16483 16484 verifyFormat( 16485 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16486 " typename LongType, typename B>\n" 16487 "auto foo() {}\n", 16488 Alignment); 16489 verifyFormat("float a, b = 1;\n" 16490 "int c = 2;\n" 16491 "int dd = 3;\n", 16492 Alignment); 16493 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16494 "float b[1][] = {{3.f}};\n", 16495 Alignment); 16496 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16497 verifyFormat("float a, b = 1;\n" 16498 "int c = 2;\n" 16499 "int dd = 3;\n", 16500 Alignment); 16501 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16502 "float b[1][] = {{3.f}};\n", 16503 Alignment); 16504 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16505 16506 Alignment.ColumnLimit = 30; 16507 Alignment.BinPackParameters = false; 16508 verifyFormat("void foo(float a,\n" 16509 " float b,\n" 16510 " int c,\n" 16511 " uint32_t *d) {\n" 16512 " int *e = 0;\n" 16513 " float f = 0;\n" 16514 " double g = 0;\n" 16515 "}\n" 16516 "void bar(ino_t a,\n" 16517 " int b,\n" 16518 " uint32_t *c,\n" 16519 " bool d) {}\n", 16520 Alignment); 16521 Alignment.BinPackParameters = true; 16522 Alignment.ColumnLimit = 80; 16523 16524 // Bug 33507 16525 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16526 verifyFormat( 16527 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16528 " static const Version verVs2017;\n" 16529 " return true;\n" 16530 "});\n", 16531 Alignment); 16532 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16533 16534 // See llvm.org/PR35641 16535 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16536 verifyFormat("int func() { //\n" 16537 " int b;\n" 16538 " unsigned c;\n" 16539 "}", 16540 Alignment); 16541 16542 // See PR37175 16543 FormatStyle Style = getMozillaStyle(); 16544 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16545 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16546 "foo(int a);", 16547 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16548 16549 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16550 verifyFormat("unsigned int* a;\n" 16551 "int* b;\n" 16552 "unsigned int Const* c;\n" 16553 "unsigned int const* d;\n" 16554 "unsigned int Const& e;\n" 16555 "unsigned int const& f;", 16556 Alignment); 16557 verifyFormat("Const unsigned int* c;\n" 16558 "const unsigned int* d;\n" 16559 "Const unsigned int& e;\n" 16560 "const unsigned int& f;\n" 16561 "const unsigned g;\n" 16562 "Const unsigned h;", 16563 Alignment); 16564 16565 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16566 verifyFormat("unsigned int * a;\n" 16567 "int * b;\n" 16568 "unsigned int Const * c;\n" 16569 "unsigned int const * d;\n" 16570 "unsigned int Const & e;\n" 16571 "unsigned int const & f;", 16572 Alignment); 16573 verifyFormat("Const unsigned int * c;\n" 16574 "const unsigned int * d;\n" 16575 "Const unsigned int & e;\n" 16576 "const unsigned int & f;\n" 16577 "const unsigned g;\n" 16578 "Const unsigned h;", 16579 Alignment); 16580 } 16581 16582 TEST_F(FormatTest, AlignWithLineBreaks) { 16583 auto Style = getLLVMStyleWithColumns(120); 16584 16585 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16586 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16587 verifyFormat("void foo() {\n" 16588 " int myVar = 5;\n" 16589 " double x = 3.14;\n" 16590 " auto str = \"Hello \"\n" 16591 " \"World\";\n" 16592 " auto s = \"Hello \"\n" 16593 " \"Again\";\n" 16594 "}", 16595 Style); 16596 16597 // clang-format off 16598 verifyFormat("void foo() {\n" 16599 " const int capacityBefore = Entries.capacity();\n" 16600 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16601 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16602 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16603 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16604 "}", 16605 Style); 16606 // clang-format on 16607 16608 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16609 verifyFormat("void foo() {\n" 16610 " int myVar = 5;\n" 16611 " double x = 3.14;\n" 16612 " auto str = \"Hello \"\n" 16613 " \"World\";\n" 16614 " auto s = \"Hello \"\n" 16615 " \"Again\";\n" 16616 "}", 16617 Style); 16618 16619 // clang-format off 16620 verifyFormat("void foo() {\n" 16621 " const int capacityBefore = Entries.capacity();\n" 16622 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16623 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16624 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16625 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16626 "}", 16627 Style); 16628 // clang-format on 16629 16630 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16631 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16632 verifyFormat("void foo() {\n" 16633 " int myVar = 5;\n" 16634 " double x = 3.14;\n" 16635 " auto str = \"Hello \"\n" 16636 " \"World\";\n" 16637 " auto s = \"Hello \"\n" 16638 " \"Again\";\n" 16639 "}", 16640 Style); 16641 16642 // clang-format off 16643 verifyFormat("void foo() {\n" 16644 " const int capacityBefore = Entries.capacity();\n" 16645 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16646 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16647 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16648 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16649 "}", 16650 Style); 16651 // clang-format on 16652 16653 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16654 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16655 16656 verifyFormat("void foo() {\n" 16657 " int myVar = 5;\n" 16658 " double x = 3.14;\n" 16659 " auto str = \"Hello \"\n" 16660 " \"World\";\n" 16661 " auto s = \"Hello \"\n" 16662 " \"Again\";\n" 16663 "}", 16664 Style); 16665 16666 // clang-format off 16667 verifyFormat("void foo() {\n" 16668 " const int capacityBefore = Entries.capacity();\n" 16669 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16670 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16671 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16672 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16673 "}", 16674 Style); 16675 // clang-format on 16676 16677 Style = getLLVMStyleWithColumns(120); 16678 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16679 Style.ContinuationIndentWidth = 4; 16680 Style.IndentWidth = 4; 16681 16682 // clang-format off 16683 verifyFormat("void SomeFunc() {\n" 16684 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16685 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16686 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16687 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16688 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16689 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16690 "}", 16691 Style); 16692 // clang-format on 16693 16694 Style.BinPackArguments = false; 16695 16696 // clang-format off 16697 verifyFormat("void SomeFunc() {\n" 16698 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 16699 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16700 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 16701 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16702 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 16703 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16704 "}", 16705 Style); 16706 // clang-format on 16707 } 16708 16709 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16710 auto Style = getLLVMStyleWithColumns(60); 16711 16712 verifyFormat("void foo1(void) {\n" 16713 " BYTE p[1] = 1;\n" 16714 " A B = {.one_foooooooooooooooo = 2,\n" 16715 " .two_fooooooooooooo = 3,\n" 16716 " .three_fooooooooooooo = 4};\n" 16717 " BYTE payload = 2;\n" 16718 "}", 16719 Style); 16720 16721 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16722 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16723 verifyFormat("void foo2(void) {\n" 16724 " BYTE p[1] = 1;\n" 16725 " A B = {.one_foooooooooooooooo = 2,\n" 16726 " .two_fooooooooooooo = 3,\n" 16727 " .three_fooooooooooooo = 4};\n" 16728 " BYTE payload = 2;\n" 16729 "}", 16730 Style); 16731 16732 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16733 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16734 verifyFormat("void foo3(void) {\n" 16735 " BYTE p[1] = 1;\n" 16736 " A B = {.one_foooooooooooooooo = 2,\n" 16737 " .two_fooooooooooooo = 3,\n" 16738 " .three_fooooooooooooo = 4};\n" 16739 " BYTE payload = 2;\n" 16740 "}", 16741 Style); 16742 16743 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16744 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16745 verifyFormat("void foo4(void) {\n" 16746 " BYTE p[1] = 1;\n" 16747 " A B = {.one_foooooooooooooooo = 2,\n" 16748 " .two_fooooooooooooo = 3,\n" 16749 " .three_fooooooooooooo = 4};\n" 16750 " BYTE payload = 2;\n" 16751 "}", 16752 Style); 16753 } 16754 16755 TEST_F(FormatTest, LinuxBraceBreaking) { 16756 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16757 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16758 verifyFormat("namespace a\n" 16759 "{\n" 16760 "class A\n" 16761 "{\n" 16762 " void f()\n" 16763 " {\n" 16764 " if (true) {\n" 16765 " a();\n" 16766 " b();\n" 16767 " } else {\n" 16768 " a();\n" 16769 " }\n" 16770 " }\n" 16771 " void g() { return; }\n" 16772 "};\n" 16773 "struct B {\n" 16774 " int x;\n" 16775 "};\n" 16776 "} // namespace a\n", 16777 LinuxBraceStyle); 16778 verifyFormat("enum X {\n" 16779 " Y = 0,\n" 16780 "}\n", 16781 LinuxBraceStyle); 16782 verifyFormat("struct S {\n" 16783 " int Type;\n" 16784 " union {\n" 16785 " int x;\n" 16786 " double y;\n" 16787 " } Value;\n" 16788 " class C\n" 16789 " {\n" 16790 " MyFavoriteType Value;\n" 16791 " } Class;\n" 16792 "}\n", 16793 LinuxBraceStyle); 16794 } 16795 16796 TEST_F(FormatTest, MozillaBraceBreaking) { 16797 FormatStyle MozillaBraceStyle = getLLVMStyle(); 16798 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 16799 MozillaBraceStyle.FixNamespaceComments = false; 16800 verifyFormat("namespace a {\n" 16801 "class A\n" 16802 "{\n" 16803 " void f()\n" 16804 " {\n" 16805 " if (true) {\n" 16806 " a();\n" 16807 " b();\n" 16808 " }\n" 16809 " }\n" 16810 " void g() { return; }\n" 16811 "};\n" 16812 "enum E\n" 16813 "{\n" 16814 " A,\n" 16815 " // foo\n" 16816 " B,\n" 16817 " C\n" 16818 "};\n" 16819 "struct B\n" 16820 "{\n" 16821 " int x;\n" 16822 "};\n" 16823 "}\n", 16824 MozillaBraceStyle); 16825 verifyFormat("struct S\n" 16826 "{\n" 16827 " int Type;\n" 16828 " union\n" 16829 " {\n" 16830 " int x;\n" 16831 " double y;\n" 16832 " } Value;\n" 16833 " class C\n" 16834 " {\n" 16835 " MyFavoriteType Value;\n" 16836 " } Class;\n" 16837 "}\n", 16838 MozillaBraceStyle); 16839 } 16840 16841 TEST_F(FormatTest, StroustrupBraceBreaking) { 16842 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 16843 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 16844 verifyFormat("namespace a {\n" 16845 "class A {\n" 16846 " void f()\n" 16847 " {\n" 16848 " if (true) {\n" 16849 " a();\n" 16850 " b();\n" 16851 " }\n" 16852 " }\n" 16853 " void g() { return; }\n" 16854 "};\n" 16855 "struct B {\n" 16856 " int x;\n" 16857 "};\n" 16858 "} // namespace a\n", 16859 StroustrupBraceStyle); 16860 16861 verifyFormat("void foo()\n" 16862 "{\n" 16863 " if (a) {\n" 16864 " a();\n" 16865 " }\n" 16866 " else {\n" 16867 " b();\n" 16868 " }\n" 16869 "}\n", 16870 StroustrupBraceStyle); 16871 16872 verifyFormat("#ifdef _DEBUG\n" 16873 "int foo(int i = 0)\n" 16874 "#else\n" 16875 "int foo(int i = 5)\n" 16876 "#endif\n" 16877 "{\n" 16878 " return i;\n" 16879 "}", 16880 StroustrupBraceStyle); 16881 16882 verifyFormat("void foo() {}\n" 16883 "void bar()\n" 16884 "#ifdef _DEBUG\n" 16885 "{\n" 16886 " foo();\n" 16887 "}\n" 16888 "#else\n" 16889 "{\n" 16890 "}\n" 16891 "#endif", 16892 StroustrupBraceStyle); 16893 16894 verifyFormat("void foobar() { int i = 5; }\n" 16895 "#ifdef _DEBUG\n" 16896 "void bar() {}\n" 16897 "#else\n" 16898 "void bar() { foobar(); }\n" 16899 "#endif", 16900 StroustrupBraceStyle); 16901 } 16902 16903 TEST_F(FormatTest, AllmanBraceBreaking) { 16904 FormatStyle AllmanBraceStyle = getLLVMStyle(); 16905 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 16906 16907 EXPECT_EQ("namespace a\n" 16908 "{\n" 16909 "void f();\n" 16910 "void g();\n" 16911 "} // namespace a\n", 16912 format("namespace a\n" 16913 "{\n" 16914 "void f();\n" 16915 "void g();\n" 16916 "}\n", 16917 AllmanBraceStyle)); 16918 16919 verifyFormat("namespace a\n" 16920 "{\n" 16921 "class A\n" 16922 "{\n" 16923 " void f()\n" 16924 " {\n" 16925 " if (true)\n" 16926 " {\n" 16927 " a();\n" 16928 " b();\n" 16929 " }\n" 16930 " }\n" 16931 " void g() { return; }\n" 16932 "};\n" 16933 "struct B\n" 16934 "{\n" 16935 " int x;\n" 16936 "};\n" 16937 "union C\n" 16938 "{\n" 16939 "};\n" 16940 "} // namespace a", 16941 AllmanBraceStyle); 16942 16943 verifyFormat("void f()\n" 16944 "{\n" 16945 " if (true)\n" 16946 " {\n" 16947 " a();\n" 16948 " }\n" 16949 " else if (false)\n" 16950 " {\n" 16951 " b();\n" 16952 " }\n" 16953 " else\n" 16954 " {\n" 16955 " c();\n" 16956 " }\n" 16957 "}\n", 16958 AllmanBraceStyle); 16959 16960 verifyFormat("void f()\n" 16961 "{\n" 16962 " for (int i = 0; i < 10; ++i)\n" 16963 " {\n" 16964 " a();\n" 16965 " }\n" 16966 " while (false)\n" 16967 " {\n" 16968 " b();\n" 16969 " }\n" 16970 " do\n" 16971 " {\n" 16972 " c();\n" 16973 " } while (false)\n" 16974 "}\n", 16975 AllmanBraceStyle); 16976 16977 verifyFormat("void f(int a)\n" 16978 "{\n" 16979 " switch (a)\n" 16980 " {\n" 16981 " case 0:\n" 16982 " break;\n" 16983 " case 1:\n" 16984 " {\n" 16985 " break;\n" 16986 " }\n" 16987 " case 2:\n" 16988 " {\n" 16989 " }\n" 16990 " break;\n" 16991 " default:\n" 16992 " break;\n" 16993 " }\n" 16994 "}\n", 16995 AllmanBraceStyle); 16996 16997 verifyFormat("enum X\n" 16998 "{\n" 16999 " Y = 0,\n" 17000 "}\n", 17001 AllmanBraceStyle); 17002 verifyFormat("enum X\n" 17003 "{\n" 17004 " Y = 0\n" 17005 "}\n", 17006 AllmanBraceStyle); 17007 17008 verifyFormat("@interface BSApplicationController ()\n" 17009 "{\n" 17010 "@private\n" 17011 " id _extraIvar;\n" 17012 "}\n" 17013 "@end\n", 17014 AllmanBraceStyle); 17015 17016 verifyFormat("#ifdef _DEBUG\n" 17017 "int foo(int i = 0)\n" 17018 "#else\n" 17019 "int foo(int i = 5)\n" 17020 "#endif\n" 17021 "{\n" 17022 " return i;\n" 17023 "}", 17024 AllmanBraceStyle); 17025 17026 verifyFormat("void foo() {}\n" 17027 "void bar()\n" 17028 "#ifdef _DEBUG\n" 17029 "{\n" 17030 " foo();\n" 17031 "}\n" 17032 "#else\n" 17033 "{\n" 17034 "}\n" 17035 "#endif", 17036 AllmanBraceStyle); 17037 17038 verifyFormat("void foobar() { int i = 5; }\n" 17039 "#ifdef _DEBUG\n" 17040 "void bar() {}\n" 17041 "#else\n" 17042 "void bar() { foobar(); }\n" 17043 "#endif", 17044 AllmanBraceStyle); 17045 17046 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 17047 FormatStyle::SLS_All); 17048 17049 verifyFormat("[](int i) { return i + 2; };\n" 17050 "[](int i, int j)\n" 17051 "{\n" 17052 " auto x = i + j;\n" 17053 " auto y = i * j;\n" 17054 " return x ^ y;\n" 17055 "};\n" 17056 "void foo()\n" 17057 "{\n" 17058 " auto shortLambda = [](int i) { return i + 2; };\n" 17059 " auto longLambda = [](int i, int j)\n" 17060 " {\n" 17061 " auto x = i + j;\n" 17062 " auto y = i * j;\n" 17063 " return x ^ y;\n" 17064 " };\n" 17065 "}", 17066 AllmanBraceStyle); 17067 17068 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17069 17070 verifyFormat("[](int i)\n" 17071 "{\n" 17072 " return i + 2;\n" 17073 "};\n" 17074 "[](int i, int j)\n" 17075 "{\n" 17076 " auto x = i + j;\n" 17077 " auto y = i * j;\n" 17078 " return x ^ y;\n" 17079 "};\n" 17080 "void foo()\n" 17081 "{\n" 17082 " auto shortLambda = [](int i)\n" 17083 " {\n" 17084 " return i + 2;\n" 17085 " };\n" 17086 " auto longLambda = [](int i, int j)\n" 17087 " {\n" 17088 " auto x = i + j;\n" 17089 " auto y = i * j;\n" 17090 " return x ^ y;\n" 17091 " };\n" 17092 "}", 17093 AllmanBraceStyle); 17094 17095 // Reset 17096 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 17097 17098 // This shouldn't affect ObjC blocks.. 17099 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17100 " // ...\n" 17101 " int i;\n" 17102 "}];", 17103 AllmanBraceStyle); 17104 verifyFormat("void (^block)(void) = ^{\n" 17105 " // ...\n" 17106 " int i;\n" 17107 "};", 17108 AllmanBraceStyle); 17109 // .. or dict literals. 17110 verifyFormat("void f()\n" 17111 "{\n" 17112 " // ...\n" 17113 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17114 "}", 17115 AllmanBraceStyle); 17116 verifyFormat("void f()\n" 17117 "{\n" 17118 " // ...\n" 17119 " [object someMethod:@{a : @\"b\"}];\n" 17120 "}", 17121 AllmanBraceStyle); 17122 verifyFormat("int f()\n" 17123 "{ // comment\n" 17124 " return 42;\n" 17125 "}", 17126 AllmanBraceStyle); 17127 17128 AllmanBraceStyle.ColumnLimit = 19; 17129 verifyFormat("void f() { int i; }", AllmanBraceStyle); 17130 AllmanBraceStyle.ColumnLimit = 18; 17131 verifyFormat("void f()\n" 17132 "{\n" 17133 " int i;\n" 17134 "}", 17135 AllmanBraceStyle); 17136 AllmanBraceStyle.ColumnLimit = 80; 17137 17138 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 17139 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17140 FormatStyle::SIS_WithoutElse; 17141 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17142 verifyFormat("void f(bool b)\n" 17143 "{\n" 17144 " if (b)\n" 17145 " {\n" 17146 " return;\n" 17147 " }\n" 17148 "}\n", 17149 BreakBeforeBraceShortIfs); 17150 verifyFormat("void f(bool b)\n" 17151 "{\n" 17152 " if constexpr (b)\n" 17153 " {\n" 17154 " return;\n" 17155 " }\n" 17156 "}\n", 17157 BreakBeforeBraceShortIfs); 17158 verifyFormat("void f(bool b)\n" 17159 "{\n" 17160 " if CONSTEXPR (b)\n" 17161 " {\n" 17162 " return;\n" 17163 " }\n" 17164 "}\n", 17165 BreakBeforeBraceShortIfs); 17166 verifyFormat("void f(bool b)\n" 17167 "{\n" 17168 " if (b) return;\n" 17169 "}\n", 17170 BreakBeforeBraceShortIfs); 17171 verifyFormat("void f(bool b)\n" 17172 "{\n" 17173 " if constexpr (b) return;\n" 17174 "}\n", 17175 BreakBeforeBraceShortIfs); 17176 verifyFormat("void f(bool b)\n" 17177 "{\n" 17178 " if CONSTEXPR (b) return;\n" 17179 "}\n", 17180 BreakBeforeBraceShortIfs); 17181 verifyFormat("void f(bool b)\n" 17182 "{\n" 17183 " while (b)\n" 17184 " {\n" 17185 " return;\n" 17186 " }\n" 17187 "}\n", 17188 BreakBeforeBraceShortIfs); 17189 } 17190 17191 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 17192 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 17193 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 17194 17195 // Make a few changes to the style for testing purposes 17196 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 17197 FormatStyle::SFS_Empty; 17198 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17199 WhitesmithsBraceStyle.ColumnLimit = 0; 17200 17201 // FIXME: this test case can't decide whether there should be a blank line 17202 // after the ~D() line or not. It adds one if one doesn't exist in the test 17203 // and it removes the line if one exists. 17204 /* 17205 verifyFormat("class A;\n" 17206 "namespace B\n" 17207 " {\n" 17208 "class C;\n" 17209 "// Comment\n" 17210 "class D\n" 17211 " {\n" 17212 "public:\n" 17213 " D();\n" 17214 " ~D() {}\n" 17215 "private:\n" 17216 " enum E\n" 17217 " {\n" 17218 " F\n" 17219 " }\n" 17220 " };\n" 17221 " } // namespace B\n", 17222 WhitesmithsBraceStyle); 17223 */ 17224 17225 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17226 verifyFormat("namespace a\n" 17227 " {\n" 17228 "class A\n" 17229 " {\n" 17230 " void f()\n" 17231 " {\n" 17232 " if (true)\n" 17233 " {\n" 17234 " a();\n" 17235 " b();\n" 17236 " }\n" 17237 " }\n" 17238 " void g()\n" 17239 " {\n" 17240 " return;\n" 17241 " }\n" 17242 " };\n" 17243 "struct B\n" 17244 " {\n" 17245 " int x;\n" 17246 " };\n" 17247 " } // namespace a", 17248 WhitesmithsBraceStyle); 17249 17250 verifyFormat("namespace a\n" 17251 " {\n" 17252 "namespace b\n" 17253 " {\n" 17254 "class A\n" 17255 " {\n" 17256 " void f()\n" 17257 " {\n" 17258 " if (true)\n" 17259 " {\n" 17260 " a();\n" 17261 " b();\n" 17262 " }\n" 17263 " }\n" 17264 " void g()\n" 17265 " {\n" 17266 " return;\n" 17267 " }\n" 17268 " };\n" 17269 "struct B\n" 17270 " {\n" 17271 " int x;\n" 17272 " };\n" 17273 " } // namespace b\n" 17274 " } // namespace a", 17275 WhitesmithsBraceStyle); 17276 17277 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17278 verifyFormat("namespace a\n" 17279 " {\n" 17280 "namespace b\n" 17281 " {\n" 17282 " class A\n" 17283 " {\n" 17284 " void f()\n" 17285 " {\n" 17286 " if (true)\n" 17287 " {\n" 17288 " a();\n" 17289 " b();\n" 17290 " }\n" 17291 " }\n" 17292 " void g()\n" 17293 " {\n" 17294 " return;\n" 17295 " }\n" 17296 " };\n" 17297 " struct B\n" 17298 " {\n" 17299 " int x;\n" 17300 " };\n" 17301 " } // namespace b\n" 17302 " } // namespace a", 17303 WhitesmithsBraceStyle); 17304 17305 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17306 verifyFormat("namespace a\n" 17307 " {\n" 17308 " namespace b\n" 17309 " {\n" 17310 " class A\n" 17311 " {\n" 17312 " void f()\n" 17313 " {\n" 17314 " if (true)\n" 17315 " {\n" 17316 " a();\n" 17317 " b();\n" 17318 " }\n" 17319 " }\n" 17320 " void g()\n" 17321 " {\n" 17322 " return;\n" 17323 " }\n" 17324 " };\n" 17325 " struct B\n" 17326 " {\n" 17327 " int x;\n" 17328 " };\n" 17329 " } // namespace b\n" 17330 " } // namespace a", 17331 WhitesmithsBraceStyle); 17332 17333 verifyFormat("void f()\n" 17334 " {\n" 17335 " if (true)\n" 17336 " {\n" 17337 " a();\n" 17338 " }\n" 17339 " else if (false)\n" 17340 " {\n" 17341 " b();\n" 17342 " }\n" 17343 " else\n" 17344 " {\n" 17345 " c();\n" 17346 " }\n" 17347 " }\n", 17348 WhitesmithsBraceStyle); 17349 17350 verifyFormat("void f()\n" 17351 " {\n" 17352 " for (int i = 0; i < 10; ++i)\n" 17353 " {\n" 17354 " a();\n" 17355 " }\n" 17356 " while (false)\n" 17357 " {\n" 17358 " b();\n" 17359 " }\n" 17360 " do\n" 17361 " {\n" 17362 " c();\n" 17363 " } while (false)\n" 17364 " }\n", 17365 WhitesmithsBraceStyle); 17366 17367 WhitesmithsBraceStyle.IndentCaseLabels = true; 17368 verifyFormat("void switchTest1(int a)\n" 17369 " {\n" 17370 " switch (a)\n" 17371 " {\n" 17372 " case 2:\n" 17373 " {\n" 17374 " }\n" 17375 " break;\n" 17376 " }\n" 17377 " }\n", 17378 WhitesmithsBraceStyle); 17379 17380 verifyFormat("void switchTest2(int a)\n" 17381 " {\n" 17382 " switch (a)\n" 17383 " {\n" 17384 " case 0:\n" 17385 " break;\n" 17386 " case 1:\n" 17387 " {\n" 17388 " break;\n" 17389 " }\n" 17390 " case 2:\n" 17391 " {\n" 17392 " }\n" 17393 " break;\n" 17394 " default:\n" 17395 " break;\n" 17396 " }\n" 17397 " }\n", 17398 WhitesmithsBraceStyle); 17399 17400 verifyFormat("void switchTest3(int a)\n" 17401 " {\n" 17402 " switch (a)\n" 17403 " {\n" 17404 " case 0:\n" 17405 " {\n" 17406 " foo(x);\n" 17407 " }\n" 17408 " break;\n" 17409 " default:\n" 17410 " {\n" 17411 " foo(1);\n" 17412 " }\n" 17413 " break;\n" 17414 " }\n" 17415 " }\n", 17416 WhitesmithsBraceStyle); 17417 17418 WhitesmithsBraceStyle.IndentCaseLabels = false; 17419 17420 verifyFormat("void switchTest4(int a)\n" 17421 " {\n" 17422 " switch (a)\n" 17423 " {\n" 17424 " case 2:\n" 17425 " {\n" 17426 " }\n" 17427 " break;\n" 17428 " }\n" 17429 " }\n", 17430 WhitesmithsBraceStyle); 17431 17432 verifyFormat("void switchTest5(int a)\n" 17433 " {\n" 17434 " switch (a)\n" 17435 " {\n" 17436 " case 0:\n" 17437 " break;\n" 17438 " case 1:\n" 17439 " {\n" 17440 " foo();\n" 17441 " break;\n" 17442 " }\n" 17443 " case 2:\n" 17444 " {\n" 17445 " }\n" 17446 " break;\n" 17447 " default:\n" 17448 " break;\n" 17449 " }\n" 17450 " }\n", 17451 WhitesmithsBraceStyle); 17452 17453 verifyFormat("void switchTest6(int a)\n" 17454 " {\n" 17455 " switch (a)\n" 17456 " {\n" 17457 " case 0:\n" 17458 " {\n" 17459 " foo(x);\n" 17460 " }\n" 17461 " break;\n" 17462 " default:\n" 17463 " {\n" 17464 " foo(1);\n" 17465 " }\n" 17466 " break;\n" 17467 " }\n" 17468 " }\n", 17469 WhitesmithsBraceStyle); 17470 17471 verifyFormat("enum X\n" 17472 " {\n" 17473 " Y = 0, // testing\n" 17474 " }\n", 17475 WhitesmithsBraceStyle); 17476 17477 verifyFormat("enum X\n" 17478 " {\n" 17479 " Y = 0\n" 17480 " }\n", 17481 WhitesmithsBraceStyle); 17482 verifyFormat("enum X\n" 17483 " {\n" 17484 " Y = 0,\n" 17485 " Z = 1\n" 17486 " };\n", 17487 WhitesmithsBraceStyle); 17488 17489 verifyFormat("@interface BSApplicationController ()\n" 17490 " {\n" 17491 "@private\n" 17492 " id _extraIvar;\n" 17493 " }\n" 17494 "@end\n", 17495 WhitesmithsBraceStyle); 17496 17497 verifyFormat("#ifdef _DEBUG\n" 17498 "int foo(int i = 0)\n" 17499 "#else\n" 17500 "int foo(int i = 5)\n" 17501 "#endif\n" 17502 " {\n" 17503 " return i;\n" 17504 " }", 17505 WhitesmithsBraceStyle); 17506 17507 verifyFormat("void foo() {}\n" 17508 "void bar()\n" 17509 "#ifdef _DEBUG\n" 17510 " {\n" 17511 " foo();\n" 17512 " }\n" 17513 "#else\n" 17514 " {\n" 17515 " }\n" 17516 "#endif", 17517 WhitesmithsBraceStyle); 17518 17519 verifyFormat("void foobar()\n" 17520 " {\n" 17521 " int i = 5;\n" 17522 " }\n" 17523 "#ifdef _DEBUG\n" 17524 "void bar()\n" 17525 " {\n" 17526 " }\n" 17527 "#else\n" 17528 "void bar()\n" 17529 " {\n" 17530 " foobar();\n" 17531 " }\n" 17532 "#endif", 17533 WhitesmithsBraceStyle); 17534 17535 // This shouldn't affect ObjC blocks.. 17536 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17537 " // ...\n" 17538 " int i;\n" 17539 "}];", 17540 WhitesmithsBraceStyle); 17541 verifyFormat("void (^block)(void) = ^{\n" 17542 " // ...\n" 17543 " int i;\n" 17544 "};", 17545 WhitesmithsBraceStyle); 17546 // .. or dict literals. 17547 verifyFormat("void f()\n" 17548 " {\n" 17549 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17550 " }", 17551 WhitesmithsBraceStyle); 17552 17553 verifyFormat("int f()\n" 17554 " { // comment\n" 17555 " return 42;\n" 17556 " }", 17557 WhitesmithsBraceStyle); 17558 17559 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17560 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17561 FormatStyle::SIS_OnlyFirstIf; 17562 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17563 verifyFormat("void f(bool b)\n" 17564 " {\n" 17565 " if (b)\n" 17566 " {\n" 17567 " return;\n" 17568 " }\n" 17569 " }\n", 17570 BreakBeforeBraceShortIfs); 17571 verifyFormat("void f(bool b)\n" 17572 " {\n" 17573 " if (b) return;\n" 17574 " }\n", 17575 BreakBeforeBraceShortIfs); 17576 verifyFormat("void f(bool b)\n" 17577 " {\n" 17578 " while (b)\n" 17579 " {\n" 17580 " return;\n" 17581 " }\n" 17582 " }\n", 17583 BreakBeforeBraceShortIfs); 17584 } 17585 17586 TEST_F(FormatTest, GNUBraceBreaking) { 17587 FormatStyle GNUBraceStyle = getLLVMStyle(); 17588 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17589 verifyFormat("namespace a\n" 17590 "{\n" 17591 "class A\n" 17592 "{\n" 17593 " void f()\n" 17594 " {\n" 17595 " int a;\n" 17596 " {\n" 17597 " int b;\n" 17598 " }\n" 17599 " if (true)\n" 17600 " {\n" 17601 " a();\n" 17602 " b();\n" 17603 " }\n" 17604 " }\n" 17605 " void g() { return; }\n" 17606 "}\n" 17607 "} // namespace a", 17608 GNUBraceStyle); 17609 17610 verifyFormat("void f()\n" 17611 "{\n" 17612 " if (true)\n" 17613 " {\n" 17614 " a();\n" 17615 " }\n" 17616 " else if (false)\n" 17617 " {\n" 17618 " b();\n" 17619 " }\n" 17620 " else\n" 17621 " {\n" 17622 " c();\n" 17623 " }\n" 17624 "}\n", 17625 GNUBraceStyle); 17626 17627 verifyFormat("void f()\n" 17628 "{\n" 17629 " for (int i = 0; i < 10; ++i)\n" 17630 " {\n" 17631 " a();\n" 17632 " }\n" 17633 " while (false)\n" 17634 " {\n" 17635 " b();\n" 17636 " }\n" 17637 " do\n" 17638 " {\n" 17639 " c();\n" 17640 " }\n" 17641 " while (false);\n" 17642 "}\n", 17643 GNUBraceStyle); 17644 17645 verifyFormat("void f(int a)\n" 17646 "{\n" 17647 " switch (a)\n" 17648 " {\n" 17649 " case 0:\n" 17650 " break;\n" 17651 " case 1:\n" 17652 " {\n" 17653 " break;\n" 17654 " }\n" 17655 " case 2:\n" 17656 " {\n" 17657 " }\n" 17658 " break;\n" 17659 " default:\n" 17660 " break;\n" 17661 " }\n" 17662 "}\n", 17663 GNUBraceStyle); 17664 17665 verifyFormat("enum X\n" 17666 "{\n" 17667 " Y = 0,\n" 17668 "}\n", 17669 GNUBraceStyle); 17670 17671 verifyFormat("@interface BSApplicationController ()\n" 17672 "{\n" 17673 "@private\n" 17674 " id _extraIvar;\n" 17675 "}\n" 17676 "@end\n", 17677 GNUBraceStyle); 17678 17679 verifyFormat("#ifdef _DEBUG\n" 17680 "int foo(int i = 0)\n" 17681 "#else\n" 17682 "int foo(int i = 5)\n" 17683 "#endif\n" 17684 "{\n" 17685 " return i;\n" 17686 "}", 17687 GNUBraceStyle); 17688 17689 verifyFormat("void foo() {}\n" 17690 "void bar()\n" 17691 "#ifdef _DEBUG\n" 17692 "{\n" 17693 " foo();\n" 17694 "}\n" 17695 "#else\n" 17696 "{\n" 17697 "}\n" 17698 "#endif", 17699 GNUBraceStyle); 17700 17701 verifyFormat("void foobar() { int i = 5; }\n" 17702 "#ifdef _DEBUG\n" 17703 "void bar() {}\n" 17704 "#else\n" 17705 "void bar() { foobar(); }\n" 17706 "#endif", 17707 GNUBraceStyle); 17708 } 17709 17710 TEST_F(FormatTest, WebKitBraceBreaking) { 17711 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17712 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17713 WebKitBraceStyle.FixNamespaceComments = false; 17714 verifyFormat("namespace a {\n" 17715 "class A {\n" 17716 " void f()\n" 17717 " {\n" 17718 " if (true) {\n" 17719 " a();\n" 17720 " b();\n" 17721 " }\n" 17722 " }\n" 17723 " void g() { return; }\n" 17724 "};\n" 17725 "enum E {\n" 17726 " A,\n" 17727 " // foo\n" 17728 " B,\n" 17729 " C\n" 17730 "};\n" 17731 "struct B {\n" 17732 " int x;\n" 17733 "};\n" 17734 "}\n", 17735 WebKitBraceStyle); 17736 verifyFormat("struct S {\n" 17737 " int Type;\n" 17738 " union {\n" 17739 " int x;\n" 17740 " double y;\n" 17741 " } Value;\n" 17742 " class C {\n" 17743 " MyFavoriteType Value;\n" 17744 " } Class;\n" 17745 "};\n", 17746 WebKitBraceStyle); 17747 } 17748 17749 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17750 verifyFormat("void f() {\n" 17751 " try {\n" 17752 " } catch (const Exception &e) {\n" 17753 " }\n" 17754 "}\n", 17755 getLLVMStyle()); 17756 } 17757 17758 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17759 auto Style = getLLVMStyle(); 17760 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17761 Style.AlignConsecutiveAssignments = 17762 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17763 Style.AlignConsecutiveDeclarations = 17764 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17765 verifyFormat("struct test demo[] = {\n" 17766 " {56, 23, \"hello\"},\n" 17767 " {-1, 93463, \"world\"},\n" 17768 " { 7, 5, \"!!\"}\n" 17769 "};\n", 17770 Style); 17771 17772 verifyFormat("struct test demo[] = {\n" 17773 " {56, 23, \"hello\"}, // first line\n" 17774 " {-1, 93463, \"world\"}, // second line\n" 17775 " { 7, 5, \"!!\"} // third line\n" 17776 "};\n", 17777 Style); 17778 17779 verifyFormat("struct test demo[4] = {\n" 17780 " { 56, 23, 21, \"oh\"}, // first line\n" 17781 " { -1, 93463, 22, \"my\"}, // second line\n" 17782 " { 7, 5, 1, \"goodness\"} // third line\n" 17783 " {234, 5, 1, \"gracious\"} // fourth line\n" 17784 "};\n", 17785 Style); 17786 17787 verifyFormat("struct test demo[3] = {\n" 17788 " {56, 23, \"hello\"},\n" 17789 " {-1, 93463, \"world\"},\n" 17790 " { 7, 5, \"!!\"}\n" 17791 "};\n", 17792 Style); 17793 17794 verifyFormat("struct test demo[3] = {\n" 17795 " {int{56}, 23, \"hello\"},\n" 17796 " {int{-1}, 93463, \"world\"},\n" 17797 " { int{7}, 5, \"!!\"}\n" 17798 "};\n", 17799 Style); 17800 17801 verifyFormat("struct test demo[] = {\n" 17802 " {56, 23, \"hello\"},\n" 17803 " {-1, 93463, \"world\"},\n" 17804 " { 7, 5, \"!!\"},\n" 17805 "};\n", 17806 Style); 17807 17808 verifyFormat("test demo[] = {\n" 17809 " {56, 23, \"hello\"},\n" 17810 " {-1, 93463, \"world\"},\n" 17811 " { 7, 5, \"!!\"},\n" 17812 "};\n", 17813 Style); 17814 17815 verifyFormat("demo = std::array<struct test, 3>{\n" 17816 " test{56, 23, \"hello\"},\n" 17817 " test{-1, 93463, \"world\"},\n" 17818 " test{ 7, 5, \"!!\"},\n" 17819 "};\n", 17820 Style); 17821 17822 verifyFormat("test demo[] = {\n" 17823 " {56, 23, \"hello\"},\n" 17824 "#if X\n" 17825 " {-1, 93463, \"world\"},\n" 17826 "#endif\n" 17827 " { 7, 5, \"!!\"}\n" 17828 "};\n", 17829 Style); 17830 17831 verifyFormat( 17832 "test demo[] = {\n" 17833 " { 7, 23,\n" 17834 " \"hello world i am a very long line that really, in any\"\n" 17835 " \"just world, ought to be split over multiple lines\"},\n" 17836 " {-1, 93463, \"world\"},\n" 17837 " {56, 5, \"!!\"}\n" 17838 "};\n", 17839 Style); 17840 17841 verifyFormat("return GradForUnaryCwise(g, {\n" 17842 " {{\"sign\"}, \"Sign\", " 17843 " {\"x\", \"dy\"}},\n" 17844 " { {\"dx\"}, \"Mul\", {\"dy\"" 17845 ", \"sign\"}},\n" 17846 "});\n", 17847 Style); 17848 17849 Style.ColumnLimit = 0; 17850 EXPECT_EQ( 17851 "test demo[] = {\n" 17852 " {56, 23, \"hello world i am a very long line that really, " 17853 "in any just world, ought to be split over multiple lines\"},\n" 17854 " {-1, 93463, " 17855 " \"world\"},\n" 17856 " { 7, 5, " 17857 " \"!!\"},\n" 17858 "};", 17859 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17860 "that really, in any just world, ought to be split over multiple " 17861 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17862 Style)); 17863 17864 Style.ColumnLimit = 80; 17865 verifyFormat("test demo[] = {\n" 17866 " {56, 23, /* a comment */ \"hello\"},\n" 17867 " {-1, 93463, \"world\"},\n" 17868 " { 7, 5, \"!!\"}\n" 17869 "};\n", 17870 Style); 17871 17872 verifyFormat("test demo[] = {\n" 17873 " {56, 23, \"hello\"},\n" 17874 " {-1, 93463, \"world\" /* comment here */},\n" 17875 " { 7, 5, \"!!\"}\n" 17876 "};\n", 17877 Style); 17878 17879 verifyFormat("test demo[] = {\n" 17880 " {56, /* a comment */ 23, \"hello\"},\n" 17881 " {-1, 93463, \"world\"},\n" 17882 " { 7, 5, \"!!\"}\n" 17883 "};\n", 17884 Style); 17885 17886 Style.ColumnLimit = 20; 17887 EXPECT_EQ( 17888 "demo = std::array<\n" 17889 " struct test, 3>{\n" 17890 " test{\n" 17891 " 56, 23,\n" 17892 " \"hello \"\n" 17893 " \"world i \"\n" 17894 " \"am a very \"\n" 17895 " \"long line \"\n" 17896 " \"that \"\n" 17897 " \"really, \"\n" 17898 " \"in any \"\n" 17899 " \"just \"\n" 17900 " \"world, \"\n" 17901 " \"ought to \"\n" 17902 " \"be split \"\n" 17903 " \"over \"\n" 17904 " \"multiple \"\n" 17905 " \"lines\"},\n" 17906 " test{-1, 93463,\n" 17907 " \"world\"},\n" 17908 " test{ 7, 5,\n" 17909 " \"!!\" },\n" 17910 "};", 17911 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17912 "i am a very long line that really, in any just world, ought " 17913 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17914 "test{7, 5, \"!!\"},};", 17915 Style)); 17916 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17917 Style = getLLVMStyleWithColumns(50); 17918 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17919 verifyFormat("static A x = {\n" 17920 " {{init1, init2, init3, init4},\n" 17921 " {init1, init2, init3, init4}}\n" 17922 "};", 17923 Style); 17924 Style.ColumnLimit = 100; 17925 EXPECT_EQ( 17926 "test demo[] = {\n" 17927 " {56, 23,\n" 17928 " \"hello world i am a very long line that really, in any just world" 17929 ", ought to be split over \"\n" 17930 " \"multiple lines\" },\n" 17931 " {-1, 93463, \"world\"},\n" 17932 " { 7, 5, \"!!\"},\n" 17933 "};", 17934 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17935 "that really, in any just world, ought to be split over multiple " 17936 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17937 Style)); 17938 17939 Style = getLLVMStyleWithColumns(50); 17940 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17941 Style.AlignConsecutiveAssignments = 17942 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17943 Style.AlignConsecutiveDeclarations = 17944 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17945 verifyFormat("struct test demo[] = {\n" 17946 " {56, 23, \"hello\"},\n" 17947 " {-1, 93463, \"world\"},\n" 17948 " { 7, 5, \"!!\"}\n" 17949 "};\n" 17950 "static A x = {\n" 17951 " {{init1, init2, init3, init4},\n" 17952 " {init1, init2, init3, init4}}\n" 17953 "};", 17954 Style); 17955 Style.ColumnLimit = 100; 17956 Style.AlignConsecutiveAssignments = 17957 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17958 Style.AlignConsecutiveDeclarations = 17959 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17960 verifyFormat("struct test demo[] = {\n" 17961 " {56, 23, \"hello\"},\n" 17962 " {-1, 93463, \"world\"},\n" 17963 " { 7, 5, \"!!\"}\n" 17964 "};\n" 17965 "struct test demo[4] = {\n" 17966 " { 56, 23, 21, \"oh\"}, // first line\n" 17967 " { -1, 93463, 22, \"my\"}, // second line\n" 17968 " { 7, 5, 1, \"goodness\"} // third line\n" 17969 " {234, 5, 1, \"gracious\"} // fourth line\n" 17970 "};\n", 17971 Style); 17972 EXPECT_EQ( 17973 "test demo[] = {\n" 17974 " {56,\n" 17975 " \"hello world i am a very long line that really, in any just world" 17976 ", ought to be split over \"\n" 17977 " \"multiple lines\", 23},\n" 17978 " {-1, \"world\", 93463},\n" 17979 " { 7, \"!!\", 5},\n" 17980 "};", 17981 format("test demo[] = {{56, \"hello world i am a very long line " 17982 "that really, in any just world, ought to be split over multiple " 17983 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 17984 Style)); 17985 } 17986 17987 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 17988 auto Style = getLLVMStyle(); 17989 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17990 /* FIXME: This case gets misformatted. 17991 verifyFormat("auto foo = Items{\n" 17992 " Section{0, bar(), },\n" 17993 " Section{1, boo() }\n" 17994 "};\n", 17995 Style); 17996 */ 17997 verifyFormat("auto foo = Items{\n" 17998 " Section{\n" 17999 " 0, bar(),\n" 18000 " }\n" 18001 "};\n", 18002 Style); 18003 verifyFormat("struct test demo[] = {\n" 18004 " {56, 23, \"hello\"},\n" 18005 " {-1, 93463, \"world\"},\n" 18006 " {7, 5, \"!!\" }\n" 18007 "};\n", 18008 Style); 18009 verifyFormat("struct test demo[] = {\n" 18010 " {56, 23, \"hello\"}, // first line\n" 18011 " {-1, 93463, \"world\"}, // second line\n" 18012 " {7, 5, \"!!\" } // third line\n" 18013 "};\n", 18014 Style); 18015 verifyFormat("struct test demo[4] = {\n" 18016 " {56, 23, 21, \"oh\" }, // first line\n" 18017 " {-1, 93463, 22, \"my\" }, // second line\n" 18018 " {7, 5, 1, \"goodness\"} // third line\n" 18019 " {234, 5, 1, \"gracious\"} // fourth line\n" 18020 "};\n", 18021 Style); 18022 verifyFormat("struct test demo[3] = {\n" 18023 " {56, 23, \"hello\"},\n" 18024 " {-1, 93463, \"world\"},\n" 18025 " {7, 5, \"!!\" }\n" 18026 "};\n", 18027 Style); 18028 18029 verifyFormat("struct test demo[3] = {\n" 18030 " {int{56}, 23, \"hello\"},\n" 18031 " {int{-1}, 93463, \"world\"},\n" 18032 " {int{7}, 5, \"!!\" }\n" 18033 "};\n", 18034 Style); 18035 verifyFormat("struct test demo[] = {\n" 18036 " {56, 23, \"hello\"},\n" 18037 " {-1, 93463, \"world\"},\n" 18038 " {7, 5, \"!!\" },\n" 18039 "};\n", 18040 Style); 18041 verifyFormat("test demo[] = {\n" 18042 " {56, 23, \"hello\"},\n" 18043 " {-1, 93463, \"world\"},\n" 18044 " {7, 5, \"!!\" },\n" 18045 "};\n", 18046 Style); 18047 verifyFormat("demo = std::array<struct test, 3>{\n" 18048 " test{56, 23, \"hello\"},\n" 18049 " test{-1, 93463, \"world\"},\n" 18050 " test{7, 5, \"!!\" },\n" 18051 "};\n", 18052 Style); 18053 verifyFormat("test demo[] = {\n" 18054 " {56, 23, \"hello\"},\n" 18055 "#if X\n" 18056 " {-1, 93463, \"world\"},\n" 18057 "#endif\n" 18058 " {7, 5, \"!!\" }\n" 18059 "};\n", 18060 Style); 18061 verifyFormat( 18062 "test demo[] = {\n" 18063 " {7, 23,\n" 18064 " \"hello world i am a very long line that really, in any\"\n" 18065 " \"just world, ought to be split over multiple lines\"},\n" 18066 " {-1, 93463, \"world\" },\n" 18067 " {56, 5, \"!!\" }\n" 18068 "};\n", 18069 Style); 18070 18071 verifyFormat("return GradForUnaryCwise(g, {\n" 18072 " {{\"sign\"}, \"Sign\", {\"x\", " 18073 "\"dy\"} },\n" 18074 " {{\"dx\"}, \"Mul\", " 18075 "{\"dy\", \"sign\"}},\n" 18076 "});\n", 18077 Style); 18078 18079 Style.ColumnLimit = 0; 18080 EXPECT_EQ( 18081 "test demo[] = {\n" 18082 " {56, 23, \"hello world i am a very long line that really, in any " 18083 "just world, ought to be split over multiple lines\"},\n" 18084 " {-1, 93463, \"world\" " 18085 " },\n" 18086 " {7, 5, \"!!\" " 18087 " },\n" 18088 "};", 18089 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18090 "that really, in any just world, ought to be split over multiple " 18091 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18092 Style)); 18093 18094 Style.ColumnLimit = 80; 18095 verifyFormat("test demo[] = {\n" 18096 " {56, 23, /* a comment */ \"hello\"},\n" 18097 " {-1, 93463, \"world\" },\n" 18098 " {7, 5, \"!!\" }\n" 18099 "};\n", 18100 Style); 18101 18102 verifyFormat("test demo[] = {\n" 18103 " {56, 23, \"hello\" },\n" 18104 " {-1, 93463, \"world\" /* comment here */},\n" 18105 " {7, 5, \"!!\" }\n" 18106 "};\n", 18107 Style); 18108 18109 verifyFormat("test demo[] = {\n" 18110 " {56, /* a comment */ 23, \"hello\"},\n" 18111 " {-1, 93463, \"world\"},\n" 18112 " {7, 5, \"!!\" }\n" 18113 "};\n", 18114 Style); 18115 18116 Style.ColumnLimit = 20; 18117 EXPECT_EQ( 18118 "demo = std::array<\n" 18119 " struct test, 3>{\n" 18120 " test{\n" 18121 " 56, 23,\n" 18122 " \"hello \"\n" 18123 " \"world i \"\n" 18124 " \"am a very \"\n" 18125 " \"long line \"\n" 18126 " \"that \"\n" 18127 " \"really, \"\n" 18128 " \"in any \"\n" 18129 " \"just \"\n" 18130 " \"world, \"\n" 18131 " \"ought to \"\n" 18132 " \"be split \"\n" 18133 " \"over \"\n" 18134 " \"multiple \"\n" 18135 " \"lines\"},\n" 18136 " test{-1, 93463,\n" 18137 " \"world\"},\n" 18138 " test{7, 5,\n" 18139 " \"!!\" },\n" 18140 "};", 18141 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18142 "i am a very long line that really, in any just world, ought " 18143 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18144 "test{7, 5, \"!!\"},};", 18145 Style)); 18146 18147 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18148 Style = getLLVMStyleWithColumns(50); 18149 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18150 verifyFormat("static A x = {\n" 18151 " {{init1, init2, init3, init4},\n" 18152 " {init1, init2, init3, init4}}\n" 18153 "};", 18154 Style); 18155 Style.ColumnLimit = 100; 18156 EXPECT_EQ( 18157 "test demo[] = {\n" 18158 " {56, 23,\n" 18159 " \"hello world i am a very long line that really, in any just world" 18160 ", ought to be split over \"\n" 18161 " \"multiple lines\" },\n" 18162 " {-1, 93463, \"world\"},\n" 18163 " {7, 5, \"!!\" },\n" 18164 "};", 18165 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18166 "that really, in any just world, ought to be split over multiple " 18167 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18168 Style)); 18169 } 18170 18171 TEST_F(FormatTest, UnderstandsPragmas) { 18172 verifyFormat("#pragma omp reduction(| : var)"); 18173 verifyFormat("#pragma omp reduction(+ : var)"); 18174 18175 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 18176 "(including parentheses).", 18177 format("#pragma mark Any non-hyphenated or hyphenated string " 18178 "(including parentheses).")); 18179 } 18180 18181 TEST_F(FormatTest, UnderstandPragmaOption) { 18182 verifyFormat("#pragma option -C -A"); 18183 18184 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 18185 } 18186 18187 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 18188 FormatStyle Style = getLLVMStyle(); 18189 Style.ColumnLimit = 20; 18190 18191 // See PR41213 18192 EXPECT_EQ("/*\n" 18193 " *\t9012345\n" 18194 " * /8901\n" 18195 " */", 18196 format("/*\n" 18197 " *\t9012345 /8901\n" 18198 " */", 18199 Style)); 18200 EXPECT_EQ("/*\n" 18201 " *345678\n" 18202 " *\t/8901\n" 18203 " */", 18204 format("/*\n" 18205 " *345678\t/8901\n" 18206 " */", 18207 Style)); 18208 18209 verifyFormat("int a; // the\n" 18210 " // comment", 18211 Style); 18212 EXPECT_EQ("int a; /* first line\n" 18213 " * second\n" 18214 " * line third\n" 18215 " * line\n" 18216 " */", 18217 format("int a; /* first line\n" 18218 " * second\n" 18219 " * line third\n" 18220 " * line\n" 18221 " */", 18222 Style)); 18223 EXPECT_EQ("int a; // first line\n" 18224 " // second\n" 18225 " // line third\n" 18226 " // line", 18227 format("int a; // first line\n" 18228 " // second line\n" 18229 " // third line", 18230 Style)); 18231 18232 Style.PenaltyExcessCharacter = 90; 18233 verifyFormat("int a; // the comment", Style); 18234 EXPECT_EQ("int a; // the comment\n" 18235 " // aaa", 18236 format("int a; // the comment aaa", Style)); 18237 EXPECT_EQ("int a; /* first line\n" 18238 " * second line\n" 18239 " * third line\n" 18240 " */", 18241 format("int a; /* first line\n" 18242 " * second line\n" 18243 " * third line\n" 18244 " */", 18245 Style)); 18246 EXPECT_EQ("int a; // first line\n" 18247 " // second line\n" 18248 " // third line", 18249 format("int a; // first line\n" 18250 " // second line\n" 18251 " // third line", 18252 Style)); 18253 // FIXME: Investigate why this is not getting the same layout as the test 18254 // above. 18255 EXPECT_EQ("int a; /* first line\n" 18256 " * second line\n" 18257 " * third line\n" 18258 " */", 18259 format("int a; /* first line second line third line" 18260 "\n*/", 18261 Style)); 18262 18263 EXPECT_EQ("// foo bar baz bazfoo\n" 18264 "// foo bar foo bar\n", 18265 format("// foo bar baz bazfoo\n" 18266 "// foo bar foo bar\n", 18267 Style)); 18268 EXPECT_EQ("// foo bar baz bazfoo\n" 18269 "// foo bar foo bar\n", 18270 format("// foo bar baz bazfoo\n" 18271 "// foo bar foo bar\n", 18272 Style)); 18273 18274 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18275 // next one. 18276 EXPECT_EQ("// foo bar baz bazfoo\n" 18277 "// bar foo bar\n", 18278 format("// foo bar baz bazfoo bar\n" 18279 "// foo bar\n", 18280 Style)); 18281 18282 EXPECT_EQ("// foo bar baz bazfoo\n" 18283 "// foo bar baz bazfoo\n" 18284 "// bar foo bar\n", 18285 format("// foo bar baz bazfoo\n" 18286 "// foo bar baz bazfoo bar\n" 18287 "// foo bar\n", 18288 Style)); 18289 18290 EXPECT_EQ("// foo bar baz bazfoo\n" 18291 "// foo bar baz bazfoo\n" 18292 "// bar foo bar\n", 18293 format("// foo bar baz bazfoo\n" 18294 "// foo bar baz bazfoo bar\n" 18295 "// foo bar\n", 18296 Style)); 18297 18298 // Make sure we do not keep protruding characters if strict mode reflow is 18299 // cheaper than keeping protruding characters. 18300 Style.ColumnLimit = 21; 18301 EXPECT_EQ( 18302 "// foo foo foo foo\n" 18303 "// foo foo foo foo\n" 18304 "// foo foo foo foo\n", 18305 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18306 18307 EXPECT_EQ("int a = /* long block\n" 18308 " comment */\n" 18309 " 42;", 18310 format("int a = /* long block comment */ 42;", Style)); 18311 } 18312 18313 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18314 for (size_t i = 1; i < Styles.size(); ++i) \ 18315 EXPECT_EQ(Styles[0], Styles[i]) \ 18316 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18317 18318 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18319 SmallVector<FormatStyle, 3> Styles; 18320 Styles.resize(3); 18321 18322 Styles[0] = getLLVMStyle(); 18323 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18324 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18325 EXPECT_ALL_STYLES_EQUAL(Styles); 18326 18327 Styles[0] = getGoogleStyle(); 18328 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18329 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18330 EXPECT_ALL_STYLES_EQUAL(Styles); 18331 18332 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18333 EXPECT_TRUE( 18334 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18335 EXPECT_TRUE( 18336 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18337 EXPECT_ALL_STYLES_EQUAL(Styles); 18338 18339 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18340 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18341 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18342 EXPECT_ALL_STYLES_EQUAL(Styles); 18343 18344 Styles[0] = getMozillaStyle(); 18345 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18346 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18347 EXPECT_ALL_STYLES_EQUAL(Styles); 18348 18349 Styles[0] = getWebKitStyle(); 18350 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18351 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18352 EXPECT_ALL_STYLES_EQUAL(Styles); 18353 18354 Styles[0] = getGNUStyle(); 18355 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18356 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18357 EXPECT_ALL_STYLES_EQUAL(Styles); 18358 18359 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18360 } 18361 18362 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18363 SmallVector<FormatStyle, 8> Styles; 18364 Styles.resize(2); 18365 18366 Styles[0] = getGoogleStyle(); 18367 Styles[1] = getLLVMStyle(); 18368 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18369 EXPECT_ALL_STYLES_EQUAL(Styles); 18370 18371 Styles.resize(5); 18372 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18373 Styles[1] = getLLVMStyle(); 18374 Styles[1].Language = FormatStyle::LK_JavaScript; 18375 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18376 18377 Styles[2] = getLLVMStyle(); 18378 Styles[2].Language = FormatStyle::LK_JavaScript; 18379 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18380 "BasedOnStyle: Google", 18381 &Styles[2]) 18382 .value()); 18383 18384 Styles[3] = getLLVMStyle(); 18385 Styles[3].Language = FormatStyle::LK_JavaScript; 18386 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18387 "Language: JavaScript", 18388 &Styles[3]) 18389 .value()); 18390 18391 Styles[4] = getLLVMStyle(); 18392 Styles[4].Language = FormatStyle::LK_JavaScript; 18393 EXPECT_EQ(0, parseConfiguration("---\n" 18394 "BasedOnStyle: LLVM\n" 18395 "IndentWidth: 123\n" 18396 "---\n" 18397 "BasedOnStyle: Google\n" 18398 "Language: JavaScript", 18399 &Styles[4]) 18400 .value()); 18401 EXPECT_ALL_STYLES_EQUAL(Styles); 18402 } 18403 18404 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18405 Style.FIELD = false; \ 18406 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18407 EXPECT_TRUE(Style.FIELD); \ 18408 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18409 EXPECT_FALSE(Style.FIELD); 18410 18411 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18412 18413 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18414 Style.STRUCT.FIELD = false; \ 18415 EXPECT_EQ(0, \ 18416 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18417 .value()); \ 18418 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18419 EXPECT_EQ(0, \ 18420 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18421 .value()); \ 18422 EXPECT_FALSE(Style.STRUCT.FIELD); 18423 18424 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18425 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18426 18427 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18428 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18429 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18430 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18431 18432 TEST_F(FormatTest, ParsesConfigurationBools) { 18433 FormatStyle Style = {}; 18434 Style.Language = FormatStyle::LK_Cpp; 18435 CHECK_PARSE_BOOL(AlignTrailingComments); 18436 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18437 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18438 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18439 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18440 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18441 CHECK_PARSE_BOOL(BinPackArguments); 18442 CHECK_PARSE_BOOL(BinPackParameters); 18443 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18444 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18445 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18446 CHECK_PARSE_BOOL(BreakStringLiterals); 18447 CHECK_PARSE_BOOL(CompactNamespaces); 18448 CHECK_PARSE_BOOL(DeriveLineEnding); 18449 CHECK_PARSE_BOOL(DerivePointerAlignment); 18450 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18451 CHECK_PARSE_BOOL(DisableFormat); 18452 CHECK_PARSE_BOOL(IndentAccessModifiers); 18453 CHECK_PARSE_BOOL(IndentCaseLabels); 18454 CHECK_PARSE_BOOL(IndentCaseBlocks); 18455 CHECK_PARSE_BOOL(IndentGotoLabels); 18456 CHECK_PARSE_BOOL(IndentRequires); 18457 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18458 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18459 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18460 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18461 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18462 CHECK_PARSE_BOOL(ReflowComments); 18463 CHECK_PARSE_BOOL(SortUsingDeclarations); 18464 CHECK_PARSE_BOOL(SpacesInParentheses); 18465 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18466 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18467 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18468 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18469 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18470 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18471 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18472 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18473 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18474 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18475 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18476 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18477 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18478 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18479 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18480 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18481 CHECK_PARSE_BOOL(UseCRLF); 18482 18483 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18484 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18485 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18486 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18487 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18488 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18489 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18490 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18491 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18492 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18493 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18494 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18495 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18496 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18497 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18498 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18499 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18500 } 18501 18502 #undef CHECK_PARSE_BOOL 18503 18504 TEST_F(FormatTest, ParsesConfiguration) { 18505 FormatStyle Style = {}; 18506 Style.Language = FormatStyle::LK_Cpp; 18507 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18508 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18509 ConstructorInitializerIndentWidth, 1234u); 18510 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18511 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18512 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18513 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18514 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18515 PenaltyBreakBeforeFirstCallParameter, 1234u); 18516 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18517 PenaltyBreakTemplateDeclaration, 1234u); 18518 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18519 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18520 PenaltyReturnTypeOnItsOwnLine, 1234u); 18521 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18522 SpacesBeforeTrailingComments, 1234u); 18523 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18524 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18525 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18526 18527 Style.QualifierAlignment = FormatStyle::QAS_Right; 18528 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 18529 FormatStyle::QAS_Leave); 18530 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 18531 FormatStyle::QAS_Right); 18532 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 18533 FormatStyle::QAS_Left); 18534 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 18535 FormatStyle::QAS_Custom); 18536 18537 Style.QualifierOrder.clear(); 18538 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 18539 std::vector<std::string>({"const", "volatile", "type"})); 18540 Style.QualifierOrder.clear(); 18541 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 18542 std::vector<std::string>({"const", "type"})); 18543 Style.QualifierOrder.clear(); 18544 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 18545 std::vector<std::string>({"volatile", "type"})); 18546 18547 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18548 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18549 FormatStyle::ACS_None); 18550 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18551 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18552 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18553 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18554 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18555 AlignConsecutiveAssignments, 18556 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18557 // For backwards compability, false / true should still parse 18558 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18559 FormatStyle::ACS_None); 18560 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18561 FormatStyle::ACS_Consecutive); 18562 18563 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18564 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18565 FormatStyle::ACS_None); 18566 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18567 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18568 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18569 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18570 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18571 AlignConsecutiveBitFields, 18572 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18573 // For backwards compability, false / true should still parse 18574 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18575 FormatStyle::ACS_None); 18576 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18577 FormatStyle::ACS_Consecutive); 18578 18579 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18580 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18581 FormatStyle::ACS_None); 18582 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18583 FormatStyle::ACS_Consecutive); 18584 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18585 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18586 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18587 AlignConsecutiveMacros, 18588 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18589 // For backwards compability, false / true should still parse 18590 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18591 FormatStyle::ACS_None); 18592 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18593 FormatStyle::ACS_Consecutive); 18594 18595 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18596 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18597 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18598 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18599 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18600 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18601 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18602 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18603 AlignConsecutiveDeclarations, 18604 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18605 // For backwards compability, false / true should still parse 18606 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18607 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18608 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18609 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18610 18611 Style.PointerAlignment = FormatStyle::PAS_Middle; 18612 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18613 FormatStyle::PAS_Left); 18614 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18615 FormatStyle::PAS_Right); 18616 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18617 FormatStyle::PAS_Middle); 18618 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18619 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18620 FormatStyle::RAS_Pointer); 18621 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18622 FormatStyle::RAS_Left); 18623 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18624 FormatStyle::RAS_Right); 18625 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18626 FormatStyle::RAS_Middle); 18627 // For backward compatibility: 18628 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18629 FormatStyle::PAS_Left); 18630 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18631 FormatStyle::PAS_Right); 18632 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18633 FormatStyle::PAS_Middle); 18634 18635 Style.Standard = FormatStyle::LS_Auto; 18636 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18637 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18638 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18639 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18640 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18641 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18642 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18643 // Legacy aliases: 18644 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18645 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18646 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18647 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18648 18649 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18650 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18651 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18652 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18653 FormatStyle::BOS_None); 18654 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18655 FormatStyle::BOS_All); 18656 // For backward compatibility: 18657 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18658 FormatStyle::BOS_None); 18659 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18660 FormatStyle::BOS_All); 18661 18662 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18663 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18664 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18665 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18666 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18667 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18668 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18669 // For backward compatibility: 18670 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18671 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18672 18673 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18674 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18675 FormatStyle::BILS_AfterComma); 18676 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18677 FormatStyle::BILS_BeforeComma); 18678 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18679 FormatStyle::BILS_AfterColon); 18680 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18681 FormatStyle::BILS_BeforeColon); 18682 // For backward compatibility: 18683 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18684 FormatStyle::BILS_BeforeComma); 18685 18686 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 18687 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 18688 FormatStyle::PCIS_Never); 18689 CHECK_PARSE("PackConstructorInitializers: BinPack", 18690 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 18691 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 18692 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18693 CHECK_PARSE("PackConstructorInitializers: NextLine", 18694 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 18695 // For backward compatibility: 18696 CHECK_PARSE("BasedOnStyle: Google\n" 18697 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18698 "AllowAllConstructorInitializersOnNextLine: false", 18699 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18700 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 18701 CHECK_PARSE("BasedOnStyle: Google\n" 18702 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 18703 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 18704 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18705 "AllowAllConstructorInitializersOnNextLine: true", 18706 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 18707 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 18708 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18709 "AllowAllConstructorInitializersOnNextLine: false", 18710 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18711 18712 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18713 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18714 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18715 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18716 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18717 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18718 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18719 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18720 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18721 18722 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18723 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18724 FormatStyle::BAS_Align); 18725 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18726 FormatStyle::BAS_DontAlign); 18727 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18728 FormatStyle::BAS_AlwaysBreak); 18729 // For backward compatibility: 18730 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18731 FormatStyle::BAS_DontAlign); 18732 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18733 FormatStyle::BAS_Align); 18734 18735 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18736 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18737 FormatStyle::ENAS_DontAlign); 18738 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18739 FormatStyle::ENAS_Left); 18740 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18741 FormatStyle::ENAS_Right); 18742 // For backward compatibility: 18743 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18744 FormatStyle::ENAS_Left); 18745 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18746 FormatStyle::ENAS_Right); 18747 18748 Style.AlignOperands = FormatStyle::OAS_Align; 18749 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18750 FormatStyle::OAS_DontAlign); 18751 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18752 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18753 FormatStyle::OAS_AlignAfterOperator); 18754 // For backward compatibility: 18755 CHECK_PARSE("AlignOperands: false", AlignOperands, 18756 FormatStyle::OAS_DontAlign); 18757 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18758 18759 Style.UseTab = FormatStyle::UT_ForIndentation; 18760 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18761 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18762 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18763 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18764 FormatStyle::UT_ForContinuationAndIndentation); 18765 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18766 FormatStyle::UT_AlignWithSpaces); 18767 // For backward compatibility: 18768 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18769 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18770 18771 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18772 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18773 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18774 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18775 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18776 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18777 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18778 // For backward compatibility: 18779 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18780 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18781 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18782 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18783 18784 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18785 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18786 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18787 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18788 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18789 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18790 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18791 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18792 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18793 // For backward compatibility: 18794 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18795 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18796 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18797 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18798 18799 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 18800 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 18801 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 18802 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 18803 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 18804 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 18805 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 18806 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 18807 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 18808 18809 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 18810 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 18811 FormatStyle::SBPO_Never); 18812 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 18813 FormatStyle::SBPO_Always); 18814 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 18815 FormatStyle::SBPO_ControlStatements); 18816 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 18817 SpaceBeforeParens, 18818 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18819 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 18820 FormatStyle::SBPO_NonEmptyParentheses); 18821 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 18822 FormatStyle::SBPO_Custom); 18823 // For backward compatibility: 18824 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 18825 FormatStyle::SBPO_Never); 18826 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 18827 FormatStyle::SBPO_ControlStatements); 18828 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 18829 SpaceBeforeParens, 18830 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18831 18832 Style.ColumnLimit = 123; 18833 FormatStyle BaseStyle = getLLVMStyle(); 18834 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 18835 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 18836 18837 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 18838 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 18839 FormatStyle::BS_Attach); 18840 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 18841 FormatStyle::BS_Linux); 18842 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 18843 FormatStyle::BS_Mozilla); 18844 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 18845 FormatStyle::BS_Stroustrup); 18846 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 18847 FormatStyle::BS_Allman); 18848 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 18849 FormatStyle::BS_Whitesmiths); 18850 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 18851 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 18852 FormatStyle::BS_WebKit); 18853 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 18854 FormatStyle::BS_Custom); 18855 18856 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 18857 CHECK_PARSE("BraceWrapping:\n" 18858 " AfterControlStatement: MultiLine", 18859 BraceWrapping.AfterControlStatement, 18860 FormatStyle::BWACS_MultiLine); 18861 CHECK_PARSE("BraceWrapping:\n" 18862 " AfterControlStatement: Always", 18863 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18864 CHECK_PARSE("BraceWrapping:\n" 18865 " AfterControlStatement: Never", 18866 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18867 // For backward compatibility: 18868 CHECK_PARSE("BraceWrapping:\n" 18869 " AfterControlStatement: true", 18870 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18871 CHECK_PARSE("BraceWrapping:\n" 18872 " AfterControlStatement: false", 18873 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18874 18875 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 18876 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 18877 FormatStyle::RTBS_None); 18878 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 18879 FormatStyle::RTBS_All); 18880 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 18881 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 18882 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 18883 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 18884 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 18885 AlwaysBreakAfterReturnType, 18886 FormatStyle::RTBS_TopLevelDefinitions); 18887 18888 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 18889 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 18890 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 18891 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 18892 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18893 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 18894 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18895 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 18896 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18897 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 18898 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18899 18900 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 18901 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 18902 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 18903 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 18904 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 18905 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 18906 AlwaysBreakAfterDefinitionReturnType, 18907 FormatStyle::DRTBS_TopLevel); 18908 18909 Style.NamespaceIndentation = FormatStyle::NI_All; 18910 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 18911 FormatStyle::NI_None); 18912 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 18913 FormatStyle::NI_Inner); 18914 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 18915 FormatStyle::NI_All); 18916 18917 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 18918 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 18919 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18920 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 18921 AllowShortIfStatementsOnASingleLine, 18922 FormatStyle::SIS_WithoutElse); 18923 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 18924 AllowShortIfStatementsOnASingleLine, 18925 FormatStyle::SIS_OnlyFirstIf); 18926 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 18927 AllowShortIfStatementsOnASingleLine, 18928 FormatStyle::SIS_AllIfsAndElse); 18929 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 18930 AllowShortIfStatementsOnASingleLine, 18931 FormatStyle::SIS_OnlyFirstIf); 18932 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 18933 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18934 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 18935 AllowShortIfStatementsOnASingleLine, 18936 FormatStyle::SIS_WithoutElse); 18937 18938 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 18939 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 18940 FormatStyle::IEBS_AfterExternBlock); 18941 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 18942 FormatStyle::IEBS_Indent); 18943 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 18944 FormatStyle::IEBS_NoIndent); 18945 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 18946 FormatStyle::IEBS_Indent); 18947 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 18948 FormatStyle::IEBS_NoIndent); 18949 18950 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 18951 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 18952 FormatStyle::BFCS_Both); 18953 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 18954 FormatStyle::BFCS_None); 18955 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 18956 FormatStyle::BFCS_Before); 18957 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 18958 FormatStyle::BFCS_After); 18959 18960 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 18961 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 18962 FormatStyle::SJSIO_After); 18963 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 18964 FormatStyle::SJSIO_Before); 18965 18966 // FIXME: This is required because parsing a configuration simply overwrites 18967 // the first N elements of the list instead of resetting it. 18968 Style.ForEachMacros.clear(); 18969 std::vector<std::string> BoostForeach; 18970 BoostForeach.push_back("BOOST_FOREACH"); 18971 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 18972 std::vector<std::string> BoostAndQForeach; 18973 BoostAndQForeach.push_back("BOOST_FOREACH"); 18974 BoostAndQForeach.push_back("Q_FOREACH"); 18975 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 18976 BoostAndQForeach); 18977 18978 Style.IfMacros.clear(); 18979 std::vector<std::string> CustomIfs; 18980 CustomIfs.push_back("MYIF"); 18981 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 18982 18983 Style.AttributeMacros.clear(); 18984 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 18985 std::vector<std::string>{"__capability"}); 18986 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 18987 std::vector<std::string>({"attr1", "attr2"})); 18988 18989 Style.StatementAttributeLikeMacros.clear(); 18990 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 18991 StatementAttributeLikeMacros, 18992 std::vector<std::string>({"emit", "Q_EMIT"})); 18993 18994 Style.StatementMacros.clear(); 18995 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 18996 std::vector<std::string>{"QUNUSED"}); 18997 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 18998 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 18999 19000 Style.NamespaceMacros.clear(); 19001 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 19002 std::vector<std::string>{"TESTSUITE"}); 19003 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 19004 std::vector<std::string>({"TESTSUITE", "SUITE"})); 19005 19006 Style.WhitespaceSensitiveMacros.clear(); 19007 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 19008 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19009 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 19010 WhitespaceSensitiveMacros, 19011 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19012 Style.WhitespaceSensitiveMacros.clear(); 19013 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 19014 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19015 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 19016 WhitespaceSensitiveMacros, 19017 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19018 19019 Style.IncludeStyle.IncludeCategories.clear(); 19020 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 19021 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 19022 CHECK_PARSE("IncludeCategories:\n" 19023 " - Regex: abc/.*\n" 19024 " Priority: 2\n" 19025 " - Regex: .*\n" 19026 " Priority: 1\n" 19027 " CaseSensitive: true\n", 19028 IncludeStyle.IncludeCategories, ExpectedCategories); 19029 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 19030 "abc$"); 19031 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 19032 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 19033 19034 Style.SortIncludes = FormatStyle::SI_Never; 19035 CHECK_PARSE("SortIncludes: true", SortIncludes, 19036 FormatStyle::SI_CaseSensitive); 19037 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 19038 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 19039 FormatStyle::SI_CaseInsensitive); 19040 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 19041 FormatStyle::SI_CaseSensitive); 19042 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 19043 19044 Style.RawStringFormats.clear(); 19045 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 19046 { 19047 FormatStyle::LK_TextProto, 19048 {"pb", "proto"}, 19049 {"PARSE_TEXT_PROTO"}, 19050 /*CanonicalDelimiter=*/"", 19051 "llvm", 19052 }, 19053 { 19054 FormatStyle::LK_Cpp, 19055 {"cc", "cpp"}, 19056 {"C_CODEBLOCK", "CPPEVAL"}, 19057 /*CanonicalDelimiter=*/"cc", 19058 /*BasedOnStyle=*/"", 19059 }, 19060 }; 19061 19062 CHECK_PARSE("RawStringFormats:\n" 19063 " - Language: TextProto\n" 19064 " Delimiters:\n" 19065 " - 'pb'\n" 19066 " - 'proto'\n" 19067 " EnclosingFunctions:\n" 19068 " - 'PARSE_TEXT_PROTO'\n" 19069 " BasedOnStyle: llvm\n" 19070 " - Language: Cpp\n" 19071 " Delimiters:\n" 19072 " - 'cc'\n" 19073 " - 'cpp'\n" 19074 " EnclosingFunctions:\n" 19075 " - 'C_CODEBLOCK'\n" 19076 " - 'CPPEVAL'\n" 19077 " CanonicalDelimiter: 'cc'", 19078 RawStringFormats, ExpectedRawStringFormats); 19079 19080 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19081 " Minimum: 0\n" 19082 " Maximum: 0", 19083 SpacesInLineCommentPrefix.Minimum, 0u); 19084 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 19085 Style.SpacesInLineCommentPrefix.Minimum = 1; 19086 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19087 " Minimum: 2", 19088 SpacesInLineCommentPrefix.Minimum, 0u); 19089 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19090 " Maximum: -1", 19091 SpacesInLineCommentPrefix.Maximum, -1u); 19092 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19093 " Minimum: 2", 19094 SpacesInLineCommentPrefix.Minimum, 2u); 19095 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19096 " Maximum: 1", 19097 SpacesInLineCommentPrefix.Maximum, 1u); 19098 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 19099 19100 Style.SpacesInAngles = FormatStyle::SIAS_Always; 19101 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 19102 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 19103 FormatStyle::SIAS_Always); 19104 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 19105 // For backward compatibility: 19106 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 19107 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 19108 } 19109 19110 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 19111 FormatStyle Style = {}; 19112 Style.Language = FormatStyle::LK_Cpp; 19113 CHECK_PARSE("Language: Cpp\n" 19114 "IndentWidth: 12", 19115 IndentWidth, 12u); 19116 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 19117 "IndentWidth: 34", 19118 &Style), 19119 ParseError::Unsuitable); 19120 FormatStyle BinPackedTCS = {}; 19121 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 19122 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 19123 "InsertTrailingCommas: Wrapped", 19124 &BinPackedTCS), 19125 ParseError::BinPackTrailingCommaConflict); 19126 EXPECT_EQ(12u, Style.IndentWidth); 19127 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19128 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19129 19130 Style.Language = FormatStyle::LK_JavaScript; 19131 CHECK_PARSE("Language: JavaScript\n" 19132 "IndentWidth: 12", 19133 IndentWidth, 12u); 19134 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 19135 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 19136 "IndentWidth: 34", 19137 &Style), 19138 ParseError::Unsuitable); 19139 EXPECT_EQ(23u, Style.IndentWidth); 19140 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19141 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19142 19143 CHECK_PARSE("BasedOnStyle: LLVM\n" 19144 "IndentWidth: 67", 19145 IndentWidth, 67u); 19146 19147 CHECK_PARSE("---\n" 19148 "Language: JavaScript\n" 19149 "IndentWidth: 12\n" 19150 "---\n" 19151 "Language: Cpp\n" 19152 "IndentWidth: 34\n" 19153 "...\n", 19154 IndentWidth, 12u); 19155 19156 Style.Language = FormatStyle::LK_Cpp; 19157 CHECK_PARSE("---\n" 19158 "Language: JavaScript\n" 19159 "IndentWidth: 12\n" 19160 "---\n" 19161 "Language: Cpp\n" 19162 "IndentWidth: 34\n" 19163 "...\n", 19164 IndentWidth, 34u); 19165 CHECK_PARSE("---\n" 19166 "IndentWidth: 78\n" 19167 "---\n" 19168 "Language: JavaScript\n" 19169 "IndentWidth: 56\n" 19170 "...\n", 19171 IndentWidth, 78u); 19172 19173 Style.ColumnLimit = 123; 19174 Style.IndentWidth = 234; 19175 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 19176 Style.TabWidth = 345; 19177 EXPECT_FALSE(parseConfiguration("---\n" 19178 "IndentWidth: 456\n" 19179 "BreakBeforeBraces: Allman\n" 19180 "---\n" 19181 "Language: JavaScript\n" 19182 "IndentWidth: 111\n" 19183 "TabWidth: 111\n" 19184 "---\n" 19185 "Language: Cpp\n" 19186 "BreakBeforeBraces: Stroustrup\n" 19187 "TabWidth: 789\n" 19188 "...\n", 19189 &Style)); 19190 EXPECT_EQ(123u, Style.ColumnLimit); 19191 EXPECT_EQ(456u, Style.IndentWidth); 19192 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 19193 EXPECT_EQ(789u, Style.TabWidth); 19194 19195 EXPECT_EQ(parseConfiguration("---\n" 19196 "Language: JavaScript\n" 19197 "IndentWidth: 56\n" 19198 "---\n" 19199 "IndentWidth: 78\n" 19200 "...\n", 19201 &Style), 19202 ParseError::Error); 19203 EXPECT_EQ(parseConfiguration("---\n" 19204 "Language: JavaScript\n" 19205 "IndentWidth: 56\n" 19206 "---\n" 19207 "Language: JavaScript\n" 19208 "IndentWidth: 78\n" 19209 "...\n", 19210 &Style), 19211 ParseError::Error); 19212 19213 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19214 } 19215 19216 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 19217 FormatStyle Style = {}; 19218 Style.Language = FormatStyle::LK_JavaScript; 19219 Style.BreakBeforeTernaryOperators = true; 19220 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 19221 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19222 19223 Style.BreakBeforeTernaryOperators = true; 19224 EXPECT_EQ(0, parseConfiguration("---\n" 19225 "BasedOnStyle: Google\n" 19226 "---\n" 19227 "Language: JavaScript\n" 19228 "IndentWidth: 76\n" 19229 "...\n", 19230 &Style) 19231 .value()); 19232 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19233 EXPECT_EQ(76u, Style.IndentWidth); 19234 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19235 } 19236 19237 TEST_F(FormatTest, ConfigurationRoundTripTest) { 19238 FormatStyle Style = getLLVMStyle(); 19239 std::string YAML = configurationAsText(Style); 19240 FormatStyle ParsedStyle = {}; 19241 ParsedStyle.Language = FormatStyle::LK_Cpp; 19242 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 19243 EXPECT_EQ(Style, ParsedStyle); 19244 } 19245 19246 TEST_F(FormatTest, WorksFor8bitEncodings) { 19247 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 19248 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 19249 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 19250 "\"\xef\xee\xf0\xf3...\"", 19251 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 19252 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 19253 "\xef\xee\xf0\xf3...\"", 19254 getLLVMStyleWithColumns(12))); 19255 } 19256 19257 TEST_F(FormatTest, HandlesUTF8BOM) { 19258 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 19259 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 19260 format("\xef\xbb\xbf#include <iostream>")); 19261 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 19262 format("\xef\xbb\xbf\n#include <iostream>")); 19263 } 19264 19265 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 19266 #if !defined(_MSC_VER) 19267 19268 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 19269 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 19270 getLLVMStyleWithColumns(35)); 19271 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 19272 getLLVMStyleWithColumns(31)); 19273 verifyFormat("// Однажды в студёную зимнюю пору...", 19274 getLLVMStyleWithColumns(36)); 19275 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 19276 verifyFormat("/* Однажды в студёную зимнюю пору... */", 19277 getLLVMStyleWithColumns(39)); 19278 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 19279 getLLVMStyleWithColumns(35)); 19280 } 19281 19282 TEST_F(FormatTest, SplitsUTF8Strings) { 19283 // Non-printable characters' width is currently considered to be the length in 19284 // bytes in UTF8. The characters can be displayed in very different manner 19285 // (zero-width, single width with a substitution glyph, expanded to their code 19286 // (e.g. "<8d>"), so there's no single correct way to handle them. 19287 EXPECT_EQ("\"aaaaÄ\"\n" 19288 "\"\xc2\x8d\";", 19289 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19290 EXPECT_EQ("\"aaaaaaaÄ\"\n" 19291 "\"\xc2\x8d\";", 19292 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19293 EXPECT_EQ("\"Однажды, в \"\n" 19294 "\"студёную \"\n" 19295 "\"зимнюю \"\n" 19296 "\"пору,\"", 19297 format("\"Однажды, в студёную зимнюю пору,\"", 19298 getLLVMStyleWithColumns(13))); 19299 EXPECT_EQ( 19300 "\"一 二 三 \"\n" 19301 "\"四 五六 \"\n" 19302 "\"七 八 九 \"\n" 19303 "\"十\"", 19304 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 19305 EXPECT_EQ("\"一\t\"\n" 19306 "\"二 \t\"\n" 19307 "\"三 四 \"\n" 19308 "\"五\t\"\n" 19309 "\"六 \t\"\n" 19310 "\"七 \"\n" 19311 "\"八九十\tqq\"", 19312 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19313 getLLVMStyleWithColumns(11))); 19314 19315 // UTF8 character in an escape sequence. 19316 EXPECT_EQ("\"aaaaaa\"\n" 19317 "\"\\\xC2\x8D\"", 19318 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19319 } 19320 19321 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19322 EXPECT_EQ("const char *sssss =\n" 19323 " \"一二三四五六七八\\\n" 19324 " 九 十\";", 19325 format("const char *sssss = \"一二三四五六七八\\\n" 19326 " 九 十\";", 19327 getLLVMStyleWithColumns(30))); 19328 } 19329 19330 TEST_F(FormatTest, SplitsUTF8LineComments) { 19331 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19332 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19333 EXPECT_EQ("// Я из лесу\n" 19334 "// вышел; был\n" 19335 "// сильный\n" 19336 "// мороз.", 19337 format("// Я из лесу вышел; был сильный мороз.", 19338 getLLVMStyleWithColumns(13))); 19339 EXPECT_EQ("// 一二三\n" 19340 "// 四五六七\n" 19341 "// 八 九\n" 19342 "// 十", 19343 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19344 } 19345 19346 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19347 EXPECT_EQ("/* Гляжу,\n" 19348 " * поднимается\n" 19349 " * медленно в\n" 19350 " * гору\n" 19351 " * Лошадка,\n" 19352 " * везущая\n" 19353 " * хворосту\n" 19354 " * воз. */", 19355 format("/* Гляжу, поднимается медленно в гору\n" 19356 " * Лошадка, везущая хворосту воз. */", 19357 getLLVMStyleWithColumns(13))); 19358 EXPECT_EQ( 19359 "/* 一二三\n" 19360 " * 四五六七\n" 19361 " * 八 九\n" 19362 " * 十 */", 19363 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19364 EXPECT_EQ("/* \n" 19365 " * \n" 19366 " * - */", 19367 format("/* - */", getLLVMStyleWithColumns(12))); 19368 } 19369 19370 #endif // _MSC_VER 19371 19372 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19373 FormatStyle Style = getLLVMStyle(); 19374 19375 Style.ConstructorInitializerIndentWidth = 4; 19376 verifyFormat( 19377 "SomeClass::Constructor()\n" 19378 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19379 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19380 Style); 19381 19382 Style.ConstructorInitializerIndentWidth = 2; 19383 verifyFormat( 19384 "SomeClass::Constructor()\n" 19385 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19386 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19387 Style); 19388 19389 Style.ConstructorInitializerIndentWidth = 0; 19390 verifyFormat( 19391 "SomeClass::Constructor()\n" 19392 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19393 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19394 Style); 19395 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19396 verifyFormat( 19397 "SomeLongTemplateVariableName<\n" 19398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19399 Style); 19400 verifyFormat("bool smaller = 1 < " 19401 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19402 " " 19403 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19404 Style); 19405 19406 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19407 verifyFormat("SomeClass::Constructor() :\n" 19408 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19409 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19410 Style); 19411 } 19412 19413 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19414 FormatStyle Style = getLLVMStyle(); 19415 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19416 Style.ConstructorInitializerIndentWidth = 4; 19417 verifyFormat("SomeClass::Constructor()\n" 19418 " : a(a)\n" 19419 " , b(b)\n" 19420 " , c(c) {}", 19421 Style); 19422 verifyFormat("SomeClass::Constructor()\n" 19423 " : a(a) {}", 19424 Style); 19425 19426 Style.ColumnLimit = 0; 19427 verifyFormat("SomeClass::Constructor()\n" 19428 " : a(a) {}", 19429 Style); 19430 verifyFormat("SomeClass::Constructor() noexcept\n" 19431 " : a(a) {}", 19432 Style); 19433 verifyFormat("SomeClass::Constructor()\n" 19434 " : a(a)\n" 19435 " , b(b)\n" 19436 " , c(c) {}", 19437 Style); 19438 verifyFormat("SomeClass::Constructor()\n" 19439 " : a(a) {\n" 19440 " foo();\n" 19441 " bar();\n" 19442 "}", 19443 Style); 19444 19445 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19446 verifyFormat("SomeClass::Constructor()\n" 19447 " : a(a)\n" 19448 " , b(b)\n" 19449 " , c(c) {\n}", 19450 Style); 19451 verifyFormat("SomeClass::Constructor()\n" 19452 " : a(a) {\n}", 19453 Style); 19454 19455 Style.ColumnLimit = 80; 19456 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19457 Style.ConstructorInitializerIndentWidth = 2; 19458 verifyFormat("SomeClass::Constructor()\n" 19459 " : a(a)\n" 19460 " , b(b)\n" 19461 " , c(c) {}", 19462 Style); 19463 19464 Style.ConstructorInitializerIndentWidth = 0; 19465 verifyFormat("SomeClass::Constructor()\n" 19466 ": a(a)\n" 19467 ", b(b)\n" 19468 ", c(c) {}", 19469 Style); 19470 19471 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19472 Style.ConstructorInitializerIndentWidth = 4; 19473 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19474 verifyFormat( 19475 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19476 Style); 19477 verifyFormat( 19478 "SomeClass::Constructor()\n" 19479 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19480 Style); 19481 Style.ConstructorInitializerIndentWidth = 4; 19482 Style.ColumnLimit = 60; 19483 verifyFormat("SomeClass::Constructor()\n" 19484 " : aaaaaaaa(aaaaaaaa)\n" 19485 " , aaaaaaaa(aaaaaaaa)\n" 19486 " , aaaaaaaa(aaaaaaaa) {}", 19487 Style); 19488 } 19489 19490 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 19491 FormatStyle Style = getLLVMStyle(); 19492 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19493 Style.ConstructorInitializerIndentWidth = 4; 19494 verifyFormat("SomeClass::Constructor()\n" 19495 " : a{a}\n" 19496 " , b{b} {}", 19497 Style); 19498 verifyFormat("SomeClass::Constructor()\n" 19499 " : a{a}\n" 19500 "#if CONDITION\n" 19501 " , b{b}\n" 19502 "#endif\n" 19503 "{\n}", 19504 Style); 19505 Style.ConstructorInitializerIndentWidth = 2; 19506 verifyFormat("SomeClass::Constructor()\n" 19507 "#if CONDITION\n" 19508 " : a{a}\n" 19509 "#endif\n" 19510 " , b{b}\n" 19511 " , c{c} {\n}", 19512 Style); 19513 Style.ConstructorInitializerIndentWidth = 0; 19514 verifyFormat("SomeClass::Constructor()\n" 19515 ": a{a}\n" 19516 "#ifdef CONDITION\n" 19517 ", b{b}\n" 19518 "#else\n" 19519 ", c{c}\n" 19520 "#endif\n" 19521 ", d{d} {\n}", 19522 Style); 19523 Style.ConstructorInitializerIndentWidth = 4; 19524 verifyFormat("SomeClass::Constructor()\n" 19525 " : a{a}\n" 19526 "#if WINDOWS\n" 19527 "#if DEBUG\n" 19528 " , b{0}\n" 19529 "#else\n" 19530 " , b{1}\n" 19531 "#endif\n" 19532 "#else\n" 19533 "#if DEBUG\n" 19534 " , b{2}\n" 19535 "#else\n" 19536 " , b{3}\n" 19537 "#endif\n" 19538 "#endif\n" 19539 "{\n}", 19540 Style); 19541 verifyFormat("SomeClass::Constructor()\n" 19542 " : a{a}\n" 19543 "#if WINDOWS\n" 19544 " , b{0}\n" 19545 "#if DEBUG\n" 19546 " , c{0}\n" 19547 "#else\n" 19548 " , c{1}\n" 19549 "#endif\n" 19550 "#else\n" 19551 "#if DEBUG\n" 19552 " , c{2}\n" 19553 "#else\n" 19554 " , c{3}\n" 19555 "#endif\n" 19556 " , b{1}\n" 19557 "#endif\n" 19558 "{\n}", 19559 Style); 19560 } 19561 19562 TEST_F(FormatTest, Destructors) { 19563 verifyFormat("void F(int &i) { i.~int(); }"); 19564 verifyFormat("void F(int &i) { i->~int(); }"); 19565 } 19566 19567 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19568 FormatStyle Style = getWebKitStyle(); 19569 19570 // Don't indent in outer namespaces. 19571 verifyFormat("namespace outer {\n" 19572 "int i;\n" 19573 "namespace inner {\n" 19574 " int i;\n" 19575 "} // namespace inner\n" 19576 "} // namespace outer\n" 19577 "namespace other_outer {\n" 19578 "int i;\n" 19579 "}", 19580 Style); 19581 19582 // Don't indent case labels. 19583 verifyFormat("switch (variable) {\n" 19584 "case 1:\n" 19585 "case 2:\n" 19586 " doSomething();\n" 19587 " break;\n" 19588 "default:\n" 19589 " ++variable;\n" 19590 "}", 19591 Style); 19592 19593 // Wrap before binary operators. 19594 EXPECT_EQ("void f()\n" 19595 "{\n" 19596 " if (aaaaaaaaaaaaaaaa\n" 19597 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19598 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19599 " return;\n" 19600 "}", 19601 format("void f() {\n" 19602 "if (aaaaaaaaaaaaaaaa\n" 19603 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19604 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19605 "return;\n" 19606 "}", 19607 Style)); 19608 19609 // Allow functions on a single line. 19610 verifyFormat("void f() { return; }", Style); 19611 19612 // Allow empty blocks on a single line and insert a space in empty blocks. 19613 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19614 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19615 // However, don't merge non-empty short loops. 19616 EXPECT_EQ("while (true) {\n" 19617 " continue;\n" 19618 "}", 19619 format("while (true) { continue; }", Style)); 19620 19621 // Constructor initializers are formatted one per line with the "," on the 19622 // new line. 19623 verifyFormat("Constructor()\n" 19624 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19625 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19626 " aaaaaaaaaaaaaa)\n" 19627 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19628 "{\n" 19629 "}", 19630 Style); 19631 verifyFormat("SomeClass::Constructor()\n" 19632 " : a(a)\n" 19633 "{\n" 19634 "}", 19635 Style); 19636 EXPECT_EQ("SomeClass::Constructor()\n" 19637 " : a(a)\n" 19638 "{\n" 19639 "}", 19640 format("SomeClass::Constructor():a(a){}", Style)); 19641 verifyFormat("SomeClass::Constructor()\n" 19642 " : a(a)\n" 19643 " , b(b)\n" 19644 " , c(c)\n" 19645 "{\n" 19646 "}", 19647 Style); 19648 verifyFormat("SomeClass::Constructor()\n" 19649 " : a(a)\n" 19650 "{\n" 19651 " foo();\n" 19652 " bar();\n" 19653 "}", 19654 Style); 19655 19656 // Access specifiers should be aligned left. 19657 verifyFormat("class C {\n" 19658 "public:\n" 19659 " int i;\n" 19660 "};", 19661 Style); 19662 19663 // Do not align comments. 19664 verifyFormat("int a; // Do not\n" 19665 "double b; // align comments.", 19666 Style); 19667 19668 // Do not align operands. 19669 EXPECT_EQ("ASSERT(aaaa\n" 19670 " || bbbb);", 19671 format("ASSERT ( aaaa\n||bbbb);", Style)); 19672 19673 // Accept input's line breaks. 19674 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19675 " || bbbbbbbbbbbbbbb) {\n" 19676 " i++;\n" 19677 "}", 19678 format("if (aaaaaaaaaaaaaaa\n" 19679 "|| bbbbbbbbbbbbbbb) { i++; }", 19680 Style)); 19681 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19682 " i++;\n" 19683 "}", 19684 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19685 19686 // Don't automatically break all macro definitions (llvm.org/PR17842). 19687 verifyFormat("#define aNumber 10", Style); 19688 // However, generally keep the line breaks that the user authored. 19689 EXPECT_EQ("#define aNumber \\\n" 19690 " 10", 19691 format("#define aNumber \\\n" 19692 " 10", 19693 Style)); 19694 19695 // Keep empty and one-element array literals on a single line. 19696 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19697 " copyItems:YES];", 19698 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19699 "copyItems:YES];", 19700 Style)); 19701 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19702 " copyItems:YES];", 19703 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19704 " copyItems:YES];", 19705 Style)); 19706 // FIXME: This does not seem right, there should be more indentation before 19707 // the array literal's entries. Nested blocks have the same problem. 19708 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19709 " @\"a\",\n" 19710 " @\"a\"\n" 19711 "]\n" 19712 " copyItems:YES];", 19713 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19714 " @\"a\",\n" 19715 " @\"a\"\n" 19716 " ]\n" 19717 " copyItems:YES];", 19718 Style)); 19719 EXPECT_EQ( 19720 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19721 " copyItems:YES];", 19722 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19723 " copyItems:YES];", 19724 Style)); 19725 19726 verifyFormat("[self.a b:c c:d];", Style); 19727 EXPECT_EQ("[self.a b:c\n" 19728 " c:d];", 19729 format("[self.a b:c\n" 19730 "c:d];", 19731 Style)); 19732 } 19733 19734 TEST_F(FormatTest, FormatsLambdas) { 19735 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19736 verifyFormat( 19737 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19738 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19739 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19740 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19741 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19742 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19743 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19744 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19745 verifyFormat("int x = f(*+[] {});"); 19746 verifyFormat("void f() {\n" 19747 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19748 "}\n"); 19749 verifyFormat("void f() {\n" 19750 " other(x.begin(), //\n" 19751 " x.end(), //\n" 19752 " [&](int, int) { return 1; });\n" 19753 "}\n"); 19754 verifyFormat("void f() {\n" 19755 " other.other.other.other.other(\n" 19756 " x.begin(), x.end(),\n" 19757 " [something, rather](int, int, int, int, int, int, int) { " 19758 "return 1; });\n" 19759 "}\n"); 19760 verifyFormat( 19761 "void f() {\n" 19762 " other.other.other.other.other(\n" 19763 " x.begin(), x.end(),\n" 19764 " [something, rather](int, int, int, int, int, int, int) {\n" 19765 " //\n" 19766 " });\n" 19767 "}\n"); 19768 verifyFormat("SomeFunction([]() { // A cool function...\n" 19769 " return 43;\n" 19770 "});"); 19771 EXPECT_EQ("SomeFunction([]() {\n" 19772 "#define A a\n" 19773 " return 43;\n" 19774 "});", 19775 format("SomeFunction([](){\n" 19776 "#define A a\n" 19777 "return 43;\n" 19778 "});")); 19779 verifyFormat("void f() {\n" 19780 " SomeFunction([](decltype(x), A *a) {});\n" 19781 " SomeFunction([](typeof(x), A *a) {});\n" 19782 " SomeFunction([](_Atomic(x), A *a) {});\n" 19783 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19784 "}"); 19785 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19786 " [](const aaaaaaaaaa &a) { return a; });"); 19787 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19788 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19789 "});"); 19790 verifyFormat("Constructor()\n" 19791 " : Field([] { // comment\n" 19792 " int i;\n" 19793 " }) {}"); 19794 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19795 " return some_parameter.size();\n" 19796 "};"); 19797 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 19798 " [](const string &s) { return s; };"); 19799 verifyFormat("int i = aaaaaa ? 1 //\n" 19800 " : [] {\n" 19801 " return 2; //\n" 19802 " }();"); 19803 verifyFormat("llvm::errs() << \"number of twos is \"\n" 19804 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 19805 " return x == 2; // force break\n" 19806 " });"); 19807 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19808 " [=](int iiiiiiiiiiii) {\n" 19809 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 19810 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 19811 " });", 19812 getLLVMStyleWithColumns(60)); 19813 19814 verifyFormat("SomeFunction({[&] {\n" 19815 " // comment\n" 19816 " },\n" 19817 " [&] {\n" 19818 " // comment\n" 19819 " }});"); 19820 verifyFormat("SomeFunction({[&] {\n" 19821 " // comment\n" 19822 "}});"); 19823 verifyFormat( 19824 "virtual aaaaaaaaaaaaaaaa(\n" 19825 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 19826 " aaaaa aaaaaaaaa);"); 19827 19828 // Lambdas with return types. 19829 verifyFormat("int c = []() -> int { return 2; }();\n"); 19830 verifyFormat("int c = []() -> int * { return 2; }();\n"); 19831 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 19832 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 19833 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 19834 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 19835 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 19836 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 19837 verifyFormat("[a, a]() -> a<1> {};"); 19838 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 19839 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 19840 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 19841 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 19842 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 19843 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 19844 verifyFormat("[]() -> foo<!5> { return {}; };"); 19845 verifyFormat("[]() -> foo<~5> { return {}; };"); 19846 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 19847 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 19848 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 19849 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 19850 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 19851 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 19852 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 19853 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 19854 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 19855 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 19856 verifyFormat("namespace bar {\n" 19857 "// broken:\n" 19858 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 19859 "} // namespace bar"); 19860 verifyFormat("namespace bar {\n" 19861 "// broken:\n" 19862 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 19863 "} // namespace bar"); 19864 verifyFormat("namespace bar {\n" 19865 "// broken:\n" 19866 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 19867 "} // namespace bar"); 19868 verifyFormat("namespace bar {\n" 19869 "// broken:\n" 19870 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 19871 "} // namespace bar"); 19872 verifyFormat("namespace bar {\n" 19873 "// broken:\n" 19874 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 19875 "} // namespace bar"); 19876 verifyFormat("namespace bar {\n" 19877 "// broken:\n" 19878 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 19879 "} // namespace bar"); 19880 verifyFormat("namespace bar {\n" 19881 "// broken:\n" 19882 "auto foo{[]() -> foo<!5> { return {}; }};\n" 19883 "} // namespace bar"); 19884 verifyFormat("namespace bar {\n" 19885 "// broken:\n" 19886 "auto foo{[]() -> foo<~5> { return {}; }};\n" 19887 "} // namespace bar"); 19888 verifyFormat("namespace bar {\n" 19889 "// broken:\n" 19890 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 19891 "} // namespace bar"); 19892 verifyFormat("namespace bar {\n" 19893 "// broken:\n" 19894 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 19895 "} // namespace bar"); 19896 verifyFormat("namespace bar {\n" 19897 "// broken:\n" 19898 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 19899 "} // namespace bar"); 19900 verifyFormat("namespace bar {\n" 19901 "// broken:\n" 19902 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 19903 "} // namespace bar"); 19904 verifyFormat("namespace bar {\n" 19905 "// broken:\n" 19906 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 19907 "} // namespace bar"); 19908 verifyFormat("namespace bar {\n" 19909 "// broken:\n" 19910 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 19911 "} // namespace bar"); 19912 verifyFormat("namespace bar {\n" 19913 "// broken:\n" 19914 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 19915 "} // namespace bar"); 19916 verifyFormat("namespace bar {\n" 19917 "// broken:\n" 19918 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 19919 "} // namespace bar"); 19920 verifyFormat("namespace bar {\n" 19921 "// broken:\n" 19922 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 19923 "} // namespace bar"); 19924 verifyFormat("namespace bar {\n" 19925 "// broken:\n" 19926 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 19927 "} // namespace bar"); 19928 verifyFormat("[]() -> a<1> {};"); 19929 verifyFormat("[]() -> a<1> { ; };"); 19930 verifyFormat("[]() -> a<1> { ; }();"); 19931 verifyFormat("[a, a]() -> a<true> {};"); 19932 verifyFormat("[]() -> a<true> {};"); 19933 verifyFormat("[]() -> a<true> { ; };"); 19934 verifyFormat("[]() -> a<true> { ; }();"); 19935 verifyFormat("[a, a]() -> a<false> {};"); 19936 verifyFormat("[]() -> a<false> {};"); 19937 verifyFormat("[]() -> a<false> { ; };"); 19938 verifyFormat("[]() -> a<false> { ; }();"); 19939 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 19940 verifyFormat("namespace bar {\n" 19941 "auto foo{[]() -> foo<false> { ; }};\n" 19942 "} // namespace bar"); 19943 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 19944 " int j) -> int {\n" 19945 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 19946 "};"); 19947 verifyFormat( 19948 "aaaaaaaaaaaaaaaaaaaaaa(\n" 19949 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 19950 " return aaaaaaaaaaaaaaaaa;\n" 19951 " });", 19952 getLLVMStyleWithColumns(70)); 19953 verifyFormat("[]() //\n" 19954 " -> int {\n" 19955 " return 1; //\n" 19956 "};"); 19957 verifyFormat("[]() -> Void<T...> {};"); 19958 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 19959 19960 // Lambdas with explicit template argument lists. 19961 verifyFormat( 19962 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 19963 19964 // Multiple lambdas in the same parentheses change indentation rules. These 19965 // lambdas are forced to start on new lines. 19966 verifyFormat("SomeFunction(\n" 19967 " []() {\n" 19968 " //\n" 19969 " },\n" 19970 " []() {\n" 19971 " //\n" 19972 " });"); 19973 19974 // A lambda passed as arg0 is always pushed to the next line. 19975 verifyFormat("SomeFunction(\n" 19976 " [this] {\n" 19977 " //\n" 19978 " },\n" 19979 " 1);\n"); 19980 19981 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 19982 // the arg0 case above. 19983 auto Style = getGoogleStyle(); 19984 Style.BinPackArguments = false; 19985 verifyFormat("SomeFunction(\n" 19986 " a,\n" 19987 " [this] {\n" 19988 " //\n" 19989 " },\n" 19990 " b);\n", 19991 Style); 19992 verifyFormat("SomeFunction(\n" 19993 " a,\n" 19994 " [this] {\n" 19995 " //\n" 19996 " },\n" 19997 " b);\n"); 19998 19999 // A lambda with a very long line forces arg0 to be pushed out irrespective of 20000 // the BinPackArguments value (as long as the code is wide enough). 20001 verifyFormat( 20002 "something->SomeFunction(\n" 20003 " a,\n" 20004 " [this] {\n" 20005 " " 20006 "D0000000000000000000000000000000000000000000000000000000000001();\n" 20007 " },\n" 20008 " b);\n"); 20009 20010 // A multi-line lambda is pulled up as long as the introducer fits on the 20011 // previous line and there are no further args. 20012 verifyFormat("function(1, [this, that] {\n" 20013 " //\n" 20014 "});\n"); 20015 verifyFormat("function([this, that] {\n" 20016 " //\n" 20017 "});\n"); 20018 // FIXME: this format is not ideal and we should consider forcing the first 20019 // arg onto its own line. 20020 verifyFormat("function(a, b, c, //\n" 20021 " d, [this, that] {\n" 20022 " //\n" 20023 " });\n"); 20024 20025 // Multiple lambdas are treated correctly even when there is a short arg0. 20026 verifyFormat("SomeFunction(\n" 20027 " 1,\n" 20028 " [this] {\n" 20029 " //\n" 20030 " },\n" 20031 " [this] {\n" 20032 " //\n" 20033 " },\n" 20034 " 1);\n"); 20035 20036 // More complex introducers. 20037 verifyFormat("return [i, args...] {};"); 20038 20039 // Not lambdas. 20040 verifyFormat("constexpr char hello[]{\"hello\"};"); 20041 verifyFormat("double &operator[](int i) { return 0; }\n" 20042 "int i;"); 20043 verifyFormat("std::unique_ptr<int[]> foo() {}"); 20044 verifyFormat("int i = a[a][a]->f();"); 20045 verifyFormat("int i = (*b)[a]->f();"); 20046 20047 // Other corner cases. 20048 verifyFormat("void f() {\n" 20049 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 20050 " );\n" 20051 "}"); 20052 20053 // Lambdas created through weird macros. 20054 verifyFormat("void f() {\n" 20055 " MACRO((const AA &a) { return 1; });\n" 20056 " MACRO((AA &a) { return 1; });\n" 20057 "}"); 20058 20059 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 20060 " doo_dah();\n" 20061 " doo_dah();\n" 20062 " })) {\n" 20063 "}"); 20064 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 20065 " doo_dah();\n" 20066 " doo_dah();\n" 20067 " })) {\n" 20068 "}"); 20069 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 20070 " doo_dah();\n" 20071 " doo_dah();\n" 20072 " })) {\n" 20073 "}"); 20074 verifyFormat("auto lambda = []() {\n" 20075 " int a = 2\n" 20076 "#if A\n" 20077 " + 2\n" 20078 "#endif\n" 20079 " ;\n" 20080 "};"); 20081 20082 // Lambdas with complex multiline introducers. 20083 verifyFormat( 20084 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20085 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 20086 " -> ::std::unordered_set<\n" 20087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 20088 " //\n" 20089 " });"); 20090 20091 FormatStyle DoNotMerge = getLLVMStyle(); 20092 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20093 verifyFormat("auto c = []() {\n" 20094 " return b;\n" 20095 "};", 20096 "auto c = []() { return b; };", DoNotMerge); 20097 verifyFormat("auto c = []() {\n" 20098 "};", 20099 " auto c = []() {};", DoNotMerge); 20100 20101 FormatStyle MergeEmptyOnly = getLLVMStyle(); 20102 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 20103 verifyFormat("auto c = []() {\n" 20104 " return b;\n" 20105 "};", 20106 "auto c = []() {\n" 20107 " return b;\n" 20108 " };", 20109 MergeEmptyOnly); 20110 verifyFormat("auto c = []() {};", 20111 "auto c = []() {\n" 20112 "};", 20113 MergeEmptyOnly); 20114 20115 FormatStyle MergeInline = getLLVMStyle(); 20116 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 20117 verifyFormat("auto c = []() {\n" 20118 " return b;\n" 20119 "};", 20120 "auto c = []() { return b; };", MergeInline); 20121 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 20122 MergeInline); 20123 verifyFormat("function([]() { return b; }, a)", 20124 "function([]() { return b; }, a)", MergeInline); 20125 verifyFormat("function(a, []() { return b; })", 20126 "function(a, []() { return b; })", MergeInline); 20127 20128 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 20129 // AllowShortLambdasOnASingleLine 20130 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20131 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20132 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20133 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20134 FormatStyle::ShortLambdaStyle::SLS_None; 20135 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 20136 " []()\n" 20137 " {\n" 20138 " return 17;\n" 20139 " });", 20140 LLVMWithBeforeLambdaBody); 20141 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 20142 " []()\n" 20143 " {\n" 20144 " });", 20145 LLVMWithBeforeLambdaBody); 20146 verifyFormat("auto fct_SLS_None = []()\n" 20147 "{\n" 20148 " return 17;\n" 20149 "};", 20150 LLVMWithBeforeLambdaBody); 20151 verifyFormat("TwoNestedLambdas_SLS_None(\n" 20152 " []()\n" 20153 " {\n" 20154 " return Call(\n" 20155 " []()\n" 20156 " {\n" 20157 " return 17;\n" 20158 " });\n" 20159 " });", 20160 LLVMWithBeforeLambdaBody); 20161 verifyFormat("void Fct() {\n" 20162 " return {[]()\n" 20163 " {\n" 20164 " return 17;\n" 20165 " }};\n" 20166 "}", 20167 LLVMWithBeforeLambdaBody); 20168 20169 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20170 FormatStyle::ShortLambdaStyle::SLS_Empty; 20171 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 20172 " []()\n" 20173 " {\n" 20174 " return 17;\n" 20175 " });", 20176 LLVMWithBeforeLambdaBody); 20177 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 20178 LLVMWithBeforeLambdaBody); 20179 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 20180 "ongFunctionName_SLS_Empty(\n" 20181 " []() {});", 20182 LLVMWithBeforeLambdaBody); 20183 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 20184 " []()\n" 20185 " {\n" 20186 " return 17;\n" 20187 " });", 20188 LLVMWithBeforeLambdaBody); 20189 verifyFormat("auto fct_SLS_Empty = []()\n" 20190 "{\n" 20191 " return 17;\n" 20192 "};", 20193 LLVMWithBeforeLambdaBody); 20194 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 20195 " []()\n" 20196 " {\n" 20197 " return Call([]() {});\n" 20198 " });", 20199 LLVMWithBeforeLambdaBody); 20200 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 20201 " []()\n" 20202 " {\n" 20203 " return Call([]() {});\n" 20204 " });", 20205 LLVMWithBeforeLambdaBody); 20206 verifyFormat( 20207 "FctWithLongLineInLambda_SLS_Empty(\n" 20208 " []()\n" 20209 " {\n" 20210 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20211 " AndShouldNotBeConsiderAsInline,\n" 20212 " LambdaBodyMustBeBreak);\n" 20213 " });", 20214 LLVMWithBeforeLambdaBody); 20215 20216 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20217 FormatStyle::ShortLambdaStyle::SLS_Inline; 20218 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 20219 LLVMWithBeforeLambdaBody); 20220 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 20221 LLVMWithBeforeLambdaBody); 20222 verifyFormat("auto fct_SLS_Inline = []()\n" 20223 "{\n" 20224 " return 17;\n" 20225 "};", 20226 LLVMWithBeforeLambdaBody); 20227 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 20228 "17; }); });", 20229 LLVMWithBeforeLambdaBody); 20230 verifyFormat( 20231 "FctWithLongLineInLambda_SLS_Inline(\n" 20232 " []()\n" 20233 " {\n" 20234 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20235 " AndShouldNotBeConsiderAsInline,\n" 20236 " LambdaBodyMustBeBreak);\n" 20237 " });", 20238 LLVMWithBeforeLambdaBody); 20239 verifyFormat("FctWithMultipleParams_SLS_Inline(" 20240 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20241 " []() { return 17; });", 20242 LLVMWithBeforeLambdaBody); 20243 verifyFormat( 20244 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 20245 LLVMWithBeforeLambdaBody); 20246 20247 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20248 FormatStyle::ShortLambdaStyle::SLS_All; 20249 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 20250 LLVMWithBeforeLambdaBody); 20251 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 20252 LLVMWithBeforeLambdaBody); 20253 verifyFormat("auto fct_SLS_All = []() { return 17; };", 20254 LLVMWithBeforeLambdaBody); 20255 verifyFormat("FctWithOneParam_SLS_All(\n" 20256 " []()\n" 20257 " {\n" 20258 " // A cool function...\n" 20259 " return 43;\n" 20260 " });", 20261 LLVMWithBeforeLambdaBody); 20262 verifyFormat("FctWithMultipleParams_SLS_All(" 20263 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20264 " []() { return 17; });", 20265 LLVMWithBeforeLambdaBody); 20266 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 20267 LLVMWithBeforeLambdaBody); 20268 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 20269 LLVMWithBeforeLambdaBody); 20270 verifyFormat( 20271 "FctWithLongLineInLambda_SLS_All(\n" 20272 " []()\n" 20273 " {\n" 20274 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20275 " AndShouldNotBeConsiderAsInline,\n" 20276 " LambdaBodyMustBeBreak);\n" 20277 " });", 20278 LLVMWithBeforeLambdaBody); 20279 verifyFormat( 20280 "auto fct_SLS_All = []()\n" 20281 "{\n" 20282 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20283 " AndShouldNotBeConsiderAsInline,\n" 20284 " LambdaBodyMustBeBreak);\n" 20285 "};", 20286 LLVMWithBeforeLambdaBody); 20287 LLVMWithBeforeLambdaBody.BinPackParameters = false; 20288 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 20289 LLVMWithBeforeLambdaBody); 20290 verifyFormat( 20291 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 20292 " FirstParam,\n" 20293 " SecondParam,\n" 20294 " ThirdParam,\n" 20295 " FourthParam);", 20296 LLVMWithBeforeLambdaBody); 20297 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20298 " []() { return " 20299 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 20300 " FirstParam,\n" 20301 " SecondParam,\n" 20302 " ThirdParam,\n" 20303 " FourthParam);", 20304 LLVMWithBeforeLambdaBody); 20305 verifyFormat( 20306 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 20307 " SecondParam,\n" 20308 " ThirdParam,\n" 20309 " FourthParam,\n" 20310 " []() { return SomeValueNotSoLong; });", 20311 LLVMWithBeforeLambdaBody); 20312 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20313 " []()\n" 20314 " {\n" 20315 " return " 20316 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 20317 "eConsiderAsInline;\n" 20318 " });", 20319 LLVMWithBeforeLambdaBody); 20320 verifyFormat( 20321 "FctWithLongLineInLambda_SLS_All(\n" 20322 " []()\n" 20323 " {\n" 20324 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20325 " AndShouldNotBeConsiderAsInline,\n" 20326 " LambdaBodyMustBeBreak);\n" 20327 " });", 20328 LLVMWithBeforeLambdaBody); 20329 verifyFormat("FctWithTwoParams_SLS_All(\n" 20330 " []()\n" 20331 " {\n" 20332 " // A cool function...\n" 20333 " return 43;\n" 20334 " },\n" 20335 " 87);", 20336 LLVMWithBeforeLambdaBody); 20337 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 20338 LLVMWithBeforeLambdaBody); 20339 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 20340 LLVMWithBeforeLambdaBody); 20341 verifyFormat( 20342 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 20343 LLVMWithBeforeLambdaBody); 20344 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 20345 "}); }, x);", 20346 LLVMWithBeforeLambdaBody); 20347 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20348 " []()\n" 20349 " {\n" 20350 " // A cool function...\n" 20351 " return Call([]() { return 17; });\n" 20352 " });", 20353 LLVMWithBeforeLambdaBody); 20354 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20355 " []()\n" 20356 " {\n" 20357 " return Call(\n" 20358 " []()\n" 20359 " {\n" 20360 " // A cool function...\n" 20361 " return 17;\n" 20362 " });\n" 20363 " });", 20364 LLVMWithBeforeLambdaBody); 20365 20366 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20367 FormatStyle::ShortLambdaStyle::SLS_None; 20368 20369 verifyFormat("auto select = [this]() -> const Library::Object *\n" 20370 "{\n" 20371 " return MyAssignment::SelectFromList(this);\n" 20372 "};\n", 20373 LLVMWithBeforeLambdaBody); 20374 20375 verifyFormat("auto select = [this]() -> const Library::Object &\n" 20376 "{\n" 20377 " return MyAssignment::SelectFromList(this);\n" 20378 "};\n", 20379 LLVMWithBeforeLambdaBody); 20380 20381 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 20382 "{\n" 20383 " return MyAssignment::SelectFromList(this);\n" 20384 "};\n", 20385 LLVMWithBeforeLambdaBody); 20386 20387 verifyFormat("namespace test {\n" 20388 "class Test {\n" 20389 "public:\n" 20390 " Test() = default;\n" 20391 "};\n" 20392 "} // namespace test", 20393 LLVMWithBeforeLambdaBody); 20394 20395 // Lambdas with different indentation styles. 20396 Style = getLLVMStyleWithColumns(100); 20397 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20398 " return promise.then(\n" 20399 " [this, &someVariable, someObject = " 20400 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20401 " return someObject.startAsyncAction().then(\n" 20402 " [this, &someVariable](AsyncActionResult result) " 20403 "mutable { result.processMore(); });\n" 20404 " });\n" 20405 "}\n", 20406 format("SomeResult doSomething(SomeObject promise) {\n" 20407 " return promise.then([this, &someVariable, someObject = " 20408 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20409 " return someObject.startAsyncAction().then([this, " 20410 "&someVariable](AsyncActionResult result) mutable {\n" 20411 " result.processMore();\n" 20412 " });\n" 20413 " });\n" 20414 "}\n", 20415 Style)); 20416 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20417 verifyFormat("test() {\n" 20418 " ([]() -> {\n" 20419 " int b = 32;\n" 20420 " return 3;\n" 20421 " }).foo();\n" 20422 "}", 20423 Style); 20424 verifyFormat("test() {\n" 20425 " []() -> {\n" 20426 " int b = 32;\n" 20427 " return 3;\n" 20428 " }\n" 20429 "}", 20430 Style); 20431 verifyFormat("std::sort(v.begin(), v.end(),\n" 20432 " [](const auto &someLongArgumentName, const auto " 20433 "&someOtherLongArgumentName) {\n" 20434 " return someLongArgumentName.someMemberVariable < " 20435 "someOtherLongArgumentName.someMemberVariable;\n" 20436 "});", 20437 Style); 20438 verifyFormat("test() {\n" 20439 " (\n" 20440 " []() -> {\n" 20441 " int b = 32;\n" 20442 " return 3;\n" 20443 " },\n" 20444 " foo, bar)\n" 20445 " .foo();\n" 20446 "}", 20447 Style); 20448 verifyFormat("test() {\n" 20449 " ([]() -> {\n" 20450 " int b = 32;\n" 20451 " return 3;\n" 20452 " })\n" 20453 " .foo()\n" 20454 " .bar();\n" 20455 "}", 20456 Style); 20457 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20458 " return promise.then(\n" 20459 " [this, &someVariable, someObject = " 20460 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20461 " return someObject.startAsyncAction().then(\n" 20462 " [this, &someVariable](AsyncActionResult result) mutable { " 20463 "result.processMore(); });\n" 20464 " });\n" 20465 "}\n", 20466 format("SomeResult doSomething(SomeObject promise) {\n" 20467 " return promise.then([this, &someVariable, someObject = " 20468 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20469 " return someObject.startAsyncAction().then([this, " 20470 "&someVariable](AsyncActionResult result) mutable {\n" 20471 " result.processMore();\n" 20472 " });\n" 20473 " });\n" 20474 "}\n", 20475 Style)); 20476 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20477 " return promise.then([this, &someVariable] {\n" 20478 " return someObject.startAsyncAction().then(\n" 20479 " [this, &someVariable](AsyncActionResult result) mutable { " 20480 "result.processMore(); });\n" 20481 " });\n" 20482 "}\n", 20483 format("SomeResult doSomething(SomeObject promise) {\n" 20484 " return promise.then([this, &someVariable] {\n" 20485 " return someObject.startAsyncAction().then([this, " 20486 "&someVariable](AsyncActionResult result) mutable {\n" 20487 " result.processMore();\n" 20488 " });\n" 20489 " });\n" 20490 "}\n", 20491 Style)); 20492 Style = getGoogleStyle(); 20493 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20494 EXPECT_EQ("#define A \\\n" 20495 " [] { \\\n" 20496 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20497 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20498 " }", 20499 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20500 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20501 Style)); 20502 // TODO: The current formatting has a minor issue that's not worth fixing 20503 // right now whereby the closing brace is indented relative to the signature 20504 // instead of being aligned. This only happens with macros. 20505 } 20506 20507 TEST_F(FormatTest, LambdaWithLineComments) { 20508 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20509 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20510 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20511 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20512 FormatStyle::ShortLambdaStyle::SLS_All; 20513 20514 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20515 verifyFormat("auto k = []() // comment\n" 20516 "{ return; }", 20517 LLVMWithBeforeLambdaBody); 20518 verifyFormat("auto k = []() /* comment */ { return; }", 20519 LLVMWithBeforeLambdaBody); 20520 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20521 LLVMWithBeforeLambdaBody); 20522 verifyFormat("auto k = []() // X\n" 20523 "{ return; }", 20524 LLVMWithBeforeLambdaBody); 20525 verifyFormat( 20526 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20527 "{ return; }", 20528 LLVMWithBeforeLambdaBody); 20529 } 20530 20531 TEST_F(FormatTest, EmptyLinesInLambdas) { 20532 verifyFormat("auto lambda = []() {\n" 20533 " x(); //\n" 20534 "};", 20535 "auto lambda = []() {\n" 20536 "\n" 20537 " x(); //\n" 20538 "\n" 20539 "};"); 20540 } 20541 20542 TEST_F(FormatTest, FormatsBlocks) { 20543 FormatStyle ShortBlocks = getLLVMStyle(); 20544 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20545 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20546 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20547 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20548 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20549 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20550 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20551 20552 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20553 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20554 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20555 20556 verifyFormat("[operation setCompletionBlock:^{\n" 20557 " [self onOperationDone];\n" 20558 "}];"); 20559 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20560 " [self onOperationDone];\n" 20561 "}]};"); 20562 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20563 " f();\n" 20564 "}];"); 20565 verifyFormat("int a = [operation block:^int(int *i) {\n" 20566 " return 1;\n" 20567 "}];"); 20568 verifyFormat("[myObject doSomethingWith:arg1\n" 20569 " aaa:^int(int *a) {\n" 20570 " return 1;\n" 20571 " }\n" 20572 " bbb:f(a * bbbbbbbb)];"); 20573 20574 verifyFormat("[operation setCompletionBlock:^{\n" 20575 " [self.delegate newDataAvailable];\n" 20576 "}];", 20577 getLLVMStyleWithColumns(60)); 20578 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20579 " NSString *path = [self sessionFilePath];\n" 20580 " if (path) {\n" 20581 " // ...\n" 20582 " }\n" 20583 "});"); 20584 verifyFormat("[[SessionService sharedService]\n" 20585 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20586 " if (window) {\n" 20587 " [self windowDidLoad:window];\n" 20588 " } else {\n" 20589 " [self errorLoadingWindow];\n" 20590 " }\n" 20591 " }];"); 20592 verifyFormat("void (^largeBlock)(void) = ^{\n" 20593 " // ...\n" 20594 "};\n", 20595 getLLVMStyleWithColumns(40)); 20596 verifyFormat("[[SessionService sharedService]\n" 20597 " loadWindowWithCompletionBlock: //\n" 20598 " ^(SessionWindow *window) {\n" 20599 " if (window) {\n" 20600 " [self windowDidLoad:window];\n" 20601 " } else {\n" 20602 " [self errorLoadingWindow];\n" 20603 " }\n" 20604 " }];", 20605 getLLVMStyleWithColumns(60)); 20606 verifyFormat("[myObject doSomethingWith:arg1\n" 20607 " firstBlock:^(Foo *a) {\n" 20608 " // ...\n" 20609 " int i;\n" 20610 " }\n" 20611 " secondBlock:^(Bar *b) {\n" 20612 " // ...\n" 20613 " int i;\n" 20614 " }\n" 20615 " thirdBlock:^Foo(Bar *b) {\n" 20616 " // ...\n" 20617 " int i;\n" 20618 " }];"); 20619 verifyFormat("[myObject doSomethingWith:arg1\n" 20620 " firstBlock:-1\n" 20621 " secondBlock:^(Bar *b) {\n" 20622 " // ...\n" 20623 " int i;\n" 20624 " }];"); 20625 20626 verifyFormat("f(^{\n" 20627 " @autoreleasepool {\n" 20628 " if (a) {\n" 20629 " g();\n" 20630 " }\n" 20631 " }\n" 20632 "});"); 20633 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20634 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20635 "};"); 20636 20637 FormatStyle FourIndent = getLLVMStyle(); 20638 FourIndent.ObjCBlockIndentWidth = 4; 20639 verifyFormat("[operation setCompletionBlock:^{\n" 20640 " [self onOperationDone];\n" 20641 "}];", 20642 FourIndent); 20643 } 20644 20645 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20646 FormatStyle ZeroColumn = getLLVMStyle(); 20647 ZeroColumn.ColumnLimit = 0; 20648 20649 verifyFormat("[[SessionService sharedService] " 20650 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20651 " if (window) {\n" 20652 " [self windowDidLoad:window];\n" 20653 " } else {\n" 20654 " [self errorLoadingWindow];\n" 20655 " }\n" 20656 "}];", 20657 ZeroColumn); 20658 EXPECT_EQ("[[SessionService sharedService]\n" 20659 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20660 " if (window) {\n" 20661 " [self windowDidLoad:window];\n" 20662 " } else {\n" 20663 " [self errorLoadingWindow];\n" 20664 " }\n" 20665 " }];", 20666 format("[[SessionService sharedService]\n" 20667 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20668 " if (window) {\n" 20669 " [self windowDidLoad:window];\n" 20670 " } else {\n" 20671 " [self errorLoadingWindow];\n" 20672 " }\n" 20673 "}];", 20674 ZeroColumn)); 20675 verifyFormat("[myObject doSomethingWith:arg1\n" 20676 " firstBlock:^(Foo *a) {\n" 20677 " // ...\n" 20678 " int i;\n" 20679 " }\n" 20680 " secondBlock:^(Bar *b) {\n" 20681 " // ...\n" 20682 " int i;\n" 20683 " }\n" 20684 " thirdBlock:^Foo(Bar *b) {\n" 20685 " // ...\n" 20686 " int i;\n" 20687 " }];", 20688 ZeroColumn); 20689 verifyFormat("f(^{\n" 20690 " @autoreleasepool {\n" 20691 " if (a) {\n" 20692 " g();\n" 20693 " }\n" 20694 " }\n" 20695 "});", 20696 ZeroColumn); 20697 verifyFormat("void (^largeBlock)(void) = ^{\n" 20698 " // ...\n" 20699 "};", 20700 ZeroColumn); 20701 20702 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20703 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20704 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20705 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20706 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20707 " int i;\n" 20708 "};", 20709 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20710 } 20711 20712 TEST_F(FormatTest, SupportsCRLF) { 20713 EXPECT_EQ("int a;\r\n" 20714 "int b;\r\n" 20715 "int c;\r\n", 20716 format("int a;\r\n" 20717 " int b;\r\n" 20718 " int c;\r\n", 20719 getLLVMStyle())); 20720 EXPECT_EQ("int a;\r\n" 20721 "int b;\r\n" 20722 "int c;\r\n", 20723 format("int a;\r\n" 20724 " int b;\n" 20725 " int c;\r\n", 20726 getLLVMStyle())); 20727 EXPECT_EQ("int a;\n" 20728 "int b;\n" 20729 "int c;\n", 20730 format("int a;\r\n" 20731 " int b;\n" 20732 " int c;\n", 20733 getLLVMStyle())); 20734 EXPECT_EQ("\"aaaaaaa \"\r\n" 20735 "\"bbbbbbb\";\r\n", 20736 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20737 EXPECT_EQ("#define A \\\r\n" 20738 " b; \\\r\n" 20739 " c; \\\r\n" 20740 " d;\r\n", 20741 format("#define A \\\r\n" 20742 " b; \\\r\n" 20743 " c; d; \r\n", 20744 getGoogleStyle())); 20745 20746 EXPECT_EQ("/*\r\n" 20747 "multi line block comments\r\n" 20748 "should not introduce\r\n" 20749 "an extra carriage return\r\n" 20750 "*/\r\n", 20751 format("/*\r\n" 20752 "multi line block comments\r\n" 20753 "should not introduce\r\n" 20754 "an extra carriage return\r\n" 20755 "*/\r\n")); 20756 EXPECT_EQ("/*\r\n" 20757 "\r\n" 20758 "*/", 20759 format("/*\r\n" 20760 " \r\r\r\n" 20761 "*/")); 20762 20763 FormatStyle style = getLLVMStyle(); 20764 20765 style.DeriveLineEnding = true; 20766 style.UseCRLF = false; 20767 EXPECT_EQ("union FooBarBazQux {\n" 20768 " int foo;\n" 20769 " int bar;\n" 20770 " int baz;\n" 20771 "};", 20772 format("union FooBarBazQux {\r\n" 20773 " int foo;\n" 20774 " int bar;\r\n" 20775 " int baz;\n" 20776 "};", 20777 style)); 20778 style.UseCRLF = true; 20779 EXPECT_EQ("union FooBarBazQux {\r\n" 20780 " int foo;\r\n" 20781 " int bar;\r\n" 20782 " int baz;\r\n" 20783 "};", 20784 format("union FooBarBazQux {\r\n" 20785 " int foo;\n" 20786 " int bar;\r\n" 20787 " int baz;\n" 20788 "};", 20789 style)); 20790 20791 style.DeriveLineEnding = false; 20792 style.UseCRLF = false; 20793 EXPECT_EQ("union FooBarBazQux {\n" 20794 " int foo;\n" 20795 " int bar;\n" 20796 " int baz;\n" 20797 " int qux;\n" 20798 "};", 20799 format("union FooBarBazQux {\r\n" 20800 " int foo;\n" 20801 " int bar;\r\n" 20802 " int baz;\n" 20803 " int qux;\r\n" 20804 "};", 20805 style)); 20806 style.UseCRLF = true; 20807 EXPECT_EQ("union FooBarBazQux {\r\n" 20808 " int foo;\r\n" 20809 " int bar;\r\n" 20810 " int baz;\r\n" 20811 " int qux;\r\n" 20812 "};", 20813 format("union FooBarBazQux {\r\n" 20814 " int foo;\n" 20815 " int bar;\r\n" 20816 " int baz;\n" 20817 " int qux;\n" 20818 "};", 20819 style)); 20820 20821 style.DeriveLineEnding = true; 20822 style.UseCRLF = false; 20823 EXPECT_EQ("union FooBarBazQux {\r\n" 20824 " int foo;\r\n" 20825 " int bar;\r\n" 20826 " int baz;\r\n" 20827 " int qux;\r\n" 20828 "};", 20829 format("union FooBarBazQux {\r\n" 20830 " int foo;\n" 20831 " int bar;\r\n" 20832 " int baz;\n" 20833 " int qux;\r\n" 20834 "};", 20835 style)); 20836 style.UseCRLF = true; 20837 EXPECT_EQ("union FooBarBazQux {\n" 20838 " int foo;\n" 20839 " int bar;\n" 20840 " int baz;\n" 20841 " int qux;\n" 20842 "};", 20843 format("union FooBarBazQux {\r\n" 20844 " int foo;\n" 20845 " int bar;\r\n" 20846 " int baz;\n" 20847 " int qux;\n" 20848 "};", 20849 style)); 20850 } 20851 20852 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 20853 verifyFormat("MY_CLASS(C) {\n" 20854 " int i;\n" 20855 " int j;\n" 20856 "};"); 20857 } 20858 20859 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 20860 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 20861 TwoIndent.ContinuationIndentWidth = 2; 20862 20863 EXPECT_EQ("int i =\n" 20864 " longFunction(\n" 20865 " arg);", 20866 format("int i = longFunction(arg);", TwoIndent)); 20867 20868 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 20869 SixIndent.ContinuationIndentWidth = 6; 20870 20871 EXPECT_EQ("int i =\n" 20872 " longFunction(\n" 20873 " arg);", 20874 format("int i = longFunction(arg);", SixIndent)); 20875 } 20876 20877 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 20878 FormatStyle Style = getLLVMStyle(); 20879 verifyFormat("int Foo::getter(\n" 20880 " //\n" 20881 ") const {\n" 20882 " return foo;\n" 20883 "}", 20884 Style); 20885 verifyFormat("void Foo::setter(\n" 20886 " //\n" 20887 ") {\n" 20888 " foo = 1;\n" 20889 "}", 20890 Style); 20891 } 20892 20893 TEST_F(FormatTest, SpacesInAngles) { 20894 FormatStyle Spaces = getLLVMStyle(); 20895 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20896 20897 verifyFormat("vector< ::std::string > x1;", Spaces); 20898 verifyFormat("Foo< int, Bar > x2;", Spaces); 20899 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 20900 20901 verifyFormat("static_cast< int >(arg);", Spaces); 20902 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 20903 verifyFormat("f< int, float >();", Spaces); 20904 verifyFormat("template <> g() {}", Spaces); 20905 verifyFormat("template < std::vector< int > > f() {}", Spaces); 20906 verifyFormat("std::function< void(int, int) > fct;", Spaces); 20907 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 20908 Spaces); 20909 20910 Spaces.Standard = FormatStyle::LS_Cpp03; 20911 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20912 verifyFormat("A< A< int > >();", Spaces); 20913 20914 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20915 verifyFormat("A<A<int> >();", Spaces); 20916 20917 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20918 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 20919 Spaces); 20920 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 20921 Spaces); 20922 20923 verifyFormat("A<A<int> >();", Spaces); 20924 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 20925 verifyFormat("A< A< int > >();", Spaces); 20926 20927 Spaces.Standard = FormatStyle::LS_Cpp11; 20928 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20929 verifyFormat("A< A< int > >();", Spaces); 20930 20931 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20932 verifyFormat("vector<::std::string> x4;", Spaces); 20933 verifyFormat("vector<int> x5;", Spaces); 20934 verifyFormat("Foo<int, Bar> x6;", Spaces); 20935 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20936 20937 verifyFormat("A<A<int>>();", Spaces); 20938 20939 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20940 verifyFormat("vector<::std::string> x4;", Spaces); 20941 verifyFormat("vector< ::std::string > x4;", Spaces); 20942 verifyFormat("vector<int> x5;", Spaces); 20943 verifyFormat("vector< int > x5;", Spaces); 20944 verifyFormat("Foo<int, Bar> x6;", Spaces); 20945 verifyFormat("Foo< int, Bar > x6;", Spaces); 20946 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20947 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 20948 20949 verifyFormat("A<A<int>>();", Spaces); 20950 verifyFormat("A< A< int > >();", Spaces); 20951 verifyFormat("A<A<int > >();", Spaces); 20952 verifyFormat("A< A< int>>();", Spaces); 20953 } 20954 20955 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 20956 FormatStyle Style = getLLVMStyle(); 20957 Style.SpaceAfterTemplateKeyword = false; 20958 verifyFormat("template<int> void foo();", Style); 20959 } 20960 20961 TEST_F(FormatTest, TripleAngleBrackets) { 20962 verifyFormat("f<<<1, 1>>>();"); 20963 verifyFormat("f<<<1, 1, 1, s>>>();"); 20964 verifyFormat("f<<<a, b, c, d>>>();"); 20965 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 20966 verifyFormat("f<param><<<1, 1>>>();"); 20967 verifyFormat("f<1><<<1, 1>>>();"); 20968 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 20969 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20970 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 20971 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 20972 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 20973 } 20974 20975 TEST_F(FormatTest, MergeLessLessAtEnd) { 20976 verifyFormat("<<"); 20977 EXPECT_EQ("< < <", format("\\\n<<<")); 20978 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20979 "aaallvm::outs() <<"); 20980 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20981 "aaaallvm::outs()\n <<"); 20982 } 20983 20984 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 20985 std::string code = "#if A\n" 20986 "#if B\n" 20987 "a.\n" 20988 "#endif\n" 20989 " a = 1;\n" 20990 "#else\n" 20991 "#endif\n" 20992 "#if C\n" 20993 "#else\n" 20994 "#endif\n"; 20995 EXPECT_EQ(code, format(code)); 20996 } 20997 20998 TEST_F(FormatTest, HandleConflictMarkers) { 20999 // Git/SVN conflict markers. 21000 EXPECT_EQ("int a;\n" 21001 "void f() {\n" 21002 " callme(some(parameter1,\n" 21003 "<<<<<<< text by the vcs\n" 21004 " parameter2),\n" 21005 "||||||| text by the vcs\n" 21006 " parameter2),\n" 21007 " parameter3,\n" 21008 "======= text by the vcs\n" 21009 " parameter2, parameter3),\n" 21010 ">>>>>>> text by the vcs\n" 21011 " otherparameter);\n", 21012 format("int a;\n" 21013 "void f() {\n" 21014 " callme(some(parameter1,\n" 21015 "<<<<<<< text by the vcs\n" 21016 " parameter2),\n" 21017 "||||||| text by the vcs\n" 21018 " parameter2),\n" 21019 " parameter3,\n" 21020 "======= text by the vcs\n" 21021 " parameter2,\n" 21022 " parameter3),\n" 21023 ">>>>>>> text by the vcs\n" 21024 " otherparameter);\n")); 21025 21026 // Perforce markers. 21027 EXPECT_EQ("void f() {\n" 21028 " function(\n" 21029 ">>>> text by the vcs\n" 21030 " parameter,\n" 21031 "==== text by the vcs\n" 21032 " parameter,\n" 21033 "==== text by the vcs\n" 21034 " parameter,\n" 21035 "<<<< text by the vcs\n" 21036 " parameter);\n", 21037 format("void f() {\n" 21038 " function(\n" 21039 ">>>> text by the vcs\n" 21040 " parameter,\n" 21041 "==== text by the vcs\n" 21042 " parameter,\n" 21043 "==== text by the vcs\n" 21044 " parameter,\n" 21045 "<<<< text by the vcs\n" 21046 " parameter);\n")); 21047 21048 EXPECT_EQ("<<<<<<<\n" 21049 "|||||||\n" 21050 "=======\n" 21051 ">>>>>>>", 21052 format("<<<<<<<\n" 21053 "|||||||\n" 21054 "=======\n" 21055 ">>>>>>>")); 21056 21057 EXPECT_EQ("<<<<<<<\n" 21058 "|||||||\n" 21059 "int i;\n" 21060 "=======\n" 21061 ">>>>>>>", 21062 format("<<<<<<<\n" 21063 "|||||||\n" 21064 "int i;\n" 21065 "=======\n" 21066 ">>>>>>>")); 21067 21068 // FIXME: Handle parsing of macros around conflict markers correctly: 21069 EXPECT_EQ("#define Macro \\\n" 21070 "<<<<<<<\n" 21071 "Something \\\n" 21072 "|||||||\n" 21073 "Else \\\n" 21074 "=======\n" 21075 "Other \\\n" 21076 ">>>>>>>\n" 21077 " End int i;\n", 21078 format("#define Macro \\\n" 21079 "<<<<<<<\n" 21080 " Something \\\n" 21081 "|||||||\n" 21082 " Else \\\n" 21083 "=======\n" 21084 " Other \\\n" 21085 ">>>>>>>\n" 21086 " End\n" 21087 "int i;\n")); 21088 } 21089 21090 TEST_F(FormatTest, DisableRegions) { 21091 EXPECT_EQ("int i;\n" 21092 "// clang-format off\n" 21093 " int j;\n" 21094 "// clang-format on\n" 21095 "int k;", 21096 format(" int i;\n" 21097 " // clang-format off\n" 21098 " int j;\n" 21099 " // clang-format on\n" 21100 " int k;")); 21101 EXPECT_EQ("int i;\n" 21102 "/* clang-format off */\n" 21103 " int j;\n" 21104 "/* clang-format on */\n" 21105 "int k;", 21106 format(" int i;\n" 21107 " /* clang-format off */\n" 21108 " int j;\n" 21109 " /* clang-format on */\n" 21110 " int k;")); 21111 21112 // Don't reflow comments within disabled regions. 21113 EXPECT_EQ("// clang-format off\n" 21114 "// long long long long long long line\n" 21115 "/* clang-format on */\n" 21116 "/* long long long\n" 21117 " * long long long\n" 21118 " * line */\n" 21119 "int i;\n" 21120 "/* clang-format off */\n" 21121 "/* long long long long long long line */\n", 21122 format("// clang-format off\n" 21123 "// long long long long long long line\n" 21124 "/* clang-format on */\n" 21125 "/* long long long long long long line */\n" 21126 "int i;\n" 21127 "/* clang-format off */\n" 21128 "/* long long long long long long line */\n", 21129 getLLVMStyleWithColumns(20))); 21130 } 21131 21132 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 21133 format("? ) ="); 21134 verifyNoCrash("#define a\\\n /**/}"); 21135 } 21136 21137 TEST_F(FormatTest, FormatsTableGenCode) { 21138 FormatStyle Style = getLLVMStyle(); 21139 Style.Language = FormatStyle::LK_TableGen; 21140 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 21141 } 21142 21143 TEST_F(FormatTest, ArrayOfTemplates) { 21144 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 21145 format("auto a = new unique_ptr<int > [ 10];")); 21146 21147 FormatStyle Spaces = getLLVMStyle(); 21148 Spaces.SpacesInSquareBrackets = true; 21149 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 21150 format("auto a = new unique_ptr<int > [10];", Spaces)); 21151 } 21152 21153 TEST_F(FormatTest, ArrayAsTemplateType) { 21154 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 21155 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 21156 21157 FormatStyle Spaces = getLLVMStyle(); 21158 Spaces.SpacesInSquareBrackets = true; 21159 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 21160 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 21161 } 21162 21163 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 21164 21165 TEST(FormatStyle, GetStyleWithEmptyFileName) { 21166 llvm::vfs::InMemoryFileSystem FS; 21167 auto Style1 = getStyle("file", "", "Google", "", &FS); 21168 ASSERT_TRUE((bool)Style1); 21169 ASSERT_EQ(*Style1, getGoogleStyle()); 21170 } 21171 21172 TEST(FormatStyle, GetStyleOfFile) { 21173 llvm::vfs::InMemoryFileSystem FS; 21174 // Test 1: format file in the same directory. 21175 ASSERT_TRUE( 21176 FS.addFile("/a/.clang-format", 0, 21177 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 21178 ASSERT_TRUE( 21179 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21180 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 21181 ASSERT_TRUE((bool)Style1); 21182 ASSERT_EQ(*Style1, getLLVMStyle()); 21183 21184 // Test 2.1: fallback to default. 21185 ASSERT_TRUE( 21186 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21187 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 21188 ASSERT_TRUE((bool)Style2); 21189 ASSERT_EQ(*Style2, getMozillaStyle()); 21190 21191 // Test 2.2: no format on 'none' fallback style. 21192 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21193 ASSERT_TRUE((bool)Style2); 21194 ASSERT_EQ(*Style2, getNoStyle()); 21195 21196 // Test 2.3: format if config is found with no based style while fallback is 21197 // 'none'. 21198 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 21199 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 21200 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21201 ASSERT_TRUE((bool)Style2); 21202 ASSERT_EQ(*Style2, getLLVMStyle()); 21203 21204 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 21205 Style2 = getStyle("{}", "a.h", "none", "", &FS); 21206 ASSERT_TRUE((bool)Style2); 21207 ASSERT_EQ(*Style2, getLLVMStyle()); 21208 21209 // Test 3: format file in parent directory. 21210 ASSERT_TRUE( 21211 FS.addFile("/c/.clang-format", 0, 21212 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 21213 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 21214 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21215 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 21216 ASSERT_TRUE((bool)Style3); 21217 ASSERT_EQ(*Style3, getGoogleStyle()); 21218 21219 // Test 4: error on invalid fallback style 21220 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 21221 ASSERT_FALSE((bool)Style4); 21222 llvm::consumeError(Style4.takeError()); 21223 21224 // Test 5: error on invalid yaml on command line 21225 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 21226 ASSERT_FALSE((bool)Style5); 21227 llvm::consumeError(Style5.takeError()); 21228 21229 // Test 6: error on invalid style 21230 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 21231 ASSERT_FALSE((bool)Style6); 21232 llvm::consumeError(Style6.takeError()); 21233 21234 // Test 7: found config file, error on parsing it 21235 ASSERT_TRUE( 21236 FS.addFile("/d/.clang-format", 0, 21237 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 21238 "InvalidKey: InvalidValue"))); 21239 ASSERT_TRUE( 21240 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21241 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 21242 ASSERT_FALSE((bool)Style7a); 21243 llvm::consumeError(Style7a.takeError()); 21244 21245 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 21246 ASSERT_TRUE((bool)Style7b); 21247 21248 // Test 8: inferred per-language defaults apply. 21249 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 21250 ASSERT_TRUE((bool)StyleTd); 21251 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 21252 21253 // Test 9.1: overwriting a file style, when parent no file exists with no 21254 // fallback style 21255 ASSERT_TRUE(FS.addFile( 21256 "/e/sub/.clang-format", 0, 21257 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 21258 "ColumnLimit: 20"))); 21259 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 21260 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21261 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21262 ASSERT_TRUE(static_cast<bool>(Style9)); 21263 ASSERT_EQ(*Style9, [] { 21264 auto Style = getNoStyle(); 21265 Style.ColumnLimit = 20; 21266 return Style; 21267 }()); 21268 21269 // Test 9.2: with LLVM fallback style 21270 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 21271 ASSERT_TRUE(static_cast<bool>(Style9)); 21272 ASSERT_EQ(*Style9, [] { 21273 auto Style = getLLVMStyle(); 21274 Style.ColumnLimit = 20; 21275 return Style; 21276 }()); 21277 21278 // Test 9.3: with a parent file 21279 ASSERT_TRUE( 21280 FS.addFile("/e/.clang-format", 0, 21281 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 21282 "UseTab: Always"))); 21283 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21284 ASSERT_TRUE(static_cast<bool>(Style9)); 21285 ASSERT_EQ(*Style9, [] { 21286 auto Style = getGoogleStyle(); 21287 Style.ColumnLimit = 20; 21288 Style.UseTab = FormatStyle::UT_Always; 21289 return Style; 21290 }()); 21291 21292 // Test 9.4: propagate more than one level 21293 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 21294 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21295 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 21296 llvm::MemoryBuffer::getMemBuffer( 21297 "BasedOnStyle: InheritParentConfig\n" 21298 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 21299 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 21300 21301 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 21302 auto Style = getGoogleStyle(); 21303 Style.ColumnLimit = 20; 21304 Style.UseTab = FormatStyle::UT_Always; 21305 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 21306 return Style; 21307 }(); 21308 21309 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 21310 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 21311 ASSERT_TRUE(static_cast<bool>(Style9)); 21312 ASSERT_EQ(*Style9, SubSubStyle); 21313 21314 // Test 9.5: use InheritParentConfig as style name 21315 Style9 = 21316 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 21317 ASSERT_TRUE(static_cast<bool>(Style9)); 21318 ASSERT_EQ(*Style9, SubSubStyle); 21319 21320 // Test 9.6: use command line style with inheritance 21321 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 21322 "none", "", &FS); 21323 ASSERT_TRUE(static_cast<bool>(Style9)); 21324 ASSERT_EQ(*Style9, SubSubStyle); 21325 21326 // Test 9.7: use command line style with inheritance and own config 21327 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 21328 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 21329 "/e/sub/code.cpp", "none", "", &FS); 21330 ASSERT_TRUE(static_cast<bool>(Style9)); 21331 ASSERT_EQ(*Style9, SubSubStyle); 21332 21333 // Test 9.8: use inheritance from a file without BasedOnStyle 21334 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 21335 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 21336 ASSERT_TRUE( 21337 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 21338 llvm::MemoryBuffer::getMemBuffer( 21339 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 21340 // Make sure we do not use the fallback style 21341 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 21342 ASSERT_TRUE(static_cast<bool>(Style9)); 21343 ASSERT_EQ(*Style9, [] { 21344 auto Style = getLLVMStyle(); 21345 Style.ColumnLimit = 123; 21346 return Style; 21347 }()); 21348 21349 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 21350 ASSERT_TRUE(static_cast<bool>(Style9)); 21351 ASSERT_EQ(*Style9, [] { 21352 auto Style = getLLVMStyle(); 21353 Style.ColumnLimit = 123; 21354 Style.IndentWidth = 7; 21355 return Style; 21356 }()); 21357 } 21358 21359 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 21360 // Column limit is 20. 21361 std::string Code = "Type *a =\n" 21362 " new Type();\n" 21363 "g(iiiii, 0, jjjjj,\n" 21364 " 0, kkkkk, 0, mm);\n" 21365 "int bad = format ;"; 21366 std::string Expected = "auto a = new Type();\n" 21367 "g(iiiii, nullptr,\n" 21368 " jjjjj, nullptr,\n" 21369 " kkkkk, nullptr,\n" 21370 " mm);\n" 21371 "int bad = format ;"; 21372 FileID ID = Context.createInMemoryFile("format.cpp", Code); 21373 tooling::Replacements Replaces = toReplacements( 21374 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 21375 "auto "), 21376 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 21377 "nullptr"), 21378 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 21379 "nullptr"), 21380 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 21381 "nullptr")}); 21382 21383 format::FormatStyle Style = format::getLLVMStyle(); 21384 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 21385 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21386 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21387 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21388 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21389 EXPECT_TRUE(static_cast<bool>(Result)); 21390 EXPECT_EQ(Expected, *Result); 21391 } 21392 21393 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 21394 std::string Code = "#include \"a.h\"\n" 21395 "#include \"c.h\"\n" 21396 "\n" 21397 "int main() {\n" 21398 " return 0;\n" 21399 "}"; 21400 std::string Expected = "#include \"a.h\"\n" 21401 "#include \"b.h\"\n" 21402 "#include \"c.h\"\n" 21403 "\n" 21404 "int main() {\n" 21405 " return 0;\n" 21406 "}"; 21407 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 21408 tooling::Replacements Replaces = toReplacements( 21409 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 21410 "#include \"b.h\"\n")}); 21411 21412 format::FormatStyle Style = format::getLLVMStyle(); 21413 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 21414 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21415 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21416 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21417 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21418 EXPECT_TRUE(static_cast<bool>(Result)); 21419 EXPECT_EQ(Expected, *Result); 21420 } 21421 21422 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 21423 EXPECT_EQ("using std::cin;\n" 21424 "using std::cout;", 21425 format("using std::cout;\n" 21426 "using std::cin;", 21427 getGoogleStyle())); 21428 } 21429 21430 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21431 format::FormatStyle Style = format::getLLVMStyle(); 21432 Style.Standard = FormatStyle::LS_Cpp03; 21433 // cpp03 recognize this string as identifier u8 and literal character 'a' 21434 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21435 } 21436 21437 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21438 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21439 // all modes, including C++11, C++14 and C++17 21440 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21441 } 21442 21443 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21444 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21445 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21446 } 21447 21448 TEST_F(FormatTest, StructuredBindings) { 21449 // Structured bindings is a C++17 feature. 21450 // all modes, including C++11, C++14 and C++17 21451 verifyFormat("auto [a, b] = f();"); 21452 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21453 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21454 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21455 EXPECT_EQ("auto const volatile [a, b] = f();", 21456 format("auto const volatile[a, b] = f();")); 21457 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21458 EXPECT_EQ("auto &[a, b, c] = f();", 21459 format("auto &[ a , b,c ] = f();")); 21460 EXPECT_EQ("auto &&[a, b, c] = f();", 21461 format("auto &&[ a , b,c ] = f();")); 21462 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21463 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21464 format("auto const volatile &&[a, b] = f();")); 21465 EXPECT_EQ("auto const &&[a, b] = f();", 21466 format("auto const && [a, b] = f();")); 21467 EXPECT_EQ("const auto &[a, b] = f();", 21468 format("const auto & [a, b] = f();")); 21469 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21470 format("const auto volatile &&[a, b] = f();")); 21471 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21472 format("volatile const auto &&[a, b] = f();")); 21473 EXPECT_EQ("const auto &&[a, b] = f();", 21474 format("const auto && [a, b] = f();")); 21475 21476 // Make sure we don't mistake structured bindings for lambdas. 21477 FormatStyle PointerMiddle = getLLVMStyle(); 21478 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21479 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21480 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21481 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21482 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21483 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21484 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21485 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21486 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21487 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21488 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21489 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21490 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21491 21492 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21493 format("for (const auto && [a, b] : some_range) {\n}")); 21494 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21495 format("for (const auto & [a, b] : some_range) {\n}")); 21496 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21497 format("for (const auto[a, b] : some_range) {\n}")); 21498 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21499 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21500 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21501 EXPECT_EQ("auto const &[x, y](expr);", 21502 format("auto const & [x,y] (expr);")); 21503 EXPECT_EQ("auto const &&[x, y](expr);", 21504 format("auto const && [x,y] (expr);")); 21505 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21506 EXPECT_EQ("auto const &[x, y]{expr};", 21507 format("auto const & [x,y] {expr};")); 21508 EXPECT_EQ("auto const &&[x, y]{expr};", 21509 format("auto const && [x,y] {expr};")); 21510 21511 format::FormatStyle Spaces = format::getLLVMStyle(); 21512 Spaces.SpacesInSquareBrackets = true; 21513 verifyFormat("auto [ a, b ] = f();", Spaces); 21514 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21515 verifyFormat("auto &[ a, b ] = f();", Spaces); 21516 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21517 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21518 } 21519 21520 TEST_F(FormatTest, FileAndCode) { 21521 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21522 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21523 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21524 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21525 EXPECT_EQ(FormatStyle::LK_ObjC, 21526 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21527 EXPECT_EQ( 21528 FormatStyle::LK_ObjC, 21529 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21530 EXPECT_EQ(FormatStyle::LK_ObjC, 21531 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21532 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21533 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21534 EXPECT_EQ(FormatStyle::LK_ObjC, 21535 guessLanguage("foo", "@interface Foo\n@end\n")); 21536 EXPECT_EQ(FormatStyle::LK_ObjC, 21537 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21538 EXPECT_EQ( 21539 FormatStyle::LK_ObjC, 21540 guessLanguage("foo.h", 21541 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21542 EXPECT_EQ( 21543 FormatStyle::LK_Cpp, 21544 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21545 } 21546 21547 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21548 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21549 EXPECT_EQ(FormatStyle::LK_ObjC, 21550 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21551 EXPECT_EQ(FormatStyle::LK_Cpp, 21552 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21553 EXPECT_EQ( 21554 FormatStyle::LK_Cpp, 21555 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21556 EXPECT_EQ(FormatStyle::LK_ObjC, 21557 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21558 EXPECT_EQ(FormatStyle::LK_Cpp, 21559 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21560 EXPECT_EQ(FormatStyle::LK_ObjC, 21561 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21562 EXPECT_EQ(FormatStyle::LK_Cpp, 21563 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21564 EXPECT_EQ(FormatStyle::LK_Cpp, 21565 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21566 EXPECT_EQ(FormatStyle::LK_ObjC, 21567 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21568 EXPECT_EQ(FormatStyle::LK_Cpp, 21569 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21570 EXPECT_EQ( 21571 FormatStyle::LK_Cpp, 21572 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21573 EXPECT_EQ( 21574 FormatStyle::LK_Cpp, 21575 guessLanguage("foo.h", 21576 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 21577 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 21578 } 21579 21580 TEST_F(FormatTest, GuessLanguageWithCaret) { 21581 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 21582 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 21583 EXPECT_EQ(FormatStyle::LK_ObjC, 21584 guessLanguage("foo.h", "int(^)(char, float);")); 21585 EXPECT_EQ(FormatStyle::LK_ObjC, 21586 guessLanguage("foo.h", "int(^foo)(char, float);")); 21587 EXPECT_EQ(FormatStyle::LK_ObjC, 21588 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 21589 EXPECT_EQ(FormatStyle::LK_ObjC, 21590 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 21591 EXPECT_EQ( 21592 FormatStyle::LK_ObjC, 21593 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 21594 } 21595 21596 TEST_F(FormatTest, GuessLanguageWithPragmas) { 21597 EXPECT_EQ(FormatStyle::LK_Cpp, 21598 guessLanguage("foo.h", "__pragma(warning(disable:))")); 21599 EXPECT_EQ(FormatStyle::LK_Cpp, 21600 guessLanguage("foo.h", "#pragma(warning(disable:))")); 21601 EXPECT_EQ(FormatStyle::LK_Cpp, 21602 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 21603 } 21604 21605 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 21606 // ASM symbolic names are identifiers that must be surrounded by [] without 21607 // space in between: 21608 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 21609 21610 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 21611 verifyFormat(R"(// 21612 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 21613 )"); 21614 21615 // A list of several ASM symbolic names. 21616 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 21617 21618 // ASM symbolic names in inline ASM with inputs and outputs. 21619 verifyFormat(R"(// 21620 asm("cmoveq %1, %2, %[result]" 21621 : [result] "=r"(result) 21622 : "r"(test), "r"(new), "[result]"(old)); 21623 )"); 21624 21625 // ASM symbolic names in inline ASM with no outputs. 21626 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 21627 } 21628 21629 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 21630 EXPECT_EQ(FormatStyle::LK_Cpp, 21631 guessLanguage("foo.h", "void f() {\n" 21632 " asm (\"mov %[e], %[d]\"\n" 21633 " : [d] \"=rm\" (d)\n" 21634 " [e] \"rm\" (*e));\n" 21635 "}")); 21636 EXPECT_EQ(FormatStyle::LK_Cpp, 21637 guessLanguage("foo.h", "void f() {\n" 21638 " _asm (\"mov %[e], %[d]\"\n" 21639 " : [d] \"=rm\" (d)\n" 21640 " [e] \"rm\" (*e));\n" 21641 "}")); 21642 EXPECT_EQ(FormatStyle::LK_Cpp, 21643 guessLanguage("foo.h", "void f() {\n" 21644 " __asm (\"mov %[e], %[d]\"\n" 21645 " : [d] \"=rm\" (d)\n" 21646 " [e] \"rm\" (*e));\n" 21647 "}")); 21648 EXPECT_EQ(FormatStyle::LK_Cpp, 21649 guessLanguage("foo.h", "void f() {\n" 21650 " __asm__ (\"mov %[e], %[d]\"\n" 21651 " : [d] \"=rm\" (d)\n" 21652 " [e] \"rm\" (*e));\n" 21653 "}")); 21654 EXPECT_EQ(FormatStyle::LK_Cpp, 21655 guessLanguage("foo.h", "void f() {\n" 21656 " asm (\"mov %[e], %[d]\"\n" 21657 " : [d] \"=rm\" (d),\n" 21658 " [e] \"rm\" (*e));\n" 21659 "}")); 21660 EXPECT_EQ(FormatStyle::LK_Cpp, 21661 guessLanguage("foo.h", "void f() {\n" 21662 " asm volatile (\"mov %[e], %[d]\"\n" 21663 " : [d] \"=rm\" (d)\n" 21664 " [e] \"rm\" (*e));\n" 21665 "}")); 21666 } 21667 21668 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21669 EXPECT_EQ(FormatStyle::LK_Cpp, 21670 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21671 EXPECT_EQ(FormatStyle::LK_ObjC, 21672 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21673 EXPECT_EQ( 21674 FormatStyle::LK_Cpp, 21675 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21676 EXPECT_EQ( 21677 FormatStyle::LK_ObjC, 21678 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21679 } 21680 21681 TEST_F(FormatTest, TypenameMacros) { 21682 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21683 21684 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21685 FormatStyle Google = getGoogleStyleWithColumns(0); 21686 Google.TypenameMacros = TypenameMacros; 21687 verifyFormat("struct foo {\n" 21688 " int bar;\n" 21689 " TAILQ_ENTRY(a) bleh;\n" 21690 "};", 21691 Google); 21692 21693 FormatStyle Macros = getLLVMStyle(); 21694 Macros.TypenameMacros = TypenameMacros; 21695 21696 verifyFormat("STACK_OF(int) a;", Macros); 21697 verifyFormat("STACK_OF(int) *a;", Macros); 21698 verifyFormat("STACK_OF(int const *) *a;", Macros); 21699 verifyFormat("STACK_OF(int *const) *a;", Macros); 21700 verifyFormat("STACK_OF(int, string) a;", Macros); 21701 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21702 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21703 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21704 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21705 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21706 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21707 21708 Macros.PointerAlignment = FormatStyle::PAS_Left; 21709 verifyFormat("STACK_OF(int)* a;", Macros); 21710 verifyFormat("STACK_OF(int*)* a;", Macros); 21711 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21712 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21713 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21714 } 21715 21716 TEST_F(FormatTest, AtomicQualifier) { 21717 // Check that we treate _Atomic as a type and not a function call 21718 FormatStyle Google = getGoogleStyleWithColumns(0); 21719 verifyFormat("struct foo {\n" 21720 " int a1;\n" 21721 " _Atomic(a) a2;\n" 21722 " _Atomic(_Atomic(int) *const) a3;\n" 21723 "};", 21724 Google); 21725 verifyFormat("_Atomic(uint64_t) a;"); 21726 verifyFormat("_Atomic(uint64_t) *a;"); 21727 verifyFormat("_Atomic(uint64_t const *) *a;"); 21728 verifyFormat("_Atomic(uint64_t *const) *a;"); 21729 verifyFormat("_Atomic(const uint64_t *) *a;"); 21730 verifyFormat("_Atomic(uint64_t) a;"); 21731 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21732 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21733 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21734 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21735 21736 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21737 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21738 FormatStyle Style = getLLVMStyle(); 21739 Style.PointerAlignment = FormatStyle::PAS_Left; 21740 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21741 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21742 verifyFormat("_Atomic(int)* a;", Style); 21743 verifyFormat("_Atomic(int*)* a;", Style); 21744 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21745 21746 Style.SpacesInCStyleCastParentheses = true; 21747 Style.SpacesInParentheses = false; 21748 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21749 Style.SpacesInCStyleCastParentheses = false; 21750 Style.SpacesInParentheses = true; 21751 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21752 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21753 } 21754 21755 TEST_F(FormatTest, AmbersandInLamda) { 21756 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21757 FormatStyle AlignStyle = getLLVMStyle(); 21758 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21759 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21760 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21761 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21762 } 21763 21764 TEST_F(FormatTest, SpacesInConditionalStatement) { 21765 FormatStyle Spaces = getLLVMStyle(); 21766 Spaces.IfMacros.clear(); 21767 Spaces.IfMacros.push_back("MYIF"); 21768 Spaces.SpacesInConditionalStatement = true; 21769 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21770 verifyFormat("if ( !a )\n return;", Spaces); 21771 verifyFormat("if ( a )\n return;", Spaces); 21772 verifyFormat("if constexpr ( a )\n return;", Spaces); 21773 verifyFormat("MYIF ( a )\n return;", Spaces); 21774 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21775 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21776 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21777 verifyFormat("while ( a )\n return;", Spaces); 21778 verifyFormat("while ( (a && b) )\n return;", Spaces); 21779 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21780 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21781 // Check that space on the left of "::" is inserted as expected at beginning 21782 // of condition. 21783 verifyFormat("while ( ::func() )\n return;", Spaces); 21784 21785 // Check impact of ControlStatementsExceptControlMacros is honored. 21786 Spaces.SpaceBeforeParens = 21787 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21788 verifyFormat("MYIF( a )\n return;", Spaces); 21789 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 21790 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 21791 } 21792 21793 TEST_F(FormatTest, AlternativeOperators) { 21794 // Test case for ensuring alternate operators are not 21795 // combined with their right most neighbour. 21796 verifyFormat("int a and b;"); 21797 verifyFormat("int a and_eq b;"); 21798 verifyFormat("int a bitand b;"); 21799 verifyFormat("int a bitor b;"); 21800 verifyFormat("int a compl b;"); 21801 verifyFormat("int a not b;"); 21802 verifyFormat("int a not_eq b;"); 21803 verifyFormat("int a or b;"); 21804 verifyFormat("int a xor b;"); 21805 verifyFormat("int a xor_eq b;"); 21806 verifyFormat("return this not_eq bitand other;"); 21807 verifyFormat("bool operator not_eq(const X bitand other)"); 21808 21809 verifyFormat("int a and 5;"); 21810 verifyFormat("int a and_eq 5;"); 21811 verifyFormat("int a bitand 5;"); 21812 verifyFormat("int a bitor 5;"); 21813 verifyFormat("int a compl 5;"); 21814 verifyFormat("int a not 5;"); 21815 verifyFormat("int a not_eq 5;"); 21816 verifyFormat("int a or 5;"); 21817 verifyFormat("int a xor 5;"); 21818 verifyFormat("int a xor_eq 5;"); 21819 21820 verifyFormat("int a compl(5);"); 21821 verifyFormat("int a not(5);"); 21822 21823 /* FIXME handle alternate tokens 21824 * https://en.cppreference.com/w/cpp/language/operator_alternative 21825 // alternative tokens 21826 verifyFormat("compl foo();"); // ~foo(); 21827 verifyFormat("foo() <%%>;"); // foo(); 21828 verifyFormat("void foo() <%%>;"); // void foo(){} 21829 verifyFormat("int a <:1:>;"); // int a[1];[ 21830 verifyFormat("%:define ABC abc"); // #define ABC abc 21831 verifyFormat("%:%:"); // ## 21832 */ 21833 } 21834 21835 TEST_F(FormatTest, STLWhileNotDefineChed) { 21836 verifyFormat("#if defined(while)\n" 21837 "#define while EMIT WARNING C4005\n" 21838 "#endif // while"); 21839 } 21840 21841 TEST_F(FormatTest, OperatorSpacing) { 21842 FormatStyle Style = getLLVMStyle(); 21843 Style.PointerAlignment = FormatStyle::PAS_Right; 21844 verifyFormat("Foo::operator*();", Style); 21845 verifyFormat("Foo::operator void *();", Style); 21846 verifyFormat("Foo::operator void **();", Style); 21847 verifyFormat("Foo::operator void *&();", Style); 21848 verifyFormat("Foo::operator void *&&();", Style); 21849 verifyFormat("Foo::operator void const *();", Style); 21850 verifyFormat("Foo::operator void const **();", Style); 21851 verifyFormat("Foo::operator void const *&();", Style); 21852 verifyFormat("Foo::operator void const *&&();", Style); 21853 verifyFormat("Foo::operator()(void *);", Style); 21854 verifyFormat("Foo::operator*(void *);", Style); 21855 verifyFormat("Foo::operator*();", Style); 21856 verifyFormat("Foo::operator**();", Style); 21857 verifyFormat("Foo::operator&();", Style); 21858 verifyFormat("Foo::operator<int> *();", Style); 21859 verifyFormat("Foo::operator<Foo> *();", Style); 21860 verifyFormat("Foo::operator<int> **();", Style); 21861 verifyFormat("Foo::operator<Foo> **();", Style); 21862 verifyFormat("Foo::operator<int> &();", Style); 21863 verifyFormat("Foo::operator<Foo> &();", Style); 21864 verifyFormat("Foo::operator<int> &&();", Style); 21865 verifyFormat("Foo::operator<Foo> &&();", Style); 21866 verifyFormat("Foo::operator<int> *&();", Style); 21867 verifyFormat("Foo::operator<Foo> *&();", Style); 21868 verifyFormat("Foo::operator<int> *&&();", Style); 21869 verifyFormat("Foo::operator<Foo> *&&();", Style); 21870 verifyFormat("operator*(int (*)(), class Foo);", Style); 21871 21872 verifyFormat("Foo::operator&();", Style); 21873 verifyFormat("Foo::operator void &();", Style); 21874 verifyFormat("Foo::operator void const &();", Style); 21875 verifyFormat("Foo::operator()(void &);", Style); 21876 verifyFormat("Foo::operator&(void &);", Style); 21877 verifyFormat("Foo::operator&();", Style); 21878 verifyFormat("operator&(int (&)(), class Foo);", Style); 21879 21880 verifyFormat("Foo::operator&&();", Style); 21881 verifyFormat("Foo::operator**();", Style); 21882 verifyFormat("Foo::operator void &&();", Style); 21883 verifyFormat("Foo::operator void const &&();", Style); 21884 verifyFormat("Foo::operator()(void &&);", Style); 21885 verifyFormat("Foo::operator&&(void &&);", Style); 21886 verifyFormat("Foo::operator&&();", Style); 21887 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21888 verifyFormat("operator const nsTArrayRight<E> &()", Style); 21889 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 21890 Style); 21891 verifyFormat("operator void **()", Style); 21892 verifyFormat("operator const FooRight<Object> &()", Style); 21893 verifyFormat("operator const FooRight<Object> *()", Style); 21894 verifyFormat("operator const FooRight<Object> **()", Style); 21895 verifyFormat("operator const FooRight<Object> *&()", Style); 21896 verifyFormat("operator const FooRight<Object> *&&()", Style); 21897 21898 Style.PointerAlignment = FormatStyle::PAS_Left; 21899 verifyFormat("Foo::operator*();", Style); 21900 verifyFormat("Foo::operator**();", Style); 21901 verifyFormat("Foo::operator void*();", Style); 21902 verifyFormat("Foo::operator void**();", Style); 21903 verifyFormat("Foo::operator void*&();", Style); 21904 verifyFormat("Foo::operator void*&&();", Style); 21905 verifyFormat("Foo::operator void const*();", Style); 21906 verifyFormat("Foo::operator void const**();", Style); 21907 verifyFormat("Foo::operator void const*&();", Style); 21908 verifyFormat("Foo::operator void const*&&();", Style); 21909 verifyFormat("Foo::operator/*comment*/ void*();", Style); 21910 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 21911 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 21912 verifyFormat("Foo::operator()(void*);", Style); 21913 verifyFormat("Foo::operator*(void*);", Style); 21914 verifyFormat("Foo::operator*();", Style); 21915 verifyFormat("Foo::operator<int>*();", Style); 21916 verifyFormat("Foo::operator<Foo>*();", Style); 21917 verifyFormat("Foo::operator<int>**();", Style); 21918 verifyFormat("Foo::operator<Foo>**();", Style); 21919 verifyFormat("Foo::operator<Foo>*&();", Style); 21920 verifyFormat("Foo::operator<int>&();", Style); 21921 verifyFormat("Foo::operator<Foo>&();", Style); 21922 verifyFormat("Foo::operator<int>&&();", Style); 21923 verifyFormat("Foo::operator<Foo>&&();", Style); 21924 verifyFormat("Foo::operator<int>*&();", Style); 21925 verifyFormat("Foo::operator<Foo>*&();", Style); 21926 verifyFormat("operator*(int (*)(), class Foo);", Style); 21927 21928 verifyFormat("Foo::operator&();", Style); 21929 verifyFormat("Foo::operator void&();", Style); 21930 verifyFormat("Foo::operator void const&();", Style); 21931 verifyFormat("Foo::operator/*comment*/ void&();", Style); 21932 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 21933 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 21934 verifyFormat("Foo::operator()(void&);", Style); 21935 verifyFormat("Foo::operator&(void&);", Style); 21936 verifyFormat("Foo::operator&();", Style); 21937 verifyFormat("operator&(int (&)(), class Foo);", Style); 21938 21939 verifyFormat("Foo::operator&&();", Style); 21940 verifyFormat("Foo::operator void&&();", Style); 21941 verifyFormat("Foo::operator void const&&();", Style); 21942 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 21943 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 21944 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 21945 verifyFormat("Foo::operator()(void&&);", Style); 21946 verifyFormat("Foo::operator&&(void&&);", Style); 21947 verifyFormat("Foo::operator&&();", Style); 21948 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21949 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 21950 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 21951 Style); 21952 verifyFormat("operator void**()", Style); 21953 verifyFormat("operator const FooLeft<Object>&()", Style); 21954 verifyFormat("operator const FooLeft<Object>*()", Style); 21955 verifyFormat("operator const FooLeft<Object>**()", Style); 21956 verifyFormat("operator const FooLeft<Object>*&()", Style); 21957 verifyFormat("operator const FooLeft<Object>*&&()", Style); 21958 21959 // PR45107 21960 verifyFormat("operator Vector<String>&();", Style); 21961 verifyFormat("operator const Vector<String>&();", Style); 21962 verifyFormat("operator foo::Bar*();", Style); 21963 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 21964 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 21965 Style); 21966 21967 Style.PointerAlignment = FormatStyle::PAS_Middle; 21968 verifyFormat("Foo::operator*();", Style); 21969 verifyFormat("Foo::operator void *();", Style); 21970 verifyFormat("Foo::operator()(void *);", Style); 21971 verifyFormat("Foo::operator*(void *);", Style); 21972 verifyFormat("Foo::operator*();", Style); 21973 verifyFormat("operator*(int (*)(), class Foo);", Style); 21974 21975 verifyFormat("Foo::operator&();", Style); 21976 verifyFormat("Foo::operator void &();", Style); 21977 verifyFormat("Foo::operator void const &();", Style); 21978 verifyFormat("Foo::operator()(void &);", Style); 21979 verifyFormat("Foo::operator&(void &);", Style); 21980 verifyFormat("Foo::operator&();", Style); 21981 verifyFormat("operator&(int (&)(), class Foo);", Style); 21982 21983 verifyFormat("Foo::operator&&();", Style); 21984 verifyFormat("Foo::operator void &&();", Style); 21985 verifyFormat("Foo::operator void const &&();", Style); 21986 verifyFormat("Foo::operator()(void &&);", Style); 21987 verifyFormat("Foo::operator&&(void &&);", Style); 21988 verifyFormat("Foo::operator&&();", Style); 21989 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21990 } 21991 21992 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 21993 FormatStyle Style = getLLVMStyle(); 21994 // PR46157 21995 verifyFormat("foo(operator+, -42);", Style); 21996 verifyFormat("foo(operator++, -42);", Style); 21997 verifyFormat("foo(operator--, -42);", Style); 21998 verifyFormat("foo(-42, operator--);", Style); 21999 verifyFormat("foo(-42, operator, );", Style); 22000 verifyFormat("foo(operator, , -42);", Style); 22001 } 22002 22003 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 22004 FormatStyle Style = getLLVMStyle(); 22005 Style.WhitespaceSensitiveMacros.push_back("FOO"); 22006 22007 // Don't use the helpers here, since 'mess up' will change the whitespace 22008 // and these are all whitespace sensitive by definition 22009 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 22010 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 22011 EXPECT_EQ( 22012 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 22013 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 22014 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 22015 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 22016 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 22017 " Still=Intentional);", 22018 format("FOO(String-ized&Messy+But,: :\n" 22019 " Still=Intentional);", 22020 Style)); 22021 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 22022 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 22023 " Still=Intentional);", 22024 format("FOO(String-ized=&Messy+But,: :\n" 22025 " Still=Intentional);", 22026 Style)); 22027 22028 Style.ColumnLimit = 21; 22029 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 22030 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 22031 } 22032 22033 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 22034 // These tests are not in NamespaceFixer because that doesn't 22035 // test its interaction with line wrapping 22036 FormatStyle Style = getLLVMStyle(); 22037 Style.ColumnLimit = 80; 22038 verifyFormat("namespace {\n" 22039 "int i;\n" 22040 "int j;\n" 22041 "} // namespace", 22042 Style); 22043 22044 verifyFormat("namespace AAA {\n" 22045 "int i;\n" 22046 "int j;\n" 22047 "} // namespace AAA", 22048 Style); 22049 22050 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 22051 "int i;\n" 22052 "int j;\n" 22053 "} // namespace Averyveryveryverylongnamespace", 22054 format("namespace Averyveryveryverylongnamespace {\n" 22055 "int i;\n" 22056 "int j;\n" 22057 "}", 22058 Style)); 22059 22060 EXPECT_EQ( 22061 "namespace " 22062 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22063 " went::mad::now {\n" 22064 "int i;\n" 22065 "int j;\n" 22066 "} // namespace\n" 22067 " // " 22068 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22069 "went::mad::now", 22070 format("namespace " 22071 "would::it::save::you::a::lot::of::time::if_::i::" 22072 "just::gave::up::and_::went::mad::now {\n" 22073 "int i;\n" 22074 "int j;\n" 22075 "}", 22076 Style)); 22077 22078 // This used to duplicate the comment again and again on subsequent runs 22079 EXPECT_EQ( 22080 "namespace " 22081 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22082 " went::mad::now {\n" 22083 "int i;\n" 22084 "int j;\n" 22085 "} // namespace\n" 22086 " // " 22087 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22088 "went::mad::now", 22089 format("namespace " 22090 "would::it::save::you::a::lot::of::time::if_::i::" 22091 "just::gave::up::and_::went::mad::now {\n" 22092 "int i;\n" 22093 "int j;\n" 22094 "} // namespace\n" 22095 " // " 22096 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 22097 "and_::went::mad::now", 22098 Style)); 22099 } 22100 22101 TEST_F(FormatTest, LikelyUnlikely) { 22102 FormatStyle Style = getLLVMStyle(); 22103 22104 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22105 " return 29;\n" 22106 "}", 22107 Style); 22108 22109 verifyFormat("if (argc > 5) [[likely]] {\n" 22110 " return 29;\n" 22111 "}", 22112 Style); 22113 22114 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22115 " return 29;\n" 22116 "} else [[likely]] {\n" 22117 " return 42;\n" 22118 "}\n", 22119 Style); 22120 22121 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22122 " return 29;\n" 22123 "} else if (argc > 10) [[likely]] {\n" 22124 " return 99;\n" 22125 "} else {\n" 22126 " return 42;\n" 22127 "}\n", 22128 Style); 22129 22130 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 22131 " return 29;\n" 22132 "}", 22133 Style); 22134 } 22135 22136 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 22137 verifyFormat("Constructor()\n" 22138 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22139 " aaaa(aaaaaaaaaaaaaaaaaa, " 22140 "aaaaaaaaaaaaaaaaaat))"); 22141 verifyFormat("Constructor()\n" 22142 " : aaaaaaaaaaaaa(aaaaaa), " 22143 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 22144 22145 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 22146 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 22147 verifyFormat("Constructor()\n" 22148 " : aaaaaa(aaaaaa),\n" 22149 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22150 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 22151 StyleWithWhitespacePenalty); 22152 verifyFormat("Constructor()\n" 22153 " : aaaaaaaaaaaaa(aaaaaa), " 22154 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 22155 StyleWithWhitespacePenalty); 22156 } 22157 22158 TEST_F(FormatTest, LLVMDefaultStyle) { 22159 FormatStyle Style = getLLVMStyle(); 22160 verifyFormat("extern \"C\" {\n" 22161 "int foo();\n" 22162 "}", 22163 Style); 22164 } 22165 TEST_F(FormatTest, GNUDefaultStyle) { 22166 FormatStyle Style = getGNUStyle(); 22167 verifyFormat("extern \"C\"\n" 22168 "{\n" 22169 " int foo ();\n" 22170 "}", 22171 Style); 22172 } 22173 TEST_F(FormatTest, MozillaDefaultStyle) { 22174 FormatStyle Style = getMozillaStyle(); 22175 verifyFormat("extern \"C\"\n" 22176 "{\n" 22177 " int foo();\n" 22178 "}", 22179 Style); 22180 } 22181 TEST_F(FormatTest, GoogleDefaultStyle) { 22182 FormatStyle Style = getGoogleStyle(); 22183 verifyFormat("extern \"C\" {\n" 22184 "int foo();\n" 22185 "}", 22186 Style); 22187 } 22188 TEST_F(FormatTest, ChromiumDefaultStyle) { 22189 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 22190 verifyFormat("extern \"C\" {\n" 22191 "int foo();\n" 22192 "}", 22193 Style); 22194 } 22195 TEST_F(FormatTest, MicrosoftDefaultStyle) { 22196 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 22197 verifyFormat("extern \"C\"\n" 22198 "{\n" 22199 " int foo();\n" 22200 "}", 22201 Style); 22202 } 22203 TEST_F(FormatTest, WebKitDefaultStyle) { 22204 FormatStyle Style = getWebKitStyle(); 22205 verifyFormat("extern \"C\" {\n" 22206 "int foo();\n" 22207 "}", 22208 Style); 22209 } 22210 22211 TEST_F(FormatTest, ConceptsAndRequires) { 22212 FormatStyle Style = getLLVMStyle(); 22213 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 22214 22215 verifyFormat("template <typename T>\n" 22216 "concept Hashable = requires(T a) {\n" 22217 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22218 "};", 22219 Style); 22220 verifyFormat("template <typename T>\n" 22221 "concept EqualityComparable = requires(T a, T b) {\n" 22222 " { a == b } -> bool;\n" 22223 "};", 22224 Style); 22225 verifyFormat("template <typename T>\n" 22226 "concept EqualityComparable = requires(T a, T b) {\n" 22227 " { a == b } -> bool;\n" 22228 " { a != b } -> bool;\n" 22229 "};", 22230 Style); 22231 verifyFormat("template <typename T>\n" 22232 "concept EqualityComparable = requires(T a, T b) {\n" 22233 " { a == b } -> bool;\n" 22234 " { a != b } -> bool;\n" 22235 "};", 22236 Style); 22237 22238 verifyFormat("template <typename It>\n" 22239 "requires Iterator<It>\n" 22240 "void sort(It begin, It end) {\n" 22241 " //....\n" 22242 "}", 22243 Style); 22244 22245 verifyFormat("template <typename T>\n" 22246 "concept Large = sizeof(T) > 10;", 22247 Style); 22248 22249 verifyFormat("template <typename T, typename U>\n" 22250 "concept FooableWith = requires(T t, U u) {\n" 22251 " typename T::foo_type;\n" 22252 " { t.foo(u) } -> typename T::foo_type;\n" 22253 " t++;\n" 22254 "};\n" 22255 "void doFoo(FooableWith<int> auto t) {\n" 22256 " t.foo(3);\n" 22257 "}", 22258 Style); 22259 verifyFormat("template <typename T>\n" 22260 "concept Context = sizeof(T) == 1;", 22261 Style); 22262 verifyFormat("template <typename T>\n" 22263 "concept Context = is_specialization_of_v<context, T>;", 22264 Style); 22265 verifyFormat("template <typename T>\n" 22266 "concept Node = std::is_object_v<T>;", 22267 Style); 22268 verifyFormat("template <typename T>\n" 22269 "concept Tree = true;", 22270 Style); 22271 22272 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 22273 " //...\n" 22274 "}", 22275 Style); 22276 22277 verifyFormat( 22278 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 22279 " //...\n" 22280 "}", 22281 Style); 22282 22283 verifyFormat( 22284 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 22285 " //...\n" 22286 "}", 22287 Style); 22288 22289 verifyFormat("template <typename T>\n" 22290 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 22291 "Concept2<I> {\n" 22292 " //...\n" 22293 "}", 22294 Style); 22295 22296 verifyFormat("template <typename T>\n" 22297 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 22298 "Concept2<I> {\n" 22299 " //...\n" 22300 "}", 22301 Style); 22302 22303 verifyFormat( 22304 "template <typename T>\n" 22305 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 22306 " //...\n" 22307 "}", 22308 Style); 22309 22310 verifyFormat( 22311 "template <typename T>\n" 22312 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 22313 " //...\n" 22314 "}", 22315 Style); 22316 22317 verifyFormat("template <typename It>\n" 22318 "requires Foo<It>() && Bar<It> {\n" 22319 " //....\n" 22320 "}", 22321 Style); 22322 22323 verifyFormat("template <typename It>\n" 22324 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 22325 " //....\n" 22326 "}", 22327 Style); 22328 22329 verifyFormat("template <typename It>\n" 22330 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 22331 " //....\n" 22332 "}", 22333 Style); 22334 22335 verifyFormat( 22336 "template <typename It>\n" 22337 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 22338 " //....\n" 22339 "}", 22340 Style); 22341 22342 Style.IndentRequires = true; 22343 verifyFormat("template <typename It>\n" 22344 " requires Iterator<It>\n" 22345 "void sort(It begin, It end) {\n" 22346 " //....\n" 22347 "}", 22348 Style); 22349 verifyFormat("template <std::size index_>\n" 22350 " requires(index_ < sizeof...(Children_))\n" 22351 "Tree auto &child() {\n" 22352 " // ...\n" 22353 "}", 22354 Style); 22355 22356 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 22357 verifyFormat("template <typename T>\n" 22358 "concept Hashable = requires (T a) {\n" 22359 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22360 "};", 22361 Style); 22362 22363 verifyFormat("template <class T = void>\n" 22364 " requires EqualityComparable<T> || Same<T, void>\n" 22365 "struct equal_to;", 22366 Style); 22367 22368 verifyFormat("template <class T>\n" 22369 " requires requires {\n" 22370 " T{};\n" 22371 " T (int);\n" 22372 " }\n", 22373 Style); 22374 22375 Style.ColumnLimit = 78; 22376 verifyFormat("template <typename T>\n" 22377 "concept Context = Traits<typename T::traits_type> and\n" 22378 " Interface<typename T::interface_type> and\n" 22379 " Request<typename T::request_type> and\n" 22380 " Response<typename T::response_type> and\n" 22381 " ContextExtension<typename T::extension_type> and\n" 22382 " ::std::is_copy_constructable<T> and " 22383 "::std::is_move_constructable<T> and\n" 22384 " requires (T c) {\n" 22385 " { c.response; } -> Response;\n" 22386 "} and requires (T c) {\n" 22387 " { c.request; } -> Request;\n" 22388 "}\n", 22389 Style); 22390 22391 verifyFormat("template <typename T>\n" 22392 "concept Context = Traits<typename T::traits_type> or\n" 22393 " Interface<typename T::interface_type> or\n" 22394 " Request<typename T::request_type> or\n" 22395 " Response<typename T::response_type> or\n" 22396 " ContextExtension<typename T::extension_type> or\n" 22397 " ::std::is_copy_constructable<T> or " 22398 "::std::is_move_constructable<T> or\n" 22399 " requires (T c) {\n" 22400 " { c.response; } -> Response;\n" 22401 "} or requires (T c) {\n" 22402 " { c.request; } -> Request;\n" 22403 "}\n", 22404 Style); 22405 22406 verifyFormat("template <typename T>\n" 22407 "concept Context = Traits<typename T::traits_type> &&\n" 22408 " Interface<typename T::interface_type> &&\n" 22409 " Request<typename T::request_type> &&\n" 22410 " Response<typename T::response_type> &&\n" 22411 " ContextExtension<typename T::extension_type> &&\n" 22412 " ::std::is_copy_constructable<T> && " 22413 "::std::is_move_constructable<T> &&\n" 22414 " requires (T c) {\n" 22415 " { c.response; } -> Response;\n" 22416 "} && requires (T c) {\n" 22417 " { c.request; } -> Request;\n" 22418 "}\n", 22419 Style); 22420 22421 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 22422 "Constraint2<T>;"); 22423 22424 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 22425 Style.BraceWrapping.AfterFunction = true; 22426 Style.BraceWrapping.AfterClass = true; 22427 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 22428 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 22429 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22430 "{\n" 22431 " return\n" 22432 "}\n", 22433 Style); 22434 22435 verifyFormat("void Foo () requires std::copyable<T>\n" 22436 "{\n" 22437 " return\n" 22438 "}\n", 22439 Style); 22440 22441 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22442 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22443 "struct constant;", 22444 Style); 22445 22446 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22447 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22448 "struct constant;", 22449 Style); 22450 22451 verifyFormat("template <class T>\n" 22452 "class plane_with_very_very_very_long_name\n" 22453 "{\n" 22454 " constexpr plane_with_very_very_very_long_name () requires " 22455 "std::copyable<T>\n" 22456 " : plane_with_very_very_very_long_name (1)\n" 22457 " {\n" 22458 " }\n" 22459 "}\n", 22460 Style); 22461 22462 verifyFormat("template <class T>\n" 22463 "class plane_with_long_name\n" 22464 "{\n" 22465 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22466 " : plane_with_long_name (1)\n" 22467 " {\n" 22468 " }\n" 22469 "}\n", 22470 Style); 22471 22472 Style.BreakBeforeConceptDeclarations = false; 22473 verifyFormat("template <typename T> concept Tree = true;", Style); 22474 22475 Style.IndentRequires = false; 22476 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22477 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22478 "struct constant;", 22479 Style); 22480 } 22481 22482 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22483 FormatStyle Style = getLLVMStyle(); 22484 StringRef Source = "void Foo::slot() {\n" 22485 " unsigned char MyChar = 'x';\n" 22486 " emit signal(MyChar);\n" 22487 " Q_EMIT signal(MyChar);\n" 22488 "}"; 22489 22490 EXPECT_EQ(Source, format(Source, Style)); 22491 22492 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22493 EXPECT_EQ("void Foo::slot() {\n" 22494 " unsigned char MyChar = 'x';\n" 22495 " emit signal(MyChar);\n" 22496 " Q_EMIT signal(MyChar);\n" 22497 "}", 22498 format(Source, Style)); 22499 22500 Style.StatementAttributeLikeMacros.push_back("emit"); 22501 EXPECT_EQ(Source, format(Source, Style)); 22502 22503 Style.StatementAttributeLikeMacros = {}; 22504 EXPECT_EQ("void Foo::slot() {\n" 22505 " unsigned char MyChar = 'x';\n" 22506 " emit signal(MyChar);\n" 22507 " Q_EMIT signal(MyChar);\n" 22508 "}", 22509 format(Source, Style)); 22510 } 22511 22512 TEST_F(FormatTest, IndentAccessModifiers) { 22513 FormatStyle Style = getLLVMStyle(); 22514 Style.IndentAccessModifiers = true; 22515 // Members are *two* levels below the record; 22516 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22517 verifyFormat("class C {\n" 22518 " int i;\n" 22519 "};\n", 22520 Style); 22521 verifyFormat("union C {\n" 22522 " int i;\n" 22523 " unsigned u;\n" 22524 "};\n", 22525 Style); 22526 // Access modifiers should be indented one level below the record. 22527 verifyFormat("class C {\n" 22528 " public:\n" 22529 " int i;\n" 22530 "};\n", 22531 Style); 22532 verifyFormat("struct S {\n" 22533 " private:\n" 22534 " class C {\n" 22535 " int j;\n" 22536 "\n" 22537 " public:\n" 22538 " C();\n" 22539 " };\n" 22540 "\n" 22541 " public:\n" 22542 " int i;\n" 22543 "};\n", 22544 Style); 22545 // Enumerations are not records and should be unaffected. 22546 Style.AllowShortEnumsOnASingleLine = false; 22547 verifyFormat("enum class E {\n" 22548 " A,\n" 22549 " B\n" 22550 "};\n", 22551 Style); 22552 // Test with a different indentation width; 22553 // also proves that the result is Style.AccessModifierOffset agnostic. 22554 Style.IndentWidth = 3; 22555 verifyFormat("class C {\n" 22556 " public:\n" 22557 " int i;\n" 22558 "};\n", 22559 Style); 22560 } 22561 22562 TEST_F(FormatTest, LimitlessStringsAndComments) { 22563 auto Style = getLLVMStyleWithColumns(0); 22564 constexpr StringRef Code = 22565 "/**\n" 22566 " * This is a multiline comment with quite some long lines, at least for " 22567 "the LLVM Style.\n" 22568 " * We will redo this with strings and line comments. Just to check if " 22569 "everything is working.\n" 22570 " */\n" 22571 "bool foo() {\n" 22572 " /* Single line multi line comment. */\n" 22573 " const std::string String = \"This is a multiline string with quite " 22574 "some long lines, at least for the LLVM Style.\"\n" 22575 " \"We already did it with multi line " 22576 "comments, and we will do it with line comments. Just to check if " 22577 "everything is working.\";\n" 22578 " // This is a line comment (block) with quite some long lines, at " 22579 "least for the LLVM Style.\n" 22580 " // We already did this with multi line comments and strings. Just to " 22581 "check if everything is working.\n" 22582 " const std::string SmallString = \"Hello World\";\n" 22583 " // Small line comment\n" 22584 " return String.size() > SmallString.size();\n" 22585 "}"; 22586 EXPECT_EQ(Code, format(Code, Style)); 22587 } 22588 22589 TEST_F(FormatTest, FormatDecayCopy) { 22590 // error cases from unit tests 22591 verifyFormat("foo(auto())"); 22592 verifyFormat("foo(auto{})"); 22593 verifyFormat("foo(auto({}))"); 22594 verifyFormat("foo(auto{{}})"); 22595 22596 verifyFormat("foo(auto(1))"); 22597 verifyFormat("foo(auto{1})"); 22598 verifyFormat("foo(new auto(1))"); 22599 verifyFormat("foo(new auto{1})"); 22600 verifyFormat("decltype(auto(1)) x;"); 22601 verifyFormat("decltype(auto{1}) x;"); 22602 verifyFormat("auto(x);"); 22603 verifyFormat("auto{x};"); 22604 verifyFormat("new auto{x};"); 22605 verifyFormat("auto{x} = y;"); 22606 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 22607 // the user's own fault 22608 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 22609 // clearly the user's own fault 22610 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 22611 } 22612 22613 } // namespace 22614 } // namespace format 22615 } // namespace clang 22616