1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "gtest/gtest.h" 17 18 #define DEBUG_TYPE "format-test" 19 20 using clang::tooling::ReplacementTest; 21 using clang::tooling::toReplacements; 22 using testing::ScopedTrace; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 33 34 std::string format(llvm::StringRef Code, 35 const FormatStyle &Style = getLLVMStyle(), 36 StatusCheck CheckComplete = SC_ExpectComplete) { 37 LLVM_DEBUG(llvm::errs() << "---\n"); 38 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 39 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 40 FormattingAttemptStatus Status; 41 tooling::Replacements Replaces = 42 reformat(Style, Code, Ranges, "<stdin>", &Status); 43 if (CheckComplete != SC_DoNotCheck) { 44 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 45 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 46 << Code << "\n\n"; 47 } 48 ReplacementCount = Replaces.size(); 49 auto Result = applyAllReplacements(Code, Replaces); 50 EXPECT_TRUE(static_cast<bool>(Result)); 51 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 52 return *Result; 53 } 54 55 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 56 Style.ColumnLimit = ColumnLimit; 57 return Style; 58 } 59 60 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 61 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 62 } 63 64 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 66 } 67 68 void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, 69 llvm::StringRef Code, 70 const FormatStyle &Style = getLLVMStyle()) { 71 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 72 EXPECT_EQ(Expected.str(), format(Expected, Style)) 73 << "Expected code is not stable"; 74 EXPECT_EQ(Expected.str(), format(Code, Style)); 75 if (Style.Language == FormatStyle::LK_Cpp) { 76 // Objective-C++ is a superset of C++, so everything checked for C++ 77 // needs to be checked for Objective-C++ as well. 78 FormatStyle ObjCStyle = Style; 79 ObjCStyle.Language = FormatStyle::LK_ObjC; 80 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 81 } 82 } 83 84 void _verifyFormat(const char *File, int Line, llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 _verifyFormat(File, Line, Code, test::messUp(Code), Style); 87 } 88 89 void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void _verifyIndependentOfContext(const char *File, int Line, 97 llvm::StringRef Text, 98 const FormatStyle &Style = getLLVMStyle()) { 99 _verifyFormat(File, Line, Text, Style); 100 _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(), 101 Style); 102 } 103 104 /// \brief Verify that clang-format does not crash on the given input. 105 void verifyNoCrash(llvm::StringRef Code, 106 const FormatStyle &Style = getLLVMStyle()) { 107 format(Code, Style, SC_DoNotCheck); 108 } 109 110 int ReplacementCount; 111 }; 112 113 #define verifyIndependentOfContext(...) \ 114 _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__) 115 #define verifyIncompleteFormat(...) \ 116 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__) 117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) 118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle()) 119 120 TEST_F(FormatTest, MessUp) { 121 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 122 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 123 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 124 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 125 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 126 } 127 128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 129 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 130 } 131 132 TEST_F(FormatTest, LLVMStyleOverride) { 133 EXPECT_EQ(FormatStyle::LK_Proto, 134 getLLVMStyle(FormatStyle::LK_Proto).Language); 135 } 136 137 //===----------------------------------------------------------------------===// 138 // Basic function tests. 139 //===----------------------------------------------------------------------===// 140 141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 142 EXPECT_EQ(";", format(";")); 143 } 144 145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 146 EXPECT_EQ("int i;", format(" int i;")); 147 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 148 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 149 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 150 } 151 152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 153 EXPECT_EQ("int i;", format("int\ni;")); 154 } 155 156 TEST_F(FormatTest, FormatsNestedBlockStatements) { 157 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 158 } 159 160 TEST_F(FormatTest, FormatsNestedCall) { 161 verifyFormat("Method(f1, f2(f3));"); 162 verifyFormat("Method(f1(f2, f3()));"); 163 verifyFormat("Method(f1(f2, (f3())));"); 164 } 165 166 TEST_F(FormatTest, NestedNameSpecifiers) { 167 verifyFormat("vector<::Type> v;"); 168 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 169 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 170 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 171 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 172 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 173 verifyFormat("bool a = 2 < ::SomeFunction();"); 174 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 175 verifyFormat("some::string getName();"); 176 } 177 178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 179 EXPECT_EQ("if (a) {\n" 180 " f();\n" 181 "}", 182 format("if(a){f();}")); 183 EXPECT_EQ(4, ReplacementCount); 184 EXPECT_EQ("if (a) {\n" 185 " f();\n" 186 "}", 187 format("if (a) {\n" 188 " f();\n" 189 "}")); 190 EXPECT_EQ(0, ReplacementCount); 191 EXPECT_EQ("/*\r\n" 192 "\r\n" 193 "*/\r\n", 194 format("/*\r\n" 195 "\r\n" 196 "*/\r\n")); 197 EXPECT_EQ(0, ReplacementCount); 198 } 199 200 TEST_F(FormatTest, RemovesEmptyLines) { 201 EXPECT_EQ("class C {\n" 202 " int i;\n" 203 "};", 204 format("class C {\n" 205 " int i;\n" 206 "\n" 207 "};")); 208 209 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 210 EXPECT_EQ("namespace N {\n" 211 "\n" 212 "int i;\n" 213 "}", 214 format("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 getGoogleStyle())); 219 EXPECT_EQ("/* something */ namespace N {\n" 220 "\n" 221 "int i;\n" 222 "}", 223 format("/* something */ namespace N {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 getGoogleStyle())); 228 EXPECT_EQ("inline namespace N {\n" 229 "\n" 230 "int i;\n" 231 "}", 232 format("inline namespace N {\n" 233 "\n" 234 "int i;\n" 235 "}", 236 getGoogleStyle())); 237 EXPECT_EQ("/* something */ inline namespace N {\n" 238 "\n" 239 "int i;\n" 240 "}", 241 format("/* something */ inline namespace N {\n" 242 "\n" 243 "int i;\n" 244 "}", 245 getGoogleStyle())); 246 EXPECT_EQ("export namespace N {\n" 247 "\n" 248 "int i;\n" 249 "}", 250 format("export namespace N {\n" 251 "\n" 252 "int i;\n" 253 "}", 254 getGoogleStyle())); 255 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 256 "\n" 257 "int i;\n" 258 "}", 259 format("extern /**/ \"C\" /**/ {\n" 260 "\n" 261 "int i;\n" 262 "}", 263 getGoogleStyle())); 264 265 auto CustomStyle = getLLVMStyle(); 266 CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom; 267 CustomStyle.BraceWrapping.AfterNamespace = true; 268 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 269 EXPECT_EQ("namespace N\n" 270 "{\n" 271 "\n" 272 "int i;\n" 273 "}", 274 format("namespace N\n" 275 "{\n" 276 "\n" 277 "\n" 278 "int i;\n" 279 "}", 280 CustomStyle)); 281 EXPECT_EQ("/* something */ namespace N\n" 282 "{\n" 283 "\n" 284 "int i;\n" 285 "}", 286 format("/* something */ namespace N {\n" 287 "\n" 288 "\n" 289 "int i;\n" 290 "}", 291 CustomStyle)); 292 EXPECT_EQ("inline namespace N\n" 293 "{\n" 294 "\n" 295 "int i;\n" 296 "}", 297 format("inline namespace N\n" 298 "{\n" 299 "\n" 300 "\n" 301 "int i;\n" 302 "}", 303 CustomStyle)); 304 EXPECT_EQ("/* something */ inline namespace N\n" 305 "{\n" 306 "\n" 307 "int i;\n" 308 "}", 309 format("/* something */ inline namespace N\n" 310 "{\n" 311 "\n" 312 "int i;\n" 313 "}", 314 CustomStyle)); 315 EXPECT_EQ("export namespace N\n" 316 "{\n" 317 "\n" 318 "int i;\n" 319 "}", 320 format("export namespace N\n" 321 "{\n" 322 "\n" 323 "int i;\n" 324 "}", 325 CustomStyle)); 326 EXPECT_EQ("namespace a\n" 327 "{\n" 328 "namespace b\n" 329 "{\n" 330 "\n" 331 "class AA {};\n" 332 "\n" 333 "} // namespace b\n" 334 "} // namespace a\n", 335 format("namespace a\n" 336 "{\n" 337 "namespace b\n" 338 "{\n" 339 "\n" 340 "\n" 341 "class AA {};\n" 342 "\n" 343 "\n" 344 "}\n" 345 "}\n", 346 CustomStyle)); 347 EXPECT_EQ("namespace A /* comment */\n" 348 "{\n" 349 "class B {}\n" 350 "} // namespace A", 351 format("namespace A /* comment */ { class B {} }", CustomStyle)); 352 EXPECT_EQ("namespace A\n" 353 "{ /* comment */\n" 354 "class B {}\n" 355 "} // namespace A", 356 format("namespace A {/* comment */ class B {} }", CustomStyle)); 357 EXPECT_EQ("namespace A\n" 358 "{ /* comment */\n" 359 "\n" 360 "class B {}\n" 361 "\n" 362 "" 363 "} // namespace A", 364 format("namespace A { /* comment */\n" 365 "\n" 366 "\n" 367 "class B {}\n" 368 "\n" 369 "\n" 370 "}", 371 CustomStyle)); 372 EXPECT_EQ("namespace A /* comment */\n" 373 "{\n" 374 "\n" 375 "class B {}\n" 376 "\n" 377 "} // namespace A", 378 format("namespace A/* comment */ {\n" 379 "\n" 380 "\n" 381 "class B {}\n" 382 "\n" 383 "\n" 384 "}", 385 CustomStyle)); 386 387 // ...but do keep inlining and removing empty lines for non-block extern "C" 388 // functions. 389 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 390 EXPECT_EQ("extern \"C\" int f() {\n" 391 " int i = 42;\n" 392 " return i;\n" 393 "}", 394 format("extern \"C\" int f() {\n" 395 "\n" 396 " int i = 42;\n" 397 " return i;\n" 398 "}", 399 getGoogleStyle())); 400 401 // Remove empty lines at the beginning and end of blocks. 402 EXPECT_EQ("void f() {\n" 403 "\n" 404 " if (a) {\n" 405 "\n" 406 " f();\n" 407 " }\n" 408 "}", 409 format("void f() {\n" 410 "\n" 411 " if (a) {\n" 412 "\n" 413 " f();\n" 414 "\n" 415 " }\n" 416 "\n" 417 "}", 418 getLLVMStyle())); 419 EXPECT_EQ("void f() {\n" 420 " if (a) {\n" 421 " f();\n" 422 " }\n" 423 "}", 424 format("void f() {\n" 425 "\n" 426 " if (a) {\n" 427 "\n" 428 " f();\n" 429 "\n" 430 " }\n" 431 "\n" 432 "}", 433 getGoogleStyle())); 434 435 // Don't remove empty lines in more complex control statements. 436 EXPECT_EQ("void f() {\n" 437 " if (a) {\n" 438 " f();\n" 439 "\n" 440 " } else if (b) {\n" 441 " f();\n" 442 " }\n" 443 "}", 444 format("void f() {\n" 445 " if (a) {\n" 446 " f();\n" 447 "\n" 448 " } else if (b) {\n" 449 " f();\n" 450 "\n" 451 " }\n" 452 "\n" 453 "}")); 454 455 // Don't remove empty lines before namespace endings. 456 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 457 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 458 EXPECT_EQ("namespace {\n" 459 "int i;\n" 460 "\n" 461 "}", 462 format("namespace {\n" 463 "int i;\n" 464 "\n" 465 "}", 466 LLVMWithNoNamespaceFix)); 467 EXPECT_EQ("namespace {\n" 468 "int i;\n" 469 "}", 470 format("namespace {\n" 471 "int i;\n" 472 "}", 473 LLVMWithNoNamespaceFix)); 474 EXPECT_EQ("namespace {\n" 475 "int i;\n" 476 "\n" 477 "};", 478 format("namespace {\n" 479 "int i;\n" 480 "\n" 481 "};", 482 LLVMWithNoNamespaceFix)); 483 EXPECT_EQ("namespace {\n" 484 "int i;\n" 485 "};", 486 format("namespace {\n" 487 "int i;\n" 488 "};", 489 LLVMWithNoNamespaceFix)); 490 EXPECT_EQ("namespace {\n" 491 "int i;\n" 492 "\n" 493 "}", 494 format("namespace {\n" 495 "int i;\n" 496 "\n" 497 "}")); 498 EXPECT_EQ("namespace {\n" 499 "int i;\n" 500 "\n" 501 "} // namespace", 502 format("namespace {\n" 503 "int i;\n" 504 "\n" 505 "} // namespace")); 506 507 FormatStyle Style = getLLVMStyle(); 508 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 509 Style.MaxEmptyLinesToKeep = 2; 510 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 511 Style.BraceWrapping.AfterClass = true; 512 Style.BraceWrapping.AfterFunction = true; 513 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 514 515 EXPECT_EQ("class Foo\n" 516 "{\n" 517 " Foo() {}\n" 518 "\n" 519 " void funk() {}\n" 520 "};", 521 format("class Foo\n" 522 "{\n" 523 " Foo()\n" 524 " {\n" 525 " }\n" 526 "\n" 527 " void funk() {}\n" 528 "};", 529 Style)); 530 } 531 532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 533 verifyFormat("x = (a) and (b);"); 534 verifyFormat("x = (a) or (b);"); 535 verifyFormat("x = (a) bitand (b);"); 536 verifyFormat("x = (a) bitor (b);"); 537 verifyFormat("x = (a) not_eq (b);"); 538 verifyFormat("x = (a) and_eq (b);"); 539 verifyFormat("x = (a) or_eq (b);"); 540 verifyFormat("x = (a) xor (b);"); 541 } 542 543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 544 verifyFormat("x = compl(a);"); 545 verifyFormat("x = not(a);"); 546 verifyFormat("x = bitand(a);"); 547 // Unary operator must not be merged with the next identifier 548 verifyFormat("x = compl a;"); 549 verifyFormat("x = not a;"); 550 verifyFormat("x = bitand a;"); 551 } 552 553 //===----------------------------------------------------------------------===// 554 // Tests for control statements. 555 //===----------------------------------------------------------------------===// 556 557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 558 verifyFormat("if (true)\n f();\ng();"); 559 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 560 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 561 verifyFormat("if constexpr (true)\n" 562 " f();\ng();"); 563 verifyFormat("if CONSTEXPR (true)\n" 564 " f();\ng();"); 565 verifyFormat("if constexpr (a)\n" 566 " if constexpr (b)\n" 567 " if constexpr (c)\n" 568 " g();\n" 569 "h();"); 570 verifyFormat("if CONSTEXPR (a)\n" 571 " if CONSTEXPR (b)\n" 572 " if CONSTEXPR (c)\n" 573 " g();\n" 574 "h();"); 575 verifyFormat("if constexpr (a)\n" 576 " if constexpr (b) {\n" 577 " f();\n" 578 " }\n" 579 "g();"); 580 verifyFormat("if CONSTEXPR (a)\n" 581 " if CONSTEXPR (b) {\n" 582 " f();\n" 583 " }\n" 584 "g();"); 585 586 verifyFormat("if (a)\n" 587 " g();"); 588 verifyFormat("if (a) {\n" 589 " g()\n" 590 "};"); 591 verifyFormat("if (a)\n" 592 " g();\n" 593 "else\n" 594 " g();"); 595 verifyFormat("if (a) {\n" 596 " g();\n" 597 "} else\n" 598 " g();"); 599 verifyFormat("if (a)\n" 600 " g();\n" 601 "else {\n" 602 " g();\n" 603 "}"); 604 verifyFormat("if (a) {\n" 605 " g();\n" 606 "} else {\n" 607 " g();\n" 608 "}"); 609 verifyFormat("if (a)\n" 610 " g();\n" 611 "else if (b)\n" 612 " g();\n" 613 "else\n" 614 " g();"); 615 verifyFormat("if (a) {\n" 616 " g();\n" 617 "} else if (b)\n" 618 " g();\n" 619 "else\n" 620 " g();"); 621 verifyFormat("if (a)\n" 622 " g();\n" 623 "else if (b) {\n" 624 " g();\n" 625 "} else\n" 626 " g();"); 627 verifyFormat("if (a)\n" 628 " g();\n" 629 "else if (b)\n" 630 " g();\n" 631 "else {\n" 632 " g();\n" 633 "}"); 634 verifyFormat("if (a)\n" 635 " g();\n" 636 "else if (b) {\n" 637 " g();\n" 638 "} else {\n" 639 " g();\n" 640 "}"); 641 verifyFormat("if (a) {\n" 642 " g();\n" 643 "} else if (b) {\n" 644 " g();\n" 645 "} else {\n" 646 " g();\n" 647 "}"); 648 649 FormatStyle AllowsMergedIf = getLLVMStyle(); 650 AllowsMergedIf.IfMacros.push_back("MYIF"); 651 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 652 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 653 FormatStyle::SIS_WithoutElse; 654 verifyFormat("if (a)\n" 655 " // comment\n" 656 " f();", 657 AllowsMergedIf); 658 verifyFormat("{\n" 659 " if (a)\n" 660 " label:\n" 661 " f();\n" 662 "}", 663 AllowsMergedIf); 664 verifyFormat("#define A \\\n" 665 " if (a) \\\n" 666 " label: \\\n" 667 " f()", 668 AllowsMergedIf); 669 verifyFormat("if (a)\n" 670 " ;", 671 AllowsMergedIf); 672 verifyFormat("if (a)\n" 673 " if (b) return;", 674 AllowsMergedIf); 675 676 verifyFormat("if (a) // Can't merge this\n" 677 " f();\n", 678 AllowsMergedIf); 679 verifyFormat("if (a) /* still don't merge */\n" 680 " f();", 681 AllowsMergedIf); 682 verifyFormat("if (a) { // Never merge this\n" 683 " f();\n" 684 "}", 685 AllowsMergedIf); 686 verifyFormat("if (a) { /* Never merge this */\n" 687 " f();\n" 688 "}", 689 AllowsMergedIf); 690 verifyFormat("MYIF (a)\n" 691 " // comment\n" 692 " f();", 693 AllowsMergedIf); 694 verifyFormat("{\n" 695 " MYIF (a)\n" 696 " label:\n" 697 " f();\n" 698 "}", 699 AllowsMergedIf); 700 verifyFormat("#define A \\\n" 701 " MYIF (a) \\\n" 702 " label: \\\n" 703 " f()", 704 AllowsMergedIf); 705 verifyFormat("MYIF (a)\n" 706 " ;", 707 AllowsMergedIf); 708 verifyFormat("MYIF (a)\n" 709 " MYIF (b) return;", 710 AllowsMergedIf); 711 712 verifyFormat("MYIF (a) // Can't merge this\n" 713 " f();\n", 714 AllowsMergedIf); 715 verifyFormat("MYIF (a) /* still don't merge */\n" 716 " f();", 717 AllowsMergedIf); 718 verifyFormat("MYIF (a) { // Never merge this\n" 719 " f();\n" 720 "}", 721 AllowsMergedIf); 722 verifyFormat("MYIF (a) { /* Never merge this */\n" 723 " f();\n" 724 "}", 725 AllowsMergedIf); 726 727 AllowsMergedIf.ColumnLimit = 14; 728 // Where line-lengths matter, a 2-letter synonym that maintains line length. 729 // Not IF to avoid any confusion that IF is somehow special. 730 AllowsMergedIf.IfMacros.push_back("FI"); 731 verifyFormat("if (a) return;", AllowsMergedIf); 732 verifyFormat("if (aaaaaaaaa)\n" 733 " return;", 734 AllowsMergedIf); 735 verifyFormat("FI (a) return;", AllowsMergedIf); 736 verifyFormat("FI (aaaaaaaaa)\n" 737 " return;", 738 AllowsMergedIf); 739 740 AllowsMergedIf.ColumnLimit = 13; 741 verifyFormat("if (a)\n return;", AllowsMergedIf); 742 verifyFormat("FI (a)\n return;", AllowsMergedIf); 743 744 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 745 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 746 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 747 FormatStyle::SIS_AllIfsAndElse; 748 verifyFormat("if (a)\n" 749 " // comment\n" 750 " f();\n" 751 "else\n" 752 " // comment\n" 753 " f();", 754 AllowsMergedIfElse); 755 verifyFormat("{\n" 756 " if (a)\n" 757 " label:\n" 758 " f();\n" 759 " else\n" 760 " label:\n" 761 " f();\n" 762 "}", 763 AllowsMergedIfElse); 764 verifyFormat("if (a)\n" 765 " ;\n" 766 "else\n" 767 " ;", 768 AllowsMergedIfElse); 769 verifyFormat("if (a) {\n" 770 "} else {\n" 771 "}", 772 AllowsMergedIfElse); 773 verifyFormat("if (a) return;\n" 774 "else if (b) return;\n" 775 "else return;", 776 AllowsMergedIfElse); 777 verifyFormat("if (a) {\n" 778 "} else return;", 779 AllowsMergedIfElse); 780 verifyFormat("if (a) {\n" 781 "} else if (b) return;\n" 782 "else return;", 783 AllowsMergedIfElse); 784 verifyFormat("if (a) return;\n" 785 "else if (b) {\n" 786 "} else return;", 787 AllowsMergedIfElse); 788 verifyFormat("if (a)\n" 789 " if (b) return;\n" 790 " else return;", 791 AllowsMergedIfElse); 792 verifyFormat("if constexpr (a)\n" 793 " if constexpr (b) return;\n" 794 " else if constexpr (c) return;\n" 795 " else return;", 796 AllowsMergedIfElse); 797 verifyFormat("MYIF (a)\n" 798 " // comment\n" 799 " f();\n" 800 "else\n" 801 " // comment\n" 802 " f();", 803 AllowsMergedIfElse); 804 verifyFormat("{\n" 805 " MYIF (a)\n" 806 " label:\n" 807 " f();\n" 808 " else\n" 809 " label:\n" 810 " f();\n" 811 "}", 812 AllowsMergedIfElse); 813 verifyFormat("MYIF (a)\n" 814 " ;\n" 815 "else\n" 816 " ;", 817 AllowsMergedIfElse); 818 verifyFormat("MYIF (a) {\n" 819 "} else {\n" 820 "}", 821 AllowsMergedIfElse); 822 verifyFormat("MYIF (a) return;\n" 823 "else MYIF (b) return;\n" 824 "else return;", 825 AllowsMergedIfElse); 826 verifyFormat("MYIF (a) {\n" 827 "} else return;", 828 AllowsMergedIfElse); 829 verifyFormat("MYIF (a) {\n" 830 "} else MYIF (b) return;\n" 831 "else return;", 832 AllowsMergedIfElse); 833 verifyFormat("MYIF (a) return;\n" 834 "else MYIF (b) {\n" 835 "} else return;", 836 AllowsMergedIfElse); 837 verifyFormat("MYIF (a)\n" 838 " MYIF (b) return;\n" 839 " else return;", 840 AllowsMergedIfElse); 841 verifyFormat("MYIF constexpr (a)\n" 842 " MYIF constexpr (b) return;\n" 843 " else MYIF constexpr (c) return;\n" 844 " else return;", 845 AllowsMergedIfElse); 846 } 847 848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 849 FormatStyle AllowsMergedIf = getLLVMStyle(); 850 AllowsMergedIf.IfMacros.push_back("MYIF"); 851 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 852 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 853 FormatStyle::SIS_WithoutElse; 854 verifyFormat("if (a)\n" 855 " f();\n" 856 "else {\n" 857 " g();\n" 858 "}", 859 AllowsMergedIf); 860 verifyFormat("if (a)\n" 861 " f();\n" 862 "else\n" 863 " g();\n", 864 AllowsMergedIf); 865 866 verifyFormat("if (a) g();", AllowsMergedIf); 867 verifyFormat("if (a) {\n" 868 " g()\n" 869 "};", 870 AllowsMergedIf); 871 verifyFormat("if (a)\n" 872 " g();\n" 873 "else\n" 874 " g();", 875 AllowsMergedIf); 876 verifyFormat("if (a) {\n" 877 " g();\n" 878 "} else\n" 879 " g();", 880 AllowsMergedIf); 881 verifyFormat("if (a)\n" 882 " g();\n" 883 "else {\n" 884 " g();\n" 885 "}", 886 AllowsMergedIf); 887 verifyFormat("if (a) {\n" 888 " g();\n" 889 "} else {\n" 890 " g();\n" 891 "}", 892 AllowsMergedIf); 893 verifyFormat("if (a)\n" 894 " g();\n" 895 "else if (b)\n" 896 " g();\n" 897 "else\n" 898 " g();", 899 AllowsMergedIf); 900 verifyFormat("if (a) {\n" 901 " g();\n" 902 "} else if (b)\n" 903 " g();\n" 904 "else\n" 905 " g();", 906 AllowsMergedIf); 907 verifyFormat("if (a)\n" 908 " g();\n" 909 "else if (b) {\n" 910 " g();\n" 911 "} else\n" 912 " g();", 913 AllowsMergedIf); 914 verifyFormat("if (a)\n" 915 " g();\n" 916 "else if (b)\n" 917 " g();\n" 918 "else {\n" 919 " g();\n" 920 "}", 921 AllowsMergedIf); 922 verifyFormat("if (a)\n" 923 " g();\n" 924 "else if (b) {\n" 925 " g();\n" 926 "} else {\n" 927 " g();\n" 928 "}", 929 AllowsMergedIf); 930 verifyFormat("if (a) {\n" 931 " g();\n" 932 "} else if (b) {\n" 933 " g();\n" 934 "} else {\n" 935 " g();\n" 936 "}", 937 AllowsMergedIf); 938 verifyFormat("MYIF (a)\n" 939 " f();\n" 940 "else {\n" 941 " g();\n" 942 "}", 943 AllowsMergedIf); 944 verifyFormat("MYIF (a)\n" 945 " f();\n" 946 "else\n" 947 " g();\n", 948 AllowsMergedIf); 949 950 verifyFormat("MYIF (a) g();", AllowsMergedIf); 951 verifyFormat("MYIF (a) {\n" 952 " g()\n" 953 "};", 954 AllowsMergedIf); 955 verifyFormat("MYIF (a)\n" 956 " g();\n" 957 "else\n" 958 " g();", 959 AllowsMergedIf); 960 verifyFormat("MYIF (a) {\n" 961 " g();\n" 962 "} else\n" 963 " g();", 964 AllowsMergedIf); 965 verifyFormat("MYIF (a)\n" 966 " g();\n" 967 "else {\n" 968 " g();\n" 969 "}", 970 AllowsMergedIf); 971 verifyFormat("MYIF (a) {\n" 972 " g();\n" 973 "} else {\n" 974 " g();\n" 975 "}", 976 AllowsMergedIf); 977 verifyFormat("MYIF (a)\n" 978 " g();\n" 979 "else MYIF (b)\n" 980 " g();\n" 981 "else\n" 982 " g();", 983 AllowsMergedIf); 984 verifyFormat("MYIF (a)\n" 985 " g();\n" 986 "else if (b)\n" 987 " g();\n" 988 "else\n" 989 " g();", 990 AllowsMergedIf); 991 verifyFormat("MYIF (a) {\n" 992 " g();\n" 993 "} else MYIF (b)\n" 994 " g();\n" 995 "else\n" 996 " g();", 997 AllowsMergedIf); 998 verifyFormat("MYIF (a) {\n" 999 " g();\n" 1000 "} else if (b)\n" 1001 " g();\n" 1002 "else\n" 1003 " g();", 1004 AllowsMergedIf); 1005 verifyFormat("MYIF (a)\n" 1006 " g();\n" 1007 "else MYIF (b) {\n" 1008 " g();\n" 1009 "} else\n" 1010 " g();", 1011 AllowsMergedIf); 1012 verifyFormat("MYIF (a)\n" 1013 " g();\n" 1014 "else if (b) {\n" 1015 " g();\n" 1016 "} else\n" 1017 " g();", 1018 AllowsMergedIf); 1019 verifyFormat("MYIF (a)\n" 1020 " g();\n" 1021 "else MYIF (b)\n" 1022 " g();\n" 1023 "else {\n" 1024 " g();\n" 1025 "}", 1026 AllowsMergedIf); 1027 verifyFormat("MYIF (a)\n" 1028 " g();\n" 1029 "else if (b)\n" 1030 " g();\n" 1031 "else {\n" 1032 " g();\n" 1033 "}", 1034 AllowsMergedIf); 1035 verifyFormat("MYIF (a)\n" 1036 " g();\n" 1037 "else MYIF (b) {\n" 1038 " g();\n" 1039 "} else {\n" 1040 " g();\n" 1041 "}", 1042 AllowsMergedIf); 1043 verifyFormat("MYIF (a)\n" 1044 " g();\n" 1045 "else if (b) {\n" 1046 " g();\n" 1047 "} else {\n" 1048 " g();\n" 1049 "}", 1050 AllowsMergedIf); 1051 verifyFormat("MYIF (a) {\n" 1052 " g();\n" 1053 "} else MYIF (b) {\n" 1054 " g();\n" 1055 "} else {\n" 1056 " g();\n" 1057 "}", 1058 AllowsMergedIf); 1059 verifyFormat("MYIF (a) {\n" 1060 " g();\n" 1061 "} else if (b) {\n" 1062 " g();\n" 1063 "} else {\n" 1064 " g();\n" 1065 "}", 1066 AllowsMergedIf); 1067 1068 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1069 FormatStyle::SIS_OnlyFirstIf; 1070 1071 verifyFormat("if (a) f();\n" 1072 "else {\n" 1073 " g();\n" 1074 "}", 1075 AllowsMergedIf); 1076 verifyFormat("if (a) f();\n" 1077 "else {\n" 1078 " if (a) f();\n" 1079 " else {\n" 1080 " g();\n" 1081 " }\n" 1082 " g();\n" 1083 "}", 1084 AllowsMergedIf); 1085 1086 verifyFormat("if (a) g();", AllowsMergedIf); 1087 verifyFormat("if (a) {\n" 1088 " g()\n" 1089 "};", 1090 AllowsMergedIf); 1091 verifyFormat("if (a) g();\n" 1092 "else\n" 1093 " g();", 1094 AllowsMergedIf); 1095 verifyFormat("if (a) {\n" 1096 " g();\n" 1097 "} else\n" 1098 " g();", 1099 AllowsMergedIf); 1100 verifyFormat("if (a) g();\n" 1101 "else {\n" 1102 " g();\n" 1103 "}", 1104 AllowsMergedIf); 1105 verifyFormat("if (a) {\n" 1106 " g();\n" 1107 "} else {\n" 1108 " g();\n" 1109 "}", 1110 AllowsMergedIf); 1111 verifyFormat("if (a) g();\n" 1112 "else if (b)\n" 1113 " g();\n" 1114 "else\n" 1115 " g();", 1116 AllowsMergedIf); 1117 verifyFormat("if (a) {\n" 1118 " g();\n" 1119 "} else if (b)\n" 1120 " g();\n" 1121 "else\n" 1122 " g();", 1123 AllowsMergedIf); 1124 verifyFormat("if (a) g();\n" 1125 "else if (b) {\n" 1126 " g();\n" 1127 "} else\n" 1128 " g();", 1129 AllowsMergedIf); 1130 verifyFormat("if (a) g();\n" 1131 "else if (b)\n" 1132 " g();\n" 1133 "else {\n" 1134 " g();\n" 1135 "}", 1136 AllowsMergedIf); 1137 verifyFormat("if (a) g();\n" 1138 "else if (b) {\n" 1139 " g();\n" 1140 "} else {\n" 1141 " g();\n" 1142 "}", 1143 AllowsMergedIf); 1144 verifyFormat("if (a) {\n" 1145 " g();\n" 1146 "} else if (b) {\n" 1147 " g();\n" 1148 "} else {\n" 1149 " g();\n" 1150 "}", 1151 AllowsMergedIf); 1152 verifyFormat("MYIF (a) f();\n" 1153 "else {\n" 1154 " g();\n" 1155 "}", 1156 AllowsMergedIf); 1157 verifyFormat("MYIF (a) f();\n" 1158 "else {\n" 1159 " if (a) f();\n" 1160 " else {\n" 1161 " g();\n" 1162 " }\n" 1163 " g();\n" 1164 "}", 1165 AllowsMergedIf); 1166 1167 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1168 verifyFormat("MYIF (a) {\n" 1169 " g()\n" 1170 "};", 1171 AllowsMergedIf); 1172 verifyFormat("MYIF (a) g();\n" 1173 "else\n" 1174 " g();", 1175 AllowsMergedIf); 1176 verifyFormat("MYIF (a) {\n" 1177 " g();\n" 1178 "} else\n" 1179 " g();", 1180 AllowsMergedIf); 1181 verifyFormat("MYIF (a) g();\n" 1182 "else {\n" 1183 " g();\n" 1184 "}", 1185 AllowsMergedIf); 1186 verifyFormat("MYIF (a) {\n" 1187 " g();\n" 1188 "} else {\n" 1189 " g();\n" 1190 "}", 1191 AllowsMergedIf); 1192 verifyFormat("MYIF (a) g();\n" 1193 "else MYIF (b)\n" 1194 " g();\n" 1195 "else\n" 1196 " g();", 1197 AllowsMergedIf); 1198 verifyFormat("MYIF (a) g();\n" 1199 "else if (b)\n" 1200 " g();\n" 1201 "else\n" 1202 " g();", 1203 AllowsMergedIf); 1204 verifyFormat("MYIF (a) {\n" 1205 " g();\n" 1206 "} else MYIF (b)\n" 1207 " g();\n" 1208 "else\n" 1209 " g();", 1210 AllowsMergedIf); 1211 verifyFormat("MYIF (a) {\n" 1212 " g();\n" 1213 "} else if (b)\n" 1214 " g();\n" 1215 "else\n" 1216 " g();", 1217 AllowsMergedIf); 1218 verifyFormat("MYIF (a) g();\n" 1219 "else MYIF (b) {\n" 1220 " g();\n" 1221 "} else\n" 1222 " g();", 1223 AllowsMergedIf); 1224 verifyFormat("MYIF (a) g();\n" 1225 "else if (b) {\n" 1226 " g();\n" 1227 "} else\n" 1228 " g();", 1229 AllowsMergedIf); 1230 verifyFormat("MYIF (a) g();\n" 1231 "else MYIF (b)\n" 1232 " g();\n" 1233 "else {\n" 1234 " g();\n" 1235 "}", 1236 AllowsMergedIf); 1237 verifyFormat("MYIF (a) g();\n" 1238 "else if (b)\n" 1239 " g();\n" 1240 "else {\n" 1241 " g();\n" 1242 "}", 1243 AllowsMergedIf); 1244 verifyFormat("MYIF (a) g();\n" 1245 "else MYIF (b) {\n" 1246 " g();\n" 1247 "} else {\n" 1248 " g();\n" 1249 "}", 1250 AllowsMergedIf); 1251 verifyFormat("MYIF (a) g();\n" 1252 "else if (b) {\n" 1253 " g();\n" 1254 "} else {\n" 1255 " g();\n" 1256 "}", 1257 AllowsMergedIf); 1258 verifyFormat("MYIF (a) {\n" 1259 " g();\n" 1260 "} else MYIF (b) {\n" 1261 " g();\n" 1262 "} else {\n" 1263 " g();\n" 1264 "}", 1265 AllowsMergedIf); 1266 verifyFormat("MYIF (a) {\n" 1267 " g();\n" 1268 "} else if (b) {\n" 1269 " g();\n" 1270 "} else {\n" 1271 " g();\n" 1272 "}", 1273 AllowsMergedIf); 1274 1275 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1276 FormatStyle::SIS_AllIfsAndElse; 1277 1278 verifyFormat("if (a) f();\n" 1279 "else {\n" 1280 " g();\n" 1281 "}", 1282 AllowsMergedIf); 1283 verifyFormat("if (a) f();\n" 1284 "else {\n" 1285 " if (a) f();\n" 1286 " else {\n" 1287 " g();\n" 1288 " }\n" 1289 " g();\n" 1290 "}", 1291 AllowsMergedIf); 1292 1293 verifyFormat("if (a) g();", AllowsMergedIf); 1294 verifyFormat("if (a) {\n" 1295 " g()\n" 1296 "};", 1297 AllowsMergedIf); 1298 verifyFormat("if (a) g();\n" 1299 "else g();", 1300 AllowsMergedIf); 1301 verifyFormat("if (a) {\n" 1302 " g();\n" 1303 "} else g();", 1304 AllowsMergedIf); 1305 verifyFormat("if (a) g();\n" 1306 "else {\n" 1307 " g();\n" 1308 "}", 1309 AllowsMergedIf); 1310 verifyFormat("if (a) {\n" 1311 " g();\n" 1312 "} else {\n" 1313 " g();\n" 1314 "}", 1315 AllowsMergedIf); 1316 verifyFormat("if (a) g();\n" 1317 "else if (b) g();\n" 1318 "else g();", 1319 AllowsMergedIf); 1320 verifyFormat("if (a) {\n" 1321 " g();\n" 1322 "} else if (b) g();\n" 1323 "else g();", 1324 AllowsMergedIf); 1325 verifyFormat("if (a) g();\n" 1326 "else if (b) {\n" 1327 " g();\n" 1328 "} else g();", 1329 AllowsMergedIf); 1330 verifyFormat("if (a) g();\n" 1331 "else if (b) g();\n" 1332 "else {\n" 1333 " g();\n" 1334 "}", 1335 AllowsMergedIf); 1336 verifyFormat("if (a) g();\n" 1337 "else if (b) {\n" 1338 " g();\n" 1339 "} else {\n" 1340 " g();\n" 1341 "}", 1342 AllowsMergedIf); 1343 verifyFormat("if (a) {\n" 1344 " g();\n" 1345 "} else if (b) {\n" 1346 " g();\n" 1347 "} else {\n" 1348 " g();\n" 1349 "}", 1350 AllowsMergedIf); 1351 verifyFormat("MYIF (a) f();\n" 1352 "else {\n" 1353 " g();\n" 1354 "}", 1355 AllowsMergedIf); 1356 verifyFormat("MYIF (a) f();\n" 1357 "else {\n" 1358 " if (a) f();\n" 1359 " else {\n" 1360 " g();\n" 1361 " }\n" 1362 " g();\n" 1363 "}", 1364 AllowsMergedIf); 1365 1366 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1367 verifyFormat("MYIF (a) {\n" 1368 " g()\n" 1369 "};", 1370 AllowsMergedIf); 1371 verifyFormat("MYIF (a) g();\n" 1372 "else g();", 1373 AllowsMergedIf); 1374 verifyFormat("MYIF (a) {\n" 1375 " g();\n" 1376 "} else g();", 1377 AllowsMergedIf); 1378 verifyFormat("MYIF (a) g();\n" 1379 "else {\n" 1380 " g();\n" 1381 "}", 1382 AllowsMergedIf); 1383 verifyFormat("MYIF (a) {\n" 1384 " g();\n" 1385 "} else {\n" 1386 " g();\n" 1387 "}", 1388 AllowsMergedIf); 1389 verifyFormat("MYIF (a) g();\n" 1390 "else MYIF (b) g();\n" 1391 "else g();", 1392 AllowsMergedIf); 1393 verifyFormat("MYIF (a) g();\n" 1394 "else if (b) g();\n" 1395 "else g();", 1396 AllowsMergedIf); 1397 verifyFormat("MYIF (a) {\n" 1398 " g();\n" 1399 "} else MYIF (b) g();\n" 1400 "else g();", 1401 AllowsMergedIf); 1402 verifyFormat("MYIF (a) {\n" 1403 " g();\n" 1404 "} else if (b) g();\n" 1405 "else g();", 1406 AllowsMergedIf); 1407 verifyFormat("MYIF (a) g();\n" 1408 "else MYIF (b) {\n" 1409 " g();\n" 1410 "} else g();", 1411 AllowsMergedIf); 1412 verifyFormat("MYIF (a) g();\n" 1413 "else if (b) {\n" 1414 " g();\n" 1415 "} else g();", 1416 AllowsMergedIf); 1417 verifyFormat("MYIF (a) g();\n" 1418 "else MYIF (b) g();\n" 1419 "else {\n" 1420 " g();\n" 1421 "}", 1422 AllowsMergedIf); 1423 verifyFormat("MYIF (a) g();\n" 1424 "else if (b) g();\n" 1425 "else {\n" 1426 " g();\n" 1427 "}", 1428 AllowsMergedIf); 1429 verifyFormat("MYIF (a) g();\n" 1430 "else MYIF (b) {\n" 1431 " g();\n" 1432 "} else {\n" 1433 " g();\n" 1434 "}", 1435 AllowsMergedIf); 1436 verifyFormat("MYIF (a) g();\n" 1437 "else if (b) {\n" 1438 " g();\n" 1439 "} else {\n" 1440 " g();\n" 1441 "}", 1442 AllowsMergedIf); 1443 verifyFormat("MYIF (a) {\n" 1444 " g();\n" 1445 "} else MYIF (b) {\n" 1446 " g();\n" 1447 "} else {\n" 1448 " g();\n" 1449 "}", 1450 AllowsMergedIf); 1451 verifyFormat("MYIF (a) {\n" 1452 " g();\n" 1453 "} else if (b) {\n" 1454 " g();\n" 1455 "} else {\n" 1456 " g();\n" 1457 "}", 1458 AllowsMergedIf); 1459 } 1460 1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1462 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1463 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1464 verifyFormat("while (true) continue;", AllowsMergedLoops); 1465 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1466 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1467 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops); 1468 verifyFormat("while (true)\n" 1469 " ;", 1470 AllowsMergedLoops); 1471 verifyFormat("for (;;)\n" 1472 " ;", 1473 AllowsMergedLoops); 1474 verifyFormat("for (;;)\n" 1475 " for (;;) continue;", 1476 AllowsMergedLoops); 1477 verifyFormat("for (;;)\n" 1478 " while (true) continue;", 1479 AllowsMergedLoops); 1480 verifyFormat("while (true)\n" 1481 " for (;;) continue;", 1482 AllowsMergedLoops); 1483 verifyFormat("BOOST_FOREACH (int &v, vec)\n" 1484 " for (;;) continue;", 1485 AllowsMergedLoops); 1486 verifyFormat("for (;;)\n" 1487 " BOOST_FOREACH (int &v, vec) continue;", 1488 AllowsMergedLoops); 1489 verifyFormat("for (;;) // Can't merge this\n" 1490 " continue;", 1491 AllowsMergedLoops); 1492 verifyFormat("for (;;) /* still don't merge */\n" 1493 " continue;", 1494 AllowsMergedLoops); 1495 verifyFormat("do a++;\n" 1496 "while (true);", 1497 AllowsMergedLoops); 1498 verifyFormat("do /* Don't merge */\n" 1499 " a++;\n" 1500 "while (true);", 1501 AllowsMergedLoops); 1502 verifyFormat("do // Don't merge\n" 1503 " a++;\n" 1504 "while (true);", 1505 AllowsMergedLoops); 1506 verifyFormat("do\n" 1507 " // Don't merge\n" 1508 " a++;\n" 1509 "while (true);", 1510 AllowsMergedLoops); 1511 // Without braces labels are interpreted differently. 1512 verifyFormat("{\n" 1513 " do\n" 1514 " label:\n" 1515 " a++;\n" 1516 " while (true);\n" 1517 "}", 1518 AllowsMergedLoops); 1519 } 1520 1521 TEST_F(FormatTest, FormatShortBracedStatements) { 1522 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1523 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1524 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1525 // Not IF to avoid any confusion that IF is somehow special. 1526 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1527 AllowSimpleBracedStatements.ColumnLimit = 40; 1528 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1529 FormatStyle::SBS_Always; 1530 1531 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1532 FormatStyle::SIS_WithoutElse; 1533 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1534 1535 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1536 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1537 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1538 1539 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1540 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1541 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1542 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1543 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1544 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1545 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1546 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1547 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1548 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1549 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1550 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1551 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1552 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1553 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1554 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1555 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1556 AllowSimpleBracedStatements); 1557 verifyFormat("if (true) {\n" 1558 " ffffffffffffffffffffffff();\n" 1559 "}", 1560 AllowSimpleBracedStatements); 1561 verifyFormat("if (true) {\n" 1562 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1563 "}", 1564 AllowSimpleBracedStatements); 1565 verifyFormat("if (true) { //\n" 1566 " f();\n" 1567 "}", 1568 AllowSimpleBracedStatements); 1569 verifyFormat("if (true) {\n" 1570 " f();\n" 1571 " f();\n" 1572 "}", 1573 AllowSimpleBracedStatements); 1574 verifyFormat("if (true) {\n" 1575 " f();\n" 1576 "} else {\n" 1577 " f();\n" 1578 "}", 1579 AllowSimpleBracedStatements); 1580 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1581 AllowSimpleBracedStatements); 1582 verifyFormat("MYIF (true) {\n" 1583 " ffffffffffffffffffffffff();\n" 1584 "}", 1585 AllowSimpleBracedStatements); 1586 verifyFormat("MYIF (true) {\n" 1587 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1588 "}", 1589 AllowSimpleBracedStatements); 1590 verifyFormat("MYIF (true) { //\n" 1591 " f();\n" 1592 "}", 1593 AllowSimpleBracedStatements); 1594 verifyFormat("MYIF (true) {\n" 1595 " f();\n" 1596 " f();\n" 1597 "}", 1598 AllowSimpleBracedStatements); 1599 verifyFormat("MYIF (true) {\n" 1600 " f();\n" 1601 "} else {\n" 1602 " f();\n" 1603 "}", 1604 AllowSimpleBracedStatements); 1605 1606 verifyFormat("struct A2 {\n" 1607 " int X;\n" 1608 "};", 1609 AllowSimpleBracedStatements); 1610 verifyFormat("typedef struct A2 {\n" 1611 " int X;\n" 1612 "} A2_t;", 1613 AllowSimpleBracedStatements); 1614 verifyFormat("template <int> struct A2 {\n" 1615 " struct B {};\n" 1616 "};", 1617 AllowSimpleBracedStatements); 1618 1619 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1620 FormatStyle::SIS_Never; 1621 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1622 verifyFormat("if (true) {\n" 1623 " f();\n" 1624 "}", 1625 AllowSimpleBracedStatements); 1626 verifyFormat("if (true) {\n" 1627 " f();\n" 1628 "} else {\n" 1629 " f();\n" 1630 "}", 1631 AllowSimpleBracedStatements); 1632 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1633 verifyFormat("MYIF (true) {\n" 1634 " f();\n" 1635 "}", 1636 AllowSimpleBracedStatements); 1637 verifyFormat("MYIF (true) {\n" 1638 " f();\n" 1639 "} else {\n" 1640 " f();\n" 1641 "}", 1642 AllowSimpleBracedStatements); 1643 1644 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1645 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1646 verifyFormat("while (true) {\n" 1647 " f();\n" 1648 "}", 1649 AllowSimpleBracedStatements); 1650 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1651 verifyFormat("for (;;) {\n" 1652 " f();\n" 1653 "}", 1654 AllowSimpleBracedStatements); 1655 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1656 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1657 " f();\n" 1658 "}", 1659 AllowSimpleBracedStatements); 1660 1661 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1662 FormatStyle::SIS_WithoutElse; 1663 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1664 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1665 FormatStyle::BWACS_Always; 1666 1667 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1668 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1669 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1670 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1671 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1672 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1673 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1674 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1675 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1676 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1677 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1678 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1679 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1680 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1681 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1682 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1683 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1684 AllowSimpleBracedStatements); 1685 verifyFormat("if (true)\n" 1686 "{\n" 1687 " ffffffffffffffffffffffff();\n" 1688 "}", 1689 AllowSimpleBracedStatements); 1690 verifyFormat("if (true)\n" 1691 "{\n" 1692 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1693 "}", 1694 AllowSimpleBracedStatements); 1695 verifyFormat("if (true)\n" 1696 "{ //\n" 1697 " f();\n" 1698 "}", 1699 AllowSimpleBracedStatements); 1700 verifyFormat("if (true)\n" 1701 "{\n" 1702 " f();\n" 1703 " f();\n" 1704 "}", 1705 AllowSimpleBracedStatements); 1706 verifyFormat("if (true)\n" 1707 "{\n" 1708 " f();\n" 1709 "} else\n" 1710 "{\n" 1711 " f();\n" 1712 "}", 1713 AllowSimpleBracedStatements); 1714 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1715 AllowSimpleBracedStatements); 1716 verifyFormat("MYIF (true)\n" 1717 "{\n" 1718 " ffffffffffffffffffffffff();\n" 1719 "}", 1720 AllowSimpleBracedStatements); 1721 verifyFormat("MYIF (true)\n" 1722 "{\n" 1723 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1724 "}", 1725 AllowSimpleBracedStatements); 1726 verifyFormat("MYIF (true)\n" 1727 "{ //\n" 1728 " f();\n" 1729 "}", 1730 AllowSimpleBracedStatements); 1731 verifyFormat("MYIF (true)\n" 1732 "{\n" 1733 " f();\n" 1734 " f();\n" 1735 "}", 1736 AllowSimpleBracedStatements); 1737 verifyFormat("MYIF (true)\n" 1738 "{\n" 1739 " f();\n" 1740 "} else\n" 1741 "{\n" 1742 " f();\n" 1743 "}", 1744 AllowSimpleBracedStatements); 1745 1746 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1747 FormatStyle::SIS_Never; 1748 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1749 verifyFormat("if (true)\n" 1750 "{\n" 1751 " f();\n" 1752 "}", 1753 AllowSimpleBracedStatements); 1754 verifyFormat("if (true)\n" 1755 "{\n" 1756 " f();\n" 1757 "} else\n" 1758 "{\n" 1759 " f();\n" 1760 "}", 1761 AllowSimpleBracedStatements); 1762 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1763 verifyFormat("MYIF (true)\n" 1764 "{\n" 1765 " f();\n" 1766 "}", 1767 AllowSimpleBracedStatements); 1768 verifyFormat("MYIF (true)\n" 1769 "{\n" 1770 " f();\n" 1771 "} else\n" 1772 "{\n" 1773 " f();\n" 1774 "}", 1775 AllowSimpleBracedStatements); 1776 1777 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1778 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1779 verifyFormat("while (true)\n" 1780 "{\n" 1781 " f();\n" 1782 "}", 1783 AllowSimpleBracedStatements); 1784 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1785 verifyFormat("for (;;)\n" 1786 "{\n" 1787 " f();\n" 1788 "}", 1789 AllowSimpleBracedStatements); 1790 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1791 verifyFormat("BOOST_FOREACH (int v, vec)\n" 1792 "{\n" 1793 " f();\n" 1794 "}", 1795 AllowSimpleBracedStatements); 1796 } 1797 1798 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1799 FormatStyle Style = getLLVMStyleWithColumns(60); 1800 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1801 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1802 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1803 EXPECT_EQ("#define A \\\n" 1804 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1805 " { \\\n" 1806 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1807 " }\n" 1808 "X;", 1809 format("#define A \\\n" 1810 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1811 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1812 " }\n" 1813 "X;", 1814 Style)); 1815 } 1816 1817 TEST_F(FormatTest, ParseIfElse) { 1818 verifyFormat("if (true)\n" 1819 " if (true)\n" 1820 " if (true)\n" 1821 " f();\n" 1822 " else\n" 1823 " g();\n" 1824 " else\n" 1825 " h();\n" 1826 "else\n" 1827 " i();"); 1828 verifyFormat("if (true)\n" 1829 " if (true)\n" 1830 " if (true) {\n" 1831 " if (true)\n" 1832 " f();\n" 1833 " } else {\n" 1834 " g();\n" 1835 " }\n" 1836 " else\n" 1837 " h();\n" 1838 "else {\n" 1839 " i();\n" 1840 "}"); 1841 verifyFormat("if (true)\n" 1842 " if constexpr (true)\n" 1843 " if (true) {\n" 1844 " if constexpr (true)\n" 1845 " f();\n" 1846 " } else {\n" 1847 " g();\n" 1848 " }\n" 1849 " else\n" 1850 " h();\n" 1851 "else {\n" 1852 " i();\n" 1853 "}"); 1854 verifyFormat("if (true)\n" 1855 " if CONSTEXPR (true)\n" 1856 " if (true) {\n" 1857 " if CONSTEXPR (true)\n" 1858 " f();\n" 1859 " } else {\n" 1860 " g();\n" 1861 " }\n" 1862 " else\n" 1863 " h();\n" 1864 "else {\n" 1865 " i();\n" 1866 "}"); 1867 verifyFormat("void f() {\n" 1868 " if (a) {\n" 1869 " } else {\n" 1870 " }\n" 1871 "}"); 1872 } 1873 1874 TEST_F(FormatTest, ElseIf) { 1875 verifyFormat("if (a) {\n} else if (b) {\n}"); 1876 verifyFormat("if (a)\n" 1877 " f();\n" 1878 "else if (b)\n" 1879 " g();\n" 1880 "else\n" 1881 " h();"); 1882 verifyFormat("if (a)\n" 1883 " f();\n" 1884 "else // comment\n" 1885 " if (b) {\n" 1886 " g();\n" 1887 " h();\n" 1888 " }"); 1889 verifyFormat("if constexpr (a)\n" 1890 " f();\n" 1891 "else if constexpr (b)\n" 1892 " g();\n" 1893 "else\n" 1894 " h();"); 1895 verifyFormat("if CONSTEXPR (a)\n" 1896 " f();\n" 1897 "else if CONSTEXPR (b)\n" 1898 " g();\n" 1899 "else\n" 1900 " h();"); 1901 verifyFormat("if (a) {\n" 1902 " f();\n" 1903 "}\n" 1904 "// or else ..\n" 1905 "else {\n" 1906 " g()\n" 1907 "}"); 1908 1909 verifyFormat("if (a) {\n" 1910 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1911 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1912 "}"); 1913 verifyFormat("if (a) {\n" 1914 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1916 "}"); 1917 verifyFormat("if (a) {\n" 1918 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1919 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1920 "}"); 1921 verifyFormat("if (a) {\n" 1922 "} else if (\n" 1923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1924 "}", 1925 getLLVMStyleWithColumns(62)); 1926 verifyFormat("if (a) {\n" 1927 "} else if constexpr (\n" 1928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1929 "}", 1930 getLLVMStyleWithColumns(62)); 1931 verifyFormat("if (a) {\n" 1932 "} else if CONSTEXPR (\n" 1933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1934 "}", 1935 getLLVMStyleWithColumns(62)); 1936 } 1937 1938 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 1939 FormatStyle Style = getLLVMStyle(); 1940 // Check first the default LLVM style 1941 // Style.PointerAlignment = FormatStyle::PAS_Right; 1942 // Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1943 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 1944 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 1945 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 1946 verifyFormat("int *f1(int &a) const &;", Style); 1947 verifyFormat("int *f1(int &a) const & = 0;", Style); 1948 verifyFormat("int *a = f1();", Style); 1949 verifyFormat("int &b = f2();", Style); 1950 verifyFormat("int &&c = f3();", Style); 1951 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 1952 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); 1953 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 1954 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 1955 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 1956 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); 1957 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 1958 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); 1959 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); 1960 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); 1961 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); 1962 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); 1963 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); 1964 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); 1965 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); 1966 verifyFormat("for (f(); int &c : {1, 2, 3})", Style); 1967 1968 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1969 verifyFormat("Const unsigned int *c;\n" 1970 "const unsigned int *d;\n" 1971 "Const unsigned int &e;\n" 1972 "const unsigned int &f;\n" 1973 "const unsigned &&g;\n" 1974 "Const unsigned h;", 1975 Style); 1976 1977 Style.PointerAlignment = FormatStyle::PAS_Left; 1978 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1979 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 1980 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 1981 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 1982 verifyFormat("int* f1(int& a) const& = 0;", Style); 1983 verifyFormat("int* a = f1();", Style); 1984 verifyFormat("int& b = f2();", Style); 1985 verifyFormat("int&& c = f3();", Style); 1986 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 1987 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); 1988 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 1989 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 1990 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 1991 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); 1992 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 1993 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 1994 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style); 1995 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style); 1996 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style); 1997 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 1998 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); 1999 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); 2000 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); 2001 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); 2002 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); 2003 verifyFormat("for (f(); int& c : {1, 2, 3})", Style); 2004 2005 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2006 verifyFormat("Const unsigned int* c;\n" 2007 "const unsigned int* d;\n" 2008 "Const unsigned int& e;\n" 2009 "const unsigned int& f;\n" 2010 "const unsigned&& g;\n" 2011 "Const unsigned h;", 2012 Style); 2013 2014 Style.PointerAlignment = FormatStyle::PAS_Right; 2015 Style.ReferenceAlignment = FormatStyle::RAS_Left; 2016 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 2017 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 2018 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 2019 verifyFormat("int *a = f1();", Style); 2020 verifyFormat("int& b = f2();", Style); 2021 verifyFormat("int&& c = f3();", Style); 2022 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2023 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2024 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); 2025 2026 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2027 verifyFormat("Const unsigned int *c;\n" 2028 "const unsigned int *d;\n" 2029 "Const unsigned int& e;\n" 2030 "const unsigned int& f;\n" 2031 "const unsigned g;\n" 2032 "Const unsigned h;", 2033 Style); 2034 2035 Style.PointerAlignment = FormatStyle::PAS_Left; 2036 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 2037 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 2038 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 2039 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 2040 verifyFormat("int* a = f1();", Style); 2041 verifyFormat("int & b = f2();", Style); 2042 verifyFormat("int && c = f3();", Style); 2043 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); 2044 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); 2045 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); 2046 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2047 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); 2048 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); 2049 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); 2050 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2051 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); 2052 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); 2053 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); 2054 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); 2055 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); 2056 verifyFormat("for (f(); int & c : {1, 2, 3})", Style); 2057 2058 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2059 verifyFormat("Const unsigned int* c;\n" 2060 "const unsigned int* d;\n" 2061 "Const unsigned int & e;\n" 2062 "const unsigned int & f;\n" 2063 "const unsigned && g;\n" 2064 "Const unsigned h;", 2065 Style); 2066 2067 Style.PointerAlignment = FormatStyle::PAS_Middle; 2068 Style.ReferenceAlignment = FormatStyle::RAS_Right; 2069 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 2070 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 2071 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 2072 verifyFormat("int * a = f1();", Style); 2073 verifyFormat("int &b = f2();", Style); 2074 verifyFormat("int &&c = f3();", Style); 2075 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2076 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2077 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); 2078 2079 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2080 // specifically handled 2081 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2082 } 2083 2084 TEST_F(FormatTest, FormatsForLoop) { 2085 verifyFormat( 2086 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2087 " ++VeryVeryLongLoopVariable)\n" 2088 " ;"); 2089 verifyFormat("for (;;)\n" 2090 " f();"); 2091 verifyFormat("for (;;) {\n}"); 2092 verifyFormat("for (;;) {\n" 2093 " f();\n" 2094 "}"); 2095 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2096 2097 verifyFormat( 2098 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2099 " E = UnwrappedLines.end();\n" 2100 " I != E; ++I) {\n}"); 2101 2102 verifyFormat( 2103 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2104 " ++IIIII) {\n}"); 2105 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2106 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2107 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2108 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2109 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2110 " E = FD->getDeclsInPrototypeScope().end();\n" 2111 " I != E; ++I) {\n}"); 2112 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2113 " I = Container.begin(),\n" 2114 " E = Container.end();\n" 2115 " I != E; ++I) {\n}", 2116 getLLVMStyleWithColumns(76)); 2117 2118 verifyFormat( 2119 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2123 " ++aaaaaaaaaaa) {\n}"); 2124 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2125 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2126 " ++i) {\n}"); 2127 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2128 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2129 "}"); 2130 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2131 " aaaaaaaaaa);\n" 2132 " iter; ++iter) {\n" 2133 "}"); 2134 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2135 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2136 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2137 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2138 2139 // These should not be formatted as Objective-C for-in loops. 2140 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2141 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2142 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2143 verifyFormat( 2144 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2145 2146 FormatStyle NoBinPacking = getLLVMStyle(); 2147 NoBinPacking.BinPackParameters = false; 2148 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2149 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2150 " aaaaaaaaaaaaaaaa,\n" 2151 " aaaaaaaaaaaaaaaa,\n" 2152 " aaaaaaaaaaaaaaaa);\n" 2153 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2154 "}", 2155 NoBinPacking); 2156 verifyFormat( 2157 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2158 " E = UnwrappedLines.end();\n" 2159 " I != E;\n" 2160 " ++I) {\n}", 2161 NoBinPacking); 2162 2163 FormatStyle AlignLeft = getLLVMStyle(); 2164 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2165 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2166 } 2167 2168 TEST_F(FormatTest, RangeBasedForLoops) { 2169 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2171 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2172 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2173 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2175 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2176 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2177 } 2178 2179 TEST_F(FormatTest, ForEachLoops) { 2180 FormatStyle Style = getLLVMStyle(); 2181 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2182 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false); 2183 verifyFormat("void f() {\n" 2184 " for (;;) {\n" 2185 " }\n" 2186 " foreach (Item *item, itemlist) {\n" 2187 " }\n" 2188 " Q_FOREACH (Item *item, itemlist) {\n" 2189 " }\n" 2190 " BOOST_FOREACH (Item *item, itemlist) {\n" 2191 " }\n" 2192 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2193 "}", 2194 Style); 2195 verifyFormat("void f() {\n" 2196 " for (;;)\n" 2197 " int j = 1;\n" 2198 " Q_FOREACH (int v, vec)\n" 2199 " v *= 2;\n" 2200 " for (;;) {\n" 2201 " int j = 1;\n" 2202 " }\n" 2203 " Q_FOREACH (int v, vec) {\n" 2204 " v *= 2;\n" 2205 " }\n" 2206 "}", 2207 Style); 2208 2209 FormatStyle ShortBlocks = getLLVMStyle(); 2210 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2211 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false); 2212 verifyFormat("void f() {\n" 2213 " for (;;)\n" 2214 " int j = 1;\n" 2215 " Q_FOREACH (int &v, vec)\n" 2216 " v *= 2;\n" 2217 " for (;;) {\n" 2218 " int j = 1;\n" 2219 " }\n" 2220 " Q_FOREACH (int &v, vec) {\n" 2221 " int j = 1;\n" 2222 " }\n" 2223 "}", 2224 ShortBlocks); 2225 2226 FormatStyle ShortLoops = getLLVMStyle(); 2227 ShortLoops.AllowShortLoopsOnASingleLine = true; 2228 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2229 verifyFormat("void f() {\n" 2230 " for (;;) int j = 1;\n" 2231 " Q_FOREACH (int &v, vec) int j = 1;\n" 2232 " for (;;) {\n" 2233 " int j = 1;\n" 2234 " }\n" 2235 " Q_FOREACH (int &v, vec) {\n" 2236 " int j = 1;\n" 2237 " }\n" 2238 "}", 2239 ShortLoops); 2240 2241 FormatStyle ShortBlocksAndLoops = getLLVMStyle(); 2242 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2243 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true; 2244 verifyFormat("void f() {\n" 2245 " for (;;) int j = 1;\n" 2246 " Q_FOREACH (int &v, vec) int j = 1;\n" 2247 " for (;;) { int j = 1; }\n" 2248 " Q_FOREACH (int &v, vec) { int j = 1; }\n" 2249 "}", 2250 ShortBlocksAndLoops); 2251 2252 Style.SpaceBeforeParens = 2253 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2254 verifyFormat("void f() {\n" 2255 " for (;;) {\n" 2256 " }\n" 2257 " foreach(Item *item, itemlist) {\n" 2258 " }\n" 2259 " Q_FOREACH(Item *item, itemlist) {\n" 2260 " }\n" 2261 " BOOST_FOREACH(Item *item, itemlist) {\n" 2262 " }\n" 2263 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2264 "}", 2265 Style); 2266 2267 // As function-like macros. 2268 verifyFormat("#define foreach(x, y)\n" 2269 "#define Q_FOREACH(x, y)\n" 2270 "#define BOOST_FOREACH(x, y)\n" 2271 "#define UNKNOWN_FOREACH(x, y)\n"); 2272 2273 // Not as function-like macros. 2274 verifyFormat("#define foreach (x, y)\n" 2275 "#define Q_FOREACH (x, y)\n" 2276 "#define BOOST_FOREACH (x, y)\n" 2277 "#define UNKNOWN_FOREACH (x, y)\n"); 2278 2279 // handle microsoft non standard extension 2280 verifyFormat("for each (char c in x->MyStringProperty)"); 2281 } 2282 2283 TEST_F(FormatTest, FormatsWhileLoop) { 2284 verifyFormat("while (true) {\n}"); 2285 verifyFormat("while (true)\n" 2286 " f();"); 2287 verifyFormat("while () {\n}"); 2288 verifyFormat("while () {\n" 2289 " f();\n" 2290 "}"); 2291 } 2292 2293 TEST_F(FormatTest, FormatsDoWhile) { 2294 verifyFormat("do {\n" 2295 " do_something();\n" 2296 "} while (something());"); 2297 verifyFormat("do\n" 2298 " do_something();\n" 2299 "while (something());"); 2300 } 2301 2302 TEST_F(FormatTest, FormatsSwitchStatement) { 2303 verifyFormat("switch (x) {\n" 2304 "case 1:\n" 2305 " f();\n" 2306 " break;\n" 2307 "case kFoo:\n" 2308 "case ns::kBar:\n" 2309 "case kBaz:\n" 2310 " break;\n" 2311 "default:\n" 2312 " g();\n" 2313 " break;\n" 2314 "}"); 2315 verifyFormat("switch (x) {\n" 2316 "case 1: {\n" 2317 " f();\n" 2318 " break;\n" 2319 "}\n" 2320 "case 2: {\n" 2321 " break;\n" 2322 "}\n" 2323 "}"); 2324 verifyFormat("switch (x) {\n" 2325 "case 1: {\n" 2326 " f();\n" 2327 " {\n" 2328 " g();\n" 2329 " h();\n" 2330 " }\n" 2331 " break;\n" 2332 "}\n" 2333 "}"); 2334 verifyFormat("switch (x) {\n" 2335 "case 1: {\n" 2336 " f();\n" 2337 " if (foo) {\n" 2338 " g();\n" 2339 " h();\n" 2340 " }\n" 2341 " break;\n" 2342 "}\n" 2343 "}"); 2344 verifyFormat("switch (x) {\n" 2345 "case 1: {\n" 2346 " f();\n" 2347 " g();\n" 2348 "} break;\n" 2349 "}"); 2350 verifyFormat("switch (test)\n" 2351 " ;"); 2352 verifyFormat("switch (x) {\n" 2353 "default: {\n" 2354 " // Do nothing.\n" 2355 "}\n" 2356 "}"); 2357 verifyFormat("switch (x) {\n" 2358 "// comment\n" 2359 "// if 1, do f()\n" 2360 "case 1:\n" 2361 " f();\n" 2362 "}"); 2363 verifyFormat("switch (x) {\n" 2364 "case 1:\n" 2365 " // Do amazing stuff\n" 2366 " {\n" 2367 " f();\n" 2368 " g();\n" 2369 " }\n" 2370 " break;\n" 2371 "}"); 2372 verifyFormat("#define A \\\n" 2373 " switch (x) { \\\n" 2374 " case a: \\\n" 2375 " foo = b; \\\n" 2376 " }", 2377 getLLVMStyleWithColumns(20)); 2378 verifyFormat("#define OPERATION_CASE(name) \\\n" 2379 " case OP_name: \\\n" 2380 " return operations::Operation##name\n", 2381 getLLVMStyleWithColumns(40)); 2382 verifyFormat("switch (x) {\n" 2383 "case 1:;\n" 2384 "default:;\n" 2385 " int i;\n" 2386 "}"); 2387 2388 verifyGoogleFormat("switch (x) {\n" 2389 " case 1:\n" 2390 " f();\n" 2391 " break;\n" 2392 " case kFoo:\n" 2393 " case ns::kBar:\n" 2394 " case kBaz:\n" 2395 " break;\n" 2396 " default:\n" 2397 " g();\n" 2398 " break;\n" 2399 "}"); 2400 verifyGoogleFormat("switch (x) {\n" 2401 " case 1: {\n" 2402 " f();\n" 2403 " break;\n" 2404 " }\n" 2405 "}"); 2406 verifyGoogleFormat("switch (test)\n" 2407 " ;"); 2408 2409 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2410 " case OP_name: \\\n" 2411 " return operations::Operation##name\n"); 2412 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2413 " // Get the correction operation class.\n" 2414 " switch (OpCode) {\n" 2415 " CASE(Add);\n" 2416 " CASE(Subtract);\n" 2417 " default:\n" 2418 " return operations::Unknown;\n" 2419 " }\n" 2420 "#undef OPERATION_CASE\n" 2421 "}"); 2422 verifyFormat("DEBUG({\n" 2423 " switch (x) {\n" 2424 " case A:\n" 2425 " f();\n" 2426 " break;\n" 2427 " // fallthrough\n" 2428 " case B:\n" 2429 " g();\n" 2430 " break;\n" 2431 " }\n" 2432 "});"); 2433 EXPECT_EQ("DEBUG({\n" 2434 " switch (x) {\n" 2435 " case A:\n" 2436 " f();\n" 2437 " break;\n" 2438 " // On B:\n" 2439 " case B:\n" 2440 " g();\n" 2441 " break;\n" 2442 " }\n" 2443 "});", 2444 format("DEBUG({\n" 2445 " switch (x) {\n" 2446 " case A:\n" 2447 " f();\n" 2448 " break;\n" 2449 " // On B:\n" 2450 " case B:\n" 2451 " g();\n" 2452 " break;\n" 2453 " }\n" 2454 "});", 2455 getLLVMStyle())); 2456 EXPECT_EQ("switch (n) {\n" 2457 "case 0: {\n" 2458 " return false;\n" 2459 "}\n" 2460 "default: {\n" 2461 " return true;\n" 2462 "}\n" 2463 "}", 2464 format("switch (n)\n" 2465 "{\n" 2466 "case 0: {\n" 2467 " return false;\n" 2468 "}\n" 2469 "default: {\n" 2470 " return true;\n" 2471 "}\n" 2472 "}", 2473 getLLVMStyle())); 2474 verifyFormat("switch (a) {\n" 2475 "case (b):\n" 2476 " return;\n" 2477 "}"); 2478 2479 verifyFormat("switch (a) {\n" 2480 "case some_namespace::\n" 2481 " some_constant:\n" 2482 " return;\n" 2483 "}", 2484 getLLVMStyleWithColumns(34)); 2485 2486 FormatStyle Style = getLLVMStyle(); 2487 Style.IndentCaseLabels = true; 2488 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2489 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2490 Style.BraceWrapping.AfterCaseLabel = true; 2491 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2492 EXPECT_EQ("switch (n)\n" 2493 "{\n" 2494 " case 0:\n" 2495 " {\n" 2496 " return false;\n" 2497 " }\n" 2498 " default:\n" 2499 " {\n" 2500 " return true;\n" 2501 " }\n" 2502 "}", 2503 format("switch (n) {\n" 2504 " case 0: {\n" 2505 " return false;\n" 2506 " }\n" 2507 " default: {\n" 2508 " return true;\n" 2509 " }\n" 2510 "}", 2511 Style)); 2512 Style.BraceWrapping.AfterCaseLabel = false; 2513 EXPECT_EQ("switch (n)\n" 2514 "{\n" 2515 " case 0: {\n" 2516 " return false;\n" 2517 " }\n" 2518 " default: {\n" 2519 " return true;\n" 2520 " }\n" 2521 "}", 2522 format("switch (n) {\n" 2523 " case 0:\n" 2524 " {\n" 2525 " return false;\n" 2526 " }\n" 2527 " default:\n" 2528 " {\n" 2529 " return true;\n" 2530 " }\n" 2531 "}", 2532 Style)); 2533 Style.IndentCaseLabels = false; 2534 Style.IndentCaseBlocks = true; 2535 EXPECT_EQ("switch (n)\n" 2536 "{\n" 2537 "case 0:\n" 2538 " {\n" 2539 " return false;\n" 2540 " }\n" 2541 "case 1:\n" 2542 " break;\n" 2543 "default:\n" 2544 " {\n" 2545 " return true;\n" 2546 " }\n" 2547 "}", 2548 format("switch (n) {\n" 2549 "case 0: {\n" 2550 " return false;\n" 2551 "}\n" 2552 "case 1:\n" 2553 " break;\n" 2554 "default: {\n" 2555 " return true;\n" 2556 "}\n" 2557 "}", 2558 Style)); 2559 Style.IndentCaseLabels = true; 2560 Style.IndentCaseBlocks = true; 2561 EXPECT_EQ("switch (n)\n" 2562 "{\n" 2563 " case 0:\n" 2564 " {\n" 2565 " return false;\n" 2566 " }\n" 2567 " case 1:\n" 2568 " break;\n" 2569 " default:\n" 2570 " {\n" 2571 " return true;\n" 2572 " }\n" 2573 "}", 2574 format("switch (n) {\n" 2575 "case 0: {\n" 2576 " return false;\n" 2577 "}\n" 2578 "case 1:\n" 2579 " break;\n" 2580 "default: {\n" 2581 " return true;\n" 2582 "}\n" 2583 "}", 2584 Style)); 2585 } 2586 2587 TEST_F(FormatTest, CaseRanges) { 2588 verifyFormat("switch (x) {\n" 2589 "case 'A' ... 'Z':\n" 2590 "case 1 ... 5:\n" 2591 "case a ... b:\n" 2592 " break;\n" 2593 "}"); 2594 } 2595 2596 TEST_F(FormatTest, ShortEnums) { 2597 FormatStyle Style = getLLVMStyle(); 2598 Style.AllowShortEnumsOnASingleLine = true; 2599 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2600 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2601 Style.AllowShortEnumsOnASingleLine = false; 2602 verifyFormat("enum {\n" 2603 " A,\n" 2604 " B,\n" 2605 " C\n" 2606 "} ShortEnum1, ShortEnum2;", 2607 Style); 2608 verifyFormat("typedef enum {\n" 2609 " A,\n" 2610 " B,\n" 2611 " C\n" 2612 "} ShortEnum1, ShortEnum2;", 2613 Style); 2614 verifyFormat("enum {\n" 2615 " A,\n" 2616 "} ShortEnum1, ShortEnum2;", 2617 Style); 2618 verifyFormat("typedef enum {\n" 2619 " A,\n" 2620 "} ShortEnum1, ShortEnum2;", 2621 Style); 2622 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2623 Style.BraceWrapping.AfterEnum = true; 2624 verifyFormat("enum\n" 2625 "{\n" 2626 " A,\n" 2627 " B,\n" 2628 " C\n" 2629 "} ShortEnum1, ShortEnum2;", 2630 Style); 2631 verifyFormat("typedef enum\n" 2632 "{\n" 2633 " A,\n" 2634 " B,\n" 2635 " C\n" 2636 "} ShortEnum1, ShortEnum2;", 2637 Style); 2638 } 2639 2640 TEST_F(FormatTest, ShortCaseLabels) { 2641 FormatStyle Style = getLLVMStyle(); 2642 Style.AllowShortCaseLabelsOnASingleLine = true; 2643 verifyFormat("switch (a) {\n" 2644 "case 1: x = 1; break;\n" 2645 "case 2: return;\n" 2646 "case 3:\n" 2647 "case 4:\n" 2648 "case 5: return;\n" 2649 "case 6: // comment\n" 2650 " return;\n" 2651 "case 7:\n" 2652 " // comment\n" 2653 " return;\n" 2654 "case 8:\n" 2655 " x = 8; // comment\n" 2656 " break;\n" 2657 "default: y = 1; break;\n" 2658 "}", 2659 Style); 2660 verifyFormat("switch (a) {\n" 2661 "case 0: return; // comment\n" 2662 "case 1: break; // comment\n" 2663 "case 2: return;\n" 2664 "// comment\n" 2665 "case 3: return;\n" 2666 "// comment 1\n" 2667 "// comment 2\n" 2668 "// comment 3\n" 2669 "case 4: break; /* comment */\n" 2670 "case 5:\n" 2671 " // comment\n" 2672 " break;\n" 2673 "case 6: /* comment */ x = 1; break;\n" 2674 "case 7: x = /* comment */ 1; break;\n" 2675 "case 8:\n" 2676 " x = 1; /* comment */\n" 2677 " break;\n" 2678 "case 9:\n" 2679 " break; // comment line 1\n" 2680 " // comment line 2\n" 2681 "}", 2682 Style); 2683 EXPECT_EQ("switch (a) {\n" 2684 "case 1:\n" 2685 " x = 8;\n" 2686 " // fall through\n" 2687 "case 2: x = 8;\n" 2688 "// comment\n" 2689 "case 3:\n" 2690 " return; /* comment line 1\n" 2691 " * comment line 2 */\n" 2692 "case 4: i = 8;\n" 2693 "// something else\n" 2694 "#if FOO\n" 2695 "case 5: break;\n" 2696 "#endif\n" 2697 "}", 2698 format("switch (a) {\n" 2699 "case 1: x = 8;\n" 2700 " // fall through\n" 2701 "case 2:\n" 2702 " x = 8;\n" 2703 "// comment\n" 2704 "case 3:\n" 2705 " return; /* comment line 1\n" 2706 " * comment line 2 */\n" 2707 "case 4:\n" 2708 " i = 8;\n" 2709 "// something else\n" 2710 "#if FOO\n" 2711 "case 5: break;\n" 2712 "#endif\n" 2713 "}", 2714 Style)); 2715 EXPECT_EQ("switch (a) {\n" 2716 "case 0:\n" 2717 " return; // long long long long long long long long long long " 2718 "long long comment\n" 2719 " // line\n" 2720 "}", 2721 format("switch (a) {\n" 2722 "case 0: return; // long long long long long long long long " 2723 "long long long long comment line\n" 2724 "}", 2725 Style)); 2726 EXPECT_EQ("switch (a) {\n" 2727 "case 0:\n" 2728 " return; /* long long long long long long long long long long " 2729 "long long comment\n" 2730 " line */\n" 2731 "}", 2732 format("switch (a) {\n" 2733 "case 0: return; /* long long long long long long long long " 2734 "long long long long comment line */\n" 2735 "}", 2736 Style)); 2737 verifyFormat("switch (a) {\n" 2738 "#if FOO\n" 2739 "case 0: return 0;\n" 2740 "#endif\n" 2741 "}", 2742 Style); 2743 verifyFormat("switch (a) {\n" 2744 "case 1: {\n" 2745 "}\n" 2746 "case 2: {\n" 2747 " return;\n" 2748 "}\n" 2749 "case 3: {\n" 2750 " x = 1;\n" 2751 " return;\n" 2752 "}\n" 2753 "case 4:\n" 2754 " if (x)\n" 2755 " return;\n" 2756 "}", 2757 Style); 2758 Style.ColumnLimit = 21; 2759 verifyFormat("switch (a) {\n" 2760 "case 1: x = 1; break;\n" 2761 "case 2: return;\n" 2762 "case 3:\n" 2763 "case 4:\n" 2764 "case 5: return;\n" 2765 "default:\n" 2766 " y = 1;\n" 2767 " break;\n" 2768 "}", 2769 Style); 2770 Style.ColumnLimit = 80; 2771 Style.AllowShortCaseLabelsOnASingleLine = false; 2772 Style.IndentCaseLabels = true; 2773 EXPECT_EQ("switch (n) {\n" 2774 " default /*comments*/:\n" 2775 " return true;\n" 2776 " case 0:\n" 2777 " return false;\n" 2778 "}", 2779 format("switch (n) {\n" 2780 "default/*comments*/:\n" 2781 " return true;\n" 2782 "case 0:\n" 2783 " return false;\n" 2784 "}", 2785 Style)); 2786 Style.AllowShortCaseLabelsOnASingleLine = true; 2787 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2788 Style.BraceWrapping.AfterCaseLabel = true; 2789 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2790 EXPECT_EQ("switch (n)\n" 2791 "{\n" 2792 " case 0:\n" 2793 " {\n" 2794 " return false;\n" 2795 " }\n" 2796 " default:\n" 2797 " {\n" 2798 " return true;\n" 2799 " }\n" 2800 "}", 2801 format("switch (n) {\n" 2802 " case 0: {\n" 2803 " return false;\n" 2804 " }\n" 2805 " default:\n" 2806 " {\n" 2807 " return true;\n" 2808 " }\n" 2809 "}", 2810 Style)); 2811 } 2812 2813 TEST_F(FormatTest, FormatsLabels) { 2814 verifyFormat("void f() {\n" 2815 " some_code();\n" 2816 "test_label:\n" 2817 " some_other_code();\n" 2818 " {\n" 2819 " some_more_code();\n" 2820 " another_label:\n" 2821 " some_more_code();\n" 2822 " }\n" 2823 "}"); 2824 verifyFormat("{\n" 2825 " some_code();\n" 2826 "test_label:\n" 2827 " some_other_code();\n" 2828 "}"); 2829 verifyFormat("{\n" 2830 " some_code();\n" 2831 "test_label:;\n" 2832 " int i = 0;\n" 2833 "}"); 2834 FormatStyle Style = getLLVMStyle(); 2835 Style.IndentGotoLabels = false; 2836 verifyFormat("void f() {\n" 2837 " some_code();\n" 2838 "test_label:\n" 2839 " some_other_code();\n" 2840 " {\n" 2841 " some_more_code();\n" 2842 "another_label:\n" 2843 " some_more_code();\n" 2844 " }\n" 2845 "}", 2846 Style); 2847 verifyFormat("{\n" 2848 " some_code();\n" 2849 "test_label:\n" 2850 " some_other_code();\n" 2851 "}", 2852 Style); 2853 verifyFormat("{\n" 2854 " some_code();\n" 2855 "test_label:;\n" 2856 " int i = 0;\n" 2857 "}"); 2858 } 2859 2860 TEST_F(FormatTest, MultiLineControlStatements) { 2861 FormatStyle Style = getLLVMStyleWithColumns(20); 2862 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2863 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2864 // Short lines should keep opening brace on same line. 2865 EXPECT_EQ("if (foo) {\n" 2866 " bar();\n" 2867 "}", 2868 format("if(foo){bar();}", Style)); 2869 EXPECT_EQ("if (foo) {\n" 2870 " bar();\n" 2871 "} else {\n" 2872 " baz();\n" 2873 "}", 2874 format("if(foo){bar();}else{baz();}", Style)); 2875 EXPECT_EQ("if (foo && bar) {\n" 2876 " baz();\n" 2877 "}", 2878 format("if(foo&&bar){baz();}", Style)); 2879 EXPECT_EQ("if (foo) {\n" 2880 " bar();\n" 2881 "} else if (baz) {\n" 2882 " quux();\n" 2883 "}", 2884 format("if(foo){bar();}else if(baz){quux();}", Style)); 2885 EXPECT_EQ( 2886 "if (foo) {\n" 2887 " bar();\n" 2888 "} else if (baz) {\n" 2889 " quux();\n" 2890 "} else {\n" 2891 " foobar();\n" 2892 "}", 2893 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2894 EXPECT_EQ("for (;;) {\n" 2895 " foo();\n" 2896 "}", 2897 format("for(;;){foo();}")); 2898 EXPECT_EQ("while (1) {\n" 2899 " foo();\n" 2900 "}", 2901 format("while(1){foo();}", Style)); 2902 EXPECT_EQ("switch (foo) {\n" 2903 "case bar:\n" 2904 " return;\n" 2905 "}", 2906 format("switch(foo){case bar:return;}", Style)); 2907 EXPECT_EQ("try {\n" 2908 " foo();\n" 2909 "} catch (...) {\n" 2910 " bar();\n" 2911 "}", 2912 format("try{foo();}catch(...){bar();}", Style)); 2913 EXPECT_EQ("do {\n" 2914 " foo();\n" 2915 "} while (bar &&\n" 2916 " baz);", 2917 format("do{foo();}while(bar&&baz);", Style)); 2918 // Long lines should put opening brace on new line. 2919 EXPECT_EQ("if (foo && bar &&\n" 2920 " baz)\n" 2921 "{\n" 2922 " quux();\n" 2923 "}", 2924 format("if(foo&&bar&&baz){quux();}", Style)); 2925 EXPECT_EQ("if (foo && bar &&\n" 2926 " baz)\n" 2927 "{\n" 2928 " quux();\n" 2929 "}", 2930 format("if (foo && bar &&\n" 2931 " baz) {\n" 2932 " quux();\n" 2933 "}", 2934 Style)); 2935 EXPECT_EQ("if (foo) {\n" 2936 " bar();\n" 2937 "} else if (baz ||\n" 2938 " quux)\n" 2939 "{\n" 2940 " foobar();\n" 2941 "}", 2942 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2943 EXPECT_EQ( 2944 "if (foo) {\n" 2945 " bar();\n" 2946 "} else if (baz ||\n" 2947 " quux)\n" 2948 "{\n" 2949 " foobar();\n" 2950 "} else {\n" 2951 " barbaz();\n" 2952 "}", 2953 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2954 Style)); 2955 EXPECT_EQ("for (int i = 0;\n" 2956 " i < 10; ++i)\n" 2957 "{\n" 2958 " foo();\n" 2959 "}", 2960 format("for(int i=0;i<10;++i){foo();}", Style)); 2961 EXPECT_EQ("foreach (int i,\n" 2962 " list)\n" 2963 "{\n" 2964 " foo();\n" 2965 "}", 2966 format("foreach(int i, list){foo();}", Style)); 2967 Style.ColumnLimit = 2968 40; // to concentrate at brace wrapping, not line wrap due to column limit 2969 EXPECT_EQ("foreach (int i, list) {\n" 2970 " foo();\n" 2971 "}", 2972 format("foreach(int i, list){foo();}", Style)); 2973 Style.ColumnLimit = 2974 20; // to concentrate at brace wrapping, not line wrap due to column limit 2975 EXPECT_EQ("while (foo || bar ||\n" 2976 " baz)\n" 2977 "{\n" 2978 " quux();\n" 2979 "}", 2980 format("while(foo||bar||baz){quux();}", Style)); 2981 EXPECT_EQ("switch (\n" 2982 " foo = barbaz)\n" 2983 "{\n" 2984 "case quux:\n" 2985 " return;\n" 2986 "}", 2987 format("switch(foo=barbaz){case quux:return;}", Style)); 2988 EXPECT_EQ("try {\n" 2989 " foo();\n" 2990 "} catch (\n" 2991 " Exception &bar)\n" 2992 "{\n" 2993 " baz();\n" 2994 "}", 2995 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2996 Style.ColumnLimit = 2997 40; // to concentrate at brace wrapping, not line wrap due to column limit 2998 EXPECT_EQ("try {\n" 2999 " foo();\n" 3000 "} catch (Exception &bar) {\n" 3001 " baz();\n" 3002 "}", 3003 format("try{foo();}catch(Exception&bar){baz();}", Style)); 3004 Style.ColumnLimit = 3005 20; // to concentrate at brace wrapping, not line wrap due to column limit 3006 3007 Style.BraceWrapping.BeforeElse = true; 3008 EXPECT_EQ( 3009 "if (foo) {\n" 3010 " bar();\n" 3011 "}\n" 3012 "else if (baz ||\n" 3013 " quux)\n" 3014 "{\n" 3015 " foobar();\n" 3016 "}\n" 3017 "else {\n" 3018 " barbaz();\n" 3019 "}", 3020 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3021 Style)); 3022 3023 Style.BraceWrapping.BeforeCatch = true; 3024 EXPECT_EQ("try {\n" 3025 " foo();\n" 3026 "}\n" 3027 "catch (...) {\n" 3028 " baz();\n" 3029 "}", 3030 format("try{foo();}catch(...){baz();}", Style)); 3031 3032 Style.BraceWrapping.AfterFunction = true; 3033 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3034 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3035 Style.ColumnLimit = 80; 3036 verifyFormat("void shortfunction() { bar(); }", Style); 3037 3038 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3039 verifyFormat("void shortfunction()\n" 3040 "{\n" 3041 " bar();\n" 3042 "}", 3043 Style); 3044 } 3045 3046 TEST_F(FormatTest, BeforeWhile) { 3047 FormatStyle Style = getLLVMStyle(); 3048 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3049 3050 verifyFormat("do {\n" 3051 " foo();\n" 3052 "} while (1);", 3053 Style); 3054 Style.BraceWrapping.BeforeWhile = true; 3055 verifyFormat("do {\n" 3056 " foo();\n" 3057 "}\n" 3058 "while (1);", 3059 Style); 3060 } 3061 3062 //===----------------------------------------------------------------------===// 3063 // Tests for classes, namespaces, etc. 3064 //===----------------------------------------------------------------------===// 3065 3066 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 3067 verifyFormat("class A {};"); 3068 } 3069 3070 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 3071 verifyFormat("class A {\n" 3072 "public:\n" 3073 "public: // comment\n" 3074 "protected:\n" 3075 "private:\n" 3076 " void f() {}\n" 3077 "};"); 3078 verifyFormat("export class A {\n" 3079 "public:\n" 3080 "public: // comment\n" 3081 "protected:\n" 3082 "private:\n" 3083 " void f() {}\n" 3084 "};"); 3085 verifyGoogleFormat("class A {\n" 3086 " public:\n" 3087 " protected:\n" 3088 " private:\n" 3089 " void f() {}\n" 3090 "};"); 3091 verifyGoogleFormat("export class A {\n" 3092 " public:\n" 3093 " protected:\n" 3094 " private:\n" 3095 " void f() {}\n" 3096 "};"); 3097 verifyFormat("class A {\n" 3098 "public slots:\n" 3099 " void f1() {}\n" 3100 "public Q_SLOTS:\n" 3101 " void f2() {}\n" 3102 "protected slots:\n" 3103 " void f3() {}\n" 3104 "protected Q_SLOTS:\n" 3105 " void f4() {}\n" 3106 "private slots:\n" 3107 " void f5() {}\n" 3108 "private Q_SLOTS:\n" 3109 " void f6() {}\n" 3110 "signals:\n" 3111 " void g1();\n" 3112 "Q_SIGNALS:\n" 3113 " void g2();\n" 3114 "};"); 3115 3116 // Don't interpret 'signals' the wrong way. 3117 verifyFormat("signals.set();"); 3118 verifyFormat("for (Signals signals : f()) {\n}"); 3119 verifyFormat("{\n" 3120 " signals.set(); // This needs indentation.\n" 3121 "}"); 3122 verifyFormat("void f() {\n" 3123 "label:\n" 3124 " signals.baz();\n" 3125 "}"); 3126 } 3127 3128 TEST_F(FormatTest, SeparatesLogicalBlocks) { 3129 EXPECT_EQ("class A {\n" 3130 "public:\n" 3131 " void f();\n" 3132 "\n" 3133 "private:\n" 3134 " void g() {}\n" 3135 " // test\n" 3136 "protected:\n" 3137 " int h;\n" 3138 "};", 3139 format("class A {\n" 3140 "public:\n" 3141 "void f();\n" 3142 "private:\n" 3143 "void g() {}\n" 3144 "// test\n" 3145 "protected:\n" 3146 "int h;\n" 3147 "};")); 3148 EXPECT_EQ("class A {\n" 3149 "protected:\n" 3150 "public:\n" 3151 " void f();\n" 3152 "};", 3153 format("class A {\n" 3154 "protected:\n" 3155 "\n" 3156 "public:\n" 3157 "\n" 3158 " void f();\n" 3159 "};")); 3160 3161 // Even ensure proper spacing inside macros. 3162 EXPECT_EQ("#define B \\\n" 3163 " class A { \\\n" 3164 " protected: \\\n" 3165 " public: \\\n" 3166 " void f(); \\\n" 3167 " };", 3168 format("#define B \\\n" 3169 " class A { \\\n" 3170 " protected: \\\n" 3171 " \\\n" 3172 " public: \\\n" 3173 " \\\n" 3174 " void f(); \\\n" 3175 " };", 3176 getGoogleStyle())); 3177 // But don't remove empty lines after macros ending in access specifiers. 3178 EXPECT_EQ("#define A private:\n" 3179 "\n" 3180 "int i;", 3181 format("#define A private:\n" 3182 "\n" 3183 "int i;")); 3184 } 3185 3186 TEST_F(FormatTest, FormatsClasses) { 3187 verifyFormat("class A : public B {};"); 3188 verifyFormat("class A : public ::B {};"); 3189 3190 verifyFormat( 3191 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3192 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3193 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3194 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3195 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3196 verifyFormat( 3197 "class A : public B, public C, public D, public E, public F {};"); 3198 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3199 " public C,\n" 3200 " public D,\n" 3201 " public E,\n" 3202 " public F,\n" 3203 " public G {};"); 3204 3205 verifyFormat("class\n" 3206 " ReallyReallyLongClassName {\n" 3207 " int i;\n" 3208 "};", 3209 getLLVMStyleWithColumns(32)); 3210 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3211 " aaaaaaaaaaaaaaaa> {};"); 3212 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3213 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3214 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3215 verifyFormat("template <class R, class C>\n" 3216 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3217 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3218 verifyFormat("class ::A::B {};"); 3219 } 3220 3221 TEST_F(FormatTest, BreakInheritanceStyle) { 3222 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3223 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3224 FormatStyle::BILS_BeforeComma; 3225 verifyFormat("class MyClass : public X {};", 3226 StyleWithInheritanceBreakBeforeComma); 3227 verifyFormat("class MyClass\n" 3228 " : public X\n" 3229 " , public Y {};", 3230 StyleWithInheritanceBreakBeforeComma); 3231 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3232 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3233 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3234 StyleWithInheritanceBreakBeforeComma); 3235 verifyFormat("struct aaaaaaaaaaaaa\n" 3236 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3237 " aaaaaaaaaaaaaaaa> {};", 3238 StyleWithInheritanceBreakBeforeComma); 3239 3240 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3241 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3242 FormatStyle::BILS_AfterColon; 3243 verifyFormat("class MyClass : public X {};", 3244 StyleWithInheritanceBreakAfterColon); 3245 verifyFormat("class MyClass : public X, public Y {};", 3246 StyleWithInheritanceBreakAfterColon); 3247 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3248 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3249 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3250 StyleWithInheritanceBreakAfterColon); 3251 verifyFormat("struct aaaaaaaaaaaaa :\n" 3252 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3253 " aaaaaaaaaaaaaaaa> {};", 3254 StyleWithInheritanceBreakAfterColon); 3255 3256 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3257 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3258 FormatStyle::BILS_AfterComma; 3259 verifyFormat("class MyClass : public X {};", 3260 StyleWithInheritanceBreakAfterComma); 3261 verifyFormat("class MyClass : public X,\n" 3262 " public Y {};", 3263 StyleWithInheritanceBreakAfterComma); 3264 verifyFormat( 3265 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3266 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3267 "{};", 3268 StyleWithInheritanceBreakAfterComma); 3269 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3270 " aaaaaaaaaaaaaaaa> {};", 3271 StyleWithInheritanceBreakAfterComma); 3272 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3273 " : public OnceBreak,\n" 3274 " public AlwaysBreak,\n" 3275 " EvenBasesFitInOneLine {};", 3276 StyleWithInheritanceBreakAfterComma); 3277 } 3278 3279 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 3280 verifyFormat("class A {\n} a, b;"); 3281 verifyFormat("struct A {\n} a, b;"); 3282 verifyFormat("union A {\n} a;"); 3283 } 3284 3285 TEST_F(FormatTest, FormatsEnum) { 3286 verifyFormat("enum {\n" 3287 " Zero,\n" 3288 " One = 1,\n" 3289 " Two = One + 1,\n" 3290 " Three = (One + Two),\n" 3291 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3292 " Five = (One, Two, Three, Four, 5)\n" 3293 "};"); 3294 verifyGoogleFormat("enum {\n" 3295 " Zero,\n" 3296 " One = 1,\n" 3297 " Two = One + 1,\n" 3298 " Three = (One + Two),\n" 3299 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3300 " Five = (One, Two, Three, Four, 5)\n" 3301 "};"); 3302 verifyFormat("enum Enum {};"); 3303 verifyFormat("enum {};"); 3304 verifyFormat("enum X E {} d;"); 3305 verifyFormat("enum __attribute__((...)) E {} d;"); 3306 verifyFormat("enum __declspec__((...)) E {} d;"); 3307 verifyFormat("enum {\n" 3308 " Bar = Foo<int, int>::value\n" 3309 "};", 3310 getLLVMStyleWithColumns(30)); 3311 3312 verifyFormat("enum ShortEnum { A, B, C };"); 3313 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3314 3315 EXPECT_EQ("enum KeepEmptyLines {\n" 3316 " ONE,\n" 3317 "\n" 3318 " TWO,\n" 3319 "\n" 3320 " THREE\n" 3321 "}", 3322 format("enum KeepEmptyLines {\n" 3323 " ONE,\n" 3324 "\n" 3325 " TWO,\n" 3326 "\n" 3327 "\n" 3328 " THREE\n" 3329 "}")); 3330 verifyFormat("enum E { // comment\n" 3331 " ONE,\n" 3332 " TWO\n" 3333 "};\n" 3334 "int i;"); 3335 3336 FormatStyle EightIndent = getLLVMStyle(); 3337 EightIndent.IndentWidth = 8; 3338 verifyFormat("enum {\n" 3339 " VOID,\n" 3340 " CHAR,\n" 3341 " SHORT,\n" 3342 " INT,\n" 3343 " LONG,\n" 3344 " SIGNED,\n" 3345 " UNSIGNED,\n" 3346 " BOOL,\n" 3347 " FLOAT,\n" 3348 " DOUBLE,\n" 3349 " COMPLEX\n" 3350 "};", 3351 EightIndent); 3352 3353 // Not enums. 3354 verifyFormat("enum X f() {\n" 3355 " a();\n" 3356 " return 42;\n" 3357 "}"); 3358 verifyFormat("enum X Type::f() {\n" 3359 " a();\n" 3360 " return 42;\n" 3361 "}"); 3362 verifyFormat("enum ::X f() {\n" 3363 " a();\n" 3364 " return 42;\n" 3365 "}"); 3366 verifyFormat("enum ns::X f() {\n" 3367 " a();\n" 3368 " return 42;\n" 3369 "}"); 3370 } 3371 3372 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3373 verifyFormat("enum Type {\n" 3374 " One = 0; // These semicolons should be commas.\n" 3375 " Two = 1;\n" 3376 "};"); 3377 verifyFormat("namespace n {\n" 3378 "enum Type {\n" 3379 " One,\n" 3380 " Two, // missing };\n" 3381 " int i;\n" 3382 "}\n" 3383 "void g() {}"); 3384 } 3385 3386 TEST_F(FormatTest, FormatsEnumStruct) { 3387 verifyFormat("enum struct {\n" 3388 " Zero,\n" 3389 " One = 1,\n" 3390 " Two = One + 1,\n" 3391 " Three = (One + Two),\n" 3392 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3393 " Five = (One, Two, Three, Four, 5)\n" 3394 "};"); 3395 verifyFormat("enum struct Enum {};"); 3396 verifyFormat("enum struct {};"); 3397 verifyFormat("enum struct X E {} d;"); 3398 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3399 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3400 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3401 } 3402 3403 TEST_F(FormatTest, FormatsEnumClass) { 3404 verifyFormat("enum class {\n" 3405 " Zero,\n" 3406 " One = 1,\n" 3407 " Two = One + 1,\n" 3408 " Three = (One + Two),\n" 3409 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3410 " Five = (One, Two, Three, Four, 5)\n" 3411 "};"); 3412 verifyFormat("enum class Enum {};"); 3413 verifyFormat("enum class {};"); 3414 verifyFormat("enum class X E {} d;"); 3415 verifyFormat("enum class __attribute__((...)) E {} d;"); 3416 verifyFormat("enum class __declspec__((...)) E {} d;"); 3417 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3418 } 3419 3420 TEST_F(FormatTest, FormatsEnumTypes) { 3421 verifyFormat("enum X : int {\n" 3422 " A, // Force multiple lines.\n" 3423 " B\n" 3424 "};"); 3425 verifyFormat("enum X : int { A, B };"); 3426 verifyFormat("enum X : std::uint32_t { A, B };"); 3427 } 3428 3429 TEST_F(FormatTest, FormatsTypedefEnum) { 3430 FormatStyle Style = getLLVMStyleWithColumns(40); 3431 verifyFormat("typedef enum {} EmptyEnum;"); 3432 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3433 verifyFormat("typedef enum {\n" 3434 " ZERO = 0,\n" 3435 " ONE = 1,\n" 3436 " TWO = 2,\n" 3437 " THREE = 3\n" 3438 "} LongEnum;", 3439 Style); 3440 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3441 Style.BraceWrapping.AfterEnum = true; 3442 verifyFormat("typedef enum {} EmptyEnum;"); 3443 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3444 verifyFormat("typedef enum\n" 3445 "{\n" 3446 " ZERO = 0,\n" 3447 " ONE = 1,\n" 3448 " TWO = 2,\n" 3449 " THREE = 3\n" 3450 "} LongEnum;", 3451 Style); 3452 } 3453 3454 TEST_F(FormatTest, FormatsNSEnums) { 3455 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3456 verifyGoogleFormat( 3457 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3458 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3459 " // Information about someDecentlyLongValue.\n" 3460 " someDecentlyLongValue,\n" 3461 " // Information about anotherDecentlyLongValue.\n" 3462 " anotherDecentlyLongValue,\n" 3463 " // Information about aThirdDecentlyLongValue.\n" 3464 " aThirdDecentlyLongValue\n" 3465 "};"); 3466 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3467 " // Information about someDecentlyLongValue.\n" 3468 " someDecentlyLongValue,\n" 3469 " // Information about anotherDecentlyLongValue.\n" 3470 " anotherDecentlyLongValue,\n" 3471 " // Information about aThirdDecentlyLongValue.\n" 3472 " aThirdDecentlyLongValue\n" 3473 "};"); 3474 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3475 " a = 1,\n" 3476 " b = 2,\n" 3477 " c = 3,\n" 3478 "};"); 3479 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3480 " a = 1,\n" 3481 " b = 2,\n" 3482 " c = 3,\n" 3483 "};"); 3484 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3485 " a = 1,\n" 3486 " b = 2,\n" 3487 " c = 3,\n" 3488 "};"); 3489 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3490 " a = 1,\n" 3491 " b = 2,\n" 3492 " c = 3,\n" 3493 "};"); 3494 } 3495 3496 TEST_F(FormatTest, FormatsBitfields) { 3497 verifyFormat("struct Bitfields {\n" 3498 " unsigned sClass : 8;\n" 3499 " unsigned ValueKind : 2;\n" 3500 "};"); 3501 verifyFormat("struct A {\n" 3502 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3503 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3504 "};"); 3505 verifyFormat("struct MyStruct {\n" 3506 " uchar data;\n" 3507 " uchar : 8;\n" 3508 " uchar : 8;\n" 3509 " uchar other;\n" 3510 "};"); 3511 FormatStyle Style = getLLVMStyle(); 3512 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3513 verifyFormat("struct Bitfields {\n" 3514 " unsigned sClass:8;\n" 3515 " unsigned ValueKind:2;\n" 3516 " uchar other;\n" 3517 "};", 3518 Style); 3519 verifyFormat("struct A {\n" 3520 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3521 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3522 "};", 3523 Style); 3524 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3525 verifyFormat("struct Bitfields {\n" 3526 " unsigned sClass :8;\n" 3527 " unsigned ValueKind :2;\n" 3528 " uchar other;\n" 3529 "};", 3530 Style); 3531 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3532 verifyFormat("struct Bitfields {\n" 3533 " unsigned sClass: 8;\n" 3534 " unsigned ValueKind: 2;\n" 3535 " uchar other;\n" 3536 "};", 3537 Style); 3538 } 3539 3540 TEST_F(FormatTest, FormatsNamespaces) { 3541 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3542 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3543 3544 verifyFormat("namespace some_namespace {\n" 3545 "class A {};\n" 3546 "void f() { f(); }\n" 3547 "}", 3548 LLVMWithNoNamespaceFix); 3549 verifyFormat("namespace N::inline D {\n" 3550 "class A {};\n" 3551 "void f() { f(); }\n" 3552 "}", 3553 LLVMWithNoNamespaceFix); 3554 verifyFormat("namespace N::inline D::E {\n" 3555 "class A {};\n" 3556 "void f() { f(); }\n" 3557 "}", 3558 LLVMWithNoNamespaceFix); 3559 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3560 "class A {};\n" 3561 "void f() { f(); }\n" 3562 "}", 3563 LLVMWithNoNamespaceFix); 3564 verifyFormat("/* something */ namespace some_namespace {\n" 3565 "class A {};\n" 3566 "void f() { f(); }\n" 3567 "}", 3568 LLVMWithNoNamespaceFix); 3569 verifyFormat("namespace {\n" 3570 "class A {};\n" 3571 "void f() { f(); }\n" 3572 "}", 3573 LLVMWithNoNamespaceFix); 3574 verifyFormat("/* something */ namespace {\n" 3575 "class A {};\n" 3576 "void f() { f(); }\n" 3577 "}", 3578 LLVMWithNoNamespaceFix); 3579 verifyFormat("inline namespace X {\n" 3580 "class A {};\n" 3581 "void f() { f(); }\n" 3582 "}", 3583 LLVMWithNoNamespaceFix); 3584 verifyFormat("/* something */ inline namespace X {\n" 3585 "class A {};\n" 3586 "void f() { f(); }\n" 3587 "}", 3588 LLVMWithNoNamespaceFix); 3589 verifyFormat("export namespace X {\n" 3590 "class A {};\n" 3591 "void f() { f(); }\n" 3592 "}", 3593 LLVMWithNoNamespaceFix); 3594 verifyFormat("using namespace some_namespace;\n" 3595 "class A {};\n" 3596 "void f() { f(); }", 3597 LLVMWithNoNamespaceFix); 3598 3599 // This code is more common than we thought; if we 3600 // layout this correctly the semicolon will go into 3601 // its own line, which is undesirable. 3602 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3603 verifyFormat("namespace {\n" 3604 "class A {};\n" 3605 "};", 3606 LLVMWithNoNamespaceFix); 3607 3608 verifyFormat("namespace {\n" 3609 "int SomeVariable = 0; // comment\n" 3610 "} // namespace", 3611 LLVMWithNoNamespaceFix); 3612 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3613 "#define HEADER_GUARD\n" 3614 "namespace my_namespace {\n" 3615 "int i;\n" 3616 "} // my_namespace\n" 3617 "#endif // HEADER_GUARD", 3618 format("#ifndef HEADER_GUARD\n" 3619 " #define HEADER_GUARD\n" 3620 " namespace my_namespace {\n" 3621 "int i;\n" 3622 "} // my_namespace\n" 3623 "#endif // HEADER_GUARD", 3624 LLVMWithNoNamespaceFix)); 3625 3626 EXPECT_EQ("namespace A::B {\n" 3627 "class C {};\n" 3628 "}", 3629 format("namespace A::B {\n" 3630 "class C {};\n" 3631 "}", 3632 LLVMWithNoNamespaceFix)); 3633 3634 FormatStyle Style = getLLVMStyle(); 3635 Style.NamespaceIndentation = FormatStyle::NI_All; 3636 EXPECT_EQ("namespace out {\n" 3637 " int i;\n" 3638 " namespace in {\n" 3639 " int i;\n" 3640 " } // namespace in\n" 3641 "} // namespace out", 3642 format("namespace out {\n" 3643 "int i;\n" 3644 "namespace in {\n" 3645 "int i;\n" 3646 "} // namespace in\n" 3647 "} // namespace out", 3648 Style)); 3649 3650 FormatStyle ShortInlineFunctions = getLLVMStyle(); 3651 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All; 3652 ShortInlineFunctions.AllowShortFunctionsOnASingleLine = 3653 FormatStyle::SFS_Inline; 3654 verifyFormat("namespace {\n" 3655 " void f() {\n" 3656 " return;\n" 3657 " }\n" 3658 "} // namespace\n", 3659 ShortInlineFunctions); 3660 verifyFormat("namespace {\n" 3661 " int some_int;\n" 3662 " void f() {\n" 3663 " return;\n" 3664 " }\n" 3665 "} // namespace\n", 3666 ShortInlineFunctions); 3667 verifyFormat("namespace interface {\n" 3668 " void f() {\n" 3669 " return;\n" 3670 " }\n" 3671 "} // namespace interface\n", 3672 ShortInlineFunctions); 3673 verifyFormat("namespace {\n" 3674 " class X {\n" 3675 " void f() { return; }\n" 3676 " };\n" 3677 "} // namespace\n", 3678 ShortInlineFunctions); 3679 verifyFormat("namespace {\n" 3680 " struct X {\n" 3681 " void f() { return; }\n" 3682 " };\n" 3683 "} // namespace\n", 3684 ShortInlineFunctions); 3685 verifyFormat("namespace {\n" 3686 " union X {\n" 3687 " void f() { return; }\n" 3688 " };\n" 3689 "} // namespace\n", 3690 ShortInlineFunctions); 3691 verifyFormat("extern \"C\" {\n" 3692 "void f() {\n" 3693 " return;\n" 3694 "}\n" 3695 "} // namespace\n", 3696 ShortInlineFunctions); 3697 verifyFormat("namespace {\n" 3698 " class X {\n" 3699 " void f() { return; }\n" 3700 " } x;\n" 3701 "} // namespace\n", 3702 ShortInlineFunctions); 3703 verifyFormat("namespace {\n" 3704 " [[nodiscard]] class X {\n" 3705 " void f() { return; }\n" 3706 " };\n" 3707 "} // namespace\n", 3708 ShortInlineFunctions); 3709 verifyFormat("namespace {\n" 3710 " static class X {\n" 3711 " void f() { return; }\n" 3712 " } x;\n" 3713 "} // namespace\n", 3714 ShortInlineFunctions); 3715 verifyFormat("namespace {\n" 3716 " constexpr class X {\n" 3717 " void f() { return; }\n" 3718 " } x;\n" 3719 "} // namespace\n", 3720 ShortInlineFunctions); 3721 3722 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent; 3723 verifyFormat("extern \"C\" {\n" 3724 " void f() {\n" 3725 " return;\n" 3726 " }\n" 3727 "} // namespace\n", 3728 ShortInlineFunctions); 3729 3730 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3731 EXPECT_EQ("namespace out {\n" 3732 "int i;\n" 3733 "namespace in {\n" 3734 " int i;\n" 3735 "} // namespace in\n" 3736 "} // namespace out", 3737 format("namespace out {\n" 3738 "int i;\n" 3739 "namespace in {\n" 3740 "int i;\n" 3741 "} // namespace in\n" 3742 "} // namespace out", 3743 Style)); 3744 3745 Style.NamespaceIndentation = FormatStyle::NI_None; 3746 verifyFormat("template <class T>\n" 3747 "concept a_concept = X<>;\n" 3748 "namespace B {\n" 3749 "struct b_struct {};\n" 3750 "} // namespace B\n", 3751 Style); 3752 verifyFormat("template <int I> constexpr void foo requires(I == 42) {}\n" 3753 "namespace ns {\n" 3754 "void foo() {}\n" 3755 "} // namespace ns\n", 3756 Style); 3757 } 3758 3759 TEST_F(FormatTest, NamespaceMacros) { 3760 FormatStyle Style = getLLVMStyle(); 3761 Style.NamespaceMacros.push_back("TESTSUITE"); 3762 3763 verifyFormat("TESTSUITE(A) {\n" 3764 "int foo();\n" 3765 "} // TESTSUITE(A)", 3766 Style); 3767 3768 verifyFormat("TESTSUITE(A, B) {\n" 3769 "int foo();\n" 3770 "} // TESTSUITE(A)", 3771 Style); 3772 3773 // Properly indent according to NamespaceIndentation style 3774 Style.NamespaceIndentation = FormatStyle::NI_All; 3775 verifyFormat("TESTSUITE(A) {\n" 3776 " int foo();\n" 3777 "} // TESTSUITE(A)", 3778 Style); 3779 verifyFormat("TESTSUITE(A) {\n" 3780 " namespace B {\n" 3781 " int foo();\n" 3782 " } // namespace B\n" 3783 "} // TESTSUITE(A)", 3784 Style); 3785 verifyFormat("namespace A {\n" 3786 " TESTSUITE(B) {\n" 3787 " int foo();\n" 3788 " } // TESTSUITE(B)\n" 3789 "} // namespace A", 3790 Style); 3791 3792 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3793 verifyFormat("TESTSUITE(A) {\n" 3794 "TESTSUITE(B) {\n" 3795 " int foo();\n" 3796 "} // TESTSUITE(B)\n" 3797 "} // TESTSUITE(A)", 3798 Style); 3799 verifyFormat("TESTSUITE(A) {\n" 3800 "namespace B {\n" 3801 " int foo();\n" 3802 "} // namespace B\n" 3803 "} // TESTSUITE(A)", 3804 Style); 3805 verifyFormat("namespace A {\n" 3806 "TESTSUITE(B) {\n" 3807 " int foo();\n" 3808 "} // TESTSUITE(B)\n" 3809 "} // namespace A", 3810 Style); 3811 3812 // Properly merge namespace-macros blocks in CompactNamespaces mode 3813 Style.NamespaceIndentation = FormatStyle::NI_None; 3814 Style.CompactNamespaces = true; 3815 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3816 "}} // TESTSUITE(A::B)", 3817 Style); 3818 3819 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3820 "}} // TESTSUITE(out::in)", 3821 format("TESTSUITE(out) {\n" 3822 "TESTSUITE(in) {\n" 3823 "} // TESTSUITE(in)\n" 3824 "} // TESTSUITE(out)", 3825 Style)); 3826 3827 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3828 "}} // TESTSUITE(out::in)", 3829 format("TESTSUITE(out) {\n" 3830 "TESTSUITE(in) {\n" 3831 "} // TESTSUITE(in)\n" 3832 "} // TESTSUITE(out)", 3833 Style)); 3834 3835 // Do not merge different namespaces/macros 3836 EXPECT_EQ("namespace out {\n" 3837 "TESTSUITE(in) {\n" 3838 "} // TESTSUITE(in)\n" 3839 "} // namespace out", 3840 format("namespace out {\n" 3841 "TESTSUITE(in) {\n" 3842 "} // TESTSUITE(in)\n" 3843 "} // namespace out", 3844 Style)); 3845 EXPECT_EQ("TESTSUITE(out) {\n" 3846 "namespace in {\n" 3847 "} // namespace in\n" 3848 "} // TESTSUITE(out)", 3849 format("TESTSUITE(out) {\n" 3850 "namespace in {\n" 3851 "} // namespace in\n" 3852 "} // TESTSUITE(out)", 3853 Style)); 3854 Style.NamespaceMacros.push_back("FOOBAR"); 3855 EXPECT_EQ("TESTSUITE(out) {\n" 3856 "FOOBAR(in) {\n" 3857 "} // FOOBAR(in)\n" 3858 "} // TESTSUITE(out)", 3859 format("TESTSUITE(out) {\n" 3860 "FOOBAR(in) {\n" 3861 "} // FOOBAR(in)\n" 3862 "} // TESTSUITE(out)", 3863 Style)); 3864 } 3865 3866 TEST_F(FormatTest, FormatsCompactNamespaces) { 3867 FormatStyle Style = getLLVMStyle(); 3868 Style.CompactNamespaces = true; 3869 Style.NamespaceMacros.push_back("TESTSUITE"); 3870 3871 verifyFormat("namespace A { namespace B {\n" 3872 "}} // namespace A::B", 3873 Style); 3874 3875 EXPECT_EQ("namespace out { namespace in {\n" 3876 "}} // namespace out::in", 3877 format("namespace out {\n" 3878 "namespace in {\n" 3879 "} // namespace in\n" 3880 "} // namespace out", 3881 Style)); 3882 3883 // Only namespaces which have both consecutive opening and end get compacted 3884 EXPECT_EQ("namespace out {\n" 3885 "namespace in1 {\n" 3886 "} // namespace in1\n" 3887 "namespace in2 {\n" 3888 "} // namespace in2\n" 3889 "} // namespace out", 3890 format("namespace out {\n" 3891 "namespace in1 {\n" 3892 "} // namespace in1\n" 3893 "namespace in2 {\n" 3894 "} // namespace in2\n" 3895 "} // namespace out", 3896 Style)); 3897 3898 EXPECT_EQ("namespace out {\n" 3899 "int i;\n" 3900 "namespace in {\n" 3901 "int j;\n" 3902 "} // namespace in\n" 3903 "int k;\n" 3904 "} // namespace out", 3905 format("namespace out { int i;\n" 3906 "namespace in { int j; } // namespace in\n" 3907 "int k; } // namespace out", 3908 Style)); 3909 3910 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 3911 "}}} // namespace A::B::C\n", 3912 format("namespace A { namespace B {\n" 3913 "namespace C {\n" 3914 "}} // namespace B::C\n" 3915 "} // namespace A\n", 3916 Style)); 3917 3918 Style.ColumnLimit = 40; 3919 EXPECT_EQ("namespace aaaaaaaaaa {\n" 3920 "namespace bbbbbbbbbb {\n" 3921 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 3922 format("namespace aaaaaaaaaa {\n" 3923 "namespace bbbbbbbbbb {\n" 3924 "} // namespace bbbbbbbbbb\n" 3925 "} // namespace aaaaaaaaaa", 3926 Style)); 3927 3928 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 3929 "namespace cccccc {\n" 3930 "}}} // namespace aaaaaa::bbbbbb::cccccc", 3931 format("namespace aaaaaa {\n" 3932 "namespace bbbbbb {\n" 3933 "namespace cccccc {\n" 3934 "} // namespace cccccc\n" 3935 "} // namespace bbbbbb\n" 3936 "} // namespace aaaaaa", 3937 Style)); 3938 Style.ColumnLimit = 80; 3939 3940 // Extra semicolon after 'inner' closing brace prevents merging 3941 EXPECT_EQ("namespace out { namespace in {\n" 3942 "}; } // namespace out::in", 3943 format("namespace out {\n" 3944 "namespace in {\n" 3945 "}; // namespace in\n" 3946 "} // namespace out", 3947 Style)); 3948 3949 // Extra semicolon after 'outer' closing brace is conserved 3950 EXPECT_EQ("namespace out { namespace in {\n" 3951 "}}; // namespace out::in", 3952 format("namespace out {\n" 3953 "namespace in {\n" 3954 "} // namespace in\n" 3955 "}; // namespace out", 3956 Style)); 3957 3958 Style.NamespaceIndentation = FormatStyle::NI_All; 3959 EXPECT_EQ("namespace out { namespace in {\n" 3960 " int i;\n" 3961 "}} // namespace out::in", 3962 format("namespace out {\n" 3963 "namespace in {\n" 3964 "int i;\n" 3965 "} // namespace in\n" 3966 "} // namespace out", 3967 Style)); 3968 EXPECT_EQ("namespace out { namespace mid {\n" 3969 " namespace in {\n" 3970 " int j;\n" 3971 " } // namespace in\n" 3972 " int k;\n" 3973 "}} // namespace out::mid", 3974 format("namespace out { namespace mid {\n" 3975 "namespace in { int j; } // namespace in\n" 3976 "int k; }} // namespace out::mid", 3977 Style)); 3978 3979 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3980 EXPECT_EQ("namespace out { namespace in {\n" 3981 " int i;\n" 3982 "}} // namespace out::in", 3983 format("namespace out {\n" 3984 "namespace in {\n" 3985 "int i;\n" 3986 "} // namespace in\n" 3987 "} // namespace out", 3988 Style)); 3989 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 3990 " int i;\n" 3991 "}}} // namespace out::mid::in", 3992 format("namespace out {\n" 3993 "namespace mid {\n" 3994 "namespace in {\n" 3995 "int i;\n" 3996 "} // namespace in\n" 3997 "} // namespace mid\n" 3998 "} // namespace out", 3999 Style)); 4000 4001 Style.CompactNamespaces = true; 4002 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 4003 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4004 Style.BraceWrapping.BeforeLambdaBody = true; 4005 verifyFormat("namespace out { namespace in {\n" 4006 "}} // namespace out::in", 4007 Style); 4008 EXPECT_EQ("namespace out { namespace in {\n" 4009 "}} // namespace out::in", 4010 format("namespace out {\n" 4011 "namespace in {\n" 4012 "} // namespace in\n" 4013 "} // namespace out", 4014 Style)); 4015 } 4016 4017 TEST_F(FormatTest, FormatsExternC) { 4018 verifyFormat("extern \"C\" {\nint a;"); 4019 verifyFormat("extern \"C\" {}"); 4020 verifyFormat("extern \"C\" {\n" 4021 "int foo();\n" 4022 "}"); 4023 verifyFormat("extern \"C\" int foo() {}"); 4024 verifyFormat("extern \"C\" int foo();"); 4025 verifyFormat("extern \"C\" int foo() {\n" 4026 " int i = 42;\n" 4027 " return i;\n" 4028 "}"); 4029 4030 FormatStyle Style = getLLVMStyle(); 4031 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4032 Style.BraceWrapping.AfterFunction = true; 4033 verifyFormat("extern \"C\" int foo() {}", Style); 4034 verifyFormat("extern \"C\" int foo();", Style); 4035 verifyFormat("extern \"C\" int foo()\n" 4036 "{\n" 4037 " int i = 42;\n" 4038 " return i;\n" 4039 "}", 4040 Style); 4041 4042 Style.BraceWrapping.AfterExternBlock = true; 4043 Style.BraceWrapping.SplitEmptyRecord = false; 4044 verifyFormat("extern \"C\"\n" 4045 "{}", 4046 Style); 4047 verifyFormat("extern \"C\"\n" 4048 "{\n" 4049 " int foo();\n" 4050 "}", 4051 Style); 4052 } 4053 4054 TEST_F(FormatTest, IndentExternBlockStyle) { 4055 FormatStyle Style = getLLVMStyle(); 4056 Style.IndentWidth = 2; 4057 4058 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4059 verifyFormat("extern \"C\" { /*9*/\n" 4060 "}", 4061 Style); 4062 verifyFormat("extern \"C\" {\n" 4063 " int foo10();\n" 4064 "}", 4065 Style); 4066 4067 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4068 verifyFormat("extern \"C\" { /*11*/\n" 4069 "}", 4070 Style); 4071 verifyFormat("extern \"C\" {\n" 4072 "int foo12();\n" 4073 "}", 4074 Style); 4075 4076 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4077 Style.BraceWrapping.AfterExternBlock = true; 4078 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4079 verifyFormat("extern \"C\"\n" 4080 "{ /*13*/\n" 4081 "}", 4082 Style); 4083 verifyFormat("extern \"C\"\n{\n" 4084 " int foo14();\n" 4085 "}", 4086 Style); 4087 4088 Style.BraceWrapping.AfterExternBlock = false; 4089 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4090 verifyFormat("extern \"C\" { /*15*/\n" 4091 "}", 4092 Style); 4093 verifyFormat("extern \"C\" {\n" 4094 "int foo16();\n" 4095 "}", 4096 Style); 4097 4098 Style.BraceWrapping.AfterExternBlock = true; 4099 verifyFormat("extern \"C\"\n" 4100 "{ /*13*/\n" 4101 "}", 4102 Style); 4103 verifyFormat("extern \"C\"\n" 4104 "{\n" 4105 "int foo14();\n" 4106 "}", 4107 Style); 4108 4109 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4110 verifyFormat("extern \"C\"\n" 4111 "{ /*13*/\n" 4112 "}", 4113 Style); 4114 verifyFormat("extern \"C\"\n" 4115 "{\n" 4116 " int foo14();\n" 4117 "}", 4118 Style); 4119 } 4120 4121 TEST_F(FormatTest, FormatsInlineASM) { 4122 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 4123 verifyFormat("asm(\"nop\" ::: \"memory\");"); 4124 verifyFormat( 4125 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 4126 " \"cpuid\\n\\t\"\n" 4127 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 4128 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 4129 " : \"a\"(value));"); 4130 EXPECT_EQ( 4131 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4132 " __asm {\n" 4133 " mov edx,[that] // vtable in edx\n" 4134 " mov eax,methodIndex\n" 4135 " call [edx][eax*4] // stdcall\n" 4136 " }\n" 4137 "}", 4138 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4139 " __asm {\n" 4140 " mov edx,[that] // vtable in edx\n" 4141 " mov eax,methodIndex\n" 4142 " call [edx][eax*4] // stdcall\n" 4143 " }\n" 4144 "}")); 4145 EXPECT_EQ("_asm {\n" 4146 " xor eax, eax;\n" 4147 " cpuid;\n" 4148 "}", 4149 format("_asm {\n" 4150 " xor eax, eax;\n" 4151 " cpuid;\n" 4152 "}")); 4153 verifyFormat("void function() {\n" 4154 " // comment\n" 4155 " asm(\"\");\n" 4156 "}"); 4157 EXPECT_EQ("__asm {\n" 4158 "}\n" 4159 "int i;", 4160 format("__asm {\n" 4161 "}\n" 4162 "int i;")); 4163 } 4164 4165 TEST_F(FormatTest, FormatTryCatch) { 4166 verifyFormat("try {\n" 4167 " throw a * b;\n" 4168 "} catch (int a) {\n" 4169 " // Do nothing.\n" 4170 "} catch (...) {\n" 4171 " exit(42);\n" 4172 "}"); 4173 4174 // Function-level try statements. 4175 verifyFormat("int f() try { return 4; } catch (...) {\n" 4176 " return 5;\n" 4177 "}"); 4178 verifyFormat("class A {\n" 4179 " int a;\n" 4180 " A() try : a(0) {\n" 4181 " } catch (...) {\n" 4182 " throw;\n" 4183 " }\n" 4184 "};\n"); 4185 verifyFormat("class A {\n" 4186 " int a;\n" 4187 " A() try : a(0), b{1} {\n" 4188 " } catch (...) {\n" 4189 " throw;\n" 4190 " }\n" 4191 "};\n"); 4192 verifyFormat("class A {\n" 4193 " int a;\n" 4194 " A() try : a(0), b{1}, c{2} {\n" 4195 " } catch (...) {\n" 4196 " throw;\n" 4197 " }\n" 4198 "};\n"); 4199 verifyFormat("class A {\n" 4200 " int a;\n" 4201 " A() try : a(0), b{1}, c{2} {\n" 4202 " { // New scope.\n" 4203 " }\n" 4204 " } catch (...) {\n" 4205 " throw;\n" 4206 " }\n" 4207 "};\n"); 4208 4209 // Incomplete try-catch blocks. 4210 verifyIncompleteFormat("try {} catch ("); 4211 } 4212 4213 TEST_F(FormatTest, FormatTryAsAVariable) { 4214 verifyFormat("int try;"); 4215 verifyFormat("int try, size;"); 4216 verifyFormat("try = foo();"); 4217 verifyFormat("if (try < size) {\n return true;\n}"); 4218 4219 verifyFormat("int catch;"); 4220 verifyFormat("int catch, size;"); 4221 verifyFormat("catch = foo();"); 4222 verifyFormat("if (catch < size) {\n return true;\n}"); 4223 4224 FormatStyle Style = getLLVMStyle(); 4225 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4226 Style.BraceWrapping.AfterFunction = true; 4227 Style.BraceWrapping.BeforeCatch = true; 4228 verifyFormat("try {\n" 4229 " int bar = 1;\n" 4230 "}\n" 4231 "catch (...) {\n" 4232 " int bar = 1;\n" 4233 "}", 4234 Style); 4235 verifyFormat("#if NO_EX\n" 4236 "try\n" 4237 "#endif\n" 4238 "{\n" 4239 "}\n" 4240 "#if NO_EX\n" 4241 "catch (...) {\n" 4242 "}", 4243 Style); 4244 verifyFormat("try /* abc */ {\n" 4245 " int bar = 1;\n" 4246 "}\n" 4247 "catch (...) {\n" 4248 " int bar = 1;\n" 4249 "}", 4250 Style); 4251 verifyFormat("try\n" 4252 "// abc\n" 4253 "{\n" 4254 " int bar = 1;\n" 4255 "}\n" 4256 "catch (...) {\n" 4257 " int bar = 1;\n" 4258 "}", 4259 Style); 4260 } 4261 4262 TEST_F(FormatTest, FormatSEHTryCatch) { 4263 verifyFormat("__try {\n" 4264 " int a = b * c;\n" 4265 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 4266 " // Do nothing.\n" 4267 "}"); 4268 4269 verifyFormat("__try {\n" 4270 " int a = b * c;\n" 4271 "} __finally {\n" 4272 " // Do nothing.\n" 4273 "}"); 4274 4275 verifyFormat("DEBUG({\n" 4276 " __try {\n" 4277 " } __finally {\n" 4278 " }\n" 4279 "});\n"); 4280 } 4281 4282 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 4283 verifyFormat("try {\n" 4284 " f();\n" 4285 "} catch {\n" 4286 " g();\n" 4287 "}"); 4288 verifyFormat("try {\n" 4289 " f();\n" 4290 "} catch (A a) MACRO(x) {\n" 4291 " g();\n" 4292 "} catch (B b) MACRO(x) {\n" 4293 " g();\n" 4294 "}"); 4295 } 4296 4297 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 4298 FormatStyle Style = getLLVMStyle(); 4299 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 4300 FormatStyle::BS_WebKit}) { 4301 Style.BreakBeforeBraces = BraceStyle; 4302 verifyFormat("try {\n" 4303 " // something\n" 4304 "} catch (...) {\n" 4305 " // something\n" 4306 "}", 4307 Style); 4308 } 4309 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4310 verifyFormat("try {\n" 4311 " // something\n" 4312 "}\n" 4313 "catch (...) {\n" 4314 " // something\n" 4315 "}", 4316 Style); 4317 verifyFormat("__try {\n" 4318 " // something\n" 4319 "}\n" 4320 "__finally {\n" 4321 " // something\n" 4322 "}", 4323 Style); 4324 verifyFormat("@try {\n" 4325 " // something\n" 4326 "}\n" 4327 "@finally {\n" 4328 " // something\n" 4329 "}", 4330 Style); 4331 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4332 verifyFormat("try\n" 4333 "{\n" 4334 " // something\n" 4335 "}\n" 4336 "catch (...)\n" 4337 "{\n" 4338 " // something\n" 4339 "}", 4340 Style); 4341 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4342 verifyFormat("try\n" 4343 " {\n" 4344 " // something white\n" 4345 " }\n" 4346 "catch (...)\n" 4347 " {\n" 4348 " // something white\n" 4349 " }", 4350 Style); 4351 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4352 verifyFormat("try\n" 4353 " {\n" 4354 " // something\n" 4355 " }\n" 4356 "catch (...)\n" 4357 " {\n" 4358 " // something\n" 4359 " }", 4360 Style); 4361 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4362 Style.BraceWrapping.BeforeCatch = true; 4363 verifyFormat("try {\n" 4364 " // something\n" 4365 "}\n" 4366 "catch (...) {\n" 4367 " // something\n" 4368 "}", 4369 Style); 4370 } 4371 4372 TEST_F(FormatTest, StaticInitializers) { 4373 verifyFormat("static SomeClass SC = {1, 'a'};"); 4374 4375 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4376 " 100000000, " 4377 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4378 4379 // Here, everything other than the "}" would fit on a line. 4380 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4381 " 10000000000000000000000000};"); 4382 EXPECT_EQ("S s = {a,\n" 4383 "\n" 4384 " b};", 4385 format("S s = {\n" 4386 " a,\n" 4387 "\n" 4388 " b\n" 4389 "};")); 4390 4391 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4392 // line. However, the formatting looks a bit off and this probably doesn't 4393 // happen often in practice. 4394 verifyFormat("static int Variable[1] = {\n" 4395 " {1000000000000000000000000000000000000}};", 4396 getLLVMStyleWithColumns(40)); 4397 } 4398 4399 TEST_F(FormatTest, DesignatedInitializers) { 4400 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4401 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4402 " .bbbbbbbbbb = 2,\n" 4403 " .cccccccccc = 3,\n" 4404 " .dddddddddd = 4,\n" 4405 " .eeeeeeeeee = 5};"); 4406 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4407 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4408 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4409 " .ccccccccccccccccccccccccccc = 3,\n" 4410 " .ddddddddddddddddddddddddddd = 4,\n" 4411 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4412 4413 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4414 4415 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4416 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4417 " [2] = bbbbbbbbbb,\n" 4418 " [3] = cccccccccc,\n" 4419 " [4] = dddddddddd,\n" 4420 " [5] = eeeeeeeeee};"); 4421 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4422 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4423 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4424 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4425 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4426 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4427 } 4428 4429 TEST_F(FormatTest, NestedStaticInitializers) { 4430 verifyFormat("static A x = {{{}}};\n"); 4431 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4432 " {init1, init2, init3, init4}}};", 4433 getLLVMStyleWithColumns(50)); 4434 4435 verifyFormat("somes Status::global_reps[3] = {\n" 4436 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4437 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4438 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4439 getLLVMStyleWithColumns(60)); 4440 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4441 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4442 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4443 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4444 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4445 " {rect.fRight - rect.fLeft, rect.fBottom - " 4446 "rect.fTop}};"); 4447 4448 verifyFormat( 4449 "SomeArrayOfSomeType a = {\n" 4450 " {{1, 2, 3},\n" 4451 " {1, 2, 3},\n" 4452 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4453 " 333333333333333333333333333333},\n" 4454 " {1, 2, 3},\n" 4455 " {1, 2, 3}}};"); 4456 verifyFormat( 4457 "SomeArrayOfSomeType a = {\n" 4458 " {{1, 2, 3}},\n" 4459 " {{1, 2, 3}},\n" 4460 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4461 " 333333333333333333333333333333}},\n" 4462 " {{1, 2, 3}},\n" 4463 " {{1, 2, 3}}};"); 4464 4465 verifyFormat("struct {\n" 4466 " unsigned bit;\n" 4467 " const char *const name;\n" 4468 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4469 " {kOsWin, \"Windows\"},\n" 4470 " {kOsLinux, \"Linux\"},\n" 4471 " {kOsCrOS, \"Chrome OS\"}};"); 4472 verifyFormat("struct {\n" 4473 " unsigned bit;\n" 4474 " const char *const name;\n" 4475 "} kBitsToOs[] = {\n" 4476 " {kOsMac, \"Mac\"},\n" 4477 " {kOsWin, \"Windows\"},\n" 4478 " {kOsLinux, \"Linux\"},\n" 4479 " {kOsCrOS, \"Chrome OS\"},\n" 4480 "};"); 4481 } 4482 4483 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4484 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4485 " \\\n" 4486 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4487 } 4488 4489 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4490 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4491 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4492 4493 // Do break defaulted and deleted functions. 4494 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4495 " default;", 4496 getLLVMStyleWithColumns(40)); 4497 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4498 " delete;", 4499 getLLVMStyleWithColumns(40)); 4500 } 4501 4502 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4503 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4504 getLLVMStyleWithColumns(40)); 4505 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4506 getLLVMStyleWithColumns(40)); 4507 EXPECT_EQ("#define Q \\\n" 4508 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4509 " \"aaaaaaaa.cpp\"", 4510 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4511 getLLVMStyleWithColumns(40))); 4512 } 4513 4514 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4515 EXPECT_EQ("# 123 \"A string literal\"", 4516 format(" # 123 \"A string literal\"")); 4517 } 4518 4519 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4520 EXPECT_EQ("#;", format("#;")); 4521 verifyFormat("#\n;\n;\n;"); 4522 } 4523 4524 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4525 EXPECT_EQ("#line 42 \"test\"\n", 4526 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4527 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4528 getLLVMStyleWithColumns(12))); 4529 } 4530 4531 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4532 EXPECT_EQ("#line 42 \"test\"", 4533 format("# \\\n line \\\n 42 \\\n \"test\"")); 4534 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4535 } 4536 4537 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4538 verifyFormat("#define A \\x20"); 4539 verifyFormat("#define A \\ x20"); 4540 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4541 verifyFormat("#define A ''"); 4542 verifyFormat("#define A ''qqq"); 4543 verifyFormat("#define A `qqq"); 4544 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4545 EXPECT_EQ("const char *c = STRINGIFY(\n" 4546 "\\na : b);", 4547 format("const char * c = STRINGIFY(\n" 4548 "\\na : b);")); 4549 4550 verifyFormat("a\r\\"); 4551 verifyFormat("a\v\\"); 4552 verifyFormat("a\f\\"); 4553 } 4554 4555 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4556 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4557 style.IndentWidth = 4; 4558 style.PPIndentWidth = 1; 4559 4560 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4561 verifyFormat("#ifdef __linux__\n" 4562 "void foo() {\n" 4563 " int x = 0;\n" 4564 "}\n" 4565 "#define FOO\n" 4566 "#endif\n" 4567 "void bar() {\n" 4568 " int y = 0;\n" 4569 "}\n", 4570 style); 4571 4572 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4573 verifyFormat("#ifdef __linux__\n" 4574 "void foo() {\n" 4575 " int x = 0;\n" 4576 "}\n" 4577 "# define FOO foo\n" 4578 "#endif\n" 4579 "void bar() {\n" 4580 " int y = 0;\n" 4581 "}\n", 4582 style); 4583 4584 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4585 verifyFormat("#ifdef __linux__\n" 4586 "void foo() {\n" 4587 " int x = 0;\n" 4588 "}\n" 4589 " #define FOO foo\n" 4590 "#endif\n" 4591 "void bar() {\n" 4592 " int y = 0;\n" 4593 "}\n", 4594 style); 4595 } 4596 4597 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4598 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4599 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4600 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4601 // FIXME: We never break before the macro name. 4602 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4603 4604 verifyFormat("#define A A\n#define A A"); 4605 verifyFormat("#define A(X) A\n#define A A"); 4606 4607 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4608 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4609 } 4610 4611 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4612 EXPECT_EQ("// somecomment\n" 4613 "#include \"a.h\"\n" 4614 "#define A( \\\n" 4615 " A, B)\n" 4616 "#include \"b.h\"\n" 4617 "// somecomment\n", 4618 format(" // somecomment\n" 4619 " #include \"a.h\"\n" 4620 "#define A(A,\\\n" 4621 " B)\n" 4622 " #include \"b.h\"\n" 4623 " // somecomment\n", 4624 getLLVMStyleWithColumns(13))); 4625 } 4626 4627 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4628 4629 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4630 EXPECT_EQ("#define A \\\n" 4631 " c; \\\n" 4632 " e;\n" 4633 "f;", 4634 format("#define A c; e;\n" 4635 "f;", 4636 getLLVMStyleWithColumns(14))); 4637 } 4638 4639 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4640 4641 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4642 EXPECT_EQ("int x,\n" 4643 "#define A\n" 4644 " y;", 4645 format("int x,\n#define A\ny;")); 4646 } 4647 4648 TEST_F(FormatTest, HashInMacroDefinition) { 4649 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4650 EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle())); 4651 EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle())); 4652 EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle())); 4653 EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle())); 4654 EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle())); 4655 EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle())); 4656 EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle())); 4657 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4658 verifyFormat("#define A \\\n" 4659 " { \\\n" 4660 " f(#c); \\\n" 4661 " }", 4662 getLLVMStyleWithColumns(11)); 4663 4664 verifyFormat("#define A(X) \\\n" 4665 " void function##X()", 4666 getLLVMStyleWithColumns(22)); 4667 4668 verifyFormat("#define A(a, b, c) \\\n" 4669 " void a##b##c()", 4670 getLLVMStyleWithColumns(22)); 4671 4672 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4673 } 4674 4675 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4676 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4677 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4678 4679 FormatStyle Style = getLLVMStyle(); 4680 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4681 verifyFormat("#define true ((foo)1)", Style); 4682 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4683 verifyFormat("#define false((foo)0)", Style); 4684 } 4685 4686 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4687 EXPECT_EQ("#define A b;", format("#define A \\\n" 4688 " \\\n" 4689 " b;", 4690 getLLVMStyleWithColumns(25))); 4691 EXPECT_EQ("#define A \\\n" 4692 " \\\n" 4693 " a; \\\n" 4694 " b;", 4695 format("#define A \\\n" 4696 " \\\n" 4697 " a; \\\n" 4698 " b;", 4699 getLLVMStyleWithColumns(11))); 4700 EXPECT_EQ("#define A \\\n" 4701 " a; \\\n" 4702 " \\\n" 4703 " b;", 4704 format("#define A \\\n" 4705 " a; \\\n" 4706 " \\\n" 4707 " b;", 4708 getLLVMStyleWithColumns(11))); 4709 } 4710 4711 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4712 verifyIncompleteFormat("#define A :"); 4713 verifyFormat("#define SOMECASES \\\n" 4714 " case 1: \\\n" 4715 " case 2\n", 4716 getLLVMStyleWithColumns(20)); 4717 verifyFormat("#define MACRO(a) \\\n" 4718 " if (a) \\\n" 4719 " f(); \\\n" 4720 " else \\\n" 4721 " g()", 4722 getLLVMStyleWithColumns(18)); 4723 verifyFormat("#define A template <typename T>"); 4724 verifyIncompleteFormat("#define STR(x) #x\n" 4725 "f(STR(this_is_a_string_literal{));"); 4726 verifyFormat("#pragma omp threadprivate( \\\n" 4727 " y)), // expected-warning", 4728 getLLVMStyleWithColumns(28)); 4729 verifyFormat("#d, = };"); 4730 verifyFormat("#if \"a"); 4731 verifyIncompleteFormat("({\n" 4732 "#define b \\\n" 4733 " } \\\n" 4734 " a\n" 4735 "a", 4736 getLLVMStyleWithColumns(15)); 4737 verifyFormat("#define A \\\n" 4738 " { \\\n" 4739 " {\n" 4740 "#define B \\\n" 4741 " } \\\n" 4742 " }", 4743 getLLVMStyleWithColumns(15)); 4744 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4745 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4746 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4747 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4748 } 4749 4750 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4751 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4752 EXPECT_EQ("class A : public QObject {\n" 4753 " Q_OBJECT\n" 4754 "\n" 4755 " A() {}\n" 4756 "};", 4757 format("class A : public QObject {\n" 4758 " Q_OBJECT\n" 4759 "\n" 4760 " A() {\n}\n" 4761 "} ;")); 4762 EXPECT_EQ("MACRO\n" 4763 "/*static*/ int i;", 4764 format("MACRO\n" 4765 " /*static*/ int i;")); 4766 EXPECT_EQ("SOME_MACRO\n" 4767 "namespace {\n" 4768 "void f();\n" 4769 "} // namespace", 4770 format("SOME_MACRO\n" 4771 " namespace {\n" 4772 "void f( );\n" 4773 "} // namespace")); 4774 // Only if the identifier contains at least 5 characters. 4775 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4776 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4777 // Only if everything is upper case. 4778 EXPECT_EQ("class A : public QObject {\n" 4779 " Q_Object A() {}\n" 4780 "};", 4781 format("class A : public QObject {\n" 4782 " Q_Object\n" 4783 " A() {\n}\n" 4784 "} ;")); 4785 4786 // Only if the next line can actually start an unwrapped line. 4787 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4788 format("SOME_WEIRD_LOG_MACRO\n" 4789 "<< SomeThing;")); 4790 4791 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4792 "(n, buffers))\n", 4793 getChromiumStyle(FormatStyle::LK_Cpp)); 4794 4795 // See PR41483 4796 EXPECT_EQ("/**/ FOO(a)\n" 4797 "FOO(b)", 4798 format("/**/ FOO(a)\n" 4799 "FOO(b)")); 4800 } 4801 4802 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4803 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4804 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4805 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4806 "class X {};\n" 4807 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4808 "int *createScopDetectionPass() { return 0; }", 4809 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4810 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4811 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4812 " class X {};\n" 4813 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4814 " int *createScopDetectionPass() { return 0; }")); 4815 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4816 // braces, so that inner block is indented one level more. 4817 EXPECT_EQ("int q() {\n" 4818 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4819 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4820 " IPC_END_MESSAGE_MAP()\n" 4821 "}", 4822 format("int q() {\n" 4823 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4824 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4825 " IPC_END_MESSAGE_MAP()\n" 4826 "}")); 4827 4828 // Same inside macros. 4829 EXPECT_EQ("#define LIST(L) \\\n" 4830 " L(A) \\\n" 4831 " L(B) \\\n" 4832 " L(C)", 4833 format("#define LIST(L) \\\n" 4834 " L(A) \\\n" 4835 " L(B) \\\n" 4836 " L(C)", 4837 getGoogleStyle())); 4838 4839 // These must not be recognized as macros. 4840 EXPECT_EQ("int q() {\n" 4841 " f(x);\n" 4842 " f(x) {}\n" 4843 " f(x)->g();\n" 4844 " f(x)->*g();\n" 4845 " f(x).g();\n" 4846 " f(x) = x;\n" 4847 " f(x) += x;\n" 4848 " f(x) -= x;\n" 4849 " f(x) *= x;\n" 4850 " f(x) /= x;\n" 4851 " f(x) %= x;\n" 4852 " f(x) &= x;\n" 4853 " f(x) |= x;\n" 4854 " f(x) ^= x;\n" 4855 " f(x) >>= x;\n" 4856 " f(x) <<= x;\n" 4857 " f(x)[y].z();\n" 4858 " LOG(INFO) << x;\n" 4859 " ifstream(x) >> x;\n" 4860 "}\n", 4861 format("int q() {\n" 4862 " f(x)\n;\n" 4863 " f(x)\n {}\n" 4864 " f(x)\n->g();\n" 4865 " f(x)\n->*g();\n" 4866 " f(x)\n.g();\n" 4867 " f(x)\n = x;\n" 4868 " f(x)\n += x;\n" 4869 " f(x)\n -= x;\n" 4870 " f(x)\n *= x;\n" 4871 " f(x)\n /= x;\n" 4872 " f(x)\n %= x;\n" 4873 " f(x)\n &= x;\n" 4874 " f(x)\n |= x;\n" 4875 " f(x)\n ^= x;\n" 4876 " f(x)\n >>= x;\n" 4877 " f(x)\n <<= x;\n" 4878 " f(x)\n[y].z();\n" 4879 " LOG(INFO)\n << x;\n" 4880 " ifstream(x)\n >> x;\n" 4881 "}\n")); 4882 EXPECT_EQ("int q() {\n" 4883 " F(x)\n" 4884 " if (1) {\n" 4885 " }\n" 4886 " F(x)\n" 4887 " while (1) {\n" 4888 " }\n" 4889 " F(x)\n" 4890 " G(x);\n" 4891 " F(x)\n" 4892 " try {\n" 4893 " Q();\n" 4894 " } catch (...) {\n" 4895 " }\n" 4896 "}\n", 4897 format("int q() {\n" 4898 "F(x)\n" 4899 "if (1) {}\n" 4900 "F(x)\n" 4901 "while (1) {}\n" 4902 "F(x)\n" 4903 "G(x);\n" 4904 "F(x)\n" 4905 "try { Q(); } catch (...) {}\n" 4906 "}\n")); 4907 EXPECT_EQ("class A {\n" 4908 " A() : t(0) {}\n" 4909 " A(int i) noexcept() : {}\n" 4910 " A(X x)\n" // FIXME: function-level try blocks are broken. 4911 " try : t(0) {\n" 4912 " } catch (...) {\n" 4913 " }\n" 4914 "};", 4915 format("class A {\n" 4916 " A()\n : t(0) {}\n" 4917 " A(int i)\n noexcept() : {}\n" 4918 " A(X x)\n" 4919 " try : t(0) {} catch (...) {}\n" 4920 "};")); 4921 FormatStyle Style = getLLVMStyle(); 4922 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4923 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 4924 Style.BraceWrapping.AfterFunction = true; 4925 EXPECT_EQ("void f()\n" 4926 "try\n" 4927 "{\n" 4928 "}", 4929 format("void f() try {\n" 4930 "}", 4931 Style)); 4932 EXPECT_EQ("class SomeClass {\n" 4933 "public:\n" 4934 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4935 "};", 4936 format("class SomeClass {\n" 4937 "public:\n" 4938 " SomeClass()\n" 4939 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4940 "};")); 4941 EXPECT_EQ("class SomeClass {\n" 4942 "public:\n" 4943 " SomeClass()\n" 4944 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4945 "};", 4946 format("class SomeClass {\n" 4947 "public:\n" 4948 " SomeClass()\n" 4949 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4950 "};", 4951 getLLVMStyleWithColumns(40))); 4952 4953 verifyFormat("MACRO(>)"); 4954 4955 // Some macros contain an implicit semicolon. 4956 Style = getLLVMStyle(); 4957 Style.StatementMacros.push_back("FOO"); 4958 verifyFormat("FOO(a) int b = 0;"); 4959 verifyFormat("FOO(a)\n" 4960 "int b = 0;", 4961 Style); 4962 verifyFormat("FOO(a);\n" 4963 "int b = 0;", 4964 Style); 4965 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 4966 "int b = 0;", 4967 Style); 4968 verifyFormat("FOO()\n" 4969 "int b = 0;", 4970 Style); 4971 verifyFormat("FOO\n" 4972 "int b = 0;", 4973 Style); 4974 verifyFormat("void f() {\n" 4975 " FOO(a)\n" 4976 " return a;\n" 4977 "}", 4978 Style); 4979 verifyFormat("FOO(a)\n" 4980 "FOO(b)", 4981 Style); 4982 verifyFormat("int a = 0;\n" 4983 "FOO(b)\n" 4984 "int c = 0;", 4985 Style); 4986 verifyFormat("int a = 0;\n" 4987 "int x = FOO(a)\n" 4988 "int b = 0;", 4989 Style); 4990 verifyFormat("void foo(int a) { FOO(a) }\n" 4991 "uint32_t bar() {}", 4992 Style); 4993 } 4994 4995 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) { 4996 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 4997 4998 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()", 4999 ZeroColumn); 5000 } 5001 5002 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 5003 verifyFormat("#define A \\\n" 5004 " f({ \\\n" 5005 " g(); \\\n" 5006 " });", 5007 getLLVMStyleWithColumns(11)); 5008 } 5009 5010 TEST_F(FormatTest, IndentPreprocessorDirectives) { 5011 FormatStyle Style = getLLVMStyleWithColumns(40); 5012 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 5013 verifyFormat("#ifdef _WIN32\n" 5014 "#define A 0\n" 5015 "#ifdef VAR2\n" 5016 "#define B 1\n" 5017 "#include <someheader.h>\n" 5018 "#define MACRO \\\n" 5019 " some_very_long_func_aaaaaaaaaa();\n" 5020 "#endif\n" 5021 "#else\n" 5022 "#define A 1\n" 5023 "#endif", 5024 Style); 5025 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 5026 verifyFormat("#ifdef _WIN32\n" 5027 "# define A 0\n" 5028 "# ifdef VAR2\n" 5029 "# define B 1\n" 5030 "# include <someheader.h>\n" 5031 "# define MACRO \\\n" 5032 " some_very_long_func_aaaaaaaaaa();\n" 5033 "# endif\n" 5034 "#else\n" 5035 "# define A 1\n" 5036 "#endif", 5037 Style); 5038 verifyFormat("#if A\n" 5039 "# define MACRO \\\n" 5040 " void a(int x) { \\\n" 5041 " b(); \\\n" 5042 " c(); \\\n" 5043 " d(); \\\n" 5044 " e(); \\\n" 5045 " f(); \\\n" 5046 " }\n" 5047 "#endif", 5048 Style); 5049 // Comments before include guard. 5050 verifyFormat("// file comment\n" 5051 "// file comment\n" 5052 "#ifndef HEADER_H\n" 5053 "#define HEADER_H\n" 5054 "code();\n" 5055 "#endif", 5056 Style); 5057 // Test with include guards. 5058 verifyFormat("#ifndef HEADER_H\n" 5059 "#define HEADER_H\n" 5060 "code();\n" 5061 "#endif", 5062 Style); 5063 // Include guards must have a #define with the same variable immediately 5064 // after #ifndef. 5065 verifyFormat("#ifndef NOT_GUARD\n" 5066 "# define FOO\n" 5067 "code();\n" 5068 "#endif", 5069 Style); 5070 5071 // Include guards must cover the entire file. 5072 verifyFormat("code();\n" 5073 "code();\n" 5074 "#ifndef NOT_GUARD\n" 5075 "# define NOT_GUARD\n" 5076 "code();\n" 5077 "#endif", 5078 Style); 5079 verifyFormat("#ifndef NOT_GUARD\n" 5080 "# define NOT_GUARD\n" 5081 "code();\n" 5082 "#endif\n" 5083 "code();", 5084 Style); 5085 // Test with trailing blank lines. 5086 verifyFormat("#ifndef HEADER_H\n" 5087 "#define HEADER_H\n" 5088 "code();\n" 5089 "#endif\n", 5090 Style); 5091 // Include guards don't have #else. 5092 verifyFormat("#ifndef NOT_GUARD\n" 5093 "# define NOT_GUARD\n" 5094 "code();\n" 5095 "#else\n" 5096 "#endif", 5097 Style); 5098 verifyFormat("#ifndef NOT_GUARD\n" 5099 "# define NOT_GUARD\n" 5100 "code();\n" 5101 "#elif FOO\n" 5102 "#endif", 5103 Style); 5104 // Non-identifier #define after potential include guard. 5105 verifyFormat("#ifndef FOO\n" 5106 "# define 1\n" 5107 "#endif\n", 5108 Style); 5109 // #if closes past last non-preprocessor line. 5110 verifyFormat("#ifndef FOO\n" 5111 "#define FOO\n" 5112 "#if 1\n" 5113 "int i;\n" 5114 "# define A 0\n" 5115 "#endif\n" 5116 "#endif\n", 5117 Style); 5118 // Don't crash if there is an #elif directive without a condition. 5119 verifyFormat("#if 1\n" 5120 "int x;\n" 5121 "#elif\n" 5122 "int y;\n" 5123 "#else\n" 5124 "int z;\n" 5125 "#endif", 5126 Style); 5127 // FIXME: This doesn't handle the case where there's code between the 5128 // #ifndef and #define but all other conditions hold. This is because when 5129 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 5130 // previous code line yet, so we can't detect it. 5131 EXPECT_EQ("#ifndef NOT_GUARD\n" 5132 "code();\n" 5133 "#define NOT_GUARD\n" 5134 "code();\n" 5135 "#endif", 5136 format("#ifndef NOT_GUARD\n" 5137 "code();\n" 5138 "# define NOT_GUARD\n" 5139 "code();\n" 5140 "#endif", 5141 Style)); 5142 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 5143 // be outside an include guard. Examples are #pragma once and 5144 // #pragma GCC diagnostic, or anything else that does not change the meaning 5145 // of the file if it's included multiple times. 5146 EXPECT_EQ("#ifdef WIN32\n" 5147 "# pragma once\n" 5148 "#endif\n" 5149 "#ifndef HEADER_H\n" 5150 "# define HEADER_H\n" 5151 "code();\n" 5152 "#endif", 5153 format("#ifdef WIN32\n" 5154 "# pragma once\n" 5155 "#endif\n" 5156 "#ifndef HEADER_H\n" 5157 "#define HEADER_H\n" 5158 "code();\n" 5159 "#endif", 5160 Style)); 5161 // FIXME: This does not detect when there is a single non-preprocessor line 5162 // in front of an include-guard-like structure where other conditions hold 5163 // because ScopedLineState hides the line. 5164 EXPECT_EQ("code();\n" 5165 "#ifndef HEADER_H\n" 5166 "#define HEADER_H\n" 5167 "code();\n" 5168 "#endif", 5169 format("code();\n" 5170 "#ifndef HEADER_H\n" 5171 "# define HEADER_H\n" 5172 "code();\n" 5173 "#endif", 5174 Style)); 5175 // Keep comments aligned with #, otherwise indent comments normally. These 5176 // tests cannot use verifyFormat because messUp manipulates leading 5177 // whitespace. 5178 { 5179 const char *Expected = "" 5180 "void f() {\n" 5181 "#if 1\n" 5182 "// Preprocessor aligned.\n" 5183 "# define A 0\n" 5184 " // Code. Separated by blank line.\n" 5185 "\n" 5186 "# define B 0\n" 5187 " // Code. Not aligned with #\n" 5188 "# define C 0\n" 5189 "#endif"; 5190 const char *ToFormat = "" 5191 "void f() {\n" 5192 "#if 1\n" 5193 "// Preprocessor aligned.\n" 5194 "# define A 0\n" 5195 "// Code. Separated by blank line.\n" 5196 "\n" 5197 "# define B 0\n" 5198 " // Code. Not aligned with #\n" 5199 "# define C 0\n" 5200 "#endif"; 5201 EXPECT_EQ(Expected, format(ToFormat, Style)); 5202 EXPECT_EQ(Expected, format(Expected, Style)); 5203 } 5204 // Keep block quotes aligned. 5205 { 5206 const char *Expected = "" 5207 "void f() {\n" 5208 "#if 1\n" 5209 "/* Preprocessor aligned. */\n" 5210 "# define A 0\n" 5211 " /* Code. Separated by blank line. */\n" 5212 "\n" 5213 "# define B 0\n" 5214 " /* Code. Not aligned with # */\n" 5215 "# define C 0\n" 5216 "#endif"; 5217 const char *ToFormat = "" 5218 "void f() {\n" 5219 "#if 1\n" 5220 "/* Preprocessor aligned. */\n" 5221 "# define A 0\n" 5222 "/* Code. Separated by blank line. */\n" 5223 "\n" 5224 "# define B 0\n" 5225 " /* Code. Not aligned with # */\n" 5226 "# define C 0\n" 5227 "#endif"; 5228 EXPECT_EQ(Expected, format(ToFormat, Style)); 5229 EXPECT_EQ(Expected, format(Expected, Style)); 5230 } 5231 // Keep comments aligned with un-indented directives. 5232 { 5233 const char *Expected = "" 5234 "void f() {\n" 5235 "// Preprocessor aligned.\n" 5236 "#define A 0\n" 5237 " // Code. Separated by blank line.\n" 5238 "\n" 5239 "#define B 0\n" 5240 " // Code. Not aligned with #\n" 5241 "#define C 0\n"; 5242 const char *ToFormat = "" 5243 "void f() {\n" 5244 "// Preprocessor aligned.\n" 5245 "#define A 0\n" 5246 "// Code. Separated by blank line.\n" 5247 "\n" 5248 "#define B 0\n" 5249 " // Code. Not aligned with #\n" 5250 "#define C 0\n"; 5251 EXPECT_EQ(Expected, format(ToFormat, Style)); 5252 EXPECT_EQ(Expected, format(Expected, Style)); 5253 } 5254 // Test AfterHash with tabs. 5255 { 5256 FormatStyle Tabbed = Style; 5257 Tabbed.UseTab = FormatStyle::UT_Always; 5258 Tabbed.IndentWidth = 8; 5259 Tabbed.TabWidth = 8; 5260 verifyFormat("#ifdef _WIN32\n" 5261 "#\tdefine A 0\n" 5262 "#\tifdef VAR2\n" 5263 "#\t\tdefine B 1\n" 5264 "#\t\tinclude <someheader.h>\n" 5265 "#\t\tdefine MACRO \\\n" 5266 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 5267 "#\tendif\n" 5268 "#else\n" 5269 "#\tdefine A 1\n" 5270 "#endif", 5271 Tabbed); 5272 } 5273 5274 // Regression test: Multiline-macro inside include guards. 5275 verifyFormat("#ifndef HEADER_H\n" 5276 "#define HEADER_H\n" 5277 "#define A() \\\n" 5278 " int i; \\\n" 5279 " int j;\n" 5280 "#endif // HEADER_H", 5281 getLLVMStyleWithColumns(20)); 5282 5283 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5284 // Basic before hash indent tests 5285 verifyFormat("#ifdef _WIN32\n" 5286 " #define A 0\n" 5287 " #ifdef VAR2\n" 5288 " #define B 1\n" 5289 " #include <someheader.h>\n" 5290 " #define MACRO \\\n" 5291 " some_very_long_func_aaaaaaaaaa();\n" 5292 " #endif\n" 5293 "#else\n" 5294 " #define A 1\n" 5295 "#endif", 5296 Style); 5297 verifyFormat("#if A\n" 5298 " #define MACRO \\\n" 5299 " void a(int x) { \\\n" 5300 " b(); \\\n" 5301 " c(); \\\n" 5302 " d(); \\\n" 5303 " e(); \\\n" 5304 " f(); \\\n" 5305 " }\n" 5306 "#endif", 5307 Style); 5308 // Keep comments aligned with indented directives. These 5309 // tests cannot use verifyFormat because messUp manipulates leading 5310 // whitespace. 5311 { 5312 const char *Expected = "void f() {\n" 5313 "// Aligned to preprocessor.\n" 5314 "#if 1\n" 5315 " // Aligned to code.\n" 5316 " int a;\n" 5317 " #if 1\n" 5318 " // Aligned to preprocessor.\n" 5319 " #define A 0\n" 5320 " // Aligned to code.\n" 5321 " int b;\n" 5322 " #endif\n" 5323 "#endif\n" 5324 "}"; 5325 const char *ToFormat = "void f() {\n" 5326 "// Aligned to preprocessor.\n" 5327 "#if 1\n" 5328 "// Aligned to code.\n" 5329 "int a;\n" 5330 "#if 1\n" 5331 "// Aligned to preprocessor.\n" 5332 "#define A 0\n" 5333 "// Aligned to code.\n" 5334 "int b;\n" 5335 "#endif\n" 5336 "#endif\n" 5337 "}"; 5338 EXPECT_EQ(Expected, format(ToFormat, Style)); 5339 EXPECT_EQ(Expected, format(Expected, Style)); 5340 } 5341 { 5342 const char *Expected = "void f() {\n" 5343 "/* Aligned to preprocessor. */\n" 5344 "#if 1\n" 5345 " /* Aligned to code. */\n" 5346 " int a;\n" 5347 " #if 1\n" 5348 " /* Aligned to preprocessor. */\n" 5349 " #define A 0\n" 5350 " /* Aligned to code. */\n" 5351 " int b;\n" 5352 " #endif\n" 5353 "#endif\n" 5354 "}"; 5355 const char *ToFormat = "void f() {\n" 5356 "/* Aligned to preprocessor. */\n" 5357 "#if 1\n" 5358 "/* Aligned to code. */\n" 5359 "int a;\n" 5360 "#if 1\n" 5361 "/* Aligned to preprocessor. */\n" 5362 "#define A 0\n" 5363 "/* Aligned to code. */\n" 5364 "int b;\n" 5365 "#endif\n" 5366 "#endif\n" 5367 "}"; 5368 EXPECT_EQ(Expected, format(ToFormat, Style)); 5369 EXPECT_EQ(Expected, format(Expected, Style)); 5370 } 5371 5372 // Test single comment before preprocessor 5373 verifyFormat("// Comment\n" 5374 "\n" 5375 "#if 1\n" 5376 "#endif", 5377 Style); 5378 } 5379 5380 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5381 verifyFormat("{\n { a #c; }\n}"); 5382 } 5383 5384 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5385 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5386 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5387 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5388 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5389 } 5390 5391 TEST_F(FormatTest, EscapedNewlines) { 5392 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5393 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5394 format("#define A \\\nint i;\\\n int j;", Narrow)); 5395 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5396 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5397 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5398 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5399 5400 FormatStyle AlignLeft = getLLVMStyle(); 5401 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5402 EXPECT_EQ("#define MACRO(x) \\\n" 5403 "private: \\\n" 5404 " int x(int a);\n", 5405 format("#define MACRO(x) \\\n" 5406 "private: \\\n" 5407 " int x(int a);\n", 5408 AlignLeft)); 5409 5410 // CRLF line endings 5411 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5412 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5413 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5414 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5415 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5416 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5417 EXPECT_EQ("#define MACRO(x) \\\r\n" 5418 "private: \\\r\n" 5419 " int x(int a);\r\n", 5420 format("#define MACRO(x) \\\r\n" 5421 "private: \\\r\n" 5422 " int x(int a);\r\n", 5423 AlignLeft)); 5424 5425 FormatStyle DontAlign = getLLVMStyle(); 5426 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5427 DontAlign.MaxEmptyLinesToKeep = 3; 5428 // FIXME: can't use verifyFormat here because the newline before 5429 // "public:" is not inserted the first time it's reformatted 5430 EXPECT_EQ("#define A \\\n" 5431 " class Foo { \\\n" 5432 " void bar(); \\\n" 5433 "\\\n" 5434 "\\\n" 5435 "\\\n" 5436 " public: \\\n" 5437 " void baz(); \\\n" 5438 " };", 5439 format("#define A \\\n" 5440 " class Foo { \\\n" 5441 " void bar(); \\\n" 5442 "\\\n" 5443 "\\\n" 5444 "\\\n" 5445 " public: \\\n" 5446 " void baz(); \\\n" 5447 " };", 5448 DontAlign)); 5449 } 5450 5451 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5452 verifyFormat("#define A \\\n" 5453 " int v( \\\n" 5454 " a); \\\n" 5455 " int i;", 5456 getLLVMStyleWithColumns(11)); 5457 } 5458 5459 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5460 EXPECT_EQ( 5461 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5462 " \\\n" 5463 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5464 "\n" 5465 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5466 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5467 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5468 "\\\n" 5469 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5470 " \n" 5471 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5472 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5473 } 5474 5475 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5476 EXPECT_EQ("int\n" 5477 "#define A\n" 5478 " a;", 5479 format("int\n#define A\na;")); 5480 verifyFormat("functionCallTo(\n" 5481 " someOtherFunction(\n" 5482 " withSomeParameters, whichInSequence,\n" 5483 " areLongerThanALine(andAnotherCall,\n" 5484 "#define A B\n" 5485 " withMoreParamters,\n" 5486 " whichStronglyInfluenceTheLayout),\n" 5487 " andMoreParameters),\n" 5488 " trailing);", 5489 getLLVMStyleWithColumns(69)); 5490 verifyFormat("Foo::Foo()\n" 5491 "#ifdef BAR\n" 5492 " : baz(0)\n" 5493 "#endif\n" 5494 "{\n" 5495 "}"); 5496 verifyFormat("void f() {\n" 5497 " if (true)\n" 5498 "#ifdef A\n" 5499 " f(42);\n" 5500 " x();\n" 5501 "#else\n" 5502 " g();\n" 5503 " x();\n" 5504 "#endif\n" 5505 "}"); 5506 verifyFormat("void f(param1, param2,\n" 5507 " param3,\n" 5508 "#ifdef A\n" 5509 " param4(param5,\n" 5510 "#ifdef A1\n" 5511 " param6,\n" 5512 "#ifdef A2\n" 5513 " param7),\n" 5514 "#else\n" 5515 " param8),\n" 5516 " param9,\n" 5517 "#endif\n" 5518 " param10,\n" 5519 "#endif\n" 5520 " param11)\n" 5521 "#else\n" 5522 " param12)\n" 5523 "#endif\n" 5524 "{\n" 5525 " x();\n" 5526 "}", 5527 getLLVMStyleWithColumns(28)); 5528 verifyFormat("#if 1\n" 5529 "int i;"); 5530 verifyFormat("#if 1\n" 5531 "#endif\n" 5532 "#if 1\n" 5533 "#else\n" 5534 "#endif\n"); 5535 verifyFormat("DEBUG({\n" 5536 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5537 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5538 "});\n" 5539 "#if a\n" 5540 "#else\n" 5541 "#endif"); 5542 5543 verifyIncompleteFormat("void f(\n" 5544 "#if A\n" 5545 ");\n" 5546 "#else\n" 5547 "#endif"); 5548 } 5549 5550 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5551 verifyFormat("#endif\n" 5552 "#if B"); 5553 } 5554 5555 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5556 FormatStyle SingleLine = getLLVMStyle(); 5557 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5558 verifyFormat("#if 0\n" 5559 "#elif 1\n" 5560 "#endif\n" 5561 "void foo() {\n" 5562 " if (test) foo2();\n" 5563 "}", 5564 SingleLine); 5565 } 5566 5567 TEST_F(FormatTest, LayoutBlockInsideParens) { 5568 verifyFormat("functionCall({ int i; });"); 5569 verifyFormat("functionCall({\n" 5570 " int i;\n" 5571 " int j;\n" 5572 "});"); 5573 verifyFormat("functionCall(\n" 5574 " {\n" 5575 " int i;\n" 5576 " int j;\n" 5577 " },\n" 5578 " aaaa, bbbb, cccc);"); 5579 verifyFormat("functionA(functionB({\n" 5580 " int i;\n" 5581 " int j;\n" 5582 " }),\n" 5583 " aaaa, bbbb, cccc);"); 5584 verifyFormat("functionCall(\n" 5585 " {\n" 5586 " int i;\n" 5587 " int j;\n" 5588 " },\n" 5589 " aaaa, bbbb, // comment\n" 5590 " cccc);"); 5591 verifyFormat("functionA(functionB({\n" 5592 " int i;\n" 5593 " int j;\n" 5594 " }),\n" 5595 " aaaa, bbbb, // comment\n" 5596 " cccc);"); 5597 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5598 verifyFormat("functionCall(aaaa, bbbb, {\n" 5599 " int i;\n" 5600 " int j;\n" 5601 "});"); 5602 verifyFormat( 5603 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5604 " {\n" 5605 " int i; // break\n" 5606 " },\n" 5607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5608 " ccccccccccccccccc));"); 5609 verifyFormat("DEBUG({\n" 5610 " if (a)\n" 5611 " f();\n" 5612 "});"); 5613 } 5614 5615 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5616 EXPECT_EQ("SOME_MACRO { int i; }\n" 5617 "int i;", 5618 format(" SOME_MACRO {int i;} int i;")); 5619 } 5620 5621 TEST_F(FormatTest, LayoutNestedBlocks) { 5622 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5623 " struct s {\n" 5624 " int i;\n" 5625 " };\n" 5626 " s kBitsToOs[] = {{10}};\n" 5627 " for (int i = 0; i < 10; ++i)\n" 5628 " return;\n" 5629 "}"); 5630 verifyFormat("call(parameter, {\n" 5631 " something();\n" 5632 " // Comment using all columns.\n" 5633 " somethingelse();\n" 5634 "});", 5635 getLLVMStyleWithColumns(40)); 5636 verifyFormat("DEBUG( //\n" 5637 " { f(); }, a);"); 5638 verifyFormat("DEBUG( //\n" 5639 " {\n" 5640 " f(); //\n" 5641 " },\n" 5642 " a);"); 5643 5644 EXPECT_EQ("call(parameter, {\n" 5645 " something();\n" 5646 " // Comment too\n" 5647 " // looooooooooong.\n" 5648 " somethingElse();\n" 5649 "});", 5650 format("call(parameter, {\n" 5651 " something();\n" 5652 " // Comment too looooooooooong.\n" 5653 " somethingElse();\n" 5654 "});", 5655 getLLVMStyleWithColumns(29))); 5656 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5657 EXPECT_EQ("DEBUG({ // comment\n" 5658 " int i;\n" 5659 "});", 5660 format("DEBUG({ // comment\n" 5661 "int i;\n" 5662 "});")); 5663 EXPECT_EQ("DEBUG({\n" 5664 " int i;\n" 5665 "\n" 5666 " // comment\n" 5667 " int j;\n" 5668 "});", 5669 format("DEBUG({\n" 5670 " int i;\n" 5671 "\n" 5672 " // comment\n" 5673 " int j;\n" 5674 "});")); 5675 5676 verifyFormat("DEBUG({\n" 5677 " if (a)\n" 5678 " return;\n" 5679 "});"); 5680 verifyGoogleFormat("DEBUG({\n" 5681 " if (a) return;\n" 5682 "});"); 5683 FormatStyle Style = getGoogleStyle(); 5684 Style.ColumnLimit = 45; 5685 verifyFormat("Debug(\n" 5686 " aaaaa,\n" 5687 " {\n" 5688 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5689 " },\n" 5690 " a);", 5691 Style); 5692 5693 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5694 5695 verifyNoCrash("^{v^{a}}"); 5696 } 5697 5698 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5699 EXPECT_EQ("#define MACRO() \\\n" 5700 " Debug(aaa, /* force line break */ \\\n" 5701 " { \\\n" 5702 " int i; \\\n" 5703 " int j; \\\n" 5704 " })", 5705 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5706 " { int i; int j; })", 5707 getGoogleStyle())); 5708 5709 EXPECT_EQ("#define A \\\n" 5710 " [] { \\\n" 5711 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5712 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5713 " }", 5714 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5715 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5716 getGoogleStyle())); 5717 } 5718 5719 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5720 EXPECT_EQ("{}", format("{}")); 5721 verifyFormat("enum E {};"); 5722 verifyFormat("enum E {}"); 5723 FormatStyle Style = getLLVMStyle(); 5724 Style.SpaceInEmptyBlock = true; 5725 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5726 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5727 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5728 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5729 Style.BraceWrapping.BeforeElse = false; 5730 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5731 verifyFormat("if (a)\n" 5732 "{\n" 5733 "} else if (b)\n" 5734 "{\n" 5735 "} else\n" 5736 "{ }", 5737 Style); 5738 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 5739 verifyFormat("if (a) {\n" 5740 "} else if (b) {\n" 5741 "} else {\n" 5742 "}", 5743 Style); 5744 Style.BraceWrapping.BeforeElse = true; 5745 verifyFormat("if (a) { }\n" 5746 "else if (b) { }\n" 5747 "else { }", 5748 Style); 5749 } 5750 5751 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5752 FormatStyle Style = getLLVMStyle(); 5753 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5754 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5755 verifyFormat("FOO_BEGIN\n" 5756 " FOO_ENTRY\n" 5757 "FOO_END", 5758 Style); 5759 verifyFormat("FOO_BEGIN\n" 5760 " NESTED_FOO_BEGIN\n" 5761 " NESTED_FOO_ENTRY\n" 5762 " NESTED_FOO_END\n" 5763 "FOO_END", 5764 Style); 5765 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5766 " int x;\n" 5767 " x = 1;\n" 5768 "FOO_END(Baz)", 5769 Style); 5770 } 5771 5772 //===----------------------------------------------------------------------===// 5773 // Line break tests. 5774 //===----------------------------------------------------------------------===// 5775 5776 TEST_F(FormatTest, PreventConfusingIndents) { 5777 verifyFormat( 5778 "void f() {\n" 5779 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5780 " parameter, parameter, parameter)),\n" 5781 " SecondLongCall(parameter));\n" 5782 "}"); 5783 verifyFormat( 5784 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5785 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5787 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5788 verifyFormat( 5789 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5790 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5791 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5792 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5793 verifyFormat( 5794 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5795 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5797 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5798 verifyFormat("int a = bbbb && ccc &&\n" 5799 " fffff(\n" 5800 "#define A Just forcing a new line\n" 5801 " ddd);"); 5802 } 5803 5804 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5805 verifyFormat( 5806 "bool aaaaaaa =\n" 5807 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5808 " bbbbbbbb();"); 5809 verifyFormat( 5810 "bool aaaaaaa =\n" 5811 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5812 " bbbbbbbb();"); 5813 5814 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5816 " ccccccccc == ddddddddddd;"); 5817 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5818 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5819 " ccccccccc == ddddddddddd;"); 5820 verifyFormat( 5821 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5822 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5823 " ccccccccc == ddddddddddd;"); 5824 5825 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5826 " aaaaaa) &&\n" 5827 " bbbbbb && cccccc;"); 5828 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5829 " aaaaaa) >>\n" 5830 " bbbbbb;"); 5831 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5832 " SourceMgr.getSpellingColumnNumber(\n" 5833 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5834 " 1);"); 5835 5836 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5837 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5838 " cccccc) {\n}"); 5839 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5840 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5841 " cccccc) {\n}"); 5842 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5843 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5844 " cccccc) {\n}"); 5845 verifyFormat("b = a &&\n" 5846 " // Comment\n" 5847 " b.c && d;"); 5848 5849 // If the LHS of a comparison is not a binary expression itself, the 5850 // additional linebreak confuses many people. 5851 verifyFormat( 5852 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5854 "}"); 5855 verifyFormat( 5856 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5858 "}"); 5859 verifyFormat( 5860 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5862 "}"); 5863 verifyFormat( 5864 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5866 "}"); 5867 // Even explicit parentheses stress the precedence enough to make the 5868 // additional break unnecessary. 5869 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5870 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5871 "}"); 5872 // This cases is borderline, but with the indentation it is still readable. 5873 verifyFormat( 5874 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5875 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5877 "}", 5878 getLLVMStyleWithColumns(75)); 5879 5880 // If the LHS is a binary expression, we should still use the additional break 5881 // as otherwise the formatting hides the operator precedence. 5882 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5884 " 5) {\n" 5885 "}"); 5886 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5888 " 5) {\n" 5889 "}"); 5890 5891 FormatStyle OnePerLine = getLLVMStyle(); 5892 OnePerLine.BinPackParameters = false; 5893 verifyFormat( 5894 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5897 OnePerLine); 5898 5899 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5900 " .aaa(aaaaaaaaaaaaa) *\n" 5901 " aaaaaaa +\n" 5902 " aaaaaaa;", 5903 getLLVMStyleWithColumns(40)); 5904 } 5905 5906 TEST_F(FormatTest, ExpressionIndentation) { 5907 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5908 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5911 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5912 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5913 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5915 " ccccccccccccccccccccccccccccccccccccccccc;"); 5916 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5919 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5920 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5922 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5923 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5924 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5925 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5927 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5928 verifyFormat("if () {\n" 5929 "} else if (aaaaa && bbbbb > // break\n" 5930 " ccccc) {\n" 5931 "}"); 5932 verifyFormat("if () {\n" 5933 "} else if constexpr (aaaaa && bbbbb > // break\n" 5934 " ccccc) {\n" 5935 "}"); 5936 verifyFormat("if () {\n" 5937 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 5938 " ccccc) {\n" 5939 "}"); 5940 verifyFormat("if () {\n" 5941 "} else if (aaaaa &&\n" 5942 " bbbbb > // break\n" 5943 " ccccc &&\n" 5944 " ddddd) {\n" 5945 "}"); 5946 5947 // Presence of a trailing comment used to change indentation of b. 5948 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 5949 " b;\n" 5950 "return aaaaaaaaaaaaaaaaaaa +\n" 5951 " b; //", 5952 getLLVMStyleWithColumns(30)); 5953 } 5954 5955 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 5956 // Not sure what the best system is here. Like this, the LHS can be found 5957 // immediately above an operator (everything with the same or a higher 5958 // indent). The RHS is aligned right of the operator and so compasses 5959 // everything until something with the same indent as the operator is found. 5960 // FIXME: Is this a good system? 5961 FormatStyle Style = getLLVMStyle(); 5962 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5963 verifyFormat( 5964 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5965 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5966 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5967 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5968 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5969 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5970 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5971 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5972 " > ccccccccccccccccccccccccccccccccccccccccc;", 5973 Style); 5974 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5975 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5976 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5977 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5978 Style); 5979 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5980 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5981 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5982 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5983 Style); 5984 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5985 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5986 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5987 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5988 Style); 5989 verifyFormat("if () {\n" 5990 "} else if (aaaaa\n" 5991 " && bbbbb // break\n" 5992 " > ccccc) {\n" 5993 "}", 5994 Style); 5995 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5996 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5997 Style); 5998 verifyFormat("return (a)\n" 5999 " // comment\n" 6000 " + b;", 6001 Style); 6002 verifyFormat( 6003 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6004 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6005 " + cc;", 6006 Style); 6007 6008 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6009 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6010 Style); 6011 6012 // Forced by comments. 6013 verifyFormat( 6014 "unsigned ContentSize =\n" 6015 " sizeof(int16_t) // DWARF ARange version number\n" 6016 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6017 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6018 " + sizeof(int8_t); // Segment Size (in bytes)"); 6019 6020 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6021 " == boost::fusion::at_c<1>(iiii).second;", 6022 Style); 6023 6024 Style.ColumnLimit = 60; 6025 verifyFormat("zzzzzzzzzz\n" 6026 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6027 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6028 Style); 6029 6030 Style.ColumnLimit = 80; 6031 Style.IndentWidth = 4; 6032 Style.TabWidth = 4; 6033 Style.UseTab = FormatStyle::UT_Always; 6034 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6035 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6036 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 6037 "\t&& (someOtherLongishConditionPart1\n" 6038 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 6039 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 6040 "(someOtherLongishConditionPart1 || " 6041 "someOtherEvenLongerNestedConditionPart2);", 6042 Style)); 6043 } 6044 6045 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 6046 FormatStyle Style = getLLVMStyle(); 6047 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6048 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 6049 6050 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6051 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6052 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6053 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6054 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6055 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6056 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6057 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6058 " > ccccccccccccccccccccccccccccccccccccccccc;", 6059 Style); 6060 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6061 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6062 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6063 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6064 Style); 6065 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6066 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6067 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6068 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6069 Style); 6070 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6071 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6072 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6073 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6074 Style); 6075 verifyFormat("if () {\n" 6076 "} else if (aaaaa\n" 6077 " && bbbbb // break\n" 6078 " > ccccc) {\n" 6079 "}", 6080 Style); 6081 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6082 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6083 Style); 6084 verifyFormat("return (a)\n" 6085 " // comment\n" 6086 " + b;", 6087 Style); 6088 verifyFormat( 6089 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6090 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6091 " + cc;", 6092 Style); 6093 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6094 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6095 " : 3333333333333333;", 6096 Style); 6097 verifyFormat( 6098 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6099 " : ccccccccccccccc ? dddddddddddddddddd\n" 6100 " : eeeeeeeeeeeeeeeeee)\n" 6101 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6102 " : 3333333333333333;", 6103 Style); 6104 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6105 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6106 Style); 6107 6108 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6109 " == boost::fusion::at_c<1>(iiii).second;", 6110 Style); 6111 6112 Style.ColumnLimit = 60; 6113 verifyFormat("zzzzzzzzzzzzz\n" 6114 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6115 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6116 Style); 6117 6118 // Forced by comments. 6119 Style.ColumnLimit = 80; 6120 verifyFormat( 6121 "unsigned ContentSize\n" 6122 " = sizeof(int16_t) // DWARF ARange version number\n" 6123 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6124 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6125 " + sizeof(int8_t); // Segment Size (in bytes)", 6126 Style); 6127 6128 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6129 verifyFormat( 6130 "unsigned ContentSize =\n" 6131 " sizeof(int16_t) // DWARF ARange version number\n" 6132 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6133 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6134 " + sizeof(int8_t); // Segment Size (in bytes)", 6135 Style); 6136 6137 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6138 verifyFormat( 6139 "unsigned ContentSize =\n" 6140 " sizeof(int16_t) // DWARF ARange version number\n" 6141 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6142 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6143 " + sizeof(int8_t); // Segment Size (in bytes)", 6144 Style); 6145 } 6146 6147 TEST_F(FormatTest, EnforcedOperatorWraps) { 6148 // Here we'd like to wrap after the || operators, but a comment is forcing an 6149 // earlier wrap. 6150 verifyFormat("bool x = aaaaa //\n" 6151 " || bbbbb\n" 6152 " //\n" 6153 " || cccc;"); 6154 } 6155 6156 TEST_F(FormatTest, NoOperandAlignment) { 6157 FormatStyle Style = getLLVMStyle(); 6158 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6159 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 6160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6162 Style); 6163 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6164 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6165 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6166 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6167 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6168 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6169 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6170 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6171 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6172 " > ccccccccccccccccccccccccccccccccccccccccc;", 6173 Style); 6174 6175 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6176 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6177 " + cc;", 6178 Style); 6179 verifyFormat("int a = aa\n" 6180 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6181 " * cccccccccccccccccccccccccccccccccccc;\n", 6182 Style); 6183 6184 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6185 verifyFormat("return (a > b\n" 6186 " // comment1\n" 6187 " // comment2\n" 6188 " || c);", 6189 Style); 6190 } 6191 6192 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 6193 FormatStyle Style = getLLVMStyle(); 6194 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6195 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6197 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6198 Style); 6199 } 6200 6201 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 6202 FormatStyle Style = getLLVMStyleWithColumns(40); 6203 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6204 Style.BinPackArguments = false; 6205 verifyFormat("void test() {\n" 6206 " someFunction(\n" 6207 " this + argument + is + quite\n" 6208 " + long + so + it + gets + wrapped\n" 6209 " + but + remains + bin - packed);\n" 6210 "}", 6211 Style); 6212 verifyFormat("void test() {\n" 6213 " someFunction(arg1,\n" 6214 " this + argument + is\n" 6215 " + quite + long + so\n" 6216 " + it + gets + wrapped\n" 6217 " + but + remains + bin\n" 6218 " - packed,\n" 6219 " arg3);\n" 6220 "}", 6221 Style); 6222 verifyFormat("void test() {\n" 6223 " someFunction(\n" 6224 " arg1,\n" 6225 " this + argument + has\n" 6226 " + anotherFunc(nested,\n" 6227 " calls + whose\n" 6228 " + arguments\n" 6229 " + are + also\n" 6230 " + wrapped,\n" 6231 " in + addition)\n" 6232 " + to + being + bin - packed,\n" 6233 " arg3);\n" 6234 "}", 6235 Style); 6236 6237 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6238 verifyFormat("void test() {\n" 6239 " someFunction(\n" 6240 " arg1,\n" 6241 " this + argument + has +\n" 6242 " anotherFunc(nested,\n" 6243 " calls + whose +\n" 6244 " arguments +\n" 6245 " are + also +\n" 6246 " wrapped,\n" 6247 " in + addition) +\n" 6248 " to + being + bin - packed,\n" 6249 " arg3);\n" 6250 "}", 6251 Style); 6252 } 6253 6254 TEST_F(FormatTest, ConstructorInitializers) { 6255 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6256 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 6257 getLLVMStyleWithColumns(45)); 6258 verifyFormat("Constructor()\n" 6259 " : Inttializer(FitsOnTheLine) {}", 6260 getLLVMStyleWithColumns(44)); 6261 verifyFormat("Constructor()\n" 6262 " : Inttializer(FitsOnTheLine) {}", 6263 getLLVMStyleWithColumns(43)); 6264 6265 verifyFormat("template <typename T>\n" 6266 "Constructor() : Initializer(FitsOnTheLine) {}", 6267 getLLVMStyleWithColumns(45)); 6268 6269 verifyFormat( 6270 "SomeClass::Constructor()\n" 6271 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6272 6273 verifyFormat( 6274 "SomeClass::Constructor()\n" 6275 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6276 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 6277 verifyFormat( 6278 "SomeClass::Constructor()\n" 6279 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6280 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6281 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6282 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6283 " : aaaaaaaaaa(aaaaaa) {}"); 6284 6285 verifyFormat("Constructor()\n" 6286 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6287 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6288 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6289 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 6290 6291 verifyFormat("Constructor()\n" 6292 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6294 6295 verifyFormat("Constructor(int Parameter = 0)\n" 6296 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6297 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 6298 verifyFormat("Constructor()\n" 6299 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6300 "}", 6301 getLLVMStyleWithColumns(60)); 6302 verifyFormat("Constructor()\n" 6303 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6304 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 6305 6306 // Here a line could be saved by splitting the second initializer onto two 6307 // lines, but that is not desirable. 6308 verifyFormat("Constructor()\n" 6309 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6310 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6311 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6312 6313 FormatStyle OnePerLine = getLLVMStyle(); 6314 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 6315 verifyFormat("MyClass::MyClass()\n" 6316 " : a(a),\n" 6317 " b(b),\n" 6318 " c(c) {}", 6319 OnePerLine); 6320 verifyFormat("MyClass::MyClass()\n" 6321 " : a(a), // comment\n" 6322 " b(b),\n" 6323 " c(c) {}", 6324 OnePerLine); 6325 verifyFormat("MyClass::MyClass(int a)\n" 6326 " : b(a), // comment\n" 6327 " c(a + 1) { // lined up\n" 6328 "}", 6329 OnePerLine); 6330 verifyFormat("Constructor()\n" 6331 " : a(b, b, b) {}", 6332 OnePerLine); 6333 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6334 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 6335 verifyFormat("SomeClass::Constructor()\n" 6336 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6337 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6338 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6339 OnePerLine); 6340 verifyFormat("SomeClass::Constructor()\n" 6341 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6342 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6343 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6344 OnePerLine); 6345 verifyFormat("MyClass::MyClass(int var)\n" 6346 " : some_var_(var), // 4 space indent\n" 6347 " some_other_var_(var + 1) { // lined up\n" 6348 "}", 6349 OnePerLine); 6350 verifyFormat("Constructor()\n" 6351 " : aaaaa(aaaaaa),\n" 6352 " aaaaa(aaaaaa),\n" 6353 " aaaaa(aaaaaa),\n" 6354 " aaaaa(aaaaaa),\n" 6355 " aaaaa(aaaaaa) {}", 6356 OnePerLine); 6357 verifyFormat("Constructor()\n" 6358 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6359 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6360 OnePerLine); 6361 OnePerLine.BinPackParameters = false; 6362 verifyFormat( 6363 "Constructor()\n" 6364 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6365 " aaaaaaaaaaa().aaa(),\n" 6366 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6367 OnePerLine); 6368 OnePerLine.ColumnLimit = 60; 6369 verifyFormat("Constructor()\n" 6370 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6371 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6372 OnePerLine); 6373 6374 EXPECT_EQ("Constructor()\n" 6375 " : // Comment forcing unwanted break.\n" 6376 " aaaa(aaaa) {}", 6377 format("Constructor() :\n" 6378 " // Comment forcing unwanted break.\n" 6379 " aaaa(aaaa) {}")); 6380 } 6381 6382 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6383 FormatStyle Style = getLLVMStyleWithColumns(60); 6384 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6385 Style.BinPackParameters = false; 6386 6387 for (int i = 0; i < 4; ++i) { 6388 // Test all combinations of parameters that should not have an effect. 6389 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6390 Style.AllowAllArgumentsOnNextLine = i & 2; 6391 6392 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6393 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6394 verifyFormat("Constructor()\n" 6395 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6396 Style); 6397 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6398 6399 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6400 verifyFormat("Constructor()\n" 6401 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6402 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6403 Style); 6404 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6405 6406 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6407 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6408 verifyFormat("Constructor()\n" 6409 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6410 Style); 6411 6412 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6413 verifyFormat("Constructor()\n" 6414 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6415 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6416 Style); 6417 6418 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6419 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6420 verifyFormat("Constructor() :\n" 6421 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6422 Style); 6423 6424 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6425 verifyFormat("Constructor() :\n" 6426 " aaaaaaaaaaaaaaaaaa(a),\n" 6427 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6428 Style); 6429 } 6430 6431 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6432 // AllowAllConstructorInitializersOnNextLine in all 6433 // BreakConstructorInitializers modes 6434 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6435 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6436 verifyFormat("SomeClassWithALongName::Constructor(\n" 6437 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6438 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6439 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6440 Style); 6441 6442 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6443 verifyFormat("SomeClassWithALongName::Constructor(\n" 6444 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6445 " int bbbbbbbbbbbbb,\n" 6446 " int cccccccccccccccc)\n" 6447 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6448 Style); 6449 6450 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6451 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6452 verifyFormat("SomeClassWithALongName::Constructor(\n" 6453 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6454 " int bbbbbbbbbbbbb)\n" 6455 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6456 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6457 Style); 6458 6459 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6460 6461 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6462 verifyFormat("SomeClassWithALongName::Constructor(\n" 6463 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6464 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6465 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6466 Style); 6467 6468 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6469 verifyFormat("SomeClassWithALongName::Constructor(\n" 6470 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6471 " int bbbbbbbbbbbbb,\n" 6472 " int cccccccccccccccc)\n" 6473 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6474 Style); 6475 6476 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6477 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6478 verifyFormat("SomeClassWithALongName::Constructor(\n" 6479 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6480 " int bbbbbbbbbbbbb)\n" 6481 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6482 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6483 Style); 6484 6485 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6486 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6487 verifyFormat("SomeClassWithALongName::Constructor(\n" 6488 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6489 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6490 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6491 Style); 6492 6493 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6494 verifyFormat("SomeClassWithALongName::Constructor(\n" 6495 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6496 " int bbbbbbbbbbbbb,\n" 6497 " int cccccccccccccccc) :\n" 6498 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6499 Style); 6500 6501 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6502 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6503 verifyFormat("SomeClassWithALongName::Constructor(\n" 6504 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6505 " int bbbbbbbbbbbbb) :\n" 6506 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6507 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6508 Style); 6509 } 6510 6511 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6512 FormatStyle Style = getLLVMStyleWithColumns(60); 6513 Style.BinPackArguments = false; 6514 for (int i = 0; i < 4; ++i) { 6515 // Test all combinations of parameters that should not have an effect. 6516 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6517 Style.PackConstructorInitializers = 6518 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6519 6520 Style.AllowAllArgumentsOnNextLine = true; 6521 verifyFormat("void foo() {\n" 6522 " FunctionCallWithReallyLongName(\n" 6523 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6524 "}", 6525 Style); 6526 Style.AllowAllArgumentsOnNextLine = false; 6527 verifyFormat("void foo() {\n" 6528 " FunctionCallWithReallyLongName(\n" 6529 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6530 " bbbbbbbbbbbb);\n" 6531 "}", 6532 Style); 6533 6534 Style.AllowAllArgumentsOnNextLine = true; 6535 verifyFormat("void foo() {\n" 6536 " auto VariableWithReallyLongName = {\n" 6537 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6538 "}", 6539 Style); 6540 Style.AllowAllArgumentsOnNextLine = false; 6541 verifyFormat("void foo() {\n" 6542 " auto VariableWithReallyLongName = {\n" 6543 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6544 " bbbbbbbbbbbb};\n" 6545 "}", 6546 Style); 6547 } 6548 6549 // This parameter should not affect declarations. 6550 Style.BinPackParameters = false; 6551 Style.AllowAllArgumentsOnNextLine = false; 6552 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6553 verifyFormat("void FunctionCallWithReallyLongName(\n" 6554 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6555 Style); 6556 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6557 verifyFormat("void FunctionCallWithReallyLongName(\n" 6558 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6559 " int bbbbbbbbbbbb);", 6560 Style); 6561 } 6562 6563 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6564 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6565 // and BAS_Align. 6566 FormatStyle Style = getLLVMStyleWithColumns(35); 6567 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6568 "void functionDecl(int A, int B, int C);"; 6569 Style.AllowAllArgumentsOnNextLine = false; 6570 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6571 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6572 " paramC);\n" 6573 "void functionDecl(int A, int B,\n" 6574 " int C);"), 6575 format(Input, Style)); 6576 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6577 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6578 " paramC);\n" 6579 "void functionDecl(int A, int B,\n" 6580 " int C);"), 6581 format(Input, Style)); 6582 // However, BAS_AlwaysBreak should take precedence over 6583 // AllowAllArgumentsOnNextLine. 6584 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6585 EXPECT_EQ(StringRef("functionCall(\n" 6586 " paramA, paramB, paramC);\n" 6587 "void functionDecl(\n" 6588 " int A, int B, int C);"), 6589 format(Input, Style)); 6590 6591 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6592 // first argument. 6593 Style.AllowAllArgumentsOnNextLine = true; 6594 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6595 EXPECT_EQ(StringRef("functionCall(\n" 6596 " paramA, paramB, paramC);\n" 6597 "void functionDecl(\n" 6598 " int A, int B, int C);"), 6599 format(Input, Style)); 6600 // It wouldn't fit on one line with aligned parameters so this setting 6601 // doesn't change anything for BAS_Align. 6602 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6603 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6604 " paramC);\n" 6605 "void functionDecl(int A, int B,\n" 6606 " int C);"), 6607 format(Input, Style)); 6608 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6609 EXPECT_EQ(StringRef("functionCall(\n" 6610 " paramA, paramB, paramC);\n" 6611 "void functionDecl(\n" 6612 " int A, int B, int C);"), 6613 format(Input, Style)); 6614 } 6615 6616 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6617 FormatStyle Style = getLLVMStyle(); 6618 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6619 6620 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6621 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6622 getStyleWithColumns(Style, 45)); 6623 verifyFormat("Constructor() :\n" 6624 " Initializer(FitsOnTheLine) {}", 6625 getStyleWithColumns(Style, 44)); 6626 verifyFormat("Constructor() :\n" 6627 " Initializer(FitsOnTheLine) {}", 6628 getStyleWithColumns(Style, 43)); 6629 6630 verifyFormat("template <typename T>\n" 6631 "Constructor() : Initializer(FitsOnTheLine) {}", 6632 getStyleWithColumns(Style, 50)); 6633 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6634 verifyFormat( 6635 "SomeClass::Constructor() :\n" 6636 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6637 Style); 6638 6639 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 6640 verifyFormat( 6641 "SomeClass::Constructor() :\n" 6642 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6643 Style); 6644 6645 verifyFormat( 6646 "SomeClass::Constructor() :\n" 6647 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6648 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6649 Style); 6650 verifyFormat( 6651 "SomeClass::Constructor() :\n" 6652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6653 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6654 Style); 6655 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6656 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6657 " aaaaaaaaaa(aaaaaa) {}", 6658 Style); 6659 6660 verifyFormat("Constructor() :\n" 6661 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6662 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6663 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6664 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6665 Style); 6666 6667 verifyFormat("Constructor() :\n" 6668 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6669 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6670 Style); 6671 6672 verifyFormat("Constructor(int Parameter = 0) :\n" 6673 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6674 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6675 Style); 6676 verifyFormat("Constructor() :\n" 6677 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6678 "}", 6679 getStyleWithColumns(Style, 60)); 6680 verifyFormat("Constructor() :\n" 6681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6682 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6683 Style); 6684 6685 // Here a line could be saved by splitting the second initializer onto two 6686 // lines, but that is not desirable. 6687 verifyFormat("Constructor() :\n" 6688 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6689 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6690 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6691 Style); 6692 6693 FormatStyle OnePerLine = Style; 6694 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6695 verifyFormat("SomeClass::Constructor() :\n" 6696 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6697 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6698 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6699 OnePerLine); 6700 verifyFormat("SomeClass::Constructor() :\n" 6701 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6702 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6703 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6704 OnePerLine); 6705 verifyFormat("MyClass::MyClass(int var) :\n" 6706 " some_var_(var), // 4 space indent\n" 6707 " some_other_var_(var + 1) { // lined up\n" 6708 "}", 6709 OnePerLine); 6710 verifyFormat("Constructor() :\n" 6711 " aaaaa(aaaaaa),\n" 6712 " aaaaa(aaaaaa),\n" 6713 " aaaaa(aaaaaa),\n" 6714 " aaaaa(aaaaaa),\n" 6715 " aaaaa(aaaaaa) {}", 6716 OnePerLine); 6717 verifyFormat("Constructor() :\n" 6718 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6719 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6720 OnePerLine); 6721 OnePerLine.BinPackParameters = false; 6722 verifyFormat("Constructor() :\n" 6723 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6724 " aaaaaaaaaaa().aaa(),\n" 6725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6726 OnePerLine); 6727 OnePerLine.ColumnLimit = 60; 6728 verifyFormat("Constructor() :\n" 6729 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6730 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6731 OnePerLine); 6732 6733 EXPECT_EQ("Constructor() :\n" 6734 " // Comment forcing unwanted break.\n" 6735 " aaaa(aaaa) {}", 6736 format("Constructor() :\n" 6737 " // Comment forcing unwanted break.\n" 6738 " aaaa(aaaa) {}", 6739 Style)); 6740 6741 Style.ColumnLimit = 0; 6742 verifyFormat("SomeClass::Constructor() :\n" 6743 " a(a) {}", 6744 Style); 6745 verifyFormat("SomeClass::Constructor() noexcept :\n" 6746 " a(a) {}", 6747 Style); 6748 verifyFormat("SomeClass::Constructor() :\n" 6749 " a(a), b(b), c(c) {}", 6750 Style); 6751 verifyFormat("SomeClass::Constructor() :\n" 6752 " a(a) {\n" 6753 " foo();\n" 6754 " bar();\n" 6755 "}", 6756 Style); 6757 6758 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6759 verifyFormat("SomeClass::Constructor() :\n" 6760 " a(a), b(b), c(c) {\n" 6761 "}", 6762 Style); 6763 verifyFormat("SomeClass::Constructor() :\n" 6764 " a(a) {\n" 6765 "}", 6766 Style); 6767 6768 Style.ColumnLimit = 80; 6769 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6770 Style.ConstructorInitializerIndentWidth = 2; 6771 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6772 verifyFormat("SomeClass::Constructor() :\n" 6773 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6774 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6775 Style); 6776 6777 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6778 // well 6779 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6780 verifyFormat( 6781 "class SomeClass\n" 6782 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6783 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6784 Style); 6785 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6786 verifyFormat( 6787 "class SomeClass\n" 6788 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6789 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6790 Style); 6791 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6792 verifyFormat( 6793 "class SomeClass :\n" 6794 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6795 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6796 Style); 6797 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6798 verifyFormat( 6799 "class SomeClass\n" 6800 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6801 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6802 Style); 6803 } 6804 6805 #ifndef EXPENSIVE_CHECKS 6806 // Expensive checks enables libstdc++ checking which includes validating the 6807 // state of ranges used in std::priority_queue - this blows out the 6808 // runtime/scalability of the function and makes this test unacceptably slow. 6809 TEST_F(FormatTest, MemoizationTests) { 6810 // This breaks if the memoization lookup does not take \c Indent and 6811 // \c LastSpace into account. 6812 verifyFormat( 6813 "extern CFRunLoopTimerRef\n" 6814 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6815 " CFTimeInterval interval, CFOptionFlags flags,\n" 6816 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6817 " CFRunLoopTimerContext *context) {}"); 6818 6819 // Deep nesting somewhat works around our memoization. 6820 verifyFormat( 6821 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6822 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6823 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6824 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6825 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6826 getLLVMStyleWithColumns(65)); 6827 verifyFormat( 6828 "aaaaa(\n" 6829 " aaaaa,\n" 6830 " aaaaa(\n" 6831 " aaaaa,\n" 6832 " aaaaa(\n" 6833 " aaaaa,\n" 6834 " aaaaa(\n" 6835 " aaaaa,\n" 6836 " aaaaa(\n" 6837 " aaaaa,\n" 6838 " aaaaa(\n" 6839 " aaaaa,\n" 6840 " aaaaa(\n" 6841 " aaaaa,\n" 6842 " aaaaa(\n" 6843 " aaaaa,\n" 6844 " aaaaa(\n" 6845 " aaaaa,\n" 6846 " aaaaa(\n" 6847 " aaaaa,\n" 6848 " aaaaa(\n" 6849 " aaaaa,\n" 6850 " aaaaa(\n" 6851 " aaaaa,\n" 6852 " aaaaa))))))))))));", 6853 getLLVMStyleWithColumns(65)); 6854 verifyFormat( 6855 "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" 6856 " a),\n" 6857 " a),\n" 6858 " a),\n" 6859 " a),\n" 6860 " a),\n" 6861 " a),\n" 6862 " a),\n" 6863 " a),\n" 6864 " a),\n" 6865 " a),\n" 6866 " a),\n" 6867 " a),\n" 6868 " a),\n" 6869 " a),\n" 6870 " a),\n" 6871 " a),\n" 6872 " a)", 6873 getLLVMStyleWithColumns(65)); 6874 6875 // This test takes VERY long when memoization is broken. 6876 FormatStyle OnePerLine = getLLVMStyle(); 6877 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6878 OnePerLine.BinPackParameters = false; 6879 std::string input = "Constructor()\n" 6880 " : aaaa(a,\n"; 6881 for (unsigned i = 0, e = 80; i != e; ++i) { 6882 input += " a,\n"; 6883 } 6884 input += " a) {}"; 6885 verifyFormat(input, OnePerLine); 6886 } 6887 #endif 6888 6889 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6890 verifyFormat( 6891 "void f() {\n" 6892 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6893 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6894 " f();\n" 6895 "}"); 6896 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6897 " Intervals[i - 1].getRange().getLast()) {\n}"); 6898 } 6899 6900 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6901 // Principially, we break function declarations in a certain order: 6902 // 1) break amongst arguments. 6903 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6904 " Cccccccccccccc cccccccccccccc);"); 6905 verifyFormat("template <class TemplateIt>\n" 6906 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6907 " TemplateIt *stop) {}"); 6908 6909 // 2) break after return type. 6910 verifyFormat( 6911 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6912 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6913 getGoogleStyle()); 6914 6915 // 3) break after (. 6916 verifyFormat( 6917 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6918 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6919 getGoogleStyle()); 6920 6921 // 4) break before after nested name specifiers. 6922 verifyFormat( 6923 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6924 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6925 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6926 getGoogleStyle()); 6927 6928 // However, there are exceptions, if a sufficient amount of lines can be 6929 // saved. 6930 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6931 // more adjusting. 6932 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6933 " Cccccccccccccc cccccccccc,\n" 6934 " Cccccccccccccc cccccccccc,\n" 6935 " Cccccccccccccc cccccccccc,\n" 6936 " Cccccccccccccc cccccccccc);"); 6937 verifyFormat( 6938 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6939 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6940 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6941 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 6942 getGoogleStyle()); 6943 verifyFormat( 6944 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6945 " Cccccccccccccc cccccccccc,\n" 6946 " Cccccccccccccc cccccccccc,\n" 6947 " Cccccccccccccc cccccccccc,\n" 6948 " Cccccccccccccc cccccccccc,\n" 6949 " Cccccccccccccc cccccccccc,\n" 6950 " Cccccccccccccc cccccccccc);"); 6951 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6952 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6953 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6954 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6955 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 6956 6957 // Break after multi-line parameters. 6958 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6959 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6961 " bbbb bbbb);"); 6962 verifyFormat("void SomeLoooooooooooongFunction(\n" 6963 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6964 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6965 " int bbbbbbbbbbbbb);"); 6966 6967 // Treat overloaded operators like other functions. 6968 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6969 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 6970 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6971 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 6972 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6973 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 6974 verifyGoogleFormat( 6975 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 6976 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6977 verifyGoogleFormat( 6978 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 6979 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6980 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6981 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6982 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 6983 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6984 verifyGoogleFormat( 6985 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 6986 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6987 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 6988 verifyGoogleFormat("template <typename T>\n" 6989 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6990 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 6991 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 6992 6993 FormatStyle Style = getLLVMStyle(); 6994 Style.PointerAlignment = FormatStyle::PAS_Left; 6995 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6996 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 6997 Style); 6998 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 6999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7000 Style); 7001 } 7002 7003 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 7004 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 7005 // Prefer keeping `::` followed by `operator` together. 7006 EXPECT_EQ("const aaaa::bbbbbbb &\n" 7007 "ccccccccc::operator++() {\n" 7008 " stuff();\n" 7009 "}", 7010 format("const aaaa::bbbbbbb\n" 7011 "&ccccccccc::operator++() { stuff(); }", 7012 getLLVMStyleWithColumns(40))); 7013 } 7014 7015 TEST_F(FormatTest, TrailingReturnType) { 7016 verifyFormat("auto foo() -> int;\n"); 7017 // correct trailing return type spacing 7018 verifyFormat("auto operator->() -> int;\n"); 7019 verifyFormat("auto operator++(int) -> int;\n"); 7020 7021 verifyFormat("struct S {\n" 7022 " auto bar() const -> int;\n" 7023 "};"); 7024 verifyFormat("template <size_t Order, typename T>\n" 7025 "auto load_img(const std::string &filename)\n" 7026 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 7027 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 7028 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 7029 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 7030 verifyFormat("template <typename T>\n" 7031 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 7032 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 7033 7034 // Not trailing return types. 7035 verifyFormat("void f() { auto a = b->c(); }"); 7036 verifyFormat("auto a = p->foo();"); 7037 verifyFormat("int a = p->foo();"); 7038 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); 7039 } 7040 7041 TEST_F(FormatTest, DeductionGuides) { 7042 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 7043 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 7044 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 7045 verifyFormat( 7046 "template <class... T>\n" 7047 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 7048 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 7049 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 7050 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 7051 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 7052 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 7053 verifyFormat("template <class T> x() -> x<1>;"); 7054 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 7055 7056 // Ensure not deduction guides. 7057 verifyFormat("c()->f<int>();"); 7058 verifyFormat("x()->foo<1>;"); 7059 verifyFormat("x = p->foo<3>();"); 7060 verifyFormat("x()->x<1>();"); 7061 verifyFormat("x()->x<1>;"); 7062 } 7063 7064 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 7065 // Avoid breaking before trailing 'const' or other trailing annotations, if 7066 // they are not function-like. 7067 FormatStyle Style = getGoogleStyleWithColumns(47); 7068 verifyFormat("void someLongFunction(\n" 7069 " int someLoooooooooooooongParameter) const {\n}", 7070 getLLVMStyleWithColumns(47)); 7071 verifyFormat("LoooooongReturnType\n" 7072 "someLoooooooongFunction() const {}", 7073 getLLVMStyleWithColumns(47)); 7074 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 7075 " const {}", 7076 Style); 7077 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7078 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 7079 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7080 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 7081 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7082 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 7083 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 7084 " aaaaaaaaaaa aaaaa) const override;"); 7085 verifyGoogleFormat( 7086 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7087 " const override;"); 7088 7089 // Even if the first parameter has to be wrapped. 7090 verifyFormat("void someLongFunction(\n" 7091 " int someLongParameter) const {}", 7092 getLLVMStyleWithColumns(46)); 7093 verifyFormat("void someLongFunction(\n" 7094 " int someLongParameter) const {}", 7095 Style); 7096 verifyFormat("void someLongFunction(\n" 7097 " int someLongParameter) override {}", 7098 Style); 7099 verifyFormat("void someLongFunction(\n" 7100 " int someLongParameter) OVERRIDE {}", 7101 Style); 7102 verifyFormat("void someLongFunction(\n" 7103 " int someLongParameter) final {}", 7104 Style); 7105 verifyFormat("void someLongFunction(\n" 7106 " int someLongParameter) FINAL {}", 7107 Style); 7108 verifyFormat("void someLongFunction(\n" 7109 " int parameter) const override {}", 7110 Style); 7111 7112 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 7113 verifyFormat("void someLongFunction(\n" 7114 " int someLongParameter) const\n" 7115 "{\n" 7116 "}", 7117 Style); 7118 7119 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 7120 verifyFormat("void someLongFunction(\n" 7121 " int someLongParameter) const\n" 7122 " {\n" 7123 " }", 7124 Style); 7125 7126 // Unless these are unknown annotations. 7127 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 7128 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7129 " LONG_AND_UGLY_ANNOTATION;"); 7130 7131 // Breaking before function-like trailing annotations is fine to keep them 7132 // close to their arguments. 7133 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7134 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7135 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7136 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7137 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7138 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 7139 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 7140 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 7141 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 7142 7143 verifyFormat( 7144 "void aaaaaaaaaaaaaaaaaa()\n" 7145 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 7146 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 7147 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7148 " __attribute__((unused));"); 7149 verifyGoogleFormat( 7150 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7151 " GUARDED_BY(aaaaaaaaaaaa);"); 7152 verifyGoogleFormat( 7153 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7154 " GUARDED_BY(aaaaaaaaaaaa);"); 7155 verifyGoogleFormat( 7156 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7157 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7158 verifyGoogleFormat( 7159 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7160 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7161 } 7162 7163 TEST_F(FormatTest, FunctionAnnotations) { 7164 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7165 "int OldFunction(const string ¶meter) {}"); 7166 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7167 "string OldFunction(const string ¶meter) {}"); 7168 verifyFormat("template <typename T>\n" 7169 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7170 "string OldFunction(const string ¶meter) {}"); 7171 7172 // Not function annotations. 7173 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7174 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 7175 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 7176 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 7177 verifyFormat("MACRO(abc).function() // wrap\n" 7178 " << abc;"); 7179 verifyFormat("MACRO(abc)->function() // wrap\n" 7180 " << abc;"); 7181 verifyFormat("MACRO(abc)::function() // wrap\n" 7182 " << abc;"); 7183 } 7184 7185 TEST_F(FormatTest, BreaksDesireably) { 7186 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7187 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7188 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 7189 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 7191 "}"); 7192 7193 verifyFormat( 7194 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7195 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7196 7197 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7199 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7200 7201 verifyFormat( 7202 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7203 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7205 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7206 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 7207 7208 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7209 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7210 7211 verifyFormat( 7212 "void f() {\n" 7213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7214 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7215 "}"); 7216 verifyFormat( 7217 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7218 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7219 verifyFormat( 7220 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7222 verifyFormat( 7223 "aaaaaa(aaa,\n" 7224 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7225 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7226 " aaaa);"); 7227 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7228 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7229 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7230 7231 // Indent consistently independent of call expression and unary operator. 7232 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7233 " dddddddddddddddddddddddddddddd));"); 7234 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7235 " dddddddddddddddddddddddddddddd));"); 7236 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 7237 " dddddddddddddddddddddddddddddd));"); 7238 7239 // This test case breaks on an incorrect memoization, i.e. an optimization not 7240 // taking into account the StopAt value. 7241 verifyFormat( 7242 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7243 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7244 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7245 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7246 7247 verifyFormat("{\n {\n {\n" 7248 " Annotation.SpaceRequiredBefore =\n" 7249 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 7250 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 7251 " }\n }\n}"); 7252 7253 // Break on an outer level if there was a break on an inner level. 7254 EXPECT_EQ("f(g(h(a, // comment\n" 7255 " b, c),\n" 7256 " d, e),\n" 7257 " x, y);", 7258 format("f(g(h(a, // comment\n" 7259 " b, c), d, e), x, y);")); 7260 7261 // Prefer breaking similar line breaks. 7262 verifyFormat( 7263 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 7264 " NSTrackingMouseEnteredAndExited |\n" 7265 " NSTrackingActiveAlways;"); 7266 } 7267 7268 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 7269 FormatStyle NoBinPacking = getGoogleStyle(); 7270 NoBinPacking.BinPackParameters = false; 7271 NoBinPacking.BinPackArguments = true; 7272 verifyFormat("void f() {\n" 7273 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 7274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7275 "}", 7276 NoBinPacking); 7277 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 7278 " int aaaaaaaaaaaaaaaaaaaa,\n" 7279 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7280 NoBinPacking); 7281 7282 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7283 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7284 " vector<int> bbbbbbbbbbbbbbb);", 7285 NoBinPacking); 7286 // FIXME: This behavior difference is probably not wanted. However, currently 7287 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 7288 // template arguments from BreakBeforeParameter being set because of the 7289 // one-per-line formatting. 7290 verifyFormat( 7291 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7292 " aaaaaaaaaa> aaaaaaaaaa);", 7293 NoBinPacking); 7294 verifyFormat( 7295 "void fffffffffff(\n" 7296 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 7297 " aaaaaaaaaa);"); 7298 } 7299 7300 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 7301 FormatStyle NoBinPacking = getGoogleStyle(); 7302 NoBinPacking.BinPackParameters = false; 7303 NoBinPacking.BinPackArguments = false; 7304 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 7305 " aaaaaaaaaaaaaaaaaaaa,\n" 7306 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 7307 NoBinPacking); 7308 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 7309 " aaaaaaaaaaaaa,\n" 7310 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 7311 NoBinPacking); 7312 verifyFormat( 7313 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7314 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7315 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7316 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 7318 NoBinPacking); 7319 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7320 " .aaaaaaaaaaaaaaaaaa();", 7321 NoBinPacking); 7322 verifyFormat("void f() {\n" 7323 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7324 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 7325 "}", 7326 NoBinPacking); 7327 7328 verifyFormat( 7329 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7330 " aaaaaaaaaaaa,\n" 7331 " aaaaaaaaaaaa);", 7332 NoBinPacking); 7333 verifyFormat( 7334 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 7335 " ddddddddddddddddddddddddddddd),\n" 7336 " test);", 7337 NoBinPacking); 7338 7339 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7340 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 7341 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 7342 " aaaaaaaaaaaaaaaaaa;", 7343 NoBinPacking); 7344 verifyFormat("a(\"a\"\n" 7345 " \"a\",\n" 7346 " a);"); 7347 7348 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7349 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7350 " aaaaaaaaa,\n" 7351 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7352 NoBinPacking); 7353 verifyFormat( 7354 "void f() {\n" 7355 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7356 " .aaaaaaa();\n" 7357 "}", 7358 NoBinPacking); 7359 verifyFormat( 7360 "template <class SomeType, class SomeOtherType>\n" 7361 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7362 NoBinPacking); 7363 } 7364 7365 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7366 FormatStyle Style = getLLVMStyleWithColumns(15); 7367 Style.ExperimentalAutoDetectBinPacking = true; 7368 EXPECT_EQ("aaa(aaaa,\n" 7369 " aaaa,\n" 7370 " aaaa);\n" 7371 "aaa(aaaa,\n" 7372 " aaaa,\n" 7373 " aaaa);", 7374 format("aaa(aaaa,\n" // one-per-line 7375 " aaaa,\n" 7376 " aaaa );\n" 7377 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7378 Style)); 7379 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7380 " aaaa);\n" 7381 "aaa(aaaa, aaaa,\n" 7382 " aaaa);", 7383 format("aaa(aaaa, aaaa,\n" // bin-packed 7384 " aaaa );\n" 7385 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7386 Style)); 7387 } 7388 7389 TEST_F(FormatTest, FormatsBuilderPattern) { 7390 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7391 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7392 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7393 " .StartsWith(\".init\", ORDER_INIT)\n" 7394 " .StartsWith(\".fini\", ORDER_FINI)\n" 7395 " .StartsWith(\".hash\", ORDER_HASH)\n" 7396 " .Default(ORDER_TEXT);\n"); 7397 7398 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7399 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7400 verifyFormat("aaaaaaa->aaaaaaa\n" 7401 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7402 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7403 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7404 verifyFormat( 7405 "aaaaaaa->aaaaaaa\n" 7406 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7407 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7408 verifyFormat( 7409 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7410 " aaaaaaaaaaaaaa);"); 7411 verifyFormat( 7412 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7413 " aaaaaa->aaaaaaaaaaaa()\n" 7414 " ->aaaaaaaaaaaaaaaa(\n" 7415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7416 " ->aaaaaaaaaaaaaaaaa();"); 7417 verifyGoogleFormat( 7418 "void f() {\n" 7419 " someo->Add((new util::filetools::Handler(dir))\n" 7420 " ->OnEvent1(NewPermanentCallback(\n" 7421 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7422 " ->OnEvent2(NewPermanentCallback(\n" 7423 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7424 " ->OnEvent3(NewPermanentCallback(\n" 7425 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7426 " ->OnEvent5(NewPermanentCallback(\n" 7427 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7428 " ->OnEvent6(NewPermanentCallback(\n" 7429 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7430 "}"); 7431 7432 verifyFormat( 7433 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7434 verifyFormat("aaaaaaaaaaaaaaa()\n" 7435 " .aaaaaaaaaaaaaaa()\n" 7436 " .aaaaaaaaaaaaaaa()\n" 7437 " .aaaaaaaaaaaaaaa()\n" 7438 " .aaaaaaaaaaaaaaa();"); 7439 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7440 " .aaaaaaaaaaaaaaa()\n" 7441 " .aaaaaaaaaaaaaaa()\n" 7442 " .aaaaaaaaaaaaaaa();"); 7443 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7444 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7445 " .aaaaaaaaaaaaaaa();"); 7446 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7447 " ->aaaaaaaaaaaaaae(0)\n" 7448 " ->aaaaaaaaaaaaaaa();"); 7449 7450 // Don't linewrap after very short segments. 7451 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7452 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7453 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7454 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7455 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7456 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7457 verifyFormat("aaa()\n" 7458 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7459 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7460 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7461 7462 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7463 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7464 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7465 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7466 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7467 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7468 7469 // Prefer not to break after empty parentheses. 7470 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7471 " First->LastNewlineOffset);"); 7472 7473 // Prefer not to create "hanging" indents. 7474 verifyFormat( 7475 "return !soooooooooooooome_map\n" 7476 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7477 " .second;"); 7478 verifyFormat( 7479 "return aaaaaaaaaaaaaaaa\n" 7480 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7481 " .aaaa(aaaaaaaaaaaaaa);"); 7482 // No hanging indent here. 7483 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7484 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7485 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7487 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7488 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7489 getLLVMStyleWithColumns(60)); 7490 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7491 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7492 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7493 getLLVMStyleWithColumns(59)); 7494 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7496 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7497 7498 // Dont break if only closing statements before member call 7499 verifyFormat("test() {\n" 7500 " ([]() -> {\n" 7501 " int b = 32;\n" 7502 " return 3;\n" 7503 " }).foo();\n" 7504 "}"); 7505 verifyFormat("test() {\n" 7506 " (\n" 7507 " []() -> {\n" 7508 " int b = 32;\n" 7509 " return 3;\n" 7510 " },\n" 7511 " foo, bar)\n" 7512 " .foo();\n" 7513 "}"); 7514 verifyFormat("test() {\n" 7515 " ([]() -> {\n" 7516 " int b = 32;\n" 7517 " return 3;\n" 7518 " })\n" 7519 " .foo()\n" 7520 " .bar();\n" 7521 "}"); 7522 verifyFormat("test() {\n" 7523 " ([]() -> {\n" 7524 " int b = 32;\n" 7525 " return 3;\n" 7526 " })\n" 7527 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7528 " \"bbbb\");\n" 7529 "}", 7530 getLLVMStyleWithColumns(30)); 7531 } 7532 7533 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7534 verifyFormat( 7535 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7536 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7537 verifyFormat( 7538 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7539 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7540 7541 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7542 " ccccccccccccccccccccccccc) {\n}"); 7543 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7544 " ccccccccccccccccccccccccc) {\n}"); 7545 7546 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7547 " ccccccccccccccccccccccccc) {\n}"); 7548 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7549 " ccccccccccccccccccccccccc) {\n}"); 7550 7551 verifyFormat( 7552 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7553 " ccccccccccccccccccccccccc) {\n}"); 7554 verifyFormat( 7555 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7556 " ccccccccccccccccccccccccc) {\n}"); 7557 7558 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7559 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7560 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7561 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7562 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7563 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7564 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7565 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7566 7567 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7568 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7569 " aaaaaaaaaaaaaaa != aa) {\n}"); 7570 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7571 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7572 " aaaaaaaaaaaaaaa != aa) {\n}"); 7573 } 7574 7575 TEST_F(FormatTest, BreaksAfterAssignments) { 7576 verifyFormat( 7577 "unsigned Cost =\n" 7578 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7579 " SI->getPointerAddressSpaceee());\n"); 7580 verifyFormat( 7581 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7582 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7583 7584 verifyFormat( 7585 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7586 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7587 verifyFormat("unsigned OriginalStartColumn =\n" 7588 " SourceMgr.getSpellingColumnNumber(\n" 7589 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7590 " 1;"); 7591 } 7592 7593 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7594 FormatStyle Style = getLLVMStyle(); 7595 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7596 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7597 Style); 7598 7599 Style.PenaltyBreakAssignment = 20; 7600 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7601 " cccccccccccccccccccccccccc;", 7602 Style); 7603 } 7604 7605 TEST_F(FormatTest, AlignsAfterAssignments) { 7606 verifyFormat( 7607 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7608 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7609 verifyFormat( 7610 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7611 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7612 verifyFormat( 7613 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7614 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7615 verifyFormat( 7616 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7617 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7618 verifyFormat( 7619 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7620 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7621 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7622 } 7623 7624 TEST_F(FormatTest, AlignsAfterReturn) { 7625 verifyFormat( 7626 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7627 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7628 verifyFormat( 7629 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7630 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7631 verifyFormat( 7632 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7633 " aaaaaaaaaaaaaaaaaaaaaa();"); 7634 verifyFormat( 7635 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7636 " aaaaaaaaaaaaaaaaaaaaaa());"); 7637 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7639 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7640 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7642 verifyFormat("return\n" 7643 " // true if code is one of a or b.\n" 7644 " code == a || code == b;"); 7645 } 7646 7647 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7648 verifyFormat( 7649 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7650 " aaaaaaaaa aaaaaaa) {}"); 7651 verifyFormat( 7652 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7653 " aaaaaaaaaaa aaaaaaaaa);"); 7654 verifyFormat( 7655 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7656 " aaaaaaaaaaaaaaaaaaaaa));"); 7657 FormatStyle Style = getLLVMStyle(); 7658 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7659 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7660 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7661 Style); 7662 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7663 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7664 Style); 7665 verifyFormat("SomeLongVariableName->someFunction(\n" 7666 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7667 Style); 7668 verifyFormat( 7669 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7670 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7671 Style); 7672 verifyFormat( 7673 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7674 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7675 Style); 7676 verifyFormat( 7677 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7678 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7679 Style); 7680 7681 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7682 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7683 " b));", 7684 Style); 7685 7686 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7687 Style.BinPackArguments = false; 7688 Style.BinPackParameters = false; 7689 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7690 " aaaaaaaaaaa aaaaaaaa,\n" 7691 " aaaaaaaaa aaaaaaa,\n" 7692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7693 Style); 7694 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7695 " aaaaaaaaaaa aaaaaaaaa,\n" 7696 " aaaaaaaaaaa aaaaaaaaa,\n" 7697 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7698 Style); 7699 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7700 " aaaaaaaaaaaaaaa,\n" 7701 " aaaaaaaaaaaaaaaaaaaaa,\n" 7702 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7703 Style); 7704 verifyFormat( 7705 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7706 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7707 Style); 7708 verifyFormat( 7709 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7710 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7711 Style); 7712 verifyFormat( 7713 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7714 " aaaaaaaaaaaaaaaaaaaaa(\n" 7715 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7716 " aaaaaaaaaaaaaaaa);", 7717 Style); 7718 verifyFormat( 7719 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7720 " aaaaaaaaaaaaaaaaaaaaa(\n" 7721 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7722 " aaaaaaaaaaaaaaaa);", 7723 Style); 7724 } 7725 7726 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7727 FormatStyle Style = getLLVMStyleWithColumns(40); 7728 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7729 " bbbbbbbbbbbbbbbbbbbbbb);", 7730 Style); 7731 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7732 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7733 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7734 " bbbbbbbbbbbbbbbbbbbbbb);", 7735 Style); 7736 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7737 Style.AlignOperands = FormatStyle::OAS_Align; 7738 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7739 " bbbbbbbbbbbbbbbbbbbbbb);", 7740 Style); 7741 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7742 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7743 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7744 " bbbbbbbbbbbbbbbbbbbbbb);", 7745 Style); 7746 } 7747 7748 TEST_F(FormatTest, BreaksConditionalExpressions) { 7749 verifyFormat( 7750 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7751 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7752 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7753 verifyFormat( 7754 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7755 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7756 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7757 verifyFormat( 7758 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7759 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7760 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7761 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7762 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7763 verifyFormat( 7764 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7765 " : aaaaaaaaaaaaa);"); 7766 verifyFormat( 7767 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7768 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7769 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7770 " aaaaaaaaaaaaa);"); 7771 verifyFormat( 7772 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7773 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7774 " aaaaaaaaaaaaa);"); 7775 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7776 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7778 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7779 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7780 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7781 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7782 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7784 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7786 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7787 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7789 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7791 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7792 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7793 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7794 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7795 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7797 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7798 " : aaaaaaaaaaaaaaaa;"); 7799 verifyFormat( 7800 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7801 " ? aaaaaaaaaaaaaaa\n" 7802 " : aaaaaaaaaaaaaaa;"); 7803 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7804 " aaaaaaaaa\n" 7805 " ? b\n" 7806 " : c);"); 7807 verifyFormat("return aaaa == bbbb\n" 7808 " // comment\n" 7809 " ? aaaa\n" 7810 " : bbbb;"); 7811 verifyFormat("unsigned Indent =\n" 7812 " format(TheLine.First,\n" 7813 " IndentForLevel[TheLine.Level] >= 0\n" 7814 " ? IndentForLevel[TheLine.Level]\n" 7815 " : TheLine * 2,\n" 7816 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7817 getLLVMStyleWithColumns(60)); 7818 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7819 " ? aaaaaaaaaaaaaaa\n" 7820 " : bbbbbbbbbbbbbbb //\n" 7821 " ? ccccccccccccccc\n" 7822 " : ddddddddddddddd;"); 7823 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7824 " ? aaaaaaaaaaaaaaa\n" 7825 " : (bbbbbbbbbbbbbbb //\n" 7826 " ? ccccccccccccccc\n" 7827 " : ddddddddddddddd);"); 7828 verifyFormat( 7829 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7830 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7831 " aaaaaaaaaaaaaaaaaaaaa +\n" 7832 " aaaaaaaaaaaaaaaaaaaaa\n" 7833 " : aaaaaaaaaa;"); 7834 verifyFormat( 7835 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7836 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7837 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7838 7839 FormatStyle NoBinPacking = getLLVMStyle(); 7840 NoBinPacking.BinPackArguments = false; 7841 verifyFormat( 7842 "void f() {\n" 7843 " g(aaa,\n" 7844 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7845 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7846 " ? aaaaaaaaaaaaaaa\n" 7847 " : aaaaaaaaaaaaaaa);\n" 7848 "}", 7849 NoBinPacking); 7850 verifyFormat( 7851 "void f() {\n" 7852 " g(aaa,\n" 7853 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7855 " ?: aaaaaaaaaaaaaaa);\n" 7856 "}", 7857 NoBinPacking); 7858 7859 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7860 " // comment.\n" 7861 " ccccccccccccccccccccccccccccccccccccccc\n" 7862 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7863 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7864 7865 // Assignments in conditional expressions. Apparently not uncommon :-(. 7866 verifyFormat("return a != b\n" 7867 " // comment\n" 7868 " ? a = b\n" 7869 " : a = b;"); 7870 verifyFormat("return a != b\n" 7871 " // comment\n" 7872 " ? a = a != b\n" 7873 " // comment\n" 7874 " ? a = b\n" 7875 " : a\n" 7876 " : a;\n"); 7877 verifyFormat("return a != b\n" 7878 " // comment\n" 7879 " ? a\n" 7880 " : a = a != b\n" 7881 " // comment\n" 7882 " ? a = b\n" 7883 " : a;"); 7884 7885 // Chained conditionals 7886 FormatStyle Style = getLLVMStyleWithColumns(70); 7887 Style.AlignOperands = FormatStyle::OAS_Align; 7888 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7889 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7890 " : 3333333333333333;", 7891 Style); 7892 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7893 " : bbbbbbbbbb ? 2222222222222222\n" 7894 " : 3333333333333333;", 7895 Style); 7896 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7897 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7898 " : 3333333333333333;", 7899 Style); 7900 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7901 " : bbbbbbbbbbbbbb ? 222222\n" 7902 " : 333333;", 7903 Style); 7904 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7905 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7906 " : cccccccccccccc ? 3333333333333333\n" 7907 " : 4444444444444444;", 7908 Style); 7909 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7910 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7911 " : 3333333333333333;", 7912 Style); 7913 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7914 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7915 " : (aaa ? bbb : ccc);", 7916 Style); 7917 verifyFormat( 7918 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7919 " : cccccccccccccccccc)\n" 7920 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7921 " : 3333333333333333;", 7922 Style); 7923 verifyFormat( 7924 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7925 " : cccccccccccccccccc)\n" 7926 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7927 " : 3333333333333333;", 7928 Style); 7929 verifyFormat( 7930 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7931 " : dddddddddddddddddd)\n" 7932 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7933 " : 3333333333333333;", 7934 Style); 7935 verifyFormat( 7936 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7937 " : dddddddddddddddddd)\n" 7938 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7939 " : 3333333333333333;", 7940 Style); 7941 verifyFormat( 7942 "return aaaaaaaaa ? 1111111111111111\n" 7943 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7944 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7945 " : dddddddddddddddddd)\n", 7946 Style); 7947 verifyFormat( 7948 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7949 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7950 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7951 " : cccccccccccccccccc);", 7952 Style); 7953 verifyFormat( 7954 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7955 " : ccccccccccccccc ? dddddddddddddddddd\n" 7956 " : eeeeeeeeeeeeeeeeee)\n" 7957 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7958 " : 3333333333333333;", 7959 Style); 7960 verifyFormat( 7961 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7962 " : ccccccccccccccc ? dddddddddddddddddd\n" 7963 " : eeeeeeeeeeeeeeeeee)\n" 7964 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7965 " : 3333333333333333;", 7966 Style); 7967 verifyFormat( 7968 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7969 " : cccccccccccc ? dddddddddddddddddd\n" 7970 " : eeeeeeeeeeeeeeeeee)\n" 7971 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7972 " : 3333333333333333;", 7973 Style); 7974 verifyFormat( 7975 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7976 " : cccccccccccccccccc\n" 7977 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7978 " : 3333333333333333;", 7979 Style); 7980 verifyFormat( 7981 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7982 " : cccccccccccccccc ? dddddddddddddddddd\n" 7983 " : eeeeeeeeeeeeeeeeee\n" 7984 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7985 " : 3333333333333333;", 7986 Style); 7987 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 7988 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7989 " : cccccccccccccccccc ? dddddddddddddddddd\n" 7990 " : eeeeeeeeeeeeeeeeee)\n" 7991 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7992 " : 3333333333333333;", 7993 Style); 7994 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7995 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7996 " : cccccccccccccccc ? dddddddddddddddddd\n" 7997 " : eeeeeeeeeeeeeeeeee\n" 7998 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7999 " : 3333333333333333;", 8000 Style); 8001 8002 Style.AlignOperands = FormatStyle::OAS_DontAlign; 8003 Style.BreakBeforeTernaryOperators = false; 8004 // FIXME: Aligning the question marks is weird given DontAlign. 8005 // Consider disabling this alignment in this case. Also check whether this 8006 // will render the adjustment from https://reviews.llvm.org/D82199 8007 // unnecessary. 8008 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 8009 " bbbb ? cccccccccccccccccc :\n" 8010 " ddddd;\n", 8011 Style); 8012 8013 EXPECT_EQ( 8014 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8015 " /*\n" 8016 " */\n" 8017 " function() {\n" 8018 " try {\n" 8019 " return JJJJJJJJJJJJJJ(\n" 8020 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8021 " }\n" 8022 " } :\n" 8023 " function() {};", 8024 format( 8025 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8026 " /*\n" 8027 " */\n" 8028 " function() {\n" 8029 " try {\n" 8030 " return JJJJJJJJJJJJJJ(\n" 8031 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8032 " }\n" 8033 " } :\n" 8034 " function() {};", 8035 getGoogleStyle(FormatStyle::LK_JavaScript))); 8036 } 8037 8038 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 8039 FormatStyle Style = getLLVMStyleWithColumns(70); 8040 Style.BreakBeforeTernaryOperators = false; 8041 verifyFormat( 8042 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8045 Style); 8046 verifyFormat( 8047 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 8048 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8050 Style); 8051 verifyFormat( 8052 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8053 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8054 Style); 8055 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 8056 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8058 Style); 8059 verifyFormat( 8060 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 8061 " aaaaaaaaaaaaa);", 8062 Style); 8063 verifyFormat( 8064 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8065 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8067 " aaaaaaaaaaaaa);", 8068 Style); 8069 verifyFormat( 8070 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8071 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8072 " aaaaaaaaaaaaa);", 8073 Style); 8074 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8075 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8079 Style); 8080 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8083 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8084 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8086 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8087 Style); 8088 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 8090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8091 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8092 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8093 Style); 8094 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8095 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8096 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8097 Style); 8098 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 8099 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8102 Style); 8103 verifyFormat( 8104 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8105 " aaaaaaaaaaaaaaa :\n" 8106 " aaaaaaaaaaaaaaa;", 8107 Style); 8108 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 8109 " aaaaaaaaa ?\n" 8110 " b :\n" 8111 " c);", 8112 Style); 8113 verifyFormat("unsigned Indent =\n" 8114 " format(TheLine.First,\n" 8115 " IndentForLevel[TheLine.Level] >= 0 ?\n" 8116 " IndentForLevel[TheLine.Level] :\n" 8117 " TheLine * 2,\n" 8118 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 8119 Style); 8120 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8121 " aaaaaaaaaaaaaaa :\n" 8122 " bbbbbbbbbbbbbbb ? //\n" 8123 " ccccccccccccccc :\n" 8124 " ddddddddddddddd;", 8125 Style); 8126 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8127 " aaaaaaaaaaaaaaa :\n" 8128 " (bbbbbbbbbbbbbbb ? //\n" 8129 " ccccccccccccccc :\n" 8130 " ddddddddddddddd);", 8131 Style); 8132 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8133 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 8134 " ccccccccccccccccccccccccccc;", 8135 Style); 8136 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8137 " aaaaa :\n" 8138 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 8139 Style); 8140 8141 // Chained conditionals 8142 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8143 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8144 " 3333333333333333;", 8145 Style); 8146 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8147 " bbbbbbbbbb ? 2222222222222222 :\n" 8148 " 3333333333333333;", 8149 Style); 8150 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 8151 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8152 " 3333333333333333;", 8153 Style); 8154 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8155 " bbbbbbbbbbbbbbbb ? 222222 :\n" 8156 " 333333;", 8157 Style); 8158 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8159 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8160 " cccccccccccccccc ? 3333333333333333 :\n" 8161 " 4444444444444444;", 8162 Style); 8163 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 8164 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8165 " 3333333333333333;", 8166 Style); 8167 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8168 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8169 " (aaa ? bbb : ccc);", 8170 Style); 8171 verifyFormat( 8172 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8173 " cccccccccccccccccc) :\n" 8174 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8175 " 3333333333333333;", 8176 Style); 8177 verifyFormat( 8178 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8179 " cccccccccccccccccc) :\n" 8180 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8181 " 3333333333333333;", 8182 Style); 8183 verifyFormat( 8184 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8185 " dddddddddddddddddd) :\n" 8186 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8187 " 3333333333333333;", 8188 Style); 8189 verifyFormat( 8190 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8191 " dddddddddddddddddd) :\n" 8192 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8193 " 3333333333333333;", 8194 Style); 8195 verifyFormat( 8196 "return aaaaaaaaa ? 1111111111111111 :\n" 8197 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8198 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8199 " dddddddddddddddddd)\n", 8200 Style); 8201 verifyFormat( 8202 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8203 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8204 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8205 " cccccccccccccccccc);", 8206 Style); 8207 verifyFormat( 8208 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8209 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8210 " eeeeeeeeeeeeeeeeee) :\n" 8211 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8212 " 3333333333333333;", 8213 Style); 8214 verifyFormat( 8215 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8216 " ccccccccccccc ? dddddddddddddddddd :\n" 8217 " eeeeeeeeeeeeeeeeee) :\n" 8218 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8219 " 3333333333333333;", 8220 Style); 8221 verifyFormat( 8222 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8223 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8224 " eeeeeeeeeeeeeeeeee) :\n" 8225 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8226 " 3333333333333333;", 8227 Style); 8228 verifyFormat( 8229 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8230 " cccccccccccccccccc :\n" 8231 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8232 " 3333333333333333;", 8233 Style); 8234 verifyFormat( 8235 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8236 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8237 " eeeeeeeeeeeeeeeeee :\n" 8238 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8239 " 3333333333333333;", 8240 Style); 8241 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8242 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8243 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8244 " eeeeeeeeeeeeeeeeee) :\n" 8245 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8246 " 3333333333333333;", 8247 Style); 8248 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8249 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8250 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 8251 " eeeeeeeeeeeeeeeeee :\n" 8252 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8253 " 3333333333333333;", 8254 Style); 8255 } 8256 8257 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 8258 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 8259 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 8260 verifyFormat("bool a = true, b = false;"); 8261 8262 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8263 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 8264 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 8265 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 8266 verifyFormat( 8267 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 8268 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 8269 " d = e && f;"); 8270 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 8271 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 8272 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8273 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 8274 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 8275 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 8276 8277 FormatStyle Style = getGoogleStyle(); 8278 Style.PointerAlignment = FormatStyle::PAS_Left; 8279 Style.DerivePointerAlignment = false; 8280 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8281 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 8282 " *b = bbbbbbbbbbbbbbbbbbb;", 8283 Style); 8284 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8285 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 8286 Style); 8287 verifyFormat("vector<int*> a, b;", Style); 8288 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 8289 } 8290 8291 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 8292 verifyFormat("arr[foo ? bar : baz];"); 8293 verifyFormat("f()[foo ? bar : baz];"); 8294 verifyFormat("(a + b)[foo ? bar : baz];"); 8295 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 8296 } 8297 8298 TEST_F(FormatTest, AlignsStringLiterals) { 8299 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 8300 " \"short literal\");"); 8301 verifyFormat( 8302 "looooooooooooooooooooooooongFunction(\n" 8303 " \"short literal\"\n" 8304 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 8305 verifyFormat("someFunction(\"Always break between multi-line\"\n" 8306 " \" string literals\",\n" 8307 " and, other, parameters);"); 8308 EXPECT_EQ("fun + \"1243\" /* comment */\n" 8309 " \"5678\";", 8310 format("fun + \"1243\" /* comment */\n" 8311 " \"5678\";", 8312 getLLVMStyleWithColumns(28))); 8313 EXPECT_EQ( 8314 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8315 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 8316 " \"aaaaaaaaaaaaaaaa\";", 8317 format("aaaaaa =" 8318 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8319 "aaaaaaaaaaaaaaaaaaaaa\" " 8320 "\"aaaaaaaaaaaaaaaa\";")); 8321 verifyFormat("a = a + \"a\"\n" 8322 " \"a\"\n" 8323 " \"a\";"); 8324 verifyFormat("f(\"a\", \"b\"\n" 8325 " \"c\");"); 8326 8327 verifyFormat( 8328 "#define LL_FORMAT \"ll\"\n" 8329 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 8330 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 8331 8332 verifyFormat("#define A(X) \\\n" 8333 " \"aaaaa\" #X \"bbbbbb\" \\\n" 8334 " \"ccccc\"", 8335 getLLVMStyleWithColumns(23)); 8336 verifyFormat("#define A \"def\"\n" 8337 "f(\"abc\" A \"ghi\"\n" 8338 " \"jkl\");"); 8339 8340 verifyFormat("f(L\"a\"\n" 8341 " L\"b\");"); 8342 verifyFormat("#define A(X) \\\n" 8343 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 8344 " L\"ccccc\"", 8345 getLLVMStyleWithColumns(25)); 8346 8347 verifyFormat("f(@\"a\"\n" 8348 " @\"b\");"); 8349 verifyFormat("NSString s = @\"a\"\n" 8350 " @\"b\"\n" 8351 " @\"c\";"); 8352 verifyFormat("NSString s = @\"a\"\n" 8353 " \"b\"\n" 8354 " \"c\";"); 8355 } 8356 8357 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8358 FormatStyle Style = getLLVMStyle(); 8359 // No declarations or definitions should be moved to own line. 8360 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8361 verifyFormat("class A {\n" 8362 " int f() { return 1; }\n" 8363 " int g();\n" 8364 "};\n" 8365 "int f() { return 1; }\n" 8366 "int g();\n", 8367 Style); 8368 8369 // All declarations and definitions should have the return type moved to its 8370 // own line. 8371 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8372 Style.TypenameMacros = {"LIST"}; 8373 verifyFormat("SomeType\n" 8374 "funcdecl(LIST(uint64_t));", 8375 Style); 8376 verifyFormat("class E {\n" 8377 " int\n" 8378 " f() {\n" 8379 " return 1;\n" 8380 " }\n" 8381 " int\n" 8382 " g();\n" 8383 "};\n" 8384 "int\n" 8385 "f() {\n" 8386 " return 1;\n" 8387 "}\n" 8388 "int\n" 8389 "g();\n", 8390 Style); 8391 8392 // Top-level definitions, and no kinds of declarations should have the 8393 // return type moved to its own line. 8394 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8395 verifyFormat("class B {\n" 8396 " int f() { return 1; }\n" 8397 " int g();\n" 8398 "};\n" 8399 "int\n" 8400 "f() {\n" 8401 " return 1;\n" 8402 "}\n" 8403 "int g();\n", 8404 Style); 8405 8406 // Top-level definitions and declarations should have the return type moved 8407 // to its own line. 8408 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8409 verifyFormat("class C {\n" 8410 " int f() { return 1; }\n" 8411 " int g();\n" 8412 "};\n" 8413 "int\n" 8414 "f() {\n" 8415 " return 1;\n" 8416 "}\n" 8417 "int\n" 8418 "g();\n", 8419 Style); 8420 8421 // All definitions should have the return type moved to its own line, but no 8422 // kinds of declarations. 8423 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8424 verifyFormat("class D {\n" 8425 " int\n" 8426 " f() {\n" 8427 " return 1;\n" 8428 " }\n" 8429 " int g();\n" 8430 "};\n" 8431 "int\n" 8432 "f() {\n" 8433 " return 1;\n" 8434 "}\n" 8435 "int g();\n", 8436 Style); 8437 verifyFormat("const char *\n" 8438 "f(void) {\n" // Break here. 8439 " return \"\";\n" 8440 "}\n" 8441 "const char *bar(void);\n", // No break here. 8442 Style); 8443 verifyFormat("template <class T>\n" 8444 "T *\n" 8445 "f(T &c) {\n" // Break here. 8446 " return NULL;\n" 8447 "}\n" 8448 "template <class T> T *f(T &c);\n", // No break here. 8449 Style); 8450 verifyFormat("class C {\n" 8451 " int\n" 8452 " operator+() {\n" 8453 " return 1;\n" 8454 " }\n" 8455 " int\n" 8456 " operator()() {\n" 8457 " return 1;\n" 8458 " }\n" 8459 "};\n", 8460 Style); 8461 verifyFormat("void\n" 8462 "A::operator()() {}\n" 8463 "void\n" 8464 "A::operator>>() {}\n" 8465 "void\n" 8466 "A::operator+() {}\n" 8467 "void\n" 8468 "A::operator*() {}\n" 8469 "void\n" 8470 "A::operator->() {}\n" 8471 "void\n" 8472 "A::operator void *() {}\n" 8473 "void\n" 8474 "A::operator void &() {}\n" 8475 "void\n" 8476 "A::operator void &&() {}\n" 8477 "void\n" 8478 "A::operator char *() {}\n" 8479 "void\n" 8480 "A::operator[]() {}\n" 8481 "void\n" 8482 "A::operator!() {}\n" 8483 "void\n" 8484 "A::operator**() {}\n" 8485 "void\n" 8486 "A::operator<Foo> *() {}\n" 8487 "void\n" 8488 "A::operator<Foo> **() {}\n" 8489 "void\n" 8490 "A::operator<Foo> &() {}\n" 8491 "void\n" 8492 "A::operator void **() {}\n", 8493 Style); 8494 verifyFormat("constexpr auto\n" 8495 "operator()() const -> reference {}\n" 8496 "constexpr auto\n" 8497 "operator>>() const -> reference {}\n" 8498 "constexpr auto\n" 8499 "operator+() const -> reference {}\n" 8500 "constexpr auto\n" 8501 "operator*() const -> reference {}\n" 8502 "constexpr auto\n" 8503 "operator->() const -> reference {}\n" 8504 "constexpr auto\n" 8505 "operator++() const -> reference {}\n" 8506 "constexpr auto\n" 8507 "operator void *() const -> reference {}\n" 8508 "constexpr auto\n" 8509 "operator void **() const -> reference {}\n" 8510 "constexpr auto\n" 8511 "operator void *() const -> reference {}\n" 8512 "constexpr auto\n" 8513 "operator void &() const -> reference {}\n" 8514 "constexpr auto\n" 8515 "operator void &&() const -> reference {}\n" 8516 "constexpr auto\n" 8517 "operator char *() const -> reference {}\n" 8518 "constexpr auto\n" 8519 "operator!() const -> reference {}\n" 8520 "constexpr auto\n" 8521 "operator[]() const -> reference {}\n", 8522 Style); 8523 verifyFormat("void *operator new(std::size_t s);", // No break here. 8524 Style); 8525 verifyFormat("void *\n" 8526 "operator new(std::size_t s) {}", 8527 Style); 8528 verifyFormat("void *\n" 8529 "operator delete[](void *ptr) {}", 8530 Style); 8531 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8532 verifyFormat("const char *\n" 8533 "f(void)\n" // Break here. 8534 "{\n" 8535 " return \"\";\n" 8536 "}\n" 8537 "const char *bar(void);\n", // No break here. 8538 Style); 8539 verifyFormat("template <class T>\n" 8540 "T *\n" // Problem here: no line break 8541 "f(T &c)\n" // Break here. 8542 "{\n" 8543 " return NULL;\n" 8544 "}\n" 8545 "template <class T> T *f(T &c);\n", // No break here. 8546 Style); 8547 verifyFormat("int\n" 8548 "foo(A<bool> a)\n" 8549 "{\n" 8550 " return a;\n" 8551 "}\n", 8552 Style); 8553 verifyFormat("int\n" 8554 "foo(A<8> a)\n" 8555 "{\n" 8556 " return a;\n" 8557 "}\n", 8558 Style); 8559 verifyFormat("int\n" 8560 "foo(A<B<bool>, 8> a)\n" 8561 "{\n" 8562 " return a;\n" 8563 "}\n", 8564 Style); 8565 verifyFormat("int\n" 8566 "foo(A<B<8>, bool> a)\n" 8567 "{\n" 8568 " return a;\n" 8569 "}\n", 8570 Style); 8571 verifyFormat("int\n" 8572 "foo(A<B<bool>, bool> a)\n" 8573 "{\n" 8574 " return a;\n" 8575 "}\n", 8576 Style); 8577 verifyFormat("int\n" 8578 "foo(A<B<8>, 8> a)\n" 8579 "{\n" 8580 " return a;\n" 8581 "}\n", 8582 Style); 8583 8584 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8585 Style.BraceWrapping.AfterFunction = true; 8586 verifyFormat("int f(i);\n" // No break here. 8587 "int\n" // Break here. 8588 "f(i)\n" 8589 "{\n" 8590 " return i + 1;\n" 8591 "}\n" 8592 "int\n" // Break here. 8593 "f(i)\n" 8594 "{\n" 8595 " return i + 1;\n" 8596 "};", 8597 Style); 8598 verifyFormat("int f(a, b, c);\n" // No break here. 8599 "int\n" // Break here. 8600 "f(a, b, c)\n" // Break here. 8601 "short a, b;\n" 8602 "float c;\n" 8603 "{\n" 8604 " return a + b < c;\n" 8605 "}\n" 8606 "int\n" // Break here. 8607 "f(a, b, c)\n" // Break here. 8608 "short a, b;\n" 8609 "float c;\n" 8610 "{\n" 8611 " return a + b < c;\n" 8612 "};", 8613 Style); 8614 verifyFormat("byte *\n" // Break here. 8615 "f(a)\n" // Break here. 8616 "byte a[];\n" 8617 "{\n" 8618 " return a;\n" 8619 "}", 8620 Style); 8621 verifyFormat("bool f(int a, int) override;\n" 8622 "Bar g(int a, Bar) final;\n" 8623 "Bar h(a, Bar) final;", 8624 Style); 8625 verifyFormat("int\n" 8626 "f(a)", 8627 Style); 8628 verifyFormat("bool\n" 8629 "f(size_t = 0, bool b = false)\n" 8630 "{\n" 8631 " return !b;\n" 8632 "}", 8633 Style); 8634 8635 // The return breaking style doesn't affect: 8636 // * function and object definitions with attribute-like macros 8637 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8638 " ABSL_GUARDED_BY(mutex) = {};", 8639 getGoogleStyleWithColumns(40)); 8640 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8641 " ABSL_GUARDED_BY(mutex); // comment", 8642 getGoogleStyleWithColumns(40)); 8643 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8644 " ABSL_GUARDED_BY(mutex1)\n" 8645 " ABSL_GUARDED_BY(mutex2);", 8646 getGoogleStyleWithColumns(40)); 8647 verifyFormat("Tttttt f(int a, int b)\n" 8648 " ABSL_GUARDED_BY(mutex1)\n" 8649 " ABSL_GUARDED_BY(mutex2);", 8650 getGoogleStyleWithColumns(40)); 8651 // * typedefs 8652 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8653 8654 Style = getGNUStyle(); 8655 8656 // Test for comments at the end of function declarations. 8657 verifyFormat("void\n" 8658 "foo (int a, /*abc*/ int b) // def\n" 8659 "{\n" 8660 "}\n", 8661 Style); 8662 8663 verifyFormat("void\n" 8664 "foo (int a, /* abc */ int b) /* def */\n" 8665 "{\n" 8666 "}\n", 8667 Style); 8668 8669 // Definitions that should not break after return type 8670 verifyFormat("void foo (int a, int b); // def\n", Style); 8671 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8672 verifyFormat("void foo (int a, int b);\n", Style); 8673 } 8674 8675 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8676 FormatStyle NoBreak = getLLVMStyle(); 8677 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8678 FormatStyle Break = getLLVMStyle(); 8679 Break.AlwaysBreakBeforeMultilineStrings = true; 8680 verifyFormat("aaaa = \"bbbb\"\n" 8681 " \"cccc\";", 8682 NoBreak); 8683 verifyFormat("aaaa =\n" 8684 " \"bbbb\"\n" 8685 " \"cccc\";", 8686 Break); 8687 verifyFormat("aaaa(\"bbbb\"\n" 8688 " \"cccc\");", 8689 NoBreak); 8690 verifyFormat("aaaa(\n" 8691 " \"bbbb\"\n" 8692 " \"cccc\");", 8693 Break); 8694 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8695 " \"cccc\");", 8696 NoBreak); 8697 verifyFormat("aaaa(qqq,\n" 8698 " \"bbbb\"\n" 8699 " \"cccc\");", 8700 Break); 8701 verifyFormat("aaaa(qqq,\n" 8702 " L\"bbbb\"\n" 8703 " L\"cccc\");", 8704 Break); 8705 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8706 " \"bbbb\"));", 8707 Break); 8708 verifyFormat("string s = someFunction(\n" 8709 " \"abc\"\n" 8710 " \"abc\");", 8711 Break); 8712 8713 // As we break before unary operators, breaking right after them is bad. 8714 verifyFormat("string foo = abc ? \"x\"\n" 8715 " \"blah blah blah blah blah blah\"\n" 8716 " : \"y\";", 8717 Break); 8718 8719 // Don't break if there is no column gain. 8720 verifyFormat("f(\"aaaa\"\n" 8721 " \"bbbb\");", 8722 Break); 8723 8724 // Treat literals with escaped newlines like multi-line string literals. 8725 EXPECT_EQ("x = \"a\\\n" 8726 "b\\\n" 8727 "c\";", 8728 format("x = \"a\\\n" 8729 "b\\\n" 8730 "c\";", 8731 NoBreak)); 8732 EXPECT_EQ("xxxx =\n" 8733 " \"a\\\n" 8734 "b\\\n" 8735 "c\";", 8736 format("xxxx = \"a\\\n" 8737 "b\\\n" 8738 "c\";", 8739 Break)); 8740 8741 EXPECT_EQ("NSString *const kString =\n" 8742 " @\"aaaa\"\n" 8743 " @\"bbbb\";", 8744 format("NSString *const kString = @\"aaaa\"\n" 8745 "@\"bbbb\";", 8746 Break)); 8747 8748 Break.ColumnLimit = 0; 8749 verifyFormat("const char *hello = \"hello llvm\";", Break); 8750 } 8751 8752 TEST_F(FormatTest, AlignsPipes) { 8753 verifyFormat( 8754 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8755 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8756 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8757 verifyFormat( 8758 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8759 " << aaaaaaaaaaaaaaaaaaaa;"); 8760 verifyFormat( 8761 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8762 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8763 verifyFormat( 8764 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8765 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8766 verifyFormat( 8767 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8768 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8769 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8770 verifyFormat( 8771 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8772 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8773 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8774 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8777 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8778 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8779 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8780 verifyFormat( 8781 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8782 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8783 verifyFormat( 8784 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8785 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8786 8787 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8788 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8789 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8791 " aaaaaaaaaaaaaaaaaaaaa)\n" 8792 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8793 verifyFormat("LOG_IF(aaa == //\n" 8794 " bbb)\n" 8795 " << a << b;"); 8796 8797 // But sometimes, breaking before the first "<<" is desirable. 8798 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8799 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8800 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8801 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8802 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8803 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8804 " << BEF << IsTemplate << Description << E->getType();"); 8805 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8806 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8807 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8808 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8809 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8810 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8811 " << aaa;"); 8812 8813 verifyFormat( 8814 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8815 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8816 8817 // Incomplete string literal. 8818 EXPECT_EQ("llvm::errs() << \"\n" 8819 " << a;", 8820 format("llvm::errs() << \"\n<<a;")); 8821 8822 verifyFormat("void f() {\n" 8823 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8824 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8825 "}"); 8826 8827 // Handle 'endl'. 8828 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8829 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8830 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8831 8832 // Handle '\n'. 8833 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8834 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8835 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8836 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8837 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8838 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8839 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8840 } 8841 8842 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8843 verifyFormat("return out << \"somepacket = {\\n\"\n" 8844 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8845 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8846 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8847 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8848 " << \"}\";"); 8849 8850 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8851 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8852 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8853 verifyFormat( 8854 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8855 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8856 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8857 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8858 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8859 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8860 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8861 verifyFormat( 8862 "void f() {\n" 8863 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8864 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8865 "}"); 8866 8867 // Breaking before the first "<<" is generally not desirable. 8868 verifyFormat( 8869 "llvm::errs()\n" 8870 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8871 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8872 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8873 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8874 getLLVMStyleWithColumns(70)); 8875 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8876 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8877 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8878 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8879 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8880 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8881 getLLVMStyleWithColumns(70)); 8882 8883 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8884 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8885 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8886 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8887 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8888 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8889 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8890 " (aaaa + aaaa);", 8891 getLLVMStyleWithColumns(40)); 8892 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8893 " (aaaaaaa + aaaaa));", 8894 getLLVMStyleWithColumns(40)); 8895 verifyFormat( 8896 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8897 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8898 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8899 } 8900 8901 TEST_F(FormatTest, UnderstandsEquals) { 8902 verifyFormat( 8903 "aaaaaaaaaaaaaaaaa =\n" 8904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8905 verifyFormat( 8906 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8908 verifyFormat( 8909 "if (a) {\n" 8910 " f();\n" 8911 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8913 "}"); 8914 8915 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8916 " 100000000 + 10000000) {\n}"); 8917 } 8918 8919 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8920 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8921 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8922 8923 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8924 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8925 8926 verifyFormat( 8927 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8928 " Parameter2);"); 8929 8930 verifyFormat( 8931 "ShortObject->shortFunction(\n" 8932 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8933 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8934 8935 verifyFormat("loooooooooooooongFunction(\n" 8936 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8937 8938 verifyFormat( 8939 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8940 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8941 8942 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8943 " .WillRepeatedly(Return(SomeValue));"); 8944 verifyFormat("void f() {\n" 8945 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8946 " .Times(2)\n" 8947 " .WillRepeatedly(Return(SomeValue));\n" 8948 "}"); 8949 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8950 " ccccccccccccccccccccccc);"); 8951 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8952 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8953 " .aaaaa(aaaaa),\n" 8954 " aaaaaaaaaaaaaaaaaaaaa);"); 8955 verifyFormat("void f() {\n" 8956 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8957 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8958 "}"); 8959 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8961 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8962 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8963 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8964 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8965 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8966 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8967 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8968 "}"); 8969 8970 // Here, it is not necessary to wrap at "." or "->". 8971 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8972 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8973 verifyFormat( 8974 "aaaaaaaaaaa->aaaaaaaaa(\n" 8975 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8976 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8977 8978 verifyFormat( 8979 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8980 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8981 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8982 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8983 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8984 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8985 8986 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8987 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8988 " .a();"); 8989 8990 FormatStyle NoBinPacking = getLLVMStyle(); 8991 NoBinPacking.BinPackParameters = false; 8992 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8993 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8994 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8995 " aaaaaaaaaaaaaaaaaaa,\n" 8996 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8997 NoBinPacking); 8998 8999 // If there is a subsequent call, change to hanging indentation. 9000 verifyFormat( 9001 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9002 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 9003 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9004 verifyFormat( 9005 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9006 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 9007 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9009 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9010 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9012 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9013 } 9014 9015 TEST_F(FormatTest, WrapsTemplateDeclarations) { 9016 verifyFormat("template <typename T>\n" 9017 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9018 verifyFormat("template <typename T>\n" 9019 "// T should be one of {A, B}.\n" 9020 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9021 verifyFormat( 9022 "template <typename T>\n" 9023 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 9024 verifyFormat("template <typename T>\n" 9025 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 9026 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 9027 verifyFormat( 9028 "template <typename T>\n" 9029 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 9030 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 9031 verifyFormat( 9032 "template <typename T>\n" 9033 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 9034 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 9035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9036 verifyFormat("template <typename T>\n" 9037 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9038 " int aaaaaaaaaaaaaaaaaaaaaa);"); 9039 verifyFormat( 9040 "template <typename T1, typename T2 = char, typename T3 = char,\n" 9041 " typename T4 = char>\n" 9042 "void f();"); 9043 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 9044 " template <typename> class cccccccccccccccccccccc,\n" 9045 " typename ddddddddddddd>\n" 9046 "class C {};"); 9047 verifyFormat( 9048 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 9049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9050 9051 verifyFormat("void f() {\n" 9052 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 9053 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 9054 "}"); 9055 9056 verifyFormat("template <typename T> class C {};"); 9057 verifyFormat("template <typename T> void f();"); 9058 verifyFormat("template <typename T> void f() {}"); 9059 verifyFormat( 9060 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9061 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 9063 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 9066 " bbbbbbbbbbbbbbbbbbbbbbbb);", 9067 getLLVMStyleWithColumns(72)); 9068 EXPECT_EQ("static_cast<A< //\n" 9069 " B> *>(\n" 9070 "\n" 9071 ");", 9072 format("static_cast<A<//\n" 9073 " B>*>(\n" 9074 "\n" 9075 " );")); 9076 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9077 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 9078 9079 FormatStyle AlwaysBreak = getLLVMStyle(); 9080 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9081 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 9082 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 9083 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 9084 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9085 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9086 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 9087 verifyFormat("template <template <typename> class Fooooooo,\n" 9088 " template <typename> class Baaaaaaar>\n" 9089 "struct C {};", 9090 AlwaysBreak); 9091 verifyFormat("template <typename T> // T can be A, B or C.\n" 9092 "struct C {};", 9093 AlwaysBreak); 9094 verifyFormat("template <enum E> class A {\n" 9095 "public:\n" 9096 " E *f();\n" 9097 "};"); 9098 9099 FormatStyle NeverBreak = getLLVMStyle(); 9100 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 9101 verifyFormat("template <typename T> class C {};", NeverBreak); 9102 verifyFormat("template <typename T> void f();", NeverBreak); 9103 verifyFormat("template <typename T> void f() {}", NeverBreak); 9104 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9105 "bbbbbbbbbbbbbbbbbbbb) {}", 9106 NeverBreak); 9107 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9108 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9109 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 9110 NeverBreak); 9111 verifyFormat("template <template <typename> class Fooooooo,\n" 9112 " template <typename> class Baaaaaaar>\n" 9113 "struct C {};", 9114 NeverBreak); 9115 verifyFormat("template <typename T> // T can be A, B or C.\n" 9116 "struct C {};", 9117 NeverBreak); 9118 verifyFormat("template <enum E> class A {\n" 9119 "public:\n" 9120 " E *f();\n" 9121 "};", 9122 NeverBreak); 9123 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 9124 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9125 "bbbbbbbbbbbbbbbbbbbb) {}", 9126 NeverBreak); 9127 } 9128 9129 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 9130 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 9131 Style.ColumnLimit = 60; 9132 EXPECT_EQ("// Baseline - no comments.\n" 9133 "template <\n" 9134 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9135 "void f() {}", 9136 format("// Baseline - no comments.\n" 9137 "template <\n" 9138 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9139 "void f() {}", 9140 Style)); 9141 9142 EXPECT_EQ("template <\n" 9143 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9144 "void f() {}", 9145 format("template <\n" 9146 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9147 "void f() {}", 9148 Style)); 9149 9150 EXPECT_EQ( 9151 "template <\n" 9152 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9153 "void f() {}", 9154 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9155 "void f() {}", 9156 Style)); 9157 9158 EXPECT_EQ( 9159 "template <\n" 9160 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9161 " // multiline\n" 9162 "void f() {}", 9163 format("template <\n" 9164 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9165 " // multiline\n" 9166 "void f() {}", 9167 Style)); 9168 9169 EXPECT_EQ( 9170 "template <typename aaaaaaaaaa<\n" 9171 " bbbbbbbbbbbb>::value> // trailing loooong\n" 9172 "void f() {}", 9173 format( 9174 "template <\n" 9175 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 9176 "void f() {}", 9177 Style)); 9178 } 9179 9180 TEST_F(FormatTest, WrapsTemplateParameters) { 9181 FormatStyle Style = getLLVMStyle(); 9182 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9183 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9184 verifyFormat( 9185 "template <typename... a> struct q {};\n" 9186 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9187 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9188 " y;", 9189 Style); 9190 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9191 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9192 verifyFormat( 9193 "template <typename... a> struct r {};\n" 9194 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9195 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9196 " y;", 9197 Style); 9198 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9199 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9200 verifyFormat("template <typename... a> struct s {};\n" 9201 "extern s<\n" 9202 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9203 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9204 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9205 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9206 " y;", 9207 Style); 9208 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9209 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9210 verifyFormat("template <typename... a> struct t {};\n" 9211 "extern t<\n" 9212 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9213 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9214 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9215 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9216 " y;", 9217 Style); 9218 } 9219 9220 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 9221 verifyFormat( 9222 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9224 verifyFormat( 9225 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9227 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9228 9229 // FIXME: Should we have the extra indent after the second break? 9230 verifyFormat( 9231 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9233 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9234 9235 verifyFormat( 9236 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 9237 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 9238 9239 // Breaking at nested name specifiers is generally not desirable. 9240 verifyFormat( 9241 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9242 " aaaaaaaaaaaaaaaaaaaaaaa);"); 9243 9244 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 9245 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9247 " aaaaaaaaaaaaaaaaaaaaa);", 9248 getLLVMStyleWithColumns(74)); 9249 9250 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9252 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9253 } 9254 9255 TEST_F(FormatTest, UnderstandsTemplateParameters) { 9256 verifyFormat("A<int> a;"); 9257 verifyFormat("A<A<A<int>>> a;"); 9258 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 9259 verifyFormat("bool x = a < 1 || 2 > a;"); 9260 verifyFormat("bool x = 5 < f<int>();"); 9261 verifyFormat("bool x = f<int>() > 5;"); 9262 verifyFormat("bool x = 5 < a<int>::x;"); 9263 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 9264 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 9265 9266 verifyGoogleFormat("A<A<int>> a;"); 9267 verifyGoogleFormat("A<A<A<int>>> a;"); 9268 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 9269 verifyGoogleFormat("A<A<int> > a;"); 9270 verifyGoogleFormat("A<A<A<int> > > a;"); 9271 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 9272 verifyGoogleFormat("A<::A<int>> a;"); 9273 verifyGoogleFormat("A<::A> a;"); 9274 verifyGoogleFormat("A< ::A> a;"); 9275 verifyGoogleFormat("A< ::A<int> > a;"); 9276 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 9277 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 9278 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 9279 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 9280 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 9281 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 9282 9283 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 9284 9285 // template closer followed by a token that starts with > or = 9286 verifyFormat("bool b = a<1> > 1;"); 9287 verifyFormat("bool b = a<1> >= 1;"); 9288 verifyFormat("int i = a<1> >> 1;"); 9289 FormatStyle Style = getLLVMStyle(); 9290 Style.SpaceBeforeAssignmentOperators = false; 9291 verifyFormat("bool b= a<1> == 1;", Style); 9292 verifyFormat("a<int> = 1;", Style); 9293 verifyFormat("a<int> >>= 1;", Style); 9294 9295 verifyFormat("test < a | b >> c;"); 9296 verifyFormat("test<test<a | b>> c;"); 9297 verifyFormat("test >> a >> b;"); 9298 verifyFormat("test << a >> b;"); 9299 9300 verifyFormat("f<int>();"); 9301 verifyFormat("template <typename T> void f() {}"); 9302 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 9303 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 9304 "sizeof(char)>::type>;"); 9305 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 9306 verifyFormat("f(a.operator()<A>());"); 9307 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9308 " .template operator()<A>());", 9309 getLLVMStyleWithColumns(35)); 9310 9311 // Not template parameters. 9312 verifyFormat("return a < b && c > d;"); 9313 verifyFormat("void f() {\n" 9314 " while (a < b && c > d) {\n" 9315 " }\n" 9316 "}"); 9317 verifyFormat("template <typename... Types>\n" 9318 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 9319 9320 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 9322 getLLVMStyleWithColumns(60)); 9323 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 9324 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 9325 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 9326 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 9327 } 9328 9329 TEST_F(FormatTest, UnderstandsShiftOperators) { 9330 verifyFormat("if (i < x >> 1)"); 9331 verifyFormat("while (i < x >> 1)"); 9332 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 9333 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 9334 verifyFormat( 9335 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 9336 verifyFormat("Foo.call<Bar<Function>>()"); 9337 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 9338 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 9339 "++i, v = v >> 1)"); 9340 verifyFormat("if (w<u<v<x>>, 1>::t)"); 9341 } 9342 9343 TEST_F(FormatTest, BitshiftOperatorWidth) { 9344 EXPECT_EQ("int a = 1 << 2; /* foo\n" 9345 " bar */", 9346 format("int a=1<<2; /* foo\n" 9347 " bar */")); 9348 9349 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 9350 " bar */", 9351 format("int b =256>>1 ; /* foo\n" 9352 " bar */")); 9353 } 9354 9355 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9356 verifyFormat("COMPARE(a, ==, b);"); 9357 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9358 } 9359 9360 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9361 verifyFormat("int A::*x;"); 9362 verifyFormat("int (S::*func)(void *);"); 9363 verifyFormat("void f() { int (S::*func)(void *); }"); 9364 verifyFormat("typedef bool *(Class::*Member)() const;"); 9365 verifyFormat("void f() {\n" 9366 " (a->*f)();\n" 9367 " a->*x;\n" 9368 " (a.*f)();\n" 9369 " ((*a).*f)();\n" 9370 " a.*x;\n" 9371 "}"); 9372 verifyFormat("void f() {\n" 9373 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9374 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9375 "}"); 9376 verifyFormat( 9377 "(aaaaaaaaaa->*bbbbbbb)(\n" 9378 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9379 FormatStyle Style = getLLVMStyle(); 9380 Style.PointerAlignment = FormatStyle::PAS_Left; 9381 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9382 } 9383 9384 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9385 verifyFormat("int a = -2;"); 9386 verifyFormat("f(-1, -2, -3);"); 9387 verifyFormat("a[-1] = 5;"); 9388 verifyFormat("int a = 5 + -2;"); 9389 verifyFormat("if (i == -1) {\n}"); 9390 verifyFormat("if (i != -1) {\n}"); 9391 verifyFormat("if (i > -1) {\n}"); 9392 verifyFormat("if (i < -1) {\n}"); 9393 verifyFormat("++(a->f());"); 9394 verifyFormat("--(a->f());"); 9395 verifyFormat("(a->f())++;"); 9396 verifyFormat("a[42]++;"); 9397 verifyFormat("if (!(a->f())) {\n}"); 9398 verifyFormat("if (!+i) {\n}"); 9399 verifyFormat("~&a;"); 9400 9401 verifyFormat("a-- > b;"); 9402 verifyFormat("b ? -a : c;"); 9403 verifyFormat("n * sizeof char16;"); 9404 verifyFormat("n * alignof char16;", getGoogleStyle()); 9405 verifyFormat("sizeof(char);"); 9406 verifyFormat("alignof(char);", getGoogleStyle()); 9407 9408 verifyFormat("return -1;"); 9409 verifyFormat("throw -1;"); 9410 verifyFormat("switch (a) {\n" 9411 "case -1:\n" 9412 " break;\n" 9413 "}"); 9414 verifyFormat("#define X -1"); 9415 verifyFormat("#define X -kConstant"); 9416 9417 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9418 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9419 9420 verifyFormat("int a = /* confusing comment */ -1;"); 9421 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9422 verifyFormat("int a = i /* confusing comment */++;"); 9423 9424 verifyFormat("co_yield -1;"); 9425 verifyFormat("co_return -1;"); 9426 9427 // Check that * is not treated as a binary operator when we set 9428 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9429 FormatStyle PASLeftStyle = getLLVMStyle(); 9430 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9431 verifyFormat("co_return *a;", PASLeftStyle); 9432 verifyFormat("co_await *a;", PASLeftStyle); 9433 verifyFormat("co_yield *a", PASLeftStyle); 9434 verifyFormat("return *a;", PASLeftStyle); 9435 } 9436 9437 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9438 verifyFormat("if (!aaaaaaaaaa( // break\n" 9439 " aaaaa)) {\n" 9440 "}"); 9441 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9442 " aaaaa));"); 9443 verifyFormat("*aaa = aaaaaaa( // break\n" 9444 " bbbbbb);"); 9445 } 9446 9447 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9448 verifyFormat("bool operator<();"); 9449 verifyFormat("bool operator>();"); 9450 verifyFormat("bool operator=();"); 9451 verifyFormat("bool operator==();"); 9452 verifyFormat("bool operator!=();"); 9453 verifyFormat("int operator+();"); 9454 verifyFormat("int operator++();"); 9455 verifyFormat("int operator++(int) volatile noexcept;"); 9456 verifyFormat("bool operator,();"); 9457 verifyFormat("bool operator();"); 9458 verifyFormat("bool operator()();"); 9459 verifyFormat("bool operator[]();"); 9460 verifyFormat("operator bool();"); 9461 verifyFormat("operator int();"); 9462 verifyFormat("operator void *();"); 9463 verifyFormat("operator SomeType<int>();"); 9464 verifyFormat("operator SomeType<int, int>();"); 9465 verifyFormat("operator SomeType<SomeType<int>>();"); 9466 verifyFormat("operator< <>();"); 9467 verifyFormat("operator<< <>();"); 9468 verifyFormat("< <>"); 9469 9470 verifyFormat("void *operator new(std::size_t size);"); 9471 verifyFormat("void *operator new[](std::size_t size);"); 9472 verifyFormat("void operator delete(void *ptr);"); 9473 verifyFormat("void operator delete[](void *ptr);"); 9474 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9475 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9476 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9477 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9478 9479 verifyFormat( 9480 "ostream &operator<<(ostream &OutputStream,\n" 9481 " SomeReallyLongType WithSomeReallyLongValue);"); 9482 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9483 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9484 " return left.group < right.group;\n" 9485 "}"); 9486 verifyFormat("SomeType &operator=(const SomeType &S);"); 9487 verifyFormat("f.template operator()<int>();"); 9488 9489 verifyGoogleFormat("operator void*();"); 9490 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9491 verifyGoogleFormat("operator ::A();"); 9492 9493 verifyFormat("using A::operator+;"); 9494 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9495 "int i;"); 9496 9497 // Calling an operator as a member function. 9498 verifyFormat("void f() { a.operator*(); }"); 9499 verifyFormat("void f() { a.operator*(b & b); }"); 9500 verifyFormat("void f() { a->operator&(a * b); }"); 9501 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9502 // TODO: Calling an operator as a non-member function is hard to distinguish. 9503 // https://llvm.org/PR50629 9504 // verifyFormat("void f() { operator*(a & a); }"); 9505 // verifyFormat("void f() { operator&(a, b * b); }"); 9506 9507 verifyFormat("::operator delete(foo);"); 9508 verifyFormat("::operator new(n * sizeof(foo));"); 9509 verifyFormat("foo() { ::operator delete(foo); }"); 9510 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 9511 } 9512 9513 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9514 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9515 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9516 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9517 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9518 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9519 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9520 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9521 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9522 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9523 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9524 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9525 verifyFormat("void Fn(T const &) const &;"); 9526 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9527 verifyFormat("template <typename T>\n" 9528 "void F(T) && = delete;", 9529 getGoogleStyle()); 9530 9531 FormatStyle AlignLeft = getLLVMStyle(); 9532 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9533 verifyFormat("void A::b() && {}", AlignLeft); 9534 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9535 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9536 AlignLeft); 9537 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9538 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9539 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9540 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9541 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9542 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9543 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9544 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9545 9546 FormatStyle Spaces = getLLVMStyle(); 9547 Spaces.SpacesInCStyleCastParentheses = true; 9548 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9549 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9550 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9551 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9552 9553 Spaces.SpacesInCStyleCastParentheses = false; 9554 Spaces.SpacesInParentheses = true; 9555 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9556 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9557 Spaces); 9558 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9559 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9560 9561 FormatStyle BreakTemplate = getLLVMStyle(); 9562 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9563 9564 verifyFormat("struct f {\n" 9565 " template <class T>\n" 9566 " int &foo(const std::string &str) &noexcept {}\n" 9567 "};", 9568 BreakTemplate); 9569 9570 verifyFormat("struct f {\n" 9571 " template <class T>\n" 9572 " int &foo(const std::string &str) &&noexcept {}\n" 9573 "};", 9574 BreakTemplate); 9575 9576 verifyFormat("struct f {\n" 9577 " template <class T>\n" 9578 " int &foo(const std::string &str) const &noexcept {}\n" 9579 "};", 9580 BreakTemplate); 9581 9582 verifyFormat("struct f {\n" 9583 " template <class T>\n" 9584 " int &foo(const std::string &str) const &noexcept {}\n" 9585 "};", 9586 BreakTemplate); 9587 9588 verifyFormat("struct f {\n" 9589 " template <class T>\n" 9590 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9591 "};", 9592 BreakTemplate); 9593 9594 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9595 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9596 FormatStyle::BTDS_Yes; 9597 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9598 9599 verifyFormat("struct f {\n" 9600 " template <class T>\n" 9601 " int& foo(const std::string& str) & noexcept {}\n" 9602 "};", 9603 AlignLeftBreakTemplate); 9604 9605 verifyFormat("struct f {\n" 9606 " template <class T>\n" 9607 " int& foo(const std::string& str) && noexcept {}\n" 9608 "};", 9609 AlignLeftBreakTemplate); 9610 9611 verifyFormat("struct f {\n" 9612 " template <class T>\n" 9613 " int& foo(const std::string& str) const& noexcept {}\n" 9614 "};", 9615 AlignLeftBreakTemplate); 9616 9617 verifyFormat("struct f {\n" 9618 " template <class T>\n" 9619 " int& foo(const std::string& str) const&& noexcept {}\n" 9620 "};", 9621 AlignLeftBreakTemplate); 9622 9623 verifyFormat("struct f {\n" 9624 " template <class T>\n" 9625 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9626 "};", 9627 AlignLeftBreakTemplate); 9628 9629 // The `&` in `Type&` should not be confused with a trailing `&` of 9630 // DEPRECATED(reason) member function. 9631 verifyFormat("struct f {\n" 9632 " template <class T>\n" 9633 " DEPRECATED(reason)\n" 9634 " Type &foo(arguments) {}\n" 9635 "};", 9636 BreakTemplate); 9637 9638 verifyFormat("struct f {\n" 9639 " template <class T>\n" 9640 " DEPRECATED(reason)\n" 9641 " Type& foo(arguments) {}\n" 9642 "};", 9643 AlignLeftBreakTemplate); 9644 9645 verifyFormat("void (*foopt)(int) = &func;"); 9646 } 9647 9648 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9649 verifyFormat("void f() {\n" 9650 " A *a = new A;\n" 9651 " A *a = new (placement) A;\n" 9652 " delete a;\n" 9653 " delete (A *)a;\n" 9654 "}"); 9655 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9656 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9657 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9658 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9659 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9660 verifyFormat("delete[] h->p;"); 9661 9662 verifyFormat("void operator delete(void *foo) ATTRIB;"); 9663 verifyFormat("void operator new(void *foo) ATTRIB;"); 9664 verifyFormat("void operator delete[](void *foo) ATTRIB;"); 9665 verifyFormat("void operator delete(void *ptr) noexcept;"); 9666 } 9667 9668 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9669 verifyFormat("int *f(int *a) {}"); 9670 verifyFormat("int main(int argc, char **argv) {}"); 9671 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9672 verifyIndependentOfContext("f(a, *a);"); 9673 verifyFormat("void g() { f(*a); }"); 9674 verifyIndependentOfContext("int a = b * 10;"); 9675 verifyIndependentOfContext("int a = 10 * b;"); 9676 verifyIndependentOfContext("int a = b * c;"); 9677 verifyIndependentOfContext("int a += b * c;"); 9678 verifyIndependentOfContext("int a -= b * c;"); 9679 verifyIndependentOfContext("int a *= b * c;"); 9680 verifyIndependentOfContext("int a /= b * c;"); 9681 verifyIndependentOfContext("int a = *b;"); 9682 verifyIndependentOfContext("int a = *b * c;"); 9683 verifyIndependentOfContext("int a = b * *c;"); 9684 verifyIndependentOfContext("int a = b * (10);"); 9685 verifyIndependentOfContext("S << b * (10);"); 9686 verifyIndependentOfContext("return 10 * b;"); 9687 verifyIndependentOfContext("return *b * *c;"); 9688 verifyIndependentOfContext("return a & ~b;"); 9689 verifyIndependentOfContext("f(b ? *c : *d);"); 9690 verifyIndependentOfContext("int a = b ? *c : *d;"); 9691 verifyIndependentOfContext("*b = a;"); 9692 verifyIndependentOfContext("a * ~b;"); 9693 verifyIndependentOfContext("a * !b;"); 9694 verifyIndependentOfContext("a * +b;"); 9695 verifyIndependentOfContext("a * -b;"); 9696 verifyIndependentOfContext("a * ++b;"); 9697 verifyIndependentOfContext("a * --b;"); 9698 verifyIndependentOfContext("a[4] * b;"); 9699 verifyIndependentOfContext("a[a * a] = 1;"); 9700 verifyIndependentOfContext("f() * b;"); 9701 verifyIndependentOfContext("a * [self dostuff];"); 9702 verifyIndependentOfContext("int x = a * (a + b);"); 9703 verifyIndependentOfContext("(a *)(a + b);"); 9704 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9705 verifyIndependentOfContext("int *pa = (int *)&a;"); 9706 verifyIndependentOfContext("return sizeof(int **);"); 9707 verifyIndependentOfContext("return sizeof(int ******);"); 9708 verifyIndependentOfContext("return (int **&)a;"); 9709 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9710 verifyFormat("void f(Type (*parameter)[10]) {}"); 9711 verifyFormat("void f(Type (¶meter)[10]) {}"); 9712 verifyGoogleFormat("return sizeof(int**);"); 9713 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9714 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9715 verifyFormat("auto a = [](int **&, int ***) {};"); 9716 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9717 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9718 verifyFormat("[](const decltype(*a) &value) {}"); 9719 verifyFormat("[](const typeof(*a) &value) {}"); 9720 verifyFormat("[](const _Atomic(a *) &value) {}"); 9721 verifyFormat("[](const __underlying_type(a) &value) {}"); 9722 verifyFormat("decltype(a * b) F();"); 9723 verifyFormat("typeof(a * b) F();"); 9724 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9725 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9726 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9727 verifyIndependentOfContext("int i{a * b};"); 9728 verifyIndependentOfContext("aaa && aaa->f();"); 9729 verifyIndependentOfContext("int x = ~*p;"); 9730 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9731 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9732 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9733 verifyFormat("void f() { f(a, c * d); }"); 9734 verifyFormat("void f() { f(new a(), c * d); }"); 9735 verifyFormat("void f(const MyOverride &override);"); 9736 verifyFormat("void f(const MyFinal &final);"); 9737 verifyIndependentOfContext("bool a = f() && override.f();"); 9738 verifyIndependentOfContext("bool a = f() && final.f();"); 9739 9740 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9741 9742 verifyIndependentOfContext("A<int *> a;"); 9743 verifyIndependentOfContext("A<int **> a;"); 9744 verifyIndependentOfContext("A<int *, int *> a;"); 9745 verifyIndependentOfContext("A<int *[]> a;"); 9746 verifyIndependentOfContext( 9747 "const char *const p = reinterpret_cast<const char *const>(q);"); 9748 verifyIndependentOfContext("A<int **, int **> a;"); 9749 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9750 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9751 verifyFormat("for (; a && b;) {\n}"); 9752 verifyFormat("bool foo = true && [] { return false; }();"); 9753 9754 verifyFormat( 9755 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9757 9758 verifyGoogleFormat("int const* a = &b;"); 9759 verifyGoogleFormat("**outparam = 1;"); 9760 verifyGoogleFormat("*outparam = a * b;"); 9761 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9762 verifyGoogleFormat("A<int*> a;"); 9763 verifyGoogleFormat("A<int**> a;"); 9764 verifyGoogleFormat("A<int*, int*> a;"); 9765 verifyGoogleFormat("A<int**, int**> a;"); 9766 verifyGoogleFormat("f(b ? *c : *d);"); 9767 verifyGoogleFormat("int a = b ? *c : *d;"); 9768 verifyGoogleFormat("Type* t = **x;"); 9769 verifyGoogleFormat("Type* t = *++*x;"); 9770 verifyGoogleFormat("*++*x;"); 9771 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9772 verifyGoogleFormat("Type* t = x++ * y;"); 9773 verifyGoogleFormat( 9774 "const char* const p = reinterpret_cast<const char* const>(q);"); 9775 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9776 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9777 verifyGoogleFormat("template <typename T>\n" 9778 "void f(int i = 0, SomeType** temps = NULL);"); 9779 9780 FormatStyle Left = getLLVMStyle(); 9781 Left.PointerAlignment = FormatStyle::PAS_Left; 9782 verifyFormat("x = *a(x) = *a(y);", Left); 9783 verifyFormat("for (;; *a = b) {\n}", Left); 9784 verifyFormat("return *this += 1;", Left); 9785 verifyFormat("throw *x;", Left); 9786 verifyFormat("delete *x;", Left); 9787 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9788 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9789 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9790 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9791 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9792 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9793 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9794 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9795 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9796 9797 verifyIndependentOfContext("a = *(x + y);"); 9798 verifyIndependentOfContext("a = &(x + y);"); 9799 verifyIndependentOfContext("*(x + y).call();"); 9800 verifyIndependentOfContext("&(x + y)->call();"); 9801 verifyFormat("void f() { &(*I).first; }"); 9802 9803 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9804 verifyFormat("f(* /* confusing comment */ foo);"); 9805 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 9806 verifyFormat("void foo(int * // this is the first paramters\n" 9807 " ,\n" 9808 " int second);"); 9809 verifyFormat("double term = a * // first\n" 9810 " b;"); 9811 verifyFormat( 9812 "int *MyValues = {\n" 9813 " *A, // Operator detection might be confused by the '{'\n" 9814 " *BB // Operator detection might be confused by previous comment\n" 9815 "};"); 9816 9817 verifyIndependentOfContext("if (int *a = &b)"); 9818 verifyIndependentOfContext("if (int &a = *b)"); 9819 verifyIndependentOfContext("if (a & b[i])"); 9820 verifyIndependentOfContext("if constexpr (a & b[i])"); 9821 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9822 verifyIndependentOfContext("if (a * (b * c))"); 9823 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9824 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9825 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9826 verifyIndependentOfContext("if (*b[i])"); 9827 verifyIndependentOfContext("if (int *a = (&b))"); 9828 verifyIndependentOfContext("while (int *a = &b)"); 9829 verifyIndependentOfContext("while (a * (b * c))"); 9830 verifyIndependentOfContext("size = sizeof *a;"); 9831 verifyIndependentOfContext("if (a && (b = c))"); 9832 verifyFormat("void f() {\n" 9833 " for (const int &v : Values) {\n" 9834 " }\n" 9835 "}"); 9836 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9837 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9838 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9839 9840 verifyFormat("#define A (!a * b)"); 9841 verifyFormat("#define MACRO \\\n" 9842 " int *i = a * b; \\\n" 9843 " void f(a *b);", 9844 getLLVMStyleWithColumns(19)); 9845 9846 verifyIndependentOfContext("A = new SomeType *[Length];"); 9847 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9848 verifyIndependentOfContext("T **t = new T *;"); 9849 verifyIndependentOfContext("T **t = new T *();"); 9850 verifyGoogleFormat("A = new SomeType*[Length]();"); 9851 verifyGoogleFormat("A = new SomeType*[Length];"); 9852 verifyGoogleFormat("T** t = new T*;"); 9853 verifyGoogleFormat("T** t = new T*();"); 9854 9855 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9856 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9857 verifyFormat("template <bool a, bool b> " 9858 "typename t::if<x && y>::type f() {}"); 9859 verifyFormat("template <int *y> f() {}"); 9860 verifyFormat("vector<int *> v;"); 9861 verifyFormat("vector<int *const> v;"); 9862 verifyFormat("vector<int *const **const *> v;"); 9863 verifyFormat("vector<int *volatile> v;"); 9864 verifyFormat("vector<a *_Nonnull> v;"); 9865 verifyFormat("vector<a *_Nullable> v;"); 9866 verifyFormat("vector<a *_Null_unspecified> v;"); 9867 verifyFormat("vector<a *__ptr32> v;"); 9868 verifyFormat("vector<a *__ptr64> v;"); 9869 verifyFormat("vector<a *__capability> v;"); 9870 FormatStyle TypeMacros = getLLVMStyle(); 9871 TypeMacros.TypenameMacros = {"LIST"}; 9872 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9873 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9874 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9875 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9876 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9877 9878 FormatStyle CustomQualifier = getLLVMStyle(); 9879 // Add identifiers that should not be parsed as a qualifier by default. 9880 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9881 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9882 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9883 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9884 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9885 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9886 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9887 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9888 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9889 verifyFormat("vector<a * _NotAQualifier> v;"); 9890 verifyFormat("vector<a * __not_a_qualifier> v;"); 9891 verifyFormat("vector<a * b> v;"); 9892 verifyFormat("foo<b && false>();"); 9893 verifyFormat("foo<b & 1>();"); 9894 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9895 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9896 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9897 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9898 verifyFormat( 9899 "template <class T, class = typename std::enable_if<\n" 9900 " std::is_integral<T>::value &&\n" 9901 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9902 "void F();", 9903 getLLVMStyleWithColumns(70)); 9904 verifyFormat("template <class T,\n" 9905 " class = typename std::enable_if<\n" 9906 " std::is_integral<T>::value &&\n" 9907 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9908 " class U>\n" 9909 "void F();", 9910 getLLVMStyleWithColumns(70)); 9911 verifyFormat( 9912 "template <class T,\n" 9913 " class = typename ::std::enable_if<\n" 9914 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9915 "void F();", 9916 getGoogleStyleWithColumns(68)); 9917 9918 verifyIndependentOfContext("MACRO(int *i);"); 9919 verifyIndependentOfContext("MACRO(auto *a);"); 9920 verifyIndependentOfContext("MACRO(const A *a);"); 9921 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9922 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9923 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9924 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9925 verifyIndependentOfContext("MACRO(A *const a);"); 9926 verifyIndependentOfContext("MACRO(A *restrict a);"); 9927 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9928 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9929 verifyIndependentOfContext("MACRO(A *volatile a);"); 9930 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9931 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9932 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9933 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9934 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9935 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9936 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9937 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9938 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9939 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9940 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9941 verifyIndependentOfContext("MACRO(A *__capability);"); 9942 verifyIndependentOfContext("MACRO(A &__capability);"); 9943 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9944 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9945 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9946 // a type declaration: 9947 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9948 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9949 // Also check that TypenameMacros prevents parsing it as multiplication: 9950 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9951 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9952 9953 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9954 verifyFormat("void f() { f(float{1}, a * a); }"); 9955 verifyFormat("void f() { f(float(1), a * a); }"); 9956 9957 verifyFormat("f((void (*)(int))g);"); 9958 verifyFormat("f((void (&)(int))g);"); 9959 verifyFormat("f((void (^)(int))g);"); 9960 9961 // FIXME: Is there a way to make this work? 9962 // verifyIndependentOfContext("MACRO(A *a);"); 9963 verifyFormat("MACRO(A &B);"); 9964 verifyFormat("MACRO(A *B);"); 9965 verifyFormat("void f() { MACRO(A * B); }"); 9966 verifyFormat("void f() { MACRO(A & B); }"); 9967 9968 // This lambda was mis-formatted after D88956 (treating it as a binop): 9969 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9970 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9971 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9972 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9973 9974 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9975 verifyFormat("return options != nullptr && operator==(*options);"); 9976 9977 EXPECT_EQ("#define OP(x) \\\n" 9978 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9979 " return s << a.DebugString(); \\\n" 9980 " }", 9981 format("#define OP(x) \\\n" 9982 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9983 " return s << a.DebugString(); \\\n" 9984 " }", 9985 getLLVMStyleWithColumns(50))); 9986 9987 // FIXME: We cannot handle this case yet; we might be able to figure out that 9988 // foo<x> d > v; doesn't make sense. 9989 verifyFormat("foo<a<b && c> d> v;"); 9990 9991 FormatStyle PointerMiddle = getLLVMStyle(); 9992 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9993 verifyFormat("delete *x;", PointerMiddle); 9994 verifyFormat("int * x;", PointerMiddle); 9995 verifyFormat("int *[] x;", PointerMiddle); 9996 verifyFormat("template <int * y> f() {}", PointerMiddle); 9997 verifyFormat("int * f(int * a) {}", PointerMiddle); 9998 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9999 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 10000 verifyFormat("A<int *> a;", PointerMiddle); 10001 verifyFormat("A<int **> a;", PointerMiddle); 10002 verifyFormat("A<int *, int *> a;", PointerMiddle); 10003 verifyFormat("A<int *[]> a;", PointerMiddle); 10004 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 10005 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 10006 verifyFormat("T ** t = new T *;", PointerMiddle); 10007 10008 // Member function reference qualifiers aren't binary operators. 10009 verifyFormat("string // break\n" 10010 "operator()() & {}"); 10011 verifyFormat("string // break\n" 10012 "operator()() && {}"); 10013 verifyGoogleFormat("template <typename T>\n" 10014 "auto x() & -> int {}"); 10015 10016 // Should be binary operators when used as an argument expression (overloaded 10017 // operator invoked as a member function). 10018 verifyFormat("void f() { a.operator()(a * a); }"); 10019 verifyFormat("void f() { a->operator()(a & a); }"); 10020 verifyFormat("void f() { a.operator()(*a & *a); }"); 10021 verifyFormat("void f() { a->operator()(*a * *a); }"); 10022 10023 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 10024 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 10025 } 10026 10027 TEST_F(FormatTest, UnderstandsAttributes) { 10028 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 10029 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 10030 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10031 verifyFormat("__attribute__((nodebug)) ::qualified_type f();"); 10032 FormatStyle AfterType = getLLVMStyle(); 10033 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10034 verifyFormat("__attribute__((nodebug)) void\n" 10035 "foo() {}\n", 10036 AfterType); 10037 verifyFormat("__unused void\n" 10038 "foo() {}", 10039 AfterType); 10040 10041 FormatStyle CustomAttrs = getLLVMStyle(); 10042 CustomAttrs.AttributeMacros.push_back("__unused"); 10043 CustomAttrs.AttributeMacros.push_back("__attr1"); 10044 CustomAttrs.AttributeMacros.push_back("__attr2"); 10045 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 10046 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 10047 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 10048 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 10049 // Check that it is parsed as a multiplication without AttributeMacros and 10050 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 10051 verifyFormat("vector<SomeType * __attr1> v;"); 10052 verifyFormat("vector<SomeType __attr1 *> v;"); 10053 verifyFormat("vector<SomeType __attr1 *const> v;"); 10054 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 10055 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 10056 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 10057 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 10058 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 10059 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 10060 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 10061 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 10062 10063 // Check that these are not parsed as function declarations: 10064 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10065 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 10066 verifyFormat("SomeType s(InitValue);", CustomAttrs); 10067 verifyFormat("SomeType s{InitValue};", CustomAttrs); 10068 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 10069 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 10070 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 10071 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 10072 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 10073 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 10074 } 10075 10076 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 10077 // Check that qualifiers on pointers don't break parsing of casts. 10078 verifyFormat("x = (foo *const)*v;"); 10079 verifyFormat("x = (foo *volatile)*v;"); 10080 verifyFormat("x = (foo *restrict)*v;"); 10081 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 10082 verifyFormat("x = (foo *_Nonnull)*v;"); 10083 verifyFormat("x = (foo *_Nullable)*v;"); 10084 verifyFormat("x = (foo *_Null_unspecified)*v;"); 10085 verifyFormat("x = (foo *_Nonnull)*v;"); 10086 verifyFormat("x = (foo *[[clang::attr]])*v;"); 10087 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 10088 verifyFormat("x = (foo *__ptr32)*v;"); 10089 verifyFormat("x = (foo *__ptr64)*v;"); 10090 verifyFormat("x = (foo *__capability)*v;"); 10091 10092 // Check that we handle multiple trailing qualifiers and skip them all to 10093 // determine that the expression is a cast to a pointer type. 10094 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 10095 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 10096 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 10097 StringRef AllQualifiers = 10098 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 10099 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 10100 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 10101 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 10102 10103 // Also check that address-of is not parsed as a binary bitwise-and: 10104 verifyFormat("x = (foo *const)&v;"); 10105 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 10106 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 10107 10108 // Check custom qualifiers: 10109 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 10110 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 10111 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 10112 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 10113 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 10114 CustomQualifier); 10115 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 10116 CustomQualifier); 10117 10118 // Check that unknown identifiers result in binary operator parsing: 10119 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 10120 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 10121 } 10122 10123 TEST_F(FormatTest, UnderstandsSquareAttributes) { 10124 verifyFormat("SomeType s [[unused]] (InitValue);"); 10125 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 10126 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 10127 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 10128 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 10129 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10130 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10131 verifyFormat("[[nodiscard]] bool f() { return false; }"); 10132 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 10133 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 10134 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 10135 verifyFormat("[[nodiscard]] ::qualified_type f();"); 10136 10137 // Make sure we do not mistake attributes for array subscripts. 10138 verifyFormat("int a() {}\n" 10139 "[[unused]] int b() {}\n"); 10140 verifyFormat("NSArray *arr;\n" 10141 "arr[[Foo() bar]];"); 10142 10143 // On the other hand, we still need to correctly find array subscripts. 10144 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 10145 10146 // Make sure that we do not mistake Objective-C method inside array literals 10147 // as attributes, even if those method names are also keywords. 10148 verifyFormat("@[ [foo bar] ];"); 10149 verifyFormat("@[ [NSArray class] ];"); 10150 verifyFormat("@[ [foo enum] ];"); 10151 10152 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 10153 10154 // Make sure we do not parse attributes as lambda introducers. 10155 FormatStyle MultiLineFunctions = getLLVMStyle(); 10156 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10157 verifyFormat("[[unused]] int b() {\n" 10158 " return 42;\n" 10159 "}\n", 10160 MultiLineFunctions); 10161 } 10162 10163 TEST_F(FormatTest, AttributeClass) { 10164 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 10165 verifyFormat("class S {\n" 10166 " S(S&&) = default;\n" 10167 "};", 10168 Style); 10169 verifyFormat("class [[nodiscard]] S {\n" 10170 " S(S&&) = default;\n" 10171 "};", 10172 Style); 10173 verifyFormat("class __attribute((maybeunused)) S {\n" 10174 " S(S&&) = default;\n" 10175 "};", 10176 Style); 10177 verifyFormat("struct S {\n" 10178 " S(S&&) = default;\n" 10179 "};", 10180 Style); 10181 verifyFormat("struct [[nodiscard]] S {\n" 10182 " S(S&&) = default;\n" 10183 "};", 10184 Style); 10185 } 10186 10187 TEST_F(FormatTest, AttributesAfterMacro) { 10188 FormatStyle Style = getLLVMStyle(); 10189 verifyFormat("MACRO;\n" 10190 "__attribute__((maybe_unused)) int foo() {\n" 10191 " //...\n" 10192 "}"); 10193 10194 verifyFormat("MACRO;\n" 10195 "[[nodiscard]] int foo() {\n" 10196 " //...\n" 10197 "}"); 10198 10199 EXPECT_EQ("MACRO\n\n" 10200 "__attribute__((maybe_unused)) int foo() {\n" 10201 " //...\n" 10202 "}", 10203 format("MACRO\n\n" 10204 "__attribute__((maybe_unused)) int foo() {\n" 10205 " //...\n" 10206 "}")); 10207 10208 EXPECT_EQ("MACRO\n\n" 10209 "[[nodiscard]] int foo() {\n" 10210 " //...\n" 10211 "}", 10212 format("MACRO\n\n" 10213 "[[nodiscard]] int foo() {\n" 10214 " //...\n" 10215 "}")); 10216 } 10217 10218 TEST_F(FormatTest, AttributePenaltyBreaking) { 10219 FormatStyle Style = getLLVMStyle(); 10220 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 10221 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10222 Style); 10223 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 10224 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10225 Style); 10226 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 10227 "shared_ptr<ALongTypeName> &C d) {\n}", 10228 Style); 10229 } 10230 10231 TEST_F(FormatTest, UnderstandsEllipsis) { 10232 FormatStyle Style = getLLVMStyle(); 10233 verifyFormat("int printf(const char *fmt, ...);"); 10234 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 10235 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 10236 10237 verifyFormat("template <int *...PP> a;", Style); 10238 10239 Style.PointerAlignment = FormatStyle::PAS_Left; 10240 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 10241 10242 verifyFormat("template <int*... PP> a;", Style); 10243 10244 Style.PointerAlignment = FormatStyle::PAS_Middle; 10245 verifyFormat("template <int *... PP> a;", Style); 10246 } 10247 10248 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 10249 EXPECT_EQ("int *a;\n" 10250 "int *a;\n" 10251 "int *a;", 10252 format("int *a;\n" 10253 "int* a;\n" 10254 "int *a;", 10255 getGoogleStyle())); 10256 EXPECT_EQ("int* a;\n" 10257 "int* a;\n" 10258 "int* a;", 10259 format("int* a;\n" 10260 "int* a;\n" 10261 "int *a;", 10262 getGoogleStyle())); 10263 EXPECT_EQ("int *a;\n" 10264 "int *a;\n" 10265 "int *a;", 10266 format("int *a;\n" 10267 "int * a;\n" 10268 "int * a;", 10269 getGoogleStyle())); 10270 EXPECT_EQ("auto x = [] {\n" 10271 " int *a;\n" 10272 " int *a;\n" 10273 " int *a;\n" 10274 "};", 10275 format("auto x=[]{int *a;\n" 10276 "int * a;\n" 10277 "int * a;};", 10278 getGoogleStyle())); 10279 } 10280 10281 TEST_F(FormatTest, UnderstandsRvalueReferences) { 10282 verifyFormat("int f(int &&a) {}"); 10283 verifyFormat("int f(int a, char &&b) {}"); 10284 verifyFormat("void f() { int &&a = b; }"); 10285 verifyGoogleFormat("int f(int a, char&& b) {}"); 10286 verifyGoogleFormat("void f() { int&& a = b; }"); 10287 10288 verifyIndependentOfContext("A<int &&> a;"); 10289 verifyIndependentOfContext("A<int &&, int &&> a;"); 10290 verifyGoogleFormat("A<int&&> a;"); 10291 verifyGoogleFormat("A<int&&, int&&> a;"); 10292 10293 // Not rvalue references: 10294 verifyFormat("template <bool B, bool C> class A {\n" 10295 " static_assert(B && C, \"Something is wrong\");\n" 10296 "};"); 10297 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 10298 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 10299 verifyFormat("#define A(a, b) (a && b)"); 10300 } 10301 10302 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 10303 verifyFormat("void f() {\n" 10304 " x[aaaaaaaaa -\n" 10305 " b] = 23;\n" 10306 "}", 10307 getLLVMStyleWithColumns(15)); 10308 } 10309 10310 TEST_F(FormatTest, FormatsCasts) { 10311 verifyFormat("Type *A = static_cast<Type *>(P);"); 10312 verifyFormat("Type *A = (Type *)P;"); 10313 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 10314 verifyFormat("int a = (int)(2.0f);"); 10315 verifyFormat("int a = (int)2.0f;"); 10316 verifyFormat("x[(int32)y];"); 10317 verifyFormat("x = (int32)y;"); 10318 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 10319 verifyFormat("int a = (int)*b;"); 10320 verifyFormat("int a = (int)2.0f;"); 10321 verifyFormat("int a = (int)~0;"); 10322 verifyFormat("int a = (int)++a;"); 10323 verifyFormat("int a = (int)sizeof(int);"); 10324 verifyFormat("int a = (int)+2;"); 10325 verifyFormat("my_int a = (my_int)2.0f;"); 10326 verifyFormat("my_int a = (my_int)sizeof(int);"); 10327 verifyFormat("return (my_int)aaa;"); 10328 verifyFormat("#define x ((int)-1)"); 10329 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 10330 verifyFormat("#define p(q) ((int *)&q)"); 10331 verifyFormat("fn(a)(b) + 1;"); 10332 10333 verifyFormat("void f() { my_int a = (my_int)*b; }"); 10334 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 10335 verifyFormat("my_int a = (my_int)~0;"); 10336 verifyFormat("my_int a = (my_int)++a;"); 10337 verifyFormat("my_int a = (my_int)-2;"); 10338 verifyFormat("my_int a = (my_int)1;"); 10339 verifyFormat("my_int a = (my_int *)1;"); 10340 verifyFormat("my_int a = (const my_int)-1;"); 10341 verifyFormat("my_int a = (const my_int *)-1;"); 10342 verifyFormat("my_int a = (my_int)(my_int)-1;"); 10343 verifyFormat("my_int a = (ns::my_int)-2;"); 10344 verifyFormat("case (my_int)ONE:"); 10345 verifyFormat("auto x = (X)this;"); 10346 // Casts in Obj-C style calls used to not be recognized as such. 10347 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 10348 10349 // FIXME: single value wrapped with paren will be treated as cast. 10350 verifyFormat("void f(int i = (kValue)*kMask) {}"); 10351 10352 verifyFormat("{ (void)F; }"); 10353 10354 // Don't break after a cast's 10355 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10356 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 10357 " bbbbbbbbbbbbbbbbbbbbbb);"); 10358 10359 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)"); 10360 verifyFormat("#define CONF_BOOL(x) (bool *)(x)"); 10361 verifyFormat("#define CONF_BOOL(x) (bool)(x)"); 10362 verifyFormat("bool *y = (bool *)(void *)(x);"); 10363 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)"); 10364 verifyFormat("bool *y = (bool *)(void *)(int)(x);"); 10365 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)"); 10366 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);"); 10367 10368 // These are not casts. 10369 verifyFormat("void f(int *) {}"); 10370 verifyFormat("f(foo)->b;"); 10371 verifyFormat("f(foo).b;"); 10372 verifyFormat("f(foo)(b);"); 10373 verifyFormat("f(foo)[b];"); 10374 verifyFormat("[](foo) { return 4; }(bar);"); 10375 verifyFormat("(*funptr)(foo)[4];"); 10376 verifyFormat("funptrs[4](foo)[4];"); 10377 verifyFormat("void f(int *);"); 10378 verifyFormat("void f(int *) = 0;"); 10379 verifyFormat("void f(SmallVector<int>) {}"); 10380 verifyFormat("void f(SmallVector<int>);"); 10381 verifyFormat("void f(SmallVector<int>) = 0;"); 10382 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 10383 verifyFormat("int a = sizeof(int) * b;"); 10384 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 10385 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 10386 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 10387 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 10388 10389 // These are not casts, but at some point were confused with casts. 10390 verifyFormat("virtual void foo(int *) override;"); 10391 verifyFormat("virtual void foo(char &) const;"); 10392 verifyFormat("virtual void foo(int *a, char *) const;"); 10393 verifyFormat("int a = sizeof(int *) + b;"); 10394 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10395 verifyFormat("bool b = f(g<int>) && c;"); 10396 verifyFormat("typedef void (*f)(int i) func;"); 10397 verifyFormat("void operator++(int) noexcept;"); 10398 verifyFormat("void operator++(int &) noexcept;"); 10399 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10400 "&) noexcept;"); 10401 verifyFormat( 10402 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10403 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10404 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10405 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10406 verifyFormat("void operator delete(foo &) noexcept;"); 10407 verifyFormat("void operator delete(foo) noexcept;"); 10408 verifyFormat("void operator delete(int) noexcept;"); 10409 verifyFormat("void operator delete(int &) noexcept;"); 10410 verifyFormat("void operator delete(int &) volatile noexcept;"); 10411 verifyFormat("void operator delete(int &) const"); 10412 verifyFormat("void operator delete(int &) = default"); 10413 verifyFormat("void operator delete(int &) = delete"); 10414 verifyFormat("void operator delete(int &) [[noreturn]]"); 10415 verifyFormat("void operator delete(int &) throw();"); 10416 verifyFormat("void operator delete(int &) throw(int);"); 10417 verifyFormat("auto operator delete(int &) -> int;"); 10418 verifyFormat("auto operator delete(int &) override"); 10419 verifyFormat("auto operator delete(int &) final"); 10420 10421 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10422 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10423 // FIXME: The indentation here is not ideal. 10424 verifyFormat( 10425 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10426 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10427 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10428 } 10429 10430 TEST_F(FormatTest, FormatsFunctionTypes) { 10431 verifyFormat("A<bool()> a;"); 10432 verifyFormat("A<SomeType()> a;"); 10433 verifyFormat("A<void (*)(int, std::string)> a;"); 10434 verifyFormat("A<void *(int)>;"); 10435 verifyFormat("void *(*a)(int *, SomeType *);"); 10436 verifyFormat("int (*func)(void *);"); 10437 verifyFormat("void f() { int (*func)(void *); }"); 10438 verifyFormat("template <class CallbackClass>\n" 10439 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10440 10441 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10442 verifyGoogleFormat("void* (*a)(int);"); 10443 verifyGoogleFormat( 10444 "template <class CallbackClass>\n" 10445 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10446 10447 // Other constructs can look somewhat like function types: 10448 verifyFormat("A<sizeof(*x)> a;"); 10449 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10450 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10451 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10452 verifyFormat("int x = f(&h)();"); 10453 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10454 verifyFormat("std::function<\n" 10455 " LooooooooooongTemplatedType<\n" 10456 " SomeType>*(\n" 10457 " LooooooooooooooooongType type)>\n" 10458 " function;", 10459 getGoogleStyleWithColumns(40)); 10460 } 10461 10462 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10463 verifyFormat("A (*foo_)[6];"); 10464 verifyFormat("vector<int> (*foo_)[6];"); 10465 } 10466 10467 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10468 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10469 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10470 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10471 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10472 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10473 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10474 10475 // Different ways of ()-initializiation. 10476 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10477 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10478 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10479 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10480 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10481 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10482 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10483 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10484 10485 // Lambdas should not confuse the variable declaration heuristic. 10486 verifyFormat("LooooooooooooooooongType\n" 10487 " variable(nullptr, [](A *a) {});", 10488 getLLVMStyleWithColumns(40)); 10489 } 10490 10491 TEST_F(FormatTest, BreaksLongDeclarations) { 10492 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10493 " AnotherNameForTheLongType;"); 10494 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10496 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10497 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10498 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10499 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10500 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10501 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10502 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10503 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10504 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10505 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10506 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10507 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10508 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10509 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10510 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10511 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10512 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10513 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10514 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10515 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10516 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10517 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10518 FormatStyle Indented = getLLVMStyle(); 10519 Indented.IndentWrappedFunctionNames = true; 10520 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10521 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10522 Indented); 10523 verifyFormat( 10524 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10525 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10526 Indented); 10527 verifyFormat( 10528 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10529 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10530 Indented); 10531 verifyFormat( 10532 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10533 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10534 Indented); 10535 10536 // FIXME: Without the comment, this breaks after "(". 10537 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10538 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10539 getGoogleStyle()); 10540 10541 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10542 " int LoooooooooooooooooooongParam2) {}"); 10543 verifyFormat( 10544 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10545 " SourceLocation L, IdentifierIn *II,\n" 10546 " Type *T) {}"); 10547 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10548 "ReallyReaaallyLongFunctionName(\n" 10549 " const std::string &SomeParameter,\n" 10550 " const SomeType<string, SomeOtherTemplateParameter>\n" 10551 " &ReallyReallyLongParameterName,\n" 10552 " const SomeType<string, SomeOtherTemplateParameter>\n" 10553 " &AnotherLongParameterName) {}"); 10554 verifyFormat("template <typename A>\n" 10555 "SomeLoooooooooooooooooooooongType<\n" 10556 " typename some_namespace::SomeOtherType<A>::Type>\n" 10557 "Function() {}"); 10558 10559 verifyGoogleFormat( 10560 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10561 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10562 verifyGoogleFormat( 10563 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10564 " SourceLocation L) {}"); 10565 verifyGoogleFormat( 10566 "some_namespace::LongReturnType\n" 10567 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10568 " int first_long_parameter, int second_parameter) {}"); 10569 10570 verifyGoogleFormat("template <typename T>\n" 10571 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10572 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10573 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10574 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10575 10576 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10577 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10578 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10579 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10580 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10581 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10582 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10583 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10585 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10586 10587 verifyFormat("template <typename T> // Templates on own line.\n" 10588 "static int // Some comment.\n" 10589 "MyFunction(int a);", 10590 getLLVMStyle()); 10591 } 10592 10593 TEST_F(FormatTest, FormatsAccessModifiers) { 10594 FormatStyle Style = getLLVMStyle(); 10595 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10596 FormatStyle::ELBAMS_LogicalBlock); 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 verifyFormat("struct foo {\n" 10609 "private:\n" 10610 " void f() {}\n" 10611 "\n" 10612 "private:\n" 10613 " int i;\n" 10614 "\n" 10615 "protected:\n" 10616 " int j;\n" 10617 "};\n", 10618 "struct foo {\n" 10619 "private:\n" 10620 " void f() {}\n" 10621 "private:\n" 10622 " int i;\n" 10623 "protected:\n" 10624 " int j;\n" 10625 "};\n", 10626 Style); 10627 verifyFormat("struct foo { /* comment */\n" 10628 "private:\n" 10629 " int i;\n" 10630 " // comment\n" 10631 "private:\n" 10632 " int j;\n" 10633 "};\n", 10634 Style); 10635 verifyFormat("struct foo {\n" 10636 "#ifdef FOO\n" 10637 "#endif\n" 10638 "private:\n" 10639 " int i;\n" 10640 "#ifdef FOO\n" 10641 "private:\n" 10642 "#endif\n" 10643 " int j;\n" 10644 "};\n", 10645 Style); 10646 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10647 verifyFormat("struct foo {\n" 10648 "private:\n" 10649 " void f() {}\n" 10650 "private:\n" 10651 " int i;\n" 10652 "protected:\n" 10653 " int j;\n" 10654 "};\n", 10655 Style); 10656 verifyFormat("struct foo {\n" 10657 "private:\n" 10658 " void f() {}\n" 10659 "private:\n" 10660 " int i;\n" 10661 "protected:\n" 10662 " int j;\n" 10663 "};\n", 10664 "struct foo {\n" 10665 "\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 verifyFormat("struct foo { /* comment */\n" 10677 "private:\n" 10678 " int i;\n" 10679 " // comment\n" 10680 "private:\n" 10681 " int j;\n" 10682 "};\n", 10683 "struct foo { /* comment */\n" 10684 "\n" 10685 "private:\n" 10686 " int i;\n" 10687 " // comment\n" 10688 "\n" 10689 "private:\n" 10690 " int j;\n" 10691 "};\n", 10692 Style); 10693 verifyFormat("struct foo {\n" 10694 "#ifdef FOO\n" 10695 "#endif\n" 10696 "private:\n" 10697 " int i;\n" 10698 "#ifdef FOO\n" 10699 "private:\n" 10700 "#endif\n" 10701 " int j;\n" 10702 "};\n", 10703 "struct foo {\n" 10704 "#ifdef FOO\n" 10705 "#endif\n" 10706 "\n" 10707 "private:\n" 10708 " int i;\n" 10709 "#ifdef FOO\n" 10710 "\n" 10711 "private:\n" 10712 "#endif\n" 10713 " int j;\n" 10714 "};\n", 10715 Style); 10716 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10717 verifyFormat("struct foo {\n" 10718 "private:\n" 10719 " void f() {}\n" 10720 "\n" 10721 "private:\n" 10722 " int i;\n" 10723 "\n" 10724 "protected:\n" 10725 " int j;\n" 10726 "};\n", 10727 Style); 10728 verifyFormat("struct foo {\n" 10729 "private:\n" 10730 " void f() {}\n" 10731 "\n" 10732 "private:\n" 10733 " int i;\n" 10734 "\n" 10735 "protected:\n" 10736 " int j;\n" 10737 "};\n", 10738 "struct foo {\n" 10739 "private:\n" 10740 " void f() {}\n" 10741 "private:\n" 10742 " int i;\n" 10743 "protected:\n" 10744 " int j;\n" 10745 "};\n", 10746 Style); 10747 verifyFormat("struct foo { /* comment */\n" 10748 "private:\n" 10749 " int i;\n" 10750 " // comment\n" 10751 "\n" 10752 "private:\n" 10753 " int j;\n" 10754 "};\n", 10755 "struct foo { /* comment */\n" 10756 "private:\n" 10757 " int i;\n" 10758 " // comment\n" 10759 "\n" 10760 "private:\n" 10761 " int j;\n" 10762 "};\n", 10763 Style); 10764 verifyFormat("struct foo {\n" 10765 "#ifdef FOO\n" 10766 "#endif\n" 10767 "\n" 10768 "private:\n" 10769 " int i;\n" 10770 "#ifdef FOO\n" 10771 "\n" 10772 "private:\n" 10773 "#endif\n" 10774 " int j;\n" 10775 "};\n", 10776 "struct foo {\n" 10777 "#ifdef FOO\n" 10778 "#endif\n" 10779 "private:\n" 10780 " int i;\n" 10781 "#ifdef FOO\n" 10782 "private:\n" 10783 "#endif\n" 10784 " int j;\n" 10785 "};\n", 10786 Style); 10787 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10788 EXPECT_EQ("struct foo {\n" 10789 "\n" 10790 "private:\n" 10791 " void f() {}\n" 10792 "\n" 10793 "private:\n" 10794 " int i;\n" 10795 "\n" 10796 "protected:\n" 10797 " int j;\n" 10798 "};\n", 10799 format("struct foo {\n" 10800 "\n" 10801 "private:\n" 10802 " void f() {}\n" 10803 "\n" 10804 "private:\n" 10805 " int i;\n" 10806 "\n" 10807 "protected:\n" 10808 " int j;\n" 10809 "};\n", 10810 Style)); 10811 verifyFormat("struct foo {\n" 10812 "private:\n" 10813 " void f() {}\n" 10814 "private:\n" 10815 " int i;\n" 10816 "protected:\n" 10817 " int j;\n" 10818 "};\n", 10819 Style); 10820 EXPECT_EQ("struct foo { /* comment */\n" 10821 "\n" 10822 "private:\n" 10823 " int i;\n" 10824 " // comment\n" 10825 "\n" 10826 "private:\n" 10827 " int j;\n" 10828 "};\n", 10829 format("struct foo { /* comment */\n" 10830 "\n" 10831 "private:\n" 10832 " int i;\n" 10833 " // comment\n" 10834 "\n" 10835 "private:\n" 10836 " int j;\n" 10837 "};\n", 10838 Style)); 10839 verifyFormat("struct foo { /* comment */\n" 10840 "private:\n" 10841 " int i;\n" 10842 " // comment\n" 10843 "private:\n" 10844 " int j;\n" 10845 "};\n", 10846 Style); 10847 EXPECT_EQ("struct foo {\n" 10848 "#ifdef FOO\n" 10849 "#endif\n" 10850 "\n" 10851 "private:\n" 10852 " int i;\n" 10853 "#ifdef FOO\n" 10854 "\n" 10855 "private:\n" 10856 "#endif\n" 10857 " int j;\n" 10858 "};\n", 10859 format("struct foo {\n" 10860 "#ifdef FOO\n" 10861 "#endif\n" 10862 "\n" 10863 "private:\n" 10864 " int i;\n" 10865 "#ifdef FOO\n" 10866 "\n" 10867 "private:\n" 10868 "#endif\n" 10869 " int j;\n" 10870 "};\n", 10871 Style)); 10872 verifyFormat("struct foo {\n" 10873 "#ifdef FOO\n" 10874 "#endif\n" 10875 "private:\n" 10876 " int i;\n" 10877 "#ifdef FOO\n" 10878 "private:\n" 10879 "#endif\n" 10880 " int j;\n" 10881 "};\n", 10882 Style); 10883 10884 FormatStyle NoEmptyLines = getLLVMStyle(); 10885 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10886 verifyFormat("struct foo {\n" 10887 "private:\n" 10888 " void f() {}\n" 10889 "\n" 10890 "private:\n" 10891 " int i;\n" 10892 "\n" 10893 "public:\n" 10894 "protected:\n" 10895 " int j;\n" 10896 "};\n", 10897 NoEmptyLines); 10898 10899 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10900 verifyFormat("struct foo {\n" 10901 "private:\n" 10902 " void f() {}\n" 10903 "private:\n" 10904 " int i;\n" 10905 "public:\n" 10906 "protected:\n" 10907 " int j;\n" 10908 "};\n", 10909 NoEmptyLines); 10910 10911 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10912 verifyFormat("struct foo {\n" 10913 "private:\n" 10914 " void f() {}\n" 10915 "\n" 10916 "private:\n" 10917 " int i;\n" 10918 "\n" 10919 "public:\n" 10920 "\n" 10921 "protected:\n" 10922 " int j;\n" 10923 "};\n", 10924 NoEmptyLines); 10925 } 10926 10927 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10928 10929 FormatStyle Style = getLLVMStyle(); 10930 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10931 verifyFormat("struct foo {\n" 10932 "private:\n" 10933 " void f() {}\n" 10934 "\n" 10935 "private:\n" 10936 " int i;\n" 10937 "\n" 10938 "protected:\n" 10939 " int j;\n" 10940 "};\n", 10941 Style); 10942 10943 // Check if lines are removed. 10944 verifyFormat("struct foo {\n" 10945 "private:\n" 10946 " void f() {}\n" 10947 "\n" 10948 "private:\n" 10949 " int i;\n" 10950 "\n" 10951 "protected:\n" 10952 " int j;\n" 10953 "};\n", 10954 "struct foo {\n" 10955 "private:\n" 10956 "\n" 10957 " void f() {}\n" 10958 "\n" 10959 "private:\n" 10960 "\n" 10961 " int i;\n" 10962 "\n" 10963 "protected:\n" 10964 "\n" 10965 " int j;\n" 10966 "};\n", 10967 Style); 10968 10969 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10970 verifyFormat("struct foo {\n" 10971 "private:\n" 10972 "\n" 10973 " void f() {}\n" 10974 "\n" 10975 "private:\n" 10976 "\n" 10977 " int i;\n" 10978 "\n" 10979 "protected:\n" 10980 "\n" 10981 " int j;\n" 10982 "};\n", 10983 Style); 10984 10985 // Check if lines are added. 10986 verifyFormat("struct foo {\n" 10987 "private:\n" 10988 "\n" 10989 " void f() {}\n" 10990 "\n" 10991 "private:\n" 10992 "\n" 10993 " int i;\n" 10994 "\n" 10995 "protected:\n" 10996 "\n" 10997 " int j;\n" 10998 "};\n", 10999 "struct foo {\n" 11000 "private:\n" 11001 " void f() {}\n" 11002 "\n" 11003 "private:\n" 11004 " int i;\n" 11005 "\n" 11006 "protected:\n" 11007 " int j;\n" 11008 "};\n", 11009 Style); 11010 11011 // Leave tests rely on the code layout, test::messUp can not be used. 11012 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11013 Style.MaxEmptyLinesToKeep = 0u; 11014 verifyFormat("struct foo {\n" 11015 "private:\n" 11016 " void f() {}\n" 11017 "\n" 11018 "private:\n" 11019 " int i;\n" 11020 "\n" 11021 "protected:\n" 11022 " int j;\n" 11023 "};\n", 11024 Style); 11025 11026 // Check if MaxEmptyLinesToKeep is respected. 11027 EXPECT_EQ("struct foo {\n" 11028 "private:\n" 11029 " void f() {}\n" 11030 "\n" 11031 "private:\n" 11032 " int i;\n" 11033 "\n" 11034 "protected:\n" 11035 " int j;\n" 11036 "};\n", 11037 format("struct foo {\n" 11038 "private:\n" 11039 "\n\n\n" 11040 " void f() {}\n" 11041 "\n" 11042 "private:\n" 11043 "\n\n\n" 11044 " int i;\n" 11045 "\n" 11046 "protected:\n" 11047 "\n\n\n" 11048 " int j;\n" 11049 "};\n", 11050 Style)); 11051 11052 Style.MaxEmptyLinesToKeep = 1u; 11053 EXPECT_EQ("struct foo {\n" 11054 "private:\n" 11055 "\n" 11056 " void f() {}\n" 11057 "\n" 11058 "private:\n" 11059 "\n" 11060 " int i;\n" 11061 "\n" 11062 "protected:\n" 11063 "\n" 11064 " int j;\n" 11065 "};\n", 11066 format("struct foo {\n" 11067 "private:\n" 11068 "\n" 11069 " void f() {}\n" 11070 "\n" 11071 "private:\n" 11072 "\n" 11073 " int i;\n" 11074 "\n" 11075 "protected:\n" 11076 "\n" 11077 " int j;\n" 11078 "};\n", 11079 Style)); 11080 // Check if no lines are kept. 11081 EXPECT_EQ("struct foo {\n" 11082 "private:\n" 11083 " void f() {}\n" 11084 "\n" 11085 "private:\n" 11086 " int i;\n" 11087 "\n" 11088 "protected:\n" 11089 " int j;\n" 11090 "};\n", 11091 format("struct foo {\n" 11092 "private:\n" 11093 " void f() {}\n" 11094 "\n" 11095 "private:\n" 11096 " int i;\n" 11097 "\n" 11098 "protected:\n" 11099 " int j;\n" 11100 "};\n", 11101 Style)); 11102 // Check if MaxEmptyLinesToKeep is respected. 11103 EXPECT_EQ("struct foo {\n" 11104 "private:\n" 11105 "\n" 11106 " void f() {}\n" 11107 "\n" 11108 "private:\n" 11109 "\n" 11110 " int i;\n" 11111 "\n" 11112 "protected:\n" 11113 "\n" 11114 " int j;\n" 11115 "};\n", 11116 format("struct foo {\n" 11117 "private:\n" 11118 "\n\n\n" 11119 " void f() {}\n" 11120 "\n" 11121 "private:\n" 11122 "\n\n\n" 11123 " int i;\n" 11124 "\n" 11125 "protected:\n" 11126 "\n\n\n" 11127 " int j;\n" 11128 "};\n", 11129 Style)); 11130 11131 Style.MaxEmptyLinesToKeep = 10u; 11132 EXPECT_EQ("struct foo {\n" 11133 "private:\n" 11134 "\n\n\n" 11135 " void f() {}\n" 11136 "\n" 11137 "private:\n" 11138 "\n\n\n" 11139 " int i;\n" 11140 "\n" 11141 "protected:\n" 11142 "\n\n\n" 11143 " int j;\n" 11144 "};\n", 11145 format("struct foo {\n" 11146 "private:\n" 11147 "\n\n\n" 11148 " void f() {}\n" 11149 "\n" 11150 "private:\n" 11151 "\n\n\n" 11152 " int i;\n" 11153 "\n" 11154 "protected:\n" 11155 "\n\n\n" 11156 " int j;\n" 11157 "};\n", 11158 Style)); 11159 11160 // Test with comments. 11161 Style = getLLVMStyle(); 11162 verifyFormat("struct foo {\n" 11163 "private:\n" 11164 " // comment\n" 11165 " void f() {}\n" 11166 "\n" 11167 "private: /* comment */\n" 11168 " int i;\n" 11169 "};\n", 11170 Style); 11171 verifyFormat("struct foo {\n" 11172 "private:\n" 11173 " // comment\n" 11174 " void f() {}\n" 11175 "\n" 11176 "private: /* comment */\n" 11177 " int i;\n" 11178 "};\n", 11179 "struct foo {\n" 11180 "private:\n" 11181 "\n" 11182 " // comment\n" 11183 " void f() {}\n" 11184 "\n" 11185 "private: /* comment */\n" 11186 "\n" 11187 " int i;\n" 11188 "};\n", 11189 Style); 11190 11191 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11192 verifyFormat("struct foo {\n" 11193 "private:\n" 11194 "\n" 11195 " // comment\n" 11196 " void f() {}\n" 11197 "\n" 11198 "private: /* comment */\n" 11199 "\n" 11200 " int i;\n" 11201 "};\n", 11202 "struct foo {\n" 11203 "private:\n" 11204 " // comment\n" 11205 " void f() {}\n" 11206 "\n" 11207 "private: /* comment */\n" 11208 " int i;\n" 11209 "};\n", 11210 Style); 11211 verifyFormat("struct foo {\n" 11212 "private:\n" 11213 "\n" 11214 " // comment\n" 11215 " void f() {}\n" 11216 "\n" 11217 "private: /* comment */\n" 11218 "\n" 11219 " int i;\n" 11220 "};\n", 11221 Style); 11222 11223 // Test with preprocessor defines. 11224 Style = getLLVMStyle(); 11225 verifyFormat("struct foo {\n" 11226 "private:\n" 11227 "#ifdef FOO\n" 11228 "#endif\n" 11229 " void f() {}\n" 11230 "};\n", 11231 Style); 11232 verifyFormat("struct foo {\n" 11233 "private:\n" 11234 "#ifdef FOO\n" 11235 "#endif\n" 11236 " void f() {}\n" 11237 "};\n", 11238 "struct foo {\n" 11239 "private:\n" 11240 "\n" 11241 "#ifdef FOO\n" 11242 "#endif\n" 11243 " void f() {}\n" 11244 "};\n", 11245 Style); 11246 11247 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11248 verifyFormat("struct foo {\n" 11249 "private:\n" 11250 "\n" 11251 "#ifdef FOO\n" 11252 "#endif\n" 11253 " void f() {}\n" 11254 "};\n", 11255 "struct foo {\n" 11256 "private:\n" 11257 "#ifdef FOO\n" 11258 "#endif\n" 11259 " void f() {}\n" 11260 "};\n", 11261 Style); 11262 verifyFormat("struct foo {\n" 11263 "private:\n" 11264 "\n" 11265 "#ifdef FOO\n" 11266 "#endif\n" 11267 " void f() {}\n" 11268 "};\n", 11269 Style); 11270 } 11271 11272 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 11273 // Combined tests of EmptyLineAfterAccessModifier and 11274 // EmptyLineBeforeAccessModifier. 11275 FormatStyle Style = getLLVMStyle(); 11276 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11277 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11278 verifyFormat("struct foo {\n" 11279 "private:\n" 11280 "\n" 11281 "protected:\n" 11282 "};\n", 11283 Style); 11284 11285 Style.MaxEmptyLinesToKeep = 10u; 11286 // Both remove all new lines. 11287 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11288 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11289 verifyFormat("struct foo {\n" 11290 "private:\n" 11291 "protected:\n" 11292 "};\n", 11293 "struct foo {\n" 11294 "private:\n" 11295 "\n\n\n" 11296 "protected:\n" 11297 "};\n", 11298 Style); 11299 11300 // Leave tests rely on the code layout, test::messUp can not be used. 11301 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11302 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11303 Style.MaxEmptyLinesToKeep = 10u; 11304 EXPECT_EQ("struct foo {\n" 11305 "private:\n" 11306 "\n\n\n" 11307 "protected:\n" 11308 "};\n", 11309 format("struct foo {\n" 11310 "private:\n" 11311 "\n\n\n" 11312 "protected:\n" 11313 "};\n", 11314 Style)); 11315 Style.MaxEmptyLinesToKeep = 3u; 11316 EXPECT_EQ("struct foo {\n" 11317 "private:\n" 11318 "\n\n\n" 11319 "protected:\n" 11320 "};\n", 11321 format("struct foo {\n" 11322 "private:\n" 11323 "\n\n\n" 11324 "protected:\n" 11325 "};\n", 11326 Style)); 11327 Style.MaxEmptyLinesToKeep = 1u; 11328 EXPECT_EQ("struct foo {\n" 11329 "private:\n" 11330 "\n\n\n" 11331 "protected:\n" 11332 "};\n", 11333 format("struct foo {\n" 11334 "private:\n" 11335 "\n\n\n" 11336 "protected:\n" 11337 "};\n", 11338 Style)); // Based on new lines in original document and not 11339 // on the setting. 11340 11341 Style.MaxEmptyLinesToKeep = 10u; 11342 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11343 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11344 // Newlines are kept if they are greater than zero, 11345 // test::messUp removes all new lines which changes the logic 11346 EXPECT_EQ("struct foo {\n" 11347 "private:\n" 11348 "\n\n\n" 11349 "protected:\n" 11350 "};\n", 11351 format("struct foo {\n" 11352 "private:\n" 11353 "\n\n\n" 11354 "protected:\n" 11355 "};\n", 11356 Style)); 11357 11358 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11359 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11360 // test::messUp removes all new lines which changes the logic 11361 EXPECT_EQ("struct foo {\n" 11362 "private:\n" 11363 "\n\n\n" 11364 "protected:\n" 11365 "};\n", 11366 format("struct foo {\n" 11367 "private:\n" 11368 "\n\n\n" 11369 "protected:\n" 11370 "};\n", 11371 Style)); 11372 11373 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11374 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11375 EXPECT_EQ("struct foo {\n" 11376 "private:\n" 11377 "\n\n\n" 11378 "protected:\n" 11379 "};\n", 11380 format("struct foo {\n" 11381 "private:\n" 11382 "\n\n\n" 11383 "protected:\n" 11384 "};\n", 11385 Style)); // test::messUp removes all new lines which changes 11386 // the logic. 11387 11388 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11389 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11390 verifyFormat("struct foo {\n" 11391 "private:\n" 11392 "protected:\n" 11393 "};\n", 11394 "struct foo {\n" 11395 "private:\n" 11396 "\n\n\n" 11397 "protected:\n" 11398 "};\n", 11399 Style); 11400 11401 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11402 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11403 EXPECT_EQ("struct foo {\n" 11404 "private:\n" 11405 "\n\n\n" 11406 "protected:\n" 11407 "};\n", 11408 format("struct foo {\n" 11409 "private:\n" 11410 "\n\n\n" 11411 "protected:\n" 11412 "};\n", 11413 Style)); // test::messUp removes all new lines which changes 11414 // the logic. 11415 11416 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11417 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11418 verifyFormat("struct foo {\n" 11419 "private:\n" 11420 "protected:\n" 11421 "};\n", 11422 "struct foo {\n" 11423 "private:\n" 11424 "\n\n\n" 11425 "protected:\n" 11426 "};\n", 11427 Style); 11428 11429 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11430 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11431 verifyFormat("struct foo {\n" 11432 "private:\n" 11433 "protected:\n" 11434 "};\n", 11435 "struct foo {\n" 11436 "private:\n" 11437 "\n\n\n" 11438 "protected:\n" 11439 "};\n", 11440 Style); 11441 11442 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11443 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11444 verifyFormat("struct foo {\n" 11445 "private:\n" 11446 "protected:\n" 11447 "};\n", 11448 "struct foo {\n" 11449 "private:\n" 11450 "\n\n\n" 11451 "protected:\n" 11452 "};\n", 11453 Style); 11454 11455 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11456 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11457 verifyFormat("struct foo {\n" 11458 "private:\n" 11459 "protected:\n" 11460 "};\n", 11461 "struct foo {\n" 11462 "private:\n" 11463 "\n\n\n" 11464 "protected:\n" 11465 "};\n", 11466 Style); 11467 } 11468 11469 TEST_F(FormatTest, FormatsArrays) { 11470 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11471 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11472 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11473 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11474 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11475 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11476 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11477 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11478 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11479 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11480 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11481 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11482 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11483 verifyFormat( 11484 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11485 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11486 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11487 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11488 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11489 11490 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11492 verifyFormat( 11493 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11494 " .aaaaaaa[0]\n" 11495 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11496 verifyFormat("a[::b::c];"); 11497 11498 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11499 11500 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11501 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11502 } 11503 11504 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11505 verifyFormat("(a)->b();"); 11506 verifyFormat("--a;"); 11507 } 11508 11509 TEST_F(FormatTest, HandlesIncludeDirectives) { 11510 verifyFormat("#include <string>\n" 11511 "#include <a/b/c.h>\n" 11512 "#include \"a/b/string\"\n" 11513 "#include \"string.h\"\n" 11514 "#include \"string.h\"\n" 11515 "#include <a-a>\n" 11516 "#include < path with space >\n" 11517 "#include_next <test.h>" 11518 "#include \"abc.h\" // this is included for ABC\n" 11519 "#include \"some long include\" // with a comment\n" 11520 "#include \"some very long include path\"\n" 11521 "#include <some/very/long/include/path>\n", 11522 getLLVMStyleWithColumns(35)); 11523 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11524 EXPECT_EQ("#include <a>", format("#include<a>")); 11525 11526 verifyFormat("#import <string>"); 11527 verifyFormat("#import <a/b/c.h>"); 11528 verifyFormat("#import \"a/b/string\""); 11529 verifyFormat("#import \"string.h\""); 11530 verifyFormat("#import \"string.h\""); 11531 verifyFormat("#if __has_include(<strstream>)\n" 11532 "#include <strstream>\n" 11533 "#endif"); 11534 11535 verifyFormat("#define MY_IMPORT <a/b>"); 11536 11537 verifyFormat("#if __has_include(<a/b>)"); 11538 verifyFormat("#if __has_include_next(<a/b>)"); 11539 verifyFormat("#define F __has_include(<a/b>)"); 11540 verifyFormat("#define F __has_include_next(<a/b>)"); 11541 11542 // Protocol buffer definition or missing "#". 11543 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11544 getLLVMStyleWithColumns(30)); 11545 11546 FormatStyle Style = getLLVMStyle(); 11547 Style.AlwaysBreakBeforeMultilineStrings = true; 11548 Style.ColumnLimit = 0; 11549 verifyFormat("#import \"abc.h\"", Style); 11550 11551 // But 'import' might also be a regular C++ namespace. 11552 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11554 } 11555 11556 //===----------------------------------------------------------------------===// 11557 // Error recovery tests. 11558 //===----------------------------------------------------------------------===// 11559 11560 TEST_F(FormatTest, IncompleteParameterLists) { 11561 FormatStyle NoBinPacking = getLLVMStyle(); 11562 NoBinPacking.BinPackParameters = false; 11563 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11564 " double *min_x,\n" 11565 " double *max_x,\n" 11566 " double *min_y,\n" 11567 " double *max_y,\n" 11568 " double *min_z,\n" 11569 " double *max_z, ) {}", 11570 NoBinPacking); 11571 } 11572 11573 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11574 verifyFormat("void f() { return; }\n42"); 11575 verifyFormat("void f() {\n" 11576 " if (0)\n" 11577 " return;\n" 11578 "}\n" 11579 "42"); 11580 verifyFormat("void f() { return }\n42"); 11581 verifyFormat("void f() {\n" 11582 " if (0)\n" 11583 " return\n" 11584 "}\n" 11585 "42"); 11586 } 11587 11588 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11589 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11590 EXPECT_EQ("void f() {\n" 11591 " if (a)\n" 11592 " return\n" 11593 "}", 11594 format("void f ( ) { if ( a ) return }")); 11595 EXPECT_EQ("namespace N {\n" 11596 "void f()\n" 11597 "}", 11598 format("namespace N { void f() }")); 11599 EXPECT_EQ("namespace N {\n" 11600 "void f() {}\n" 11601 "void g()\n" 11602 "} // namespace N", 11603 format("namespace N { void f( ) { } void g( ) }")); 11604 } 11605 11606 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11607 verifyFormat("int aaaaaaaa =\n" 11608 " // Overlylongcomment\n" 11609 " b;", 11610 getLLVMStyleWithColumns(20)); 11611 verifyFormat("function(\n" 11612 " ShortArgument,\n" 11613 " LoooooooooooongArgument);\n", 11614 getLLVMStyleWithColumns(20)); 11615 } 11616 11617 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11618 verifyFormat("public:"); 11619 verifyFormat("class A {\n" 11620 "public\n" 11621 " void f() {}\n" 11622 "};"); 11623 verifyFormat("public\n" 11624 "int qwerty;"); 11625 verifyFormat("public\n" 11626 "B {}"); 11627 verifyFormat("public\n" 11628 "{}"); 11629 verifyFormat("public\n" 11630 "B { int x; }"); 11631 } 11632 11633 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11634 verifyFormat("{"); 11635 verifyFormat("#})"); 11636 verifyNoCrash("(/**/[:!] ?[)."); 11637 } 11638 11639 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11640 // Found by oss-fuzz: 11641 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11642 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11643 Style.ColumnLimit = 60; 11644 verifyNoCrash( 11645 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11646 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11647 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11648 Style); 11649 } 11650 11651 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11652 verifyFormat("do {\n}"); 11653 verifyFormat("do {\n}\n" 11654 "f();"); 11655 verifyFormat("do {\n}\n" 11656 "wheeee(fun);"); 11657 verifyFormat("do {\n" 11658 " f();\n" 11659 "}"); 11660 } 11661 11662 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11663 verifyFormat("if {\n foo;\n foo();\n}"); 11664 verifyFormat("switch {\n foo;\n foo();\n}"); 11665 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11666 verifyFormat("while {\n foo;\n foo();\n}"); 11667 verifyFormat("do {\n foo;\n foo();\n} while;"); 11668 } 11669 11670 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11671 verifyIncompleteFormat("namespace {\n" 11672 "class Foo { Foo (\n" 11673 "};\n" 11674 "} // namespace"); 11675 } 11676 11677 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11678 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11679 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11680 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11681 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11682 11683 EXPECT_EQ("{\n" 11684 " {\n" 11685 " breakme(\n" 11686 " qwe);\n" 11687 " }\n", 11688 format("{\n" 11689 " {\n" 11690 " breakme(qwe);\n" 11691 "}\n", 11692 getLLVMStyleWithColumns(10))); 11693 } 11694 11695 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11696 verifyFormat("int x = {\n" 11697 " avariable,\n" 11698 " b(alongervariable)};", 11699 getLLVMStyleWithColumns(25)); 11700 } 11701 11702 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11703 verifyFormat("return (a)(b){1, 2, 3};"); 11704 } 11705 11706 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11707 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11708 verifyFormat("vector<int> x{\n" 11709 " 1,\n" 11710 " 2,\n" 11711 " 3,\n" 11712 " 4,\n" 11713 "};"); 11714 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11715 verifyFormat("f({1, 2});"); 11716 verifyFormat("auto v = Foo{-1};"); 11717 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11718 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11719 verifyFormat("new vector<int>{1, 2, 3};"); 11720 verifyFormat("new int[3]{1, 2, 3};"); 11721 verifyFormat("new int{1};"); 11722 verifyFormat("return {arg1, arg2};"); 11723 verifyFormat("return {arg1, SomeType{parameter}};"); 11724 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11725 verifyFormat("new T{arg1, arg2};"); 11726 verifyFormat("f(MyMap[{composite, key}]);"); 11727 verifyFormat("class Class {\n" 11728 " T member = {arg1, arg2};\n" 11729 "};"); 11730 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11731 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11732 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11733 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11734 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11735 11736 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11737 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11738 verifyFormat("auto i = decltype(x){};"); 11739 verifyFormat("auto i = typeof(x){};"); 11740 verifyFormat("auto i = _Atomic(x){};"); 11741 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11742 verifyFormat("Node n{1, Node{1000}, //\n" 11743 " 2};"); 11744 verifyFormat("Aaaa aaaaaaa{\n" 11745 " {\n" 11746 " aaaa,\n" 11747 " },\n" 11748 "};"); 11749 verifyFormat("class C : public D {\n" 11750 " SomeClass SC{2};\n" 11751 "};"); 11752 verifyFormat("class C : public A {\n" 11753 " class D : public B {\n" 11754 " void f() { int i{2}; }\n" 11755 " };\n" 11756 "};"); 11757 verifyFormat("#define A {a, a},"); 11758 // Don't confuse braced list initializers with compound statements. 11759 verifyFormat( 11760 "class A {\n" 11761 " A() : a{} {}\n" 11762 " A(int b) : b(b) {}\n" 11763 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" 11764 " int a, b;\n" 11765 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" 11766 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " 11767 "{}\n" 11768 "};"); 11769 11770 // Avoid breaking between equal sign and opening brace 11771 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11772 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11773 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11774 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11775 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11776 " {\"ccccccccccccccccccccc\", 2}};", 11777 AvoidBreakingFirstArgument); 11778 11779 // Binpacking only if there is no trailing comma 11780 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11781 " cccccccccc, dddddddddd};", 11782 getLLVMStyleWithColumns(50)); 11783 verifyFormat("const Aaaaaa aaaaa = {\n" 11784 " aaaaaaaaaaa,\n" 11785 " bbbbbbbbbbb,\n" 11786 " ccccccccccc,\n" 11787 " ddddddddddd,\n" 11788 "};", 11789 getLLVMStyleWithColumns(50)); 11790 11791 // Cases where distinguising braced lists and blocks is hard. 11792 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11793 verifyFormat("void f() {\n" 11794 " return; // comment\n" 11795 "}\n" 11796 "SomeType t;"); 11797 verifyFormat("void f() {\n" 11798 " if (a) {\n" 11799 " f();\n" 11800 " }\n" 11801 "}\n" 11802 "SomeType t;"); 11803 11804 // In combination with BinPackArguments = false. 11805 FormatStyle NoBinPacking = getLLVMStyle(); 11806 NoBinPacking.BinPackArguments = false; 11807 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11808 " bbbbb,\n" 11809 " ccccc,\n" 11810 " ddddd,\n" 11811 " eeeee,\n" 11812 " ffffff,\n" 11813 " ggggg,\n" 11814 " hhhhhh,\n" 11815 " iiiiii,\n" 11816 " jjjjjj,\n" 11817 " kkkkkk};", 11818 NoBinPacking); 11819 verifyFormat("const Aaaaaa aaaaa = {\n" 11820 " aaaaa,\n" 11821 " bbbbb,\n" 11822 " ccccc,\n" 11823 " ddddd,\n" 11824 " eeeee,\n" 11825 " ffffff,\n" 11826 " ggggg,\n" 11827 " hhhhhh,\n" 11828 " iiiiii,\n" 11829 " jjjjjj,\n" 11830 " kkkkkk,\n" 11831 "};", 11832 NoBinPacking); 11833 verifyFormat( 11834 "const Aaaaaa aaaaa = {\n" 11835 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11836 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11837 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11838 "};", 11839 NoBinPacking); 11840 11841 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11842 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11843 " CDDDP83848_BMCR_REGISTER,\n" 11844 " CDDDP83848_BMSR_REGISTER,\n" 11845 " CDDDP83848_RBR_REGISTER};", 11846 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11847 " CDDDP83848_BMSR_REGISTER,\n" 11848 " CDDDP83848_RBR_REGISTER};", 11849 NoBinPacking)); 11850 11851 // FIXME: The alignment of these trailing comments might be bad. Then again, 11852 // this might be utterly useless in real code. 11853 verifyFormat("Constructor::Constructor()\n" 11854 " : some_value{ //\n" 11855 " aaaaaaa, //\n" 11856 " bbbbbbb} {}"); 11857 11858 // In braced lists, the first comment is always assumed to belong to the 11859 // first element. Thus, it can be moved to the next or previous line as 11860 // appropriate. 11861 EXPECT_EQ("function({// First element:\n" 11862 " 1,\n" 11863 " // Second element:\n" 11864 " 2});", 11865 format("function({\n" 11866 " // First element:\n" 11867 " 1,\n" 11868 " // Second element:\n" 11869 " 2});")); 11870 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11871 " // First element:\n" 11872 " 1,\n" 11873 " // Second element:\n" 11874 " 2};", 11875 format("std::vector<int> MyNumbers{// First element:\n" 11876 " 1,\n" 11877 " // Second element:\n" 11878 " 2};", 11879 getLLVMStyleWithColumns(30))); 11880 // A trailing comma should still lead to an enforced line break and no 11881 // binpacking. 11882 EXPECT_EQ("vector<int> SomeVector = {\n" 11883 " // aaa\n" 11884 " 1,\n" 11885 " 2,\n" 11886 "};", 11887 format("vector<int> SomeVector = { // aaa\n" 11888 " 1, 2, };")); 11889 11890 // C++11 brace initializer list l-braces should not be treated any differently 11891 // when breaking before lambda bodies is enabled 11892 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11893 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11894 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11895 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11896 verifyFormat( 11897 "std::runtime_error{\n" 11898 " \"Long string which will force a break onto the next line...\"};", 11899 BreakBeforeLambdaBody); 11900 11901 FormatStyle ExtraSpaces = getLLVMStyle(); 11902 ExtraSpaces.Cpp11BracedListStyle = false; 11903 ExtraSpaces.ColumnLimit = 75; 11904 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11905 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11906 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11907 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11908 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11909 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11910 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11911 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11912 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11913 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11914 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11915 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11916 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11917 verifyFormat("class Class {\n" 11918 " T member = { arg1, arg2 };\n" 11919 "};", 11920 ExtraSpaces); 11921 verifyFormat( 11922 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11923 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11924 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11925 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11926 ExtraSpaces); 11927 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11928 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11929 ExtraSpaces); 11930 verifyFormat( 11931 "someFunction(OtherParam,\n" 11932 " BracedList{ // comment 1 (Forcing interesting break)\n" 11933 " param1, param2,\n" 11934 " // comment 2\n" 11935 " param3, param4 });", 11936 ExtraSpaces); 11937 verifyFormat( 11938 "std::this_thread::sleep_for(\n" 11939 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11940 ExtraSpaces); 11941 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11942 " aaaaaaa,\n" 11943 " aaaaaaaaaa,\n" 11944 " aaaaa,\n" 11945 " aaaaaaaaaaaaaaa,\n" 11946 " aaa,\n" 11947 " aaaaaaaaaa,\n" 11948 " a,\n" 11949 " aaaaaaaaaaaaaaaaaaaaa,\n" 11950 " aaaaaaaaaaaa,\n" 11951 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11952 " aaaaaaa,\n" 11953 " a};"); 11954 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11955 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11956 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11957 11958 // Avoid breaking between initializer/equal sign and opening brace 11959 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11960 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11961 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11962 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11963 " { \"ccccccccccccccccccccc\", 2 }\n" 11964 "};", 11965 ExtraSpaces); 11966 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11967 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11968 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11969 " { \"ccccccccccccccccccccc\", 2 }\n" 11970 "};", 11971 ExtraSpaces); 11972 11973 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11974 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11975 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11976 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11977 11978 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11979 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11980 SpaceBetweenBraces.SpacesInParentheses = true; 11981 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11982 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11983 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11984 verifyFormat("vector< int > x{ // comment 1\n" 11985 " 1, 2, 3, 4 };", 11986 SpaceBetweenBraces); 11987 SpaceBetweenBraces.ColumnLimit = 20; 11988 EXPECT_EQ("vector< int > x{\n" 11989 " 1, 2, 3, 4 };", 11990 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11991 SpaceBetweenBraces.ColumnLimit = 24; 11992 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11993 " 3, 4 };", 11994 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11995 EXPECT_EQ("vector< int > x{\n" 11996 " 1,\n" 11997 " 2,\n" 11998 " 3,\n" 11999 " 4,\n" 12000 "};", 12001 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 12002 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 12003 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 12004 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 12005 } 12006 12007 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 12008 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12009 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12010 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12011 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12012 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12013 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12014 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 12015 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12016 " 1, 22, 333, 4444, 55555, //\n" 12017 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12018 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12019 verifyFormat( 12020 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12021 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12022 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 12023 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12024 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12025 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12026 " 7777777};"); 12027 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12028 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12029 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12030 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12031 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12032 " // Separating comment.\n" 12033 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12034 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12035 " // Leading comment\n" 12036 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12037 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12038 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12039 " 1, 1, 1, 1};", 12040 getLLVMStyleWithColumns(39)); 12041 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12042 " 1, 1, 1, 1};", 12043 getLLVMStyleWithColumns(38)); 12044 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 12045 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 12046 getLLVMStyleWithColumns(43)); 12047 verifyFormat( 12048 "static unsigned SomeValues[10][3] = {\n" 12049 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 12050 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 12051 verifyFormat("static auto fields = new vector<string>{\n" 12052 " \"aaaaaaaaaaaaa\",\n" 12053 " \"aaaaaaaaaaaaa\",\n" 12054 " \"aaaaaaaaaaaa\",\n" 12055 " \"aaaaaaaaaaaaaa\",\n" 12056 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12057 " \"aaaaaaaaaaaa\",\n" 12058 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12059 "};"); 12060 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 12061 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 12062 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 12063 " 3, cccccccccccccccccccccc};", 12064 getLLVMStyleWithColumns(60)); 12065 12066 // Trailing commas. 12067 verifyFormat("vector<int> x = {\n" 12068 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 12069 "};", 12070 getLLVMStyleWithColumns(39)); 12071 verifyFormat("vector<int> x = {\n" 12072 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 12073 "};", 12074 getLLVMStyleWithColumns(39)); 12075 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12076 " 1, 1, 1, 1,\n" 12077 " /**/ /**/};", 12078 getLLVMStyleWithColumns(39)); 12079 12080 // Trailing comment in the first line. 12081 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 12082 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 12083 " 111111111, 222222222, 3333333333, 444444444, //\n" 12084 " 11111111, 22222222, 333333333, 44444444};"); 12085 // Trailing comment in the last line. 12086 verifyFormat("int aaaaa[] = {\n" 12087 " 1, 2, 3, // comment\n" 12088 " 4, 5, 6 // comment\n" 12089 "};"); 12090 12091 // With nested lists, we should either format one item per line or all nested 12092 // lists one on line. 12093 // FIXME: For some nested lists, we can do better. 12094 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 12095 " {aaaaaaaaaaaaaaaaaaa},\n" 12096 " {aaaaaaaaaaaaaaaaaaaaa},\n" 12097 " {aaaaaaaaaaaaaaaaa}};", 12098 getLLVMStyleWithColumns(60)); 12099 verifyFormat( 12100 "SomeStruct my_struct_array = {\n" 12101 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 12102 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 12103 " {aaa, aaa},\n" 12104 " {aaa, aaa},\n" 12105 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 12106 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 12107 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 12108 12109 // No column layout should be used here. 12110 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 12111 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 12112 12113 verifyNoCrash("a<,"); 12114 12115 // No braced initializer here. 12116 verifyFormat("void f() {\n" 12117 " struct Dummy {};\n" 12118 " f(v);\n" 12119 "}"); 12120 12121 // Long lists should be formatted in columns even if they are nested. 12122 verifyFormat( 12123 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12124 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12125 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12126 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12127 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12128 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 12129 12130 // Allow "single-column" layout even if that violates the column limit. There 12131 // isn't going to be a better way. 12132 verifyFormat("std::vector<int> a = {\n" 12133 " aaaaaaaa,\n" 12134 " aaaaaaaa,\n" 12135 " aaaaaaaa,\n" 12136 " aaaaaaaa,\n" 12137 " aaaaaaaaaa,\n" 12138 " aaaaaaaa,\n" 12139 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 12140 getLLVMStyleWithColumns(30)); 12141 verifyFormat("vector<int> aaaa = {\n" 12142 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12143 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12144 " aaaaaa.aaaaaaa,\n" 12145 " aaaaaa.aaaaaaa,\n" 12146 " aaaaaa.aaaaaaa,\n" 12147 " aaaaaa.aaaaaaa,\n" 12148 "};"); 12149 12150 // Don't create hanging lists. 12151 verifyFormat("someFunction(Param, {List1, List2,\n" 12152 " List3});", 12153 getLLVMStyleWithColumns(35)); 12154 verifyFormat("someFunction(Param, Param,\n" 12155 " {List1, List2,\n" 12156 " List3});", 12157 getLLVMStyleWithColumns(35)); 12158 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 12159 " aaaaaaaaaaaaaaaaaaaaaaa);"); 12160 } 12161 12162 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 12163 FormatStyle DoNotMerge = getLLVMStyle(); 12164 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12165 12166 verifyFormat("void f() { return 42; }"); 12167 verifyFormat("void f() {\n" 12168 " return 42;\n" 12169 "}", 12170 DoNotMerge); 12171 verifyFormat("void f() {\n" 12172 " // Comment\n" 12173 "}"); 12174 verifyFormat("{\n" 12175 "#error {\n" 12176 " int a;\n" 12177 "}"); 12178 verifyFormat("{\n" 12179 " int a;\n" 12180 "#error {\n" 12181 "}"); 12182 verifyFormat("void f() {} // comment"); 12183 verifyFormat("void f() { int a; } // comment"); 12184 verifyFormat("void f() {\n" 12185 "} // comment", 12186 DoNotMerge); 12187 verifyFormat("void f() {\n" 12188 " int a;\n" 12189 "} // comment", 12190 DoNotMerge); 12191 verifyFormat("void f() {\n" 12192 "} // comment", 12193 getLLVMStyleWithColumns(15)); 12194 12195 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 12196 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 12197 12198 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 12199 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 12200 verifyFormat("class C {\n" 12201 " C()\n" 12202 " : iiiiiiii(nullptr),\n" 12203 " kkkkkkk(nullptr),\n" 12204 " mmmmmmm(nullptr),\n" 12205 " nnnnnnn(nullptr) {}\n" 12206 "};", 12207 getGoogleStyle()); 12208 12209 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 12210 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 12211 EXPECT_EQ("class C {\n" 12212 " A() : b(0) {}\n" 12213 "};", 12214 format("class C{A():b(0){}};", NoColumnLimit)); 12215 EXPECT_EQ("A()\n" 12216 " : b(0) {\n" 12217 "}", 12218 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 12219 12220 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 12221 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 12222 FormatStyle::SFS_None; 12223 EXPECT_EQ("A()\n" 12224 " : b(0) {\n" 12225 "}", 12226 format("A():b(0){}", DoNotMergeNoColumnLimit)); 12227 EXPECT_EQ("A()\n" 12228 " : b(0) {\n" 12229 "}", 12230 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 12231 12232 verifyFormat("#define A \\\n" 12233 " void f() { \\\n" 12234 " int i; \\\n" 12235 " }", 12236 getLLVMStyleWithColumns(20)); 12237 verifyFormat("#define A \\\n" 12238 " void f() { int i; }", 12239 getLLVMStyleWithColumns(21)); 12240 verifyFormat("#define A \\\n" 12241 " void f() { \\\n" 12242 " int i; \\\n" 12243 " } \\\n" 12244 " int j;", 12245 getLLVMStyleWithColumns(22)); 12246 verifyFormat("#define A \\\n" 12247 " void f() { int i; } \\\n" 12248 " int j;", 12249 getLLVMStyleWithColumns(23)); 12250 } 12251 12252 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 12253 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12254 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12255 verifyFormat("class C {\n" 12256 " int f() {}\n" 12257 "};", 12258 MergeEmptyOnly); 12259 verifyFormat("class C {\n" 12260 " int f() {\n" 12261 " return 42;\n" 12262 " }\n" 12263 "};", 12264 MergeEmptyOnly); 12265 verifyFormat("int f() {}", MergeEmptyOnly); 12266 verifyFormat("int f() {\n" 12267 " return 42;\n" 12268 "}", 12269 MergeEmptyOnly); 12270 12271 // Also verify behavior when BraceWrapping.AfterFunction = true 12272 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12273 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 12274 verifyFormat("int f() {}", MergeEmptyOnly); 12275 verifyFormat("class C {\n" 12276 " int f() {}\n" 12277 "};", 12278 MergeEmptyOnly); 12279 } 12280 12281 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 12282 FormatStyle MergeInlineOnly = getLLVMStyle(); 12283 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12284 verifyFormat("class C {\n" 12285 " int f() { return 42; }\n" 12286 "};", 12287 MergeInlineOnly); 12288 verifyFormat("int f() {\n" 12289 " return 42;\n" 12290 "}", 12291 MergeInlineOnly); 12292 12293 // SFS_Inline implies SFS_Empty 12294 verifyFormat("class C {\n" 12295 " int f() {}\n" 12296 "};", 12297 MergeInlineOnly); 12298 verifyFormat("int f() {}", MergeInlineOnly); 12299 12300 // Also verify behavior when BraceWrapping.AfterFunction = true 12301 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12302 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12303 verifyFormat("class C {\n" 12304 " int f() { return 42; }\n" 12305 "};", 12306 MergeInlineOnly); 12307 verifyFormat("int f()\n" 12308 "{\n" 12309 " return 42;\n" 12310 "}", 12311 MergeInlineOnly); 12312 12313 // SFS_Inline implies SFS_Empty 12314 verifyFormat("int f() {}", MergeInlineOnly); 12315 verifyFormat("class C {\n" 12316 " int f() {}\n" 12317 "};", 12318 MergeInlineOnly); 12319 12320 MergeInlineOnly.BraceWrapping.AfterClass = true; 12321 MergeInlineOnly.BraceWrapping.AfterStruct = true; 12322 verifyFormat("class C\n" 12323 "{\n" 12324 " int f() { return 42; }\n" 12325 "};", 12326 MergeInlineOnly); 12327 verifyFormat("struct C\n" 12328 "{\n" 12329 " int f() { return 42; }\n" 12330 "};", 12331 MergeInlineOnly); 12332 verifyFormat("int f()\n" 12333 "{\n" 12334 " return 42;\n" 12335 "}", 12336 MergeInlineOnly); 12337 verifyFormat("int f() {}", MergeInlineOnly); 12338 verifyFormat("class C\n" 12339 "{\n" 12340 " int f() { return 42; }\n" 12341 "};", 12342 MergeInlineOnly); 12343 verifyFormat("struct C\n" 12344 "{\n" 12345 " int f() { return 42; }\n" 12346 "};", 12347 MergeInlineOnly); 12348 verifyFormat("struct C\n" 12349 "// comment\n" 12350 "/* comment */\n" 12351 "// comment\n" 12352 "{\n" 12353 " int f() { return 42; }\n" 12354 "};", 12355 MergeInlineOnly); 12356 verifyFormat("/* comment */ struct C\n" 12357 "{\n" 12358 " int f() { return 42; }\n" 12359 "};", 12360 MergeInlineOnly); 12361 } 12362 12363 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 12364 FormatStyle MergeInlineOnly = getLLVMStyle(); 12365 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 12366 FormatStyle::SFS_InlineOnly; 12367 verifyFormat("class C {\n" 12368 " int f() { return 42; }\n" 12369 "};", 12370 MergeInlineOnly); 12371 verifyFormat("int f() {\n" 12372 " return 42;\n" 12373 "}", 12374 MergeInlineOnly); 12375 12376 // SFS_InlineOnly does not imply SFS_Empty 12377 verifyFormat("class C {\n" 12378 " int f() {}\n" 12379 "};", 12380 MergeInlineOnly); 12381 verifyFormat("int f() {\n" 12382 "}", 12383 MergeInlineOnly); 12384 12385 // Also verify behavior when BraceWrapping.AfterFunction = true 12386 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12387 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12388 verifyFormat("class C {\n" 12389 " int f() { return 42; }\n" 12390 "};", 12391 MergeInlineOnly); 12392 verifyFormat("int f()\n" 12393 "{\n" 12394 " return 42;\n" 12395 "}", 12396 MergeInlineOnly); 12397 12398 // SFS_InlineOnly does not imply SFS_Empty 12399 verifyFormat("int f()\n" 12400 "{\n" 12401 "}", 12402 MergeInlineOnly); 12403 verifyFormat("class C {\n" 12404 " int f() {}\n" 12405 "};", 12406 MergeInlineOnly); 12407 } 12408 12409 TEST_F(FormatTest, SplitEmptyFunction) { 12410 FormatStyle Style = getLLVMStyleWithColumns(40); 12411 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12412 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12413 Style.BraceWrapping.AfterFunction = true; 12414 Style.BraceWrapping.SplitEmptyFunction = false; 12415 12416 verifyFormat("int f()\n" 12417 "{}", 12418 Style); 12419 verifyFormat("int f()\n" 12420 "{\n" 12421 " return 42;\n" 12422 "}", 12423 Style); 12424 verifyFormat("int f()\n" 12425 "{\n" 12426 " // some comment\n" 12427 "}", 12428 Style); 12429 12430 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12431 verifyFormat("int f() {}", Style); 12432 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12433 "{}", 12434 Style); 12435 verifyFormat("int f()\n" 12436 "{\n" 12437 " return 0;\n" 12438 "}", 12439 Style); 12440 12441 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12442 verifyFormat("class Foo {\n" 12443 " int f() {}\n" 12444 "};\n", 12445 Style); 12446 verifyFormat("class Foo {\n" 12447 " int f() { return 0; }\n" 12448 "};\n", 12449 Style); 12450 verifyFormat("class Foo {\n" 12451 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12452 " {}\n" 12453 "};\n", 12454 Style); 12455 verifyFormat("class Foo {\n" 12456 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12457 " {\n" 12458 " return 0;\n" 12459 " }\n" 12460 "};\n", 12461 Style); 12462 12463 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12464 verifyFormat("int f() {}", Style); 12465 verifyFormat("int f() { return 0; }", Style); 12466 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12467 "{}", 12468 Style); 12469 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12470 "{\n" 12471 " return 0;\n" 12472 "}", 12473 Style); 12474 } 12475 12476 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { 12477 FormatStyle Style = getLLVMStyleWithColumns(40); 12478 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12479 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12480 Style.BraceWrapping.AfterFunction = true; 12481 Style.BraceWrapping.SplitEmptyFunction = true; 12482 Style.BraceWrapping.SplitEmptyRecord = false; 12483 12484 verifyFormat("class C {};", Style); 12485 verifyFormat("struct C {};", Style); 12486 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12487 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12488 "{\n" 12489 "}", 12490 Style); 12491 verifyFormat("class C {\n" 12492 " C()\n" 12493 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" 12494 " bbbbbbbbbbbbbbbbbbb()\n" 12495 " {\n" 12496 " }\n" 12497 " void\n" 12498 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12499 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12500 " {\n" 12501 " }\n" 12502 "};", 12503 Style); 12504 } 12505 12506 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12507 FormatStyle Style = getLLVMStyle(); 12508 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12509 verifyFormat("#ifdef A\n" 12510 "int f() {}\n" 12511 "#else\n" 12512 "int g() {}\n" 12513 "#endif", 12514 Style); 12515 } 12516 12517 TEST_F(FormatTest, SplitEmptyClass) { 12518 FormatStyle Style = getLLVMStyle(); 12519 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12520 Style.BraceWrapping.AfterClass = true; 12521 Style.BraceWrapping.SplitEmptyRecord = false; 12522 12523 verifyFormat("class Foo\n" 12524 "{};", 12525 Style); 12526 verifyFormat("/* something */ class Foo\n" 12527 "{};", 12528 Style); 12529 verifyFormat("template <typename X> class Foo\n" 12530 "{};", 12531 Style); 12532 verifyFormat("class Foo\n" 12533 "{\n" 12534 " Foo();\n" 12535 "};", 12536 Style); 12537 verifyFormat("typedef class Foo\n" 12538 "{\n" 12539 "} Foo_t;", 12540 Style); 12541 12542 Style.BraceWrapping.SplitEmptyRecord = true; 12543 Style.BraceWrapping.AfterStruct = true; 12544 verifyFormat("class rep\n" 12545 "{\n" 12546 "};", 12547 Style); 12548 verifyFormat("struct rep\n" 12549 "{\n" 12550 "};", 12551 Style); 12552 verifyFormat("template <typename T> class rep\n" 12553 "{\n" 12554 "};", 12555 Style); 12556 verifyFormat("template <typename T> struct rep\n" 12557 "{\n" 12558 "};", 12559 Style); 12560 verifyFormat("class rep\n" 12561 "{\n" 12562 " int x;\n" 12563 "};", 12564 Style); 12565 verifyFormat("struct rep\n" 12566 "{\n" 12567 " int x;\n" 12568 "};", 12569 Style); 12570 verifyFormat("template <typename T> class rep\n" 12571 "{\n" 12572 " int x;\n" 12573 "};", 12574 Style); 12575 verifyFormat("template <typename T> struct rep\n" 12576 "{\n" 12577 " int x;\n" 12578 "};", 12579 Style); 12580 verifyFormat("template <typename T> class rep // Foo\n" 12581 "{\n" 12582 " int x;\n" 12583 "};", 12584 Style); 12585 verifyFormat("template <typename T> struct rep // Bar\n" 12586 "{\n" 12587 " int x;\n" 12588 "};", 12589 Style); 12590 12591 verifyFormat("template <typename T> class rep<T>\n" 12592 "{\n" 12593 " int x;\n" 12594 "};", 12595 Style); 12596 12597 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12598 "{\n" 12599 " int x;\n" 12600 "};", 12601 Style); 12602 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12603 "{\n" 12604 "};", 12605 Style); 12606 12607 verifyFormat("#include \"stdint.h\"\n" 12608 "namespace rep {}", 12609 Style); 12610 verifyFormat("#include <stdint.h>\n" 12611 "namespace rep {}", 12612 Style); 12613 verifyFormat("#include <stdint.h>\n" 12614 "namespace rep {}", 12615 "#include <stdint.h>\n" 12616 "namespace rep {\n" 12617 "\n" 12618 "\n" 12619 "}", 12620 Style); 12621 } 12622 12623 TEST_F(FormatTest, SplitEmptyStruct) { 12624 FormatStyle Style = getLLVMStyle(); 12625 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12626 Style.BraceWrapping.AfterStruct = true; 12627 Style.BraceWrapping.SplitEmptyRecord = false; 12628 12629 verifyFormat("struct Foo\n" 12630 "{};", 12631 Style); 12632 verifyFormat("/* something */ struct Foo\n" 12633 "{};", 12634 Style); 12635 verifyFormat("template <typename X> struct Foo\n" 12636 "{};", 12637 Style); 12638 verifyFormat("struct Foo\n" 12639 "{\n" 12640 " Foo();\n" 12641 "};", 12642 Style); 12643 verifyFormat("typedef struct Foo\n" 12644 "{\n" 12645 "} Foo_t;", 12646 Style); 12647 // typedef struct Bar {} Bar_t; 12648 } 12649 12650 TEST_F(FormatTest, SplitEmptyUnion) { 12651 FormatStyle Style = getLLVMStyle(); 12652 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12653 Style.BraceWrapping.AfterUnion = true; 12654 Style.BraceWrapping.SplitEmptyRecord = false; 12655 12656 verifyFormat("union Foo\n" 12657 "{};", 12658 Style); 12659 verifyFormat("/* something */ union Foo\n" 12660 "{};", 12661 Style); 12662 verifyFormat("union Foo\n" 12663 "{\n" 12664 " A,\n" 12665 "};", 12666 Style); 12667 verifyFormat("typedef union Foo\n" 12668 "{\n" 12669 "} Foo_t;", 12670 Style); 12671 } 12672 12673 TEST_F(FormatTest, SplitEmptyNamespace) { 12674 FormatStyle Style = getLLVMStyle(); 12675 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12676 Style.BraceWrapping.AfterNamespace = true; 12677 Style.BraceWrapping.SplitEmptyNamespace = false; 12678 12679 verifyFormat("namespace Foo\n" 12680 "{};", 12681 Style); 12682 verifyFormat("/* something */ namespace Foo\n" 12683 "{};", 12684 Style); 12685 verifyFormat("inline namespace Foo\n" 12686 "{};", 12687 Style); 12688 verifyFormat("/* something */ inline namespace Foo\n" 12689 "{};", 12690 Style); 12691 verifyFormat("export namespace Foo\n" 12692 "{};", 12693 Style); 12694 verifyFormat("namespace Foo\n" 12695 "{\n" 12696 "void Bar();\n" 12697 "};", 12698 Style); 12699 } 12700 12701 TEST_F(FormatTest, NeverMergeShortRecords) { 12702 FormatStyle Style = getLLVMStyle(); 12703 12704 verifyFormat("class Foo {\n" 12705 " Foo();\n" 12706 "};", 12707 Style); 12708 verifyFormat("typedef class Foo {\n" 12709 " Foo();\n" 12710 "} Foo_t;", 12711 Style); 12712 verifyFormat("struct Foo {\n" 12713 " Foo();\n" 12714 "};", 12715 Style); 12716 verifyFormat("typedef struct Foo {\n" 12717 " Foo();\n" 12718 "} Foo_t;", 12719 Style); 12720 verifyFormat("union Foo {\n" 12721 " A,\n" 12722 "};", 12723 Style); 12724 verifyFormat("typedef union Foo {\n" 12725 " A,\n" 12726 "} Foo_t;", 12727 Style); 12728 verifyFormat("namespace Foo {\n" 12729 "void Bar();\n" 12730 "};", 12731 Style); 12732 12733 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12734 Style.BraceWrapping.AfterClass = true; 12735 Style.BraceWrapping.AfterStruct = true; 12736 Style.BraceWrapping.AfterUnion = true; 12737 Style.BraceWrapping.AfterNamespace = true; 12738 verifyFormat("class Foo\n" 12739 "{\n" 12740 " Foo();\n" 12741 "};", 12742 Style); 12743 verifyFormat("typedef class Foo\n" 12744 "{\n" 12745 " Foo();\n" 12746 "} Foo_t;", 12747 Style); 12748 verifyFormat("struct Foo\n" 12749 "{\n" 12750 " Foo();\n" 12751 "};", 12752 Style); 12753 verifyFormat("typedef struct Foo\n" 12754 "{\n" 12755 " Foo();\n" 12756 "} Foo_t;", 12757 Style); 12758 verifyFormat("union Foo\n" 12759 "{\n" 12760 " A,\n" 12761 "};", 12762 Style); 12763 verifyFormat("typedef union Foo\n" 12764 "{\n" 12765 " A,\n" 12766 "} Foo_t;", 12767 Style); 12768 verifyFormat("namespace Foo\n" 12769 "{\n" 12770 "void Bar();\n" 12771 "};", 12772 Style); 12773 } 12774 12775 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12776 // Elaborate type variable declarations. 12777 verifyFormat("struct foo a = {bar};\nint n;"); 12778 verifyFormat("class foo a = {bar};\nint n;"); 12779 verifyFormat("union foo a = {bar};\nint n;"); 12780 12781 // Elaborate types inside function definitions. 12782 verifyFormat("struct foo f() {}\nint n;"); 12783 verifyFormat("class foo f() {}\nint n;"); 12784 verifyFormat("union foo f() {}\nint n;"); 12785 12786 // Templates. 12787 verifyFormat("template <class X> void f() {}\nint n;"); 12788 verifyFormat("template <struct X> void f() {}\nint n;"); 12789 verifyFormat("template <union X> void f() {}\nint n;"); 12790 12791 // Actual definitions... 12792 verifyFormat("struct {\n} n;"); 12793 verifyFormat( 12794 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12795 verifyFormat("union Z {\n int n;\n} x;"); 12796 verifyFormat("class MACRO Z {\n} n;"); 12797 verifyFormat("class MACRO(X) Z {\n} n;"); 12798 verifyFormat("class __attribute__(X) Z {\n} n;"); 12799 verifyFormat("class __declspec(X) Z {\n} n;"); 12800 verifyFormat("class A##B##C {\n} n;"); 12801 verifyFormat("class alignas(16) Z {\n} n;"); 12802 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12803 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12804 12805 // Redefinition from nested context: 12806 verifyFormat("class A::B::C {\n} n;"); 12807 12808 // Template definitions. 12809 verifyFormat( 12810 "template <typename F>\n" 12811 "Matcher(const Matcher<F> &Other,\n" 12812 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12813 " !is_same<F, T>::value>::type * = 0)\n" 12814 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12815 12816 // FIXME: This is still incorrectly handled at the formatter side. 12817 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12818 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12819 12820 // FIXME: 12821 // This now gets parsed incorrectly as class definition. 12822 // verifyFormat("class A<int> f() {\n}\nint n;"); 12823 12824 // Elaborate types where incorrectly parsing the structural element would 12825 // break the indent. 12826 verifyFormat("if (true)\n" 12827 " class X x;\n" 12828 "else\n" 12829 " f();\n"); 12830 12831 // This is simply incomplete. Formatting is not important, but must not crash. 12832 verifyFormat("class A:"); 12833 } 12834 12835 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12836 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12837 format("#error Leave all white!!!!! space* alone!\n")); 12838 EXPECT_EQ( 12839 "#warning Leave all white!!!!! space* alone!\n", 12840 format("#warning Leave all white!!!!! space* alone!\n")); 12841 EXPECT_EQ("#error 1", format(" # error 1")); 12842 EXPECT_EQ("#warning 1", format(" # warning 1")); 12843 } 12844 12845 TEST_F(FormatTest, FormatHashIfExpressions) { 12846 verifyFormat("#if AAAA && BBBB"); 12847 verifyFormat("#if (AAAA && BBBB)"); 12848 verifyFormat("#elif (AAAA && BBBB)"); 12849 // FIXME: Come up with a better indentation for #elif. 12850 verifyFormat( 12851 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12852 " defined(BBBBBBBB)\n" 12853 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12854 " defined(BBBBBBBB)\n" 12855 "#endif", 12856 getLLVMStyleWithColumns(65)); 12857 } 12858 12859 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12860 FormatStyle AllowsMergedIf = getGoogleStyle(); 12861 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12862 FormatStyle::SIS_WithoutElse; 12863 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12864 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12865 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12866 EXPECT_EQ("if (true) return 42;", 12867 format("if (true)\nreturn 42;", AllowsMergedIf)); 12868 FormatStyle ShortMergedIf = AllowsMergedIf; 12869 ShortMergedIf.ColumnLimit = 25; 12870 verifyFormat("#define A \\\n" 12871 " if (true) return 42;", 12872 ShortMergedIf); 12873 verifyFormat("#define A \\\n" 12874 " f(); \\\n" 12875 " if (true)\n" 12876 "#define B", 12877 ShortMergedIf); 12878 verifyFormat("#define A \\\n" 12879 " f(); \\\n" 12880 " if (true)\n" 12881 "g();", 12882 ShortMergedIf); 12883 verifyFormat("{\n" 12884 "#ifdef A\n" 12885 " // Comment\n" 12886 " if (true) continue;\n" 12887 "#endif\n" 12888 " // Comment\n" 12889 " if (true) continue;\n" 12890 "}", 12891 ShortMergedIf); 12892 ShortMergedIf.ColumnLimit = 33; 12893 verifyFormat("#define A \\\n" 12894 " if constexpr (true) return 42;", 12895 ShortMergedIf); 12896 verifyFormat("#define A \\\n" 12897 " if CONSTEXPR (true) return 42;", 12898 ShortMergedIf); 12899 ShortMergedIf.ColumnLimit = 29; 12900 verifyFormat("#define A \\\n" 12901 " if (aaaaaaaaaa) return 1; \\\n" 12902 " return 2;", 12903 ShortMergedIf); 12904 ShortMergedIf.ColumnLimit = 28; 12905 verifyFormat("#define A \\\n" 12906 " if (aaaaaaaaaa) \\\n" 12907 " return 1; \\\n" 12908 " return 2;", 12909 ShortMergedIf); 12910 verifyFormat("#define A \\\n" 12911 " if constexpr (aaaaaaa) \\\n" 12912 " return 1; \\\n" 12913 " return 2;", 12914 ShortMergedIf); 12915 verifyFormat("#define A \\\n" 12916 " if CONSTEXPR (aaaaaaa) \\\n" 12917 " return 1; \\\n" 12918 " return 2;", 12919 ShortMergedIf); 12920 } 12921 12922 TEST_F(FormatTest, FormatStarDependingOnContext) { 12923 verifyFormat("void f(int *a);"); 12924 verifyFormat("void f() { f(fint * b); }"); 12925 verifyFormat("class A {\n void f(int *a);\n};"); 12926 verifyFormat("class A {\n int *a;\n};"); 12927 verifyFormat("namespace a {\n" 12928 "namespace b {\n" 12929 "class A {\n" 12930 " void f() {}\n" 12931 " int *a;\n" 12932 "};\n" 12933 "} // namespace b\n" 12934 "} // namespace a"); 12935 } 12936 12937 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12938 verifyFormat("while"); 12939 verifyFormat("operator"); 12940 } 12941 12942 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12943 // This code would be painfully slow to format if we didn't skip it. 12944 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 12945 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12946 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12947 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12948 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12949 "A(1, 1)\n" 12950 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12951 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12952 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12953 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12954 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12955 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12956 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12957 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12958 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12959 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12960 // Deeply nested part is untouched, rest is formatted. 12961 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12962 format(std::string("int i;\n") + Code + "int j;\n", 12963 getLLVMStyle(), SC_ExpectIncomplete)); 12964 } 12965 12966 //===----------------------------------------------------------------------===// 12967 // Objective-C tests. 12968 //===----------------------------------------------------------------------===// 12969 12970 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12971 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12972 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12973 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12974 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12975 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12976 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12977 format("-(NSInteger)Method3:(id)anObject;")); 12978 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12979 format("-(NSInteger)Method4:(id)anObject;")); 12980 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12981 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12982 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12983 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12984 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12985 "forAllCells:(BOOL)flag;", 12986 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12987 "forAllCells:(BOOL)flag;")); 12988 12989 // Very long objectiveC method declaration. 12990 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12991 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12992 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12993 " inRange:(NSRange)range\n" 12994 " outRange:(NSRange)out_range\n" 12995 " outRange1:(NSRange)out_range1\n" 12996 " outRange2:(NSRange)out_range2\n" 12997 " outRange3:(NSRange)out_range3\n" 12998 " outRange4:(NSRange)out_range4\n" 12999 " outRange5:(NSRange)out_range5\n" 13000 " outRange6:(NSRange)out_range6\n" 13001 " outRange7:(NSRange)out_range7\n" 13002 " outRange8:(NSRange)out_range8\n" 13003 " outRange9:(NSRange)out_range9;"); 13004 13005 // When the function name has to be wrapped. 13006 FormatStyle Style = getLLVMStyle(); 13007 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 13008 // and always indents instead. 13009 Style.IndentWrappedFunctionNames = false; 13010 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13011 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 13012 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 13013 "}", 13014 Style); 13015 Style.IndentWrappedFunctionNames = true; 13016 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13017 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 13018 " anotherName:(NSString)dddddddddddddd {\n" 13019 "}", 13020 Style); 13021 13022 verifyFormat("- (int)sum:(vector<int>)numbers;"); 13023 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 13024 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 13025 // protocol lists (but not for template classes): 13026 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 13027 13028 verifyFormat("- (int (*)())foo:(int (*)())f;"); 13029 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 13030 13031 // If there's no return type (very rare in practice!), LLVM and Google style 13032 // agree. 13033 verifyFormat("- foo;"); 13034 verifyFormat("- foo:(int)f;"); 13035 verifyGoogleFormat("- foo:(int)foo;"); 13036 } 13037 13038 TEST_F(FormatTest, BreaksStringLiterals) { 13039 EXPECT_EQ("\"some text \"\n" 13040 "\"other\";", 13041 format("\"some text other\";", getLLVMStyleWithColumns(12))); 13042 EXPECT_EQ("\"some text \"\n" 13043 "\"other\";", 13044 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 13045 EXPECT_EQ( 13046 "#define A \\\n" 13047 " \"some \" \\\n" 13048 " \"text \" \\\n" 13049 " \"other\";", 13050 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 13051 EXPECT_EQ( 13052 "#define A \\\n" 13053 " \"so \" \\\n" 13054 " \"text \" \\\n" 13055 " \"other\";", 13056 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 13057 13058 EXPECT_EQ("\"some text\"", 13059 format("\"some text\"", getLLVMStyleWithColumns(1))); 13060 EXPECT_EQ("\"some text\"", 13061 format("\"some text\"", getLLVMStyleWithColumns(11))); 13062 EXPECT_EQ("\"some \"\n" 13063 "\"text\"", 13064 format("\"some text\"", getLLVMStyleWithColumns(10))); 13065 EXPECT_EQ("\"some \"\n" 13066 "\"text\"", 13067 format("\"some text\"", getLLVMStyleWithColumns(7))); 13068 EXPECT_EQ("\"some\"\n" 13069 "\" tex\"\n" 13070 "\"t\"", 13071 format("\"some text\"", getLLVMStyleWithColumns(6))); 13072 EXPECT_EQ("\"some\"\n" 13073 "\" tex\"\n" 13074 "\" and\"", 13075 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 13076 EXPECT_EQ("\"some\"\n" 13077 "\"/tex\"\n" 13078 "\"/and\"", 13079 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 13080 13081 EXPECT_EQ("variable =\n" 13082 " \"long string \"\n" 13083 " \"literal\";", 13084 format("variable = \"long string literal\";", 13085 getLLVMStyleWithColumns(20))); 13086 13087 EXPECT_EQ("variable = f(\n" 13088 " \"long string \"\n" 13089 " \"literal\",\n" 13090 " short,\n" 13091 " loooooooooooooooooooong);", 13092 format("variable = f(\"long string literal\", short, " 13093 "loooooooooooooooooooong);", 13094 getLLVMStyleWithColumns(20))); 13095 13096 EXPECT_EQ( 13097 "f(g(\"long string \"\n" 13098 " \"literal\"),\n" 13099 " b);", 13100 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 13101 EXPECT_EQ("f(g(\"long string \"\n" 13102 " \"literal\",\n" 13103 " a),\n" 13104 " b);", 13105 format("f(g(\"long string literal\", a), b);", 13106 getLLVMStyleWithColumns(20))); 13107 EXPECT_EQ( 13108 "f(\"one two\".split(\n" 13109 " variable));", 13110 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 13111 EXPECT_EQ("f(\"one two three four five six \"\n" 13112 " \"seven\".split(\n" 13113 " really_looooong_variable));", 13114 format("f(\"one two three four five six seven\"." 13115 "split(really_looooong_variable));", 13116 getLLVMStyleWithColumns(33))); 13117 13118 EXPECT_EQ("f(\"some \"\n" 13119 " \"text\",\n" 13120 " other);", 13121 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 13122 13123 // Only break as a last resort. 13124 verifyFormat( 13125 "aaaaaaaaaaaaaaaaaaaa(\n" 13126 " aaaaaaaaaaaaaaaaaaaa,\n" 13127 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 13128 13129 EXPECT_EQ("\"splitmea\"\n" 13130 "\"trandomp\"\n" 13131 "\"oint\"", 13132 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 13133 13134 EXPECT_EQ("\"split/\"\n" 13135 "\"pathat/\"\n" 13136 "\"slashes\"", 13137 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13138 13139 EXPECT_EQ("\"split/\"\n" 13140 "\"pathat/\"\n" 13141 "\"slashes\"", 13142 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13143 EXPECT_EQ("\"split at \"\n" 13144 "\"spaces/at/\"\n" 13145 "\"slashes.at.any$\"\n" 13146 "\"non-alphanumeric%\"\n" 13147 "\"1111111111characte\"\n" 13148 "\"rs\"", 13149 format("\"split at " 13150 "spaces/at/" 13151 "slashes.at." 13152 "any$non-" 13153 "alphanumeric%" 13154 "1111111111characte" 13155 "rs\"", 13156 getLLVMStyleWithColumns(20))); 13157 13158 // Verify that splitting the strings understands 13159 // Style::AlwaysBreakBeforeMultilineStrings. 13160 EXPECT_EQ("aaaaaaaaaaaa(\n" 13161 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 13162 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 13163 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 13164 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13165 "aaaaaaaaaaaaaaaaaaaaaa\");", 13166 getGoogleStyle())); 13167 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13168 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 13169 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 13170 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13171 "aaaaaaaaaaaaaaaaaaaaaa\";", 13172 getGoogleStyle())); 13173 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13174 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13175 format("llvm::outs() << " 13176 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 13177 "aaaaaaaaaaaaaaaaaaa\";")); 13178 EXPECT_EQ("ffff(\n" 13179 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13180 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13181 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 13182 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13183 getGoogleStyle())); 13184 13185 FormatStyle Style = getLLVMStyleWithColumns(12); 13186 Style.BreakStringLiterals = false; 13187 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 13188 13189 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 13190 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13191 EXPECT_EQ("#define A \\\n" 13192 " \"some \" \\\n" 13193 " \"text \" \\\n" 13194 " \"other\";", 13195 format("#define A \"some text other\";", AlignLeft)); 13196 } 13197 13198 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 13199 EXPECT_EQ("C a = \"some more \"\n" 13200 " \"text\";", 13201 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 13202 } 13203 13204 TEST_F(FormatTest, FullyRemoveEmptyLines) { 13205 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 13206 NoEmptyLines.MaxEmptyLinesToKeep = 0; 13207 EXPECT_EQ("int i = a(b());", 13208 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 13209 } 13210 13211 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 13212 EXPECT_EQ( 13213 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13214 "(\n" 13215 " \"x\t\");", 13216 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13217 "aaaaaaa(" 13218 "\"x\t\");")); 13219 } 13220 13221 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 13222 EXPECT_EQ( 13223 "u8\"utf8 string \"\n" 13224 "u8\"literal\";", 13225 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 13226 EXPECT_EQ( 13227 "u\"utf16 string \"\n" 13228 "u\"literal\";", 13229 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 13230 EXPECT_EQ( 13231 "U\"utf32 string \"\n" 13232 "U\"literal\";", 13233 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 13234 EXPECT_EQ("L\"wide string \"\n" 13235 "L\"literal\";", 13236 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 13237 EXPECT_EQ("@\"NSString \"\n" 13238 "@\"literal\";", 13239 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 13240 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 13241 13242 // This input makes clang-format try to split the incomplete unicode escape 13243 // sequence, which used to lead to a crasher. 13244 verifyNoCrash( 13245 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 13246 getLLVMStyleWithColumns(60)); 13247 } 13248 13249 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 13250 FormatStyle Style = getGoogleStyleWithColumns(15); 13251 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 13252 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 13253 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 13254 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 13255 EXPECT_EQ("u8R\"x(raw literal)x\";", 13256 format("u8R\"x(raw literal)x\";", Style)); 13257 } 13258 13259 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 13260 FormatStyle Style = getLLVMStyleWithColumns(20); 13261 EXPECT_EQ( 13262 "_T(\"aaaaaaaaaaaaaa\")\n" 13263 "_T(\"aaaaaaaaaaaaaa\")\n" 13264 "_T(\"aaaaaaaaaaaa\")", 13265 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 13266 EXPECT_EQ("f(x,\n" 13267 " _T(\"aaaaaaaaaaaa\")\n" 13268 " _T(\"aaa\"),\n" 13269 " z);", 13270 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 13271 13272 // FIXME: Handle embedded spaces in one iteration. 13273 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 13274 // "_T(\"aaaaaaaaaaaaa\")\n" 13275 // "_T(\"aaaaaaaaaaaaa\")\n" 13276 // "_T(\"a\")", 13277 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13278 // getLLVMStyleWithColumns(20))); 13279 EXPECT_EQ( 13280 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13281 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 13282 EXPECT_EQ("f(\n" 13283 "#if !TEST\n" 13284 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13285 "#endif\n" 13286 ");", 13287 format("f(\n" 13288 "#if !TEST\n" 13289 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13290 "#endif\n" 13291 ");")); 13292 EXPECT_EQ("f(\n" 13293 "\n" 13294 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 13295 format("f(\n" 13296 "\n" 13297 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 13298 // Regression test for accessing tokens past the end of a vector in the 13299 // TokenLexer. 13300 verifyNoCrash(R"(_T( 13301 " 13302 ) 13303 )"); 13304 } 13305 13306 TEST_F(FormatTest, BreaksStringLiteralOperands) { 13307 // In a function call with two operands, the second can be broken with no line 13308 // break before it. 13309 EXPECT_EQ( 13310 "func(a, \"long long \"\n" 13311 " \"long long\");", 13312 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 13313 // In a function call with three operands, the second must be broken with a 13314 // line break before it. 13315 EXPECT_EQ("func(a,\n" 13316 " \"long long long \"\n" 13317 " \"long\",\n" 13318 " c);", 13319 format("func(a, \"long long long long\", c);", 13320 getLLVMStyleWithColumns(24))); 13321 // In a function call with three operands, the third must be broken with a 13322 // line break before it. 13323 EXPECT_EQ("func(a, b,\n" 13324 " \"long long long \"\n" 13325 " \"long\");", 13326 format("func(a, b, \"long long long long\");", 13327 getLLVMStyleWithColumns(24))); 13328 // In a function call with three operands, both the second and the third must 13329 // be broken with a line break before them. 13330 EXPECT_EQ("func(a,\n" 13331 " \"long long long \"\n" 13332 " \"long\",\n" 13333 " \"long long long \"\n" 13334 " \"long\");", 13335 format("func(a, \"long long long long\", \"long long long long\");", 13336 getLLVMStyleWithColumns(24))); 13337 // In a chain of << with two operands, the second can be broken with no line 13338 // break before it. 13339 EXPECT_EQ("a << \"line line \"\n" 13340 " \"line\";", 13341 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 13342 // In a chain of << with three operands, the second can be broken with no line 13343 // break before it. 13344 EXPECT_EQ( 13345 "abcde << \"line \"\n" 13346 " \"line line\"\n" 13347 " << c;", 13348 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 13349 // In a chain of << with three operands, the third must be broken with a line 13350 // break before it. 13351 EXPECT_EQ( 13352 "a << b\n" 13353 " << \"line line \"\n" 13354 " \"line\";", 13355 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 13356 // In a chain of << with three operands, the second can be broken with no line 13357 // break before it and the third must be broken with a line break before it. 13358 EXPECT_EQ("abcd << \"line line \"\n" 13359 " \"line\"\n" 13360 " << \"line line \"\n" 13361 " \"line\";", 13362 format("abcd << \"line line line\" << \"line line line\";", 13363 getLLVMStyleWithColumns(20))); 13364 // In a chain of binary operators with two operands, the second can be broken 13365 // with no line break before it. 13366 EXPECT_EQ( 13367 "abcd + \"line line \"\n" 13368 " \"line line\";", 13369 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 13370 // In a chain of binary operators with three operands, the second must be 13371 // broken with a line break before it. 13372 EXPECT_EQ("abcd +\n" 13373 " \"line line \"\n" 13374 " \"line line\" +\n" 13375 " e;", 13376 format("abcd + \"line line line line\" + e;", 13377 getLLVMStyleWithColumns(20))); 13378 // In a function call with two operands, with AlignAfterOpenBracket enabled, 13379 // the first must be broken with a line break before it. 13380 FormatStyle Style = getLLVMStyleWithColumns(25); 13381 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13382 EXPECT_EQ("someFunction(\n" 13383 " \"long long long \"\n" 13384 " \"long\",\n" 13385 " a);", 13386 format("someFunction(\"long long long long\", a);", Style)); 13387 } 13388 13389 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 13390 EXPECT_EQ( 13391 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13393 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13394 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13395 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13396 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 13397 } 13398 13399 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 13400 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 13401 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 13402 EXPECT_EQ("fffffffffff(g(R\"x(\n" 13403 "multiline raw string literal xxxxxxxxxxxxxx\n" 13404 ")x\",\n" 13405 " a),\n" 13406 " b);", 13407 format("fffffffffff(g(R\"x(\n" 13408 "multiline raw string literal xxxxxxxxxxxxxx\n" 13409 ")x\", a), b);", 13410 getGoogleStyleWithColumns(20))); 13411 EXPECT_EQ("fffffffffff(\n" 13412 " g(R\"x(qqq\n" 13413 "multiline raw string literal xxxxxxxxxxxxxx\n" 13414 ")x\",\n" 13415 " a),\n" 13416 " b);", 13417 format("fffffffffff(g(R\"x(qqq\n" 13418 "multiline raw string literal xxxxxxxxxxxxxx\n" 13419 ")x\", a), b);", 13420 getGoogleStyleWithColumns(20))); 13421 13422 EXPECT_EQ("fffffffffff(R\"x(\n" 13423 "multiline raw string literal xxxxxxxxxxxxxx\n" 13424 ")x\");", 13425 format("fffffffffff(R\"x(\n" 13426 "multiline raw string literal xxxxxxxxxxxxxx\n" 13427 ")x\");", 13428 getGoogleStyleWithColumns(20))); 13429 EXPECT_EQ("fffffffffff(R\"x(\n" 13430 "multiline raw string literal xxxxxxxxxxxxxx\n" 13431 ")x\" + bbbbbb);", 13432 format("fffffffffff(R\"x(\n" 13433 "multiline raw string literal xxxxxxxxxxxxxx\n" 13434 ")x\" + bbbbbb);", 13435 getGoogleStyleWithColumns(20))); 13436 EXPECT_EQ("fffffffffff(\n" 13437 " R\"x(\n" 13438 "multiline raw string literal xxxxxxxxxxxxxx\n" 13439 ")x\" +\n" 13440 " bbbbbb);", 13441 format("fffffffffff(\n" 13442 " R\"x(\n" 13443 "multiline raw string literal xxxxxxxxxxxxxx\n" 13444 ")x\" + bbbbbb);", 13445 getGoogleStyleWithColumns(20))); 13446 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 13447 format("fffffffffff(\n" 13448 " R\"(single line raw string)\" + bbbbbb);")); 13449 } 13450 13451 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13452 verifyFormat("string a = \"unterminated;"); 13453 EXPECT_EQ("function(\"unterminated,\n" 13454 " OtherParameter);", 13455 format("function( \"unterminated,\n" 13456 " OtherParameter);")); 13457 } 13458 13459 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13460 FormatStyle Style = getLLVMStyle(); 13461 Style.Standard = FormatStyle::LS_Cpp03; 13462 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13463 format("#define x(_a) printf(\"foo\"_a);", Style)); 13464 } 13465 13466 TEST_F(FormatTest, CppLexVersion) { 13467 FormatStyle Style = getLLVMStyle(); 13468 // Formatting of x * y differs if x is a type. 13469 verifyFormat("void foo() { MACRO(a * b); }", Style); 13470 verifyFormat("void foo() { MACRO(int *b); }", Style); 13471 13472 // LLVM style uses latest lexer. 13473 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13474 Style.Standard = FormatStyle::LS_Cpp17; 13475 // But in c++17, char8_t isn't a keyword. 13476 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13477 } 13478 13479 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13480 13481 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13482 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13483 " \"ddeeefff\");", 13484 format("someFunction(\"aaabbbcccdddeeefff\");", 13485 getLLVMStyleWithColumns(25))); 13486 EXPECT_EQ("someFunction1234567890(\n" 13487 " \"aaabbbcccdddeeefff\");", 13488 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13489 getLLVMStyleWithColumns(26))); 13490 EXPECT_EQ("someFunction1234567890(\n" 13491 " \"aaabbbcccdddeeeff\"\n" 13492 " \"f\");", 13493 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13494 getLLVMStyleWithColumns(25))); 13495 EXPECT_EQ("someFunction1234567890(\n" 13496 " \"aaabbbcccdddeeeff\"\n" 13497 " \"f\");", 13498 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13499 getLLVMStyleWithColumns(24))); 13500 EXPECT_EQ("someFunction(\n" 13501 " \"aaabbbcc ddde \"\n" 13502 " \"efff\");", 13503 format("someFunction(\"aaabbbcc ddde efff\");", 13504 getLLVMStyleWithColumns(25))); 13505 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13506 " \"ddeeefff\");", 13507 format("someFunction(\"aaabbbccc ddeeefff\");", 13508 getLLVMStyleWithColumns(25))); 13509 EXPECT_EQ("someFunction1234567890(\n" 13510 " \"aaabb \"\n" 13511 " \"cccdddeeefff\");", 13512 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13513 getLLVMStyleWithColumns(25))); 13514 EXPECT_EQ("#define A \\\n" 13515 " string s = \\\n" 13516 " \"123456789\" \\\n" 13517 " \"0\"; \\\n" 13518 " int i;", 13519 format("#define A string s = \"1234567890\"; int i;", 13520 getLLVMStyleWithColumns(20))); 13521 EXPECT_EQ("someFunction(\n" 13522 " \"aaabbbcc \"\n" 13523 " \"dddeeefff\");", 13524 format("someFunction(\"aaabbbcc dddeeefff\");", 13525 getLLVMStyleWithColumns(25))); 13526 } 13527 13528 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13529 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13530 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13531 EXPECT_EQ("\"test\"\n" 13532 "\"\\n\"", 13533 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13534 EXPECT_EQ("\"tes\\\\\"\n" 13535 "\"n\"", 13536 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13537 EXPECT_EQ("\"\\\\\\\\\"\n" 13538 "\"\\n\"", 13539 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13540 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13541 EXPECT_EQ("\"\\uff01\"\n" 13542 "\"test\"", 13543 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13544 EXPECT_EQ("\"\\Uff01ff02\"", 13545 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13546 EXPECT_EQ("\"\\x000000000001\"\n" 13547 "\"next\"", 13548 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13549 EXPECT_EQ("\"\\x000000000001next\"", 13550 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13551 EXPECT_EQ("\"\\x000000000001\"", 13552 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13553 EXPECT_EQ("\"test\"\n" 13554 "\"\\000000\"\n" 13555 "\"000001\"", 13556 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13557 EXPECT_EQ("\"test\\000\"\n" 13558 "\"00000000\"\n" 13559 "\"1\"", 13560 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13561 } 13562 13563 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13564 verifyFormat("void f() {\n" 13565 " return g() {}\n" 13566 " void h() {}"); 13567 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13568 "g();\n" 13569 "}"); 13570 } 13571 13572 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13573 verifyFormat( 13574 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13575 } 13576 13577 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13578 verifyFormat("class X {\n" 13579 " void f() {\n" 13580 " }\n" 13581 "};", 13582 getLLVMStyleWithColumns(12)); 13583 } 13584 13585 TEST_F(FormatTest, ConfigurableIndentWidth) { 13586 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13587 EightIndent.IndentWidth = 8; 13588 EightIndent.ContinuationIndentWidth = 8; 13589 verifyFormat("void f() {\n" 13590 " someFunction();\n" 13591 " if (true) {\n" 13592 " f();\n" 13593 " }\n" 13594 "}", 13595 EightIndent); 13596 verifyFormat("class X {\n" 13597 " void f() {\n" 13598 " }\n" 13599 "};", 13600 EightIndent); 13601 verifyFormat("int x[] = {\n" 13602 " call(),\n" 13603 " call()};", 13604 EightIndent); 13605 } 13606 13607 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13608 verifyFormat("double\n" 13609 "f();", 13610 getLLVMStyleWithColumns(8)); 13611 } 13612 13613 TEST_F(FormatTest, ConfigurableUseOfTab) { 13614 FormatStyle Tab = getLLVMStyleWithColumns(42); 13615 Tab.IndentWidth = 8; 13616 Tab.UseTab = FormatStyle::UT_Always; 13617 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13618 13619 EXPECT_EQ("if (aaaaaaaa && // q\n" 13620 " bb)\t\t// w\n" 13621 "\t;", 13622 format("if (aaaaaaaa &&// q\n" 13623 "bb)// w\n" 13624 ";", 13625 Tab)); 13626 EXPECT_EQ("if (aaa && bbb) // w\n" 13627 "\t;", 13628 format("if(aaa&&bbb)// w\n" 13629 ";", 13630 Tab)); 13631 13632 verifyFormat("class X {\n" 13633 "\tvoid f() {\n" 13634 "\t\tsomeFunction(parameter1,\n" 13635 "\t\t\t parameter2);\n" 13636 "\t}\n" 13637 "};", 13638 Tab); 13639 verifyFormat("#define A \\\n" 13640 "\tvoid f() { \\\n" 13641 "\t\tsomeFunction( \\\n" 13642 "\t\t parameter1, \\\n" 13643 "\t\t parameter2); \\\n" 13644 "\t}", 13645 Tab); 13646 verifyFormat("int a;\t // x\n" 13647 "int bbbbbbbb; // x\n", 13648 Tab); 13649 13650 Tab.TabWidth = 4; 13651 Tab.IndentWidth = 8; 13652 verifyFormat("class TabWidth4Indent8 {\n" 13653 "\t\tvoid f() {\n" 13654 "\t\t\t\tsomeFunction(parameter1,\n" 13655 "\t\t\t\t\t\t\t parameter2);\n" 13656 "\t\t}\n" 13657 "};", 13658 Tab); 13659 13660 Tab.TabWidth = 4; 13661 Tab.IndentWidth = 4; 13662 verifyFormat("class TabWidth4Indent4 {\n" 13663 "\tvoid f() {\n" 13664 "\t\tsomeFunction(parameter1,\n" 13665 "\t\t\t\t\t parameter2);\n" 13666 "\t}\n" 13667 "};", 13668 Tab); 13669 13670 Tab.TabWidth = 8; 13671 Tab.IndentWidth = 4; 13672 verifyFormat("class TabWidth8Indent4 {\n" 13673 " void f() {\n" 13674 "\tsomeFunction(parameter1,\n" 13675 "\t\t parameter2);\n" 13676 " }\n" 13677 "};", 13678 Tab); 13679 13680 Tab.TabWidth = 8; 13681 Tab.IndentWidth = 8; 13682 EXPECT_EQ("/*\n" 13683 "\t a\t\tcomment\n" 13684 "\t in multiple lines\n" 13685 " */", 13686 format(" /*\t \t \n" 13687 " \t \t a\t\tcomment\t \t\n" 13688 " \t \t in multiple lines\t\n" 13689 " \t */", 13690 Tab)); 13691 13692 Tab.UseTab = FormatStyle::UT_ForIndentation; 13693 verifyFormat("{\n" 13694 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13695 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13696 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13697 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13698 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13699 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13700 "};", 13701 Tab); 13702 verifyFormat("enum AA {\n" 13703 "\ta1, // Force multiple lines\n" 13704 "\ta2,\n" 13705 "\ta3\n" 13706 "};", 13707 Tab); 13708 EXPECT_EQ("if (aaaaaaaa && // q\n" 13709 " bb) // w\n" 13710 "\t;", 13711 format("if (aaaaaaaa &&// q\n" 13712 "bb)// w\n" 13713 ";", 13714 Tab)); 13715 verifyFormat("class X {\n" 13716 "\tvoid f() {\n" 13717 "\t\tsomeFunction(parameter1,\n" 13718 "\t\t parameter2);\n" 13719 "\t}\n" 13720 "};", 13721 Tab); 13722 verifyFormat("{\n" 13723 "\tQ(\n" 13724 "\t {\n" 13725 "\t\t int a;\n" 13726 "\t\t someFunction(aaaaaaaa,\n" 13727 "\t\t bbbbbbb);\n" 13728 "\t },\n" 13729 "\t p);\n" 13730 "}", 13731 Tab); 13732 EXPECT_EQ("{\n" 13733 "\t/* aaaa\n" 13734 "\t bbbb */\n" 13735 "}", 13736 format("{\n" 13737 "/* aaaa\n" 13738 " bbbb */\n" 13739 "}", 13740 Tab)); 13741 EXPECT_EQ("{\n" 13742 "\t/*\n" 13743 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13744 "\t bbbbbbbbbbbbb\n" 13745 "\t*/\n" 13746 "}", 13747 format("{\n" 13748 "/*\n" 13749 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13750 "*/\n" 13751 "}", 13752 Tab)); 13753 EXPECT_EQ("{\n" 13754 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13755 "\t// bbbbbbbbbbbbb\n" 13756 "}", 13757 format("{\n" 13758 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13759 "}", 13760 Tab)); 13761 EXPECT_EQ("{\n" 13762 "\t/*\n" 13763 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13764 "\t bbbbbbbbbbbbb\n" 13765 "\t*/\n" 13766 "}", 13767 format("{\n" 13768 "\t/*\n" 13769 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13770 "\t*/\n" 13771 "}", 13772 Tab)); 13773 EXPECT_EQ("{\n" 13774 "\t/*\n" 13775 "\n" 13776 "\t*/\n" 13777 "}", 13778 format("{\n" 13779 "\t/*\n" 13780 "\n" 13781 "\t*/\n" 13782 "}", 13783 Tab)); 13784 EXPECT_EQ("{\n" 13785 "\t/*\n" 13786 " asdf\n" 13787 "\t*/\n" 13788 "}", 13789 format("{\n" 13790 "\t/*\n" 13791 " asdf\n" 13792 "\t*/\n" 13793 "}", 13794 Tab)); 13795 13796 verifyFormat("void f() {\n" 13797 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 13798 "\t : bbbbbbbbbbbbbbbbbb\n" 13799 "}", 13800 Tab); 13801 FormatStyle TabNoBreak = Tab; 13802 TabNoBreak.BreakBeforeTernaryOperators = false; 13803 verifyFormat("void f() {\n" 13804 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 13805 "\t bbbbbbbbbbbbbbbbbb\n" 13806 "}", 13807 TabNoBreak); 13808 verifyFormat("void f() {\n" 13809 "\treturn true ?\n" 13810 "\t aaaaaaaaaaaaaaaaaaaa :\n" 13811 "\t bbbbbbbbbbbbbbbbbbbb\n" 13812 "}", 13813 TabNoBreak); 13814 13815 Tab.UseTab = FormatStyle::UT_Never; 13816 EXPECT_EQ("/*\n" 13817 " a\t\tcomment\n" 13818 " in multiple lines\n" 13819 " */", 13820 format(" /*\t \t \n" 13821 " \t \t a\t\tcomment\t \t\n" 13822 " \t \t in multiple lines\t\n" 13823 " \t */", 13824 Tab)); 13825 EXPECT_EQ("/* some\n" 13826 " comment */", 13827 format(" \t \t /* some\n" 13828 " \t \t comment */", 13829 Tab)); 13830 EXPECT_EQ("int a; /* some\n" 13831 " comment */", 13832 format(" \t \t int a; /* some\n" 13833 " \t \t comment */", 13834 Tab)); 13835 13836 EXPECT_EQ("int a; /* some\n" 13837 "comment */", 13838 format(" \t \t int\ta; /* some\n" 13839 " \t \t comment */", 13840 Tab)); 13841 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13842 " comment */", 13843 format(" \t \t f(\"\t\t\"); /* some\n" 13844 " \t \t comment */", 13845 Tab)); 13846 EXPECT_EQ("{\n" 13847 " /*\n" 13848 " * Comment\n" 13849 " */\n" 13850 " int i;\n" 13851 "}", 13852 format("{\n" 13853 "\t/*\n" 13854 "\t * Comment\n" 13855 "\t */\n" 13856 "\t int i;\n" 13857 "}", 13858 Tab)); 13859 13860 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13861 Tab.TabWidth = 8; 13862 Tab.IndentWidth = 8; 13863 EXPECT_EQ("if (aaaaaaaa && // q\n" 13864 " bb) // w\n" 13865 "\t;", 13866 format("if (aaaaaaaa &&// q\n" 13867 "bb)// w\n" 13868 ";", 13869 Tab)); 13870 EXPECT_EQ("if (aaa && bbb) // w\n" 13871 "\t;", 13872 format("if(aaa&&bbb)// w\n" 13873 ";", 13874 Tab)); 13875 verifyFormat("class X {\n" 13876 "\tvoid f() {\n" 13877 "\t\tsomeFunction(parameter1,\n" 13878 "\t\t\t parameter2);\n" 13879 "\t}\n" 13880 "};", 13881 Tab); 13882 verifyFormat("#define A \\\n" 13883 "\tvoid f() { \\\n" 13884 "\t\tsomeFunction( \\\n" 13885 "\t\t parameter1, \\\n" 13886 "\t\t parameter2); \\\n" 13887 "\t}", 13888 Tab); 13889 Tab.TabWidth = 4; 13890 Tab.IndentWidth = 8; 13891 verifyFormat("class TabWidth4Indent8 {\n" 13892 "\t\tvoid f() {\n" 13893 "\t\t\t\tsomeFunction(parameter1,\n" 13894 "\t\t\t\t\t\t\t parameter2);\n" 13895 "\t\t}\n" 13896 "};", 13897 Tab); 13898 Tab.TabWidth = 4; 13899 Tab.IndentWidth = 4; 13900 verifyFormat("class TabWidth4Indent4 {\n" 13901 "\tvoid f() {\n" 13902 "\t\tsomeFunction(parameter1,\n" 13903 "\t\t\t\t\t parameter2);\n" 13904 "\t}\n" 13905 "};", 13906 Tab); 13907 Tab.TabWidth = 8; 13908 Tab.IndentWidth = 4; 13909 verifyFormat("class TabWidth8Indent4 {\n" 13910 " void f() {\n" 13911 "\tsomeFunction(parameter1,\n" 13912 "\t\t parameter2);\n" 13913 " }\n" 13914 "};", 13915 Tab); 13916 Tab.TabWidth = 8; 13917 Tab.IndentWidth = 8; 13918 EXPECT_EQ("/*\n" 13919 "\t a\t\tcomment\n" 13920 "\t in multiple lines\n" 13921 " */", 13922 format(" /*\t \t \n" 13923 " \t \t a\t\tcomment\t \t\n" 13924 " \t \t in multiple lines\t\n" 13925 " \t */", 13926 Tab)); 13927 verifyFormat("{\n" 13928 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13929 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13930 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13931 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13932 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13933 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13934 "};", 13935 Tab); 13936 verifyFormat("enum AA {\n" 13937 "\ta1, // Force multiple lines\n" 13938 "\ta2,\n" 13939 "\ta3\n" 13940 "};", 13941 Tab); 13942 EXPECT_EQ("if (aaaaaaaa && // q\n" 13943 " bb) // w\n" 13944 "\t;", 13945 format("if (aaaaaaaa &&// q\n" 13946 "bb)// w\n" 13947 ";", 13948 Tab)); 13949 verifyFormat("class X {\n" 13950 "\tvoid f() {\n" 13951 "\t\tsomeFunction(parameter1,\n" 13952 "\t\t\t parameter2);\n" 13953 "\t}\n" 13954 "};", 13955 Tab); 13956 verifyFormat("{\n" 13957 "\tQ(\n" 13958 "\t {\n" 13959 "\t\t int a;\n" 13960 "\t\t someFunction(aaaaaaaa,\n" 13961 "\t\t\t\t bbbbbbb);\n" 13962 "\t },\n" 13963 "\t p);\n" 13964 "}", 13965 Tab); 13966 EXPECT_EQ("{\n" 13967 "\t/* aaaa\n" 13968 "\t bbbb */\n" 13969 "}", 13970 format("{\n" 13971 "/* aaaa\n" 13972 " bbbb */\n" 13973 "}", 13974 Tab)); 13975 EXPECT_EQ("{\n" 13976 "\t/*\n" 13977 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13978 "\t bbbbbbbbbbbbb\n" 13979 "\t*/\n" 13980 "}", 13981 format("{\n" 13982 "/*\n" 13983 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13984 "*/\n" 13985 "}", 13986 Tab)); 13987 EXPECT_EQ("{\n" 13988 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13989 "\t// bbbbbbbbbbbbb\n" 13990 "}", 13991 format("{\n" 13992 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13993 "}", 13994 Tab)); 13995 EXPECT_EQ("{\n" 13996 "\t/*\n" 13997 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13998 "\t bbbbbbbbbbbbb\n" 13999 "\t*/\n" 14000 "}", 14001 format("{\n" 14002 "\t/*\n" 14003 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14004 "\t*/\n" 14005 "}", 14006 Tab)); 14007 EXPECT_EQ("{\n" 14008 "\t/*\n" 14009 "\n" 14010 "\t*/\n" 14011 "}", 14012 format("{\n" 14013 "\t/*\n" 14014 "\n" 14015 "\t*/\n" 14016 "}", 14017 Tab)); 14018 EXPECT_EQ("{\n" 14019 "\t/*\n" 14020 " asdf\n" 14021 "\t*/\n" 14022 "}", 14023 format("{\n" 14024 "\t/*\n" 14025 " asdf\n" 14026 "\t*/\n" 14027 "}", 14028 Tab)); 14029 EXPECT_EQ("/* some\n" 14030 " comment */", 14031 format(" \t \t /* some\n" 14032 " \t \t comment */", 14033 Tab)); 14034 EXPECT_EQ("int a; /* some\n" 14035 " comment */", 14036 format(" \t \t int a; /* some\n" 14037 " \t \t comment */", 14038 Tab)); 14039 EXPECT_EQ("int a; /* some\n" 14040 "comment */", 14041 format(" \t \t int\ta; /* some\n" 14042 " \t \t comment */", 14043 Tab)); 14044 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14045 " comment */", 14046 format(" \t \t f(\"\t\t\"); /* some\n" 14047 " \t \t comment */", 14048 Tab)); 14049 EXPECT_EQ("{\n" 14050 "\t/*\n" 14051 "\t * Comment\n" 14052 "\t */\n" 14053 "\tint i;\n" 14054 "}", 14055 format("{\n" 14056 "\t/*\n" 14057 "\t * Comment\n" 14058 "\t */\n" 14059 "\t int i;\n" 14060 "}", 14061 Tab)); 14062 Tab.TabWidth = 2; 14063 Tab.IndentWidth = 2; 14064 EXPECT_EQ("{\n" 14065 "\t/* aaaa\n" 14066 "\t\t bbbb */\n" 14067 "}", 14068 format("{\n" 14069 "/* aaaa\n" 14070 "\t bbbb */\n" 14071 "}", 14072 Tab)); 14073 EXPECT_EQ("{\n" 14074 "\t/*\n" 14075 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14076 "\t\tbbbbbbbbbbbbb\n" 14077 "\t*/\n" 14078 "}", 14079 format("{\n" 14080 "/*\n" 14081 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14082 "*/\n" 14083 "}", 14084 Tab)); 14085 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14086 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14087 Tab.TabWidth = 4; 14088 Tab.IndentWidth = 4; 14089 verifyFormat("class Assign {\n" 14090 "\tvoid f() {\n" 14091 "\t\tint x = 123;\n" 14092 "\t\tint random = 4;\n" 14093 "\t\tstd::string alphabet =\n" 14094 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14095 "\t}\n" 14096 "};", 14097 Tab); 14098 14099 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14100 Tab.TabWidth = 8; 14101 Tab.IndentWidth = 8; 14102 EXPECT_EQ("if (aaaaaaaa && // q\n" 14103 " bb) // w\n" 14104 "\t;", 14105 format("if (aaaaaaaa &&// q\n" 14106 "bb)// w\n" 14107 ";", 14108 Tab)); 14109 EXPECT_EQ("if (aaa && bbb) // w\n" 14110 "\t;", 14111 format("if(aaa&&bbb)// w\n" 14112 ";", 14113 Tab)); 14114 verifyFormat("class X {\n" 14115 "\tvoid f() {\n" 14116 "\t\tsomeFunction(parameter1,\n" 14117 "\t\t parameter2);\n" 14118 "\t}\n" 14119 "};", 14120 Tab); 14121 verifyFormat("#define A \\\n" 14122 "\tvoid f() { \\\n" 14123 "\t\tsomeFunction( \\\n" 14124 "\t\t parameter1, \\\n" 14125 "\t\t parameter2); \\\n" 14126 "\t}", 14127 Tab); 14128 Tab.TabWidth = 4; 14129 Tab.IndentWidth = 8; 14130 verifyFormat("class TabWidth4Indent8 {\n" 14131 "\t\tvoid f() {\n" 14132 "\t\t\t\tsomeFunction(parameter1,\n" 14133 "\t\t\t\t parameter2);\n" 14134 "\t\t}\n" 14135 "};", 14136 Tab); 14137 Tab.TabWidth = 4; 14138 Tab.IndentWidth = 4; 14139 verifyFormat("class TabWidth4Indent4 {\n" 14140 "\tvoid f() {\n" 14141 "\t\tsomeFunction(parameter1,\n" 14142 "\t\t parameter2);\n" 14143 "\t}\n" 14144 "};", 14145 Tab); 14146 Tab.TabWidth = 8; 14147 Tab.IndentWidth = 4; 14148 verifyFormat("class TabWidth8Indent4 {\n" 14149 " void f() {\n" 14150 "\tsomeFunction(parameter1,\n" 14151 "\t parameter2);\n" 14152 " }\n" 14153 "};", 14154 Tab); 14155 Tab.TabWidth = 8; 14156 Tab.IndentWidth = 8; 14157 EXPECT_EQ("/*\n" 14158 " a\t\tcomment\n" 14159 " in multiple lines\n" 14160 " */", 14161 format(" /*\t \t \n" 14162 " \t \t a\t\tcomment\t \t\n" 14163 " \t \t in multiple lines\t\n" 14164 " \t */", 14165 Tab)); 14166 verifyFormat("{\n" 14167 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14168 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14169 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14170 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14171 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14172 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14173 "};", 14174 Tab); 14175 verifyFormat("enum AA {\n" 14176 "\ta1, // Force multiple lines\n" 14177 "\ta2,\n" 14178 "\ta3\n" 14179 "};", 14180 Tab); 14181 EXPECT_EQ("if (aaaaaaaa && // q\n" 14182 " bb) // w\n" 14183 "\t;", 14184 format("if (aaaaaaaa &&// q\n" 14185 "bb)// w\n" 14186 ";", 14187 Tab)); 14188 verifyFormat("class X {\n" 14189 "\tvoid f() {\n" 14190 "\t\tsomeFunction(parameter1,\n" 14191 "\t\t parameter2);\n" 14192 "\t}\n" 14193 "};", 14194 Tab); 14195 verifyFormat("{\n" 14196 "\tQ(\n" 14197 "\t {\n" 14198 "\t\t int a;\n" 14199 "\t\t someFunction(aaaaaaaa,\n" 14200 "\t\t bbbbbbb);\n" 14201 "\t },\n" 14202 "\t p);\n" 14203 "}", 14204 Tab); 14205 EXPECT_EQ("{\n" 14206 "\t/* aaaa\n" 14207 "\t bbbb */\n" 14208 "}", 14209 format("{\n" 14210 "/* aaaa\n" 14211 " bbbb */\n" 14212 "}", 14213 Tab)); 14214 EXPECT_EQ("{\n" 14215 "\t/*\n" 14216 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14217 "\t bbbbbbbbbbbbb\n" 14218 "\t*/\n" 14219 "}", 14220 format("{\n" 14221 "/*\n" 14222 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14223 "*/\n" 14224 "}", 14225 Tab)); 14226 EXPECT_EQ("{\n" 14227 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14228 "\t// bbbbbbbbbbbbb\n" 14229 "}", 14230 format("{\n" 14231 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14232 "}", 14233 Tab)); 14234 EXPECT_EQ("{\n" 14235 "\t/*\n" 14236 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14237 "\t bbbbbbbbbbbbb\n" 14238 "\t*/\n" 14239 "}", 14240 format("{\n" 14241 "\t/*\n" 14242 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14243 "\t*/\n" 14244 "}", 14245 Tab)); 14246 EXPECT_EQ("{\n" 14247 "\t/*\n" 14248 "\n" 14249 "\t*/\n" 14250 "}", 14251 format("{\n" 14252 "\t/*\n" 14253 "\n" 14254 "\t*/\n" 14255 "}", 14256 Tab)); 14257 EXPECT_EQ("{\n" 14258 "\t/*\n" 14259 " asdf\n" 14260 "\t*/\n" 14261 "}", 14262 format("{\n" 14263 "\t/*\n" 14264 " asdf\n" 14265 "\t*/\n" 14266 "}", 14267 Tab)); 14268 EXPECT_EQ("/* some\n" 14269 " comment */", 14270 format(" \t \t /* some\n" 14271 " \t \t comment */", 14272 Tab)); 14273 EXPECT_EQ("int a; /* some\n" 14274 " comment */", 14275 format(" \t \t int a; /* some\n" 14276 " \t \t comment */", 14277 Tab)); 14278 EXPECT_EQ("int a; /* some\n" 14279 "comment */", 14280 format(" \t \t int\ta; /* some\n" 14281 " \t \t comment */", 14282 Tab)); 14283 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14284 " comment */", 14285 format(" \t \t f(\"\t\t\"); /* some\n" 14286 " \t \t comment */", 14287 Tab)); 14288 EXPECT_EQ("{\n" 14289 "\t/*\n" 14290 "\t * Comment\n" 14291 "\t */\n" 14292 "\tint i;\n" 14293 "}", 14294 format("{\n" 14295 "\t/*\n" 14296 "\t * Comment\n" 14297 "\t */\n" 14298 "\t int i;\n" 14299 "}", 14300 Tab)); 14301 Tab.TabWidth = 2; 14302 Tab.IndentWidth = 2; 14303 EXPECT_EQ("{\n" 14304 "\t/* aaaa\n" 14305 "\t bbbb */\n" 14306 "}", 14307 format("{\n" 14308 "/* aaaa\n" 14309 " bbbb */\n" 14310 "}", 14311 Tab)); 14312 EXPECT_EQ("{\n" 14313 "\t/*\n" 14314 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14315 "\t bbbbbbbbbbbbb\n" 14316 "\t*/\n" 14317 "}", 14318 format("{\n" 14319 "/*\n" 14320 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14321 "*/\n" 14322 "}", 14323 Tab)); 14324 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14325 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14326 Tab.TabWidth = 4; 14327 Tab.IndentWidth = 4; 14328 verifyFormat("class Assign {\n" 14329 "\tvoid f() {\n" 14330 "\t\tint x = 123;\n" 14331 "\t\tint random = 4;\n" 14332 "\t\tstd::string alphabet =\n" 14333 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14334 "\t}\n" 14335 "};", 14336 Tab); 14337 Tab.AlignOperands = FormatStyle::OAS_Align; 14338 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 14339 " cccccccccccccccccccc;", 14340 Tab); 14341 // no alignment 14342 verifyFormat("int aaaaaaaaaa =\n" 14343 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 14344 Tab); 14345 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 14346 " : bbbbbbbbbbbbbb ? 222222222222222\n" 14347 " : 333333333333333;", 14348 Tab); 14349 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14350 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 14351 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 14352 " + cccccccccccccccccccc;", 14353 Tab); 14354 } 14355 14356 TEST_F(FormatTest, ZeroTabWidth) { 14357 FormatStyle Tab = getLLVMStyleWithColumns(42); 14358 Tab.IndentWidth = 8; 14359 Tab.UseTab = FormatStyle::UT_Never; 14360 Tab.TabWidth = 0; 14361 EXPECT_EQ("void a(){\n" 14362 " // line starts with '\t'\n" 14363 "};", 14364 format("void a(){\n" 14365 "\t// line starts with '\t'\n" 14366 "};", 14367 Tab)); 14368 14369 EXPECT_EQ("void a(){\n" 14370 " // line starts with '\t'\n" 14371 "};", 14372 format("void a(){\n" 14373 "\t\t// line starts with '\t'\n" 14374 "};", 14375 Tab)); 14376 14377 Tab.UseTab = FormatStyle::UT_ForIndentation; 14378 EXPECT_EQ("void a(){\n" 14379 " // line starts with '\t'\n" 14380 "};", 14381 format("void a(){\n" 14382 "\t// line starts with '\t'\n" 14383 "};", 14384 Tab)); 14385 14386 EXPECT_EQ("void a(){\n" 14387 " // line starts with '\t'\n" 14388 "};", 14389 format("void a(){\n" 14390 "\t\t// line starts with '\t'\n" 14391 "};", 14392 Tab)); 14393 14394 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14395 EXPECT_EQ("void a(){\n" 14396 " // line starts with '\t'\n" 14397 "};", 14398 format("void a(){\n" 14399 "\t// line starts with '\t'\n" 14400 "};", 14401 Tab)); 14402 14403 EXPECT_EQ("void a(){\n" 14404 " // line starts with '\t'\n" 14405 "};", 14406 format("void a(){\n" 14407 "\t\t// line starts with '\t'\n" 14408 "};", 14409 Tab)); 14410 14411 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14412 EXPECT_EQ("void a(){\n" 14413 " // line starts with '\t'\n" 14414 "};", 14415 format("void a(){\n" 14416 "\t// line starts with '\t'\n" 14417 "};", 14418 Tab)); 14419 14420 EXPECT_EQ("void a(){\n" 14421 " // line starts with '\t'\n" 14422 "};", 14423 format("void a(){\n" 14424 "\t\t// line starts with '\t'\n" 14425 "};", 14426 Tab)); 14427 14428 Tab.UseTab = FormatStyle::UT_Always; 14429 EXPECT_EQ("void a(){\n" 14430 "// line starts with '\t'\n" 14431 "};", 14432 format("void a(){\n" 14433 "\t// line starts with '\t'\n" 14434 "};", 14435 Tab)); 14436 14437 EXPECT_EQ("void a(){\n" 14438 "// line starts with '\t'\n" 14439 "};", 14440 format("void a(){\n" 14441 "\t\t// line starts with '\t'\n" 14442 "};", 14443 Tab)); 14444 } 14445 14446 TEST_F(FormatTest, CalculatesOriginalColumn) { 14447 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14448 "q\"; /* some\n" 14449 " comment */", 14450 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14451 "q\"; /* some\n" 14452 " comment */", 14453 getLLVMStyle())); 14454 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14455 "/* some\n" 14456 " comment */", 14457 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14458 " /* some\n" 14459 " comment */", 14460 getLLVMStyle())); 14461 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14462 "qqq\n" 14463 "/* some\n" 14464 " comment */", 14465 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14466 "qqq\n" 14467 " /* some\n" 14468 " comment */", 14469 getLLVMStyle())); 14470 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14471 "wwww; /* some\n" 14472 " comment */", 14473 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14474 "wwww; /* some\n" 14475 " comment */", 14476 getLLVMStyle())); 14477 } 14478 14479 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 14480 FormatStyle NoSpace = getLLVMStyle(); 14481 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 14482 14483 verifyFormat("while(true)\n" 14484 " continue;", 14485 NoSpace); 14486 verifyFormat("for(;;)\n" 14487 " continue;", 14488 NoSpace); 14489 verifyFormat("if(true)\n" 14490 " f();\n" 14491 "else if(true)\n" 14492 " f();", 14493 NoSpace); 14494 verifyFormat("do {\n" 14495 " do_something();\n" 14496 "} while(something());", 14497 NoSpace); 14498 verifyFormat("switch(x) {\n" 14499 "default:\n" 14500 " break;\n" 14501 "}", 14502 NoSpace); 14503 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14504 verifyFormat("size_t x = sizeof(x);", NoSpace); 14505 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14506 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14507 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14508 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14509 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14510 verifyFormat("alignas(128) char a[128];", NoSpace); 14511 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14512 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14513 verifyFormat("int f() throw(Deprecated);", NoSpace); 14514 verifyFormat("typedef void (*cb)(int);", NoSpace); 14515 verifyFormat("T A::operator()();", NoSpace); 14516 verifyFormat("X A::operator++(T);", NoSpace); 14517 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14518 14519 FormatStyle Space = getLLVMStyle(); 14520 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14521 14522 verifyFormat("int f ();", Space); 14523 verifyFormat("void f (int a, T b) {\n" 14524 " while (true)\n" 14525 " continue;\n" 14526 "}", 14527 Space); 14528 verifyFormat("if (true)\n" 14529 " f ();\n" 14530 "else if (true)\n" 14531 " f ();", 14532 Space); 14533 verifyFormat("do {\n" 14534 " do_something ();\n" 14535 "} while (something ());", 14536 Space); 14537 verifyFormat("switch (x) {\n" 14538 "default:\n" 14539 " break;\n" 14540 "}", 14541 Space); 14542 verifyFormat("A::A () : a (1) {}", Space); 14543 verifyFormat("void f () __attribute__ ((asdf));", Space); 14544 verifyFormat("*(&a + 1);\n" 14545 "&((&a)[1]);\n" 14546 "a[(b + c) * d];\n" 14547 "(((a + 1) * 2) + 3) * 4;", 14548 Space); 14549 verifyFormat("#define A(x) x", Space); 14550 verifyFormat("#define A (x) x", Space); 14551 verifyFormat("#if defined(x)\n" 14552 "#endif", 14553 Space); 14554 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14555 verifyFormat("size_t x = sizeof (x);", Space); 14556 verifyFormat("auto f (int x) -> decltype (x);", Space); 14557 verifyFormat("auto f (int x) -> typeof (x);", Space); 14558 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14559 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14560 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14561 verifyFormat("alignas (128) char a[128];", Space); 14562 verifyFormat("size_t x = alignof (MyType);", Space); 14563 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14564 verifyFormat("int f () throw (Deprecated);", Space); 14565 verifyFormat("typedef void (*cb) (int);", Space); 14566 // FIXME these tests regressed behaviour. 14567 // verifyFormat("T A::operator() ();", Space); 14568 // verifyFormat("X A::operator++ (T);", Space); 14569 verifyFormat("auto lambda = [] () { return 0; };", Space); 14570 verifyFormat("int x = int (y);", Space); 14571 14572 FormatStyle SomeSpace = getLLVMStyle(); 14573 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14574 14575 verifyFormat("[]() -> float {}", SomeSpace); 14576 verifyFormat("[] (auto foo) {}", SomeSpace); 14577 verifyFormat("[foo]() -> int {}", SomeSpace); 14578 verifyFormat("int f();", SomeSpace); 14579 verifyFormat("void f (int a, T b) {\n" 14580 " while (true)\n" 14581 " continue;\n" 14582 "}", 14583 SomeSpace); 14584 verifyFormat("if (true)\n" 14585 " f();\n" 14586 "else if (true)\n" 14587 " f();", 14588 SomeSpace); 14589 verifyFormat("do {\n" 14590 " do_something();\n" 14591 "} while (something());", 14592 SomeSpace); 14593 verifyFormat("switch (x) {\n" 14594 "default:\n" 14595 " break;\n" 14596 "}", 14597 SomeSpace); 14598 verifyFormat("A::A() : a (1) {}", SomeSpace); 14599 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14600 verifyFormat("*(&a + 1);\n" 14601 "&((&a)[1]);\n" 14602 "a[(b + c) * d];\n" 14603 "(((a + 1) * 2) + 3) * 4;", 14604 SomeSpace); 14605 verifyFormat("#define A(x) x", SomeSpace); 14606 verifyFormat("#define A (x) x", SomeSpace); 14607 verifyFormat("#if defined(x)\n" 14608 "#endif", 14609 SomeSpace); 14610 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14611 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14612 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14613 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14614 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14615 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14616 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14617 verifyFormat("alignas (128) char a[128];", SomeSpace); 14618 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14619 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14620 SomeSpace); 14621 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14622 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14623 verifyFormat("T A::operator()();", SomeSpace); 14624 // FIXME these tests regressed behaviour. 14625 // verifyFormat("X A::operator++ (T);", SomeSpace); 14626 verifyFormat("int x = int (y);", SomeSpace); 14627 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14628 14629 FormatStyle SpaceControlStatements = getLLVMStyle(); 14630 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14631 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 14632 14633 verifyFormat("while (true)\n" 14634 " continue;", 14635 SpaceControlStatements); 14636 verifyFormat("if (true)\n" 14637 " f();\n" 14638 "else if (true)\n" 14639 " f();", 14640 SpaceControlStatements); 14641 verifyFormat("for (;;) {\n" 14642 " do_something();\n" 14643 "}", 14644 SpaceControlStatements); 14645 verifyFormat("do {\n" 14646 " do_something();\n" 14647 "} while (something());", 14648 SpaceControlStatements); 14649 verifyFormat("switch (x) {\n" 14650 "default:\n" 14651 " break;\n" 14652 "}", 14653 SpaceControlStatements); 14654 14655 FormatStyle SpaceFuncDecl = getLLVMStyle(); 14656 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14657 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 14658 14659 verifyFormat("int f ();", SpaceFuncDecl); 14660 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 14661 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 14662 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 14663 verifyFormat("#define A(x) x", SpaceFuncDecl); 14664 verifyFormat("#define A (x) x", SpaceFuncDecl); 14665 verifyFormat("#if defined(x)\n" 14666 "#endif", 14667 SpaceFuncDecl); 14668 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 14669 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 14670 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 14671 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 14672 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 14673 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 14674 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 14675 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 14676 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 14677 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14678 SpaceFuncDecl); 14679 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 14680 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 14681 // FIXME these tests regressed behaviour. 14682 // verifyFormat("T A::operator() ();", SpaceFuncDecl); 14683 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 14684 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 14685 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 14686 verifyFormat("int x = int(y);", SpaceFuncDecl); 14687 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14688 SpaceFuncDecl); 14689 14690 FormatStyle SpaceFuncDef = getLLVMStyle(); 14691 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14692 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 14693 14694 verifyFormat("int f();", SpaceFuncDef); 14695 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 14696 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 14697 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 14698 verifyFormat("#define A(x) x", SpaceFuncDef); 14699 verifyFormat("#define A (x) x", SpaceFuncDef); 14700 verifyFormat("#if defined(x)\n" 14701 "#endif", 14702 SpaceFuncDef); 14703 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 14704 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 14705 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 14706 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 14707 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 14708 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 14709 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 14710 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 14711 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 14712 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14713 SpaceFuncDef); 14714 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 14715 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 14716 verifyFormat("T A::operator()();", SpaceFuncDef); 14717 verifyFormat("X A::operator++(T);", SpaceFuncDef); 14718 // verifyFormat("T A::operator() () {}", SpaceFuncDef); 14719 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 14720 verifyFormat("int x = int(y);", SpaceFuncDef); 14721 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14722 SpaceFuncDef); 14723 14724 FormatStyle SpaceIfMacros = getLLVMStyle(); 14725 SpaceIfMacros.IfMacros.clear(); 14726 SpaceIfMacros.IfMacros.push_back("MYIF"); 14727 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14728 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 14729 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 14730 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 14731 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 14732 14733 FormatStyle SpaceForeachMacros = getLLVMStyle(); 14734 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine, 14735 FormatStyle::SBS_Never); 14736 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false); 14737 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14738 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 14739 verifyFormat("for (;;) {\n" 14740 "}", 14741 SpaceForeachMacros); 14742 verifyFormat("foreach (Item *item, itemlist) {\n" 14743 "}", 14744 SpaceForeachMacros); 14745 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n" 14746 "}", 14747 SpaceForeachMacros); 14748 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n" 14749 "}", 14750 SpaceForeachMacros); 14751 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 14752 14753 FormatStyle SomeSpace2 = getLLVMStyle(); 14754 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14755 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 14756 verifyFormat("[]() -> float {}", SomeSpace2); 14757 verifyFormat("[] (auto foo) {}", SomeSpace2); 14758 verifyFormat("[foo]() -> int {}", SomeSpace2); 14759 verifyFormat("int f();", SomeSpace2); 14760 verifyFormat("void f (int a, T b) {\n" 14761 " while (true)\n" 14762 " continue;\n" 14763 "}", 14764 SomeSpace2); 14765 verifyFormat("if (true)\n" 14766 " f();\n" 14767 "else if (true)\n" 14768 " f();", 14769 SomeSpace2); 14770 verifyFormat("do {\n" 14771 " do_something();\n" 14772 "} while (something());", 14773 SomeSpace2); 14774 verifyFormat("switch (x) {\n" 14775 "default:\n" 14776 " break;\n" 14777 "}", 14778 SomeSpace2); 14779 verifyFormat("A::A() : a (1) {}", SomeSpace2); 14780 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 14781 verifyFormat("*(&a + 1);\n" 14782 "&((&a)[1]);\n" 14783 "a[(b + c) * d];\n" 14784 "(((a + 1) * 2) + 3) * 4;", 14785 SomeSpace2); 14786 verifyFormat("#define A(x) x", SomeSpace2); 14787 verifyFormat("#define A (x) x", SomeSpace2); 14788 verifyFormat("#if defined(x)\n" 14789 "#endif", 14790 SomeSpace2); 14791 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 14792 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 14793 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 14794 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 14795 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 14796 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 14797 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 14798 verifyFormat("alignas (128) char a[128];", SomeSpace2); 14799 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 14800 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14801 SomeSpace2); 14802 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 14803 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 14804 verifyFormat("T A::operator()();", SomeSpace2); 14805 // verifyFormat("X A::operator++ (T);", SomeSpace2); 14806 verifyFormat("int x = int (y);", SomeSpace2); 14807 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 14808 14809 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle(); 14810 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14811 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 14812 .AfterOverloadedOperator = true; 14813 14814 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator); 14815 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator); 14816 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator); 14817 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 14818 14819 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 14820 .AfterOverloadedOperator = false; 14821 14822 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator); 14823 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator); 14824 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator); 14825 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 14826 } 14827 14828 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14829 FormatStyle Spaces = getLLVMStyle(); 14830 Spaces.SpaceAfterLogicalNot = true; 14831 14832 verifyFormat("bool x = ! y", Spaces); 14833 verifyFormat("if (! isFailure())", Spaces); 14834 verifyFormat("if (! (a && b))", Spaces); 14835 verifyFormat("\"Error!\"", Spaces); 14836 verifyFormat("! ! x", Spaces); 14837 } 14838 14839 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14840 FormatStyle Spaces = getLLVMStyle(); 14841 14842 Spaces.SpacesInParentheses = true; 14843 verifyFormat("do_something( ::globalVar );", Spaces); 14844 verifyFormat("call( x, y, z );", Spaces); 14845 verifyFormat("call();", Spaces); 14846 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14847 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14848 Spaces); 14849 verifyFormat("while ( (bool)1 )\n" 14850 " continue;", 14851 Spaces); 14852 verifyFormat("for ( ;; )\n" 14853 " continue;", 14854 Spaces); 14855 verifyFormat("if ( true )\n" 14856 " f();\n" 14857 "else if ( true )\n" 14858 " f();", 14859 Spaces); 14860 verifyFormat("do {\n" 14861 " do_something( (int)i );\n" 14862 "} while ( something() );", 14863 Spaces); 14864 verifyFormat("switch ( x ) {\n" 14865 "default:\n" 14866 " break;\n" 14867 "}", 14868 Spaces); 14869 14870 Spaces.SpacesInParentheses = false; 14871 Spaces.SpacesInCStyleCastParentheses = true; 14872 verifyFormat("Type *A = ( Type * )P;", Spaces); 14873 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14874 verifyFormat("x = ( int32 )y;", Spaces); 14875 verifyFormat("int a = ( int )(2.0f);", Spaces); 14876 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14877 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14878 verifyFormat("#define x (( int )-1)", Spaces); 14879 14880 // Run the first set of tests again with: 14881 Spaces.SpacesInParentheses = false; 14882 Spaces.SpaceInEmptyParentheses = true; 14883 Spaces.SpacesInCStyleCastParentheses = true; 14884 verifyFormat("call(x, y, z);", Spaces); 14885 verifyFormat("call( );", Spaces); 14886 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14887 verifyFormat("while (( bool )1)\n" 14888 " continue;", 14889 Spaces); 14890 verifyFormat("for (;;)\n" 14891 " continue;", 14892 Spaces); 14893 verifyFormat("if (true)\n" 14894 " f( );\n" 14895 "else if (true)\n" 14896 " f( );", 14897 Spaces); 14898 verifyFormat("do {\n" 14899 " do_something(( int )i);\n" 14900 "} while (something( ));", 14901 Spaces); 14902 verifyFormat("switch (x) {\n" 14903 "default:\n" 14904 " break;\n" 14905 "}", 14906 Spaces); 14907 14908 // Run the first set of tests again with: 14909 Spaces.SpaceAfterCStyleCast = true; 14910 verifyFormat("call(x, y, z);", Spaces); 14911 verifyFormat("call( );", Spaces); 14912 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14913 verifyFormat("while (( bool ) 1)\n" 14914 " continue;", 14915 Spaces); 14916 verifyFormat("for (;;)\n" 14917 " continue;", 14918 Spaces); 14919 verifyFormat("if (true)\n" 14920 " f( );\n" 14921 "else if (true)\n" 14922 " f( );", 14923 Spaces); 14924 verifyFormat("do {\n" 14925 " do_something(( int ) i);\n" 14926 "} while (something( ));", 14927 Spaces); 14928 verifyFormat("switch (x) {\n" 14929 "default:\n" 14930 " break;\n" 14931 "}", 14932 Spaces); 14933 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces); 14934 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces); 14935 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces); 14936 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); 14937 verifyFormat("bool *y = ( bool * ) (x);", Spaces); 14938 14939 // Run subset of tests again with: 14940 Spaces.SpacesInCStyleCastParentheses = false; 14941 Spaces.SpaceAfterCStyleCast = true; 14942 verifyFormat("while ((bool) 1)\n" 14943 " continue;", 14944 Spaces); 14945 verifyFormat("do {\n" 14946 " do_something((int) i);\n" 14947 "} while (something( ));", 14948 Spaces); 14949 14950 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14951 verifyFormat("size_t idx = (size_t) a;", Spaces); 14952 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14953 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14954 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14955 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14956 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14957 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces); 14958 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces); 14959 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces); 14960 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); 14961 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); 14962 Spaces.ColumnLimit = 80; 14963 Spaces.IndentWidth = 4; 14964 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14965 verifyFormat("void foo( ) {\n" 14966 " size_t foo = (*(function))(\n" 14967 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14968 "BarrrrrrrrrrrrLong,\n" 14969 " FoooooooooLooooong);\n" 14970 "}", 14971 Spaces); 14972 Spaces.SpaceAfterCStyleCast = false; 14973 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14974 verifyFormat("size_t idx = (size_t)a;", Spaces); 14975 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14976 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14977 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14978 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14979 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14980 14981 verifyFormat("void foo( ) {\n" 14982 " size_t foo = (*(function))(\n" 14983 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14984 "BarrrrrrrrrrrrLong,\n" 14985 " FoooooooooLooooong);\n" 14986 "}", 14987 Spaces); 14988 } 14989 14990 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14991 verifyFormat("int a[5];"); 14992 verifyFormat("a[3] += 42;"); 14993 14994 FormatStyle Spaces = getLLVMStyle(); 14995 Spaces.SpacesInSquareBrackets = true; 14996 // Not lambdas. 14997 verifyFormat("int a[ 5 ];", Spaces); 14998 verifyFormat("a[ 3 ] += 42;", Spaces); 14999 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 15000 verifyFormat("double &operator[](int i) { return 0; }\n" 15001 "int i;", 15002 Spaces); 15003 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 15004 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 15005 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 15006 // Lambdas. 15007 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 15008 verifyFormat("return [ i, args... ] {};", Spaces); 15009 verifyFormat("int foo = [ &bar ]() {};", Spaces); 15010 verifyFormat("int foo = [ = ]() {};", Spaces); 15011 verifyFormat("int foo = [ & ]() {};", Spaces); 15012 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 15013 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 15014 } 15015 15016 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 15017 FormatStyle NoSpaceStyle = getLLVMStyle(); 15018 verifyFormat("int a[5];", NoSpaceStyle); 15019 verifyFormat("a[3] += 42;", NoSpaceStyle); 15020 15021 verifyFormat("int a[1];", NoSpaceStyle); 15022 verifyFormat("int 1 [a];", NoSpaceStyle); 15023 verifyFormat("int a[1][2];", NoSpaceStyle); 15024 verifyFormat("a[7] = 5;", NoSpaceStyle); 15025 verifyFormat("int a = (f())[23];", NoSpaceStyle); 15026 verifyFormat("f([] {})", NoSpaceStyle); 15027 15028 FormatStyle Space = getLLVMStyle(); 15029 Space.SpaceBeforeSquareBrackets = true; 15030 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 15031 verifyFormat("return [i, args...] {};", Space); 15032 15033 verifyFormat("int a [5];", Space); 15034 verifyFormat("a [3] += 42;", Space); 15035 verifyFormat("constexpr char hello []{\"hello\"};", Space); 15036 verifyFormat("double &operator[](int i) { return 0; }\n" 15037 "int i;", 15038 Space); 15039 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 15040 verifyFormat("int i = a [a][a]->f();", Space); 15041 verifyFormat("int i = (*b) [a]->f();", Space); 15042 15043 verifyFormat("int a [1];", Space); 15044 verifyFormat("int 1 [a];", Space); 15045 verifyFormat("int a [1][2];", Space); 15046 verifyFormat("a [7] = 5;", Space); 15047 verifyFormat("int a = (f()) [23];", Space); 15048 verifyFormat("f([] {})", Space); 15049 } 15050 15051 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 15052 verifyFormat("int a = 5;"); 15053 verifyFormat("a += 42;"); 15054 verifyFormat("a or_eq 8;"); 15055 15056 FormatStyle Spaces = getLLVMStyle(); 15057 Spaces.SpaceBeforeAssignmentOperators = false; 15058 verifyFormat("int a= 5;", Spaces); 15059 verifyFormat("a+= 42;", Spaces); 15060 verifyFormat("a or_eq 8;", Spaces); 15061 } 15062 15063 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 15064 verifyFormat("class Foo : public Bar {};"); 15065 verifyFormat("Foo::Foo() : foo(1) {}"); 15066 verifyFormat("for (auto a : b) {\n}"); 15067 verifyFormat("int x = a ? b : c;"); 15068 verifyFormat("{\n" 15069 "label0:\n" 15070 " int x = 0;\n" 15071 "}"); 15072 verifyFormat("switch (x) {\n" 15073 "case 1:\n" 15074 "default:\n" 15075 "}"); 15076 verifyFormat("switch (allBraces) {\n" 15077 "case 1: {\n" 15078 " break;\n" 15079 "}\n" 15080 "case 2: {\n" 15081 " [[fallthrough]];\n" 15082 "}\n" 15083 "default: {\n" 15084 " break;\n" 15085 "}\n" 15086 "}"); 15087 15088 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 15089 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 15090 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 15091 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 15092 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 15093 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 15094 verifyFormat("{\n" 15095 "label1:\n" 15096 " int x = 0;\n" 15097 "}", 15098 CtorInitializerStyle); 15099 verifyFormat("switch (x) {\n" 15100 "case 1:\n" 15101 "default:\n" 15102 "}", 15103 CtorInitializerStyle); 15104 verifyFormat("switch (allBraces) {\n" 15105 "case 1: {\n" 15106 " break;\n" 15107 "}\n" 15108 "case 2: {\n" 15109 " [[fallthrough]];\n" 15110 "}\n" 15111 "default: {\n" 15112 " break;\n" 15113 "}\n" 15114 "}", 15115 CtorInitializerStyle); 15116 CtorInitializerStyle.BreakConstructorInitializers = 15117 FormatStyle::BCIS_AfterColon; 15118 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 15119 " aaaaaaaaaaaaaaaa(1),\n" 15120 " bbbbbbbbbbbbbbbb(2) {}", 15121 CtorInitializerStyle); 15122 CtorInitializerStyle.BreakConstructorInitializers = 15123 FormatStyle::BCIS_BeforeComma; 15124 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15125 " : aaaaaaaaaaaaaaaa(1)\n" 15126 " , bbbbbbbbbbbbbbbb(2) {}", 15127 CtorInitializerStyle); 15128 CtorInitializerStyle.BreakConstructorInitializers = 15129 FormatStyle::BCIS_BeforeColon; 15130 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15131 " : aaaaaaaaaaaaaaaa(1),\n" 15132 " bbbbbbbbbbbbbbbb(2) {}", 15133 CtorInitializerStyle); 15134 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 15135 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15136 ": aaaaaaaaaaaaaaaa(1),\n" 15137 " bbbbbbbbbbbbbbbb(2) {}", 15138 CtorInitializerStyle); 15139 15140 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 15141 InheritanceStyle.SpaceBeforeInheritanceColon = false; 15142 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 15143 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 15144 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 15145 verifyFormat("int x = a ? b : c;", InheritanceStyle); 15146 verifyFormat("{\n" 15147 "label2:\n" 15148 " int x = 0;\n" 15149 "}", 15150 InheritanceStyle); 15151 verifyFormat("switch (x) {\n" 15152 "case 1:\n" 15153 "default:\n" 15154 "}", 15155 InheritanceStyle); 15156 verifyFormat("switch (allBraces) {\n" 15157 "case 1: {\n" 15158 " break;\n" 15159 "}\n" 15160 "case 2: {\n" 15161 " [[fallthrough]];\n" 15162 "}\n" 15163 "default: {\n" 15164 " break;\n" 15165 "}\n" 15166 "}", 15167 InheritanceStyle); 15168 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 15169 verifyFormat("class Foooooooooooooooooooooo\n" 15170 " : public aaaaaaaaaaaaaaaaaa,\n" 15171 " public bbbbbbbbbbbbbbbbbb {\n" 15172 "}", 15173 InheritanceStyle); 15174 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 15175 verifyFormat("class Foooooooooooooooooooooo:\n" 15176 " public aaaaaaaaaaaaaaaaaa,\n" 15177 " public bbbbbbbbbbbbbbbbbb {\n" 15178 "}", 15179 InheritanceStyle); 15180 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 15181 verifyFormat("class Foooooooooooooooooooooo\n" 15182 " : public aaaaaaaaaaaaaaaaaa\n" 15183 " , public bbbbbbbbbbbbbbbbbb {\n" 15184 "}", 15185 InheritanceStyle); 15186 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 15187 verifyFormat("class Foooooooooooooooooooooo\n" 15188 " : public aaaaaaaaaaaaaaaaaa,\n" 15189 " public bbbbbbbbbbbbbbbbbb {\n" 15190 "}", 15191 InheritanceStyle); 15192 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 15193 verifyFormat("class Foooooooooooooooooooooo\n" 15194 ": public aaaaaaaaaaaaaaaaaa,\n" 15195 " public bbbbbbbbbbbbbbbbbb {}", 15196 InheritanceStyle); 15197 15198 FormatStyle ForLoopStyle = getLLVMStyle(); 15199 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 15200 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 15201 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 15202 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 15203 verifyFormat("int x = a ? b : c;", ForLoopStyle); 15204 verifyFormat("{\n" 15205 "label2:\n" 15206 " int x = 0;\n" 15207 "}", 15208 ForLoopStyle); 15209 verifyFormat("switch (x) {\n" 15210 "case 1:\n" 15211 "default:\n" 15212 "}", 15213 ForLoopStyle); 15214 verifyFormat("switch (allBraces) {\n" 15215 "case 1: {\n" 15216 " break;\n" 15217 "}\n" 15218 "case 2: {\n" 15219 " [[fallthrough]];\n" 15220 "}\n" 15221 "default: {\n" 15222 " break;\n" 15223 "}\n" 15224 "}", 15225 ForLoopStyle); 15226 15227 FormatStyle CaseStyle = getLLVMStyle(); 15228 CaseStyle.SpaceBeforeCaseColon = true; 15229 verifyFormat("class Foo : public Bar {};", CaseStyle); 15230 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 15231 verifyFormat("for (auto a : b) {\n}", CaseStyle); 15232 verifyFormat("int x = a ? b : c;", CaseStyle); 15233 verifyFormat("switch (x) {\n" 15234 "case 1 :\n" 15235 "default :\n" 15236 "}", 15237 CaseStyle); 15238 verifyFormat("switch (allBraces) {\n" 15239 "case 1 : {\n" 15240 " break;\n" 15241 "}\n" 15242 "case 2 : {\n" 15243 " [[fallthrough]];\n" 15244 "}\n" 15245 "default : {\n" 15246 " break;\n" 15247 "}\n" 15248 "}", 15249 CaseStyle); 15250 15251 FormatStyle NoSpaceStyle = getLLVMStyle(); 15252 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 15253 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15254 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 15255 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15256 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 15257 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 15258 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 15259 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 15260 verifyFormat("{\n" 15261 "label3:\n" 15262 " int x = 0;\n" 15263 "}", 15264 NoSpaceStyle); 15265 verifyFormat("switch (x) {\n" 15266 "case 1:\n" 15267 "default:\n" 15268 "}", 15269 NoSpaceStyle); 15270 verifyFormat("switch (allBraces) {\n" 15271 "case 1: {\n" 15272 " break;\n" 15273 "}\n" 15274 "case 2: {\n" 15275 " [[fallthrough]];\n" 15276 "}\n" 15277 "default: {\n" 15278 " break;\n" 15279 "}\n" 15280 "}", 15281 NoSpaceStyle); 15282 15283 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 15284 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 15285 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15286 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 15287 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15288 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 15289 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 15290 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 15291 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 15292 verifyFormat("{\n" 15293 "label3:\n" 15294 " int x = 0;\n" 15295 "}", 15296 InvertedSpaceStyle); 15297 verifyFormat("switch (x) {\n" 15298 "case 1 :\n" 15299 "case 2 : {\n" 15300 " break;\n" 15301 "}\n" 15302 "default :\n" 15303 " break;\n" 15304 "}", 15305 InvertedSpaceStyle); 15306 verifyFormat("switch (allBraces) {\n" 15307 "case 1 : {\n" 15308 " break;\n" 15309 "}\n" 15310 "case 2 : {\n" 15311 " [[fallthrough]];\n" 15312 "}\n" 15313 "default : {\n" 15314 " break;\n" 15315 "}\n" 15316 "}", 15317 InvertedSpaceStyle); 15318 } 15319 15320 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 15321 FormatStyle Style = getLLVMStyle(); 15322 15323 Style.PointerAlignment = FormatStyle::PAS_Left; 15324 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15325 verifyFormat("void* const* x = NULL;", Style); 15326 15327 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 15328 do { \ 15329 Style.PointerAlignment = FormatStyle::Pointers; \ 15330 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 15331 verifyFormat(Code, Style); \ 15332 } while (false) 15333 15334 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 15335 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 15336 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 15337 15338 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 15339 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 15340 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 15341 15342 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 15343 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 15344 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 15345 15346 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 15347 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 15348 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 15349 15350 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 15351 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15352 SAPQ_Default); 15353 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15354 SAPQ_Default); 15355 15356 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 15357 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15358 SAPQ_Before); 15359 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15360 SAPQ_Before); 15361 15362 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 15363 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 15364 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15365 SAPQ_After); 15366 15367 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 15368 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 15369 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 15370 15371 #undef verifyQualifierSpaces 15372 15373 FormatStyle Spaces = getLLVMStyle(); 15374 Spaces.AttributeMacros.push_back("qualified"); 15375 Spaces.PointerAlignment = FormatStyle::PAS_Right; 15376 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15377 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 15378 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 15379 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 15380 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 15381 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15382 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15383 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 15384 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 15385 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15386 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15387 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15388 15389 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 15390 Spaces.PointerAlignment = FormatStyle::PAS_Left; 15391 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15392 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 15393 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 15394 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 15395 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 15396 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15397 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 15398 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15399 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 15400 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 15401 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 15402 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 15403 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15404 15405 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 15406 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 15407 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15408 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 15409 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 15410 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15411 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15412 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15413 } 15414 15415 TEST_F(FormatTest, AlignConsecutiveMacros) { 15416 FormatStyle Style = getLLVMStyle(); 15417 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15418 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15419 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15420 15421 verifyFormat("#define a 3\n" 15422 "#define bbbb 4\n" 15423 "#define ccc (5)", 15424 Style); 15425 15426 verifyFormat("#define f(x) (x * x)\n" 15427 "#define fff(x, y, z) (x * y + z)\n" 15428 "#define ffff(x, y) (x - y)", 15429 Style); 15430 15431 verifyFormat("#define foo(x, y) (x + y)\n" 15432 "#define bar (5, 6)(2 + 2)", 15433 Style); 15434 15435 verifyFormat("#define a 3\n" 15436 "#define bbbb 4\n" 15437 "#define ccc (5)\n" 15438 "#define f(x) (x * x)\n" 15439 "#define fff(x, y, z) (x * y + z)\n" 15440 "#define ffff(x, y) (x - y)", 15441 Style); 15442 15443 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15444 verifyFormat("#define a 3\n" 15445 "#define bbbb 4\n" 15446 "#define ccc (5)", 15447 Style); 15448 15449 verifyFormat("#define f(x) (x * x)\n" 15450 "#define fff(x, y, z) (x * y + z)\n" 15451 "#define ffff(x, y) (x - y)", 15452 Style); 15453 15454 verifyFormat("#define foo(x, y) (x + y)\n" 15455 "#define bar (5, 6)(2 + 2)", 15456 Style); 15457 15458 verifyFormat("#define a 3\n" 15459 "#define bbbb 4\n" 15460 "#define ccc (5)\n" 15461 "#define f(x) (x * x)\n" 15462 "#define fff(x, y, z) (x * y + z)\n" 15463 "#define ffff(x, y) (x - y)", 15464 Style); 15465 15466 verifyFormat("#define a 5\n" 15467 "#define foo(x, y) (x + y)\n" 15468 "#define CCC (6)\n" 15469 "auto lambda = []() {\n" 15470 " auto ii = 0;\n" 15471 " float j = 0;\n" 15472 " return 0;\n" 15473 "};\n" 15474 "int i = 0;\n" 15475 "float i2 = 0;\n" 15476 "auto v = type{\n" 15477 " i = 1, //\n" 15478 " (i = 2), //\n" 15479 " i = 3 //\n" 15480 "};", 15481 Style); 15482 15483 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15484 Style.ColumnLimit = 20; 15485 15486 verifyFormat("#define a \\\n" 15487 " \"aabbbbbbbbbbbb\"\n" 15488 "#define D \\\n" 15489 " \"aabbbbbbbbbbbb\" \\\n" 15490 " \"ccddeeeeeeeee\"\n" 15491 "#define B \\\n" 15492 " \"QQQQQQQQQQQQQ\" \\\n" 15493 " \"FFFFFFFFFFFFF\" \\\n" 15494 " \"LLLLLLLL\"\n", 15495 Style); 15496 15497 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15498 verifyFormat("#define a \\\n" 15499 " \"aabbbbbbbbbbbb\"\n" 15500 "#define D \\\n" 15501 " \"aabbbbbbbbbbbb\" \\\n" 15502 " \"ccddeeeeeeeee\"\n" 15503 "#define B \\\n" 15504 " \"QQQQQQQQQQQQQ\" \\\n" 15505 " \"FFFFFFFFFFFFF\" \\\n" 15506 " \"LLLLLLLL\"\n", 15507 Style); 15508 15509 // Test across comments 15510 Style.MaxEmptyLinesToKeep = 10; 15511 Style.ReflowComments = false; 15512 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 15513 EXPECT_EQ("#define a 3\n" 15514 "// line comment\n" 15515 "#define bbbb 4\n" 15516 "#define ccc (5)", 15517 format("#define a 3\n" 15518 "// line comment\n" 15519 "#define bbbb 4\n" 15520 "#define ccc (5)", 15521 Style)); 15522 15523 EXPECT_EQ("#define a 3\n" 15524 "/* block comment */\n" 15525 "#define bbbb 4\n" 15526 "#define ccc (5)", 15527 format("#define a 3\n" 15528 "/* block comment */\n" 15529 "#define bbbb 4\n" 15530 "#define ccc (5)", 15531 Style)); 15532 15533 EXPECT_EQ("#define a 3\n" 15534 "/* multi-line *\n" 15535 " * block comment */\n" 15536 "#define bbbb 4\n" 15537 "#define ccc (5)", 15538 format("#define a 3\n" 15539 "/* multi-line *\n" 15540 " * block comment */\n" 15541 "#define bbbb 4\n" 15542 "#define ccc (5)", 15543 Style)); 15544 15545 EXPECT_EQ("#define a 3\n" 15546 "// multi-line line comment\n" 15547 "//\n" 15548 "#define bbbb 4\n" 15549 "#define ccc (5)", 15550 format("#define a 3\n" 15551 "// multi-line line comment\n" 15552 "//\n" 15553 "#define bbbb 4\n" 15554 "#define ccc (5)", 15555 Style)); 15556 15557 EXPECT_EQ("#define a 3\n" 15558 "// empty lines still break.\n" 15559 "\n" 15560 "#define bbbb 4\n" 15561 "#define ccc (5)", 15562 format("#define a 3\n" 15563 "// empty lines still break.\n" 15564 "\n" 15565 "#define bbbb 4\n" 15566 "#define ccc (5)", 15567 Style)); 15568 15569 // Test across empty lines 15570 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 15571 EXPECT_EQ("#define a 3\n" 15572 "\n" 15573 "#define bbbb 4\n" 15574 "#define ccc (5)", 15575 format("#define a 3\n" 15576 "\n" 15577 "#define bbbb 4\n" 15578 "#define ccc (5)", 15579 Style)); 15580 15581 EXPECT_EQ("#define a 3\n" 15582 "\n" 15583 "\n" 15584 "\n" 15585 "#define bbbb 4\n" 15586 "#define ccc (5)", 15587 format("#define a 3\n" 15588 "\n" 15589 "\n" 15590 "\n" 15591 "#define bbbb 4\n" 15592 "#define ccc (5)", 15593 Style)); 15594 15595 EXPECT_EQ("#define a 3\n" 15596 "// comments should break alignment\n" 15597 "//\n" 15598 "#define bbbb 4\n" 15599 "#define ccc (5)", 15600 format("#define a 3\n" 15601 "// comments should break alignment\n" 15602 "//\n" 15603 "#define bbbb 4\n" 15604 "#define ccc (5)", 15605 Style)); 15606 15607 // Test across empty lines and comments 15608 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 15609 verifyFormat("#define a 3\n" 15610 "\n" 15611 "// line comment\n" 15612 "#define bbbb 4\n" 15613 "#define ccc (5)", 15614 Style); 15615 15616 EXPECT_EQ("#define a 3\n" 15617 "\n" 15618 "\n" 15619 "/* multi-line *\n" 15620 " * block comment */\n" 15621 "\n" 15622 "\n" 15623 "#define bbbb 4\n" 15624 "#define ccc (5)", 15625 format("#define a 3\n" 15626 "\n" 15627 "\n" 15628 "/* multi-line *\n" 15629 " * block comment */\n" 15630 "\n" 15631 "\n" 15632 "#define bbbb 4\n" 15633 "#define ccc (5)", 15634 Style)); 15635 15636 EXPECT_EQ("#define a 3\n" 15637 "\n" 15638 "\n" 15639 "/* multi-line *\n" 15640 " * block comment */\n" 15641 "\n" 15642 "\n" 15643 "#define bbbb 4\n" 15644 "#define ccc (5)", 15645 format("#define a 3\n" 15646 "\n" 15647 "\n" 15648 "/* multi-line *\n" 15649 " * block comment */\n" 15650 "\n" 15651 "\n" 15652 "#define bbbb 4\n" 15653 "#define ccc (5)", 15654 Style)); 15655 } 15656 15657 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 15658 FormatStyle Alignment = getLLVMStyle(); 15659 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15660 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 15661 15662 Alignment.MaxEmptyLinesToKeep = 10; 15663 /* Test alignment across empty lines */ 15664 EXPECT_EQ("int a = 5;\n" 15665 "\n" 15666 "int oneTwoThree = 123;", 15667 format("int a = 5;\n" 15668 "\n" 15669 "int oneTwoThree= 123;", 15670 Alignment)); 15671 EXPECT_EQ("int a = 5;\n" 15672 "int one = 1;\n" 15673 "\n" 15674 "int oneTwoThree = 123;", 15675 format("int a = 5;\n" 15676 "int one = 1;\n" 15677 "\n" 15678 "int oneTwoThree = 123;", 15679 Alignment)); 15680 EXPECT_EQ("int a = 5;\n" 15681 "int one = 1;\n" 15682 "\n" 15683 "int oneTwoThree = 123;\n" 15684 "int oneTwo = 12;", 15685 format("int a = 5;\n" 15686 "int one = 1;\n" 15687 "\n" 15688 "int oneTwoThree = 123;\n" 15689 "int oneTwo = 12;", 15690 Alignment)); 15691 15692 /* Test across comments */ 15693 EXPECT_EQ("int a = 5;\n" 15694 "/* block comment */\n" 15695 "int oneTwoThree = 123;", 15696 format("int a = 5;\n" 15697 "/* block comment */\n" 15698 "int oneTwoThree=123;", 15699 Alignment)); 15700 15701 EXPECT_EQ("int a = 5;\n" 15702 "// line comment\n" 15703 "int oneTwoThree = 123;", 15704 format("int a = 5;\n" 15705 "// line comment\n" 15706 "int oneTwoThree=123;", 15707 Alignment)); 15708 15709 /* Test across comments and newlines */ 15710 EXPECT_EQ("int a = 5;\n" 15711 "\n" 15712 "/* block comment */\n" 15713 "int oneTwoThree = 123;", 15714 format("int a = 5;\n" 15715 "\n" 15716 "/* block comment */\n" 15717 "int oneTwoThree=123;", 15718 Alignment)); 15719 15720 EXPECT_EQ("int a = 5;\n" 15721 "\n" 15722 "// line comment\n" 15723 "int oneTwoThree = 123;", 15724 format("int a = 5;\n" 15725 "\n" 15726 "// line comment\n" 15727 "int oneTwoThree=123;", 15728 Alignment)); 15729 } 15730 15731 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15732 FormatStyle Alignment = getLLVMStyle(); 15733 Alignment.AlignConsecutiveDeclarations = 15734 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15735 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15736 15737 Alignment.MaxEmptyLinesToKeep = 10; 15738 /* Test alignment across empty lines */ 15739 EXPECT_EQ("int a = 5;\n" 15740 "\n" 15741 "float const oneTwoThree = 123;", 15742 format("int a = 5;\n" 15743 "\n" 15744 "float const oneTwoThree = 123;", 15745 Alignment)); 15746 EXPECT_EQ("int a = 5;\n" 15747 "float const one = 1;\n" 15748 "\n" 15749 "int oneTwoThree = 123;", 15750 format("int a = 5;\n" 15751 "float const one = 1;\n" 15752 "\n" 15753 "int oneTwoThree = 123;", 15754 Alignment)); 15755 15756 /* Test across comments */ 15757 EXPECT_EQ("float const a = 5;\n" 15758 "/* block comment */\n" 15759 "int oneTwoThree = 123;", 15760 format("float const a = 5;\n" 15761 "/* block comment */\n" 15762 "int oneTwoThree=123;", 15763 Alignment)); 15764 15765 EXPECT_EQ("float const a = 5;\n" 15766 "// line comment\n" 15767 "int oneTwoThree = 123;", 15768 format("float const a = 5;\n" 15769 "// line comment\n" 15770 "int oneTwoThree=123;", 15771 Alignment)); 15772 15773 /* Test across comments and newlines */ 15774 EXPECT_EQ("float const a = 5;\n" 15775 "\n" 15776 "/* block comment */\n" 15777 "int oneTwoThree = 123;", 15778 format("float const a = 5;\n" 15779 "\n" 15780 "/* block comment */\n" 15781 "int oneTwoThree=123;", 15782 Alignment)); 15783 15784 EXPECT_EQ("float const a = 5;\n" 15785 "\n" 15786 "// line comment\n" 15787 "int oneTwoThree = 123;", 15788 format("float const a = 5;\n" 15789 "\n" 15790 "// line comment\n" 15791 "int oneTwoThree=123;", 15792 Alignment)); 15793 } 15794 15795 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15796 FormatStyle Alignment = getLLVMStyle(); 15797 Alignment.AlignConsecutiveBitFields = 15798 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15799 15800 Alignment.MaxEmptyLinesToKeep = 10; 15801 /* Test alignment across empty lines */ 15802 EXPECT_EQ("int a : 5;\n" 15803 "\n" 15804 "int longbitfield : 6;", 15805 format("int a : 5;\n" 15806 "\n" 15807 "int longbitfield : 6;", 15808 Alignment)); 15809 EXPECT_EQ("int a : 5;\n" 15810 "int one : 1;\n" 15811 "\n" 15812 "int longbitfield : 6;", 15813 format("int a : 5;\n" 15814 "int one : 1;\n" 15815 "\n" 15816 "int longbitfield : 6;", 15817 Alignment)); 15818 15819 /* Test across comments */ 15820 EXPECT_EQ("int a : 5;\n" 15821 "/* block comment */\n" 15822 "int longbitfield : 6;", 15823 format("int a : 5;\n" 15824 "/* block comment */\n" 15825 "int longbitfield : 6;", 15826 Alignment)); 15827 EXPECT_EQ("int a : 5;\n" 15828 "int one : 1;\n" 15829 "// line comment\n" 15830 "int longbitfield : 6;", 15831 format("int a : 5;\n" 15832 "int one : 1;\n" 15833 "// line comment\n" 15834 "int longbitfield : 6;", 15835 Alignment)); 15836 15837 /* Test across comments and newlines */ 15838 EXPECT_EQ("int a : 5;\n" 15839 "/* block comment */\n" 15840 "\n" 15841 "int longbitfield : 6;", 15842 format("int a : 5;\n" 15843 "/* block comment */\n" 15844 "\n" 15845 "int longbitfield : 6;", 15846 Alignment)); 15847 EXPECT_EQ("int a : 5;\n" 15848 "int one : 1;\n" 15849 "\n" 15850 "// line comment\n" 15851 "\n" 15852 "int longbitfield : 6;", 15853 format("int a : 5;\n" 15854 "int one : 1;\n" 15855 "\n" 15856 "// line comment \n" 15857 "\n" 15858 "int longbitfield : 6;", 15859 Alignment)); 15860 } 15861 15862 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15863 FormatStyle Alignment = getLLVMStyle(); 15864 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15865 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15866 15867 Alignment.MaxEmptyLinesToKeep = 10; 15868 /* Test alignment across empty lines */ 15869 EXPECT_EQ("int a = 5;\n" 15870 "\n" 15871 "int oneTwoThree = 123;", 15872 format("int a = 5;\n" 15873 "\n" 15874 "int oneTwoThree= 123;", 15875 Alignment)); 15876 EXPECT_EQ("int a = 5;\n" 15877 "int one = 1;\n" 15878 "\n" 15879 "int oneTwoThree = 123;", 15880 format("int a = 5;\n" 15881 "int one = 1;\n" 15882 "\n" 15883 "int oneTwoThree = 123;", 15884 Alignment)); 15885 15886 /* Test across comments */ 15887 EXPECT_EQ("int a = 5;\n" 15888 "/* block comment */\n" 15889 "int oneTwoThree = 123;", 15890 format("int a = 5;\n" 15891 "/* block comment */\n" 15892 "int oneTwoThree=123;", 15893 Alignment)); 15894 15895 EXPECT_EQ("int a = 5;\n" 15896 "// line comment\n" 15897 "int oneTwoThree = 123;", 15898 format("int a = 5;\n" 15899 "// line comment\n" 15900 "int oneTwoThree=123;", 15901 Alignment)); 15902 15903 EXPECT_EQ("int a = 5;\n" 15904 "/*\n" 15905 " * multi-line block comment\n" 15906 " */\n" 15907 "int oneTwoThree = 123;", 15908 format("int a = 5;\n" 15909 "/*\n" 15910 " * multi-line block comment\n" 15911 " */\n" 15912 "int oneTwoThree=123;", 15913 Alignment)); 15914 15915 EXPECT_EQ("int a = 5;\n" 15916 "//\n" 15917 "// multi-line line comment\n" 15918 "//\n" 15919 "int oneTwoThree = 123;", 15920 format("int a = 5;\n" 15921 "//\n" 15922 "// multi-line line comment\n" 15923 "//\n" 15924 "int oneTwoThree=123;", 15925 Alignment)); 15926 15927 /* Test across comments and newlines */ 15928 EXPECT_EQ("int a = 5;\n" 15929 "\n" 15930 "/* block comment */\n" 15931 "int oneTwoThree = 123;", 15932 format("int a = 5;\n" 15933 "\n" 15934 "/* block comment */\n" 15935 "int oneTwoThree=123;", 15936 Alignment)); 15937 15938 EXPECT_EQ("int a = 5;\n" 15939 "\n" 15940 "// line comment\n" 15941 "int oneTwoThree = 123;", 15942 format("int a = 5;\n" 15943 "\n" 15944 "// line comment\n" 15945 "int oneTwoThree=123;", 15946 Alignment)); 15947 } 15948 15949 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15950 FormatStyle Alignment = getLLVMStyle(); 15951 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15952 Alignment.AlignConsecutiveAssignments = 15953 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15954 verifyFormat("int a = 5;\n" 15955 "int oneTwoThree = 123;", 15956 Alignment); 15957 verifyFormat("int a = method();\n" 15958 "int oneTwoThree = 133;", 15959 Alignment); 15960 verifyFormat("a &= 5;\n" 15961 "bcd *= 5;\n" 15962 "ghtyf += 5;\n" 15963 "dvfvdb -= 5;\n" 15964 "a /= 5;\n" 15965 "vdsvsv %= 5;\n" 15966 "sfdbddfbdfbb ^= 5;\n" 15967 "dvsdsv |= 5;\n" 15968 "int dsvvdvsdvvv = 123;", 15969 Alignment); 15970 verifyFormat("int i = 1, j = 10;\n" 15971 "something = 2000;", 15972 Alignment); 15973 verifyFormat("something = 2000;\n" 15974 "int i = 1, j = 10;\n", 15975 Alignment); 15976 verifyFormat("something = 2000;\n" 15977 "another = 911;\n" 15978 "int i = 1, j = 10;\n" 15979 "oneMore = 1;\n" 15980 "i = 2;", 15981 Alignment); 15982 verifyFormat("int a = 5;\n" 15983 "int one = 1;\n" 15984 "method();\n" 15985 "int oneTwoThree = 123;\n" 15986 "int oneTwo = 12;", 15987 Alignment); 15988 verifyFormat("int oneTwoThree = 123;\n" 15989 "int oneTwo = 12;\n" 15990 "method();\n", 15991 Alignment); 15992 verifyFormat("int oneTwoThree = 123; // comment\n" 15993 "int oneTwo = 12; // comment", 15994 Alignment); 15995 15996 // Bug 25167 15997 /* Uncomment when fixed 15998 verifyFormat("#if A\n" 15999 "#else\n" 16000 "int aaaaaaaa = 12;\n" 16001 "#endif\n" 16002 "#if B\n" 16003 "#else\n" 16004 "int a = 12;\n" 16005 "#endif\n", 16006 Alignment); 16007 verifyFormat("enum foo {\n" 16008 "#if A\n" 16009 "#else\n" 16010 " aaaaaaaa = 12;\n" 16011 "#endif\n" 16012 "#if B\n" 16013 "#else\n" 16014 " a = 12;\n" 16015 "#endif\n" 16016 "};\n", 16017 Alignment); 16018 */ 16019 16020 Alignment.MaxEmptyLinesToKeep = 10; 16021 /* Test alignment across empty lines */ 16022 EXPECT_EQ("int a = 5;\n" 16023 "\n" 16024 "int oneTwoThree = 123;", 16025 format("int a = 5;\n" 16026 "\n" 16027 "int oneTwoThree= 123;", 16028 Alignment)); 16029 EXPECT_EQ("int a = 5;\n" 16030 "int one = 1;\n" 16031 "\n" 16032 "int oneTwoThree = 123;", 16033 format("int a = 5;\n" 16034 "int one = 1;\n" 16035 "\n" 16036 "int oneTwoThree = 123;", 16037 Alignment)); 16038 EXPECT_EQ("int a = 5;\n" 16039 "int one = 1;\n" 16040 "\n" 16041 "int oneTwoThree = 123;\n" 16042 "int oneTwo = 12;", 16043 format("int a = 5;\n" 16044 "int one = 1;\n" 16045 "\n" 16046 "int oneTwoThree = 123;\n" 16047 "int oneTwo = 12;", 16048 Alignment)); 16049 16050 /* Test across comments */ 16051 EXPECT_EQ("int a = 5;\n" 16052 "/* block comment */\n" 16053 "int oneTwoThree = 123;", 16054 format("int a = 5;\n" 16055 "/* block comment */\n" 16056 "int oneTwoThree=123;", 16057 Alignment)); 16058 16059 EXPECT_EQ("int a = 5;\n" 16060 "// line comment\n" 16061 "int oneTwoThree = 123;", 16062 format("int a = 5;\n" 16063 "// line comment\n" 16064 "int oneTwoThree=123;", 16065 Alignment)); 16066 16067 /* Test across comments and newlines */ 16068 EXPECT_EQ("int a = 5;\n" 16069 "\n" 16070 "/* block comment */\n" 16071 "int oneTwoThree = 123;", 16072 format("int a = 5;\n" 16073 "\n" 16074 "/* block comment */\n" 16075 "int oneTwoThree=123;", 16076 Alignment)); 16077 16078 EXPECT_EQ("int a = 5;\n" 16079 "\n" 16080 "// line comment\n" 16081 "int oneTwoThree = 123;", 16082 format("int a = 5;\n" 16083 "\n" 16084 "// line comment\n" 16085 "int oneTwoThree=123;", 16086 Alignment)); 16087 16088 EXPECT_EQ("int a = 5;\n" 16089 "//\n" 16090 "// multi-line line comment\n" 16091 "//\n" 16092 "int oneTwoThree = 123;", 16093 format("int a = 5;\n" 16094 "//\n" 16095 "// multi-line line comment\n" 16096 "//\n" 16097 "int oneTwoThree=123;", 16098 Alignment)); 16099 16100 EXPECT_EQ("int a = 5;\n" 16101 "/*\n" 16102 " * multi-line block comment\n" 16103 " */\n" 16104 "int oneTwoThree = 123;", 16105 format("int a = 5;\n" 16106 "/*\n" 16107 " * multi-line block comment\n" 16108 " */\n" 16109 "int oneTwoThree=123;", 16110 Alignment)); 16111 16112 EXPECT_EQ("int a = 5;\n" 16113 "\n" 16114 "/* block comment */\n" 16115 "\n" 16116 "\n" 16117 "\n" 16118 "int oneTwoThree = 123;", 16119 format("int a = 5;\n" 16120 "\n" 16121 "/* block comment */\n" 16122 "\n" 16123 "\n" 16124 "\n" 16125 "int oneTwoThree=123;", 16126 Alignment)); 16127 16128 EXPECT_EQ("int a = 5;\n" 16129 "\n" 16130 "// line comment\n" 16131 "\n" 16132 "\n" 16133 "\n" 16134 "int oneTwoThree = 123;", 16135 format("int a = 5;\n" 16136 "\n" 16137 "// line comment\n" 16138 "\n" 16139 "\n" 16140 "\n" 16141 "int oneTwoThree=123;", 16142 Alignment)); 16143 16144 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16145 verifyFormat("#define A \\\n" 16146 " int aaaa = 12; \\\n" 16147 " int b = 23; \\\n" 16148 " int ccc = 234; \\\n" 16149 " int dddddddddd = 2345;", 16150 Alignment); 16151 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16152 verifyFormat("#define A \\\n" 16153 " int aaaa = 12; \\\n" 16154 " int b = 23; \\\n" 16155 " int ccc = 234; \\\n" 16156 " int dddddddddd = 2345;", 16157 Alignment); 16158 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16159 verifyFormat("#define A " 16160 " \\\n" 16161 " int aaaa = 12; " 16162 " \\\n" 16163 " int b = 23; " 16164 " \\\n" 16165 " int ccc = 234; " 16166 " \\\n" 16167 " int dddddddddd = 2345;", 16168 Alignment); 16169 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16170 "k = 4, int l = 5,\n" 16171 " int m = 6) {\n" 16172 " int j = 10;\n" 16173 " otherThing = 1;\n" 16174 "}", 16175 Alignment); 16176 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16177 " int i = 1;\n" 16178 " int j = 2;\n" 16179 " int big = 10000;\n" 16180 "}", 16181 Alignment); 16182 verifyFormat("class C {\n" 16183 "public:\n" 16184 " int i = 1;\n" 16185 " virtual void f() = 0;\n" 16186 "};", 16187 Alignment); 16188 verifyFormat("int i = 1;\n" 16189 "if (SomeType t = getSomething()) {\n" 16190 "}\n" 16191 "int j = 2;\n" 16192 "int big = 10000;", 16193 Alignment); 16194 verifyFormat("int j = 7;\n" 16195 "for (int k = 0; k < N; ++k) {\n" 16196 "}\n" 16197 "int j = 2;\n" 16198 "int big = 10000;\n" 16199 "}", 16200 Alignment); 16201 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16202 verifyFormat("int i = 1;\n" 16203 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16204 " = someLooooooooooooooooongFunction();\n" 16205 "int j = 2;", 16206 Alignment); 16207 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16208 verifyFormat("int i = 1;\n" 16209 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16210 " someLooooooooooooooooongFunction();\n" 16211 "int j = 2;", 16212 Alignment); 16213 16214 verifyFormat("auto lambda = []() {\n" 16215 " auto i = 0;\n" 16216 " return 0;\n" 16217 "};\n" 16218 "int i = 0;\n" 16219 "auto v = type{\n" 16220 " i = 1, //\n" 16221 " (i = 2), //\n" 16222 " i = 3 //\n" 16223 "};", 16224 Alignment); 16225 16226 verifyFormat( 16227 "int i = 1;\n" 16228 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16229 " loooooooooooooooooooooongParameterB);\n" 16230 "int j = 2;", 16231 Alignment); 16232 16233 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16234 " typename B = very_long_type_name_1,\n" 16235 " typename T_2 = very_long_type_name_2>\n" 16236 "auto foo() {}\n", 16237 Alignment); 16238 verifyFormat("int a, b = 1;\n" 16239 "int c = 2;\n" 16240 "int dd = 3;\n", 16241 Alignment); 16242 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16243 "float b[1][] = {{3.f}};\n", 16244 Alignment); 16245 verifyFormat("for (int i = 0; i < 1; i++)\n" 16246 " int x = 1;\n", 16247 Alignment); 16248 verifyFormat("for (i = 0; i < 1; i++)\n" 16249 " x = 1;\n" 16250 "y = 1;\n", 16251 Alignment); 16252 16253 Alignment.ReflowComments = true; 16254 Alignment.ColumnLimit = 50; 16255 EXPECT_EQ("int x = 0;\n" 16256 "int yy = 1; /// specificlennospace\n" 16257 "int zzz = 2;\n", 16258 format("int x = 0;\n" 16259 "int yy = 1; ///specificlennospace\n" 16260 "int zzz = 2;\n", 16261 Alignment)); 16262 } 16263 16264 TEST_F(FormatTest, AlignConsecutiveAssignments) { 16265 FormatStyle Alignment = getLLVMStyle(); 16266 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16267 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16268 verifyFormat("int a = 5;\n" 16269 "int oneTwoThree = 123;", 16270 Alignment); 16271 verifyFormat("int a = 5;\n" 16272 "int oneTwoThree = 123;", 16273 Alignment); 16274 16275 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16276 verifyFormat("int a = 5;\n" 16277 "int oneTwoThree = 123;", 16278 Alignment); 16279 verifyFormat("int a = method();\n" 16280 "int oneTwoThree = 133;", 16281 Alignment); 16282 verifyFormat("a &= 5;\n" 16283 "bcd *= 5;\n" 16284 "ghtyf += 5;\n" 16285 "dvfvdb -= 5;\n" 16286 "a /= 5;\n" 16287 "vdsvsv %= 5;\n" 16288 "sfdbddfbdfbb ^= 5;\n" 16289 "dvsdsv |= 5;\n" 16290 "int dsvvdvsdvvv = 123;", 16291 Alignment); 16292 verifyFormat("int i = 1, j = 10;\n" 16293 "something = 2000;", 16294 Alignment); 16295 verifyFormat("something = 2000;\n" 16296 "int i = 1, j = 10;\n", 16297 Alignment); 16298 verifyFormat("something = 2000;\n" 16299 "another = 911;\n" 16300 "int i = 1, j = 10;\n" 16301 "oneMore = 1;\n" 16302 "i = 2;", 16303 Alignment); 16304 verifyFormat("int a = 5;\n" 16305 "int one = 1;\n" 16306 "method();\n" 16307 "int oneTwoThree = 123;\n" 16308 "int oneTwo = 12;", 16309 Alignment); 16310 verifyFormat("int oneTwoThree = 123;\n" 16311 "int oneTwo = 12;\n" 16312 "method();\n", 16313 Alignment); 16314 verifyFormat("int oneTwoThree = 123; // comment\n" 16315 "int oneTwo = 12; // comment", 16316 Alignment); 16317 verifyFormat("int f() = default;\n" 16318 "int &operator() = default;\n" 16319 "int &operator=() {", 16320 Alignment); 16321 verifyFormat("int f() = delete;\n" 16322 "int &operator() = delete;\n" 16323 "int &operator=() {", 16324 Alignment); 16325 verifyFormat("int f() = default; // comment\n" 16326 "int &operator() = default; // comment\n" 16327 "int &operator=() {", 16328 Alignment); 16329 verifyFormat("int f() = default;\n" 16330 "int &operator() = default;\n" 16331 "int &operator==() {", 16332 Alignment); 16333 verifyFormat("int f() = default;\n" 16334 "int &operator() = default;\n" 16335 "int &operator<=() {", 16336 Alignment); 16337 verifyFormat("int f() = default;\n" 16338 "int &operator() = default;\n" 16339 "int &operator!=() {", 16340 Alignment); 16341 verifyFormat("int f() = default;\n" 16342 "int &operator() = default;\n" 16343 "int &operator=();", 16344 Alignment); 16345 verifyFormat("int f() = delete;\n" 16346 "int &operator() = delete;\n" 16347 "int &operator=();", 16348 Alignment); 16349 verifyFormat("/* long long padding */ int f() = default;\n" 16350 "int &operator() = default;\n" 16351 "int &operator/**/ =();", 16352 Alignment); 16353 // https://llvm.org/PR33697 16354 FormatStyle AlignmentWithPenalty = getLLVMStyle(); 16355 AlignmentWithPenalty.AlignConsecutiveAssignments = 16356 FormatStyle::ACS_Consecutive; 16357 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000; 16358 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n" 16359 " void f() = delete;\n" 16360 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n" 16361 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n" 16362 "};\n", 16363 AlignmentWithPenalty); 16364 16365 // Bug 25167 16366 /* Uncomment when fixed 16367 verifyFormat("#if A\n" 16368 "#else\n" 16369 "int aaaaaaaa = 12;\n" 16370 "#endif\n" 16371 "#if B\n" 16372 "#else\n" 16373 "int a = 12;\n" 16374 "#endif\n", 16375 Alignment); 16376 verifyFormat("enum foo {\n" 16377 "#if A\n" 16378 "#else\n" 16379 " aaaaaaaa = 12;\n" 16380 "#endif\n" 16381 "#if B\n" 16382 "#else\n" 16383 " a = 12;\n" 16384 "#endif\n" 16385 "};\n", 16386 Alignment); 16387 */ 16388 16389 EXPECT_EQ("int a = 5;\n" 16390 "\n" 16391 "int oneTwoThree = 123;", 16392 format("int a = 5;\n" 16393 "\n" 16394 "int oneTwoThree= 123;", 16395 Alignment)); 16396 EXPECT_EQ("int a = 5;\n" 16397 "int one = 1;\n" 16398 "\n" 16399 "int oneTwoThree = 123;", 16400 format("int a = 5;\n" 16401 "int one = 1;\n" 16402 "\n" 16403 "int oneTwoThree = 123;", 16404 Alignment)); 16405 EXPECT_EQ("int a = 5;\n" 16406 "int one = 1;\n" 16407 "\n" 16408 "int oneTwoThree = 123;\n" 16409 "int oneTwo = 12;", 16410 format("int a = 5;\n" 16411 "int one = 1;\n" 16412 "\n" 16413 "int oneTwoThree = 123;\n" 16414 "int oneTwo = 12;", 16415 Alignment)); 16416 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16417 verifyFormat("#define A \\\n" 16418 " int aaaa = 12; \\\n" 16419 " int b = 23; \\\n" 16420 " int ccc = 234; \\\n" 16421 " int dddddddddd = 2345;", 16422 Alignment); 16423 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16424 verifyFormat("#define A \\\n" 16425 " int aaaa = 12; \\\n" 16426 " int b = 23; \\\n" 16427 " int ccc = 234; \\\n" 16428 " int dddddddddd = 2345;", 16429 Alignment); 16430 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16431 verifyFormat("#define A " 16432 " \\\n" 16433 " int aaaa = 12; " 16434 " \\\n" 16435 " int b = 23; " 16436 " \\\n" 16437 " int ccc = 234; " 16438 " \\\n" 16439 " int dddddddddd = 2345;", 16440 Alignment); 16441 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16442 "k = 4, int l = 5,\n" 16443 " int m = 6) {\n" 16444 " int j = 10;\n" 16445 " otherThing = 1;\n" 16446 "}", 16447 Alignment); 16448 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16449 " int i = 1;\n" 16450 " int j = 2;\n" 16451 " int big = 10000;\n" 16452 "}", 16453 Alignment); 16454 verifyFormat("class C {\n" 16455 "public:\n" 16456 " int i = 1;\n" 16457 " virtual void f() = 0;\n" 16458 "};", 16459 Alignment); 16460 verifyFormat("int i = 1;\n" 16461 "if (SomeType t = getSomething()) {\n" 16462 "}\n" 16463 "int j = 2;\n" 16464 "int big = 10000;", 16465 Alignment); 16466 verifyFormat("int j = 7;\n" 16467 "for (int k = 0; k < N; ++k) {\n" 16468 "}\n" 16469 "int j = 2;\n" 16470 "int big = 10000;\n" 16471 "}", 16472 Alignment); 16473 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16474 verifyFormat("int i = 1;\n" 16475 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16476 " = someLooooooooooooooooongFunction();\n" 16477 "int j = 2;", 16478 Alignment); 16479 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16480 verifyFormat("int i = 1;\n" 16481 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16482 " someLooooooooooooooooongFunction();\n" 16483 "int j = 2;", 16484 Alignment); 16485 16486 verifyFormat("auto lambda = []() {\n" 16487 " auto i = 0;\n" 16488 " return 0;\n" 16489 "};\n" 16490 "int i = 0;\n" 16491 "auto v = type{\n" 16492 " i = 1, //\n" 16493 " (i = 2), //\n" 16494 " i = 3 //\n" 16495 "};", 16496 Alignment); 16497 16498 verifyFormat( 16499 "int i = 1;\n" 16500 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16501 " loooooooooooooooooooooongParameterB);\n" 16502 "int j = 2;", 16503 Alignment); 16504 16505 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16506 " typename B = very_long_type_name_1,\n" 16507 " typename T_2 = very_long_type_name_2>\n" 16508 "auto foo() {}\n", 16509 Alignment); 16510 verifyFormat("int a, b = 1;\n" 16511 "int c = 2;\n" 16512 "int dd = 3;\n", 16513 Alignment); 16514 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16515 "float b[1][] = {{3.f}};\n", 16516 Alignment); 16517 verifyFormat("for (int i = 0; i < 1; i++)\n" 16518 " int x = 1;\n", 16519 Alignment); 16520 verifyFormat("for (i = 0; i < 1; i++)\n" 16521 " x = 1;\n" 16522 "y = 1;\n", 16523 Alignment); 16524 16525 Alignment.ReflowComments = true; 16526 Alignment.ColumnLimit = 50; 16527 EXPECT_EQ("int x = 0;\n" 16528 "int yy = 1; /// specificlennospace\n" 16529 "int zzz = 2;\n", 16530 format("int x = 0;\n" 16531 "int yy = 1; ///specificlennospace\n" 16532 "int zzz = 2;\n", 16533 Alignment)); 16534 } 16535 16536 TEST_F(FormatTest, AlignConsecutiveBitFields) { 16537 FormatStyle Alignment = getLLVMStyle(); 16538 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 16539 verifyFormat("int const a : 5;\n" 16540 "int oneTwoThree : 23;", 16541 Alignment); 16542 16543 // Initializers are allowed starting with c++2a 16544 verifyFormat("int const a : 5 = 1;\n" 16545 "int oneTwoThree : 23 = 0;", 16546 Alignment); 16547 16548 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16549 verifyFormat("int const a : 5;\n" 16550 "int oneTwoThree : 23;", 16551 Alignment); 16552 16553 verifyFormat("int const a : 5; // comment\n" 16554 "int oneTwoThree : 23; // comment", 16555 Alignment); 16556 16557 verifyFormat("int const a : 5 = 1;\n" 16558 "int oneTwoThree : 23 = 0;", 16559 Alignment); 16560 16561 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16562 verifyFormat("int const a : 5 = 1;\n" 16563 "int oneTwoThree : 23 = 0;", 16564 Alignment); 16565 verifyFormat("int const a : 5 = {1};\n" 16566 "int oneTwoThree : 23 = 0;", 16567 Alignment); 16568 16569 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 16570 verifyFormat("int const a :5;\n" 16571 "int oneTwoThree:23;", 16572 Alignment); 16573 16574 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 16575 verifyFormat("int const a :5;\n" 16576 "int oneTwoThree :23;", 16577 Alignment); 16578 16579 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 16580 verifyFormat("int const a : 5;\n" 16581 "int oneTwoThree: 23;", 16582 Alignment); 16583 16584 // Known limitations: ':' is only recognized as a bitfield colon when 16585 // followed by a number. 16586 /* 16587 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 16588 "int a : 5;", 16589 Alignment); 16590 */ 16591 } 16592 16593 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 16594 FormatStyle Alignment = getLLVMStyle(); 16595 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16596 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16597 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16598 verifyFormat("float const a = 5;\n" 16599 "int oneTwoThree = 123;", 16600 Alignment); 16601 verifyFormat("int a = 5;\n" 16602 "float const oneTwoThree = 123;", 16603 Alignment); 16604 16605 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16606 verifyFormat("float const a = 5;\n" 16607 "int oneTwoThree = 123;", 16608 Alignment); 16609 verifyFormat("int a = method();\n" 16610 "float const oneTwoThree = 133;", 16611 Alignment); 16612 verifyFormat("int i = 1, j = 10;\n" 16613 "something = 2000;", 16614 Alignment); 16615 verifyFormat("something = 2000;\n" 16616 "int i = 1, j = 10;\n", 16617 Alignment); 16618 verifyFormat("float something = 2000;\n" 16619 "double another = 911;\n" 16620 "int i = 1, j = 10;\n" 16621 "const int *oneMore = 1;\n" 16622 "unsigned i = 2;", 16623 Alignment); 16624 verifyFormat("float a = 5;\n" 16625 "int one = 1;\n" 16626 "method();\n" 16627 "const double oneTwoThree = 123;\n" 16628 "const unsigned int oneTwo = 12;", 16629 Alignment); 16630 verifyFormat("int oneTwoThree{0}; // comment\n" 16631 "unsigned oneTwo; // comment", 16632 Alignment); 16633 verifyFormat("unsigned int *a;\n" 16634 "int *b;\n" 16635 "unsigned int Const *c;\n" 16636 "unsigned int const *d;\n" 16637 "unsigned int Const &e;\n" 16638 "unsigned int const &f;", 16639 Alignment); 16640 verifyFormat("Const unsigned int *c;\n" 16641 "const unsigned int *d;\n" 16642 "Const unsigned int &e;\n" 16643 "const unsigned int &f;\n" 16644 "const unsigned g;\n" 16645 "Const unsigned h;", 16646 Alignment); 16647 EXPECT_EQ("float const a = 5;\n" 16648 "\n" 16649 "int oneTwoThree = 123;", 16650 format("float const a = 5;\n" 16651 "\n" 16652 "int oneTwoThree= 123;", 16653 Alignment)); 16654 EXPECT_EQ("float a = 5;\n" 16655 "int one = 1;\n" 16656 "\n" 16657 "unsigned oneTwoThree = 123;", 16658 format("float a = 5;\n" 16659 "int one = 1;\n" 16660 "\n" 16661 "unsigned oneTwoThree = 123;", 16662 Alignment)); 16663 EXPECT_EQ("float a = 5;\n" 16664 "int one = 1;\n" 16665 "\n" 16666 "unsigned oneTwoThree = 123;\n" 16667 "int oneTwo = 12;", 16668 format("float a = 5;\n" 16669 "int one = 1;\n" 16670 "\n" 16671 "unsigned oneTwoThree = 123;\n" 16672 "int oneTwo = 12;", 16673 Alignment)); 16674 // Function prototype alignment 16675 verifyFormat("int a();\n" 16676 "double b();", 16677 Alignment); 16678 verifyFormat("int a(int x);\n" 16679 "double b();", 16680 Alignment); 16681 unsigned OldColumnLimit = Alignment.ColumnLimit; 16682 // We need to set ColumnLimit to zero, in order to stress nested alignments, 16683 // otherwise the function parameters will be re-flowed onto a single line. 16684 Alignment.ColumnLimit = 0; 16685 EXPECT_EQ("int a(int x,\n" 16686 " float y);\n" 16687 "double b(int x,\n" 16688 " double y);", 16689 format("int a(int x,\n" 16690 " float y);\n" 16691 "double b(int x,\n" 16692 " double y);", 16693 Alignment)); 16694 // This ensures that function parameters of function declarations are 16695 // correctly indented when their owning functions are indented. 16696 // The failure case here is for 'double y' to not be indented enough. 16697 EXPECT_EQ("double a(int x);\n" 16698 "int b(int y,\n" 16699 " double z);", 16700 format("double a(int x);\n" 16701 "int b(int y,\n" 16702 " double z);", 16703 Alignment)); 16704 // Set ColumnLimit low so that we induce wrapping immediately after 16705 // the function name and opening paren. 16706 Alignment.ColumnLimit = 13; 16707 verifyFormat("int function(\n" 16708 " int x,\n" 16709 " bool y);", 16710 Alignment); 16711 Alignment.ColumnLimit = OldColumnLimit; 16712 // Ensure function pointers don't screw up recursive alignment 16713 verifyFormat("int a(int x, void (*fp)(int y));\n" 16714 "double b();", 16715 Alignment); 16716 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16717 // Ensure recursive alignment is broken by function braces, so that the 16718 // "a = 1" does not align with subsequent assignments inside the function 16719 // body. 16720 verifyFormat("int func(int a = 1) {\n" 16721 " int b = 2;\n" 16722 " int cc = 3;\n" 16723 "}", 16724 Alignment); 16725 verifyFormat("float something = 2000;\n" 16726 "double another = 911;\n" 16727 "int i = 1, j = 10;\n" 16728 "const int *oneMore = 1;\n" 16729 "unsigned i = 2;", 16730 Alignment); 16731 verifyFormat("int oneTwoThree = {0}; // comment\n" 16732 "unsigned oneTwo = 0; // comment", 16733 Alignment); 16734 // Make sure that scope is correctly tracked, in the absence of braces 16735 verifyFormat("for (int i = 0; i < n; i++)\n" 16736 " j = i;\n" 16737 "double x = 1;\n", 16738 Alignment); 16739 verifyFormat("if (int i = 0)\n" 16740 " j = i;\n" 16741 "double x = 1;\n", 16742 Alignment); 16743 // Ensure operator[] and operator() are comprehended 16744 verifyFormat("struct test {\n" 16745 " long long int foo();\n" 16746 " int operator[](int a);\n" 16747 " double bar();\n" 16748 "};\n", 16749 Alignment); 16750 verifyFormat("struct test {\n" 16751 " long long int foo();\n" 16752 " int operator()(int a);\n" 16753 " double bar();\n" 16754 "};\n", 16755 Alignment); 16756 // http://llvm.org/PR52914 16757 verifyFormat("char *a[] = {\"a\", // comment\n" 16758 " \"bb\"};\n" 16759 "int bbbbbbb = 0;", 16760 Alignment); 16761 16762 // PAS_Right 16763 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16764 " int const i = 1;\n" 16765 " int *j = 2;\n" 16766 " int big = 10000;\n" 16767 "\n" 16768 " unsigned oneTwoThree = 123;\n" 16769 " int oneTwo = 12;\n" 16770 " method();\n" 16771 " float k = 2;\n" 16772 " int ll = 10000;\n" 16773 "}", 16774 format("void SomeFunction(int parameter= 0) {\n" 16775 " int const i= 1;\n" 16776 " int *j=2;\n" 16777 " int big = 10000;\n" 16778 "\n" 16779 "unsigned oneTwoThree =123;\n" 16780 "int oneTwo = 12;\n" 16781 " method();\n" 16782 "float k= 2;\n" 16783 "int ll=10000;\n" 16784 "}", 16785 Alignment)); 16786 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16787 " int const i = 1;\n" 16788 " int **j = 2, ***k;\n" 16789 " int &k = i;\n" 16790 " int &&l = i + j;\n" 16791 " int big = 10000;\n" 16792 "\n" 16793 " unsigned oneTwoThree = 123;\n" 16794 " int oneTwo = 12;\n" 16795 " method();\n" 16796 " float k = 2;\n" 16797 " int ll = 10000;\n" 16798 "}", 16799 format("void SomeFunction(int parameter= 0) {\n" 16800 " int const i= 1;\n" 16801 " int **j=2,***k;\n" 16802 "int &k=i;\n" 16803 "int &&l=i+j;\n" 16804 " int big = 10000;\n" 16805 "\n" 16806 "unsigned oneTwoThree =123;\n" 16807 "int oneTwo = 12;\n" 16808 " method();\n" 16809 "float k= 2;\n" 16810 "int ll=10000;\n" 16811 "}", 16812 Alignment)); 16813 // variables are aligned at their name, pointers are at the right most 16814 // position 16815 verifyFormat("int *a;\n" 16816 "int **b;\n" 16817 "int ***c;\n" 16818 "int foobar;\n", 16819 Alignment); 16820 16821 // PAS_Left 16822 FormatStyle AlignmentLeft = Alignment; 16823 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16824 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16825 " int const i = 1;\n" 16826 " int* j = 2;\n" 16827 " int big = 10000;\n" 16828 "\n" 16829 " unsigned oneTwoThree = 123;\n" 16830 " int oneTwo = 12;\n" 16831 " method();\n" 16832 " float k = 2;\n" 16833 " int ll = 10000;\n" 16834 "}", 16835 format("void SomeFunction(int parameter= 0) {\n" 16836 " int const i= 1;\n" 16837 " int *j=2;\n" 16838 " int big = 10000;\n" 16839 "\n" 16840 "unsigned oneTwoThree =123;\n" 16841 "int oneTwo = 12;\n" 16842 " method();\n" 16843 "float k= 2;\n" 16844 "int ll=10000;\n" 16845 "}", 16846 AlignmentLeft)); 16847 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16848 " int const i = 1;\n" 16849 " int** j = 2;\n" 16850 " int& k = i;\n" 16851 " int&& l = i + j;\n" 16852 " int big = 10000;\n" 16853 "\n" 16854 " unsigned oneTwoThree = 123;\n" 16855 " int oneTwo = 12;\n" 16856 " method();\n" 16857 " float k = 2;\n" 16858 " int ll = 10000;\n" 16859 "}", 16860 format("void SomeFunction(int parameter= 0) {\n" 16861 " int const i= 1;\n" 16862 " int **j=2;\n" 16863 "int &k=i;\n" 16864 "int &&l=i+j;\n" 16865 " int big = 10000;\n" 16866 "\n" 16867 "unsigned oneTwoThree =123;\n" 16868 "int oneTwo = 12;\n" 16869 " method();\n" 16870 "float k= 2;\n" 16871 "int ll=10000;\n" 16872 "}", 16873 AlignmentLeft)); 16874 // variables are aligned at their name, pointers are at the left most position 16875 verifyFormat("int* a;\n" 16876 "int** b;\n" 16877 "int*** c;\n" 16878 "int foobar;\n", 16879 AlignmentLeft); 16880 16881 // PAS_Middle 16882 FormatStyle AlignmentMiddle = Alignment; 16883 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16884 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16885 " int const i = 1;\n" 16886 " int * j = 2;\n" 16887 " int big = 10000;\n" 16888 "\n" 16889 " unsigned oneTwoThree = 123;\n" 16890 " int oneTwo = 12;\n" 16891 " method();\n" 16892 " float k = 2;\n" 16893 " int ll = 10000;\n" 16894 "}", 16895 format("void SomeFunction(int parameter= 0) {\n" 16896 " int const i= 1;\n" 16897 " int *j=2;\n" 16898 " int big = 10000;\n" 16899 "\n" 16900 "unsigned oneTwoThree =123;\n" 16901 "int oneTwo = 12;\n" 16902 " method();\n" 16903 "float k= 2;\n" 16904 "int ll=10000;\n" 16905 "}", 16906 AlignmentMiddle)); 16907 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16908 " int const i = 1;\n" 16909 " int ** j = 2, ***k;\n" 16910 " int & k = i;\n" 16911 " int && l = i + j;\n" 16912 " int big = 10000;\n" 16913 "\n" 16914 " unsigned oneTwoThree = 123;\n" 16915 " int oneTwo = 12;\n" 16916 " method();\n" 16917 " float k = 2;\n" 16918 " int ll = 10000;\n" 16919 "}", 16920 format("void SomeFunction(int parameter= 0) {\n" 16921 " int const i= 1;\n" 16922 " int **j=2,***k;\n" 16923 "int &k=i;\n" 16924 "int &&l=i+j;\n" 16925 " int big = 10000;\n" 16926 "\n" 16927 "unsigned oneTwoThree =123;\n" 16928 "int oneTwo = 12;\n" 16929 " method();\n" 16930 "float k= 2;\n" 16931 "int ll=10000;\n" 16932 "}", 16933 AlignmentMiddle)); 16934 // variables are aligned at their name, pointers are in the middle 16935 verifyFormat("int * a;\n" 16936 "int * b;\n" 16937 "int *** c;\n" 16938 "int foobar;\n", 16939 AlignmentMiddle); 16940 16941 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16942 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16943 verifyFormat("#define A \\\n" 16944 " int aaaa = 12; \\\n" 16945 " float b = 23; \\\n" 16946 " const int ccc = 234; \\\n" 16947 " unsigned dddddddddd = 2345;", 16948 Alignment); 16949 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16950 verifyFormat("#define A \\\n" 16951 " int aaaa = 12; \\\n" 16952 " float b = 23; \\\n" 16953 " const int ccc = 234; \\\n" 16954 " unsigned dddddddddd = 2345;", 16955 Alignment); 16956 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16957 Alignment.ColumnLimit = 30; 16958 verifyFormat("#define A \\\n" 16959 " int aaaa = 12; \\\n" 16960 " float b = 23; \\\n" 16961 " const int ccc = 234; \\\n" 16962 " int dddddddddd = 2345;", 16963 Alignment); 16964 Alignment.ColumnLimit = 80; 16965 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16966 "k = 4, int l = 5,\n" 16967 " int m = 6) {\n" 16968 " const int j = 10;\n" 16969 " otherThing = 1;\n" 16970 "}", 16971 Alignment); 16972 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16973 " int const i = 1;\n" 16974 " int *j = 2;\n" 16975 " int big = 10000;\n" 16976 "}", 16977 Alignment); 16978 verifyFormat("class C {\n" 16979 "public:\n" 16980 " int i = 1;\n" 16981 " virtual void f() = 0;\n" 16982 "};", 16983 Alignment); 16984 verifyFormat("float i = 1;\n" 16985 "if (SomeType t = getSomething()) {\n" 16986 "}\n" 16987 "const unsigned j = 2;\n" 16988 "int big = 10000;", 16989 Alignment); 16990 verifyFormat("float j = 7;\n" 16991 "for (int k = 0; k < N; ++k) {\n" 16992 "}\n" 16993 "unsigned j = 2;\n" 16994 "int big = 10000;\n" 16995 "}", 16996 Alignment); 16997 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16998 verifyFormat("float i = 1;\n" 16999 "LooooooooooongType loooooooooooooooooooooongVariable\n" 17000 " = someLooooooooooooooooongFunction();\n" 17001 "int j = 2;", 17002 Alignment); 17003 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 17004 verifyFormat("int i = 1;\n" 17005 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 17006 " someLooooooooooooooooongFunction();\n" 17007 "int j = 2;", 17008 Alignment); 17009 17010 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17011 verifyFormat("auto lambda = []() {\n" 17012 " auto ii = 0;\n" 17013 " float j = 0;\n" 17014 " return 0;\n" 17015 "};\n" 17016 "int i = 0;\n" 17017 "float i2 = 0;\n" 17018 "auto v = type{\n" 17019 " i = 1, //\n" 17020 " (i = 2), //\n" 17021 " i = 3 //\n" 17022 "};", 17023 Alignment); 17024 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17025 17026 verifyFormat( 17027 "int i = 1;\n" 17028 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 17029 " loooooooooooooooooooooongParameterB);\n" 17030 "int j = 2;", 17031 Alignment); 17032 17033 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 17034 // We expect declarations and assignments to align, as long as it doesn't 17035 // exceed the column limit, starting a new alignment sequence whenever it 17036 // happens. 17037 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17038 Alignment.ColumnLimit = 30; 17039 verifyFormat("float ii = 1;\n" 17040 "unsigned j = 2;\n" 17041 "int someVerylongVariable = 1;\n" 17042 "AnotherLongType ll = 123456;\n" 17043 "VeryVeryLongType k = 2;\n" 17044 "int myvar = 1;", 17045 Alignment); 17046 Alignment.ColumnLimit = 80; 17047 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17048 17049 verifyFormat( 17050 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 17051 " typename LongType, typename B>\n" 17052 "auto foo() {}\n", 17053 Alignment); 17054 verifyFormat("float a, b = 1;\n" 17055 "int c = 2;\n" 17056 "int dd = 3;\n", 17057 Alignment); 17058 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17059 "float b[1][] = {{3.f}};\n", 17060 Alignment); 17061 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17062 verifyFormat("float a, b = 1;\n" 17063 "int c = 2;\n" 17064 "int dd = 3;\n", 17065 Alignment); 17066 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17067 "float b[1][] = {{3.f}};\n", 17068 Alignment); 17069 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17070 17071 Alignment.ColumnLimit = 30; 17072 Alignment.BinPackParameters = false; 17073 verifyFormat("void foo(float a,\n" 17074 " float b,\n" 17075 " int c,\n" 17076 " uint32_t *d) {\n" 17077 " int *e = 0;\n" 17078 " float f = 0;\n" 17079 " double g = 0;\n" 17080 "}\n" 17081 "void bar(ino_t a,\n" 17082 " int b,\n" 17083 " uint32_t *c,\n" 17084 " bool d) {}\n", 17085 Alignment); 17086 Alignment.BinPackParameters = true; 17087 Alignment.ColumnLimit = 80; 17088 17089 // Bug 33507 17090 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17091 verifyFormat( 17092 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 17093 " static const Version verVs2017;\n" 17094 " return true;\n" 17095 "});\n", 17096 Alignment); 17097 Alignment.PointerAlignment = FormatStyle::PAS_Right; 17098 17099 // See llvm.org/PR35641 17100 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17101 verifyFormat("int func() { //\n" 17102 " int b;\n" 17103 " unsigned c;\n" 17104 "}", 17105 Alignment); 17106 17107 // See PR37175 17108 FormatStyle Style = getMozillaStyle(); 17109 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17110 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 17111 "foo(int a);", 17112 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 17113 17114 Alignment.PointerAlignment = FormatStyle::PAS_Left; 17115 verifyFormat("unsigned int* a;\n" 17116 "int* b;\n" 17117 "unsigned int Const* c;\n" 17118 "unsigned int const* d;\n" 17119 "unsigned int Const& e;\n" 17120 "unsigned int const& f;", 17121 Alignment); 17122 verifyFormat("Const unsigned int* c;\n" 17123 "const unsigned int* d;\n" 17124 "Const unsigned int& e;\n" 17125 "const unsigned int& f;\n" 17126 "const unsigned g;\n" 17127 "Const unsigned h;", 17128 Alignment); 17129 17130 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17131 verifyFormat("unsigned int * a;\n" 17132 "int * b;\n" 17133 "unsigned int Const * c;\n" 17134 "unsigned int const * d;\n" 17135 "unsigned int Const & e;\n" 17136 "unsigned int const & f;", 17137 Alignment); 17138 verifyFormat("Const unsigned int * c;\n" 17139 "const unsigned int * d;\n" 17140 "Const unsigned int & e;\n" 17141 "const unsigned int & f;\n" 17142 "const unsigned g;\n" 17143 "Const unsigned h;", 17144 Alignment); 17145 } 17146 17147 TEST_F(FormatTest, AlignWithLineBreaks) { 17148 auto Style = getLLVMStyleWithColumns(120); 17149 17150 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 17151 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 17152 verifyFormat("void foo() {\n" 17153 " int myVar = 5;\n" 17154 " double x = 3.14;\n" 17155 " auto str = \"Hello \"\n" 17156 " \"World\";\n" 17157 " auto s = \"Hello \"\n" 17158 " \"Again\";\n" 17159 "}", 17160 Style); 17161 17162 // clang-format off 17163 verifyFormat("void foo() {\n" 17164 " const int capacityBefore = Entries.capacity();\n" 17165 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17166 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17167 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17168 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17169 "}", 17170 Style); 17171 // clang-format on 17172 17173 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17174 verifyFormat("void foo() {\n" 17175 " int myVar = 5;\n" 17176 " double x = 3.14;\n" 17177 " auto str = \"Hello \"\n" 17178 " \"World\";\n" 17179 " auto s = \"Hello \"\n" 17180 " \"Again\";\n" 17181 "}", 17182 Style); 17183 17184 // clang-format off 17185 verifyFormat("void foo() {\n" 17186 " const int capacityBefore = Entries.capacity();\n" 17187 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17188 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17189 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17190 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17191 "}", 17192 Style); 17193 // clang-format on 17194 17195 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17196 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17197 verifyFormat("void foo() {\n" 17198 " int myVar = 5;\n" 17199 " double x = 3.14;\n" 17200 " auto str = \"Hello \"\n" 17201 " \"World\";\n" 17202 " auto s = \"Hello \"\n" 17203 " \"Again\";\n" 17204 "}", 17205 Style); 17206 17207 // clang-format off 17208 verifyFormat("void foo() {\n" 17209 " const int capacityBefore = Entries.capacity();\n" 17210 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17211 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17212 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17213 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17214 "}", 17215 Style); 17216 // clang-format on 17217 17218 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17219 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17220 17221 verifyFormat("void foo() {\n" 17222 " int myVar = 5;\n" 17223 " double x = 3.14;\n" 17224 " auto str = \"Hello \"\n" 17225 " \"World\";\n" 17226 " auto s = \"Hello \"\n" 17227 " \"Again\";\n" 17228 "}", 17229 Style); 17230 17231 // clang-format off 17232 verifyFormat("void foo() {\n" 17233 " const int capacityBefore = Entries.capacity();\n" 17234 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17235 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17236 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17237 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17238 "}", 17239 Style); 17240 // clang-format on 17241 17242 Style = getLLVMStyleWithColumns(120); 17243 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17244 Style.ContinuationIndentWidth = 4; 17245 Style.IndentWidth = 4; 17246 17247 // clang-format off 17248 verifyFormat("void SomeFunc() {\n" 17249 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17250 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17251 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17252 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17253 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17254 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17255 "}", 17256 Style); 17257 // clang-format on 17258 17259 Style.BinPackArguments = false; 17260 17261 // clang-format off 17262 verifyFormat("void SomeFunc() {\n" 17263 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 17264 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17265 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 17266 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17267 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 17268 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17269 "}", 17270 Style); 17271 // clang-format on 17272 } 17273 17274 TEST_F(FormatTest, AlignWithInitializerPeriods) { 17275 auto Style = getLLVMStyleWithColumns(60); 17276 17277 verifyFormat("void foo1(void) {\n" 17278 " BYTE p[1] = 1;\n" 17279 " A B = {.one_foooooooooooooooo = 2,\n" 17280 " .two_fooooooooooooo = 3,\n" 17281 " .three_fooooooooooooo = 4};\n" 17282 " BYTE payload = 2;\n" 17283 "}", 17284 Style); 17285 17286 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17287 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 17288 verifyFormat("void foo2(void) {\n" 17289 " BYTE p[1] = 1;\n" 17290 " A B = {.one_foooooooooooooooo = 2,\n" 17291 " .two_fooooooooooooo = 3,\n" 17292 " .three_fooooooooooooo = 4};\n" 17293 " BYTE payload = 2;\n" 17294 "}", 17295 Style); 17296 17297 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17298 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17299 verifyFormat("void foo3(void) {\n" 17300 " BYTE p[1] = 1;\n" 17301 " A B = {.one_foooooooooooooooo = 2,\n" 17302 " .two_fooooooooooooo = 3,\n" 17303 " .three_fooooooooooooo = 4};\n" 17304 " BYTE payload = 2;\n" 17305 "}", 17306 Style); 17307 17308 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17309 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17310 verifyFormat("void foo4(void) {\n" 17311 " BYTE p[1] = 1;\n" 17312 " A B = {.one_foooooooooooooooo = 2,\n" 17313 " .two_fooooooooooooo = 3,\n" 17314 " .three_fooooooooooooo = 4};\n" 17315 " BYTE payload = 2;\n" 17316 "}", 17317 Style); 17318 } 17319 17320 TEST_F(FormatTest, LinuxBraceBreaking) { 17321 FormatStyle LinuxBraceStyle = getLLVMStyle(); 17322 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 17323 verifyFormat("namespace a\n" 17324 "{\n" 17325 "class A\n" 17326 "{\n" 17327 " void f()\n" 17328 " {\n" 17329 " if (true) {\n" 17330 " a();\n" 17331 " b();\n" 17332 " } else {\n" 17333 " a();\n" 17334 " }\n" 17335 " }\n" 17336 " void g() { return; }\n" 17337 "};\n" 17338 "struct B {\n" 17339 " int x;\n" 17340 "};\n" 17341 "} // namespace a\n", 17342 LinuxBraceStyle); 17343 verifyFormat("enum X {\n" 17344 " Y = 0,\n" 17345 "}\n", 17346 LinuxBraceStyle); 17347 verifyFormat("struct S {\n" 17348 " int Type;\n" 17349 " union {\n" 17350 " int x;\n" 17351 " double y;\n" 17352 " } Value;\n" 17353 " class C\n" 17354 " {\n" 17355 " MyFavoriteType Value;\n" 17356 " } Class;\n" 17357 "}\n", 17358 LinuxBraceStyle); 17359 } 17360 17361 TEST_F(FormatTest, MozillaBraceBreaking) { 17362 FormatStyle MozillaBraceStyle = getLLVMStyle(); 17363 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 17364 MozillaBraceStyle.FixNamespaceComments = false; 17365 verifyFormat("namespace a {\n" 17366 "class A\n" 17367 "{\n" 17368 " void f()\n" 17369 " {\n" 17370 " if (true) {\n" 17371 " a();\n" 17372 " b();\n" 17373 " }\n" 17374 " }\n" 17375 " void g() { return; }\n" 17376 "};\n" 17377 "enum E\n" 17378 "{\n" 17379 " A,\n" 17380 " // foo\n" 17381 " B,\n" 17382 " C\n" 17383 "};\n" 17384 "struct B\n" 17385 "{\n" 17386 " int x;\n" 17387 "};\n" 17388 "}\n", 17389 MozillaBraceStyle); 17390 verifyFormat("struct S\n" 17391 "{\n" 17392 " int Type;\n" 17393 " union\n" 17394 " {\n" 17395 " int x;\n" 17396 " double y;\n" 17397 " } Value;\n" 17398 " class C\n" 17399 " {\n" 17400 " MyFavoriteType Value;\n" 17401 " } Class;\n" 17402 "}\n", 17403 MozillaBraceStyle); 17404 } 17405 17406 TEST_F(FormatTest, StroustrupBraceBreaking) { 17407 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 17408 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 17409 verifyFormat("namespace a {\n" 17410 "class A {\n" 17411 " void f()\n" 17412 " {\n" 17413 " if (true) {\n" 17414 " a();\n" 17415 " b();\n" 17416 " }\n" 17417 " }\n" 17418 " void g() { return; }\n" 17419 "};\n" 17420 "struct B {\n" 17421 " int x;\n" 17422 "};\n" 17423 "} // namespace a\n", 17424 StroustrupBraceStyle); 17425 17426 verifyFormat("void foo()\n" 17427 "{\n" 17428 " if (a) {\n" 17429 " a();\n" 17430 " }\n" 17431 " else {\n" 17432 " b();\n" 17433 " }\n" 17434 "}\n", 17435 StroustrupBraceStyle); 17436 17437 verifyFormat("#ifdef _DEBUG\n" 17438 "int foo(int i = 0)\n" 17439 "#else\n" 17440 "int foo(int i = 5)\n" 17441 "#endif\n" 17442 "{\n" 17443 " return i;\n" 17444 "}", 17445 StroustrupBraceStyle); 17446 17447 verifyFormat("void foo() {}\n" 17448 "void bar()\n" 17449 "#ifdef _DEBUG\n" 17450 "{\n" 17451 " foo();\n" 17452 "}\n" 17453 "#else\n" 17454 "{\n" 17455 "}\n" 17456 "#endif", 17457 StroustrupBraceStyle); 17458 17459 verifyFormat("void foobar() { int i = 5; }\n" 17460 "#ifdef _DEBUG\n" 17461 "void bar() {}\n" 17462 "#else\n" 17463 "void bar() { foobar(); }\n" 17464 "#endif", 17465 StroustrupBraceStyle); 17466 } 17467 17468 TEST_F(FormatTest, AllmanBraceBreaking) { 17469 FormatStyle AllmanBraceStyle = getLLVMStyle(); 17470 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 17471 17472 EXPECT_EQ("namespace a\n" 17473 "{\n" 17474 "void f();\n" 17475 "void g();\n" 17476 "} // namespace a\n", 17477 format("namespace a\n" 17478 "{\n" 17479 "void f();\n" 17480 "void g();\n" 17481 "}\n", 17482 AllmanBraceStyle)); 17483 17484 verifyFormat("namespace a\n" 17485 "{\n" 17486 "class A\n" 17487 "{\n" 17488 " void f()\n" 17489 " {\n" 17490 " if (true)\n" 17491 " {\n" 17492 " a();\n" 17493 " b();\n" 17494 " }\n" 17495 " }\n" 17496 " void g() { return; }\n" 17497 "};\n" 17498 "struct B\n" 17499 "{\n" 17500 " int x;\n" 17501 "};\n" 17502 "union C\n" 17503 "{\n" 17504 "};\n" 17505 "} // namespace a", 17506 AllmanBraceStyle); 17507 17508 verifyFormat("void f()\n" 17509 "{\n" 17510 " if (true)\n" 17511 " {\n" 17512 " a();\n" 17513 " }\n" 17514 " else if (false)\n" 17515 " {\n" 17516 " b();\n" 17517 " }\n" 17518 " else\n" 17519 " {\n" 17520 " c();\n" 17521 " }\n" 17522 "}\n", 17523 AllmanBraceStyle); 17524 17525 verifyFormat("void f()\n" 17526 "{\n" 17527 " for (int i = 0; i < 10; ++i)\n" 17528 " {\n" 17529 " a();\n" 17530 " }\n" 17531 " while (false)\n" 17532 " {\n" 17533 " b();\n" 17534 " }\n" 17535 " do\n" 17536 " {\n" 17537 " c();\n" 17538 " } while (false)\n" 17539 "}\n", 17540 AllmanBraceStyle); 17541 17542 verifyFormat("void f(int a)\n" 17543 "{\n" 17544 " switch (a)\n" 17545 " {\n" 17546 " case 0:\n" 17547 " break;\n" 17548 " case 1:\n" 17549 " {\n" 17550 " break;\n" 17551 " }\n" 17552 " case 2:\n" 17553 " {\n" 17554 " }\n" 17555 " break;\n" 17556 " default:\n" 17557 " break;\n" 17558 " }\n" 17559 "}\n", 17560 AllmanBraceStyle); 17561 17562 verifyFormat("enum X\n" 17563 "{\n" 17564 " Y = 0,\n" 17565 "}\n", 17566 AllmanBraceStyle); 17567 verifyFormat("enum X\n" 17568 "{\n" 17569 " Y = 0\n" 17570 "}\n", 17571 AllmanBraceStyle); 17572 17573 verifyFormat("@interface BSApplicationController ()\n" 17574 "{\n" 17575 "@private\n" 17576 " id _extraIvar;\n" 17577 "}\n" 17578 "@end\n", 17579 AllmanBraceStyle); 17580 17581 verifyFormat("#ifdef _DEBUG\n" 17582 "int foo(int i = 0)\n" 17583 "#else\n" 17584 "int foo(int i = 5)\n" 17585 "#endif\n" 17586 "{\n" 17587 " return i;\n" 17588 "}", 17589 AllmanBraceStyle); 17590 17591 verifyFormat("void foo() {}\n" 17592 "void bar()\n" 17593 "#ifdef _DEBUG\n" 17594 "{\n" 17595 " foo();\n" 17596 "}\n" 17597 "#else\n" 17598 "{\n" 17599 "}\n" 17600 "#endif", 17601 AllmanBraceStyle); 17602 17603 verifyFormat("void foobar() { int i = 5; }\n" 17604 "#ifdef _DEBUG\n" 17605 "void bar() {}\n" 17606 "#else\n" 17607 "void bar() { foobar(); }\n" 17608 "#endif", 17609 AllmanBraceStyle); 17610 17611 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 17612 FormatStyle::SLS_All); 17613 17614 verifyFormat("[](int i) { return i + 2; };\n" 17615 "[](int i, int j)\n" 17616 "{\n" 17617 " auto x = i + j;\n" 17618 " auto y = i * j;\n" 17619 " return x ^ y;\n" 17620 "};\n" 17621 "void foo()\n" 17622 "{\n" 17623 " auto shortLambda = [](int i) { return i + 2; };\n" 17624 " auto longLambda = [](int i, int j)\n" 17625 " {\n" 17626 " auto x = i + j;\n" 17627 " auto y = i * j;\n" 17628 " return x ^ y;\n" 17629 " };\n" 17630 "}", 17631 AllmanBraceStyle); 17632 17633 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17634 17635 verifyFormat("[](int i)\n" 17636 "{\n" 17637 " return i + 2;\n" 17638 "};\n" 17639 "[](int i, int j)\n" 17640 "{\n" 17641 " auto x = i + j;\n" 17642 " auto y = i * j;\n" 17643 " return x ^ y;\n" 17644 "};\n" 17645 "void foo()\n" 17646 "{\n" 17647 " auto shortLambda = [](int i)\n" 17648 " {\n" 17649 " return i + 2;\n" 17650 " };\n" 17651 " auto longLambda = [](int i, int j)\n" 17652 " {\n" 17653 " auto x = i + j;\n" 17654 " auto y = i * j;\n" 17655 " return x ^ y;\n" 17656 " };\n" 17657 "}", 17658 AllmanBraceStyle); 17659 17660 // Reset 17661 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 17662 17663 // This shouldn't affect ObjC blocks.. 17664 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17665 " // ...\n" 17666 " int i;\n" 17667 "}];", 17668 AllmanBraceStyle); 17669 verifyFormat("void (^block)(void) = ^{\n" 17670 " // ...\n" 17671 " int i;\n" 17672 "};", 17673 AllmanBraceStyle); 17674 // .. or dict literals. 17675 verifyFormat("void f()\n" 17676 "{\n" 17677 " // ...\n" 17678 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17679 "}", 17680 AllmanBraceStyle); 17681 verifyFormat("void f()\n" 17682 "{\n" 17683 " // ...\n" 17684 " [object someMethod:@{a : @\"b\"}];\n" 17685 "}", 17686 AllmanBraceStyle); 17687 verifyFormat("int f()\n" 17688 "{ // comment\n" 17689 " return 42;\n" 17690 "}", 17691 AllmanBraceStyle); 17692 17693 AllmanBraceStyle.ColumnLimit = 19; 17694 verifyFormat("void f() { int i; }", AllmanBraceStyle); 17695 AllmanBraceStyle.ColumnLimit = 18; 17696 verifyFormat("void f()\n" 17697 "{\n" 17698 " int i;\n" 17699 "}", 17700 AllmanBraceStyle); 17701 AllmanBraceStyle.ColumnLimit = 80; 17702 17703 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 17704 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17705 FormatStyle::SIS_WithoutElse; 17706 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17707 verifyFormat("void f(bool b)\n" 17708 "{\n" 17709 " if (b)\n" 17710 " {\n" 17711 " return;\n" 17712 " }\n" 17713 "}\n", 17714 BreakBeforeBraceShortIfs); 17715 verifyFormat("void f(bool b)\n" 17716 "{\n" 17717 " if constexpr (b)\n" 17718 " {\n" 17719 " return;\n" 17720 " }\n" 17721 "}\n", 17722 BreakBeforeBraceShortIfs); 17723 verifyFormat("void f(bool b)\n" 17724 "{\n" 17725 " if CONSTEXPR (b)\n" 17726 " {\n" 17727 " return;\n" 17728 " }\n" 17729 "}\n", 17730 BreakBeforeBraceShortIfs); 17731 verifyFormat("void f(bool b)\n" 17732 "{\n" 17733 " if (b) return;\n" 17734 "}\n", 17735 BreakBeforeBraceShortIfs); 17736 verifyFormat("void f(bool b)\n" 17737 "{\n" 17738 " if constexpr (b) return;\n" 17739 "}\n", 17740 BreakBeforeBraceShortIfs); 17741 verifyFormat("void f(bool b)\n" 17742 "{\n" 17743 " if CONSTEXPR (b) return;\n" 17744 "}\n", 17745 BreakBeforeBraceShortIfs); 17746 verifyFormat("void f(bool b)\n" 17747 "{\n" 17748 " while (b)\n" 17749 " {\n" 17750 " return;\n" 17751 " }\n" 17752 "}\n", 17753 BreakBeforeBraceShortIfs); 17754 } 17755 17756 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 17757 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0); 17758 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 17759 17760 // Make a few changes to the style for testing purposes 17761 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 17762 FormatStyle::SFS_Empty; 17763 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17764 17765 // FIXME: this test case can't decide whether there should be a blank line 17766 // after the ~D() line or not. It adds one if one doesn't exist in the test 17767 // and it removes the line if one exists. 17768 /* 17769 verifyFormat("class A;\n" 17770 "namespace B\n" 17771 " {\n" 17772 "class C;\n" 17773 "// Comment\n" 17774 "class D\n" 17775 " {\n" 17776 "public:\n" 17777 " D();\n" 17778 " ~D() {}\n" 17779 "private:\n" 17780 " enum E\n" 17781 " {\n" 17782 " F\n" 17783 " }\n" 17784 " };\n" 17785 " } // namespace B\n", 17786 WhitesmithsBraceStyle); 17787 */ 17788 17789 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17790 verifyFormat("namespace a\n" 17791 " {\n" 17792 "class A\n" 17793 " {\n" 17794 " void f()\n" 17795 " {\n" 17796 " if (true)\n" 17797 " {\n" 17798 " a();\n" 17799 " b();\n" 17800 " }\n" 17801 " }\n" 17802 " void g()\n" 17803 " {\n" 17804 " return;\n" 17805 " }\n" 17806 " };\n" 17807 "struct B\n" 17808 " {\n" 17809 " int x;\n" 17810 " };\n" 17811 " } // namespace a", 17812 WhitesmithsBraceStyle); 17813 17814 verifyFormat("namespace a\n" 17815 " {\n" 17816 "namespace b\n" 17817 " {\n" 17818 "class A\n" 17819 " {\n" 17820 " void f()\n" 17821 " {\n" 17822 " if (true)\n" 17823 " {\n" 17824 " a();\n" 17825 " b();\n" 17826 " }\n" 17827 " }\n" 17828 " void g()\n" 17829 " {\n" 17830 " return;\n" 17831 " }\n" 17832 " };\n" 17833 "struct B\n" 17834 " {\n" 17835 " int x;\n" 17836 " };\n" 17837 " } // namespace b\n" 17838 " } // namespace a", 17839 WhitesmithsBraceStyle); 17840 17841 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17842 verifyFormat("namespace a\n" 17843 " {\n" 17844 "namespace b\n" 17845 " {\n" 17846 " class A\n" 17847 " {\n" 17848 " void f()\n" 17849 " {\n" 17850 " if (true)\n" 17851 " {\n" 17852 " a();\n" 17853 " b();\n" 17854 " }\n" 17855 " }\n" 17856 " void g()\n" 17857 " {\n" 17858 " return;\n" 17859 " }\n" 17860 " };\n" 17861 " struct B\n" 17862 " {\n" 17863 " int x;\n" 17864 " };\n" 17865 " } // namespace b\n" 17866 " } // namespace a", 17867 WhitesmithsBraceStyle); 17868 17869 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17870 verifyFormat("namespace a\n" 17871 " {\n" 17872 " namespace b\n" 17873 " {\n" 17874 " class A\n" 17875 " {\n" 17876 " void f()\n" 17877 " {\n" 17878 " if (true)\n" 17879 " {\n" 17880 " a();\n" 17881 " b();\n" 17882 " }\n" 17883 " }\n" 17884 " void g()\n" 17885 " {\n" 17886 " return;\n" 17887 " }\n" 17888 " };\n" 17889 " struct B\n" 17890 " {\n" 17891 " int x;\n" 17892 " };\n" 17893 " } // namespace b\n" 17894 " } // namespace a", 17895 WhitesmithsBraceStyle); 17896 17897 verifyFormat("void f()\n" 17898 " {\n" 17899 " if (true)\n" 17900 " {\n" 17901 " a();\n" 17902 " }\n" 17903 " else if (false)\n" 17904 " {\n" 17905 " b();\n" 17906 " }\n" 17907 " else\n" 17908 " {\n" 17909 " c();\n" 17910 " }\n" 17911 " }\n", 17912 WhitesmithsBraceStyle); 17913 17914 verifyFormat("void f()\n" 17915 " {\n" 17916 " for (int i = 0; i < 10; ++i)\n" 17917 " {\n" 17918 " a();\n" 17919 " }\n" 17920 " while (false)\n" 17921 " {\n" 17922 " b();\n" 17923 " }\n" 17924 " do\n" 17925 " {\n" 17926 " c();\n" 17927 " } while (false)\n" 17928 " }\n", 17929 WhitesmithsBraceStyle); 17930 17931 WhitesmithsBraceStyle.IndentCaseLabels = true; 17932 verifyFormat("void switchTest1(int a)\n" 17933 " {\n" 17934 " switch (a)\n" 17935 " {\n" 17936 " case 2:\n" 17937 " {\n" 17938 " }\n" 17939 " break;\n" 17940 " }\n" 17941 " }\n", 17942 WhitesmithsBraceStyle); 17943 17944 verifyFormat("void switchTest2(int a)\n" 17945 " {\n" 17946 " switch (a)\n" 17947 " {\n" 17948 " case 0:\n" 17949 " break;\n" 17950 " case 1:\n" 17951 " {\n" 17952 " break;\n" 17953 " }\n" 17954 " case 2:\n" 17955 " {\n" 17956 " }\n" 17957 " break;\n" 17958 " default:\n" 17959 " break;\n" 17960 " }\n" 17961 " }\n", 17962 WhitesmithsBraceStyle); 17963 17964 verifyFormat("void switchTest3(int a)\n" 17965 " {\n" 17966 " switch (a)\n" 17967 " {\n" 17968 " case 0:\n" 17969 " {\n" 17970 " foo(x);\n" 17971 " }\n" 17972 " break;\n" 17973 " default:\n" 17974 " {\n" 17975 " foo(1);\n" 17976 " }\n" 17977 " break;\n" 17978 " }\n" 17979 " }\n", 17980 WhitesmithsBraceStyle); 17981 17982 WhitesmithsBraceStyle.IndentCaseLabels = false; 17983 17984 verifyFormat("void switchTest4(int a)\n" 17985 " {\n" 17986 " switch (a)\n" 17987 " {\n" 17988 " case 2:\n" 17989 " {\n" 17990 " }\n" 17991 " break;\n" 17992 " }\n" 17993 " }\n", 17994 WhitesmithsBraceStyle); 17995 17996 verifyFormat("void switchTest5(int a)\n" 17997 " {\n" 17998 " switch (a)\n" 17999 " {\n" 18000 " case 0:\n" 18001 " break;\n" 18002 " case 1:\n" 18003 " {\n" 18004 " foo();\n" 18005 " break;\n" 18006 " }\n" 18007 " case 2:\n" 18008 " {\n" 18009 " }\n" 18010 " break;\n" 18011 " default:\n" 18012 " break;\n" 18013 " }\n" 18014 " }\n", 18015 WhitesmithsBraceStyle); 18016 18017 verifyFormat("void switchTest6(int a)\n" 18018 " {\n" 18019 " switch (a)\n" 18020 " {\n" 18021 " case 0:\n" 18022 " {\n" 18023 " foo(x);\n" 18024 " }\n" 18025 " break;\n" 18026 " default:\n" 18027 " {\n" 18028 " foo(1);\n" 18029 " }\n" 18030 " break;\n" 18031 " }\n" 18032 " }\n", 18033 WhitesmithsBraceStyle); 18034 18035 verifyFormat("enum X\n" 18036 " {\n" 18037 " Y = 0, // testing\n" 18038 " }\n", 18039 WhitesmithsBraceStyle); 18040 18041 verifyFormat("enum X\n" 18042 " {\n" 18043 " Y = 0\n" 18044 " }\n", 18045 WhitesmithsBraceStyle); 18046 verifyFormat("enum X\n" 18047 " {\n" 18048 " Y = 0,\n" 18049 " Z = 1\n" 18050 " };\n", 18051 WhitesmithsBraceStyle); 18052 18053 verifyFormat("@interface BSApplicationController ()\n" 18054 " {\n" 18055 "@private\n" 18056 " id _extraIvar;\n" 18057 " }\n" 18058 "@end\n", 18059 WhitesmithsBraceStyle); 18060 18061 verifyFormat("#ifdef _DEBUG\n" 18062 "int foo(int i = 0)\n" 18063 "#else\n" 18064 "int foo(int i = 5)\n" 18065 "#endif\n" 18066 " {\n" 18067 " return i;\n" 18068 " }", 18069 WhitesmithsBraceStyle); 18070 18071 verifyFormat("void foo() {}\n" 18072 "void bar()\n" 18073 "#ifdef _DEBUG\n" 18074 " {\n" 18075 " foo();\n" 18076 " }\n" 18077 "#else\n" 18078 " {\n" 18079 " }\n" 18080 "#endif", 18081 WhitesmithsBraceStyle); 18082 18083 verifyFormat("void foobar()\n" 18084 " {\n" 18085 " int i = 5;\n" 18086 " }\n" 18087 "#ifdef _DEBUG\n" 18088 "void bar()\n" 18089 " {\n" 18090 " }\n" 18091 "#else\n" 18092 "void bar()\n" 18093 " {\n" 18094 " foobar();\n" 18095 " }\n" 18096 "#endif", 18097 WhitesmithsBraceStyle); 18098 18099 // This shouldn't affect ObjC blocks.. 18100 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 18101 " // ...\n" 18102 " int i;\n" 18103 "}];", 18104 WhitesmithsBraceStyle); 18105 verifyFormat("void (^block)(void) = ^{\n" 18106 " // ...\n" 18107 " int i;\n" 18108 "};", 18109 WhitesmithsBraceStyle); 18110 // .. or dict literals. 18111 verifyFormat("void f()\n" 18112 " {\n" 18113 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 18114 " }", 18115 WhitesmithsBraceStyle); 18116 18117 verifyFormat("int f()\n" 18118 " { // comment\n" 18119 " return 42;\n" 18120 " }", 18121 WhitesmithsBraceStyle); 18122 18123 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 18124 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 18125 FormatStyle::SIS_OnlyFirstIf; 18126 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 18127 verifyFormat("void f(bool b)\n" 18128 " {\n" 18129 " if (b)\n" 18130 " {\n" 18131 " return;\n" 18132 " }\n" 18133 " }\n", 18134 BreakBeforeBraceShortIfs); 18135 verifyFormat("void f(bool b)\n" 18136 " {\n" 18137 " if (b) return;\n" 18138 " }\n", 18139 BreakBeforeBraceShortIfs); 18140 verifyFormat("void f(bool b)\n" 18141 " {\n" 18142 " while (b)\n" 18143 " {\n" 18144 " return;\n" 18145 " }\n" 18146 " }\n", 18147 BreakBeforeBraceShortIfs); 18148 } 18149 18150 TEST_F(FormatTest, GNUBraceBreaking) { 18151 FormatStyle GNUBraceStyle = getLLVMStyle(); 18152 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 18153 verifyFormat("namespace a\n" 18154 "{\n" 18155 "class A\n" 18156 "{\n" 18157 " void f()\n" 18158 " {\n" 18159 " int a;\n" 18160 " {\n" 18161 " int b;\n" 18162 " }\n" 18163 " if (true)\n" 18164 " {\n" 18165 " a();\n" 18166 " b();\n" 18167 " }\n" 18168 " }\n" 18169 " void g() { return; }\n" 18170 "}\n" 18171 "} // namespace a", 18172 GNUBraceStyle); 18173 18174 verifyFormat("void f()\n" 18175 "{\n" 18176 " if (true)\n" 18177 " {\n" 18178 " a();\n" 18179 " }\n" 18180 " else if (false)\n" 18181 " {\n" 18182 " b();\n" 18183 " }\n" 18184 " else\n" 18185 " {\n" 18186 " c();\n" 18187 " }\n" 18188 "}\n", 18189 GNUBraceStyle); 18190 18191 verifyFormat("void f()\n" 18192 "{\n" 18193 " for (int i = 0; i < 10; ++i)\n" 18194 " {\n" 18195 " a();\n" 18196 " }\n" 18197 " while (false)\n" 18198 " {\n" 18199 " b();\n" 18200 " }\n" 18201 " do\n" 18202 " {\n" 18203 " c();\n" 18204 " }\n" 18205 " while (false);\n" 18206 "}\n", 18207 GNUBraceStyle); 18208 18209 verifyFormat("void f(int a)\n" 18210 "{\n" 18211 " switch (a)\n" 18212 " {\n" 18213 " case 0:\n" 18214 " break;\n" 18215 " case 1:\n" 18216 " {\n" 18217 " break;\n" 18218 " }\n" 18219 " case 2:\n" 18220 " {\n" 18221 " }\n" 18222 " break;\n" 18223 " default:\n" 18224 " break;\n" 18225 " }\n" 18226 "}\n", 18227 GNUBraceStyle); 18228 18229 verifyFormat("enum X\n" 18230 "{\n" 18231 " Y = 0,\n" 18232 "}\n", 18233 GNUBraceStyle); 18234 18235 verifyFormat("@interface BSApplicationController ()\n" 18236 "{\n" 18237 "@private\n" 18238 " id _extraIvar;\n" 18239 "}\n" 18240 "@end\n", 18241 GNUBraceStyle); 18242 18243 verifyFormat("#ifdef _DEBUG\n" 18244 "int foo(int i = 0)\n" 18245 "#else\n" 18246 "int foo(int i = 5)\n" 18247 "#endif\n" 18248 "{\n" 18249 " return i;\n" 18250 "}", 18251 GNUBraceStyle); 18252 18253 verifyFormat("void foo() {}\n" 18254 "void bar()\n" 18255 "#ifdef _DEBUG\n" 18256 "{\n" 18257 " foo();\n" 18258 "}\n" 18259 "#else\n" 18260 "{\n" 18261 "}\n" 18262 "#endif", 18263 GNUBraceStyle); 18264 18265 verifyFormat("void foobar() { int i = 5; }\n" 18266 "#ifdef _DEBUG\n" 18267 "void bar() {}\n" 18268 "#else\n" 18269 "void bar() { foobar(); }\n" 18270 "#endif", 18271 GNUBraceStyle); 18272 } 18273 18274 TEST_F(FormatTest, WebKitBraceBreaking) { 18275 FormatStyle WebKitBraceStyle = getLLVMStyle(); 18276 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 18277 WebKitBraceStyle.FixNamespaceComments = false; 18278 verifyFormat("namespace a {\n" 18279 "class A {\n" 18280 " void f()\n" 18281 " {\n" 18282 " if (true) {\n" 18283 " a();\n" 18284 " b();\n" 18285 " }\n" 18286 " }\n" 18287 " void g() { return; }\n" 18288 "};\n" 18289 "enum E {\n" 18290 " A,\n" 18291 " // foo\n" 18292 " B,\n" 18293 " C\n" 18294 "};\n" 18295 "struct B {\n" 18296 " int x;\n" 18297 "};\n" 18298 "}\n", 18299 WebKitBraceStyle); 18300 verifyFormat("struct S {\n" 18301 " int Type;\n" 18302 " union {\n" 18303 " int x;\n" 18304 " double y;\n" 18305 " } Value;\n" 18306 " class C {\n" 18307 " MyFavoriteType Value;\n" 18308 " } Class;\n" 18309 "};\n", 18310 WebKitBraceStyle); 18311 } 18312 18313 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 18314 verifyFormat("void f() {\n" 18315 " try {\n" 18316 " } catch (const Exception &e) {\n" 18317 " }\n" 18318 "}\n", 18319 getLLVMStyle()); 18320 } 18321 18322 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 18323 auto Style = getLLVMStyle(); 18324 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18325 Style.AlignConsecutiveAssignments = 18326 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18327 Style.AlignConsecutiveDeclarations = 18328 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18329 verifyFormat("struct test demo[] = {\n" 18330 " {56, 23, \"hello\"},\n" 18331 " {-1, 93463, \"world\"},\n" 18332 " { 7, 5, \"!!\"}\n" 18333 "};\n", 18334 Style); 18335 18336 verifyFormat("struct test demo[] = {\n" 18337 " {56, 23, \"hello\"}, // first line\n" 18338 " {-1, 93463, \"world\"}, // second line\n" 18339 " { 7, 5, \"!!\"} // third line\n" 18340 "};\n", 18341 Style); 18342 18343 verifyFormat("struct test demo[4] = {\n" 18344 " { 56, 23, 21, \"oh\"}, // first line\n" 18345 " { -1, 93463, 22, \"my\"}, // second line\n" 18346 " { 7, 5, 1, \"goodness\"} // third line\n" 18347 " {234, 5, 1, \"gracious\"} // fourth line\n" 18348 "};\n", 18349 Style); 18350 18351 verifyFormat("struct test demo[3] = {\n" 18352 " {56, 23, \"hello\"},\n" 18353 " {-1, 93463, \"world\"},\n" 18354 " { 7, 5, \"!!\"}\n" 18355 "};\n", 18356 Style); 18357 18358 verifyFormat("struct test demo[3] = {\n" 18359 " {int{56}, 23, \"hello\"},\n" 18360 " {int{-1}, 93463, \"world\"},\n" 18361 " { int{7}, 5, \"!!\"}\n" 18362 "};\n", 18363 Style); 18364 18365 verifyFormat("struct test demo[] = {\n" 18366 " {56, 23, \"hello\"},\n" 18367 " {-1, 93463, \"world\"},\n" 18368 " { 7, 5, \"!!\"},\n" 18369 "};\n", 18370 Style); 18371 18372 verifyFormat("test demo[] = {\n" 18373 " {56, 23, \"hello\"},\n" 18374 " {-1, 93463, \"world\"},\n" 18375 " { 7, 5, \"!!\"},\n" 18376 "};\n", 18377 Style); 18378 18379 verifyFormat("demo = std::array<struct test, 3>{\n" 18380 " test{56, 23, \"hello\"},\n" 18381 " test{-1, 93463, \"world\"},\n" 18382 " test{ 7, 5, \"!!\"},\n" 18383 "};\n", 18384 Style); 18385 18386 verifyFormat("test demo[] = {\n" 18387 " {56, 23, \"hello\"},\n" 18388 "#if X\n" 18389 " {-1, 93463, \"world\"},\n" 18390 "#endif\n" 18391 " { 7, 5, \"!!\"}\n" 18392 "};\n", 18393 Style); 18394 18395 verifyFormat( 18396 "test demo[] = {\n" 18397 " { 7, 23,\n" 18398 " \"hello world i am a very long line that really, in any\"\n" 18399 " \"just world, ought to be split over multiple lines\"},\n" 18400 " {-1, 93463, \"world\"},\n" 18401 " {56, 5, \"!!\"}\n" 18402 "};\n", 18403 Style); 18404 18405 verifyFormat("return GradForUnaryCwise(g, {\n" 18406 " {{\"sign\"}, \"Sign\", " 18407 " {\"x\", \"dy\"}},\n" 18408 " { {\"dx\"}, \"Mul\", {\"dy\"" 18409 ", \"sign\"}},\n" 18410 "});\n", 18411 Style); 18412 18413 Style.ColumnLimit = 0; 18414 EXPECT_EQ( 18415 "test demo[] = {\n" 18416 " {56, 23, \"hello world i am a very long line that really, " 18417 "in any just world, ought to be split over multiple lines\"},\n" 18418 " {-1, 93463, " 18419 " \"world\"},\n" 18420 " { 7, 5, " 18421 " \"!!\"},\n" 18422 "};", 18423 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18424 "that really, in any just world, ought to be split over multiple " 18425 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18426 Style)); 18427 18428 Style.ColumnLimit = 80; 18429 verifyFormat("test demo[] = {\n" 18430 " {56, 23, /* a comment */ \"hello\"},\n" 18431 " {-1, 93463, \"world\"},\n" 18432 " { 7, 5, \"!!\"}\n" 18433 "};\n", 18434 Style); 18435 18436 verifyFormat("test demo[] = {\n" 18437 " {56, 23, \"hello\"},\n" 18438 " {-1, 93463, \"world\" /* comment here */},\n" 18439 " { 7, 5, \"!!\"}\n" 18440 "};\n", 18441 Style); 18442 18443 verifyFormat("test demo[] = {\n" 18444 " {56, /* a comment */ 23, \"hello\"},\n" 18445 " {-1, 93463, \"world\"},\n" 18446 " { 7, 5, \"!!\"}\n" 18447 "};\n", 18448 Style); 18449 18450 Style.ColumnLimit = 20; 18451 EXPECT_EQ( 18452 "demo = std::array<\n" 18453 " struct test, 3>{\n" 18454 " test{\n" 18455 " 56, 23,\n" 18456 " \"hello \"\n" 18457 " \"world i \"\n" 18458 " \"am a very \"\n" 18459 " \"long line \"\n" 18460 " \"that \"\n" 18461 " \"really, \"\n" 18462 " \"in any \"\n" 18463 " \"just \"\n" 18464 " \"world, \"\n" 18465 " \"ought to \"\n" 18466 " \"be split \"\n" 18467 " \"over \"\n" 18468 " \"multiple \"\n" 18469 " \"lines\"},\n" 18470 " test{-1, 93463,\n" 18471 " \"world\"},\n" 18472 " test{ 7, 5,\n" 18473 " \"!!\" },\n" 18474 "};", 18475 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18476 "i am a very long line that really, in any just world, ought " 18477 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18478 "test{7, 5, \"!!\"},};", 18479 Style)); 18480 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18481 Style = getLLVMStyleWithColumns(50); 18482 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18483 verifyFormat("static A x = {\n" 18484 " {{init1, init2, init3, init4},\n" 18485 " {init1, init2, init3, init4}}\n" 18486 "};", 18487 Style); 18488 Style.ColumnLimit = 100; 18489 EXPECT_EQ( 18490 "test demo[] = {\n" 18491 " {56, 23,\n" 18492 " \"hello world i am a very long line that really, in any just world" 18493 ", ought to be split over \"\n" 18494 " \"multiple lines\" },\n" 18495 " {-1, 93463, \"world\"},\n" 18496 " { 7, 5, \"!!\"},\n" 18497 "};", 18498 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18499 "that really, in any just world, ought to be split over multiple " 18500 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18501 Style)); 18502 18503 Style = getLLVMStyleWithColumns(50); 18504 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18505 Style.AlignConsecutiveAssignments = 18506 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18507 Style.AlignConsecutiveDeclarations = 18508 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18509 verifyFormat("struct test demo[] = {\n" 18510 " {56, 23, \"hello\"},\n" 18511 " {-1, 93463, \"world\"},\n" 18512 " { 7, 5, \"!!\"}\n" 18513 "};\n" 18514 "static A x = {\n" 18515 " {{init1, init2, init3, init4},\n" 18516 " {init1, init2, init3, init4}}\n" 18517 "};", 18518 Style); 18519 Style.ColumnLimit = 100; 18520 Style.AlignConsecutiveAssignments = 18521 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18522 Style.AlignConsecutiveDeclarations = 18523 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18524 verifyFormat("struct test demo[] = {\n" 18525 " {56, 23, \"hello\"},\n" 18526 " {-1, 93463, \"world\"},\n" 18527 " { 7, 5, \"!!\"}\n" 18528 "};\n" 18529 "struct test demo[4] = {\n" 18530 " { 56, 23, 21, \"oh\"}, // first line\n" 18531 " { -1, 93463, 22, \"my\"}, // second line\n" 18532 " { 7, 5, 1, \"goodness\"} // third line\n" 18533 " {234, 5, 1, \"gracious\"} // fourth line\n" 18534 "};\n", 18535 Style); 18536 EXPECT_EQ( 18537 "test demo[] = {\n" 18538 " {56,\n" 18539 " \"hello world i am a very long line that really, in any just world" 18540 ", ought to be split over \"\n" 18541 " \"multiple lines\", 23},\n" 18542 " {-1, \"world\", 93463},\n" 18543 " { 7, \"!!\", 5},\n" 18544 "};", 18545 format("test demo[] = {{56, \"hello world i am a very long line " 18546 "that really, in any just world, ought to be split over multiple " 18547 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 18548 Style)); 18549 } 18550 18551 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 18552 auto Style = getLLVMStyle(); 18553 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18554 /* FIXME: This case gets misformatted. 18555 verifyFormat("auto foo = Items{\n" 18556 " Section{0, bar(), },\n" 18557 " Section{1, boo() }\n" 18558 "};\n", 18559 Style); 18560 */ 18561 verifyFormat("auto foo = Items{\n" 18562 " Section{\n" 18563 " 0, bar(),\n" 18564 " }\n" 18565 "};\n", 18566 Style); 18567 verifyFormat("struct test demo[] = {\n" 18568 " {56, 23, \"hello\"},\n" 18569 " {-1, 93463, \"world\"},\n" 18570 " {7, 5, \"!!\" }\n" 18571 "};\n", 18572 Style); 18573 verifyFormat("struct test demo[] = {\n" 18574 " {56, 23, \"hello\"}, // first line\n" 18575 " {-1, 93463, \"world\"}, // second line\n" 18576 " {7, 5, \"!!\" } // third line\n" 18577 "};\n", 18578 Style); 18579 verifyFormat("struct test demo[4] = {\n" 18580 " {56, 23, 21, \"oh\" }, // first line\n" 18581 " {-1, 93463, 22, \"my\" }, // second line\n" 18582 " {7, 5, 1, \"goodness\"} // third line\n" 18583 " {234, 5, 1, \"gracious\"} // fourth line\n" 18584 "};\n", 18585 Style); 18586 verifyFormat("struct test demo[3] = {\n" 18587 " {56, 23, \"hello\"},\n" 18588 " {-1, 93463, \"world\"},\n" 18589 " {7, 5, \"!!\" }\n" 18590 "};\n", 18591 Style); 18592 18593 verifyFormat("struct test demo[3] = {\n" 18594 " {int{56}, 23, \"hello\"},\n" 18595 " {int{-1}, 93463, \"world\"},\n" 18596 " {int{7}, 5, \"!!\" }\n" 18597 "};\n", 18598 Style); 18599 verifyFormat("struct test demo[] = {\n" 18600 " {56, 23, \"hello\"},\n" 18601 " {-1, 93463, \"world\"},\n" 18602 " {7, 5, \"!!\" },\n" 18603 "};\n", 18604 Style); 18605 verifyFormat("test demo[] = {\n" 18606 " {56, 23, \"hello\"},\n" 18607 " {-1, 93463, \"world\"},\n" 18608 " {7, 5, \"!!\" },\n" 18609 "};\n", 18610 Style); 18611 verifyFormat("demo = std::array<struct test, 3>{\n" 18612 " test{56, 23, \"hello\"},\n" 18613 " test{-1, 93463, \"world\"},\n" 18614 " test{7, 5, \"!!\" },\n" 18615 "};\n", 18616 Style); 18617 verifyFormat("test demo[] = {\n" 18618 " {56, 23, \"hello\"},\n" 18619 "#if X\n" 18620 " {-1, 93463, \"world\"},\n" 18621 "#endif\n" 18622 " {7, 5, \"!!\" }\n" 18623 "};\n", 18624 Style); 18625 verifyFormat( 18626 "test demo[] = {\n" 18627 " {7, 23,\n" 18628 " \"hello world i am a very long line that really, in any\"\n" 18629 " \"just world, ought to be split over multiple lines\"},\n" 18630 " {-1, 93463, \"world\" },\n" 18631 " {56, 5, \"!!\" }\n" 18632 "};\n", 18633 Style); 18634 18635 verifyFormat("return GradForUnaryCwise(g, {\n" 18636 " {{\"sign\"}, \"Sign\", {\"x\", " 18637 "\"dy\"} },\n" 18638 " {{\"dx\"}, \"Mul\", " 18639 "{\"dy\", \"sign\"}},\n" 18640 "});\n", 18641 Style); 18642 18643 Style.ColumnLimit = 0; 18644 EXPECT_EQ( 18645 "test demo[] = {\n" 18646 " {56, 23, \"hello world i am a very long line that really, in any " 18647 "just world, ought to be split over multiple lines\"},\n" 18648 " {-1, 93463, \"world\" " 18649 " },\n" 18650 " {7, 5, \"!!\" " 18651 " },\n" 18652 "};", 18653 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18654 "that really, in any just world, ought to be split over multiple " 18655 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18656 Style)); 18657 18658 Style.ColumnLimit = 80; 18659 verifyFormat("test demo[] = {\n" 18660 " {56, 23, /* a comment */ \"hello\"},\n" 18661 " {-1, 93463, \"world\" },\n" 18662 " {7, 5, \"!!\" }\n" 18663 "};\n", 18664 Style); 18665 18666 verifyFormat("test demo[] = {\n" 18667 " {56, 23, \"hello\" },\n" 18668 " {-1, 93463, \"world\" /* comment here */},\n" 18669 " {7, 5, \"!!\" }\n" 18670 "};\n", 18671 Style); 18672 18673 verifyFormat("test demo[] = {\n" 18674 " {56, /* a comment */ 23, \"hello\"},\n" 18675 " {-1, 93463, \"world\"},\n" 18676 " {7, 5, \"!!\" }\n" 18677 "};\n", 18678 Style); 18679 18680 Style.ColumnLimit = 20; 18681 EXPECT_EQ( 18682 "demo = std::array<\n" 18683 " struct test, 3>{\n" 18684 " test{\n" 18685 " 56, 23,\n" 18686 " \"hello \"\n" 18687 " \"world i \"\n" 18688 " \"am a very \"\n" 18689 " \"long line \"\n" 18690 " \"that \"\n" 18691 " \"really, \"\n" 18692 " \"in any \"\n" 18693 " \"just \"\n" 18694 " \"world, \"\n" 18695 " \"ought to \"\n" 18696 " \"be split \"\n" 18697 " \"over \"\n" 18698 " \"multiple \"\n" 18699 " \"lines\"},\n" 18700 " test{-1, 93463,\n" 18701 " \"world\"},\n" 18702 " test{7, 5,\n" 18703 " \"!!\" },\n" 18704 "};", 18705 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18706 "i am a very long line that really, in any just world, ought " 18707 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18708 "test{7, 5, \"!!\"},};", 18709 Style)); 18710 18711 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18712 Style = getLLVMStyleWithColumns(50); 18713 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18714 verifyFormat("static A x = {\n" 18715 " {{init1, init2, init3, init4},\n" 18716 " {init1, init2, init3, init4}}\n" 18717 "};", 18718 Style); 18719 Style.ColumnLimit = 100; 18720 EXPECT_EQ( 18721 "test demo[] = {\n" 18722 " {56, 23,\n" 18723 " \"hello world i am a very long line that really, in any just world" 18724 ", ought to be split over \"\n" 18725 " \"multiple lines\" },\n" 18726 " {-1, 93463, \"world\"},\n" 18727 " {7, 5, \"!!\" },\n" 18728 "};", 18729 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18730 "that really, in any just world, ought to be split over multiple " 18731 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18732 Style)); 18733 } 18734 18735 TEST_F(FormatTest, UnderstandsPragmas) { 18736 verifyFormat("#pragma omp reduction(| : var)"); 18737 verifyFormat("#pragma omp reduction(+ : var)"); 18738 18739 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 18740 "(including parentheses).", 18741 format("#pragma mark Any non-hyphenated or hyphenated string " 18742 "(including parentheses).")); 18743 } 18744 18745 TEST_F(FormatTest, UnderstandPragmaOption) { 18746 verifyFormat("#pragma option -C -A"); 18747 18748 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 18749 } 18750 18751 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 18752 FormatStyle Style = getLLVMStyleWithColumns(20); 18753 18754 // See PR41213 18755 EXPECT_EQ("/*\n" 18756 " *\t9012345\n" 18757 " * /8901\n" 18758 " */", 18759 format("/*\n" 18760 " *\t9012345 /8901\n" 18761 " */", 18762 Style)); 18763 EXPECT_EQ("/*\n" 18764 " *345678\n" 18765 " *\t/8901\n" 18766 " */", 18767 format("/*\n" 18768 " *345678\t/8901\n" 18769 " */", 18770 Style)); 18771 18772 verifyFormat("int a; // the\n" 18773 " // comment", 18774 Style); 18775 EXPECT_EQ("int a; /* first line\n" 18776 " * second\n" 18777 " * line third\n" 18778 " * line\n" 18779 " */", 18780 format("int a; /* first line\n" 18781 " * second\n" 18782 " * line third\n" 18783 " * line\n" 18784 " */", 18785 Style)); 18786 EXPECT_EQ("int a; // first line\n" 18787 " // second\n" 18788 " // line third\n" 18789 " // line", 18790 format("int a; // first line\n" 18791 " // second line\n" 18792 " // third line", 18793 Style)); 18794 18795 Style.PenaltyExcessCharacter = 90; 18796 verifyFormat("int a; // the comment", Style); 18797 EXPECT_EQ("int a; // the comment\n" 18798 " // aaa", 18799 format("int a; // the comment aaa", Style)); 18800 EXPECT_EQ("int a; /* first line\n" 18801 " * second line\n" 18802 " * third line\n" 18803 " */", 18804 format("int a; /* first line\n" 18805 " * second line\n" 18806 " * third line\n" 18807 " */", 18808 Style)); 18809 EXPECT_EQ("int a; // first line\n" 18810 " // second line\n" 18811 " // third line", 18812 format("int a; // first line\n" 18813 " // second line\n" 18814 " // third line", 18815 Style)); 18816 // FIXME: Investigate why this is not getting the same layout as the test 18817 // above. 18818 EXPECT_EQ("int a; /* first line\n" 18819 " * second line\n" 18820 " * third line\n" 18821 " */", 18822 format("int a; /* first line second line third line" 18823 "\n*/", 18824 Style)); 18825 18826 EXPECT_EQ("// foo bar baz bazfoo\n" 18827 "// foo bar foo bar\n", 18828 format("// foo bar baz bazfoo\n" 18829 "// foo bar foo bar\n", 18830 Style)); 18831 EXPECT_EQ("// foo bar baz bazfoo\n" 18832 "// foo bar foo bar\n", 18833 format("// foo bar baz bazfoo\n" 18834 "// foo bar foo bar\n", 18835 Style)); 18836 18837 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18838 // next one. 18839 EXPECT_EQ("// foo bar baz bazfoo\n" 18840 "// bar foo bar\n", 18841 format("// foo bar baz bazfoo bar\n" 18842 "// foo bar\n", 18843 Style)); 18844 18845 EXPECT_EQ("// foo bar baz bazfoo\n" 18846 "// foo bar baz bazfoo\n" 18847 "// bar foo bar\n", 18848 format("// foo bar baz bazfoo\n" 18849 "// foo bar baz bazfoo bar\n" 18850 "// foo bar\n", 18851 Style)); 18852 18853 EXPECT_EQ("// foo bar baz bazfoo\n" 18854 "// foo bar baz bazfoo\n" 18855 "// bar foo bar\n", 18856 format("// foo bar baz bazfoo\n" 18857 "// foo bar baz bazfoo bar\n" 18858 "// foo bar\n", 18859 Style)); 18860 18861 // Make sure we do not keep protruding characters if strict mode reflow is 18862 // cheaper than keeping protruding characters. 18863 Style.ColumnLimit = 21; 18864 EXPECT_EQ( 18865 "// foo foo foo foo\n" 18866 "// foo foo foo foo\n" 18867 "// foo foo foo foo\n", 18868 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18869 18870 EXPECT_EQ("int a = /* long block\n" 18871 " comment */\n" 18872 " 42;", 18873 format("int a = /* long block comment */ 42;", Style)); 18874 } 18875 18876 TEST_F(FormatTest, BreakPenaltyAfterLParen) { 18877 FormatStyle Style = getLLVMStyle(); 18878 Style.ColumnLimit = 8; 18879 Style.PenaltyExcessCharacter = 15; 18880 verifyFormat("int foo(\n" 18881 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 18882 Style); 18883 Style.PenaltyBreakOpenParenthesis = 200; 18884 EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", 18885 format("int foo(\n" 18886 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 18887 Style)); 18888 } 18889 18890 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { 18891 FormatStyle Style = getLLVMStyle(); 18892 Style.ColumnLimit = 5; 18893 Style.PenaltyExcessCharacter = 150; 18894 verifyFormat("foo((\n" 18895 " int)aaaaaaaaaaaaaaaaaaaaaaaa);", 18896 18897 Style); 18898 Style.PenaltyBreakOpenParenthesis = 100000; 18899 EXPECT_EQ("foo((int)\n" 18900 " aaaaaaaaaaaaaaaaaaaaaaaa);", 18901 format("foo((\n" 18902 "int)aaaaaaaaaaaaaaaaaaaaaaaa);", 18903 Style)); 18904 } 18905 18906 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { 18907 FormatStyle Style = getLLVMStyle(); 18908 Style.ColumnLimit = 4; 18909 Style.PenaltyExcessCharacter = 100; 18910 verifyFormat("for (\n" 18911 " int iiiiiiiiiiiiiiiii =\n" 18912 " 0;\n" 18913 " iiiiiiiiiiiiiiiii <\n" 18914 " 2;\n" 18915 " iiiiiiiiiiiiiiiii++) {\n" 18916 "}", 18917 18918 Style); 18919 Style.PenaltyBreakOpenParenthesis = 1250; 18920 EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n" 18921 " 0;\n" 18922 " iiiiiiiiiiiiiiiii <\n" 18923 " 2;\n" 18924 " iiiiiiiiiiiiiiiii++) {\n" 18925 "}", 18926 format("for (\n" 18927 " int iiiiiiiiiiiiiiiii =\n" 18928 " 0;\n" 18929 " iiiiiiiiiiiiiiiii <\n" 18930 " 2;\n" 18931 " iiiiiiiiiiiiiiiii++) {\n" 18932 "}", 18933 Style)); 18934 } 18935 18936 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18937 for (size_t i = 1; i < Styles.size(); ++i) \ 18938 EXPECT_EQ(Styles[0], Styles[i]) \ 18939 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18940 18941 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18942 SmallVector<FormatStyle, 3> Styles; 18943 Styles.resize(3); 18944 18945 Styles[0] = getLLVMStyle(); 18946 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18947 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18948 EXPECT_ALL_STYLES_EQUAL(Styles); 18949 18950 Styles[0] = getGoogleStyle(); 18951 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18952 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18953 EXPECT_ALL_STYLES_EQUAL(Styles); 18954 18955 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18956 EXPECT_TRUE( 18957 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18958 EXPECT_TRUE( 18959 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18960 EXPECT_ALL_STYLES_EQUAL(Styles); 18961 18962 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18963 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18964 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18965 EXPECT_ALL_STYLES_EQUAL(Styles); 18966 18967 Styles[0] = getMozillaStyle(); 18968 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18969 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18970 EXPECT_ALL_STYLES_EQUAL(Styles); 18971 18972 Styles[0] = getWebKitStyle(); 18973 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18974 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18975 EXPECT_ALL_STYLES_EQUAL(Styles); 18976 18977 Styles[0] = getGNUStyle(); 18978 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18979 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18980 EXPECT_ALL_STYLES_EQUAL(Styles); 18981 18982 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18983 } 18984 18985 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18986 SmallVector<FormatStyle, 8> Styles; 18987 Styles.resize(2); 18988 18989 Styles[0] = getGoogleStyle(); 18990 Styles[1] = getLLVMStyle(); 18991 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18992 EXPECT_ALL_STYLES_EQUAL(Styles); 18993 18994 Styles.resize(5); 18995 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18996 Styles[1] = getLLVMStyle(); 18997 Styles[1].Language = FormatStyle::LK_JavaScript; 18998 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18999 19000 Styles[2] = getLLVMStyle(); 19001 Styles[2].Language = FormatStyle::LK_JavaScript; 19002 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 19003 "BasedOnStyle: Google", 19004 &Styles[2]) 19005 .value()); 19006 19007 Styles[3] = getLLVMStyle(); 19008 Styles[3].Language = FormatStyle::LK_JavaScript; 19009 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 19010 "Language: JavaScript", 19011 &Styles[3]) 19012 .value()); 19013 19014 Styles[4] = getLLVMStyle(); 19015 Styles[4].Language = FormatStyle::LK_JavaScript; 19016 EXPECT_EQ(0, parseConfiguration("---\n" 19017 "BasedOnStyle: LLVM\n" 19018 "IndentWidth: 123\n" 19019 "---\n" 19020 "BasedOnStyle: Google\n" 19021 "Language: JavaScript", 19022 &Styles[4]) 19023 .value()); 19024 EXPECT_ALL_STYLES_EQUAL(Styles); 19025 } 19026 19027 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 19028 Style.FIELD = false; \ 19029 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 19030 EXPECT_TRUE(Style.FIELD); \ 19031 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 19032 EXPECT_FALSE(Style.FIELD); 19033 19034 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 19035 19036 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 19037 Style.STRUCT.FIELD = false; \ 19038 EXPECT_EQ(0, \ 19039 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 19040 .value()); \ 19041 EXPECT_TRUE(Style.STRUCT.FIELD); \ 19042 EXPECT_EQ(0, \ 19043 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 19044 .value()); \ 19045 EXPECT_FALSE(Style.STRUCT.FIELD); 19046 19047 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 19048 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 19049 19050 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 19051 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 19052 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 19053 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 19054 19055 TEST_F(FormatTest, ParsesConfigurationBools) { 19056 FormatStyle Style = {}; 19057 Style.Language = FormatStyle::LK_Cpp; 19058 CHECK_PARSE_BOOL(AlignTrailingComments); 19059 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 19060 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 19061 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 19062 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 19063 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 19064 CHECK_PARSE_BOOL(BinPackArguments); 19065 CHECK_PARSE_BOOL(BinPackParameters); 19066 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 19067 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 19068 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 19069 CHECK_PARSE_BOOL(BreakStringLiterals); 19070 CHECK_PARSE_BOOL(CompactNamespaces); 19071 CHECK_PARSE_BOOL(DeriveLineEnding); 19072 CHECK_PARSE_BOOL(DerivePointerAlignment); 19073 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 19074 CHECK_PARSE_BOOL(DisableFormat); 19075 CHECK_PARSE_BOOL(IndentAccessModifiers); 19076 CHECK_PARSE_BOOL(IndentCaseLabels); 19077 CHECK_PARSE_BOOL(IndentCaseBlocks); 19078 CHECK_PARSE_BOOL(IndentGotoLabels); 19079 CHECK_PARSE_BOOL(IndentRequires); 19080 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 19081 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 19082 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 19083 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 19084 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 19085 CHECK_PARSE_BOOL(ReflowComments); 19086 CHECK_PARSE_BOOL(RemoveBracesLLVM); 19087 CHECK_PARSE_BOOL(SortUsingDeclarations); 19088 CHECK_PARSE_BOOL(SpacesInParentheses); 19089 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 19090 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 19091 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 19092 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 19093 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 19094 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 19095 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 19096 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 19097 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 19098 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 19099 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 19100 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 19101 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 19102 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 19103 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 19104 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 19105 CHECK_PARSE_BOOL(UseCRLF); 19106 19107 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 19108 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 19109 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 19110 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 19111 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 19112 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 19113 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 19114 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 19115 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 19116 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 19117 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 19118 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 19119 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 19120 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 19121 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 19122 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 19123 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 19124 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements); 19125 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros); 19126 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19127 AfterFunctionDeclarationName); 19128 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19129 AfterFunctionDefinitionName); 19130 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros); 19131 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator); 19132 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses); 19133 } 19134 19135 #undef CHECK_PARSE_BOOL 19136 19137 TEST_F(FormatTest, ParsesConfiguration) { 19138 FormatStyle Style = {}; 19139 Style.Language = FormatStyle::LK_Cpp; 19140 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 19141 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 19142 ConstructorInitializerIndentWidth, 1234u); 19143 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 19144 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 19145 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 19146 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 19147 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 19148 PenaltyBreakBeforeFirstCallParameter, 1234u); 19149 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 19150 PenaltyBreakTemplateDeclaration, 1234u); 19151 CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis, 19152 1234u); 19153 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 19154 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 19155 PenaltyReturnTypeOnItsOwnLine, 1234u); 19156 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 19157 SpacesBeforeTrailingComments, 1234u); 19158 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 19159 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 19160 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 19161 19162 Style.QualifierAlignment = FormatStyle::QAS_Right; 19163 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 19164 FormatStyle::QAS_Leave); 19165 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 19166 FormatStyle::QAS_Right); 19167 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 19168 FormatStyle::QAS_Left); 19169 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 19170 FormatStyle::QAS_Custom); 19171 19172 Style.QualifierOrder.clear(); 19173 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 19174 std::vector<std::string>({"const", "volatile", "type"})); 19175 Style.QualifierOrder.clear(); 19176 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 19177 std::vector<std::string>({"const", "type"})); 19178 Style.QualifierOrder.clear(); 19179 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 19180 std::vector<std::string>({"volatile", "type"})); 19181 19182 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 19183 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 19184 FormatStyle::ACS_None); 19185 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 19186 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 19187 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 19188 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 19189 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 19190 AlignConsecutiveAssignments, 19191 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19192 // For backwards compability, false / true should still parse 19193 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 19194 FormatStyle::ACS_None); 19195 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 19196 FormatStyle::ACS_Consecutive); 19197 19198 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 19199 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 19200 FormatStyle::ACS_None); 19201 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 19202 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 19203 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 19204 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 19205 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 19206 AlignConsecutiveBitFields, 19207 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19208 // For backwards compability, false / true should still parse 19209 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 19210 FormatStyle::ACS_None); 19211 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 19212 FormatStyle::ACS_Consecutive); 19213 19214 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 19215 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 19216 FormatStyle::ACS_None); 19217 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 19218 FormatStyle::ACS_Consecutive); 19219 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 19220 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 19221 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 19222 AlignConsecutiveMacros, 19223 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19224 // For backwards compability, false / true should still parse 19225 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 19226 FormatStyle::ACS_None); 19227 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 19228 FormatStyle::ACS_Consecutive); 19229 19230 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 19231 CHECK_PARSE("AlignConsecutiveDeclarations: None", 19232 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 19233 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 19234 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 19235 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 19236 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 19237 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 19238 AlignConsecutiveDeclarations, 19239 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19240 // For backwards compability, false / true should still parse 19241 CHECK_PARSE("AlignConsecutiveDeclarations: false", 19242 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 19243 CHECK_PARSE("AlignConsecutiveDeclarations: true", 19244 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 19245 19246 Style.PointerAlignment = FormatStyle::PAS_Middle; 19247 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 19248 FormatStyle::PAS_Left); 19249 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 19250 FormatStyle::PAS_Right); 19251 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 19252 FormatStyle::PAS_Middle); 19253 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 19254 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 19255 FormatStyle::RAS_Pointer); 19256 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 19257 FormatStyle::RAS_Left); 19258 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 19259 FormatStyle::RAS_Right); 19260 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 19261 FormatStyle::RAS_Middle); 19262 // For backward compatibility: 19263 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 19264 FormatStyle::PAS_Left); 19265 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 19266 FormatStyle::PAS_Right); 19267 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 19268 FormatStyle::PAS_Middle); 19269 19270 Style.Standard = FormatStyle::LS_Auto; 19271 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 19272 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 19273 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 19274 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 19275 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 19276 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 19277 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 19278 // Legacy aliases: 19279 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 19280 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 19281 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 19282 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 19283 19284 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 19285 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 19286 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 19287 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 19288 FormatStyle::BOS_None); 19289 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 19290 FormatStyle::BOS_All); 19291 // For backward compatibility: 19292 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 19293 FormatStyle::BOS_None); 19294 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 19295 FormatStyle::BOS_All); 19296 19297 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 19298 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 19299 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 19300 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 19301 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 19302 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 19303 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 19304 // For backward compatibility: 19305 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 19306 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 19307 19308 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 19309 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 19310 FormatStyle::BILS_AfterComma); 19311 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 19312 FormatStyle::BILS_BeforeComma); 19313 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 19314 FormatStyle::BILS_AfterColon); 19315 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 19316 FormatStyle::BILS_BeforeColon); 19317 // For backward compatibility: 19318 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 19319 FormatStyle::BILS_BeforeComma); 19320 19321 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19322 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 19323 FormatStyle::PCIS_Never); 19324 CHECK_PARSE("PackConstructorInitializers: BinPack", 19325 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19326 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 19327 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19328 CHECK_PARSE("PackConstructorInitializers: NextLine", 19329 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19330 // For backward compatibility: 19331 CHECK_PARSE("BasedOnStyle: Google\n" 19332 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19333 "AllowAllConstructorInitializersOnNextLine: false", 19334 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19335 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19336 CHECK_PARSE("BasedOnStyle: Google\n" 19337 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 19338 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19339 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19340 "AllowAllConstructorInitializersOnNextLine: true", 19341 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19342 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19343 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19344 "AllowAllConstructorInitializersOnNextLine: false", 19345 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19346 19347 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 19348 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 19349 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 19350 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 19351 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 19352 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 19353 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 19354 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 19355 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 19356 19357 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19358 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 19359 FormatStyle::BAS_Align); 19360 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 19361 FormatStyle::BAS_DontAlign); 19362 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 19363 FormatStyle::BAS_AlwaysBreak); 19364 CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket, 19365 FormatStyle::BAS_BlockIndent); 19366 // For backward compatibility: 19367 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 19368 FormatStyle::BAS_DontAlign); 19369 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 19370 FormatStyle::BAS_Align); 19371 19372 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 19373 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 19374 FormatStyle::ENAS_DontAlign); 19375 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 19376 FormatStyle::ENAS_Left); 19377 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 19378 FormatStyle::ENAS_Right); 19379 // For backward compatibility: 19380 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 19381 FormatStyle::ENAS_Left); 19382 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 19383 FormatStyle::ENAS_Right); 19384 19385 Style.AlignOperands = FormatStyle::OAS_Align; 19386 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 19387 FormatStyle::OAS_DontAlign); 19388 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 19389 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 19390 FormatStyle::OAS_AlignAfterOperator); 19391 // For backward compatibility: 19392 CHECK_PARSE("AlignOperands: false", AlignOperands, 19393 FormatStyle::OAS_DontAlign); 19394 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 19395 19396 Style.UseTab = FormatStyle::UT_ForIndentation; 19397 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 19398 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 19399 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 19400 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 19401 FormatStyle::UT_ForContinuationAndIndentation); 19402 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 19403 FormatStyle::UT_AlignWithSpaces); 19404 // For backward compatibility: 19405 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 19406 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 19407 19408 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 19409 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 19410 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19411 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 19412 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 19413 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 19414 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19415 // For backward compatibility: 19416 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 19417 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19418 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 19419 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19420 19421 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 19422 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 19423 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19424 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 19425 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 19426 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 19427 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 19428 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 19429 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19430 // For backward compatibility: 19431 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 19432 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19433 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 19434 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19435 19436 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 19437 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 19438 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 19439 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 19440 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 19441 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 19442 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 19443 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 19444 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 19445 19446 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 19447 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 19448 FormatStyle::SBPO_Never); 19449 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 19450 FormatStyle::SBPO_Always); 19451 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 19452 FormatStyle::SBPO_ControlStatements); 19453 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 19454 SpaceBeforeParens, 19455 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19456 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 19457 FormatStyle::SBPO_NonEmptyParentheses); 19458 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 19459 FormatStyle::SBPO_Custom); 19460 // For backward compatibility: 19461 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 19462 FormatStyle::SBPO_Never); 19463 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 19464 FormatStyle::SBPO_ControlStatements); 19465 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 19466 SpaceBeforeParens, 19467 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19468 19469 Style.ColumnLimit = 123; 19470 FormatStyle BaseStyle = getLLVMStyle(); 19471 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 19472 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 19473 19474 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 19475 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 19476 FormatStyle::BS_Attach); 19477 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 19478 FormatStyle::BS_Linux); 19479 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 19480 FormatStyle::BS_Mozilla); 19481 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 19482 FormatStyle::BS_Stroustrup); 19483 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 19484 FormatStyle::BS_Allman); 19485 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 19486 FormatStyle::BS_Whitesmiths); 19487 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 19488 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 19489 FormatStyle::BS_WebKit); 19490 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 19491 FormatStyle::BS_Custom); 19492 19493 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 19494 CHECK_PARSE("BraceWrapping:\n" 19495 " AfterControlStatement: MultiLine", 19496 BraceWrapping.AfterControlStatement, 19497 FormatStyle::BWACS_MultiLine); 19498 CHECK_PARSE("BraceWrapping:\n" 19499 " AfterControlStatement: Always", 19500 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19501 CHECK_PARSE("BraceWrapping:\n" 19502 " AfterControlStatement: Never", 19503 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19504 // For backward compatibility: 19505 CHECK_PARSE("BraceWrapping:\n" 19506 " AfterControlStatement: true", 19507 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19508 CHECK_PARSE("BraceWrapping:\n" 19509 " AfterControlStatement: false", 19510 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19511 19512 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 19513 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 19514 FormatStyle::RTBS_None); 19515 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 19516 FormatStyle::RTBS_All); 19517 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 19518 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 19519 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 19520 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 19521 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 19522 AlwaysBreakAfterReturnType, 19523 FormatStyle::RTBS_TopLevelDefinitions); 19524 19525 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 19526 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 19527 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 19528 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 19529 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19530 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 19531 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19532 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 19533 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19534 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 19535 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19536 19537 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 19538 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 19539 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 19540 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 19541 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 19542 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 19543 AlwaysBreakAfterDefinitionReturnType, 19544 FormatStyle::DRTBS_TopLevel); 19545 19546 Style.NamespaceIndentation = FormatStyle::NI_All; 19547 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 19548 FormatStyle::NI_None); 19549 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 19550 FormatStyle::NI_Inner); 19551 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 19552 FormatStyle::NI_All); 19553 19554 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 19555 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 19556 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19557 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 19558 AllowShortIfStatementsOnASingleLine, 19559 FormatStyle::SIS_WithoutElse); 19560 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 19561 AllowShortIfStatementsOnASingleLine, 19562 FormatStyle::SIS_OnlyFirstIf); 19563 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 19564 AllowShortIfStatementsOnASingleLine, 19565 FormatStyle::SIS_AllIfsAndElse); 19566 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 19567 AllowShortIfStatementsOnASingleLine, 19568 FormatStyle::SIS_OnlyFirstIf); 19569 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 19570 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19571 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 19572 AllowShortIfStatementsOnASingleLine, 19573 FormatStyle::SIS_WithoutElse); 19574 19575 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 19576 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 19577 FormatStyle::IEBS_AfterExternBlock); 19578 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 19579 FormatStyle::IEBS_Indent); 19580 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 19581 FormatStyle::IEBS_NoIndent); 19582 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 19583 FormatStyle::IEBS_Indent); 19584 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 19585 FormatStyle::IEBS_NoIndent); 19586 19587 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 19588 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 19589 FormatStyle::BFCS_Both); 19590 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 19591 FormatStyle::BFCS_None); 19592 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 19593 FormatStyle::BFCS_Before); 19594 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 19595 FormatStyle::BFCS_After); 19596 19597 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 19598 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 19599 FormatStyle::SJSIO_After); 19600 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 19601 FormatStyle::SJSIO_Before); 19602 19603 // FIXME: This is required because parsing a configuration simply overwrites 19604 // the first N elements of the list instead of resetting it. 19605 Style.ForEachMacros.clear(); 19606 std::vector<std::string> BoostForeach; 19607 BoostForeach.push_back("BOOST_FOREACH"); 19608 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 19609 std::vector<std::string> BoostAndQForeach; 19610 BoostAndQForeach.push_back("BOOST_FOREACH"); 19611 BoostAndQForeach.push_back("Q_FOREACH"); 19612 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 19613 BoostAndQForeach); 19614 19615 Style.IfMacros.clear(); 19616 std::vector<std::string> CustomIfs; 19617 CustomIfs.push_back("MYIF"); 19618 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 19619 19620 Style.AttributeMacros.clear(); 19621 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 19622 std::vector<std::string>{"__capability"}); 19623 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 19624 std::vector<std::string>({"attr1", "attr2"})); 19625 19626 Style.StatementAttributeLikeMacros.clear(); 19627 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 19628 StatementAttributeLikeMacros, 19629 std::vector<std::string>({"emit", "Q_EMIT"})); 19630 19631 Style.StatementMacros.clear(); 19632 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 19633 std::vector<std::string>{"QUNUSED"}); 19634 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 19635 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 19636 19637 Style.NamespaceMacros.clear(); 19638 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 19639 std::vector<std::string>{"TESTSUITE"}); 19640 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 19641 std::vector<std::string>({"TESTSUITE", "SUITE"})); 19642 19643 Style.WhitespaceSensitiveMacros.clear(); 19644 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 19645 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19646 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 19647 WhitespaceSensitiveMacros, 19648 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19649 Style.WhitespaceSensitiveMacros.clear(); 19650 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 19651 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19652 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 19653 WhitespaceSensitiveMacros, 19654 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19655 19656 Style.IncludeStyle.IncludeCategories.clear(); 19657 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 19658 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 19659 CHECK_PARSE("IncludeCategories:\n" 19660 " - Regex: abc/.*\n" 19661 " Priority: 2\n" 19662 " - Regex: .*\n" 19663 " Priority: 1\n" 19664 " CaseSensitive: true\n", 19665 IncludeStyle.IncludeCategories, ExpectedCategories); 19666 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 19667 "abc$"); 19668 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 19669 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 19670 19671 Style.SortIncludes = FormatStyle::SI_Never; 19672 CHECK_PARSE("SortIncludes: true", SortIncludes, 19673 FormatStyle::SI_CaseSensitive); 19674 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 19675 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 19676 FormatStyle::SI_CaseInsensitive); 19677 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 19678 FormatStyle::SI_CaseSensitive); 19679 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 19680 19681 Style.RawStringFormats.clear(); 19682 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 19683 { 19684 FormatStyle::LK_TextProto, 19685 {"pb", "proto"}, 19686 {"PARSE_TEXT_PROTO"}, 19687 /*CanonicalDelimiter=*/"", 19688 "llvm", 19689 }, 19690 { 19691 FormatStyle::LK_Cpp, 19692 {"cc", "cpp"}, 19693 {"C_CODEBLOCK", "CPPEVAL"}, 19694 /*CanonicalDelimiter=*/"cc", 19695 /*BasedOnStyle=*/"", 19696 }, 19697 }; 19698 19699 CHECK_PARSE("RawStringFormats:\n" 19700 " - Language: TextProto\n" 19701 " Delimiters:\n" 19702 " - 'pb'\n" 19703 " - 'proto'\n" 19704 " EnclosingFunctions:\n" 19705 " - 'PARSE_TEXT_PROTO'\n" 19706 " BasedOnStyle: llvm\n" 19707 " - Language: Cpp\n" 19708 " Delimiters:\n" 19709 " - 'cc'\n" 19710 " - 'cpp'\n" 19711 " EnclosingFunctions:\n" 19712 " - 'C_CODEBLOCK'\n" 19713 " - 'CPPEVAL'\n" 19714 " CanonicalDelimiter: 'cc'", 19715 RawStringFormats, ExpectedRawStringFormats); 19716 19717 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19718 " Minimum: 0\n" 19719 " Maximum: 0", 19720 SpacesInLineCommentPrefix.Minimum, 0u); 19721 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 19722 Style.SpacesInLineCommentPrefix.Minimum = 1; 19723 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19724 " Minimum: 2", 19725 SpacesInLineCommentPrefix.Minimum, 0u); 19726 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19727 " Maximum: -1", 19728 SpacesInLineCommentPrefix.Maximum, -1u); 19729 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19730 " Minimum: 2", 19731 SpacesInLineCommentPrefix.Minimum, 2u); 19732 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19733 " Maximum: 1", 19734 SpacesInLineCommentPrefix.Maximum, 1u); 19735 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 19736 19737 Style.SpacesInAngles = FormatStyle::SIAS_Always; 19738 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 19739 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 19740 FormatStyle::SIAS_Always); 19741 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 19742 // For backward compatibility: 19743 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 19744 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 19745 } 19746 19747 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 19748 FormatStyle Style = {}; 19749 Style.Language = FormatStyle::LK_Cpp; 19750 CHECK_PARSE("Language: Cpp\n" 19751 "IndentWidth: 12", 19752 IndentWidth, 12u); 19753 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 19754 "IndentWidth: 34", 19755 &Style), 19756 ParseError::Unsuitable); 19757 FormatStyle BinPackedTCS = {}; 19758 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 19759 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 19760 "InsertTrailingCommas: Wrapped", 19761 &BinPackedTCS), 19762 ParseError::BinPackTrailingCommaConflict); 19763 EXPECT_EQ(12u, Style.IndentWidth); 19764 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19765 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19766 19767 Style.Language = FormatStyle::LK_JavaScript; 19768 CHECK_PARSE("Language: JavaScript\n" 19769 "IndentWidth: 12", 19770 IndentWidth, 12u); 19771 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 19772 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 19773 "IndentWidth: 34", 19774 &Style), 19775 ParseError::Unsuitable); 19776 EXPECT_EQ(23u, Style.IndentWidth); 19777 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19778 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19779 19780 CHECK_PARSE("BasedOnStyle: LLVM\n" 19781 "IndentWidth: 67", 19782 IndentWidth, 67u); 19783 19784 CHECK_PARSE("---\n" 19785 "Language: JavaScript\n" 19786 "IndentWidth: 12\n" 19787 "---\n" 19788 "Language: Cpp\n" 19789 "IndentWidth: 34\n" 19790 "...\n", 19791 IndentWidth, 12u); 19792 19793 Style.Language = FormatStyle::LK_Cpp; 19794 CHECK_PARSE("---\n" 19795 "Language: JavaScript\n" 19796 "IndentWidth: 12\n" 19797 "---\n" 19798 "Language: Cpp\n" 19799 "IndentWidth: 34\n" 19800 "...\n", 19801 IndentWidth, 34u); 19802 CHECK_PARSE("---\n" 19803 "IndentWidth: 78\n" 19804 "---\n" 19805 "Language: JavaScript\n" 19806 "IndentWidth: 56\n" 19807 "...\n", 19808 IndentWidth, 78u); 19809 19810 Style.ColumnLimit = 123; 19811 Style.IndentWidth = 234; 19812 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 19813 Style.TabWidth = 345; 19814 EXPECT_FALSE(parseConfiguration("---\n" 19815 "IndentWidth: 456\n" 19816 "BreakBeforeBraces: Allman\n" 19817 "---\n" 19818 "Language: JavaScript\n" 19819 "IndentWidth: 111\n" 19820 "TabWidth: 111\n" 19821 "---\n" 19822 "Language: Cpp\n" 19823 "BreakBeforeBraces: Stroustrup\n" 19824 "TabWidth: 789\n" 19825 "...\n", 19826 &Style)); 19827 EXPECT_EQ(123u, Style.ColumnLimit); 19828 EXPECT_EQ(456u, Style.IndentWidth); 19829 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 19830 EXPECT_EQ(789u, Style.TabWidth); 19831 19832 EXPECT_EQ(parseConfiguration("---\n" 19833 "Language: JavaScript\n" 19834 "IndentWidth: 56\n" 19835 "---\n" 19836 "IndentWidth: 78\n" 19837 "...\n", 19838 &Style), 19839 ParseError::Error); 19840 EXPECT_EQ(parseConfiguration("---\n" 19841 "Language: JavaScript\n" 19842 "IndentWidth: 56\n" 19843 "---\n" 19844 "Language: JavaScript\n" 19845 "IndentWidth: 78\n" 19846 "...\n", 19847 &Style), 19848 ParseError::Error); 19849 19850 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19851 } 19852 19853 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 19854 FormatStyle Style = {}; 19855 Style.Language = FormatStyle::LK_JavaScript; 19856 Style.BreakBeforeTernaryOperators = true; 19857 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 19858 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19859 19860 Style.BreakBeforeTernaryOperators = true; 19861 EXPECT_EQ(0, parseConfiguration("---\n" 19862 "BasedOnStyle: Google\n" 19863 "---\n" 19864 "Language: JavaScript\n" 19865 "IndentWidth: 76\n" 19866 "...\n", 19867 &Style) 19868 .value()); 19869 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19870 EXPECT_EQ(76u, Style.IndentWidth); 19871 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19872 } 19873 19874 TEST_F(FormatTest, ConfigurationRoundTripTest) { 19875 FormatStyle Style = getLLVMStyle(); 19876 std::string YAML = configurationAsText(Style); 19877 FormatStyle ParsedStyle = {}; 19878 ParsedStyle.Language = FormatStyle::LK_Cpp; 19879 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 19880 EXPECT_EQ(Style, ParsedStyle); 19881 } 19882 19883 TEST_F(FormatTest, WorksFor8bitEncodings) { 19884 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 19885 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 19886 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 19887 "\"\xef\xee\xf0\xf3...\"", 19888 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 19889 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 19890 "\xef\xee\xf0\xf3...\"", 19891 getLLVMStyleWithColumns(12))); 19892 } 19893 19894 TEST_F(FormatTest, HandlesUTF8BOM) { 19895 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 19896 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 19897 format("\xef\xbb\xbf#include <iostream>")); 19898 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 19899 format("\xef\xbb\xbf\n#include <iostream>")); 19900 } 19901 19902 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 19903 #if !defined(_MSC_VER) 19904 19905 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 19906 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 19907 getLLVMStyleWithColumns(35)); 19908 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 19909 getLLVMStyleWithColumns(31)); 19910 verifyFormat("// Однажды в студёную зимнюю пору...", 19911 getLLVMStyleWithColumns(36)); 19912 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 19913 verifyFormat("/* Однажды в студёную зимнюю пору... */", 19914 getLLVMStyleWithColumns(39)); 19915 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 19916 getLLVMStyleWithColumns(35)); 19917 } 19918 19919 TEST_F(FormatTest, SplitsUTF8Strings) { 19920 // Non-printable characters' width is currently considered to be the length in 19921 // bytes in UTF8. The characters can be displayed in very different manner 19922 // (zero-width, single width with a substitution glyph, expanded to their code 19923 // (e.g. "<8d>"), so there's no single correct way to handle them. 19924 EXPECT_EQ("\"aaaaÄ\"\n" 19925 "\"\xc2\x8d\";", 19926 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19927 EXPECT_EQ("\"aaaaaaaÄ\"\n" 19928 "\"\xc2\x8d\";", 19929 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19930 EXPECT_EQ("\"Однажды, в \"\n" 19931 "\"студёную \"\n" 19932 "\"зимнюю \"\n" 19933 "\"пору,\"", 19934 format("\"Однажды, в студёную зимнюю пору,\"", 19935 getLLVMStyleWithColumns(13))); 19936 EXPECT_EQ( 19937 "\"一 二 三 \"\n" 19938 "\"四 五六 \"\n" 19939 "\"七 八 九 \"\n" 19940 "\"十\"", 19941 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 19942 EXPECT_EQ("\"一\t\"\n" 19943 "\"二 \t\"\n" 19944 "\"三 四 \"\n" 19945 "\"五\t\"\n" 19946 "\"六 \t\"\n" 19947 "\"七 \"\n" 19948 "\"八九十\tqq\"", 19949 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19950 getLLVMStyleWithColumns(11))); 19951 19952 // UTF8 character in an escape sequence. 19953 EXPECT_EQ("\"aaaaaa\"\n" 19954 "\"\\\xC2\x8D\"", 19955 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19956 } 19957 19958 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19959 EXPECT_EQ("const char *sssss =\n" 19960 " \"一二三四五六七八\\\n" 19961 " 九 十\";", 19962 format("const char *sssss = \"一二三四五六七八\\\n" 19963 " 九 十\";", 19964 getLLVMStyleWithColumns(30))); 19965 } 19966 19967 TEST_F(FormatTest, SplitsUTF8LineComments) { 19968 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19969 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19970 EXPECT_EQ("// Я из лесу\n" 19971 "// вышел; был\n" 19972 "// сильный\n" 19973 "// мороз.", 19974 format("// Я из лесу вышел; был сильный мороз.", 19975 getLLVMStyleWithColumns(13))); 19976 EXPECT_EQ("// 一二三\n" 19977 "// 四五六七\n" 19978 "// 八 九\n" 19979 "// 十", 19980 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19981 } 19982 19983 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19984 EXPECT_EQ("/* Гляжу,\n" 19985 " * поднимается\n" 19986 " * медленно в\n" 19987 " * гору\n" 19988 " * Лошадка,\n" 19989 " * везущая\n" 19990 " * хворосту\n" 19991 " * воз. */", 19992 format("/* Гляжу, поднимается медленно в гору\n" 19993 " * Лошадка, везущая хворосту воз. */", 19994 getLLVMStyleWithColumns(13))); 19995 EXPECT_EQ( 19996 "/* 一二三\n" 19997 " * 四五六七\n" 19998 " * 八 九\n" 19999 " * 十 */", 20000 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 20001 EXPECT_EQ("/* \n" 20002 " * \n" 20003 " * - */", 20004 format("/* - */", getLLVMStyleWithColumns(12))); 20005 } 20006 20007 #endif // _MSC_VER 20008 20009 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 20010 FormatStyle Style = getLLVMStyle(); 20011 20012 Style.ConstructorInitializerIndentWidth = 4; 20013 verifyFormat( 20014 "SomeClass::Constructor()\n" 20015 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20016 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20017 Style); 20018 20019 Style.ConstructorInitializerIndentWidth = 2; 20020 verifyFormat( 20021 "SomeClass::Constructor()\n" 20022 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20023 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20024 Style); 20025 20026 Style.ConstructorInitializerIndentWidth = 0; 20027 verifyFormat( 20028 "SomeClass::Constructor()\n" 20029 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20030 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20031 Style); 20032 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 20033 verifyFormat( 20034 "SomeLongTemplateVariableName<\n" 20035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 20036 Style); 20037 verifyFormat("bool smaller = 1 < " 20038 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 20039 " " 20040 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 20041 Style); 20042 20043 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 20044 verifyFormat("SomeClass::Constructor() :\n" 20045 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 20046 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 20047 Style); 20048 } 20049 20050 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 20051 FormatStyle Style = getLLVMStyle(); 20052 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20053 Style.ConstructorInitializerIndentWidth = 4; 20054 verifyFormat("SomeClass::Constructor()\n" 20055 " : a(a)\n" 20056 " , b(b)\n" 20057 " , c(c) {}", 20058 Style); 20059 verifyFormat("SomeClass::Constructor()\n" 20060 " : a(a) {}", 20061 Style); 20062 20063 Style.ColumnLimit = 0; 20064 verifyFormat("SomeClass::Constructor()\n" 20065 " : a(a) {}", 20066 Style); 20067 verifyFormat("SomeClass::Constructor() noexcept\n" 20068 " : a(a) {}", 20069 Style); 20070 verifyFormat("SomeClass::Constructor()\n" 20071 " : a(a)\n" 20072 " , b(b)\n" 20073 " , c(c) {}", 20074 Style); 20075 verifyFormat("SomeClass::Constructor()\n" 20076 " : a(a) {\n" 20077 " foo();\n" 20078 " bar();\n" 20079 "}", 20080 Style); 20081 20082 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 20083 verifyFormat("SomeClass::Constructor()\n" 20084 " : a(a)\n" 20085 " , b(b)\n" 20086 " , c(c) {\n}", 20087 Style); 20088 verifyFormat("SomeClass::Constructor()\n" 20089 " : a(a) {\n}", 20090 Style); 20091 20092 Style.ColumnLimit = 80; 20093 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 20094 Style.ConstructorInitializerIndentWidth = 2; 20095 verifyFormat("SomeClass::Constructor()\n" 20096 " : a(a)\n" 20097 " , b(b)\n" 20098 " , c(c) {}", 20099 Style); 20100 20101 Style.ConstructorInitializerIndentWidth = 0; 20102 verifyFormat("SomeClass::Constructor()\n" 20103 ": a(a)\n" 20104 ", b(b)\n" 20105 ", c(c) {}", 20106 Style); 20107 20108 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 20109 Style.ConstructorInitializerIndentWidth = 4; 20110 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 20111 verifyFormat( 20112 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 20113 Style); 20114 verifyFormat( 20115 "SomeClass::Constructor()\n" 20116 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 20117 Style); 20118 Style.ConstructorInitializerIndentWidth = 4; 20119 Style.ColumnLimit = 60; 20120 verifyFormat("SomeClass::Constructor()\n" 20121 " : aaaaaaaa(aaaaaaaa)\n" 20122 " , aaaaaaaa(aaaaaaaa)\n" 20123 " , aaaaaaaa(aaaaaaaa) {}", 20124 Style); 20125 } 20126 20127 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 20128 FormatStyle Style = getLLVMStyle(); 20129 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20130 Style.ConstructorInitializerIndentWidth = 4; 20131 verifyFormat("SomeClass::Constructor()\n" 20132 " : a{a}\n" 20133 " , b{b} {}", 20134 Style); 20135 verifyFormat("SomeClass::Constructor()\n" 20136 " : a{a}\n" 20137 "#if CONDITION\n" 20138 " , b{b}\n" 20139 "#endif\n" 20140 "{\n}", 20141 Style); 20142 Style.ConstructorInitializerIndentWidth = 2; 20143 verifyFormat("SomeClass::Constructor()\n" 20144 "#if CONDITION\n" 20145 " : a{a}\n" 20146 "#endif\n" 20147 " , b{b}\n" 20148 " , c{c} {\n}", 20149 Style); 20150 Style.ConstructorInitializerIndentWidth = 0; 20151 verifyFormat("SomeClass::Constructor()\n" 20152 ": a{a}\n" 20153 "#ifdef CONDITION\n" 20154 ", b{b}\n" 20155 "#else\n" 20156 ", c{c}\n" 20157 "#endif\n" 20158 ", d{d} {\n}", 20159 Style); 20160 Style.ConstructorInitializerIndentWidth = 4; 20161 verifyFormat("SomeClass::Constructor()\n" 20162 " : a{a}\n" 20163 "#if WINDOWS\n" 20164 "#if DEBUG\n" 20165 " , b{0}\n" 20166 "#else\n" 20167 " , b{1}\n" 20168 "#endif\n" 20169 "#else\n" 20170 "#if DEBUG\n" 20171 " , b{2}\n" 20172 "#else\n" 20173 " , b{3}\n" 20174 "#endif\n" 20175 "#endif\n" 20176 "{\n}", 20177 Style); 20178 verifyFormat("SomeClass::Constructor()\n" 20179 " : a{a}\n" 20180 "#if WINDOWS\n" 20181 " , b{0}\n" 20182 "#if DEBUG\n" 20183 " , c{0}\n" 20184 "#else\n" 20185 " , c{1}\n" 20186 "#endif\n" 20187 "#else\n" 20188 "#if DEBUG\n" 20189 " , c{2}\n" 20190 "#else\n" 20191 " , c{3}\n" 20192 "#endif\n" 20193 " , b{1}\n" 20194 "#endif\n" 20195 "{\n}", 20196 Style); 20197 } 20198 20199 TEST_F(FormatTest, Destructors) { 20200 verifyFormat("void F(int &i) { i.~int(); }"); 20201 verifyFormat("void F(int &i) { i->~int(); }"); 20202 } 20203 20204 TEST_F(FormatTest, FormatsWithWebKitStyle) { 20205 FormatStyle Style = getWebKitStyle(); 20206 20207 // Don't indent in outer namespaces. 20208 verifyFormat("namespace outer {\n" 20209 "int i;\n" 20210 "namespace inner {\n" 20211 " int i;\n" 20212 "} // namespace inner\n" 20213 "} // namespace outer\n" 20214 "namespace other_outer {\n" 20215 "int i;\n" 20216 "}", 20217 Style); 20218 20219 // Don't indent case labels. 20220 verifyFormat("switch (variable) {\n" 20221 "case 1:\n" 20222 "case 2:\n" 20223 " doSomething();\n" 20224 " break;\n" 20225 "default:\n" 20226 " ++variable;\n" 20227 "}", 20228 Style); 20229 20230 // Wrap before binary operators. 20231 EXPECT_EQ("void f()\n" 20232 "{\n" 20233 " if (aaaaaaaaaaaaaaaa\n" 20234 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 20235 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 20236 " return;\n" 20237 "}", 20238 format("void f() {\n" 20239 "if (aaaaaaaaaaaaaaaa\n" 20240 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 20241 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 20242 "return;\n" 20243 "}", 20244 Style)); 20245 20246 // Allow functions on a single line. 20247 verifyFormat("void f() { return; }", Style); 20248 20249 // Allow empty blocks on a single line and insert a space in empty blocks. 20250 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 20251 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 20252 // However, don't merge non-empty short loops. 20253 EXPECT_EQ("while (true) {\n" 20254 " continue;\n" 20255 "}", 20256 format("while (true) { continue; }", Style)); 20257 20258 // Constructor initializers are formatted one per line with the "," on the 20259 // new line. 20260 verifyFormat("Constructor()\n" 20261 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 20262 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 20263 " aaaaaaaaaaaaaa)\n" 20264 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 20265 "{\n" 20266 "}", 20267 Style); 20268 verifyFormat("SomeClass::Constructor()\n" 20269 " : a(a)\n" 20270 "{\n" 20271 "}", 20272 Style); 20273 EXPECT_EQ("SomeClass::Constructor()\n" 20274 " : a(a)\n" 20275 "{\n" 20276 "}", 20277 format("SomeClass::Constructor():a(a){}", Style)); 20278 verifyFormat("SomeClass::Constructor()\n" 20279 " : a(a)\n" 20280 " , b(b)\n" 20281 " , c(c)\n" 20282 "{\n" 20283 "}", 20284 Style); 20285 verifyFormat("SomeClass::Constructor()\n" 20286 " : a(a)\n" 20287 "{\n" 20288 " foo();\n" 20289 " bar();\n" 20290 "}", 20291 Style); 20292 20293 // Access specifiers should be aligned left. 20294 verifyFormat("class C {\n" 20295 "public:\n" 20296 " int i;\n" 20297 "};", 20298 Style); 20299 20300 // Do not align comments. 20301 verifyFormat("int a; // Do not\n" 20302 "double b; // align comments.", 20303 Style); 20304 20305 // Do not align operands. 20306 EXPECT_EQ("ASSERT(aaaa\n" 20307 " || bbbb);", 20308 format("ASSERT ( aaaa\n||bbbb);", Style)); 20309 20310 // Accept input's line breaks. 20311 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 20312 " || bbbbbbbbbbbbbbb) {\n" 20313 " i++;\n" 20314 "}", 20315 format("if (aaaaaaaaaaaaaaa\n" 20316 "|| bbbbbbbbbbbbbbb) { i++; }", 20317 Style)); 20318 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 20319 " i++;\n" 20320 "}", 20321 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 20322 20323 // Don't automatically break all macro definitions (llvm.org/PR17842). 20324 verifyFormat("#define aNumber 10", Style); 20325 // However, generally keep the line breaks that the user authored. 20326 EXPECT_EQ("#define aNumber \\\n" 20327 " 10", 20328 format("#define aNumber \\\n" 20329 " 10", 20330 Style)); 20331 20332 // Keep empty and one-element array literals on a single line. 20333 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 20334 " copyItems:YES];", 20335 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 20336 "copyItems:YES];", 20337 Style)); 20338 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 20339 " copyItems:YES];", 20340 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 20341 " copyItems:YES];", 20342 Style)); 20343 // FIXME: This does not seem right, there should be more indentation before 20344 // the array literal's entries. Nested blocks have the same problem. 20345 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20346 " @\"a\",\n" 20347 " @\"a\"\n" 20348 "]\n" 20349 " copyItems:YES];", 20350 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20351 " @\"a\",\n" 20352 " @\"a\"\n" 20353 " ]\n" 20354 " copyItems:YES];", 20355 Style)); 20356 EXPECT_EQ( 20357 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20358 " copyItems:YES];", 20359 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20360 " copyItems:YES];", 20361 Style)); 20362 20363 verifyFormat("[self.a b:c c:d];", Style); 20364 EXPECT_EQ("[self.a b:c\n" 20365 " c:d];", 20366 format("[self.a b:c\n" 20367 "c:d];", 20368 Style)); 20369 } 20370 20371 TEST_F(FormatTest, FormatsLambdas) { 20372 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 20373 verifyFormat( 20374 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 20375 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 20376 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 20377 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 20378 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 20379 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 20380 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 20381 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 20382 verifyFormat("int x = f(*+[] {});"); 20383 verifyFormat("void f() {\n" 20384 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 20385 "}\n"); 20386 verifyFormat("void f() {\n" 20387 " other(x.begin(), //\n" 20388 " x.end(), //\n" 20389 " [&](int, int) { return 1; });\n" 20390 "}\n"); 20391 verifyFormat("void f() {\n" 20392 " other.other.other.other.other(\n" 20393 " x.begin(), x.end(),\n" 20394 " [something, rather](int, int, int, int, int, int, int) { " 20395 "return 1; });\n" 20396 "}\n"); 20397 verifyFormat( 20398 "void f() {\n" 20399 " other.other.other.other.other(\n" 20400 " x.begin(), x.end(),\n" 20401 " [something, rather](int, int, int, int, int, int, int) {\n" 20402 " //\n" 20403 " });\n" 20404 "}\n"); 20405 verifyFormat("SomeFunction([]() { // A cool function...\n" 20406 " return 43;\n" 20407 "});"); 20408 EXPECT_EQ("SomeFunction([]() {\n" 20409 "#define A a\n" 20410 " return 43;\n" 20411 "});", 20412 format("SomeFunction([](){\n" 20413 "#define A a\n" 20414 "return 43;\n" 20415 "});")); 20416 verifyFormat("void f() {\n" 20417 " SomeFunction([](decltype(x), A *a) {});\n" 20418 " SomeFunction([](typeof(x), A *a) {});\n" 20419 " SomeFunction([](_Atomic(x), A *a) {});\n" 20420 " SomeFunction([](__underlying_type(x), A *a) {});\n" 20421 "}"); 20422 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20423 " [](const aaaaaaaaaa &a) { return a; });"); 20424 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 20425 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 20426 "});"); 20427 verifyFormat("Constructor()\n" 20428 " : Field([] { // comment\n" 20429 " int i;\n" 20430 " }) {}"); 20431 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 20432 " return some_parameter.size();\n" 20433 "};"); 20434 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 20435 " [](const string &s) { return s; };"); 20436 verifyFormat("int i = aaaaaa ? 1 //\n" 20437 " : [] {\n" 20438 " return 2; //\n" 20439 " }();"); 20440 verifyFormat("llvm::errs() << \"number of twos is \"\n" 20441 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 20442 " return x == 2; // force break\n" 20443 " });"); 20444 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20445 " [=](int iiiiiiiiiiii) {\n" 20446 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 20447 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 20448 " });", 20449 getLLVMStyleWithColumns(60)); 20450 20451 verifyFormat("SomeFunction({[&] {\n" 20452 " // comment\n" 20453 " },\n" 20454 " [&] {\n" 20455 " // comment\n" 20456 " }});"); 20457 verifyFormat("SomeFunction({[&] {\n" 20458 " // comment\n" 20459 "}});"); 20460 verifyFormat( 20461 "virtual aaaaaaaaaaaaaaaa(\n" 20462 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 20463 " aaaaa aaaaaaaaa);"); 20464 20465 // Lambdas with return types. 20466 verifyFormat("int c = []() -> int { return 2; }();\n"); 20467 verifyFormat("int c = []() -> int * { return 2; }();\n"); 20468 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 20469 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 20470 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 20471 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 20472 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 20473 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 20474 verifyFormat("[a, a]() -> a<1> {};"); 20475 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 20476 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 20477 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 20478 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 20479 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 20480 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 20481 verifyFormat("[]() -> foo<!5> { return {}; };"); 20482 verifyFormat("[]() -> foo<~5> { return {}; };"); 20483 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 20484 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 20485 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 20486 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 20487 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 20488 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 20489 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 20490 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 20491 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 20492 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 20493 verifyFormat("namespace bar {\n" 20494 "// broken:\n" 20495 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 20496 "} // namespace bar"); 20497 verifyFormat("namespace bar {\n" 20498 "// broken:\n" 20499 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 20500 "} // namespace bar"); 20501 verifyFormat("namespace bar {\n" 20502 "// broken:\n" 20503 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 20504 "} // namespace bar"); 20505 verifyFormat("namespace bar {\n" 20506 "// broken:\n" 20507 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 20508 "} // namespace bar"); 20509 verifyFormat("namespace bar {\n" 20510 "// broken:\n" 20511 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 20512 "} // namespace bar"); 20513 verifyFormat("namespace bar {\n" 20514 "// broken:\n" 20515 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 20516 "} // namespace bar"); 20517 verifyFormat("namespace bar {\n" 20518 "// broken:\n" 20519 "auto foo{[]() -> foo<!5> { return {}; }};\n" 20520 "} // namespace bar"); 20521 verifyFormat("namespace bar {\n" 20522 "// broken:\n" 20523 "auto foo{[]() -> foo<~5> { return {}; }};\n" 20524 "} // namespace bar"); 20525 verifyFormat("namespace bar {\n" 20526 "// broken:\n" 20527 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 20528 "} // namespace bar"); 20529 verifyFormat("namespace bar {\n" 20530 "// broken:\n" 20531 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 20532 "} // namespace bar"); 20533 verifyFormat("namespace bar {\n" 20534 "// broken:\n" 20535 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 20536 "} // namespace bar"); 20537 verifyFormat("namespace bar {\n" 20538 "// broken:\n" 20539 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 20540 "} // namespace bar"); 20541 verifyFormat("namespace bar {\n" 20542 "// broken:\n" 20543 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 20544 "} // namespace bar"); 20545 verifyFormat("namespace bar {\n" 20546 "// broken:\n" 20547 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 20548 "} // namespace bar"); 20549 verifyFormat("namespace bar {\n" 20550 "// broken:\n" 20551 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 20552 "} // namespace bar"); 20553 verifyFormat("namespace bar {\n" 20554 "// broken:\n" 20555 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 20556 "} // namespace bar"); 20557 verifyFormat("namespace bar {\n" 20558 "// broken:\n" 20559 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 20560 "} // namespace bar"); 20561 verifyFormat("namespace bar {\n" 20562 "// broken:\n" 20563 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 20564 "} // namespace bar"); 20565 verifyFormat("[]() -> a<1> {};"); 20566 verifyFormat("[]() -> a<1> { ; };"); 20567 verifyFormat("[]() -> a<1> { ; }();"); 20568 verifyFormat("[a, a]() -> a<true> {};"); 20569 verifyFormat("[]() -> a<true> {};"); 20570 verifyFormat("[]() -> a<true> { ; };"); 20571 verifyFormat("[]() -> a<true> { ; }();"); 20572 verifyFormat("[a, a]() -> a<false> {};"); 20573 verifyFormat("[]() -> a<false> {};"); 20574 verifyFormat("[]() -> a<false> { ; };"); 20575 verifyFormat("[]() -> a<false> { ; }();"); 20576 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 20577 verifyFormat("namespace bar {\n" 20578 "auto foo{[]() -> foo<false> { ; }};\n" 20579 "} // namespace bar"); 20580 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 20581 " int j) -> int {\n" 20582 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 20583 "};"); 20584 verifyFormat( 20585 "aaaaaaaaaaaaaaaaaaaaaa(\n" 20586 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 20587 " return aaaaaaaaaaaaaaaaa;\n" 20588 " });", 20589 getLLVMStyleWithColumns(70)); 20590 verifyFormat("[]() //\n" 20591 " -> int {\n" 20592 " return 1; //\n" 20593 "};"); 20594 verifyFormat("[]() -> Void<T...> {};"); 20595 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 20596 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});"); 20597 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});"); 20598 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});"); 20599 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});"); 20600 verifyFormat("return int{[x = x]() { return x; }()};"); 20601 20602 // Lambdas with explicit template argument lists. 20603 verifyFormat( 20604 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 20605 verifyFormat("auto L = []<class T>(T) {\n" 20606 " {\n" 20607 " f();\n" 20608 " g();\n" 20609 " }\n" 20610 "};\n"); 20611 verifyFormat("auto L = []<class... T>(T...) {\n" 20612 " {\n" 20613 " f();\n" 20614 " g();\n" 20615 " }\n" 20616 "};\n"); 20617 verifyFormat("auto L = []<typename... T>(T...) {\n" 20618 " {\n" 20619 " f();\n" 20620 " g();\n" 20621 " }\n" 20622 "};\n"); 20623 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n" 20624 " {\n" 20625 " f();\n" 20626 " g();\n" 20627 " }\n" 20628 "};\n"); 20629 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n" 20630 " {\n" 20631 " f();\n" 20632 " g();\n" 20633 " }\n" 20634 "};\n"); 20635 20636 // Multiple lambdas in the same parentheses change indentation rules. These 20637 // lambdas are forced to start on new lines. 20638 verifyFormat("SomeFunction(\n" 20639 " []() {\n" 20640 " //\n" 20641 " },\n" 20642 " []() {\n" 20643 " //\n" 20644 " });"); 20645 20646 // A lambda passed as arg0 is always pushed to the next line. 20647 verifyFormat("SomeFunction(\n" 20648 " [this] {\n" 20649 " //\n" 20650 " },\n" 20651 " 1);\n"); 20652 20653 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 20654 // the arg0 case above. 20655 auto Style = getGoogleStyle(); 20656 Style.BinPackArguments = false; 20657 verifyFormat("SomeFunction(\n" 20658 " a,\n" 20659 " [this] {\n" 20660 " //\n" 20661 " },\n" 20662 " b);\n", 20663 Style); 20664 verifyFormat("SomeFunction(\n" 20665 " a,\n" 20666 " [this] {\n" 20667 " //\n" 20668 " },\n" 20669 " b);\n"); 20670 20671 // A lambda with a very long line forces arg0 to be pushed out irrespective of 20672 // the BinPackArguments value (as long as the code is wide enough). 20673 verifyFormat( 20674 "something->SomeFunction(\n" 20675 " a,\n" 20676 " [this] {\n" 20677 " " 20678 "D0000000000000000000000000000000000000000000000000000000000001();\n" 20679 " },\n" 20680 " b);\n"); 20681 20682 // A multi-line lambda is pulled up as long as the introducer fits on the 20683 // previous line and there are no further args. 20684 verifyFormat("function(1, [this, that] {\n" 20685 " //\n" 20686 "});\n"); 20687 verifyFormat("function([this, that] {\n" 20688 " //\n" 20689 "});\n"); 20690 // FIXME: this format is not ideal and we should consider forcing the first 20691 // arg onto its own line. 20692 verifyFormat("function(a, b, c, //\n" 20693 " d, [this, that] {\n" 20694 " //\n" 20695 " });\n"); 20696 20697 // Multiple lambdas are treated correctly even when there is a short arg0. 20698 verifyFormat("SomeFunction(\n" 20699 " 1,\n" 20700 " [this] {\n" 20701 " //\n" 20702 " },\n" 20703 " [this] {\n" 20704 " //\n" 20705 " },\n" 20706 " 1);\n"); 20707 20708 // More complex introducers. 20709 verifyFormat("return [i, args...] {};"); 20710 20711 // Not lambdas. 20712 verifyFormat("constexpr char hello[]{\"hello\"};"); 20713 verifyFormat("double &operator[](int i) { return 0; }\n" 20714 "int i;"); 20715 verifyFormat("std::unique_ptr<int[]> foo() {}"); 20716 verifyFormat("int i = a[a][a]->f();"); 20717 verifyFormat("int i = (*b)[a]->f();"); 20718 20719 // Other corner cases. 20720 verifyFormat("void f() {\n" 20721 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 20722 " );\n" 20723 "}"); 20724 20725 // Lambdas created through weird macros. 20726 verifyFormat("void f() {\n" 20727 " MACRO((const AA &a) { return 1; });\n" 20728 " MACRO((AA &a) { return 1; });\n" 20729 "}"); 20730 20731 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 20732 " doo_dah();\n" 20733 " doo_dah();\n" 20734 " })) {\n" 20735 "}"); 20736 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 20737 " doo_dah();\n" 20738 " doo_dah();\n" 20739 " })) {\n" 20740 "}"); 20741 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 20742 " doo_dah();\n" 20743 " doo_dah();\n" 20744 " })) {\n" 20745 "}"); 20746 verifyFormat("auto lambda = []() {\n" 20747 " int a = 2\n" 20748 "#if A\n" 20749 " + 2\n" 20750 "#endif\n" 20751 " ;\n" 20752 "};"); 20753 20754 // Lambdas with complex multiline introducers. 20755 verifyFormat( 20756 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20757 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 20758 " -> ::std::unordered_set<\n" 20759 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 20760 " //\n" 20761 " });"); 20762 20763 FormatStyle DoNotMerge = getLLVMStyle(); 20764 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20765 verifyFormat("auto c = []() {\n" 20766 " return b;\n" 20767 "};", 20768 "auto c = []() { return b; };", DoNotMerge); 20769 verifyFormat("auto c = []() {\n" 20770 "};", 20771 " auto c = []() {};", DoNotMerge); 20772 20773 FormatStyle MergeEmptyOnly = getLLVMStyle(); 20774 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 20775 verifyFormat("auto c = []() {\n" 20776 " return b;\n" 20777 "};", 20778 "auto c = []() {\n" 20779 " return b;\n" 20780 " };", 20781 MergeEmptyOnly); 20782 verifyFormat("auto c = []() {};", 20783 "auto c = []() {\n" 20784 "};", 20785 MergeEmptyOnly); 20786 20787 FormatStyle MergeInline = getLLVMStyle(); 20788 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 20789 verifyFormat("auto c = []() {\n" 20790 " return b;\n" 20791 "};", 20792 "auto c = []() { return b; };", MergeInline); 20793 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 20794 MergeInline); 20795 verifyFormat("function([]() { return b; }, a)", 20796 "function([]() { return b; }, a)", MergeInline); 20797 verifyFormat("function(a, []() { return b; })", 20798 "function(a, []() { return b; })", MergeInline); 20799 20800 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 20801 // AllowShortLambdasOnASingleLine 20802 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20803 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20804 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20805 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20806 FormatStyle::ShortLambdaStyle::SLS_None; 20807 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 20808 " []()\n" 20809 " {\n" 20810 " return 17;\n" 20811 " });", 20812 LLVMWithBeforeLambdaBody); 20813 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 20814 " []()\n" 20815 " {\n" 20816 " });", 20817 LLVMWithBeforeLambdaBody); 20818 verifyFormat("auto fct_SLS_None = []()\n" 20819 "{\n" 20820 " return 17;\n" 20821 "};", 20822 LLVMWithBeforeLambdaBody); 20823 verifyFormat("TwoNestedLambdas_SLS_None(\n" 20824 " []()\n" 20825 " {\n" 20826 " return Call(\n" 20827 " []()\n" 20828 " {\n" 20829 " return 17;\n" 20830 " });\n" 20831 " });", 20832 LLVMWithBeforeLambdaBody); 20833 verifyFormat("void Fct() {\n" 20834 " return {[]()\n" 20835 " {\n" 20836 " return 17;\n" 20837 " }};\n" 20838 "}", 20839 LLVMWithBeforeLambdaBody); 20840 20841 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20842 FormatStyle::ShortLambdaStyle::SLS_Empty; 20843 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 20844 " []()\n" 20845 " {\n" 20846 " return 17;\n" 20847 " });", 20848 LLVMWithBeforeLambdaBody); 20849 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 20850 LLVMWithBeforeLambdaBody); 20851 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 20852 "ongFunctionName_SLS_Empty(\n" 20853 " []() {});", 20854 LLVMWithBeforeLambdaBody); 20855 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 20856 " []()\n" 20857 " {\n" 20858 " return 17;\n" 20859 " });", 20860 LLVMWithBeforeLambdaBody); 20861 verifyFormat("auto fct_SLS_Empty = []()\n" 20862 "{\n" 20863 " return 17;\n" 20864 "};", 20865 LLVMWithBeforeLambdaBody); 20866 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 20867 " []()\n" 20868 " {\n" 20869 " return Call([]() {});\n" 20870 " });", 20871 LLVMWithBeforeLambdaBody); 20872 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 20873 " []()\n" 20874 " {\n" 20875 " return Call([]() {});\n" 20876 " });", 20877 LLVMWithBeforeLambdaBody); 20878 verifyFormat( 20879 "FctWithLongLineInLambda_SLS_Empty(\n" 20880 " []()\n" 20881 " {\n" 20882 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20883 " AndShouldNotBeConsiderAsInline,\n" 20884 " LambdaBodyMustBeBreak);\n" 20885 " });", 20886 LLVMWithBeforeLambdaBody); 20887 20888 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20889 FormatStyle::ShortLambdaStyle::SLS_Inline; 20890 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 20891 LLVMWithBeforeLambdaBody); 20892 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 20893 LLVMWithBeforeLambdaBody); 20894 verifyFormat("auto fct_SLS_Inline = []()\n" 20895 "{\n" 20896 " return 17;\n" 20897 "};", 20898 LLVMWithBeforeLambdaBody); 20899 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 20900 "17; }); });", 20901 LLVMWithBeforeLambdaBody); 20902 verifyFormat( 20903 "FctWithLongLineInLambda_SLS_Inline(\n" 20904 " []()\n" 20905 " {\n" 20906 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20907 " AndShouldNotBeConsiderAsInline,\n" 20908 " LambdaBodyMustBeBreak);\n" 20909 " });", 20910 LLVMWithBeforeLambdaBody); 20911 verifyFormat("FctWithMultipleParams_SLS_Inline(" 20912 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20913 " []() { return 17; });", 20914 LLVMWithBeforeLambdaBody); 20915 verifyFormat( 20916 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 20917 LLVMWithBeforeLambdaBody); 20918 20919 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20920 FormatStyle::ShortLambdaStyle::SLS_All; 20921 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 20922 LLVMWithBeforeLambdaBody); 20923 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 20924 LLVMWithBeforeLambdaBody); 20925 verifyFormat("auto fct_SLS_All = []() { return 17; };", 20926 LLVMWithBeforeLambdaBody); 20927 verifyFormat("FctWithOneParam_SLS_All(\n" 20928 " []()\n" 20929 " {\n" 20930 " // A cool function...\n" 20931 " return 43;\n" 20932 " });", 20933 LLVMWithBeforeLambdaBody); 20934 verifyFormat("FctWithMultipleParams_SLS_All(" 20935 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20936 " []() { return 17; });", 20937 LLVMWithBeforeLambdaBody); 20938 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 20939 LLVMWithBeforeLambdaBody); 20940 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 20941 LLVMWithBeforeLambdaBody); 20942 verifyFormat( 20943 "FctWithLongLineInLambda_SLS_All(\n" 20944 " []()\n" 20945 " {\n" 20946 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20947 " AndShouldNotBeConsiderAsInline,\n" 20948 " LambdaBodyMustBeBreak);\n" 20949 " });", 20950 LLVMWithBeforeLambdaBody); 20951 verifyFormat( 20952 "auto fct_SLS_All = []()\n" 20953 "{\n" 20954 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20955 " AndShouldNotBeConsiderAsInline,\n" 20956 " LambdaBodyMustBeBreak);\n" 20957 "};", 20958 LLVMWithBeforeLambdaBody); 20959 LLVMWithBeforeLambdaBody.BinPackParameters = false; 20960 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 20961 LLVMWithBeforeLambdaBody); 20962 verifyFormat( 20963 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 20964 " FirstParam,\n" 20965 " SecondParam,\n" 20966 " ThirdParam,\n" 20967 " FourthParam);", 20968 LLVMWithBeforeLambdaBody); 20969 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20970 " []() { return " 20971 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 20972 " FirstParam,\n" 20973 " SecondParam,\n" 20974 " ThirdParam,\n" 20975 " FourthParam);", 20976 LLVMWithBeforeLambdaBody); 20977 verifyFormat( 20978 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 20979 " SecondParam,\n" 20980 " ThirdParam,\n" 20981 " FourthParam,\n" 20982 " []() { return SomeValueNotSoLong; });", 20983 LLVMWithBeforeLambdaBody); 20984 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20985 " []()\n" 20986 " {\n" 20987 " return " 20988 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 20989 "eConsiderAsInline;\n" 20990 " });", 20991 LLVMWithBeforeLambdaBody); 20992 verifyFormat( 20993 "FctWithLongLineInLambda_SLS_All(\n" 20994 " []()\n" 20995 " {\n" 20996 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20997 " AndShouldNotBeConsiderAsInline,\n" 20998 " LambdaBodyMustBeBreak);\n" 20999 " });", 21000 LLVMWithBeforeLambdaBody); 21001 verifyFormat("FctWithTwoParams_SLS_All(\n" 21002 " []()\n" 21003 " {\n" 21004 " // A cool function...\n" 21005 " return 43;\n" 21006 " },\n" 21007 " 87);", 21008 LLVMWithBeforeLambdaBody); 21009 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 21010 LLVMWithBeforeLambdaBody); 21011 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 21012 LLVMWithBeforeLambdaBody); 21013 verifyFormat( 21014 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 21015 LLVMWithBeforeLambdaBody); 21016 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 21017 "}); }, x);", 21018 LLVMWithBeforeLambdaBody); 21019 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21020 " []()\n" 21021 " {\n" 21022 " // A cool function...\n" 21023 " return Call([]() { return 17; });\n" 21024 " });", 21025 LLVMWithBeforeLambdaBody); 21026 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21027 " []()\n" 21028 " {\n" 21029 " return Call(\n" 21030 " []()\n" 21031 " {\n" 21032 " // A cool function...\n" 21033 " return 17;\n" 21034 " });\n" 21035 " });", 21036 LLVMWithBeforeLambdaBody); 21037 21038 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21039 FormatStyle::ShortLambdaStyle::SLS_None; 21040 21041 verifyFormat("auto select = [this]() -> const Library::Object *\n" 21042 "{\n" 21043 " return MyAssignment::SelectFromList(this);\n" 21044 "};\n", 21045 LLVMWithBeforeLambdaBody); 21046 21047 verifyFormat("auto select = [this]() -> const Library::Object &\n" 21048 "{\n" 21049 " return MyAssignment::SelectFromList(this);\n" 21050 "};\n", 21051 LLVMWithBeforeLambdaBody); 21052 21053 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 21054 "{\n" 21055 " return MyAssignment::SelectFromList(this);\n" 21056 "};\n", 21057 LLVMWithBeforeLambdaBody); 21058 21059 verifyFormat("namespace test {\n" 21060 "class Test {\n" 21061 "public:\n" 21062 " Test() = default;\n" 21063 "};\n" 21064 "} // namespace test", 21065 LLVMWithBeforeLambdaBody); 21066 21067 // Lambdas with different indentation styles. 21068 Style = getLLVMStyleWithColumns(100); 21069 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21070 " return promise.then(\n" 21071 " [this, &someVariable, someObject = " 21072 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21073 " return someObject.startAsyncAction().then(\n" 21074 " [this, &someVariable](AsyncActionResult result) " 21075 "mutable { result.processMore(); });\n" 21076 " });\n" 21077 "}\n", 21078 format("SomeResult doSomething(SomeObject promise) {\n" 21079 " return promise.then([this, &someVariable, someObject = " 21080 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21081 " return someObject.startAsyncAction().then([this, " 21082 "&someVariable](AsyncActionResult result) mutable {\n" 21083 " result.processMore();\n" 21084 " });\n" 21085 " });\n" 21086 "}\n", 21087 Style)); 21088 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21089 verifyFormat("test() {\n" 21090 " ([]() -> {\n" 21091 " int b = 32;\n" 21092 " return 3;\n" 21093 " }).foo();\n" 21094 "}", 21095 Style); 21096 verifyFormat("test() {\n" 21097 " []() -> {\n" 21098 " int b = 32;\n" 21099 " return 3;\n" 21100 " }\n" 21101 "}", 21102 Style); 21103 verifyFormat("std::sort(v.begin(), v.end(),\n" 21104 " [](const auto &someLongArgumentName, const auto " 21105 "&someOtherLongArgumentName) {\n" 21106 " return someLongArgumentName.someMemberVariable < " 21107 "someOtherLongArgumentName.someMemberVariable;\n" 21108 "});", 21109 Style); 21110 verifyFormat("test() {\n" 21111 " (\n" 21112 " []() -> {\n" 21113 " int b = 32;\n" 21114 " return 3;\n" 21115 " },\n" 21116 " foo, bar)\n" 21117 " .foo();\n" 21118 "}", 21119 Style); 21120 verifyFormat("test() {\n" 21121 " ([]() -> {\n" 21122 " int b = 32;\n" 21123 " return 3;\n" 21124 " })\n" 21125 " .foo()\n" 21126 " .bar();\n" 21127 "}", 21128 Style); 21129 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21130 " return promise.then(\n" 21131 " [this, &someVariable, someObject = " 21132 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21133 " return someObject.startAsyncAction().then(\n" 21134 " [this, &someVariable](AsyncActionResult result) mutable { " 21135 "result.processMore(); });\n" 21136 " });\n" 21137 "}\n", 21138 format("SomeResult doSomething(SomeObject promise) {\n" 21139 " return promise.then([this, &someVariable, someObject = " 21140 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21141 " return someObject.startAsyncAction().then([this, " 21142 "&someVariable](AsyncActionResult result) mutable {\n" 21143 " result.processMore();\n" 21144 " });\n" 21145 " });\n" 21146 "}\n", 21147 Style)); 21148 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21149 " return promise.then([this, &someVariable] {\n" 21150 " return someObject.startAsyncAction().then(\n" 21151 " [this, &someVariable](AsyncActionResult result) mutable { " 21152 "result.processMore(); });\n" 21153 " });\n" 21154 "}\n", 21155 format("SomeResult doSomething(SomeObject promise) {\n" 21156 " return promise.then([this, &someVariable] {\n" 21157 " return someObject.startAsyncAction().then([this, " 21158 "&someVariable](AsyncActionResult result) mutable {\n" 21159 " result.processMore();\n" 21160 " });\n" 21161 " });\n" 21162 "}\n", 21163 Style)); 21164 Style = getGoogleStyle(); 21165 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21166 EXPECT_EQ("#define A \\\n" 21167 " [] { \\\n" 21168 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 21169 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 21170 " }", 21171 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 21172 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 21173 Style)); 21174 // TODO: The current formatting has a minor issue that's not worth fixing 21175 // right now whereby the closing brace is indented relative to the signature 21176 // instead of being aligned. This only happens with macros. 21177 } 21178 21179 TEST_F(FormatTest, LambdaWithLineComments) { 21180 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 21181 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 21182 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 21183 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21184 FormatStyle::ShortLambdaStyle::SLS_All; 21185 21186 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 21187 verifyFormat("auto k = []() // comment\n" 21188 "{ return; }", 21189 LLVMWithBeforeLambdaBody); 21190 verifyFormat("auto k = []() /* comment */ { return; }", 21191 LLVMWithBeforeLambdaBody); 21192 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 21193 LLVMWithBeforeLambdaBody); 21194 verifyFormat("auto k = []() // X\n" 21195 "{ return; }", 21196 LLVMWithBeforeLambdaBody); 21197 verifyFormat( 21198 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 21199 "{ return; }", 21200 LLVMWithBeforeLambdaBody); 21201 } 21202 21203 TEST_F(FormatTest, EmptyLinesInLambdas) { 21204 verifyFormat("auto lambda = []() {\n" 21205 " x(); //\n" 21206 "};", 21207 "auto lambda = []() {\n" 21208 "\n" 21209 " x(); //\n" 21210 "\n" 21211 "};"); 21212 } 21213 21214 TEST_F(FormatTest, FormatsBlocks) { 21215 FormatStyle ShortBlocks = getLLVMStyle(); 21216 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21217 verifyFormat("int (^Block)(int, int);", ShortBlocks); 21218 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 21219 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 21220 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 21221 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 21222 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 21223 21224 verifyFormat("foo(^{ bar(); });", ShortBlocks); 21225 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 21226 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 21227 21228 verifyFormat("[operation setCompletionBlock:^{\n" 21229 " [self onOperationDone];\n" 21230 "}];"); 21231 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 21232 " [self onOperationDone];\n" 21233 "}]};"); 21234 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 21235 " f();\n" 21236 "}];"); 21237 verifyFormat("int a = [operation block:^int(int *i) {\n" 21238 " return 1;\n" 21239 "}];"); 21240 verifyFormat("[myObject doSomethingWith:arg1\n" 21241 " aaa:^int(int *a) {\n" 21242 " return 1;\n" 21243 " }\n" 21244 " bbb:f(a * bbbbbbbb)];"); 21245 21246 verifyFormat("[operation setCompletionBlock:^{\n" 21247 " [self.delegate newDataAvailable];\n" 21248 "}];", 21249 getLLVMStyleWithColumns(60)); 21250 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 21251 " NSString *path = [self sessionFilePath];\n" 21252 " if (path) {\n" 21253 " // ...\n" 21254 " }\n" 21255 "});"); 21256 verifyFormat("[[SessionService sharedService]\n" 21257 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21258 " if (window) {\n" 21259 " [self windowDidLoad:window];\n" 21260 " } else {\n" 21261 " [self errorLoadingWindow];\n" 21262 " }\n" 21263 " }];"); 21264 verifyFormat("void (^largeBlock)(void) = ^{\n" 21265 " // ...\n" 21266 "};\n", 21267 getLLVMStyleWithColumns(40)); 21268 verifyFormat("[[SessionService sharedService]\n" 21269 " loadWindowWithCompletionBlock: //\n" 21270 " ^(SessionWindow *window) {\n" 21271 " if (window) {\n" 21272 " [self windowDidLoad:window];\n" 21273 " } else {\n" 21274 " [self errorLoadingWindow];\n" 21275 " }\n" 21276 " }];", 21277 getLLVMStyleWithColumns(60)); 21278 verifyFormat("[myObject doSomethingWith:arg1\n" 21279 " firstBlock:^(Foo *a) {\n" 21280 " // ...\n" 21281 " int i;\n" 21282 " }\n" 21283 " secondBlock:^(Bar *b) {\n" 21284 " // ...\n" 21285 " int i;\n" 21286 " }\n" 21287 " thirdBlock:^Foo(Bar *b) {\n" 21288 " // ...\n" 21289 " int i;\n" 21290 " }];"); 21291 verifyFormat("[myObject doSomethingWith:arg1\n" 21292 " firstBlock:-1\n" 21293 " secondBlock:^(Bar *b) {\n" 21294 " // ...\n" 21295 " int i;\n" 21296 " }];"); 21297 21298 verifyFormat("f(^{\n" 21299 " @autoreleasepool {\n" 21300 " if (a) {\n" 21301 " g();\n" 21302 " }\n" 21303 " }\n" 21304 "});"); 21305 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 21306 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 21307 "};"); 21308 21309 FormatStyle FourIndent = getLLVMStyle(); 21310 FourIndent.ObjCBlockIndentWidth = 4; 21311 verifyFormat("[operation setCompletionBlock:^{\n" 21312 " [self onOperationDone];\n" 21313 "}];", 21314 FourIndent); 21315 } 21316 21317 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 21318 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 21319 21320 verifyFormat("[[SessionService sharedService] " 21321 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21322 " if (window) {\n" 21323 " [self windowDidLoad:window];\n" 21324 " } else {\n" 21325 " [self errorLoadingWindow];\n" 21326 " }\n" 21327 "}];", 21328 ZeroColumn); 21329 EXPECT_EQ("[[SessionService sharedService]\n" 21330 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21331 " if (window) {\n" 21332 " [self windowDidLoad:window];\n" 21333 " } else {\n" 21334 " [self errorLoadingWindow];\n" 21335 " }\n" 21336 " }];", 21337 format("[[SessionService sharedService]\n" 21338 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21339 " if (window) {\n" 21340 " [self windowDidLoad:window];\n" 21341 " } else {\n" 21342 " [self errorLoadingWindow];\n" 21343 " }\n" 21344 "}];", 21345 ZeroColumn)); 21346 verifyFormat("[myObject doSomethingWith:arg1\n" 21347 " firstBlock:^(Foo *a) {\n" 21348 " // ...\n" 21349 " int i;\n" 21350 " }\n" 21351 " secondBlock:^(Bar *b) {\n" 21352 " // ...\n" 21353 " int i;\n" 21354 " }\n" 21355 " thirdBlock:^Foo(Bar *b) {\n" 21356 " // ...\n" 21357 " int i;\n" 21358 " }];", 21359 ZeroColumn); 21360 verifyFormat("f(^{\n" 21361 " @autoreleasepool {\n" 21362 " if (a) {\n" 21363 " g();\n" 21364 " }\n" 21365 " }\n" 21366 "});", 21367 ZeroColumn); 21368 verifyFormat("void (^largeBlock)(void) = ^{\n" 21369 " // ...\n" 21370 "};", 21371 ZeroColumn); 21372 21373 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21374 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 21375 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21376 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 21377 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 21378 " int i;\n" 21379 "};", 21380 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21381 } 21382 21383 TEST_F(FormatTest, SupportsCRLF) { 21384 EXPECT_EQ("int a;\r\n" 21385 "int b;\r\n" 21386 "int c;\r\n", 21387 format("int a;\r\n" 21388 " int b;\r\n" 21389 " int c;\r\n", 21390 getLLVMStyle())); 21391 EXPECT_EQ("int a;\r\n" 21392 "int b;\r\n" 21393 "int c;\r\n", 21394 format("int a;\r\n" 21395 " int b;\n" 21396 " int c;\r\n", 21397 getLLVMStyle())); 21398 EXPECT_EQ("int a;\n" 21399 "int b;\n" 21400 "int c;\n", 21401 format("int a;\r\n" 21402 " int b;\n" 21403 " int c;\n", 21404 getLLVMStyle())); 21405 EXPECT_EQ("\"aaaaaaa \"\r\n" 21406 "\"bbbbbbb\";\r\n", 21407 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 21408 EXPECT_EQ("#define A \\\r\n" 21409 " b; \\\r\n" 21410 " c; \\\r\n" 21411 " d;\r\n", 21412 format("#define A \\\r\n" 21413 " b; \\\r\n" 21414 " c; d; \r\n", 21415 getGoogleStyle())); 21416 21417 EXPECT_EQ("/*\r\n" 21418 "multi line block comments\r\n" 21419 "should not introduce\r\n" 21420 "an extra carriage return\r\n" 21421 "*/\r\n", 21422 format("/*\r\n" 21423 "multi line block comments\r\n" 21424 "should not introduce\r\n" 21425 "an extra carriage return\r\n" 21426 "*/\r\n")); 21427 EXPECT_EQ("/*\r\n" 21428 "\r\n" 21429 "*/", 21430 format("/*\r\n" 21431 " \r\r\r\n" 21432 "*/")); 21433 21434 FormatStyle style = getLLVMStyle(); 21435 21436 style.DeriveLineEnding = true; 21437 style.UseCRLF = false; 21438 EXPECT_EQ("union FooBarBazQux {\n" 21439 " int foo;\n" 21440 " int bar;\n" 21441 " int baz;\n" 21442 "};", 21443 format("union FooBarBazQux {\r\n" 21444 " int foo;\n" 21445 " int bar;\r\n" 21446 " int baz;\n" 21447 "};", 21448 style)); 21449 style.UseCRLF = true; 21450 EXPECT_EQ("union FooBarBazQux {\r\n" 21451 " int foo;\r\n" 21452 " int bar;\r\n" 21453 " int baz;\r\n" 21454 "};", 21455 format("union FooBarBazQux {\r\n" 21456 " int foo;\n" 21457 " int bar;\r\n" 21458 " int baz;\n" 21459 "};", 21460 style)); 21461 21462 style.DeriveLineEnding = false; 21463 style.UseCRLF = false; 21464 EXPECT_EQ("union FooBarBazQux {\n" 21465 " int foo;\n" 21466 " int bar;\n" 21467 " int baz;\n" 21468 " int qux;\n" 21469 "};", 21470 format("union FooBarBazQux {\r\n" 21471 " int foo;\n" 21472 " int bar;\r\n" 21473 " int baz;\n" 21474 " int qux;\r\n" 21475 "};", 21476 style)); 21477 style.UseCRLF = true; 21478 EXPECT_EQ("union FooBarBazQux {\r\n" 21479 " int foo;\r\n" 21480 " int bar;\r\n" 21481 " int baz;\r\n" 21482 " int qux;\r\n" 21483 "};", 21484 format("union FooBarBazQux {\r\n" 21485 " int foo;\n" 21486 " int bar;\r\n" 21487 " int baz;\n" 21488 " int qux;\n" 21489 "};", 21490 style)); 21491 21492 style.DeriveLineEnding = true; 21493 style.UseCRLF = false; 21494 EXPECT_EQ("union FooBarBazQux {\r\n" 21495 " int foo;\r\n" 21496 " int bar;\r\n" 21497 " int baz;\r\n" 21498 " int qux;\r\n" 21499 "};", 21500 format("union FooBarBazQux {\r\n" 21501 " int foo;\n" 21502 " int bar;\r\n" 21503 " int baz;\n" 21504 " int qux;\r\n" 21505 "};", 21506 style)); 21507 style.UseCRLF = true; 21508 EXPECT_EQ("union FooBarBazQux {\n" 21509 " int foo;\n" 21510 " int bar;\n" 21511 " int baz;\n" 21512 " int qux;\n" 21513 "};", 21514 format("union FooBarBazQux {\r\n" 21515 " int foo;\n" 21516 " int bar;\r\n" 21517 " int baz;\n" 21518 " int qux;\n" 21519 "};", 21520 style)); 21521 } 21522 21523 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 21524 verifyFormat("MY_CLASS(C) {\n" 21525 " int i;\n" 21526 " int j;\n" 21527 "};"); 21528 } 21529 21530 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 21531 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 21532 TwoIndent.ContinuationIndentWidth = 2; 21533 21534 EXPECT_EQ("int i =\n" 21535 " longFunction(\n" 21536 " arg);", 21537 format("int i = longFunction(arg);", TwoIndent)); 21538 21539 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 21540 SixIndent.ContinuationIndentWidth = 6; 21541 21542 EXPECT_EQ("int i =\n" 21543 " longFunction(\n" 21544 " arg);", 21545 format("int i = longFunction(arg);", SixIndent)); 21546 } 21547 21548 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 21549 FormatStyle Style = getLLVMStyle(); 21550 verifyFormat("int Foo::getter(\n" 21551 " //\n" 21552 ") const {\n" 21553 " return foo;\n" 21554 "}", 21555 Style); 21556 verifyFormat("void Foo::setter(\n" 21557 " //\n" 21558 ") {\n" 21559 " foo = 1;\n" 21560 "}", 21561 Style); 21562 } 21563 21564 TEST_F(FormatTest, SpacesInAngles) { 21565 FormatStyle Spaces = getLLVMStyle(); 21566 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21567 21568 verifyFormat("vector< ::std::string > x1;", Spaces); 21569 verifyFormat("Foo< int, Bar > x2;", Spaces); 21570 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 21571 21572 verifyFormat("static_cast< int >(arg);", Spaces); 21573 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 21574 verifyFormat("f< int, float >();", Spaces); 21575 verifyFormat("template <> g() {}", Spaces); 21576 verifyFormat("template < std::vector< int > > f() {}", Spaces); 21577 verifyFormat("std::function< void(int, int) > fct;", Spaces); 21578 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 21579 Spaces); 21580 21581 Spaces.Standard = FormatStyle::LS_Cpp03; 21582 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21583 verifyFormat("A< A< int > >();", Spaces); 21584 21585 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21586 verifyFormat("A<A<int> >();", Spaces); 21587 21588 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21589 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 21590 Spaces); 21591 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 21592 Spaces); 21593 21594 verifyFormat("A<A<int> >();", Spaces); 21595 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 21596 verifyFormat("A< A< int > >();", Spaces); 21597 21598 Spaces.Standard = FormatStyle::LS_Cpp11; 21599 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21600 verifyFormat("A< A< int > >();", Spaces); 21601 21602 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21603 verifyFormat("vector<::std::string> x4;", Spaces); 21604 verifyFormat("vector<int> x5;", Spaces); 21605 verifyFormat("Foo<int, Bar> x6;", Spaces); 21606 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21607 21608 verifyFormat("A<A<int>>();", Spaces); 21609 21610 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21611 verifyFormat("vector<::std::string> x4;", Spaces); 21612 verifyFormat("vector< ::std::string > x4;", Spaces); 21613 verifyFormat("vector<int> x5;", Spaces); 21614 verifyFormat("vector< int > x5;", Spaces); 21615 verifyFormat("Foo<int, Bar> x6;", Spaces); 21616 verifyFormat("Foo< int, Bar > x6;", Spaces); 21617 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21618 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 21619 21620 verifyFormat("A<A<int>>();", Spaces); 21621 verifyFormat("A< A< int > >();", Spaces); 21622 verifyFormat("A<A<int > >();", Spaces); 21623 verifyFormat("A< A< int>>();", Spaces); 21624 21625 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21626 verifyFormat("// clang-format off\n" 21627 "foo<<<1, 1>>>();\n" 21628 "// clang-format on\n", 21629 Spaces); 21630 verifyFormat("// clang-format off\n" 21631 "foo< < <1, 1> > >();\n" 21632 "// clang-format on\n", 21633 Spaces); 21634 } 21635 21636 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 21637 FormatStyle Style = getLLVMStyle(); 21638 Style.SpaceAfterTemplateKeyword = false; 21639 verifyFormat("template<int> void foo();", Style); 21640 } 21641 21642 TEST_F(FormatTest, TripleAngleBrackets) { 21643 verifyFormat("f<<<1, 1>>>();"); 21644 verifyFormat("f<<<1, 1, 1, s>>>();"); 21645 verifyFormat("f<<<a, b, c, d>>>();"); 21646 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 21647 verifyFormat("f<param><<<1, 1>>>();"); 21648 verifyFormat("f<1><<<1, 1>>>();"); 21649 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 21650 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21651 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 21652 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 21653 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 21654 } 21655 21656 TEST_F(FormatTest, MergeLessLessAtEnd) { 21657 verifyFormat("<<"); 21658 EXPECT_EQ("< < <", format("\\\n<<<")); 21659 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21660 "aaallvm::outs() <<"); 21661 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21662 "aaaallvm::outs()\n <<"); 21663 } 21664 21665 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 21666 std::string code = "#if A\n" 21667 "#if B\n" 21668 "a.\n" 21669 "#endif\n" 21670 " a = 1;\n" 21671 "#else\n" 21672 "#endif\n" 21673 "#if C\n" 21674 "#else\n" 21675 "#endif\n"; 21676 EXPECT_EQ(code, format(code)); 21677 } 21678 21679 TEST_F(FormatTest, HandleConflictMarkers) { 21680 // Git/SVN conflict markers. 21681 EXPECT_EQ("int a;\n" 21682 "void f() {\n" 21683 " callme(some(parameter1,\n" 21684 "<<<<<<< text by the vcs\n" 21685 " parameter2),\n" 21686 "||||||| text by the vcs\n" 21687 " parameter2),\n" 21688 " parameter3,\n" 21689 "======= text by the vcs\n" 21690 " parameter2, parameter3),\n" 21691 ">>>>>>> text by the vcs\n" 21692 " otherparameter);\n", 21693 format("int a;\n" 21694 "void f() {\n" 21695 " callme(some(parameter1,\n" 21696 "<<<<<<< text by the vcs\n" 21697 " parameter2),\n" 21698 "||||||| text by the vcs\n" 21699 " parameter2),\n" 21700 " parameter3,\n" 21701 "======= text by the vcs\n" 21702 " parameter2,\n" 21703 " parameter3),\n" 21704 ">>>>>>> text by the vcs\n" 21705 " otherparameter);\n")); 21706 21707 // Perforce markers. 21708 EXPECT_EQ("void f() {\n" 21709 " function(\n" 21710 ">>>> text by the vcs\n" 21711 " parameter,\n" 21712 "==== text by the vcs\n" 21713 " parameter,\n" 21714 "==== text by the vcs\n" 21715 " parameter,\n" 21716 "<<<< text by the vcs\n" 21717 " parameter);\n", 21718 format("void f() {\n" 21719 " function(\n" 21720 ">>>> text by the vcs\n" 21721 " parameter,\n" 21722 "==== text by the vcs\n" 21723 " parameter,\n" 21724 "==== text by the vcs\n" 21725 " parameter,\n" 21726 "<<<< text by the vcs\n" 21727 " parameter);\n")); 21728 21729 EXPECT_EQ("<<<<<<<\n" 21730 "|||||||\n" 21731 "=======\n" 21732 ">>>>>>>", 21733 format("<<<<<<<\n" 21734 "|||||||\n" 21735 "=======\n" 21736 ">>>>>>>")); 21737 21738 EXPECT_EQ("<<<<<<<\n" 21739 "|||||||\n" 21740 "int i;\n" 21741 "=======\n" 21742 ">>>>>>>", 21743 format("<<<<<<<\n" 21744 "|||||||\n" 21745 "int i;\n" 21746 "=======\n" 21747 ">>>>>>>")); 21748 21749 // FIXME: Handle parsing of macros around conflict markers correctly: 21750 EXPECT_EQ("#define Macro \\\n" 21751 "<<<<<<<\n" 21752 "Something \\\n" 21753 "|||||||\n" 21754 "Else \\\n" 21755 "=======\n" 21756 "Other \\\n" 21757 ">>>>>>>\n" 21758 " End int i;\n", 21759 format("#define Macro \\\n" 21760 "<<<<<<<\n" 21761 " Something \\\n" 21762 "|||||||\n" 21763 " Else \\\n" 21764 "=======\n" 21765 " Other \\\n" 21766 ">>>>>>>\n" 21767 " End\n" 21768 "int i;\n")); 21769 21770 verifyFormat(R"(==== 21771 #ifdef A 21772 a 21773 #else 21774 b 21775 #endif 21776 )"); 21777 } 21778 21779 TEST_F(FormatTest, DisableRegions) { 21780 EXPECT_EQ("int i;\n" 21781 "// clang-format off\n" 21782 " int j;\n" 21783 "// clang-format on\n" 21784 "int k;", 21785 format(" int i;\n" 21786 " // clang-format off\n" 21787 " int j;\n" 21788 " // clang-format on\n" 21789 " int k;")); 21790 EXPECT_EQ("int i;\n" 21791 "/* clang-format off */\n" 21792 " int j;\n" 21793 "/* clang-format on */\n" 21794 "int k;", 21795 format(" int i;\n" 21796 " /* clang-format off */\n" 21797 " int j;\n" 21798 " /* clang-format on */\n" 21799 " int k;")); 21800 21801 // Don't reflow comments within disabled regions. 21802 EXPECT_EQ("// clang-format off\n" 21803 "// long long long long long long line\n" 21804 "/* clang-format on */\n" 21805 "/* long long long\n" 21806 " * long long long\n" 21807 " * line */\n" 21808 "int i;\n" 21809 "/* clang-format off */\n" 21810 "/* long long long long long long line */\n", 21811 format("// clang-format off\n" 21812 "// long long long long long long line\n" 21813 "/* clang-format on */\n" 21814 "/* long long long long long long line */\n" 21815 "int i;\n" 21816 "/* clang-format off */\n" 21817 "/* long long long long long long line */\n", 21818 getLLVMStyleWithColumns(20))); 21819 } 21820 21821 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 21822 format("? ) ="); 21823 verifyNoCrash("#define a\\\n /**/}"); 21824 } 21825 21826 TEST_F(FormatTest, FormatsTableGenCode) { 21827 FormatStyle Style = getLLVMStyle(); 21828 Style.Language = FormatStyle::LK_TableGen; 21829 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 21830 } 21831 21832 TEST_F(FormatTest, ArrayOfTemplates) { 21833 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 21834 format("auto a = new unique_ptr<int > [ 10];")); 21835 21836 FormatStyle Spaces = getLLVMStyle(); 21837 Spaces.SpacesInSquareBrackets = true; 21838 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 21839 format("auto a = new unique_ptr<int > [10];", Spaces)); 21840 } 21841 21842 TEST_F(FormatTest, ArrayAsTemplateType) { 21843 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 21844 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 21845 21846 FormatStyle Spaces = getLLVMStyle(); 21847 Spaces.SpacesInSquareBrackets = true; 21848 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 21849 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 21850 } 21851 21852 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 21853 21854 TEST(FormatStyle, GetStyleWithEmptyFileName) { 21855 llvm::vfs::InMemoryFileSystem FS; 21856 auto Style1 = getStyle("file", "", "Google", "", &FS); 21857 ASSERT_TRUE((bool)Style1); 21858 ASSERT_EQ(*Style1, getGoogleStyle()); 21859 } 21860 21861 TEST(FormatStyle, GetStyleOfFile) { 21862 llvm::vfs::InMemoryFileSystem FS; 21863 // Test 1: format file in the same directory. 21864 ASSERT_TRUE( 21865 FS.addFile("/a/.clang-format", 0, 21866 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 21867 ASSERT_TRUE( 21868 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21869 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 21870 ASSERT_TRUE((bool)Style1); 21871 ASSERT_EQ(*Style1, getLLVMStyle()); 21872 21873 // Test 2.1: fallback to default. 21874 ASSERT_TRUE( 21875 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21876 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 21877 ASSERT_TRUE((bool)Style2); 21878 ASSERT_EQ(*Style2, getMozillaStyle()); 21879 21880 // Test 2.2: no format on 'none' fallback style. 21881 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21882 ASSERT_TRUE((bool)Style2); 21883 ASSERT_EQ(*Style2, getNoStyle()); 21884 21885 // Test 2.3: format if config is found with no based style while fallback is 21886 // 'none'. 21887 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 21888 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 21889 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21890 ASSERT_TRUE((bool)Style2); 21891 ASSERT_EQ(*Style2, getLLVMStyle()); 21892 21893 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 21894 Style2 = getStyle("{}", "a.h", "none", "", &FS); 21895 ASSERT_TRUE((bool)Style2); 21896 ASSERT_EQ(*Style2, getLLVMStyle()); 21897 21898 // Test 3: format file in parent directory. 21899 ASSERT_TRUE( 21900 FS.addFile("/c/.clang-format", 0, 21901 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 21902 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 21903 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21904 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 21905 ASSERT_TRUE((bool)Style3); 21906 ASSERT_EQ(*Style3, getGoogleStyle()); 21907 21908 // Test 4: error on invalid fallback style 21909 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 21910 ASSERT_FALSE((bool)Style4); 21911 llvm::consumeError(Style4.takeError()); 21912 21913 // Test 5: error on invalid yaml on command line 21914 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 21915 ASSERT_FALSE((bool)Style5); 21916 llvm::consumeError(Style5.takeError()); 21917 21918 // Test 6: error on invalid style 21919 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 21920 ASSERT_FALSE((bool)Style6); 21921 llvm::consumeError(Style6.takeError()); 21922 21923 // Test 7: found config file, error on parsing it 21924 ASSERT_TRUE( 21925 FS.addFile("/d/.clang-format", 0, 21926 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 21927 "InvalidKey: InvalidValue"))); 21928 ASSERT_TRUE( 21929 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21930 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 21931 ASSERT_FALSE((bool)Style7a); 21932 llvm::consumeError(Style7a.takeError()); 21933 21934 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 21935 ASSERT_TRUE((bool)Style7b); 21936 21937 // Test 8: inferred per-language defaults apply. 21938 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 21939 ASSERT_TRUE((bool)StyleTd); 21940 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 21941 21942 // Test 9.1.1: overwriting a file style, when no parent file exists with no 21943 // fallback style. 21944 ASSERT_TRUE(FS.addFile( 21945 "/e/sub/.clang-format", 0, 21946 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 21947 "ColumnLimit: 20"))); 21948 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 21949 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21950 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21951 ASSERT_TRUE(static_cast<bool>(Style9)); 21952 ASSERT_EQ(*Style9, [] { 21953 auto Style = getNoStyle(); 21954 Style.ColumnLimit = 20; 21955 return Style; 21956 }()); 21957 21958 // Test 9.1.2: propagate more than one level with no parent file. 21959 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 21960 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21961 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 21962 llvm::MemoryBuffer::getMemBuffer( 21963 "BasedOnStyle: InheritParentConfig\n" 21964 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 21965 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 21966 21967 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 21968 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 21969 ASSERT_TRUE(static_cast<bool>(Style9)); 21970 ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] { 21971 auto Style = getNoStyle(); 21972 Style.ColumnLimit = 20; 21973 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 21974 return Style; 21975 }()); 21976 21977 // Test 9.2: with LLVM fallback style 21978 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 21979 ASSERT_TRUE(static_cast<bool>(Style9)); 21980 ASSERT_EQ(*Style9, [] { 21981 auto Style = getLLVMStyle(); 21982 Style.ColumnLimit = 20; 21983 return Style; 21984 }()); 21985 21986 // Test 9.3: with a parent file 21987 ASSERT_TRUE( 21988 FS.addFile("/e/.clang-format", 0, 21989 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 21990 "UseTab: Always"))); 21991 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21992 ASSERT_TRUE(static_cast<bool>(Style9)); 21993 ASSERT_EQ(*Style9, [] { 21994 auto Style = getGoogleStyle(); 21995 Style.ColumnLimit = 20; 21996 Style.UseTab = FormatStyle::UT_Always; 21997 return Style; 21998 }()); 21999 22000 // Test 9.4: propagate more than one level with a parent file. 22001 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 22002 auto Style = getGoogleStyle(); 22003 Style.ColumnLimit = 20; 22004 Style.UseTab = FormatStyle::UT_Always; 22005 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22006 return Style; 22007 }(); 22008 22009 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22010 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22011 ASSERT_TRUE(static_cast<bool>(Style9)); 22012 ASSERT_EQ(*Style9, SubSubStyle); 22013 22014 // Test 9.5: use InheritParentConfig as style name 22015 Style9 = 22016 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 22017 ASSERT_TRUE(static_cast<bool>(Style9)); 22018 ASSERT_EQ(*Style9, SubSubStyle); 22019 22020 // Test 9.6: use command line style with inheritance 22021 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 22022 "none", "", &FS); 22023 ASSERT_TRUE(static_cast<bool>(Style9)); 22024 ASSERT_EQ(*Style9, SubSubStyle); 22025 22026 // Test 9.7: use command line style with inheritance and own config 22027 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 22028 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 22029 "/e/sub/code.cpp", "none", "", &FS); 22030 ASSERT_TRUE(static_cast<bool>(Style9)); 22031 ASSERT_EQ(*Style9, SubSubStyle); 22032 22033 // Test 9.8: use inheritance from a file without BasedOnStyle 22034 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 22035 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 22036 ASSERT_TRUE( 22037 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 22038 llvm::MemoryBuffer::getMemBuffer( 22039 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 22040 // Make sure we do not use the fallback style 22041 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 22042 ASSERT_TRUE(static_cast<bool>(Style9)); 22043 ASSERT_EQ(*Style9, [] { 22044 auto Style = getLLVMStyle(); 22045 Style.ColumnLimit = 123; 22046 return Style; 22047 }()); 22048 22049 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 22050 ASSERT_TRUE(static_cast<bool>(Style9)); 22051 ASSERT_EQ(*Style9, [] { 22052 auto Style = getLLVMStyle(); 22053 Style.ColumnLimit = 123; 22054 Style.IndentWidth = 7; 22055 return Style; 22056 }()); 22057 22058 // Test 9.9: use inheritance from a specific config file. 22059 Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp", 22060 "none", "", &FS); 22061 ASSERT_TRUE(static_cast<bool>(Style9)); 22062 ASSERT_EQ(*Style9, SubSubStyle); 22063 } 22064 22065 TEST(FormatStyle, GetStyleOfSpecificFile) { 22066 llvm::vfs::InMemoryFileSystem FS; 22067 // Specify absolute path to a format file in a parent directory. 22068 ASSERT_TRUE( 22069 FS.addFile("/e/.clang-format", 0, 22070 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 22071 ASSERT_TRUE( 22072 FS.addFile("/e/explicit.clang-format", 0, 22073 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22074 ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0, 22075 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22076 auto Style = getStyle("file:/e/explicit.clang-format", 22077 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22078 ASSERT_TRUE(static_cast<bool>(Style)); 22079 ASSERT_EQ(*Style, getGoogleStyle()); 22080 22081 // Specify relative path to a format file. 22082 ASSERT_TRUE( 22083 FS.addFile("../../e/explicit.clang-format", 0, 22084 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22085 Style = getStyle("file:../../e/explicit.clang-format", 22086 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22087 ASSERT_TRUE(static_cast<bool>(Style)); 22088 ASSERT_EQ(*Style, getGoogleStyle()); 22089 22090 // Specify path to a format file that does not exist. 22091 Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp", 22092 "LLVM", "", &FS); 22093 ASSERT_FALSE(static_cast<bool>(Style)); 22094 llvm::consumeError(Style.takeError()); 22095 22096 // Specify path to a file on the filesystem. 22097 SmallString<128> FormatFilePath; 22098 std::error_code ECF = llvm::sys::fs::createTemporaryFile( 22099 "FormatFileTest", "tpl", FormatFilePath); 22100 EXPECT_FALSE((bool)ECF); 22101 llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF); 22102 EXPECT_FALSE((bool)ECF); 22103 FormatFileTest << "BasedOnStyle: Google\n"; 22104 FormatFileTest.close(); 22105 22106 SmallString<128> TestFilePath; 22107 std::error_code ECT = 22108 llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath); 22109 EXPECT_FALSE((bool)ECT); 22110 llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT); 22111 CodeFileTest << "int i;\n"; 22112 CodeFileTest.close(); 22113 22114 std::string format_file_arg = std::string("file:") + FormatFilePath.c_str(); 22115 Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr); 22116 22117 llvm::sys::fs::remove(FormatFilePath.c_str()); 22118 llvm::sys::fs::remove(TestFilePath.c_str()); 22119 ASSERT_TRUE(static_cast<bool>(Style)); 22120 ASSERT_EQ(*Style, getGoogleStyle()); 22121 } 22122 22123 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 22124 // Column limit is 20. 22125 std::string Code = "Type *a =\n" 22126 " new Type();\n" 22127 "g(iiiii, 0, jjjjj,\n" 22128 " 0, kkkkk, 0, mm);\n" 22129 "int bad = format ;"; 22130 std::string Expected = "auto a = new Type();\n" 22131 "g(iiiii, nullptr,\n" 22132 " jjjjj, nullptr,\n" 22133 " kkkkk, nullptr,\n" 22134 " mm);\n" 22135 "int bad = format ;"; 22136 FileID ID = Context.createInMemoryFile("format.cpp", Code); 22137 tooling::Replacements Replaces = toReplacements( 22138 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 22139 "auto "), 22140 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 22141 "nullptr"), 22142 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 22143 "nullptr"), 22144 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 22145 "nullptr")}); 22146 22147 FormatStyle Style = getLLVMStyle(); 22148 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 22149 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 22150 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 22151 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 22152 auto Result = applyAllReplacements(Code, *FormattedReplaces); 22153 EXPECT_TRUE(static_cast<bool>(Result)); 22154 EXPECT_EQ(Expected, *Result); 22155 } 22156 22157 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 22158 std::string Code = "#include \"a.h\"\n" 22159 "#include \"c.h\"\n" 22160 "\n" 22161 "int main() {\n" 22162 " return 0;\n" 22163 "}"; 22164 std::string Expected = "#include \"a.h\"\n" 22165 "#include \"b.h\"\n" 22166 "#include \"c.h\"\n" 22167 "\n" 22168 "int main() {\n" 22169 " return 0;\n" 22170 "}"; 22171 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 22172 tooling::Replacements Replaces = toReplacements( 22173 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 22174 "#include \"b.h\"\n")}); 22175 22176 FormatStyle Style = getLLVMStyle(); 22177 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 22178 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 22179 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 22180 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 22181 auto Result = applyAllReplacements(Code, *FormattedReplaces); 22182 EXPECT_TRUE(static_cast<bool>(Result)); 22183 EXPECT_EQ(Expected, *Result); 22184 } 22185 22186 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 22187 EXPECT_EQ("using std::cin;\n" 22188 "using std::cout;", 22189 format("using std::cout;\n" 22190 "using std::cin;", 22191 getGoogleStyle())); 22192 } 22193 22194 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 22195 FormatStyle Style = getLLVMStyle(); 22196 Style.Standard = FormatStyle::LS_Cpp03; 22197 // cpp03 recognize this string as identifier u8 and literal character 'a' 22198 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 22199 } 22200 22201 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 22202 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 22203 // all modes, including C++11, C++14 and C++17 22204 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 22205 } 22206 22207 TEST_F(FormatTest, DoNotFormatLikelyXml) { 22208 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 22209 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 22210 } 22211 22212 TEST_F(FormatTest, StructuredBindings) { 22213 // Structured bindings is a C++17 feature. 22214 // all modes, including C++11, C++14 and C++17 22215 verifyFormat("auto [a, b] = f();"); 22216 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 22217 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 22218 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 22219 EXPECT_EQ("auto const volatile [a, b] = f();", 22220 format("auto const volatile[a, b] = f();")); 22221 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 22222 EXPECT_EQ("auto &[a, b, c] = f();", 22223 format("auto &[ a , b,c ] = f();")); 22224 EXPECT_EQ("auto &&[a, b, c] = f();", 22225 format("auto &&[ a , b,c ] = f();")); 22226 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 22227 EXPECT_EQ("auto const volatile &&[a, b] = f();", 22228 format("auto const volatile &&[a, b] = f();")); 22229 EXPECT_EQ("auto const &&[a, b] = f();", 22230 format("auto const && [a, b] = f();")); 22231 EXPECT_EQ("const auto &[a, b] = f();", 22232 format("const auto & [a, b] = f();")); 22233 EXPECT_EQ("const auto volatile &&[a, b] = f();", 22234 format("const auto volatile &&[a, b] = f();")); 22235 EXPECT_EQ("volatile const auto &&[a, b] = f();", 22236 format("volatile const auto &&[a, b] = f();")); 22237 EXPECT_EQ("const auto &&[a, b] = f();", 22238 format("const auto && [a, b] = f();")); 22239 22240 // Make sure we don't mistake structured bindings for lambdas. 22241 FormatStyle PointerMiddle = getLLVMStyle(); 22242 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 22243 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 22244 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 22245 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 22246 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 22247 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 22248 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 22249 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 22250 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 22251 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 22252 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 22253 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 22254 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 22255 22256 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 22257 format("for (const auto && [a, b] : some_range) {\n}")); 22258 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 22259 format("for (const auto & [a, b] : some_range) {\n}")); 22260 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 22261 format("for (const auto[a, b] : some_range) {\n}")); 22262 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 22263 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 22264 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 22265 EXPECT_EQ("auto const &[x, y](expr);", 22266 format("auto const & [x,y] (expr);")); 22267 EXPECT_EQ("auto const &&[x, y](expr);", 22268 format("auto const && [x,y] (expr);")); 22269 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 22270 EXPECT_EQ("auto const &[x, y]{expr};", 22271 format("auto const & [x,y] {expr};")); 22272 EXPECT_EQ("auto const &&[x, y]{expr};", 22273 format("auto const && [x,y] {expr};")); 22274 22275 FormatStyle Spaces = getLLVMStyle(); 22276 Spaces.SpacesInSquareBrackets = true; 22277 verifyFormat("auto [ a, b ] = f();", Spaces); 22278 verifyFormat("auto &&[ a, b ] = f();", Spaces); 22279 verifyFormat("auto &[ a, b ] = f();", Spaces); 22280 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 22281 verifyFormat("auto const &[ a, b ] = f();", Spaces); 22282 } 22283 22284 TEST_F(FormatTest, FileAndCode) { 22285 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 22286 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 22287 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 22288 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 22289 EXPECT_EQ(FormatStyle::LK_ObjC, 22290 guessLanguage("foo.h", "@interface Foo\n@end\n")); 22291 EXPECT_EQ( 22292 FormatStyle::LK_ObjC, 22293 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 22294 EXPECT_EQ(FormatStyle::LK_ObjC, 22295 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 22296 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 22297 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 22298 EXPECT_EQ(FormatStyle::LK_ObjC, 22299 guessLanguage("foo", "@interface Foo\n@end\n")); 22300 EXPECT_EQ(FormatStyle::LK_ObjC, 22301 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 22302 EXPECT_EQ( 22303 FormatStyle::LK_ObjC, 22304 guessLanguage("foo.h", 22305 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 22306 EXPECT_EQ( 22307 FormatStyle::LK_Cpp, 22308 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 22309 } 22310 22311 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 22312 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 22313 EXPECT_EQ(FormatStyle::LK_ObjC, 22314 guessLanguage("foo.h", "array[[calculator getIndex]];")); 22315 EXPECT_EQ(FormatStyle::LK_Cpp, 22316 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 22317 EXPECT_EQ( 22318 FormatStyle::LK_Cpp, 22319 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 22320 EXPECT_EQ(FormatStyle::LK_ObjC, 22321 guessLanguage("foo.h", "[[noreturn foo] bar];")); 22322 EXPECT_EQ(FormatStyle::LK_Cpp, 22323 guessLanguage("foo.h", "[[clang::fallthrough]];")); 22324 EXPECT_EQ(FormatStyle::LK_ObjC, 22325 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 22326 EXPECT_EQ(FormatStyle::LK_Cpp, 22327 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 22328 EXPECT_EQ(FormatStyle::LK_Cpp, 22329 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 22330 EXPECT_EQ(FormatStyle::LK_ObjC, 22331 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 22332 EXPECT_EQ(FormatStyle::LK_Cpp, 22333 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 22334 EXPECT_EQ( 22335 FormatStyle::LK_Cpp, 22336 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 22337 EXPECT_EQ( 22338 FormatStyle::LK_Cpp, 22339 guessLanguage("foo.h", 22340 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 22341 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 22342 } 22343 22344 TEST_F(FormatTest, GuessLanguageWithCaret) { 22345 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 22346 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 22347 EXPECT_EQ(FormatStyle::LK_ObjC, 22348 guessLanguage("foo.h", "int(^)(char, float);")); 22349 EXPECT_EQ(FormatStyle::LK_ObjC, 22350 guessLanguage("foo.h", "int(^foo)(char, float);")); 22351 EXPECT_EQ(FormatStyle::LK_ObjC, 22352 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 22353 EXPECT_EQ(FormatStyle::LK_ObjC, 22354 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 22355 EXPECT_EQ( 22356 FormatStyle::LK_ObjC, 22357 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 22358 } 22359 22360 TEST_F(FormatTest, GuessLanguageWithPragmas) { 22361 EXPECT_EQ(FormatStyle::LK_Cpp, 22362 guessLanguage("foo.h", "__pragma(warning(disable:))")); 22363 EXPECT_EQ(FormatStyle::LK_Cpp, 22364 guessLanguage("foo.h", "#pragma(warning(disable:))")); 22365 EXPECT_EQ(FormatStyle::LK_Cpp, 22366 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 22367 } 22368 22369 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 22370 // ASM symbolic names are identifiers that must be surrounded by [] without 22371 // space in between: 22372 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 22373 22374 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 22375 verifyFormat(R"(// 22376 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 22377 )"); 22378 22379 // A list of several ASM symbolic names. 22380 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 22381 22382 // ASM symbolic names in inline ASM with inputs and outputs. 22383 verifyFormat(R"(// 22384 asm("cmoveq %1, %2, %[result]" 22385 : [result] "=r"(result) 22386 : "r"(test), "r"(new), "[result]"(old)); 22387 )"); 22388 22389 // ASM symbolic names in inline ASM with no outputs. 22390 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 22391 } 22392 22393 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 22394 EXPECT_EQ(FormatStyle::LK_Cpp, 22395 guessLanguage("foo.h", "void f() {\n" 22396 " asm (\"mov %[e], %[d]\"\n" 22397 " : [d] \"=rm\" (d)\n" 22398 " [e] \"rm\" (*e));\n" 22399 "}")); 22400 EXPECT_EQ(FormatStyle::LK_Cpp, 22401 guessLanguage("foo.h", "void f() {\n" 22402 " _asm (\"mov %[e], %[d]\"\n" 22403 " : [d] \"=rm\" (d)\n" 22404 " [e] \"rm\" (*e));\n" 22405 "}")); 22406 EXPECT_EQ(FormatStyle::LK_Cpp, 22407 guessLanguage("foo.h", "void f() {\n" 22408 " __asm (\"mov %[e], %[d]\"\n" 22409 " : [d] \"=rm\" (d)\n" 22410 " [e] \"rm\" (*e));\n" 22411 "}")); 22412 EXPECT_EQ(FormatStyle::LK_Cpp, 22413 guessLanguage("foo.h", "void f() {\n" 22414 " __asm__ (\"mov %[e], %[d]\"\n" 22415 " : [d] \"=rm\" (d)\n" 22416 " [e] \"rm\" (*e));\n" 22417 "}")); 22418 EXPECT_EQ(FormatStyle::LK_Cpp, 22419 guessLanguage("foo.h", "void f() {\n" 22420 " asm (\"mov %[e], %[d]\"\n" 22421 " : [d] \"=rm\" (d),\n" 22422 " [e] \"rm\" (*e));\n" 22423 "}")); 22424 EXPECT_EQ(FormatStyle::LK_Cpp, 22425 guessLanguage("foo.h", "void f() {\n" 22426 " asm volatile (\"mov %[e], %[d]\"\n" 22427 " : [d] \"=rm\" (d)\n" 22428 " [e] \"rm\" (*e));\n" 22429 "}")); 22430 } 22431 22432 TEST_F(FormatTest, GuessLanguageWithChildLines) { 22433 EXPECT_EQ(FormatStyle::LK_Cpp, 22434 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 22435 EXPECT_EQ(FormatStyle::LK_ObjC, 22436 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 22437 EXPECT_EQ( 22438 FormatStyle::LK_Cpp, 22439 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 22440 EXPECT_EQ( 22441 FormatStyle::LK_ObjC, 22442 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 22443 } 22444 22445 TEST_F(FormatTest, TypenameMacros) { 22446 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 22447 22448 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 22449 FormatStyle Google = getGoogleStyleWithColumns(0); 22450 Google.TypenameMacros = TypenameMacros; 22451 verifyFormat("struct foo {\n" 22452 " int bar;\n" 22453 " TAILQ_ENTRY(a) bleh;\n" 22454 "};", 22455 Google); 22456 22457 FormatStyle Macros = getLLVMStyle(); 22458 Macros.TypenameMacros = TypenameMacros; 22459 22460 verifyFormat("STACK_OF(int) a;", Macros); 22461 verifyFormat("STACK_OF(int) *a;", Macros); 22462 verifyFormat("STACK_OF(int const *) *a;", Macros); 22463 verifyFormat("STACK_OF(int *const) *a;", Macros); 22464 verifyFormat("STACK_OF(int, string) a;", Macros); 22465 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 22466 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 22467 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 22468 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 22469 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 22470 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 22471 22472 Macros.PointerAlignment = FormatStyle::PAS_Left; 22473 verifyFormat("STACK_OF(int)* a;", Macros); 22474 verifyFormat("STACK_OF(int*)* a;", Macros); 22475 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 22476 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 22477 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 22478 } 22479 22480 TEST_F(FormatTest, AtomicQualifier) { 22481 // Check that we treate _Atomic as a type and not a function call 22482 FormatStyle Google = getGoogleStyleWithColumns(0); 22483 verifyFormat("struct foo {\n" 22484 " int a1;\n" 22485 " _Atomic(a) a2;\n" 22486 " _Atomic(_Atomic(int) *const) a3;\n" 22487 "};", 22488 Google); 22489 verifyFormat("_Atomic(uint64_t) a;"); 22490 verifyFormat("_Atomic(uint64_t) *a;"); 22491 verifyFormat("_Atomic(uint64_t const *) *a;"); 22492 verifyFormat("_Atomic(uint64_t *const) *a;"); 22493 verifyFormat("_Atomic(const uint64_t *) *a;"); 22494 verifyFormat("_Atomic(uint64_t) a;"); 22495 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 22496 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 22497 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 22498 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 22499 22500 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 22501 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 22502 FormatStyle Style = getLLVMStyle(); 22503 Style.PointerAlignment = FormatStyle::PAS_Left; 22504 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 22505 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 22506 verifyFormat("_Atomic(int)* a;", Style); 22507 verifyFormat("_Atomic(int*)* a;", Style); 22508 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 22509 22510 Style.SpacesInCStyleCastParentheses = true; 22511 Style.SpacesInParentheses = false; 22512 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 22513 Style.SpacesInCStyleCastParentheses = false; 22514 Style.SpacesInParentheses = true; 22515 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 22516 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 22517 } 22518 22519 TEST_F(FormatTest, AmbersandInLamda) { 22520 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 22521 FormatStyle AlignStyle = getLLVMStyle(); 22522 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 22523 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 22524 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 22525 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 22526 } 22527 22528 TEST_F(FormatTest, SpacesInConditionalStatement) { 22529 FormatStyle Spaces = getLLVMStyle(); 22530 Spaces.IfMacros.clear(); 22531 Spaces.IfMacros.push_back("MYIF"); 22532 Spaces.SpacesInConditionalStatement = true; 22533 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 22534 verifyFormat("if ( !a )\n return;", Spaces); 22535 verifyFormat("if ( a )\n return;", Spaces); 22536 verifyFormat("if constexpr ( a )\n return;", Spaces); 22537 verifyFormat("MYIF ( a )\n return;", Spaces); 22538 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 22539 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 22540 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 22541 verifyFormat("while ( a )\n return;", Spaces); 22542 verifyFormat("while ( (a && b) )\n return;", Spaces); 22543 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 22544 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 22545 // Check that space on the left of "::" is inserted as expected at beginning 22546 // of condition. 22547 verifyFormat("while ( ::func() )\n return;", Spaces); 22548 22549 // Check impact of ControlStatementsExceptControlMacros is honored. 22550 Spaces.SpaceBeforeParens = 22551 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 22552 verifyFormat("MYIF( a )\n return;", Spaces); 22553 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 22554 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 22555 } 22556 22557 TEST_F(FormatTest, AlternativeOperators) { 22558 // Test case for ensuring alternate operators are not 22559 // combined with their right most neighbour. 22560 verifyFormat("int a and b;"); 22561 verifyFormat("int a and_eq b;"); 22562 verifyFormat("int a bitand b;"); 22563 verifyFormat("int a bitor b;"); 22564 verifyFormat("int a compl b;"); 22565 verifyFormat("int a not b;"); 22566 verifyFormat("int a not_eq b;"); 22567 verifyFormat("int a or b;"); 22568 verifyFormat("int a xor b;"); 22569 verifyFormat("int a xor_eq b;"); 22570 verifyFormat("return this not_eq bitand other;"); 22571 verifyFormat("bool operator not_eq(const X bitand other)"); 22572 22573 verifyFormat("int a and 5;"); 22574 verifyFormat("int a and_eq 5;"); 22575 verifyFormat("int a bitand 5;"); 22576 verifyFormat("int a bitor 5;"); 22577 verifyFormat("int a compl 5;"); 22578 verifyFormat("int a not 5;"); 22579 verifyFormat("int a not_eq 5;"); 22580 verifyFormat("int a or 5;"); 22581 verifyFormat("int a xor 5;"); 22582 verifyFormat("int a xor_eq 5;"); 22583 22584 verifyFormat("int a compl(5);"); 22585 verifyFormat("int a not(5);"); 22586 22587 /* FIXME handle alternate tokens 22588 * https://en.cppreference.com/w/cpp/language/operator_alternative 22589 // alternative tokens 22590 verifyFormat("compl foo();"); // ~foo(); 22591 verifyFormat("foo() <%%>;"); // foo(); 22592 verifyFormat("void foo() <%%>;"); // void foo(){} 22593 verifyFormat("int a <:1:>;"); // int a[1];[ 22594 verifyFormat("%:define ABC abc"); // #define ABC abc 22595 verifyFormat("%:%:"); // ## 22596 */ 22597 } 22598 22599 TEST_F(FormatTest, STLWhileNotDefineChed) { 22600 verifyFormat("#if defined(while)\n" 22601 "#define while EMIT WARNING C4005\n" 22602 "#endif // while"); 22603 } 22604 22605 TEST_F(FormatTest, OperatorSpacing) { 22606 FormatStyle Style = getLLVMStyle(); 22607 Style.PointerAlignment = FormatStyle::PAS_Right; 22608 verifyFormat("Foo::operator*();", Style); 22609 verifyFormat("Foo::operator void *();", Style); 22610 verifyFormat("Foo::operator void **();", Style); 22611 verifyFormat("Foo::operator void *&();", Style); 22612 verifyFormat("Foo::operator void *&&();", Style); 22613 verifyFormat("Foo::operator void const *();", Style); 22614 verifyFormat("Foo::operator void const **();", Style); 22615 verifyFormat("Foo::operator void const *&();", Style); 22616 verifyFormat("Foo::operator void const *&&();", Style); 22617 verifyFormat("Foo::operator()(void *);", Style); 22618 verifyFormat("Foo::operator*(void *);", Style); 22619 verifyFormat("Foo::operator*();", Style); 22620 verifyFormat("Foo::operator**();", Style); 22621 verifyFormat("Foo::operator&();", Style); 22622 verifyFormat("Foo::operator<int> *();", Style); 22623 verifyFormat("Foo::operator<Foo> *();", Style); 22624 verifyFormat("Foo::operator<int> **();", Style); 22625 verifyFormat("Foo::operator<Foo> **();", Style); 22626 verifyFormat("Foo::operator<int> &();", Style); 22627 verifyFormat("Foo::operator<Foo> &();", Style); 22628 verifyFormat("Foo::operator<int> &&();", Style); 22629 verifyFormat("Foo::operator<Foo> &&();", Style); 22630 verifyFormat("Foo::operator<int> *&();", Style); 22631 verifyFormat("Foo::operator<Foo> *&();", Style); 22632 verifyFormat("Foo::operator<int> *&&();", Style); 22633 verifyFormat("Foo::operator<Foo> *&&();", Style); 22634 verifyFormat("operator*(int (*)(), class Foo);", Style); 22635 22636 verifyFormat("Foo::operator&();", Style); 22637 verifyFormat("Foo::operator void &();", Style); 22638 verifyFormat("Foo::operator void const &();", Style); 22639 verifyFormat("Foo::operator()(void &);", Style); 22640 verifyFormat("Foo::operator&(void &);", Style); 22641 verifyFormat("Foo::operator&();", Style); 22642 verifyFormat("operator&(int (&)(), class Foo);", Style); 22643 verifyFormat("operator&&(int (&)(), class Foo);", Style); 22644 22645 verifyFormat("Foo::operator&&();", Style); 22646 verifyFormat("Foo::operator**();", Style); 22647 verifyFormat("Foo::operator void &&();", Style); 22648 verifyFormat("Foo::operator void const &&();", Style); 22649 verifyFormat("Foo::operator()(void &&);", Style); 22650 verifyFormat("Foo::operator&&(void &&);", Style); 22651 verifyFormat("Foo::operator&&();", Style); 22652 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22653 verifyFormat("operator const nsTArrayRight<E> &()", Style); 22654 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 22655 Style); 22656 verifyFormat("operator void **()", Style); 22657 verifyFormat("operator const FooRight<Object> &()", Style); 22658 verifyFormat("operator const FooRight<Object> *()", Style); 22659 verifyFormat("operator const FooRight<Object> **()", Style); 22660 verifyFormat("operator const FooRight<Object> *&()", Style); 22661 verifyFormat("operator const FooRight<Object> *&&()", Style); 22662 22663 Style.PointerAlignment = FormatStyle::PAS_Left; 22664 verifyFormat("Foo::operator*();", Style); 22665 verifyFormat("Foo::operator**();", Style); 22666 verifyFormat("Foo::operator void*();", Style); 22667 verifyFormat("Foo::operator void**();", Style); 22668 verifyFormat("Foo::operator void*&();", Style); 22669 verifyFormat("Foo::operator void*&&();", Style); 22670 verifyFormat("Foo::operator void const*();", Style); 22671 verifyFormat("Foo::operator void const**();", Style); 22672 verifyFormat("Foo::operator void const*&();", Style); 22673 verifyFormat("Foo::operator void const*&&();", Style); 22674 verifyFormat("Foo::operator/*comment*/ void*();", Style); 22675 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 22676 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 22677 verifyFormat("Foo::operator()(void*);", Style); 22678 verifyFormat("Foo::operator*(void*);", Style); 22679 verifyFormat("Foo::operator*();", Style); 22680 verifyFormat("Foo::operator<int>*();", Style); 22681 verifyFormat("Foo::operator<Foo>*();", Style); 22682 verifyFormat("Foo::operator<int>**();", Style); 22683 verifyFormat("Foo::operator<Foo>**();", Style); 22684 verifyFormat("Foo::operator<Foo>*&();", Style); 22685 verifyFormat("Foo::operator<int>&();", Style); 22686 verifyFormat("Foo::operator<Foo>&();", Style); 22687 verifyFormat("Foo::operator<int>&&();", Style); 22688 verifyFormat("Foo::operator<Foo>&&();", Style); 22689 verifyFormat("Foo::operator<int>*&();", Style); 22690 verifyFormat("Foo::operator<Foo>*&();", Style); 22691 verifyFormat("operator*(int (*)(), class Foo);", Style); 22692 22693 verifyFormat("Foo::operator&();", Style); 22694 verifyFormat("Foo::operator void&();", Style); 22695 verifyFormat("Foo::operator void const&();", Style); 22696 verifyFormat("Foo::operator/*comment*/ void&();", Style); 22697 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 22698 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 22699 verifyFormat("Foo::operator()(void&);", Style); 22700 verifyFormat("Foo::operator&(void&);", Style); 22701 verifyFormat("Foo::operator&();", Style); 22702 verifyFormat("operator&(int (&)(), class Foo);", Style); 22703 verifyFormat("operator&(int (&&)(), class Foo);", Style); 22704 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22705 22706 verifyFormat("Foo::operator&&();", Style); 22707 verifyFormat("Foo::operator void&&();", Style); 22708 verifyFormat("Foo::operator void const&&();", Style); 22709 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 22710 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 22711 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 22712 verifyFormat("Foo::operator()(void&&);", Style); 22713 verifyFormat("Foo::operator&&(void&&);", Style); 22714 verifyFormat("Foo::operator&&();", Style); 22715 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22716 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 22717 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 22718 Style); 22719 verifyFormat("operator void**()", Style); 22720 verifyFormat("operator const FooLeft<Object>&()", Style); 22721 verifyFormat("operator const FooLeft<Object>*()", Style); 22722 verifyFormat("operator const FooLeft<Object>**()", Style); 22723 verifyFormat("operator const FooLeft<Object>*&()", Style); 22724 verifyFormat("operator const FooLeft<Object>*&&()", Style); 22725 22726 // PR45107 22727 verifyFormat("operator Vector<String>&();", Style); 22728 verifyFormat("operator const Vector<String>&();", Style); 22729 verifyFormat("operator foo::Bar*();", Style); 22730 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 22731 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 22732 Style); 22733 22734 Style.PointerAlignment = FormatStyle::PAS_Middle; 22735 verifyFormat("Foo::operator*();", Style); 22736 verifyFormat("Foo::operator void *();", Style); 22737 verifyFormat("Foo::operator()(void *);", Style); 22738 verifyFormat("Foo::operator*(void *);", Style); 22739 verifyFormat("Foo::operator*();", Style); 22740 verifyFormat("operator*(int (*)(), class Foo);", Style); 22741 22742 verifyFormat("Foo::operator&();", Style); 22743 verifyFormat("Foo::operator void &();", Style); 22744 verifyFormat("Foo::operator void const &();", Style); 22745 verifyFormat("Foo::operator()(void &);", Style); 22746 verifyFormat("Foo::operator&(void &);", Style); 22747 verifyFormat("Foo::operator&();", Style); 22748 verifyFormat("operator&(int (&)(), class Foo);", Style); 22749 22750 verifyFormat("Foo::operator&&();", Style); 22751 verifyFormat("Foo::operator void &&();", Style); 22752 verifyFormat("Foo::operator void const &&();", Style); 22753 verifyFormat("Foo::operator()(void &&);", Style); 22754 verifyFormat("Foo::operator&&(void &&);", Style); 22755 verifyFormat("Foo::operator&&();", Style); 22756 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22757 } 22758 22759 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 22760 FormatStyle Style = getLLVMStyle(); 22761 // PR46157 22762 verifyFormat("foo(operator+, -42);", Style); 22763 verifyFormat("foo(operator++, -42);", Style); 22764 verifyFormat("foo(operator--, -42);", Style); 22765 verifyFormat("foo(-42, operator--);", Style); 22766 verifyFormat("foo(-42, operator, );", Style); 22767 verifyFormat("foo(operator, , -42);", Style); 22768 } 22769 22770 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 22771 FormatStyle Style = getLLVMStyle(); 22772 Style.WhitespaceSensitiveMacros.push_back("FOO"); 22773 22774 // Don't use the helpers here, since 'mess up' will change the whitespace 22775 // and these are all whitespace sensitive by definition 22776 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 22777 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 22778 EXPECT_EQ( 22779 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 22780 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 22781 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 22782 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 22783 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 22784 " Still=Intentional);", 22785 format("FOO(String-ized&Messy+But,: :\n" 22786 " Still=Intentional);", 22787 Style)); 22788 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 22789 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 22790 " Still=Intentional);", 22791 format("FOO(String-ized=&Messy+But,: :\n" 22792 " Still=Intentional);", 22793 Style)); 22794 22795 Style.ColumnLimit = 21; 22796 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 22797 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 22798 } 22799 22800 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 22801 // These tests are not in NamespaceFixer because that doesn't 22802 // test its interaction with line wrapping 22803 FormatStyle Style = getLLVMStyleWithColumns(80); 22804 verifyFormat("namespace {\n" 22805 "int i;\n" 22806 "int j;\n" 22807 "} // namespace", 22808 Style); 22809 22810 verifyFormat("namespace AAA {\n" 22811 "int i;\n" 22812 "int j;\n" 22813 "} // namespace AAA", 22814 Style); 22815 22816 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 22817 "int i;\n" 22818 "int j;\n" 22819 "} // namespace Averyveryveryverylongnamespace", 22820 format("namespace Averyveryveryverylongnamespace {\n" 22821 "int i;\n" 22822 "int j;\n" 22823 "}", 22824 Style)); 22825 22826 EXPECT_EQ( 22827 "namespace " 22828 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22829 " went::mad::now {\n" 22830 "int i;\n" 22831 "int j;\n" 22832 "} // namespace\n" 22833 " // " 22834 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22835 "went::mad::now", 22836 format("namespace " 22837 "would::it::save::you::a::lot::of::time::if_::i::" 22838 "just::gave::up::and_::went::mad::now {\n" 22839 "int i;\n" 22840 "int j;\n" 22841 "}", 22842 Style)); 22843 22844 // This used to duplicate the comment again and again on subsequent runs 22845 EXPECT_EQ( 22846 "namespace " 22847 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22848 " went::mad::now {\n" 22849 "int i;\n" 22850 "int j;\n" 22851 "} // namespace\n" 22852 " // " 22853 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22854 "went::mad::now", 22855 format("namespace " 22856 "would::it::save::you::a::lot::of::time::if_::i::" 22857 "just::gave::up::and_::went::mad::now {\n" 22858 "int i;\n" 22859 "int j;\n" 22860 "} // namespace\n" 22861 " // " 22862 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 22863 "and_::went::mad::now", 22864 Style)); 22865 } 22866 22867 TEST_F(FormatTest, LikelyUnlikely) { 22868 FormatStyle Style = getLLVMStyle(); 22869 22870 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22871 " return 29;\n" 22872 "}", 22873 Style); 22874 22875 verifyFormat("if (argc > 5) [[likely]] {\n" 22876 " return 29;\n" 22877 "}", 22878 Style); 22879 22880 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22881 " return 29;\n" 22882 "} else [[likely]] {\n" 22883 " return 42;\n" 22884 "}\n", 22885 Style); 22886 22887 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22888 " return 29;\n" 22889 "} else if (argc > 10) [[likely]] {\n" 22890 " return 99;\n" 22891 "} else {\n" 22892 " return 42;\n" 22893 "}\n", 22894 Style); 22895 22896 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 22897 " return 29;\n" 22898 "}", 22899 Style); 22900 22901 verifyFormat("if (argc > 5) [[unlikely]]\n" 22902 " return 29;\n", 22903 Style); 22904 verifyFormat("if (argc > 5) [[likely]]\n" 22905 " return 29;\n", 22906 Style); 22907 22908 Style.AttributeMacros.push_back("UNLIKELY"); 22909 Style.AttributeMacros.push_back("LIKELY"); 22910 verifyFormat("if (argc > 5) UNLIKELY\n" 22911 " return 29;\n", 22912 Style); 22913 22914 verifyFormat("if (argc > 5) UNLIKELY {\n" 22915 " return 29;\n" 22916 "}", 22917 Style); 22918 verifyFormat("if (argc > 5) UNLIKELY {\n" 22919 " return 29;\n" 22920 "} else [[likely]] {\n" 22921 " return 42;\n" 22922 "}\n", 22923 Style); 22924 verifyFormat("if (argc > 5) UNLIKELY {\n" 22925 " return 29;\n" 22926 "} else LIKELY {\n" 22927 " return 42;\n" 22928 "}\n", 22929 Style); 22930 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22931 " return 29;\n" 22932 "} else LIKELY {\n" 22933 " return 42;\n" 22934 "}\n", 22935 Style); 22936 } 22937 22938 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 22939 verifyFormat("Constructor()\n" 22940 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22941 " aaaa(aaaaaaaaaaaaaaaaaa, " 22942 "aaaaaaaaaaaaaaaaaat))"); 22943 verifyFormat("Constructor()\n" 22944 " : aaaaaaaaaaaaa(aaaaaa), " 22945 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 22946 22947 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 22948 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 22949 verifyFormat("Constructor()\n" 22950 " : aaaaaa(aaaaaa),\n" 22951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22952 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 22953 StyleWithWhitespacePenalty); 22954 verifyFormat("Constructor()\n" 22955 " : aaaaaaaaaaaaa(aaaaaa), " 22956 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 22957 StyleWithWhitespacePenalty); 22958 } 22959 22960 TEST_F(FormatTest, LLVMDefaultStyle) { 22961 FormatStyle Style = getLLVMStyle(); 22962 verifyFormat("extern \"C\" {\n" 22963 "int foo();\n" 22964 "}", 22965 Style); 22966 } 22967 TEST_F(FormatTest, GNUDefaultStyle) { 22968 FormatStyle Style = getGNUStyle(); 22969 verifyFormat("extern \"C\"\n" 22970 "{\n" 22971 " int foo ();\n" 22972 "}", 22973 Style); 22974 } 22975 TEST_F(FormatTest, MozillaDefaultStyle) { 22976 FormatStyle Style = getMozillaStyle(); 22977 verifyFormat("extern \"C\"\n" 22978 "{\n" 22979 " int foo();\n" 22980 "}", 22981 Style); 22982 } 22983 TEST_F(FormatTest, GoogleDefaultStyle) { 22984 FormatStyle Style = getGoogleStyle(); 22985 verifyFormat("extern \"C\" {\n" 22986 "int foo();\n" 22987 "}", 22988 Style); 22989 } 22990 TEST_F(FormatTest, ChromiumDefaultStyle) { 22991 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 22992 verifyFormat("extern \"C\" {\n" 22993 "int foo();\n" 22994 "}", 22995 Style); 22996 } 22997 TEST_F(FormatTest, MicrosoftDefaultStyle) { 22998 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 22999 verifyFormat("extern \"C\"\n" 23000 "{\n" 23001 " int foo();\n" 23002 "}", 23003 Style); 23004 } 23005 TEST_F(FormatTest, WebKitDefaultStyle) { 23006 FormatStyle Style = getWebKitStyle(); 23007 verifyFormat("extern \"C\" {\n" 23008 "int foo();\n" 23009 "}", 23010 Style); 23011 } 23012 23013 TEST_F(FormatTest, ConceptsAndRequires) { 23014 FormatStyle Style = getLLVMStyle(); 23015 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 23016 23017 verifyFormat("template <typename T>\n" 23018 "concept Hashable = requires(T a) {\n" 23019 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 23020 "};", 23021 Style); 23022 verifyFormat("template <typename T>\n" 23023 "concept EqualityComparable = requires(T a, T b) {\n" 23024 " { a == b } -> bool;\n" 23025 "};", 23026 Style); 23027 verifyFormat("template <typename T>\n" 23028 "concept EqualityComparable = requires(T a, T b) {\n" 23029 " { a == b } -> bool;\n" 23030 " { a != b } -> bool;\n" 23031 "};", 23032 Style); 23033 verifyFormat("template <typename T>\n" 23034 "concept EqualityComparable = requires(T a, T b) {\n" 23035 " { a == b } -> bool;\n" 23036 " { a != b } -> bool;\n" 23037 "};", 23038 Style); 23039 23040 verifyFormat("template <typename It>\n" 23041 "requires Iterator<It>\n" 23042 "void sort(It begin, It end) {\n" 23043 " //....\n" 23044 "}", 23045 Style); 23046 23047 verifyFormat("template <typename T>\n" 23048 "concept Large = sizeof(T) > 10;", 23049 Style); 23050 23051 verifyFormat("template <typename T, typename U>\n" 23052 "concept FooableWith = requires(T t, U u) {\n" 23053 " typename T::foo_type;\n" 23054 " { t.foo(u) } -> typename T::foo_type;\n" 23055 " t++;\n" 23056 "};\n" 23057 "void doFoo(FooableWith<int> auto t) {\n" 23058 " t.foo(3);\n" 23059 "}", 23060 Style); 23061 verifyFormat("template <typename T>\n" 23062 "concept Context = sizeof(T) == 1;", 23063 Style); 23064 verifyFormat("template <typename T>\n" 23065 "concept Context = is_specialization_of_v<context, T>;", 23066 Style); 23067 verifyFormat("template <typename T>\n" 23068 "concept Node = std::is_object_v<T>;", 23069 Style); 23070 verifyFormat("template <typename T>\n" 23071 "concept Tree = true;", 23072 Style); 23073 23074 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 23075 " //...\n" 23076 "}", 23077 Style); 23078 23079 verifyFormat( 23080 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 23081 " //...\n" 23082 "}", 23083 Style); 23084 23085 verifyFormat( 23086 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 23087 " //...\n" 23088 "}", 23089 Style); 23090 23091 verifyFormat("template <typename T>\n" 23092 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 23093 "Concept2<I> {\n" 23094 " //...\n" 23095 "}", 23096 Style); 23097 23098 verifyFormat("template <typename T>\n" 23099 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 23100 "Concept2<I> {\n" 23101 " //...\n" 23102 "}", 23103 Style); 23104 23105 verifyFormat( 23106 "template <typename T>\n" 23107 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 23108 " //...\n" 23109 "}", 23110 Style); 23111 23112 verifyFormat( 23113 "template <typename T>\n" 23114 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 23115 " //...\n" 23116 "}", 23117 Style); 23118 23119 verifyFormat("template <typename It>\n" 23120 "requires Foo<It>() && Bar<It> {\n" 23121 " //....\n" 23122 "}", 23123 Style); 23124 23125 verifyFormat("template <typename It>\n" 23126 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 23127 " //....\n" 23128 "}", 23129 Style); 23130 23131 verifyFormat("template <typename It>\n" 23132 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 23133 " //....\n" 23134 "}", 23135 Style); 23136 23137 verifyFormat( 23138 "template <typename It>\n" 23139 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 23140 " //....\n" 23141 "}", 23142 Style); 23143 23144 Style.IndentRequires = true; 23145 verifyFormat("template <typename It>\n" 23146 " requires Iterator<It>\n" 23147 "void sort(It begin, It end) {\n" 23148 " //....\n" 23149 "}", 23150 Style); 23151 verifyFormat("template <std::size index_>\n" 23152 " requires(index_ < sizeof...(Children_))\n" 23153 "Tree auto &child() {\n" 23154 " // ...\n" 23155 "}", 23156 Style); 23157 23158 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 23159 verifyFormat("template <typename T>\n" 23160 "concept Hashable = requires (T a) {\n" 23161 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 23162 "};", 23163 Style); 23164 23165 verifyFormat("template <class T = void>\n" 23166 " requires EqualityComparable<T> || Same<T, void>\n" 23167 "struct equal_to;", 23168 Style); 23169 23170 verifyFormat("template <class T>\n" 23171 " requires requires {\n" 23172 " T{};\n" 23173 " T (int);\n" 23174 " }\n", 23175 Style); 23176 23177 Style.ColumnLimit = 78; 23178 verifyFormat("template <typename T>\n" 23179 "concept Context = Traits<typename T::traits_type> and\n" 23180 " Interface<typename T::interface_type> and\n" 23181 " Request<typename T::request_type> and\n" 23182 " Response<typename T::response_type> and\n" 23183 " ContextExtension<typename T::extension_type> and\n" 23184 " ::std::is_copy_constructable<T> and " 23185 "::std::is_move_constructable<T> and\n" 23186 " requires (T c) {\n" 23187 " { c.response; } -> Response;\n" 23188 "} and requires (T c) {\n" 23189 " { c.request; } -> Request;\n" 23190 "}\n", 23191 Style); 23192 23193 verifyFormat("template <typename T>\n" 23194 "concept Context = Traits<typename T::traits_type> or\n" 23195 " Interface<typename T::interface_type> or\n" 23196 " Request<typename T::request_type> or\n" 23197 " Response<typename T::response_type> or\n" 23198 " ContextExtension<typename T::extension_type> or\n" 23199 " ::std::is_copy_constructable<T> or " 23200 "::std::is_move_constructable<T> or\n" 23201 " requires (T c) {\n" 23202 " { c.response; } -> Response;\n" 23203 "} or requires (T c) {\n" 23204 " { c.request; } -> Request;\n" 23205 "}\n", 23206 Style); 23207 23208 verifyFormat("template <typename T>\n" 23209 "concept Context = Traits<typename T::traits_type> &&\n" 23210 " Interface<typename T::interface_type> &&\n" 23211 " Request<typename T::request_type> &&\n" 23212 " Response<typename T::response_type> &&\n" 23213 " ContextExtension<typename T::extension_type> &&\n" 23214 " ::std::is_copy_constructable<T> && " 23215 "::std::is_move_constructable<T> &&\n" 23216 " requires (T c) {\n" 23217 " { c.response; } -> Response;\n" 23218 "} && requires (T c) {\n" 23219 " { c.request; } -> Request;\n" 23220 "}\n", 23221 Style); 23222 23223 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 23224 "Constraint2<T>;"); 23225 23226 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 23227 Style.BraceWrapping.AfterFunction = true; 23228 Style.BraceWrapping.AfterClass = true; 23229 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 23230 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 23231 verifyFormat("void Foo () requires (std::copyable<T>)\n" 23232 "{\n" 23233 " return\n" 23234 "}\n", 23235 Style); 23236 23237 verifyFormat("void Foo () requires std::copyable<T>\n" 23238 "{\n" 23239 " return\n" 23240 "}\n", 23241 Style); 23242 23243 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 23244 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 23245 "struct constant;", 23246 Style); 23247 23248 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 23249 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 23250 "struct constant;", 23251 Style); 23252 23253 verifyFormat("template <class T>\n" 23254 "class plane_with_very_very_very_long_name\n" 23255 "{\n" 23256 " constexpr plane_with_very_very_very_long_name () requires " 23257 "std::copyable<T>\n" 23258 " : plane_with_very_very_very_long_name (1)\n" 23259 " {\n" 23260 " }\n" 23261 "}\n", 23262 Style); 23263 23264 verifyFormat("template <class T>\n" 23265 "class plane_with_long_name\n" 23266 "{\n" 23267 " constexpr plane_with_long_name () requires std::copyable<T>\n" 23268 " : plane_with_long_name (1)\n" 23269 " {\n" 23270 " }\n" 23271 "}\n", 23272 Style); 23273 23274 Style.BreakBeforeConceptDeclarations = false; 23275 verifyFormat("template <typename T> concept Tree = true;", Style); 23276 23277 Style.IndentRequires = false; 23278 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 23279 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 23280 "struct constant;", 23281 Style); 23282 } 23283 23284 TEST_F(FormatTest, StatementAttributeLikeMacros) { 23285 FormatStyle Style = getLLVMStyle(); 23286 StringRef Source = "void Foo::slot() {\n" 23287 " unsigned char MyChar = 'x';\n" 23288 " emit signal(MyChar);\n" 23289 " Q_EMIT signal(MyChar);\n" 23290 "}"; 23291 23292 EXPECT_EQ(Source, format(Source, Style)); 23293 23294 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 23295 EXPECT_EQ("void Foo::slot() {\n" 23296 " unsigned char MyChar = 'x';\n" 23297 " emit signal(MyChar);\n" 23298 " Q_EMIT signal(MyChar);\n" 23299 "}", 23300 format(Source, Style)); 23301 23302 Style.StatementAttributeLikeMacros.push_back("emit"); 23303 EXPECT_EQ(Source, format(Source, Style)); 23304 23305 Style.StatementAttributeLikeMacros = {}; 23306 EXPECT_EQ("void Foo::slot() {\n" 23307 " unsigned char MyChar = 'x';\n" 23308 " emit signal(MyChar);\n" 23309 " Q_EMIT signal(MyChar);\n" 23310 "}", 23311 format(Source, Style)); 23312 } 23313 23314 TEST_F(FormatTest, IndentAccessModifiers) { 23315 FormatStyle Style = getLLVMStyle(); 23316 Style.IndentAccessModifiers = true; 23317 // Members are *two* levels below the record; 23318 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 23319 verifyFormat("class C {\n" 23320 " int i;\n" 23321 "};\n", 23322 Style); 23323 verifyFormat("union C {\n" 23324 " int i;\n" 23325 " unsigned u;\n" 23326 "};\n", 23327 Style); 23328 // Access modifiers should be indented one level below the record. 23329 verifyFormat("class C {\n" 23330 " public:\n" 23331 " int i;\n" 23332 "};\n", 23333 Style); 23334 verifyFormat("struct S {\n" 23335 " private:\n" 23336 " class C {\n" 23337 " int j;\n" 23338 "\n" 23339 " public:\n" 23340 " C();\n" 23341 " };\n" 23342 "\n" 23343 " public:\n" 23344 " int i;\n" 23345 "};\n", 23346 Style); 23347 // Enumerations are not records and should be unaffected. 23348 Style.AllowShortEnumsOnASingleLine = false; 23349 verifyFormat("enum class E {\n" 23350 " A,\n" 23351 " B\n" 23352 "};\n", 23353 Style); 23354 // Test with a different indentation width; 23355 // also proves that the result is Style.AccessModifierOffset agnostic. 23356 Style.IndentWidth = 3; 23357 verifyFormat("class C {\n" 23358 " public:\n" 23359 " int i;\n" 23360 "};\n", 23361 Style); 23362 } 23363 23364 TEST_F(FormatTest, LimitlessStringsAndComments) { 23365 auto Style = getLLVMStyleWithColumns(0); 23366 constexpr StringRef Code = 23367 "/**\n" 23368 " * This is a multiline comment with quite some long lines, at least for " 23369 "the LLVM Style.\n" 23370 " * We will redo this with strings and line comments. Just to check if " 23371 "everything is working.\n" 23372 " */\n" 23373 "bool foo() {\n" 23374 " /* Single line multi line comment. */\n" 23375 " const std::string String = \"This is a multiline string with quite " 23376 "some long lines, at least for the LLVM Style.\"\n" 23377 " \"We already did it with multi line " 23378 "comments, and we will do it with line comments. Just to check if " 23379 "everything is working.\";\n" 23380 " // This is a line comment (block) with quite some long lines, at " 23381 "least for the LLVM Style.\n" 23382 " // We already did this with multi line comments and strings. Just to " 23383 "check if everything is working.\n" 23384 " const std::string SmallString = \"Hello World\";\n" 23385 " // Small line comment\n" 23386 " return String.size() > SmallString.size();\n" 23387 "}"; 23388 EXPECT_EQ(Code, format(Code, Style)); 23389 } 23390 23391 TEST_F(FormatTest, FormatDecayCopy) { 23392 // error cases from unit tests 23393 verifyFormat("foo(auto())"); 23394 verifyFormat("foo(auto{})"); 23395 verifyFormat("foo(auto({}))"); 23396 verifyFormat("foo(auto{{}})"); 23397 23398 verifyFormat("foo(auto(1))"); 23399 verifyFormat("foo(auto{1})"); 23400 verifyFormat("foo(new auto(1))"); 23401 verifyFormat("foo(new auto{1})"); 23402 verifyFormat("decltype(auto(1)) x;"); 23403 verifyFormat("decltype(auto{1}) x;"); 23404 verifyFormat("auto(x);"); 23405 verifyFormat("auto{x};"); 23406 verifyFormat("new auto{x};"); 23407 verifyFormat("auto{x} = y;"); 23408 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 23409 // the user's own fault 23410 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 23411 // clearly the user's own fault 23412 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 23413 } 23414 23415 TEST_F(FormatTest, Cpp20ModulesSupport) { 23416 FormatStyle Style = getLLVMStyle(); 23417 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 23418 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 23419 23420 verifyFormat("export import foo;", Style); 23421 verifyFormat("export import foo:bar;", Style); 23422 verifyFormat("export import foo.bar;", Style); 23423 verifyFormat("export import foo.bar:baz;", Style); 23424 verifyFormat("export import :bar;", Style); 23425 verifyFormat("export module foo:bar;", Style); 23426 verifyFormat("export module foo;", Style); 23427 verifyFormat("export module foo.bar;", Style); 23428 verifyFormat("export module foo.bar:baz;", Style); 23429 verifyFormat("export import <string_view>;", Style); 23430 23431 verifyFormat("export type_name var;", Style); 23432 verifyFormat("template <class T> export using A = B<T>;", Style); 23433 verifyFormat("export using A = B;", Style); 23434 verifyFormat("export int func() {\n" 23435 " foo();\n" 23436 "}", 23437 Style); 23438 verifyFormat("export struct {\n" 23439 " int foo;\n" 23440 "};", 23441 Style); 23442 verifyFormat("export {\n" 23443 " int foo;\n" 23444 "};", 23445 Style); 23446 verifyFormat("export export char const *hello() { return \"hello\"; }"); 23447 23448 verifyFormat("import bar;", Style); 23449 verifyFormat("import foo.bar;", Style); 23450 verifyFormat("import foo:bar;", Style); 23451 verifyFormat("import :bar;", Style); 23452 verifyFormat("import <ctime>;", Style); 23453 verifyFormat("import \"header\";", Style); 23454 23455 verifyFormat("module foo;", Style); 23456 verifyFormat("module foo:bar;", Style); 23457 verifyFormat("module foo.bar;", Style); 23458 verifyFormat("module;", Style); 23459 23460 verifyFormat("export namespace hi {\n" 23461 "const char *sayhi();\n" 23462 "}", 23463 Style); 23464 23465 verifyFormat("module :private;", Style); 23466 verifyFormat("import <foo/bar.h>;", Style); 23467 verifyFormat("import foo...bar;", Style); 23468 verifyFormat("import ..........;", Style); 23469 verifyFormat("module foo:private;", Style); 23470 verifyFormat("import a", Style); 23471 verifyFormat("module a", Style); 23472 verifyFormat("export import a", Style); 23473 verifyFormat("export module a", Style); 23474 23475 verifyFormat("import", Style); 23476 verifyFormat("module", Style); 23477 verifyFormat("export", Style); 23478 } 23479 23480 TEST_F(FormatTest, CoroutineForCoawait) { 23481 FormatStyle Style = getLLVMStyle(); 23482 verifyFormat("for co_await (auto x : range())\n ;"); 23483 verifyFormat("for (auto i : arr) {\n" 23484 "}", 23485 Style); 23486 verifyFormat("for co_await (auto i : arr) {\n" 23487 "}", 23488 Style); 23489 verifyFormat("for co_await (auto i : foo(T{})) {\n" 23490 "}", 23491 Style); 23492 } 23493 23494 TEST_F(FormatTest, CoroutineCoAwait) { 23495 verifyFormat("int x = co_await foo();"); 23496 verifyFormat("int x = (co_await foo());"); 23497 verifyFormat("co_await (42);"); 23498 verifyFormat("void operator co_await(int);"); 23499 verifyFormat("void operator co_await(a);"); 23500 verifyFormat("co_await a;"); 23501 verifyFormat("co_await missing_await_resume{};"); 23502 verifyFormat("co_await a; // comment"); 23503 verifyFormat("void test0() { co_await a; }"); 23504 verifyFormat("co_await co_await co_await foo();"); 23505 verifyFormat("co_await foo().bar();"); 23506 verifyFormat("co_await [this]() -> Task { co_return x; }"); 23507 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 23508 "foo(); }(x, y);"); 23509 23510 FormatStyle Style = getLLVMStyleWithColumns(40); 23511 verifyFormat("co_await [this](int a, int b) -> Task {\n" 23512 " co_return co_await foo();\n" 23513 "}(x, y);", 23514 Style); 23515 verifyFormat("co_await;"); 23516 } 23517 23518 TEST_F(FormatTest, CoroutineCoYield) { 23519 verifyFormat("int x = co_yield foo();"); 23520 verifyFormat("int x = (co_yield foo());"); 23521 verifyFormat("co_yield (42);"); 23522 verifyFormat("co_yield {42};"); 23523 verifyFormat("co_yield 42;"); 23524 verifyFormat("co_yield n++;"); 23525 verifyFormat("co_yield ++n;"); 23526 verifyFormat("co_yield;"); 23527 } 23528 23529 TEST_F(FormatTest, CoroutineCoReturn) { 23530 verifyFormat("co_return (42);"); 23531 verifyFormat("co_return;"); 23532 verifyFormat("co_return {};"); 23533 verifyFormat("co_return x;"); 23534 verifyFormat("co_return co_await foo();"); 23535 verifyFormat("co_return co_yield foo();"); 23536 } 23537 23538 TEST_F(FormatTest, EmptyShortBlock) { 23539 auto Style = getLLVMStyle(); 23540 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 23541 23542 verifyFormat("try {\n" 23543 " doA();\n" 23544 "} catch (Exception &e) {\n" 23545 " e.printStackTrace();\n" 23546 "}\n", 23547 Style); 23548 23549 verifyFormat("try {\n" 23550 " doA();\n" 23551 "} catch (Exception &e) {}\n", 23552 Style); 23553 } 23554 23555 TEST_F(FormatTest, ShortTemplatedArgumentLists) { 23556 auto Style = getLLVMStyle(); 23557 23558 verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style); 23559 verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style); 23560 verifyFormat("struct Y : X<[] { return 0; }> {};", Style); 23561 verifyFormat("struct Y<[] { return 0; }> {};", Style); 23562 23563 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style); 23564 } 23565 23566 TEST_F(FormatTest, RemoveBraces) { 23567 FormatStyle Style = getLLVMStyle(); 23568 Style.RemoveBracesLLVM = true; 23569 23570 // The following eight test cases are fully-braced versions of the examples at 23571 // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single- 23572 // statement-bodies-of-if-else-loop-statements". 23573 23574 // 1. Omit the braces, since the body is simple and clearly associated with 23575 // the if. 23576 verifyFormat("if (isa<FunctionDecl>(D))\n" 23577 " handleFunctionDecl(D);\n" 23578 "else if (isa<VarDecl>(D))\n" 23579 " handleVarDecl(D);", 23580 "if (isa<FunctionDecl>(D)) {\n" 23581 " handleFunctionDecl(D);\n" 23582 "} else if (isa<VarDecl>(D)) {\n" 23583 " handleVarDecl(D);\n" 23584 "}", 23585 Style); 23586 23587 // 2. Here we document the condition itself and not the body. 23588 verifyFormat("if (isa<VarDecl>(D)) {\n" 23589 " // It is necessary that we explain the situation with this\n" 23590 " // surprisingly long comment, so it would be unclear\n" 23591 " // without the braces whether the following statement is in\n" 23592 " // the scope of the `if`.\n" 23593 " // Because the condition is documented, we can't really\n" 23594 " // hoist this comment that applies to the body above the\n" 23595 " // if.\n" 23596 " handleOtherDecl(D);\n" 23597 "}", 23598 Style); 23599 23600 // 3. Use braces on the outer `if` to avoid a potential dangling else 23601 // situation. 23602 verifyFormat("if (isa<VarDecl>(D)) {\n" 23603 " for (auto *A : D.attrs())\n" 23604 " if (shouldProcessAttr(A))\n" 23605 " handleAttr(A);\n" 23606 "}", 23607 "if (isa<VarDecl>(D)) {\n" 23608 " for (auto *A : D.attrs()) {\n" 23609 " if (shouldProcessAttr(A)) {\n" 23610 " handleAttr(A);\n" 23611 " }\n" 23612 " }\n" 23613 "}", 23614 Style); 23615 23616 // 4. Use braces for the `if` block to keep it uniform with the else block. 23617 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 23618 " handleFunctionDecl(D);\n" 23619 "} else {\n" 23620 " // In this else case, it is necessary that we explain the\n" 23621 " // situation with this surprisingly long comment, so it\n" 23622 " // would be unclear without the braces whether the\n" 23623 " // following statement is in the scope of the `if`.\n" 23624 " handleOtherDecl(D);\n" 23625 "}", 23626 Style); 23627 23628 // 5. This should also omit braces. The `for` loop contains only a single 23629 // statement, so it shouldn't have braces. The `if` also only contains a 23630 // single simple statement (the for loop), so it also should omit braces. 23631 verifyFormat("if (isa<FunctionDecl>(D))\n" 23632 " for (auto *A : D.attrs())\n" 23633 " handleAttr(A);", 23634 "if (isa<FunctionDecl>(D)) {\n" 23635 " for (auto *A : D.attrs()) {\n" 23636 " handleAttr(A);\n" 23637 " }\n" 23638 "}", 23639 Style); 23640 23641 // 6. Use braces for the outer `if` since the nested `for` is braced. 23642 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 23643 " for (auto *A : D.attrs()) {\n" 23644 " // In this for loop body, it is necessary that we explain\n" 23645 " // the situation with this surprisingly long comment,\n" 23646 " // forcing braces on the `for` block.\n" 23647 " handleAttr(A);\n" 23648 " }\n" 23649 "}", 23650 Style); 23651 23652 // 7. Use braces on the outer block because there are more than two levels of 23653 // nesting. 23654 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 23655 " for (auto *A : D.attrs())\n" 23656 " for (ssize_t i : llvm::seq<ssize_t>(count))\n" 23657 " handleAttrOnDecl(D, A, i);\n" 23658 "}", 23659 "if (isa<FunctionDecl>(D)) {\n" 23660 " for (auto *A : D.attrs()) {\n" 23661 " for (ssize_t i : llvm::seq<ssize_t>(count)) {\n" 23662 " handleAttrOnDecl(D, A, i);\n" 23663 " }\n" 23664 " }\n" 23665 "}", 23666 Style); 23667 23668 // 8. Use braces on the outer block because of a nested `if`, otherwise the 23669 // compiler would warn: `add explicit braces to avoid dangling else` 23670 verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 23671 " if (shouldProcess(D))\n" 23672 " handleVarDecl(D);\n" 23673 " else\n" 23674 " markAsIgnored(D);\n" 23675 "}", 23676 "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 23677 " if (shouldProcess(D)) {\n" 23678 " handleVarDecl(D);\n" 23679 " } else {\n" 23680 " markAsIgnored(D);\n" 23681 " }\n" 23682 "}", 23683 Style); 23684 23685 verifyFormat("if (a)\n" 23686 " b; // comment\n" 23687 "else if (c)\n" 23688 " d; /* comment */\n" 23689 "else\n" 23690 " e;", 23691 "if (a) {\n" 23692 " b; // comment\n" 23693 "} else if (c) {\n" 23694 " d; /* comment */\n" 23695 "} else {\n" 23696 " e;\n" 23697 "}", 23698 Style); 23699 23700 verifyFormat("if (a) {\n" 23701 " b;\n" 23702 " c;\n" 23703 "} else if (d) {\n" 23704 " e;\n" 23705 "}", 23706 Style); 23707 23708 verifyFormat("if (a) {\n" 23709 "#undef NDEBUG\n" 23710 " b;\n" 23711 "} else {\n" 23712 " c;\n" 23713 "}", 23714 Style); 23715 23716 verifyFormat("if (a) {\n" 23717 " // comment\n" 23718 "} else if (b) {\n" 23719 " c;\n" 23720 "}", 23721 Style); 23722 23723 verifyFormat("if (a) {\n" 23724 " b;\n" 23725 "} else {\n" 23726 " { c; }\n" 23727 "}", 23728 Style); 23729 23730 verifyFormat("if (a) {\n" 23731 " if (b) // comment\n" 23732 " c;\n" 23733 "} else if (d) {\n" 23734 " e;\n" 23735 "}", 23736 "if (a) {\n" 23737 " if (b) { // comment\n" 23738 " c;\n" 23739 " }\n" 23740 "} else if (d) {\n" 23741 " e;\n" 23742 "}", 23743 Style); 23744 23745 verifyFormat("if (a) {\n" 23746 " if (b) {\n" 23747 " c;\n" 23748 " // comment\n" 23749 " } else if (d) {\n" 23750 " e;\n" 23751 " }\n" 23752 "}", 23753 Style); 23754 23755 verifyFormat("if (a) {\n" 23756 " if (b)\n" 23757 " c;\n" 23758 "}", 23759 "if (a) {\n" 23760 " if (b) {\n" 23761 " c;\n" 23762 " }\n" 23763 "}", 23764 Style); 23765 23766 verifyFormat("if (a)\n" 23767 " if (b)\n" 23768 " c;\n" 23769 " else\n" 23770 " d;\n" 23771 "else\n" 23772 " e;", 23773 "if (a) {\n" 23774 " if (b) {\n" 23775 " c;\n" 23776 " } else {\n" 23777 " d;\n" 23778 " }\n" 23779 "} else {\n" 23780 " e;\n" 23781 "}", 23782 Style); 23783 23784 verifyFormat("if (a) {\n" 23785 " // comment\n" 23786 " if (b)\n" 23787 " c;\n" 23788 " else if (d)\n" 23789 " e;\n" 23790 "} else {\n" 23791 " g;\n" 23792 "}", 23793 "if (a) {\n" 23794 " // comment\n" 23795 " if (b) {\n" 23796 " c;\n" 23797 " } else if (d) {\n" 23798 " e;\n" 23799 " }\n" 23800 "} else {\n" 23801 " g;\n" 23802 "}", 23803 Style); 23804 23805 verifyFormat("if (a)\n" 23806 " b;\n" 23807 "else if (c)\n" 23808 " d;\n" 23809 "else\n" 23810 " e;", 23811 "if (a) {\n" 23812 " b;\n" 23813 "} else {\n" 23814 " if (c) {\n" 23815 " d;\n" 23816 " } else {\n" 23817 " e;\n" 23818 " }\n" 23819 "}", 23820 Style); 23821 23822 verifyFormat("if (a) {\n" 23823 " if (b)\n" 23824 " c;\n" 23825 " else if (d)\n" 23826 " e;\n" 23827 "} else {\n" 23828 " g;\n" 23829 "}", 23830 "if (a) {\n" 23831 " if (b)\n" 23832 " c;\n" 23833 " else {\n" 23834 " if (d)\n" 23835 " e;\n" 23836 " }\n" 23837 "} else {\n" 23838 " g;\n" 23839 "}", 23840 Style); 23841 23842 verifyFormat("if (a)\n" 23843 " b;\n" 23844 "else if (c)\n" 23845 " while (d)\n" 23846 " e;\n" 23847 "// comment", 23848 "if (a)\n" 23849 "{\n" 23850 " b;\n" 23851 "} else if (c) {\n" 23852 " while (d) {\n" 23853 " e;\n" 23854 " }\n" 23855 "}\n" 23856 "// comment", 23857 Style); 23858 23859 verifyFormat("if (a) {\n" 23860 " b;\n" 23861 "} else if (c) {\n" 23862 " d;\n" 23863 "} else {\n" 23864 " e;\n" 23865 " g;\n" 23866 "}", 23867 Style); 23868 23869 verifyFormat("if (a) {\n" 23870 " b;\n" 23871 "} else if (c) {\n" 23872 " d;\n" 23873 "} else {\n" 23874 " e;\n" 23875 "} // comment", 23876 Style); 23877 23878 verifyFormat("int abs = [](int i) {\n" 23879 " if (i >= 0)\n" 23880 " return i;\n" 23881 " return -i;\n" 23882 "};", 23883 "int abs = [](int i) {\n" 23884 " if (i >= 0) {\n" 23885 " return i;\n" 23886 " }\n" 23887 " return -i;\n" 23888 "};", 23889 Style); 23890 23891 Style.ColumnLimit = 20; 23892 23893 verifyFormat("if (a) {\n" 23894 " b = c + // 1 -\n" 23895 " d;\n" 23896 "}", 23897 Style); 23898 23899 verifyFormat("if (a) {\n" 23900 " b = c >= 0 ? d\n" 23901 " : e;\n" 23902 "}", 23903 "if (a) {\n" 23904 " b = c >= 0 ? d : e;\n" 23905 "}", 23906 Style); 23907 23908 verifyFormat("if (a)\n" 23909 " b = c > 0 ? d : e;", 23910 "if (a) {\n" 23911 " b = c > 0 ? d : e;\n" 23912 "}", 23913 Style); 23914 23915 Style.ColumnLimit = 0; 23916 23917 verifyFormat("if (a)\n" 23918 " b234567890223456789032345678904234567890 = " 23919 "c234567890223456789032345678904234567890;", 23920 "if (a) {\n" 23921 " b234567890223456789032345678904234567890 = " 23922 "c234567890223456789032345678904234567890;\n" 23923 "}", 23924 Style); 23925 } 23926 23927 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { 23928 auto Style = getLLVMStyle(); 23929 23930 StringRef Short = "functionCall(paramA, paramB, paramC);\n" 23931 "void functionDecl(int a, int b, int c);"; 23932 23933 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, " 23934 "paramF, paramG, paramH, paramI);\n" 23935 "void functionDecl(int argumentA, int argumentB, int " 23936 "argumentC, int argumentD, int argumentE);"; 23937 23938 verifyFormat(Short, Style); 23939 23940 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, " 23941 "paramF, paramG, paramH,\n" 23942 " paramI);\n" 23943 "void functionDecl(int argumentA, int argumentB, int " 23944 "argumentC, int argumentD,\n" 23945 " int argumentE);"; 23946 23947 verifyFormat(NoBreak, Medium, Style); 23948 verifyFormat(NoBreak, 23949 "functionCall(\n" 23950 " paramA,\n" 23951 " paramB,\n" 23952 " paramC,\n" 23953 " paramD,\n" 23954 " paramE,\n" 23955 " paramF,\n" 23956 " paramG,\n" 23957 " paramH,\n" 23958 " paramI\n" 23959 ");\n" 23960 "void functionDecl(\n" 23961 " int argumentA,\n" 23962 " int argumentB,\n" 23963 " int argumentC,\n" 23964 " int argumentD,\n" 23965 " int argumentE\n" 23966 ");", 23967 Style); 23968 23969 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n" 23970 " nestedLongFunctionCall(argument1, " 23971 "argument2, argument3,\n" 23972 " argument4, " 23973 "argument5));", 23974 Style); 23975 23976 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 23977 23978 verifyFormat(Short, Style); 23979 verifyFormat( 23980 "functionCall(\n" 23981 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 23982 "paramI\n" 23983 ");\n" 23984 "void functionDecl(\n" 23985 " int argumentA, int argumentB, int argumentC, int argumentD, int " 23986 "argumentE\n" 23987 ");", 23988 Medium, Style); 23989 23990 Style.AllowAllArgumentsOnNextLine = false; 23991 Style.AllowAllParametersOfDeclarationOnNextLine = false; 23992 23993 verifyFormat(Short, Style); 23994 verifyFormat( 23995 "functionCall(\n" 23996 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 23997 "paramI\n" 23998 ");\n" 23999 "void functionDecl(\n" 24000 " int argumentA, int argumentB, int argumentC, int argumentD, int " 24001 "argumentE\n" 24002 ");", 24003 Medium, Style); 24004 24005 Style.BinPackArguments = false; 24006 Style.BinPackParameters = false; 24007 24008 verifyFormat(Short, Style); 24009 24010 verifyFormat("functionCall(\n" 24011 " paramA,\n" 24012 " paramB,\n" 24013 " paramC,\n" 24014 " paramD,\n" 24015 " paramE,\n" 24016 " paramF,\n" 24017 " paramG,\n" 24018 " paramH,\n" 24019 " paramI\n" 24020 ");\n" 24021 "void functionDecl(\n" 24022 " int argumentA,\n" 24023 " int argumentB,\n" 24024 " int argumentC,\n" 24025 " int argumentD,\n" 24026 " int argumentE\n" 24027 ");", 24028 Medium, Style); 24029 24030 verifyFormat("outerFunctionCall(\n" 24031 " nestedFunctionCall(argument1),\n" 24032 " nestedLongFunctionCall(\n" 24033 " argument1,\n" 24034 " argument2,\n" 24035 " argument3,\n" 24036 " argument4,\n" 24037 " argument5\n" 24038 " )\n" 24039 ");", 24040 Style); 24041 24042 verifyFormat("int a = (int)b;", Style); 24043 verifyFormat("int a = (int)b;", 24044 "int a = (\n" 24045 " int\n" 24046 ") b;", 24047 Style); 24048 24049 verifyFormat("return (true);", Style); 24050 verifyFormat("return (true);", 24051 "return (\n" 24052 " true\n" 24053 ");", 24054 Style); 24055 24056 verifyFormat("void foo();", Style); 24057 verifyFormat("void foo();", 24058 "void foo(\n" 24059 ");", 24060 Style); 24061 24062 verifyFormat("void foo() {}", Style); 24063 verifyFormat("void foo() {}", 24064 "void foo(\n" 24065 ") {\n" 24066 "}", 24067 Style); 24068 24069 verifyFormat("auto string = std::string();", Style); 24070 verifyFormat("auto string = std::string();", 24071 "auto string = std::string(\n" 24072 ");", 24073 Style); 24074 24075 verifyFormat("void (*functionPointer)() = nullptr;", Style); 24076 verifyFormat("void (*functionPointer)() = nullptr;", 24077 "void (\n" 24078 " *functionPointer\n" 24079 ")\n" 24080 "(\n" 24081 ") = nullptr;", 24082 Style); 24083 } 24084 24085 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) { 24086 auto Style = getLLVMStyle(); 24087 24088 verifyFormat("if (foo()) {\n" 24089 " return;\n" 24090 "}", 24091 Style); 24092 24093 verifyFormat("if (quitelongarg !=\n" 24094 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 24095 "comment\n" 24096 " return;\n" 24097 "}", 24098 Style); 24099 24100 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24101 24102 verifyFormat("if (foo()) {\n" 24103 " return;\n" 24104 "}", 24105 Style); 24106 24107 verifyFormat("if (quitelongarg !=\n" 24108 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 24109 "comment\n" 24110 " return;\n" 24111 "}", 24112 Style); 24113 } 24114 24115 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) { 24116 auto Style = getLLVMStyle(); 24117 24118 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 24119 " doSomething();\n" 24120 "}", 24121 Style); 24122 24123 verifyFormat("for (int myReallyLongCountVariable = 0; " 24124 "myReallyLongCountVariable < count;\n" 24125 " myReallyLongCountVariable++) {\n" 24126 " doSomething();\n" 24127 "}", 24128 Style); 24129 24130 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24131 24132 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 24133 " doSomething();\n" 24134 "}", 24135 Style); 24136 24137 verifyFormat("for (int myReallyLongCountVariable = 0; " 24138 "myReallyLongCountVariable < count;\n" 24139 " myReallyLongCountVariable++) {\n" 24140 " doSomething();\n" 24141 "}", 24142 Style); 24143 } 24144 24145 } // namespace 24146 } // namespace format 24147 } // namespace clang 24148