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 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5411 Style.BraceWrapping.BeforeElse = false; 5412 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5413 verifyFormat("if (a)\n" 5414 "{\n" 5415 "} else if (b)\n" 5416 "{\n" 5417 "} else\n" 5418 "{ }", 5419 Style); 5420 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 5421 verifyFormat("if (a) {\n" 5422 "} else if (b) {\n" 5423 "} else {\n" 5424 "}", 5425 Style); 5426 Style.BraceWrapping.BeforeElse = true; 5427 verifyFormat("if (a) { }\n" 5428 "else if (b) { }\n" 5429 "else { }", 5430 Style); 5431 } 5432 5433 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5434 FormatStyle Style = getLLVMStyle(); 5435 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5436 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5437 verifyFormat("FOO_BEGIN\n" 5438 " FOO_ENTRY\n" 5439 "FOO_END", 5440 Style); 5441 verifyFormat("FOO_BEGIN\n" 5442 " NESTED_FOO_BEGIN\n" 5443 " NESTED_FOO_ENTRY\n" 5444 " NESTED_FOO_END\n" 5445 "FOO_END", 5446 Style); 5447 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5448 " int x;\n" 5449 " x = 1;\n" 5450 "FOO_END(Baz)", 5451 Style); 5452 } 5453 5454 //===----------------------------------------------------------------------===// 5455 // Line break tests. 5456 //===----------------------------------------------------------------------===// 5457 5458 TEST_F(FormatTest, PreventConfusingIndents) { 5459 verifyFormat( 5460 "void f() {\n" 5461 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5462 " parameter, parameter, parameter)),\n" 5463 " SecondLongCall(parameter));\n" 5464 "}"); 5465 verifyFormat( 5466 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5467 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5469 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5470 verifyFormat( 5471 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5472 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5473 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5474 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5475 verifyFormat( 5476 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5477 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5479 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5480 verifyFormat("int a = bbbb && ccc &&\n" 5481 " fffff(\n" 5482 "#define A Just forcing a new line\n" 5483 " ddd);"); 5484 } 5485 5486 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5487 verifyFormat( 5488 "bool aaaaaaa =\n" 5489 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5490 " bbbbbbbb();"); 5491 verifyFormat( 5492 "bool aaaaaaa =\n" 5493 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5494 " bbbbbbbb();"); 5495 5496 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5498 " ccccccccc == ddddddddddd;"); 5499 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5501 " ccccccccc == ddddddddddd;"); 5502 verifyFormat( 5503 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5505 " ccccccccc == ddddddddddd;"); 5506 5507 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5508 " aaaaaa) &&\n" 5509 " bbbbbb && cccccc;"); 5510 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5511 " aaaaaa) >>\n" 5512 " bbbbbb;"); 5513 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5514 " SourceMgr.getSpellingColumnNumber(\n" 5515 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5516 " 1);"); 5517 5518 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5519 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5520 " cccccc) {\n}"); 5521 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5522 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5523 " cccccc) {\n}"); 5524 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5525 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5526 " cccccc) {\n}"); 5527 verifyFormat("b = a &&\n" 5528 " // Comment\n" 5529 " b.c && d;"); 5530 5531 // If the LHS of a comparison is not a binary expression itself, the 5532 // additional linebreak confuses many people. 5533 verifyFormat( 5534 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5536 "}"); 5537 verifyFormat( 5538 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5539 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5540 "}"); 5541 verifyFormat( 5542 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5543 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5544 "}"); 5545 verifyFormat( 5546 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5547 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5548 "}"); 5549 // Even explicit parentheses stress the precedence enough to make the 5550 // additional break unnecessary. 5551 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5553 "}"); 5554 // This cases is borderline, but with the indentation it is still readable. 5555 verifyFormat( 5556 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5557 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5559 "}", 5560 getLLVMStyleWithColumns(75)); 5561 5562 // If the LHS is a binary expression, we should still use the additional break 5563 // as otherwise the formatting hides the operator precedence. 5564 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5566 " 5) {\n" 5567 "}"); 5568 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5570 " 5) {\n" 5571 "}"); 5572 5573 FormatStyle OnePerLine = getLLVMStyle(); 5574 OnePerLine.BinPackParameters = false; 5575 verifyFormat( 5576 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5579 OnePerLine); 5580 5581 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5582 " .aaa(aaaaaaaaaaaaa) *\n" 5583 " aaaaaaa +\n" 5584 " aaaaaaa;", 5585 getLLVMStyleWithColumns(40)); 5586 } 5587 5588 TEST_F(FormatTest, ExpressionIndentation) { 5589 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5590 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5593 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5594 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5597 " ccccccccccccccccccccccccccccccccccccccccc;"); 5598 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5599 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5600 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5601 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5602 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5605 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5606 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5608 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5609 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5610 verifyFormat("if () {\n" 5611 "} else if (aaaaa && bbbbb > // break\n" 5612 " ccccc) {\n" 5613 "}"); 5614 verifyFormat("if () {\n" 5615 "} else if constexpr (aaaaa && bbbbb > // break\n" 5616 " ccccc) {\n" 5617 "}"); 5618 verifyFormat("if () {\n" 5619 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 5620 " ccccc) {\n" 5621 "}"); 5622 verifyFormat("if () {\n" 5623 "} else if (aaaaa &&\n" 5624 " bbbbb > // break\n" 5625 " ccccc &&\n" 5626 " ddddd) {\n" 5627 "}"); 5628 5629 // Presence of a trailing comment used to change indentation of b. 5630 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 5631 " b;\n" 5632 "return aaaaaaaaaaaaaaaaaaa +\n" 5633 " b; //", 5634 getLLVMStyleWithColumns(30)); 5635 } 5636 5637 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 5638 // Not sure what the best system is here. Like this, the LHS can be found 5639 // immediately above an operator (everything with the same or a higher 5640 // indent). The RHS is aligned right of the operator and so compasses 5641 // everything until something with the same indent as the operator is found. 5642 // FIXME: Is this a good system? 5643 FormatStyle Style = getLLVMStyle(); 5644 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5645 verifyFormat( 5646 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5647 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5648 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5649 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5650 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5651 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5652 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5653 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5654 " > ccccccccccccccccccccccccccccccccccccccccc;", 5655 Style); 5656 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5657 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5658 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5659 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5660 Style); 5661 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5662 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5663 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5664 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5665 Style); 5666 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5667 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5668 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5669 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5670 Style); 5671 verifyFormat("if () {\n" 5672 "} else if (aaaaa\n" 5673 " && bbbbb // break\n" 5674 " > ccccc) {\n" 5675 "}", 5676 Style); 5677 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5678 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5679 Style); 5680 verifyFormat("return (a)\n" 5681 " // comment\n" 5682 " + b;", 5683 Style); 5684 verifyFormat( 5685 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5686 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5687 " + cc;", 5688 Style); 5689 5690 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5691 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5692 Style); 5693 5694 // Forced by comments. 5695 verifyFormat( 5696 "unsigned ContentSize =\n" 5697 " sizeof(int16_t) // DWARF ARange version number\n" 5698 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5699 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5700 " + sizeof(int8_t); // Segment Size (in bytes)"); 5701 5702 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5703 " == boost::fusion::at_c<1>(iiii).second;", 5704 Style); 5705 5706 Style.ColumnLimit = 60; 5707 verifyFormat("zzzzzzzzzz\n" 5708 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5709 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5710 Style); 5711 5712 Style.ColumnLimit = 80; 5713 Style.IndentWidth = 4; 5714 Style.TabWidth = 4; 5715 Style.UseTab = FormatStyle::UT_Always; 5716 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5717 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5718 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 5719 "\t&& (someOtherLongishConditionPart1\n" 5720 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 5721 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 5722 "(someOtherLongishConditionPart1 || " 5723 "someOtherEvenLongerNestedConditionPart2);", 5724 Style)); 5725 } 5726 5727 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 5728 FormatStyle Style = getLLVMStyle(); 5729 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5730 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 5731 5732 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5733 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5734 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5735 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5736 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5737 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5738 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5739 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5740 " > ccccccccccccccccccccccccccccccccccccccccc;", 5741 Style); 5742 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5743 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5744 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5745 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5746 Style); 5747 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5748 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5749 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5750 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5751 Style); 5752 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5753 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5754 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5755 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5756 Style); 5757 verifyFormat("if () {\n" 5758 "} else if (aaaaa\n" 5759 " && bbbbb // break\n" 5760 " > ccccc) {\n" 5761 "}", 5762 Style); 5763 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5764 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5765 Style); 5766 verifyFormat("return (a)\n" 5767 " // comment\n" 5768 " + b;", 5769 Style); 5770 verifyFormat( 5771 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5772 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5773 " + cc;", 5774 Style); 5775 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 5776 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5777 " : 3333333333333333;", 5778 Style); 5779 verifyFormat( 5780 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 5781 " : ccccccccccccccc ? dddddddddddddddddd\n" 5782 " : eeeeeeeeeeeeeeeeee)\n" 5783 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5784 " : 3333333333333333;", 5785 Style); 5786 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5787 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5788 Style); 5789 5790 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5791 " == boost::fusion::at_c<1>(iiii).second;", 5792 Style); 5793 5794 Style.ColumnLimit = 60; 5795 verifyFormat("zzzzzzzzzzzzz\n" 5796 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5797 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5798 Style); 5799 5800 // Forced by comments. 5801 Style.ColumnLimit = 80; 5802 verifyFormat( 5803 "unsigned ContentSize\n" 5804 " = sizeof(int16_t) // DWARF ARange version number\n" 5805 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5806 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5807 " + sizeof(int8_t); // Segment Size (in bytes)", 5808 Style); 5809 5810 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5811 verifyFormat( 5812 "unsigned ContentSize =\n" 5813 " sizeof(int16_t) // DWARF ARange version number\n" 5814 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5815 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5816 " + sizeof(int8_t); // Segment Size (in bytes)", 5817 Style); 5818 5819 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5820 verifyFormat( 5821 "unsigned ContentSize =\n" 5822 " sizeof(int16_t) // DWARF ARange version number\n" 5823 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5824 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5825 " + sizeof(int8_t); // Segment Size (in bytes)", 5826 Style); 5827 } 5828 5829 TEST_F(FormatTest, EnforcedOperatorWraps) { 5830 // Here we'd like to wrap after the || operators, but a comment is forcing an 5831 // earlier wrap. 5832 verifyFormat("bool x = aaaaa //\n" 5833 " || bbbbb\n" 5834 " //\n" 5835 " || cccc;"); 5836 } 5837 5838 TEST_F(FormatTest, NoOperandAlignment) { 5839 FormatStyle Style = getLLVMStyle(); 5840 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5841 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 5842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5843 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5844 Style); 5845 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5846 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5847 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5848 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5849 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5850 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5851 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5852 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5853 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5854 " > ccccccccccccccccccccccccccccccccccccccccc;", 5855 Style); 5856 5857 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5858 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5859 " + cc;", 5860 Style); 5861 verifyFormat("int a = aa\n" 5862 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5863 " * cccccccccccccccccccccccccccccccccccc;\n", 5864 Style); 5865 5866 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5867 verifyFormat("return (a > b\n" 5868 " // comment1\n" 5869 " // comment2\n" 5870 " || c);", 5871 Style); 5872 } 5873 5874 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 5875 FormatStyle Style = getLLVMStyle(); 5876 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5877 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5879 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5880 Style); 5881 } 5882 5883 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 5884 FormatStyle Style = getLLVMStyle(); 5885 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5886 Style.BinPackArguments = false; 5887 Style.ColumnLimit = 40; 5888 verifyFormat("void test() {\n" 5889 " someFunction(\n" 5890 " this + argument + is + quite\n" 5891 " + long + so + it + gets + wrapped\n" 5892 " + but + remains + bin - packed);\n" 5893 "}", 5894 Style); 5895 verifyFormat("void test() {\n" 5896 " someFunction(arg1,\n" 5897 " this + argument + is\n" 5898 " + quite + long + so\n" 5899 " + it + gets + wrapped\n" 5900 " + but + remains + bin\n" 5901 " - packed,\n" 5902 " arg3);\n" 5903 "}", 5904 Style); 5905 verifyFormat("void test() {\n" 5906 " someFunction(\n" 5907 " arg1,\n" 5908 " this + argument + has\n" 5909 " + anotherFunc(nested,\n" 5910 " calls + whose\n" 5911 " + arguments\n" 5912 " + are + also\n" 5913 " + wrapped,\n" 5914 " in + addition)\n" 5915 " + to + being + bin - packed,\n" 5916 " arg3);\n" 5917 "}", 5918 Style); 5919 5920 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5921 verifyFormat("void test() {\n" 5922 " someFunction(\n" 5923 " arg1,\n" 5924 " this + argument + has +\n" 5925 " anotherFunc(nested,\n" 5926 " calls + whose +\n" 5927 " arguments +\n" 5928 " are + also +\n" 5929 " wrapped,\n" 5930 " in + addition) +\n" 5931 " to + being + bin - packed,\n" 5932 " arg3);\n" 5933 "}", 5934 Style); 5935 } 5936 5937 TEST_F(FormatTest, ConstructorInitializers) { 5938 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5939 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 5940 getLLVMStyleWithColumns(45)); 5941 verifyFormat("Constructor()\n" 5942 " : Inttializer(FitsOnTheLine) {}", 5943 getLLVMStyleWithColumns(44)); 5944 verifyFormat("Constructor()\n" 5945 " : Inttializer(FitsOnTheLine) {}", 5946 getLLVMStyleWithColumns(43)); 5947 5948 verifyFormat("template <typename T>\n" 5949 "Constructor() : Initializer(FitsOnTheLine) {}", 5950 getLLVMStyleWithColumns(45)); 5951 5952 verifyFormat( 5953 "SomeClass::Constructor()\n" 5954 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5955 5956 verifyFormat( 5957 "SomeClass::Constructor()\n" 5958 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5959 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 5960 verifyFormat( 5961 "SomeClass::Constructor()\n" 5962 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5963 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5964 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5965 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5966 " : aaaaaaaaaa(aaaaaa) {}"); 5967 5968 verifyFormat("Constructor()\n" 5969 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5970 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5971 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5972 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 5973 5974 verifyFormat("Constructor()\n" 5975 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5976 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5977 5978 verifyFormat("Constructor(int Parameter = 0)\n" 5979 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5980 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 5981 verifyFormat("Constructor()\n" 5982 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5983 "}", 5984 getLLVMStyleWithColumns(60)); 5985 verifyFormat("Constructor()\n" 5986 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5987 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 5988 5989 // Here a line could be saved by splitting the second initializer onto two 5990 // lines, but that is not desirable. 5991 verifyFormat("Constructor()\n" 5992 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5993 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5994 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5995 5996 FormatStyle OnePerLine = getLLVMStyle(); 5997 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 5998 verifyFormat("MyClass::MyClass()\n" 5999 " : a(a),\n" 6000 " b(b),\n" 6001 " c(c) {}", 6002 OnePerLine); 6003 verifyFormat("MyClass::MyClass()\n" 6004 " : a(a), // comment\n" 6005 " b(b),\n" 6006 " c(c) {}", 6007 OnePerLine); 6008 verifyFormat("MyClass::MyClass(int a)\n" 6009 " : b(a), // comment\n" 6010 " c(a + 1) { // lined up\n" 6011 "}", 6012 OnePerLine); 6013 verifyFormat("Constructor()\n" 6014 " : a(b, b, b) {}", 6015 OnePerLine); 6016 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6017 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 6018 verifyFormat("SomeClass::Constructor()\n" 6019 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6020 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6021 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6022 OnePerLine); 6023 verifyFormat("SomeClass::Constructor()\n" 6024 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6025 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6026 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6027 OnePerLine); 6028 verifyFormat("MyClass::MyClass(int var)\n" 6029 " : some_var_(var), // 4 space indent\n" 6030 " some_other_var_(var + 1) { // lined up\n" 6031 "}", 6032 OnePerLine); 6033 verifyFormat("Constructor()\n" 6034 " : aaaaa(aaaaaa),\n" 6035 " aaaaa(aaaaaa),\n" 6036 " aaaaa(aaaaaa),\n" 6037 " aaaaa(aaaaaa),\n" 6038 " aaaaa(aaaaaa) {}", 6039 OnePerLine); 6040 verifyFormat("Constructor()\n" 6041 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6042 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6043 OnePerLine); 6044 OnePerLine.BinPackParameters = false; 6045 verifyFormat( 6046 "Constructor()\n" 6047 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6048 " aaaaaaaaaaa().aaa(),\n" 6049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6050 OnePerLine); 6051 OnePerLine.ColumnLimit = 60; 6052 verifyFormat("Constructor()\n" 6053 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6054 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6055 OnePerLine); 6056 6057 EXPECT_EQ("Constructor()\n" 6058 " : // Comment forcing unwanted break.\n" 6059 " aaaa(aaaa) {}", 6060 format("Constructor() :\n" 6061 " // Comment forcing unwanted break.\n" 6062 " aaaa(aaaa) {}")); 6063 } 6064 6065 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6066 FormatStyle Style = getLLVMStyle(); 6067 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6068 Style.ColumnLimit = 60; 6069 Style.BinPackParameters = false; 6070 6071 for (int i = 0; i < 4; ++i) { 6072 // Test all combinations of parameters that should not have an effect. 6073 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6074 Style.AllowAllArgumentsOnNextLine = i & 2; 6075 6076 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6077 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6078 verifyFormat("Constructor()\n" 6079 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6080 Style); 6081 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6082 6083 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6084 verifyFormat("Constructor()\n" 6085 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6086 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6087 Style); 6088 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6089 6090 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6091 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6092 verifyFormat("Constructor()\n" 6093 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6094 Style); 6095 6096 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6097 verifyFormat("Constructor()\n" 6098 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6099 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6100 Style); 6101 6102 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6103 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6104 verifyFormat("Constructor() :\n" 6105 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6106 Style); 6107 6108 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6109 verifyFormat("Constructor() :\n" 6110 " aaaaaaaaaaaaaaaaaa(a),\n" 6111 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6112 Style); 6113 } 6114 6115 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6116 // AllowAllConstructorInitializersOnNextLine in all 6117 // BreakConstructorInitializers modes 6118 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6119 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6120 verifyFormat("SomeClassWithALongName::Constructor(\n" 6121 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6122 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6123 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6124 Style); 6125 6126 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6127 verifyFormat("SomeClassWithALongName::Constructor(\n" 6128 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6129 " int bbbbbbbbbbbbb,\n" 6130 " int cccccccccccccccc)\n" 6131 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6132 Style); 6133 6134 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6135 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6136 verifyFormat("SomeClassWithALongName::Constructor(\n" 6137 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6138 " int bbbbbbbbbbbbb)\n" 6139 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6140 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6141 Style); 6142 6143 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6144 6145 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6146 verifyFormat("SomeClassWithALongName::Constructor(\n" 6147 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6148 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6149 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6150 Style); 6151 6152 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6153 verifyFormat("SomeClassWithALongName::Constructor(\n" 6154 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6155 " int bbbbbbbbbbbbb,\n" 6156 " int cccccccccccccccc)\n" 6157 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6158 Style); 6159 6160 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6161 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6162 verifyFormat("SomeClassWithALongName::Constructor(\n" 6163 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6164 " int bbbbbbbbbbbbb)\n" 6165 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6166 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6167 Style); 6168 6169 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6170 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6171 verifyFormat("SomeClassWithALongName::Constructor(\n" 6172 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6173 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6174 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6175 Style); 6176 6177 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6178 verifyFormat("SomeClassWithALongName::Constructor(\n" 6179 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6180 " int bbbbbbbbbbbbb,\n" 6181 " int cccccccccccccccc) :\n" 6182 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6183 Style); 6184 6185 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6186 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6187 verifyFormat("SomeClassWithALongName::Constructor(\n" 6188 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6189 " int bbbbbbbbbbbbb) :\n" 6190 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6191 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6192 Style); 6193 } 6194 6195 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6196 FormatStyle Style = getLLVMStyle(); 6197 Style.ColumnLimit = 60; 6198 Style.BinPackArguments = false; 6199 for (int i = 0; i < 4; ++i) { 6200 // Test all combinations of parameters that should not have an effect. 6201 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6202 Style.PackConstructorInitializers = 6203 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6204 6205 Style.AllowAllArgumentsOnNextLine = true; 6206 verifyFormat("void foo() {\n" 6207 " FunctionCallWithReallyLongName(\n" 6208 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6209 "}", 6210 Style); 6211 Style.AllowAllArgumentsOnNextLine = false; 6212 verifyFormat("void foo() {\n" 6213 " FunctionCallWithReallyLongName(\n" 6214 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6215 " bbbbbbbbbbbb);\n" 6216 "}", 6217 Style); 6218 6219 Style.AllowAllArgumentsOnNextLine = true; 6220 verifyFormat("void foo() {\n" 6221 " auto VariableWithReallyLongName = {\n" 6222 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6223 "}", 6224 Style); 6225 Style.AllowAllArgumentsOnNextLine = false; 6226 verifyFormat("void foo() {\n" 6227 " auto VariableWithReallyLongName = {\n" 6228 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6229 " bbbbbbbbbbbb};\n" 6230 "}", 6231 Style); 6232 } 6233 6234 // This parameter should not affect declarations. 6235 Style.BinPackParameters = false; 6236 Style.AllowAllArgumentsOnNextLine = false; 6237 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6238 verifyFormat("void FunctionCallWithReallyLongName(\n" 6239 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6240 Style); 6241 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6242 verifyFormat("void FunctionCallWithReallyLongName(\n" 6243 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6244 " int bbbbbbbbbbbb);", 6245 Style); 6246 } 6247 6248 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6249 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6250 // and BAS_Align. 6251 auto Style = getLLVMStyle(); 6252 Style.ColumnLimit = 35; 6253 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6254 "void functionDecl(int A, int B, int C);"; 6255 Style.AllowAllArgumentsOnNextLine = false; 6256 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6257 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6258 " paramC);\n" 6259 "void functionDecl(int A, int B,\n" 6260 " int C);"), 6261 format(Input, Style)); 6262 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6263 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6264 " paramC);\n" 6265 "void functionDecl(int A, int B,\n" 6266 " int C);"), 6267 format(Input, Style)); 6268 // However, BAS_AlwaysBreak should take precedence over 6269 // AllowAllArgumentsOnNextLine. 6270 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6271 EXPECT_EQ(StringRef("functionCall(\n" 6272 " paramA, paramB, paramC);\n" 6273 "void functionDecl(\n" 6274 " int A, int B, int C);"), 6275 format(Input, Style)); 6276 6277 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6278 // first argument. 6279 Style.AllowAllArgumentsOnNextLine = true; 6280 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6281 EXPECT_EQ(StringRef("functionCall(\n" 6282 " paramA, paramB, paramC);\n" 6283 "void functionDecl(\n" 6284 " int A, int B, int C);"), 6285 format(Input, Style)); 6286 // It wouldn't fit on one line with aligned parameters so this setting 6287 // doesn't change anything for BAS_Align. 6288 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6289 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6290 " paramC);\n" 6291 "void functionDecl(int A, int B,\n" 6292 " int C);"), 6293 format(Input, Style)); 6294 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6295 EXPECT_EQ(StringRef("functionCall(\n" 6296 " paramA, paramB, paramC);\n" 6297 "void functionDecl(\n" 6298 " int A, int B, int C);"), 6299 format(Input, Style)); 6300 } 6301 6302 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6303 FormatStyle Style = getLLVMStyle(); 6304 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6305 6306 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6307 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6308 getStyleWithColumns(Style, 45)); 6309 verifyFormat("Constructor() :\n" 6310 " Initializer(FitsOnTheLine) {}", 6311 getStyleWithColumns(Style, 44)); 6312 verifyFormat("Constructor() :\n" 6313 " Initializer(FitsOnTheLine) {}", 6314 getStyleWithColumns(Style, 43)); 6315 6316 verifyFormat("template <typename T>\n" 6317 "Constructor() : Initializer(FitsOnTheLine) {}", 6318 getStyleWithColumns(Style, 50)); 6319 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6320 verifyFormat( 6321 "SomeClass::Constructor() :\n" 6322 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6323 Style); 6324 6325 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 6326 verifyFormat( 6327 "SomeClass::Constructor() :\n" 6328 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6329 Style); 6330 6331 verifyFormat( 6332 "SomeClass::Constructor() :\n" 6333 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6334 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6335 Style); 6336 verifyFormat( 6337 "SomeClass::Constructor() :\n" 6338 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6339 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6340 Style); 6341 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6342 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6343 " aaaaaaaaaa(aaaaaa) {}", 6344 Style); 6345 6346 verifyFormat("Constructor() :\n" 6347 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6348 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6349 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6350 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6351 Style); 6352 6353 verifyFormat("Constructor() :\n" 6354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6356 Style); 6357 6358 verifyFormat("Constructor(int Parameter = 0) :\n" 6359 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6360 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6361 Style); 6362 verifyFormat("Constructor() :\n" 6363 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6364 "}", 6365 getStyleWithColumns(Style, 60)); 6366 verifyFormat("Constructor() :\n" 6367 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6368 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6369 Style); 6370 6371 // Here a line could be saved by splitting the second initializer onto two 6372 // lines, but that is not desirable. 6373 verifyFormat("Constructor() :\n" 6374 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6375 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6376 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6377 Style); 6378 6379 FormatStyle OnePerLine = Style; 6380 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6381 verifyFormat("SomeClass::Constructor() :\n" 6382 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6383 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6384 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6385 OnePerLine); 6386 verifyFormat("SomeClass::Constructor() :\n" 6387 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6388 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6389 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6390 OnePerLine); 6391 verifyFormat("MyClass::MyClass(int var) :\n" 6392 " some_var_(var), // 4 space indent\n" 6393 " some_other_var_(var + 1) { // lined up\n" 6394 "}", 6395 OnePerLine); 6396 verifyFormat("Constructor() :\n" 6397 " aaaaa(aaaaaa),\n" 6398 " aaaaa(aaaaaa),\n" 6399 " aaaaa(aaaaaa),\n" 6400 " aaaaa(aaaaaa),\n" 6401 " aaaaa(aaaaaa) {}", 6402 OnePerLine); 6403 verifyFormat("Constructor() :\n" 6404 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6405 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6406 OnePerLine); 6407 OnePerLine.BinPackParameters = false; 6408 verifyFormat("Constructor() :\n" 6409 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6410 " aaaaaaaaaaa().aaa(),\n" 6411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6412 OnePerLine); 6413 OnePerLine.ColumnLimit = 60; 6414 verifyFormat("Constructor() :\n" 6415 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6416 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6417 OnePerLine); 6418 6419 EXPECT_EQ("Constructor() :\n" 6420 " // Comment forcing unwanted break.\n" 6421 " aaaa(aaaa) {}", 6422 format("Constructor() :\n" 6423 " // Comment forcing unwanted break.\n" 6424 " aaaa(aaaa) {}", 6425 Style)); 6426 6427 Style.ColumnLimit = 0; 6428 verifyFormat("SomeClass::Constructor() :\n" 6429 " a(a) {}", 6430 Style); 6431 verifyFormat("SomeClass::Constructor() noexcept :\n" 6432 " a(a) {}", 6433 Style); 6434 verifyFormat("SomeClass::Constructor() :\n" 6435 " a(a), b(b), c(c) {}", 6436 Style); 6437 verifyFormat("SomeClass::Constructor() :\n" 6438 " a(a) {\n" 6439 " foo();\n" 6440 " bar();\n" 6441 "}", 6442 Style); 6443 6444 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6445 verifyFormat("SomeClass::Constructor() :\n" 6446 " a(a), b(b), c(c) {\n" 6447 "}", 6448 Style); 6449 verifyFormat("SomeClass::Constructor() :\n" 6450 " a(a) {\n" 6451 "}", 6452 Style); 6453 6454 Style.ColumnLimit = 80; 6455 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6456 Style.ConstructorInitializerIndentWidth = 2; 6457 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6458 verifyFormat("SomeClass::Constructor() :\n" 6459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6460 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6461 Style); 6462 6463 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6464 // well 6465 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6466 verifyFormat( 6467 "class SomeClass\n" 6468 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6469 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6470 Style); 6471 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6472 verifyFormat( 6473 "class SomeClass\n" 6474 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6475 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6476 Style); 6477 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6478 verifyFormat( 6479 "class SomeClass :\n" 6480 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6481 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6482 Style); 6483 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6484 verifyFormat( 6485 "class SomeClass\n" 6486 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6487 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6488 Style); 6489 } 6490 6491 #ifndef EXPENSIVE_CHECKS 6492 // Expensive checks enables libstdc++ checking which includes validating the 6493 // state of ranges used in std::priority_queue - this blows out the 6494 // runtime/scalability of the function and makes this test unacceptably slow. 6495 TEST_F(FormatTest, MemoizationTests) { 6496 // This breaks if the memoization lookup does not take \c Indent and 6497 // \c LastSpace into account. 6498 verifyFormat( 6499 "extern CFRunLoopTimerRef\n" 6500 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6501 " CFTimeInterval interval, CFOptionFlags flags,\n" 6502 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6503 " CFRunLoopTimerContext *context) {}"); 6504 6505 // Deep nesting somewhat works around our memoization. 6506 verifyFormat( 6507 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6508 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6509 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6510 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6511 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6512 getLLVMStyleWithColumns(65)); 6513 verifyFormat( 6514 "aaaaa(\n" 6515 " aaaaa,\n" 6516 " aaaaa(\n" 6517 " aaaaa,\n" 6518 " aaaaa(\n" 6519 " aaaaa,\n" 6520 " aaaaa(\n" 6521 " aaaaa,\n" 6522 " aaaaa(\n" 6523 " aaaaa,\n" 6524 " aaaaa(\n" 6525 " aaaaa,\n" 6526 " aaaaa(\n" 6527 " aaaaa,\n" 6528 " aaaaa(\n" 6529 " aaaaa,\n" 6530 " aaaaa(\n" 6531 " aaaaa,\n" 6532 " aaaaa(\n" 6533 " aaaaa,\n" 6534 " aaaaa(\n" 6535 " aaaaa,\n" 6536 " aaaaa(\n" 6537 " aaaaa,\n" 6538 " aaaaa))))))))))));", 6539 getLLVMStyleWithColumns(65)); 6540 verifyFormat( 6541 "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" 6542 " a),\n" 6543 " a),\n" 6544 " a),\n" 6545 " a),\n" 6546 " a),\n" 6547 " a),\n" 6548 " a),\n" 6549 " a),\n" 6550 " a),\n" 6551 " a),\n" 6552 " a),\n" 6553 " a),\n" 6554 " a),\n" 6555 " a),\n" 6556 " a),\n" 6557 " a),\n" 6558 " a)", 6559 getLLVMStyleWithColumns(65)); 6560 6561 // This test takes VERY long when memoization is broken. 6562 FormatStyle OnePerLine = getLLVMStyle(); 6563 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6564 OnePerLine.BinPackParameters = false; 6565 std::string input = "Constructor()\n" 6566 " : aaaa(a,\n"; 6567 for (unsigned i = 0, e = 80; i != e; ++i) { 6568 input += " a,\n"; 6569 } 6570 input += " a) {}"; 6571 verifyFormat(input, OnePerLine); 6572 } 6573 #endif 6574 6575 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6576 verifyFormat( 6577 "void f() {\n" 6578 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6579 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6580 " f();\n" 6581 "}"); 6582 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6583 " Intervals[i - 1].getRange().getLast()) {\n}"); 6584 } 6585 6586 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6587 // Principially, we break function declarations in a certain order: 6588 // 1) break amongst arguments. 6589 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6590 " Cccccccccccccc cccccccccccccc);"); 6591 verifyFormat("template <class TemplateIt>\n" 6592 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6593 " TemplateIt *stop) {}"); 6594 6595 // 2) break after return type. 6596 verifyFormat( 6597 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6598 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6599 getGoogleStyle()); 6600 6601 // 3) break after (. 6602 verifyFormat( 6603 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6604 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6605 getGoogleStyle()); 6606 6607 // 4) break before after nested name specifiers. 6608 verifyFormat( 6609 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6610 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6611 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6612 getGoogleStyle()); 6613 6614 // However, there are exceptions, if a sufficient amount of lines can be 6615 // saved. 6616 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6617 // more adjusting. 6618 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6619 " Cccccccccccccc cccccccccc,\n" 6620 " Cccccccccccccc cccccccccc,\n" 6621 " Cccccccccccccc cccccccccc,\n" 6622 " Cccccccccccccc cccccccccc);"); 6623 verifyFormat( 6624 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6625 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6626 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6627 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 6628 getGoogleStyle()); 6629 verifyFormat( 6630 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6631 " Cccccccccccccc cccccccccc,\n" 6632 " Cccccccccccccc cccccccccc,\n" 6633 " Cccccccccccccc cccccccccc,\n" 6634 " Cccccccccccccc cccccccccc,\n" 6635 " Cccccccccccccc cccccccccc,\n" 6636 " Cccccccccccccc cccccccccc);"); 6637 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6638 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6639 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6640 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6641 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 6642 6643 // Break after multi-line parameters. 6644 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6645 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6646 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6647 " bbbb bbbb);"); 6648 verifyFormat("void SomeLoooooooooooongFunction(\n" 6649 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6650 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6651 " int bbbbbbbbbbbbb);"); 6652 6653 // Treat overloaded operators like other functions. 6654 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6655 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 6656 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6657 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 6658 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6659 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 6660 verifyGoogleFormat( 6661 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 6662 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6663 verifyGoogleFormat( 6664 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 6665 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6666 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6667 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6668 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 6669 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6670 verifyGoogleFormat( 6671 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 6672 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6673 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 6674 verifyGoogleFormat("template <typename T>\n" 6675 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6676 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 6677 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 6678 6679 FormatStyle Style = getLLVMStyle(); 6680 Style.PointerAlignment = FormatStyle::PAS_Left; 6681 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6682 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 6683 Style); 6684 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 6685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6686 Style); 6687 } 6688 6689 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 6690 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 6691 // Prefer keeping `::` followed by `operator` together. 6692 EXPECT_EQ("const aaaa::bbbbbbb &\n" 6693 "ccccccccc::operator++() {\n" 6694 " stuff();\n" 6695 "}", 6696 format("const aaaa::bbbbbbb\n" 6697 "&ccccccccc::operator++() { stuff(); }", 6698 getLLVMStyleWithColumns(40))); 6699 } 6700 6701 TEST_F(FormatTest, TrailingReturnType) { 6702 verifyFormat("auto foo() -> int;\n"); 6703 // correct trailing return type spacing 6704 verifyFormat("auto operator->() -> int;\n"); 6705 verifyFormat("auto operator++(int) -> int;\n"); 6706 6707 verifyFormat("struct S {\n" 6708 " auto bar() const -> int;\n" 6709 "};"); 6710 verifyFormat("template <size_t Order, typename T>\n" 6711 "auto load_img(const std::string &filename)\n" 6712 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 6713 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 6714 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 6715 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 6716 verifyFormat("template <typename T>\n" 6717 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 6718 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 6719 6720 // Not trailing return types. 6721 verifyFormat("void f() { auto a = b->c(); }"); 6722 } 6723 6724 TEST_F(FormatTest, DeductionGuides) { 6725 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 6726 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 6727 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 6728 verifyFormat( 6729 "template <class... T>\n" 6730 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 6731 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 6732 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 6733 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 6734 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 6735 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 6736 verifyFormat("template <class T> x() -> x<1>;"); 6737 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 6738 6739 // Ensure not deduction guides. 6740 verifyFormat("c()->f<int>();"); 6741 verifyFormat("x()->foo<1>;"); 6742 verifyFormat("x = p->foo<3>();"); 6743 verifyFormat("x()->x<1>();"); 6744 verifyFormat("x()->x<1>;"); 6745 } 6746 6747 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 6748 // Avoid breaking before trailing 'const' or other trailing annotations, if 6749 // they are not function-like. 6750 FormatStyle Style = getGoogleStyle(); 6751 Style.ColumnLimit = 47; 6752 verifyFormat("void someLongFunction(\n" 6753 " int someLoooooooooooooongParameter) const {\n}", 6754 getLLVMStyleWithColumns(47)); 6755 verifyFormat("LoooooongReturnType\n" 6756 "someLoooooooongFunction() const {}", 6757 getLLVMStyleWithColumns(47)); 6758 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 6759 " const {}", 6760 Style); 6761 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6762 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 6763 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6764 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 6765 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6766 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 6767 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 6768 " aaaaaaaaaaa aaaaa) const override;"); 6769 verifyGoogleFormat( 6770 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6771 " const override;"); 6772 6773 // Even if the first parameter has to be wrapped. 6774 verifyFormat("void someLongFunction(\n" 6775 " int someLongParameter) const {}", 6776 getLLVMStyleWithColumns(46)); 6777 verifyFormat("void someLongFunction(\n" 6778 " int someLongParameter) const {}", 6779 Style); 6780 verifyFormat("void someLongFunction(\n" 6781 " int someLongParameter) override {}", 6782 Style); 6783 verifyFormat("void someLongFunction(\n" 6784 " int someLongParameter) OVERRIDE {}", 6785 Style); 6786 verifyFormat("void someLongFunction(\n" 6787 " int someLongParameter) final {}", 6788 Style); 6789 verifyFormat("void someLongFunction(\n" 6790 " int someLongParameter) FINAL {}", 6791 Style); 6792 verifyFormat("void someLongFunction(\n" 6793 " int parameter) const override {}", 6794 Style); 6795 6796 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 6797 verifyFormat("void someLongFunction(\n" 6798 " int someLongParameter) const\n" 6799 "{\n" 6800 "}", 6801 Style); 6802 6803 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 6804 verifyFormat("void someLongFunction(\n" 6805 " int someLongParameter) const\n" 6806 " {\n" 6807 " }", 6808 Style); 6809 6810 // Unless these are unknown annotations. 6811 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 6812 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6813 " LONG_AND_UGLY_ANNOTATION;"); 6814 6815 // Breaking before function-like trailing annotations is fine to keep them 6816 // close to their arguments. 6817 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6818 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6819 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6820 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6821 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6822 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 6823 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 6824 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 6825 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 6826 6827 verifyFormat( 6828 "void aaaaaaaaaaaaaaaaaa()\n" 6829 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 6830 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 6831 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6832 " __attribute__((unused));"); 6833 verifyGoogleFormat( 6834 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6835 " GUARDED_BY(aaaaaaaaaaaa);"); 6836 verifyGoogleFormat( 6837 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6838 " GUARDED_BY(aaaaaaaaaaaa);"); 6839 verifyGoogleFormat( 6840 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6841 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6842 verifyGoogleFormat( 6843 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6844 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6845 } 6846 6847 TEST_F(FormatTest, FunctionAnnotations) { 6848 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6849 "int OldFunction(const string ¶meter) {}"); 6850 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6851 "string OldFunction(const string ¶meter) {}"); 6852 verifyFormat("template <typename T>\n" 6853 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6854 "string OldFunction(const string ¶meter) {}"); 6855 6856 // Not function annotations. 6857 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6858 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 6859 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 6860 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 6861 verifyFormat("MACRO(abc).function() // wrap\n" 6862 " << abc;"); 6863 verifyFormat("MACRO(abc)->function() // wrap\n" 6864 " << abc;"); 6865 verifyFormat("MACRO(abc)::function() // wrap\n" 6866 " << abc;"); 6867 } 6868 6869 TEST_F(FormatTest, BreaksDesireably) { 6870 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6871 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6872 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 6873 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6874 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 6875 "}"); 6876 6877 verifyFormat( 6878 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6879 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6880 6881 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6882 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6884 6885 verifyFormat( 6886 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6887 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6889 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 6891 6892 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6893 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6894 6895 verifyFormat( 6896 "void f() {\n" 6897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6899 "}"); 6900 verifyFormat( 6901 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6902 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6903 verifyFormat( 6904 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6906 verifyFormat( 6907 "aaaaaa(aaa,\n" 6908 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6910 " aaaa);"); 6911 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6913 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6914 6915 // Indent consistently independent of call expression and unary operator. 6916 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6917 " dddddddddddddddddddddddddddddd));"); 6918 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6919 " dddddddddddddddddddddddddddddd));"); 6920 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 6921 " dddddddddddddddddddddddddddddd));"); 6922 6923 // This test case breaks on an incorrect memoization, i.e. an optimization not 6924 // taking into account the StopAt value. 6925 verifyFormat( 6926 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6927 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6928 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6929 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6930 6931 verifyFormat("{\n {\n {\n" 6932 " Annotation.SpaceRequiredBefore =\n" 6933 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 6934 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 6935 " }\n }\n}"); 6936 6937 // Break on an outer level if there was a break on an inner level. 6938 EXPECT_EQ("f(g(h(a, // comment\n" 6939 " b, c),\n" 6940 " d, e),\n" 6941 " x, y);", 6942 format("f(g(h(a, // comment\n" 6943 " b, c), d, e), x, y);")); 6944 6945 // Prefer breaking similar line breaks. 6946 verifyFormat( 6947 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 6948 " NSTrackingMouseEnteredAndExited |\n" 6949 " NSTrackingActiveAlways;"); 6950 } 6951 6952 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 6953 FormatStyle NoBinPacking = getGoogleStyle(); 6954 NoBinPacking.BinPackParameters = false; 6955 NoBinPacking.BinPackArguments = true; 6956 verifyFormat("void f() {\n" 6957 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 6958 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6959 "}", 6960 NoBinPacking); 6961 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 6962 " int aaaaaaaaaaaaaaaaaaaa,\n" 6963 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6964 NoBinPacking); 6965 6966 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6967 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6968 " vector<int> bbbbbbbbbbbbbbb);", 6969 NoBinPacking); 6970 // FIXME: This behavior difference is probably not wanted. However, currently 6971 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 6972 // template arguments from BreakBeforeParameter being set because of the 6973 // one-per-line formatting. 6974 verifyFormat( 6975 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6976 " aaaaaaaaaa> aaaaaaaaaa);", 6977 NoBinPacking); 6978 verifyFormat( 6979 "void fffffffffff(\n" 6980 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 6981 " aaaaaaaaaa);"); 6982 } 6983 6984 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 6985 FormatStyle NoBinPacking = getGoogleStyle(); 6986 NoBinPacking.BinPackParameters = false; 6987 NoBinPacking.BinPackArguments = false; 6988 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 6989 " aaaaaaaaaaaaaaaaaaaa,\n" 6990 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 6991 NoBinPacking); 6992 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 6993 " aaaaaaaaaaaaa,\n" 6994 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 6995 NoBinPacking); 6996 verifyFormat( 6997 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6998 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7000 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 7002 NoBinPacking); 7003 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7004 " .aaaaaaaaaaaaaaaaaa();", 7005 NoBinPacking); 7006 verifyFormat("void f() {\n" 7007 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7008 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 7009 "}", 7010 NoBinPacking); 7011 7012 verifyFormat( 7013 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7014 " aaaaaaaaaaaa,\n" 7015 " aaaaaaaaaaaa);", 7016 NoBinPacking); 7017 verifyFormat( 7018 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 7019 " ddddddddddddddddddddddddddddd),\n" 7020 " test);", 7021 NoBinPacking); 7022 7023 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7024 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 7025 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 7026 " aaaaaaaaaaaaaaaaaa;", 7027 NoBinPacking); 7028 verifyFormat("a(\"a\"\n" 7029 " \"a\",\n" 7030 " a);"); 7031 7032 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7033 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7034 " aaaaaaaaa,\n" 7035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7036 NoBinPacking); 7037 verifyFormat( 7038 "void f() {\n" 7039 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7040 " .aaaaaaa();\n" 7041 "}", 7042 NoBinPacking); 7043 verifyFormat( 7044 "template <class SomeType, class SomeOtherType>\n" 7045 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7046 NoBinPacking); 7047 } 7048 7049 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7050 FormatStyle Style = getLLVMStyleWithColumns(15); 7051 Style.ExperimentalAutoDetectBinPacking = true; 7052 EXPECT_EQ("aaa(aaaa,\n" 7053 " aaaa,\n" 7054 " aaaa);\n" 7055 "aaa(aaaa,\n" 7056 " aaaa,\n" 7057 " aaaa);", 7058 format("aaa(aaaa,\n" // one-per-line 7059 " aaaa,\n" 7060 " aaaa );\n" 7061 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7062 Style)); 7063 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7064 " aaaa);\n" 7065 "aaa(aaaa, aaaa,\n" 7066 " aaaa);", 7067 format("aaa(aaaa, aaaa,\n" // bin-packed 7068 " aaaa );\n" 7069 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7070 Style)); 7071 } 7072 7073 TEST_F(FormatTest, FormatsBuilderPattern) { 7074 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7075 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7076 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7077 " .StartsWith(\".init\", ORDER_INIT)\n" 7078 " .StartsWith(\".fini\", ORDER_FINI)\n" 7079 " .StartsWith(\".hash\", ORDER_HASH)\n" 7080 " .Default(ORDER_TEXT);\n"); 7081 7082 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7083 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7084 verifyFormat("aaaaaaa->aaaaaaa\n" 7085 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7087 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7088 verifyFormat( 7089 "aaaaaaa->aaaaaaa\n" 7090 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7091 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7092 verifyFormat( 7093 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7094 " aaaaaaaaaaaaaa);"); 7095 verifyFormat( 7096 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7097 " aaaaaa->aaaaaaaaaaaa()\n" 7098 " ->aaaaaaaaaaaaaaaa(\n" 7099 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7100 " ->aaaaaaaaaaaaaaaaa();"); 7101 verifyGoogleFormat( 7102 "void f() {\n" 7103 " someo->Add((new util::filetools::Handler(dir))\n" 7104 " ->OnEvent1(NewPermanentCallback(\n" 7105 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7106 " ->OnEvent2(NewPermanentCallback(\n" 7107 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7108 " ->OnEvent3(NewPermanentCallback(\n" 7109 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7110 " ->OnEvent5(NewPermanentCallback(\n" 7111 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7112 " ->OnEvent6(NewPermanentCallback(\n" 7113 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7114 "}"); 7115 7116 verifyFormat( 7117 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7118 verifyFormat("aaaaaaaaaaaaaaa()\n" 7119 " .aaaaaaaaaaaaaaa()\n" 7120 " .aaaaaaaaaaaaaaa()\n" 7121 " .aaaaaaaaaaaaaaa()\n" 7122 " .aaaaaaaaaaaaaaa();"); 7123 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7124 " .aaaaaaaaaaaaaaa()\n" 7125 " .aaaaaaaaaaaaaaa()\n" 7126 " .aaaaaaaaaaaaaaa();"); 7127 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7128 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7129 " .aaaaaaaaaaaaaaa();"); 7130 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7131 " ->aaaaaaaaaaaaaae(0)\n" 7132 " ->aaaaaaaaaaaaaaa();"); 7133 7134 // Don't linewrap after very short segments. 7135 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7136 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7137 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7138 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7139 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7140 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7141 verifyFormat("aaa()\n" 7142 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7143 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7144 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7145 7146 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7147 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7148 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7149 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7150 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7152 7153 // Prefer not to break after empty parentheses. 7154 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7155 " First->LastNewlineOffset);"); 7156 7157 // Prefer not to create "hanging" indents. 7158 verifyFormat( 7159 "return !soooooooooooooome_map\n" 7160 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7161 " .second;"); 7162 verifyFormat( 7163 "return aaaaaaaaaaaaaaaa\n" 7164 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7165 " .aaaa(aaaaaaaaaaaaaa);"); 7166 // No hanging indent here. 7167 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7169 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7171 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7172 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7173 getLLVMStyleWithColumns(60)); 7174 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7175 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7176 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7177 getLLVMStyleWithColumns(59)); 7178 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7179 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7180 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7181 7182 // Dont break if only closing statements before member call 7183 verifyFormat("test() {\n" 7184 " ([]() -> {\n" 7185 " int b = 32;\n" 7186 " return 3;\n" 7187 " }).foo();\n" 7188 "}"); 7189 verifyFormat("test() {\n" 7190 " (\n" 7191 " []() -> {\n" 7192 " int b = 32;\n" 7193 " return 3;\n" 7194 " },\n" 7195 " foo, bar)\n" 7196 " .foo();\n" 7197 "}"); 7198 verifyFormat("test() {\n" 7199 " ([]() -> {\n" 7200 " int b = 32;\n" 7201 " return 3;\n" 7202 " })\n" 7203 " .foo()\n" 7204 " .bar();\n" 7205 "}"); 7206 verifyFormat("test() {\n" 7207 " ([]() -> {\n" 7208 " int b = 32;\n" 7209 " return 3;\n" 7210 " })\n" 7211 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7212 " \"bbbb\");\n" 7213 "}", 7214 getLLVMStyleWithColumns(30)); 7215 } 7216 7217 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7218 verifyFormat( 7219 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7220 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7221 verifyFormat( 7222 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7223 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7224 7225 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7226 " ccccccccccccccccccccccccc) {\n}"); 7227 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7228 " ccccccccccccccccccccccccc) {\n}"); 7229 7230 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7231 " ccccccccccccccccccccccccc) {\n}"); 7232 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7233 " ccccccccccccccccccccccccc) {\n}"); 7234 7235 verifyFormat( 7236 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7237 " ccccccccccccccccccccccccc) {\n}"); 7238 verifyFormat( 7239 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7240 " ccccccccccccccccccccccccc) {\n}"); 7241 7242 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7243 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7244 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7245 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7246 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7247 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7248 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7249 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7250 7251 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7252 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7253 " aaaaaaaaaaaaaaa != aa) {\n}"); 7254 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7255 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7256 " aaaaaaaaaaaaaaa != aa) {\n}"); 7257 } 7258 7259 TEST_F(FormatTest, BreaksAfterAssignments) { 7260 verifyFormat( 7261 "unsigned Cost =\n" 7262 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7263 " SI->getPointerAddressSpaceee());\n"); 7264 verifyFormat( 7265 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7266 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7267 7268 verifyFormat( 7269 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7270 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7271 verifyFormat("unsigned OriginalStartColumn =\n" 7272 " SourceMgr.getSpellingColumnNumber(\n" 7273 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7274 " 1;"); 7275 } 7276 7277 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7278 FormatStyle Style = getLLVMStyle(); 7279 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7280 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7281 Style); 7282 7283 Style.PenaltyBreakAssignment = 20; 7284 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7285 " cccccccccccccccccccccccccc;", 7286 Style); 7287 } 7288 7289 TEST_F(FormatTest, AlignsAfterAssignments) { 7290 verifyFormat( 7291 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7292 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7293 verifyFormat( 7294 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7295 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7296 verifyFormat( 7297 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7298 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7299 verifyFormat( 7300 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7301 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7302 verifyFormat( 7303 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7304 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7305 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7306 } 7307 7308 TEST_F(FormatTest, AlignsAfterReturn) { 7309 verifyFormat( 7310 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7311 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7312 verifyFormat( 7313 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7314 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7315 verifyFormat( 7316 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7317 " aaaaaaaaaaaaaaaaaaaaaa();"); 7318 verifyFormat( 7319 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7320 " aaaaaaaaaaaaaaaaaaaaaa());"); 7321 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7323 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7324 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7326 verifyFormat("return\n" 7327 " // true if code is one of a or b.\n" 7328 " code == a || code == b;"); 7329 } 7330 7331 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7332 verifyFormat( 7333 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7334 " aaaaaaaaa aaaaaaa) {}"); 7335 verifyFormat( 7336 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7337 " aaaaaaaaaaa aaaaaaaaa);"); 7338 verifyFormat( 7339 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7340 " aaaaaaaaaaaaaaaaaaaaa));"); 7341 FormatStyle Style = getLLVMStyle(); 7342 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7343 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7344 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7345 Style); 7346 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7347 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7348 Style); 7349 verifyFormat("SomeLongVariableName->someFunction(\n" 7350 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7351 Style); 7352 verifyFormat( 7353 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7354 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7355 Style); 7356 verifyFormat( 7357 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7358 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7359 Style); 7360 verifyFormat( 7361 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7362 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7363 Style); 7364 7365 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7366 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7367 " b));", 7368 Style); 7369 7370 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7371 Style.BinPackArguments = false; 7372 Style.BinPackParameters = false; 7373 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7374 " aaaaaaaaaaa aaaaaaaa,\n" 7375 " aaaaaaaaa aaaaaaa,\n" 7376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7377 Style); 7378 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7379 " aaaaaaaaaaa aaaaaaaaa,\n" 7380 " aaaaaaaaaaa aaaaaaaaa,\n" 7381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7382 Style); 7383 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7384 " aaaaaaaaaaaaaaa,\n" 7385 " aaaaaaaaaaaaaaaaaaaaa,\n" 7386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7387 Style); 7388 verifyFormat( 7389 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7390 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7391 Style); 7392 verifyFormat( 7393 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7394 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7395 Style); 7396 verifyFormat( 7397 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7398 " aaaaaaaaaaaaaaaaaaaaa(\n" 7399 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7400 " aaaaaaaaaaaaaaaa);", 7401 Style); 7402 verifyFormat( 7403 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7404 " aaaaaaaaaaaaaaaaaaaaa(\n" 7405 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7406 " aaaaaaaaaaaaaaaa);", 7407 Style); 7408 } 7409 7410 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7411 FormatStyle Style = getLLVMStyleWithColumns(40); 7412 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7413 " bbbbbbbbbbbbbbbbbbbbbb);", 7414 Style); 7415 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7416 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7417 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7418 " bbbbbbbbbbbbbbbbbbbbbb);", 7419 Style); 7420 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7421 Style.AlignOperands = FormatStyle::OAS_Align; 7422 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7423 " bbbbbbbbbbbbbbbbbbbbbb);", 7424 Style); 7425 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7426 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7427 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7428 " bbbbbbbbbbbbbbbbbbbbbb);", 7429 Style); 7430 } 7431 7432 TEST_F(FormatTest, BreaksConditionalExpressions) { 7433 verifyFormat( 7434 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7435 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7436 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7437 verifyFormat( 7438 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7439 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7440 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7441 verifyFormat( 7442 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7443 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7444 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7445 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7446 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7447 verifyFormat( 7448 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7449 " : aaaaaaaaaaaaa);"); 7450 verifyFormat( 7451 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7452 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7453 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7454 " aaaaaaaaaaaaa);"); 7455 verifyFormat( 7456 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7457 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7458 " aaaaaaaaaaaaa);"); 7459 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7460 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7462 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7464 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7466 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7467 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7468 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7469 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7470 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7471 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7473 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7474 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7475 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7476 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7477 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7478 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7479 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7481 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7482 " : aaaaaaaaaaaaaaaa;"); 7483 verifyFormat( 7484 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7485 " ? aaaaaaaaaaaaaaa\n" 7486 " : aaaaaaaaaaaaaaa;"); 7487 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7488 " aaaaaaaaa\n" 7489 " ? b\n" 7490 " : c);"); 7491 verifyFormat("return aaaa == bbbb\n" 7492 " // comment\n" 7493 " ? aaaa\n" 7494 " : bbbb;"); 7495 verifyFormat("unsigned Indent =\n" 7496 " format(TheLine.First,\n" 7497 " IndentForLevel[TheLine.Level] >= 0\n" 7498 " ? IndentForLevel[TheLine.Level]\n" 7499 " : TheLine * 2,\n" 7500 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7501 getLLVMStyleWithColumns(60)); 7502 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7503 " ? aaaaaaaaaaaaaaa\n" 7504 " : bbbbbbbbbbbbbbb //\n" 7505 " ? ccccccccccccccc\n" 7506 " : ddddddddddddddd;"); 7507 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7508 " ? aaaaaaaaaaaaaaa\n" 7509 " : (bbbbbbbbbbbbbbb //\n" 7510 " ? ccccccccccccccc\n" 7511 " : ddddddddddddddd);"); 7512 verifyFormat( 7513 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7514 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7515 " aaaaaaaaaaaaaaaaaaaaa +\n" 7516 " aaaaaaaaaaaaaaaaaaaaa\n" 7517 " : aaaaaaaaaa;"); 7518 verifyFormat( 7519 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7520 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7521 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7522 7523 FormatStyle NoBinPacking = getLLVMStyle(); 7524 NoBinPacking.BinPackArguments = false; 7525 verifyFormat( 7526 "void f() {\n" 7527 " g(aaa,\n" 7528 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7529 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7530 " ? aaaaaaaaaaaaaaa\n" 7531 " : aaaaaaaaaaaaaaa);\n" 7532 "}", 7533 NoBinPacking); 7534 verifyFormat( 7535 "void f() {\n" 7536 " g(aaa,\n" 7537 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7539 " ?: aaaaaaaaaaaaaaa);\n" 7540 "}", 7541 NoBinPacking); 7542 7543 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7544 " // comment.\n" 7545 " ccccccccccccccccccccccccccccccccccccccc\n" 7546 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7547 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7548 7549 // Assignments in conditional expressions. Apparently not uncommon :-(. 7550 verifyFormat("return a != b\n" 7551 " // comment\n" 7552 " ? a = b\n" 7553 " : a = b;"); 7554 verifyFormat("return a != b\n" 7555 " // comment\n" 7556 " ? a = a != b\n" 7557 " // comment\n" 7558 " ? a = b\n" 7559 " : a\n" 7560 " : a;\n"); 7561 verifyFormat("return a != b\n" 7562 " // comment\n" 7563 " ? a\n" 7564 " : a = a != b\n" 7565 " // comment\n" 7566 " ? a = b\n" 7567 " : a;"); 7568 7569 // Chained conditionals 7570 FormatStyle Style = getLLVMStyle(); 7571 Style.ColumnLimit = 70; 7572 Style.AlignOperands = FormatStyle::OAS_Align; 7573 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7574 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7575 " : 3333333333333333;", 7576 Style); 7577 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7578 " : bbbbbbbbbb ? 2222222222222222\n" 7579 " : 3333333333333333;", 7580 Style); 7581 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7582 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7583 " : 3333333333333333;", 7584 Style); 7585 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7586 " : bbbbbbbbbbbbbb ? 222222\n" 7587 " : 333333;", 7588 Style); 7589 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7590 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7591 " : cccccccccccccc ? 3333333333333333\n" 7592 " : 4444444444444444;", 7593 Style); 7594 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7595 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7596 " : 3333333333333333;", 7597 Style); 7598 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7599 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7600 " : (aaa ? bbb : ccc);", 7601 Style); 7602 verifyFormat( 7603 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7604 " : cccccccccccccccccc)\n" 7605 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7606 " : 3333333333333333;", 7607 Style); 7608 verifyFormat( 7609 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7610 " : cccccccccccccccccc)\n" 7611 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7612 " : 3333333333333333;", 7613 Style); 7614 verifyFormat( 7615 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7616 " : dddddddddddddddddd)\n" 7617 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7618 " : 3333333333333333;", 7619 Style); 7620 verifyFormat( 7621 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7622 " : dddddddddddddddddd)\n" 7623 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7624 " : 3333333333333333;", 7625 Style); 7626 verifyFormat( 7627 "return aaaaaaaaa ? 1111111111111111\n" 7628 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7629 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7630 " : dddddddddddddddddd)\n", 7631 Style); 7632 verifyFormat( 7633 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7634 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7635 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7636 " : cccccccccccccccccc);", 7637 Style); 7638 verifyFormat( 7639 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7640 " : ccccccccccccccc ? dddddddddddddddddd\n" 7641 " : eeeeeeeeeeeeeeeeee)\n" 7642 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7643 " : 3333333333333333;", 7644 Style); 7645 verifyFormat( 7646 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7647 " : ccccccccccccccc ? dddddddddddddddddd\n" 7648 " : eeeeeeeeeeeeeeeeee)\n" 7649 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7650 " : 3333333333333333;", 7651 Style); 7652 verifyFormat( 7653 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7654 " : cccccccccccc ? dddddddddddddddddd\n" 7655 " : eeeeeeeeeeeeeeeeee)\n" 7656 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7657 " : 3333333333333333;", 7658 Style); 7659 verifyFormat( 7660 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7661 " : cccccccccccccccccc\n" 7662 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7663 " : 3333333333333333;", 7664 Style); 7665 verifyFormat( 7666 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7667 " : cccccccccccccccc ? dddddddddddddddddd\n" 7668 " : eeeeeeeeeeeeeeeeee\n" 7669 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7670 " : 3333333333333333;", 7671 Style); 7672 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 7673 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7674 " : cccccccccccccccccc ? dddddddddddddddddd\n" 7675 " : eeeeeeeeeeeeeeeeee)\n" 7676 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7677 " : 3333333333333333;", 7678 Style); 7679 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7680 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7681 " : cccccccccccccccc ? dddddddddddddddddd\n" 7682 " : eeeeeeeeeeeeeeeeee\n" 7683 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7684 " : 3333333333333333;", 7685 Style); 7686 7687 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7688 Style.BreakBeforeTernaryOperators = false; 7689 // FIXME: Aligning the question marks is weird given DontAlign. 7690 // Consider disabling this alignment in this case. Also check whether this 7691 // will render the adjustment from https://reviews.llvm.org/D82199 7692 // unnecessary. 7693 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 7694 " bbbb ? cccccccccccccccccc :\n" 7695 " ddddd;\n", 7696 Style); 7697 7698 EXPECT_EQ( 7699 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7700 " /*\n" 7701 " */\n" 7702 " function() {\n" 7703 " try {\n" 7704 " return JJJJJJJJJJJJJJ(\n" 7705 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7706 " }\n" 7707 " } :\n" 7708 " function() {};", 7709 format( 7710 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7711 " /*\n" 7712 " */\n" 7713 " function() {\n" 7714 " try {\n" 7715 " return JJJJJJJJJJJJJJ(\n" 7716 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7717 " }\n" 7718 " } :\n" 7719 " function() {};", 7720 getGoogleStyle(FormatStyle::LK_JavaScript))); 7721 } 7722 7723 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 7724 FormatStyle Style = getLLVMStyle(); 7725 Style.BreakBeforeTernaryOperators = false; 7726 Style.ColumnLimit = 70; 7727 verifyFormat( 7728 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7731 Style); 7732 verifyFormat( 7733 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7734 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7736 Style); 7737 verifyFormat( 7738 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7739 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7740 Style); 7741 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 7742 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7744 Style); 7745 verifyFormat( 7746 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 7747 " aaaaaaaaaaaaa);", 7748 Style); 7749 verifyFormat( 7750 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7751 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7753 " aaaaaaaaaaaaa);", 7754 Style); 7755 verifyFormat( 7756 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7757 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7758 " aaaaaaaaaaaaa);", 7759 Style); 7760 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7761 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7765 Style); 7766 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7768 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7769 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7770 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7771 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7772 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7773 Style); 7774 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 7776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7778 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7779 Style); 7780 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7781 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7782 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7783 Style); 7784 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7788 Style); 7789 verifyFormat( 7790 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7791 " aaaaaaaaaaaaaaa :\n" 7792 " aaaaaaaaaaaaaaa;", 7793 Style); 7794 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7795 " aaaaaaaaa ?\n" 7796 " b :\n" 7797 " c);", 7798 Style); 7799 verifyFormat("unsigned Indent =\n" 7800 " format(TheLine.First,\n" 7801 " IndentForLevel[TheLine.Level] >= 0 ?\n" 7802 " IndentForLevel[TheLine.Level] :\n" 7803 " TheLine * 2,\n" 7804 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7805 Style); 7806 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7807 " aaaaaaaaaaaaaaa :\n" 7808 " bbbbbbbbbbbbbbb ? //\n" 7809 " ccccccccccccccc :\n" 7810 " ddddddddddddddd;", 7811 Style); 7812 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7813 " aaaaaaaaaaaaaaa :\n" 7814 " (bbbbbbbbbbbbbbb ? //\n" 7815 " ccccccccccccccc :\n" 7816 " ddddddddddddddd);", 7817 Style); 7818 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7819 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 7820 " ccccccccccccccccccccccccccc;", 7821 Style); 7822 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7823 " aaaaa :\n" 7824 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 7825 Style); 7826 7827 // Chained conditionals 7828 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7829 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7830 " 3333333333333333;", 7831 Style); 7832 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7833 " bbbbbbbbbb ? 2222222222222222 :\n" 7834 " 3333333333333333;", 7835 Style); 7836 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 7837 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7838 " 3333333333333333;", 7839 Style); 7840 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7841 " bbbbbbbbbbbbbbbb ? 222222 :\n" 7842 " 333333;", 7843 Style); 7844 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7845 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7846 " cccccccccccccccc ? 3333333333333333 :\n" 7847 " 4444444444444444;", 7848 Style); 7849 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 7850 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7851 " 3333333333333333;", 7852 Style); 7853 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7854 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7855 " (aaa ? bbb : ccc);", 7856 Style); 7857 verifyFormat( 7858 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7859 " cccccccccccccccccc) :\n" 7860 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7861 " 3333333333333333;", 7862 Style); 7863 verifyFormat( 7864 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7865 " cccccccccccccccccc) :\n" 7866 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7867 " 3333333333333333;", 7868 Style); 7869 verifyFormat( 7870 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7871 " dddddddddddddddddd) :\n" 7872 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7873 " 3333333333333333;", 7874 Style); 7875 verifyFormat( 7876 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7877 " dddddddddddddddddd) :\n" 7878 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7879 " 3333333333333333;", 7880 Style); 7881 verifyFormat( 7882 "return aaaaaaaaa ? 1111111111111111 :\n" 7883 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7884 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7885 " dddddddddddddddddd)\n", 7886 Style); 7887 verifyFormat( 7888 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7889 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7890 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7891 " cccccccccccccccccc);", 7892 Style); 7893 verifyFormat( 7894 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7895 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7896 " eeeeeeeeeeeeeeeeee) :\n" 7897 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7898 " 3333333333333333;", 7899 Style); 7900 verifyFormat( 7901 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7902 " ccccccccccccc ? dddddddddddddddddd :\n" 7903 " eeeeeeeeeeeeeeeeee) :\n" 7904 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7905 " 3333333333333333;", 7906 Style); 7907 verifyFormat( 7908 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7909 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7910 " eeeeeeeeeeeeeeeeee) :\n" 7911 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7912 " 3333333333333333;", 7913 Style); 7914 verifyFormat( 7915 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7916 " cccccccccccccccccc :\n" 7917 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7918 " 3333333333333333;", 7919 Style); 7920 verifyFormat( 7921 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7922 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7923 " eeeeeeeeeeeeeeeeee :\n" 7924 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7925 " 3333333333333333;", 7926 Style); 7927 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7928 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7929 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7930 " eeeeeeeeeeeeeeeeee) :\n" 7931 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7932 " 3333333333333333;", 7933 Style); 7934 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7935 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7936 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 7937 " eeeeeeeeeeeeeeeeee :\n" 7938 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7939 " 3333333333333333;", 7940 Style); 7941 } 7942 7943 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 7944 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 7945 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 7946 verifyFormat("bool a = true, b = false;"); 7947 7948 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7949 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 7950 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 7951 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 7952 verifyFormat( 7953 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 7954 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 7955 " d = e && f;"); 7956 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 7957 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 7958 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7959 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 7960 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 7961 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 7962 7963 FormatStyle Style = getGoogleStyle(); 7964 Style.PointerAlignment = FormatStyle::PAS_Left; 7965 Style.DerivePointerAlignment = false; 7966 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7967 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 7968 " *b = bbbbbbbbbbbbbbbbbbb;", 7969 Style); 7970 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7971 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 7972 Style); 7973 verifyFormat("vector<int*> a, b;", Style); 7974 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 7975 } 7976 7977 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 7978 verifyFormat("arr[foo ? bar : baz];"); 7979 verifyFormat("f()[foo ? bar : baz];"); 7980 verifyFormat("(a + b)[foo ? bar : baz];"); 7981 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 7982 } 7983 7984 TEST_F(FormatTest, AlignsStringLiterals) { 7985 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 7986 " \"short literal\");"); 7987 verifyFormat( 7988 "looooooooooooooooooooooooongFunction(\n" 7989 " \"short literal\"\n" 7990 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 7991 verifyFormat("someFunction(\"Always break between multi-line\"\n" 7992 " \" string literals\",\n" 7993 " and, other, parameters);"); 7994 EXPECT_EQ("fun + \"1243\" /* comment */\n" 7995 " \"5678\";", 7996 format("fun + \"1243\" /* comment */\n" 7997 " \"5678\";", 7998 getLLVMStyleWithColumns(28))); 7999 EXPECT_EQ( 8000 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8001 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 8002 " \"aaaaaaaaaaaaaaaa\";", 8003 format("aaaaaa =" 8004 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8005 "aaaaaaaaaaaaaaaaaaaaa\" " 8006 "\"aaaaaaaaaaaaaaaa\";")); 8007 verifyFormat("a = a + \"a\"\n" 8008 " \"a\"\n" 8009 " \"a\";"); 8010 verifyFormat("f(\"a\", \"b\"\n" 8011 " \"c\");"); 8012 8013 verifyFormat( 8014 "#define LL_FORMAT \"ll\"\n" 8015 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 8016 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 8017 8018 verifyFormat("#define A(X) \\\n" 8019 " \"aaaaa\" #X \"bbbbbb\" \\\n" 8020 " \"ccccc\"", 8021 getLLVMStyleWithColumns(23)); 8022 verifyFormat("#define A \"def\"\n" 8023 "f(\"abc\" A \"ghi\"\n" 8024 " \"jkl\");"); 8025 8026 verifyFormat("f(L\"a\"\n" 8027 " L\"b\");"); 8028 verifyFormat("#define A(X) \\\n" 8029 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 8030 " L\"ccccc\"", 8031 getLLVMStyleWithColumns(25)); 8032 8033 verifyFormat("f(@\"a\"\n" 8034 " @\"b\");"); 8035 verifyFormat("NSString s = @\"a\"\n" 8036 " @\"b\"\n" 8037 " @\"c\";"); 8038 verifyFormat("NSString s = @\"a\"\n" 8039 " \"b\"\n" 8040 " \"c\";"); 8041 } 8042 8043 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8044 FormatStyle Style = getLLVMStyle(); 8045 // No declarations or definitions should be moved to own line. 8046 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8047 verifyFormat("class A {\n" 8048 " int f() { return 1; }\n" 8049 " int g();\n" 8050 "};\n" 8051 "int f() { return 1; }\n" 8052 "int g();\n", 8053 Style); 8054 8055 // All declarations and definitions should have the return type moved to its 8056 // own line. 8057 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8058 Style.TypenameMacros = {"LIST"}; 8059 verifyFormat("SomeType\n" 8060 "funcdecl(LIST(uint64_t));", 8061 Style); 8062 verifyFormat("class E {\n" 8063 " int\n" 8064 " f() {\n" 8065 " return 1;\n" 8066 " }\n" 8067 " int\n" 8068 " g();\n" 8069 "};\n" 8070 "int\n" 8071 "f() {\n" 8072 " return 1;\n" 8073 "}\n" 8074 "int\n" 8075 "g();\n", 8076 Style); 8077 8078 // Top-level definitions, and no kinds of declarations should have the 8079 // return type moved to its own line. 8080 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8081 verifyFormat("class B {\n" 8082 " int f() { return 1; }\n" 8083 " int g();\n" 8084 "};\n" 8085 "int\n" 8086 "f() {\n" 8087 " return 1;\n" 8088 "}\n" 8089 "int g();\n", 8090 Style); 8091 8092 // Top-level definitions and declarations should have the return type moved 8093 // to its own line. 8094 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8095 verifyFormat("class C {\n" 8096 " int f() { return 1; }\n" 8097 " int g();\n" 8098 "};\n" 8099 "int\n" 8100 "f() {\n" 8101 " return 1;\n" 8102 "}\n" 8103 "int\n" 8104 "g();\n", 8105 Style); 8106 8107 // All definitions should have the return type moved to its own line, but no 8108 // kinds of declarations. 8109 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8110 verifyFormat("class D {\n" 8111 " int\n" 8112 " f() {\n" 8113 " return 1;\n" 8114 " }\n" 8115 " int g();\n" 8116 "};\n" 8117 "int\n" 8118 "f() {\n" 8119 " return 1;\n" 8120 "}\n" 8121 "int g();\n", 8122 Style); 8123 verifyFormat("const char *\n" 8124 "f(void) {\n" // Break here. 8125 " return \"\";\n" 8126 "}\n" 8127 "const char *bar(void);\n", // No break here. 8128 Style); 8129 verifyFormat("template <class T>\n" 8130 "T *\n" 8131 "f(T &c) {\n" // Break here. 8132 " return NULL;\n" 8133 "}\n" 8134 "template <class T> T *f(T &c);\n", // No break here. 8135 Style); 8136 verifyFormat("class C {\n" 8137 " int\n" 8138 " operator+() {\n" 8139 " return 1;\n" 8140 " }\n" 8141 " int\n" 8142 " operator()() {\n" 8143 " return 1;\n" 8144 " }\n" 8145 "};\n", 8146 Style); 8147 verifyFormat("void\n" 8148 "A::operator()() {}\n" 8149 "void\n" 8150 "A::operator>>() {}\n" 8151 "void\n" 8152 "A::operator+() {}\n" 8153 "void\n" 8154 "A::operator*() {}\n" 8155 "void\n" 8156 "A::operator->() {}\n" 8157 "void\n" 8158 "A::operator void *() {}\n" 8159 "void\n" 8160 "A::operator void &() {}\n" 8161 "void\n" 8162 "A::operator void &&() {}\n" 8163 "void\n" 8164 "A::operator char *() {}\n" 8165 "void\n" 8166 "A::operator[]() {}\n" 8167 "void\n" 8168 "A::operator!() {}\n" 8169 "void\n" 8170 "A::operator**() {}\n" 8171 "void\n" 8172 "A::operator<Foo> *() {}\n" 8173 "void\n" 8174 "A::operator<Foo> **() {}\n" 8175 "void\n" 8176 "A::operator<Foo> &() {}\n" 8177 "void\n" 8178 "A::operator void **() {}\n", 8179 Style); 8180 verifyFormat("constexpr auto\n" 8181 "operator()() const -> reference {}\n" 8182 "constexpr auto\n" 8183 "operator>>() const -> reference {}\n" 8184 "constexpr auto\n" 8185 "operator+() const -> reference {}\n" 8186 "constexpr auto\n" 8187 "operator*() const -> reference {}\n" 8188 "constexpr auto\n" 8189 "operator->() const -> reference {}\n" 8190 "constexpr auto\n" 8191 "operator++() const -> reference {}\n" 8192 "constexpr auto\n" 8193 "operator void *() const -> reference {}\n" 8194 "constexpr auto\n" 8195 "operator void **() const -> reference {}\n" 8196 "constexpr auto\n" 8197 "operator void *() const -> reference {}\n" 8198 "constexpr auto\n" 8199 "operator void &() const -> reference {}\n" 8200 "constexpr auto\n" 8201 "operator void &&() const -> reference {}\n" 8202 "constexpr auto\n" 8203 "operator char *() const -> reference {}\n" 8204 "constexpr auto\n" 8205 "operator!() const -> reference {}\n" 8206 "constexpr auto\n" 8207 "operator[]() const -> reference {}\n", 8208 Style); 8209 verifyFormat("void *operator new(std::size_t s);", // No break here. 8210 Style); 8211 verifyFormat("void *\n" 8212 "operator new(std::size_t s) {}", 8213 Style); 8214 verifyFormat("void *\n" 8215 "operator delete[](void *ptr) {}", 8216 Style); 8217 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8218 verifyFormat("const char *\n" 8219 "f(void)\n" // Break here. 8220 "{\n" 8221 " return \"\";\n" 8222 "}\n" 8223 "const char *bar(void);\n", // No break here. 8224 Style); 8225 verifyFormat("template <class T>\n" 8226 "T *\n" // Problem here: no line break 8227 "f(T &c)\n" // Break here. 8228 "{\n" 8229 " return NULL;\n" 8230 "}\n" 8231 "template <class T> T *f(T &c);\n", // No break here. 8232 Style); 8233 verifyFormat("int\n" 8234 "foo(A<bool> a)\n" 8235 "{\n" 8236 " return a;\n" 8237 "}\n", 8238 Style); 8239 verifyFormat("int\n" 8240 "foo(A<8> a)\n" 8241 "{\n" 8242 " return a;\n" 8243 "}\n", 8244 Style); 8245 verifyFormat("int\n" 8246 "foo(A<B<bool>, 8> a)\n" 8247 "{\n" 8248 " return a;\n" 8249 "}\n", 8250 Style); 8251 verifyFormat("int\n" 8252 "foo(A<B<8>, bool> a)\n" 8253 "{\n" 8254 " return a;\n" 8255 "}\n", 8256 Style); 8257 verifyFormat("int\n" 8258 "foo(A<B<bool>, bool> a)\n" 8259 "{\n" 8260 " return a;\n" 8261 "}\n", 8262 Style); 8263 verifyFormat("int\n" 8264 "foo(A<B<8>, 8> a)\n" 8265 "{\n" 8266 " return a;\n" 8267 "}\n", 8268 Style); 8269 8270 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8271 Style.BraceWrapping.AfterFunction = true; 8272 verifyFormat("int f(i);\n" // No break here. 8273 "int\n" // Break here. 8274 "f(i)\n" 8275 "{\n" 8276 " return i + 1;\n" 8277 "}\n" 8278 "int\n" // Break here. 8279 "f(i)\n" 8280 "{\n" 8281 " return i + 1;\n" 8282 "};", 8283 Style); 8284 verifyFormat("int f(a, b, c);\n" // No break here. 8285 "int\n" // Break here. 8286 "f(a, b, c)\n" // Break here. 8287 "short a, b;\n" 8288 "float c;\n" 8289 "{\n" 8290 " return a + b < c;\n" 8291 "}\n" 8292 "int\n" // Break here. 8293 "f(a, b, c)\n" // Break here. 8294 "short a, b;\n" 8295 "float c;\n" 8296 "{\n" 8297 " return a + b < c;\n" 8298 "};", 8299 Style); 8300 verifyFormat("byte *\n" // Break here. 8301 "f(a)\n" // Break here. 8302 "byte a[];\n" 8303 "{\n" 8304 " return a;\n" 8305 "}", 8306 Style); 8307 verifyFormat("bool f(int a, int) override;\n" 8308 "Bar g(int a, Bar) final;\n" 8309 "Bar h(a, Bar) final;", 8310 Style); 8311 verifyFormat("int\n" 8312 "f(a)", 8313 Style); 8314 verifyFormat("bool\n" 8315 "f(size_t = 0, bool b = false)\n" 8316 "{\n" 8317 " return !b;\n" 8318 "}", 8319 Style); 8320 8321 // The return breaking style doesn't affect: 8322 // * function and object definitions with attribute-like macros 8323 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8324 " ABSL_GUARDED_BY(mutex) = {};", 8325 getGoogleStyleWithColumns(40)); 8326 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8327 " ABSL_GUARDED_BY(mutex); // comment", 8328 getGoogleStyleWithColumns(40)); 8329 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8330 " ABSL_GUARDED_BY(mutex1)\n" 8331 " ABSL_GUARDED_BY(mutex2);", 8332 getGoogleStyleWithColumns(40)); 8333 verifyFormat("Tttttt f(int a, int b)\n" 8334 " ABSL_GUARDED_BY(mutex1)\n" 8335 " ABSL_GUARDED_BY(mutex2);", 8336 getGoogleStyleWithColumns(40)); 8337 // * typedefs 8338 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8339 8340 Style = getGNUStyle(); 8341 8342 // Test for comments at the end of function declarations. 8343 verifyFormat("void\n" 8344 "foo (int a, /*abc*/ int b) // def\n" 8345 "{\n" 8346 "}\n", 8347 Style); 8348 8349 verifyFormat("void\n" 8350 "foo (int a, /* abc */ int b) /* def */\n" 8351 "{\n" 8352 "}\n", 8353 Style); 8354 8355 // Definitions that should not break after return type 8356 verifyFormat("void foo (int a, int b); // def\n", Style); 8357 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8358 verifyFormat("void foo (int a, int b);\n", Style); 8359 } 8360 8361 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8362 FormatStyle NoBreak = getLLVMStyle(); 8363 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8364 FormatStyle Break = getLLVMStyle(); 8365 Break.AlwaysBreakBeforeMultilineStrings = true; 8366 verifyFormat("aaaa = \"bbbb\"\n" 8367 " \"cccc\";", 8368 NoBreak); 8369 verifyFormat("aaaa =\n" 8370 " \"bbbb\"\n" 8371 " \"cccc\";", 8372 Break); 8373 verifyFormat("aaaa(\"bbbb\"\n" 8374 " \"cccc\");", 8375 NoBreak); 8376 verifyFormat("aaaa(\n" 8377 " \"bbbb\"\n" 8378 " \"cccc\");", 8379 Break); 8380 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8381 " \"cccc\");", 8382 NoBreak); 8383 verifyFormat("aaaa(qqq,\n" 8384 " \"bbbb\"\n" 8385 " \"cccc\");", 8386 Break); 8387 verifyFormat("aaaa(qqq,\n" 8388 " L\"bbbb\"\n" 8389 " L\"cccc\");", 8390 Break); 8391 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8392 " \"bbbb\"));", 8393 Break); 8394 verifyFormat("string s = someFunction(\n" 8395 " \"abc\"\n" 8396 " \"abc\");", 8397 Break); 8398 8399 // As we break before unary operators, breaking right after them is bad. 8400 verifyFormat("string foo = abc ? \"x\"\n" 8401 " \"blah blah blah blah blah blah\"\n" 8402 " : \"y\";", 8403 Break); 8404 8405 // Don't break if there is no column gain. 8406 verifyFormat("f(\"aaaa\"\n" 8407 " \"bbbb\");", 8408 Break); 8409 8410 // Treat literals with escaped newlines like multi-line string literals. 8411 EXPECT_EQ("x = \"a\\\n" 8412 "b\\\n" 8413 "c\";", 8414 format("x = \"a\\\n" 8415 "b\\\n" 8416 "c\";", 8417 NoBreak)); 8418 EXPECT_EQ("xxxx =\n" 8419 " \"a\\\n" 8420 "b\\\n" 8421 "c\";", 8422 format("xxxx = \"a\\\n" 8423 "b\\\n" 8424 "c\";", 8425 Break)); 8426 8427 EXPECT_EQ("NSString *const kString =\n" 8428 " @\"aaaa\"\n" 8429 " @\"bbbb\";", 8430 format("NSString *const kString = @\"aaaa\"\n" 8431 "@\"bbbb\";", 8432 Break)); 8433 8434 Break.ColumnLimit = 0; 8435 verifyFormat("const char *hello = \"hello llvm\";", Break); 8436 } 8437 8438 TEST_F(FormatTest, AlignsPipes) { 8439 verifyFormat( 8440 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8441 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8442 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8443 verifyFormat( 8444 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8445 " << aaaaaaaaaaaaaaaaaaaa;"); 8446 verifyFormat( 8447 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8448 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8449 verifyFormat( 8450 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8451 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8452 verifyFormat( 8453 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8454 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8455 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8456 verifyFormat( 8457 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8458 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8459 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8460 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8463 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8464 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8465 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8466 verifyFormat( 8467 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8468 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8469 verifyFormat( 8470 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8471 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8472 8473 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8474 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8475 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8476 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8477 " aaaaaaaaaaaaaaaaaaaaa)\n" 8478 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8479 verifyFormat("LOG_IF(aaa == //\n" 8480 " bbb)\n" 8481 " << a << b;"); 8482 8483 // But sometimes, breaking before the first "<<" is desirable. 8484 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8485 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8486 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8487 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8488 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8489 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8490 " << BEF << IsTemplate << Description << E->getType();"); 8491 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8492 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8493 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8494 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8495 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8496 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8497 " << aaa;"); 8498 8499 verifyFormat( 8500 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8501 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8502 8503 // Incomplete string literal. 8504 EXPECT_EQ("llvm::errs() << \"\n" 8505 " << a;", 8506 format("llvm::errs() << \"\n<<a;")); 8507 8508 verifyFormat("void f() {\n" 8509 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8510 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8511 "}"); 8512 8513 // Handle 'endl'. 8514 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8515 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8516 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8517 8518 // Handle '\n'. 8519 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8520 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8521 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8522 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8523 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8524 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8525 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8526 } 8527 8528 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8529 verifyFormat("return out << \"somepacket = {\\n\"\n" 8530 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8531 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8532 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8533 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8534 " << \"}\";"); 8535 8536 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8537 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8538 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8539 verifyFormat( 8540 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8541 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8542 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8543 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8544 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8545 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8546 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8547 verifyFormat( 8548 "void f() {\n" 8549 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8550 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8551 "}"); 8552 8553 // Breaking before the first "<<" is generally not desirable. 8554 verifyFormat( 8555 "llvm::errs()\n" 8556 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8557 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8558 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8559 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8560 getLLVMStyleWithColumns(70)); 8561 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8562 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8563 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8564 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8565 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8566 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8567 getLLVMStyleWithColumns(70)); 8568 8569 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8570 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8571 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8572 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8573 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8574 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8575 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8576 " (aaaa + aaaa);", 8577 getLLVMStyleWithColumns(40)); 8578 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8579 " (aaaaaaa + aaaaa));", 8580 getLLVMStyleWithColumns(40)); 8581 verifyFormat( 8582 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8583 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8584 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8585 } 8586 8587 TEST_F(FormatTest, UnderstandsEquals) { 8588 verifyFormat( 8589 "aaaaaaaaaaaaaaaaa =\n" 8590 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8591 verifyFormat( 8592 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8594 verifyFormat( 8595 "if (a) {\n" 8596 " f();\n" 8597 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8598 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8599 "}"); 8600 8601 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8602 " 100000000 + 10000000) {\n}"); 8603 } 8604 8605 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8606 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8607 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8608 8609 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8610 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8611 8612 verifyFormat( 8613 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8614 " Parameter2);"); 8615 8616 verifyFormat( 8617 "ShortObject->shortFunction(\n" 8618 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8619 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8620 8621 verifyFormat("loooooooooooooongFunction(\n" 8622 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8623 8624 verifyFormat( 8625 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8626 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8627 8628 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8629 " .WillRepeatedly(Return(SomeValue));"); 8630 verifyFormat("void f() {\n" 8631 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8632 " .Times(2)\n" 8633 " .WillRepeatedly(Return(SomeValue));\n" 8634 "}"); 8635 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8636 " ccccccccccccccccccccccc);"); 8637 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8639 " .aaaaa(aaaaa),\n" 8640 " aaaaaaaaaaaaaaaaaaaaa);"); 8641 verifyFormat("void f() {\n" 8642 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8643 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8644 "}"); 8645 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8646 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8647 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8648 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8649 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8650 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8651 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8652 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8653 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8654 "}"); 8655 8656 // Here, it is not necessary to wrap at "." or "->". 8657 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8658 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8659 verifyFormat( 8660 "aaaaaaaaaaa->aaaaaaaaa(\n" 8661 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8662 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8663 8664 verifyFormat( 8665 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8666 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8667 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8668 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8669 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8670 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8671 8672 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8673 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8674 " .a();"); 8675 8676 FormatStyle NoBinPacking = getLLVMStyle(); 8677 NoBinPacking.BinPackParameters = false; 8678 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8679 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8680 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8681 " aaaaaaaaaaaaaaaaaaa,\n" 8682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8683 NoBinPacking); 8684 8685 // If there is a subsequent call, change to hanging indentation. 8686 verifyFormat( 8687 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8688 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8689 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8690 verifyFormat( 8691 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8692 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8693 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8694 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8695 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8696 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8697 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8698 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8699 } 8700 8701 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8702 verifyFormat("template <typename T>\n" 8703 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8704 verifyFormat("template <typename T>\n" 8705 "// T should be one of {A, B}.\n" 8706 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8707 verifyFormat( 8708 "template <typename T>\n" 8709 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8710 verifyFormat("template <typename T>\n" 8711 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8712 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8713 verifyFormat( 8714 "template <typename T>\n" 8715 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8716 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8717 verifyFormat( 8718 "template <typename T>\n" 8719 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8720 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8722 verifyFormat("template <typename T>\n" 8723 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8724 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8725 verifyFormat( 8726 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8727 " typename T4 = char>\n" 8728 "void f();"); 8729 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8730 " template <typename> class cccccccccccccccccccccc,\n" 8731 " typename ddddddddddddd>\n" 8732 "class C {};"); 8733 verifyFormat( 8734 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8736 8737 verifyFormat("void f() {\n" 8738 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8739 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8740 "}"); 8741 8742 verifyFormat("template <typename T> class C {};"); 8743 verifyFormat("template <typename T> void f();"); 8744 verifyFormat("template <typename T> void f() {}"); 8745 verifyFormat( 8746 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8747 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8748 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8749 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8752 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8753 getLLVMStyleWithColumns(72)); 8754 EXPECT_EQ("static_cast<A< //\n" 8755 " B> *>(\n" 8756 "\n" 8757 ");", 8758 format("static_cast<A<//\n" 8759 " B>*>(\n" 8760 "\n" 8761 " );")); 8762 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8763 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8764 8765 FormatStyle AlwaysBreak = getLLVMStyle(); 8766 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8767 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8768 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8769 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8770 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8771 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8772 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8773 verifyFormat("template <template <typename> class Fooooooo,\n" 8774 " template <typename> class Baaaaaaar>\n" 8775 "struct C {};", 8776 AlwaysBreak); 8777 verifyFormat("template <typename T> // T can be A, B or C.\n" 8778 "struct C {};", 8779 AlwaysBreak); 8780 verifyFormat("template <enum E> class A {\n" 8781 "public:\n" 8782 " E *f();\n" 8783 "};"); 8784 8785 FormatStyle NeverBreak = getLLVMStyle(); 8786 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8787 verifyFormat("template <typename T> class C {};", NeverBreak); 8788 verifyFormat("template <typename T> void f();", NeverBreak); 8789 verifyFormat("template <typename T> void f() {}", NeverBreak); 8790 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8791 "bbbbbbbbbbbbbbbbbbbb) {}", 8792 NeverBreak); 8793 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8794 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8795 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8796 NeverBreak); 8797 verifyFormat("template <template <typename> class Fooooooo,\n" 8798 " template <typename> class Baaaaaaar>\n" 8799 "struct C {};", 8800 NeverBreak); 8801 verifyFormat("template <typename T> // T can be A, B or C.\n" 8802 "struct C {};", 8803 NeverBreak); 8804 verifyFormat("template <enum E> class A {\n" 8805 "public:\n" 8806 " E *f();\n" 8807 "};", 8808 NeverBreak); 8809 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8810 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8811 "bbbbbbbbbbbbbbbbbbbb) {}", 8812 NeverBreak); 8813 } 8814 8815 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8816 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8817 Style.ColumnLimit = 60; 8818 EXPECT_EQ("// Baseline - no comments.\n" 8819 "template <\n" 8820 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8821 "void f() {}", 8822 format("// Baseline - no comments.\n" 8823 "template <\n" 8824 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8825 "void f() {}", 8826 Style)); 8827 8828 EXPECT_EQ("template <\n" 8829 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8830 "void f() {}", 8831 format("template <\n" 8832 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8833 "void f() {}", 8834 Style)); 8835 8836 EXPECT_EQ( 8837 "template <\n" 8838 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8839 "void f() {}", 8840 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8841 "void f() {}", 8842 Style)); 8843 8844 EXPECT_EQ( 8845 "template <\n" 8846 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8847 " // multiline\n" 8848 "void f() {}", 8849 format("template <\n" 8850 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8851 " // multiline\n" 8852 "void f() {}", 8853 Style)); 8854 8855 EXPECT_EQ( 8856 "template <typename aaaaaaaaaa<\n" 8857 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8858 "void f() {}", 8859 format( 8860 "template <\n" 8861 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8862 "void f() {}", 8863 Style)); 8864 } 8865 8866 TEST_F(FormatTest, WrapsTemplateParameters) { 8867 FormatStyle Style = getLLVMStyle(); 8868 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8869 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8870 verifyFormat( 8871 "template <typename... a> struct q {};\n" 8872 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8873 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8874 " y;", 8875 Style); 8876 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8877 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8878 verifyFormat( 8879 "template <typename... a> struct r {};\n" 8880 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8881 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8882 " y;", 8883 Style); 8884 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8885 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8886 verifyFormat("template <typename... a> struct s {};\n" 8887 "extern s<\n" 8888 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8889 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8890 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8891 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8892 " y;", 8893 Style); 8894 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8895 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8896 verifyFormat("template <typename... a> struct t {};\n" 8897 "extern t<\n" 8898 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8899 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8900 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8901 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8902 " y;", 8903 Style); 8904 } 8905 8906 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8907 verifyFormat( 8908 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8910 verifyFormat( 8911 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8913 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8914 8915 // FIXME: Should we have the extra indent after the second break? 8916 verifyFormat( 8917 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8919 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8920 8921 verifyFormat( 8922 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8923 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8924 8925 // Breaking at nested name specifiers is generally not desirable. 8926 verifyFormat( 8927 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8928 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8929 8930 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8931 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8933 " aaaaaaaaaaaaaaaaaaaaa);", 8934 getLLVMStyleWithColumns(74)); 8935 8936 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8938 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8939 } 8940 8941 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8942 verifyFormat("A<int> a;"); 8943 verifyFormat("A<A<A<int>>> a;"); 8944 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8945 verifyFormat("bool x = a < 1 || 2 > a;"); 8946 verifyFormat("bool x = 5 < f<int>();"); 8947 verifyFormat("bool x = f<int>() > 5;"); 8948 verifyFormat("bool x = 5 < a<int>::x;"); 8949 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8950 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8951 8952 verifyGoogleFormat("A<A<int>> a;"); 8953 verifyGoogleFormat("A<A<A<int>>> a;"); 8954 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8955 verifyGoogleFormat("A<A<int> > a;"); 8956 verifyGoogleFormat("A<A<A<int> > > a;"); 8957 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8958 verifyGoogleFormat("A<::A<int>> a;"); 8959 verifyGoogleFormat("A<::A> a;"); 8960 verifyGoogleFormat("A< ::A> a;"); 8961 verifyGoogleFormat("A< ::A<int> > a;"); 8962 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8963 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8964 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8965 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8966 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8967 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8968 8969 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8970 8971 // template closer followed by a token that starts with > or = 8972 verifyFormat("bool b = a<1> > 1;"); 8973 verifyFormat("bool b = a<1> >= 1;"); 8974 verifyFormat("int i = a<1> >> 1;"); 8975 FormatStyle Style = getLLVMStyle(); 8976 Style.SpaceBeforeAssignmentOperators = false; 8977 verifyFormat("bool b= a<1> == 1;", Style); 8978 verifyFormat("a<int> = 1;", Style); 8979 verifyFormat("a<int> >>= 1;", Style); 8980 8981 verifyFormat("test < a | b >> c;"); 8982 verifyFormat("test<test<a | b>> c;"); 8983 verifyFormat("test >> a >> b;"); 8984 verifyFormat("test << a >> b;"); 8985 8986 verifyFormat("f<int>();"); 8987 verifyFormat("template <typename T> void f() {}"); 8988 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8989 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8990 "sizeof(char)>::type>;"); 8991 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8992 verifyFormat("f(a.operator()<A>());"); 8993 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8994 " .template operator()<A>());", 8995 getLLVMStyleWithColumns(35)); 8996 8997 // Not template parameters. 8998 verifyFormat("return a < b && c > d;"); 8999 verifyFormat("void f() {\n" 9000 " while (a < b && c > d) {\n" 9001 " }\n" 9002 "}"); 9003 verifyFormat("template <typename... Types>\n" 9004 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 9005 9006 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9007 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 9008 getLLVMStyleWithColumns(60)); 9009 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 9010 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 9011 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 9012 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 9013 } 9014 9015 TEST_F(FormatTest, UnderstandsShiftOperators) { 9016 verifyFormat("if (i < x >> 1)"); 9017 verifyFormat("while (i < x >> 1)"); 9018 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 9019 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 9020 verifyFormat( 9021 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 9022 verifyFormat("Foo.call<Bar<Function>>()"); 9023 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 9024 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 9025 "++i, v = v >> 1)"); 9026 verifyFormat("if (w<u<v<x>>, 1>::t)"); 9027 } 9028 9029 TEST_F(FormatTest, BitshiftOperatorWidth) { 9030 EXPECT_EQ("int a = 1 << 2; /* foo\n" 9031 " bar */", 9032 format("int a=1<<2; /* foo\n" 9033 " bar */")); 9034 9035 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 9036 " bar */", 9037 format("int b =256>>1 ; /* foo\n" 9038 " bar */")); 9039 } 9040 9041 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9042 verifyFormat("COMPARE(a, ==, b);"); 9043 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9044 } 9045 9046 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9047 verifyFormat("int A::*x;"); 9048 verifyFormat("int (S::*func)(void *);"); 9049 verifyFormat("void f() { int (S::*func)(void *); }"); 9050 verifyFormat("typedef bool *(Class::*Member)() const;"); 9051 verifyFormat("void f() {\n" 9052 " (a->*f)();\n" 9053 " a->*x;\n" 9054 " (a.*f)();\n" 9055 " ((*a).*f)();\n" 9056 " a.*x;\n" 9057 "}"); 9058 verifyFormat("void f() {\n" 9059 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9060 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9061 "}"); 9062 verifyFormat( 9063 "(aaaaaaaaaa->*bbbbbbb)(\n" 9064 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9065 FormatStyle Style = getLLVMStyle(); 9066 Style.PointerAlignment = FormatStyle::PAS_Left; 9067 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9068 } 9069 9070 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9071 verifyFormat("int a = -2;"); 9072 verifyFormat("f(-1, -2, -3);"); 9073 verifyFormat("a[-1] = 5;"); 9074 verifyFormat("int a = 5 + -2;"); 9075 verifyFormat("if (i == -1) {\n}"); 9076 verifyFormat("if (i != -1) {\n}"); 9077 verifyFormat("if (i > -1) {\n}"); 9078 verifyFormat("if (i < -1) {\n}"); 9079 verifyFormat("++(a->f());"); 9080 verifyFormat("--(a->f());"); 9081 verifyFormat("(a->f())++;"); 9082 verifyFormat("a[42]++;"); 9083 verifyFormat("if (!(a->f())) {\n}"); 9084 verifyFormat("if (!+i) {\n}"); 9085 verifyFormat("~&a;"); 9086 9087 verifyFormat("a-- > b;"); 9088 verifyFormat("b ? -a : c;"); 9089 verifyFormat("n * sizeof char16;"); 9090 verifyFormat("n * alignof char16;", getGoogleStyle()); 9091 verifyFormat("sizeof(char);"); 9092 verifyFormat("alignof(char);", getGoogleStyle()); 9093 9094 verifyFormat("return -1;"); 9095 verifyFormat("throw -1;"); 9096 verifyFormat("switch (a) {\n" 9097 "case -1:\n" 9098 " break;\n" 9099 "}"); 9100 verifyFormat("#define X -1"); 9101 verifyFormat("#define X -kConstant"); 9102 9103 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9104 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9105 9106 verifyFormat("int a = /* confusing comment */ -1;"); 9107 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9108 verifyFormat("int a = i /* confusing comment */++;"); 9109 9110 verifyFormat("co_yield -1;"); 9111 verifyFormat("co_return -1;"); 9112 9113 // Check that * is not treated as a binary operator when we set 9114 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9115 FormatStyle PASLeftStyle = getLLVMStyle(); 9116 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9117 verifyFormat("co_return *a;", PASLeftStyle); 9118 verifyFormat("co_await *a;", PASLeftStyle); 9119 verifyFormat("co_yield *a", PASLeftStyle); 9120 verifyFormat("return *a;", PASLeftStyle); 9121 } 9122 9123 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9124 verifyFormat("if (!aaaaaaaaaa( // break\n" 9125 " aaaaa)) {\n" 9126 "}"); 9127 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9128 " aaaaa));"); 9129 verifyFormat("*aaa = aaaaaaa( // break\n" 9130 " bbbbbb);"); 9131 } 9132 9133 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9134 verifyFormat("bool operator<();"); 9135 verifyFormat("bool operator>();"); 9136 verifyFormat("bool operator=();"); 9137 verifyFormat("bool operator==();"); 9138 verifyFormat("bool operator!=();"); 9139 verifyFormat("int operator+();"); 9140 verifyFormat("int operator++();"); 9141 verifyFormat("int operator++(int) volatile noexcept;"); 9142 verifyFormat("bool operator,();"); 9143 verifyFormat("bool operator();"); 9144 verifyFormat("bool operator()();"); 9145 verifyFormat("bool operator[]();"); 9146 verifyFormat("operator bool();"); 9147 verifyFormat("operator int();"); 9148 verifyFormat("operator void *();"); 9149 verifyFormat("operator SomeType<int>();"); 9150 verifyFormat("operator SomeType<int, int>();"); 9151 verifyFormat("operator SomeType<SomeType<int>>();"); 9152 verifyFormat("void *operator new(std::size_t size);"); 9153 verifyFormat("void *operator new[](std::size_t size);"); 9154 verifyFormat("void operator delete(void *ptr);"); 9155 verifyFormat("void operator delete[](void *ptr);"); 9156 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9157 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9158 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9159 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9160 9161 verifyFormat( 9162 "ostream &operator<<(ostream &OutputStream,\n" 9163 " SomeReallyLongType WithSomeReallyLongValue);"); 9164 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9165 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9166 " return left.group < right.group;\n" 9167 "}"); 9168 verifyFormat("SomeType &operator=(const SomeType &S);"); 9169 verifyFormat("f.template operator()<int>();"); 9170 9171 verifyGoogleFormat("operator void*();"); 9172 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9173 verifyGoogleFormat("operator ::A();"); 9174 9175 verifyFormat("using A::operator+;"); 9176 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9177 "int i;"); 9178 9179 // Calling an operator as a member function. 9180 verifyFormat("void f() { a.operator*(); }"); 9181 verifyFormat("void f() { a.operator*(b & b); }"); 9182 verifyFormat("void f() { a->operator&(a * b); }"); 9183 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9184 // TODO: Calling an operator as a non-member function is hard to distinguish. 9185 // https://llvm.org/PR50629 9186 // verifyFormat("void f() { operator*(a & a); }"); 9187 // verifyFormat("void f() { operator&(a, b * b); }"); 9188 9189 verifyFormat("::operator delete(foo);"); 9190 verifyFormat("::operator new(n * sizeof(foo));"); 9191 verifyFormat("foo() { ::operator delete(foo); }"); 9192 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 9193 } 9194 9195 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9196 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9197 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9198 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9199 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9200 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9201 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9202 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9203 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9204 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9205 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9206 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9207 verifyFormat("void Fn(T const &) const &;"); 9208 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9209 verifyFormat("template <typename T>\n" 9210 "void F(T) && = delete;", 9211 getGoogleStyle()); 9212 9213 FormatStyle AlignLeft = getLLVMStyle(); 9214 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9215 verifyFormat("void A::b() && {}", AlignLeft); 9216 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9217 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9218 AlignLeft); 9219 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9220 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9221 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9222 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9223 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9224 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9225 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9226 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9227 9228 FormatStyle Spaces = getLLVMStyle(); 9229 Spaces.SpacesInCStyleCastParentheses = true; 9230 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9231 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9232 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9233 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9234 9235 Spaces.SpacesInCStyleCastParentheses = false; 9236 Spaces.SpacesInParentheses = true; 9237 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9238 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9239 Spaces); 9240 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9241 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9242 9243 FormatStyle BreakTemplate = getLLVMStyle(); 9244 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9245 9246 verifyFormat("struct f {\n" 9247 " template <class T>\n" 9248 " int &foo(const std::string &str) &noexcept {}\n" 9249 "};", 9250 BreakTemplate); 9251 9252 verifyFormat("struct f {\n" 9253 " template <class T>\n" 9254 " int &foo(const std::string &str) &&noexcept {}\n" 9255 "};", 9256 BreakTemplate); 9257 9258 verifyFormat("struct f {\n" 9259 " template <class T>\n" 9260 " int &foo(const std::string &str) const &noexcept {}\n" 9261 "};", 9262 BreakTemplate); 9263 9264 verifyFormat("struct f {\n" 9265 " template <class T>\n" 9266 " int &foo(const std::string &str) const &noexcept {}\n" 9267 "};", 9268 BreakTemplate); 9269 9270 verifyFormat("struct f {\n" 9271 " template <class T>\n" 9272 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9273 "};", 9274 BreakTemplate); 9275 9276 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9277 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9278 FormatStyle::BTDS_Yes; 9279 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9280 9281 verifyFormat("struct f {\n" 9282 " template <class T>\n" 9283 " int& foo(const std::string& str) & noexcept {}\n" 9284 "};", 9285 AlignLeftBreakTemplate); 9286 9287 verifyFormat("struct f {\n" 9288 " template <class T>\n" 9289 " int& foo(const std::string& str) && noexcept {}\n" 9290 "};", 9291 AlignLeftBreakTemplate); 9292 9293 verifyFormat("struct f {\n" 9294 " template <class T>\n" 9295 " int& foo(const std::string& str) const& noexcept {}\n" 9296 "};", 9297 AlignLeftBreakTemplate); 9298 9299 verifyFormat("struct f {\n" 9300 " template <class T>\n" 9301 " int& foo(const std::string& str) const&& noexcept {}\n" 9302 "};", 9303 AlignLeftBreakTemplate); 9304 9305 verifyFormat("struct f {\n" 9306 " template <class T>\n" 9307 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9308 "};", 9309 AlignLeftBreakTemplate); 9310 9311 // The `&` in `Type&` should not be confused with a trailing `&` of 9312 // DEPRECATED(reason) member function. 9313 verifyFormat("struct f {\n" 9314 " template <class T>\n" 9315 " DEPRECATED(reason)\n" 9316 " Type &foo(arguments) {}\n" 9317 "};", 9318 BreakTemplate); 9319 9320 verifyFormat("struct f {\n" 9321 " template <class T>\n" 9322 " DEPRECATED(reason)\n" 9323 " Type& foo(arguments) {}\n" 9324 "};", 9325 AlignLeftBreakTemplate); 9326 9327 verifyFormat("void (*foopt)(int) = &func;"); 9328 } 9329 9330 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9331 verifyFormat("void f() {\n" 9332 " A *a = new A;\n" 9333 " A *a = new (placement) A;\n" 9334 " delete a;\n" 9335 " delete (A *)a;\n" 9336 "}"); 9337 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9338 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9339 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9340 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9341 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9342 verifyFormat("delete[] h->p;"); 9343 } 9344 9345 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9346 verifyFormat("int *f(int *a) {}"); 9347 verifyFormat("int main(int argc, char **argv) {}"); 9348 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9349 verifyIndependentOfContext("f(a, *a);"); 9350 verifyFormat("void g() { f(*a); }"); 9351 verifyIndependentOfContext("int a = b * 10;"); 9352 verifyIndependentOfContext("int a = 10 * b;"); 9353 verifyIndependentOfContext("int a = b * c;"); 9354 verifyIndependentOfContext("int a += b * c;"); 9355 verifyIndependentOfContext("int a -= b * c;"); 9356 verifyIndependentOfContext("int a *= b * c;"); 9357 verifyIndependentOfContext("int a /= b * c;"); 9358 verifyIndependentOfContext("int a = *b;"); 9359 verifyIndependentOfContext("int a = *b * c;"); 9360 verifyIndependentOfContext("int a = b * *c;"); 9361 verifyIndependentOfContext("int a = b * (10);"); 9362 verifyIndependentOfContext("S << b * (10);"); 9363 verifyIndependentOfContext("return 10 * b;"); 9364 verifyIndependentOfContext("return *b * *c;"); 9365 verifyIndependentOfContext("return a & ~b;"); 9366 verifyIndependentOfContext("f(b ? *c : *d);"); 9367 verifyIndependentOfContext("int a = b ? *c : *d;"); 9368 verifyIndependentOfContext("*b = a;"); 9369 verifyIndependentOfContext("a * ~b;"); 9370 verifyIndependentOfContext("a * !b;"); 9371 verifyIndependentOfContext("a * +b;"); 9372 verifyIndependentOfContext("a * -b;"); 9373 verifyIndependentOfContext("a * ++b;"); 9374 verifyIndependentOfContext("a * --b;"); 9375 verifyIndependentOfContext("a[4] * b;"); 9376 verifyIndependentOfContext("a[a * a] = 1;"); 9377 verifyIndependentOfContext("f() * b;"); 9378 verifyIndependentOfContext("a * [self dostuff];"); 9379 verifyIndependentOfContext("int x = a * (a + b);"); 9380 verifyIndependentOfContext("(a *)(a + b);"); 9381 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9382 verifyIndependentOfContext("int *pa = (int *)&a;"); 9383 verifyIndependentOfContext("return sizeof(int **);"); 9384 verifyIndependentOfContext("return sizeof(int ******);"); 9385 verifyIndependentOfContext("return (int **&)a;"); 9386 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9387 verifyFormat("void f(Type (*parameter)[10]) {}"); 9388 verifyFormat("void f(Type (¶meter)[10]) {}"); 9389 verifyGoogleFormat("return sizeof(int**);"); 9390 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9391 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9392 verifyFormat("auto a = [](int **&, int ***) {};"); 9393 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9394 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9395 verifyFormat("[](const decltype(*a) &value) {}"); 9396 verifyFormat("[](const typeof(*a) &value) {}"); 9397 verifyFormat("[](const _Atomic(a *) &value) {}"); 9398 verifyFormat("[](const __underlying_type(a) &value) {}"); 9399 verifyFormat("decltype(a * b) F();"); 9400 verifyFormat("typeof(a * b) F();"); 9401 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9402 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9403 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9404 verifyIndependentOfContext("int i{a * b};"); 9405 verifyIndependentOfContext("aaa && aaa->f();"); 9406 verifyIndependentOfContext("int x = ~*p;"); 9407 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9408 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9409 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9410 verifyFormat("void f() { f(a, c * d); }"); 9411 verifyFormat("void f() { f(new a(), c * d); }"); 9412 verifyFormat("void f(const MyOverride &override);"); 9413 verifyFormat("void f(const MyFinal &final);"); 9414 verifyIndependentOfContext("bool a = f() && override.f();"); 9415 verifyIndependentOfContext("bool a = f() && final.f();"); 9416 9417 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9418 9419 verifyIndependentOfContext("A<int *> a;"); 9420 verifyIndependentOfContext("A<int **> a;"); 9421 verifyIndependentOfContext("A<int *, int *> a;"); 9422 verifyIndependentOfContext("A<int *[]> a;"); 9423 verifyIndependentOfContext( 9424 "const char *const p = reinterpret_cast<const char *const>(q);"); 9425 verifyIndependentOfContext("A<int **, int **> a;"); 9426 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9427 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9428 verifyFormat("for (; a && b;) {\n}"); 9429 verifyFormat("bool foo = true && [] { return false; }();"); 9430 9431 verifyFormat( 9432 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9434 9435 verifyGoogleFormat("int const* a = &b;"); 9436 verifyGoogleFormat("**outparam = 1;"); 9437 verifyGoogleFormat("*outparam = a * b;"); 9438 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9439 verifyGoogleFormat("A<int*> a;"); 9440 verifyGoogleFormat("A<int**> a;"); 9441 verifyGoogleFormat("A<int*, int*> a;"); 9442 verifyGoogleFormat("A<int**, int**> a;"); 9443 verifyGoogleFormat("f(b ? *c : *d);"); 9444 verifyGoogleFormat("int a = b ? *c : *d;"); 9445 verifyGoogleFormat("Type* t = **x;"); 9446 verifyGoogleFormat("Type* t = *++*x;"); 9447 verifyGoogleFormat("*++*x;"); 9448 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9449 verifyGoogleFormat("Type* t = x++ * y;"); 9450 verifyGoogleFormat( 9451 "const char* const p = reinterpret_cast<const char* const>(q);"); 9452 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9453 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9454 verifyGoogleFormat("template <typename T>\n" 9455 "void f(int i = 0, SomeType** temps = NULL);"); 9456 9457 FormatStyle Left = getLLVMStyle(); 9458 Left.PointerAlignment = FormatStyle::PAS_Left; 9459 verifyFormat("x = *a(x) = *a(y);", Left); 9460 verifyFormat("for (;; *a = b) {\n}", Left); 9461 verifyFormat("return *this += 1;", Left); 9462 verifyFormat("throw *x;", Left); 9463 verifyFormat("delete *x;", Left); 9464 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9465 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9466 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9467 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9468 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9469 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9470 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9471 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9472 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9473 9474 verifyIndependentOfContext("a = *(x + y);"); 9475 verifyIndependentOfContext("a = &(x + y);"); 9476 verifyIndependentOfContext("*(x + y).call();"); 9477 verifyIndependentOfContext("&(x + y)->call();"); 9478 verifyFormat("void f() { &(*I).first; }"); 9479 9480 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9481 verifyFormat("f(* /* confusing comment */ foo);"); 9482 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 9483 verifyFormat("void foo(int * // this is the first paramters\n" 9484 " ,\n" 9485 " int second);"); 9486 verifyFormat("double term = a * // first\n" 9487 " b;"); 9488 verifyFormat( 9489 "int *MyValues = {\n" 9490 " *A, // Operator detection might be confused by the '{'\n" 9491 " *BB // Operator detection might be confused by previous comment\n" 9492 "};"); 9493 9494 verifyIndependentOfContext("if (int *a = &b)"); 9495 verifyIndependentOfContext("if (int &a = *b)"); 9496 verifyIndependentOfContext("if (a & b[i])"); 9497 verifyIndependentOfContext("if constexpr (a & b[i])"); 9498 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9499 verifyIndependentOfContext("if (a * (b * c))"); 9500 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9501 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9502 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9503 verifyIndependentOfContext("if (*b[i])"); 9504 verifyIndependentOfContext("if (int *a = (&b))"); 9505 verifyIndependentOfContext("while (int *a = &b)"); 9506 verifyIndependentOfContext("while (a * (b * c))"); 9507 verifyIndependentOfContext("size = sizeof *a;"); 9508 verifyIndependentOfContext("if (a && (b = c))"); 9509 verifyFormat("void f() {\n" 9510 " for (const int &v : Values) {\n" 9511 " }\n" 9512 "}"); 9513 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9514 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9515 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9516 9517 verifyFormat("#define A (!a * b)"); 9518 verifyFormat("#define MACRO \\\n" 9519 " int *i = a * b; \\\n" 9520 " void f(a *b);", 9521 getLLVMStyleWithColumns(19)); 9522 9523 verifyIndependentOfContext("A = new SomeType *[Length];"); 9524 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9525 verifyIndependentOfContext("T **t = new T *;"); 9526 verifyIndependentOfContext("T **t = new T *();"); 9527 verifyGoogleFormat("A = new SomeType*[Length]();"); 9528 verifyGoogleFormat("A = new SomeType*[Length];"); 9529 verifyGoogleFormat("T** t = new T*;"); 9530 verifyGoogleFormat("T** t = new T*();"); 9531 9532 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9533 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9534 verifyFormat("template <bool a, bool b> " 9535 "typename t::if<x && y>::type f() {}"); 9536 verifyFormat("template <int *y> f() {}"); 9537 verifyFormat("vector<int *> v;"); 9538 verifyFormat("vector<int *const> v;"); 9539 verifyFormat("vector<int *const **const *> v;"); 9540 verifyFormat("vector<int *volatile> v;"); 9541 verifyFormat("vector<a *_Nonnull> v;"); 9542 verifyFormat("vector<a *_Nullable> v;"); 9543 verifyFormat("vector<a *_Null_unspecified> v;"); 9544 verifyFormat("vector<a *__ptr32> v;"); 9545 verifyFormat("vector<a *__ptr64> v;"); 9546 verifyFormat("vector<a *__capability> v;"); 9547 FormatStyle TypeMacros = getLLVMStyle(); 9548 TypeMacros.TypenameMacros = {"LIST"}; 9549 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9550 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9551 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9552 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9553 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9554 9555 FormatStyle CustomQualifier = getLLVMStyle(); 9556 // Add identifiers that should not be parsed as a qualifier by default. 9557 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9558 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9559 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9560 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9561 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9562 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9563 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9564 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9565 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9566 verifyFormat("vector<a * _NotAQualifier> v;"); 9567 verifyFormat("vector<a * __not_a_qualifier> v;"); 9568 verifyFormat("vector<a * b> v;"); 9569 verifyFormat("foo<b && false>();"); 9570 verifyFormat("foo<b & 1>();"); 9571 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9572 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9573 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9574 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9575 verifyFormat( 9576 "template <class T, class = typename std::enable_if<\n" 9577 " std::is_integral<T>::value &&\n" 9578 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9579 "void F();", 9580 getLLVMStyleWithColumns(70)); 9581 verifyFormat("template <class T,\n" 9582 " class = typename std::enable_if<\n" 9583 " std::is_integral<T>::value &&\n" 9584 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9585 " class U>\n" 9586 "void F();", 9587 getLLVMStyleWithColumns(70)); 9588 verifyFormat( 9589 "template <class T,\n" 9590 " class = typename ::std::enable_if<\n" 9591 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9592 "void F();", 9593 getGoogleStyleWithColumns(68)); 9594 9595 verifyIndependentOfContext("MACRO(int *i);"); 9596 verifyIndependentOfContext("MACRO(auto *a);"); 9597 verifyIndependentOfContext("MACRO(const A *a);"); 9598 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9599 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9600 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9601 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9602 verifyIndependentOfContext("MACRO(A *const a);"); 9603 verifyIndependentOfContext("MACRO(A *restrict a);"); 9604 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9605 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9606 verifyIndependentOfContext("MACRO(A *volatile a);"); 9607 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9608 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9609 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9610 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9611 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9612 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9613 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9614 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9615 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9616 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9617 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9618 verifyIndependentOfContext("MACRO(A *__capability);"); 9619 verifyIndependentOfContext("MACRO(A &__capability);"); 9620 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9621 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9622 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9623 // a type declaration: 9624 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9625 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9626 // Also check that TypenameMacros prevents parsing it as multiplication: 9627 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9628 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9629 9630 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9631 verifyFormat("void f() { f(float{1}, a * a); }"); 9632 verifyFormat("void f() { f(float(1), a * a); }"); 9633 9634 verifyFormat("f((void (*)(int))g);"); 9635 verifyFormat("f((void (&)(int))g);"); 9636 verifyFormat("f((void (^)(int))g);"); 9637 9638 // FIXME: Is there a way to make this work? 9639 // verifyIndependentOfContext("MACRO(A *a);"); 9640 verifyFormat("MACRO(A &B);"); 9641 verifyFormat("MACRO(A *B);"); 9642 verifyFormat("void f() { MACRO(A * B); }"); 9643 verifyFormat("void f() { MACRO(A & B); }"); 9644 9645 // This lambda was mis-formatted after D88956 (treating it as a binop): 9646 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9647 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9648 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9649 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9650 9651 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9652 verifyFormat("return options != nullptr && operator==(*options);"); 9653 9654 EXPECT_EQ("#define OP(x) \\\n" 9655 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9656 " return s << a.DebugString(); \\\n" 9657 " }", 9658 format("#define OP(x) \\\n" 9659 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9660 " return s << a.DebugString(); \\\n" 9661 " }", 9662 getLLVMStyleWithColumns(50))); 9663 9664 // FIXME: We cannot handle this case yet; we might be able to figure out that 9665 // foo<x> d > v; doesn't make sense. 9666 verifyFormat("foo<a<b && c> d> v;"); 9667 9668 FormatStyle PointerMiddle = getLLVMStyle(); 9669 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9670 verifyFormat("delete *x;", PointerMiddle); 9671 verifyFormat("int * x;", PointerMiddle); 9672 verifyFormat("int *[] x;", PointerMiddle); 9673 verifyFormat("template <int * y> f() {}", PointerMiddle); 9674 verifyFormat("int * f(int * a) {}", PointerMiddle); 9675 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9676 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9677 verifyFormat("A<int *> a;", PointerMiddle); 9678 verifyFormat("A<int **> a;", PointerMiddle); 9679 verifyFormat("A<int *, int *> a;", PointerMiddle); 9680 verifyFormat("A<int *[]> a;", PointerMiddle); 9681 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9682 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9683 verifyFormat("T ** t = new T *;", PointerMiddle); 9684 9685 // Member function reference qualifiers aren't binary operators. 9686 verifyFormat("string // break\n" 9687 "operator()() & {}"); 9688 verifyFormat("string // break\n" 9689 "operator()() && {}"); 9690 verifyGoogleFormat("template <typename T>\n" 9691 "auto x() & -> int {}"); 9692 9693 // Should be binary operators when used as an argument expression (overloaded 9694 // operator invoked as a member function). 9695 verifyFormat("void f() { a.operator()(a * a); }"); 9696 verifyFormat("void f() { a->operator()(a & a); }"); 9697 verifyFormat("void f() { a.operator()(*a & *a); }"); 9698 verifyFormat("void f() { a->operator()(*a * *a); }"); 9699 9700 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 9701 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 9702 } 9703 9704 TEST_F(FormatTest, UnderstandsAttributes) { 9705 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9706 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9707 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9708 FormatStyle AfterType = getLLVMStyle(); 9709 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9710 verifyFormat("__attribute__((nodebug)) void\n" 9711 "foo() {}\n", 9712 AfterType); 9713 verifyFormat("__unused void\n" 9714 "foo() {}", 9715 AfterType); 9716 9717 FormatStyle CustomAttrs = getLLVMStyle(); 9718 CustomAttrs.AttributeMacros.push_back("__unused"); 9719 CustomAttrs.AttributeMacros.push_back("__attr1"); 9720 CustomAttrs.AttributeMacros.push_back("__attr2"); 9721 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9722 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9723 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9724 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9725 // Check that it is parsed as a multiplication without AttributeMacros and 9726 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9727 verifyFormat("vector<SomeType * __attr1> v;"); 9728 verifyFormat("vector<SomeType __attr1 *> v;"); 9729 verifyFormat("vector<SomeType __attr1 *const> v;"); 9730 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9731 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9732 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9733 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9734 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9735 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9736 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9737 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9738 9739 // Check that these are not parsed as function declarations: 9740 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9741 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9742 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9743 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9744 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9745 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9746 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9747 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9748 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9749 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9750 } 9751 9752 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9753 // Check that qualifiers on pointers don't break parsing of casts. 9754 verifyFormat("x = (foo *const)*v;"); 9755 verifyFormat("x = (foo *volatile)*v;"); 9756 verifyFormat("x = (foo *restrict)*v;"); 9757 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9758 verifyFormat("x = (foo *_Nonnull)*v;"); 9759 verifyFormat("x = (foo *_Nullable)*v;"); 9760 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9761 verifyFormat("x = (foo *_Nonnull)*v;"); 9762 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9763 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9764 verifyFormat("x = (foo *__ptr32)*v;"); 9765 verifyFormat("x = (foo *__ptr64)*v;"); 9766 verifyFormat("x = (foo *__capability)*v;"); 9767 9768 // Check that we handle multiple trailing qualifiers and skip them all to 9769 // determine that the expression is a cast to a pointer type. 9770 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9771 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9772 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9773 StringRef AllQualifiers = 9774 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9775 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9776 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9777 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9778 9779 // Also check that address-of is not parsed as a binary bitwise-and: 9780 verifyFormat("x = (foo *const)&v;"); 9781 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9782 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9783 9784 // Check custom qualifiers: 9785 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9786 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9787 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9788 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9789 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9790 CustomQualifier); 9791 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9792 CustomQualifier); 9793 9794 // Check that unknown identifiers result in binary operator parsing: 9795 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9796 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9797 } 9798 9799 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9800 verifyFormat("SomeType s [[unused]] (InitValue);"); 9801 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9802 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9803 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9804 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9805 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9806 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9807 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9808 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9809 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9810 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9811 9812 // Make sure we do not mistake attributes for array subscripts. 9813 verifyFormat("int a() {}\n" 9814 "[[unused]] int b() {}\n"); 9815 verifyFormat("NSArray *arr;\n" 9816 "arr[[Foo() bar]];"); 9817 9818 // On the other hand, we still need to correctly find array subscripts. 9819 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9820 9821 // Make sure that we do not mistake Objective-C method inside array literals 9822 // as attributes, even if those method names are also keywords. 9823 verifyFormat("@[ [foo bar] ];"); 9824 verifyFormat("@[ [NSArray class] ];"); 9825 verifyFormat("@[ [foo enum] ];"); 9826 9827 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9828 9829 // Make sure we do not parse attributes as lambda introducers. 9830 FormatStyle MultiLineFunctions = getLLVMStyle(); 9831 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9832 verifyFormat("[[unused]] int b() {\n" 9833 " return 42;\n" 9834 "}\n", 9835 MultiLineFunctions); 9836 } 9837 9838 TEST_F(FormatTest, AttributeClass) { 9839 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9840 verifyFormat("class S {\n" 9841 " S(S&&) = default;\n" 9842 "};", 9843 Style); 9844 verifyFormat("class [[nodiscard]] S {\n" 9845 " S(S&&) = default;\n" 9846 "};", 9847 Style); 9848 verifyFormat("class __attribute((maybeunused)) S {\n" 9849 " S(S&&) = default;\n" 9850 "};", 9851 Style); 9852 verifyFormat("struct S {\n" 9853 " S(S&&) = default;\n" 9854 "};", 9855 Style); 9856 verifyFormat("struct [[nodiscard]] S {\n" 9857 " S(S&&) = default;\n" 9858 "};", 9859 Style); 9860 } 9861 9862 TEST_F(FormatTest, AttributesAfterMacro) { 9863 FormatStyle Style = getLLVMStyle(); 9864 verifyFormat("MACRO;\n" 9865 "__attribute__((maybe_unused)) int foo() {\n" 9866 " //...\n" 9867 "}"); 9868 9869 verifyFormat("MACRO;\n" 9870 "[[nodiscard]] int foo() {\n" 9871 " //...\n" 9872 "}"); 9873 9874 EXPECT_EQ("MACRO\n\n" 9875 "__attribute__((maybe_unused)) int foo() {\n" 9876 " //...\n" 9877 "}", 9878 format("MACRO\n\n" 9879 "__attribute__((maybe_unused)) int foo() {\n" 9880 " //...\n" 9881 "}")); 9882 9883 EXPECT_EQ("MACRO\n\n" 9884 "[[nodiscard]] int foo() {\n" 9885 " //...\n" 9886 "}", 9887 format("MACRO\n\n" 9888 "[[nodiscard]] int foo() {\n" 9889 " //...\n" 9890 "}")); 9891 } 9892 9893 TEST_F(FormatTest, AttributePenaltyBreaking) { 9894 FormatStyle Style = getLLVMStyle(); 9895 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 9896 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9897 Style); 9898 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 9899 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9900 Style); 9901 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 9902 "shared_ptr<ALongTypeName> &C d) {\n}", 9903 Style); 9904 } 9905 9906 TEST_F(FormatTest, UnderstandsEllipsis) { 9907 FormatStyle Style = getLLVMStyle(); 9908 verifyFormat("int printf(const char *fmt, ...);"); 9909 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 9910 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 9911 9912 verifyFormat("template <int *...PP> a;", Style); 9913 9914 Style.PointerAlignment = FormatStyle::PAS_Left; 9915 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9916 9917 verifyFormat("template <int*... PP> a;", Style); 9918 9919 Style.PointerAlignment = FormatStyle::PAS_Middle; 9920 verifyFormat("template <int *... PP> a;", Style); 9921 } 9922 9923 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9924 EXPECT_EQ("int *a;\n" 9925 "int *a;\n" 9926 "int *a;", 9927 format("int *a;\n" 9928 "int* a;\n" 9929 "int *a;", 9930 getGoogleStyle())); 9931 EXPECT_EQ("int* a;\n" 9932 "int* a;\n" 9933 "int* a;", 9934 format("int* a;\n" 9935 "int* a;\n" 9936 "int *a;", 9937 getGoogleStyle())); 9938 EXPECT_EQ("int *a;\n" 9939 "int *a;\n" 9940 "int *a;", 9941 format("int *a;\n" 9942 "int * a;\n" 9943 "int * a;", 9944 getGoogleStyle())); 9945 EXPECT_EQ("auto x = [] {\n" 9946 " int *a;\n" 9947 " int *a;\n" 9948 " int *a;\n" 9949 "};", 9950 format("auto x=[]{int *a;\n" 9951 "int * a;\n" 9952 "int * a;};", 9953 getGoogleStyle())); 9954 } 9955 9956 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9957 verifyFormat("int f(int &&a) {}"); 9958 verifyFormat("int f(int a, char &&b) {}"); 9959 verifyFormat("void f() { int &&a = b; }"); 9960 verifyGoogleFormat("int f(int a, char&& b) {}"); 9961 verifyGoogleFormat("void f() { int&& a = b; }"); 9962 9963 verifyIndependentOfContext("A<int &&> a;"); 9964 verifyIndependentOfContext("A<int &&, int &&> a;"); 9965 verifyGoogleFormat("A<int&&> a;"); 9966 verifyGoogleFormat("A<int&&, int&&> a;"); 9967 9968 // Not rvalue references: 9969 verifyFormat("template <bool B, bool C> class A {\n" 9970 " static_assert(B && C, \"Something is wrong\");\n" 9971 "};"); 9972 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9973 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9974 verifyFormat("#define A(a, b) (a && b)"); 9975 } 9976 9977 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9978 verifyFormat("void f() {\n" 9979 " x[aaaaaaaaa -\n" 9980 " b] = 23;\n" 9981 "}", 9982 getLLVMStyleWithColumns(15)); 9983 } 9984 9985 TEST_F(FormatTest, FormatsCasts) { 9986 verifyFormat("Type *A = static_cast<Type *>(P);"); 9987 verifyFormat("Type *A = (Type *)P;"); 9988 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9989 verifyFormat("int a = (int)(2.0f);"); 9990 verifyFormat("int a = (int)2.0f;"); 9991 verifyFormat("x[(int32)y];"); 9992 verifyFormat("x = (int32)y;"); 9993 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9994 verifyFormat("int a = (int)*b;"); 9995 verifyFormat("int a = (int)2.0f;"); 9996 verifyFormat("int a = (int)~0;"); 9997 verifyFormat("int a = (int)++a;"); 9998 verifyFormat("int a = (int)sizeof(int);"); 9999 verifyFormat("int a = (int)+2;"); 10000 verifyFormat("my_int a = (my_int)2.0f;"); 10001 verifyFormat("my_int a = (my_int)sizeof(int);"); 10002 verifyFormat("return (my_int)aaa;"); 10003 verifyFormat("#define x ((int)-1)"); 10004 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 10005 verifyFormat("#define p(q) ((int *)&q)"); 10006 verifyFormat("fn(a)(b) + 1;"); 10007 10008 verifyFormat("void f() { my_int a = (my_int)*b; }"); 10009 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 10010 verifyFormat("my_int a = (my_int)~0;"); 10011 verifyFormat("my_int a = (my_int)++a;"); 10012 verifyFormat("my_int a = (my_int)-2;"); 10013 verifyFormat("my_int a = (my_int)1;"); 10014 verifyFormat("my_int a = (my_int *)1;"); 10015 verifyFormat("my_int a = (const my_int)-1;"); 10016 verifyFormat("my_int a = (const my_int *)-1;"); 10017 verifyFormat("my_int a = (my_int)(my_int)-1;"); 10018 verifyFormat("my_int a = (ns::my_int)-2;"); 10019 verifyFormat("case (my_int)ONE:"); 10020 verifyFormat("auto x = (X)this;"); 10021 // Casts in Obj-C style calls used to not be recognized as such. 10022 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 10023 10024 // FIXME: single value wrapped with paren will be treated as cast. 10025 verifyFormat("void f(int i = (kValue)*kMask) {}"); 10026 10027 verifyFormat("{ (void)F; }"); 10028 10029 // Don't break after a cast's 10030 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10031 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 10032 " bbbbbbbbbbbbbbbbbbbbbb);"); 10033 10034 // These are not casts. 10035 verifyFormat("void f(int *) {}"); 10036 verifyFormat("f(foo)->b;"); 10037 verifyFormat("f(foo).b;"); 10038 verifyFormat("f(foo)(b);"); 10039 verifyFormat("f(foo)[b];"); 10040 verifyFormat("[](foo) { return 4; }(bar);"); 10041 verifyFormat("(*funptr)(foo)[4];"); 10042 verifyFormat("funptrs[4](foo)[4];"); 10043 verifyFormat("void f(int *);"); 10044 verifyFormat("void f(int *) = 0;"); 10045 verifyFormat("void f(SmallVector<int>) {}"); 10046 verifyFormat("void f(SmallVector<int>);"); 10047 verifyFormat("void f(SmallVector<int>) = 0;"); 10048 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 10049 verifyFormat("int a = sizeof(int) * b;"); 10050 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 10051 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 10052 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 10053 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 10054 10055 // These are not casts, but at some point were confused with casts. 10056 verifyFormat("virtual void foo(int *) override;"); 10057 verifyFormat("virtual void foo(char &) const;"); 10058 verifyFormat("virtual void foo(int *a, char *) const;"); 10059 verifyFormat("int a = sizeof(int *) + b;"); 10060 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10061 verifyFormat("bool b = f(g<int>) && c;"); 10062 verifyFormat("typedef void (*f)(int i) func;"); 10063 verifyFormat("void operator++(int) noexcept;"); 10064 verifyFormat("void operator++(int &) noexcept;"); 10065 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10066 "&) noexcept;"); 10067 verifyFormat( 10068 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10069 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10070 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10071 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10072 verifyFormat("void operator delete(foo &) noexcept;"); 10073 verifyFormat("void operator delete(foo) noexcept;"); 10074 verifyFormat("void operator delete(int) noexcept;"); 10075 verifyFormat("void operator delete(int &) noexcept;"); 10076 verifyFormat("void operator delete(int &) volatile noexcept;"); 10077 verifyFormat("void operator delete(int &) const"); 10078 verifyFormat("void operator delete(int &) = default"); 10079 verifyFormat("void operator delete(int &) = delete"); 10080 verifyFormat("void operator delete(int &) [[noreturn]]"); 10081 verifyFormat("void operator delete(int &) throw();"); 10082 verifyFormat("void operator delete(int &) throw(int);"); 10083 verifyFormat("auto operator delete(int &) -> int;"); 10084 verifyFormat("auto operator delete(int &) override"); 10085 verifyFormat("auto operator delete(int &) final"); 10086 10087 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10088 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10089 // FIXME: The indentation here is not ideal. 10090 verifyFormat( 10091 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10092 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10093 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10094 } 10095 10096 TEST_F(FormatTest, FormatsFunctionTypes) { 10097 verifyFormat("A<bool()> a;"); 10098 verifyFormat("A<SomeType()> a;"); 10099 verifyFormat("A<void (*)(int, std::string)> a;"); 10100 verifyFormat("A<void *(int)>;"); 10101 verifyFormat("void *(*a)(int *, SomeType *);"); 10102 verifyFormat("int (*func)(void *);"); 10103 verifyFormat("void f() { int (*func)(void *); }"); 10104 verifyFormat("template <class CallbackClass>\n" 10105 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10106 10107 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10108 verifyGoogleFormat("void* (*a)(int);"); 10109 verifyGoogleFormat( 10110 "template <class CallbackClass>\n" 10111 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10112 10113 // Other constructs can look somewhat like function types: 10114 verifyFormat("A<sizeof(*x)> a;"); 10115 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10116 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10117 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10118 verifyFormat("int x = f(&h)();"); 10119 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10120 verifyFormat("std::function<\n" 10121 " LooooooooooongTemplatedType<\n" 10122 " SomeType>*(\n" 10123 " LooooooooooooooooongType type)>\n" 10124 " function;", 10125 getGoogleStyleWithColumns(40)); 10126 } 10127 10128 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10129 verifyFormat("A (*foo_)[6];"); 10130 verifyFormat("vector<int> (*foo_)[6];"); 10131 } 10132 10133 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10134 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10135 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10136 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10137 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10138 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10139 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10140 10141 // Different ways of ()-initializiation. 10142 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10143 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10144 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10145 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10146 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10147 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10148 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10149 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10150 10151 // Lambdas should not confuse the variable declaration heuristic. 10152 verifyFormat("LooooooooooooooooongType\n" 10153 " variable(nullptr, [](A *a) {});", 10154 getLLVMStyleWithColumns(40)); 10155 } 10156 10157 TEST_F(FormatTest, BreaksLongDeclarations) { 10158 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10159 " AnotherNameForTheLongType;"); 10160 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10162 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10163 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10164 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10165 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10166 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10167 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10168 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10169 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10170 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10171 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10172 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10173 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10174 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10175 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10176 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10177 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10178 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10179 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10180 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10181 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10182 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10183 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10184 FormatStyle Indented = getLLVMStyle(); 10185 Indented.IndentWrappedFunctionNames = true; 10186 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10187 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10188 Indented); 10189 verifyFormat( 10190 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10191 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10192 Indented); 10193 verifyFormat( 10194 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10195 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10196 Indented); 10197 verifyFormat( 10198 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10199 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10200 Indented); 10201 10202 // FIXME: Without the comment, this breaks after "(". 10203 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10204 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10205 getGoogleStyle()); 10206 10207 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10208 " int LoooooooooooooooooooongParam2) {}"); 10209 verifyFormat( 10210 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10211 " SourceLocation L, IdentifierIn *II,\n" 10212 " Type *T) {}"); 10213 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10214 "ReallyReaaallyLongFunctionName(\n" 10215 " const std::string &SomeParameter,\n" 10216 " const SomeType<string, SomeOtherTemplateParameter>\n" 10217 " &ReallyReallyLongParameterName,\n" 10218 " const SomeType<string, SomeOtherTemplateParameter>\n" 10219 " &AnotherLongParameterName) {}"); 10220 verifyFormat("template <typename A>\n" 10221 "SomeLoooooooooooooooooooooongType<\n" 10222 " typename some_namespace::SomeOtherType<A>::Type>\n" 10223 "Function() {}"); 10224 10225 verifyGoogleFormat( 10226 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10227 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10228 verifyGoogleFormat( 10229 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10230 " SourceLocation L) {}"); 10231 verifyGoogleFormat( 10232 "some_namespace::LongReturnType\n" 10233 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10234 " int first_long_parameter, int second_parameter) {}"); 10235 10236 verifyGoogleFormat("template <typename T>\n" 10237 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10238 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10239 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10240 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10241 10242 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10243 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10244 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10245 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10246 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10247 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10248 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10249 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10250 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10252 10253 verifyFormat("template <typename T> // Templates on own line.\n" 10254 "static int // Some comment.\n" 10255 "MyFunction(int a);", 10256 getLLVMStyle()); 10257 } 10258 10259 TEST_F(FormatTest, FormatsAccessModifiers) { 10260 FormatStyle Style = getLLVMStyle(); 10261 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10262 FormatStyle::ELBAMS_LogicalBlock); 10263 verifyFormat("struct foo {\n" 10264 "private:\n" 10265 " void f() {}\n" 10266 "\n" 10267 "private:\n" 10268 " int i;\n" 10269 "\n" 10270 "protected:\n" 10271 " int j;\n" 10272 "};\n", 10273 Style); 10274 verifyFormat("struct foo {\n" 10275 "private:\n" 10276 " void f() {}\n" 10277 "\n" 10278 "private:\n" 10279 " int i;\n" 10280 "\n" 10281 "protected:\n" 10282 " int j;\n" 10283 "};\n", 10284 "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 { /* comment */\n" 10294 "private:\n" 10295 " int i;\n" 10296 " // comment\n" 10297 "private:\n" 10298 " int j;\n" 10299 "};\n", 10300 Style); 10301 verifyFormat("struct foo {\n" 10302 "#ifdef FOO\n" 10303 "#endif\n" 10304 "private:\n" 10305 " int i;\n" 10306 "#ifdef FOO\n" 10307 "private:\n" 10308 "#endif\n" 10309 " int j;\n" 10310 "};\n", 10311 Style); 10312 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10313 verifyFormat("struct foo {\n" 10314 "private:\n" 10315 " void f() {}\n" 10316 "private:\n" 10317 " int i;\n" 10318 "protected:\n" 10319 " int j;\n" 10320 "};\n", 10321 Style); 10322 verifyFormat("struct foo {\n" 10323 "private:\n" 10324 " void f() {}\n" 10325 "private:\n" 10326 " int i;\n" 10327 "protected:\n" 10328 " int j;\n" 10329 "};\n", 10330 "struct foo {\n" 10331 "\n" 10332 "private:\n" 10333 " void f() {}\n" 10334 "\n" 10335 "private:\n" 10336 " int i;\n" 10337 "\n" 10338 "protected:\n" 10339 " int j;\n" 10340 "};\n", 10341 Style); 10342 verifyFormat("struct foo { /* comment */\n" 10343 "private:\n" 10344 " int i;\n" 10345 " // comment\n" 10346 "private:\n" 10347 " int j;\n" 10348 "};\n", 10349 "struct foo { /* comment */\n" 10350 "\n" 10351 "private:\n" 10352 " int i;\n" 10353 " // comment\n" 10354 "\n" 10355 "private:\n" 10356 " int j;\n" 10357 "};\n", 10358 Style); 10359 verifyFormat("struct foo {\n" 10360 "#ifdef FOO\n" 10361 "#endif\n" 10362 "private:\n" 10363 " int i;\n" 10364 "#ifdef FOO\n" 10365 "private:\n" 10366 "#endif\n" 10367 " int j;\n" 10368 "};\n", 10369 "struct foo {\n" 10370 "#ifdef FOO\n" 10371 "#endif\n" 10372 "\n" 10373 "private:\n" 10374 " int i;\n" 10375 "#ifdef FOO\n" 10376 "\n" 10377 "private:\n" 10378 "#endif\n" 10379 " int j;\n" 10380 "};\n", 10381 Style); 10382 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10383 verifyFormat("struct foo {\n" 10384 "private:\n" 10385 " void f() {}\n" 10386 "\n" 10387 "private:\n" 10388 " int i;\n" 10389 "\n" 10390 "protected:\n" 10391 " int j;\n" 10392 "};\n", 10393 Style); 10394 verifyFormat("struct foo {\n" 10395 "private:\n" 10396 " void f() {}\n" 10397 "\n" 10398 "private:\n" 10399 " int i;\n" 10400 "\n" 10401 "protected:\n" 10402 " int j;\n" 10403 "};\n", 10404 "struct foo {\n" 10405 "private:\n" 10406 " void f() {}\n" 10407 "private:\n" 10408 " int i;\n" 10409 "protected:\n" 10410 " int j;\n" 10411 "};\n", 10412 Style); 10413 verifyFormat("struct foo { /* comment */\n" 10414 "private:\n" 10415 " int i;\n" 10416 " // comment\n" 10417 "\n" 10418 "private:\n" 10419 " int j;\n" 10420 "};\n", 10421 "struct foo { /* comment */\n" 10422 "private:\n" 10423 " int i;\n" 10424 " // comment\n" 10425 "\n" 10426 "private:\n" 10427 " int j;\n" 10428 "};\n", 10429 Style); 10430 verifyFormat("struct foo {\n" 10431 "#ifdef FOO\n" 10432 "#endif\n" 10433 "\n" 10434 "private:\n" 10435 " int i;\n" 10436 "#ifdef FOO\n" 10437 "\n" 10438 "private:\n" 10439 "#endif\n" 10440 " int j;\n" 10441 "};\n", 10442 "struct foo {\n" 10443 "#ifdef FOO\n" 10444 "#endif\n" 10445 "private:\n" 10446 " int i;\n" 10447 "#ifdef FOO\n" 10448 "private:\n" 10449 "#endif\n" 10450 " int j;\n" 10451 "};\n", 10452 Style); 10453 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10454 EXPECT_EQ("struct foo {\n" 10455 "\n" 10456 "private:\n" 10457 " void f() {}\n" 10458 "\n" 10459 "private:\n" 10460 " int i;\n" 10461 "\n" 10462 "protected:\n" 10463 " int j;\n" 10464 "};\n", 10465 format("struct foo {\n" 10466 "\n" 10467 "private:\n" 10468 " void f() {}\n" 10469 "\n" 10470 "private:\n" 10471 " int i;\n" 10472 "\n" 10473 "protected:\n" 10474 " int j;\n" 10475 "};\n", 10476 Style)); 10477 verifyFormat("struct foo {\n" 10478 "private:\n" 10479 " void f() {}\n" 10480 "private:\n" 10481 " int i;\n" 10482 "protected:\n" 10483 " int j;\n" 10484 "};\n", 10485 Style); 10486 EXPECT_EQ("struct foo { /* comment */\n" 10487 "\n" 10488 "private:\n" 10489 " int i;\n" 10490 " // comment\n" 10491 "\n" 10492 "private:\n" 10493 " int j;\n" 10494 "};\n", 10495 format("struct foo { /* comment */\n" 10496 "\n" 10497 "private:\n" 10498 " int i;\n" 10499 " // comment\n" 10500 "\n" 10501 "private:\n" 10502 " int j;\n" 10503 "};\n", 10504 Style)); 10505 verifyFormat("struct foo { /* comment */\n" 10506 "private:\n" 10507 " int i;\n" 10508 " // comment\n" 10509 "private:\n" 10510 " int j;\n" 10511 "};\n", 10512 Style); 10513 EXPECT_EQ("struct foo {\n" 10514 "#ifdef FOO\n" 10515 "#endif\n" 10516 "\n" 10517 "private:\n" 10518 " int i;\n" 10519 "#ifdef FOO\n" 10520 "\n" 10521 "private:\n" 10522 "#endif\n" 10523 " int j;\n" 10524 "};\n", 10525 format("struct foo {\n" 10526 "#ifdef FOO\n" 10527 "#endif\n" 10528 "\n" 10529 "private:\n" 10530 " int i;\n" 10531 "#ifdef FOO\n" 10532 "\n" 10533 "private:\n" 10534 "#endif\n" 10535 " int j;\n" 10536 "};\n", 10537 Style)); 10538 verifyFormat("struct foo {\n" 10539 "#ifdef FOO\n" 10540 "#endif\n" 10541 "private:\n" 10542 " int i;\n" 10543 "#ifdef FOO\n" 10544 "private:\n" 10545 "#endif\n" 10546 " int j;\n" 10547 "};\n", 10548 Style); 10549 10550 FormatStyle NoEmptyLines = getLLVMStyle(); 10551 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10552 verifyFormat("struct foo {\n" 10553 "private:\n" 10554 " void f() {}\n" 10555 "\n" 10556 "private:\n" 10557 " int i;\n" 10558 "\n" 10559 "public:\n" 10560 "protected:\n" 10561 " int j;\n" 10562 "};\n", 10563 NoEmptyLines); 10564 10565 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10566 verifyFormat("struct foo {\n" 10567 "private:\n" 10568 " void f() {}\n" 10569 "private:\n" 10570 " int i;\n" 10571 "public:\n" 10572 "protected:\n" 10573 " int j;\n" 10574 "};\n", 10575 NoEmptyLines); 10576 10577 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10578 verifyFormat("struct foo {\n" 10579 "private:\n" 10580 " void f() {}\n" 10581 "\n" 10582 "private:\n" 10583 " int i;\n" 10584 "\n" 10585 "public:\n" 10586 "\n" 10587 "protected:\n" 10588 " int j;\n" 10589 "};\n", 10590 NoEmptyLines); 10591 } 10592 10593 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10594 10595 FormatStyle Style = getLLVMStyle(); 10596 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10597 verifyFormat("struct foo {\n" 10598 "private:\n" 10599 " void f() {}\n" 10600 "\n" 10601 "private:\n" 10602 " int i;\n" 10603 "\n" 10604 "protected:\n" 10605 " int j;\n" 10606 "};\n", 10607 Style); 10608 10609 // Check if lines are removed. 10610 verifyFormat("struct foo {\n" 10611 "private:\n" 10612 " void f() {}\n" 10613 "\n" 10614 "private:\n" 10615 " int i;\n" 10616 "\n" 10617 "protected:\n" 10618 " int j;\n" 10619 "};\n", 10620 "struct foo {\n" 10621 "private:\n" 10622 "\n" 10623 " void f() {}\n" 10624 "\n" 10625 "private:\n" 10626 "\n" 10627 " int i;\n" 10628 "\n" 10629 "protected:\n" 10630 "\n" 10631 " int j;\n" 10632 "};\n", 10633 Style); 10634 10635 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10636 verifyFormat("struct foo {\n" 10637 "private:\n" 10638 "\n" 10639 " void f() {}\n" 10640 "\n" 10641 "private:\n" 10642 "\n" 10643 " int i;\n" 10644 "\n" 10645 "protected:\n" 10646 "\n" 10647 " int j;\n" 10648 "};\n", 10649 Style); 10650 10651 // Check if lines are added. 10652 verifyFormat("struct foo {\n" 10653 "private:\n" 10654 "\n" 10655 " void f() {}\n" 10656 "\n" 10657 "private:\n" 10658 "\n" 10659 " int i;\n" 10660 "\n" 10661 "protected:\n" 10662 "\n" 10663 " int j;\n" 10664 "};\n", 10665 "struct foo {\n" 10666 "private:\n" 10667 " void f() {}\n" 10668 "\n" 10669 "private:\n" 10670 " int i;\n" 10671 "\n" 10672 "protected:\n" 10673 " int j;\n" 10674 "};\n", 10675 Style); 10676 10677 // Leave tests rely on the code layout, test::messUp can not be used. 10678 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10679 Style.MaxEmptyLinesToKeep = 0u; 10680 verifyFormat("struct foo {\n" 10681 "private:\n" 10682 " void f() {}\n" 10683 "\n" 10684 "private:\n" 10685 " int i;\n" 10686 "\n" 10687 "protected:\n" 10688 " int j;\n" 10689 "};\n", 10690 Style); 10691 10692 // Check if MaxEmptyLinesToKeep is respected. 10693 EXPECT_EQ("struct foo {\n" 10694 "private:\n" 10695 " void f() {}\n" 10696 "\n" 10697 "private:\n" 10698 " int i;\n" 10699 "\n" 10700 "protected:\n" 10701 " int j;\n" 10702 "};\n", 10703 format("struct foo {\n" 10704 "private:\n" 10705 "\n\n\n" 10706 " void f() {}\n" 10707 "\n" 10708 "private:\n" 10709 "\n\n\n" 10710 " int i;\n" 10711 "\n" 10712 "protected:\n" 10713 "\n\n\n" 10714 " int j;\n" 10715 "};\n", 10716 Style)); 10717 10718 Style.MaxEmptyLinesToKeep = 1u; 10719 EXPECT_EQ("struct foo {\n" 10720 "private:\n" 10721 "\n" 10722 " void f() {}\n" 10723 "\n" 10724 "private:\n" 10725 "\n" 10726 " int i;\n" 10727 "\n" 10728 "protected:\n" 10729 "\n" 10730 " int j;\n" 10731 "};\n", 10732 format("struct foo {\n" 10733 "private:\n" 10734 "\n" 10735 " void f() {}\n" 10736 "\n" 10737 "private:\n" 10738 "\n" 10739 " int i;\n" 10740 "\n" 10741 "protected:\n" 10742 "\n" 10743 " int j;\n" 10744 "};\n", 10745 Style)); 10746 // Check if no lines are kept. 10747 EXPECT_EQ("struct foo {\n" 10748 "private:\n" 10749 " void f() {}\n" 10750 "\n" 10751 "private:\n" 10752 " int i;\n" 10753 "\n" 10754 "protected:\n" 10755 " int j;\n" 10756 "};\n", 10757 format("struct foo {\n" 10758 "private:\n" 10759 " void f() {}\n" 10760 "\n" 10761 "private:\n" 10762 " int i;\n" 10763 "\n" 10764 "protected:\n" 10765 " int j;\n" 10766 "};\n", 10767 Style)); 10768 // Check if MaxEmptyLinesToKeep is respected. 10769 EXPECT_EQ("struct foo {\n" 10770 "private:\n" 10771 "\n" 10772 " void f() {}\n" 10773 "\n" 10774 "private:\n" 10775 "\n" 10776 " int i;\n" 10777 "\n" 10778 "protected:\n" 10779 "\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 Style.MaxEmptyLinesToKeep = 10u; 10798 EXPECT_EQ("struct foo {\n" 10799 "private:\n" 10800 "\n\n\n" 10801 " void f() {}\n" 10802 "\n" 10803 "private:\n" 10804 "\n\n\n" 10805 " int i;\n" 10806 "\n" 10807 "protected:\n" 10808 "\n\n\n" 10809 " int j;\n" 10810 "};\n", 10811 format("struct foo {\n" 10812 "private:\n" 10813 "\n\n\n" 10814 " void f() {}\n" 10815 "\n" 10816 "private:\n" 10817 "\n\n\n" 10818 " int i;\n" 10819 "\n" 10820 "protected:\n" 10821 "\n\n\n" 10822 " int j;\n" 10823 "};\n", 10824 Style)); 10825 10826 // Test with comments. 10827 Style = getLLVMStyle(); 10828 verifyFormat("struct foo {\n" 10829 "private:\n" 10830 " // comment\n" 10831 " void f() {}\n" 10832 "\n" 10833 "private: /* comment */\n" 10834 " int i;\n" 10835 "};\n", 10836 Style); 10837 verifyFormat("struct foo {\n" 10838 "private:\n" 10839 " // comment\n" 10840 " void f() {}\n" 10841 "\n" 10842 "private: /* comment */\n" 10843 " int i;\n" 10844 "};\n", 10845 "struct foo {\n" 10846 "private:\n" 10847 "\n" 10848 " // comment\n" 10849 " void f() {}\n" 10850 "\n" 10851 "private: /* comment */\n" 10852 "\n" 10853 " int i;\n" 10854 "};\n", 10855 Style); 10856 10857 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10858 verifyFormat("struct foo {\n" 10859 "private:\n" 10860 "\n" 10861 " // comment\n" 10862 " void f() {}\n" 10863 "\n" 10864 "private: /* comment */\n" 10865 "\n" 10866 " int i;\n" 10867 "};\n", 10868 "struct foo {\n" 10869 "private:\n" 10870 " // comment\n" 10871 " void f() {}\n" 10872 "\n" 10873 "private: /* comment */\n" 10874 " int i;\n" 10875 "};\n", 10876 Style); 10877 verifyFormat("struct foo {\n" 10878 "private:\n" 10879 "\n" 10880 " // comment\n" 10881 " void f() {}\n" 10882 "\n" 10883 "private: /* comment */\n" 10884 "\n" 10885 " int i;\n" 10886 "};\n", 10887 Style); 10888 10889 // Test with preprocessor defines. 10890 Style = getLLVMStyle(); 10891 verifyFormat("struct foo {\n" 10892 "private:\n" 10893 "#ifdef FOO\n" 10894 "#endif\n" 10895 " void f() {}\n" 10896 "};\n", 10897 Style); 10898 verifyFormat("struct foo {\n" 10899 "private:\n" 10900 "#ifdef FOO\n" 10901 "#endif\n" 10902 " void f() {}\n" 10903 "};\n", 10904 "struct foo {\n" 10905 "private:\n" 10906 "\n" 10907 "#ifdef FOO\n" 10908 "#endif\n" 10909 " void f() {}\n" 10910 "};\n", 10911 Style); 10912 10913 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10914 verifyFormat("struct foo {\n" 10915 "private:\n" 10916 "\n" 10917 "#ifdef FOO\n" 10918 "#endif\n" 10919 " void f() {}\n" 10920 "};\n", 10921 "struct foo {\n" 10922 "private:\n" 10923 "#ifdef FOO\n" 10924 "#endif\n" 10925 " void f() {}\n" 10926 "};\n", 10927 Style); 10928 verifyFormat("struct foo {\n" 10929 "private:\n" 10930 "\n" 10931 "#ifdef FOO\n" 10932 "#endif\n" 10933 " void f() {}\n" 10934 "};\n", 10935 Style); 10936 } 10937 10938 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10939 // Combined tests of EmptyLineAfterAccessModifier and 10940 // EmptyLineBeforeAccessModifier. 10941 FormatStyle Style = getLLVMStyle(); 10942 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10943 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10944 verifyFormat("struct foo {\n" 10945 "private:\n" 10946 "\n" 10947 "protected:\n" 10948 "};\n", 10949 Style); 10950 10951 Style.MaxEmptyLinesToKeep = 10u; 10952 // Both remove all new lines. 10953 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10954 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10955 verifyFormat("struct foo {\n" 10956 "private:\n" 10957 "protected:\n" 10958 "};\n", 10959 "struct foo {\n" 10960 "private:\n" 10961 "\n\n\n" 10962 "protected:\n" 10963 "};\n", 10964 Style); 10965 10966 // Leave tests rely on the code layout, test::messUp can not be used. 10967 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10968 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10969 Style.MaxEmptyLinesToKeep = 10u; 10970 EXPECT_EQ("struct foo {\n" 10971 "private:\n" 10972 "\n\n\n" 10973 "protected:\n" 10974 "};\n", 10975 format("struct foo {\n" 10976 "private:\n" 10977 "\n\n\n" 10978 "protected:\n" 10979 "};\n", 10980 Style)); 10981 Style.MaxEmptyLinesToKeep = 3u; 10982 EXPECT_EQ("struct foo {\n" 10983 "private:\n" 10984 "\n\n\n" 10985 "protected:\n" 10986 "};\n", 10987 format("struct foo {\n" 10988 "private:\n" 10989 "\n\n\n" 10990 "protected:\n" 10991 "};\n", 10992 Style)); 10993 Style.MaxEmptyLinesToKeep = 1u; 10994 EXPECT_EQ("struct foo {\n" 10995 "private:\n" 10996 "\n\n\n" 10997 "protected:\n" 10998 "};\n", 10999 format("struct foo {\n" 11000 "private:\n" 11001 "\n\n\n" 11002 "protected:\n" 11003 "};\n", 11004 Style)); // Based on new lines in original document and not 11005 // on the setting. 11006 11007 Style.MaxEmptyLinesToKeep = 10u; 11008 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11009 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11010 // Newlines are kept if they are greater than zero, 11011 // test::messUp removes all new lines which changes the logic 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)); 11023 11024 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11025 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11026 // test::messUp removes all new lines which changes the logic 11027 EXPECT_EQ("struct foo {\n" 11028 "private:\n" 11029 "\n\n\n" 11030 "protected:\n" 11031 "};\n", 11032 format("struct foo {\n" 11033 "private:\n" 11034 "\n\n\n" 11035 "protected:\n" 11036 "};\n", 11037 Style)); 11038 11039 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11040 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11041 EXPECT_EQ("struct foo {\n" 11042 "private:\n" 11043 "\n\n\n" 11044 "protected:\n" 11045 "};\n", 11046 format("struct foo {\n" 11047 "private:\n" 11048 "\n\n\n" 11049 "protected:\n" 11050 "};\n", 11051 Style)); // test::messUp removes all new lines which changes 11052 // the logic. 11053 11054 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11055 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11056 verifyFormat("struct foo {\n" 11057 "private:\n" 11058 "protected:\n" 11059 "};\n", 11060 "struct foo {\n" 11061 "private:\n" 11062 "\n\n\n" 11063 "protected:\n" 11064 "};\n", 11065 Style); 11066 11067 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11068 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11069 EXPECT_EQ("struct foo {\n" 11070 "private:\n" 11071 "\n\n\n" 11072 "protected:\n" 11073 "};\n", 11074 format("struct foo {\n" 11075 "private:\n" 11076 "\n\n\n" 11077 "protected:\n" 11078 "};\n", 11079 Style)); // test::messUp removes all new lines which changes 11080 // the logic. 11081 11082 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11083 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11084 verifyFormat("struct foo {\n" 11085 "private:\n" 11086 "protected:\n" 11087 "};\n", 11088 "struct foo {\n" 11089 "private:\n" 11090 "\n\n\n" 11091 "protected:\n" 11092 "};\n", 11093 Style); 11094 11095 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11096 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11097 verifyFormat("struct foo {\n" 11098 "private:\n" 11099 "protected:\n" 11100 "};\n", 11101 "struct foo {\n" 11102 "private:\n" 11103 "\n\n\n" 11104 "protected:\n" 11105 "};\n", 11106 Style); 11107 11108 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11109 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11110 verifyFormat("struct foo {\n" 11111 "private:\n" 11112 "protected:\n" 11113 "};\n", 11114 "struct foo {\n" 11115 "private:\n" 11116 "\n\n\n" 11117 "protected:\n" 11118 "};\n", 11119 Style); 11120 11121 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11122 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11123 verifyFormat("struct foo {\n" 11124 "private:\n" 11125 "protected:\n" 11126 "};\n", 11127 "struct foo {\n" 11128 "private:\n" 11129 "\n\n\n" 11130 "protected:\n" 11131 "};\n", 11132 Style); 11133 } 11134 11135 TEST_F(FormatTest, FormatsArrays) { 11136 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11137 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11138 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11139 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11140 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11141 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11142 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11143 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11144 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11145 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11146 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11147 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11148 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11149 verifyFormat( 11150 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11151 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11152 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11153 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11154 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11155 11156 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11157 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11158 verifyFormat( 11159 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11160 " .aaaaaaa[0]\n" 11161 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11162 verifyFormat("a[::b::c];"); 11163 11164 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11165 11166 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11167 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11168 } 11169 11170 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11171 verifyFormat("(a)->b();"); 11172 verifyFormat("--a;"); 11173 } 11174 11175 TEST_F(FormatTest, HandlesIncludeDirectives) { 11176 verifyFormat("#include <string>\n" 11177 "#include <a/b/c.h>\n" 11178 "#include \"a/b/string\"\n" 11179 "#include \"string.h\"\n" 11180 "#include \"string.h\"\n" 11181 "#include <a-a>\n" 11182 "#include < path with space >\n" 11183 "#include_next <test.h>" 11184 "#include \"abc.h\" // this is included for ABC\n" 11185 "#include \"some long include\" // with a comment\n" 11186 "#include \"some very long include path\"\n" 11187 "#include <some/very/long/include/path>\n", 11188 getLLVMStyleWithColumns(35)); 11189 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11190 EXPECT_EQ("#include <a>", format("#include<a>")); 11191 11192 verifyFormat("#import <string>"); 11193 verifyFormat("#import <a/b/c.h>"); 11194 verifyFormat("#import \"a/b/string\""); 11195 verifyFormat("#import \"string.h\""); 11196 verifyFormat("#import \"string.h\""); 11197 verifyFormat("#if __has_include(<strstream>)\n" 11198 "#include <strstream>\n" 11199 "#endif"); 11200 11201 verifyFormat("#define MY_IMPORT <a/b>"); 11202 11203 verifyFormat("#if __has_include(<a/b>)"); 11204 verifyFormat("#if __has_include_next(<a/b>)"); 11205 verifyFormat("#define F __has_include(<a/b>)"); 11206 verifyFormat("#define F __has_include_next(<a/b>)"); 11207 11208 // Protocol buffer definition or missing "#". 11209 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11210 getLLVMStyleWithColumns(30)); 11211 11212 FormatStyle Style = getLLVMStyle(); 11213 Style.AlwaysBreakBeforeMultilineStrings = true; 11214 Style.ColumnLimit = 0; 11215 verifyFormat("#import \"abc.h\"", Style); 11216 11217 // But 'import' might also be a regular C++ namespace. 11218 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11220 } 11221 11222 //===----------------------------------------------------------------------===// 11223 // Error recovery tests. 11224 //===----------------------------------------------------------------------===// 11225 11226 TEST_F(FormatTest, IncompleteParameterLists) { 11227 FormatStyle NoBinPacking = getLLVMStyle(); 11228 NoBinPacking.BinPackParameters = false; 11229 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11230 " double *min_x,\n" 11231 " double *max_x,\n" 11232 " double *min_y,\n" 11233 " double *max_y,\n" 11234 " double *min_z,\n" 11235 " double *max_z, ) {}", 11236 NoBinPacking); 11237 } 11238 11239 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11240 verifyFormat("void f() { return; }\n42"); 11241 verifyFormat("void f() {\n" 11242 " if (0)\n" 11243 " return;\n" 11244 "}\n" 11245 "42"); 11246 verifyFormat("void f() { return }\n42"); 11247 verifyFormat("void f() {\n" 11248 " if (0)\n" 11249 " return\n" 11250 "}\n" 11251 "42"); 11252 } 11253 11254 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11255 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11256 EXPECT_EQ("void f() {\n" 11257 " if (a)\n" 11258 " return\n" 11259 "}", 11260 format("void f ( ) { if ( a ) return }")); 11261 EXPECT_EQ("namespace N {\n" 11262 "void f()\n" 11263 "}", 11264 format("namespace N { void f() }")); 11265 EXPECT_EQ("namespace N {\n" 11266 "void f() {}\n" 11267 "void g()\n" 11268 "} // namespace N", 11269 format("namespace N { void f( ) { } void g( ) }")); 11270 } 11271 11272 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11273 verifyFormat("int aaaaaaaa =\n" 11274 " // Overlylongcomment\n" 11275 " b;", 11276 getLLVMStyleWithColumns(20)); 11277 verifyFormat("function(\n" 11278 " ShortArgument,\n" 11279 " LoooooooooooongArgument);\n", 11280 getLLVMStyleWithColumns(20)); 11281 } 11282 11283 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11284 verifyFormat("public:"); 11285 verifyFormat("class A {\n" 11286 "public\n" 11287 " void f() {}\n" 11288 "};"); 11289 verifyFormat("public\n" 11290 "int qwerty;"); 11291 verifyFormat("public\n" 11292 "B {}"); 11293 verifyFormat("public\n" 11294 "{}"); 11295 verifyFormat("public\n" 11296 "B { int x; }"); 11297 } 11298 11299 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11300 verifyFormat("{"); 11301 verifyFormat("#})"); 11302 verifyNoCrash("(/**/[:!] ?[)."); 11303 } 11304 11305 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11306 // Found by oss-fuzz: 11307 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11308 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11309 Style.ColumnLimit = 60; 11310 verifyNoCrash( 11311 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11312 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11313 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11314 Style); 11315 } 11316 11317 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11318 verifyFormat("do {\n}"); 11319 verifyFormat("do {\n}\n" 11320 "f();"); 11321 verifyFormat("do {\n}\n" 11322 "wheeee(fun);"); 11323 verifyFormat("do {\n" 11324 " f();\n" 11325 "}"); 11326 } 11327 11328 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11329 verifyFormat("if {\n foo;\n foo();\n}"); 11330 verifyFormat("switch {\n foo;\n foo();\n}"); 11331 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11332 verifyFormat("while {\n foo;\n foo();\n}"); 11333 verifyFormat("do {\n foo;\n foo();\n} while;"); 11334 } 11335 11336 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11337 verifyIncompleteFormat("namespace {\n" 11338 "class Foo { Foo (\n" 11339 "};\n" 11340 "} // namespace"); 11341 } 11342 11343 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11344 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11345 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11346 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11347 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11348 11349 EXPECT_EQ("{\n" 11350 " {\n" 11351 " breakme(\n" 11352 " qwe);\n" 11353 " }\n", 11354 format("{\n" 11355 " {\n" 11356 " breakme(qwe);\n" 11357 "}\n", 11358 getLLVMStyleWithColumns(10))); 11359 } 11360 11361 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11362 verifyFormat("int x = {\n" 11363 " avariable,\n" 11364 " b(alongervariable)};", 11365 getLLVMStyleWithColumns(25)); 11366 } 11367 11368 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11369 verifyFormat("return (a)(b){1, 2, 3};"); 11370 } 11371 11372 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11373 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11374 verifyFormat("vector<int> x{\n" 11375 " 1,\n" 11376 " 2,\n" 11377 " 3,\n" 11378 " 4,\n" 11379 "};"); 11380 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11381 verifyFormat("f({1, 2});"); 11382 verifyFormat("auto v = Foo{-1};"); 11383 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11384 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11385 verifyFormat("new vector<int>{1, 2, 3};"); 11386 verifyFormat("new int[3]{1, 2, 3};"); 11387 verifyFormat("new int{1};"); 11388 verifyFormat("return {arg1, arg2};"); 11389 verifyFormat("return {arg1, SomeType{parameter}};"); 11390 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11391 verifyFormat("new T{arg1, arg2};"); 11392 verifyFormat("f(MyMap[{composite, key}]);"); 11393 verifyFormat("class Class {\n" 11394 " T member = {arg1, arg2};\n" 11395 "};"); 11396 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11397 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11398 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11399 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11400 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11401 11402 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11403 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11404 verifyFormat("auto i = decltype(x){};"); 11405 verifyFormat("auto i = typeof(x){};"); 11406 verifyFormat("auto i = _Atomic(x){};"); 11407 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11408 verifyFormat("Node n{1, Node{1000}, //\n" 11409 " 2};"); 11410 verifyFormat("Aaaa aaaaaaa{\n" 11411 " {\n" 11412 " aaaa,\n" 11413 " },\n" 11414 "};"); 11415 verifyFormat("class C : public D {\n" 11416 " SomeClass SC{2};\n" 11417 "};"); 11418 verifyFormat("class C : public A {\n" 11419 " class D : public B {\n" 11420 " void f() { int i{2}; }\n" 11421 " };\n" 11422 "};"); 11423 verifyFormat("#define A {a, a},"); 11424 11425 // Avoid breaking between equal sign and opening brace 11426 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11427 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11428 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11429 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11430 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11431 " {\"ccccccccccccccccccccc\", 2}};", 11432 AvoidBreakingFirstArgument); 11433 11434 // Binpacking only if there is no trailing comma 11435 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11436 " cccccccccc, dddddddddd};", 11437 getLLVMStyleWithColumns(50)); 11438 verifyFormat("const Aaaaaa aaaaa = {\n" 11439 " aaaaaaaaaaa,\n" 11440 " bbbbbbbbbbb,\n" 11441 " ccccccccccc,\n" 11442 " ddddddddddd,\n" 11443 "};", 11444 getLLVMStyleWithColumns(50)); 11445 11446 // Cases where distinguising braced lists and blocks is hard. 11447 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11448 verifyFormat("void f() {\n" 11449 " return; // comment\n" 11450 "}\n" 11451 "SomeType t;"); 11452 verifyFormat("void f() {\n" 11453 " if (a) {\n" 11454 " f();\n" 11455 " }\n" 11456 "}\n" 11457 "SomeType t;"); 11458 11459 // In combination with BinPackArguments = false. 11460 FormatStyle NoBinPacking = getLLVMStyle(); 11461 NoBinPacking.BinPackArguments = false; 11462 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11463 " bbbbb,\n" 11464 " ccccc,\n" 11465 " ddddd,\n" 11466 " eeeee,\n" 11467 " ffffff,\n" 11468 " ggggg,\n" 11469 " hhhhhh,\n" 11470 " iiiiii,\n" 11471 " jjjjjj,\n" 11472 " kkkkkk};", 11473 NoBinPacking); 11474 verifyFormat("const Aaaaaa aaaaa = {\n" 11475 " aaaaa,\n" 11476 " bbbbb,\n" 11477 " ccccc,\n" 11478 " ddddd,\n" 11479 " eeeee,\n" 11480 " ffffff,\n" 11481 " ggggg,\n" 11482 " hhhhhh,\n" 11483 " iiiiii,\n" 11484 " jjjjjj,\n" 11485 " kkkkkk,\n" 11486 "};", 11487 NoBinPacking); 11488 verifyFormat( 11489 "const Aaaaaa aaaaa = {\n" 11490 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11491 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11492 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11493 "};", 11494 NoBinPacking); 11495 11496 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11497 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11498 " CDDDP83848_BMCR_REGISTER,\n" 11499 " CDDDP83848_BMSR_REGISTER,\n" 11500 " CDDDP83848_RBR_REGISTER};", 11501 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11502 " CDDDP83848_BMSR_REGISTER,\n" 11503 " CDDDP83848_RBR_REGISTER};", 11504 NoBinPacking)); 11505 11506 // FIXME: The alignment of these trailing comments might be bad. Then again, 11507 // this might be utterly useless in real code. 11508 verifyFormat("Constructor::Constructor()\n" 11509 " : some_value{ //\n" 11510 " aaaaaaa, //\n" 11511 " bbbbbbb} {}"); 11512 11513 // In braced lists, the first comment is always assumed to belong to the 11514 // first element. Thus, it can be moved to the next or previous line as 11515 // appropriate. 11516 EXPECT_EQ("function({// First element:\n" 11517 " 1,\n" 11518 " // Second element:\n" 11519 " 2});", 11520 format("function({\n" 11521 " // First element:\n" 11522 " 1,\n" 11523 " // Second element:\n" 11524 " 2});")); 11525 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11526 " // First element:\n" 11527 " 1,\n" 11528 " // Second element:\n" 11529 " 2};", 11530 format("std::vector<int> MyNumbers{// First element:\n" 11531 " 1,\n" 11532 " // Second element:\n" 11533 " 2};", 11534 getLLVMStyleWithColumns(30))); 11535 // A trailing comma should still lead to an enforced line break and no 11536 // binpacking. 11537 EXPECT_EQ("vector<int> SomeVector = {\n" 11538 " // aaa\n" 11539 " 1,\n" 11540 " 2,\n" 11541 "};", 11542 format("vector<int> SomeVector = { // aaa\n" 11543 " 1, 2, };")); 11544 11545 // C++11 brace initializer list l-braces should not be treated any differently 11546 // when breaking before lambda bodies is enabled 11547 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11548 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11549 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11550 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11551 verifyFormat( 11552 "std::runtime_error{\n" 11553 " \"Long string which will force a break onto the next line...\"};", 11554 BreakBeforeLambdaBody); 11555 11556 FormatStyle ExtraSpaces = getLLVMStyle(); 11557 ExtraSpaces.Cpp11BracedListStyle = false; 11558 ExtraSpaces.ColumnLimit = 75; 11559 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11560 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11561 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11562 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11563 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11564 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11565 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11566 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11567 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11568 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11569 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11570 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11571 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11572 verifyFormat("class Class {\n" 11573 " T member = { arg1, arg2 };\n" 11574 "};", 11575 ExtraSpaces); 11576 verifyFormat( 11577 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11578 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11579 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11580 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11581 ExtraSpaces); 11582 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11583 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11584 ExtraSpaces); 11585 verifyFormat( 11586 "someFunction(OtherParam,\n" 11587 " BracedList{ // comment 1 (Forcing interesting break)\n" 11588 " param1, param2,\n" 11589 " // comment 2\n" 11590 " param3, param4 });", 11591 ExtraSpaces); 11592 verifyFormat( 11593 "std::this_thread::sleep_for(\n" 11594 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11595 ExtraSpaces); 11596 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11597 " aaaaaaa,\n" 11598 " aaaaaaaaaa,\n" 11599 " aaaaa,\n" 11600 " aaaaaaaaaaaaaaa,\n" 11601 " aaa,\n" 11602 " aaaaaaaaaa,\n" 11603 " a,\n" 11604 " aaaaaaaaaaaaaaaaaaaaa,\n" 11605 " aaaaaaaaaaaa,\n" 11606 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11607 " aaaaaaa,\n" 11608 " a};"); 11609 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11610 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11611 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11612 11613 // Avoid breaking between initializer/equal sign and opening brace 11614 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11615 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11616 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11617 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11618 " { \"ccccccccccccccccccccc\", 2 }\n" 11619 "};", 11620 ExtraSpaces); 11621 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11622 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11623 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11624 " { \"ccccccccccccccccccccc\", 2 }\n" 11625 "};", 11626 ExtraSpaces); 11627 11628 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11629 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11630 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11631 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11632 11633 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11634 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11635 SpaceBetweenBraces.SpacesInParentheses = true; 11636 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11637 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11638 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11639 verifyFormat("vector< int > x{ // comment 1\n" 11640 " 1, 2, 3, 4 };", 11641 SpaceBetweenBraces); 11642 SpaceBetweenBraces.ColumnLimit = 20; 11643 EXPECT_EQ("vector< int > x{\n" 11644 " 1, 2, 3, 4 };", 11645 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11646 SpaceBetweenBraces.ColumnLimit = 24; 11647 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11648 " 3, 4 };", 11649 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11650 EXPECT_EQ("vector< int > x{\n" 11651 " 1,\n" 11652 " 2,\n" 11653 " 3,\n" 11654 " 4,\n" 11655 "};", 11656 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11657 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11658 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11659 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11660 } 11661 11662 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11663 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11664 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11665 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11666 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11667 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11668 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11669 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11670 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11671 " 1, 22, 333, 4444, 55555, //\n" 11672 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11673 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11674 verifyFormat( 11675 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11676 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11677 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11678 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11679 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11680 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11681 " 7777777};"); 11682 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11683 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11684 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11685 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11686 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11687 " // Separating comment.\n" 11688 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11689 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11690 " // Leading comment\n" 11691 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11692 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11693 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11694 " 1, 1, 1, 1};", 11695 getLLVMStyleWithColumns(39)); 11696 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11697 " 1, 1, 1, 1};", 11698 getLLVMStyleWithColumns(38)); 11699 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11700 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11701 getLLVMStyleWithColumns(43)); 11702 verifyFormat( 11703 "static unsigned SomeValues[10][3] = {\n" 11704 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11705 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11706 verifyFormat("static auto fields = new vector<string>{\n" 11707 " \"aaaaaaaaaaaaa\",\n" 11708 " \"aaaaaaaaaaaaa\",\n" 11709 " \"aaaaaaaaaaaa\",\n" 11710 " \"aaaaaaaaaaaaaa\",\n" 11711 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11712 " \"aaaaaaaaaaaa\",\n" 11713 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11714 "};"); 11715 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11716 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11717 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11718 " 3, cccccccccccccccccccccc};", 11719 getLLVMStyleWithColumns(60)); 11720 11721 // Trailing commas. 11722 verifyFormat("vector<int> x = {\n" 11723 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11724 "};", 11725 getLLVMStyleWithColumns(39)); 11726 verifyFormat("vector<int> x = {\n" 11727 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11728 "};", 11729 getLLVMStyleWithColumns(39)); 11730 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11731 " 1, 1, 1, 1,\n" 11732 " /**/ /**/};", 11733 getLLVMStyleWithColumns(39)); 11734 11735 // Trailing comment in the first line. 11736 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11737 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11738 " 111111111, 222222222, 3333333333, 444444444, //\n" 11739 " 11111111, 22222222, 333333333, 44444444};"); 11740 // Trailing comment in the last line. 11741 verifyFormat("int aaaaa[] = {\n" 11742 " 1, 2, 3, // comment\n" 11743 " 4, 5, 6 // comment\n" 11744 "};"); 11745 11746 // With nested lists, we should either format one item per line or all nested 11747 // lists one on line. 11748 // FIXME: For some nested lists, we can do better. 11749 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11750 " {aaaaaaaaaaaaaaaaaaa},\n" 11751 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11752 " {aaaaaaaaaaaaaaaaa}};", 11753 getLLVMStyleWithColumns(60)); 11754 verifyFormat( 11755 "SomeStruct my_struct_array = {\n" 11756 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11757 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11758 " {aaa, aaa},\n" 11759 " {aaa, aaa},\n" 11760 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11761 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11762 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11763 11764 // No column layout should be used here. 11765 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11766 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11767 11768 verifyNoCrash("a<,"); 11769 11770 // No braced initializer here. 11771 verifyFormat("void f() {\n" 11772 " struct Dummy {};\n" 11773 " f(v);\n" 11774 "}"); 11775 11776 // Long lists should be formatted in columns even if they are nested. 11777 verifyFormat( 11778 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11779 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11780 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11781 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11782 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11783 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11784 11785 // Allow "single-column" layout even if that violates the column limit. There 11786 // isn't going to be a better way. 11787 verifyFormat("std::vector<int> a = {\n" 11788 " aaaaaaaa,\n" 11789 " aaaaaaaa,\n" 11790 " aaaaaaaa,\n" 11791 " aaaaaaaa,\n" 11792 " aaaaaaaaaa,\n" 11793 " aaaaaaaa,\n" 11794 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11795 getLLVMStyleWithColumns(30)); 11796 verifyFormat("vector<int> aaaa = {\n" 11797 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11798 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11799 " aaaaaa.aaaaaaa,\n" 11800 " aaaaaa.aaaaaaa,\n" 11801 " aaaaaa.aaaaaaa,\n" 11802 " aaaaaa.aaaaaaa,\n" 11803 "};"); 11804 11805 // Don't create hanging lists. 11806 verifyFormat("someFunction(Param, {List1, List2,\n" 11807 " List3});", 11808 getLLVMStyleWithColumns(35)); 11809 verifyFormat("someFunction(Param, Param,\n" 11810 " {List1, List2,\n" 11811 " List3});", 11812 getLLVMStyleWithColumns(35)); 11813 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11814 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11815 } 11816 11817 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11818 FormatStyle DoNotMerge = getLLVMStyle(); 11819 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11820 11821 verifyFormat("void f() { return 42; }"); 11822 verifyFormat("void f() {\n" 11823 " return 42;\n" 11824 "}", 11825 DoNotMerge); 11826 verifyFormat("void f() {\n" 11827 " // Comment\n" 11828 "}"); 11829 verifyFormat("{\n" 11830 "#error {\n" 11831 " int a;\n" 11832 "}"); 11833 verifyFormat("{\n" 11834 " int a;\n" 11835 "#error {\n" 11836 "}"); 11837 verifyFormat("void f() {} // comment"); 11838 verifyFormat("void f() { int a; } // comment"); 11839 verifyFormat("void f() {\n" 11840 "} // comment", 11841 DoNotMerge); 11842 verifyFormat("void f() {\n" 11843 " int a;\n" 11844 "} // comment", 11845 DoNotMerge); 11846 verifyFormat("void f() {\n" 11847 "} // comment", 11848 getLLVMStyleWithColumns(15)); 11849 11850 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11851 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11852 11853 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11854 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11855 verifyFormat("class C {\n" 11856 " C()\n" 11857 " : iiiiiiii(nullptr),\n" 11858 " kkkkkkk(nullptr),\n" 11859 " mmmmmmm(nullptr),\n" 11860 " nnnnnnn(nullptr) {}\n" 11861 "};", 11862 getGoogleStyle()); 11863 11864 FormatStyle NoColumnLimit = getLLVMStyle(); 11865 NoColumnLimit.ColumnLimit = 0; 11866 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 11867 EXPECT_EQ("class C {\n" 11868 " A() : b(0) {}\n" 11869 "};", 11870 format("class C{A():b(0){}};", NoColumnLimit)); 11871 EXPECT_EQ("A()\n" 11872 " : b(0) {\n" 11873 "}", 11874 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 11875 11876 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 11877 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 11878 FormatStyle::SFS_None; 11879 EXPECT_EQ("A()\n" 11880 " : b(0) {\n" 11881 "}", 11882 format("A():b(0){}", DoNotMergeNoColumnLimit)); 11883 EXPECT_EQ("A()\n" 11884 " : b(0) {\n" 11885 "}", 11886 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 11887 11888 verifyFormat("#define A \\\n" 11889 " void f() { \\\n" 11890 " int i; \\\n" 11891 " }", 11892 getLLVMStyleWithColumns(20)); 11893 verifyFormat("#define A \\\n" 11894 " void f() { int i; }", 11895 getLLVMStyleWithColumns(21)); 11896 verifyFormat("#define A \\\n" 11897 " void f() { \\\n" 11898 " int i; \\\n" 11899 " } \\\n" 11900 " int j;", 11901 getLLVMStyleWithColumns(22)); 11902 verifyFormat("#define A \\\n" 11903 " void f() { int i; } \\\n" 11904 " int j;", 11905 getLLVMStyleWithColumns(23)); 11906 } 11907 11908 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 11909 FormatStyle MergeEmptyOnly = getLLVMStyle(); 11910 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11911 verifyFormat("class C {\n" 11912 " int f() {}\n" 11913 "};", 11914 MergeEmptyOnly); 11915 verifyFormat("class C {\n" 11916 " int f() {\n" 11917 " return 42;\n" 11918 " }\n" 11919 "};", 11920 MergeEmptyOnly); 11921 verifyFormat("int f() {}", MergeEmptyOnly); 11922 verifyFormat("int f() {\n" 11923 " return 42;\n" 11924 "}", 11925 MergeEmptyOnly); 11926 11927 // Also verify behavior when BraceWrapping.AfterFunction = true 11928 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11929 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11930 verifyFormat("int f() {}", MergeEmptyOnly); 11931 verifyFormat("class C {\n" 11932 " int f() {}\n" 11933 "};", 11934 MergeEmptyOnly); 11935 } 11936 11937 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11938 FormatStyle MergeInlineOnly = getLLVMStyle(); 11939 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11940 verifyFormat("class C {\n" 11941 " int f() { return 42; }\n" 11942 "};", 11943 MergeInlineOnly); 11944 verifyFormat("int f() {\n" 11945 " return 42;\n" 11946 "}", 11947 MergeInlineOnly); 11948 11949 // SFS_Inline implies SFS_Empty 11950 verifyFormat("class C {\n" 11951 " int f() {}\n" 11952 "};", 11953 MergeInlineOnly); 11954 verifyFormat("int f() {}", MergeInlineOnly); 11955 11956 // Also verify behavior when BraceWrapping.AfterFunction = true 11957 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11958 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11959 verifyFormat("class C {\n" 11960 " int f() { return 42; }\n" 11961 "};", 11962 MergeInlineOnly); 11963 verifyFormat("int f()\n" 11964 "{\n" 11965 " return 42;\n" 11966 "}", 11967 MergeInlineOnly); 11968 11969 // SFS_Inline implies SFS_Empty 11970 verifyFormat("int f() {}", MergeInlineOnly); 11971 verifyFormat("class C {\n" 11972 " int f() {}\n" 11973 "};", 11974 MergeInlineOnly); 11975 } 11976 11977 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11978 FormatStyle MergeInlineOnly = getLLVMStyle(); 11979 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11980 FormatStyle::SFS_InlineOnly; 11981 verifyFormat("class C {\n" 11982 " int f() { return 42; }\n" 11983 "};", 11984 MergeInlineOnly); 11985 verifyFormat("int f() {\n" 11986 " return 42;\n" 11987 "}", 11988 MergeInlineOnly); 11989 11990 // SFS_InlineOnly does not imply SFS_Empty 11991 verifyFormat("class C {\n" 11992 " int f() {}\n" 11993 "};", 11994 MergeInlineOnly); 11995 verifyFormat("int f() {\n" 11996 "}", 11997 MergeInlineOnly); 11998 11999 // Also verify behavior when BraceWrapping.AfterFunction = true 12000 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12001 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12002 verifyFormat("class C {\n" 12003 " int f() { return 42; }\n" 12004 "};", 12005 MergeInlineOnly); 12006 verifyFormat("int f()\n" 12007 "{\n" 12008 " return 42;\n" 12009 "}", 12010 MergeInlineOnly); 12011 12012 // SFS_InlineOnly does not imply SFS_Empty 12013 verifyFormat("int f()\n" 12014 "{\n" 12015 "}", 12016 MergeInlineOnly); 12017 verifyFormat("class C {\n" 12018 " int f() {}\n" 12019 "};", 12020 MergeInlineOnly); 12021 } 12022 12023 TEST_F(FormatTest, SplitEmptyFunction) { 12024 FormatStyle Style = getLLVMStyle(); 12025 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12026 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12027 Style.BraceWrapping.AfterFunction = true; 12028 Style.BraceWrapping.SplitEmptyFunction = false; 12029 Style.ColumnLimit = 40; 12030 12031 verifyFormat("int f()\n" 12032 "{}", 12033 Style); 12034 verifyFormat("int f()\n" 12035 "{\n" 12036 " return 42;\n" 12037 "}", 12038 Style); 12039 verifyFormat("int f()\n" 12040 "{\n" 12041 " // some comment\n" 12042 "}", 12043 Style); 12044 12045 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12046 verifyFormat("int f() {}", Style); 12047 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12048 "{}", 12049 Style); 12050 verifyFormat("int f()\n" 12051 "{\n" 12052 " return 0;\n" 12053 "}", 12054 Style); 12055 12056 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12057 verifyFormat("class Foo {\n" 12058 " int f() {}\n" 12059 "};\n", 12060 Style); 12061 verifyFormat("class Foo {\n" 12062 " int f() { return 0; }\n" 12063 "};\n", 12064 Style); 12065 verifyFormat("class Foo {\n" 12066 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12067 " {}\n" 12068 "};\n", 12069 Style); 12070 verifyFormat("class Foo {\n" 12071 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12072 " {\n" 12073 " return 0;\n" 12074 " }\n" 12075 "};\n", 12076 Style); 12077 12078 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12079 verifyFormat("int f() {}", Style); 12080 verifyFormat("int f() { return 0; }", Style); 12081 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12082 "{}", 12083 Style); 12084 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12085 "{\n" 12086 " return 0;\n" 12087 "}", 12088 Style); 12089 } 12090 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12091 FormatStyle Style = getLLVMStyle(); 12092 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12093 verifyFormat("#ifdef A\n" 12094 "int f() {}\n" 12095 "#else\n" 12096 "int g() {}\n" 12097 "#endif", 12098 Style); 12099 } 12100 12101 TEST_F(FormatTest, SplitEmptyClass) { 12102 FormatStyle Style = getLLVMStyle(); 12103 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12104 Style.BraceWrapping.AfterClass = true; 12105 Style.BraceWrapping.SplitEmptyRecord = false; 12106 12107 verifyFormat("class Foo\n" 12108 "{};", 12109 Style); 12110 verifyFormat("/* something */ class Foo\n" 12111 "{};", 12112 Style); 12113 verifyFormat("template <typename X> class Foo\n" 12114 "{};", 12115 Style); 12116 verifyFormat("class Foo\n" 12117 "{\n" 12118 " Foo();\n" 12119 "};", 12120 Style); 12121 verifyFormat("typedef class Foo\n" 12122 "{\n" 12123 "} Foo_t;", 12124 Style); 12125 12126 Style.BraceWrapping.SplitEmptyRecord = true; 12127 Style.BraceWrapping.AfterStruct = true; 12128 verifyFormat("class rep\n" 12129 "{\n" 12130 "};", 12131 Style); 12132 verifyFormat("struct rep\n" 12133 "{\n" 12134 "};", 12135 Style); 12136 verifyFormat("template <typename T> class rep\n" 12137 "{\n" 12138 "};", 12139 Style); 12140 verifyFormat("template <typename T> struct rep\n" 12141 "{\n" 12142 "};", 12143 Style); 12144 verifyFormat("class rep\n" 12145 "{\n" 12146 " int x;\n" 12147 "};", 12148 Style); 12149 verifyFormat("struct rep\n" 12150 "{\n" 12151 " int x;\n" 12152 "};", 12153 Style); 12154 verifyFormat("template <typename T> class rep\n" 12155 "{\n" 12156 " int x;\n" 12157 "};", 12158 Style); 12159 verifyFormat("template <typename T> struct rep\n" 12160 "{\n" 12161 " int x;\n" 12162 "};", 12163 Style); 12164 verifyFormat("template <typename T> class rep // Foo\n" 12165 "{\n" 12166 " int x;\n" 12167 "};", 12168 Style); 12169 verifyFormat("template <typename T> struct rep // Bar\n" 12170 "{\n" 12171 " int x;\n" 12172 "};", 12173 Style); 12174 12175 verifyFormat("template <typename T> class rep<T>\n" 12176 "{\n" 12177 " int x;\n" 12178 "};", 12179 Style); 12180 12181 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12182 "{\n" 12183 " int x;\n" 12184 "};", 12185 Style); 12186 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12187 "{\n" 12188 "};", 12189 Style); 12190 12191 verifyFormat("#include \"stdint.h\"\n" 12192 "namespace rep {}", 12193 Style); 12194 verifyFormat("#include <stdint.h>\n" 12195 "namespace rep {}", 12196 Style); 12197 verifyFormat("#include <stdint.h>\n" 12198 "namespace rep {}", 12199 "#include <stdint.h>\n" 12200 "namespace rep {\n" 12201 "\n" 12202 "\n" 12203 "}", 12204 Style); 12205 } 12206 12207 TEST_F(FormatTest, SplitEmptyStruct) { 12208 FormatStyle Style = getLLVMStyle(); 12209 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12210 Style.BraceWrapping.AfterStruct = true; 12211 Style.BraceWrapping.SplitEmptyRecord = false; 12212 12213 verifyFormat("struct Foo\n" 12214 "{};", 12215 Style); 12216 verifyFormat("/* something */ struct Foo\n" 12217 "{};", 12218 Style); 12219 verifyFormat("template <typename X> struct Foo\n" 12220 "{};", 12221 Style); 12222 verifyFormat("struct Foo\n" 12223 "{\n" 12224 " Foo();\n" 12225 "};", 12226 Style); 12227 verifyFormat("typedef struct Foo\n" 12228 "{\n" 12229 "} Foo_t;", 12230 Style); 12231 // typedef struct Bar {} Bar_t; 12232 } 12233 12234 TEST_F(FormatTest, SplitEmptyUnion) { 12235 FormatStyle Style = getLLVMStyle(); 12236 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12237 Style.BraceWrapping.AfterUnion = true; 12238 Style.BraceWrapping.SplitEmptyRecord = false; 12239 12240 verifyFormat("union Foo\n" 12241 "{};", 12242 Style); 12243 verifyFormat("/* something */ union Foo\n" 12244 "{};", 12245 Style); 12246 verifyFormat("union Foo\n" 12247 "{\n" 12248 " A,\n" 12249 "};", 12250 Style); 12251 verifyFormat("typedef union Foo\n" 12252 "{\n" 12253 "} Foo_t;", 12254 Style); 12255 } 12256 12257 TEST_F(FormatTest, SplitEmptyNamespace) { 12258 FormatStyle Style = getLLVMStyle(); 12259 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12260 Style.BraceWrapping.AfterNamespace = true; 12261 Style.BraceWrapping.SplitEmptyNamespace = false; 12262 12263 verifyFormat("namespace Foo\n" 12264 "{};", 12265 Style); 12266 verifyFormat("/* something */ namespace Foo\n" 12267 "{};", 12268 Style); 12269 verifyFormat("inline namespace Foo\n" 12270 "{};", 12271 Style); 12272 verifyFormat("/* something */ inline namespace Foo\n" 12273 "{};", 12274 Style); 12275 verifyFormat("export namespace Foo\n" 12276 "{};", 12277 Style); 12278 verifyFormat("namespace Foo\n" 12279 "{\n" 12280 "void Bar();\n" 12281 "};", 12282 Style); 12283 } 12284 12285 TEST_F(FormatTest, NeverMergeShortRecords) { 12286 FormatStyle Style = getLLVMStyle(); 12287 12288 verifyFormat("class Foo {\n" 12289 " Foo();\n" 12290 "};", 12291 Style); 12292 verifyFormat("typedef class Foo {\n" 12293 " Foo();\n" 12294 "} Foo_t;", 12295 Style); 12296 verifyFormat("struct Foo {\n" 12297 " Foo();\n" 12298 "};", 12299 Style); 12300 verifyFormat("typedef struct Foo {\n" 12301 " Foo();\n" 12302 "} Foo_t;", 12303 Style); 12304 verifyFormat("union Foo {\n" 12305 " A,\n" 12306 "};", 12307 Style); 12308 verifyFormat("typedef union Foo {\n" 12309 " A,\n" 12310 "} Foo_t;", 12311 Style); 12312 verifyFormat("namespace Foo {\n" 12313 "void Bar();\n" 12314 "};", 12315 Style); 12316 12317 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12318 Style.BraceWrapping.AfterClass = true; 12319 Style.BraceWrapping.AfterStruct = true; 12320 Style.BraceWrapping.AfterUnion = true; 12321 Style.BraceWrapping.AfterNamespace = true; 12322 verifyFormat("class Foo\n" 12323 "{\n" 12324 " Foo();\n" 12325 "};", 12326 Style); 12327 verifyFormat("typedef class Foo\n" 12328 "{\n" 12329 " Foo();\n" 12330 "} Foo_t;", 12331 Style); 12332 verifyFormat("struct Foo\n" 12333 "{\n" 12334 " Foo();\n" 12335 "};", 12336 Style); 12337 verifyFormat("typedef struct Foo\n" 12338 "{\n" 12339 " Foo();\n" 12340 "} Foo_t;", 12341 Style); 12342 verifyFormat("union Foo\n" 12343 "{\n" 12344 " A,\n" 12345 "};", 12346 Style); 12347 verifyFormat("typedef union Foo\n" 12348 "{\n" 12349 " A,\n" 12350 "} Foo_t;", 12351 Style); 12352 verifyFormat("namespace Foo\n" 12353 "{\n" 12354 "void Bar();\n" 12355 "};", 12356 Style); 12357 } 12358 12359 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12360 // Elaborate type variable declarations. 12361 verifyFormat("struct foo a = {bar};\nint n;"); 12362 verifyFormat("class foo a = {bar};\nint n;"); 12363 verifyFormat("union foo a = {bar};\nint n;"); 12364 12365 // Elaborate types inside function definitions. 12366 verifyFormat("struct foo f() {}\nint n;"); 12367 verifyFormat("class foo f() {}\nint n;"); 12368 verifyFormat("union foo f() {}\nint n;"); 12369 12370 // Templates. 12371 verifyFormat("template <class X> void f() {}\nint n;"); 12372 verifyFormat("template <struct X> void f() {}\nint n;"); 12373 verifyFormat("template <union X> void f() {}\nint n;"); 12374 12375 // Actual definitions... 12376 verifyFormat("struct {\n} n;"); 12377 verifyFormat( 12378 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12379 verifyFormat("union Z {\n int n;\n} x;"); 12380 verifyFormat("class MACRO Z {\n} n;"); 12381 verifyFormat("class MACRO(X) Z {\n} n;"); 12382 verifyFormat("class __attribute__(X) Z {\n} n;"); 12383 verifyFormat("class __declspec(X) Z {\n} n;"); 12384 verifyFormat("class A##B##C {\n} n;"); 12385 verifyFormat("class alignas(16) Z {\n} n;"); 12386 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12387 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12388 12389 // Redefinition from nested context: 12390 verifyFormat("class A::B::C {\n} n;"); 12391 12392 // Template definitions. 12393 verifyFormat( 12394 "template <typename F>\n" 12395 "Matcher(const Matcher<F> &Other,\n" 12396 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12397 " !is_same<F, T>::value>::type * = 0)\n" 12398 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12399 12400 // FIXME: This is still incorrectly handled at the formatter side. 12401 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12402 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12403 12404 // FIXME: 12405 // This now gets parsed incorrectly as class definition. 12406 // verifyFormat("class A<int> f() {\n}\nint n;"); 12407 12408 // Elaborate types where incorrectly parsing the structural element would 12409 // break the indent. 12410 verifyFormat("if (true)\n" 12411 " class X x;\n" 12412 "else\n" 12413 " f();\n"); 12414 12415 // This is simply incomplete. Formatting is not important, but must not crash. 12416 verifyFormat("class A:"); 12417 } 12418 12419 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12420 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12421 format("#error Leave all white!!!!! space* alone!\n")); 12422 EXPECT_EQ( 12423 "#warning Leave all white!!!!! space* alone!\n", 12424 format("#warning Leave all white!!!!! space* alone!\n")); 12425 EXPECT_EQ("#error 1", format(" # error 1")); 12426 EXPECT_EQ("#warning 1", format(" # warning 1")); 12427 } 12428 12429 TEST_F(FormatTest, FormatHashIfExpressions) { 12430 verifyFormat("#if AAAA && BBBB"); 12431 verifyFormat("#if (AAAA && BBBB)"); 12432 verifyFormat("#elif (AAAA && BBBB)"); 12433 // FIXME: Come up with a better indentation for #elif. 12434 verifyFormat( 12435 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12436 " defined(BBBBBBBB)\n" 12437 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12438 " defined(BBBBBBBB)\n" 12439 "#endif", 12440 getLLVMStyleWithColumns(65)); 12441 } 12442 12443 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12444 FormatStyle AllowsMergedIf = getGoogleStyle(); 12445 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12446 FormatStyle::SIS_WithoutElse; 12447 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12448 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12449 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12450 EXPECT_EQ("if (true) return 42;", 12451 format("if (true)\nreturn 42;", AllowsMergedIf)); 12452 FormatStyle ShortMergedIf = AllowsMergedIf; 12453 ShortMergedIf.ColumnLimit = 25; 12454 verifyFormat("#define A \\\n" 12455 " if (true) return 42;", 12456 ShortMergedIf); 12457 verifyFormat("#define A \\\n" 12458 " f(); \\\n" 12459 " if (true)\n" 12460 "#define B", 12461 ShortMergedIf); 12462 verifyFormat("#define A \\\n" 12463 " f(); \\\n" 12464 " if (true)\n" 12465 "g();", 12466 ShortMergedIf); 12467 verifyFormat("{\n" 12468 "#ifdef A\n" 12469 " // Comment\n" 12470 " if (true) continue;\n" 12471 "#endif\n" 12472 " // Comment\n" 12473 " if (true) continue;\n" 12474 "}", 12475 ShortMergedIf); 12476 ShortMergedIf.ColumnLimit = 33; 12477 verifyFormat("#define A \\\n" 12478 " if constexpr (true) return 42;", 12479 ShortMergedIf); 12480 verifyFormat("#define A \\\n" 12481 " if CONSTEXPR (true) return 42;", 12482 ShortMergedIf); 12483 ShortMergedIf.ColumnLimit = 29; 12484 verifyFormat("#define A \\\n" 12485 " if (aaaaaaaaaa) return 1; \\\n" 12486 " return 2;", 12487 ShortMergedIf); 12488 ShortMergedIf.ColumnLimit = 28; 12489 verifyFormat("#define A \\\n" 12490 " if (aaaaaaaaaa) \\\n" 12491 " return 1; \\\n" 12492 " return 2;", 12493 ShortMergedIf); 12494 verifyFormat("#define A \\\n" 12495 " if constexpr (aaaaaaa) \\\n" 12496 " return 1; \\\n" 12497 " return 2;", 12498 ShortMergedIf); 12499 verifyFormat("#define A \\\n" 12500 " if CONSTEXPR (aaaaaaa) \\\n" 12501 " return 1; \\\n" 12502 " return 2;", 12503 ShortMergedIf); 12504 } 12505 12506 TEST_F(FormatTest, FormatStarDependingOnContext) { 12507 verifyFormat("void f(int *a);"); 12508 verifyFormat("void f() { f(fint * b); }"); 12509 verifyFormat("class A {\n void f(int *a);\n};"); 12510 verifyFormat("class A {\n int *a;\n};"); 12511 verifyFormat("namespace a {\n" 12512 "namespace b {\n" 12513 "class A {\n" 12514 " void f() {}\n" 12515 " int *a;\n" 12516 "};\n" 12517 "} // namespace b\n" 12518 "} // namespace a"); 12519 } 12520 12521 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12522 verifyFormat("while"); 12523 verifyFormat("operator"); 12524 } 12525 12526 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12527 // This code would be painfully slow to format if we didn't skip it. 12528 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 12529 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12530 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12531 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12532 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12533 "A(1, 1)\n" 12534 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12535 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12536 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12537 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12538 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12539 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12540 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12541 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12542 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12543 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12544 // Deeply nested part is untouched, rest is formatted. 12545 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12546 format(std::string("int i;\n") + Code + "int j;\n", 12547 getLLVMStyle(), SC_ExpectIncomplete)); 12548 } 12549 12550 //===----------------------------------------------------------------------===// 12551 // Objective-C tests. 12552 //===----------------------------------------------------------------------===// 12553 12554 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12555 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12556 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12557 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12558 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12559 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12560 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12561 format("-(NSInteger)Method3:(id)anObject;")); 12562 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12563 format("-(NSInteger)Method4:(id)anObject;")); 12564 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12565 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12566 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12567 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12568 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12569 "forAllCells:(BOOL)flag;", 12570 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12571 "forAllCells:(BOOL)flag;")); 12572 12573 // Very long objectiveC method declaration. 12574 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12575 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12576 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12577 " inRange:(NSRange)range\n" 12578 " outRange:(NSRange)out_range\n" 12579 " outRange1:(NSRange)out_range1\n" 12580 " outRange2:(NSRange)out_range2\n" 12581 " outRange3:(NSRange)out_range3\n" 12582 " outRange4:(NSRange)out_range4\n" 12583 " outRange5:(NSRange)out_range5\n" 12584 " outRange6:(NSRange)out_range6\n" 12585 " outRange7:(NSRange)out_range7\n" 12586 " outRange8:(NSRange)out_range8\n" 12587 " outRange9:(NSRange)out_range9;"); 12588 12589 // When the function name has to be wrapped. 12590 FormatStyle Style = getLLVMStyle(); 12591 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12592 // and always indents instead. 12593 Style.IndentWrappedFunctionNames = false; 12594 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12595 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12596 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12597 "}", 12598 Style); 12599 Style.IndentWrappedFunctionNames = true; 12600 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12601 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12602 " anotherName:(NSString)dddddddddddddd {\n" 12603 "}", 12604 Style); 12605 12606 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12607 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12608 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12609 // protocol lists (but not for template classes): 12610 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12611 12612 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12613 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12614 12615 // If there's no return type (very rare in practice!), LLVM and Google style 12616 // agree. 12617 verifyFormat("- foo;"); 12618 verifyFormat("- foo:(int)f;"); 12619 verifyGoogleFormat("- foo:(int)foo;"); 12620 } 12621 12622 TEST_F(FormatTest, BreaksStringLiterals) { 12623 EXPECT_EQ("\"some text \"\n" 12624 "\"other\";", 12625 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12626 EXPECT_EQ("\"some text \"\n" 12627 "\"other\";", 12628 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12629 EXPECT_EQ( 12630 "#define A \\\n" 12631 " \"some \" \\\n" 12632 " \"text \" \\\n" 12633 " \"other\";", 12634 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12635 EXPECT_EQ( 12636 "#define A \\\n" 12637 " \"so \" \\\n" 12638 " \"text \" \\\n" 12639 " \"other\";", 12640 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12641 12642 EXPECT_EQ("\"some text\"", 12643 format("\"some text\"", getLLVMStyleWithColumns(1))); 12644 EXPECT_EQ("\"some text\"", 12645 format("\"some text\"", getLLVMStyleWithColumns(11))); 12646 EXPECT_EQ("\"some \"\n" 12647 "\"text\"", 12648 format("\"some text\"", getLLVMStyleWithColumns(10))); 12649 EXPECT_EQ("\"some \"\n" 12650 "\"text\"", 12651 format("\"some text\"", getLLVMStyleWithColumns(7))); 12652 EXPECT_EQ("\"some\"\n" 12653 "\" tex\"\n" 12654 "\"t\"", 12655 format("\"some text\"", getLLVMStyleWithColumns(6))); 12656 EXPECT_EQ("\"some\"\n" 12657 "\" tex\"\n" 12658 "\" and\"", 12659 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12660 EXPECT_EQ("\"some\"\n" 12661 "\"/tex\"\n" 12662 "\"/and\"", 12663 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12664 12665 EXPECT_EQ("variable =\n" 12666 " \"long string \"\n" 12667 " \"literal\";", 12668 format("variable = \"long string literal\";", 12669 getLLVMStyleWithColumns(20))); 12670 12671 EXPECT_EQ("variable = f(\n" 12672 " \"long string \"\n" 12673 " \"literal\",\n" 12674 " short,\n" 12675 " loooooooooooooooooooong);", 12676 format("variable = f(\"long string literal\", short, " 12677 "loooooooooooooooooooong);", 12678 getLLVMStyleWithColumns(20))); 12679 12680 EXPECT_EQ( 12681 "f(g(\"long string \"\n" 12682 " \"literal\"),\n" 12683 " b);", 12684 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12685 EXPECT_EQ("f(g(\"long string \"\n" 12686 " \"literal\",\n" 12687 " a),\n" 12688 " b);", 12689 format("f(g(\"long string literal\", a), b);", 12690 getLLVMStyleWithColumns(20))); 12691 EXPECT_EQ( 12692 "f(\"one two\".split(\n" 12693 " variable));", 12694 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12695 EXPECT_EQ("f(\"one two three four five six \"\n" 12696 " \"seven\".split(\n" 12697 " really_looooong_variable));", 12698 format("f(\"one two three four five six seven\"." 12699 "split(really_looooong_variable));", 12700 getLLVMStyleWithColumns(33))); 12701 12702 EXPECT_EQ("f(\"some \"\n" 12703 " \"text\",\n" 12704 " other);", 12705 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12706 12707 // Only break as a last resort. 12708 verifyFormat( 12709 "aaaaaaaaaaaaaaaaaaaa(\n" 12710 " aaaaaaaaaaaaaaaaaaaa,\n" 12711 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12712 12713 EXPECT_EQ("\"splitmea\"\n" 12714 "\"trandomp\"\n" 12715 "\"oint\"", 12716 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12717 12718 EXPECT_EQ("\"split/\"\n" 12719 "\"pathat/\"\n" 12720 "\"slashes\"", 12721 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12722 12723 EXPECT_EQ("\"split/\"\n" 12724 "\"pathat/\"\n" 12725 "\"slashes\"", 12726 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12727 EXPECT_EQ("\"split at \"\n" 12728 "\"spaces/at/\"\n" 12729 "\"slashes.at.any$\"\n" 12730 "\"non-alphanumeric%\"\n" 12731 "\"1111111111characte\"\n" 12732 "\"rs\"", 12733 format("\"split at " 12734 "spaces/at/" 12735 "slashes.at." 12736 "any$non-" 12737 "alphanumeric%" 12738 "1111111111characte" 12739 "rs\"", 12740 getLLVMStyleWithColumns(20))); 12741 12742 // Verify that splitting the strings understands 12743 // Style::AlwaysBreakBeforeMultilineStrings. 12744 EXPECT_EQ("aaaaaaaaaaaa(\n" 12745 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12746 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12747 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12748 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12749 "aaaaaaaaaaaaaaaaaaaaaa\");", 12750 getGoogleStyle())); 12751 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12752 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12753 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12754 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12755 "aaaaaaaaaaaaaaaaaaaaaa\";", 12756 getGoogleStyle())); 12757 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12758 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12759 format("llvm::outs() << " 12760 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12761 "aaaaaaaaaaaaaaaaaaa\";")); 12762 EXPECT_EQ("ffff(\n" 12763 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12764 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12765 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12766 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12767 getGoogleStyle())); 12768 12769 FormatStyle Style = getLLVMStyleWithColumns(12); 12770 Style.BreakStringLiterals = false; 12771 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12772 12773 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12774 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12775 EXPECT_EQ("#define A \\\n" 12776 " \"some \" \\\n" 12777 " \"text \" \\\n" 12778 " \"other\";", 12779 format("#define A \"some text other\";", AlignLeft)); 12780 } 12781 12782 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12783 EXPECT_EQ("C a = \"some more \"\n" 12784 " \"text\";", 12785 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12786 } 12787 12788 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12789 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12790 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12791 EXPECT_EQ("int i = a(b());", 12792 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12793 } 12794 12795 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12796 EXPECT_EQ( 12797 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12798 "(\n" 12799 " \"x\t\");", 12800 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12801 "aaaaaaa(" 12802 "\"x\t\");")); 12803 } 12804 12805 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12806 EXPECT_EQ( 12807 "u8\"utf8 string \"\n" 12808 "u8\"literal\";", 12809 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12810 EXPECT_EQ( 12811 "u\"utf16 string \"\n" 12812 "u\"literal\";", 12813 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12814 EXPECT_EQ( 12815 "U\"utf32 string \"\n" 12816 "U\"literal\";", 12817 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12818 EXPECT_EQ("L\"wide string \"\n" 12819 "L\"literal\";", 12820 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12821 EXPECT_EQ("@\"NSString \"\n" 12822 "@\"literal\";", 12823 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12824 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12825 12826 // This input makes clang-format try to split the incomplete unicode escape 12827 // sequence, which used to lead to a crasher. 12828 verifyNoCrash( 12829 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12830 getLLVMStyleWithColumns(60)); 12831 } 12832 12833 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12834 FormatStyle Style = getGoogleStyleWithColumns(15); 12835 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12836 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12837 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12838 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12839 EXPECT_EQ("u8R\"x(raw literal)x\";", 12840 format("u8R\"x(raw literal)x\";", Style)); 12841 } 12842 12843 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12844 FormatStyle Style = getLLVMStyleWithColumns(20); 12845 EXPECT_EQ( 12846 "_T(\"aaaaaaaaaaaaaa\")\n" 12847 "_T(\"aaaaaaaaaaaaaa\")\n" 12848 "_T(\"aaaaaaaaaaaa\")", 12849 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12850 EXPECT_EQ("f(x,\n" 12851 " _T(\"aaaaaaaaaaaa\")\n" 12852 " _T(\"aaa\"),\n" 12853 " z);", 12854 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 12855 12856 // FIXME: Handle embedded spaces in one iteration. 12857 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 12858 // "_T(\"aaaaaaaaaaaaa\")\n" 12859 // "_T(\"aaaaaaaaaaaaa\")\n" 12860 // "_T(\"a\")", 12861 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12862 // getLLVMStyleWithColumns(20))); 12863 EXPECT_EQ( 12864 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12865 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 12866 EXPECT_EQ("f(\n" 12867 "#if !TEST\n" 12868 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12869 "#endif\n" 12870 ");", 12871 format("f(\n" 12872 "#if !TEST\n" 12873 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12874 "#endif\n" 12875 ");")); 12876 EXPECT_EQ("f(\n" 12877 "\n" 12878 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 12879 format("f(\n" 12880 "\n" 12881 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 12882 } 12883 12884 TEST_F(FormatTest, BreaksStringLiteralOperands) { 12885 // In a function call with two operands, the second can be broken with no line 12886 // break before it. 12887 EXPECT_EQ( 12888 "func(a, \"long long \"\n" 12889 " \"long long\");", 12890 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 12891 // In a function call with three operands, the second must be broken with a 12892 // line break before it. 12893 EXPECT_EQ("func(a,\n" 12894 " \"long long long \"\n" 12895 " \"long\",\n" 12896 " c);", 12897 format("func(a, \"long long long long\", c);", 12898 getLLVMStyleWithColumns(24))); 12899 // In a function call with three operands, the third must be broken with a 12900 // line break before it. 12901 EXPECT_EQ("func(a, b,\n" 12902 " \"long long long \"\n" 12903 " \"long\");", 12904 format("func(a, b, \"long long long long\");", 12905 getLLVMStyleWithColumns(24))); 12906 // In a function call with three operands, both the second and the third must 12907 // be broken with a line break before them. 12908 EXPECT_EQ("func(a,\n" 12909 " \"long long long \"\n" 12910 " \"long\",\n" 12911 " \"long long long \"\n" 12912 " \"long\");", 12913 format("func(a, \"long long long long\", \"long long long long\");", 12914 getLLVMStyleWithColumns(24))); 12915 // In a chain of << with two operands, the second can be broken with no line 12916 // break before it. 12917 EXPECT_EQ("a << \"line line \"\n" 12918 " \"line\";", 12919 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12920 // In a chain of << with three operands, the second can be broken with no line 12921 // break before it. 12922 EXPECT_EQ( 12923 "abcde << \"line \"\n" 12924 " \"line line\"\n" 12925 " << c;", 12926 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12927 // In a chain of << with three operands, the third must be broken with a line 12928 // break before it. 12929 EXPECT_EQ( 12930 "a << b\n" 12931 " << \"line line \"\n" 12932 " \"line\";", 12933 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 12934 // In a chain of << with three operands, the second can be broken with no line 12935 // break before it and the third must be broken with a line break before it. 12936 EXPECT_EQ("abcd << \"line line \"\n" 12937 " \"line\"\n" 12938 " << \"line line \"\n" 12939 " \"line\";", 12940 format("abcd << \"line line line\" << \"line line line\";", 12941 getLLVMStyleWithColumns(20))); 12942 // In a chain of binary operators with two operands, the second can be broken 12943 // with no line break before it. 12944 EXPECT_EQ( 12945 "abcd + \"line line \"\n" 12946 " \"line line\";", 12947 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12948 // In a chain of binary operators with three operands, the second must be 12949 // broken with a line break before it. 12950 EXPECT_EQ("abcd +\n" 12951 " \"line line \"\n" 12952 " \"line line\" +\n" 12953 " e;", 12954 format("abcd + \"line line line line\" + e;", 12955 getLLVMStyleWithColumns(20))); 12956 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12957 // the first must be broken with a line break before it. 12958 FormatStyle Style = getLLVMStyleWithColumns(25); 12959 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12960 EXPECT_EQ("someFunction(\n" 12961 " \"long long long \"\n" 12962 " \"long\",\n" 12963 " a);", 12964 format("someFunction(\"long long long long\", a);", Style)); 12965 } 12966 12967 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12968 EXPECT_EQ( 12969 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12972 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12973 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12974 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12975 } 12976 12977 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12978 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12979 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12980 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12981 "multiline raw string literal xxxxxxxxxxxxxx\n" 12982 ")x\",\n" 12983 " a),\n" 12984 " b);", 12985 format("fffffffffff(g(R\"x(\n" 12986 "multiline raw string literal xxxxxxxxxxxxxx\n" 12987 ")x\", a), b);", 12988 getGoogleStyleWithColumns(20))); 12989 EXPECT_EQ("fffffffffff(\n" 12990 " g(R\"x(qqq\n" 12991 "multiline raw string literal xxxxxxxxxxxxxx\n" 12992 ")x\",\n" 12993 " a),\n" 12994 " b);", 12995 format("fffffffffff(g(R\"x(qqq\n" 12996 "multiline raw string literal xxxxxxxxxxxxxx\n" 12997 ")x\", a), b);", 12998 getGoogleStyleWithColumns(20))); 12999 13000 EXPECT_EQ("fffffffffff(R\"x(\n" 13001 "multiline raw string literal xxxxxxxxxxxxxx\n" 13002 ")x\");", 13003 format("fffffffffff(R\"x(\n" 13004 "multiline raw string literal xxxxxxxxxxxxxx\n" 13005 ")x\");", 13006 getGoogleStyleWithColumns(20))); 13007 EXPECT_EQ("fffffffffff(R\"x(\n" 13008 "multiline raw string literal xxxxxxxxxxxxxx\n" 13009 ")x\" + bbbbbb);", 13010 format("fffffffffff(R\"x(\n" 13011 "multiline raw string literal xxxxxxxxxxxxxx\n" 13012 ")x\" + bbbbbb);", 13013 getGoogleStyleWithColumns(20))); 13014 EXPECT_EQ("fffffffffff(\n" 13015 " R\"x(\n" 13016 "multiline raw string literal xxxxxxxxxxxxxx\n" 13017 ")x\" +\n" 13018 " bbbbbb);", 13019 format("fffffffffff(\n" 13020 " R\"x(\n" 13021 "multiline raw string literal xxxxxxxxxxxxxx\n" 13022 ")x\" + bbbbbb);", 13023 getGoogleStyleWithColumns(20))); 13024 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 13025 format("fffffffffff(\n" 13026 " R\"(single line raw string)\" + bbbbbb);")); 13027 } 13028 13029 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13030 verifyFormat("string a = \"unterminated;"); 13031 EXPECT_EQ("function(\"unterminated,\n" 13032 " OtherParameter);", 13033 format("function( \"unterminated,\n" 13034 " OtherParameter);")); 13035 } 13036 13037 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13038 FormatStyle Style = getLLVMStyle(); 13039 Style.Standard = FormatStyle::LS_Cpp03; 13040 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13041 format("#define x(_a) printf(\"foo\"_a);", Style)); 13042 } 13043 13044 TEST_F(FormatTest, CppLexVersion) { 13045 FormatStyle Style = getLLVMStyle(); 13046 // Formatting of x * y differs if x is a type. 13047 verifyFormat("void foo() { MACRO(a * b); }", Style); 13048 verifyFormat("void foo() { MACRO(int *b); }", Style); 13049 13050 // LLVM style uses latest lexer. 13051 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13052 Style.Standard = FormatStyle::LS_Cpp17; 13053 // But in c++17, char8_t isn't a keyword. 13054 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13055 } 13056 13057 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13058 13059 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13060 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13061 " \"ddeeefff\");", 13062 format("someFunction(\"aaabbbcccdddeeefff\");", 13063 getLLVMStyleWithColumns(25))); 13064 EXPECT_EQ("someFunction1234567890(\n" 13065 " \"aaabbbcccdddeeefff\");", 13066 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13067 getLLVMStyleWithColumns(26))); 13068 EXPECT_EQ("someFunction1234567890(\n" 13069 " \"aaabbbcccdddeeeff\"\n" 13070 " \"f\");", 13071 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13072 getLLVMStyleWithColumns(25))); 13073 EXPECT_EQ("someFunction1234567890(\n" 13074 " \"aaabbbcccdddeeeff\"\n" 13075 " \"f\");", 13076 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13077 getLLVMStyleWithColumns(24))); 13078 EXPECT_EQ("someFunction(\n" 13079 " \"aaabbbcc ddde \"\n" 13080 " \"efff\");", 13081 format("someFunction(\"aaabbbcc ddde efff\");", 13082 getLLVMStyleWithColumns(25))); 13083 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13084 " \"ddeeefff\");", 13085 format("someFunction(\"aaabbbccc ddeeefff\");", 13086 getLLVMStyleWithColumns(25))); 13087 EXPECT_EQ("someFunction1234567890(\n" 13088 " \"aaabb \"\n" 13089 " \"cccdddeeefff\");", 13090 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13091 getLLVMStyleWithColumns(25))); 13092 EXPECT_EQ("#define A \\\n" 13093 " string s = \\\n" 13094 " \"123456789\" \\\n" 13095 " \"0\"; \\\n" 13096 " int i;", 13097 format("#define A string s = \"1234567890\"; int i;", 13098 getLLVMStyleWithColumns(20))); 13099 EXPECT_EQ("someFunction(\n" 13100 " \"aaabbbcc \"\n" 13101 " \"dddeeefff\");", 13102 format("someFunction(\"aaabbbcc dddeeefff\");", 13103 getLLVMStyleWithColumns(25))); 13104 } 13105 13106 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13107 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13108 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13109 EXPECT_EQ("\"test\"\n" 13110 "\"\\n\"", 13111 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13112 EXPECT_EQ("\"tes\\\\\"\n" 13113 "\"n\"", 13114 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13115 EXPECT_EQ("\"\\\\\\\\\"\n" 13116 "\"\\n\"", 13117 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13118 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13119 EXPECT_EQ("\"\\uff01\"\n" 13120 "\"test\"", 13121 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13122 EXPECT_EQ("\"\\Uff01ff02\"", 13123 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13124 EXPECT_EQ("\"\\x000000000001\"\n" 13125 "\"next\"", 13126 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13127 EXPECT_EQ("\"\\x000000000001next\"", 13128 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13129 EXPECT_EQ("\"\\x000000000001\"", 13130 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13131 EXPECT_EQ("\"test\"\n" 13132 "\"\\000000\"\n" 13133 "\"000001\"", 13134 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13135 EXPECT_EQ("\"test\\000\"\n" 13136 "\"00000000\"\n" 13137 "\"1\"", 13138 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13139 } 13140 13141 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13142 verifyFormat("void f() {\n" 13143 " return g() {}\n" 13144 " void h() {}"); 13145 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13146 "g();\n" 13147 "}"); 13148 } 13149 13150 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13151 verifyFormat( 13152 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13153 } 13154 13155 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13156 verifyFormat("class X {\n" 13157 " void f() {\n" 13158 " }\n" 13159 "};", 13160 getLLVMStyleWithColumns(12)); 13161 } 13162 13163 TEST_F(FormatTest, ConfigurableIndentWidth) { 13164 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13165 EightIndent.IndentWidth = 8; 13166 EightIndent.ContinuationIndentWidth = 8; 13167 verifyFormat("void f() {\n" 13168 " someFunction();\n" 13169 " if (true) {\n" 13170 " f();\n" 13171 " }\n" 13172 "}", 13173 EightIndent); 13174 verifyFormat("class X {\n" 13175 " void f() {\n" 13176 " }\n" 13177 "};", 13178 EightIndent); 13179 verifyFormat("int x[] = {\n" 13180 " call(),\n" 13181 " call()};", 13182 EightIndent); 13183 } 13184 13185 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13186 verifyFormat("double\n" 13187 "f();", 13188 getLLVMStyleWithColumns(8)); 13189 } 13190 13191 TEST_F(FormatTest, ConfigurableUseOfTab) { 13192 FormatStyle Tab = getLLVMStyleWithColumns(42); 13193 Tab.IndentWidth = 8; 13194 Tab.UseTab = FormatStyle::UT_Always; 13195 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13196 13197 EXPECT_EQ("if (aaaaaaaa && // q\n" 13198 " bb)\t\t// w\n" 13199 "\t;", 13200 format("if (aaaaaaaa &&// q\n" 13201 "bb)// w\n" 13202 ";", 13203 Tab)); 13204 EXPECT_EQ("if (aaa && bbb) // w\n" 13205 "\t;", 13206 format("if(aaa&&bbb)// w\n" 13207 ";", 13208 Tab)); 13209 13210 verifyFormat("class X {\n" 13211 "\tvoid f() {\n" 13212 "\t\tsomeFunction(parameter1,\n" 13213 "\t\t\t parameter2);\n" 13214 "\t}\n" 13215 "};", 13216 Tab); 13217 verifyFormat("#define A \\\n" 13218 "\tvoid f() { \\\n" 13219 "\t\tsomeFunction( \\\n" 13220 "\t\t parameter1, \\\n" 13221 "\t\t parameter2); \\\n" 13222 "\t}", 13223 Tab); 13224 verifyFormat("int a;\t // x\n" 13225 "int bbbbbbbb; // x\n", 13226 Tab); 13227 13228 Tab.TabWidth = 4; 13229 Tab.IndentWidth = 8; 13230 verifyFormat("class TabWidth4Indent8 {\n" 13231 "\t\tvoid f() {\n" 13232 "\t\t\t\tsomeFunction(parameter1,\n" 13233 "\t\t\t\t\t\t\t parameter2);\n" 13234 "\t\t}\n" 13235 "};", 13236 Tab); 13237 13238 Tab.TabWidth = 4; 13239 Tab.IndentWidth = 4; 13240 verifyFormat("class TabWidth4Indent4 {\n" 13241 "\tvoid f() {\n" 13242 "\t\tsomeFunction(parameter1,\n" 13243 "\t\t\t\t\t parameter2);\n" 13244 "\t}\n" 13245 "};", 13246 Tab); 13247 13248 Tab.TabWidth = 8; 13249 Tab.IndentWidth = 4; 13250 verifyFormat("class TabWidth8Indent4 {\n" 13251 " void f() {\n" 13252 "\tsomeFunction(parameter1,\n" 13253 "\t\t parameter2);\n" 13254 " }\n" 13255 "};", 13256 Tab); 13257 13258 Tab.TabWidth = 8; 13259 Tab.IndentWidth = 8; 13260 EXPECT_EQ("/*\n" 13261 "\t a\t\tcomment\n" 13262 "\t in multiple lines\n" 13263 " */", 13264 format(" /*\t \t \n" 13265 " \t \t a\t\tcomment\t \t\n" 13266 " \t \t in multiple lines\t\n" 13267 " \t */", 13268 Tab)); 13269 13270 Tab.UseTab = FormatStyle::UT_ForIndentation; 13271 verifyFormat("{\n" 13272 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13273 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13274 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13275 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13276 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13277 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13278 "};", 13279 Tab); 13280 verifyFormat("enum AA {\n" 13281 "\ta1, // Force multiple lines\n" 13282 "\ta2,\n" 13283 "\ta3\n" 13284 "};", 13285 Tab); 13286 EXPECT_EQ("if (aaaaaaaa && // q\n" 13287 " bb) // w\n" 13288 "\t;", 13289 format("if (aaaaaaaa &&// q\n" 13290 "bb)// w\n" 13291 ";", 13292 Tab)); 13293 verifyFormat("class X {\n" 13294 "\tvoid f() {\n" 13295 "\t\tsomeFunction(parameter1,\n" 13296 "\t\t parameter2);\n" 13297 "\t}\n" 13298 "};", 13299 Tab); 13300 verifyFormat("{\n" 13301 "\tQ(\n" 13302 "\t {\n" 13303 "\t\t int a;\n" 13304 "\t\t someFunction(aaaaaaaa,\n" 13305 "\t\t bbbbbbb);\n" 13306 "\t },\n" 13307 "\t p);\n" 13308 "}", 13309 Tab); 13310 EXPECT_EQ("{\n" 13311 "\t/* aaaa\n" 13312 "\t bbbb */\n" 13313 "}", 13314 format("{\n" 13315 "/* aaaa\n" 13316 " bbbb */\n" 13317 "}", 13318 Tab)); 13319 EXPECT_EQ("{\n" 13320 "\t/*\n" 13321 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13322 "\t bbbbbbbbbbbbb\n" 13323 "\t*/\n" 13324 "}", 13325 format("{\n" 13326 "/*\n" 13327 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13328 "*/\n" 13329 "}", 13330 Tab)); 13331 EXPECT_EQ("{\n" 13332 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13333 "\t// bbbbbbbbbbbbb\n" 13334 "}", 13335 format("{\n" 13336 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13337 "}", 13338 Tab)); 13339 EXPECT_EQ("{\n" 13340 "\t/*\n" 13341 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13342 "\t bbbbbbbbbbbbb\n" 13343 "\t*/\n" 13344 "}", 13345 format("{\n" 13346 "\t/*\n" 13347 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13348 "\t*/\n" 13349 "}", 13350 Tab)); 13351 EXPECT_EQ("{\n" 13352 "\t/*\n" 13353 "\n" 13354 "\t*/\n" 13355 "}", 13356 format("{\n" 13357 "\t/*\n" 13358 "\n" 13359 "\t*/\n" 13360 "}", 13361 Tab)); 13362 EXPECT_EQ("{\n" 13363 "\t/*\n" 13364 " asdf\n" 13365 "\t*/\n" 13366 "}", 13367 format("{\n" 13368 "\t/*\n" 13369 " asdf\n" 13370 "\t*/\n" 13371 "}", 13372 Tab)); 13373 13374 Tab.UseTab = FormatStyle::UT_Never; 13375 EXPECT_EQ("/*\n" 13376 " a\t\tcomment\n" 13377 " in multiple lines\n" 13378 " */", 13379 format(" /*\t \t \n" 13380 " \t \t a\t\tcomment\t \t\n" 13381 " \t \t in multiple lines\t\n" 13382 " \t */", 13383 Tab)); 13384 EXPECT_EQ("/* some\n" 13385 " comment */", 13386 format(" \t \t /* some\n" 13387 " \t \t comment */", 13388 Tab)); 13389 EXPECT_EQ("int a; /* some\n" 13390 " comment */", 13391 format(" \t \t int a; /* some\n" 13392 " \t \t comment */", 13393 Tab)); 13394 13395 EXPECT_EQ("int a; /* some\n" 13396 "comment */", 13397 format(" \t \t int\ta; /* some\n" 13398 " \t \t comment */", 13399 Tab)); 13400 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13401 " comment */", 13402 format(" \t \t f(\"\t\t\"); /* some\n" 13403 " \t \t comment */", 13404 Tab)); 13405 EXPECT_EQ("{\n" 13406 " /*\n" 13407 " * Comment\n" 13408 " */\n" 13409 " int i;\n" 13410 "}", 13411 format("{\n" 13412 "\t/*\n" 13413 "\t * Comment\n" 13414 "\t */\n" 13415 "\t int i;\n" 13416 "}", 13417 Tab)); 13418 13419 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13420 Tab.TabWidth = 8; 13421 Tab.IndentWidth = 8; 13422 EXPECT_EQ("if (aaaaaaaa && // q\n" 13423 " bb) // w\n" 13424 "\t;", 13425 format("if (aaaaaaaa &&// q\n" 13426 "bb)// w\n" 13427 ";", 13428 Tab)); 13429 EXPECT_EQ("if (aaa && bbb) // w\n" 13430 "\t;", 13431 format("if(aaa&&bbb)// w\n" 13432 ";", 13433 Tab)); 13434 verifyFormat("class X {\n" 13435 "\tvoid f() {\n" 13436 "\t\tsomeFunction(parameter1,\n" 13437 "\t\t\t parameter2);\n" 13438 "\t}\n" 13439 "};", 13440 Tab); 13441 verifyFormat("#define A \\\n" 13442 "\tvoid f() { \\\n" 13443 "\t\tsomeFunction( \\\n" 13444 "\t\t parameter1, \\\n" 13445 "\t\t parameter2); \\\n" 13446 "\t}", 13447 Tab); 13448 Tab.TabWidth = 4; 13449 Tab.IndentWidth = 8; 13450 verifyFormat("class TabWidth4Indent8 {\n" 13451 "\t\tvoid f() {\n" 13452 "\t\t\t\tsomeFunction(parameter1,\n" 13453 "\t\t\t\t\t\t\t parameter2);\n" 13454 "\t\t}\n" 13455 "};", 13456 Tab); 13457 Tab.TabWidth = 4; 13458 Tab.IndentWidth = 4; 13459 verifyFormat("class TabWidth4Indent4 {\n" 13460 "\tvoid f() {\n" 13461 "\t\tsomeFunction(parameter1,\n" 13462 "\t\t\t\t\t parameter2);\n" 13463 "\t}\n" 13464 "};", 13465 Tab); 13466 Tab.TabWidth = 8; 13467 Tab.IndentWidth = 4; 13468 verifyFormat("class TabWidth8Indent4 {\n" 13469 " void f() {\n" 13470 "\tsomeFunction(parameter1,\n" 13471 "\t\t parameter2);\n" 13472 " }\n" 13473 "};", 13474 Tab); 13475 Tab.TabWidth = 8; 13476 Tab.IndentWidth = 8; 13477 EXPECT_EQ("/*\n" 13478 "\t a\t\tcomment\n" 13479 "\t in multiple lines\n" 13480 " */", 13481 format(" /*\t \t \n" 13482 " \t \t a\t\tcomment\t \t\n" 13483 " \t \t in multiple lines\t\n" 13484 " \t */", 13485 Tab)); 13486 verifyFormat("{\n" 13487 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13488 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13489 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13490 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13491 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13492 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13493 "};", 13494 Tab); 13495 verifyFormat("enum AA {\n" 13496 "\ta1, // Force multiple lines\n" 13497 "\ta2,\n" 13498 "\ta3\n" 13499 "};", 13500 Tab); 13501 EXPECT_EQ("if (aaaaaaaa && // q\n" 13502 " bb) // w\n" 13503 "\t;", 13504 format("if (aaaaaaaa &&// q\n" 13505 "bb)// w\n" 13506 ";", 13507 Tab)); 13508 verifyFormat("class X {\n" 13509 "\tvoid f() {\n" 13510 "\t\tsomeFunction(parameter1,\n" 13511 "\t\t\t parameter2);\n" 13512 "\t}\n" 13513 "};", 13514 Tab); 13515 verifyFormat("{\n" 13516 "\tQ(\n" 13517 "\t {\n" 13518 "\t\t int a;\n" 13519 "\t\t someFunction(aaaaaaaa,\n" 13520 "\t\t\t\t bbbbbbb);\n" 13521 "\t },\n" 13522 "\t p);\n" 13523 "}", 13524 Tab); 13525 EXPECT_EQ("{\n" 13526 "\t/* aaaa\n" 13527 "\t bbbb */\n" 13528 "}", 13529 format("{\n" 13530 "/* aaaa\n" 13531 " bbbb */\n" 13532 "}", 13533 Tab)); 13534 EXPECT_EQ("{\n" 13535 "\t/*\n" 13536 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13537 "\t bbbbbbbbbbbbb\n" 13538 "\t*/\n" 13539 "}", 13540 format("{\n" 13541 "/*\n" 13542 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13543 "*/\n" 13544 "}", 13545 Tab)); 13546 EXPECT_EQ("{\n" 13547 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13548 "\t// bbbbbbbbbbbbb\n" 13549 "}", 13550 format("{\n" 13551 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13552 "}", 13553 Tab)); 13554 EXPECT_EQ("{\n" 13555 "\t/*\n" 13556 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13557 "\t bbbbbbbbbbbbb\n" 13558 "\t*/\n" 13559 "}", 13560 format("{\n" 13561 "\t/*\n" 13562 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13563 "\t*/\n" 13564 "}", 13565 Tab)); 13566 EXPECT_EQ("{\n" 13567 "\t/*\n" 13568 "\n" 13569 "\t*/\n" 13570 "}", 13571 format("{\n" 13572 "\t/*\n" 13573 "\n" 13574 "\t*/\n" 13575 "}", 13576 Tab)); 13577 EXPECT_EQ("{\n" 13578 "\t/*\n" 13579 " asdf\n" 13580 "\t*/\n" 13581 "}", 13582 format("{\n" 13583 "\t/*\n" 13584 " asdf\n" 13585 "\t*/\n" 13586 "}", 13587 Tab)); 13588 EXPECT_EQ("/* some\n" 13589 " comment */", 13590 format(" \t \t /* some\n" 13591 " \t \t comment */", 13592 Tab)); 13593 EXPECT_EQ("int a; /* some\n" 13594 " comment */", 13595 format(" \t \t int a; /* some\n" 13596 " \t \t comment */", 13597 Tab)); 13598 EXPECT_EQ("int a; /* some\n" 13599 "comment */", 13600 format(" \t \t int\ta; /* some\n" 13601 " \t \t comment */", 13602 Tab)); 13603 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13604 " comment */", 13605 format(" \t \t f(\"\t\t\"); /* some\n" 13606 " \t \t comment */", 13607 Tab)); 13608 EXPECT_EQ("{\n" 13609 "\t/*\n" 13610 "\t * Comment\n" 13611 "\t */\n" 13612 "\tint i;\n" 13613 "}", 13614 format("{\n" 13615 "\t/*\n" 13616 "\t * Comment\n" 13617 "\t */\n" 13618 "\t int i;\n" 13619 "}", 13620 Tab)); 13621 Tab.TabWidth = 2; 13622 Tab.IndentWidth = 2; 13623 EXPECT_EQ("{\n" 13624 "\t/* aaaa\n" 13625 "\t\t bbbb */\n" 13626 "}", 13627 format("{\n" 13628 "/* aaaa\n" 13629 "\t bbbb */\n" 13630 "}", 13631 Tab)); 13632 EXPECT_EQ("{\n" 13633 "\t/*\n" 13634 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13635 "\t\tbbbbbbbbbbbbb\n" 13636 "\t*/\n" 13637 "}", 13638 format("{\n" 13639 "/*\n" 13640 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13641 "*/\n" 13642 "}", 13643 Tab)); 13644 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13645 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13646 Tab.TabWidth = 4; 13647 Tab.IndentWidth = 4; 13648 verifyFormat("class Assign {\n" 13649 "\tvoid f() {\n" 13650 "\t\tint x = 123;\n" 13651 "\t\tint random = 4;\n" 13652 "\t\tstd::string alphabet =\n" 13653 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13654 "\t}\n" 13655 "};", 13656 Tab); 13657 13658 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13659 Tab.TabWidth = 8; 13660 Tab.IndentWidth = 8; 13661 EXPECT_EQ("if (aaaaaaaa && // q\n" 13662 " bb) // w\n" 13663 "\t;", 13664 format("if (aaaaaaaa &&// q\n" 13665 "bb)// w\n" 13666 ";", 13667 Tab)); 13668 EXPECT_EQ("if (aaa && bbb) // w\n" 13669 "\t;", 13670 format("if(aaa&&bbb)// w\n" 13671 ";", 13672 Tab)); 13673 verifyFormat("class X {\n" 13674 "\tvoid f() {\n" 13675 "\t\tsomeFunction(parameter1,\n" 13676 "\t\t parameter2);\n" 13677 "\t}\n" 13678 "};", 13679 Tab); 13680 verifyFormat("#define A \\\n" 13681 "\tvoid f() { \\\n" 13682 "\t\tsomeFunction( \\\n" 13683 "\t\t parameter1, \\\n" 13684 "\t\t parameter2); \\\n" 13685 "\t}", 13686 Tab); 13687 Tab.TabWidth = 4; 13688 Tab.IndentWidth = 8; 13689 verifyFormat("class TabWidth4Indent8 {\n" 13690 "\t\tvoid f() {\n" 13691 "\t\t\t\tsomeFunction(parameter1,\n" 13692 "\t\t\t\t parameter2);\n" 13693 "\t\t}\n" 13694 "};", 13695 Tab); 13696 Tab.TabWidth = 4; 13697 Tab.IndentWidth = 4; 13698 verifyFormat("class TabWidth4Indent4 {\n" 13699 "\tvoid f() {\n" 13700 "\t\tsomeFunction(parameter1,\n" 13701 "\t\t parameter2);\n" 13702 "\t}\n" 13703 "};", 13704 Tab); 13705 Tab.TabWidth = 8; 13706 Tab.IndentWidth = 4; 13707 verifyFormat("class TabWidth8Indent4 {\n" 13708 " void f() {\n" 13709 "\tsomeFunction(parameter1,\n" 13710 "\t parameter2);\n" 13711 " }\n" 13712 "};", 13713 Tab); 13714 Tab.TabWidth = 8; 13715 Tab.IndentWidth = 8; 13716 EXPECT_EQ("/*\n" 13717 " a\t\tcomment\n" 13718 " in multiple lines\n" 13719 " */", 13720 format(" /*\t \t \n" 13721 " \t \t a\t\tcomment\t \t\n" 13722 " \t \t in multiple lines\t\n" 13723 " \t */", 13724 Tab)); 13725 verifyFormat("{\n" 13726 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13727 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13728 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13729 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13730 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13731 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13732 "};", 13733 Tab); 13734 verifyFormat("enum AA {\n" 13735 "\ta1, // Force multiple lines\n" 13736 "\ta2,\n" 13737 "\ta3\n" 13738 "};", 13739 Tab); 13740 EXPECT_EQ("if (aaaaaaaa && // q\n" 13741 " bb) // w\n" 13742 "\t;", 13743 format("if (aaaaaaaa &&// q\n" 13744 "bb)// w\n" 13745 ";", 13746 Tab)); 13747 verifyFormat("class X {\n" 13748 "\tvoid f() {\n" 13749 "\t\tsomeFunction(parameter1,\n" 13750 "\t\t parameter2);\n" 13751 "\t}\n" 13752 "};", 13753 Tab); 13754 verifyFormat("{\n" 13755 "\tQ(\n" 13756 "\t {\n" 13757 "\t\t int a;\n" 13758 "\t\t someFunction(aaaaaaaa,\n" 13759 "\t\t bbbbbbb);\n" 13760 "\t },\n" 13761 "\t p);\n" 13762 "}", 13763 Tab); 13764 EXPECT_EQ("{\n" 13765 "\t/* aaaa\n" 13766 "\t bbbb */\n" 13767 "}", 13768 format("{\n" 13769 "/* aaaa\n" 13770 " bbbb */\n" 13771 "}", 13772 Tab)); 13773 EXPECT_EQ("{\n" 13774 "\t/*\n" 13775 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13776 "\t bbbbbbbbbbbbb\n" 13777 "\t*/\n" 13778 "}", 13779 format("{\n" 13780 "/*\n" 13781 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13782 "*/\n" 13783 "}", 13784 Tab)); 13785 EXPECT_EQ("{\n" 13786 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13787 "\t// bbbbbbbbbbbbb\n" 13788 "}", 13789 format("{\n" 13790 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13791 "}", 13792 Tab)); 13793 EXPECT_EQ("{\n" 13794 "\t/*\n" 13795 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13796 "\t bbbbbbbbbbbbb\n" 13797 "\t*/\n" 13798 "}", 13799 format("{\n" 13800 "\t/*\n" 13801 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13802 "\t*/\n" 13803 "}", 13804 Tab)); 13805 EXPECT_EQ("{\n" 13806 "\t/*\n" 13807 "\n" 13808 "\t*/\n" 13809 "}", 13810 format("{\n" 13811 "\t/*\n" 13812 "\n" 13813 "\t*/\n" 13814 "}", 13815 Tab)); 13816 EXPECT_EQ("{\n" 13817 "\t/*\n" 13818 " asdf\n" 13819 "\t*/\n" 13820 "}", 13821 format("{\n" 13822 "\t/*\n" 13823 " asdf\n" 13824 "\t*/\n" 13825 "}", 13826 Tab)); 13827 EXPECT_EQ("/* some\n" 13828 " comment */", 13829 format(" \t \t /* some\n" 13830 " \t \t comment */", 13831 Tab)); 13832 EXPECT_EQ("int a; /* some\n" 13833 " comment */", 13834 format(" \t \t int a; /* some\n" 13835 " \t \t comment */", 13836 Tab)); 13837 EXPECT_EQ("int a; /* some\n" 13838 "comment */", 13839 format(" \t \t int\ta; /* some\n" 13840 " \t \t comment */", 13841 Tab)); 13842 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13843 " comment */", 13844 format(" \t \t f(\"\t\t\"); /* some\n" 13845 " \t \t comment */", 13846 Tab)); 13847 EXPECT_EQ("{\n" 13848 "\t/*\n" 13849 "\t * Comment\n" 13850 "\t */\n" 13851 "\tint i;\n" 13852 "}", 13853 format("{\n" 13854 "\t/*\n" 13855 "\t * Comment\n" 13856 "\t */\n" 13857 "\t int i;\n" 13858 "}", 13859 Tab)); 13860 Tab.TabWidth = 2; 13861 Tab.IndentWidth = 2; 13862 EXPECT_EQ("{\n" 13863 "\t/* aaaa\n" 13864 "\t bbbb */\n" 13865 "}", 13866 format("{\n" 13867 "/* aaaa\n" 13868 " bbbb */\n" 13869 "}", 13870 Tab)); 13871 EXPECT_EQ("{\n" 13872 "\t/*\n" 13873 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13874 "\t bbbbbbbbbbbbb\n" 13875 "\t*/\n" 13876 "}", 13877 format("{\n" 13878 "/*\n" 13879 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13880 "*/\n" 13881 "}", 13882 Tab)); 13883 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13884 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13885 Tab.TabWidth = 4; 13886 Tab.IndentWidth = 4; 13887 verifyFormat("class Assign {\n" 13888 "\tvoid f() {\n" 13889 "\t\tint x = 123;\n" 13890 "\t\tint random = 4;\n" 13891 "\t\tstd::string alphabet =\n" 13892 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13893 "\t}\n" 13894 "};", 13895 Tab); 13896 Tab.AlignOperands = FormatStyle::OAS_Align; 13897 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 13898 " cccccccccccccccccccc;", 13899 Tab); 13900 // no alignment 13901 verifyFormat("int aaaaaaaaaa =\n" 13902 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 13903 Tab); 13904 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 13905 " : bbbbbbbbbbbbbb ? 222222222222222\n" 13906 " : 333333333333333;", 13907 Tab); 13908 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 13909 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 13910 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 13911 " + cccccccccccccccccccc;", 13912 Tab); 13913 } 13914 13915 TEST_F(FormatTest, ZeroTabWidth) { 13916 FormatStyle Tab = getLLVMStyleWithColumns(42); 13917 Tab.IndentWidth = 8; 13918 Tab.UseTab = FormatStyle::UT_Never; 13919 Tab.TabWidth = 0; 13920 EXPECT_EQ("void a(){\n" 13921 " // line starts with '\t'\n" 13922 "};", 13923 format("void a(){\n" 13924 "\t// line starts with '\t'\n" 13925 "};", 13926 Tab)); 13927 13928 EXPECT_EQ("void a(){\n" 13929 " // line starts with '\t'\n" 13930 "};", 13931 format("void a(){\n" 13932 "\t\t// line starts with '\t'\n" 13933 "};", 13934 Tab)); 13935 13936 Tab.UseTab = FormatStyle::UT_ForIndentation; 13937 EXPECT_EQ("void a(){\n" 13938 " // line starts with '\t'\n" 13939 "};", 13940 format("void a(){\n" 13941 "\t// line starts with '\t'\n" 13942 "};", 13943 Tab)); 13944 13945 EXPECT_EQ("void a(){\n" 13946 " // line starts with '\t'\n" 13947 "};", 13948 format("void a(){\n" 13949 "\t\t// line starts with '\t'\n" 13950 "};", 13951 Tab)); 13952 13953 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13954 EXPECT_EQ("void a(){\n" 13955 " // line starts with '\t'\n" 13956 "};", 13957 format("void a(){\n" 13958 "\t// line starts with '\t'\n" 13959 "};", 13960 Tab)); 13961 13962 EXPECT_EQ("void a(){\n" 13963 " // line starts with '\t'\n" 13964 "};", 13965 format("void a(){\n" 13966 "\t\t// line starts with '\t'\n" 13967 "};", 13968 Tab)); 13969 13970 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13971 EXPECT_EQ("void a(){\n" 13972 " // line starts with '\t'\n" 13973 "};", 13974 format("void a(){\n" 13975 "\t// line starts with '\t'\n" 13976 "};", 13977 Tab)); 13978 13979 EXPECT_EQ("void a(){\n" 13980 " // line starts with '\t'\n" 13981 "};", 13982 format("void a(){\n" 13983 "\t\t// line starts with '\t'\n" 13984 "};", 13985 Tab)); 13986 13987 Tab.UseTab = FormatStyle::UT_Always; 13988 EXPECT_EQ("void a(){\n" 13989 "// line starts with '\t'\n" 13990 "};", 13991 format("void a(){\n" 13992 "\t// line starts with '\t'\n" 13993 "};", 13994 Tab)); 13995 13996 EXPECT_EQ("void a(){\n" 13997 "// line starts with '\t'\n" 13998 "};", 13999 format("void a(){\n" 14000 "\t\t// line starts with '\t'\n" 14001 "};", 14002 Tab)); 14003 } 14004 14005 TEST_F(FormatTest, CalculatesOriginalColumn) { 14006 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14007 "q\"; /* some\n" 14008 " comment */", 14009 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14010 "q\"; /* some\n" 14011 " comment */", 14012 getLLVMStyle())); 14013 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14014 "/* some\n" 14015 " comment */", 14016 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14017 " /* some\n" 14018 " comment */", 14019 getLLVMStyle())); 14020 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14021 "qqq\n" 14022 "/* some\n" 14023 " comment */", 14024 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14025 "qqq\n" 14026 " /* some\n" 14027 " comment */", 14028 getLLVMStyle())); 14029 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14030 "wwww; /* some\n" 14031 " comment */", 14032 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14033 "wwww; /* some\n" 14034 " comment */", 14035 getLLVMStyle())); 14036 } 14037 14038 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 14039 FormatStyle NoSpace = getLLVMStyle(); 14040 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 14041 14042 verifyFormat("while(true)\n" 14043 " continue;", 14044 NoSpace); 14045 verifyFormat("for(;;)\n" 14046 " continue;", 14047 NoSpace); 14048 verifyFormat("if(true)\n" 14049 " f();\n" 14050 "else if(true)\n" 14051 " f();", 14052 NoSpace); 14053 verifyFormat("do {\n" 14054 " do_something();\n" 14055 "} while(something());", 14056 NoSpace); 14057 verifyFormat("switch(x) {\n" 14058 "default:\n" 14059 " break;\n" 14060 "}", 14061 NoSpace); 14062 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14063 verifyFormat("size_t x = sizeof(x);", NoSpace); 14064 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14065 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14066 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14067 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14068 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14069 verifyFormat("alignas(128) char a[128];", NoSpace); 14070 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14071 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14072 verifyFormat("int f() throw(Deprecated);", NoSpace); 14073 verifyFormat("typedef void (*cb)(int);", NoSpace); 14074 verifyFormat("T A::operator()();", NoSpace); 14075 verifyFormat("X A::operator++(T);", NoSpace); 14076 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14077 14078 FormatStyle Space = getLLVMStyle(); 14079 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14080 14081 verifyFormat("int f ();", Space); 14082 verifyFormat("void f (int a, T b) {\n" 14083 " while (true)\n" 14084 " continue;\n" 14085 "}", 14086 Space); 14087 verifyFormat("if (true)\n" 14088 " f ();\n" 14089 "else if (true)\n" 14090 " f ();", 14091 Space); 14092 verifyFormat("do {\n" 14093 " do_something ();\n" 14094 "} while (something ());", 14095 Space); 14096 verifyFormat("switch (x) {\n" 14097 "default:\n" 14098 " break;\n" 14099 "}", 14100 Space); 14101 verifyFormat("A::A () : a (1) {}", Space); 14102 verifyFormat("void f () __attribute__ ((asdf));", Space); 14103 verifyFormat("*(&a + 1);\n" 14104 "&((&a)[1]);\n" 14105 "a[(b + c) * d];\n" 14106 "(((a + 1) * 2) + 3) * 4;", 14107 Space); 14108 verifyFormat("#define A(x) x", Space); 14109 verifyFormat("#define A (x) x", Space); 14110 verifyFormat("#if defined(x)\n" 14111 "#endif", 14112 Space); 14113 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14114 verifyFormat("size_t x = sizeof (x);", Space); 14115 verifyFormat("auto f (int x) -> decltype (x);", Space); 14116 verifyFormat("auto f (int x) -> typeof (x);", Space); 14117 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14118 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14119 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14120 verifyFormat("alignas (128) char a[128];", Space); 14121 verifyFormat("size_t x = alignof (MyType);", Space); 14122 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14123 verifyFormat("int f () throw (Deprecated);", Space); 14124 verifyFormat("typedef void (*cb) (int);", Space); 14125 // FIXME these tests regressed behaviour. 14126 // verifyFormat("T A::operator() ();", Space); 14127 // verifyFormat("X A::operator++ (T);", Space); 14128 verifyFormat("auto lambda = [] () { return 0; };", Space); 14129 verifyFormat("int x = int (y);", Space); 14130 14131 FormatStyle SomeSpace = getLLVMStyle(); 14132 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14133 14134 verifyFormat("[]() -> float {}", SomeSpace); 14135 verifyFormat("[] (auto foo) {}", SomeSpace); 14136 verifyFormat("[foo]() -> int {}", SomeSpace); 14137 verifyFormat("int f();", SomeSpace); 14138 verifyFormat("void f (int a, T b) {\n" 14139 " while (true)\n" 14140 " continue;\n" 14141 "}", 14142 SomeSpace); 14143 verifyFormat("if (true)\n" 14144 " f();\n" 14145 "else if (true)\n" 14146 " f();", 14147 SomeSpace); 14148 verifyFormat("do {\n" 14149 " do_something();\n" 14150 "} while (something());", 14151 SomeSpace); 14152 verifyFormat("switch (x) {\n" 14153 "default:\n" 14154 " break;\n" 14155 "}", 14156 SomeSpace); 14157 verifyFormat("A::A() : a (1) {}", SomeSpace); 14158 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14159 verifyFormat("*(&a + 1);\n" 14160 "&((&a)[1]);\n" 14161 "a[(b + c) * d];\n" 14162 "(((a + 1) * 2) + 3) * 4;", 14163 SomeSpace); 14164 verifyFormat("#define A(x) x", SomeSpace); 14165 verifyFormat("#define A (x) x", SomeSpace); 14166 verifyFormat("#if defined(x)\n" 14167 "#endif", 14168 SomeSpace); 14169 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14170 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14171 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14172 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14173 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14174 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14175 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14176 verifyFormat("alignas (128) char a[128];", SomeSpace); 14177 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14178 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14179 SomeSpace); 14180 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14181 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14182 verifyFormat("T A::operator()();", SomeSpace); 14183 // FIXME these tests regressed behaviour. 14184 // verifyFormat("X A::operator++ (T);", SomeSpace); 14185 verifyFormat("int x = int (y);", SomeSpace); 14186 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14187 14188 FormatStyle SpaceControlStatements = getLLVMStyle(); 14189 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14190 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 14191 14192 verifyFormat("while (true)\n" 14193 " continue;", 14194 SpaceControlStatements); 14195 verifyFormat("if (true)\n" 14196 " f();\n" 14197 "else if (true)\n" 14198 " f();", 14199 SpaceControlStatements); 14200 verifyFormat("for (;;) {\n" 14201 " do_something();\n" 14202 "}", 14203 SpaceControlStatements); 14204 verifyFormat("do {\n" 14205 " do_something();\n" 14206 "} while (something());", 14207 SpaceControlStatements); 14208 verifyFormat("switch (x) {\n" 14209 "default:\n" 14210 " break;\n" 14211 "}", 14212 SpaceControlStatements); 14213 14214 FormatStyle SpaceFuncDecl = getLLVMStyle(); 14215 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14216 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 14217 14218 verifyFormat("int f ();", SpaceFuncDecl); 14219 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 14220 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 14221 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 14222 verifyFormat("#define A(x) x", SpaceFuncDecl); 14223 verifyFormat("#define A (x) x", SpaceFuncDecl); 14224 verifyFormat("#if defined(x)\n" 14225 "#endif", 14226 SpaceFuncDecl); 14227 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 14228 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 14229 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 14230 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 14231 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 14232 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 14233 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 14234 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 14235 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 14236 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14237 SpaceFuncDecl); 14238 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 14239 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 14240 // FIXME these tests regressed behaviour. 14241 // verifyFormat("T A::operator() ();", SpaceFuncDecl); 14242 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 14243 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 14244 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 14245 verifyFormat("int x = int(y);", SpaceFuncDecl); 14246 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14247 SpaceFuncDecl); 14248 14249 FormatStyle SpaceFuncDef = getLLVMStyle(); 14250 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14251 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 14252 14253 verifyFormat("int f();", SpaceFuncDef); 14254 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 14255 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 14256 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 14257 verifyFormat("#define A(x) x", SpaceFuncDef); 14258 verifyFormat("#define A (x) x", SpaceFuncDef); 14259 verifyFormat("#if defined(x)\n" 14260 "#endif", 14261 SpaceFuncDef); 14262 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 14263 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 14264 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 14265 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 14266 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 14267 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 14268 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 14269 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 14270 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 14271 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14272 SpaceFuncDef); 14273 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 14274 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 14275 verifyFormat("T A::operator()();", SpaceFuncDef); 14276 verifyFormat("X A::operator++(T);", SpaceFuncDef); 14277 // verifyFormat("T A::operator() () {}", SpaceFuncDef); 14278 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 14279 verifyFormat("int x = int(y);", SpaceFuncDef); 14280 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14281 SpaceFuncDef); 14282 14283 FormatStyle SpaceIfMacros = getLLVMStyle(); 14284 SpaceIfMacros.IfMacros.clear(); 14285 SpaceIfMacros.IfMacros.push_back("MYIF"); 14286 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14287 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 14288 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 14289 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 14290 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 14291 14292 FormatStyle SpaceForeachMacros = getLLVMStyle(); 14293 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14294 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 14295 verifyFormat("foreach (Item *item, itemlist) {}", SpaceForeachMacros); 14296 verifyFormat("Q_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros); 14297 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros); 14298 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 14299 14300 FormatStyle SomeSpace2 = getLLVMStyle(); 14301 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14302 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 14303 verifyFormat("[]() -> float {}", SomeSpace2); 14304 verifyFormat("[] (auto foo) {}", SomeSpace2); 14305 verifyFormat("[foo]() -> int {}", SomeSpace2); 14306 verifyFormat("int f();", SomeSpace2); 14307 verifyFormat("void f (int a, T b) {\n" 14308 " while (true)\n" 14309 " continue;\n" 14310 "}", 14311 SomeSpace2); 14312 verifyFormat("if (true)\n" 14313 " f();\n" 14314 "else if (true)\n" 14315 " f();", 14316 SomeSpace2); 14317 verifyFormat("do {\n" 14318 " do_something();\n" 14319 "} while (something());", 14320 SomeSpace2); 14321 verifyFormat("switch (x) {\n" 14322 "default:\n" 14323 " break;\n" 14324 "}", 14325 SomeSpace2); 14326 verifyFormat("A::A() : a (1) {}", SomeSpace2); 14327 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 14328 verifyFormat("*(&a + 1);\n" 14329 "&((&a)[1]);\n" 14330 "a[(b + c) * d];\n" 14331 "(((a + 1) * 2) + 3) * 4;", 14332 SomeSpace2); 14333 verifyFormat("#define A(x) x", SomeSpace2); 14334 verifyFormat("#define A (x) x", SomeSpace2); 14335 verifyFormat("#if defined(x)\n" 14336 "#endif", 14337 SomeSpace2); 14338 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 14339 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 14340 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 14341 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 14342 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 14343 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 14344 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 14345 verifyFormat("alignas (128) char a[128];", SomeSpace2); 14346 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 14347 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14348 SomeSpace2); 14349 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 14350 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 14351 verifyFormat("T A::operator()();", SomeSpace2); 14352 // verifyFormat("X A::operator++ (T);", SomeSpace2); 14353 verifyFormat("int x = int (y);", SomeSpace2); 14354 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 14355 } 14356 14357 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14358 FormatStyle Spaces = getLLVMStyle(); 14359 Spaces.SpaceAfterLogicalNot = true; 14360 14361 verifyFormat("bool x = ! y", Spaces); 14362 verifyFormat("if (! isFailure())", Spaces); 14363 verifyFormat("if (! (a && b))", Spaces); 14364 verifyFormat("\"Error!\"", Spaces); 14365 verifyFormat("! ! x", Spaces); 14366 } 14367 14368 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14369 FormatStyle Spaces = getLLVMStyle(); 14370 14371 Spaces.SpacesInParentheses = true; 14372 verifyFormat("do_something( ::globalVar );", Spaces); 14373 verifyFormat("call( x, y, z );", Spaces); 14374 verifyFormat("call();", Spaces); 14375 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14376 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14377 Spaces); 14378 verifyFormat("while ( (bool)1 )\n" 14379 " continue;", 14380 Spaces); 14381 verifyFormat("for ( ;; )\n" 14382 " continue;", 14383 Spaces); 14384 verifyFormat("if ( true )\n" 14385 " f();\n" 14386 "else if ( true )\n" 14387 " f();", 14388 Spaces); 14389 verifyFormat("do {\n" 14390 " do_something( (int)i );\n" 14391 "} while ( something() );", 14392 Spaces); 14393 verifyFormat("switch ( x ) {\n" 14394 "default:\n" 14395 " break;\n" 14396 "}", 14397 Spaces); 14398 14399 Spaces.SpacesInParentheses = false; 14400 Spaces.SpacesInCStyleCastParentheses = true; 14401 verifyFormat("Type *A = ( Type * )P;", Spaces); 14402 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14403 verifyFormat("x = ( int32 )y;", Spaces); 14404 verifyFormat("int a = ( int )(2.0f);", Spaces); 14405 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14406 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14407 verifyFormat("#define x (( int )-1)", Spaces); 14408 14409 // Run the first set of tests again with: 14410 Spaces.SpacesInParentheses = false; 14411 Spaces.SpaceInEmptyParentheses = true; 14412 Spaces.SpacesInCStyleCastParentheses = true; 14413 verifyFormat("call(x, y, z);", Spaces); 14414 verifyFormat("call( );", Spaces); 14415 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14416 verifyFormat("while (( bool )1)\n" 14417 " continue;", 14418 Spaces); 14419 verifyFormat("for (;;)\n" 14420 " continue;", 14421 Spaces); 14422 verifyFormat("if (true)\n" 14423 " f( );\n" 14424 "else if (true)\n" 14425 " f( );", 14426 Spaces); 14427 verifyFormat("do {\n" 14428 " do_something(( int )i);\n" 14429 "} while (something( ));", 14430 Spaces); 14431 verifyFormat("switch (x) {\n" 14432 "default:\n" 14433 " break;\n" 14434 "}", 14435 Spaces); 14436 14437 // Run the first set of tests again with: 14438 Spaces.SpaceAfterCStyleCast = true; 14439 verifyFormat("call(x, y, z);", Spaces); 14440 verifyFormat("call( );", Spaces); 14441 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14442 verifyFormat("while (( bool ) 1)\n" 14443 " continue;", 14444 Spaces); 14445 verifyFormat("for (;;)\n" 14446 " continue;", 14447 Spaces); 14448 verifyFormat("if (true)\n" 14449 " f( );\n" 14450 "else if (true)\n" 14451 " f( );", 14452 Spaces); 14453 verifyFormat("do {\n" 14454 " do_something(( int ) i);\n" 14455 "} while (something( ));", 14456 Spaces); 14457 verifyFormat("switch (x) {\n" 14458 "default:\n" 14459 " break;\n" 14460 "}", 14461 Spaces); 14462 14463 // Run subset of tests again with: 14464 Spaces.SpacesInCStyleCastParentheses = false; 14465 Spaces.SpaceAfterCStyleCast = true; 14466 verifyFormat("while ((bool) 1)\n" 14467 " continue;", 14468 Spaces); 14469 verifyFormat("do {\n" 14470 " do_something((int) i);\n" 14471 "} while (something( ));", 14472 Spaces); 14473 14474 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14475 verifyFormat("size_t idx = (size_t) a;", Spaces); 14476 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14477 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14478 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14479 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14480 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14481 Spaces.ColumnLimit = 80; 14482 Spaces.IndentWidth = 4; 14483 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14484 verifyFormat("void foo( ) {\n" 14485 " size_t foo = (*(function))(\n" 14486 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14487 "BarrrrrrrrrrrrLong,\n" 14488 " FoooooooooLooooong);\n" 14489 "}", 14490 Spaces); 14491 Spaces.SpaceAfterCStyleCast = false; 14492 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14493 verifyFormat("size_t idx = (size_t)a;", Spaces); 14494 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14495 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14496 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14497 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14498 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14499 14500 verifyFormat("void foo( ) {\n" 14501 " size_t foo = (*(function))(\n" 14502 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14503 "BarrrrrrrrrrrrLong,\n" 14504 " FoooooooooLooooong);\n" 14505 "}", 14506 Spaces); 14507 } 14508 14509 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14510 verifyFormat("int a[5];"); 14511 verifyFormat("a[3] += 42;"); 14512 14513 FormatStyle Spaces = getLLVMStyle(); 14514 Spaces.SpacesInSquareBrackets = true; 14515 // Not lambdas. 14516 verifyFormat("int a[ 5 ];", Spaces); 14517 verifyFormat("a[ 3 ] += 42;", Spaces); 14518 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14519 verifyFormat("double &operator[](int i) { return 0; }\n" 14520 "int i;", 14521 Spaces); 14522 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14523 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14524 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14525 // Lambdas. 14526 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14527 verifyFormat("return [ i, args... ] {};", Spaces); 14528 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14529 verifyFormat("int foo = [ = ]() {};", Spaces); 14530 verifyFormat("int foo = [ & ]() {};", Spaces); 14531 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14532 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14533 } 14534 14535 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14536 FormatStyle NoSpaceStyle = getLLVMStyle(); 14537 verifyFormat("int a[5];", NoSpaceStyle); 14538 verifyFormat("a[3] += 42;", NoSpaceStyle); 14539 14540 verifyFormat("int a[1];", NoSpaceStyle); 14541 verifyFormat("int 1 [a];", NoSpaceStyle); 14542 verifyFormat("int a[1][2];", NoSpaceStyle); 14543 verifyFormat("a[7] = 5;", NoSpaceStyle); 14544 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14545 verifyFormat("f([] {})", NoSpaceStyle); 14546 14547 FormatStyle Space = getLLVMStyle(); 14548 Space.SpaceBeforeSquareBrackets = true; 14549 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14550 verifyFormat("return [i, args...] {};", Space); 14551 14552 verifyFormat("int a [5];", Space); 14553 verifyFormat("a [3] += 42;", Space); 14554 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14555 verifyFormat("double &operator[](int i) { return 0; }\n" 14556 "int i;", 14557 Space); 14558 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14559 verifyFormat("int i = a [a][a]->f();", Space); 14560 verifyFormat("int i = (*b) [a]->f();", Space); 14561 14562 verifyFormat("int a [1];", Space); 14563 verifyFormat("int 1 [a];", Space); 14564 verifyFormat("int a [1][2];", Space); 14565 verifyFormat("a [7] = 5;", Space); 14566 verifyFormat("int a = (f()) [23];", Space); 14567 verifyFormat("f([] {})", Space); 14568 } 14569 14570 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14571 verifyFormat("int a = 5;"); 14572 verifyFormat("a += 42;"); 14573 verifyFormat("a or_eq 8;"); 14574 14575 FormatStyle Spaces = getLLVMStyle(); 14576 Spaces.SpaceBeforeAssignmentOperators = false; 14577 verifyFormat("int a= 5;", Spaces); 14578 verifyFormat("a+= 42;", Spaces); 14579 verifyFormat("a or_eq 8;", Spaces); 14580 } 14581 14582 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14583 verifyFormat("class Foo : public Bar {};"); 14584 verifyFormat("Foo::Foo() : foo(1) {}"); 14585 verifyFormat("for (auto a : b) {\n}"); 14586 verifyFormat("int x = a ? b : c;"); 14587 verifyFormat("{\n" 14588 "label0:\n" 14589 " int x = 0;\n" 14590 "}"); 14591 verifyFormat("switch (x) {\n" 14592 "case 1:\n" 14593 "default:\n" 14594 "}"); 14595 verifyFormat("switch (allBraces) {\n" 14596 "case 1: {\n" 14597 " break;\n" 14598 "}\n" 14599 "case 2: {\n" 14600 " [[fallthrough]];\n" 14601 "}\n" 14602 "default: {\n" 14603 " break;\n" 14604 "}\n" 14605 "}"); 14606 14607 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14608 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14609 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14610 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14611 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14612 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14613 verifyFormat("{\n" 14614 "label1:\n" 14615 " int x = 0;\n" 14616 "}", 14617 CtorInitializerStyle); 14618 verifyFormat("switch (x) {\n" 14619 "case 1:\n" 14620 "default:\n" 14621 "}", 14622 CtorInitializerStyle); 14623 verifyFormat("switch (allBraces) {\n" 14624 "case 1: {\n" 14625 " break;\n" 14626 "}\n" 14627 "case 2: {\n" 14628 " [[fallthrough]];\n" 14629 "}\n" 14630 "default: {\n" 14631 " break;\n" 14632 "}\n" 14633 "}", 14634 CtorInitializerStyle); 14635 CtorInitializerStyle.BreakConstructorInitializers = 14636 FormatStyle::BCIS_AfterColon; 14637 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14638 " aaaaaaaaaaaaaaaa(1),\n" 14639 " bbbbbbbbbbbbbbbb(2) {}", 14640 CtorInitializerStyle); 14641 CtorInitializerStyle.BreakConstructorInitializers = 14642 FormatStyle::BCIS_BeforeComma; 14643 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14644 " : aaaaaaaaaaaaaaaa(1)\n" 14645 " , bbbbbbbbbbbbbbbb(2) {}", 14646 CtorInitializerStyle); 14647 CtorInitializerStyle.BreakConstructorInitializers = 14648 FormatStyle::BCIS_BeforeColon; 14649 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14650 " : aaaaaaaaaaaaaaaa(1),\n" 14651 " bbbbbbbbbbbbbbbb(2) {}", 14652 CtorInitializerStyle); 14653 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14654 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14655 ": aaaaaaaaaaaaaaaa(1),\n" 14656 " bbbbbbbbbbbbbbbb(2) {}", 14657 CtorInitializerStyle); 14658 14659 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14660 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14661 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14662 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14663 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14664 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14665 verifyFormat("{\n" 14666 "label2:\n" 14667 " int x = 0;\n" 14668 "}", 14669 InheritanceStyle); 14670 verifyFormat("switch (x) {\n" 14671 "case 1:\n" 14672 "default:\n" 14673 "}", 14674 InheritanceStyle); 14675 verifyFormat("switch (allBraces) {\n" 14676 "case 1: {\n" 14677 " break;\n" 14678 "}\n" 14679 "case 2: {\n" 14680 " [[fallthrough]];\n" 14681 "}\n" 14682 "default: {\n" 14683 " break;\n" 14684 "}\n" 14685 "}", 14686 InheritanceStyle); 14687 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14688 verifyFormat("class Foooooooooooooooooooooo\n" 14689 " : public aaaaaaaaaaaaaaaaaa,\n" 14690 " public bbbbbbbbbbbbbbbbbb {\n" 14691 "}", 14692 InheritanceStyle); 14693 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14694 verifyFormat("class Foooooooooooooooooooooo:\n" 14695 " public aaaaaaaaaaaaaaaaaa,\n" 14696 " public bbbbbbbbbbbbbbbbbb {\n" 14697 "}", 14698 InheritanceStyle); 14699 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14700 verifyFormat("class Foooooooooooooooooooooo\n" 14701 " : public aaaaaaaaaaaaaaaaaa\n" 14702 " , public bbbbbbbbbbbbbbbbbb {\n" 14703 "}", 14704 InheritanceStyle); 14705 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14706 verifyFormat("class Foooooooooooooooooooooo\n" 14707 " : public aaaaaaaaaaaaaaaaaa,\n" 14708 " public bbbbbbbbbbbbbbbbbb {\n" 14709 "}", 14710 InheritanceStyle); 14711 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14712 verifyFormat("class Foooooooooooooooooooooo\n" 14713 ": public aaaaaaaaaaaaaaaaaa,\n" 14714 " public bbbbbbbbbbbbbbbbbb {}", 14715 InheritanceStyle); 14716 14717 FormatStyle ForLoopStyle = getLLVMStyle(); 14718 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14719 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14720 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14721 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14722 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14723 verifyFormat("{\n" 14724 "label2:\n" 14725 " int x = 0;\n" 14726 "}", 14727 ForLoopStyle); 14728 verifyFormat("switch (x) {\n" 14729 "case 1:\n" 14730 "default:\n" 14731 "}", 14732 ForLoopStyle); 14733 verifyFormat("switch (allBraces) {\n" 14734 "case 1: {\n" 14735 " break;\n" 14736 "}\n" 14737 "case 2: {\n" 14738 " [[fallthrough]];\n" 14739 "}\n" 14740 "default: {\n" 14741 " break;\n" 14742 "}\n" 14743 "}", 14744 ForLoopStyle); 14745 14746 FormatStyle CaseStyle = getLLVMStyle(); 14747 CaseStyle.SpaceBeforeCaseColon = true; 14748 verifyFormat("class Foo : public Bar {};", CaseStyle); 14749 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14750 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14751 verifyFormat("int x = a ? b : c;", CaseStyle); 14752 verifyFormat("switch (x) {\n" 14753 "case 1 :\n" 14754 "default :\n" 14755 "}", 14756 CaseStyle); 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 CaseStyle); 14769 14770 FormatStyle NoSpaceStyle = getLLVMStyle(); 14771 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14772 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14773 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14774 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14775 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14776 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14777 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14778 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14779 verifyFormat("{\n" 14780 "label3:\n" 14781 " int x = 0;\n" 14782 "}", 14783 NoSpaceStyle); 14784 verifyFormat("switch (x) {\n" 14785 "case 1:\n" 14786 "default:\n" 14787 "}", 14788 NoSpaceStyle); 14789 verifyFormat("switch (allBraces) {\n" 14790 "case 1: {\n" 14791 " break;\n" 14792 "}\n" 14793 "case 2: {\n" 14794 " [[fallthrough]];\n" 14795 "}\n" 14796 "default: {\n" 14797 " break;\n" 14798 "}\n" 14799 "}", 14800 NoSpaceStyle); 14801 14802 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14803 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14804 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14805 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14806 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14807 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14808 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14809 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14810 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14811 verifyFormat("{\n" 14812 "label3:\n" 14813 " int x = 0;\n" 14814 "}", 14815 InvertedSpaceStyle); 14816 verifyFormat("switch (x) {\n" 14817 "case 1 :\n" 14818 "case 2 : {\n" 14819 " break;\n" 14820 "}\n" 14821 "default :\n" 14822 " break;\n" 14823 "}", 14824 InvertedSpaceStyle); 14825 verifyFormat("switch (allBraces) {\n" 14826 "case 1 : {\n" 14827 " break;\n" 14828 "}\n" 14829 "case 2 : {\n" 14830 " [[fallthrough]];\n" 14831 "}\n" 14832 "default : {\n" 14833 " break;\n" 14834 "}\n" 14835 "}", 14836 InvertedSpaceStyle); 14837 } 14838 14839 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 14840 FormatStyle Style = getLLVMStyle(); 14841 14842 Style.PointerAlignment = FormatStyle::PAS_Left; 14843 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14844 verifyFormat("void* const* x = NULL;", Style); 14845 14846 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 14847 do { \ 14848 Style.PointerAlignment = FormatStyle::Pointers; \ 14849 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 14850 verifyFormat(Code, Style); \ 14851 } while (false) 14852 14853 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 14854 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 14855 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 14856 14857 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 14858 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 14859 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 14860 14861 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 14862 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 14863 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 14864 14865 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 14866 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 14867 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 14868 14869 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 14870 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14871 SAPQ_Default); 14872 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14873 SAPQ_Default); 14874 14875 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 14876 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14877 SAPQ_Before); 14878 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14879 SAPQ_Before); 14880 14881 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 14882 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 14883 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14884 SAPQ_After); 14885 14886 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 14887 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 14888 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 14889 14890 #undef verifyQualifierSpaces 14891 14892 FormatStyle Spaces = getLLVMStyle(); 14893 Spaces.AttributeMacros.push_back("qualified"); 14894 Spaces.PointerAlignment = FormatStyle::PAS_Right; 14895 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14896 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 14897 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 14898 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 14899 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 14900 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14901 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14902 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 14903 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 14904 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14905 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14906 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14907 14908 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 14909 Spaces.PointerAlignment = FormatStyle::PAS_Left; 14910 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14911 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 14912 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 14913 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 14914 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 14915 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14916 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 14917 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14918 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 14919 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 14920 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 14921 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 14922 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14923 14924 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 14925 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 14926 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14927 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 14928 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 14929 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14930 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14931 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14932 } 14933 14934 TEST_F(FormatTest, AlignConsecutiveMacros) { 14935 FormatStyle Style = getLLVMStyle(); 14936 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14937 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14938 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14939 14940 verifyFormat("#define a 3\n" 14941 "#define bbbb 4\n" 14942 "#define ccc (5)", 14943 Style); 14944 14945 verifyFormat("#define f(x) (x * x)\n" 14946 "#define fff(x, y, z) (x * y + z)\n" 14947 "#define ffff(x, y) (x - y)", 14948 Style); 14949 14950 verifyFormat("#define foo(x, y) (x + y)\n" 14951 "#define bar (5, 6)(2 + 2)", 14952 Style); 14953 14954 verifyFormat("#define a 3\n" 14955 "#define bbbb 4\n" 14956 "#define ccc (5)\n" 14957 "#define f(x) (x * x)\n" 14958 "#define fff(x, y, z) (x * y + z)\n" 14959 "#define ffff(x, y) (x - y)", 14960 Style); 14961 14962 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14963 verifyFormat("#define a 3\n" 14964 "#define bbbb 4\n" 14965 "#define ccc (5)", 14966 Style); 14967 14968 verifyFormat("#define f(x) (x * x)\n" 14969 "#define fff(x, y, z) (x * y + z)\n" 14970 "#define ffff(x, y) (x - y)", 14971 Style); 14972 14973 verifyFormat("#define foo(x, y) (x + y)\n" 14974 "#define bar (5, 6)(2 + 2)", 14975 Style); 14976 14977 verifyFormat("#define a 3\n" 14978 "#define bbbb 4\n" 14979 "#define ccc (5)\n" 14980 "#define f(x) (x * x)\n" 14981 "#define fff(x, y, z) (x * y + z)\n" 14982 "#define ffff(x, y) (x - y)", 14983 Style); 14984 14985 verifyFormat("#define a 5\n" 14986 "#define foo(x, y) (x + y)\n" 14987 "#define CCC (6)\n" 14988 "auto lambda = []() {\n" 14989 " auto ii = 0;\n" 14990 " float j = 0;\n" 14991 " return 0;\n" 14992 "};\n" 14993 "int i = 0;\n" 14994 "float i2 = 0;\n" 14995 "auto v = type{\n" 14996 " i = 1, //\n" 14997 " (i = 2), //\n" 14998 " i = 3 //\n" 14999 "};", 15000 Style); 15001 15002 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15003 Style.ColumnLimit = 20; 15004 15005 verifyFormat("#define a \\\n" 15006 " \"aabbbbbbbbbbbb\"\n" 15007 "#define D \\\n" 15008 " \"aabbbbbbbbbbbb\" \\\n" 15009 " \"ccddeeeeeeeee\"\n" 15010 "#define B \\\n" 15011 " \"QQQQQQQQQQQQQ\" \\\n" 15012 " \"FFFFFFFFFFFFF\" \\\n" 15013 " \"LLLLLLLL\"\n", 15014 Style); 15015 15016 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15017 verifyFormat("#define a \\\n" 15018 " \"aabbbbbbbbbbbb\"\n" 15019 "#define D \\\n" 15020 " \"aabbbbbbbbbbbb\" \\\n" 15021 " \"ccddeeeeeeeee\"\n" 15022 "#define B \\\n" 15023 " \"QQQQQQQQQQQQQ\" \\\n" 15024 " \"FFFFFFFFFFFFF\" \\\n" 15025 " \"LLLLLLLL\"\n", 15026 Style); 15027 15028 // Test across comments 15029 Style.MaxEmptyLinesToKeep = 10; 15030 Style.ReflowComments = false; 15031 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 15032 EXPECT_EQ("#define a 3\n" 15033 "// line comment\n" 15034 "#define bbbb 4\n" 15035 "#define ccc (5)", 15036 format("#define a 3\n" 15037 "// line comment\n" 15038 "#define bbbb 4\n" 15039 "#define ccc (5)", 15040 Style)); 15041 15042 EXPECT_EQ("#define a 3\n" 15043 "/* block comment */\n" 15044 "#define bbbb 4\n" 15045 "#define ccc (5)", 15046 format("#define a 3\n" 15047 "/* block comment */\n" 15048 "#define bbbb 4\n" 15049 "#define ccc (5)", 15050 Style)); 15051 15052 EXPECT_EQ("#define a 3\n" 15053 "/* multi-line *\n" 15054 " * block comment */\n" 15055 "#define bbbb 4\n" 15056 "#define ccc (5)", 15057 format("#define a 3\n" 15058 "/* multi-line *\n" 15059 " * block comment */\n" 15060 "#define bbbb 4\n" 15061 "#define ccc (5)", 15062 Style)); 15063 15064 EXPECT_EQ("#define a 3\n" 15065 "// multi-line line comment\n" 15066 "//\n" 15067 "#define bbbb 4\n" 15068 "#define ccc (5)", 15069 format("#define a 3\n" 15070 "// multi-line line comment\n" 15071 "//\n" 15072 "#define bbbb 4\n" 15073 "#define ccc (5)", 15074 Style)); 15075 15076 EXPECT_EQ("#define a 3\n" 15077 "// empty lines still break.\n" 15078 "\n" 15079 "#define bbbb 4\n" 15080 "#define ccc (5)", 15081 format("#define a 3\n" 15082 "// empty lines still break.\n" 15083 "\n" 15084 "#define bbbb 4\n" 15085 "#define ccc (5)", 15086 Style)); 15087 15088 // Test across empty lines 15089 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 15090 EXPECT_EQ("#define a 3\n" 15091 "\n" 15092 "#define bbbb 4\n" 15093 "#define ccc (5)", 15094 format("#define a 3\n" 15095 "\n" 15096 "#define bbbb 4\n" 15097 "#define ccc (5)", 15098 Style)); 15099 15100 EXPECT_EQ("#define a 3\n" 15101 "\n" 15102 "\n" 15103 "\n" 15104 "#define bbbb 4\n" 15105 "#define ccc (5)", 15106 format("#define a 3\n" 15107 "\n" 15108 "\n" 15109 "\n" 15110 "#define bbbb 4\n" 15111 "#define ccc (5)", 15112 Style)); 15113 15114 EXPECT_EQ("#define a 3\n" 15115 "// comments should break alignment\n" 15116 "//\n" 15117 "#define bbbb 4\n" 15118 "#define ccc (5)", 15119 format("#define a 3\n" 15120 "// comments should break alignment\n" 15121 "//\n" 15122 "#define bbbb 4\n" 15123 "#define ccc (5)", 15124 Style)); 15125 15126 // Test across empty lines and comments 15127 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 15128 verifyFormat("#define a 3\n" 15129 "\n" 15130 "// line comment\n" 15131 "#define bbbb 4\n" 15132 "#define ccc (5)", 15133 Style); 15134 15135 EXPECT_EQ("#define a 3\n" 15136 "\n" 15137 "\n" 15138 "/* multi-line *\n" 15139 " * block comment */\n" 15140 "\n" 15141 "\n" 15142 "#define bbbb 4\n" 15143 "#define ccc (5)", 15144 format("#define a 3\n" 15145 "\n" 15146 "\n" 15147 "/* multi-line *\n" 15148 " * block comment */\n" 15149 "\n" 15150 "\n" 15151 "#define bbbb 4\n" 15152 "#define ccc (5)", 15153 Style)); 15154 15155 EXPECT_EQ("#define a 3\n" 15156 "\n" 15157 "\n" 15158 "/* multi-line *\n" 15159 " * block comment */\n" 15160 "\n" 15161 "\n" 15162 "#define bbbb 4\n" 15163 "#define ccc (5)", 15164 format("#define a 3\n" 15165 "\n" 15166 "\n" 15167 "/* multi-line *\n" 15168 " * block comment */\n" 15169 "\n" 15170 "\n" 15171 "#define bbbb 4\n" 15172 "#define ccc (5)", 15173 Style)); 15174 } 15175 15176 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 15177 FormatStyle Alignment = getLLVMStyle(); 15178 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15179 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 15180 15181 Alignment.MaxEmptyLinesToKeep = 10; 15182 /* Test alignment across empty lines */ 15183 EXPECT_EQ("int a = 5;\n" 15184 "\n" 15185 "int oneTwoThree = 123;", 15186 format("int a = 5;\n" 15187 "\n" 15188 "int oneTwoThree= 123;", 15189 Alignment)); 15190 EXPECT_EQ("int a = 5;\n" 15191 "int one = 1;\n" 15192 "\n" 15193 "int oneTwoThree = 123;", 15194 format("int a = 5;\n" 15195 "int one = 1;\n" 15196 "\n" 15197 "int oneTwoThree = 123;", 15198 Alignment)); 15199 EXPECT_EQ("int a = 5;\n" 15200 "int one = 1;\n" 15201 "\n" 15202 "int oneTwoThree = 123;\n" 15203 "int oneTwo = 12;", 15204 format("int a = 5;\n" 15205 "int one = 1;\n" 15206 "\n" 15207 "int oneTwoThree = 123;\n" 15208 "int oneTwo = 12;", 15209 Alignment)); 15210 15211 /* Test across comments */ 15212 EXPECT_EQ("int a = 5;\n" 15213 "/* block comment */\n" 15214 "int oneTwoThree = 123;", 15215 format("int a = 5;\n" 15216 "/* block comment */\n" 15217 "int oneTwoThree=123;", 15218 Alignment)); 15219 15220 EXPECT_EQ("int a = 5;\n" 15221 "// line comment\n" 15222 "int oneTwoThree = 123;", 15223 format("int a = 5;\n" 15224 "// line comment\n" 15225 "int oneTwoThree=123;", 15226 Alignment)); 15227 15228 /* Test across comments and newlines */ 15229 EXPECT_EQ("int a = 5;\n" 15230 "\n" 15231 "/* block comment */\n" 15232 "int oneTwoThree = 123;", 15233 format("int a = 5;\n" 15234 "\n" 15235 "/* block comment */\n" 15236 "int oneTwoThree=123;", 15237 Alignment)); 15238 15239 EXPECT_EQ("int a = 5;\n" 15240 "\n" 15241 "// line comment\n" 15242 "int oneTwoThree = 123;", 15243 format("int a = 5;\n" 15244 "\n" 15245 "// line comment\n" 15246 "int oneTwoThree=123;", 15247 Alignment)); 15248 } 15249 15250 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15251 FormatStyle Alignment = getLLVMStyle(); 15252 Alignment.AlignConsecutiveDeclarations = 15253 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15254 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15255 15256 Alignment.MaxEmptyLinesToKeep = 10; 15257 /* Test alignment across empty lines */ 15258 EXPECT_EQ("int a = 5;\n" 15259 "\n" 15260 "float const oneTwoThree = 123;", 15261 format("int a = 5;\n" 15262 "\n" 15263 "float const oneTwoThree = 123;", 15264 Alignment)); 15265 EXPECT_EQ("int a = 5;\n" 15266 "float const one = 1;\n" 15267 "\n" 15268 "int oneTwoThree = 123;", 15269 format("int a = 5;\n" 15270 "float const one = 1;\n" 15271 "\n" 15272 "int oneTwoThree = 123;", 15273 Alignment)); 15274 15275 /* Test across comments */ 15276 EXPECT_EQ("float const a = 5;\n" 15277 "/* block comment */\n" 15278 "int oneTwoThree = 123;", 15279 format("float const a = 5;\n" 15280 "/* block comment */\n" 15281 "int oneTwoThree=123;", 15282 Alignment)); 15283 15284 EXPECT_EQ("float const a = 5;\n" 15285 "// line comment\n" 15286 "int oneTwoThree = 123;", 15287 format("float const a = 5;\n" 15288 "// line comment\n" 15289 "int oneTwoThree=123;", 15290 Alignment)); 15291 15292 /* Test across comments and newlines */ 15293 EXPECT_EQ("float const a = 5;\n" 15294 "\n" 15295 "/* block comment */\n" 15296 "int oneTwoThree = 123;", 15297 format("float const a = 5;\n" 15298 "\n" 15299 "/* block comment */\n" 15300 "int oneTwoThree=123;", 15301 Alignment)); 15302 15303 EXPECT_EQ("float const a = 5;\n" 15304 "\n" 15305 "// line comment\n" 15306 "int oneTwoThree = 123;", 15307 format("float const a = 5;\n" 15308 "\n" 15309 "// line comment\n" 15310 "int oneTwoThree=123;", 15311 Alignment)); 15312 } 15313 15314 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15315 FormatStyle Alignment = getLLVMStyle(); 15316 Alignment.AlignConsecutiveBitFields = 15317 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15318 15319 Alignment.MaxEmptyLinesToKeep = 10; 15320 /* Test alignment across empty lines */ 15321 EXPECT_EQ("int a : 5;\n" 15322 "\n" 15323 "int longbitfield : 6;", 15324 format("int a : 5;\n" 15325 "\n" 15326 "int longbitfield : 6;", 15327 Alignment)); 15328 EXPECT_EQ("int a : 5;\n" 15329 "int one : 1;\n" 15330 "\n" 15331 "int longbitfield : 6;", 15332 format("int a : 5;\n" 15333 "int one : 1;\n" 15334 "\n" 15335 "int longbitfield : 6;", 15336 Alignment)); 15337 15338 /* Test across comments */ 15339 EXPECT_EQ("int a : 5;\n" 15340 "/* block comment */\n" 15341 "int longbitfield : 6;", 15342 format("int a : 5;\n" 15343 "/* block comment */\n" 15344 "int longbitfield : 6;", 15345 Alignment)); 15346 EXPECT_EQ("int a : 5;\n" 15347 "int one : 1;\n" 15348 "// line comment\n" 15349 "int longbitfield : 6;", 15350 format("int a : 5;\n" 15351 "int one : 1;\n" 15352 "// line comment\n" 15353 "int longbitfield : 6;", 15354 Alignment)); 15355 15356 /* Test across comments and newlines */ 15357 EXPECT_EQ("int a : 5;\n" 15358 "/* block comment */\n" 15359 "\n" 15360 "int longbitfield : 6;", 15361 format("int a : 5;\n" 15362 "/* block comment */\n" 15363 "\n" 15364 "int longbitfield : 6;", 15365 Alignment)); 15366 EXPECT_EQ("int a : 5;\n" 15367 "int one : 1;\n" 15368 "\n" 15369 "// line comment\n" 15370 "\n" 15371 "int longbitfield : 6;", 15372 format("int a : 5;\n" 15373 "int one : 1;\n" 15374 "\n" 15375 "// line comment \n" 15376 "\n" 15377 "int longbitfield : 6;", 15378 Alignment)); 15379 } 15380 15381 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15382 FormatStyle Alignment = getLLVMStyle(); 15383 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15384 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15385 15386 Alignment.MaxEmptyLinesToKeep = 10; 15387 /* Test alignment across empty lines */ 15388 EXPECT_EQ("int a = 5;\n" 15389 "\n" 15390 "int oneTwoThree = 123;", 15391 format("int a = 5;\n" 15392 "\n" 15393 "int oneTwoThree= 123;", 15394 Alignment)); 15395 EXPECT_EQ("int a = 5;\n" 15396 "int one = 1;\n" 15397 "\n" 15398 "int oneTwoThree = 123;", 15399 format("int a = 5;\n" 15400 "int one = 1;\n" 15401 "\n" 15402 "int oneTwoThree = 123;", 15403 Alignment)); 15404 15405 /* Test across comments */ 15406 EXPECT_EQ("int a = 5;\n" 15407 "/* block comment */\n" 15408 "int oneTwoThree = 123;", 15409 format("int a = 5;\n" 15410 "/* block comment */\n" 15411 "int oneTwoThree=123;", 15412 Alignment)); 15413 15414 EXPECT_EQ("int a = 5;\n" 15415 "// line comment\n" 15416 "int oneTwoThree = 123;", 15417 format("int a = 5;\n" 15418 "// line comment\n" 15419 "int oneTwoThree=123;", 15420 Alignment)); 15421 15422 EXPECT_EQ("int a = 5;\n" 15423 "/*\n" 15424 " * multi-line block comment\n" 15425 " */\n" 15426 "int oneTwoThree = 123;", 15427 format("int a = 5;\n" 15428 "/*\n" 15429 " * multi-line block comment\n" 15430 " */\n" 15431 "int oneTwoThree=123;", 15432 Alignment)); 15433 15434 EXPECT_EQ("int a = 5;\n" 15435 "//\n" 15436 "// multi-line line comment\n" 15437 "//\n" 15438 "int oneTwoThree = 123;", 15439 format("int a = 5;\n" 15440 "//\n" 15441 "// multi-line line comment\n" 15442 "//\n" 15443 "int oneTwoThree=123;", 15444 Alignment)); 15445 15446 /* Test across comments and newlines */ 15447 EXPECT_EQ("int a = 5;\n" 15448 "\n" 15449 "/* block comment */\n" 15450 "int oneTwoThree = 123;", 15451 format("int a = 5;\n" 15452 "\n" 15453 "/* block comment */\n" 15454 "int oneTwoThree=123;", 15455 Alignment)); 15456 15457 EXPECT_EQ("int a = 5;\n" 15458 "\n" 15459 "// line comment\n" 15460 "int oneTwoThree = 123;", 15461 format("int a = 5;\n" 15462 "\n" 15463 "// line comment\n" 15464 "int oneTwoThree=123;", 15465 Alignment)); 15466 } 15467 15468 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15469 FormatStyle Alignment = getLLVMStyle(); 15470 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15471 Alignment.AlignConsecutiveAssignments = 15472 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15473 verifyFormat("int a = 5;\n" 15474 "int oneTwoThree = 123;", 15475 Alignment); 15476 verifyFormat("int a = method();\n" 15477 "int oneTwoThree = 133;", 15478 Alignment); 15479 verifyFormat("a &= 5;\n" 15480 "bcd *= 5;\n" 15481 "ghtyf += 5;\n" 15482 "dvfvdb -= 5;\n" 15483 "a /= 5;\n" 15484 "vdsvsv %= 5;\n" 15485 "sfdbddfbdfbb ^= 5;\n" 15486 "dvsdsv |= 5;\n" 15487 "int dsvvdvsdvvv = 123;", 15488 Alignment); 15489 verifyFormat("int i = 1, j = 10;\n" 15490 "something = 2000;", 15491 Alignment); 15492 verifyFormat("something = 2000;\n" 15493 "int i = 1, j = 10;\n", 15494 Alignment); 15495 verifyFormat("something = 2000;\n" 15496 "another = 911;\n" 15497 "int i = 1, j = 10;\n" 15498 "oneMore = 1;\n" 15499 "i = 2;", 15500 Alignment); 15501 verifyFormat("int a = 5;\n" 15502 "int one = 1;\n" 15503 "method();\n" 15504 "int oneTwoThree = 123;\n" 15505 "int oneTwo = 12;", 15506 Alignment); 15507 verifyFormat("int oneTwoThree = 123;\n" 15508 "int oneTwo = 12;\n" 15509 "method();\n", 15510 Alignment); 15511 verifyFormat("int oneTwoThree = 123; // comment\n" 15512 "int oneTwo = 12; // comment", 15513 Alignment); 15514 15515 // Bug 25167 15516 /* Uncomment when fixed 15517 verifyFormat("#if A\n" 15518 "#else\n" 15519 "int aaaaaaaa = 12;\n" 15520 "#endif\n" 15521 "#if B\n" 15522 "#else\n" 15523 "int a = 12;\n" 15524 "#endif\n", 15525 Alignment); 15526 verifyFormat("enum foo {\n" 15527 "#if A\n" 15528 "#else\n" 15529 " aaaaaaaa = 12;\n" 15530 "#endif\n" 15531 "#if B\n" 15532 "#else\n" 15533 " a = 12;\n" 15534 "#endif\n" 15535 "};\n", 15536 Alignment); 15537 */ 15538 15539 Alignment.MaxEmptyLinesToKeep = 10; 15540 /* Test alignment across empty lines */ 15541 EXPECT_EQ("int a = 5;\n" 15542 "\n" 15543 "int oneTwoThree = 123;", 15544 format("int a = 5;\n" 15545 "\n" 15546 "int oneTwoThree= 123;", 15547 Alignment)); 15548 EXPECT_EQ("int a = 5;\n" 15549 "int one = 1;\n" 15550 "\n" 15551 "int oneTwoThree = 123;", 15552 format("int a = 5;\n" 15553 "int one = 1;\n" 15554 "\n" 15555 "int oneTwoThree = 123;", 15556 Alignment)); 15557 EXPECT_EQ("int a = 5;\n" 15558 "int one = 1;\n" 15559 "\n" 15560 "int oneTwoThree = 123;\n" 15561 "int oneTwo = 12;", 15562 format("int a = 5;\n" 15563 "int one = 1;\n" 15564 "\n" 15565 "int oneTwoThree = 123;\n" 15566 "int oneTwo = 12;", 15567 Alignment)); 15568 15569 /* Test across comments */ 15570 EXPECT_EQ("int a = 5;\n" 15571 "/* block comment */\n" 15572 "int oneTwoThree = 123;", 15573 format("int a = 5;\n" 15574 "/* block comment */\n" 15575 "int oneTwoThree=123;", 15576 Alignment)); 15577 15578 EXPECT_EQ("int a = 5;\n" 15579 "// line comment\n" 15580 "int oneTwoThree = 123;", 15581 format("int a = 5;\n" 15582 "// line comment\n" 15583 "int oneTwoThree=123;", 15584 Alignment)); 15585 15586 /* Test across comments and newlines */ 15587 EXPECT_EQ("int a = 5;\n" 15588 "\n" 15589 "/* block comment */\n" 15590 "int oneTwoThree = 123;", 15591 format("int a = 5;\n" 15592 "\n" 15593 "/* block comment */\n" 15594 "int oneTwoThree=123;", 15595 Alignment)); 15596 15597 EXPECT_EQ("int a = 5;\n" 15598 "\n" 15599 "// line comment\n" 15600 "int oneTwoThree = 123;", 15601 format("int a = 5;\n" 15602 "\n" 15603 "// line comment\n" 15604 "int oneTwoThree=123;", 15605 Alignment)); 15606 15607 EXPECT_EQ("int a = 5;\n" 15608 "//\n" 15609 "// multi-line line comment\n" 15610 "//\n" 15611 "int oneTwoThree = 123;", 15612 format("int a = 5;\n" 15613 "//\n" 15614 "// multi-line line comment\n" 15615 "//\n" 15616 "int oneTwoThree=123;", 15617 Alignment)); 15618 15619 EXPECT_EQ("int a = 5;\n" 15620 "/*\n" 15621 " * multi-line block comment\n" 15622 " */\n" 15623 "int oneTwoThree = 123;", 15624 format("int a = 5;\n" 15625 "/*\n" 15626 " * multi-line block comment\n" 15627 " */\n" 15628 "int oneTwoThree=123;", 15629 Alignment)); 15630 15631 EXPECT_EQ("int a = 5;\n" 15632 "\n" 15633 "/* block comment */\n" 15634 "\n" 15635 "\n" 15636 "\n" 15637 "int oneTwoThree = 123;", 15638 format("int a = 5;\n" 15639 "\n" 15640 "/* block comment */\n" 15641 "\n" 15642 "\n" 15643 "\n" 15644 "int oneTwoThree=123;", 15645 Alignment)); 15646 15647 EXPECT_EQ("int a = 5;\n" 15648 "\n" 15649 "// line comment\n" 15650 "\n" 15651 "\n" 15652 "\n" 15653 "int oneTwoThree = 123;", 15654 format("int a = 5;\n" 15655 "\n" 15656 "// line comment\n" 15657 "\n" 15658 "\n" 15659 "\n" 15660 "int oneTwoThree=123;", 15661 Alignment)); 15662 15663 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15664 verifyFormat("#define A \\\n" 15665 " int aaaa = 12; \\\n" 15666 " int b = 23; \\\n" 15667 " int ccc = 234; \\\n" 15668 " int dddddddddd = 2345;", 15669 Alignment); 15670 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15671 verifyFormat("#define A \\\n" 15672 " int aaaa = 12; \\\n" 15673 " int b = 23; \\\n" 15674 " int ccc = 234; \\\n" 15675 " int dddddddddd = 2345;", 15676 Alignment); 15677 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15678 verifyFormat("#define A " 15679 " \\\n" 15680 " int aaaa = 12; " 15681 " \\\n" 15682 " int b = 23; " 15683 " \\\n" 15684 " int ccc = 234; " 15685 " \\\n" 15686 " int dddddddddd = 2345;", 15687 Alignment); 15688 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15689 "k = 4, int l = 5,\n" 15690 " int m = 6) {\n" 15691 " int j = 10;\n" 15692 " otherThing = 1;\n" 15693 "}", 15694 Alignment); 15695 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15696 " int i = 1;\n" 15697 " int j = 2;\n" 15698 " int big = 10000;\n" 15699 "}", 15700 Alignment); 15701 verifyFormat("class C {\n" 15702 "public:\n" 15703 " int i = 1;\n" 15704 " virtual void f() = 0;\n" 15705 "};", 15706 Alignment); 15707 verifyFormat("int i = 1;\n" 15708 "if (SomeType t = getSomething()) {\n" 15709 "}\n" 15710 "int j = 2;\n" 15711 "int big = 10000;", 15712 Alignment); 15713 verifyFormat("int j = 7;\n" 15714 "for (int k = 0; k < N; ++k) {\n" 15715 "}\n" 15716 "int j = 2;\n" 15717 "int big = 10000;\n" 15718 "}", 15719 Alignment); 15720 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15721 verifyFormat("int i = 1;\n" 15722 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15723 " = someLooooooooooooooooongFunction();\n" 15724 "int j = 2;", 15725 Alignment); 15726 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15727 verifyFormat("int i = 1;\n" 15728 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15729 " someLooooooooooooooooongFunction();\n" 15730 "int j = 2;", 15731 Alignment); 15732 15733 verifyFormat("auto lambda = []() {\n" 15734 " auto i = 0;\n" 15735 " return 0;\n" 15736 "};\n" 15737 "int i = 0;\n" 15738 "auto v = type{\n" 15739 " i = 1, //\n" 15740 " (i = 2), //\n" 15741 " i = 3 //\n" 15742 "};", 15743 Alignment); 15744 15745 verifyFormat( 15746 "int i = 1;\n" 15747 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15748 " loooooooooooooooooooooongParameterB);\n" 15749 "int j = 2;", 15750 Alignment); 15751 15752 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15753 " typename B = very_long_type_name_1,\n" 15754 " typename T_2 = very_long_type_name_2>\n" 15755 "auto foo() {}\n", 15756 Alignment); 15757 verifyFormat("int a, b = 1;\n" 15758 "int c = 2;\n" 15759 "int dd = 3;\n", 15760 Alignment); 15761 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15762 "float b[1][] = {{3.f}};\n", 15763 Alignment); 15764 verifyFormat("for (int i = 0; i < 1; i++)\n" 15765 " int x = 1;\n", 15766 Alignment); 15767 verifyFormat("for (i = 0; i < 1; i++)\n" 15768 " x = 1;\n" 15769 "y = 1;\n", 15770 Alignment); 15771 15772 Alignment.ReflowComments = true; 15773 Alignment.ColumnLimit = 50; 15774 EXPECT_EQ("int x = 0;\n" 15775 "int yy = 1; /// specificlennospace\n" 15776 "int zzz = 2;\n", 15777 format("int x = 0;\n" 15778 "int yy = 1; ///specificlennospace\n" 15779 "int zzz = 2;\n", 15780 Alignment)); 15781 } 15782 15783 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15784 FormatStyle Alignment = getLLVMStyle(); 15785 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15786 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15787 verifyFormat("int a = 5;\n" 15788 "int oneTwoThree = 123;", 15789 Alignment); 15790 verifyFormat("int a = 5;\n" 15791 "int oneTwoThree = 123;", 15792 Alignment); 15793 15794 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15795 verifyFormat("int a = 5;\n" 15796 "int oneTwoThree = 123;", 15797 Alignment); 15798 verifyFormat("int a = method();\n" 15799 "int oneTwoThree = 133;", 15800 Alignment); 15801 verifyFormat("a &= 5;\n" 15802 "bcd *= 5;\n" 15803 "ghtyf += 5;\n" 15804 "dvfvdb -= 5;\n" 15805 "a /= 5;\n" 15806 "vdsvsv %= 5;\n" 15807 "sfdbddfbdfbb ^= 5;\n" 15808 "dvsdsv |= 5;\n" 15809 "int dsvvdvsdvvv = 123;", 15810 Alignment); 15811 verifyFormat("int i = 1, j = 10;\n" 15812 "something = 2000;", 15813 Alignment); 15814 verifyFormat("something = 2000;\n" 15815 "int i = 1, j = 10;\n", 15816 Alignment); 15817 verifyFormat("something = 2000;\n" 15818 "another = 911;\n" 15819 "int i = 1, j = 10;\n" 15820 "oneMore = 1;\n" 15821 "i = 2;", 15822 Alignment); 15823 verifyFormat("int a = 5;\n" 15824 "int one = 1;\n" 15825 "method();\n" 15826 "int oneTwoThree = 123;\n" 15827 "int oneTwo = 12;", 15828 Alignment); 15829 verifyFormat("int oneTwoThree = 123;\n" 15830 "int oneTwo = 12;\n" 15831 "method();\n", 15832 Alignment); 15833 verifyFormat("int oneTwoThree = 123; // comment\n" 15834 "int oneTwo = 12; // comment", 15835 Alignment); 15836 15837 // Bug 25167 15838 /* Uncomment when fixed 15839 verifyFormat("#if A\n" 15840 "#else\n" 15841 "int aaaaaaaa = 12;\n" 15842 "#endif\n" 15843 "#if B\n" 15844 "#else\n" 15845 "int a = 12;\n" 15846 "#endif\n", 15847 Alignment); 15848 verifyFormat("enum foo {\n" 15849 "#if A\n" 15850 "#else\n" 15851 " aaaaaaaa = 12;\n" 15852 "#endif\n" 15853 "#if B\n" 15854 "#else\n" 15855 " a = 12;\n" 15856 "#endif\n" 15857 "};\n", 15858 Alignment); 15859 */ 15860 15861 EXPECT_EQ("int a = 5;\n" 15862 "\n" 15863 "int oneTwoThree = 123;", 15864 format("int a = 5;\n" 15865 "\n" 15866 "int oneTwoThree= 123;", 15867 Alignment)); 15868 EXPECT_EQ("int a = 5;\n" 15869 "int one = 1;\n" 15870 "\n" 15871 "int oneTwoThree = 123;", 15872 format("int a = 5;\n" 15873 "int one = 1;\n" 15874 "\n" 15875 "int oneTwoThree = 123;", 15876 Alignment)); 15877 EXPECT_EQ("int a = 5;\n" 15878 "int one = 1;\n" 15879 "\n" 15880 "int oneTwoThree = 123;\n" 15881 "int oneTwo = 12;", 15882 format("int a = 5;\n" 15883 "int one = 1;\n" 15884 "\n" 15885 "int oneTwoThree = 123;\n" 15886 "int oneTwo = 12;", 15887 Alignment)); 15888 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15889 verifyFormat("#define A \\\n" 15890 " int aaaa = 12; \\\n" 15891 " int b = 23; \\\n" 15892 " int ccc = 234; \\\n" 15893 " int dddddddddd = 2345;", 15894 Alignment); 15895 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15896 verifyFormat("#define A \\\n" 15897 " int aaaa = 12; \\\n" 15898 " int b = 23; \\\n" 15899 " int ccc = 234; \\\n" 15900 " int dddddddddd = 2345;", 15901 Alignment); 15902 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15903 verifyFormat("#define A " 15904 " \\\n" 15905 " int aaaa = 12; " 15906 " \\\n" 15907 " int b = 23; " 15908 " \\\n" 15909 " int ccc = 234; " 15910 " \\\n" 15911 " int dddddddddd = 2345;", 15912 Alignment); 15913 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15914 "k = 4, int l = 5,\n" 15915 " int m = 6) {\n" 15916 " int j = 10;\n" 15917 " otherThing = 1;\n" 15918 "}", 15919 Alignment); 15920 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15921 " int i = 1;\n" 15922 " int j = 2;\n" 15923 " int big = 10000;\n" 15924 "}", 15925 Alignment); 15926 verifyFormat("class C {\n" 15927 "public:\n" 15928 " int i = 1;\n" 15929 " virtual void f() = 0;\n" 15930 "};", 15931 Alignment); 15932 verifyFormat("int i = 1;\n" 15933 "if (SomeType t = getSomething()) {\n" 15934 "}\n" 15935 "int j = 2;\n" 15936 "int big = 10000;", 15937 Alignment); 15938 verifyFormat("int j = 7;\n" 15939 "for (int k = 0; k < N; ++k) {\n" 15940 "}\n" 15941 "int j = 2;\n" 15942 "int big = 10000;\n" 15943 "}", 15944 Alignment); 15945 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15946 verifyFormat("int i = 1;\n" 15947 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15948 " = someLooooooooooooooooongFunction();\n" 15949 "int j = 2;", 15950 Alignment); 15951 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15952 verifyFormat("int i = 1;\n" 15953 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15954 " someLooooooooooooooooongFunction();\n" 15955 "int j = 2;", 15956 Alignment); 15957 15958 verifyFormat("auto lambda = []() {\n" 15959 " auto i = 0;\n" 15960 " return 0;\n" 15961 "};\n" 15962 "int i = 0;\n" 15963 "auto v = type{\n" 15964 " i = 1, //\n" 15965 " (i = 2), //\n" 15966 " i = 3 //\n" 15967 "};", 15968 Alignment); 15969 15970 verifyFormat( 15971 "int i = 1;\n" 15972 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15973 " loooooooooooooooooooooongParameterB);\n" 15974 "int j = 2;", 15975 Alignment); 15976 15977 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15978 " typename B = very_long_type_name_1,\n" 15979 " typename T_2 = very_long_type_name_2>\n" 15980 "auto foo() {}\n", 15981 Alignment); 15982 verifyFormat("int a, b = 1;\n" 15983 "int c = 2;\n" 15984 "int dd = 3;\n", 15985 Alignment); 15986 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15987 "float b[1][] = {{3.f}};\n", 15988 Alignment); 15989 verifyFormat("for (int i = 0; i < 1; i++)\n" 15990 " int x = 1;\n", 15991 Alignment); 15992 verifyFormat("for (i = 0; i < 1; i++)\n" 15993 " x = 1;\n" 15994 "y = 1;\n", 15995 Alignment); 15996 15997 Alignment.ReflowComments = true; 15998 Alignment.ColumnLimit = 50; 15999 EXPECT_EQ("int x = 0;\n" 16000 "int yy = 1; /// specificlennospace\n" 16001 "int zzz = 2;\n", 16002 format("int x = 0;\n" 16003 "int yy = 1; ///specificlennospace\n" 16004 "int zzz = 2;\n", 16005 Alignment)); 16006 } 16007 16008 TEST_F(FormatTest, AlignConsecutiveBitFields) { 16009 FormatStyle Alignment = getLLVMStyle(); 16010 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 16011 verifyFormat("int const a : 5;\n" 16012 "int oneTwoThree : 23;", 16013 Alignment); 16014 16015 // Initializers are allowed starting with c++2a 16016 verifyFormat("int const a : 5 = 1;\n" 16017 "int oneTwoThree : 23 = 0;", 16018 Alignment); 16019 16020 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16021 verifyFormat("int const a : 5;\n" 16022 "int oneTwoThree : 23;", 16023 Alignment); 16024 16025 verifyFormat("int const a : 5; // comment\n" 16026 "int oneTwoThree : 23; // comment", 16027 Alignment); 16028 16029 verifyFormat("int const a : 5 = 1;\n" 16030 "int oneTwoThree : 23 = 0;", 16031 Alignment); 16032 16033 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16034 verifyFormat("int const a : 5 = 1;\n" 16035 "int oneTwoThree : 23 = 0;", 16036 Alignment); 16037 verifyFormat("int const a : 5 = {1};\n" 16038 "int oneTwoThree : 23 = 0;", 16039 Alignment); 16040 16041 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 16042 verifyFormat("int const a :5;\n" 16043 "int oneTwoThree:23;", 16044 Alignment); 16045 16046 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 16047 verifyFormat("int const a :5;\n" 16048 "int oneTwoThree :23;", 16049 Alignment); 16050 16051 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 16052 verifyFormat("int const a : 5;\n" 16053 "int oneTwoThree: 23;", 16054 Alignment); 16055 16056 // Known limitations: ':' is only recognized as a bitfield colon when 16057 // followed by a number. 16058 /* 16059 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 16060 "int a : 5;", 16061 Alignment); 16062 */ 16063 } 16064 16065 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 16066 FormatStyle Alignment = getLLVMStyle(); 16067 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16068 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16069 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16070 verifyFormat("float const a = 5;\n" 16071 "int oneTwoThree = 123;", 16072 Alignment); 16073 verifyFormat("int a = 5;\n" 16074 "float const oneTwoThree = 123;", 16075 Alignment); 16076 16077 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16078 verifyFormat("float const a = 5;\n" 16079 "int oneTwoThree = 123;", 16080 Alignment); 16081 verifyFormat("int a = method();\n" 16082 "float const oneTwoThree = 133;", 16083 Alignment); 16084 verifyFormat("int i = 1, j = 10;\n" 16085 "something = 2000;", 16086 Alignment); 16087 verifyFormat("something = 2000;\n" 16088 "int i = 1, j = 10;\n", 16089 Alignment); 16090 verifyFormat("float something = 2000;\n" 16091 "double another = 911;\n" 16092 "int i = 1, j = 10;\n" 16093 "const int *oneMore = 1;\n" 16094 "unsigned i = 2;", 16095 Alignment); 16096 verifyFormat("float a = 5;\n" 16097 "int one = 1;\n" 16098 "method();\n" 16099 "const double oneTwoThree = 123;\n" 16100 "const unsigned int oneTwo = 12;", 16101 Alignment); 16102 verifyFormat("int oneTwoThree{0}; // comment\n" 16103 "unsigned oneTwo; // comment", 16104 Alignment); 16105 verifyFormat("unsigned int *a;\n" 16106 "int *b;\n" 16107 "unsigned int Const *c;\n" 16108 "unsigned int const *d;\n" 16109 "unsigned int Const &e;\n" 16110 "unsigned int const &f;", 16111 Alignment); 16112 verifyFormat("Const unsigned int *c;\n" 16113 "const unsigned int *d;\n" 16114 "Const unsigned int &e;\n" 16115 "const unsigned int &f;\n" 16116 "const unsigned g;\n" 16117 "Const unsigned h;", 16118 Alignment); 16119 EXPECT_EQ("float const a = 5;\n" 16120 "\n" 16121 "int oneTwoThree = 123;", 16122 format("float const a = 5;\n" 16123 "\n" 16124 "int oneTwoThree= 123;", 16125 Alignment)); 16126 EXPECT_EQ("float a = 5;\n" 16127 "int one = 1;\n" 16128 "\n" 16129 "unsigned oneTwoThree = 123;", 16130 format("float a = 5;\n" 16131 "int one = 1;\n" 16132 "\n" 16133 "unsigned oneTwoThree = 123;", 16134 Alignment)); 16135 EXPECT_EQ("float a = 5;\n" 16136 "int one = 1;\n" 16137 "\n" 16138 "unsigned oneTwoThree = 123;\n" 16139 "int oneTwo = 12;", 16140 format("float a = 5;\n" 16141 "int one = 1;\n" 16142 "\n" 16143 "unsigned oneTwoThree = 123;\n" 16144 "int oneTwo = 12;", 16145 Alignment)); 16146 // Function prototype alignment 16147 verifyFormat("int a();\n" 16148 "double b();", 16149 Alignment); 16150 verifyFormat("int a(int x);\n" 16151 "double b();", 16152 Alignment); 16153 unsigned OldColumnLimit = Alignment.ColumnLimit; 16154 // We need to set ColumnLimit to zero, in order to stress nested alignments, 16155 // otherwise the function parameters will be re-flowed onto a single line. 16156 Alignment.ColumnLimit = 0; 16157 EXPECT_EQ("int a(int x,\n" 16158 " float y);\n" 16159 "double b(int x,\n" 16160 " double y);", 16161 format("int a(int x,\n" 16162 " float y);\n" 16163 "double b(int x,\n" 16164 " double y);", 16165 Alignment)); 16166 // This ensures that function parameters of function declarations are 16167 // correctly indented when their owning functions are indented. 16168 // The failure case here is for 'double y' to not be indented enough. 16169 EXPECT_EQ("double a(int x);\n" 16170 "int b(int y,\n" 16171 " double z);", 16172 format("double a(int x);\n" 16173 "int b(int y,\n" 16174 " double z);", 16175 Alignment)); 16176 // Set ColumnLimit low so that we induce wrapping immediately after 16177 // the function name and opening paren. 16178 Alignment.ColumnLimit = 13; 16179 verifyFormat("int function(\n" 16180 " int x,\n" 16181 " bool y);", 16182 Alignment); 16183 Alignment.ColumnLimit = OldColumnLimit; 16184 // Ensure function pointers don't screw up recursive alignment 16185 verifyFormat("int a(int x, void (*fp)(int y));\n" 16186 "double b();", 16187 Alignment); 16188 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16189 // Ensure recursive alignment is broken by function braces, so that the 16190 // "a = 1" does not align with subsequent assignments inside the function 16191 // body. 16192 verifyFormat("int func(int a = 1) {\n" 16193 " int b = 2;\n" 16194 " int cc = 3;\n" 16195 "}", 16196 Alignment); 16197 verifyFormat("float something = 2000;\n" 16198 "double another = 911;\n" 16199 "int i = 1, j = 10;\n" 16200 "const int *oneMore = 1;\n" 16201 "unsigned i = 2;", 16202 Alignment); 16203 verifyFormat("int oneTwoThree = {0}; // comment\n" 16204 "unsigned oneTwo = 0; // comment", 16205 Alignment); 16206 // Make sure that scope is correctly tracked, in the absence of braces 16207 verifyFormat("for (int i = 0; i < n; i++)\n" 16208 " j = i;\n" 16209 "double x = 1;\n", 16210 Alignment); 16211 verifyFormat("if (int i = 0)\n" 16212 " j = i;\n" 16213 "double x = 1;\n", 16214 Alignment); 16215 // Ensure operator[] and operator() are comprehended 16216 verifyFormat("struct test {\n" 16217 " long long int foo();\n" 16218 " int operator[](int a);\n" 16219 " double bar();\n" 16220 "};\n", 16221 Alignment); 16222 verifyFormat("struct test {\n" 16223 " long long int foo();\n" 16224 " int operator()(int a);\n" 16225 " double bar();\n" 16226 "};\n", 16227 Alignment); 16228 16229 // PAS_Right 16230 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16231 " int const i = 1;\n" 16232 " int *j = 2;\n" 16233 " int big = 10000;\n" 16234 "\n" 16235 " unsigned oneTwoThree = 123;\n" 16236 " int oneTwo = 12;\n" 16237 " method();\n" 16238 " float k = 2;\n" 16239 " int ll = 10000;\n" 16240 "}", 16241 format("void SomeFunction(int parameter= 0) {\n" 16242 " int const i= 1;\n" 16243 " int *j=2;\n" 16244 " int big = 10000;\n" 16245 "\n" 16246 "unsigned oneTwoThree =123;\n" 16247 "int oneTwo = 12;\n" 16248 " method();\n" 16249 "float k= 2;\n" 16250 "int ll=10000;\n" 16251 "}", 16252 Alignment)); 16253 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16254 " int const i = 1;\n" 16255 " int **j = 2, ***k;\n" 16256 " int &k = i;\n" 16257 " int &&l = i + j;\n" 16258 " int big = 10000;\n" 16259 "\n" 16260 " unsigned oneTwoThree = 123;\n" 16261 " int oneTwo = 12;\n" 16262 " method();\n" 16263 " float k = 2;\n" 16264 " int ll = 10000;\n" 16265 "}", 16266 format("void SomeFunction(int parameter= 0) {\n" 16267 " int const i= 1;\n" 16268 " int **j=2,***k;\n" 16269 "int &k=i;\n" 16270 "int &&l=i+j;\n" 16271 " int big = 10000;\n" 16272 "\n" 16273 "unsigned oneTwoThree =123;\n" 16274 "int oneTwo = 12;\n" 16275 " method();\n" 16276 "float k= 2;\n" 16277 "int ll=10000;\n" 16278 "}", 16279 Alignment)); 16280 // variables are aligned at their name, pointers are at the right most 16281 // position 16282 verifyFormat("int *a;\n" 16283 "int **b;\n" 16284 "int ***c;\n" 16285 "int foobar;\n", 16286 Alignment); 16287 16288 // PAS_Left 16289 FormatStyle AlignmentLeft = Alignment; 16290 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16291 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16292 " int const i = 1;\n" 16293 " int* j = 2;\n" 16294 " int big = 10000;\n" 16295 "\n" 16296 " unsigned oneTwoThree = 123;\n" 16297 " int oneTwo = 12;\n" 16298 " method();\n" 16299 " float k = 2;\n" 16300 " int ll = 10000;\n" 16301 "}", 16302 format("void SomeFunction(int parameter= 0) {\n" 16303 " int const i= 1;\n" 16304 " int *j=2;\n" 16305 " int big = 10000;\n" 16306 "\n" 16307 "unsigned oneTwoThree =123;\n" 16308 "int oneTwo = 12;\n" 16309 " method();\n" 16310 "float k= 2;\n" 16311 "int ll=10000;\n" 16312 "}", 16313 AlignmentLeft)); 16314 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16315 " int const i = 1;\n" 16316 " int** j = 2;\n" 16317 " int& k = i;\n" 16318 " int&& l = i + j;\n" 16319 " int big = 10000;\n" 16320 "\n" 16321 " unsigned oneTwoThree = 123;\n" 16322 " int oneTwo = 12;\n" 16323 " method();\n" 16324 " float k = 2;\n" 16325 " int ll = 10000;\n" 16326 "}", 16327 format("void SomeFunction(int parameter= 0) {\n" 16328 " int const i= 1;\n" 16329 " int **j=2;\n" 16330 "int &k=i;\n" 16331 "int &&l=i+j;\n" 16332 " int big = 10000;\n" 16333 "\n" 16334 "unsigned oneTwoThree =123;\n" 16335 "int oneTwo = 12;\n" 16336 " method();\n" 16337 "float k= 2;\n" 16338 "int ll=10000;\n" 16339 "}", 16340 AlignmentLeft)); 16341 // variables are aligned at their name, pointers are at the left most position 16342 verifyFormat("int* a;\n" 16343 "int** b;\n" 16344 "int*** c;\n" 16345 "int foobar;\n", 16346 AlignmentLeft); 16347 16348 // PAS_Middle 16349 FormatStyle AlignmentMiddle = Alignment; 16350 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16351 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16352 " int const i = 1;\n" 16353 " int * j = 2;\n" 16354 " int big = 10000;\n" 16355 "\n" 16356 " unsigned oneTwoThree = 123;\n" 16357 " int oneTwo = 12;\n" 16358 " method();\n" 16359 " float k = 2;\n" 16360 " int ll = 10000;\n" 16361 "}", 16362 format("void SomeFunction(int parameter= 0) {\n" 16363 " int const i= 1;\n" 16364 " int *j=2;\n" 16365 " int big = 10000;\n" 16366 "\n" 16367 "unsigned oneTwoThree =123;\n" 16368 "int oneTwo = 12;\n" 16369 " method();\n" 16370 "float k= 2;\n" 16371 "int ll=10000;\n" 16372 "}", 16373 AlignmentMiddle)); 16374 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16375 " int const i = 1;\n" 16376 " int ** j = 2, ***k;\n" 16377 " int & k = i;\n" 16378 " int && l = i + j;\n" 16379 " int big = 10000;\n" 16380 "\n" 16381 " unsigned oneTwoThree = 123;\n" 16382 " int oneTwo = 12;\n" 16383 " method();\n" 16384 " float k = 2;\n" 16385 " int ll = 10000;\n" 16386 "}", 16387 format("void SomeFunction(int parameter= 0) {\n" 16388 " int const i= 1;\n" 16389 " int **j=2,***k;\n" 16390 "int &k=i;\n" 16391 "int &&l=i+j;\n" 16392 " int big = 10000;\n" 16393 "\n" 16394 "unsigned oneTwoThree =123;\n" 16395 "int oneTwo = 12;\n" 16396 " method();\n" 16397 "float k= 2;\n" 16398 "int ll=10000;\n" 16399 "}", 16400 AlignmentMiddle)); 16401 // variables are aligned at their name, pointers are in the middle 16402 verifyFormat("int * a;\n" 16403 "int * b;\n" 16404 "int *** c;\n" 16405 "int foobar;\n", 16406 AlignmentMiddle); 16407 16408 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16409 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16410 verifyFormat("#define A \\\n" 16411 " int aaaa = 12; \\\n" 16412 " float b = 23; \\\n" 16413 " const int ccc = 234; \\\n" 16414 " unsigned dddddddddd = 2345;", 16415 Alignment); 16416 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16417 verifyFormat("#define A \\\n" 16418 " int aaaa = 12; \\\n" 16419 " float b = 23; \\\n" 16420 " const int ccc = 234; \\\n" 16421 " unsigned dddddddddd = 2345;", 16422 Alignment); 16423 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16424 Alignment.ColumnLimit = 30; 16425 verifyFormat("#define A \\\n" 16426 " int aaaa = 12; \\\n" 16427 " float b = 23; \\\n" 16428 " const int ccc = 234; \\\n" 16429 " int dddddddddd = 2345;", 16430 Alignment); 16431 Alignment.ColumnLimit = 80; 16432 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16433 "k = 4, int l = 5,\n" 16434 " int m = 6) {\n" 16435 " const int j = 10;\n" 16436 " otherThing = 1;\n" 16437 "}", 16438 Alignment); 16439 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16440 " int const i = 1;\n" 16441 " int *j = 2;\n" 16442 " int big = 10000;\n" 16443 "}", 16444 Alignment); 16445 verifyFormat("class C {\n" 16446 "public:\n" 16447 " int i = 1;\n" 16448 " virtual void f() = 0;\n" 16449 "};", 16450 Alignment); 16451 verifyFormat("float i = 1;\n" 16452 "if (SomeType t = getSomething()) {\n" 16453 "}\n" 16454 "const unsigned j = 2;\n" 16455 "int big = 10000;", 16456 Alignment); 16457 verifyFormat("float j = 7;\n" 16458 "for (int k = 0; k < N; ++k) {\n" 16459 "}\n" 16460 "unsigned j = 2;\n" 16461 "int big = 10000;\n" 16462 "}", 16463 Alignment); 16464 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16465 verifyFormat("float i = 1;\n" 16466 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16467 " = someLooooooooooooooooongFunction();\n" 16468 "int j = 2;", 16469 Alignment); 16470 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16471 verifyFormat("int i = 1;\n" 16472 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16473 " someLooooooooooooooooongFunction();\n" 16474 "int j = 2;", 16475 Alignment); 16476 16477 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16478 verifyFormat("auto lambda = []() {\n" 16479 " auto ii = 0;\n" 16480 " float j = 0;\n" 16481 " return 0;\n" 16482 "};\n" 16483 "int i = 0;\n" 16484 "float i2 = 0;\n" 16485 "auto v = type{\n" 16486 " i = 1, //\n" 16487 " (i = 2), //\n" 16488 " i = 3 //\n" 16489 "};", 16490 Alignment); 16491 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16492 16493 verifyFormat( 16494 "int i = 1;\n" 16495 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16496 " loooooooooooooooooooooongParameterB);\n" 16497 "int j = 2;", 16498 Alignment); 16499 16500 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16501 // We expect declarations and assignments to align, as long as it doesn't 16502 // exceed the column limit, starting a new alignment sequence whenever it 16503 // happens. 16504 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16505 Alignment.ColumnLimit = 30; 16506 verifyFormat("float ii = 1;\n" 16507 "unsigned j = 2;\n" 16508 "int someVerylongVariable = 1;\n" 16509 "AnotherLongType ll = 123456;\n" 16510 "VeryVeryLongType k = 2;\n" 16511 "int myvar = 1;", 16512 Alignment); 16513 Alignment.ColumnLimit = 80; 16514 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16515 16516 verifyFormat( 16517 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16518 " typename LongType, typename B>\n" 16519 "auto foo() {}\n", 16520 Alignment); 16521 verifyFormat("float a, b = 1;\n" 16522 "int c = 2;\n" 16523 "int dd = 3;\n", 16524 Alignment); 16525 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16526 "float b[1][] = {{3.f}};\n", 16527 Alignment); 16528 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16529 verifyFormat("float a, b = 1;\n" 16530 "int c = 2;\n" 16531 "int dd = 3;\n", 16532 Alignment); 16533 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16534 "float b[1][] = {{3.f}};\n", 16535 Alignment); 16536 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16537 16538 Alignment.ColumnLimit = 30; 16539 Alignment.BinPackParameters = false; 16540 verifyFormat("void foo(float a,\n" 16541 " float b,\n" 16542 " int c,\n" 16543 " uint32_t *d) {\n" 16544 " int *e = 0;\n" 16545 " float f = 0;\n" 16546 " double g = 0;\n" 16547 "}\n" 16548 "void bar(ino_t a,\n" 16549 " int b,\n" 16550 " uint32_t *c,\n" 16551 " bool d) {}\n", 16552 Alignment); 16553 Alignment.BinPackParameters = true; 16554 Alignment.ColumnLimit = 80; 16555 16556 // Bug 33507 16557 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16558 verifyFormat( 16559 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16560 " static const Version verVs2017;\n" 16561 " return true;\n" 16562 "});\n", 16563 Alignment); 16564 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16565 16566 // See llvm.org/PR35641 16567 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16568 verifyFormat("int func() { //\n" 16569 " int b;\n" 16570 " unsigned c;\n" 16571 "}", 16572 Alignment); 16573 16574 // See PR37175 16575 FormatStyle Style = getMozillaStyle(); 16576 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16577 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16578 "foo(int a);", 16579 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16580 16581 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16582 verifyFormat("unsigned int* a;\n" 16583 "int* b;\n" 16584 "unsigned int Const* c;\n" 16585 "unsigned int const* d;\n" 16586 "unsigned int Const& e;\n" 16587 "unsigned int const& f;", 16588 Alignment); 16589 verifyFormat("Const unsigned int* c;\n" 16590 "const unsigned int* d;\n" 16591 "Const unsigned int& e;\n" 16592 "const unsigned int& f;\n" 16593 "const unsigned g;\n" 16594 "Const unsigned h;", 16595 Alignment); 16596 16597 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16598 verifyFormat("unsigned int * a;\n" 16599 "int * b;\n" 16600 "unsigned int Const * c;\n" 16601 "unsigned int const * d;\n" 16602 "unsigned int Const & e;\n" 16603 "unsigned int const & f;", 16604 Alignment); 16605 verifyFormat("Const unsigned int * c;\n" 16606 "const unsigned int * d;\n" 16607 "Const unsigned int & e;\n" 16608 "const unsigned int & f;\n" 16609 "const unsigned g;\n" 16610 "Const unsigned h;", 16611 Alignment); 16612 } 16613 16614 TEST_F(FormatTest, AlignWithLineBreaks) { 16615 auto Style = getLLVMStyleWithColumns(120); 16616 16617 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16618 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16619 verifyFormat("void foo() {\n" 16620 " int myVar = 5;\n" 16621 " double x = 3.14;\n" 16622 " auto str = \"Hello \"\n" 16623 " \"World\";\n" 16624 " auto s = \"Hello \"\n" 16625 " \"Again\";\n" 16626 "}", 16627 Style); 16628 16629 // clang-format off 16630 verifyFormat("void foo() {\n" 16631 " const int capacityBefore = Entries.capacity();\n" 16632 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16633 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16634 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16635 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16636 "}", 16637 Style); 16638 // clang-format on 16639 16640 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16641 verifyFormat("void foo() {\n" 16642 " int myVar = 5;\n" 16643 " double x = 3.14;\n" 16644 " auto str = \"Hello \"\n" 16645 " \"World\";\n" 16646 " auto s = \"Hello \"\n" 16647 " \"Again\";\n" 16648 "}", 16649 Style); 16650 16651 // clang-format off 16652 verifyFormat("void foo() {\n" 16653 " const int capacityBefore = Entries.capacity();\n" 16654 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16655 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16656 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16657 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16658 "}", 16659 Style); 16660 // clang-format on 16661 16662 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16663 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16664 verifyFormat("void foo() {\n" 16665 " int myVar = 5;\n" 16666 " double x = 3.14;\n" 16667 " auto str = \"Hello \"\n" 16668 " \"World\";\n" 16669 " auto s = \"Hello \"\n" 16670 " \"Again\";\n" 16671 "}", 16672 Style); 16673 16674 // clang-format off 16675 verifyFormat("void foo() {\n" 16676 " const int capacityBefore = Entries.capacity();\n" 16677 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16678 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16679 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16680 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16681 "}", 16682 Style); 16683 // clang-format on 16684 16685 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16686 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16687 16688 verifyFormat("void foo() {\n" 16689 " int myVar = 5;\n" 16690 " double x = 3.14;\n" 16691 " auto str = \"Hello \"\n" 16692 " \"World\";\n" 16693 " auto s = \"Hello \"\n" 16694 " \"Again\";\n" 16695 "}", 16696 Style); 16697 16698 // clang-format off 16699 verifyFormat("void foo() {\n" 16700 " const int capacityBefore = Entries.capacity();\n" 16701 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16702 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16703 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16704 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16705 "}", 16706 Style); 16707 // clang-format on 16708 16709 Style = getLLVMStyleWithColumns(120); 16710 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16711 Style.ContinuationIndentWidth = 4; 16712 Style.IndentWidth = 4; 16713 16714 // clang-format off 16715 verifyFormat("void SomeFunc() {\n" 16716 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16717 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16718 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16719 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16720 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16721 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16722 "}", 16723 Style); 16724 // clang-format on 16725 16726 Style.BinPackArguments = false; 16727 16728 // clang-format off 16729 verifyFormat("void SomeFunc() {\n" 16730 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 16731 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16732 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 16733 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16734 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 16735 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16736 "}", 16737 Style); 16738 // clang-format on 16739 } 16740 16741 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16742 auto Style = getLLVMStyleWithColumns(60); 16743 16744 verifyFormat("void foo1(void) {\n" 16745 " BYTE p[1] = 1;\n" 16746 " A B = {.one_foooooooooooooooo = 2,\n" 16747 " .two_fooooooooooooo = 3,\n" 16748 " .three_fooooooooooooo = 4};\n" 16749 " BYTE payload = 2;\n" 16750 "}", 16751 Style); 16752 16753 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16754 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16755 verifyFormat("void foo2(void) {\n" 16756 " BYTE p[1] = 1;\n" 16757 " A B = {.one_foooooooooooooooo = 2,\n" 16758 " .two_fooooooooooooo = 3,\n" 16759 " .three_fooooooooooooo = 4};\n" 16760 " BYTE payload = 2;\n" 16761 "}", 16762 Style); 16763 16764 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16765 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16766 verifyFormat("void foo3(void) {\n" 16767 " BYTE p[1] = 1;\n" 16768 " A B = {.one_foooooooooooooooo = 2,\n" 16769 " .two_fooooooooooooo = 3,\n" 16770 " .three_fooooooooooooo = 4};\n" 16771 " BYTE payload = 2;\n" 16772 "}", 16773 Style); 16774 16775 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16776 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16777 verifyFormat("void foo4(void) {\n" 16778 " BYTE p[1] = 1;\n" 16779 " A B = {.one_foooooooooooooooo = 2,\n" 16780 " .two_fooooooooooooo = 3,\n" 16781 " .three_fooooooooooooo = 4};\n" 16782 " BYTE payload = 2;\n" 16783 "}", 16784 Style); 16785 } 16786 16787 TEST_F(FormatTest, LinuxBraceBreaking) { 16788 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16789 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16790 verifyFormat("namespace a\n" 16791 "{\n" 16792 "class A\n" 16793 "{\n" 16794 " void f()\n" 16795 " {\n" 16796 " if (true) {\n" 16797 " a();\n" 16798 " b();\n" 16799 " } else {\n" 16800 " a();\n" 16801 " }\n" 16802 " }\n" 16803 " void g() { return; }\n" 16804 "};\n" 16805 "struct B {\n" 16806 " int x;\n" 16807 "};\n" 16808 "} // namespace a\n", 16809 LinuxBraceStyle); 16810 verifyFormat("enum X {\n" 16811 " Y = 0,\n" 16812 "}\n", 16813 LinuxBraceStyle); 16814 verifyFormat("struct S {\n" 16815 " int Type;\n" 16816 " union {\n" 16817 " int x;\n" 16818 " double y;\n" 16819 " } Value;\n" 16820 " class C\n" 16821 " {\n" 16822 " MyFavoriteType Value;\n" 16823 " } Class;\n" 16824 "}\n", 16825 LinuxBraceStyle); 16826 } 16827 16828 TEST_F(FormatTest, MozillaBraceBreaking) { 16829 FormatStyle MozillaBraceStyle = getLLVMStyle(); 16830 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 16831 MozillaBraceStyle.FixNamespaceComments = false; 16832 verifyFormat("namespace a {\n" 16833 "class A\n" 16834 "{\n" 16835 " void f()\n" 16836 " {\n" 16837 " if (true) {\n" 16838 " a();\n" 16839 " b();\n" 16840 " }\n" 16841 " }\n" 16842 " void g() { return; }\n" 16843 "};\n" 16844 "enum E\n" 16845 "{\n" 16846 " A,\n" 16847 " // foo\n" 16848 " B,\n" 16849 " C\n" 16850 "};\n" 16851 "struct B\n" 16852 "{\n" 16853 " int x;\n" 16854 "};\n" 16855 "}\n", 16856 MozillaBraceStyle); 16857 verifyFormat("struct S\n" 16858 "{\n" 16859 " int Type;\n" 16860 " union\n" 16861 " {\n" 16862 " int x;\n" 16863 " double y;\n" 16864 " } Value;\n" 16865 " class C\n" 16866 " {\n" 16867 " MyFavoriteType Value;\n" 16868 " } Class;\n" 16869 "}\n", 16870 MozillaBraceStyle); 16871 } 16872 16873 TEST_F(FormatTest, StroustrupBraceBreaking) { 16874 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 16875 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 16876 verifyFormat("namespace a {\n" 16877 "class A {\n" 16878 " void f()\n" 16879 " {\n" 16880 " if (true) {\n" 16881 " a();\n" 16882 " b();\n" 16883 " }\n" 16884 " }\n" 16885 " void g() { return; }\n" 16886 "};\n" 16887 "struct B {\n" 16888 " int x;\n" 16889 "};\n" 16890 "} // namespace a\n", 16891 StroustrupBraceStyle); 16892 16893 verifyFormat("void foo()\n" 16894 "{\n" 16895 " if (a) {\n" 16896 " a();\n" 16897 " }\n" 16898 " else {\n" 16899 " b();\n" 16900 " }\n" 16901 "}\n", 16902 StroustrupBraceStyle); 16903 16904 verifyFormat("#ifdef _DEBUG\n" 16905 "int foo(int i = 0)\n" 16906 "#else\n" 16907 "int foo(int i = 5)\n" 16908 "#endif\n" 16909 "{\n" 16910 " return i;\n" 16911 "}", 16912 StroustrupBraceStyle); 16913 16914 verifyFormat("void foo() {}\n" 16915 "void bar()\n" 16916 "#ifdef _DEBUG\n" 16917 "{\n" 16918 " foo();\n" 16919 "}\n" 16920 "#else\n" 16921 "{\n" 16922 "}\n" 16923 "#endif", 16924 StroustrupBraceStyle); 16925 16926 verifyFormat("void foobar() { int i = 5; }\n" 16927 "#ifdef _DEBUG\n" 16928 "void bar() {}\n" 16929 "#else\n" 16930 "void bar() { foobar(); }\n" 16931 "#endif", 16932 StroustrupBraceStyle); 16933 } 16934 16935 TEST_F(FormatTest, AllmanBraceBreaking) { 16936 FormatStyle AllmanBraceStyle = getLLVMStyle(); 16937 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 16938 16939 EXPECT_EQ("namespace a\n" 16940 "{\n" 16941 "void f();\n" 16942 "void g();\n" 16943 "} // namespace a\n", 16944 format("namespace a\n" 16945 "{\n" 16946 "void f();\n" 16947 "void g();\n" 16948 "}\n", 16949 AllmanBraceStyle)); 16950 16951 verifyFormat("namespace a\n" 16952 "{\n" 16953 "class A\n" 16954 "{\n" 16955 " void f()\n" 16956 " {\n" 16957 " if (true)\n" 16958 " {\n" 16959 " a();\n" 16960 " b();\n" 16961 " }\n" 16962 " }\n" 16963 " void g() { return; }\n" 16964 "};\n" 16965 "struct B\n" 16966 "{\n" 16967 " int x;\n" 16968 "};\n" 16969 "union C\n" 16970 "{\n" 16971 "};\n" 16972 "} // namespace a", 16973 AllmanBraceStyle); 16974 16975 verifyFormat("void f()\n" 16976 "{\n" 16977 " if (true)\n" 16978 " {\n" 16979 " a();\n" 16980 " }\n" 16981 " else if (false)\n" 16982 " {\n" 16983 " b();\n" 16984 " }\n" 16985 " else\n" 16986 " {\n" 16987 " c();\n" 16988 " }\n" 16989 "}\n", 16990 AllmanBraceStyle); 16991 16992 verifyFormat("void f()\n" 16993 "{\n" 16994 " for (int i = 0; i < 10; ++i)\n" 16995 " {\n" 16996 " a();\n" 16997 " }\n" 16998 " while (false)\n" 16999 " {\n" 17000 " b();\n" 17001 " }\n" 17002 " do\n" 17003 " {\n" 17004 " c();\n" 17005 " } while (false)\n" 17006 "}\n", 17007 AllmanBraceStyle); 17008 17009 verifyFormat("void f(int a)\n" 17010 "{\n" 17011 " switch (a)\n" 17012 " {\n" 17013 " case 0:\n" 17014 " break;\n" 17015 " case 1:\n" 17016 " {\n" 17017 " break;\n" 17018 " }\n" 17019 " case 2:\n" 17020 " {\n" 17021 " }\n" 17022 " break;\n" 17023 " default:\n" 17024 " break;\n" 17025 " }\n" 17026 "}\n", 17027 AllmanBraceStyle); 17028 17029 verifyFormat("enum X\n" 17030 "{\n" 17031 " Y = 0,\n" 17032 "}\n", 17033 AllmanBraceStyle); 17034 verifyFormat("enum X\n" 17035 "{\n" 17036 " Y = 0\n" 17037 "}\n", 17038 AllmanBraceStyle); 17039 17040 verifyFormat("@interface BSApplicationController ()\n" 17041 "{\n" 17042 "@private\n" 17043 " id _extraIvar;\n" 17044 "}\n" 17045 "@end\n", 17046 AllmanBraceStyle); 17047 17048 verifyFormat("#ifdef _DEBUG\n" 17049 "int foo(int i = 0)\n" 17050 "#else\n" 17051 "int foo(int i = 5)\n" 17052 "#endif\n" 17053 "{\n" 17054 " return i;\n" 17055 "}", 17056 AllmanBraceStyle); 17057 17058 verifyFormat("void foo() {}\n" 17059 "void bar()\n" 17060 "#ifdef _DEBUG\n" 17061 "{\n" 17062 " foo();\n" 17063 "}\n" 17064 "#else\n" 17065 "{\n" 17066 "}\n" 17067 "#endif", 17068 AllmanBraceStyle); 17069 17070 verifyFormat("void foobar() { int i = 5; }\n" 17071 "#ifdef _DEBUG\n" 17072 "void bar() {}\n" 17073 "#else\n" 17074 "void bar() { foobar(); }\n" 17075 "#endif", 17076 AllmanBraceStyle); 17077 17078 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 17079 FormatStyle::SLS_All); 17080 17081 verifyFormat("[](int i) { return i + 2; };\n" 17082 "[](int i, int j)\n" 17083 "{\n" 17084 " auto x = i + j;\n" 17085 " auto y = i * j;\n" 17086 " return x ^ y;\n" 17087 "};\n" 17088 "void foo()\n" 17089 "{\n" 17090 " auto shortLambda = [](int i) { return i + 2; };\n" 17091 " auto longLambda = [](int i, int j)\n" 17092 " {\n" 17093 " auto x = i + j;\n" 17094 " auto y = i * j;\n" 17095 " return x ^ y;\n" 17096 " };\n" 17097 "}", 17098 AllmanBraceStyle); 17099 17100 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17101 17102 verifyFormat("[](int i)\n" 17103 "{\n" 17104 " return i + 2;\n" 17105 "};\n" 17106 "[](int i, int j)\n" 17107 "{\n" 17108 " auto x = i + j;\n" 17109 " auto y = i * j;\n" 17110 " return x ^ y;\n" 17111 "};\n" 17112 "void foo()\n" 17113 "{\n" 17114 " auto shortLambda = [](int i)\n" 17115 " {\n" 17116 " return i + 2;\n" 17117 " };\n" 17118 " auto longLambda = [](int i, int j)\n" 17119 " {\n" 17120 " auto x = i + j;\n" 17121 " auto y = i * j;\n" 17122 " return x ^ y;\n" 17123 " };\n" 17124 "}", 17125 AllmanBraceStyle); 17126 17127 // Reset 17128 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 17129 17130 // This shouldn't affect ObjC blocks.. 17131 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17132 " // ...\n" 17133 " int i;\n" 17134 "}];", 17135 AllmanBraceStyle); 17136 verifyFormat("void (^block)(void) = ^{\n" 17137 " // ...\n" 17138 " int i;\n" 17139 "};", 17140 AllmanBraceStyle); 17141 // .. or dict literals. 17142 verifyFormat("void f()\n" 17143 "{\n" 17144 " // ...\n" 17145 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17146 "}", 17147 AllmanBraceStyle); 17148 verifyFormat("void f()\n" 17149 "{\n" 17150 " // ...\n" 17151 " [object someMethod:@{a : @\"b\"}];\n" 17152 "}", 17153 AllmanBraceStyle); 17154 verifyFormat("int f()\n" 17155 "{ // comment\n" 17156 " return 42;\n" 17157 "}", 17158 AllmanBraceStyle); 17159 17160 AllmanBraceStyle.ColumnLimit = 19; 17161 verifyFormat("void f() { int i; }", AllmanBraceStyle); 17162 AllmanBraceStyle.ColumnLimit = 18; 17163 verifyFormat("void f()\n" 17164 "{\n" 17165 " int i;\n" 17166 "}", 17167 AllmanBraceStyle); 17168 AllmanBraceStyle.ColumnLimit = 80; 17169 17170 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 17171 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17172 FormatStyle::SIS_WithoutElse; 17173 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17174 verifyFormat("void f(bool b)\n" 17175 "{\n" 17176 " if (b)\n" 17177 " {\n" 17178 " return;\n" 17179 " }\n" 17180 "}\n", 17181 BreakBeforeBraceShortIfs); 17182 verifyFormat("void f(bool b)\n" 17183 "{\n" 17184 " if constexpr (b)\n" 17185 " {\n" 17186 " return;\n" 17187 " }\n" 17188 "}\n", 17189 BreakBeforeBraceShortIfs); 17190 verifyFormat("void f(bool b)\n" 17191 "{\n" 17192 " if CONSTEXPR (b)\n" 17193 " {\n" 17194 " return;\n" 17195 " }\n" 17196 "}\n", 17197 BreakBeforeBraceShortIfs); 17198 verifyFormat("void f(bool b)\n" 17199 "{\n" 17200 " if (b) return;\n" 17201 "}\n", 17202 BreakBeforeBraceShortIfs); 17203 verifyFormat("void f(bool b)\n" 17204 "{\n" 17205 " if constexpr (b) return;\n" 17206 "}\n", 17207 BreakBeforeBraceShortIfs); 17208 verifyFormat("void f(bool b)\n" 17209 "{\n" 17210 " if CONSTEXPR (b) return;\n" 17211 "}\n", 17212 BreakBeforeBraceShortIfs); 17213 verifyFormat("void f(bool b)\n" 17214 "{\n" 17215 " while (b)\n" 17216 " {\n" 17217 " return;\n" 17218 " }\n" 17219 "}\n", 17220 BreakBeforeBraceShortIfs); 17221 } 17222 17223 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 17224 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 17225 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 17226 17227 // Make a few changes to the style for testing purposes 17228 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 17229 FormatStyle::SFS_Empty; 17230 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17231 WhitesmithsBraceStyle.ColumnLimit = 0; 17232 17233 // FIXME: this test case can't decide whether there should be a blank line 17234 // after the ~D() line or not. It adds one if one doesn't exist in the test 17235 // and it removes the line if one exists. 17236 /* 17237 verifyFormat("class A;\n" 17238 "namespace B\n" 17239 " {\n" 17240 "class C;\n" 17241 "// Comment\n" 17242 "class D\n" 17243 " {\n" 17244 "public:\n" 17245 " D();\n" 17246 " ~D() {}\n" 17247 "private:\n" 17248 " enum E\n" 17249 " {\n" 17250 " F\n" 17251 " }\n" 17252 " };\n" 17253 " } // namespace B\n", 17254 WhitesmithsBraceStyle); 17255 */ 17256 17257 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17258 verifyFormat("namespace a\n" 17259 " {\n" 17260 "class A\n" 17261 " {\n" 17262 " void f()\n" 17263 " {\n" 17264 " if (true)\n" 17265 " {\n" 17266 " a();\n" 17267 " b();\n" 17268 " }\n" 17269 " }\n" 17270 " void g()\n" 17271 " {\n" 17272 " return;\n" 17273 " }\n" 17274 " };\n" 17275 "struct B\n" 17276 " {\n" 17277 " int x;\n" 17278 " };\n" 17279 " } // namespace a", 17280 WhitesmithsBraceStyle); 17281 17282 verifyFormat("namespace a\n" 17283 " {\n" 17284 "namespace b\n" 17285 " {\n" 17286 "class A\n" 17287 " {\n" 17288 " void f()\n" 17289 " {\n" 17290 " if (true)\n" 17291 " {\n" 17292 " a();\n" 17293 " b();\n" 17294 " }\n" 17295 " }\n" 17296 " void g()\n" 17297 " {\n" 17298 " return;\n" 17299 " }\n" 17300 " };\n" 17301 "struct B\n" 17302 " {\n" 17303 " int x;\n" 17304 " };\n" 17305 " } // namespace b\n" 17306 " } // namespace a", 17307 WhitesmithsBraceStyle); 17308 17309 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17310 verifyFormat("namespace a\n" 17311 " {\n" 17312 "namespace b\n" 17313 " {\n" 17314 " class A\n" 17315 " {\n" 17316 " void f()\n" 17317 " {\n" 17318 " if (true)\n" 17319 " {\n" 17320 " a();\n" 17321 " b();\n" 17322 " }\n" 17323 " }\n" 17324 " void g()\n" 17325 " {\n" 17326 " return;\n" 17327 " }\n" 17328 " };\n" 17329 " struct B\n" 17330 " {\n" 17331 " int x;\n" 17332 " };\n" 17333 " } // namespace b\n" 17334 " } // namespace a", 17335 WhitesmithsBraceStyle); 17336 17337 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17338 verifyFormat("namespace a\n" 17339 " {\n" 17340 " namespace b\n" 17341 " {\n" 17342 " class A\n" 17343 " {\n" 17344 " void f()\n" 17345 " {\n" 17346 " if (true)\n" 17347 " {\n" 17348 " a();\n" 17349 " b();\n" 17350 " }\n" 17351 " }\n" 17352 " void g()\n" 17353 " {\n" 17354 " return;\n" 17355 " }\n" 17356 " };\n" 17357 " struct B\n" 17358 " {\n" 17359 " int x;\n" 17360 " };\n" 17361 " } // namespace b\n" 17362 " } // namespace a", 17363 WhitesmithsBraceStyle); 17364 17365 verifyFormat("void f()\n" 17366 " {\n" 17367 " if (true)\n" 17368 " {\n" 17369 " a();\n" 17370 " }\n" 17371 " else if (false)\n" 17372 " {\n" 17373 " b();\n" 17374 " }\n" 17375 " else\n" 17376 " {\n" 17377 " c();\n" 17378 " }\n" 17379 " }\n", 17380 WhitesmithsBraceStyle); 17381 17382 verifyFormat("void f()\n" 17383 " {\n" 17384 " for (int i = 0; i < 10; ++i)\n" 17385 " {\n" 17386 " a();\n" 17387 " }\n" 17388 " while (false)\n" 17389 " {\n" 17390 " b();\n" 17391 " }\n" 17392 " do\n" 17393 " {\n" 17394 " c();\n" 17395 " } while (false)\n" 17396 " }\n", 17397 WhitesmithsBraceStyle); 17398 17399 WhitesmithsBraceStyle.IndentCaseLabels = true; 17400 verifyFormat("void switchTest1(int a)\n" 17401 " {\n" 17402 " switch (a)\n" 17403 " {\n" 17404 " case 2:\n" 17405 " {\n" 17406 " }\n" 17407 " break;\n" 17408 " }\n" 17409 " }\n", 17410 WhitesmithsBraceStyle); 17411 17412 verifyFormat("void switchTest2(int a)\n" 17413 " {\n" 17414 " switch (a)\n" 17415 " {\n" 17416 " case 0:\n" 17417 " break;\n" 17418 " case 1:\n" 17419 " {\n" 17420 " break;\n" 17421 " }\n" 17422 " case 2:\n" 17423 " {\n" 17424 " }\n" 17425 " break;\n" 17426 " default:\n" 17427 " break;\n" 17428 " }\n" 17429 " }\n", 17430 WhitesmithsBraceStyle); 17431 17432 verifyFormat("void switchTest3(int a)\n" 17433 " {\n" 17434 " switch (a)\n" 17435 " {\n" 17436 " case 0:\n" 17437 " {\n" 17438 " foo(x);\n" 17439 " }\n" 17440 " break;\n" 17441 " default:\n" 17442 " {\n" 17443 " foo(1);\n" 17444 " }\n" 17445 " break;\n" 17446 " }\n" 17447 " }\n", 17448 WhitesmithsBraceStyle); 17449 17450 WhitesmithsBraceStyle.IndentCaseLabels = false; 17451 17452 verifyFormat("void switchTest4(int a)\n" 17453 " {\n" 17454 " switch (a)\n" 17455 " {\n" 17456 " case 2:\n" 17457 " {\n" 17458 " }\n" 17459 " break;\n" 17460 " }\n" 17461 " }\n", 17462 WhitesmithsBraceStyle); 17463 17464 verifyFormat("void switchTest5(int a)\n" 17465 " {\n" 17466 " switch (a)\n" 17467 " {\n" 17468 " case 0:\n" 17469 " break;\n" 17470 " case 1:\n" 17471 " {\n" 17472 " foo();\n" 17473 " break;\n" 17474 " }\n" 17475 " case 2:\n" 17476 " {\n" 17477 " }\n" 17478 " break;\n" 17479 " default:\n" 17480 " break;\n" 17481 " }\n" 17482 " }\n", 17483 WhitesmithsBraceStyle); 17484 17485 verifyFormat("void switchTest6(int a)\n" 17486 " {\n" 17487 " switch (a)\n" 17488 " {\n" 17489 " case 0:\n" 17490 " {\n" 17491 " foo(x);\n" 17492 " }\n" 17493 " break;\n" 17494 " default:\n" 17495 " {\n" 17496 " foo(1);\n" 17497 " }\n" 17498 " break;\n" 17499 " }\n" 17500 " }\n", 17501 WhitesmithsBraceStyle); 17502 17503 verifyFormat("enum X\n" 17504 " {\n" 17505 " Y = 0, // testing\n" 17506 " }\n", 17507 WhitesmithsBraceStyle); 17508 17509 verifyFormat("enum X\n" 17510 " {\n" 17511 " Y = 0\n" 17512 " }\n", 17513 WhitesmithsBraceStyle); 17514 verifyFormat("enum X\n" 17515 " {\n" 17516 " Y = 0,\n" 17517 " Z = 1\n" 17518 " };\n", 17519 WhitesmithsBraceStyle); 17520 17521 verifyFormat("@interface BSApplicationController ()\n" 17522 " {\n" 17523 "@private\n" 17524 " id _extraIvar;\n" 17525 " }\n" 17526 "@end\n", 17527 WhitesmithsBraceStyle); 17528 17529 verifyFormat("#ifdef _DEBUG\n" 17530 "int foo(int i = 0)\n" 17531 "#else\n" 17532 "int foo(int i = 5)\n" 17533 "#endif\n" 17534 " {\n" 17535 " return i;\n" 17536 " }", 17537 WhitesmithsBraceStyle); 17538 17539 verifyFormat("void foo() {}\n" 17540 "void bar()\n" 17541 "#ifdef _DEBUG\n" 17542 " {\n" 17543 " foo();\n" 17544 " }\n" 17545 "#else\n" 17546 " {\n" 17547 " }\n" 17548 "#endif", 17549 WhitesmithsBraceStyle); 17550 17551 verifyFormat("void foobar()\n" 17552 " {\n" 17553 " int i = 5;\n" 17554 " }\n" 17555 "#ifdef _DEBUG\n" 17556 "void bar()\n" 17557 " {\n" 17558 " }\n" 17559 "#else\n" 17560 "void bar()\n" 17561 " {\n" 17562 " foobar();\n" 17563 " }\n" 17564 "#endif", 17565 WhitesmithsBraceStyle); 17566 17567 // This shouldn't affect ObjC blocks.. 17568 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17569 " // ...\n" 17570 " int i;\n" 17571 "}];", 17572 WhitesmithsBraceStyle); 17573 verifyFormat("void (^block)(void) = ^{\n" 17574 " // ...\n" 17575 " int i;\n" 17576 "};", 17577 WhitesmithsBraceStyle); 17578 // .. or dict literals. 17579 verifyFormat("void f()\n" 17580 " {\n" 17581 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17582 " }", 17583 WhitesmithsBraceStyle); 17584 17585 verifyFormat("int f()\n" 17586 " { // comment\n" 17587 " return 42;\n" 17588 " }", 17589 WhitesmithsBraceStyle); 17590 17591 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17592 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17593 FormatStyle::SIS_OnlyFirstIf; 17594 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17595 verifyFormat("void f(bool b)\n" 17596 " {\n" 17597 " if (b)\n" 17598 " {\n" 17599 " return;\n" 17600 " }\n" 17601 " }\n", 17602 BreakBeforeBraceShortIfs); 17603 verifyFormat("void f(bool b)\n" 17604 " {\n" 17605 " if (b) return;\n" 17606 " }\n", 17607 BreakBeforeBraceShortIfs); 17608 verifyFormat("void f(bool b)\n" 17609 " {\n" 17610 " while (b)\n" 17611 " {\n" 17612 " return;\n" 17613 " }\n" 17614 " }\n", 17615 BreakBeforeBraceShortIfs); 17616 } 17617 17618 TEST_F(FormatTest, GNUBraceBreaking) { 17619 FormatStyle GNUBraceStyle = getLLVMStyle(); 17620 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17621 verifyFormat("namespace a\n" 17622 "{\n" 17623 "class A\n" 17624 "{\n" 17625 " void f()\n" 17626 " {\n" 17627 " int a;\n" 17628 " {\n" 17629 " int b;\n" 17630 " }\n" 17631 " if (true)\n" 17632 " {\n" 17633 " a();\n" 17634 " b();\n" 17635 " }\n" 17636 " }\n" 17637 " void g() { return; }\n" 17638 "}\n" 17639 "} // namespace a", 17640 GNUBraceStyle); 17641 17642 verifyFormat("void f()\n" 17643 "{\n" 17644 " if (true)\n" 17645 " {\n" 17646 " a();\n" 17647 " }\n" 17648 " else if (false)\n" 17649 " {\n" 17650 " b();\n" 17651 " }\n" 17652 " else\n" 17653 " {\n" 17654 " c();\n" 17655 " }\n" 17656 "}\n", 17657 GNUBraceStyle); 17658 17659 verifyFormat("void f()\n" 17660 "{\n" 17661 " for (int i = 0; i < 10; ++i)\n" 17662 " {\n" 17663 " a();\n" 17664 " }\n" 17665 " while (false)\n" 17666 " {\n" 17667 " b();\n" 17668 " }\n" 17669 " do\n" 17670 " {\n" 17671 " c();\n" 17672 " }\n" 17673 " while (false);\n" 17674 "}\n", 17675 GNUBraceStyle); 17676 17677 verifyFormat("void f(int a)\n" 17678 "{\n" 17679 " switch (a)\n" 17680 " {\n" 17681 " case 0:\n" 17682 " break;\n" 17683 " case 1:\n" 17684 " {\n" 17685 " break;\n" 17686 " }\n" 17687 " case 2:\n" 17688 " {\n" 17689 " }\n" 17690 " break;\n" 17691 " default:\n" 17692 " break;\n" 17693 " }\n" 17694 "}\n", 17695 GNUBraceStyle); 17696 17697 verifyFormat("enum X\n" 17698 "{\n" 17699 " Y = 0,\n" 17700 "}\n", 17701 GNUBraceStyle); 17702 17703 verifyFormat("@interface BSApplicationController ()\n" 17704 "{\n" 17705 "@private\n" 17706 " id _extraIvar;\n" 17707 "}\n" 17708 "@end\n", 17709 GNUBraceStyle); 17710 17711 verifyFormat("#ifdef _DEBUG\n" 17712 "int foo(int i = 0)\n" 17713 "#else\n" 17714 "int foo(int i = 5)\n" 17715 "#endif\n" 17716 "{\n" 17717 " return i;\n" 17718 "}", 17719 GNUBraceStyle); 17720 17721 verifyFormat("void foo() {}\n" 17722 "void bar()\n" 17723 "#ifdef _DEBUG\n" 17724 "{\n" 17725 " foo();\n" 17726 "}\n" 17727 "#else\n" 17728 "{\n" 17729 "}\n" 17730 "#endif", 17731 GNUBraceStyle); 17732 17733 verifyFormat("void foobar() { int i = 5; }\n" 17734 "#ifdef _DEBUG\n" 17735 "void bar() {}\n" 17736 "#else\n" 17737 "void bar() { foobar(); }\n" 17738 "#endif", 17739 GNUBraceStyle); 17740 } 17741 17742 TEST_F(FormatTest, WebKitBraceBreaking) { 17743 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17744 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17745 WebKitBraceStyle.FixNamespaceComments = false; 17746 verifyFormat("namespace a {\n" 17747 "class A {\n" 17748 " void f()\n" 17749 " {\n" 17750 " if (true) {\n" 17751 " a();\n" 17752 " b();\n" 17753 " }\n" 17754 " }\n" 17755 " void g() { return; }\n" 17756 "};\n" 17757 "enum E {\n" 17758 " A,\n" 17759 " // foo\n" 17760 " B,\n" 17761 " C\n" 17762 "};\n" 17763 "struct B {\n" 17764 " int x;\n" 17765 "};\n" 17766 "}\n", 17767 WebKitBraceStyle); 17768 verifyFormat("struct S {\n" 17769 " int Type;\n" 17770 " union {\n" 17771 " int x;\n" 17772 " double y;\n" 17773 " } Value;\n" 17774 " class C {\n" 17775 " MyFavoriteType Value;\n" 17776 " } Class;\n" 17777 "};\n", 17778 WebKitBraceStyle); 17779 } 17780 17781 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17782 verifyFormat("void f() {\n" 17783 " try {\n" 17784 " } catch (const Exception &e) {\n" 17785 " }\n" 17786 "}\n", 17787 getLLVMStyle()); 17788 } 17789 17790 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17791 auto Style = getLLVMStyle(); 17792 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17793 Style.AlignConsecutiveAssignments = 17794 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17795 Style.AlignConsecutiveDeclarations = 17796 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17797 verifyFormat("struct test demo[] = {\n" 17798 " {56, 23, \"hello\"},\n" 17799 " {-1, 93463, \"world\"},\n" 17800 " { 7, 5, \"!!\"}\n" 17801 "};\n", 17802 Style); 17803 17804 verifyFormat("struct test demo[] = {\n" 17805 " {56, 23, \"hello\"}, // first line\n" 17806 " {-1, 93463, \"world\"}, // second line\n" 17807 " { 7, 5, \"!!\"} // third line\n" 17808 "};\n", 17809 Style); 17810 17811 verifyFormat("struct test demo[4] = {\n" 17812 " { 56, 23, 21, \"oh\"}, // first line\n" 17813 " { -1, 93463, 22, \"my\"}, // second line\n" 17814 " { 7, 5, 1, \"goodness\"} // third line\n" 17815 " {234, 5, 1, \"gracious\"} // fourth line\n" 17816 "};\n", 17817 Style); 17818 17819 verifyFormat("struct test demo[3] = {\n" 17820 " {56, 23, \"hello\"},\n" 17821 " {-1, 93463, \"world\"},\n" 17822 " { 7, 5, \"!!\"}\n" 17823 "};\n", 17824 Style); 17825 17826 verifyFormat("struct test demo[3] = {\n" 17827 " {int{56}, 23, \"hello\"},\n" 17828 " {int{-1}, 93463, \"world\"},\n" 17829 " { int{7}, 5, \"!!\"}\n" 17830 "};\n", 17831 Style); 17832 17833 verifyFormat("struct test demo[] = {\n" 17834 " {56, 23, \"hello\"},\n" 17835 " {-1, 93463, \"world\"},\n" 17836 " { 7, 5, \"!!\"},\n" 17837 "};\n", 17838 Style); 17839 17840 verifyFormat("test demo[] = {\n" 17841 " {56, 23, \"hello\"},\n" 17842 " {-1, 93463, \"world\"},\n" 17843 " { 7, 5, \"!!\"},\n" 17844 "};\n", 17845 Style); 17846 17847 verifyFormat("demo = std::array<struct test, 3>{\n" 17848 " test{56, 23, \"hello\"},\n" 17849 " test{-1, 93463, \"world\"},\n" 17850 " test{ 7, 5, \"!!\"},\n" 17851 "};\n", 17852 Style); 17853 17854 verifyFormat("test demo[] = {\n" 17855 " {56, 23, \"hello\"},\n" 17856 "#if X\n" 17857 " {-1, 93463, \"world\"},\n" 17858 "#endif\n" 17859 " { 7, 5, \"!!\"}\n" 17860 "};\n", 17861 Style); 17862 17863 verifyFormat( 17864 "test demo[] = {\n" 17865 " { 7, 23,\n" 17866 " \"hello world i am a very long line that really, in any\"\n" 17867 " \"just world, ought to be split over multiple lines\"},\n" 17868 " {-1, 93463, \"world\"},\n" 17869 " {56, 5, \"!!\"}\n" 17870 "};\n", 17871 Style); 17872 17873 verifyFormat("return GradForUnaryCwise(g, {\n" 17874 " {{\"sign\"}, \"Sign\", " 17875 " {\"x\", \"dy\"}},\n" 17876 " { {\"dx\"}, \"Mul\", {\"dy\"" 17877 ", \"sign\"}},\n" 17878 "});\n", 17879 Style); 17880 17881 Style.ColumnLimit = 0; 17882 EXPECT_EQ( 17883 "test demo[] = {\n" 17884 " {56, 23, \"hello world i am a very long line that really, " 17885 "in any just world, ought to be split over multiple lines\"},\n" 17886 " {-1, 93463, " 17887 " \"world\"},\n" 17888 " { 7, 5, " 17889 " \"!!\"},\n" 17890 "};", 17891 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17892 "that really, in any just world, ought to be split over multiple " 17893 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17894 Style)); 17895 17896 Style.ColumnLimit = 80; 17897 verifyFormat("test demo[] = {\n" 17898 " {56, 23, /* a comment */ \"hello\"},\n" 17899 " {-1, 93463, \"world\"},\n" 17900 " { 7, 5, \"!!\"}\n" 17901 "};\n", 17902 Style); 17903 17904 verifyFormat("test demo[] = {\n" 17905 " {56, 23, \"hello\"},\n" 17906 " {-1, 93463, \"world\" /* comment here */},\n" 17907 " { 7, 5, \"!!\"}\n" 17908 "};\n", 17909 Style); 17910 17911 verifyFormat("test demo[] = {\n" 17912 " {56, /* a comment */ 23, \"hello\"},\n" 17913 " {-1, 93463, \"world\"},\n" 17914 " { 7, 5, \"!!\"}\n" 17915 "};\n", 17916 Style); 17917 17918 Style.ColumnLimit = 20; 17919 EXPECT_EQ( 17920 "demo = std::array<\n" 17921 " struct test, 3>{\n" 17922 " test{\n" 17923 " 56, 23,\n" 17924 " \"hello \"\n" 17925 " \"world i \"\n" 17926 " \"am a very \"\n" 17927 " \"long line \"\n" 17928 " \"that \"\n" 17929 " \"really, \"\n" 17930 " \"in any \"\n" 17931 " \"just \"\n" 17932 " \"world, \"\n" 17933 " \"ought to \"\n" 17934 " \"be split \"\n" 17935 " \"over \"\n" 17936 " \"multiple \"\n" 17937 " \"lines\"},\n" 17938 " test{-1, 93463,\n" 17939 " \"world\"},\n" 17940 " test{ 7, 5,\n" 17941 " \"!!\" },\n" 17942 "};", 17943 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17944 "i am a very long line that really, in any just world, ought " 17945 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17946 "test{7, 5, \"!!\"},};", 17947 Style)); 17948 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17949 Style = getLLVMStyleWithColumns(50); 17950 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17951 verifyFormat("static A x = {\n" 17952 " {{init1, init2, init3, init4},\n" 17953 " {init1, init2, init3, init4}}\n" 17954 "};", 17955 Style); 17956 Style.ColumnLimit = 100; 17957 EXPECT_EQ( 17958 "test demo[] = {\n" 17959 " {56, 23,\n" 17960 " \"hello world i am a very long line that really, in any just world" 17961 ", ought to be split over \"\n" 17962 " \"multiple lines\" },\n" 17963 " {-1, 93463, \"world\"},\n" 17964 " { 7, 5, \"!!\"},\n" 17965 "};", 17966 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17967 "that really, in any just world, ought to be split over multiple " 17968 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17969 Style)); 17970 17971 Style = getLLVMStyleWithColumns(50); 17972 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17973 Style.AlignConsecutiveAssignments = 17974 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17975 Style.AlignConsecutiveDeclarations = 17976 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17977 verifyFormat("struct test demo[] = {\n" 17978 " {56, 23, \"hello\"},\n" 17979 " {-1, 93463, \"world\"},\n" 17980 " { 7, 5, \"!!\"}\n" 17981 "};\n" 17982 "static A x = {\n" 17983 " {{init1, init2, init3, init4},\n" 17984 " {init1, init2, init3, init4}}\n" 17985 "};", 17986 Style); 17987 Style.ColumnLimit = 100; 17988 Style.AlignConsecutiveAssignments = 17989 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17990 Style.AlignConsecutiveDeclarations = 17991 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17992 verifyFormat("struct test demo[] = {\n" 17993 " {56, 23, \"hello\"},\n" 17994 " {-1, 93463, \"world\"},\n" 17995 " { 7, 5, \"!!\"}\n" 17996 "};\n" 17997 "struct test demo[4] = {\n" 17998 " { 56, 23, 21, \"oh\"}, // first line\n" 17999 " { -1, 93463, 22, \"my\"}, // second line\n" 18000 " { 7, 5, 1, \"goodness\"} // third line\n" 18001 " {234, 5, 1, \"gracious\"} // fourth line\n" 18002 "};\n", 18003 Style); 18004 EXPECT_EQ( 18005 "test demo[] = {\n" 18006 " {56,\n" 18007 " \"hello world i am a very long line that really, in any just world" 18008 ", ought to be split over \"\n" 18009 " \"multiple lines\", 23},\n" 18010 " {-1, \"world\", 93463},\n" 18011 " { 7, \"!!\", 5},\n" 18012 "};", 18013 format("test demo[] = {{56, \"hello world i am a very long line " 18014 "that really, in any just world, ought to be split over multiple " 18015 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 18016 Style)); 18017 } 18018 18019 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 18020 auto Style = getLLVMStyle(); 18021 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18022 /* FIXME: This case gets misformatted. 18023 verifyFormat("auto foo = Items{\n" 18024 " Section{0, bar(), },\n" 18025 " Section{1, boo() }\n" 18026 "};\n", 18027 Style); 18028 */ 18029 verifyFormat("auto foo = Items{\n" 18030 " Section{\n" 18031 " 0, bar(),\n" 18032 " }\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("struct test demo[] = {\n" 18042 " {56, 23, \"hello\"}, // first line\n" 18043 " {-1, 93463, \"world\"}, // second line\n" 18044 " {7, 5, \"!!\" } // third line\n" 18045 "};\n", 18046 Style); 18047 verifyFormat("struct test demo[4] = {\n" 18048 " {56, 23, 21, \"oh\" }, // first line\n" 18049 " {-1, 93463, 22, \"my\" }, // second line\n" 18050 " {7, 5, 1, \"goodness\"} // third line\n" 18051 " {234, 5, 1, \"gracious\"} // fourth line\n" 18052 "};\n", 18053 Style); 18054 verifyFormat("struct test demo[3] = {\n" 18055 " {56, 23, \"hello\"},\n" 18056 " {-1, 93463, \"world\"},\n" 18057 " {7, 5, \"!!\" }\n" 18058 "};\n", 18059 Style); 18060 18061 verifyFormat("struct test demo[3] = {\n" 18062 " {int{56}, 23, \"hello\"},\n" 18063 " {int{-1}, 93463, \"world\"},\n" 18064 " {int{7}, 5, \"!!\" }\n" 18065 "};\n", 18066 Style); 18067 verifyFormat("struct test demo[] = {\n" 18068 " {56, 23, \"hello\"},\n" 18069 " {-1, 93463, \"world\"},\n" 18070 " {7, 5, \"!!\" },\n" 18071 "};\n", 18072 Style); 18073 verifyFormat("test demo[] = {\n" 18074 " {56, 23, \"hello\"},\n" 18075 " {-1, 93463, \"world\"},\n" 18076 " {7, 5, \"!!\" },\n" 18077 "};\n", 18078 Style); 18079 verifyFormat("demo = std::array<struct test, 3>{\n" 18080 " test{56, 23, \"hello\"},\n" 18081 " test{-1, 93463, \"world\"},\n" 18082 " test{7, 5, \"!!\" },\n" 18083 "};\n", 18084 Style); 18085 verifyFormat("test demo[] = {\n" 18086 " {56, 23, \"hello\"},\n" 18087 "#if X\n" 18088 " {-1, 93463, \"world\"},\n" 18089 "#endif\n" 18090 " {7, 5, \"!!\" }\n" 18091 "};\n", 18092 Style); 18093 verifyFormat( 18094 "test demo[] = {\n" 18095 " {7, 23,\n" 18096 " \"hello world i am a very long line that really, in any\"\n" 18097 " \"just world, ought to be split over multiple lines\"},\n" 18098 " {-1, 93463, \"world\" },\n" 18099 " {56, 5, \"!!\" }\n" 18100 "};\n", 18101 Style); 18102 18103 verifyFormat("return GradForUnaryCwise(g, {\n" 18104 " {{\"sign\"}, \"Sign\", {\"x\", " 18105 "\"dy\"} },\n" 18106 " {{\"dx\"}, \"Mul\", " 18107 "{\"dy\", \"sign\"}},\n" 18108 "});\n", 18109 Style); 18110 18111 Style.ColumnLimit = 0; 18112 EXPECT_EQ( 18113 "test demo[] = {\n" 18114 " {56, 23, \"hello world i am a very long line that really, in any " 18115 "just world, ought to be split over multiple lines\"},\n" 18116 " {-1, 93463, \"world\" " 18117 " },\n" 18118 " {7, 5, \"!!\" " 18119 " },\n" 18120 "};", 18121 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18122 "that really, in any just world, ought to be split over multiple " 18123 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18124 Style)); 18125 18126 Style.ColumnLimit = 80; 18127 verifyFormat("test demo[] = {\n" 18128 " {56, 23, /* a comment */ \"hello\"},\n" 18129 " {-1, 93463, \"world\" },\n" 18130 " {7, 5, \"!!\" }\n" 18131 "};\n", 18132 Style); 18133 18134 verifyFormat("test demo[] = {\n" 18135 " {56, 23, \"hello\" },\n" 18136 " {-1, 93463, \"world\" /* comment here */},\n" 18137 " {7, 5, \"!!\" }\n" 18138 "};\n", 18139 Style); 18140 18141 verifyFormat("test demo[] = {\n" 18142 " {56, /* a comment */ 23, \"hello\"},\n" 18143 " {-1, 93463, \"world\"},\n" 18144 " {7, 5, \"!!\" }\n" 18145 "};\n", 18146 Style); 18147 18148 Style.ColumnLimit = 20; 18149 EXPECT_EQ( 18150 "demo = std::array<\n" 18151 " struct test, 3>{\n" 18152 " test{\n" 18153 " 56, 23,\n" 18154 " \"hello \"\n" 18155 " \"world i \"\n" 18156 " \"am a very \"\n" 18157 " \"long line \"\n" 18158 " \"that \"\n" 18159 " \"really, \"\n" 18160 " \"in any \"\n" 18161 " \"just \"\n" 18162 " \"world, \"\n" 18163 " \"ought to \"\n" 18164 " \"be split \"\n" 18165 " \"over \"\n" 18166 " \"multiple \"\n" 18167 " \"lines\"},\n" 18168 " test{-1, 93463,\n" 18169 " \"world\"},\n" 18170 " test{7, 5,\n" 18171 " \"!!\" },\n" 18172 "};", 18173 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18174 "i am a very long line that really, in any just world, ought " 18175 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18176 "test{7, 5, \"!!\"},};", 18177 Style)); 18178 18179 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18180 Style = getLLVMStyleWithColumns(50); 18181 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18182 verifyFormat("static A x = {\n" 18183 " {{init1, init2, init3, init4},\n" 18184 " {init1, init2, init3, init4}}\n" 18185 "};", 18186 Style); 18187 Style.ColumnLimit = 100; 18188 EXPECT_EQ( 18189 "test demo[] = {\n" 18190 " {56, 23,\n" 18191 " \"hello world i am a very long line that really, in any just world" 18192 ", ought to be split over \"\n" 18193 " \"multiple lines\" },\n" 18194 " {-1, 93463, \"world\"},\n" 18195 " {7, 5, \"!!\" },\n" 18196 "};", 18197 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18198 "that really, in any just world, ought to be split over multiple " 18199 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18200 Style)); 18201 } 18202 18203 TEST_F(FormatTest, UnderstandsPragmas) { 18204 verifyFormat("#pragma omp reduction(| : var)"); 18205 verifyFormat("#pragma omp reduction(+ : var)"); 18206 18207 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 18208 "(including parentheses).", 18209 format("#pragma mark Any non-hyphenated or hyphenated string " 18210 "(including parentheses).")); 18211 } 18212 18213 TEST_F(FormatTest, UnderstandPragmaOption) { 18214 verifyFormat("#pragma option -C -A"); 18215 18216 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 18217 } 18218 18219 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 18220 FormatStyle Style = getLLVMStyle(); 18221 Style.ColumnLimit = 20; 18222 18223 // See PR41213 18224 EXPECT_EQ("/*\n" 18225 " *\t9012345\n" 18226 " * /8901\n" 18227 " */", 18228 format("/*\n" 18229 " *\t9012345 /8901\n" 18230 " */", 18231 Style)); 18232 EXPECT_EQ("/*\n" 18233 " *345678\n" 18234 " *\t/8901\n" 18235 " */", 18236 format("/*\n" 18237 " *345678\t/8901\n" 18238 " */", 18239 Style)); 18240 18241 verifyFormat("int a; // the\n" 18242 " // comment", 18243 Style); 18244 EXPECT_EQ("int a; /* first line\n" 18245 " * second\n" 18246 " * line third\n" 18247 " * line\n" 18248 " */", 18249 format("int a; /* first line\n" 18250 " * second\n" 18251 " * line third\n" 18252 " * line\n" 18253 " */", 18254 Style)); 18255 EXPECT_EQ("int a; // first line\n" 18256 " // second\n" 18257 " // line third\n" 18258 " // line", 18259 format("int a; // first line\n" 18260 " // second line\n" 18261 " // third line", 18262 Style)); 18263 18264 Style.PenaltyExcessCharacter = 90; 18265 verifyFormat("int a; // the comment", Style); 18266 EXPECT_EQ("int a; // the comment\n" 18267 " // aaa", 18268 format("int a; // the comment aaa", Style)); 18269 EXPECT_EQ("int a; /* first line\n" 18270 " * second line\n" 18271 " * third line\n" 18272 " */", 18273 format("int a; /* first line\n" 18274 " * second line\n" 18275 " * third line\n" 18276 " */", 18277 Style)); 18278 EXPECT_EQ("int a; // first line\n" 18279 " // second line\n" 18280 " // third line", 18281 format("int a; // first line\n" 18282 " // second line\n" 18283 " // third line", 18284 Style)); 18285 // FIXME: Investigate why this is not getting the same layout as the test 18286 // above. 18287 EXPECT_EQ("int a; /* first line\n" 18288 " * second line\n" 18289 " * third line\n" 18290 " */", 18291 format("int a; /* first line second line third line" 18292 "\n*/", 18293 Style)); 18294 18295 EXPECT_EQ("// foo bar baz bazfoo\n" 18296 "// foo bar foo bar\n", 18297 format("// foo bar baz bazfoo\n" 18298 "// foo bar foo bar\n", 18299 Style)); 18300 EXPECT_EQ("// foo bar baz bazfoo\n" 18301 "// foo bar foo bar\n", 18302 format("// foo bar baz bazfoo\n" 18303 "// foo bar foo bar\n", 18304 Style)); 18305 18306 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18307 // next one. 18308 EXPECT_EQ("// foo bar baz bazfoo\n" 18309 "// bar foo bar\n", 18310 format("// foo bar baz bazfoo bar\n" 18311 "// foo bar\n", 18312 Style)); 18313 18314 EXPECT_EQ("// foo bar baz bazfoo\n" 18315 "// foo bar baz bazfoo\n" 18316 "// bar foo bar\n", 18317 format("// foo bar baz bazfoo\n" 18318 "// foo bar baz bazfoo bar\n" 18319 "// foo bar\n", 18320 Style)); 18321 18322 EXPECT_EQ("// foo bar baz bazfoo\n" 18323 "// foo bar baz bazfoo\n" 18324 "// bar foo bar\n", 18325 format("// foo bar baz bazfoo\n" 18326 "// foo bar baz bazfoo bar\n" 18327 "// foo bar\n", 18328 Style)); 18329 18330 // Make sure we do not keep protruding characters if strict mode reflow is 18331 // cheaper than keeping protruding characters. 18332 Style.ColumnLimit = 21; 18333 EXPECT_EQ( 18334 "// foo foo foo foo\n" 18335 "// foo foo foo foo\n" 18336 "// foo foo foo foo\n", 18337 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18338 18339 EXPECT_EQ("int a = /* long block\n" 18340 " comment */\n" 18341 " 42;", 18342 format("int a = /* long block comment */ 42;", Style)); 18343 } 18344 18345 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18346 for (size_t i = 1; i < Styles.size(); ++i) \ 18347 EXPECT_EQ(Styles[0], Styles[i]) \ 18348 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18349 18350 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18351 SmallVector<FormatStyle, 3> Styles; 18352 Styles.resize(3); 18353 18354 Styles[0] = getLLVMStyle(); 18355 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18356 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18357 EXPECT_ALL_STYLES_EQUAL(Styles); 18358 18359 Styles[0] = getGoogleStyle(); 18360 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18361 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18362 EXPECT_ALL_STYLES_EQUAL(Styles); 18363 18364 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18365 EXPECT_TRUE( 18366 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18367 EXPECT_TRUE( 18368 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18369 EXPECT_ALL_STYLES_EQUAL(Styles); 18370 18371 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18372 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18373 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18374 EXPECT_ALL_STYLES_EQUAL(Styles); 18375 18376 Styles[0] = getMozillaStyle(); 18377 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18378 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18379 EXPECT_ALL_STYLES_EQUAL(Styles); 18380 18381 Styles[0] = getWebKitStyle(); 18382 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18383 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18384 EXPECT_ALL_STYLES_EQUAL(Styles); 18385 18386 Styles[0] = getGNUStyle(); 18387 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18388 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18389 EXPECT_ALL_STYLES_EQUAL(Styles); 18390 18391 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18392 } 18393 18394 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18395 SmallVector<FormatStyle, 8> Styles; 18396 Styles.resize(2); 18397 18398 Styles[0] = getGoogleStyle(); 18399 Styles[1] = getLLVMStyle(); 18400 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18401 EXPECT_ALL_STYLES_EQUAL(Styles); 18402 18403 Styles.resize(5); 18404 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18405 Styles[1] = getLLVMStyle(); 18406 Styles[1].Language = FormatStyle::LK_JavaScript; 18407 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18408 18409 Styles[2] = getLLVMStyle(); 18410 Styles[2].Language = FormatStyle::LK_JavaScript; 18411 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18412 "BasedOnStyle: Google", 18413 &Styles[2]) 18414 .value()); 18415 18416 Styles[3] = getLLVMStyle(); 18417 Styles[3].Language = FormatStyle::LK_JavaScript; 18418 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18419 "Language: JavaScript", 18420 &Styles[3]) 18421 .value()); 18422 18423 Styles[4] = getLLVMStyle(); 18424 Styles[4].Language = FormatStyle::LK_JavaScript; 18425 EXPECT_EQ(0, parseConfiguration("---\n" 18426 "BasedOnStyle: LLVM\n" 18427 "IndentWidth: 123\n" 18428 "---\n" 18429 "BasedOnStyle: Google\n" 18430 "Language: JavaScript", 18431 &Styles[4]) 18432 .value()); 18433 EXPECT_ALL_STYLES_EQUAL(Styles); 18434 } 18435 18436 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18437 Style.FIELD = false; \ 18438 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18439 EXPECT_TRUE(Style.FIELD); \ 18440 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18441 EXPECT_FALSE(Style.FIELD); 18442 18443 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18444 18445 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18446 Style.STRUCT.FIELD = false; \ 18447 EXPECT_EQ(0, \ 18448 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18449 .value()); \ 18450 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18451 EXPECT_EQ(0, \ 18452 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18453 .value()); \ 18454 EXPECT_FALSE(Style.STRUCT.FIELD); 18455 18456 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18457 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18458 18459 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18460 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18461 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18462 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18463 18464 TEST_F(FormatTest, ParsesConfigurationBools) { 18465 FormatStyle Style = {}; 18466 Style.Language = FormatStyle::LK_Cpp; 18467 CHECK_PARSE_BOOL(AlignTrailingComments); 18468 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18469 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18470 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18471 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18472 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18473 CHECK_PARSE_BOOL(BinPackArguments); 18474 CHECK_PARSE_BOOL(BinPackParameters); 18475 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18476 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18477 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18478 CHECK_PARSE_BOOL(BreakStringLiterals); 18479 CHECK_PARSE_BOOL(CompactNamespaces); 18480 CHECK_PARSE_BOOL(DeriveLineEnding); 18481 CHECK_PARSE_BOOL(DerivePointerAlignment); 18482 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18483 CHECK_PARSE_BOOL(DisableFormat); 18484 CHECK_PARSE_BOOL(IndentAccessModifiers); 18485 CHECK_PARSE_BOOL(IndentCaseLabels); 18486 CHECK_PARSE_BOOL(IndentCaseBlocks); 18487 CHECK_PARSE_BOOL(IndentGotoLabels); 18488 CHECK_PARSE_BOOL(IndentRequires); 18489 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18490 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18491 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18492 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18493 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18494 CHECK_PARSE_BOOL(ReflowComments); 18495 CHECK_PARSE_BOOL(SortUsingDeclarations); 18496 CHECK_PARSE_BOOL(SpacesInParentheses); 18497 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18498 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18499 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18500 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18501 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18502 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18503 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18504 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18505 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18506 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18507 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18508 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18509 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18510 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18511 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18512 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18513 CHECK_PARSE_BOOL(UseCRLF); 18514 18515 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18516 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18517 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18518 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18519 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18520 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18521 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18522 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18523 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18524 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18525 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18526 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18527 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18528 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18529 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18530 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18531 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18532 } 18533 18534 #undef CHECK_PARSE_BOOL 18535 18536 TEST_F(FormatTest, ParsesConfiguration) { 18537 FormatStyle Style = {}; 18538 Style.Language = FormatStyle::LK_Cpp; 18539 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18540 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18541 ConstructorInitializerIndentWidth, 1234u); 18542 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18543 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18544 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18545 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18546 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18547 PenaltyBreakBeforeFirstCallParameter, 1234u); 18548 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18549 PenaltyBreakTemplateDeclaration, 1234u); 18550 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18551 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18552 PenaltyReturnTypeOnItsOwnLine, 1234u); 18553 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18554 SpacesBeforeTrailingComments, 1234u); 18555 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18556 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18557 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18558 18559 Style.QualifierAlignment = FormatStyle::QAS_Right; 18560 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 18561 FormatStyle::QAS_Leave); 18562 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 18563 FormatStyle::QAS_Right); 18564 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 18565 FormatStyle::QAS_Left); 18566 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 18567 FormatStyle::QAS_Custom); 18568 18569 Style.QualifierOrder.clear(); 18570 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 18571 std::vector<std::string>({"const", "volatile", "type"})); 18572 Style.QualifierOrder.clear(); 18573 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 18574 std::vector<std::string>({"const", "type"})); 18575 Style.QualifierOrder.clear(); 18576 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 18577 std::vector<std::string>({"volatile", "type"})); 18578 18579 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18580 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18581 FormatStyle::ACS_None); 18582 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18583 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18584 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18585 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18586 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18587 AlignConsecutiveAssignments, 18588 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18589 // For backwards compability, false / true should still parse 18590 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18591 FormatStyle::ACS_None); 18592 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18593 FormatStyle::ACS_Consecutive); 18594 18595 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18596 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18597 FormatStyle::ACS_None); 18598 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18599 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18600 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18601 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18602 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18603 AlignConsecutiveBitFields, 18604 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18605 // For backwards compability, false / true should still parse 18606 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18607 FormatStyle::ACS_None); 18608 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18609 FormatStyle::ACS_Consecutive); 18610 18611 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18612 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18613 FormatStyle::ACS_None); 18614 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18615 FormatStyle::ACS_Consecutive); 18616 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18617 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18618 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18619 AlignConsecutiveMacros, 18620 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18621 // For backwards compability, false / true should still parse 18622 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18623 FormatStyle::ACS_None); 18624 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18625 FormatStyle::ACS_Consecutive); 18626 18627 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18628 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18629 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18630 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18631 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18632 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18633 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18634 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18635 AlignConsecutiveDeclarations, 18636 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18637 // For backwards compability, false / true should still parse 18638 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18639 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18640 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18641 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18642 18643 Style.PointerAlignment = FormatStyle::PAS_Middle; 18644 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18645 FormatStyle::PAS_Left); 18646 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18647 FormatStyle::PAS_Right); 18648 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18649 FormatStyle::PAS_Middle); 18650 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18651 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18652 FormatStyle::RAS_Pointer); 18653 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18654 FormatStyle::RAS_Left); 18655 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18656 FormatStyle::RAS_Right); 18657 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18658 FormatStyle::RAS_Middle); 18659 // For backward compatibility: 18660 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18661 FormatStyle::PAS_Left); 18662 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18663 FormatStyle::PAS_Right); 18664 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18665 FormatStyle::PAS_Middle); 18666 18667 Style.Standard = FormatStyle::LS_Auto; 18668 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18669 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18670 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18671 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18672 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18673 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18674 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18675 // Legacy aliases: 18676 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18677 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18678 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18679 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18680 18681 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18682 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18683 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18684 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18685 FormatStyle::BOS_None); 18686 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18687 FormatStyle::BOS_All); 18688 // For backward compatibility: 18689 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18690 FormatStyle::BOS_None); 18691 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18692 FormatStyle::BOS_All); 18693 18694 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18695 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18696 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18697 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18698 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18699 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18700 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18701 // For backward compatibility: 18702 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18703 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18704 18705 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18706 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18707 FormatStyle::BILS_AfterComma); 18708 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18709 FormatStyle::BILS_BeforeComma); 18710 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18711 FormatStyle::BILS_AfterColon); 18712 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18713 FormatStyle::BILS_BeforeColon); 18714 // For backward compatibility: 18715 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18716 FormatStyle::BILS_BeforeComma); 18717 18718 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 18719 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 18720 FormatStyle::PCIS_Never); 18721 CHECK_PARSE("PackConstructorInitializers: BinPack", 18722 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 18723 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 18724 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18725 CHECK_PARSE("PackConstructorInitializers: NextLine", 18726 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 18727 // For backward compatibility: 18728 CHECK_PARSE("BasedOnStyle: Google\n" 18729 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18730 "AllowAllConstructorInitializersOnNextLine: false", 18731 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18732 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 18733 CHECK_PARSE("BasedOnStyle: Google\n" 18734 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 18735 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 18736 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18737 "AllowAllConstructorInitializersOnNextLine: true", 18738 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 18739 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 18740 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18741 "AllowAllConstructorInitializersOnNextLine: false", 18742 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18743 18744 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18745 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18746 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18747 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18748 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18749 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18750 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18751 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18752 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18753 18754 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18755 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18756 FormatStyle::BAS_Align); 18757 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18758 FormatStyle::BAS_DontAlign); 18759 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18760 FormatStyle::BAS_AlwaysBreak); 18761 // For backward compatibility: 18762 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18763 FormatStyle::BAS_DontAlign); 18764 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18765 FormatStyle::BAS_Align); 18766 18767 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18768 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18769 FormatStyle::ENAS_DontAlign); 18770 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18771 FormatStyle::ENAS_Left); 18772 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18773 FormatStyle::ENAS_Right); 18774 // For backward compatibility: 18775 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18776 FormatStyle::ENAS_Left); 18777 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18778 FormatStyle::ENAS_Right); 18779 18780 Style.AlignOperands = FormatStyle::OAS_Align; 18781 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18782 FormatStyle::OAS_DontAlign); 18783 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18784 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18785 FormatStyle::OAS_AlignAfterOperator); 18786 // For backward compatibility: 18787 CHECK_PARSE("AlignOperands: false", AlignOperands, 18788 FormatStyle::OAS_DontAlign); 18789 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18790 18791 Style.UseTab = FormatStyle::UT_ForIndentation; 18792 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18793 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18794 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18795 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18796 FormatStyle::UT_ForContinuationAndIndentation); 18797 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18798 FormatStyle::UT_AlignWithSpaces); 18799 // For backward compatibility: 18800 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18801 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18802 18803 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18804 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18805 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18806 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18807 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18808 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18809 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18810 // For backward compatibility: 18811 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18812 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18813 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18814 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18815 18816 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18817 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18818 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18819 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18820 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18821 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18822 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18823 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18824 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18825 // For backward compatibility: 18826 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18827 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18828 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18829 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18830 18831 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 18832 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 18833 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 18834 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 18835 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 18836 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 18837 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 18838 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 18839 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 18840 18841 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 18842 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 18843 FormatStyle::SBPO_Never); 18844 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 18845 FormatStyle::SBPO_Always); 18846 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 18847 FormatStyle::SBPO_ControlStatements); 18848 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 18849 SpaceBeforeParens, 18850 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18851 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 18852 FormatStyle::SBPO_NonEmptyParentheses); 18853 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 18854 FormatStyle::SBPO_Custom); 18855 // For backward compatibility: 18856 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 18857 FormatStyle::SBPO_Never); 18858 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 18859 FormatStyle::SBPO_ControlStatements); 18860 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 18861 SpaceBeforeParens, 18862 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18863 18864 Style.ColumnLimit = 123; 18865 FormatStyle BaseStyle = getLLVMStyle(); 18866 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 18867 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 18868 18869 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 18870 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 18871 FormatStyle::BS_Attach); 18872 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 18873 FormatStyle::BS_Linux); 18874 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 18875 FormatStyle::BS_Mozilla); 18876 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 18877 FormatStyle::BS_Stroustrup); 18878 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 18879 FormatStyle::BS_Allman); 18880 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 18881 FormatStyle::BS_Whitesmiths); 18882 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 18883 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 18884 FormatStyle::BS_WebKit); 18885 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 18886 FormatStyle::BS_Custom); 18887 18888 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 18889 CHECK_PARSE("BraceWrapping:\n" 18890 " AfterControlStatement: MultiLine", 18891 BraceWrapping.AfterControlStatement, 18892 FormatStyle::BWACS_MultiLine); 18893 CHECK_PARSE("BraceWrapping:\n" 18894 " AfterControlStatement: Always", 18895 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18896 CHECK_PARSE("BraceWrapping:\n" 18897 " AfterControlStatement: Never", 18898 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18899 // For backward compatibility: 18900 CHECK_PARSE("BraceWrapping:\n" 18901 " AfterControlStatement: true", 18902 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18903 CHECK_PARSE("BraceWrapping:\n" 18904 " AfterControlStatement: false", 18905 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18906 18907 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 18908 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 18909 FormatStyle::RTBS_None); 18910 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 18911 FormatStyle::RTBS_All); 18912 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 18913 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 18914 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 18915 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 18916 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 18917 AlwaysBreakAfterReturnType, 18918 FormatStyle::RTBS_TopLevelDefinitions); 18919 18920 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 18921 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 18922 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 18923 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 18924 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18925 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 18926 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18927 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 18928 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18929 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 18930 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18931 18932 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 18933 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 18934 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 18935 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 18936 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 18937 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 18938 AlwaysBreakAfterDefinitionReturnType, 18939 FormatStyle::DRTBS_TopLevel); 18940 18941 Style.NamespaceIndentation = FormatStyle::NI_All; 18942 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 18943 FormatStyle::NI_None); 18944 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 18945 FormatStyle::NI_Inner); 18946 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 18947 FormatStyle::NI_All); 18948 18949 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 18950 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 18951 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18952 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 18953 AllowShortIfStatementsOnASingleLine, 18954 FormatStyle::SIS_WithoutElse); 18955 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 18956 AllowShortIfStatementsOnASingleLine, 18957 FormatStyle::SIS_OnlyFirstIf); 18958 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 18959 AllowShortIfStatementsOnASingleLine, 18960 FormatStyle::SIS_AllIfsAndElse); 18961 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 18962 AllowShortIfStatementsOnASingleLine, 18963 FormatStyle::SIS_OnlyFirstIf); 18964 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 18965 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18966 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 18967 AllowShortIfStatementsOnASingleLine, 18968 FormatStyle::SIS_WithoutElse); 18969 18970 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 18971 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 18972 FormatStyle::IEBS_AfterExternBlock); 18973 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 18974 FormatStyle::IEBS_Indent); 18975 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 18976 FormatStyle::IEBS_NoIndent); 18977 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 18978 FormatStyle::IEBS_Indent); 18979 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 18980 FormatStyle::IEBS_NoIndent); 18981 18982 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 18983 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 18984 FormatStyle::BFCS_Both); 18985 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 18986 FormatStyle::BFCS_None); 18987 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 18988 FormatStyle::BFCS_Before); 18989 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 18990 FormatStyle::BFCS_After); 18991 18992 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 18993 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 18994 FormatStyle::SJSIO_After); 18995 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 18996 FormatStyle::SJSIO_Before); 18997 18998 // FIXME: This is required because parsing a configuration simply overwrites 18999 // the first N elements of the list instead of resetting it. 19000 Style.ForEachMacros.clear(); 19001 std::vector<std::string> BoostForeach; 19002 BoostForeach.push_back("BOOST_FOREACH"); 19003 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 19004 std::vector<std::string> BoostAndQForeach; 19005 BoostAndQForeach.push_back("BOOST_FOREACH"); 19006 BoostAndQForeach.push_back("Q_FOREACH"); 19007 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 19008 BoostAndQForeach); 19009 19010 Style.IfMacros.clear(); 19011 std::vector<std::string> CustomIfs; 19012 CustomIfs.push_back("MYIF"); 19013 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 19014 19015 Style.AttributeMacros.clear(); 19016 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 19017 std::vector<std::string>{"__capability"}); 19018 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 19019 std::vector<std::string>({"attr1", "attr2"})); 19020 19021 Style.StatementAttributeLikeMacros.clear(); 19022 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 19023 StatementAttributeLikeMacros, 19024 std::vector<std::string>({"emit", "Q_EMIT"})); 19025 19026 Style.StatementMacros.clear(); 19027 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 19028 std::vector<std::string>{"QUNUSED"}); 19029 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 19030 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 19031 19032 Style.NamespaceMacros.clear(); 19033 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 19034 std::vector<std::string>{"TESTSUITE"}); 19035 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 19036 std::vector<std::string>({"TESTSUITE", "SUITE"})); 19037 19038 Style.WhitespaceSensitiveMacros.clear(); 19039 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 19040 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19041 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 19042 WhitespaceSensitiveMacros, 19043 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19044 Style.WhitespaceSensitiveMacros.clear(); 19045 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 19046 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19047 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 19048 WhitespaceSensitiveMacros, 19049 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19050 19051 Style.IncludeStyle.IncludeCategories.clear(); 19052 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 19053 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 19054 CHECK_PARSE("IncludeCategories:\n" 19055 " - Regex: abc/.*\n" 19056 " Priority: 2\n" 19057 " - Regex: .*\n" 19058 " Priority: 1\n" 19059 " CaseSensitive: true\n", 19060 IncludeStyle.IncludeCategories, ExpectedCategories); 19061 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 19062 "abc$"); 19063 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 19064 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 19065 19066 Style.SortIncludes = FormatStyle::SI_Never; 19067 CHECK_PARSE("SortIncludes: true", SortIncludes, 19068 FormatStyle::SI_CaseSensitive); 19069 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 19070 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 19071 FormatStyle::SI_CaseInsensitive); 19072 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 19073 FormatStyle::SI_CaseSensitive); 19074 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 19075 19076 Style.RawStringFormats.clear(); 19077 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 19078 { 19079 FormatStyle::LK_TextProto, 19080 {"pb", "proto"}, 19081 {"PARSE_TEXT_PROTO"}, 19082 /*CanonicalDelimiter=*/"", 19083 "llvm", 19084 }, 19085 { 19086 FormatStyle::LK_Cpp, 19087 {"cc", "cpp"}, 19088 {"C_CODEBLOCK", "CPPEVAL"}, 19089 /*CanonicalDelimiter=*/"cc", 19090 /*BasedOnStyle=*/"", 19091 }, 19092 }; 19093 19094 CHECK_PARSE("RawStringFormats:\n" 19095 " - Language: TextProto\n" 19096 " Delimiters:\n" 19097 " - 'pb'\n" 19098 " - 'proto'\n" 19099 " EnclosingFunctions:\n" 19100 " - 'PARSE_TEXT_PROTO'\n" 19101 " BasedOnStyle: llvm\n" 19102 " - Language: Cpp\n" 19103 " Delimiters:\n" 19104 " - 'cc'\n" 19105 " - 'cpp'\n" 19106 " EnclosingFunctions:\n" 19107 " - 'C_CODEBLOCK'\n" 19108 " - 'CPPEVAL'\n" 19109 " CanonicalDelimiter: 'cc'", 19110 RawStringFormats, ExpectedRawStringFormats); 19111 19112 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19113 " Minimum: 0\n" 19114 " Maximum: 0", 19115 SpacesInLineCommentPrefix.Minimum, 0u); 19116 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 19117 Style.SpacesInLineCommentPrefix.Minimum = 1; 19118 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19119 " Minimum: 2", 19120 SpacesInLineCommentPrefix.Minimum, 0u); 19121 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19122 " Maximum: -1", 19123 SpacesInLineCommentPrefix.Maximum, -1u); 19124 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19125 " Minimum: 2", 19126 SpacesInLineCommentPrefix.Minimum, 2u); 19127 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19128 " Maximum: 1", 19129 SpacesInLineCommentPrefix.Maximum, 1u); 19130 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 19131 19132 Style.SpacesInAngles = FormatStyle::SIAS_Always; 19133 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 19134 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 19135 FormatStyle::SIAS_Always); 19136 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 19137 // For backward compatibility: 19138 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 19139 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 19140 } 19141 19142 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 19143 FormatStyle Style = {}; 19144 Style.Language = FormatStyle::LK_Cpp; 19145 CHECK_PARSE("Language: Cpp\n" 19146 "IndentWidth: 12", 19147 IndentWidth, 12u); 19148 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 19149 "IndentWidth: 34", 19150 &Style), 19151 ParseError::Unsuitable); 19152 FormatStyle BinPackedTCS = {}; 19153 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 19154 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 19155 "InsertTrailingCommas: Wrapped", 19156 &BinPackedTCS), 19157 ParseError::BinPackTrailingCommaConflict); 19158 EXPECT_EQ(12u, Style.IndentWidth); 19159 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19160 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19161 19162 Style.Language = FormatStyle::LK_JavaScript; 19163 CHECK_PARSE("Language: JavaScript\n" 19164 "IndentWidth: 12", 19165 IndentWidth, 12u); 19166 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 19167 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 19168 "IndentWidth: 34", 19169 &Style), 19170 ParseError::Unsuitable); 19171 EXPECT_EQ(23u, Style.IndentWidth); 19172 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19173 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19174 19175 CHECK_PARSE("BasedOnStyle: LLVM\n" 19176 "IndentWidth: 67", 19177 IndentWidth, 67u); 19178 19179 CHECK_PARSE("---\n" 19180 "Language: JavaScript\n" 19181 "IndentWidth: 12\n" 19182 "---\n" 19183 "Language: Cpp\n" 19184 "IndentWidth: 34\n" 19185 "...\n", 19186 IndentWidth, 12u); 19187 19188 Style.Language = FormatStyle::LK_Cpp; 19189 CHECK_PARSE("---\n" 19190 "Language: JavaScript\n" 19191 "IndentWidth: 12\n" 19192 "---\n" 19193 "Language: Cpp\n" 19194 "IndentWidth: 34\n" 19195 "...\n", 19196 IndentWidth, 34u); 19197 CHECK_PARSE("---\n" 19198 "IndentWidth: 78\n" 19199 "---\n" 19200 "Language: JavaScript\n" 19201 "IndentWidth: 56\n" 19202 "...\n", 19203 IndentWidth, 78u); 19204 19205 Style.ColumnLimit = 123; 19206 Style.IndentWidth = 234; 19207 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 19208 Style.TabWidth = 345; 19209 EXPECT_FALSE(parseConfiguration("---\n" 19210 "IndentWidth: 456\n" 19211 "BreakBeforeBraces: Allman\n" 19212 "---\n" 19213 "Language: JavaScript\n" 19214 "IndentWidth: 111\n" 19215 "TabWidth: 111\n" 19216 "---\n" 19217 "Language: Cpp\n" 19218 "BreakBeforeBraces: Stroustrup\n" 19219 "TabWidth: 789\n" 19220 "...\n", 19221 &Style)); 19222 EXPECT_EQ(123u, Style.ColumnLimit); 19223 EXPECT_EQ(456u, Style.IndentWidth); 19224 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 19225 EXPECT_EQ(789u, Style.TabWidth); 19226 19227 EXPECT_EQ(parseConfiguration("---\n" 19228 "Language: JavaScript\n" 19229 "IndentWidth: 56\n" 19230 "---\n" 19231 "IndentWidth: 78\n" 19232 "...\n", 19233 &Style), 19234 ParseError::Error); 19235 EXPECT_EQ(parseConfiguration("---\n" 19236 "Language: JavaScript\n" 19237 "IndentWidth: 56\n" 19238 "---\n" 19239 "Language: JavaScript\n" 19240 "IndentWidth: 78\n" 19241 "...\n", 19242 &Style), 19243 ParseError::Error); 19244 19245 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19246 } 19247 19248 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 19249 FormatStyle Style = {}; 19250 Style.Language = FormatStyle::LK_JavaScript; 19251 Style.BreakBeforeTernaryOperators = true; 19252 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 19253 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19254 19255 Style.BreakBeforeTernaryOperators = true; 19256 EXPECT_EQ(0, parseConfiguration("---\n" 19257 "BasedOnStyle: Google\n" 19258 "---\n" 19259 "Language: JavaScript\n" 19260 "IndentWidth: 76\n" 19261 "...\n", 19262 &Style) 19263 .value()); 19264 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19265 EXPECT_EQ(76u, Style.IndentWidth); 19266 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19267 } 19268 19269 TEST_F(FormatTest, ConfigurationRoundTripTest) { 19270 FormatStyle Style = getLLVMStyle(); 19271 std::string YAML = configurationAsText(Style); 19272 FormatStyle ParsedStyle = {}; 19273 ParsedStyle.Language = FormatStyle::LK_Cpp; 19274 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 19275 EXPECT_EQ(Style, ParsedStyle); 19276 } 19277 19278 TEST_F(FormatTest, WorksFor8bitEncodings) { 19279 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 19280 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 19281 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 19282 "\"\xef\xee\xf0\xf3...\"", 19283 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 19284 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 19285 "\xef\xee\xf0\xf3...\"", 19286 getLLVMStyleWithColumns(12))); 19287 } 19288 19289 TEST_F(FormatTest, HandlesUTF8BOM) { 19290 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 19291 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 19292 format("\xef\xbb\xbf#include <iostream>")); 19293 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 19294 format("\xef\xbb\xbf\n#include <iostream>")); 19295 } 19296 19297 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 19298 #if !defined(_MSC_VER) 19299 19300 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 19301 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 19302 getLLVMStyleWithColumns(35)); 19303 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 19304 getLLVMStyleWithColumns(31)); 19305 verifyFormat("// Однажды в студёную зимнюю пору...", 19306 getLLVMStyleWithColumns(36)); 19307 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 19308 verifyFormat("/* Однажды в студёную зимнюю пору... */", 19309 getLLVMStyleWithColumns(39)); 19310 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 19311 getLLVMStyleWithColumns(35)); 19312 } 19313 19314 TEST_F(FormatTest, SplitsUTF8Strings) { 19315 // Non-printable characters' width is currently considered to be the length in 19316 // bytes in UTF8. The characters can be displayed in very different manner 19317 // (zero-width, single width with a substitution glyph, expanded to their code 19318 // (e.g. "<8d>"), so there's no single correct way to handle them. 19319 EXPECT_EQ("\"aaaaÄ\"\n" 19320 "\"\xc2\x8d\";", 19321 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19322 EXPECT_EQ("\"aaaaaaaÄ\"\n" 19323 "\"\xc2\x8d\";", 19324 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19325 EXPECT_EQ("\"Однажды, в \"\n" 19326 "\"студёную \"\n" 19327 "\"зимнюю \"\n" 19328 "\"пору,\"", 19329 format("\"Однажды, в студёную зимнюю пору,\"", 19330 getLLVMStyleWithColumns(13))); 19331 EXPECT_EQ( 19332 "\"一 二 三 \"\n" 19333 "\"四 五六 \"\n" 19334 "\"七 八 九 \"\n" 19335 "\"十\"", 19336 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 19337 EXPECT_EQ("\"一\t\"\n" 19338 "\"二 \t\"\n" 19339 "\"三 四 \"\n" 19340 "\"五\t\"\n" 19341 "\"六 \t\"\n" 19342 "\"七 \"\n" 19343 "\"八九十\tqq\"", 19344 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19345 getLLVMStyleWithColumns(11))); 19346 19347 // UTF8 character in an escape sequence. 19348 EXPECT_EQ("\"aaaaaa\"\n" 19349 "\"\\\xC2\x8D\"", 19350 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19351 } 19352 19353 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19354 EXPECT_EQ("const char *sssss =\n" 19355 " \"一二三四五六七八\\\n" 19356 " 九 十\";", 19357 format("const char *sssss = \"一二三四五六七八\\\n" 19358 " 九 十\";", 19359 getLLVMStyleWithColumns(30))); 19360 } 19361 19362 TEST_F(FormatTest, SplitsUTF8LineComments) { 19363 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19364 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19365 EXPECT_EQ("// Я из лесу\n" 19366 "// вышел; был\n" 19367 "// сильный\n" 19368 "// мороз.", 19369 format("// Я из лесу вышел; был сильный мороз.", 19370 getLLVMStyleWithColumns(13))); 19371 EXPECT_EQ("// 一二三\n" 19372 "// 四五六七\n" 19373 "// 八 九\n" 19374 "// 十", 19375 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19376 } 19377 19378 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19379 EXPECT_EQ("/* Гляжу,\n" 19380 " * поднимается\n" 19381 " * медленно в\n" 19382 " * гору\n" 19383 " * Лошадка,\n" 19384 " * везущая\n" 19385 " * хворосту\n" 19386 " * воз. */", 19387 format("/* Гляжу, поднимается медленно в гору\n" 19388 " * Лошадка, везущая хворосту воз. */", 19389 getLLVMStyleWithColumns(13))); 19390 EXPECT_EQ( 19391 "/* 一二三\n" 19392 " * 四五六七\n" 19393 " * 八 九\n" 19394 " * 十 */", 19395 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19396 EXPECT_EQ("/* \n" 19397 " * \n" 19398 " * - */", 19399 format("/* - */", getLLVMStyleWithColumns(12))); 19400 } 19401 19402 #endif // _MSC_VER 19403 19404 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19405 FormatStyle Style = getLLVMStyle(); 19406 19407 Style.ConstructorInitializerIndentWidth = 4; 19408 verifyFormat( 19409 "SomeClass::Constructor()\n" 19410 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19411 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19412 Style); 19413 19414 Style.ConstructorInitializerIndentWidth = 2; 19415 verifyFormat( 19416 "SomeClass::Constructor()\n" 19417 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19418 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19419 Style); 19420 19421 Style.ConstructorInitializerIndentWidth = 0; 19422 verifyFormat( 19423 "SomeClass::Constructor()\n" 19424 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19425 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19426 Style); 19427 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19428 verifyFormat( 19429 "SomeLongTemplateVariableName<\n" 19430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19431 Style); 19432 verifyFormat("bool smaller = 1 < " 19433 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19434 " " 19435 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19436 Style); 19437 19438 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19439 verifyFormat("SomeClass::Constructor() :\n" 19440 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19441 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19442 Style); 19443 } 19444 19445 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19446 FormatStyle Style = getLLVMStyle(); 19447 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19448 Style.ConstructorInitializerIndentWidth = 4; 19449 verifyFormat("SomeClass::Constructor()\n" 19450 " : a(a)\n" 19451 " , b(b)\n" 19452 " , c(c) {}", 19453 Style); 19454 verifyFormat("SomeClass::Constructor()\n" 19455 " : a(a) {}", 19456 Style); 19457 19458 Style.ColumnLimit = 0; 19459 verifyFormat("SomeClass::Constructor()\n" 19460 " : a(a) {}", 19461 Style); 19462 verifyFormat("SomeClass::Constructor() noexcept\n" 19463 " : a(a) {}", 19464 Style); 19465 verifyFormat("SomeClass::Constructor()\n" 19466 " : a(a)\n" 19467 " , b(b)\n" 19468 " , c(c) {}", 19469 Style); 19470 verifyFormat("SomeClass::Constructor()\n" 19471 " : a(a) {\n" 19472 " foo();\n" 19473 " bar();\n" 19474 "}", 19475 Style); 19476 19477 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19478 verifyFormat("SomeClass::Constructor()\n" 19479 " : a(a)\n" 19480 " , b(b)\n" 19481 " , c(c) {\n}", 19482 Style); 19483 verifyFormat("SomeClass::Constructor()\n" 19484 " : a(a) {\n}", 19485 Style); 19486 19487 Style.ColumnLimit = 80; 19488 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19489 Style.ConstructorInitializerIndentWidth = 2; 19490 verifyFormat("SomeClass::Constructor()\n" 19491 " : a(a)\n" 19492 " , b(b)\n" 19493 " , c(c) {}", 19494 Style); 19495 19496 Style.ConstructorInitializerIndentWidth = 0; 19497 verifyFormat("SomeClass::Constructor()\n" 19498 ": a(a)\n" 19499 ", b(b)\n" 19500 ", c(c) {}", 19501 Style); 19502 19503 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19504 Style.ConstructorInitializerIndentWidth = 4; 19505 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19506 verifyFormat( 19507 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19508 Style); 19509 verifyFormat( 19510 "SomeClass::Constructor()\n" 19511 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19512 Style); 19513 Style.ConstructorInitializerIndentWidth = 4; 19514 Style.ColumnLimit = 60; 19515 verifyFormat("SomeClass::Constructor()\n" 19516 " : aaaaaaaa(aaaaaaaa)\n" 19517 " , aaaaaaaa(aaaaaaaa)\n" 19518 " , aaaaaaaa(aaaaaaaa) {}", 19519 Style); 19520 } 19521 19522 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 19523 FormatStyle Style = getLLVMStyle(); 19524 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19525 Style.ConstructorInitializerIndentWidth = 4; 19526 verifyFormat("SomeClass::Constructor()\n" 19527 " : a{a}\n" 19528 " , b{b} {}", 19529 Style); 19530 verifyFormat("SomeClass::Constructor()\n" 19531 " : a{a}\n" 19532 "#if CONDITION\n" 19533 " , b{b}\n" 19534 "#endif\n" 19535 "{\n}", 19536 Style); 19537 Style.ConstructorInitializerIndentWidth = 2; 19538 verifyFormat("SomeClass::Constructor()\n" 19539 "#if CONDITION\n" 19540 " : a{a}\n" 19541 "#endif\n" 19542 " , b{b}\n" 19543 " , c{c} {\n}", 19544 Style); 19545 Style.ConstructorInitializerIndentWidth = 0; 19546 verifyFormat("SomeClass::Constructor()\n" 19547 ": a{a}\n" 19548 "#ifdef CONDITION\n" 19549 ", b{b}\n" 19550 "#else\n" 19551 ", c{c}\n" 19552 "#endif\n" 19553 ", d{d} {\n}", 19554 Style); 19555 Style.ConstructorInitializerIndentWidth = 4; 19556 verifyFormat("SomeClass::Constructor()\n" 19557 " : a{a}\n" 19558 "#if WINDOWS\n" 19559 "#if DEBUG\n" 19560 " , b{0}\n" 19561 "#else\n" 19562 " , b{1}\n" 19563 "#endif\n" 19564 "#else\n" 19565 "#if DEBUG\n" 19566 " , b{2}\n" 19567 "#else\n" 19568 " , b{3}\n" 19569 "#endif\n" 19570 "#endif\n" 19571 "{\n}", 19572 Style); 19573 verifyFormat("SomeClass::Constructor()\n" 19574 " : a{a}\n" 19575 "#if WINDOWS\n" 19576 " , b{0}\n" 19577 "#if DEBUG\n" 19578 " , c{0}\n" 19579 "#else\n" 19580 " , c{1}\n" 19581 "#endif\n" 19582 "#else\n" 19583 "#if DEBUG\n" 19584 " , c{2}\n" 19585 "#else\n" 19586 " , c{3}\n" 19587 "#endif\n" 19588 " , b{1}\n" 19589 "#endif\n" 19590 "{\n}", 19591 Style); 19592 } 19593 19594 TEST_F(FormatTest, Destructors) { 19595 verifyFormat("void F(int &i) { i.~int(); }"); 19596 verifyFormat("void F(int &i) { i->~int(); }"); 19597 } 19598 19599 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19600 FormatStyle Style = getWebKitStyle(); 19601 19602 // Don't indent in outer namespaces. 19603 verifyFormat("namespace outer {\n" 19604 "int i;\n" 19605 "namespace inner {\n" 19606 " int i;\n" 19607 "} // namespace inner\n" 19608 "} // namespace outer\n" 19609 "namespace other_outer {\n" 19610 "int i;\n" 19611 "}", 19612 Style); 19613 19614 // Don't indent case labels. 19615 verifyFormat("switch (variable) {\n" 19616 "case 1:\n" 19617 "case 2:\n" 19618 " doSomething();\n" 19619 " break;\n" 19620 "default:\n" 19621 " ++variable;\n" 19622 "}", 19623 Style); 19624 19625 // Wrap before binary operators. 19626 EXPECT_EQ("void f()\n" 19627 "{\n" 19628 " if (aaaaaaaaaaaaaaaa\n" 19629 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19630 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19631 " return;\n" 19632 "}", 19633 format("void f() {\n" 19634 "if (aaaaaaaaaaaaaaaa\n" 19635 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19636 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19637 "return;\n" 19638 "}", 19639 Style)); 19640 19641 // Allow functions on a single line. 19642 verifyFormat("void f() { return; }", Style); 19643 19644 // Allow empty blocks on a single line and insert a space in empty blocks. 19645 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19646 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19647 // However, don't merge non-empty short loops. 19648 EXPECT_EQ("while (true) {\n" 19649 " continue;\n" 19650 "}", 19651 format("while (true) { continue; }", Style)); 19652 19653 // Constructor initializers are formatted one per line with the "," on the 19654 // new line. 19655 verifyFormat("Constructor()\n" 19656 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19657 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19658 " aaaaaaaaaaaaaa)\n" 19659 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19660 "{\n" 19661 "}", 19662 Style); 19663 verifyFormat("SomeClass::Constructor()\n" 19664 " : a(a)\n" 19665 "{\n" 19666 "}", 19667 Style); 19668 EXPECT_EQ("SomeClass::Constructor()\n" 19669 " : a(a)\n" 19670 "{\n" 19671 "}", 19672 format("SomeClass::Constructor():a(a){}", Style)); 19673 verifyFormat("SomeClass::Constructor()\n" 19674 " : a(a)\n" 19675 " , b(b)\n" 19676 " , c(c)\n" 19677 "{\n" 19678 "}", 19679 Style); 19680 verifyFormat("SomeClass::Constructor()\n" 19681 " : a(a)\n" 19682 "{\n" 19683 " foo();\n" 19684 " bar();\n" 19685 "}", 19686 Style); 19687 19688 // Access specifiers should be aligned left. 19689 verifyFormat("class C {\n" 19690 "public:\n" 19691 " int i;\n" 19692 "};", 19693 Style); 19694 19695 // Do not align comments. 19696 verifyFormat("int a; // Do not\n" 19697 "double b; // align comments.", 19698 Style); 19699 19700 // Do not align operands. 19701 EXPECT_EQ("ASSERT(aaaa\n" 19702 " || bbbb);", 19703 format("ASSERT ( aaaa\n||bbbb);", Style)); 19704 19705 // Accept input's line breaks. 19706 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19707 " || bbbbbbbbbbbbbbb) {\n" 19708 " i++;\n" 19709 "}", 19710 format("if (aaaaaaaaaaaaaaa\n" 19711 "|| bbbbbbbbbbbbbbb) { i++; }", 19712 Style)); 19713 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19714 " i++;\n" 19715 "}", 19716 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19717 19718 // Don't automatically break all macro definitions (llvm.org/PR17842). 19719 verifyFormat("#define aNumber 10", Style); 19720 // However, generally keep the line breaks that the user authored. 19721 EXPECT_EQ("#define aNumber \\\n" 19722 " 10", 19723 format("#define aNumber \\\n" 19724 " 10", 19725 Style)); 19726 19727 // Keep empty and one-element array literals on a single line. 19728 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19729 " copyItems:YES];", 19730 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19731 "copyItems:YES];", 19732 Style)); 19733 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19734 " copyItems:YES];", 19735 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19736 " copyItems:YES];", 19737 Style)); 19738 // FIXME: This does not seem right, there should be more indentation before 19739 // the array literal's entries. Nested blocks have the same problem. 19740 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19741 " @\"a\",\n" 19742 " @\"a\"\n" 19743 "]\n" 19744 " copyItems:YES];", 19745 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19746 " @\"a\",\n" 19747 " @\"a\"\n" 19748 " ]\n" 19749 " copyItems:YES];", 19750 Style)); 19751 EXPECT_EQ( 19752 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19753 " copyItems:YES];", 19754 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19755 " copyItems:YES];", 19756 Style)); 19757 19758 verifyFormat("[self.a b:c c:d];", Style); 19759 EXPECT_EQ("[self.a b:c\n" 19760 " c:d];", 19761 format("[self.a b:c\n" 19762 "c:d];", 19763 Style)); 19764 } 19765 19766 TEST_F(FormatTest, FormatsLambdas) { 19767 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19768 verifyFormat( 19769 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19770 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19771 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19772 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19773 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19774 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19775 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19776 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19777 verifyFormat("int x = f(*+[] {});"); 19778 verifyFormat("void f() {\n" 19779 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19780 "}\n"); 19781 verifyFormat("void f() {\n" 19782 " other(x.begin(), //\n" 19783 " x.end(), //\n" 19784 " [&](int, int) { return 1; });\n" 19785 "}\n"); 19786 verifyFormat("void f() {\n" 19787 " other.other.other.other.other(\n" 19788 " x.begin(), x.end(),\n" 19789 " [something, rather](int, int, int, int, int, int, int) { " 19790 "return 1; });\n" 19791 "}\n"); 19792 verifyFormat( 19793 "void f() {\n" 19794 " other.other.other.other.other(\n" 19795 " x.begin(), x.end(),\n" 19796 " [something, rather](int, int, int, int, int, int, int) {\n" 19797 " //\n" 19798 " });\n" 19799 "}\n"); 19800 verifyFormat("SomeFunction([]() { // A cool function...\n" 19801 " return 43;\n" 19802 "});"); 19803 EXPECT_EQ("SomeFunction([]() {\n" 19804 "#define A a\n" 19805 " return 43;\n" 19806 "});", 19807 format("SomeFunction([](){\n" 19808 "#define A a\n" 19809 "return 43;\n" 19810 "});")); 19811 verifyFormat("void f() {\n" 19812 " SomeFunction([](decltype(x), A *a) {});\n" 19813 " SomeFunction([](typeof(x), A *a) {});\n" 19814 " SomeFunction([](_Atomic(x), A *a) {});\n" 19815 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19816 "}"); 19817 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19818 " [](const aaaaaaaaaa &a) { return a; });"); 19819 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19820 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19821 "});"); 19822 verifyFormat("Constructor()\n" 19823 " : Field([] { // comment\n" 19824 " int i;\n" 19825 " }) {}"); 19826 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19827 " return some_parameter.size();\n" 19828 "};"); 19829 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 19830 " [](const string &s) { return s; };"); 19831 verifyFormat("int i = aaaaaa ? 1 //\n" 19832 " : [] {\n" 19833 " return 2; //\n" 19834 " }();"); 19835 verifyFormat("llvm::errs() << \"number of twos is \"\n" 19836 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 19837 " return x == 2; // force break\n" 19838 " });"); 19839 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19840 " [=](int iiiiiiiiiiii) {\n" 19841 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 19842 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 19843 " });", 19844 getLLVMStyleWithColumns(60)); 19845 19846 verifyFormat("SomeFunction({[&] {\n" 19847 " // comment\n" 19848 " },\n" 19849 " [&] {\n" 19850 " // comment\n" 19851 " }});"); 19852 verifyFormat("SomeFunction({[&] {\n" 19853 " // comment\n" 19854 "}});"); 19855 verifyFormat( 19856 "virtual aaaaaaaaaaaaaaaa(\n" 19857 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 19858 " aaaaa aaaaaaaaa);"); 19859 19860 // Lambdas with return types. 19861 verifyFormat("int c = []() -> int { return 2; }();\n"); 19862 verifyFormat("int c = []() -> int * { return 2; }();\n"); 19863 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 19864 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 19865 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 19866 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 19867 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 19868 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 19869 verifyFormat("[a, a]() -> a<1> {};"); 19870 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 19871 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 19872 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 19873 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 19874 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 19875 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 19876 verifyFormat("[]() -> foo<!5> { return {}; };"); 19877 verifyFormat("[]() -> foo<~5> { return {}; };"); 19878 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 19879 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 19880 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 19881 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 19882 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 19883 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 19884 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 19885 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 19886 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 19887 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 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> { return {}; }};\n" 19915 "} // namespace bar"); 19916 verifyFormat("namespace bar {\n" 19917 "// broken:\n" 19918 "auto foo{[]() -> foo<~5> { 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<5 || 2> { return {}; }};\n" 19927 "} // namespace bar"); 19928 verifyFormat("namespace bar {\n" 19929 "// broken:\n" 19930 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 19931 "} // namespace bar"); 19932 verifyFormat("namespace bar {\n" 19933 "// broken:\n" 19934 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 19935 "} // namespace bar"); 19936 verifyFormat("namespace bar {\n" 19937 "// broken:\n" 19938 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 19939 "} // namespace bar"); 19940 verifyFormat("namespace bar {\n" 19941 "// broken:\n" 19942 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 19943 "} // namespace bar"); 19944 verifyFormat("namespace bar {\n" 19945 "// broken:\n" 19946 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 19947 "} // namespace bar"); 19948 verifyFormat("namespace bar {\n" 19949 "// broken:\n" 19950 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 19951 "} // namespace bar"); 19952 verifyFormat("namespace bar {\n" 19953 "// broken:\n" 19954 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 19955 "} // namespace bar"); 19956 verifyFormat("namespace bar {\n" 19957 "// broken:\n" 19958 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 19959 "} // namespace bar"); 19960 verifyFormat("[]() -> a<1> {};"); 19961 verifyFormat("[]() -> a<1> { ; };"); 19962 verifyFormat("[]() -> a<1> { ; }();"); 19963 verifyFormat("[a, a]() -> a<true> {};"); 19964 verifyFormat("[]() -> a<true> {};"); 19965 verifyFormat("[]() -> a<true> { ; };"); 19966 verifyFormat("[]() -> a<true> { ; }();"); 19967 verifyFormat("[a, a]() -> a<false> {};"); 19968 verifyFormat("[]() -> a<false> {};"); 19969 verifyFormat("[]() -> a<false> { ; };"); 19970 verifyFormat("[]() -> a<false> { ; }();"); 19971 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 19972 verifyFormat("namespace bar {\n" 19973 "auto foo{[]() -> foo<false> { ; }};\n" 19974 "} // namespace bar"); 19975 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 19976 " int j) -> int {\n" 19977 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 19978 "};"); 19979 verifyFormat( 19980 "aaaaaaaaaaaaaaaaaaaaaa(\n" 19981 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 19982 " return aaaaaaaaaaaaaaaaa;\n" 19983 " });", 19984 getLLVMStyleWithColumns(70)); 19985 verifyFormat("[]() //\n" 19986 " -> int {\n" 19987 " return 1; //\n" 19988 "};"); 19989 verifyFormat("[]() -> Void<T...> {};"); 19990 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 19991 19992 // Lambdas with explicit template argument lists. 19993 verifyFormat( 19994 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 19995 19996 // Multiple lambdas in the same parentheses change indentation rules. These 19997 // lambdas are forced to start on new lines. 19998 verifyFormat("SomeFunction(\n" 19999 " []() {\n" 20000 " //\n" 20001 " },\n" 20002 " []() {\n" 20003 " //\n" 20004 " });"); 20005 20006 // A lambda passed as arg0 is always pushed to the next line. 20007 verifyFormat("SomeFunction(\n" 20008 " [this] {\n" 20009 " //\n" 20010 " },\n" 20011 " 1);\n"); 20012 20013 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 20014 // the arg0 case above. 20015 auto Style = getGoogleStyle(); 20016 Style.BinPackArguments = false; 20017 verifyFormat("SomeFunction(\n" 20018 " a,\n" 20019 " [this] {\n" 20020 " //\n" 20021 " },\n" 20022 " b);\n", 20023 Style); 20024 verifyFormat("SomeFunction(\n" 20025 " a,\n" 20026 " [this] {\n" 20027 " //\n" 20028 " },\n" 20029 " b);\n"); 20030 20031 // A lambda with a very long line forces arg0 to be pushed out irrespective of 20032 // the BinPackArguments value (as long as the code is wide enough). 20033 verifyFormat( 20034 "something->SomeFunction(\n" 20035 " a,\n" 20036 " [this] {\n" 20037 " " 20038 "D0000000000000000000000000000000000000000000000000000000000001();\n" 20039 " },\n" 20040 " b);\n"); 20041 20042 // A multi-line lambda is pulled up as long as the introducer fits on the 20043 // previous line and there are no further args. 20044 verifyFormat("function(1, [this, that] {\n" 20045 " //\n" 20046 "});\n"); 20047 verifyFormat("function([this, that] {\n" 20048 " //\n" 20049 "});\n"); 20050 // FIXME: this format is not ideal and we should consider forcing the first 20051 // arg onto its own line. 20052 verifyFormat("function(a, b, c, //\n" 20053 " d, [this, that] {\n" 20054 " //\n" 20055 " });\n"); 20056 20057 // Multiple lambdas are treated correctly even when there is a short arg0. 20058 verifyFormat("SomeFunction(\n" 20059 " 1,\n" 20060 " [this] {\n" 20061 " //\n" 20062 " },\n" 20063 " [this] {\n" 20064 " //\n" 20065 " },\n" 20066 " 1);\n"); 20067 20068 // More complex introducers. 20069 verifyFormat("return [i, args...] {};"); 20070 20071 // Not lambdas. 20072 verifyFormat("constexpr char hello[]{\"hello\"};"); 20073 verifyFormat("double &operator[](int i) { return 0; }\n" 20074 "int i;"); 20075 verifyFormat("std::unique_ptr<int[]> foo() {}"); 20076 verifyFormat("int i = a[a][a]->f();"); 20077 verifyFormat("int i = (*b)[a]->f();"); 20078 20079 // Other corner cases. 20080 verifyFormat("void f() {\n" 20081 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 20082 " );\n" 20083 "}"); 20084 20085 // Lambdas created through weird macros. 20086 verifyFormat("void f() {\n" 20087 " MACRO((const AA &a) { return 1; });\n" 20088 " MACRO((AA &a) { return 1; });\n" 20089 "}"); 20090 20091 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 20092 " doo_dah();\n" 20093 " doo_dah();\n" 20094 " })) {\n" 20095 "}"); 20096 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 20097 " doo_dah();\n" 20098 " doo_dah();\n" 20099 " })) {\n" 20100 "}"); 20101 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 20102 " doo_dah();\n" 20103 " doo_dah();\n" 20104 " })) {\n" 20105 "}"); 20106 verifyFormat("auto lambda = []() {\n" 20107 " int a = 2\n" 20108 "#if A\n" 20109 " + 2\n" 20110 "#endif\n" 20111 " ;\n" 20112 "};"); 20113 20114 // Lambdas with complex multiline introducers. 20115 verifyFormat( 20116 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20117 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 20118 " -> ::std::unordered_set<\n" 20119 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 20120 " //\n" 20121 " });"); 20122 20123 FormatStyle DoNotMerge = getLLVMStyle(); 20124 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20125 verifyFormat("auto c = []() {\n" 20126 " return b;\n" 20127 "};", 20128 "auto c = []() { return b; };", DoNotMerge); 20129 verifyFormat("auto c = []() {\n" 20130 "};", 20131 " auto c = []() {};", DoNotMerge); 20132 20133 FormatStyle MergeEmptyOnly = getLLVMStyle(); 20134 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 20135 verifyFormat("auto c = []() {\n" 20136 " return b;\n" 20137 "};", 20138 "auto c = []() {\n" 20139 " return b;\n" 20140 " };", 20141 MergeEmptyOnly); 20142 verifyFormat("auto c = []() {};", 20143 "auto c = []() {\n" 20144 "};", 20145 MergeEmptyOnly); 20146 20147 FormatStyle MergeInline = getLLVMStyle(); 20148 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 20149 verifyFormat("auto c = []() {\n" 20150 " return b;\n" 20151 "};", 20152 "auto c = []() { return b; };", MergeInline); 20153 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 20154 MergeInline); 20155 verifyFormat("function([]() { return b; }, a)", 20156 "function([]() { return b; }, a)", MergeInline); 20157 verifyFormat("function(a, []() { return b; })", 20158 "function(a, []() { return b; })", MergeInline); 20159 20160 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 20161 // AllowShortLambdasOnASingleLine 20162 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20163 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20164 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20165 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20166 FormatStyle::ShortLambdaStyle::SLS_None; 20167 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 20168 " []()\n" 20169 " {\n" 20170 " return 17;\n" 20171 " });", 20172 LLVMWithBeforeLambdaBody); 20173 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 20174 " []()\n" 20175 " {\n" 20176 " });", 20177 LLVMWithBeforeLambdaBody); 20178 verifyFormat("auto fct_SLS_None = []()\n" 20179 "{\n" 20180 " return 17;\n" 20181 "};", 20182 LLVMWithBeforeLambdaBody); 20183 verifyFormat("TwoNestedLambdas_SLS_None(\n" 20184 " []()\n" 20185 " {\n" 20186 " return Call(\n" 20187 " []()\n" 20188 " {\n" 20189 " return 17;\n" 20190 " });\n" 20191 " });", 20192 LLVMWithBeforeLambdaBody); 20193 verifyFormat("void Fct() {\n" 20194 " return {[]()\n" 20195 " {\n" 20196 " return 17;\n" 20197 " }};\n" 20198 "}", 20199 LLVMWithBeforeLambdaBody); 20200 20201 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20202 FormatStyle::ShortLambdaStyle::SLS_Empty; 20203 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 20204 " []()\n" 20205 " {\n" 20206 " return 17;\n" 20207 " });", 20208 LLVMWithBeforeLambdaBody); 20209 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 20210 LLVMWithBeforeLambdaBody); 20211 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 20212 "ongFunctionName_SLS_Empty(\n" 20213 " []() {});", 20214 LLVMWithBeforeLambdaBody); 20215 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 20216 " []()\n" 20217 " {\n" 20218 " return 17;\n" 20219 " });", 20220 LLVMWithBeforeLambdaBody); 20221 verifyFormat("auto fct_SLS_Empty = []()\n" 20222 "{\n" 20223 " return 17;\n" 20224 "};", 20225 LLVMWithBeforeLambdaBody); 20226 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 20227 " []()\n" 20228 " {\n" 20229 " return Call([]() {});\n" 20230 " });", 20231 LLVMWithBeforeLambdaBody); 20232 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 20233 " []()\n" 20234 " {\n" 20235 " return Call([]() {});\n" 20236 " });", 20237 LLVMWithBeforeLambdaBody); 20238 verifyFormat( 20239 "FctWithLongLineInLambda_SLS_Empty(\n" 20240 " []()\n" 20241 " {\n" 20242 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20243 " AndShouldNotBeConsiderAsInline,\n" 20244 " LambdaBodyMustBeBreak);\n" 20245 " });", 20246 LLVMWithBeforeLambdaBody); 20247 20248 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20249 FormatStyle::ShortLambdaStyle::SLS_Inline; 20250 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 20251 LLVMWithBeforeLambdaBody); 20252 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 20253 LLVMWithBeforeLambdaBody); 20254 verifyFormat("auto fct_SLS_Inline = []()\n" 20255 "{\n" 20256 " return 17;\n" 20257 "};", 20258 LLVMWithBeforeLambdaBody); 20259 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 20260 "17; }); });", 20261 LLVMWithBeforeLambdaBody); 20262 verifyFormat( 20263 "FctWithLongLineInLambda_SLS_Inline(\n" 20264 " []()\n" 20265 " {\n" 20266 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20267 " AndShouldNotBeConsiderAsInline,\n" 20268 " LambdaBodyMustBeBreak);\n" 20269 " });", 20270 LLVMWithBeforeLambdaBody); 20271 verifyFormat("FctWithMultipleParams_SLS_Inline(" 20272 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20273 " []() { return 17; });", 20274 LLVMWithBeforeLambdaBody); 20275 verifyFormat( 20276 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 20277 LLVMWithBeforeLambdaBody); 20278 20279 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20280 FormatStyle::ShortLambdaStyle::SLS_All; 20281 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 20282 LLVMWithBeforeLambdaBody); 20283 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 20284 LLVMWithBeforeLambdaBody); 20285 verifyFormat("auto fct_SLS_All = []() { return 17; };", 20286 LLVMWithBeforeLambdaBody); 20287 verifyFormat("FctWithOneParam_SLS_All(\n" 20288 " []()\n" 20289 " {\n" 20290 " // A cool function...\n" 20291 " return 43;\n" 20292 " });", 20293 LLVMWithBeforeLambdaBody); 20294 verifyFormat("FctWithMultipleParams_SLS_All(" 20295 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20296 " []() { return 17; });", 20297 LLVMWithBeforeLambdaBody); 20298 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 20299 LLVMWithBeforeLambdaBody); 20300 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 20301 LLVMWithBeforeLambdaBody); 20302 verifyFormat( 20303 "FctWithLongLineInLambda_SLS_All(\n" 20304 " []()\n" 20305 " {\n" 20306 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20307 " AndShouldNotBeConsiderAsInline,\n" 20308 " LambdaBodyMustBeBreak);\n" 20309 " });", 20310 LLVMWithBeforeLambdaBody); 20311 verifyFormat( 20312 "auto fct_SLS_All = []()\n" 20313 "{\n" 20314 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20315 " AndShouldNotBeConsiderAsInline,\n" 20316 " LambdaBodyMustBeBreak);\n" 20317 "};", 20318 LLVMWithBeforeLambdaBody); 20319 LLVMWithBeforeLambdaBody.BinPackParameters = false; 20320 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 20321 LLVMWithBeforeLambdaBody); 20322 verifyFormat( 20323 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 20324 " FirstParam,\n" 20325 " SecondParam,\n" 20326 " ThirdParam,\n" 20327 " FourthParam);", 20328 LLVMWithBeforeLambdaBody); 20329 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20330 " []() { return " 20331 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 20332 " FirstParam,\n" 20333 " SecondParam,\n" 20334 " ThirdParam,\n" 20335 " FourthParam);", 20336 LLVMWithBeforeLambdaBody); 20337 verifyFormat( 20338 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 20339 " SecondParam,\n" 20340 " ThirdParam,\n" 20341 " FourthParam,\n" 20342 " []() { return SomeValueNotSoLong; });", 20343 LLVMWithBeforeLambdaBody); 20344 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20345 " []()\n" 20346 " {\n" 20347 " return " 20348 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 20349 "eConsiderAsInline;\n" 20350 " });", 20351 LLVMWithBeforeLambdaBody); 20352 verifyFormat( 20353 "FctWithLongLineInLambda_SLS_All(\n" 20354 " []()\n" 20355 " {\n" 20356 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20357 " AndShouldNotBeConsiderAsInline,\n" 20358 " LambdaBodyMustBeBreak);\n" 20359 " });", 20360 LLVMWithBeforeLambdaBody); 20361 verifyFormat("FctWithTwoParams_SLS_All(\n" 20362 " []()\n" 20363 " {\n" 20364 " // A cool function...\n" 20365 " return 43;\n" 20366 " },\n" 20367 " 87);", 20368 LLVMWithBeforeLambdaBody); 20369 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 20370 LLVMWithBeforeLambdaBody); 20371 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 20372 LLVMWithBeforeLambdaBody); 20373 verifyFormat( 20374 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 20375 LLVMWithBeforeLambdaBody); 20376 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 20377 "}); }, x);", 20378 LLVMWithBeforeLambdaBody); 20379 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20380 " []()\n" 20381 " {\n" 20382 " // A cool function...\n" 20383 " return Call([]() { return 17; });\n" 20384 " });", 20385 LLVMWithBeforeLambdaBody); 20386 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20387 " []()\n" 20388 " {\n" 20389 " return Call(\n" 20390 " []()\n" 20391 " {\n" 20392 " // A cool function...\n" 20393 " return 17;\n" 20394 " });\n" 20395 " });", 20396 LLVMWithBeforeLambdaBody); 20397 20398 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20399 FormatStyle::ShortLambdaStyle::SLS_None; 20400 20401 verifyFormat("auto select = [this]() -> const Library::Object *\n" 20402 "{\n" 20403 " return MyAssignment::SelectFromList(this);\n" 20404 "};\n", 20405 LLVMWithBeforeLambdaBody); 20406 20407 verifyFormat("auto select = [this]() -> const Library::Object &\n" 20408 "{\n" 20409 " return MyAssignment::SelectFromList(this);\n" 20410 "};\n", 20411 LLVMWithBeforeLambdaBody); 20412 20413 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 20414 "{\n" 20415 " return MyAssignment::SelectFromList(this);\n" 20416 "};\n", 20417 LLVMWithBeforeLambdaBody); 20418 20419 verifyFormat("namespace test {\n" 20420 "class Test {\n" 20421 "public:\n" 20422 " Test() = default;\n" 20423 "};\n" 20424 "} // namespace test", 20425 LLVMWithBeforeLambdaBody); 20426 20427 // Lambdas with different indentation styles. 20428 Style = getLLVMStyleWithColumns(100); 20429 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20430 " return promise.then(\n" 20431 " [this, &someVariable, someObject = " 20432 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20433 " return someObject.startAsyncAction().then(\n" 20434 " [this, &someVariable](AsyncActionResult result) " 20435 "mutable { result.processMore(); });\n" 20436 " });\n" 20437 "}\n", 20438 format("SomeResult doSomething(SomeObject promise) {\n" 20439 " return promise.then([this, &someVariable, someObject = " 20440 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20441 " return someObject.startAsyncAction().then([this, " 20442 "&someVariable](AsyncActionResult result) mutable {\n" 20443 " result.processMore();\n" 20444 " });\n" 20445 " });\n" 20446 "}\n", 20447 Style)); 20448 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20449 verifyFormat("test() {\n" 20450 " ([]() -> {\n" 20451 " int b = 32;\n" 20452 " return 3;\n" 20453 " }).foo();\n" 20454 "}", 20455 Style); 20456 verifyFormat("test() {\n" 20457 " []() -> {\n" 20458 " int b = 32;\n" 20459 " return 3;\n" 20460 " }\n" 20461 "}", 20462 Style); 20463 verifyFormat("std::sort(v.begin(), v.end(),\n" 20464 " [](const auto &someLongArgumentName, const auto " 20465 "&someOtherLongArgumentName) {\n" 20466 " return someLongArgumentName.someMemberVariable < " 20467 "someOtherLongArgumentName.someMemberVariable;\n" 20468 "});", 20469 Style); 20470 verifyFormat("test() {\n" 20471 " (\n" 20472 " []() -> {\n" 20473 " int b = 32;\n" 20474 " return 3;\n" 20475 " },\n" 20476 " foo, bar)\n" 20477 " .foo();\n" 20478 "}", 20479 Style); 20480 verifyFormat("test() {\n" 20481 " ([]() -> {\n" 20482 " int b = 32;\n" 20483 " return 3;\n" 20484 " })\n" 20485 " .foo()\n" 20486 " .bar();\n" 20487 "}", 20488 Style); 20489 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20490 " return promise.then(\n" 20491 " [this, &someVariable, someObject = " 20492 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20493 " return someObject.startAsyncAction().then(\n" 20494 " [this, &someVariable](AsyncActionResult result) mutable { " 20495 "result.processMore(); });\n" 20496 " });\n" 20497 "}\n", 20498 format("SomeResult doSomething(SomeObject promise) {\n" 20499 " return promise.then([this, &someVariable, someObject = " 20500 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20501 " return someObject.startAsyncAction().then([this, " 20502 "&someVariable](AsyncActionResult result) mutable {\n" 20503 " result.processMore();\n" 20504 " });\n" 20505 " });\n" 20506 "}\n", 20507 Style)); 20508 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20509 " return promise.then([this, &someVariable] {\n" 20510 " return someObject.startAsyncAction().then(\n" 20511 " [this, &someVariable](AsyncActionResult result) mutable { " 20512 "result.processMore(); });\n" 20513 " });\n" 20514 "}\n", 20515 format("SomeResult doSomething(SomeObject promise) {\n" 20516 " return promise.then([this, &someVariable] {\n" 20517 " return someObject.startAsyncAction().then([this, " 20518 "&someVariable](AsyncActionResult result) mutable {\n" 20519 " result.processMore();\n" 20520 " });\n" 20521 " });\n" 20522 "}\n", 20523 Style)); 20524 Style = getGoogleStyle(); 20525 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20526 EXPECT_EQ("#define A \\\n" 20527 " [] { \\\n" 20528 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20529 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20530 " }", 20531 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20532 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20533 Style)); 20534 // TODO: The current formatting has a minor issue that's not worth fixing 20535 // right now whereby the closing brace is indented relative to the signature 20536 // instead of being aligned. This only happens with macros. 20537 } 20538 20539 TEST_F(FormatTest, LambdaWithLineComments) { 20540 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20541 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20542 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20543 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20544 FormatStyle::ShortLambdaStyle::SLS_All; 20545 20546 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20547 verifyFormat("auto k = []() // comment\n" 20548 "{ return; }", 20549 LLVMWithBeforeLambdaBody); 20550 verifyFormat("auto k = []() /* comment */ { return; }", 20551 LLVMWithBeforeLambdaBody); 20552 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20553 LLVMWithBeforeLambdaBody); 20554 verifyFormat("auto k = []() // X\n" 20555 "{ return; }", 20556 LLVMWithBeforeLambdaBody); 20557 verifyFormat( 20558 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20559 "{ return; }", 20560 LLVMWithBeforeLambdaBody); 20561 } 20562 20563 TEST_F(FormatTest, EmptyLinesInLambdas) { 20564 verifyFormat("auto lambda = []() {\n" 20565 " x(); //\n" 20566 "};", 20567 "auto lambda = []() {\n" 20568 "\n" 20569 " x(); //\n" 20570 "\n" 20571 "};"); 20572 } 20573 20574 TEST_F(FormatTest, FormatsBlocks) { 20575 FormatStyle ShortBlocks = getLLVMStyle(); 20576 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20577 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20578 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20579 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20580 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20581 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20582 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20583 20584 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20585 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20586 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20587 20588 verifyFormat("[operation setCompletionBlock:^{\n" 20589 " [self onOperationDone];\n" 20590 "}];"); 20591 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20592 " [self onOperationDone];\n" 20593 "}]};"); 20594 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20595 " f();\n" 20596 "}];"); 20597 verifyFormat("int a = [operation block:^int(int *i) {\n" 20598 " return 1;\n" 20599 "}];"); 20600 verifyFormat("[myObject doSomethingWith:arg1\n" 20601 " aaa:^int(int *a) {\n" 20602 " return 1;\n" 20603 " }\n" 20604 " bbb:f(a * bbbbbbbb)];"); 20605 20606 verifyFormat("[operation setCompletionBlock:^{\n" 20607 " [self.delegate newDataAvailable];\n" 20608 "}];", 20609 getLLVMStyleWithColumns(60)); 20610 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20611 " NSString *path = [self sessionFilePath];\n" 20612 " if (path) {\n" 20613 " // ...\n" 20614 " }\n" 20615 "});"); 20616 verifyFormat("[[SessionService sharedService]\n" 20617 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20618 " if (window) {\n" 20619 " [self windowDidLoad:window];\n" 20620 " } else {\n" 20621 " [self errorLoadingWindow];\n" 20622 " }\n" 20623 " }];"); 20624 verifyFormat("void (^largeBlock)(void) = ^{\n" 20625 " // ...\n" 20626 "};\n", 20627 getLLVMStyleWithColumns(40)); 20628 verifyFormat("[[SessionService sharedService]\n" 20629 " loadWindowWithCompletionBlock: //\n" 20630 " ^(SessionWindow *window) {\n" 20631 " if (window) {\n" 20632 " [self windowDidLoad:window];\n" 20633 " } else {\n" 20634 " [self errorLoadingWindow];\n" 20635 " }\n" 20636 " }];", 20637 getLLVMStyleWithColumns(60)); 20638 verifyFormat("[myObject doSomethingWith:arg1\n" 20639 " firstBlock:^(Foo *a) {\n" 20640 " // ...\n" 20641 " int i;\n" 20642 " }\n" 20643 " secondBlock:^(Bar *b) {\n" 20644 " // ...\n" 20645 " int i;\n" 20646 " }\n" 20647 " thirdBlock:^Foo(Bar *b) {\n" 20648 " // ...\n" 20649 " int i;\n" 20650 " }];"); 20651 verifyFormat("[myObject doSomethingWith:arg1\n" 20652 " firstBlock:-1\n" 20653 " secondBlock:^(Bar *b) {\n" 20654 " // ...\n" 20655 " int i;\n" 20656 " }];"); 20657 20658 verifyFormat("f(^{\n" 20659 " @autoreleasepool {\n" 20660 " if (a) {\n" 20661 " g();\n" 20662 " }\n" 20663 " }\n" 20664 "});"); 20665 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20666 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20667 "};"); 20668 20669 FormatStyle FourIndent = getLLVMStyle(); 20670 FourIndent.ObjCBlockIndentWidth = 4; 20671 verifyFormat("[operation setCompletionBlock:^{\n" 20672 " [self onOperationDone];\n" 20673 "}];", 20674 FourIndent); 20675 } 20676 20677 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20678 FormatStyle ZeroColumn = getLLVMStyle(); 20679 ZeroColumn.ColumnLimit = 0; 20680 20681 verifyFormat("[[SessionService sharedService] " 20682 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20683 " if (window) {\n" 20684 " [self windowDidLoad:window];\n" 20685 " } else {\n" 20686 " [self errorLoadingWindow];\n" 20687 " }\n" 20688 "}];", 20689 ZeroColumn); 20690 EXPECT_EQ("[[SessionService sharedService]\n" 20691 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20692 " if (window) {\n" 20693 " [self windowDidLoad:window];\n" 20694 " } else {\n" 20695 " [self errorLoadingWindow];\n" 20696 " }\n" 20697 " }];", 20698 format("[[SessionService sharedService]\n" 20699 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20700 " if (window) {\n" 20701 " [self windowDidLoad:window];\n" 20702 " } else {\n" 20703 " [self errorLoadingWindow];\n" 20704 " }\n" 20705 "}];", 20706 ZeroColumn)); 20707 verifyFormat("[myObject doSomethingWith:arg1\n" 20708 " firstBlock:^(Foo *a) {\n" 20709 " // ...\n" 20710 " int i;\n" 20711 " }\n" 20712 " secondBlock:^(Bar *b) {\n" 20713 " // ...\n" 20714 " int i;\n" 20715 " }\n" 20716 " thirdBlock:^Foo(Bar *b) {\n" 20717 " // ...\n" 20718 " int i;\n" 20719 " }];", 20720 ZeroColumn); 20721 verifyFormat("f(^{\n" 20722 " @autoreleasepool {\n" 20723 " if (a) {\n" 20724 " g();\n" 20725 " }\n" 20726 " }\n" 20727 "});", 20728 ZeroColumn); 20729 verifyFormat("void (^largeBlock)(void) = ^{\n" 20730 " // ...\n" 20731 "};", 20732 ZeroColumn); 20733 20734 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20735 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20736 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20737 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20738 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20739 " int i;\n" 20740 "};", 20741 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20742 } 20743 20744 TEST_F(FormatTest, SupportsCRLF) { 20745 EXPECT_EQ("int a;\r\n" 20746 "int b;\r\n" 20747 "int c;\r\n", 20748 format("int a;\r\n" 20749 " int b;\r\n" 20750 " int c;\r\n", 20751 getLLVMStyle())); 20752 EXPECT_EQ("int a;\r\n" 20753 "int b;\r\n" 20754 "int c;\r\n", 20755 format("int a;\r\n" 20756 " int b;\n" 20757 " int c;\r\n", 20758 getLLVMStyle())); 20759 EXPECT_EQ("int a;\n" 20760 "int b;\n" 20761 "int c;\n", 20762 format("int a;\r\n" 20763 " int b;\n" 20764 " int c;\n", 20765 getLLVMStyle())); 20766 EXPECT_EQ("\"aaaaaaa \"\r\n" 20767 "\"bbbbbbb\";\r\n", 20768 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20769 EXPECT_EQ("#define A \\\r\n" 20770 " b; \\\r\n" 20771 " c; \\\r\n" 20772 " d;\r\n", 20773 format("#define A \\\r\n" 20774 " b; \\\r\n" 20775 " c; d; \r\n", 20776 getGoogleStyle())); 20777 20778 EXPECT_EQ("/*\r\n" 20779 "multi line block comments\r\n" 20780 "should not introduce\r\n" 20781 "an extra carriage return\r\n" 20782 "*/\r\n", 20783 format("/*\r\n" 20784 "multi line block comments\r\n" 20785 "should not introduce\r\n" 20786 "an extra carriage return\r\n" 20787 "*/\r\n")); 20788 EXPECT_EQ("/*\r\n" 20789 "\r\n" 20790 "*/", 20791 format("/*\r\n" 20792 " \r\r\r\n" 20793 "*/")); 20794 20795 FormatStyle style = getLLVMStyle(); 20796 20797 style.DeriveLineEnding = true; 20798 style.UseCRLF = false; 20799 EXPECT_EQ("union FooBarBazQux {\n" 20800 " int foo;\n" 20801 " int bar;\n" 20802 " int baz;\n" 20803 "};", 20804 format("union FooBarBazQux {\r\n" 20805 " int foo;\n" 20806 " int bar;\r\n" 20807 " int baz;\n" 20808 "};", 20809 style)); 20810 style.UseCRLF = true; 20811 EXPECT_EQ("union FooBarBazQux {\r\n" 20812 " int foo;\r\n" 20813 " int bar;\r\n" 20814 " int baz;\r\n" 20815 "};", 20816 format("union FooBarBazQux {\r\n" 20817 " int foo;\n" 20818 " int bar;\r\n" 20819 " int baz;\n" 20820 "};", 20821 style)); 20822 20823 style.DeriveLineEnding = false; 20824 style.UseCRLF = false; 20825 EXPECT_EQ("union FooBarBazQux {\n" 20826 " int foo;\n" 20827 " int bar;\n" 20828 " int baz;\n" 20829 " int qux;\n" 20830 "};", 20831 format("union FooBarBazQux {\r\n" 20832 " int foo;\n" 20833 " int bar;\r\n" 20834 " int baz;\n" 20835 " int qux;\r\n" 20836 "};", 20837 style)); 20838 style.UseCRLF = true; 20839 EXPECT_EQ("union FooBarBazQux {\r\n" 20840 " int foo;\r\n" 20841 " int bar;\r\n" 20842 " int baz;\r\n" 20843 " int qux;\r\n" 20844 "};", 20845 format("union FooBarBazQux {\r\n" 20846 " int foo;\n" 20847 " int bar;\r\n" 20848 " int baz;\n" 20849 " int qux;\n" 20850 "};", 20851 style)); 20852 20853 style.DeriveLineEnding = true; 20854 style.UseCRLF = false; 20855 EXPECT_EQ("union FooBarBazQux {\r\n" 20856 " int foo;\r\n" 20857 " int bar;\r\n" 20858 " int baz;\r\n" 20859 " int qux;\r\n" 20860 "};", 20861 format("union FooBarBazQux {\r\n" 20862 " int foo;\n" 20863 " int bar;\r\n" 20864 " int baz;\n" 20865 " int qux;\r\n" 20866 "};", 20867 style)); 20868 style.UseCRLF = true; 20869 EXPECT_EQ("union FooBarBazQux {\n" 20870 " int foo;\n" 20871 " int bar;\n" 20872 " int baz;\n" 20873 " int qux;\n" 20874 "};", 20875 format("union FooBarBazQux {\r\n" 20876 " int foo;\n" 20877 " int bar;\r\n" 20878 " int baz;\n" 20879 " int qux;\n" 20880 "};", 20881 style)); 20882 } 20883 20884 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 20885 verifyFormat("MY_CLASS(C) {\n" 20886 " int i;\n" 20887 " int j;\n" 20888 "};"); 20889 } 20890 20891 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 20892 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 20893 TwoIndent.ContinuationIndentWidth = 2; 20894 20895 EXPECT_EQ("int i =\n" 20896 " longFunction(\n" 20897 " arg);", 20898 format("int i = longFunction(arg);", TwoIndent)); 20899 20900 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 20901 SixIndent.ContinuationIndentWidth = 6; 20902 20903 EXPECT_EQ("int i =\n" 20904 " longFunction(\n" 20905 " arg);", 20906 format("int i = longFunction(arg);", SixIndent)); 20907 } 20908 20909 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 20910 FormatStyle Style = getLLVMStyle(); 20911 verifyFormat("int Foo::getter(\n" 20912 " //\n" 20913 ") const {\n" 20914 " return foo;\n" 20915 "}", 20916 Style); 20917 verifyFormat("void Foo::setter(\n" 20918 " //\n" 20919 ") {\n" 20920 " foo = 1;\n" 20921 "}", 20922 Style); 20923 } 20924 20925 TEST_F(FormatTest, SpacesInAngles) { 20926 FormatStyle Spaces = getLLVMStyle(); 20927 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20928 20929 verifyFormat("vector< ::std::string > x1;", Spaces); 20930 verifyFormat("Foo< int, Bar > x2;", Spaces); 20931 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 20932 20933 verifyFormat("static_cast< int >(arg);", Spaces); 20934 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 20935 verifyFormat("f< int, float >();", Spaces); 20936 verifyFormat("template <> g() {}", Spaces); 20937 verifyFormat("template < std::vector< int > > f() {}", Spaces); 20938 verifyFormat("std::function< void(int, int) > fct;", Spaces); 20939 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 20940 Spaces); 20941 20942 Spaces.Standard = FormatStyle::LS_Cpp03; 20943 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20944 verifyFormat("A< A< int > >();", Spaces); 20945 20946 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20947 verifyFormat("A<A<int> >();", Spaces); 20948 20949 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20950 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 20951 Spaces); 20952 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 20953 Spaces); 20954 20955 verifyFormat("A<A<int> >();", Spaces); 20956 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 20957 verifyFormat("A< A< int > >();", Spaces); 20958 20959 Spaces.Standard = FormatStyle::LS_Cpp11; 20960 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20961 verifyFormat("A< A< int > >();", Spaces); 20962 20963 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20964 verifyFormat("vector<::std::string> x4;", Spaces); 20965 verifyFormat("vector<int> x5;", Spaces); 20966 verifyFormat("Foo<int, Bar> x6;", Spaces); 20967 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20968 20969 verifyFormat("A<A<int>>();", Spaces); 20970 20971 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20972 verifyFormat("vector<::std::string> x4;", Spaces); 20973 verifyFormat("vector< ::std::string > x4;", Spaces); 20974 verifyFormat("vector<int> x5;", Spaces); 20975 verifyFormat("vector< int > x5;", Spaces); 20976 verifyFormat("Foo<int, Bar> x6;", Spaces); 20977 verifyFormat("Foo< int, Bar > x6;", Spaces); 20978 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20979 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 20980 20981 verifyFormat("A<A<int>>();", Spaces); 20982 verifyFormat("A< A< int > >();", Spaces); 20983 verifyFormat("A<A<int > >();", Spaces); 20984 verifyFormat("A< A< int>>();", Spaces); 20985 } 20986 20987 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 20988 FormatStyle Style = getLLVMStyle(); 20989 Style.SpaceAfterTemplateKeyword = false; 20990 verifyFormat("template<int> void foo();", Style); 20991 } 20992 20993 TEST_F(FormatTest, TripleAngleBrackets) { 20994 verifyFormat("f<<<1, 1>>>();"); 20995 verifyFormat("f<<<1, 1, 1, s>>>();"); 20996 verifyFormat("f<<<a, b, c, d>>>();"); 20997 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 20998 verifyFormat("f<param><<<1, 1>>>();"); 20999 verifyFormat("f<1><<<1, 1>>>();"); 21000 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 21001 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21002 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 21003 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 21004 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 21005 } 21006 21007 TEST_F(FormatTest, MergeLessLessAtEnd) { 21008 verifyFormat("<<"); 21009 EXPECT_EQ("< < <", format("\\\n<<<")); 21010 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21011 "aaallvm::outs() <<"); 21012 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21013 "aaaallvm::outs()\n <<"); 21014 } 21015 21016 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 21017 std::string code = "#if A\n" 21018 "#if B\n" 21019 "a.\n" 21020 "#endif\n" 21021 " a = 1;\n" 21022 "#else\n" 21023 "#endif\n" 21024 "#if C\n" 21025 "#else\n" 21026 "#endif\n"; 21027 EXPECT_EQ(code, format(code)); 21028 } 21029 21030 TEST_F(FormatTest, HandleConflictMarkers) { 21031 // Git/SVN conflict markers. 21032 EXPECT_EQ("int a;\n" 21033 "void f() {\n" 21034 " callme(some(parameter1,\n" 21035 "<<<<<<< text by the vcs\n" 21036 " parameter2),\n" 21037 "||||||| text by the vcs\n" 21038 " parameter2),\n" 21039 " parameter3,\n" 21040 "======= text by the vcs\n" 21041 " parameter2, parameter3),\n" 21042 ">>>>>>> text by the vcs\n" 21043 " otherparameter);\n", 21044 format("int a;\n" 21045 "void f() {\n" 21046 " callme(some(parameter1,\n" 21047 "<<<<<<< text by the vcs\n" 21048 " parameter2),\n" 21049 "||||||| text by the vcs\n" 21050 " parameter2),\n" 21051 " parameter3,\n" 21052 "======= text by the vcs\n" 21053 " parameter2,\n" 21054 " parameter3),\n" 21055 ">>>>>>> text by the vcs\n" 21056 " otherparameter);\n")); 21057 21058 // Perforce markers. 21059 EXPECT_EQ("void f() {\n" 21060 " function(\n" 21061 ">>>> text by the vcs\n" 21062 " parameter,\n" 21063 "==== text by the vcs\n" 21064 " parameter,\n" 21065 "==== text by the vcs\n" 21066 " parameter,\n" 21067 "<<<< text by the vcs\n" 21068 " parameter);\n", 21069 format("void f() {\n" 21070 " function(\n" 21071 ">>>> text by the vcs\n" 21072 " parameter,\n" 21073 "==== text by the vcs\n" 21074 " parameter,\n" 21075 "==== text by the vcs\n" 21076 " parameter,\n" 21077 "<<<< text by the vcs\n" 21078 " parameter);\n")); 21079 21080 EXPECT_EQ("<<<<<<<\n" 21081 "|||||||\n" 21082 "=======\n" 21083 ">>>>>>>", 21084 format("<<<<<<<\n" 21085 "|||||||\n" 21086 "=======\n" 21087 ">>>>>>>")); 21088 21089 EXPECT_EQ("<<<<<<<\n" 21090 "|||||||\n" 21091 "int i;\n" 21092 "=======\n" 21093 ">>>>>>>", 21094 format("<<<<<<<\n" 21095 "|||||||\n" 21096 "int i;\n" 21097 "=======\n" 21098 ">>>>>>>")); 21099 21100 // FIXME: Handle parsing of macros around conflict markers correctly: 21101 EXPECT_EQ("#define Macro \\\n" 21102 "<<<<<<<\n" 21103 "Something \\\n" 21104 "|||||||\n" 21105 "Else \\\n" 21106 "=======\n" 21107 "Other \\\n" 21108 ">>>>>>>\n" 21109 " End int i;\n", 21110 format("#define Macro \\\n" 21111 "<<<<<<<\n" 21112 " Something \\\n" 21113 "|||||||\n" 21114 " Else \\\n" 21115 "=======\n" 21116 " Other \\\n" 21117 ">>>>>>>\n" 21118 " End\n" 21119 "int i;\n")); 21120 } 21121 21122 TEST_F(FormatTest, DisableRegions) { 21123 EXPECT_EQ("int i;\n" 21124 "// clang-format off\n" 21125 " int j;\n" 21126 "// clang-format on\n" 21127 "int k;", 21128 format(" int i;\n" 21129 " // clang-format off\n" 21130 " int j;\n" 21131 " // clang-format on\n" 21132 " int k;")); 21133 EXPECT_EQ("int i;\n" 21134 "/* clang-format off */\n" 21135 " int j;\n" 21136 "/* clang-format on */\n" 21137 "int k;", 21138 format(" int i;\n" 21139 " /* clang-format off */\n" 21140 " int j;\n" 21141 " /* clang-format on */\n" 21142 " int k;")); 21143 21144 // Don't reflow comments within disabled regions. 21145 EXPECT_EQ("// clang-format off\n" 21146 "// long long long long long long line\n" 21147 "/* clang-format on */\n" 21148 "/* long long long\n" 21149 " * long long long\n" 21150 " * line */\n" 21151 "int i;\n" 21152 "/* clang-format off */\n" 21153 "/* long long long long long long line */\n", 21154 format("// clang-format off\n" 21155 "// long long long long long long line\n" 21156 "/* clang-format on */\n" 21157 "/* long long long long long long line */\n" 21158 "int i;\n" 21159 "/* clang-format off */\n" 21160 "/* long long long long long long line */\n", 21161 getLLVMStyleWithColumns(20))); 21162 } 21163 21164 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 21165 format("? ) ="); 21166 verifyNoCrash("#define a\\\n /**/}"); 21167 } 21168 21169 TEST_F(FormatTest, FormatsTableGenCode) { 21170 FormatStyle Style = getLLVMStyle(); 21171 Style.Language = FormatStyle::LK_TableGen; 21172 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 21173 } 21174 21175 TEST_F(FormatTest, ArrayOfTemplates) { 21176 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 21177 format("auto a = new unique_ptr<int > [ 10];")); 21178 21179 FormatStyle Spaces = getLLVMStyle(); 21180 Spaces.SpacesInSquareBrackets = true; 21181 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 21182 format("auto a = new unique_ptr<int > [10];", Spaces)); 21183 } 21184 21185 TEST_F(FormatTest, ArrayAsTemplateType) { 21186 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 21187 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 21188 21189 FormatStyle Spaces = getLLVMStyle(); 21190 Spaces.SpacesInSquareBrackets = true; 21191 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 21192 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 21193 } 21194 21195 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 21196 21197 TEST(FormatStyle, GetStyleWithEmptyFileName) { 21198 llvm::vfs::InMemoryFileSystem FS; 21199 auto Style1 = getStyle("file", "", "Google", "", &FS); 21200 ASSERT_TRUE((bool)Style1); 21201 ASSERT_EQ(*Style1, getGoogleStyle()); 21202 } 21203 21204 TEST(FormatStyle, GetStyleOfFile) { 21205 llvm::vfs::InMemoryFileSystem FS; 21206 // Test 1: format file in the same directory. 21207 ASSERT_TRUE( 21208 FS.addFile("/a/.clang-format", 0, 21209 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 21210 ASSERT_TRUE( 21211 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21212 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 21213 ASSERT_TRUE((bool)Style1); 21214 ASSERT_EQ(*Style1, getLLVMStyle()); 21215 21216 // Test 2.1: fallback to default. 21217 ASSERT_TRUE( 21218 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21219 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 21220 ASSERT_TRUE((bool)Style2); 21221 ASSERT_EQ(*Style2, getMozillaStyle()); 21222 21223 // Test 2.2: no format on 'none' fallback style. 21224 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21225 ASSERT_TRUE((bool)Style2); 21226 ASSERT_EQ(*Style2, getNoStyle()); 21227 21228 // Test 2.3: format if config is found with no based style while fallback is 21229 // 'none'. 21230 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 21231 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 21232 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21233 ASSERT_TRUE((bool)Style2); 21234 ASSERT_EQ(*Style2, getLLVMStyle()); 21235 21236 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 21237 Style2 = getStyle("{}", "a.h", "none", "", &FS); 21238 ASSERT_TRUE((bool)Style2); 21239 ASSERT_EQ(*Style2, getLLVMStyle()); 21240 21241 // Test 3: format file in parent directory. 21242 ASSERT_TRUE( 21243 FS.addFile("/c/.clang-format", 0, 21244 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 21245 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 21246 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21247 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 21248 ASSERT_TRUE((bool)Style3); 21249 ASSERT_EQ(*Style3, getGoogleStyle()); 21250 21251 // Test 4: error on invalid fallback style 21252 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 21253 ASSERT_FALSE((bool)Style4); 21254 llvm::consumeError(Style4.takeError()); 21255 21256 // Test 5: error on invalid yaml on command line 21257 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 21258 ASSERT_FALSE((bool)Style5); 21259 llvm::consumeError(Style5.takeError()); 21260 21261 // Test 6: error on invalid style 21262 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 21263 ASSERT_FALSE((bool)Style6); 21264 llvm::consumeError(Style6.takeError()); 21265 21266 // Test 7: found config file, error on parsing it 21267 ASSERT_TRUE( 21268 FS.addFile("/d/.clang-format", 0, 21269 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 21270 "InvalidKey: InvalidValue"))); 21271 ASSERT_TRUE( 21272 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21273 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 21274 ASSERT_FALSE((bool)Style7a); 21275 llvm::consumeError(Style7a.takeError()); 21276 21277 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 21278 ASSERT_TRUE((bool)Style7b); 21279 21280 // Test 8: inferred per-language defaults apply. 21281 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 21282 ASSERT_TRUE((bool)StyleTd); 21283 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 21284 21285 // Test 9.1: overwriting a file style, when parent no file exists with no 21286 // fallback style 21287 ASSERT_TRUE(FS.addFile( 21288 "/e/sub/.clang-format", 0, 21289 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 21290 "ColumnLimit: 20"))); 21291 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 21292 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21293 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21294 ASSERT_TRUE(static_cast<bool>(Style9)); 21295 ASSERT_EQ(*Style9, [] { 21296 auto Style = getNoStyle(); 21297 Style.ColumnLimit = 20; 21298 return Style; 21299 }()); 21300 21301 // Test 9.2: with LLVM fallback style 21302 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 21303 ASSERT_TRUE(static_cast<bool>(Style9)); 21304 ASSERT_EQ(*Style9, [] { 21305 auto Style = getLLVMStyle(); 21306 Style.ColumnLimit = 20; 21307 return Style; 21308 }()); 21309 21310 // Test 9.3: with a parent file 21311 ASSERT_TRUE( 21312 FS.addFile("/e/.clang-format", 0, 21313 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 21314 "UseTab: Always"))); 21315 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21316 ASSERT_TRUE(static_cast<bool>(Style9)); 21317 ASSERT_EQ(*Style9, [] { 21318 auto Style = getGoogleStyle(); 21319 Style.ColumnLimit = 20; 21320 Style.UseTab = FormatStyle::UT_Always; 21321 return Style; 21322 }()); 21323 21324 // Test 9.4: propagate more than one level 21325 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 21326 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21327 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 21328 llvm::MemoryBuffer::getMemBuffer( 21329 "BasedOnStyle: InheritParentConfig\n" 21330 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 21331 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 21332 21333 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 21334 auto Style = getGoogleStyle(); 21335 Style.ColumnLimit = 20; 21336 Style.UseTab = FormatStyle::UT_Always; 21337 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 21338 return Style; 21339 }(); 21340 21341 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 21342 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 21343 ASSERT_TRUE(static_cast<bool>(Style9)); 21344 ASSERT_EQ(*Style9, SubSubStyle); 21345 21346 // Test 9.5: use InheritParentConfig as style name 21347 Style9 = 21348 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 21349 ASSERT_TRUE(static_cast<bool>(Style9)); 21350 ASSERT_EQ(*Style9, SubSubStyle); 21351 21352 // Test 9.6: use command line style with inheritance 21353 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 21354 "none", "", &FS); 21355 ASSERT_TRUE(static_cast<bool>(Style9)); 21356 ASSERT_EQ(*Style9, SubSubStyle); 21357 21358 // Test 9.7: use command line style with inheritance and own config 21359 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 21360 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 21361 "/e/sub/code.cpp", "none", "", &FS); 21362 ASSERT_TRUE(static_cast<bool>(Style9)); 21363 ASSERT_EQ(*Style9, SubSubStyle); 21364 21365 // Test 9.8: use inheritance from a file without BasedOnStyle 21366 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 21367 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 21368 ASSERT_TRUE( 21369 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 21370 llvm::MemoryBuffer::getMemBuffer( 21371 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 21372 // Make sure we do not use the fallback style 21373 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 21374 ASSERT_TRUE(static_cast<bool>(Style9)); 21375 ASSERT_EQ(*Style9, [] { 21376 auto Style = getLLVMStyle(); 21377 Style.ColumnLimit = 123; 21378 return Style; 21379 }()); 21380 21381 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 21382 ASSERT_TRUE(static_cast<bool>(Style9)); 21383 ASSERT_EQ(*Style9, [] { 21384 auto Style = getLLVMStyle(); 21385 Style.ColumnLimit = 123; 21386 Style.IndentWidth = 7; 21387 return Style; 21388 }()); 21389 } 21390 21391 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 21392 // Column limit is 20. 21393 std::string Code = "Type *a =\n" 21394 " new Type();\n" 21395 "g(iiiii, 0, jjjjj,\n" 21396 " 0, kkkkk, 0, mm);\n" 21397 "int bad = format ;"; 21398 std::string Expected = "auto a = new Type();\n" 21399 "g(iiiii, nullptr,\n" 21400 " jjjjj, nullptr,\n" 21401 " kkkkk, nullptr,\n" 21402 " mm);\n" 21403 "int bad = format ;"; 21404 FileID ID = Context.createInMemoryFile("format.cpp", Code); 21405 tooling::Replacements Replaces = toReplacements( 21406 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 21407 "auto "), 21408 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 21409 "nullptr"), 21410 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 21411 "nullptr"), 21412 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 21413 "nullptr")}); 21414 21415 format::FormatStyle Style = format::getLLVMStyle(); 21416 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 21417 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21418 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21419 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21420 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21421 EXPECT_TRUE(static_cast<bool>(Result)); 21422 EXPECT_EQ(Expected, *Result); 21423 } 21424 21425 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 21426 std::string Code = "#include \"a.h\"\n" 21427 "#include \"c.h\"\n" 21428 "\n" 21429 "int main() {\n" 21430 " return 0;\n" 21431 "}"; 21432 std::string Expected = "#include \"a.h\"\n" 21433 "#include \"b.h\"\n" 21434 "#include \"c.h\"\n" 21435 "\n" 21436 "int main() {\n" 21437 " return 0;\n" 21438 "}"; 21439 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 21440 tooling::Replacements Replaces = toReplacements( 21441 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 21442 "#include \"b.h\"\n")}); 21443 21444 format::FormatStyle Style = format::getLLVMStyle(); 21445 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 21446 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21447 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21448 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21449 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21450 EXPECT_TRUE(static_cast<bool>(Result)); 21451 EXPECT_EQ(Expected, *Result); 21452 } 21453 21454 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 21455 EXPECT_EQ("using std::cin;\n" 21456 "using std::cout;", 21457 format("using std::cout;\n" 21458 "using std::cin;", 21459 getGoogleStyle())); 21460 } 21461 21462 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21463 format::FormatStyle Style = format::getLLVMStyle(); 21464 Style.Standard = FormatStyle::LS_Cpp03; 21465 // cpp03 recognize this string as identifier u8 and literal character 'a' 21466 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21467 } 21468 21469 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21470 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21471 // all modes, including C++11, C++14 and C++17 21472 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21473 } 21474 21475 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21476 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21477 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21478 } 21479 21480 TEST_F(FormatTest, StructuredBindings) { 21481 // Structured bindings is a C++17 feature. 21482 // all modes, including C++11, C++14 and C++17 21483 verifyFormat("auto [a, b] = f();"); 21484 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21485 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21486 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21487 EXPECT_EQ("auto const volatile [a, b] = f();", 21488 format("auto const volatile[a, b] = f();")); 21489 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21490 EXPECT_EQ("auto &[a, b, c] = f();", 21491 format("auto &[ a , b,c ] = f();")); 21492 EXPECT_EQ("auto &&[a, b, c] = f();", 21493 format("auto &&[ a , b,c ] = f();")); 21494 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21495 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21496 format("auto const volatile &&[a, b] = f();")); 21497 EXPECT_EQ("auto const &&[a, b] = f();", 21498 format("auto const && [a, b] = f();")); 21499 EXPECT_EQ("const auto &[a, b] = f();", 21500 format("const auto & [a, b] = f();")); 21501 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21502 format("const auto volatile &&[a, b] = f();")); 21503 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21504 format("volatile const auto &&[a, b] = f();")); 21505 EXPECT_EQ("const auto &&[a, b] = f();", 21506 format("const auto && [a, b] = f();")); 21507 21508 // Make sure we don't mistake structured bindings for lambdas. 21509 FormatStyle PointerMiddle = getLLVMStyle(); 21510 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21511 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21512 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21513 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21514 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21515 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21516 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21517 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21518 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21519 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21520 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21521 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21522 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21523 21524 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21525 format("for (const auto && [a, b] : some_range) {\n}")); 21526 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21527 format("for (const auto & [a, b] : some_range) {\n}")); 21528 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21529 format("for (const auto[a, b] : some_range) {\n}")); 21530 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21531 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21532 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21533 EXPECT_EQ("auto const &[x, y](expr);", 21534 format("auto const & [x,y] (expr);")); 21535 EXPECT_EQ("auto const &&[x, y](expr);", 21536 format("auto const && [x,y] (expr);")); 21537 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21538 EXPECT_EQ("auto const &[x, y]{expr};", 21539 format("auto const & [x,y] {expr};")); 21540 EXPECT_EQ("auto const &&[x, y]{expr};", 21541 format("auto const && [x,y] {expr};")); 21542 21543 format::FormatStyle Spaces = format::getLLVMStyle(); 21544 Spaces.SpacesInSquareBrackets = true; 21545 verifyFormat("auto [ a, b ] = f();", Spaces); 21546 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21547 verifyFormat("auto &[ a, b ] = f();", Spaces); 21548 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21549 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21550 } 21551 21552 TEST_F(FormatTest, FileAndCode) { 21553 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21554 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21555 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21556 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21557 EXPECT_EQ(FormatStyle::LK_ObjC, 21558 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21559 EXPECT_EQ( 21560 FormatStyle::LK_ObjC, 21561 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21562 EXPECT_EQ(FormatStyle::LK_ObjC, 21563 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21564 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21565 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21566 EXPECT_EQ(FormatStyle::LK_ObjC, 21567 guessLanguage("foo", "@interface Foo\n@end\n")); 21568 EXPECT_EQ(FormatStyle::LK_ObjC, 21569 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21570 EXPECT_EQ( 21571 FormatStyle::LK_ObjC, 21572 guessLanguage("foo.h", 21573 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21574 EXPECT_EQ( 21575 FormatStyle::LK_Cpp, 21576 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21577 } 21578 21579 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21580 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21581 EXPECT_EQ(FormatStyle::LK_ObjC, 21582 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21583 EXPECT_EQ(FormatStyle::LK_Cpp, 21584 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21585 EXPECT_EQ( 21586 FormatStyle::LK_Cpp, 21587 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21588 EXPECT_EQ(FormatStyle::LK_ObjC, 21589 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21590 EXPECT_EQ(FormatStyle::LK_Cpp, 21591 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21592 EXPECT_EQ(FormatStyle::LK_ObjC, 21593 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21594 EXPECT_EQ(FormatStyle::LK_Cpp, 21595 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21596 EXPECT_EQ(FormatStyle::LK_Cpp, 21597 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21598 EXPECT_EQ(FormatStyle::LK_ObjC, 21599 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21600 EXPECT_EQ(FormatStyle::LK_Cpp, 21601 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21602 EXPECT_EQ( 21603 FormatStyle::LK_Cpp, 21604 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21605 EXPECT_EQ( 21606 FormatStyle::LK_Cpp, 21607 guessLanguage("foo.h", 21608 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 21609 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 21610 } 21611 21612 TEST_F(FormatTest, GuessLanguageWithCaret) { 21613 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 21614 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 21615 EXPECT_EQ(FormatStyle::LK_ObjC, 21616 guessLanguage("foo.h", "int(^)(char, float);")); 21617 EXPECT_EQ(FormatStyle::LK_ObjC, 21618 guessLanguage("foo.h", "int(^foo)(char, float);")); 21619 EXPECT_EQ(FormatStyle::LK_ObjC, 21620 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 21621 EXPECT_EQ(FormatStyle::LK_ObjC, 21622 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 21623 EXPECT_EQ( 21624 FormatStyle::LK_ObjC, 21625 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 21626 } 21627 21628 TEST_F(FormatTest, GuessLanguageWithPragmas) { 21629 EXPECT_EQ(FormatStyle::LK_Cpp, 21630 guessLanguage("foo.h", "__pragma(warning(disable:))")); 21631 EXPECT_EQ(FormatStyle::LK_Cpp, 21632 guessLanguage("foo.h", "#pragma(warning(disable:))")); 21633 EXPECT_EQ(FormatStyle::LK_Cpp, 21634 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 21635 } 21636 21637 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 21638 // ASM symbolic names are identifiers that must be surrounded by [] without 21639 // space in between: 21640 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 21641 21642 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 21643 verifyFormat(R"(// 21644 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 21645 )"); 21646 21647 // A list of several ASM symbolic names. 21648 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 21649 21650 // ASM symbolic names in inline ASM with inputs and outputs. 21651 verifyFormat(R"(// 21652 asm("cmoveq %1, %2, %[result]" 21653 : [result] "=r"(result) 21654 : "r"(test), "r"(new), "[result]"(old)); 21655 )"); 21656 21657 // ASM symbolic names in inline ASM with no outputs. 21658 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 21659 } 21660 21661 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 21662 EXPECT_EQ(FormatStyle::LK_Cpp, 21663 guessLanguage("foo.h", "void f() {\n" 21664 " asm (\"mov %[e], %[d]\"\n" 21665 " : [d] \"=rm\" (d)\n" 21666 " [e] \"rm\" (*e));\n" 21667 "}")); 21668 EXPECT_EQ(FormatStyle::LK_Cpp, 21669 guessLanguage("foo.h", "void f() {\n" 21670 " _asm (\"mov %[e], %[d]\"\n" 21671 " : [d] \"=rm\" (d)\n" 21672 " [e] \"rm\" (*e));\n" 21673 "}")); 21674 EXPECT_EQ(FormatStyle::LK_Cpp, 21675 guessLanguage("foo.h", "void f() {\n" 21676 " __asm (\"mov %[e], %[d]\"\n" 21677 " : [d] \"=rm\" (d)\n" 21678 " [e] \"rm\" (*e));\n" 21679 "}")); 21680 EXPECT_EQ(FormatStyle::LK_Cpp, 21681 guessLanguage("foo.h", "void f() {\n" 21682 " __asm__ (\"mov %[e], %[d]\"\n" 21683 " : [d] \"=rm\" (d)\n" 21684 " [e] \"rm\" (*e));\n" 21685 "}")); 21686 EXPECT_EQ(FormatStyle::LK_Cpp, 21687 guessLanguage("foo.h", "void f() {\n" 21688 " asm (\"mov %[e], %[d]\"\n" 21689 " : [d] \"=rm\" (d),\n" 21690 " [e] \"rm\" (*e));\n" 21691 "}")); 21692 EXPECT_EQ(FormatStyle::LK_Cpp, 21693 guessLanguage("foo.h", "void f() {\n" 21694 " asm volatile (\"mov %[e], %[d]\"\n" 21695 " : [d] \"=rm\" (d)\n" 21696 " [e] \"rm\" (*e));\n" 21697 "}")); 21698 } 21699 21700 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21701 EXPECT_EQ(FormatStyle::LK_Cpp, 21702 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21703 EXPECT_EQ(FormatStyle::LK_ObjC, 21704 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21705 EXPECT_EQ( 21706 FormatStyle::LK_Cpp, 21707 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21708 EXPECT_EQ( 21709 FormatStyle::LK_ObjC, 21710 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21711 } 21712 21713 TEST_F(FormatTest, TypenameMacros) { 21714 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21715 21716 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21717 FormatStyle Google = getGoogleStyleWithColumns(0); 21718 Google.TypenameMacros = TypenameMacros; 21719 verifyFormat("struct foo {\n" 21720 " int bar;\n" 21721 " TAILQ_ENTRY(a) bleh;\n" 21722 "};", 21723 Google); 21724 21725 FormatStyle Macros = getLLVMStyle(); 21726 Macros.TypenameMacros = TypenameMacros; 21727 21728 verifyFormat("STACK_OF(int) a;", Macros); 21729 verifyFormat("STACK_OF(int) *a;", Macros); 21730 verifyFormat("STACK_OF(int const *) *a;", Macros); 21731 verifyFormat("STACK_OF(int *const) *a;", Macros); 21732 verifyFormat("STACK_OF(int, string) a;", Macros); 21733 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21734 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21735 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21736 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21737 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21738 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21739 21740 Macros.PointerAlignment = FormatStyle::PAS_Left; 21741 verifyFormat("STACK_OF(int)* a;", Macros); 21742 verifyFormat("STACK_OF(int*)* a;", Macros); 21743 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21744 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21745 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21746 } 21747 21748 TEST_F(FormatTest, AtomicQualifier) { 21749 // Check that we treate _Atomic as a type and not a function call 21750 FormatStyle Google = getGoogleStyleWithColumns(0); 21751 verifyFormat("struct foo {\n" 21752 " int a1;\n" 21753 " _Atomic(a) a2;\n" 21754 " _Atomic(_Atomic(int) *const) a3;\n" 21755 "};", 21756 Google); 21757 verifyFormat("_Atomic(uint64_t) a;"); 21758 verifyFormat("_Atomic(uint64_t) *a;"); 21759 verifyFormat("_Atomic(uint64_t const *) *a;"); 21760 verifyFormat("_Atomic(uint64_t *const) *a;"); 21761 verifyFormat("_Atomic(const uint64_t *) *a;"); 21762 verifyFormat("_Atomic(uint64_t) a;"); 21763 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21764 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21765 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21766 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21767 21768 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21769 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21770 FormatStyle Style = getLLVMStyle(); 21771 Style.PointerAlignment = FormatStyle::PAS_Left; 21772 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21773 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21774 verifyFormat("_Atomic(int)* a;", Style); 21775 verifyFormat("_Atomic(int*)* a;", Style); 21776 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21777 21778 Style.SpacesInCStyleCastParentheses = true; 21779 Style.SpacesInParentheses = false; 21780 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21781 Style.SpacesInCStyleCastParentheses = false; 21782 Style.SpacesInParentheses = true; 21783 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21784 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21785 } 21786 21787 TEST_F(FormatTest, AmbersandInLamda) { 21788 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21789 FormatStyle AlignStyle = getLLVMStyle(); 21790 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21791 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21792 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21793 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21794 } 21795 21796 TEST_F(FormatTest, SpacesInConditionalStatement) { 21797 FormatStyle Spaces = getLLVMStyle(); 21798 Spaces.IfMacros.clear(); 21799 Spaces.IfMacros.push_back("MYIF"); 21800 Spaces.SpacesInConditionalStatement = true; 21801 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21802 verifyFormat("if ( !a )\n return;", Spaces); 21803 verifyFormat("if ( a )\n return;", Spaces); 21804 verifyFormat("if constexpr ( a )\n return;", Spaces); 21805 verifyFormat("MYIF ( a )\n return;", Spaces); 21806 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21807 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21808 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21809 verifyFormat("while ( a )\n return;", Spaces); 21810 verifyFormat("while ( (a && b) )\n return;", Spaces); 21811 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21812 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21813 // Check that space on the left of "::" is inserted as expected at beginning 21814 // of condition. 21815 verifyFormat("while ( ::func() )\n return;", Spaces); 21816 21817 // Check impact of ControlStatementsExceptControlMacros is honored. 21818 Spaces.SpaceBeforeParens = 21819 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21820 verifyFormat("MYIF( a )\n return;", Spaces); 21821 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 21822 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 21823 } 21824 21825 TEST_F(FormatTest, AlternativeOperators) { 21826 // Test case for ensuring alternate operators are not 21827 // combined with their right most neighbour. 21828 verifyFormat("int a and b;"); 21829 verifyFormat("int a and_eq b;"); 21830 verifyFormat("int a bitand b;"); 21831 verifyFormat("int a bitor b;"); 21832 verifyFormat("int a compl b;"); 21833 verifyFormat("int a not b;"); 21834 verifyFormat("int a not_eq b;"); 21835 verifyFormat("int a or b;"); 21836 verifyFormat("int a xor b;"); 21837 verifyFormat("int a xor_eq b;"); 21838 verifyFormat("return this not_eq bitand other;"); 21839 verifyFormat("bool operator not_eq(const X bitand other)"); 21840 21841 verifyFormat("int a and 5;"); 21842 verifyFormat("int a and_eq 5;"); 21843 verifyFormat("int a bitand 5;"); 21844 verifyFormat("int a bitor 5;"); 21845 verifyFormat("int a compl 5;"); 21846 verifyFormat("int a not 5;"); 21847 verifyFormat("int a not_eq 5;"); 21848 verifyFormat("int a or 5;"); 21849 verifyFormat("int a xor 5;"); 21850 verifyFormat("int a xor_eq 5;"); 21851 21852 verifyFormat("int a compl(5);"); 21853 verifyFormat("int a not(5);"); 21854 21855 /* FIXME handle alternate tokens 21856 * https://en.cppreference.com/w/cpp/language/operator_alternative 21857 // alternative tokens 21858 verifyFormat("compl foo();"); // ~foo(); 21859 verifyFormat("foo() <%%>;"); // foo(); 21860 verifyFormat("void foo() <%%>;"); // void foo(){} 21861 verifyFormat("int a <:1:>;"); // int a[1];[ 21862 verifyFormat("%:define ABC abc"); // #define ABC abc 21863 verifyFormat("%:%:"); // ## 21864 */ 21865 } 21866 21867 TEST_F(FormatTest, STLWhileNotDefineChed) { 21868 verifyFormat("#if defined(while)\n" 21869 "#define while EMIT WARNING C4005\n" 21870 "#endif // while"); 21871 } 21872 21873 TEST_F(FormatTest, OperatorSpacing) { 21874 FormatStyle Style = getLLVMStyle(); 21875 Style.PointerAlignment = FormatStyle::PAS_Right; 21876 verifyFormat("Foo::operator*();", Style); 21877 verifyFormat("Foo::operator void *();", Style); 21878 verifyFormat("Foo::operator void **();", Style); 21879 verifyFormat("Foo::operator void *&();", Style); 21880 verifyFormat("Foo::operator void *&&();", Style); 21881 verifyFormat("Foo::operator void const *();", Style); 21882 verifyFormat("Foo::operator void const **();", Style); 21883 verifyFormat("Foo::operator void const *&();", Style); 21884 verifyFormat("Foo::operator void const *&&();", Style); 21885 verifyFormat("Foo::operator()(void *);", Style); 21886 verifyFormat("Foo::operator*(void *);", Style); 21887 verifyFormat("Foo::operator*();", Style); 21888 verifyFormat("Foo::operator**();", Style); 21889 verifyFormat("Foo::operator&();", Style); 21890 verifyFormat("Foo::operator<int> *();", Style); 21891 verifyFormat("Foo::operator<Foo> *();", Style); 21892 verifyFormat("Foo::operator<int> **();", Style); 21893 verifyFormat("Foo::operator<Foo> **();", Style); 21894 verifyFormat("Foo::operator<int> &();", Style); 21895 verifyFormat("Foo::operator<Foo> &();", Style); 21896 verifyFormat("Foo::operator<int> &&();", Style); 21897 verifyFormat("Foo::operator<Foo> &&();", Style); 21898 verifyFormat("Foo::operator<int> *&();", Style); 21899 verifyFormat("Foo::operator<Foo> *&();", Style); 21900 verifyFormat("Foo::operator<int> *&&();", Style); 21901 verifyFormat("Foo::operator<Foo> *&&();", Style); 21902 verifyFormat("operator*(int (*)(), class Foo);", Style); 21903 21904 verifyFormat("Foo::operator&();", Style); 21905 verifyFormat("Foo::operator void &();", Style); 21906 verifyFormat("Foo::operator void const &();", Style); 21907 verifyFormat("Foo::operator()(void &);", Style); 21908 verifyFormat("Foo::operator&(void &);", Style); 21909 verifyFormat("Foo::operator&();", Style); 21910 verifyFormat("operator&(int (&)(), class Foo);", Style); 21911 verifyFormat("operator&&(int (&)(), class Foo);", Style); 21912 21913 verifyFormat("Foo::operator&&();", Style); 21914 verifyFormat("Foo::operator**();", Style); 21915 verifyFormat("Foo::operator void &&();", Style); 21916 verifyFormat("Foo::operator void const &&();", Style); 21917 verifyFormat("Foo::operator()(void &&);", Style); 21918 verifyFormat("Foo::operator&&(void &&);", Style); 21919 verifyFormat("Foo::operator&&();", Style); 21920 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 21921 verifyFormat("operator const nsTArrayRight<E> &()", Style); 21922 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 21923 Style); 21924 verifyFormat("operator void **()", Style); 21925 verifyFormat("operator const FooRight<Object> &()", Style); 21926 verifyFormat("operator const FooRight<Object> *()", Style); 21927 verifyFormat("operator const FooRight<Object> **()", Style); 21928 verifyFormat("operator const FooRight<Object> *&()", Style); 21929 verifyFormat("operator const FooRight<Object> *&&()", Style); 21930 21931 Style.PointerAlignment = FormatStyle::PAS_Left; 21932 verifyFormat("Foo::operator*();", Style); 21933 verifyFormat("Foo::operator**();", Style); 21934 verifyFormat("Foo::operator void*();", Style); 21935 verifyFormat("Foo::operator void**();", Style); 21936 verifyFormat("Foo::operator void*&();", Style); 21937 verifyFormat("Foo::operator void*&&();", Style); 21938 verifyFormat("Foo::operator void const*();", Style); 21939 verifyFormat("Foo::operator void const**();", Style); 21940 verifyFormat("Foo::operator void const*&();", 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("Foo::operator<int>*();", Style); 21949 verifyFormat("Foo::operator<Foo>*();", Style); 21950 verifyFormat("Foo::operator<int>**();", Style); 21951 verifyFormat("Foo::operator<Foo>**();", Style); 21952 verifyFormat("Foo::operator<Foo>*&();", Style); 21953 verifyFormat("Foo::operator<int>&();", Style); 21954 verifyFormat("Foo::operator<Foo>&();", Style); 21955 verifyFormat("Foo::operator<int>&&();", Style); 21956 verifyFormat("Foo::operator<Foo>&&();", Style); 21957 verifyFormat("Foo::operator<int>*&();", Style); 21958 verifyFormat("Foo::operator<Foo>*&();", Style); 21959 verifyFormat("operator*(int (*)(), class Foo);", Style); 21960 21961 verifyFormat("Foo::operator&();", Style); 21962 verifyFormat("Foo::operator void&();", Style); 21963 verifyFormat("Foo::operator void const&();", Style); 21964 verifyFormat("Foo::operator/*comment*/ void&();", Style); 21965 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 21966 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 21967 verifyFormat("Foo::operator()(void&);", Style); 21968 verifyFormat("Foo::operator&(void&);", Style); 21969 verifyFormat("Foo::operator&();", Style); 21970 verifyFormat("operator&(int (&)(), class Foo);", Style); 21971 verifyFormat("operator&(int (&&)(), class Foo);", Style); 21972 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 21973 21974 verifyFormat("Foo::operator&&();", Style); 21975 verifyFormat("Foo::operator void&&();", Style); 21976 verifyFormat("Foo::operator void const&&();", Style); 21977 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 21978 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 21979 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 21980 verifyFormat("Foo::operator()(void&&);", Style); 21981 verifyFormat("Foo::operator&&(void&&);", Style); 21982 verifyFormat("Foo::operator&&();", Style); 21983 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 21984 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 21985 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 21986 Style); 21987 verifyFormat("operator void**()", Style); 21988 verifyFormat("operator const FooLeft<Object>&()", Style); 21989 verifyFormat("operator const FooLeft<Object>*()", Style); 21990 verifyFormat("operator const FooLeft<Object>**()", Style); 21991 verifyFormat("operator const FooLeft<Object>*&()", Style); 21992 verifyFormat("operator const FooLeft<Object>*&&()", Style); 21993 21994 // PR45107 21995 verifyFormat("operator Vector<String>&();", Style); 21996 verifyFormat("operator const Vector<String>&();", Style); 21997 verifyFormat("operator foo::Bar*();", Style); 21998 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 21999 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 22000 Style); 22001 22002 Style.PointerAlignment = FormatStyle::PAS_Middle; 22003 verifyFormat("Foo::operator*();", Style); 22004 verifyFormat("Foo::operator void *();", Style); 22005 verifyFormat("Foo::operator()(void *);", Style); 22006 verifyFormat("Foo::operator*(void *);", Style); 22007 verifyFormat("Foo::operator*();", Style); 22008 verifyFormat("operator*(int (*)(), class Foo);", Style); 22009 22010 verifyFormat("Foo::operator&();", Style); 22011 verifyFormat("Foo::operator void &();", Style); 22012 verifyFormat("Foo::operator void const &();", Style); 22013 verifyFormat("Foo::operator()(void &);", Style); 22014 verifyFormat("Foo::operator&(void &);", Style); 22015 verifyFormat("Foo::operator&();", Style); 22016 verifyFormat("operator&(int (&)(), class Foo);", Style); 22017 22018 verifyFormat("Foo::operator&&();", Style); 22019 verifyFormat("Foo::operator void &&();", Style); 22020 verifyFormat("Foo::operator void const &&();", Style); 22021 verifyFormat("Foo::operator()(void &&);", Style); 22022 verifyFormat("Foo::operator&&(void &&);", Style); 22023 verifyFormat("Foo::operator&&();", Style); 22024 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22025 } 22026 22027 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 22028 FormatStyle Style = getLLVMStyle(); 22029 // PR46157 22030 verifyFormat("foo(operator+, -42);", Style); 22031 verifyFormat("foo(operator++, -42);", Style); 22032 verifyFormat("foo(operator--, -42);", Style); 22033 verifyFormat("foo(-42, operator--);", Style); 22034 verifyFormat("foo(-42, operator, );", Style); 22035 verifyFormat("foo(operator, , -42);", Style); 22036 } 22037 22038 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 22039 FormatStyle Style = getLLVMStyle(); 22040 Style.WhitespaceSensitiveMacros.push_back("FOO"); 22041 22042 // Don't use the helpers here, since 'mess up' will change the whitespace 22043 // and these are all whitespace sensitive by definition 22044 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 22045 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 22046 EXPECT_EQ( 22047 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 22048 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 22049 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 22050 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 22051 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 22052 " Still=Intentional);", 22053 format("FOO(String-ized&Messy+But,: :\n" 22054 " Still=Intentional);", 22055 Style)); 22056 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 22057 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 22058 " Still=Intentional);", 22059 format("FOO(String-ized=&Messy+But,: :\n" 22060 " Still=Intentional);", 22061 Style)); 22062 22063 Style.ColumnLimit = 21; 22064 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 22065 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 22066 } 22067 22068 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 22069 // These tests are not in NamespaceFixer because that doesn't 22070 // test its interaction with line wrapping 22071 FormatStyle Style = getLLVMStyle(); 22072 Style.ColumnLimit = 80; 22073 verifyFormat("namespace {\n" 22074 "int i;\n" 22075 "int j;\n" 22076 "} // namespace", 22077 Style); 22078 22079 verifyFormat("namespace AAA {\n" 22080 "int i;\n" 22081 "int j;\n" 22082 "} // namespace AAA", 22083 Style); 22084 22085 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 22086 "int i;\n" 22087 "int j;\n" 22088 "} // namespace Averyveryveryverylongnamespace", 22089 format("namespace Averyveryveryverylongnamespace {\n" 22090 "int i;\n" 22091 "int j;\n" 22092 "}", 22093 Style)); 22094 22095 EXPECT_EQ( 22096 "namespace " 22097 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22098 " went::mad::now {\n" 22099 "int i;\n" 22100 "int j;\n" 22101 "} // namespace\n" 22102 " // " 22103 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22104 "went::mad::now", 22105 format("namespace " 22106 "would::it::save::you::a::lot::of::time::if_::i::" 22107 "just::gave::up::and_::went::mad::now {\n" 22108 "int i;\n" 22109 "int j;\n" 22110 "}", 22111 Style)); 22112 22113 // This used to duplicate the comment again and again on subsequent runs 22114 EXPECT_EQ( 22115 "namespace " 22116 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22117 " went::mad::now {\n" 22118 "int i;\n" 22119 "int j;\n" 22120 "} // namespace\n" 22121 " // " 22122 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22123 "went::mad::now", 22124 format("namespace " 22125 "would::it::save::you::a::lot::of::time::if_::i::" 22126 "just::gave::up::and_::went::mad::now {\n" 22127 "int i;\n" 22128 "int j;\n" 22129 "} // namespace\n" 22130 " // " 22131 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 22132 "and_::went::mad::now", 22133 Style)); 22134 } 22135 22136 TEST_F(FormatTest, LikelyUnlikely) { 22137 FormatStyle Style = getLLVMStyle(); 22138 22139 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22140 " return 29;\n" 22141 "}", 22142 Style); 22143 22144 verifyFormat("if (argc > 5) [[likely]] {\n" 22145 " return 29;\n" 22146 "}", 22147 Style); 22148 22149 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22150 " return 29;\n" 22151 "} else [[likely]] {\n" 22152 " return 42;\n" 22153 "}\n", 22154 Style); 22155 22156 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22157 " return 29;\n" 22158 "} else if (argc > 10) [[likely]] {\n" 22159 " return 99;\n" 22160 "} else {\n" 22161 " return 42;\n" 22162 "}\n", 22163 Style); 22164 22165 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 22166 " return 29;\n" 22167 "}", 22168 Style); 22169 } 22170 22171 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 22172 verifyFormat("Constructor()\n" 22173 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22174 " aaaa(aaaaaaaaaaaaaaaaaa, " 22175 "aaaaaaaaaaaaaaaaaat))"); 22176 verifyFormat("Constructor()\n" 22177 " : aaaaaaaaaaaaa(aaaaaa), " 22178 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 22179 22180 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 22181 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 22182 verifyFormat("Constructor()\n" 22183 " : aaaaaa(aaaaaa),\n" 22184 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22185 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 22186 StyleWithWhitespacePenalty); 22187 verifyFormat("Constructor()\n" 22188 " : aaaaaaaaaaaaa(aaaaaa), " 22189 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 22190 StyleWithWhitespacePenalty); 22191 } 22192 22193 TEST_F(FormatTest, LLVMDefaultStyle) { 22194 FormatStyle Style = getLLVMStyle(); 22195 verifyFormat("extern \"C\" {\n" 22196 "int foo();\n" 22197 "}", 22198 Style); 22199 } 22200 TEST_F(FormatTest, GNUDefaultStyle) { 22201 FormatStyle Style = getGNUStyle(); 22202 verifyFormat("extern \"C\"\n" 22203 "{\n" 22204 " int foo ();\n" 22205 "}", 22206 Style); 22207 } 22208 TEST_F(FormatTest, MozillaDefaultStyle) { 22209 FormatStyle Style = getMozillaStyle(); 22210 verifyFormat("extern \"C\"\n" 22211 "{\n" 22212 " int foo();\n" 22213 "}", 22214 Style); 22215 } 22216 TEST_F(FormatTest, GoogleDefaultStyle) { 22217 FormatStyle Style = getGoogleStyle(); 22218 verifyFormat("extern \"C\" {\n" 22219 "int foo();\n" 22220 "}", 22221 Style); 22222 } 22223 TEST_F(FormatTest, ChromiumDefaultStyle) { 22224 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 22225 verifyFormat("extern \"C\" {\n" 22226 "int foo();\n" 22227 "}", 22228 Style); 22229 } 22230 TEST_F(FormatTest, MicrosoftDefaultStyle) { 22231 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 22232 verifyFormat("extern \"C\"\n" 22233 "{\n" 22234 " int foo();\n" 22235 "}", 22236 Style); 22237 } 22238 TEST_F(FormatTest, WebKitDefaultStyle) { 22239 FormatStyle Style = getWebKitStyle(); 22240 verifyFormat("extern \"C\" {\n" 22241 "int foo();\n" 22242 "}", 22243 Style); 22244 } 22245 22246 TEST_F(FormatTest, ConceptsAndRequires) { 22247 FormatStyle Style = getLLVMStyle(); 22248 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 22249 22250 verifyFormat("template <typename T>\n" 22251 "concept Hashable = requires(T a) {\n" 22252 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22253 "};", 22254 Style); 22255 verifyFormat("template <typename T>\n" 22256 "concept EqualityComparable = requires(T a, T b) {\n" 22257 " { a == b } -> bool;\n" 22258 "};", 22259 Style); 22260 verifyFormat("template <typename T>\n" 22261 "concept EqualityComparable = requires(T a, T b) {\n" 22262 " { a == b } -> bool;\n" 22263 " { a != b } -> bool;\n" 22264 "};", 22265 Style); 22266 verifyFormat("template <typename T>\n" 22267 "concept EqualityComparable = requires(T a, T b) {\n" 22268 " { a == b } -> bool;\n" 22269 " { a != b } -> bool;\n" 22270 "};", 22271 Style); 22272 22273 verifyFormat("template <typename It>\n" 22274 "requires Iterator<It>\n" 22275 "void sort(It begin, It end) {\n" 22276 " //....\n" 22277 "}", 22278 Style); 22279 22280 verifyFormat("template <typename T>\n" 22281 "concept Large = sizeof(T) > 10;", 22282 Style); 22283 22284 verifyFormat("template <typename T, typename U>\n" 22285 "concept FooableWith = requires(T t, U u) {\n" 22286 " typename T::foo_type;\n" 22287 " { t.foo(u) } -> typename T::foo_type;\n" 22288 " t++;\n" 22289 "};\n" 22290 "void doFoo(FooableWith<int> auto t) {\n" 22291 " t.foo(3);\n" 22292 "}", 22293 Style); 22294 verifyFormat("template <typename T>\n" 22295 "concept Context = sizeof(T) == 1;", 22296 Style); 22297 verifyFormat("template <typename T>\n" 22298 "concept Context = is_specialization_of_v<context, T>;", 22299 Style); 22300 verifyFormat("template <typename T>\n" 22301 "concept Node = std::is_object_v<T>;", 22302 Style); 22303 verifyFormat("template <typename T>\n" 22304 "concept Tree = true;", 22305 Style); 22306 22307 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 22308 " //...\n" 22309 "}", 22310 Style); 22311 22312 verifyFormat( 22313 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 22314 " //...\n" 22315 "}", 22316 Style); 22317 22318 verifyFormat( 22319 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 22320 " //...\n" 22321 "}", 22322 Style); 22323 22324 verifyFormat("template <typename T>\n" 22325 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 22326 "Concept2<I> {\n" 22327 " //...\n" 22328 "}", 22329 Style); 22330 22331 verifyFormat("template <typename T>\n" 22332 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 22333 "Concept2<I> {\n" 22334 " //...\n" 22335 "}", 22336 Style); 22337 22338 verifyFormat( 22339 "template <typename T>\n" 22340 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 22341 " //...\n" 22342 "}", 22343 Style); 22344 22345 verifyFormat( 22346 "template <typename T>\n" 22347 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 22348 " //...\n" 22349 "}", 22350 Style); 22351 22352 verifyFormat("template <typename It>\n" 22353 "requires Foo<It>() && Bar<It> {\n" 22354 " //....\n" 22355 "}", 22356 Style); 22357 22358 verifyFormat("template <typename It>\n" 22359 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 22360 " //....\n" 22361 "}", 22362 Style); 22363 22364 verifyFormat("template <typename It>\n" 22365 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 22366 " //....\n" 22367 "}", 22368 Style); 22369 22370 verifyFormat( 22371 "template <typename It>\n" 22372 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 22373 " //....\n" 22374 "}", 22375 Style); 22376 22377 Style.IndentRequires = true; 22378 verifyFormat("template <typename It>\n" 22379 " requires Iterator<It>\n" 22380 "void sort(It begin, It end) {\n" 22381 " //....\n" 22382 "}", 22383 Style); 22384 verifyFormat("template <std::size index_>\n" 22385 " requires(index_ < sizeof...(Children_))\n" 22386 "Tree auto &child() {\n" 22387 " // ...\n" 22388 "}", 22389 Style); 22390 22391 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 22392 verifyFormat("template <typename T>\n" 22393 "concept Hashable = requires (T a) {\n" 22394 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22395 "};", 22396 Style); 22397 22398 verifyFormat("template <class T = void>\n" 22399 " requires EqualityComparable<T> || Same<T, void>\n" 22400 "struct equal_to;", 22401 Style); 22402 22403 verifyFormat("template <class T>\n" 22404 " requires requires {\n" 22405 " T{};\n" 22406 " T (int);\n" 22407 " }\n", 22408 Style); 22409 22410 Style.ColumnLimit = 78; 22411 verifyFormat("template <typename T>\n" 22412 "concept Context = Traits<typename T::traits_type> and\n" 22413 " Interface<typename T::interface_type> and\n" 22414 " Request<typename T::request_type> and\n" 22415 " Response<typename T::response_type> and\n" 22416 " ContextExtension<typename T::extension_type> and\n" 22417 " ::std::is_copy_constructable<T> and " 22418 "::std::is_move_constructable<T> and\n" 22419 " requires (T c) {\n" 22420 " { c.response; } -> Response;\n" 22421 "} and requires (T c) {\n" 22422 " { c.request; } -> Request;\n" 22423 "}\n", 22424 Style); 22425 22426 verifyFormat("template <typename T>\n" 22427 "concept Context = Traits<typename T::traits_type> or\n" 22428 " Interface<typename T::interface_type> or\n" 22429 " Request<typename T::request_type> or\n" 22430 " Response<typename T::response_type> or\n" 22431 " ContextExtension<typename T::extension_type> or\n" 22432 " ::std::is_copy_constructable<T> or " 22433 "::std::is_move_constructable<T> or\n" 22434 " requires (T c) {\n" 22435 " { c.response; } -> Response;\n" 22436 "} or requires (T c) {\n" 22437 " { c.request; } -> Request;\n" 22438 "}\n", 22439 Style); 22440 22441 verifyFormat("template <typename T>\n" 22442 "concept Context = Traits<typename T::traits_type> &&\n" 22443 " Interface<typename T::interface_type> &&\n" 22444 " Request<typename T::request_type> &&\n" 22445 " Response<typename T::response_type> &&\n" 22446 " ContextExtension<typename T::extension_type> &&\n" 22447 " ::std::is_copy_constructable<T> && " 22448 "::std::is_move_constructable<T> &&\n" 22449 " requires (T c) {\n" 22450 " { c.response; } -> Response;\n" 22451 "} && requires (T c) {\n" 22452 " { c.request; } -> Request;\n" 22453 "}\n", 22454 Style); 22455 22456 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 22457 "Constraint2<T>;"); 22458 22459 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 22460 Style.BraceWrapping.AfterFunction = true; 22461 Style.BraceWrapping.AfterClass = true; 22462 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 22463 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 22464 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22465 "{\n" 22466 " return\n" 22467 "}\n", 22468 Style); 22469 22470 verifyFormat("void Foo () requires std::copyable<T>\n" 22471 "{\n" 22472 " return\n" 22473 "}\n", 22474 Style); 22475 22476 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22477 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22478 "struct constant;", 22479 Style); 22480 22481 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22482 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22483 "struct constant;", 22484 Style); 22485 22486 verifyFormat("template <class T>\n" 22487 "class plane_with_very_very_very_long_name\n" 22488 "{\n" 22489 " constexpr plane_with_very_very_very_long_name () requires " 22490 "std::copyable<T>\n" 22491 " : plane_with_very_very_very_long_name (1)\n" 22492 " {\n" 22493 " }\n" 22494 "}\n", 22495 Style); 22496 22497 verifyFormat("template <class T>\n" 22498 "class plane_with_long_name\n" 22499 "{\n" 22500 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22501 " : plane_with_long_name (1)\n" 22502 " {\n" 22503 " }\n" 22504 "}\n", 22505 Style); 22506 22507 Style.BreakBeforeConceptDeclarations = false; 22508 verifyFormat("template <typename T> concept Tree = true;", Style); 22509 22510 Style.IndentRequires = false; 22511 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22512 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22513 "struct constant;", 22514 Style); 22515 } 22516 22517 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22518 FormatStyle Style = getLLVMStyle(); 22519 StringRef Source = "void Foo::slot() {\n" 22520 " unsigned char MyChar = 'x';\n" 22521 " emit signal(MyChar);\n" 22522 " Q_EMIT signal(MyChar);\n" 22523 "}"; 22524 22525 EXPECT_EQ(Source, format(Source, Style)); 22526 22527 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22528 EXPECT_EQ("void Foo::slot() {\n" 22529 " unsigned char MyChar = 'x';\n" 22530 " emit signal(MyChar);\n" 22531 " Q_EMIT signal(MyChar);\n" 22532 "}", 22533 format(Source, Style)); 22534 22535 Style.StatementAttributeLikeMacros.push_back("emit"); 22536 EXPECT_EQ(Source, format(Source, Style)); 22537 22538 Style.StatementAttributeLikeMacros = {}; 22539 EXPECT_EQ("void Foo::slot() {\n" 22540 " unsigned char MyChar = 'x';\n" 22541 " emit signal(MyChar);\n" 22542 " Q_EMIT signal(MyChar);\n" 22543 "}", 22544 format(Source, Style)); 22545 } 22546 22547 TEST_F(FormatTest, IndentAccessModifiers) { 22548 FormatStyle Style = getLLVMStyle(); 22549 Style.IndentAccessModifiers = true; 22550 // Members are *two* levels below the record; 22551 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22552 verifyFormat("class C {\n" 22553 " int i;\n" 22554 "};\n", 22555 Style); 22556 verifyFormat("union C {\n" 22557 " int i;\n" 22558 " unsigned u;\n" 22559 "};\n", 22560 Style); 22561 // Access modifiers should be indented one level below the record. 22562 verifyFormat("class C {\n" 22563 " public:\n" 22564 " int i;\n" 22565 "};\n", 22566 Style); 22567 verifyFormat("struct S {\n" 22568 " private:\n" 22569 " class C {\n" 22570 " int j;\n" 22571 "\n" 22572 " public:\n" 22573 " C();\n" 22574 " };\n" 22575 "\n" 22576 " public:\n" 22577 " int i;\n" 22578 "};\n", 22579 Style); 22580 // Enumerations are not records and should be unaffected. 22581 Style.AllowShortEnumsOnASingleLine = false; 22582 verifyFormat("enum class E {\n" 22583 " A,\n" 22584 " B\n" 22585 "};\n", 22586 Style); 22587 // Test with a different indentation width; 22588 // also proves that the result is Style.AccessModifierOffset agnostic. 22589 Style.IndentWidth = 3; 22590 verifyFormat("class C {\n" 22591 " public:\n" 22592 " int i;\n" 22593 "};\n", 22594 Style); 22595 } 22596 22597 TEST_F(FormatTest, LimitlessStringsAndComments) { 22598 auto Style = getLLVMStyleWithColumns(0); 22599 constexpr StringRef Code = 22600 "/**\n" 22601 " * This is a multiline comment with quite some long lines, at least for " 22602 "the LLVM Style.\n" 22603 " * We will redo this with strings and line comments. Just to check if " 22604 "everything is working.\n" 22605 " */\n" 22606 "bool foo() {\n" 22607 " /* Single line multi line comment. */\n" 22608 " const std::string String = \"This is a multiline string with quite " 22609 "some long lines, at least for the LLVM Style.\"\n" 22610 " \"We already did it with multi line " 22611 "comments, and we will do it with line comments. Just to check if " 22612 "everything is working.\";\n" 22613 " // This is a line comment (block) with quite some long lines, at " 22614 "least for the LLVM Style.\n" 22615 " // We already did this with multi line comments and strings. Just to " 22616 "check if everything is working.\n" 22617 " const std::string SmallString = \"Hello World\";\n" 22618 " // Small line comment\n" 22619 " return String.size() > SmallString.size();\n" 22620 "}"; 22621 EXPECT_EQ(Code, format(Code, Style)); 22622 } 22623 22624 TEST_F(FormatTest, FormatDecayCopy) { 22625 // error cases from unit tests 22626 verifyFormat("foo(auto())"); 22627 verifyFormat("foo(auto{})"); 22628 verifyFormat("foo(auto({}))"); 22629 verifyFormat("foo(auto{{}})"); 22630 22631 verifyFormat("foo(auto(1))"); 22632 verifyFormat("foo(auto{1})"); 22633 verifyFormat("foo(new auto(1))"); 22634 verifyFormat("foo(new auto{1})"); 22635 verifyFormat("decltype(auto(1)) x;"); 22636 verifyFormat("decltype(auto{1}) x;"); 22637 verifyFormat("auto(x);"); 22638 verifyFormat("auto{x};"); 22639 verifyFormat("new auto{x};"); 22640 verifyFormat("auto{x} = y;"); 22641 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 22642 // the user's own fault 22643 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 22644 // clearly the user's own fault 22645 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 22646 } 22647 22648 TEST_F(FormatTest, Cpp20ModulesSupport) { 22649 FormatStyle Style = getLLVMStyle(); 22650 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 22651 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 22652 22653 verifyFormat("export import foo;", Style); 22654 verifyFormat("export import foo:bar;", Style); 22655 verifyFormat("export import foo.bar;", Style); 22656 verifyFormat("export import foo.bar:baz;", Style); 22657 verifyFormat("export import :bar;", Style); 22658 verifyFormat("export module foo:bar;", Style); 22659 verifyFormat("export module foo;", Style); 22660 verifyFormat("export module foo.bar;", Style); 22661 verifyFormat("export module foo.bar:baz;", Style); 22662 verifyFormat("export import <string_view>;", Style); 22663 22664 verifyFormat("export type_name var;", Style); 22665 verifyFormat("template <class T> export using A = B<T>;", Style); 22666 verifyFormat("export using A = B;", Style); 22667 verifyFormat("export int func() {\n" 22668 " foo();\n" 22669 "}", 22670 Style); 22671 verifyFormat("export struct {\n" 22672 " int foo;\n" 22673 "};", 22674 Style); 22675 verifyFormat("export {\n" 22676 " int foo;\n" 22677 "};", 22678 Style); 22679 verifyFormat("export export char const *hello() { return \"hello\"; }"); 22680 22681 verifyFormat("import bar;", Style); 22682 verifyFormat("import foo.bar;", Style); 22683 verifyFormat("import foo:bar;", Style); 22684 verifyFormat("import :bar;", Style); 22685 verifyFormat("import <ctime>;", Style); 22686 verifyFormat("import \"header\";", Style); 22687 22688 verifyFormat("module foo;", Style); 22689 verifyFormat("module foo:bar;", Style); 22690 verifyFormat("module foo.bar;", Style); 22691 verifyFormat("module;", Style); 22692 22693 verifyFormat("export namespace hi {\n" 22694 "const char *sayhi();\n" 22695 "}", 22696 Style); 22697 22698 verifyFormat("module :private;", Style); 22699 verifyFormat("import <foo/bar.h>;", Style); 22700 verifyFormat("import foo...bar;", Style); 22701 verifyFormat("import ..........;", Style); 22702 verifyFormat("module foo:private;", Style); 22703 verifyFormat("import a", Style); 22704 verifyFormat("module a", Style); 22705 verifyFormat("export import a", Style); 22706 verifyFormat("export module a", Style); 22707 22708 verifyFormat("import", Style); 22709 verifyFormat("module", Style); 22710 verifyFormat("export", Style); 22711 } 22712 22713 } // namespace 22714 } // namespace format 22715 } // namespace clang 22716